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/types.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
64 { .domain = PF_INET6, .name = "PF_INET6" },
67 { .domain = PF_INET, .name = "PF_INET" },
69 { .domain = PF_ROUTE, .name = "PF_ROUTE" },
70 { .domain = PF_LOCAL, .name = "PF_LOCAL" },
73 static struct t_type {
77 { .type = SOCK_STREAM, .name = "SOCK_STREAM" },
78 { .type = SOCK_DGRAM, .name = "SOCK_DGRAM" },
79 { .type = SOCK_RAW, .name = "SOCK_RAW" },
83 * Number of FIBs as read from net.fibs sysctl - 1. Initialize to clear out of
84 * bounds value to not accidentally run on a limited range.
86 static int rt_numfibs = -42;
88 /* Number of test case. */
89 static int testno = 1;
93 * Try the setsockopt with given FIB number i on socket s.
94 * Handle result given on error and valid range and errno.
97 so_setfib(int s, int i, u_int dom, u_int type)
101 error = setsockopt(s, SOL_SOCKET, SO_SETFIB, &i, sizeof(i));
102 /* For out of bounds we expect an error. */
103 if (error == -1 && (i < 0 || i > rt_numfibs))
104 printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name,
105 t_type[type].name, i);
106 else if (error != -1 && (i < 0 || i > rt_numfibs))
107 printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, "
108 "SO_SETFIB, %d, ..) unexpectedly succeeded\n", testno,
109 t_dom[dom].name, t_type[type].name, i, s, i);
111 printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name,
112 t_type[type].name, i);
113 else if (errno != EINVAL)
114 printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, "
115 "SO_SETFIB, %d, ..) unexpected error: %s\n", testno,
116 t_dom[dom].name, t_type[type].name, i, s, i,
119 printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name,
120 t_type[type].name, i);
122 /* Test run done, next please. */
127 * Main test. Open socket given domain family and type. For each FIB, out of
128 * bounds FIB numbers and 3 random FIB numbers set the socket option.
131 t(u_int dom, u_int type)
135 /* PF_ROUTE only supports RAW socket types, while PF_LOCAL does not. */
136 if (t_dom[dom].domain == PF_ROUTE && t_type[type].type != SOCK_RAW)
138 if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW)
141 /* Open socket for given combination. */
142 s = socket(t_dom[dom].domain, t_type[type].type, 0);
144 printf("not ok %d %s_%s # socket(): %s\n", testno,
145 t_dom[dom].name, t_type[type].name, strerror(errno));
149 /* Test FIBs -2, -1, 0, .. n, n + 1, n + 2. */
150 for (i = -2; i <= (rt_numfibs + 2); i++)
151 so_setfib(s, i, dom, type);
153 /* Test 3 random FIB numbers. */
154 for (i = 0; i < 3; i++)
155 so_setfib(s, (int)random(), dom, type);
162 * Returns 0 if no program error, 1 on sysctlbyname error.
163 * Test results are communicated by printf("[not ]ok <n> ..").
166 main(int argc __unused, char *argv[] __unused)
171 /* Initalize randomness. */
174 /* Get number of FIBs supported by kernel. */
175 s = sizeof(rt_numfibs);
176 if (sysctlbyname("net.fibs", &rt_numfibs, &s, NULL, 0) == -1)
177 err(1, "sysctlbyname(net.fibs, ..)");
178 /* Adjust from number to index. */
182 for (i = 0; i < sizeof(t_dom) / sizeof(struct t_dom); i++)
183 for (j = 0; j < sizeof(t_type) / sizeof(struct t_type); j++)