Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / rtadvd / if.c
blobe489ad2410c756102b4d418e30b169f72c799b20
1 /* $NetBSD: if.c,v 1.17 2003/09/23 18:15:50 itojun Exp $ */
2 /* $KAME: if.c,v 1.36 2004/11/30 22:32:01 suz Exp $ */
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.
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.
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"
53 #define ROUNDUP(a, size) \
54 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
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)))
61 struct if_msghdr **iflist;
62 int iflist_init_ok;
63 size_t ifblock_size;
64 char *ifblock;
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));
70 static void
71 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
73 int i;
75 for (i = 0; i < RTAX_MAX; i++) {
76 if (addrs & (1 << i)) {
77 rti_info[i] = sa;
78 NEXT_SA(sa);
80 else
81 rti_info[i] = NULL;
85 struct sockaddr_dl *
86 if_nametosdl(char *name)
88 struct ifaddrs *ifap, *ifa;
89 struct sockaddr_dl *sdl;
91 if (getifaddrs(&ifap) != 0)
92 return (NULL);
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;
100 sdl = malloc(ifa->ifa_addr->sa_len);
101 if (!sdl)
102 continue; /*XXX*/
104 memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
105 freeifaddrs(ifap);
106 return (sdl);
109 freeifaddrs(ifap);
110 return (NULL);
114 if_getmtu(char *name)
116 struct ifaddrs *ifap, *ifa;
117 struct if_data *ifd;
118 u_long mtu = 0;
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;
130 freeifaddrs(ifap);
132 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
133 if (mtu == 0) {
134 struct ifreq ifr;
135 int s;
137 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
138 return(0);
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);
147 close(s);
149 mtu = ifr.ifr_mtu;
151 #endif
153 return(mtu);
156 /* give interface index and its old flags, then new flags returned */
158 if_getflags(int ifindex, int oifflags)
160 struct ifreq ifr;
161 int s;
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);
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);
176 close(s);
177 return (ifr.ifr_flags);
180 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
182 lladdropt_length(struct sockaddr_dl *sdl)
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);
193 void
194 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
196 char *addr;
198 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
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);
213 return;
216 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
217 #define SIN6(s) ((struct sockaddr_in6 *)(s))
218 #define SDL(s) ((struct sockaddr_dl *)(s))
219 char *
220 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
222 struct rt_msghdr *rtm;
223 struct ifa_msghdr *ifam;
224 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
226 *lenp = 0;
227 for (rtm = (struct rt_msghdr *)buf;
228 rtm < (struct rt_msghdr *)lim;
229 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
230 /* just for safety */
231 if (!rtm->rtm_msglen) {
232 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
233 "(buf=%p lim=%p rtm=%p)", __func__,
234 buf, lim, rtm);
235 break;
237 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
238 continue;
241 switch (rtm->rtm_type) {
242 case RTM_GET:
243 case RTM_ADD:
244 case RTM_DELETE:
245 /* address related checks */
246 sa = (struct sockaddr *)(rtm + 1);
247 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
248 if ((dst = rti_info[RTAX_DST]) == NULL ||
249 dst->sa_family != AF_INET6)
250 continue;
252 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
253 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
254 continue;
256 if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
257 gw->sa_family != AF_LINK)
258 continue;
259 if (ifindex && SDL(gw)->sdl_index != ifindex)
260 continue;
262 if (rti_info[RTAX_NETMASK] == NULL)
263 continue;
265 /* found */
266 *lenp = rtm->rtm_msglen;
267 return (char *)rtm;
268 /* NOTREACHED */
269 case RTM_NEWADDR:
270 case RTM_DELADDR:
271 ifam = (struct ifa_msghdr *)rtm;
273 /* address related checks */
274 sa = (struct sockaddr *)(ifam + 1);
275 get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
276 if ((ifa = rti_info[RTAX_IFA]) == NULL ||
277 (ifa->sa_family != AF_INET &&
278 ifa->sa_family != AF_INET6))
279 continue;
281 if (ifa->sa_family == AF_INET6 &&
282 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
283 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
284 continue;
286 if (ifindex && ifam->ifam_index != ifindex)
287 continue;
289 /* found */
290 *lenp = ifam->ifam_msglen;
291 return (char *)rtm;
292 /* NOTREACHED */
293 case RTM_IFINFO:
294 /* found */
295 *lenp = rtm->rtm_msglen;
296 return (char *)rtm;
297 /* NOTREACHED */
301 return (char *)rtm;
303 #undef FILTER_MATCH
305 struct in6_addr *
306 get_addr(char *buf)
308 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
309 struct sockaddr *sa, *rti_info[RTAX_MAX];
311 sa = (struct sockaddr *)(rtm + 1);
312 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
314 return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
318 get_rtm_ifindex(char *buf)
320 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
321 struct sockaddr *sa, *rti_info[RTAX_MAX];
323 sa = (struct sockaddr *)(rtm + 1);
324 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
326 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
330 get_ifm_ifindex(char *buf)
332 struct if_msghdr *ifm = (struct if_msghdr *)buf;
334 return ((int)ifm->ifm_index);
338 get_ifam_ifindex(char *buf)
340 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
342 return ((int)ifam->ifam_index);
346 get_ifm_flags(char *buf)
348 struct if_msghdr *ifm = (struct if_msghdr *)buf;
350 return (ifm->ifm_flags);
354 get_prefixlen(char *buf)
356 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
357 struct sockaddr *sa, *rti_info[RTAX_MAX];
358 u_char *p, *lim;
360 sa = (struct sockaddr *)(rtm + 1);
361 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
362 sa = rti_info[RTAX_NETMASK];
364 p = (u_char *)(&SIN6(sa)->sin6_addr);
365 lim = (u_char *)sa + sa->sa_len;
366 return prefixlen(p, lim);
370 prefixlen(u_char *p, u_char *lim)
372 int masklen;
374 for (masklen = 0; p < lim; p++) {
375 switch (*p) {
376 case 0xff:
377 masklen += 8;
378 break;
379 case 0xfe:
380 masklen += 7;
381 break;
382 case 0xfc:
383 masklen += 6;
384 break;
385 case 0xf8:
386 masklen += 5;
387 break;
388 case 0xf0:
389 masklen += 4;
390 break;
391 case 0xe0:
392 masklen += 3;
393 break;
394 case 0xc0:
395 masklen += 2;
396 break;
397 case 0x80:
398 masklen += 1;
399 break;
400 case 0x00:
401 break;
402 default:
403 return(-1);
407 return(masklen);
411 rtmsg_type(char *buf)
413 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
415 return(rtm->rtm_type);
419 rtmsg_len(char *buf)
421 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
423 return(rtm->rtm_msglen);
427 ifmsg_len(char *buf)
429 struct if_msghdr *ifm = (struct if_msghdr *)buf;
431 return(ifm->ifm_msglen);
435 * alloc buffer and get if_msghdrs block from kernel,
436 * and put them into the buffer
438 static void
439 get_iflist(char **buf, size_t *size)
441 int mib[6];
443 mib[0] = CTL_NET;
444 mib[1] = PF_ROUTE;
445 mib[2] = 0;
446 mib[3] = AF_INET6;
447 mib[4] = NET_RT_IFLIST;
448 mib[5] = 0;
450 if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
451 syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
452 __func__);
453 exit(1);
455 if ((*buf = malloc(*size)) == NULL) {
456 syslog(LOG_ERR, "<%s> malloc failed", __func__);
457 exit(1);
459 if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
460 syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
461 __func__);
462 exit(1);
464 return;
468 * alloc buffer and parse if_msghdrs block passed as arg,
469 * and init the buffer as list of pointers ot each of the if_msghdr.
471 static void
472 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
474 int iflentry_size, malloc_size;
475 struct if_msghdr *ifm;
476 struct ifa_msghdr *ifam;
477 char *lim;
480 * Estimate least size of an iflist entry, to be obtained from kernel.
481 * Should add sizeof(sockaddr) ??
483 iflentry_size = sizeof(struct if_msghdr);
484 /* roughly estimate max list size of pointers to each if_msghdr */
485 malloc_size = (bufsize/iflentry_size) * sizeof(void *);
486 if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
487 syslog(LOG_ERR, "<%s> malloc failed", __func__);
488 exit(1);
491 lim = buf + bufsize;
492 for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
493 if (ifm->ifm_msglen == 0) {
494 syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
495 "(buf=%p lim=%p ifm=%p)", __func__,
496 buf, lim, ifm);
497 return;
500 if (ifm->ifm_type == RTM_IFINFO) {
501 (*ifmlist_p)[ifm->ifm_index] = ifm;
502 } else {
503 syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
504 "expected %d, got %d\n msglen = %d\n"
505 "buf:%p, ifm:%p, lim:%p\n",
506 RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
507 buf, ifm, lim);
508 exit (1);
510 for (ifam = (struct ifa_msghdr *)
511 ((char *)ifm + ifm->ifm_msglen);
512 ifam < (struct ifa_msghdr *)lim;
513 ifam = (struct ifa_msghdr *)
514 ((char *)ifam + ifam->ifam_msglen)) {
515 /* just for safety */
516 if (!ifam->ifam_msglen) {
517 syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
518 "(buf=%p lim=%p ifam=%p)", __func__,
519 buf, lim, ifam);
520 return;
522 if (ifam->ifam_type != RTM_NEWADDR)
523 break;
525 ifm = (struct if_msghdr *)ifam;
529 void
530 init_iflist()
532 if (ifblock) {
533 free(ifblock);
534 ifblock_size = 0;
536 if (iflist)
537 free(iflist);
538 /* get iflist block from kernel */
539 get_iflist(&ifblock, &ifblock_size);
541 /* make list of pointers to each if_msghdr */
542 parse_iflist(&iflist, ifblock, ifblock_size);