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