xref: /freebsd-src/sbin/ifconfig/iffib.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
135fd7bc0SBjoern A. Zeeb /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
435fd7bc0SBjoern A. Zeeb  * Copyright (c) 2011 Alexander V. Chernikov
535fd7bc0SBjoern A. Zeeb  * Copyright (c) 2011 Christian S.J. Peron
635fd7bc0SBjoern A. Zeeb  * Copyright (c) 2011 Bjoern A. Zeeb
735fd7bc0SBjoern A. Zeeb  * All rights reserved.
835fd7bc0SBjoern A. Zeeb  *
935fd7bc0SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
1035fd7bc0SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
1135fd7bc0SBjoern A. Zeeb  * are met:
1235fd7bc0SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
1335fd7bc0SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
1435fd7bc0SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
1535fd7bc0SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
1635fd7bc0SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
1735fd7bc0SBjoern A. Zeeb  *
1835fd7bc0SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1935fd7bc0SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2035fd7bc0SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2135fd7bc0SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2235fd7bc0SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2335fd7bc0SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2435fd7bc0SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2535fd7bc0SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2635fd7bc0SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2735fd7bc0SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2835fd7bc0SBjoern A. Zeeb  * SUCH DAMAGE.
2935fd7bc0SBjoern A. Zeeb  */
3035fd7bc0SBjoern A. Zeeb 
3135fd7bc0SBjoern A. Zeeb #include <sys/param.h>
3235fd7bc0SBjoern A. Zeeb #include <sys/ioctl.h>
3335fd7bc0SBjoern A. Zeeb #include <sys/socket.h>
3435fd7bc0SBjoern A. Zeeb #include <sys/sockio.h>
3535fd7bc0SBjoern A. Zeeb 
3635fd7bc0SBjoern A. Zeeb #include <net/if.h>
3735fd7bc0SBjoern A. Zeeb #include <net/route.h>
3835fd7bc0SBjoern A. Zeeb 
3935fd7bc0SBjoern A. Zeeb #include <stdio.h>
4035fd7bc0SBjoern A. Zeeb #include <stdlib.h>
4135fd7bc0SBjoern A. Zeeb #include <string.h>
4235fd7bc0SBjoern A. Zeeb #include <err.h>
4335fd7bc0SBjoern A. Zeeb 
4435fd7bc0SBjoern A. Zeeb #include "ifconfig.h"
4535fd7bc0SBjoern A. Zeeb 
4635fd7bc0SBjoern A. Zeeb static void
fib_status(if_ctx * ctx)476e3a9d7fSAlexander V. Chernikov fib_status(if_ctx *ctx)
4835fd7bc0SBjoern A. Zeeb {
4935fd7bc0SBjoern A. Zeeb 	struct ifreq ifr;
5035fd7bc0SBjoern A. Zeeb 
5135fd7bc0SBjoern A. Zeeb 	memset(&ifr, 0, sizeof(ifr));
5285e0016aSAlexander V. Chernikov 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
536e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGIFFIB, (caddr_t)&ifr) == 0 &&
54eccfe69aSAndrey V. Elsukov 	    ifr.ifr_fib != RT_DEFAULT_FIB)
5535fd7bc0SBjoern A. Zeeb 		printf("\tfib: %u\n", ifr.ifr_fib);
56eccfe69aSAndrey V. Elsukov 
57eccfe69aSAndrey V. Elsukov 	memset(&ifr, 0, sizeof(ifr));
5885e0016aSAlexander V. Chernikov 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
596e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGTUNFIB, (caddr_t)&ifr) == 0 &&
60eccfe69aSAndrey V. Elsukov 	    ifr.ifr_fib != RT_DEFAULT_FIB)
61eccfe69aSAndrey V. Elsukov 		printf("\ttunnelfib: %u\n", ifr.ifr_fib);
6235fd7bc0SBjoern A. Zeeb }
6335fd7bc0SBjoern A. Zeeb 
6435fd7bc0SBjoern A. Zeeb static void
setiffib(if_ctx * ctx,const char * val,int dummy __unused)656e3a9d7fSAlexander V. Chernikov setiffib(if_ctx *ctx, const char *val, int dummy __unused)
6635fd7bc0SBjoern A. Zeeb {
67*7fa282e6SAlexander V. Chernikov 	struct ifreq ifr = {};
6835fd7bc0SBjoern A. Zeeb 	unsigned long fib;
6935fd7bc0SBjoern A. Zeeb 	char *ep;
7035fd7bc0SBjoern A. Zeeb 
7135fd7bc0SBjoern A. Zeeb 	fib = strtoul(val, &ep, 0);
7235fd7bc0SBjoern A. Zeeb 	if (*ep != '\0' || fib > UINT_MAX) {
7335fd7bc0SBjoern A. Zeeb 		warn("fib %s not valid", val);
7435fd7bc0SBjoern A. Zeeb 		return;
7535fd7bc0SBjoern A. Zeeb 	}
7635fd7bc0SBjoern A. Zeeb 
7735fd7bc0SBjoern A. Zeeb 	ifr.ifr_fib = fib;
78*7fa282e6SAlexander V. Chernikov 	if (ioctl_ctx_ifr(ctx, SIOCSIFFIB, &ifr) < 0)
7935fd7bc0SBjoern A. Zeeb 		warn("ioctl (SIOCSIFFIB)");
8035fd7bc0SBjoern A. Zeeb }
8135fd7bc0SBjoern A. Zeeb 
82eccfe69aSAndrey V. Elsukov static void
settunfib(if_ctx * ctx,const char * val,int dummy __unused)836e3a9d7fSAlexander V. Chernikov settunfib(if_ctx *ctx, const char *val, int dummy __unused)
84eccfe69aSAndrey V. Elsukov {
85*7fa282e6SAlexander V. Chernikov 	struct ifreq ifr = {};
86eccfe69aSAndrey V. Elsukov 	unsigned long fib;
87eccfe69aSAndrey V. Elsukov 	char *ep;
88eccfe69aSAndrey V. Elsukov 
89eccfe69aSAndrey V. Elsukov 	fib = strtoul(val, &ep, 0);
90eccfe69aSAndrey V. Elsukov 	if (*ep != '\0' || fib > UINT_MAX) {
91eccfe69aSAndrey V. Elsukov 		warn("fib %s not valid", val);
92eccfe69aSAndrey V. Elsukov 		return;
93eccfe69aSAndrey V. Elsukov 	}
94eccfe69aSAndrey V. Elsukov 
95eccfe69aSAndrey V. Elsukov 	ifr.ifr_fib = fib;
96*7fa282e6SAlexander V. Chernikov 	if (ioctl_ctx_ifr(ctx, SIOCSTUNFIB, &ifr) < 0)
97eccfe69aSAndrey V. Elsukov 		warn("ioctl (SIOCSTUNFIB)");
98eccfe69aSAndrey V. Elsukov }
99eccfe69aSAndrey V. Elsukov 
10035fd7bc0SBjoern A. Zeeb static struct cmd fib_cmds[] = {
10135fd7bc0SBjoern A. Zeeb 	DEF_CMD_ARG("fib", setiffib),
102eccfe69aSAndrey V. Elsukov 	DEF_CMD_ARG("tunnelfib", settunfib),
10335fd7bc0SBjoern A. Zeeb };
10435fd7bc0SBjoern A. Zeeb 
10535fd7bc0SBjoern A. Zeeb static struct afswtch af_fib = {
10635fd7bc0SBjoern A. Zeeb 	.af_name	= "af_fib",
10735fd7bc0SBjoern A. Zeeb 	.af_af		= AF_UNSPEC,
10835fd7bc0SBjoern A. Zeeb 	.af_other_status = fib_status,
10935fd7bc0SBjoern A. Zeeb };
11035fd7bc0SBjoern A. Zeeb 
11135fd7bc0SBjoern A. Zeeb static __constructor void
fib_ctor(void)11235fd7bc0SBjoern A. Zeeb fib_ctor(void)
11335fd7bc0SBjoern A. Zeeb {
11435fd7bc0SBjoern A. Zeeb 	size_t i;
11535fd7bc0SBjoern A. Zeeb 
116abd71050SEnji Cooper 	for (i = 0; i < nitems(fib_cmds);  i++)
11735fd7bc0SBjoern A. Zeeb 		cmd_register(&fib_cmds[i]);
11835fd7bc0SBjoern A. Zeeb 	af_register(&af_fib);
11935fd7bc0SBjoern A. Zeeb }
120