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