xref: /netbsd-src/usr.sbin/rtadvd/if.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if.c,v 1.6 2000/07/06 12:37:56 itojun Exp $	*/
2 /*	$KAME: if.c,v 1.11 2000/07/06 08:20:04 jinmei 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 #if 0
151 	struct ifreq ifr;
152 	int s;
153 
154 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
155 		return(0);
156 
157 	ifr.ifr_addr.sa_family = AF_INET6;
158 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159 	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
160 		close(s);
161 		return(0);
162 	}
163 
164 	close(s);
165 
166 	return(ifr.ifr_mtu);
167 #else
168 	struct ifaddrs *ifap, *ifa;
169 	struct if_data *ifd;
170 
171 	if (getifaddrs(&ifap) < 0)
172 		return(0);
173 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
174 		if (strcmp(ifa->ifa_name, name) == 0) {
175 			ifd = ifa->ifa_data;
176 			freeifaddrs(ifap);
177 			if (ifd)
178 				return ifd->ifi_mtu;
179 			else
180 				return 0;
181 		}
182 	}
183 	freeifaddrs(ifap);
184 	return 0;
185 #endif
186 }
187 
188 /* give interface index and its old flags, then new flags returned */
189 int
190 if_getflags(int ifindex, int oifflags)
191 {
192 	struct ifreq ifr;
193 	int s;
194 
195 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
196 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
197 		       strerror(errno));
198 		return (oifflags & ~IFF_UP);
199 	}
200 
201 	if_indextoname(ifindex, ifr.ifr_name);
202 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
203 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
204 		       __FUNCTION__, ifr.ifr_name);
205 		close(s);
206 		return (oifflags & ~IFF_UP);
207 	}
208 	close(s);
209 	return (ifr.ifr_flags);
210 }
211 
212 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
213 int
214 lladdropt_length(struct sockaddr_dl *sdl)
215 {
216 	switch(sdl->sdl_type) {
217 	 case IFT_ETHER:
218 		 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
219 	 default:
220 		 return(0);
221 	}
222 }
223 
224 void
225 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
226 {
227 	char *addr;
228 
229 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
230 
231 	switch(sdl->sdl_type) {
232 	 case IFT_ETHER:
233 		 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
234 		 addr = (char *)(ndopt + 1);
235 		 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
236 		 break;
237 	 default:
238 		 syslog(LOG_ERR,
239 			"<%s> unsupported link type(%d)",
240 			__FUNCTION__, sdl->sdl_type);
241 		 exit(1);
242 	}
243 
244 	return;
245 }
246 
247 int
248 rtbuf_len()
249 {
250 	size_t len;
251 
252 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
253 
254 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
255 		return(-1);
256 
257 	return(len);
258 }
259 
260 int
261 get_rtinfo(char *buf, size_t *len)
262 {
263 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
264 
265 	if (sysctl(mib, 6, buf, len, NULL, 0) < 0)
266 		return(-1);
267 
268 	return(0);
269 }
270 
271 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
272 #define SIN6(s) ((struct sockaddr_in6 *)(s))
273 #define SDL(s) ((struct sockaddr_dl *)(s))
274 char *
275 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
276 {
277 	struct rt_msghdr *rtm;
278 	struct ifa_msghdr *ifam;
279 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
280 
281 	*lenp = 0;
282 	for (rtm = (struct rt_msghdr *)buf;
283 	     rtm < (struct rt_msghdr *)lim;
284 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
285 		/* just for safety */
286 		if (!rtm->rtm_msglen) {
287 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
288 				"(buf=%p lim=%p rtm=%p)", __FUNCTION__,
289 				buf, lim, rtm);
290 			break;
291 		}
292 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
293 			continue;
294 		}
295 
296 		switch (rtm->rtm_type) {
297 		case RTM_GET:
298 		case RTM_ADD:
299 		case RTM_DELETE:
300 			/* address related checks */
301 			sa = (struct sockaddr *)(rtm + 1);
302 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
303 			if ((dst = rti_info[RTAX_DST]) == NULL ||
304 			    dst->sa_family != AF_INET6)
305 				continue;
306 
307 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
308 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
309 				continue;
310 
311 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
312 			    gw->sa_family != AF_LINK)
313 				continue;
314 			if (ifindex && SDL(gw)->sdl_index != ifindex)
315 				continue;
316 
317 			if (rti_info[RTAX_NETMASK] == NULL)
318 				continue;
319 
320 			/* found */
321 			*lenp = rtm->rtm_msglen;
322 			return (char *)rtm;
323 			/* NOTREACHED */
324 		case RTM_NEWADDR:
325 		case RTM_DELADDR:
326 			ifam = (struct ifa_msghdr *)rtm;
327 
328 			/* address related checks */
329 			sa = (struct sockaddr *)(ifam + 1);
330 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
331 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
332 			    (ifa->sa_family != AF_INET &&
333 			     ifa->sa_family != AF_INET6))
334 				continue;
335 
336 			if (ifa->sa_family == AF_INET6 &&
337 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
338 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
339 				continue;
340 
341 			if (ifindex && ifam->ifam_index != ifindex)
342 				continue;
343 
344 			/* found */
345 			*lenp = ifam->ifam_msglen;
346 			return (char *)rtm;
347 			/* NOTREACHED */
348 		case RTM_IFINFO:
349 			/* found */
350 			*lenp = rtm->rtm_msglen;
351 			return (char *)rtm;
352 			/* NOTREACHED */
353 		}
354 	}
355 
356 	return (char *)rtm;
357 }
358 #undef FILTER_MATCH(type, filter)
359 
360 struct in6_addr *
361 get_addr(char *buf)
362 {
363 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
364 	struct sockaddr *sa, *rti_info[RTAX_MAX];
365 
366 	sa = (struct sockaddr *)(rtm + 1);
367 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
368 
369 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
370 }
371 
372 int
373 get_rtm_ifindex(char *buf)
374 {
375 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
376 	struct sockaddr *sa, *rti_info[RTAX_MAX];
377 
378 	sa = (struct sockaddr *)(rtm + 1);
379 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
380 
381 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
382 }
383 
384 int
385 get_ifm_ifindex(char *buf)
386 {
387 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
388 
389 	return ((int)ifm->ifm_index);
390 }
391 
392 int
393 get_ifam_ifindex(char *buf)
394 {
395 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
396 
397 	return ((int)ifam->ifam_index);
398 }
399 
400 int
401 get_ifm_flags(char *buf)
402 {
403 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
404 
405 	return (ifm->ifm_flags);
406 }
407 
408 int
409 get_prefixlen(char *buf)
410 {
411 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
412 	struct sockaddr *sa, *rti_info[RTAX_MAX];
413 	int masklen;
414 	u_char *p, *lim;
415 
416 	sa = (struct sockaddr *)(rtm + 1);
417 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
418 	sa = rti_info[RTAX_NETMASK];
419 
420 	p = (u_char *)(&SIN6(sa)->sin6_addr);
421 	lim = (u_char *)sa + sa->sa_len;
422 	for (masklen = 0; p < lim; p++) {
423 		switch (*p) {
424 		case 0xff:
425 			masklen += 8;
426 			break;
427 		case 0xfe:
428 			masklen += 7;
429 			break;
430 		case 0xfc:
431 			masklen += 6;
432 			break;
433 		case 0xf8:
434 			masklen += 5;
435 			break;
436 		case 0xf0:
437 			masklen += 4;
438 			break;
439 		case 0xe0:
440 			masklen += 3;
441 			break;
442 		case 0xc0:
443 			masklen += 2;
444 			break;
445 		case 0x80:
446 			masklen += 1;
447 			break;
448 		case 0x00:
449 			break;
450 		default:
451 			return(-1);
452 		}
453 	}
454 
455 	return(masklen);
456 }
457 
458 int
459 rtmsg_type(char *buf)
460 {
461 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
462 
463 	return(rtm->rtm_type);
464 }
465 
466 int
467 rtmsg_len(char *buf)
468 {
469 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
470 
471 	return(rtm->rtm_msglen);
472 }
473 
474 int
475 ifmsg_len(char *buf)
476 {
477 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
478 
479 	return(ifm->ifm_msglen);
480 }
481 
482 /*
483  * alloc buffer and get if_msghdrs block from kernel,
484  * and put them into the buffer
485  */
486 static void
487 get_iflist(char **buf, size_t *size)
488 {
489 	int mib[6];
490 
491 	mib[0] = CTL_NET;
492 	mib[1] = PF_ROUTE;
493 	mib[2] = 0;
494 	mib[3] = AF_INET6;
495 	mib[4] = NET_RT_IFLIST;
496 	mib[5] = 0;
497 
498 	if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
499 		syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
500 		       __FUNCTION__);
501 		exit(1);
502 	}
503 	if ((*buf = malloc(*size)) == NULL) {
504 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
505 		exit(1);
506 	}
507 	if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
508 		syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
509 		       __FUNCTION__);
510 		exit(1);
511 	}
512 	return;
513 }
514 
515 /*
516  * alloc buffer and parse if_msghdrs block passed as arg,
517  * and init the buffer as list of pointers ot each of the if_msghdr.
518  */
519 static void
520 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
521 {
522 	int iflentry_size, malloc_size;
523 	struct if_msghdr *ifm;
524 	struct ifa_msghdr *ifam;
525 	char *lim;
526 
527 	/*
528 	 * Estimate least size of an iflist entry, to be obtained from kernel.
529 	 * Should add sizeof(sockaddr) ??
530 	 */
531 	iflentry_size = sizeof(struct if_msghdr);
532 	/* roughly estimate max list size of pointers to each if_msghdr */
533 	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
534 	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
535 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
536 		exit(1);
537 	}
538 
539 	lim = buf + bufsize;
540 	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
541 		if (ifm->ifm_msglen == 0) {
542 			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
543 			       "(buf=%p lim=%p ifm=%p)", __FUNCTION__,
544 			       buf, lim, ifm);
545 			return;
546 		}
547 
548 		if (ifm->ifm_type == RTM_IFINFO) {
549 			(*ifmlist_p)[ifm->ifm_index] = ifm;
550 		} else {
551 			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
552 			       "expected %d, got %d\n msglen = %d\n"
553 			       "buf:%p, ifm:%p, lim:%p\n",
554 			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
555 			       buf, ifm, lim);
556 			exit (1);
557 		}
558 		for (ifam = (struct ifa_msghdr *)
559 			((char *)ifm + ifm->ifm_msglen);
560 		     ifam < (struct ifa_msghdr *)lim;
561 		     ifam = (struct ifa_msghdr *)
562 		     	((char *)ifam + ifam->ifam_msglen)) {
563 			/* just for safety */
564 			if (!ifam->ifam_msglen) {
565 				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
566 				       "(buf=%p lim=%p ifam=%p)", __FUNCTION__,
567 				       buf, lim, ifam);
568 				return;
569 			}
570 			if (ifam->ifam_type != RTM_NEWADDR)
571 				break;
572 		}
573 		ifm = (struct if_msghdr *)ifam;
574 	}
575 }
576 
577 void
578 init_iflist()
579 {
580 	if (ifblock) {
581 		free(ifblock);
582 		ifblock_size = 0;
583 	}
584 	if (iflist)
585 		free(iflist);
586 	/* get iflist block from kernel */
587 	get_iflist(&ifblock, &ifblock_size);
588 
589 	/* make list of pointers to each if_msghdr */
590 	parse_iflist(&iflist, ifblock, ifblock_size);
591 }
592