/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 Alexander V. Chernikov4* Copyright (c) 2011 Christian S.J. Peron5* Copyright (c) 2011 Bjoern A. Zeeb6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/param.h>31#include <sys/ioctl.h>32#include <sys/socket.h>33#include <sys/sockio.h>3435#include <net/if.h>36#include <net/route.h>3738#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <err.h>4243#include "ifconfig.h"4445static void46fib_status(if_ctx *ctx)47{48struct ifreq ifr;4950memset(&ifr, 0, sizeof(ifr));51strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));52if (ioctl_ctx(ctx, SIOCGIFFIB, (caddr_t)&ifr) == 0 &&53ifr.ifr_fib != RT_DEFAULT_FIB)54printf("\tfib: %u\n", ifr.ifr_fib);5556memset(&ifr, 0, sizeof(ifr));57strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));58if (ioctl_ctx(ctx, SIOCGTUNFIB, (caddr_t)&ifr) == 0 &&59ifr.ifr_fib != RT_DEFAULT_FIB)60printf("\ttunnelfib: %u\n", ifr.ifr_fib);61}6263static void64setiffib(if_ctx *ctx, const char *val, int dummy __unused)65{66struct ifreq ifr = {};67unsigned long fib;68char *ep;6970fib = strtoul(val, &ep, 0);71if (*ep != '\0' || fib > UINT_MAX)72errx(1, "fib %s not valid", val);7374ifr.ifr_fib = fib;75if (ioctl_ctx_ifr(ctx, SIOCSIFFIB, &ifr) < 0)76err(1, "ioctl (SIOCSIFFIB)");77}7879static void80settunfib(if_ctx *ctx, const char *val, int dummy __unused)81{82struct ifreq ifr = {};83unsigned long fib;84char *ep;8586fib = strtoul(val, &ep, 0);87if (*ep != '\0' || fib > UINT_MAX)88errx(1, "fib %s not valid", val);8990ifr.ifr_fib = fib;91if (ioctl_ctx_ifr(ctx, SIOCSTUNFIB, &ifr) < 0)92err(1, "ioctl (SIOCSTUNFIB)");93}9495static struct cmd fib_cmds[] = {96DEF_CMD_ARG("fib", setiffib),97DEF_CMD_ARG("tunnelfib", settunfib),98};99100static struct afswtch af_fib = {101.af_name = "af_fib",102.af_af = AF_UNSPEC,103.af_other_status = fib_status,104};105106static __constructor void107fib_ctor(void)108{109size_t i;110111for (i = 0; i < nitems(fib_cmds); i++)112cmd_register(&fib_cmds[i]);113af_register(&af_fib);114}115116117