xref: /freebsd/usr.sbin/rtsold/if.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
18e7409edSHajimu UMEMOTO /*	$KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $	*/
2804c83d4SKris Kennaway 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
67d56d374SYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
77d56d374SYoshinobu Inoue  * All rights reserved.
87d56d374SYoshinobu Inoue  *
97d56d374SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
107d56d374SYoshinobu Inoue  * modification, are permitted provided that the following conditions
117d56d374SYoshinobu Inoue  * are met:
127d56d374SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
137d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
147d56d374SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
157d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
167d56d374SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
177d56d374SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
187d56d374SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
197d56d374SYoshinobu Inoue  *    without specific prior written permission.
207d56d374SYoshinobu Inoue  *
217d56d374SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
227d56d374SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237d56d374SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247d56d374SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
257d56d374SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267d56d374SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277d56d374SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287d56d374SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297d56d374SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307d56d374SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317d56d374SYoshinobu Inoue  * SUCH DAMAGE.
327d56d374SYoshinobu Inoue  */
337d56d374SYoshinobu Inoue 
347d56d374SYoshinobu Inoue #include <sys/param.h>
3504e9edb5SMark Johnston #include <sys/capsicum.h>
367d56d374SYoshinobu Inoue #include <sys/ioctl.h>
37804c83d4SKris Kennaway #include <sys/queue.h>
3804e9edb5SMark Johnston #include <sys/socket.h>
3904e9edb5SMark Johnston #include <sys/sysctl.h>
407d56d374SYoshinobu Inoue 
417d56d374SYoshinobu Inoue #include <net/if.h>
427d56d374SYoshinobu Inoue #include <net/if_types.h>
437d56d374SYoshinobu Inoue #include <net/route.h>
447d56d374SYoshinobu Inoue #include <net/if_dl.h>
457d56d374SYoshinobu Inoue #include <net/if_media.h>
467d56d374SYoshinobu Inoue #include <net/ethernet.h>
477d56d374SYoshinobu Inoue #include <netinet/in.h>
487d56d374SYoshinobu Inoue #include <netinet/icmp6.h>
497d56d374SYoshinobu Inoue 
507d56d374SYoshinobu Inoue #include <netinet6/in6_var.h>
51eb87e699SHiroki Sato #include <netinet6/nd6.h>
527d56d374SYoshinobu Inoue 
5304e9edb5SMark Johnston #include <capsicum_helpers.h>
547d56d374SYoshinobu Inoue #include <stdio.h>
557d56d374SYoshinobu Inoue #include <unistd.h>
567d56d374SYoshinobu Inoue #include <stdlib.h>
577d56d374SYoshinobu Inoue #include <syslog.h>
587d56d374SYoshinobu Inoue #include <string.h>
597d56d374SYoshinobu Inoue #include <fcntl.h>
607d56d374SYoshinobu Inoue #include <errno.h>
617d56d374SYoshinobu Inoue #include <limits.h>
62259df286SKris Kennaway #include <ifaddrs.h>
637d56d374SYoshinobu Inoue #include "rtsold.h"
647d56d374SYoshinobu Inoue 
657d56d374SYoshinobu Inoue static int ifsock;
66784bddbcSKevin Lo static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
677d56d374SYoshinobu Inoue 
687d56d374SYoshinobu Inoue int
ifinit(void)698e7409edSHajimu UMEMOTO ifinit(void)
707d56d374SYoshinobu Inoue {
7104e9edb5SMark Johnston 	cap_rights_t rights;
7204e9edb5SMark Johnston 	int sock;
737d56d374SYoshinobu Inoue 
7404e9edb5SMark Johnston 	sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
7504e9edb5SMark Johnston 	if (sock < 0) {
7604e9edb5SMark Johnston 		warnmsg(LOG_ERR, __func__, "socket(): %s",
7704e9edb5SMark Johnston 		    strerror(errno));
7804e9edb5SMark Johnston 		return (-1);
7904e9edb5SMark Johnston 	}
8004e9edb5SMark Johnston 	if (caph_rights_limit(sock, cap_rights_init(&rights, CAP_IOCTL)) < 0) {
8104e9edb5SMark Johnston 		warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
8204e9edb5SMark Johnston 		    strerror(errno));
8304e9edb5SMark Johnston 		(void)close(sock);
8404e9edb5SMark Johnston 		return (-1);
8504e9edb5SMark Johnston 	}
8604e9edb5SMark Johnston 	ifsock = sock;
877d56d374SYoshinobu Inoue 	return (0);
887d56d374SYoshinobu Inoue }
897d56d374SYoshinobu Inoue 
907d56d374SYoshinobu Inoue int
interface_up(char * name)917d56d374SYoshinobu Inoue interface_up(char *name)
927d56d374SYoshinobu Inoue {
937d56d374SYoshinobu Inoue 	struct ifreq ifr;
94eb87e699SHiroki Sato 	struct in6_ndireq nd;
95259df286SKris Kennaway 	int llflag;
96eb87e699SHiroki Sato 	int s;
977d56d374SYoshinobu Inoue 
98eb87e699SHiroki Sato 	memset(&ifr, 0, sizeof(ifr));
99dc47112fSDon Lewis 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
100eb87e699SHiroki Sato 	memset(&nd, 0, sizeof(nd));
101eb87e699SHiroki Sato 	strlcpy(nd.ifname, name, sizeof(nd.ifname));
1027d56d374SYoshinobu Inoue 
1037d56d374SYoshinobu Inoue 	if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
1041533bed0SHajimu UMEMOTO 		warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
1057d56d374SYoshinobu Inoue 		    strerror(errno));
1067d56d374SYoshinobu Inoue 		return (-1);
1077d56d374SYoshinobu Inoue 	}
1087d56d374SYoshinobu Inoue 	if (!(ifr.ifr_flags & IFF_UP)) {
1097d56d374SYoshinobu Inoue 		ifr.ifr_flags |= IFF_UP;
110fa19f9beSHajimu UMEMOTO 		if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
1111533bed0SHajimu UMEMOTO 			warnmsg(LOG_ERR, __func__,
1127d56d374SYoshinobu Inoue 			    "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
1137d56d374SYoshinobu Inoue 		return (-1);
1147d56d374SYoshinobu Inoue 	}
115eb87e699SHiroki Sato 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
116eb87e699SHiroki Sato 		warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
117eb87e699SHiroki Sato 		    strerror(errno));
118eb87e699SHiroki Sato 		return (-1);
119eb87e699SHiroki Sato 	}
120eb87e699SHiroki Sato 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
121eb87e699SHiroki Sato 		warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
122eb87e699SHiroki Sato 		    strerror(errno));
123eb87e699SHiroki Sato 		close(s);
124eb87e699SHiroki Sato 		return (-1);
125eb87e699SHiroki Sato 	}
1267d56d374SYoshinobu Inoue 
1271533bed0SHajimu UMEMOTO 	warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
1287d56d374SYoshinobu Inoue 
129eb87e699SHiroki Sato 	if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
130eb87e699SHiroki Sato 		if (Fflag) {
131eb87e699SHiroki Sato 			nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
132eb87e699SHiroki Sato 			if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
133eb87e699SHiroki Sato 				warnmsg(LOG_WARNING, __func__,
134eb87e699SHiroki Sato 				    "ioctl(SIOCSIFINFO_IN6): %s",
135eb87e699SHiroki Sato 		    		    strerror(errno));
136eb87e699SHiroki Sato 				close(s);
137eb87e699SHiroki Sato 				return (-1);
138eb87e699SHiroki Sato 			}
139eb87e699SHiroki Sato 		} else {
140eb87e699SHiroki Sato 			warnmsg(LOG_WARNING, __func__,
141eb87e699SHiroki Sato 			    "%s is disabled.", name);
142eb87e699SHiroki Sato 			close(s);
143eb87e699SHiroki Sato 			return (-1);
144eb87e699SHiroki Sato 		}
145eb87e699SHiroki Sato 	}
146eb87e699SHiroki Sato 	if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
147eb87e699SHiroki Sato 		if (Fflag) {
148eb87e699SHiroki Sato 			nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
149eb87e699SHiroki Sato 			if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
150eb87e699SHiroki Sato 				warnmsg(LOG_WARNING, __func__,
151eb87e699SHiroki Sato 				    "ioctl(SIOCSIFINFO_IN6): %s",
152eb87e699SHiroki Sato 		    		    strerror(errno));
153eb87e699SHiroki Sato 				close(s);
154eb87e699SHiroki Sato 				return (-1);
155eb87e699SHiroki Sato 			}
156eb87e699SHiroki Sato 		} else {
157eb87e699SHiroki Sato 			warnmsg(LOG_WARNING, __func__,
158eb87e699SHiroki Sato 			    "%s does not accept Router Advertisement.", name);
159eb87e699SHiroki Sato 			close(s);
160eb87e699SHiroki Sato 			return (-1);
161eb87e699SHiroki Sato 		}
162eb87e699SHiroki Sato 	}
163eb87e699SHiroki Sato 	close(s);
164eb87e699SHiroki Sato 
16504e9edb5SMark Johnston 	if (cap_llflags_get(capllflags, name, &llflag) != 0) {
1661533bed0SHajimu UMEMOTO 		warnmsg(LOG_WARNING, __func__,
16704e9edb5SMark Johnston 		    "cap_llflags_get() failed, anyway I'll try");
168db82af41SHiroki Sato 		return (0);
1697d56d374SYoshinobu Inoue 	}
1707d56d374SYoshinobu Inoue 
171259df286SKris Kennaway 	if (!(llflag & IN6_IFF_NOTREADY)) {
172fa19f9beSHajimu UMEMOTO 		warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
1737d56d374SYoshinobu Inoue 		return (0);
174259df286SKris Kennaway 	} else {
175259df286SKris Kennaway 		if (llflag & IN6_IFF_TENTATIVE) {
1761533bed0SHajimu UMEMOTO 			warnmsg(LOG_DEBUG, __func__, "%s is tentative",
1777d56d374SYoshinobu Inoue 			    name);
178db82af41SHiroki Sato 			return (IFS_TENTATIVE);
1797d56d374SYoshinobu Inoue 		}
180259df286SKris Kennaway 		if (llflag & IN6_IFF_DUPLICATED)
1811533bed0SHajimu UMEMOTO 			warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
1827d56d374SYoshinobu Inoue 			    name);
183db82af41SHiroki Sato 		return (-1);
1847d56d374SYoshinobu Inoue 	}
1857d56d374SYoshinobu Inoue }
1867d56d374SYoshinobu Inoue 
1877d56d374SYoshinobu Inoue int
interface_status(struct ifinfo * ifinfo)1887d56d374SYoshinobu Inoue interface_status(struct ifinfo *ifinfo)
1897d56d374SYoshinobu Inoue {
1907d56d374SYoshinobu Inoue 	char *ifname = ifinfo->ifname;
1917d56d374SYoshinobu Inoue 	struct ifreq ifr;
1927d56d374SYoshinobu Inoue 	struct ifmediareq ifmr;
1937d56d374SYoshinobu Inoue 
1947d56d374SYoshinobu Inoue 	/* get interface flags */
1957d56d374SYoshinobu Inoue 	memset(&ifr, 0, sizeof(ifr));
196dc47112fSDon Lewis 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1977d56d374SYoshinobu Inoue 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
1981533bed0SHajimu UMEMOTO 		warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
1997d56d374SYoshinobu Inoue 		    ifname, strerror(errno));
2007d56d374SYoshinobu Inoue 		return (-1);
2017d56d374SYoshinobu Inoue 	}
2027d56d374SYoshinobu Inoue 	/*
2037d56d374SYoshinobu Inoue 	 * if one of UP and RUNNING flags is dropped,
2047d56d374SYoshinobu Inoue 	 * the interface is not active.
2057d56d374SYoshinobu Inoue 	 */
206db82af41SHiroki Sato 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
2077d56d374SYoshinobu Inoue 		goto inactive;
2087d56d374SYoshinobu Inoue 	/* Next, check carrier on the interface, if possible */
2097d56d374SYoshinobu Inoue 	if (!ifinfo->mediareqok)
2107d56d374SYoshinobu Inoue 		goto active;
2117d56d374SYoshinobu Inoue 	memset(&ifmr, 0, sizeof(ifmr));
212dc47112fSDon Lewis 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
2137d56d374SYoshinobu Inoue 
2147d56d374SYoshinobu Inoue 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
2157d56d374SYoshinobu Inoue 		if (errno != EINVAL) {
2161533bed0SHajimu UMEMOTO 			warnmsg(LOG_DEBUG, __func__,
2177d56d374SYoshinobu Inoue 			    "ioctl(SIOCGIFMEDIA) on %s: %s",
2187d56d374SYoshinobu Inoue 			    ifname, strerror(errno));
2197d56d374SYoshinobu Inoue 			return(-1);
2207d56d374SYoshinobu Inoue 		}
2217d56d374SYoshinobu Inoue 		/*
2227d56d374SYoshinobu Inoue 		 * EINVAL simply means that the interface does not support
2237d56d374SYoshinobu Inoue 		 * the SIOCGIFMEDIA ioctl. We regard it alive.
2247d56d374SYoshinobu Inoue 		 */
2257d56d374SYoshinobu Inoue 		ifinfo->mediareqok = 0;
2267d56d374SYoshinobu Inoue 		goto active;
2277d56d374SYoshinobu Inoue 	}
2287d56d374SYoshinobu Inoue 
2297d56d374SYoshinobu Inoue 	if (ifmr.ifm_status & IFM_AVALID) {
2307d56d374SYoshinobu Inoue 		switch (ifmr.ifm_active & IFM_NMASK) {
2317d56d374SYoshinobu Inoue 		case IFM_ETHER:
232131033d5SHajimu UMEMOTO 		case IFM_IEEE80211:
2337d56d374SYoshinobu Inoue 			if (ifmr.ifm_status & IFM_ACTIVE)
2347d56d374SYoshinobu Inoue 				goto active;
2357d56d374SYoshinobu Inoue 			else
2367d56d374SYoshinobu Inoue 				goto inactive;
2377d56d374SYoshinobu Inoue 			break;
2387d56d374SYoshinobu Inoue 		default:
2397d56d374SYoshinobu Inoue 			goto inactive;
2407d56d374SYoshinobu Inoue 		}
2417d56d374SYoshinobu Inoue 	}
2427d56d374SYoshinobu Inoue 
2437d56d374SYoshinobu Inoue   inactive:
2447d56d374SYoshinobu Inoue 	return (0);
2457d56d374SYoshinobu Inoue 
2467d56d374SYoshinobu Inoue   active:
2477d56d374SYoshinobu Inoue 	return (1);
2487d56d374SYoshinobu Inoue }
2497d56d374SYoshinobu Inoue 
2507d56d374SYoshinobu Inoue #define ROUNDUP(a, size) \
2517d56d374SYoshinobu Inoue 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
2527d56d374SYoshinobu Inoue 
2537d56d374SYoshinobu Inoue #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
2547d56d374SYoshinobu Inoue 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
255fa19f9beSHajimu UMEMOTO 	sizeof(u_long)) : sizeof(u_long)))
2567d56d374SYoshinobu Inoue #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
2577d56d374SYoshinobu Inoue 
2587d56d374SYoshinobu Inoue int
lladdropt_length(struct sockaddr_dl * sdl)2597d56d374SYoshinobu Inoue lladdropt_length(struct sockaddr_dl *sdl)
2607d56d374SYoshinobu Inoue {
2617d56d374SYoshinobu Inoue 	switch (sdl->sdl_type) {
2627d56d374SYoshinobu Inoue 	case IFT_ETHER:
263*1a4d7030SLuiz Otavio O Souza 	case IFT_L2VLAN:
2647d56d374SYoshinobu Inoue 		return (ROUNDUP8(ETHER_ADDR_LEN + 2));
2657d56d374SYoshinobu Inoue 	default:
2667d56d374SYoshinobu Inoue 		return (0);
2677d56d374SYoshinobu Inoue 	}
2687d56d374SYoshinobu Inoue }
2697d56d374SYoshinobu Inoue 
2707d56d374SYoshinobu Inoue void
lladdropt_fill(struct sockaddr_dl * sdl,struct nd_opt_hdr * ndopt)2717d56d374SYoshinobu Inoue lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
2727d56d374SYoshinobu Inoue {
2737d56d374SYoshinobu Inoue 	char *addr;
2747d56d374SYoshinobu Inoue 
2757d56d374SYoshinobu Inoue 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
2767d56d374SYoshinobu Inoue 
2777d56d374SYoshinobu Inoue 	switch (sdl->sdl_type) {
2787d56d374SYoshinobu Inoue 	case IFT_ETHER:
279*1a4d7030SLuiz Otavio O Souza 	case IFT_L2VLAN:
2807d56d374SYoshinobu Inoue 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
2817d56d374SYoshinobu Inoue 		addr = (char *)(ndopt + 1);
2827d56d374SYoshinobu Inoue 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
2837d56d374SYoshinobu Inoue 		break;
2847d56d374SYoshinobu Inoue 	default:
2851533bed0SHajimu UMEMOTO 		warnmsg(LOG_ERR, __func__,
2867d56d374SYoshinobu Inoue 		    "unsupported link type(%d)", sdl->sdl_type);
2877d56d374SYoshinobu Inoue 		exit(1);
2887d56d374SYoshinobu Inoue 	}
2897d56d374SYoshinobu Inoue }
2907d56d374SYoshinobu Inoue 
2917d56d374SYoshinobu Inoue struct sockaddr_dl *
if_nametosdl(char * name)2927d56d374SYoshinobu Inoue if_nametosdl(char *name)
2937d56d374SYoshinobu Inoue {
294f74237f5SHiroki Sato 	int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
2957d56d374SYoshinobu Inoue 	char *buf, *next, *lim;
2967d56d374SYoshinobu Inoue 	size_t len;
2977d56d374SYoshinobu Inoue 	struct if_msghdr *ifm;
2987d56d374SYoshinobu Inoue 	struct sockaddr *sa, *rti_info[RTAX_MAX];
2997d56d374SYoshinobu Inoue 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
3007d56d374SYoshinobu Inoue 
301f74237f5SHiroki Sato 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
3027d56d374SYoshinobu Inoue 		return(NULL);
3037d56d374SYoshinobu Inoue 	if ((buf = malloc(len)) == NULL)
3047d56d374SYoshinobu Inoue 		return(NULL);
305f74237f5SHiroki Sato 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) {
3067d56d374SYoshinobu Inoue 		free(buf);
3077d56d374SYoshinobu Inoue 		return (NULL);
3087d56d374SYoshinobu Inoue 	}
3097d56d374SYoshinobu Inoue 
3107d56d374SYoshinobu Inoue 	lim = buf + len;
3117d56d374SYoshinobu Inoue 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
312a678ca23SHiroki Sato 		ifm = (struct if_msghdr *)(void *)next;
3137d56d374SYoshinobu Inoue 		if (ifm->ifm_type == RTM_IFINFO) {
3147d56d374SYoshinobu Inoue 			sa = (struct sockaddr *)(ifm + 1);
3157d56d374SYoshinobu Inoue 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
3167d56d374SYoshinobu Inoue 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
3177d56d374SYoshinobu Inoue 				if (sa->sa_family == AF_LINK) {
318a678ca23SHiroki Sato 					sdl = (struct sockaddr_dl *)(void *)sa;
319259df286SKris Kennaway 					if (strlen(name) != sdl->sdl_nlen)
320259df286SKris Kennaway 						continue; /* not same len */
3217d56d374SYoshinobu Inoue 					if (strncmp(&sdl->sdl_data[0],
3227d56d374SYoshinobu Inoue 						    name,
3237d56d374SYoshinobu Inoue 						    sdl->sdl_nlen) == 0) {
3247d56d374SYoshinobu Inoue 						break;
3257d56d374SYoshinobu Inoue 					}
3267d56d374SYoshinobu Inoue 				}
3277d56d374SYoshinobu Inoue 			}
3287d56d374SYoshinobu Inoue 		}
3297d56d374SYoshinobu Inoue 	}
330ecce515dSMark Johnston 	if (next >= lim) {
3317d56d374SYoshinobu Inoue 		/* search failed */
3327d56d374SYoshinobu Inoue 		free(buf);
3337d56d374SYoshinobu Inoue 		return (NULL);
3347d56d374SYoshinobu Inoue 	}
3357d56d374SYoshinobu Inoue 
3364b8b545eSSUZUKI Shinsuke 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
3374b8b545eSSUZUKI Shinsuke 		free(buf);
3387d56d374SYoshinobu Inoue 		return (NULL);
3394b8b545eSSUZUKI Shinsuke 	}
3407d56d374SYoshinobu Inoue 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
3417d56d374SYoshinobu Inoue 
34233841545SHajimu UMEMOTO 	free(buf);
3437d56d374SYoshinobu Inoue 	return (ret_sdl);
3447d56d374SYoshinobu Inoue }
3457d56d374SYoshinobu Inoue 
3467d56d374SYoshinobu Inoue static void
get_rtaddrs(int addrs,struct sockaddr * sa,struct sockaddr ** rti_info)3477d56d374SYoshinobu Inoue get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
3487d56d374SYoshinobu Inoue {
3497d56d374SYoshinobu Inoue 	int i;
3507d56d374SYoshinobu Inoue 
3517d56d374SYoshinobu Inoue 	for (i = 0; i < RTAX_MAX; i++) {
3527d56d374SYoshinobu Inoue 		if (addrs & (1 << i)) {
3537d56d374SYoshinobu Inoue 			rti_info[i] = sa;
3547d56d374SYoshinobu Inoue 			NEXT_SA(sa);
355fa19f9beSHajimu UMEMOTO 		} else
3567d56d374SYoshinobu Inoue 			rti_info[i] = NULL;
3577d56d374SYoshinobu Inoue 	}
3587d56d374SYoshinobu Inoue }
359