/*-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) {72warn("fib %s not valid", val);73return;74}7576ifr.ifr_fib = fib;77if (ioctl_ctx_ifr(ctx, SIOCSIFFIB, &ifr) < 0)78warn("ioctl (SIOCSIFFIB)");79}8081static void82settunfib(if_ctx *ctx, const char *val, int dummy __unused)83{84struct ifreq ifr = {};85unsigned long fib;86char *ep;8788fib = strtoul(val, &ep, 0);89if (*ep != '\0' || fib > UINT_MAX) {90warn("fib %s not valid", val);91return;92}9394ifr.ifr_fib = fib;95if (ioctl_ctx_ifr(ctx, SIOCSTUNFIB, &ifr) < 0)96warn("ioctl (SIOCSTUNFIB)");97}9899static struct cmd fib_cmds[] = {100DEF_CMD_ARG("fib", setiffib),101DEF_CMD_ARG("tunnelfib", settunfib),102};103104static struct afswtch af_fib = {105.af_name = "af_fib",106.af_af = AF_UNSPEC,107.af_other_status = fib_status,108};109110static __constructor void111fib_ctor(void)112{113size_t i;114115for (i = 0; i < nitems(fib_cmds); i++)116cmd_register(&fib_cmds[i]);117af_register(&af_fib);118}119120121