2 * Copyright (c) 2012 Cisco Systems, Inc.
5 * This software was developed by Bjoern Zeeb under contract to
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 * Regression test on SO_SETFIB setsockopt(2).
35 * Check that the expected domain(9) families all handle the socket option
36 * correctly and do proper bounds checks.
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.
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
65 { .domain = PF_INET6, .name = "PF_INET6" },
68 { .domain = PF_INET, .name = "PF_INET" },
70 { .domain = PF_ROUTE, .name = "PF_ROUTE" },
71 { .domain = PF_LOCAL, .name = "PF_LOCAL" },
74 static struct t_type {
78 { .type = SOCK_STREAM, .name = "SOCK_STREAM" },
79 { .type = SOCK_DGRAM, .name = "SOCK_DGRAM" },
80 { .type = SOCK_RAW, .name = "SOCK_RAW" },
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.
87 static int rt_numfibs = -42;
89 /* Number of test case. */
90 static int testno = 1;
94 * Try the setsockopt with given FIB number i on socket s.
95 * Handle result given on error and valid range and errno.
98 so_setfib(int s, int i, u_int dom, u_int type)
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);
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,
120 printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name,
121 t_type[type].name, i);
123 /* Test run done, next please. */
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.
132 t(u_int dom, u_int type)
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)
139 if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW)
142 /* Open socket for given combination. */
143 s = socket(t_dom[dom].domain, t_type[type].type, 0);
145 printf("not ok %d %s_%s # socket(): %s\n", testno,
146 t_dom[dom].name, t_type[type].name, strerror(errno));
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);
155 /* Test 3 random FIB numbers. */
156 for (i = 0; i < 3; i++)
157 so_setfib(s, (int)random(), dom, type);
164 * Returns 0 if no program error, 1 on sysctlbyname error.
165 * Test results are communicated by printf("[not ]ok <n> ..").
168 main(int argc __unused, char *argv[] __unused)
173 if (geteuid() != 0) {
174 printf("1..0 # SKIP: must be root\n");
178 /* Initialize randomness. */
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, ..)");
187 (nitems(t_dom) - 1) * nitems(t_type) * (2 + rt_numfibs + 2 + 3));
189 /* Adjust from number to index. */
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++)