xref: /netbsd-src/usr.sbin/rtadvd/if.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: if.c,v 1.17 2003/09/23 18:15:50 itojun Exp $	*/
2 /*	$KAME: if.c,v 1.31 2003/09/23 10:58:20 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 #include <ifaddrs.h>
40 #include <net/if_ether.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/icmp6.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <syslog.h>
50 #include "rtadvd.h"
51 #include "if.h"
52 
53 #define ROUNDUP(a, size) \
54 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
55 
56 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
57 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
58 						 sizeof(u_long)) :\
59 			  			 sizeof(u_long)))
60 
61 struct if_msghdr **iflist;
62 int iflist_init_ok;
63 size_t ifblock_size;
64 char *ifblock;
65 
66 static void get_iflist __P((char **buf, size_t *size));
67 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
68 		       size_t bufsize));
69 
70 static void
71 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
72 {
73 	int i;
74 
75 	for (i = 0; i < RTAX_MAX; i++) {
76 		if (addrs & (1 << i)) {
77 			rti_info[i] = sa;
78 			NEXT_SA(sa);
79 		}
80 		else
81 			rti_info[i] = NULL;
82 	}
83 }
84 
85 struct sockaddr_dl *
86 if_nametosdl(char *name)
87 {
88 	struct ifaddrs *ifap, *ifa;
89 	struct sockaddr_dl *sdl;
90 
91 	if (getifaddrs(&ifap) != 0)
92 		return (NULL);
93 
94 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
95 		if (strcmp(ifa->ifa_name, name) != 0)
96 			continue;
97 		if (ifa->ifa_addr->sa_family != AF_LINK)
98 			continue;
99 
100 		sdl = malloc(ifa->ifa_addr->sa_len);
101 		if (!sdl)
102 			continue;	/*XXX*/
103 
104 		memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
105 		freeifaddrs(ifap);
106 		return (sdl);
107 	}
108 
109 	freeifaddrs(ifap);
110 	return (NULL);
111 }
112 
113 int
114 if_getmtu(char *name)
115 {
116 	struct ifaddrs *ifap, *ifa;
117 	struct if_data *ifd;
118 	u_long mtu = 0;
119 
120 	if (getifaddrs(&ifap) < 0)
121 		return(0);
122 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
123 		if (strcmp(ifa->ifa_name, name) == 0) {
124 			ifd = ifa->ifa_data;
125 			if (ifd)
126 				mtu = ifd->ifi_mtu;
127 			break;
128 		}
129 	}
130 	freeifaddrs(ifap);
131 
132 #ifdef SIOCGIFMTU		/* XXX: this ifdef may not be necessary */
133 	if (mtu == 0) {
134 		struct ifreq ifr;
135 		int s;
136 
137 		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
138 			return(0);
139 
140 		ifr.ifr_addr.sa_family = AF_INET6;
141 		strncpy(ifr.ifr_name, name,
142 			sizeof(ifr.ifr_name));
143 		if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
144 			close(s);
145 			return(0);
146 		}
147 		close(s);
148 
149 		mtu = ifr.ifr_mtu;
150 	}
151 #endif
152 
153 	return(mtu);
154 }
155 
156 /* give interface index and its old flags, then new flags returned */
157 int
158 if_getflags(int ifindex, int oifflags)
159 {
160 	struct ifreq ifr;
161 	int s;
162 
163 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
164 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
165 		       strerror(errno));
166 		return (oifflags & ~IFF_UP);
167 	}
168 
169 	if_indextoname(ifindex, ifr.ifr_name);
170 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
171 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
172 		       __func__, ifr.ifr_name);
173 		close(s);
174 		return (oifflags & ~IFF_UP);
175 	}
176 	close(s);
177 	return (ifr.ifr_flags);
178 }
179 
180 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
181 int
182 lladdropt_length(struct sockaddr_dl *sdl)
183 {
184 	switch (sdl->sdl_type) {
185 	case IFT_ETHER:
186 	case IFT_FDDI:
187 		return(ROUNDUP8(ETHER_ADDR_LEN + 2));
188 	default:
189 		return(0);
190 	}
191 }
192 
193 void
194 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
195 {
196 	char *addr;
197 
198 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
199 
200 	switch (sdl->sdl_type) {
201 	case IFT_ETHER:
202 	case IFT_FDDI:
203 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
204 		addr = (char *)(ndopt + 1);
205 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
206 		break;
207 	default:
208 		syslog(LOG_ERR, "<%s> unsupported link type(%d)",
209 		    __func__, sdl->sdl_type);
210 		exit(1);
211 	}
212 
213 	return;
214 }
215 
216 int
217 rtbuf_len()
218 {
219 	size_t len;
220 
221 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
222 
223 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
224 		return(-1);
225 
226 	return(len);
227 }
228 
229 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
230 #define SIN6(s) ((struct sockaddr_in6 *)(s))
231 #define SDL(s) ((struct sockaddr_dl *)(s))
232 char *
233 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
234 {
235 	struct rt_msghdr *rtm;
236 	struct ifa_msghdr *ifam;
237 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
238 
239 	*lenp = 0;
240 	for (rtm = (struct rt_msghdr *)buf;
241 	     rtm < (struct rt_msghdr *)lim;
242 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
243 		/* just for safety */
244 		if (!rtm->rtm_msglen) {
245 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
246 				"(buf=%p lim=%p rtm=%p)", __func__,
247 				buf, lim, rtm);
248 			break;
249 		}
250 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
251 			continue;
252 		}
253 
254 		switch (rtm->rtm_type) {
255 		case RTM_GET:
256 		case RTM_ADD:
257 		case RTM_DELETE:
258 			/* address related checks */
259 			sa = (struct sockaddr *)(rtm + 1);
260 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
261 			if ((dst = rti_info[RTAX_DST]) == NULL ||
262 			    dst->sa_family != AF_INET6)
263 				continue;
264 
265 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
266 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
267 				continue;
268 
269 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
270 			    gw->sa_family != AF_LINK)
271 				continue;
272 			if (ifindex && SDL(gw)->sdl_index != ifindex)
273 				continue;
274 
275 			if (rti_info[RTAX_NETMASK] == NULL)
276 				continue;
277 
278 			/* found */
279 			*lenp = rtm->rtm_msglen;
280 			return (char *)rtm;
281 			/* NOTREACHED */
282 		case RTM_NEWADDR:
283 		case RTM_DELADDR:
284 			ifam = (struct ifa_msghdr *)rtm;
285 
286 			/* address related checks */
287 			sa = (struct sockaddr *)(ifam + 1);
288 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
289 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
290 			    (ifa->sa_family != AF_INET &&
291 			     ifa->sa_family != AF_INET6))
292 				continue;
293 
294 			if (ifa->sa_family == AF_INET6 &&
295 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
296 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
297 				continue;
298 
299 			if (ifindex && ifam->ifam_index != ifindex)
300 				continue;
301 
302 			/* found */
303 			*lenp = ifam->ifam_msglen;
304 			return (char *)rtm;
305 			/* NOTREACHED */
306 		case RTM_IFINFO:
307 			/* found */
308 			*lenp = rtm->rtm_msglen;
309 			return (char *)rtm;
310 			/* NOTREACHED */
311 		}
312 	}
313 
314 	return (char *)rtm;
315 }
316 #undef FILTER_MATCH
317 
318 struct in6_addr *
319 get_addr(char *buf)
320 {
321 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
322 	struct sockaddr *sa, *rti_info[RTAX_MAX];
323 
324 	sa = (struct sockaddr *)(rtm + 1);
325 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
326 
327 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
328 }
329 
330 int
331 get_rtm_ifindex(char *buf)
332 {
333 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
334 	struct sockaddr *sa, *rti_info[RTAX_MAX];
335 
336 	sa = (struct sockaddr *)(rtm + 1);
337 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
338 
339 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
340 }
341 
342 int
343 get_ifm_ifindex(char *buf)
344 {
345 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
346 
347 	return ((int)ifm->ifm_index);
348 }
349 
350 int
351 get_ifam_ifindex(char *buf)
352 {
353 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
354 
355 	return ((int)ifam->ifam_index);
356 }
357 
358 int
359 get_ifm_flags(char *buf)
360 {
361 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
362 
363 	return (ifm->ifm_flags);
364 }
365 
366 int
367 get_prefixlen(char *buf)
368 {
369 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
370 	struct sockaddr *sa, *rti_info[RTAX_MAX];
371 	u_char *p, *lim;
372 
373 	sa = (struct sockaddr *)(rtm + 1);
374 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
375 	sa = rti_info[RTAX_NETMASK];
376 
377 	p = (u_char *)(&SIN6(sa)->sin6_addr);
378 	lim = (u_char *)sa + sa->sa_len;
379 	return prefixlen(p, lim);
380 }
381 
382 int
383 prefixlen(u_char *p, u_char *lim)
384 {
385 	int masklen;
386 
387 	for (masklen = 0; p < lim; p++) {
388 		switch (*p) {
389 		case 0xff:
390 			masklen += 8;
391 			break;
392 		case 0xfe:
393 			masklen += 7;
394 			break;
395 		case 0xfc:
396 			masklen += 6;
397 			break;
398 		case 0xf8:
399 			masklen += 5;
400 			break;
401 		case 0xf0:
402 			masklen += 4;
403 			break;
404 		case 0xe0:
405 			masklen += 3;
406 			break;
407 		case 0xc0:
408 			masklen += 2;
409 			break;
410 		case 0x80:
411 			masklen += 1;
412 			break;
413 		case 0x00:
414 			break;
415 		default:
416 			return(-1);
417 		}
418 	}
419 
420 	return(masklen);
421 }
422 
423 int
424 rtmsg_type(char *buf)
425 {
426 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
427 
428 	return(rtm->rtm_type);
429 }
430 
431 int
432 rtmsg_len(char *buf)
433 {
434 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
435 
436 	return(rtm->rtm_msglen);
437 }
438 
439 int
440 ifmsg_len(char *buf)
441 {
442 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
443 
444 	return(ifm->ifm_msglen);
445 }
446 
447 /*
448  * alloc buffer and get if_msghdrs block from kernel,
449  * and put them into the buffer
450  */
451 static void
452 get_iflist(char **buf, size_t *size)
453 {
454 	int mib[6];
455 
456 	mib[0] = CTL_NET;
457 	mib[1] = PF_ROUTE;
458 	mib[2] = 0;
459 	mib[3] = AF_INET6;
460 	mib[4] = NET_RT_IFLIST;
461 	mib[5] = 0;
462 
463 	if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
464 		syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
465 		       __func__);
466 		exit(1);
467 	}
468 	if ((*buf = malloc(*size)) == NULL) {
469 		syslog(LOG_ERR, "<%s> malloc failed", __func__);
470 		exit(1);
471 	}
472 	if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
473 		syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
474 		       __func__);
475 		exit(1);
476 	}
477 	return;
478 }
479 
480 /*
481  * alloc buffer and parse if_msghdrs block passed as arg,
482  * and init the buffer as list of pointers ot each of the if_msghdr.
483  */
484 static void
485 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
486 {
487 	int iflentry_size, malloc_size;
488 	struct if_msghdr *ifm;
489 	struct ifa_msghdr *ifam;
490 	char *lim;
491 
492 	/*
493 	 * Estimate least size of an iflist entry, to be obtained from kernel.
494 	 * Should add sizeof(sockaddr) ??
495 	 */
496 	iflentry_size = sizeof(struct if_msghdr);
497 	/* roughly estimate max list size of pointers to each if_msghdr */
498 	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
499 	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
500 		syslog(LOG_ERR, "<%s> malloc failed", __func__);
501 		exit(1);
502 	}
503 
504 	lim = buf + bufsize;
505 	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
506 		if (ifm->ifm_msglen == 0) {
507 			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
508 			       "(buf=%p lim=%p ifm=%p)", __func__,
509 			       buf, lim, ifm);
510 			return;
511 		}
512 
513 		if (ifm->ifm_type == RTM_IFINFO) {
514 			(*ifmlist_p)[ifm->ifm_index] = ifm;
515 		} else {
516 			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
517 			       "expected %d, got %d\n msglen = %d\n"
518 			       "buf:%p, ifm:%p, lim:%p\n",
519 			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
520 			       buf, ifm, lim);
521 			exit (1);
522 		}
523 		for (ifam = (struct ifa_msghdr *)
524 			((char *)ifm + ifm->ifm_msglen);
525 		     ifam < (struct ifa_msghdr *)lim;
526 		     ifam = (struct ifa_msghdr *)
527 		     	((char *)ifam + ifam->ifam_msglen)) {
528 			/* just for safety */
529 			if (!ifam->ifam_msglen) {
530 				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
531 				       "(buf=%p lim=%p ifam=%p)", __func__,
532 				       buf, lim, ifam);
533 				return;
534 			}
535 			if (ifam->ifam_type != RTM_NEWADDR)
536 				break;
537 		}
538 		ifm = (struct if_msghdr *)ifam;
539 	}
540 }
541 
542 void
543 init_iflist()
544 {
545 	if (ifblock) {
546 		free(ifblock);
547 		ifblock_size = 0;
548 	}
549 	if (iflist)
550 		free(iflist);
551 	/* get iflist block from kernel */
552 	get_iflist(&ifblock, &ifblock_size);
553 
554 	/* make list of pointers to each if_msghdr */
555 	parse_iflist(&iflist, ifblock, ifblock_size);
556 }
557