ndp.c revision 1.21 1 /* $NetBSD: ndp.c,v 1.21 2002/06/02 23:43:21 itojun Exp $ */
2 /* $KAME: ndp.c,v 1.91 2002/06/02 15:22:07 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 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 * Copyright (c) 1984, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Sun Microsystems, Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 /*
69 * Based on:
70 * "@(#) Copyright (c) 1984, 1993\n\
71 * The Regents of the University of California. All rights reserved.\n";
72 *
73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
74 */
75
76 /*
77 * ndp - display, set, delete and flush neighbor cache
78 */
79
80
81 #include <sys/param.h>
82 #include <sys/file.h>
83 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
86 #include <sys/time.h>
87
88 #include <net/if.h>
89 #include <net/if_dl.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92
93 #include <netinet/in.h>
94
95 #include <netinet/icmp6.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/nd6.h>
98
99 #include <arpa/inet.h>
100
101 #include <netdb.h>
102 #include <errno.h>
103 #include <nlist.h>
104 #include <stdio.h>
105 #include <string.h>
106 #include <paths.h>
107 #include <err.h>
108 #include <stdlib.h>
109 #include <fcntl.h>
110 #include <unistd.h>
111 #include <err.h>
112 #include "gmt2local.h"
113
114 /* packing rule for routing socket */
115 #define ROUNDUP(a) \
116 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
117 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
118
119 static pid_t pid;
120 static int cflag;
121 static int nflag;
122 static int tflag;
123 static int32_t thiszone; /* time difference with gmt */
124 static int s = -1;
125 static int repeat = 0;
126
127 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
128 char host_buf[NI_MAXHOST]; /* getnameinfo() */
129 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
130
131 int main __P((int, char **));
132 int file __P((char *));
133 void getsocket __P((void));
134 int set __P((int, char **));
135 void get __P((char *));
136 int delete __P((char *));
137 void dump __P((struct in6_addr *));
138 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
139 static char *ether_str __P((struct sockaddr_dl *));
140 int ndp_ether_aton __P((char *, u_char *));
141 void usage __P((void));
142 int rtmsg __P((int));
143 void ifinfo __P((int, char **));
144 void rtrlist __P((void));
145 void plist __P((void));
146 void pfx_flush __P((void));
147 void rtrlist __P((void));
148 void rtr_flush __P((void));
149 void harmonize_rtr __P((void));
150 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
151 static void getdefif __P((void));
152 static void setdefif __P((char *));
153 #endif
154 static char *sec2str __P((time_t));
155 static char *ether_str __P((struct sockaddr_dl *));
156 static void ts_print __P((const struct timeval *));
157
158 #ifdef ICMPV6CTL_ND6_DRLIST
159 static char *rtpref_str[] = {
160 "medium", /* 00 */
161 "high", /* 01 */
162 "rsv", /* 10 */
163 "low" /* 11 */
164 };
165 #endif
166
167 int
168 main(argc, argv)
169 int argc;
170 char **argv;
171 {
172 int ch;
173 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0;
174 int pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
175
176 pid = getpid();
177 thiszone = gmt2local(0);
178 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
179 switch ((char)ch) {
180 case 'a':
181 aflag = 1;
182 break;
183 case 'c':
184 cflag = 1;
185 break;
186 case 'd':
187 dflag = 1;
188 break;
189 case 'I':
190 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
191 if (argc > 2)
192 setdefif(argv[2]);
193 getdefif(); /* always call it to print the result */
194 exit(0);
195 #else
196 errx(1, "not supported yet");
197 /*NOTREACHED*/
198 #endif
199 case 'i' :
200 argc -= optind;
201 argv += optind;
202 if (argc < 1)
203 usage();
204 ifinfo(argc, argv);
205 exit(0);
206 case 'n':
207 nflag = 1;
208 continue;
209 case 'p':
210 pflag = 1;
211 break;
212 case 'f' :
213 if (argc != 3)
214 usage();
215 file(argv[2]);
216 exit(0);
217 case 'l' :
218 /* obsolete, ignored */
219 break;
220 case 'r' :
221 rflag = 1;
222 break;
223 case 's':
224 sflag = 1;
225 break;
226 case 't':
227 tflag = 1;
228 break;
229 case 'A':
230 aflag = 1;
231 repeat = atoi(optarg);
232 if (repeat < 0)
233 usage();
234 break;
235 case 'H' :
236 Hflag = 1;
237 break;
238 case 'P':
239 Pflag = 1;
240 break;
241 case 'R':
242 Rflag = 1;
243 break;
244 default:
245 usage();
246 }
247
248 argc -= optind;
249 argv += optind;
250
251 if (aflag || cflag) {
252 dump(0);
253 exit(0);
254 }
255 if (dflag) {
256 if (argc != 1)
257 usage();
258 delete(argv[0]);
259 exit(0);
260 }
261 if (pflag) {
262 plist();
263 exit(0);
264 }
265 if (rflag) {
266 rtrlist();
267 exit(0);
268 }
269 if (sflag) {
270 if (argc < 2 || argc > 4)
271 usage();
272 exit(set(argc, argv) ? 1 : 0);
273 }
274 if (Hflag) {
275 harmonize_rtr();
276 exit(0);
277 }
278 if (Pflag) {
279 pfx_flush();
280 exit(0);
281 }
282 if (Rflag) {
283 rtr_flush();
284 exit(0);
285 }
286
287 if (argc != 1)
288 usage();
289 get(argv[0]);
290 exit(0);
291 }
292
293 /*
294 * Process a file to set standard ndp entries
295 */
296 int
297 file(name)
298 char *name;
299 {
300 FILE *fp;
301 int i, retval;
302 char line[100], arg[5][50], *args[5];
303
304 if ((fp = fopen(name, "r")) == NULL) {
305 fprintf(stderr, "ndp: cannot open %s\n", name);
306 exit(1);
307 }
308 args[0] = &arg[0][0];
309 args[1] = &arg[1][0];
310 args[2] = &arg[2][0];
311 args[3] = &arg[3][0];
312 args[4] = &arg[4][0];
313 retval = 0;
314 while(fgets(line, 100, fp) != NULL) {
315 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
316 arg[3], arg[4]);
317 if (i < 2) {
318 fprintf(stderr, "ndp: bad line: %s\n", line);
319 retval = 1;
320 continue;
321 }
322 if (set(i, args))
323 retval = 1;
324 }
325 fclose(fp);
326 return (retval);
327 }
328
329 void
330 getsocket()
331 {
332 if (s < 0) {
333 s = socket(PF_ROUTE, SOCK_RAW, 0);
334 if (s < 0) {
335 err(1, "ndp: socket");
336 /* NOTREACHED */
337 }
338 }
339 }
340
341 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
342 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
343 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
344 int expire_time, flags, found_entry;
345 struct {
346 struct rt_msghdr m_rtm;
347 char m_space[512];
348 } m_rtmsg;
349
350 /*
351 * Set an individual neighbor cache entry
352 */
353 int
354 set(argc, argv)
355 int argc;
356 char **argv;
357 {
358 register struct sockaddr_in6 *sin = &sin_m;
359 register struct sockaddr_dl *sdl;
360 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
361 struct addrinfo hints, *res;
362 int gai_error;
363 u_char *ea;
364 char *host = argv[0], *eaddr = argv[1];
365
366 getsocket();
367 argc -= 2;
368 argv += 2;
369 sdl_m = blank_sdl;
370 sin_m = blank_sin;
371
372 bzero(&hints, sizeof(hints));
373 hints.ai_family = AF_INET6;
374 gai_error = getaddrinfo(host, NULL, &hints, &res);
375 if (gai_error) {
376 fprintf(stderr, "ndp: %s: %s\n", host,
377 gai_strerror(gai_error));
378 return 1;
379 }
380 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
381 #ifdef __KAME__
382 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
383 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
384 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
385 }
386 #endif
387 ea = (u_char *)LLADDR(&sdl_m);
388 if (ndp_ether_aton(eaddr, ea) == 0)
389 sdl_m.sdl_alen = 6;
390 flags = expire_time = 0;
391 while (argc-- > 0) {
392 if (strncmp(argv[0], "temp", 4) == 0) {
393 struct timeval time;
394
395 gettimeofday(&time, 0);
396 expire_time = time.tv_sec + 20 * 60;
397 } else if (strncmp(argv[0], "proxy", 5) == 0)
398 flags |= RTF_ANNOUNCE;
399 argv++;
400 }
401 if (rtmsg(RTM_GET) < 0) {
402 errx(1, "RTM_GET(%s) failed", host);
403 /* NOTREACHED */
404 }
405 sin = (struct sockaddr_in6 *)(rtm + 1);
406 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
407 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
408 if (sdl->sdl_family == AF_LINK &&
409 (rtm->rtm_flags & RTF_LLINFO) &&
410 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
411 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
412 case IFT_ISO88024: case IFT_ISO88025:
413 goto overwrite;
414 }
415 /*
416 * IPv4 arp command retries with sin_other = SIN_PROXY here.
417 */
418 fprintf(stderr, "set: cannot configure a new entry\n");
419 return 1;
420 }
421
422 overwrite:
423 if (sdl->sdl_family != AF_LINK) {
424 printf("cannot intuit interface index and type for %s\n", host);
425 return (1);
426 }
427 sdl_m.sdl_type = sdl->sdl_type;
428 sdl_m.sdl_index = sdl->sdl_index;
429 return (rtmsg(RTM_ADD));
430 }
431
432 /*
433 * Display an individual neighbor cache entry
434 */
435 void
436 get(host)
437 char *host;
438 {
439 struct sockaddr_in6 *sin = &sin_m;
440 struct addrinfo hints, *res;
441 int gai_error;
442
443 sin_m = blank_sin;
444 bzero(&hints, sizeof(hints));
445 hints.ai_family = AF_INET6;
446 gai_error = getaddrinfo(host, NULL, &hints, &res);
447 if (gai_error) {
448 fprintf(stderr, "ndp: %s: %s\n", host,
449 gai_strerror(gai_error));
450 return;
451 }
452 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
453 #ifdef __KAME__
454 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
455 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
456 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
457 }
458 #endif
459 dump(&sin->sin6_addr);
460 if (found_entry == 0) {
461 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
462 sizeof(host_buf), NULL ,0,
463 (nflag ? NI_NUMERICHOST : 0));
464 printf("%s (%s) -- no entry\n", host, host_buf);
465 exit(1);
466 }
467 }
468
469 /*
470 * Delete a neighbor cache entry
471 */
472 int
473 delete(host)
474 char *host;
475 {
476 struct sockaddr_in6 *sin = &sin_m;
477 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
478 struct sockaddr_dl *sdl;
479 struct addrinfo hints, *res;
480 int gai_error;
481
482 getsocket();
483 sin_m = blank_sin;
484
485 bzero(&hints, sizeof(hints));
486 hints.ai_family = AF_INET6;
487 gai_error = getaddrinfo(host, NULL, &hints, &res);
488 if (gai_error) {
489 fprintf(stderr, "ndp: %s: %s\n", host,
490 gai_strerror(gai_error));
491 return 1;
492 }
493 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
494 #ifdef __KAME__
495 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
496 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
497 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
498 }
499 #endif
500 if (rtmsg(RTM_GET) < 0) {
501 errx(1, "RTM_GET(%s) failed", host);
502 /* NOTREACHED */
503 }
504 sin = (struct sockaddr_in6 *)(rtm + 1);
505 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
506 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
507 if (sdl->sdl_family == AF_LINK &&
508 (rtm->rtm_flags & RTF_LLINFO) &&
509 !(rtm->rtm_flags & RTF_GATEWAY)) {
510 goto delete;
511 }
512 /*
513 * IPv4 arp command retries with sin_other = SIN_PROXY here.
514 */
515 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
516 return 1;
517 }
518
519 delete:
520 if (sdl->sdl_family != AF_LINK) {
521 printf("cannot locate %s\n", host);
522 return (1);
523 }
524 if (rtmsg(RTM_DELETE) == 0) {
525 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
526
527 #ifdef __KAME__
528 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
529 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
530 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
531 }
532 #endif
533 getnameinfo((struct sockaddr *)&s6,
534 s6.sin6_len, host_buf,
535 sizeof(host_buf), NULL, 0,
536 (nflag ? NI_NUMERICHOST : 0));
537 printf("%s (%s) deleted\n", host, host_buf);
538 }
539
540 return 0;
541 }
542
543 #define W_ADDR 36
544 #define W_LL 17
545 #define W_IF 6
546
547 /*
548 * Dump the entire neighbor cache
549 */
550 void
551 dump(addr)
552 struct in6_addr *addr;
553 {
554 int mib[6];
555 size_t needed;
556 char *lim, *buf, *next;
557 struct rt_msghdr *rtm;
558 struct sockaddr_in6 *sin;
559 struct sockaddr_dl *sdl;
560 extern int h_errno;
561 struct in6_nbrinfo *nbi;
562 struct timeval time;
563 int addrwidth;
564 int llwidth;
565 int ifwidth;
566 char flgbuf[8];
567 char *ifname;
568
569 /* Print header */
570 if (!tflag && !cflag)
571 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %4s\n",
572 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
573 W_IF, W_IF, "Netif", "Expire", "S", "Flgs");
574
575 again:;
576 mib[0] = CTL_NET;
577 mib[1] = PF_ROUTE;
578 mib[2] = 0;
579 mib[3] = AF_INET6;
580 mib[4] = NET_RT_FLAGS;
581 mib[5] = RTF_LLINFO;
582 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
583 err(1, "sysctl(PF_ROUTE estimate)");
584 if (needed > 0) {
585 if ((buf = malloc(needed)) == NULL)
586 errx(1, "malloc");
587 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
588 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
589 lim = buf + needed;
590 } else
591 buf = lim = NULL;
592
593 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
594 int isrouter = 0, prbs = 0;
595
596 rtm = (struct rt_msghdr *)next;
597 sin = (struct sockaddr_in6 *)(rtm + 1);
598 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
599
600 /*
601 * Some OSes can produce a route that has the LINK flag but
602 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
603 * and BSD/OS, where xx is not the interface identifier on
604 * lo0). Such routes entry would annoy getnbrinfo() below,
605 * so we skip them.
606 * XXX: such routes should have the GATEWAY flag, not the
607 * LINK flag. However, there is rotten routing software
608 * that advertises all routes that have the GATEWAY flag.
609 * Thus, KAME kernel intentionally does not set the LINK flag.
610 * What is to be fixed is not ndp, but such routing software
611 * (and the kernel workaround)...
612 */
613 if (sdl->sdl_family != AF_LINK)
614 continue;
615
616 if (!(rtm->rtm_flags & RTF_HOST))
617 continue;
618
619 if (addr) {
620 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
621 continue;
622 found_entry = 1;
623 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
624 continue;
625 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
626 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
627 /* XXX: should scope id be filled in the kernel? */
628 if (sin->sin6_scope_id == 0)
629 sin->sin6_scope_id = sdl->sdl_index;
630 #ifdef __KAME__
631 /* KAME specific hack; removed the embedded id */
632 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
633 #endif
634 }
635 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
636 sizeof(host_buf), NULL, 0,
637 (nflag ? NI_NUMERICHOST : 0));
638 if (cflag == 1) {
639 #ifdef RTF_WASCLONED
640 if (rtm->rtm_flags & RTF_WASCLONED)
641 delete(host_buf);
642 #elif defined(RTF_CLONED)
643 if (rtm->rtm_flags & RTF_CLONED)
644 delete(host_buf);
645 #else
646 delete(host_buf);
647 #endif
648 continue;
649 }
650 gettimeofday(&time, 0);
651 if (tflag)
652 ts_print(&time);
653
654 addrwidth = strlen(host_buf);
655 if (addrwidth < W_ADDR)
656 addrwidth = W_ADDR;
657 llwidth = strlen(ether_str(sdl));
658 if (W_ADDR + W_LL - addrwidth > llwidth)
659 llwidth = W_ADDR + W_LL - addrwidth;
660 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
661 if (!ifname)
662 ifname = "?";
663 ifwidth = strlen(ifname);
664 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
665 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
666
667 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
668 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
669
670 /* Print neighbor discovery specific informations */
671 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
672 if (nbi) {
673 if (nbi->expire > time.tv_sec) {
674 printf(" %-9.9s",
675 sec2str(nbi->expire - time.tv_sec));
676 } else if (nbi->expire == 0)
677 printf(" %-9.9s", "permanent");
678 else
679 printf(" %-9.9s", "expired");
680
681 switch (nbi->state) {
682 case ND6_LLINFO_NOSTATE:
683 printf(" N");
684 break;
685 #ifdef ND6_LLINFO_WAITDELETE
686 case ND6_LLINFO_WAITDELETE:
687 printf(" W");
688 break;
689 #endif
690 case ND6_LLINFO_INCOMPLETE:
691 printf(" I");
692 break;
693 case ND6_LLINFO_REACHABLE:
694 printf(" R");
695 break;
696 case ND6_LLINFO_STALE:
697 printf(" S");
698 break;
699 case ND6_LLINFO_DELAY:
700 printf(" D");
701 break;
702 case ND6_LLINFO_PROBE:
703 printf(" P");
704 break;
705 default:
706 printf(" ?");
707 break;
708 }
709
710 isrouter = nbi->isrouter;
711 prbs = nbi->asked;
712 } else {
713 warnx("failed to get neighbor information");
714 printf(" ");
715 }
716
717 /*
718 * other flags. R: router, P: proxy, W: ??
719 */
720 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
721 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
722 isrouter ? "R" : "",
723 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
724 } else {
725 sin = (struct sockaddr_in6 *)
726 (sdl->sdl_len + (char *)sdl);
727 #if 0 /* W and P are mystery even for us */
728 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
729 isrouter ? "R" : "",
730 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
731 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
732 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
733 #else
734 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
735 isrouter ? "R" : "",
736 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
737 #endif
738 }
739 printf(" %s", flgbuf);
740
741 if (prbs)
742 printf(" %d", prbs);
743
744 printf("\n");
745 }
746 if (buf != NULL)
747 free(buf);
748
749 if (repeat) {
750 printf("\n");
751 sleep(repeat);
752 goto again;
753 }
754 }
755
756 static struct in6_nbrinfo *
757 getnbrinfo(addr, ifindex, warning)
758 struct in6_addr *addr;
759 int ifindex;
760 int warning;
761 {
762 static struct in6_nbrinfo nbi;
763 int s;
764
765 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
766 err(1, "socket");
767
768 bzero(&nbi, sizeof(nbi));
769 if_indextoname(ifindex, nbi.ifname);
770 nbi.addr = *addr;
771 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
772 if (warning)
773 warn("ioctl(SIOCGNBRINFO_IN6)");
774 close(s);
775 return(NULL);
776 }
777
778 close(s);
779 return(&nbi);
780 }
781
782 static char *
783 ether_str(sdl)
784 struct sockaddr_dl *sdl;
785 {
786 static char hbuf[NI_MAXHOST];
787
788 if (sdl->sdl_alen) {
789 if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
790 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
791 snprintf(hbuf, sizeof(hbuf), "<invalid>");
792 } else
793 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
794
795 return(hbuf);
796 }
797
798 int
799 ndp_ether_aton(a, n)
800 char *a;
801 u_char *n;
802 {
803 int i, o[6];
804
805 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
806 &o[3], &o[4], &o[5]);
807 if (i != 6) {
808 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
809 return (1);
810 }
811 for (i=0; i<6; i++)
812 n[i] = o[i];
813 return (0);
814 }
815
816 void
817 usage()
818 {
819 printf("usage: ndp hostname\n");
820 printf(" ndp -a[nt]\n");
821 printf(" ndp [-nt] -A wait\n");
822 printf(" ndp -c[nt]\n");
823 printf(" ndp -d[nt] hostname\n");
824 printf(" ndp -f[nt] filename\n");
825 printf(" ndp -i interface [flags...]\n");
826 #ifdef SIOCSDEFIFACE_IN6
827 printf(" ndp -I [interface|delete]\n");
828 #endif
829 printf(" ndp -p\n");
830 printf(" ndp -r\n");
831 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
832 printf(" ndp -H\n");
833 printf(" ndp -P\n");
834 printf(" ndp -R\n");
835 exit(1);
836 }
837
838 int
839 rtmsg(cmd)
840 int cmd;
841 {
842 static int seq;
843 int rlen;
844 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
845 register char *cp = m_rtmsg.m_space;
846 register int l;
847
848 errno = 0;
849 if (cmd == RTM_DELETE)
850 goto doit;
851 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
852 rtm->rtm_flags = flags;
853 rtm->rtm_version = RTM_VERSION;
854
855 switch (cmd) {
856 default:
857 fprintf(stderr, "ndp: internal wrong cmd\n");
858 exit(1);
859 case RTM_ADD:
860 rtm->rtm_addrs |= RTA_GATEWAY;
861 if (expire_time) {
862 rtm->rtm_rmx.rmx_expire = expire_time;
863 rtm->rtm_inits = RTV_EXPIRE;
864 }
865 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
866 if (rtm->rtm_flags & RTF_ANNOUNCE) {
867 rtm->rtm_flags &= ~RTF_HOST;
868 rtm->rtm_flags |= RTA_NETMASK;
869 }
870 /* FALLTHROUGH */
871 case RTM_GET:
872 rtm->rtm_addrs |= RTA_DST;
873 }
874 #define NEXTADDR(w, s) \
875 if (rtm->rtm_addrs & (w)) { \
876 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
877
878 NEXTADDR(RTA_DST, sin_m);
879 NEXTADDR(RTA_GATEWAY, sdl_m);
880 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
881 NEXTADDR(RTA_NETMASK, so_mask);
882
883 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
884 doit:
885 l = rtm->rtm_msglen;
886 rtm->rtm_seq = ++seq;
887 rtm->rtm_type = cmd;
888 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
889 if (errno != ESRCH || cmd != RTM_DELETE) {
890 err(1, "writing to routing socket");
891 /* NOTREACHED */
892 }
893 }
894 do {
895 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
896 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
897 if (l < 0)
898 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
899 strerror(errno));
900 return (0);
901 }
902
903 void
904 ifinfo(argc, argv)
905 int argc;
906 char **argv;
907 {
908 struct in6_ndireq nd;
909 int i, s;
910 char *ifname = argv[0];
911 u_int32_t newflags;
912 #ifdef IPV6CTL_USETEMPADDR
913 u_int8_t nullbuf[8];
914 #endif
915
916 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
917 err(1, "socket");
918 /* NOTREACHED */
919 }
920 bzero(&nd, sizeof(nd));
921 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
922 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
923 err(1, "ioctl(SIOCGIFINFO_IN6)");
924 /* NOTREACHED */
925 }
926 #define ND nd.ndi
927 newflags = ND.flags;
928 for (i = 1; i < argc; i++) {
929 int clear = 0;
930 char *cp = argv[i];
931
932 if (*cp == '-') {
933 clear = 1;
934 cp++;
935 }
936
937 #define SETFLAG(s, f) \
938 do {\
939 if (strcmp(cp, (s)) == 0) {\
940 if (clear)\
941 newflags &= ~(f);\
942 else\
943 newflags |= (f);\
944 }\
945 } while (0)
946 SETFLAG("nud", ND6_IFF_PERFORMNUD);
947 #ifdef ND6_IFF_ACCEPT_RTADV
948 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
949 #endif
950 #ifdef ND6_IFF_PREFER_SOURCE
951 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
952 #endif
953
954 ND.flags = newflags;
955 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
956 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
957 /* NOTREACHED */
958 }
959 #undef SETFLAG
960 }
961
962 if (!ND.initialized) {
963 errx(1, "%s: not initialized yet", ifname);
964 /* NOTREACHED */
965 }
966
967 printf("linkmtu=%d", ND.linkmtu);
968 printf(", curhlim=%d", ND.chlim);
969 printf(", basereachable=%ds%dms",
970 ND.basereachable / 1000, ND.basereachable % 1000);
971 printf(", reachable=%ds", ND.reachable);
972 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
973 #ifdef IPV6CTL_USETEMPADDR
974 memset(nullbuf, 0, sizeof(nullbuf));
975 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
976 int j;
977 u_int8_t *rbuf;
978
979 for (i = 0; i < 3; i++) {
980 switch (i) {
981 case 0:
982 printf("\nRandom seed(0): ");
983 rbuf = ND.randomseed0;
984 break;
985 case 1:
986 printf("\nRandom seed(1): ");
987 rbuf = ND.randomseed1;
988 break;
989 case 2:
990 printf("\nRandom ID: ");
991 rbuf = ND.randomid;
992 break;
993 }
994 for (j = 0; j < 8; j++)
995 printf("%02x", rbuf[j]);
996 }
997 }
998 #endif
999 if (ND.flags) {
1000 printf("\nFlags: ");
1001 if ((ND.flags & ND6_IFF_PERFORMNUD))
1002 printf("PERFORMNUD ");
1003 #ifdef ND6_IFF_ACCEPT_RTADV
1004 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1005 printf("ACCEPT_RA ");
1006 #endif
1007 #ifdef ND6_IFF_PREFER_SOURCE
1008 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1009 printf("PREFER_SRC ");
1010 #endif
1011 }
1012 putc('\n', stdout);
1013 #undef ND
1014
1015 close(s);
1016 }
1017
1018 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1019 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1020 #endif
1021
1022 void
1023 rtrlist()
1024 {
1025 #ifdef ICMPV6CTL_ND6_DRLIST
1026 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1027 char *buf;
1028 struct in6_defrouter *p, *ep;
1029 size_t l;
1030 struct timeval time;
1031
1032 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1033 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1034 /*NOTREACHED*/
1035 }
1036 buf = malloc(l);
1037 if (!buf) {
1038 errx(1, "not enough core");
1039 /*NOTREACHED*/
1040 }
1041 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1042 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1043 /*NOTREACHED*/
1044 }
1045
1046 ep = (struct in6_defrouter *)(buf + l);
1047 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1048 int rtpref;
1049
1050 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1051 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1052 (nflag ? NI_NUMERICHOST : 0)) != 0)
1053 strlcpy(host_buf, "?", sizeof(host_buf));
1054
1055 printf("%s if=%s", host_buf,
1056 if_indextoname(p->if_index, ifix_buf));
1057 printf(", flags=%s%s",
1058 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1059 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1060 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1061 printf(", pref=%s", rtpref_str[rtpref]);
1062
1063 gettimeofday(&time, 0);
1064 if (p->expire == 0)
1065 printf(", expire=Never\n");
1066 else
1067 printf(", expire=%s\n",
1068 sec2str(p->expire - time.tv_sec));
1069 }
1070 free(buf);
1071 #else
1072 struct in6_drlist dr;
1073 int s, i;
1074 struct timeval time;
1075
1076 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1077 err(1, "socket");
1078 /* NOTREACHED */
1079 }
1080 bzero(&dr, sizeof(dr));
1081 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1082 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1083 err(1, "ioctl(SIOCGDRLST_IN6)");
1084 /* NOTREACHED */
1085 }
1086 #define DR dr.defrouter[i]
1087 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1088 struct sockaddr_in6 sin6;
1089
1090 bzero(&sin6, sizeof(sin6));
1091 sin6.sin6_family = AF_INET6;
1092 sin6.sin6_len = sizeof(sin6);
1093 sin6.sin6_addr = DR.rtaddr;
1094 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1095 sizeof(host_buf), NULL, 0,
1096 (nflag ? NI_NUMERICHOST : 0));
1097
1098 printf("%s if=%s", host_buf,
1099 if_indextoname(DR.if_index, ifix_buf));
1100 printf(", flags=%s%s",
1101 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1102 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1103 gettimeofday(&time, 0);
1104 if (DR.expire == 0)
1105 printf(", expire=Never\n");
1106 else
1107 printf(", expire=%s\n",
1108 sec2str(DR.expire - time.tv_sec));
1109 }
1110 #undef DR
1111 close(s);
1112 #endif
1113 }
1114
1115 void
1116 plist()
1117 {
1118 #ifdef ICMPV6CTL_ND6_PRLIST
1119 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1120 char *buf;
1121 struct in6_prefix *p, *ep, *n;
1122 struct sockaddr_in6 *advrtr;
1123 size_t l;
1124 struct timeval time;
1125 const int niflags = NI_NUMERICHOST;
1126 int ninflags = nflag ? NI_NUMERICHOST : 0;
1127 char namebuf[NI_MAXHOST];
1128
1129 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1130 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1131 /*NOTREACHED*/
1132 }
1133 buf = malloc(l);
1134 if (!buf) {
1135 errx(1, "not enough core");
1136 /*NOTREACHED*/
1137 }
1138 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1139 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1140 /*NOTREACHED*/
1141 }
1142
1143 ep = (struct in6_prefix *)(buf + l);
1144 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1145 advrtr = (struct sockaddr_in6 *)(p + 1);
1146 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1147
1148 if (getnameinfo((struct sockaddr *)&p->prefix,
1149 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1150 NULL, 0, niflags) != 0)
1151 strlcpy(namebuf, "?", sizeof(namebuf));
1152 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1153 if_indextoname(p->if_index, ifix_buf));
1154
1155 gettimeofday(&time, 0);
1156 /*
1157 * meaning of fields, especially flags, is very different
1158 * by origin. notify the difference to the users.
1159 */
1160 printf("flags=%s%s%s%s%s",
1161 p->raflags.onlink ? "L" : "",
1162 p->raflags.autonomous ? "A" : "",
1163 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1164 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1165 #ifdef NDPRF_HOME
1166 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1167 #else
1168 ""
1169 #endif
1170 );
1171 if (p->vltime == ND6_INFINITE_LIFETIME)
1172 printf(" vltime=infinity");
1173 else
1174 printf(" vltime=%lu", (unsigned long)p->vltime);
1175 if (p->pltime == ND6_INFINITE_LIFETIME)
1176 printf(", pltime=infinity");
1177 else
1178 printf(", pltime=%lu", (unsigned long)p->pltime);
1179 if (p->expire == 0)
1180 printf(", expire=Never");
1181 else if (p->expire >= time.tv_sec)
1182 printf(", expire=%s",
1183 sec2str(p->expire - time.tv_sec));
1184 else
1185 printf(", expired");
1186 printf(", ref=%d", p->refcnt);
1187 printf("\n");
1188 /*
1189 * "advertising router" list is meaningful only if the prefix
1190 * information is from RA.
1191 */
1192 if (p->advrtrs) {
1193 int j;
1194 struct sockaddr_in6 *sin6;
1195
1196 sin6 = advrtr;
1197 printf(" advertised by\n");
1198 for (j = 0; j < p->advrtrs; j++) {
1199 struct in6_nbrinfo *nbi;
1200
1201 if (getnameinfo((struct sockaddr *)sin6,
1202 sin6->sin6_len, namebuf, sizeof(namebuf),
1203 NULL, 0, ninflags) != 0)
1204 strlcpy(namebuf, "?", sizeof(namebuf));
1205 printf(" %s", namebuf);
1206
1207 nbi = getnbrinfo(&sin6->sin6_addr,
1208 p->if_index, 0);
1209 if (nbi) {
1210 switch (nbi->state) {
1211 case ND6_LLINFO_REACHABLE:
1212 case ND6_LLINFO_STALE:
1213 case ND6_LLINFO_DELAY:
1214 case ND6_LLINFO_PROBE:
1215 printf(" (reachable)\n");
1216 break;
1217 default:
1218 printf(" (unreachable)\n");
1219 }
1220 } else
1221 printf(" (no neighbor state)\n");
1222 sin6++;
1223 }
1224 } else
1225 printf(" No advertising router\n");
1226 }
1227 free(buf);
1228 #else
1229 struct in6_prlist pr;
1230 int s, i;
1231 struct timeval time;
1232
1233 gettimeofday(&time, 0);
1234
1235 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1236 err(1, "socket");
1237 /* NOTREACHED */
1238 }
1239 bzero(&pr, sizeof(pr));
1240 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1241 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1242 err(1, "ioctl(SIOCGPRLST_IN6)");
1243 /* NOTREACHED */
1244 }
1245 #define PR pr.prefix[i]
1246 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1247 struct sockaddr_in6 p6;
1248 char namebuf[NI_MAXHOST];
1249 int niflags;
1250
1251 #ifdef NDPRF_ONLINK
1252 p6 = PR.prefix;
1253 #else
1254 memset(&p6, 0, sizeof(p6));
1255 p6.sin6_family = AF_INET6;
1256 p6.sin6_len = sizeof(p6);
1257 p6.sin6_addr = PR.prefix;
1258 #endif
1259
1260 /*
1261 * copy link index to sin6_scope_id field.
1262 * XXX: KAME specific.
1263 */
1264 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1265 u_int16_t linkid;
1266
1267 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1268 sizeof(linkid));
1269 linkid = ntohs(linkid);
1270 p6.sin6_scope_id = linkid;
1271 p6.sin6_addr.s6_addr[2] = 0;
1272 p6.sin6_addr.s6_addr[3] = 0;
1273 }
1274
1275 niflags = NI_NUMERICHOST;
1276 if (getnameinfo((struct sockaddr *)&p6,
1277 sizeof(p6), namebuf, sizeof(namebuf),
1278 NULL, 0, niflags)) {
1279 warnx("getnameinfo failed");
1280 continue;
1281 }
1282 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1283 if_indextoname(PR.if_index, ifix_buf));
1284
1285 gettimeofday(&time, 0);
1286 /*
1287 * meaning of fields, especially flags, is very different
1288 * by origin. notify the difference to the users.
1289 */
1290 #if 0
1291 printf(" %s",
1292 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1293 #endif
1294 #ifdef NDPRF_ONLINK
1295 printf("flags=%s%s%s%s%s",
1296 PR.raflags.onlink ? "L" : "",
1297 PR.raflags.autonomous ? "A" : "",
1298 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1299 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1300 #ifdef NDPRF_HOME
1301 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1302 #else
1303 ""
1304 #endif
1305 );
1306 #else
1307 printf("flags=%s%s",
1308 PR.raflags.onlink ? "L" : "",
1309 PR.raflags.autonomous ? "A" : "");
1310 #endif
1311 if (PR.vltime == ND6_INFINITE_LIFETIME)
1312 printf(" vltime=infinity");
1313 else
1314 printf(" vltime=%lu", PR.vltime);
1315 if (PR.pltime == ND6_INFINITE_LIFETIME)
1316 printf(", pltime=infinity");
1317 else
1318 printf(", pltime=%lu", PR.pltime);
1319 if (PR.expire == 0)
1320 printf(", expire=Never");
1321 else if (PR.expire >= time.tv_sec)
1322 printf(", expire=%s",
1323 sec2str(PR.expire - time.tv_sec));
1324 else
1325 printf(", expired");
1326 #ifdef NDPRF_ONLINK
1327 printf(", ref=%d", PR.refcnt);
1328 #endif
1329 #if 0
1330 switch (PR.origin) {
1331 case PR_ORIG_RA:
1332 printf(", origin=RA");
1333 break;
1334 case PR_ORIG_RR:
1335 printf(", origin=RR");
1336 break;
1337 case PR_ORIG_STATIC:
1338 printf(", origin=static");
1339 break;
1340 case PR_ORIG_KERNEL:
1341 printf(", origin=kernel");
1342 break;
1343 default:
1344 printf(", origin=?");
1345 break;
1346 }
1347 #endif
1348 printf("\n");
1349 /*
1350 * "advertising router" list is meaningful only if the prefix
1351 * information is from RA.
1352 */
1353 if (0 && /* prefix origin is almost obsolted */
1354 PR.origin != PR_ORIG_RA)
1355 ;
1356 else if (PR.advrtrs) {
1357 int j;
1358 printf(" advertised by\n");
1359 for (j = 0; j < PR.advrtrs; j++) {
1360 struct sockaddr_in6 sin6;
1361 struct in6_nbrinfo *nbi;
1362
1363 bzero(&sin6, sizeof(sin6));
1364 sin6.sin6_family = AF_INET6;
1365 sin6.sin6_len = sizeof(sin6);
1366 sin6.sin6_addr = PR.advrtr[j];
1367 sin6.sin6_scope_id = PR.if_index; /* XXX */
1368 getnameinfo((struct sockaddr *)&sin6,
1369 sin6.sin6_len, host_buf,
1370 sizeof(host_buf), NULL, 0,
1371 (nflag ? NI_NUMERICHOST : 0));
1372 printf(" %s", host_buf);
1373
1374 nbi = getnbrinfo(&sin6.sin6_addr,
1375 PR.if_index, 0);
1376 if (nbi) {
1377 switch (nbi->state) {
1378 case ND6_LLINFO_REACHABLE:
1379 case ND6_LLINFO_STALE:
1380 case ND6_LLINFO_DELAY:
1381 case ND6_LLINFO_PROBE:
1382 printf(" (reachable)\n");
1383 break;
1384 default:
1385 printf(" (unreachable)\n");
1386 }
1387 } else
1388 printf(" (no neighbor state)\n");
1389 }
1390 if (PR.advrtrs > DRLSTSIZ)
1391 printf(" and %d routers\n",
1392 PR.advrtrs - DRLSTSIZ);
1393 } else
1394 printf(" No advertising router\n");
1395 }
1396 #undef PR
1397 close(s);
1398 #endif
1399 }
1400
1401 void
1402 pfx_flush()
1403 {
1404 char dummyif[IFNAMSIZ+8];
1405 int s;
1406
1407 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1408 err(1, "socket");
1409 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1410 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1411 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1412 }
1413
1414 void
1415 rtr_flush()
1416 {
1417 char dummyif[IFNAMSIZ+8];
1418 int s;
1419
1420 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1421 err(1, "socket");
1422 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1423 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1424 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1425
1426 close(s);
1427 }
1428
1429 void
1430 harmonize_rtr()
1431 {
1432 char dummyif[IFNAMSIZ+8];
1433 int s;
1434
1435 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1436 err(1, "socket");
1437 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1438 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1439 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1440
1441 close(s);
1442 }
1443
1444 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1445 static void
1446 setdefif(ifname)
1447 char *ifname;
1448 {
1449 struct in6_ndifreq ndifreq;
1450 unsigned int ifindex;
1451
1452 if (strcasecmp(ifname, "delete") == 0)
1453 ifindex = 0;
1454 else {
1455 if ((ifindex = if_nametoindex(ifname)) == 0)
1456 err(1, "failed to resolve i/f index for %s", ifname);
1457 }
1458
1459 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1460 err(1, "socket");
1461
1462 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1463 ndifreq.ifindex = ifindex;
1464
1465 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1466 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1467
1468 close(s);
1469 }
1470
1471 static void
1472 getdefif()
1473 {
1474 struct in6_ndifreq ndifreq;
1475 char ifname[IFNAMSIZ+8];
1476
1477 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1478 err(1, "socket");
1479
1480 memset(&ndifreq, 0, sizeof(ndifreq));
1481 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1482
1483 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1484 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1485
1486 if (ndifreq.ifindex == 0)
1487 printf("No default interface.\n");
1488 else {
1489 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1490 err(1, "failed to resolve ifname for index %lu",
1491 ndifreq.ifindex);
1492 printf("ND default interface = %s\n", ifname);
1493 }
1494
1495 close(s);
1496 }
1497 #endif
1498
1499 static char *
1500 sec2str(total)
1501 time_t total;
1502 {
1503 static char result[256];
1504 int days, hours, mins, secs;
1505 int first = 1;
1506 char *p = result;
1507 char *ep = &result[sizeof(result)];
1508 int n;
1509
1510 days = total / 3600 / 24;
1511 hours = (total / 3600) % 24;
1512 mins = (total / 60) % 60;
1513 secs = total % 60;
1514
1515 if (days) {
1516 first = 0;
1517 n = snprintf(p, ep - p, "%dd", days);
1518 if (n < 0 || n >= ep - p)
1519 return "?";
1520 p += n;
1521 }
1522 if (!first || hours) {
1523 first = 0;
1524 n = snprintf(p, ep - p, "%dh", hours);
1525 if (n < 0 || n >= ep - p)
1526 return "?";
1527 p += n;
1528 }
1529 if (!first || mins) {
1530 first = 0;
1531 n = snprintf(p, ep - p, "%dm", mins);
1532 if (n < 0 || n >= ep - p)
1533 return "?";
1534 p += n;
1535 }
1536 snprintf(p, ep - p, "%ds", secs);
1537
1538 return(result);
1539 }
1540
1541 /*
1542 * Print the timestamp
1543 * from tcpdump/util.c
1544 */
1545 static void
1546 ts_print(tvp)
1547 const struct timeval *tvp;
1548 {
1549 int s;
1550
1551 /* Default */
1552 s = (tvp->tv_sec + thiszone) % 86400;
1553 (void)printf("%02d:%02d:%02d.%06u ",
1554 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1555 }
1556