1 /* $KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
38 #include <sys/ioctl.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
49 #include <netinet/in.h>
50 #include <netinet/icmp6.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet6/nd6.h>
55 #include <capsicum_helpers.h>
68 static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
76 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
78 warnmsg(LOG_ERR, __func__, "socket(): %s",
82 if (caph_rights_limit(sock, cap_rights_init(&rights, CAP_IOCTL)) < 0) {
83 warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
93 interface_up(char *name)
100 memset(&ifr, 0, sizeof(ifr));
101 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
102 memset(&nd, 0, sizeof(nd));
103 strlcpy(nd.ifname, name, sizeof(nd.ifname));
105 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
106 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
110 if (!(ifr.ifr_flags & IFF_UP)) {
111 ifr.ifr_flags |= IFF_UP;
112 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
113 warnmsg(LOG_ERR, __func__,
114 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
117 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
118 warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
122 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
123 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
129 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
131 if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
133 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
134 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
135 warnmsg(LOG_WARNING, __func__,
136 "ioctl(SIOCSIFINFO_IN6): %s",
142 warnmsg(LOG_WARNING, __func__,
143 "%s is disabled.", name);
148 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
150 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
151 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
152 warnmsg(LOG_WARNING, __func__,
153 "ioctl(SIOCSIFINFO_IN6): %s",
159 warnmsg(LOG_WARNING, __func__,
160 "%s does not accept Router Advertisement.", name);
167 if (cap_llflags_get(capllflags, name, &llflag) != 0) {
168 warnmsg(LOG_WARNING, __func__,
169 "cap_llflags_get() failed, anyway I'll try");
173 if (!(llflag & IN6_IFF_NOTREADY)) {
174 warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
177 if (llflag & IN6_IFF_TENTATIVE) {
178 warnmsg(LOG_DEBUG, __func__, "%s is tentative",
180 return (IFS_TENTATIVE);
182 if (llflag & IN6_IFF_DUPLICATED)
183 warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
190 interface_status(struct ifinfo *ifinfo)
192 char *ifname = ifinfo->ifname;
194 struct ifmediareq ifmr;
196 /* get interface flags */
197 memset(&ifr, 0, sizeof(ifr));
198 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
199 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
200 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
201 ifname, strerror(errno));
205 * if one of UP and RUNNING flags is dropped,
206 * the interface is not active.
208 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
210 /* Next, check carrier on the interface, if possible */
211 if (!ifinfo->mediareqok)
213 memset(&ifmr, 0, sizeof(ifmr));
214 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
216 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
217 if (errno != EINVAL) {
218 warnmsg(LOG_DEBUG, __func__,
219 "ioctl(SIOCGIFMEDIA) on %s: %s",
220 ifname, strerror(errno));
224 * EINVAL simply means that the interface does not support
225 * the SIOCGIFMEDIA ioctl. We regard it alive.
227 ifinfo->mediareqok = 0;
231 if (ifmr.ifm_status & IFM_AVALID) {
232 switch (ifmr.ifm_active & IFM_NMASK) {
235 if (ifmr.ifm_status & IFM_ACTIVE)
252 #define ROUNDUP(a, size) \
253 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
255 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
256 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
257 sizeof(u_long)) : sizeof(u_long)))
258 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
261 lladdropt_length(struct sockaddr_dl *sdl)
263 switch (sdl->sdl_type) {
265 return (ROUNDUP8(ETHER_ADDR_LEN + 2));
272 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
276 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
278 switch (sdl->sdl_type) {
280 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
281 addr = (char *)(ndopt + 1);
282 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
285 warnmsg(LOG_ERR, __func__,
286 "unsupported link type(%d)", sdl->sdl_type);
292 if_nametosdl(char *name)
294 int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
295 char *buf, *next, *lim;
297 struct if_msghdr *ifm;
298 struct sockaddr *sa, *rti_info[RTAX_MAX];
299 struct sockaddr_dl *sdl = NULL, *ret_sdl;
301 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
303 if ((buf = malloc(len)) == NULL)
305 if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) {
311 for (next = buf; next < lim; next += ifm->ifm_msglen) {
312 ifm = (struct if_msghdr *)(void *)next;
313 if (ifm->ifm_type == RTM_IFINFO) {
314 sa = (struct sockaddr *)(ifm + 1);
315 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
316 if ((sa = rti_info[RTAX_IFP]) != NULL) {
317 if (sa->sa_family == AF_LINK) {
318 sdl = (struct sockaddr_dl *)(void *)sa;
319 if (strlen(name) != sdl->sdl_nlen)
320 continue; /* not same len */
321 if (strncmp(&sdl->sdl_data[0],
323 sdl->sdl_nlen) == 0) {
336 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
340 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
347 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
351 for (i = 0; i < RTAX_MAX; i++) {
352 if (addrs & (1 << i)) {