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
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
47 #include <netinet/in.h>
48 #include <netinet/icmp6.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet6/nd6.h>
53 #include <capsicum_helpers.h>
66 static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
74 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
76 warnmsg(LOG_ERR, __func__, "socket(): %s",
80 if (caph_rights_limit(sock, cap_rights_init(&rights, CAP_IOCTL)) < 0) {
81 warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
91 interface_up(char *name)
98 memset(&ifr, 0, sizeof(ifr));
99 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
100 memset(&nd, 0, sizeof(nd));
101 strlcpy(nd.ifname, name, sizeof(nd.ifname));
103 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
104 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
108 if (!(ifr.ifr_flags & IFF_UP)) {
109 ifr.ifr_flags |= IFF_UP;
110 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
111 warnmsg(LOG_ERR, __func__,
112 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
115 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
116 warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
120 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
121 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
127 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
129 if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
131 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
132 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
133 warnmsg(LOG_WARNING, __func__,
134 "ioctl(SIOCSIFINFO_IN6): %s",
140 warnmsg(LOG_WARNING, __func__,
141 "%s is disabled.", name);
146 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
148 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
149 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
150 warnmsg(LOG_WARNING, __func__,
151 "ioctl(SIOCSIFINFO_IN6): %s",
157 warnmsg(LOG_WARNING, __func__,
158 "%s does not accept Router Advertisement.", name);
165 if (cap_llflags_get(capllflags, name, &llflag) != 0) {
166 warnmsg(LOG_WARNING, __func__,
167 "cap_llflags_get() failed, anyway I'll try");
171 if (!(llflag & IN6_IFF_NOTREADY)) {
172 warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
175 if (llflag & IN6_IFF_TENTATIVE) {
176 warnmsg(LOG_DEBUG, __func__, "%s is tentative",
178 return (IFS_TENTATIVE);
180 if (llflag & IN6_IFF_DUPLICATED)
181 warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
188 interface_status(struct ifinfo *ifinfo)
190 char *ifname = ifinfo->ifname;
192 struct ifmediareq ifmr;
194 /* get interface flags */
195 memset(&ifr, 0, sizeof(ifr));
196 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
197 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
198 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
199 ifname, strerror(errno));
203 * if one of UP and RUNNING flags is dropped,
204 * the interface is not active.
206 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
208 /* Next, check carrier on the interface, if possible */
209 if (!ifinfo->mediareqok)
211 memset(&ifmr, 0, sizeof(ifmr));
212 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
214 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
215 if (errno != EINVAL) {
216 warnmsg(LOG_DEBUG, __func__,
217 "ioctl(SIOCGIFMEDIA) on %s: %s",
218 ifname, strerror(errno));
222 * EINVAL simply means that the interface does not support
223 * the SIOCGIFMEDIA ioctl. We regard it alive.
225 ifinfo->mediareqok = 0;
229 if (ifmr.ifm_status & IFM_AVALID) {
230 switch (ifmr.ifm_active & IFM_NMASK) {
233 if (ifmr.ifm_status & IFM_ACTIVE)
250 #define ROUNDUP(a, size) \
251 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
253 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
254 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
255 sizeof(u_long)) : sizeof(u_long)))
256 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
259 lladdropt_length(struct sockaddr_dl *sdl)
261 switch (sdl->sdl_type) {
264 return (ROUNDUP8(ETHER_ADDR_LEN + 2));
271 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
275 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
277 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)) {