1 /* 2 * Check decoding of getsockopt and setsockopt for SOL_NETLINK level. 3 * 4 * Copyright (c) 2017 Dmitry V. Levin <ldv (at) altlinux.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "tests.h" 31 #include "netlink.h" 32 #include <stdio.h> 33 34 #ifndef SOL_NETLINK 35 # define SOL_NETLINK 270 36 #endif 37 38 static int rc; 39 static const char *errstr; 40 41 static int 42 get_sockopt(int fd, int name, void *val, socklen_t *len) 43 { 44 rc = getsockopt(fd, SOL_NETLINK, name, val, len); 45 errstr = sprintrc(rc); 46 return rc; 47 } 48 49 static int 50 set_sockopt(int fd, int name, void *val, socklen_t len) 51 { 52 rc = setsockopt(fd, SOL_NETLINK, name, val, len); 53 errstr = sprintrc(rc); 54 return rc; 55 } 56 57 int 58 main(void) 59 { 60 static const struct { 61 int val; 62 const char *str; 63 } names[] = { 64 #ifdef NETLINK_ADD_MEMBERSHIP 65 { ARG_STR(NETLINK_ADD_MEMBERSHIP) }, 66 #endif 67 #ifdef NETLINK_DROP_MEMBERSHIP 68 { ARG_STR(NETLINK_DROP_MEMBERSHIP) }, 69 #endif 70 #ifdef NETLINK_PKTINFO 71 { ARG_STR(NETLINK_PKTINFO) }, 72 #endif 73 #ifdef NETLINK_BROADCAST_ERROR 74 { ARG_STR(NETLINK_BROADCAST_ERROR) }, 75 #endif 76 #ifdef NETLINK_NO_ENOBUFS 77 { ARG_STR(NETLINK_NO_ENOBUFS) }, 78 #endif 79 #ifdef NETLINK_RX_RING 80 { ARG_STR(NETLINK_RX_RING) }, 81 #endif 82 #ifdef NETLINK_TX_RING 83 { ARG_STR(NETLINK_TX_RING) }, 84 #endif 85 #ifdef NETLINK_LISTEN_ALL_NSID 86 { ARG_STR(NETLINK_LISTEN_ALL_NSID) }, 87 #endif 88 #ifdef NETLINK_LIST_MEMBERSHIPS 89 { ARG_STR(NETLINK_LIST_MEMBERSHIPS) }, 90 #endif 91 #ifdef NETLINK_CAP_ACK 92 { ARG_STR(NETLINK_CAP_ACK) }, 93 #endif 94 #ifdef NETLINK_EXT_ACK 95 { ARG_STR(NETLINK_EXT_ACK) }, 96 #endif 97 }; 98 99 TAIL_ALLOC_OBJECT_CONST_PTR(int, val); 100 TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len); 101 void *const efault = val + 1; 102 int fd = socket(AF_NETLINK, SOCK_RAW, 0); 103 if (fd < 0) 104 perror_msg_and_skip("socket AF_NETLINK SOCK_RAW"); 105 unsigned int i; 106 107 for (i = 0; i < ARRAY_SIZE(names); ++i) { 108 /* getsockopt */ 109 110 /* classic */ 111 *len = sizeof(*val); 112 get_sockopt(fd, names[i].val, val, len); 113 printf("getsockopt(%d, SOL_NETLINK, %s, ", fd, names[i].str); 114 if (rc) 115 printf("%p", val); 116 else 117 printf("[%d]", *val); 118 printf(", [%d]) = %s\n", *len, errstr); 119 120 /* optlen larger than necessary - shortened */ 121 *len = sizeof(*val) + 1; 122 get_sockopt(fd, names[i].val, val, len); 123 printf("getsockopt(%d, SOL_NETLINK, %s, ", fd, names[i].str); 124 if (rc) 125 printf("%p", val); 126 else 127 printf("[%d]", *val); 128 printf(", [%d", (int) sizeof(*val) + 1); 129 if ((int) sizeof(*val) + 1 != *len) 130 printf("->%d", *len); 131 printf("]) = %s\n", errstr); 132 133 /* zero optlen - print returned optlen */ 134 *len = 0; 135 get_sockopt(fd, names[i].val, NULL, len); 136 printf("getsockopt(%d, SOL_NETLINK, %s, NULL, [0", 137 fd, names[i].str); 138 if (*len) 139 printf("->%d", *len); 140 printf("]) = %s\n", errstr); 141 142 #ifdef NETLINK_LIST_MEMBERSHIPS 143 if (names[i].val != NETLINK_LIST_MEMBERSHIPS) { 144 #endif 145 /* optlen shorter than necessary - print address */ 146 *len = sizeof(*val) - 1; 147 get_sockopt(fd, names[i].val, val, len); 148 printf("getsockopt(%d, SOL_NETLINK, %s, %p, [%d", 149 fd, names[i].str, val, (int) sizeof(*val) - 1); 150 if ((int) sizeof(*val) - 1 != *len) 151 printf("->%d", *len); 152 printf("]) = %s\n", errstr); 153 #ifdef NETLINK_LIST_MEMBERSHIPS 154 } else { 155 /* optlen shorter than required for the first element */ 156 *len = sizeof(*val) - 1; 157 get_sockopt(fd, names[i].val, efault, len); 158 printf("getsockopt(%d, SOL_NETLINK, %s, ", 159 fd, names[i].str); 160 if (rc) 161 printf("%p", efault); 162 else 163 printf("[]"); 164 printf(", [%d", (int) sizeof(*val) - 1); 165 if ((int) sizeof(*val) - 1 != *len) 166 printf("->%d", *len); 167 printf("]) = %s\n", errstr); 168 } 169 #endif 170 171 /* optval EFAULT - print address */ 172 *len = sizeof(*val); 173 get_sockopt(fd, names[i].val, efault, len); 174 printf("getsockopt(%d, SOL_NETLINK, %s, %p, [%d]) = %s\n", 175 fd, names[i].str, efault, *len, errstr); 176 177 /* optlen EFAULT - print address */ 178 get_sockopt(fd, names[i].val, val, len + 1); 179 printf("getsockopt(%d, SOL_NETLINK, %s, %p, %p) = %s\n", 180 fd, names[i].str, val, len + 1, errstr); 181 182 /* setsockopt */ 183 184 /* classic */ 185 *val = 0xdefaced; 186 set_sockopt(fd, names[i].val, val, sizeof(*val)); 187 printf("setsockopt(%d, SOL_NETLINK, %s, [%d], %d) = %s\n", 188 fd, names[i].str, *val, (int) sizeof(*val), errstr); 189 190 /* optlen larger than necessary - shortened */ 191 set_sockopt(fd, names[i].val, val, sizeof(*val) + 1); 192 printf("setsockopt(%d, SOL_NETLINK, %s, [%d], %d) = %s\n", 193 fd, names[i].str, *val, (int) sizeof(*val) + 1, errstr); 194 195 /* optlen < 0 - print address */ 196 set_sockopt(fd, names[i].val, val, -1U); 197 printf("setsockopt(%d, SOL_NETLINK, %s, %p, -1) = %s\n", 198 fd, names[i].str, val, errstr); 199 200 /* optlen smaller than necessary - print address */ 201 set_sockopt(fd, names[i].val, val, sizeof(*val) - 1); 202 printf("setsockopt(%d, SOL_NETLINK, %s, %p, %d) = %s\n", 203 fd, names[i].str, val, (int) sizeof(*val) - 1, errstr); 204 205 /* optval EFAULT - print address */ 206 set_sockopt(fd, names[i].val, efault, sizeof(*val)); 207 printf("setsockopt(%d, SOL_NETLINK, %s, %p, %d) = %s\n", 208 fd, names[i].str, efault, (int) sizeof(*val), errstr); 209 } 210 211 puts("+++ exited with 0 +++"); 212 return 0; 213 } 214