xref: /dragonfly/usr.sbin/rtadvd/if.c (revision c9f721c2)
1 /*	$KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/usr.sbin/rtadvd/if.c,v 1.2.2.3 2001/07/03 11:02:14 ume Exp $
32  * $DragonFly: src/usr.sbin/rtadvd/if.c,v 1.4 2004/02/10 02:59:43 rob Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/ioctl.h>
39 #include <net/if.h>
40 #include <net/if_types.h>
41 #ifdef __DragonFly__
42 # include <net/ethernet.h>
43 #endif
44 #include <ifaddrs.h>
45 #ifdef __NetBSD__
46 #include <net/if_ether.h>
47 #endif
48 #include <net/route.h>
49 #include <net/if_dl.h>
50 #include <netinet/in.h>
51 #include <netinet/icmp6.h>
52 #ifdef __bsdi__
53 # include <netinet/if_ether.h>
54 #endif
55 #ifdef __OpenBSD__
56 #include <netinet/if_ether.h>
57 #endif
58 #include <unistd.h>
59 #include <errno.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include "rtadvd.h"
64 #include "if.h"
65 
66 #define ROUNDUP(a, size) \
67 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
68 
69 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
70 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
71 						 sizeof(u_long)) :\
72 			  			 sizeof(u_long)))
73 
74 struct if_msghdr **iflist;
75 int iflist_init_ok;
76 size_t ifblock_size;
77 char *ifblock;
78 
79 static void get_iflist(char **buf, size_t *size);
80 static void parse_iflist(struct if_msghdr ***ifmlist_p, char *buf,
81 		       size_t bufsize);
82 
83 static void
84 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
85 {
86 	int i;
87 
88 	for (i = 0; i < RTAX_MAX; i++) {
89 		if (addrs & (1 << i)) {
90 			rti_info[i] = sa;
91 			NEXT_SA(sa);
92 		}
93 		else
94 			rti_info[i] = NULL;
95 	}
96 }
97 
98 struct sockaddr_dl *
99 if_nametosdl(char *name)
100 {
101 	int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
102 	char *buf, *next, *lim;
103 	size_t len;
104 	struct if_msghdr *ifm;
105 	struct sockaddr *sa, *rti_info[RTAX_MAX];
106 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
107 
108 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
109 		return(NULL);
110 	if ((buf = malloc(len)) == NULL)
111 		return(NULL);
112 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
113 		free(buf);
114 		return(NULL);
115 	}
116 
117 	lim = buf + len;
118 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
119 		ifm = (struct if_msghdr *)next;
120 		if (ifm->ifm_type == RTM_IFINFO) {
121 			sa = (struct sockaddr *)(ifm + 1);
122 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
123 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
124 				if (sa->sa_family == AF_LINK) {
125 					sdl = (struct sockaddr_dl *)sa;
126 					if (strlen(name) != sdl->sdl_nlen)
127 						continue; /* not same len */
128 					if (strncmp(&sdl->sdl_data[0],
129 						    name,
130 						    sdl->sdl_nlen) == 0) {
131 						break;
132 					}
133 				}
134 			}
135 		}
136 	}
137 	if (next == lim) {
138 		/* search failed */
139 		free(buf);
140 		return(NULL);
141 	}
142 
143 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
144 		return(NULL);
145 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
146 	return(ret_sdl);
147 }
148 
149 int
150 if_getmtu(char *name)
151 {
152 	struct ifaddrs *ifap, *ifa;
153 	struct if_data *ifd;
154 	u_long mtu = 0;
155 
156 	if (getifaddrs(&ifap) < 0)
157 		return(0);
158 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
159 		if (strcmp(ifa->ifa_name, name) == 0) {
160 			ifd = ifa->ifa_data;
161 			if (ifd)
162 				mtu = ifd->ifi_mtu;
163 			break;
164 		}
165 	}
166 	freeifaddrs(ifap);
167 
168 #ifdef SIOCGIFMTU		/* XXX: this ifdef may not be necessary */
169 	if (mtu == 0) {
170 		struct ifreq ifr;
171 		int s;
172 
173 		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
174 			return(0);
175 
176 		ifr.ifr_addr.sa_family = AF_INET6;
177 		strncpy(ifr.ifr_name, name,
178 			sizeof(ifr.ifr_name));
179 		if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
180 			close(s);
181 			return(0);
182 		}
183 		close(s);
184 
185 		mtu = ifr.ifr_mtu;
186 	}
187 #endif
188 
189 	return(mtu);
190 }
191 
192 /* give interface index and its old flags, then new flags returned */
193 int
194 if_getflags(int ifindex, int oifflags)
195 {
196 	struct ifreq ifr;
197 	int s;
198 
199 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
200 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
201 		       strerror(errno));
202 		return (oifflags & ~IFF_UP);
203 	}
204 
205 	if_indextoname(ifindex, ifr.ifr_name);
206 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
207 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
208 		       __FUNCTION__, ifr.ifr_name);
209 		close(s);
210 		return (oifflags & ~IFF_UP);
211 	}
212 	close(s);
213 	return (ifr.ifr_flags);
214 }
215 
216 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
217 int
218 lladdropt_length(struct sockaddr_dl *sdl)
219 {
220 	switch(sdl->sdl_type) {
221 	 case IFT_ETHER:
222 		 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
223 	 default:
224 		 return(0);
225 	}
226 }
227 
228 void
229 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
230 {
231 	char *addr;
232 
233 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
234 
235 	switch(sdl->sdl_type) {
236 	 case IFT_ETHER:
237 		 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
238 		 addr = (char *)(ndopt + 1);
239 		 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
240 		 break;
241 	 default:
242 		 syslog(LOG_ERR,
243 			"<%s> unsupported link type(%d)",
244 			__FUNCTION__, sdl->sdl_type);
245 		 exit(1);
246 	}
247 
248 	return;
249 }
250 
251 int
252 rtbuf_len()
253 {
254 	size_t len;
255 
256 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
257 
258 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
259 		return(-1);
260 
261 	return(len);
262 }
263 
264 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
265 #define SIN6(s) ((struct sockaddr_in6 *)(s))
266 #define SDL(s) ((struct sockaddr_dl *)(s))
267 char *
268 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
269 {
270 	struct rt_msghdr *rtm;
271 	struct ifa_msghdr *ifam;
272 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
273 
274 	*lenp = 0;
275 	for (rtm = (struct rt_msghdr *)buf;
276 	     rtm < (struct rt_msghdr *)lim;
277 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
278 		/* just for safety */
279 		if (!rtm->rtm_msglen) {
280 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
281 				"(buf=%p lim=%p rtm=%p)", __FUNCTION__,
282 				buf, lim, rtm);
283 			break;
284 		}
285 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
286 			continue;
287 		}
288 
289 		switch (rtm->rtm_type) {
290 		case RTM_GET:
291 		case RTM_ADD:
292 		case RTM_DELETE:
293 			/* address related checks */
294 			sa = (struct sockaddr *)(rtm + 1);
295 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
296 			if ((dst = rti_info[RTAX_DST]) == NULL ||
297 			    dst->sa_family != AF_INET6)
298 				continue;
299 
300 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
301 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
302 				continue;
303 
304 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
305 			    gw->sa_family != AF_LINK)
306 				continue;
307 			if (ifindex && SDL(gw)->sdl_index != ifindex)
308 				continue;
309 
310 			if (rti_info[RTAX_NETMASK] == NULL)
311 				continue;
312 
313 			/* found */
314 			*lenp = rtm->rtm_msglen;
315 			return (char *)rtm;
316 			/* NOTREACHED */
317 		case RTM_NEWADDR:
318 		case RTM_DELADDR:
319 			ifam = (struct ifa_msghdr *)rtm;
320 
321 			/* address related checks */
322 			sa = (struct sockaddr *)(ifam + 1);
323 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
324 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
325 			    (ifa->sa_family != AF_INET &&
326 			     ifa->sa_family != AF_INET6))
327 				continue;
328 
329 			if (ifa->sa_family == AF_INET6 &&
330 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
331 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
332 				continue;
333 
334 			if (ifindex && ifam->ifam_index != ifindex)
335 				continue;
336 
337 			/* found */
338 			*lenp = ifam->ifam_msglen;
339 			return (char *)rtm;
340 			/* NOTREACHED */
341 		case RTM_IFINFO:
342 			/* found */
343 			*lenp = rtm->rtm_msglen;
344 			return (char *)rtm;
345 			/* NOTREACHED */
346 		}
347 	}
348 
349 	return (char *)rtm;
350 }
351 #undef FILTER_MATCH
352 
353 struct in6_addr *
354 get_addr(char *buf)
355 {
356 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
357 	struct sockaddr *sa, *rti_info[RTAX_MAX];
358 
359 	sa = (struct sockaddr *)(rtm + 1);
360 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
361 
362 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
363 }
364 
365 int
366 get_rtm_ifindex(char *buf)
367 {
368 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
369 	struct sockaddr *sa, *rti_info[RTAX_MAX];
370 
371 	sa = (struct sockaddr *)(rtm + 1);
372 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
373 
374 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
375 }
376 
377 int
378 get_ifm_ifindex(char *buf)
379 {
380 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
381 
382 	return ((int)ifm->ifm_index);
383 }
384 
385 int
386 get_ifam_ifindex(char *buf)
387 {
388 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
389 
390 	return ((int)ifam->ifam_index);
391 }
392 
393 int
394 get_ifm_flags(char *buf)
395 {
396 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
397 
398 	return (ifm->ifm_flags);
399 }
400 
401 int
402 get_prefixlen(char *buf)
403 {
404 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
405 	struct sockaddr *sa, *rti_info[RTAX_MAX];
406 	u_char *p, *lim;
407 
408 	sa = (struct sockaddr *)(rtm + 1);
409 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
410 	sa = rti_info[RTAX_NETMASK];
411 
412 	p = (u_char *)(&SIN6(sa)->sin6_addr);
413 	lim = (u_char *)sa + sa->sa_len;
414 	return prefixlen(p, lim);
415 }
416 
417 int
418 prefixlen(u_char *p, u_char *lim)
419 {
420 	int masklen;
421 
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