2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018 The FreeBSD Foundation
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
38 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <netinet6/in6_var.h>
49 #include <libcasper.h>
50 #include <libcasper_service.h>
55 * A service to fetch the flags for the link-local IPv6 address on the specified
56 * interface. This cannot easily be done in capability mode because we need to
57 * use the routing socket sysctl API to find the link-local address of a
58 * particular interface. The SIOCGIFCONF ioctl is one other option, but as
59 * currently implemented it is less flexible (it cannot report the required
60 * buffer length), and hard-codes a buffer length limit.
64 llflags_get(const char *ifname, int *flagsp)
66 struct in6_ifreq ifr6;
67 struct ifaddrs *ifap, *ifa;
68 struct sockaddr_in6 *sin6;
71 s = socket(PF_INET6, SOCK_DGRAM, 0);
76 if (getifaddrs(&ifap) != 0) {
81 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
82 if (strcmp(ifa->ifa_name, ifname) != 0)
84 if (ifa->ifa_addr->sa_family != AF_INET6)
87 sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
88 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
91 memset(&ifr6, 0, sizeof(ifr6));
92 if (strlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name)) >=
93 sizeof(ifr6.ifr_name)) {
97 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
98 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
103 *flagsp = ifr6.ifr_ifru.ifru_flags6;
120 cap_llflags_get(cap_channel_t *cap, const char *ifname, int *flagsp)
126 nvl = nvlist_create(0);
127 nvlist_add_string(nvl, "cmd", "get");
128 nvlist_add_string(nvl, "ifname", ifname);
129 nvl = cap_xfer_nvlist(cap, nvl);
132 error = (int)dnvlist_get_number(nvl, "error", 0);
134 *flagsp = (int)nvlist_get_number(nvl, "flags");
138 return (error == 0 ? 0 : -1);
141 return (llflags_get(ifname, flagsp));
147 llflags_command(const char *cmd, const nvlist_t *limits __unused,
148 nvlist_t *nvlin, nvlist_t *nvlout)
153 if (strcmp(cmd, "get") != 0)
155 ifname = nvlist_get_string(nvlin, "ifname");
156 if (llflags_get(ifname, &flags) != 0)
158 nvlist_add_number(nvlout, "flags", flags);
162 CREATE_SERVICE("rtsold.llflags", NULL, llflags_command, 0);
163 #endif /* WITH_CASPER */