1231855Sbz/*- 2231855Sbz * Copyright (c) 2012 Cisco Systems, Inc. 3231855Sbz * All rights reserved. 4231855Sbz * 5231855Sbz * This software was developed by Bjoern Zeeb under contract to 6231855Sbz * Cisco Systems, Inc.. 7231855Sbz * 8231855Sbz * Redistribution and use in source and binary forms, with or without 9231855Sbz * modification, are permitted provided that the following conditions 10231855Sbz * are met: 11231855Sbz * 1. Redistributions of source code must retain the above copyright 12231855Sbz * notice, this list of conditions and the following disclaimer. 13231855Sbz * 2. Redistributions in binary form must reproduce the above copyright 14231855Sbz * notice, this list of conditions and the following disclaimer in the 15231855Sbz * documentation and/or other materials provided with the distribution. 16231855Sbz * 17231855Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18231855Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19231855Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20231855Sbz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21231855Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22231855Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23231855Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24231855Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25231855Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26231855Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27231855Sbz * SUCH DAMAGE. 28231855Sbz * 29231855Sbz * $FreeBSD$ 30231855Sbz */ 31231855Sbz 32231855Sbz/* 33231855Sbz * Regression test on SO_SETFIB setsockopt(2). 34231855Sbz * 35231855Sbz * Check that the expected domain(9) families all handle the socket option 36231855Sbz * correctly and do proper bounds checks. 37231855Sbz * 38231855Sbz * Test plan: 39231855Sbz * 1. Get system wide number of FIBs from sysctl and convert to index (-= 1). 40231855Sbz * 2. For each protocol family (INET, INET6, ROUTE and LOCAL) open socketes of 41231855Sbz * type (STREAM, DGRAM and RAW) as supported. 42231855Sbz * 3. Do a sequence of -2, -1, 0, .. n, n+1, n+2 SO_SETFIB sockopt calls, 43231855Sbz * expecting the first two and last two to fail (valid 0 ... n). 44231855Sbz * 4. Try 3 random numbers. Calculate result based on valid range. 45231855Sbz * 5. Repeat for next domain family and type from (2) on. 46231855Sbz */ 47231855Sbz 48281974Sngie#include <sys/param.h> 49231855Sbz#include <sys/types.h> 50231855Sbz#include <sys/socket.h> 51231855Sbz#include <sys/sysctl.h> 52231855Sbz 53231855Sbz#include <err.h> 54231855Sbz#include <errno.h> 55231855Sbz#include <stdio.h> 56231855Sbz#include <stdlib.h> 57231855Sbz#include <string.h> 58231855Sbz#include <unistd.h> 59231855Sbz 60231855Sbzstatic struct t_dom { 61231855Sbz int domain; 62231855Sbz const char *name; 63231855Sbz} t_dom[] = { 64231855Sbz#ifdef INET6 65231855Sbz { .domain = PF_INET6, .name = "PF_INET6" }, 66231855Sbz#endif 67231855Sbz#ifdef INET 68231855Sbz { .domain = PF_INET, .name = "PF_INET" }, 69231855Sbz#endif 70231855Sbz { .domain = PF_ROUTE, .name = "PF_ROUTE" }, 71231855Sbz { .domain = PF_LOCAL, .name = "PF_LOCAL" }, 72231855Sbz}; 73231855Sbz 74231855Sbzstatic struct t_type { 75231855Sbz int type; 76231855Sbz const char *name; 77231855Sbz} t_type[] = { 78231855Sbz { .type = SOCK_STREAM, .name = "SOCK_STREAM" }, 79231855Sbz { .type = SOCK_DGRAM, .name = "SOCK_DGRAM" }, 80231855Sbz { .type = SOCK_RAW, .name = "SOCK_RAW" }, 81231855Sbz}; 82231855Sbz 83231855Sbz/* 84231855Sbz * Number of FIBs as read from net.fibs sysctl - 1. Initialize to clear out of 85231855Sbz * bounds value to not accidentally run on a limited range. 86231855Sbz */ 87231855Sbzstatic int rt_numfibs = -42; 88231855Sbz 89231855Sbz/* Number of test case. */ 90231855Sbzstatic int testno = 1; 91231855Sbz 92231855Sbz 93231855Sbz/* 94231855Sbz * Try the setsockopt with given FIB number i on socket s. 95231855Sbz * Handle result given on error and valid range and errno. 96231855Sbz */ 97231855Sbzstatic void 98231855Sbzso_setfib(int s, int i, u_int dom, u_int type) 99231855Sbz{ 100231855Sbz int error; 101231855Sbz 102231855Sbz error = setsockopt(s, SOL_SOCKET, SO_SETFIB, &i, sizeof(i)); 103231855Sbz /* For out of bounds we expect an error. */ 104231855Sbz if (error == -1 && (i < 0 || i > rt_numfibs)) 105231855Sbz printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name, 106231855Sbz t_type[type].name, i); 107231855Sbz else if (error != -1 && (i < 0 || i > rt_numfibs)) 108231855Sbz printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, " 109231855Sbz "SO_SETFIB, %d, ..) unexpectedly succeeded\n", testno, 110231855Sbz t_dom[dom].name, t_type[type].name, i, s, i); 111231855Sbz else if (error == 0) 112231855Sbz printf("ok %d %s_%s_%d\n", testno, t_dom[dom].name, 113231855Sbz t_type[type].name, i); 114231855Sbz else if (errno != EINVAL) 115231855Sbz printf("not ok %d %s_%s_%d # setsockopt(%d, SOL_SOCKET, " 116231855Sbz "SO_SETFIB, %d, ..) unexpected error: %s\n", testno, 117231855Sbz t_dom[dom].name, t_type[type].name, i, s, i, 118231855Sbz strerror(errno)); 119231855Sbz else 120231855Sbz printf("not ok %d %s_%s_%d\n", testno, t_dom[dom].name, 121231855Sbz t_type[type].name, i); 122231855Sbz 123231855Sbz /* Test run done, next please. */ 124231855Sbz testno++; 125231855Sbz} 126231855Sbz 127231855Sbz/* 128231855Sbz * Main test. Open socket given domain family and type. For each FIB, out of 129231855Sbz * bounds FIB numbers and 3 random FIB numbers set the socket option. 130231855Sbz */ 131231855Sbzstatic void 132231855Sbzt(u_int dom, u_int type) 133231855Sbz{ 134231855Sbz int i, s; 135231855Sbz 136231855Sbz /* PF_ROUTE only supports RAW socket types, while PF_LOCAL does not. */ 137231855Sbz if (t_dom[dom].domain == PF_ROUTE && t_type[type].type != SOCK_RAW) 138231855Sbz return; 139231855Sbz if (t_dom[dom].domain == PF_LOCAL && t_type[type].type == SOCK_RAW) 140231855Sbz return; 141231855Sbz 142231855Sbz /* Open socket for given combination. */ 143231855Sbz s = socket(t_dom[dom].domain, t_type[type].type, 0); 144231855Sbz if (s == -1) { 145231855Sbz printf("not ok %d %s_%s # socket(): %s\n", testno, 146231855Sbz t_dom[dom].name, t_type[type].name, strerror(errno)); 147281974Sngie testno++; 148231855Sbz return; 149231855Sbz } 150231855Sbz 151231855Sbz /* Test FIBs -2, -1, 0, .. n, n + 1, n + 2. */ 152231855Sbz for (i = -2; i <= (rt_numfibs + 2); i++) 153231855Sbz so_setfib(s, i, dom, type); 154231855Sbz 155231855Sbz /* Test 3 random FIB numbers. */ 156231855Sbz for (i = 0; i < 3; i++) 157231855Sbz so_setfib(s, (int)random(), dom, type); 158231855Sbz 159231855Sbz /* Close socket. */ 160231855Sbz close(s); 161231855Sbz} 162231855Sbz 163231855Sbz/* 164231855Sbz * Returns 0 if no program error, 1 on sysctlbyname error. 165231855Sbz * Test results are communicated by printf("[not ]ok <n> .."). 166231855Sbz */ 167231855Sbzint 168231855Sbzmain(int argc __unused, char *argv[] __unused) 169231855Sbz{ 170231855Sbz u_int i, j; 171231855Sbz size_t s; 172231855Sbz 173281974Sngie if (geteuid() != 0) { 174294302Sngie printf("1..0 # SKIP: must be root\n"); 175281974Sngie return (0); 176281974Sngie } 177281974Sngie 178299826Spfg /* Initialize randomness. */ 179231855Sbz srandomdev(); 180231855Sbz 181231855Sbz /* Get number of FIBs supported by kernel. */ 182231855Sbz s = sizeof(rt_numfibs); 183231855Sbz if (sysctlbyname("net.fibs", &rt_numfibs, &s, NULL, 0) == -1) 184231855Sbz err(1, "sysctlbyname(net.fibs, ..)"); 185281974Sngie 186281974Sngie printf("1..%lu\n", 187281974Sngie (nitems(t_dom) - 1) * nitems(t_type) * (2 + rt_numfibs + 2 + 3)); 188281974Sngie 189231855Sbz /* Adjust from number to index. */ 190231855Sbz rt_numfibs -= 1; 191231855Sbz 192231855Sbz /* Run tests. */ 193231855Sbz for (i = 0; i < sizeof(t_dom) / sizeof(struct t_dom); i++) 194231855Sbz for (j = 0; j < sizeof(t_type) / sizeof(struct t_type); j++) 195231855Sbz t(i, j); 196231855Sbz 197231855Sbz return (0); 198231855Sbz} 199231855Sbz 200231855Sbz/* end */ 201