xref: /netbsd/usr.sbin/rtadvd/if.c (revision bf9ec67e)
1 /*	$NetBSD: if.c,v 1.12 2002/05/21 14:29:52 itojun Exp $	*/
2 /*	$KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
37 #include <net/if.h>
38 #include <net/if_types.h>
39 #ifdef __FreeBSD__
40 # include <net/ethernet.h>
41 #endif
42 #include <ifaddrs.h>
43 #ifdef __NetBSD__
44 #include <net/if_ether.h>
45 #endif
46 #include <net/route.h>
47 #include <net/if_dl.h>
48 #include <netinet/in.h>
49 #include <netinet/icmp6.h>
50 #ifdef __bsdi__
51 # include <netinet/if_ether.h>
52 #endif
53 #ifdef __OpenBSD__
54 #include <netinet/if_ether.h>
55 #endif
56 #include <unistd.h>
57 #include <errno.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include "rtadvd.h"
62 #include "if.h"
63 
64 #define ROUNDUP(a, size) \
65 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
66 
67 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
68 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
69 						 sizeof(u_long)) :\
70 			  			 sizeof(u_long)))
71 
72 struct if_msghdr **iflist;
73 int iflist_init_ok;
74 size_t ifblock_size;
75 char *ifblock;
76 
77 static void get_iflist __P((char **buf, size_t *size));
78 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
79 		       size_t bufsize));
80 
81 static void
82 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
83 {
84 	int i;
85 
86 	for (i = 0; i < RTAX_MAX; i++) {
87 		if (addrs & (1 << i)) {
88 			rti_info[i] = sa;
89 			NEXT_SA(sa);
90 		}
91 		else
92 			rti_info[i] = NULL;
93 	}
94 }
95 
96 struct sockaddr_dl *
97 if_nametosdl(char *name)
98 {
99 	int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
100 	char *buf, *next, *lim;
101 	size_t len;
102 	struct if_msghdr *ifm;
103 	struct sockaddr *sa, *rti_info[RTAX_MAX];
104 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
105 
106 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
107 		return(NULL);
108 	if ((buf = malloc(len)) == NULL)
109 		return(NULL);
110 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
111 		free(buf);
112 		return(NULL);
113 	}
114 
115 	lim = buf + len;
116 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
117 		ifm = (struct if_msghdr *)next;
118 		if (ifm->ifm_type == RTM_IFINFO) {
119 			sa = (struct sockaddr *)(ifm + 1);
120 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
121 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
122 				if (sa->sa_family == AF_LINK) {
123 					sdl = (struct sockaddr_dl *)sa;
124 					if (strlen(name) != sdl->sdl_nlen)
125 						continue; /* not same len */
126 					if (strncmp(&sdl->sdl_data[0],
127 						    name,
128 						    sdl->sdl_nlen) == 0) {
129 						break;
130 					}
131 				}
132 			}
133 		}
134 	}
135 	if (next == lim) {
136 		/* search failed */
137 		free(buf);
138 		return(NULL);
139 	}
140 
141 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
142 		return(NULL);
143 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
144 	return(ret_sdl);
145 }
146 
147 int
148 if_getmtu(char *name)
149 {
150 	struct ifaddrs *ifap, *ifa;
151 	struct if_data *ifd;
152 	u_long mtu = 0;
153 
154 	if (getifaddrs(&ifap) < 0)
155 		return(0);
156 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
157 		if (strcmp(ifa->ifa_name, name) == 0) {
158 			ifd = ifa->ifa_data;
159 			if (ifd)
160 				mtu = ifd->ifi_mtu;
161 			break;
162 		}
163 	}
164 	freeifaddrs(ifap);
165 
166 #ifdef SIOCGIFMTU		/* XXX: this ifdef may not be necessary */
167 	if (mtu == 0) {
168 		struct ifreq ifr;
169 		int s;
170 
171 		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
172 			return(0);
173 
174 		ifr.ifr_addr.sa_family = AF_INET6;
175 		strncpy(ifr.ifr_name, name,
176 			sizeof(ifr.ifr_name));
177 		if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
178 			close(s);
179 			return(0);
180 		}
181 		close(s);
182 
183 		mtu = ifr.ifr_mtu;
184 	}
185 #endif
186 
187 	return(mtu);
188 }
189 
190 /* give interface index and its old flags, then new flags returned */
191 int
192 if_getflags(int ifindex, int oifflags)
193 {
194 	struct ifreq ifr;
195 	int s;
196 
197 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
198 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
199 		       strerror(errno));
200 		return (oifflags & ~IFF_UP);
201 	}
202 
203 	if_indextoname(ifindex, ifr.ifr_name);
204 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
205 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
206 		       __FUNCTION__, ifr.ifr_name);
207 		close(s);
208 		return (oifflags & ~IFF_UP);
209 	}
210 	close(s);
211 	return (ifr.ifr_flags);
212 }
213 
214 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
215 int
216 lladdropt_length(struct sockaddr_dl *sdl)
217 {
218 	switch (sdl->sdl_type) {
219 	case IFT_ETHER:
220 		return(ROUNDUP8(ETHER_ADDR_LEN + 2));
221 	default:
222 		return(0);
223 	}
224 }
225 
226 void
227 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
228 {
229 	char *addr;
230 
231 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
232 
233 	switch (sdl->sdl_type) {
234 	case IFT_ETHER:
235 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
236 		addr = (char *)(ndopt + 1);
237 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
238 		break;
239 	default:
240 		syslog(LOG_ERR, "<%s> unsupported link type(%d)",
241 		    __FUNCTION__, sdl->sdl_type);
242 		exit(1);
243 	}
244 
245 	return;
246 }
247 
248 int
249 rtbuf_len()
250 {
251 	size_t len;
252 
253 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
254 
255 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
256 		return(-1);
257 
258 	return(len);
259 }
260 
261 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
262 #define SIN6(s) ((struct sockaddr_in6 *)(s))
263 #define SDL(s) ((struct sockaddr_dl *)(s))
264 char *
265 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
266 {
267 	struct rt_msghdr *rtm;
268 	struct ifa_msghdr *ifam;
269 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
270 
271 	*lenp = 0;
272 	for (rtm = (struct rt_msghdr *)buf;
273 	     rtm < (struct rt_msghdr *)lim;
274 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
275 		/* just for safety */
276 		if (!rtm->rtm_msglen) {
277 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
278 				"(buf=%p lim=%p rtm=%p)", __FUNCTION__,
279 				buf, lim, rtm);
280 			break;
281 		}
282 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
283 			continue;
284 		}
285 
286 		switch (rtm->rtm_type) {
287 		case RTM_GET:
288 		case RTM_ADD:
289 		case RTM_DELETE:
290 			/* address related checks */
291 			sa = (struct sockaddr *)(rtm + 1);
292 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
293 			if ((dst = rti_info[RTAX_DST]) == NULL ||
294 			    dst->sa_family != AF_INET6)
295 				continue;
296 
297 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
298 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
299 				continue;
300 
301 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
302 			    gw->sa_family != AF_LINK)
303 				continue;
304 			if (ifindex && SDL(gw)->sdl_index != ifindex)
305 				continue;
306 
307 			if (rti_info[RTAX_NETMASK] == NULL)
308 				continue;
309 
310 			/* found */
311 			*lenp = rtm->rtm_msglen;
312 			return (char *)rtm;
313 			/* NOTREACHED */
314 		case RTM_NEWADDR:
315 		case RTM_DELADDR:
316 			ifam = (struct ifa_msghdr *)rtm;
317 
318 			/* address related checks */
319 			sa = (struct sockaddr *)(ifam + 1);
320 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
321 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
322 			    (ifa->sa_family != AF_INET &&
323 			     ifa->sa_family != AF_INET6))
324 				continue;
325 
326 			if (ifa->sa_family == AF_INET6 &&
327 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
328 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
329 				continue;
330 
331 			if (ifindex && ifam->ifam_index != ifindex)
332 				continue;
333 
334 			/* found */
335 			*lenp = ifam->ifam_msglen;
336 			return (char *)rtm;
337 			/* NOTREACHED */
338 		case RTM_IFINFO:
339 			/* found */
340 			*lenp = rtm->rtm_msglen;
341 			return (char *)rtm;
342 			/* NOTREACHED */
343 		}
344 	}
345 
346 	return (char *)rtm;
347 }
348 #undef FILTER_MATCH
349 
350 struct in6_addr *
351 get_addr(char *buf)
352 {
353 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
354 	struct sockaddr *sa, *rti_info[RTAX_MAX];
355 
356 	sa = (struct sockaddr *)(rtm + 1);
357 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
358 
359 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
360 }
361 
362 int
363 get_rtm_ifindex(char *buf)
364 {
365 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
366 	struct sockaddr *sa, *rti_info[RTAX_MAX];
367 
368 	sa = (struct sockaddr *)(rtm + 1);
369 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
370 
371 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
372 }
373 
374 int
375 get_ifm_ifindex(char *buf)
376 {
377 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
378 
379 	return ((int)ifm->ifm_index);
380 }
381 
382 int
383 get_ifam_ifindex(char *buf)
384 {
385 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
386 
387 	return ((int)ifam->ifam_index);
388 }
389 
390 int
391 get_ifm_flags(char *buf)
392 {
393 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
394 
395 	return (ifm->ifm_flags);
396 }
397 
398 int
399 get_prefixlen(char *buf)
400 {
401 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
402 	struct sockaddr *sa, *rti_info[RTAX_MAX];
403 	u_char *p, *lim;
404 
405 	sa = (struct sockaddr *)(rtm + 1);
406 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
407 	sa = rti_info[RTAX_NETMASK];
408 
409 	p = (u_char *)(&SIN6(sa)->sin6_addr);
410 	lim = (u_char *)sa + sa->sa_len;
411 	return prefixlen(p, lim);
412 }
413 
414 int
415 prefixlen(u_char *p, u_char *lim)
416 {
417 	int masklen;
418 
419 	for (masklen = 0; p < lim; p++) {
420 		switch (*p) {
421 		case 0xff:
422 			masklen += 8;
423 			break;
424 		case 0xfe:
425 			masklen += 7;
426 			break;
427 		case 0xfc:
428 			masklen += 6;
429 			break;
430 		case 0xf8:
431 			masklen += 5;
432 			break;
433 		case 0xf0:
434 			masklen += 4;
435 			break;
436 		case 0xe0:
437 			masklen += 3;
438 			break;
439 		case 0xc0:
440 			masklen += 2;
441 			break;
442 		case 0x80:
443 			masklen += 1;
444 			break;
445 		case 0x00:
446 			break;
447 		default:
448 			return(-1);
449 		}
450 	}
451 
452 	return(masklen);
453 }
454 
455 int
456 rtmsg_type(char *buf)
457 {
458 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
459 
460 	return(rtm->rtm_type);
461 }
462 
463 int
464 rtmsg_len(char *buf)
465 {
466 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
467 
468 	return(rtm->rtm_msglen);
469 }
470 
471 int
472 ifmsg_len(char *buf)
473 {
474 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
475 
476 	return(ifm->ifm_msglen);
477 }
478 
479 /*
480  * alloc buffer and get if_msghdrs block from kernel,
481  * and put them into the buffer
482  */
483 static void
484 get_iflist(char **buf, size_t *size)
485 {
486 	int mib[6];
487 
488 	mib[0] = CTL_NET;
489 	mib[1] = PF_ROUTE;
490 	mib[2] = 0;
491 	mib[3] = AF_INET6;
492 	mib[4] = NET_RT_IFLIST;
493 	mib[5] = 0;
494 
495 	if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
496 		syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
497 		       __FUNCTION__);
498 		exit(1);
499 	}
500 	if ((*buf = malloc(*size)) == NULL) {
501 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
502 		exit(1);
503 	}
504 	if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
505 		syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
506 		       __FUNCTION__);
507 		exit(1);
508 	}
509 	return;
510 }
511 
512 /*
513  * alloc buffer and parse if_msghdrs block passed as arg,
514  * and init the buffer as list of pointers ot each of the if_msghdr.
515  */
516 static void
517 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
518 {
519 	int iflentry_size, malloc_size;
520 	struct if_msghdr *ifm;
521 	struct ifa_msghdr *ifam;
522 	char *lim;
523 
524 	/*
525 	 * Estimate least size of an iflist entry, to be obtained from kernel.
526 	 * Should add sizeof(sockaddr) ??
527 	 */
528 	iflentry_size = sizeof(struct if_msghdr);
529 	/* roughly estimate max list size of pointers to each if_msghdr */
530 	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
531 	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
532 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
533 		exit(1);
534 	}
535 
536 	lim = buf + bufsize;
537 	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
538 		if (ifm->ifm_msglen == 0) {
539 			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
540 			       "(buf=%p lim=%p ifm=%p)", __FUNCTION__,
541 			       buf, lim, ifm);
542 			return;
543 		}
544 
545 		if (ifm->ifm_type == RTM_IFINFO) {
546 			(*ifmlist_p)[ifm->ifm_index] = ifm;
547 		} else {
548 			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
549 			       "expected %d, got %d\n msglen = %d\n"
550 			       "buf:%p, ifm:%p, lim:%p\n",
551 			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
552 			       buf, ifm, lim);
553 			exit (1);
554 		}
555 		for (ifam = (struct ifa_msghdr *)
556 			((char *)ifm + ifm->ifm_msglen);
557 		     ifam < (struct ifa_msghdr *)lim;
558 		     ifam = (struct ifa_msghdr *)
559 		     	((char *)ifam + ifam->ifam_msglen)) {
560 			/* just for safety */
561 			if (!ifam->ifam_msglen) {
562 				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
563 				       "(buf=%p lim=%p ifam=%p)", __FUNCTION__,
564 				       buf, lim, ifam);
565 				return;
566 			}
567 			if (ifam->ifam_type != RTM_NEWADDR)
568 				break;
569 		}
570 		ifm = (struct if_msghdr *)ifam;
571 	}
572 }
573 
574 void
575 init_iflist()
576 {
577 	if (ifblock) {
578 		free(ifblock);
579 		ifblock_size = 0;
580 	}
581 	if (iflist)
582 		free(iflist);
583 	/* get iflist block from kernel */
584 	get_iflist(&ifblock, &ifblock_size);
585 
586 	/* make list of pointers to each if_msghdr */
587 	parse_iflist(&iflist, ifblock, ifblock_size);
588 }
589