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 
30 /*
31  * Regression test on SO_SETFIB setsockopt(2).
32  *
33  * Check that the expected domain(9) families all handle the socket option
34  * correctly and do proper bounds checks.
35  *
36  * Test plan:
37  * 1. Get system wide number of FIBs from sysctl and convert to index (-= 1).
38  * 2. For each protocol family (INET, INET6, ROUTE and LOCAL) open socketes of
39  *    type (STREAM, DGRAM and RAW) as supported.
40  * 3. Do a sequence of -2, -1, 0, .. n, n+1, n+2 SO_SETFIB sockopt calls,
41  *    expecting the first two and last two to fail (valid 0 ... n).
42  * 4. Try 3 random numbers.  Calculate result based on valid range.
43  * 5. Repeat for next domain family and type from (2) on.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 static struct t_dom {
59 	int		domain;
60 	const char	*name;
61 } t_dom[] = {
62 #ifdef INET6
63 	{ .domain = PF_INET6, .name = "PF_INET6" },
64 #endif
65 #ifdef INET
66 	{ .domain = PF_INET, .name = "PF_INET" },
67 #endif
68 	{ .domain = PF_ROUTE, .name = "PF_ROUTE" },
69 	{ .domain = PF_LOCAL, .name = "PF_LOCAL" },
70 };
71 
72 static struct t_type {
73 	int		type;
74 	const char	*name;
75 } t_type[] = {
76 	{ .type = SOCK_STREAM, .name = "SOCK_STREAM" },
77 	{ .type = SOCK_DGRAM, .name = "SOCK_DGRAM" },
78 	{ .type = SOCK_RAW, .name = "SOCK_RAW" },
79 };
80 
81 /*
82  * Number of FIBs as read from net.fibs sysctl - 1.  Initialize to clear out of
83  * bounds value to not accidentally run on a limited range.
84  */
85 static int rt_numfibs = -42;
86 
87 /* Number of test case. */
88 static int testno = 1;
89 
90 
91 /*
92  * Try the setsockopt with given FIB number i on socket s.
93  * Handle result given on error and valid range and errno.
94  */
95 static void
96 so_setfib(int s, int i, u_int dom, u_int type)
97 {
98 	int error;
99 
100 	error = setsockopt(s, SOL_SOCKET, SO_SETFIB, &i, sizeof(i));
101 	/* For out of bounds we expect an error. */
102 	if (error == -1 && (i < 0 || i > rt_numfibs))
103 		printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name,
104 		    t_type[type].name, i);
105 	else if (error != -1 && (i < 0 || i > rt_numfibs))
106 		printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, "
107 		    "SO_SETFIB, %d, ..) unexpectedly succeeded\n", testno,
108 		    t_dom[dom].name, t_type[type].name, i, s, i);
109 	else if (error == 0)
110 		printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name,
111 		    t_type[type].name, i);
112 	else if (errno != EINVAL)
113 		printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, "
114 		    "SO_SETFIB, %d, ..) unexpected error: %s\n", testno,
115 		    t_dom[dom].name, t_type[type].name, i, s, i,
116 		    strerror(errno));
117 	else
118 		printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name,
119 		    t_type[type].name, i);
120 
121 	/* Test run done, next please. */
122 	testno++;
123 }
124 
125 /*
126  * Main test.  Open socket given domain family and type.  For each FIB, out of
127  * bounds FIB numbers and 3 random FIB numbers set the socket option.
128  */
129 static void
130 t(u_int dom, u_int type)
131 {
132 	int i, s;
133 
134 	/* PF_ROUTE only supports RAW socket types, while PF_LOCAL does not. */
135 	if (t_dom[dom].domain == PF_ROUTE && t_type[type].type != SOCK_RAW)
136 		return;
137 	if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW)
138 		return;
139 
140 	/* Open socket for given combination. */
141 	s = socket(t_dom[dom].domain, t_type[type].type, 0);
142 	if (s == -1) {
143 		printf("not ok %d %s_%s # socket(): %s\n", testno,
144 		    t_dom[dom].name, t_type[type].name, strerror(errno));
145 		testno++;
146 		return;
147 	}
148 
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);
152 
153 	/* Test 3 random FIB numbers. */
154 	for (i = 0; i < 3; i++)
155 		so_setfib(s, (int)random(), dom, type);
156 
157 	/* Close socket. */
158 	close(s);
159 }
160 
161 /*
162  * Returns 0 if no program error, 1 on sysctlbyname error.
163  * Test results are communicated by printf("[not ]ok <n> ..").
164  */
165 int
166 main(int argc __unused, char *argv[] __unused)
167 {
168 	u_int i, j;
169 	size_t s;
170 
171 	if (geteuid() != 0) {
172 		printf("1..0 # SKIP: must be root\n");
173 		return (0);
174 	}
175 
176 	/* Initialize randomness. */
177 	srandomdev();
178 
179 	/* Get number of FIBs supported by kernel. */
180 	s = sizeof(rt_numfibs);
181 	if (sysctlbyname("net.fibs", &rt_numfibs, &s, NULL, 0) == -1)
182 		err(1, "sysctlbyname(net.fibs, ..)");
183 
184 	printf("1..%lu\n",
185 	    (nitems(t_dom) - 1) * nitems(t_type) * (2 + rt_numfibs + 2 + 3));
186 
187 	/* Adjust from number to index. */
188 	rt_numfibs -= 1;
189 
190 	/* Run tests. */
191 	for (i = 0; i < sizeof(t_dom) / sizeof(struct t_dom); i++)
192 		for (j = 0; j < sizeof(t_type) / sizeof(struct t_type); j++)
193 			t(i, j);
194 
195 	return (0);
196 }
197 
198 /* end */
199