so_setfib.c revision 231855
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: head/tools/regression/sockets/so_setfib/so_setfib.c 231855 2012-02-17 03:25:26Z bz $
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/types.h>
49#include <sys/socket.h>
50#include <sys/sysctl.h>
51
52#include <err.h>
53#include <errno.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59static struct t_dom {
60	int		domain;
61	const char	*name;
62} t_dom[] = {
63#ifdef INET6
64	{ .domain = PF_INET6, .name = "PF_INET6" },
65#endif
66#ifdef INET
67	{ .domain = PF_INET, .name = "PF_INET" },
68#endif
69	{ .domain = PF_ROUTE, .name = "PF_ROUTE" },
70	{ .domain = PF_LOCAL, .name = "PF_LOCAL" },
71};
72
73static struct t_type {
74	int		type;
75	const char	*name;
76} t_type[] = {
77	{ .type = SOCK_STREAM, .name = "SOCK_STREAM" },
78	{ .type = SOCK_DGRAM, .name = "SOCK_DGRAM" },
79	{ .type = SOCK_RAW, .name = "SOCK_RAW" },
80};
81
82/*
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.
85 */
86static int rt_numfibs = -42;
87
88/* Number of test case. */
89static int testno = 1;
90
91
92/*
93 * Try the setsockopt with given FIB number i on socket s.
94 * Handle result given on error and valid range and errno.
95 */
96static void
97so_setfib(int s, int i, u_int dom, u_int type)
98{
99	int error;
100
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);
110	else if (error == 0)
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,
117		    strerror(errno));
118	else
119		printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name,
120		    t_type[type].name, i);
121
122	/* Test run done, next please. */
123	testno++;
124}
125
126/*
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.
129 */
130static void
131t(u_int dom, u_int type)
132{
133	int i, s;
134
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)
137		return;
138	if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW)
139		return;
140
141	/* Open socket for given combination. */
142	s = socket(t_dom[dom].domain, t_type[type].type, 0);
143	if (s == -1) {
144		printf("not ok %d %s_%s # socket(): %s\n", testno,
145		    t_dom[dom].name, t_type[type].name, strerror(errno));
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 */
165int
166main(int argc __unused, char *argv[] __unused)
167{
168	u_int i, j;
169	size_t s;
170
171	/* Initalize randomness. */
172	srandomdev();
173
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. */
179	rt_numfibs -= 1;
180
181	/* Run tests. */
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++)
184			t(i, j);
185
186	return (0);
187}
188
189/* end */
190