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