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