]> git.saurik.com Git - apple/network_cmds.git/blob - rtadvd.tproj/if.c
network_cmds-245.17.tar.gz
[apple/network_cmds.git] / rtadvd.tproj / if.c
1 /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: src/usr.sbin/rtadvd/if.c,v 1.2.2.3 2001/07/03 11:02:14 ume Exp $
32 */
33
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <net/if_types.h>
40 #if defined(__FreeBSD__) || defined (__APPLE__)
41 # include <net/ethernet.h>
42 #endif
43 #include <ifaddrs.h>
44 #ifdef __NetBSD__
45 #include <net/if_ether.h>
46 #endif
47 #include <net/route.h>
48 #include <net/if_dl.h>
49 #include <netinet/in.h>
50 #include <netinet/icmp6.h>
51 #ifdef __bsdi__
52 # include <netinet/if_ether.h>
53 #endif
54 #ifdef __OpenBSD__
55 #include <netinet/if_ether.h>
56 #endif
57 #include <unistd.h>
58 #include <errno.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include "rtadvd.h"
63 #include "if.h"
64
65 #define ROUNDUP(a, size) \
66 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
67
68 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
69 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
70 sizeof(u_long)) :\
71 sizeof(u_long)))
72
73 struct if_msghdr **iflist;
74 int iflist_init_ok;
75 size_t ifblock_size;
76 char *ifblock;
77
78 static void get_iflist __P((char **buf, size_t *size));
79 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
80 size_t bufsize));
81
82 static void
83 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
84 {
85 int i;
86
87 for (i = 0; i < RTAX_MAX; i++) {
88 if (addrs & (1 << i)) {
89 rti_info[i] = sa;
90 NEXT_SA(sa);
91 }
92 else
93 rti_info[i] = NULL;
94 }
95 }
96
97 struct sockaddr_dl *
98 if_nametosdl(char *name)
99 {
100 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
101 char *buf, *next, *lim;
102 size_t len;
103 struct if_msghdr *ifm;
104 struct sockaddr *sa, *rti_info[RTAX_MAX];
105 struct sockaddr_dl *sdl = NULL, *ret_sdl;
106
107 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
108 return(NULL);
109 if ((buf = malloc(len)) == NULL)
110 return(NULL);
111 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
112 free(buf);
113 return(NULL);
114 }
115
116 lim = buf + len;
117 for (next = buf; next < lim; next += ifm->ifm_msglen) {
118 ifm = (struct if_msghdr *)next;
119 if (ifm->ifm_type == RTM_IFINFO) {
120 sa = (struct sockaddr *)(ifm + 1);
121 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
122 if ((sa = rti_info[RTAX_IFP]) != NULL) {
123 if (sa->sa_family == AF_LINK) {
124 sdl = (struct sockaddr_dl *)sa;
125 if (strlen(name) != sdl->sdl_nlen)
126 continue; /* not same len */
127 if (strncmp(&sdl->sdl_data[0],
128 name,
129 sdl->sdl_nlen) == 0) {
130 break;
131 }
132 }
133 }
134 }
135 }
136 if (next == lim) {
137 /* search failed */
138 free(buf);
139 return(NULL);
140 }
141
142 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
143 return(NULL);
144 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
145 return(ret_sdl);
146 }
147
148 int
149 if_getmtu(char *name)
150 {
151 struct ifaddrs *ifap, *ifa;
152 struct if_data *ifd;
153 u_long mtu = 0;
154
155 if (getifaddrs(&ifap) < 0)
156 return(0);
157 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
158 if (strcmp(ifa->ifa_name, name) == 0) {
159 ifd = ifa->ifa_data;
160 if (ifd)
161 mtu = ifd->ifi_mtu;
162 break;
163 }
164 }
165 freeifaddrs(ifap);
166
167 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
168 if (mtu == 0) {
169 struct ifreq ifr;
170 int s;
171
172 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
173 return(0);
174
175 ifr.ifr_addr.sa_family = AF_INET6;
176 strncpy(ifr.ifr_name, name,
177 sizeof(ifr.ifr_name));
178 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
179 close(s);
180 return(0);
181 }
182 close(s);
183
184 mtu = ifr.ifr_mtu;
185 }
186 #endif
187
188 return(mtu);
189 }
190
191 /* give interface index and its old flags, then new flags returned */
192 int
193 if_getflags(int ifindex, int oifflags)
194 {
195 struct ifreq ifr;
196 int s;
197
198 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
199 syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
200 strerror(errno));
201 return (oifflags & ~IFF_UP);
202 }
203
204 if_indextoname(ifindex, ifr.ifr_name);
205 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
206 syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
207 __FUNCTION__, ifr.ifr_name);
208 close(s);
209 return (oifflags & ~IFF_UP);
210 }
211 close(s);
212 return (ifr.ifr_flags);
213 }
214
215 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
216 int
217 lladdropt_length(struct sockaddr_dl *sdl)
218 {
219 switch(sdl->sdl_type) {
220 case IFT_ETHER:
221 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
222 default:
223 return(0);
224 }
225 }
226
227 void
228 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
229 {
230 char *addr;
231
232 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
233
234 switch(sdl->sdl_type) {
235 case IFT_ETHER:
236 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
237 addr = (char *)(ndopt + 1);
238 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
239 break;
240 default:
241 syslog(LOG_ERR,
242 "<%s> unsupported link type(%d)",
243 __FUNCTION__, sdl->sdl_type);
244 exit(1);
245 }
246
247 return;
248 }
249
250 int
251 rtbuf_len()
252 {
253 size_t len;
254
255 int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
256
257 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
258 return(-1);
259
260 return(len);
261 }
262
263 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
264 #define SIN6(s) ((struct sockaddr_in6 *)(s))
265 #define SDL(s) ((struct sockaddr_dl *)(s))
266 char *
267 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
268 {
269 struct rt_msghdr *rtm;
270 struct ifa_msghdr *ifam;
271 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
272
273 *lenp = 0;
274 for (rtm = (struct rt_msghdr *)buf;
275 rtm < (struct rt_msghdr *)lim;
276 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
277 /* just for safety */
278 if (!rtm->rtm_msglen) {
279 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
280 "(buf=%p lim=%p rtm=%p)", __FUNCTION__,
281 buf, lim, rtm);
282 break;
283 }
284 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
285 continue;
286 }
287
288 switch (rtm->rtm_type) {
289 case RTM_GET:
290 case RTM_ADD:
291 case RTM_DELETE:
292 /* address related checks */
293 sa = (struct sockaddr *)(rtm + 1);
294 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
295 if ((dst = rti_info[RTAX_DST]) == NULL ||
296 dst->sa_family != AF_INET6)
297 continue;
298
299 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
300 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
301 continue;
302
303 if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
304 gw->sa_family != AF_LINK)
305 continue;
306 if (ifindex && SDL(gw)->sdl_index != ifindex)
307 continue;
308
309 if (rti_info[RTAX_NETMASK] == NULL)
310 continue;
311
312 /* found */
313 *lenp = rtm->rtm_msglen;
314 return (char *)rtm;
315 /* NOTREACHED */
316 case RTM_NEWADDR:
317 case RTM_DELADDR:
318 ifam = (struct ifa_msghdr *)rtm;
319
320 /* address related checks */
321 sa = (struct sockaddr *)(ifam + 1);
322 get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
323 if ((ifa = rti_info[RTAX_IFA]) == NULL ||
324 (ifa->sa_family != AF_INET &&
325 ifa->sa_family != AF_INET6))
326 continue;
327
328 if (ifa->sa_family == AF_INET6 &&
329 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
330 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
331 continue;
332
333 if (ifindex && ifam->ifam_index != ifindex)
334 continue;
335
336 /* found */
337 *lenp = ifam->ifam_msglen;
338 return (char *)rtm;
339 /* NOTREACHED */
340 case RTM_IFINFO:
341 /* found */
342 *lenp = rtm->rtm_msglen;
343 return (char *)rtm;
344 /* NOTREACHED */
345 }
346 }
347
348 return (char *)rtm;
349 }
350 #undef FILTER_MATCH
351
352 struct in6_addr *
353 get_addr(char *buf)
354 {
355 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
356 struct sockaddr *sa, *rti_info[RTAX_MAX];
357
358 sa = (struct sockaddr *)(rtm + 1);
359 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
360
361 return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
362 }
363
364 int
365 get_rtm_ifindex(char *buf)
366 {
367 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
368 struct sockaddr *sa, *rti_info[RTAX_MAX];
369
370 sa = (struct sockaddr *)(rtm + 1);
371 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
372
373 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
374 }
375
376 int
377 get_ifm_ifindex(char *buf)
378 {
379 struct if_msghdr *ifm = (struct if_msghdr *)buf;
380
381 return ((int)ifm->ifm_index);
382 }
383
384 int
385 get_ifam_ifindex(char *buf)
386 {
387 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
388
389 return ((int)ifam->ifam_index);
390 }
391
392 int
393 get_ifm_flags(char *buf)
394 {
395 struct if_msghdr *ifm = (struct if_msghdr *)buf;
396
397 return (ifm->ifm_flags);
398 }
399
400 int
401 get_prefixlen(char *buf)
402 {
403 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
404 struct sockaddr *sa, *rti_info[RTAX_MAX];
405 u_char *p, *lim;
406
407 sa = (struct sockaddr *)(rtm + 1);
408 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
409 sa = rti_info[RTAX_NETMASK];
410
411 p = (u_char *)(&SIN6(sa)->sin6_addr);
412 lim = (u_char *)sa + sa->sa_len;
413 return prefixlen(p, lim);
414 }
415
416 int
417 prefixlen(u_char *p, u_char *lim)
418 {
419 int masklen;
420
421 for (masklen = 0; p < lim; p++) {
422 switch (*p) {
423 case 0xff:
424 masklen += 8;
425 break;
426 case 0xfe:
427 masklen += 7;
428 break;
429 case 0xfc:
430 masklen += 6;
431 break;
432 case 0xf8:
433 masklen += 5;
434 break;
435 case 0xf0:
436 masklen += 4;
437 break;
438 case 0xe0:
439 masklen += 3;
440 break;
441 case 0xc0:
442 masklen += 2;
443 break;
444 case 0x80:
445 masklen += 1;
446 break;
447 case 0x00:
448 break;
449 default:
450 return(-1);
451 }
452 }
453
454 return(masklen);
455 }
456
457 int
458 rtmsg_type(char *buf)
459 {
460 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
461
462 return(rtm->rtm_type);
463 }
464
465 int
466 rtmsg_len(char *buf)
467 {
468 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
469
470 return(rtm->rtm_msglen);
471 }
472
473 int
474 ifmsg_len(char *buf)
475 {
476 struct if_msghdr *ifm = (struct if_msghdr *)buf;
477
478 return(ifm->ifm_msglen);
479 }
480
481 /*
482 * alloc buffer and get if_msghdrs block from kernel,
483 * and put them into the buffer
484 */
485 static void
486 get_iflist(char **buf, size_t *size)
487 {
488 int mib[6];
489
490 mib[0] = CTL_NET;
491 mib[1] = PF_ROUTE;
492 mib[2] = 0;
493 mib[3] = AF_INET6;
494 mib[4] = NET_RT_IFLIST;
495 mib[5] = 0;
496
497 if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
498 syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
499 __FUNCTION__);
500 exit(1);
501 }
502 if ((*buf = malloc(*size)) == NULL) {
503 syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
504 exit(1);
505 }
506 if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
507 syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
508 __FUNCTION__);
509 exit(1);
510 }
511 return;
512 }
513
514 /*
515 * alloc buffer and parse if_msghdrs block passed as arg,
516 * and init the buffer as list of pointers ot each of the if_msghdr.
517 */
518 static void
519 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
520 {
521 int iflentry_size, malloc_size;
522 struct if_msghdr *ifm;
523 struct ifa_msghdr *ifam;
524 char *lim;
525
526 /*
527 * Estimate least size of an iflist entry, to be obtained from kernel.
528 * Should add sizeof(sockaddr) ??
529 */
530 iflentry_size = sizeof(struct if_msghdr);
531 /* roughly estimate max list size of pointers to each if_msghdr */
532 malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
533 if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
534 syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
535 exit(1);
536 }
537
538 lim = buf + bufsize;
539 for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
540 if (ifm->ifm_msglen == 0) {
541 syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
542 "(buf=%p lim=%p ifm=%p)", __FUNCTION__,
543 buf, lim, ifm);
544 return;
545 }
546
547 if (ifm->ifm_type == RTM_IFINFO) {
548 (*ifmlist_p)[ifm->ifm_index] = ifm;
549 } else {
550 syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
551 "expected %d, got %d\n msglen = %d\n"
552 "buf:%p, ifm:%p, lim:%p\n",
553 RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
554 buf, ifm, lim);
555 exit (1);
556 }
557 for (ifam = (struct ifa_msghdr *)
558 ((char *)ifm + ifm->ifm_msglen);
559 ifam < (struct ifa_msghdr *)lim;
560 ifam = (struct ifa_msghdr *)
561 ((char *)ifam + ifam->ifam_msglen)) {
562 /* just for safety */
563 if (!ifam->ifam_msglen) {
564 syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
565 "(buf=%p lim=%p ifam=%p)", __FUNCTION__,
566 buf, lim, ifam);
567 return;
568 }
569 if (ifam->ifam_type != RTM_NEWADDR)
570 break;
571 }
572 ifm = (struct if_msghdr *)ifam;
573 }
574 }
575
576 void
577 init_iflist()
578 {
579 if (ifblock) {
580 free(ifblock);
581 ifblock_size = 0;
582 }
583 if (iflist)
584 free(iflist);
585 /* get iflist block from kernel */
586 get_iflist(&ifblock, &ifblock_size);
587
588 /* make list of pointers to each if_msghdr */
589 parse_iflist(&iflist, ifblock, ifblock_size);
590 }