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