1 /*- 2 * Copyright (c) 2012 Cisco Systems, Inc. 3 * All rights reserved. 4 * 5 * This software was developed by Bjoern Zeeb under contract to 6 * Cisco Systems, Inc.. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * Regression test on SO_SETFIB setsockopt(2). 34 * 35 * Check that the expected domain(9) families all handle the socket option 36 * correctly and do proper bounds checks. 37 * 38 * Test plan: 39 * 1. Get system wide number of FIBs from sysctl and convert to index (-= 1). 40 * 2. For each protocol family (INET, INET6, ROUTE and LOCAL) open socketes of 41 * type (STREAM, DGRAM and RAW) as supported. 42 * 3. Do a sequence of -2, -1, 0, .. n, n+1, n+2 SO_SETFIB sockopt calls, 43 * expecting the first two and last two to fail (valid 0 ... n). 44 * 4. Try 3 random numbers. Calculate result based on valid range. 45 * 5. Repeat for next domain family and type from (2) on. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <sys/sysctl.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 static struct t_dom { 61 int domain; 62 const char *name; 63 } t_dom[] = { 64 #ifdef INET6 65 { .domain = PF_INET6, .name = "PF_INET6" }, 66 #endif 67 #ifdef INET 68 { .domain = PF_INET, .name = "PF_INET" }, 69 #endif 70 { .domain = PF_ROUTE, .name = "PF_ROUTE" }, 71 { .domain = PF_LOCAL, .name = "PF_LOCAL" }, 72 }; 73 74 static struct t_type { 75 int type; 76 const char *name; 77 } t_type[] = { 78 { .type = SOCK_STREAM, .name = "SOCK_STREAM" }, 79 { .type = SOCK_DGRAM, .name = "SOCK_DGRAM" }, 80 { .type = SOCK_RAW, .name = "SOCK_RAW" }, 81 }; 82 83 /* 84 * Number of FIBs as read from net.fibs sysctl - 1. Initialize to clear out of 85 * bounds value to not accidentally run on a limited range. 86 */ 87 static int rt_numfibs = -42; 88 89 /* Number of test case. */ 90 static int testno = 1; 91 92 93 /* 94 * Try the setsockopt with given FIB number i on socket s. 95 * Handle result given on error and valid range and errno. 96 */ 97 static void 98 so_setfib(int s, int i, u_int dom, u_int type) 99 { 100 int error; 101 102 error = setsockopt(s, SOL_SOCKET, SO_SETFIB, &i, sizeof(i)); 103 /* For out of bounds we expect an error. */ 104 if (error == -1 && (i < 0 || i > rt_numfibs)) 105 printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name, 106 t_type[type].name, i); 107 else if (error != -1 && (i < 0 || i > rt_numfibs)) 108 printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, " 109 "SO_SETFIB, %d, ..) unexpectedly succeeded\n", testno, 110 t_dom[dom].name, t_type[type].name, i, s, i); 111 else if (error == 0) 112 printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name, 113 t_type[type].name, i); 114 else if (errno != EINVAL) 115 printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, " 116 "SO_SETFIB, %d, ..) unexpected error: %s\n", testno, 117 t_dom[dom].name, t_type[type].name, i, s, i, 118 strerror(errno)); 119 else 120 printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name, 121 t_type[type].name, i); 122 123 /* Test run done, next please. */ 124 testno++; 125 } 126 127 /* 128 * Main test. Open socket given domain family and type. For each FIB, out of 129 * bounds FIB numbers and 3 random FIB numbers set the socket option. 130 */ 131 static void 132 t(u_int dom, u_int type) 133 { 134 int i, s; 135 136 /* PF_ROUTE only supports RAW socket types, while PF_LOCAL does not. */ 137 if (t_dom[dom].domain == PF_ROUTE && t_type[type].type != SOCK_RAW) 138 return; 139 if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW) 140 return; 141 142 /* Open socket for given combination. */ 143 s = socket(t_dom[dom].domain, t_type[type].type, 0); 144 if (s == -1) { 145 printf("not ok %d %s_%s # socket(): %s\n", testno, 146 t_dom[dom].name, t_type[type].name, strerror(errno)); 147 testno++; 148 return; 149 } 150 151 /* Test FIBs -2, -1, 0, .. n, n + 1, n + 2. */ 152 for (i = -2; i <= (rt_numfibs + 2); i++) 153 so_setfib(s, i, dom, type); 154 155 /* Test 3 random FIB numbers. */ 156 for (i = 0; i < 3; i++) 157 so_setfib(s, (int)random(), dom, type); 158 159 /* Close socket. */ 160 close(s); 161 } 162 163 /* 164 * Returns 0 if no program error, 1 on sysctlbyname error. 165 * Test results are communicated by printf("[not ]ok <n> .."). 166 */ 167 int 168 main(int argc __unused, char *argv[] __unused) 169 { 170 u_int i, j; 171 size_t s; 172 173 if (geteuid() != 0) { 174 printf("1..0 # SKIP: must be root"); 175 return (0); 176 } 177 178 /* Initalize randomness. */ 179 srandomdev(); 180 181 /* Get number of FIBs supported by kernel. */ 182 s = sizeof(rt_numfibs); 183 if (sysctlbyname("net.fibs", &rt_numfibs, &s, NULL, 0) == -1) 184 err(1, "sysctlbyname(net.fibs, ..)"); 185 186 printf("1..%lu\n", 187 (nitems(t_dom) - 1) * nitems(t_type) * (2 + rt_numfibs + 2 + 3)); 188 189 /* Adjust from number to index. */ 190 rt_numfibs -= 1; 191 192 /* Run tests. */ 193 for (i = 0; i < sizeof(t_dom) / sizeof(struct t_dom); i++) 194 for (j = 0; j < sizeof(t_type) / sizeof(struct t_type); j++) 195 t(i, j); 196 197 return (0); 198 } 199 200 /* end */ 201