ndp.c revision 1.34 1 /* $NetBSD: ndp.c,v 1.34 2006/03/05 23:47:08 rpaulo Exp $ */
2 /* $KAME: ndp.c,v 1.121 2005/07/13 11:30:13 keiichi 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. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * Based on:
66 * "@(#) Copyright (c) 1984, 1993\n\
67 * The Regents of the University of California. All rights reserved.\n";
68 *
69 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
70 */
71
72 /*
73 * ndp - display, set, delete and flush neighbor cache
74 */
75
76
77 #include <sys/param.h>
78 #include <sys/file.h>
79 #include <sys/ioctl.h>
80 #include <sys/socket.h>
81 #include <sys/sysctl.h>
82 #include <sys/time.h>
83
84 #include <net/if.h>
85 #include <net/if_dl.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90
91 #include <netinet/icmp6.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/nd6.h>
94
95 #include <arpa/inet.h>
96
97 #include <netdb.h>
98 #include <errno.h>
99 #include <nlist.h>
100 #include <stdio.h>
101 #include <string.h>
102 #include <paths.h>
103 #include <err.h>
104 #include <stdlib.h>
105 #include <fcntl.h>
106 #include <unistd.h>
107 #include <err.h>
108 #include "gmt2local.h"
109
110 /* packing rule for routing socket */
111 #define ROUNDUP(a) \
112 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
114
115 static pid_t pid;
116 static int nflag;
117 static int tflag;
118 static int32_t thiszone; /* time difference with gmt */
119 static int s = -1;
120 static int repeat = 0;
121
122 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
123 char host_buf[NI_MAXHOST]; /* getnameinfo() */
124 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
125
126 int main __P((int, char **));
127 int file __P((char *));
128 void getsocket __P((void));
129 int set __P((int, char **));
130 void get __P((char *));
131 int delete __P((char *));
132 void dump __P((struct in6_addr *, int));
133 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
134 static char *ether_str __P((struct sockaddr_dl *));
135 int ndp_ether_aton __P((char *, u_char *));
136 void usage __P((void));
137 int rtmsg __P((int));
138 void ifinfo __P((char *, int, char **));
139 void rtrlist __P((void));
140 void plist __P((void));
141 void pfx_flush __P((void));
142 void rtrlist __P((void));
143 void rtr_flush __P((void));
144 void harmonize_rtr __P((void));
145 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
146 static void getdefif __P((void));
147 static void setdefif __P((char *));
148 #endif
149 static char *sec2str __P((time_t));
150 static char *ether_str __P((struct sockaddr_dl *));
151 static void ts_print __P((const struct timeval *));
152
153 #ifdef ICMPV6CTL_ND6_DRLIST
154 static char *rtpref_str[] = {
155 "medium", /* 00 */
156 "high", /* 01 */
157 "rsv", /* 10 */
158 "low" /* 11 */
159 };
160 #endif
161
162 int mode = 0;
163 char *arg = NULL;
164
165 int
166 main(argc, argv)
167 int argc;
168 char **argv;
169 {
170 int ch;
171
172 pid = getpid();
173 thiszone = gmt2local(0);
174 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
175 switch (ch) {
176 case 'a':
177 case 'c':
178 case 'p':
179 case 'r':
180 case 'H':
181 case 'P':
182 case 'R':
183 case 's':
184 case 'I':
185 if (mode) {
186 usage();
187 /*NOTREACHED*/
188 }
189 mode = ch;
190 arg = NULL;
191 break;
192 case 'd':
193 case 'f':
194 case 'i' :
195 if (mode) {
196 usage();
197 /*NOTREACHED*/
198 }
199 mode = ch;
200 arg = optarg;
201 break;
202 case 'n':
203 nflag = 1;
204 break;
205 case 't':
206 tflag = 1;
207 break;
208 case 'A':
209 if (mode) {
210 usage();
211 /*NOTREACHED*/
212 }
213 mode = 'a';
214 repeat = atoi(optarg);
215 if (repeat < 0) {
216 usage();
217 /*NOTREACHED*/
218 }
219 break;
220 default:
221 usage();
222 }
223
224 argc -= optind;
225 argv += optind;
226
227 switch (mode) {
228 case 'a':
229 case 'c':
230 if (argc != 0) {
231 usage();
232 /*NOTREACHED*/
233 }
234 dump(0, mode == 'c');
235 break;
236 case 'd':
237 if (argc != 0) {
238 usage();
239 /*NOTREACHED*/
240 }
241 delete(arg);
242 break;
243 case 'I':
244 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
245 if (argc > 1) {
246 usage();
247 /*NOTREACHED*/
248 } else if (argc == 1) {
249 if (strcmp(*argv, "delete") == 0 ||
250 if_nametoindex(*argv))
251 setdefif(*argv);
252 else
253 errx(1, "invalid interface %s", *argv);
254 }
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, "%49s %49s %49s %49s %49s",
337 arg[0], arg[1], arg[2], 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, "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 err(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 fflush(stdout);
775 sleep(repeat);
776 goto again;
777 }
778 }
779
780 static struct in6_nbrinfo *
781 getnbrinfo(addr, ifindex, warning)
782 struct in6_addr *addr;
783 int ifindex;
784 int warning;
785 {
786 static struct in6_nbrinfo nbi;
787 int s;
788
789 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
790 err(1, "socket");
791
792 bzero(&nbi, sizeof(nbi));
793 if_indextoname(ifindex, nbi.ifname);
794 nbi.addr = *addr;
795 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
796 if (warning)
797 warn("ioctl(SIOCGNBRINFO_IN6)");
798 close(s);
799 return(NULL);
800 }
801
802 close(s);
803 return(&nbi);
804 }
805
806 static char *
807 ether_str(sdl)
808 struct sockaddr_dl *sdl;
809 {
810 static char hbuf[NI_MAXHOST];
811
812 if (sdl->sdl_alen) {
813 if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
814 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
815 snprintf(hbuf, sizeof(hbuf), "<invalid>");
816 } else
817 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
818
819 return(hbuf);
820 }
821
822 int
823 ndp_ether_aton(a, n)
824 char *a;
825 u_char *n;
826 {
827 int i, o[6];
828
829 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
830 &o[3], &o[4], &o[5]);
831 if (i != 6) {
832 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
833 return (1);
834 }
835 for (i = 0; i < 6; i++)
836 n[i] = o[i];
837 return (0);
838 }
839
840 void
841 usage()
842 {
843 printf("usage: ndp [-nt] hostname\n");
844 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
845 printf(" ndp [-nt] -A wait\n");
846 printf(" ndp [-nt] -d hostname\n");
847 printf(" ndp [-nt] -f filename\n");
848 printf(" ndp [-nt] -i interface [flags...]\n");
849 #ifdef SIOCSDEFIFACE_IN6
850 printf(" ndp [-nt] -I [interface|delete]\n");
851 #endif
852 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
853 exit(1);
854 }
855
856 int
857 rtmsg(cmd)
858 int cmd;
859 {
860 static int seq;
861 int rlen;
862 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
863 register char *cp = m_rtmsg.m_space;
864 register int l;
865
866 errno = 0;
867 if (cmd == RTM_DELETE)
868 goto doit;
869 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
870 rtm->rtm_flags = flags;
871 rtm->rtm_version = RTM_VERSION;
872
873 switch (cmd) {
874 default:
875 fprintf(stderr, "ndp: internal wrong cmd\n");
876 exit(1);
877 case RTM_ADD:
878 rtm->rtm_addrs |= RTA_GATEWAY;
879 if (expire_time) {
880 rtm->rtm_rmx.rmx_expire = expire_time;
881 rtm->rtm_inits = RTV_EXPIRE;
882 }
883 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
884 #if 0 /* we don't support ipv6addr/128 type proxying. */
885 if (rtm->rtm_flags & RTF_ANNOUNCE) {
886 rtm->rtm_flags &= ~RTF_HOST;
887 rtm->rtm_addrs |= RTA_NETMASK;
888 }
889 #endif
890 /* FALLTHROUGH */
891 case RTM_GET:
892 rtm->rtm_addrs |= RTA_DST;
893 }
894 #define NEXTADDR(w, s) \
895 if (rtm->rtm_addrs & (w)) { \
896 bcopy((char *)&s, cp, sizeof(s)); ADVANCE(cp, (struct sockaddr *)&s); }
897
898 NEXTADDR(RTA_DST, sin_m);
899 NEXTADDR(RTA_GATEWAY, sdl_m);
900 #if 0 /* we don't support ipv6addr/128 type proxying. */
901 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
902 NEXTADDR(RTA_NETMASK, so_mask);
903 #endif
904
905 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
906 doit:
907 l = rtm->rtm_msglen;
908 rtm->rtm_seq = ++seq;
909 rtm->rtm_type = cmd;
910 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
911 if (errno != ESRCH || cmd != RTM_DELETE) {
912 err(1, "writing to routing socket");
913 /* NOTREACHED */
914 }
915 }
916 do {
917 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
918 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
919 if (l < 0)
920 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
921 strerror(errno));
922 return (0);
923 }
924
925 void
926 ifinfo(ifname, argc, argv)
927 char *ifname;
928 int argc;
929 char **argv;
930 {
931 struct in6_ndireq nd;
932 int i, s;
933 u_int32_t newflags;
934 #ifdef IPV6CTL_USETEMPADDR
935 u_int8_t nullbuf[8];
936 #endif
937
938 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
939 err(1, "socket");
940 /* NOTREACHED */
941 }
942 bzero(&nd, sizeof(nd));
943 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
944 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
945 err(1, "ioctl(SIOCGIFINFO_IN6)");
946 /* NOTREACHED */
947 }
948 #define ND nd.ndi
949 newflags = ND.flags;
950 for (i = 0; i < argc; i++) {
951 int clear = 0;
952 char *cp = argv[i];
953
954 if (*cp == '-') {
955 clear = 1;
956 cp++;
957 }
958
959 #define SETFLAG(s, f) \
960 do {\
961 if (strcmp(cp, (s)) == 0) {\
962 if (clear)\
963 newflags &= ~(f);\
964 else\
965 newflags |= (f);\
966 }\
967 } while (0)
968 /*
969 * XXX: this macro is not 100% correct, in that it matches "nud" against
970 * "nudbogus". But we just let it go since this is minor.
971 */
972 #define SETVALUE(f, v) \
973 do { \
974 char *valptr; \
975 unsigned long newval; \
976 v = 0; /* unspecified */ \
977 if (strncmp(cp, f, strlen(f)) == 0) { \
978 valptr = strchr(cp, '='); \
979 if (valptr == NULL) \
980 err(1, "syntax error in %s field", (f)); \
981 errno = 0; \
982 newval = strtoul(++valptr, NULL, 0); \
983 if (errno) \
984 err(1, "syntax error in %s's value", (f)); \
985 v = newval; \
986 } \
987 } while (0)
988
989 #ifdef ND6_IFF_IFDISABLED
990 SETFLAG("disabled", ND6_IFF_IFDISABLED);
991 #endif
992 SETFLAG("nud", ND6_IFF_PERFORMNUD);
993 #ifdef ND6_IFF_ACCEPT_RTADV
994 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
995 #endif
996 #ifdef ND6_IFF_PREFER_SOURCE
997 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
998 #endif
999 #ifdef ND6_IFF_DONT_SET_IFROUTE
1000 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE);
1001 #endif
1002 SETVALUE("basereachable", ND.basereachable);
1003 SETVALUE("retrans", ND.retrans);
1004 SETVALUE("curhlim", ND.chlim);
1005
1006 ND.flags = newflags;
1007 #ifdef SIOCSIFINFO_IN6
1008 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1009 err(1, "ioctl(SIOCSIFINFO_IN6)");
1010 /* NOTREACHED */
1011 }
1012 #else
1013 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
1014 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
1015 /* NOTREACHED */
1016 }
1017 #endif
1018 #undef SETFLAG
1019 #undef SETVALUE
1020 }
1021
1022 if (!ND.initialized) {
1023 errx(1, "%s: not initialized yet", ifname);
1024 /* NOTREACHED */
1025 }
1026
1027 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1028 err(1, "ioctl(SIOCGIFINFO_IN6)");
1029 /* NOTREACHED */
1030 }
1031 printf("linkmtu=%d", ND.linkmtu);
1032 printf(", maxmtu=%d", ND.maxmtu);
1033 printf(", curhlim=%d", ND.chlim);
1034 printf(", basereachable=%ds%dms",
1035 ND.basereachable / 1000, ND.basereachable % 1000);
1036 printf(", reachable=%ds", ND.reachable);
1037 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1038 #ifdef IPV6CTL_USETEMPADDR
1039 memset(nullbuf, 0, sizeof(nullbuf));
1040 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1041 int j;
1042 u_int8_t *rbuf;
1043
1044 for (i = 0; i < 3; i++) {
1045 switch (i) {
1046 case 0:
1047 printf("\nRandom seed(0): ");
1048 rbuf = ND.randomseed0;
1049 break;
1050 case 1:
1051 printf("\nRandom seed(1): ");
1052 rbuf = ND.randomseed1;
1053 break;
1054 case 2:
1055 printf("\nRandom ID: ");
1056 rbuf = ND.randomid;
1057 break;
1058 default:
1059 errx(1, "impossible case for tempaddr display");
1060 }
1061 for (j = 0; j < 8; j++)
1062 printf("%02x", rbuf[j]);
1063 }
1064 }
1065 #endif
1066 if (ND.flags) {
1067 printf("\nFlags: ");
1068 if ((ND.flags & ND6_IFF_PERFORMNUD))
1069 printf("nud ");
1070 #ifdef ND6_IFF_IFDISABLED
1071 if ((ND.flags & ND6_IFF_IFDISABLED))
1072 printf("disabled ");
1073 #endif
1074 #ifdef ND6_IFF_ACCEPT_RTADV
1075 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1076 printf("accept_rtadv ");
1077 #endif
1078 #ifdef ND6_IFF_PREFER_SOURCE
1079 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1080 printf("prefer_source ");
1081 #endif
1082 }
1083 putc('\n', stdout);
1084 #undef ND
1085
1086 close(s);
1087 }
1088
1089 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1090 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1091 #endif
1092
1093 void
1094 rtrlist()
1095 {
1096 #ifdef ICMPV6CTL_ND6_DRLIST
1097 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1098 char *buf;
1099 struct in6_defrouter *p, *ep;
1100 size_t l;
1101 struct timeval time;
1102
1103 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1104 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1105 /*NOTREACHED*/
1106 }
1107 if (l == 0)
1108 return;
1109 buf = malloc(l);
1110 if (!buf) {
1111 err(1, "malloc");
1112 /*NOTREACHED*/
1113 }
1114 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1115 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1116 /*NOTREACHED*/
1117 }
1118
1119 ep = (struct in6_defrouter *)(buf + l);
1120 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1121 int rtpref;
1122
1123 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1124 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1125 (nflag ? NI_NUMERICHOST : 0)) != 0)
1126 strlcpy(host_buf, "?", sizeof(host_buf));
1127
1128 printf("%s if=%s", host_buf,
1129 if_indextoname(p->if_index, ifix_buf));
1130 printf(", flags=%s%s",
1131 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1132 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1133 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1134 printf(", pref=%s", rtpref_str[rtpref]);
1135
1136 gettimeofday(&time, 0);
1137 if (p->expire == 0)
1138 printf(", expire=Never\n");
1139 else
1140 printf(", expire=%s\n",
1141 sec2str(p->expire - time.tv_sec));
1142 }
1143 free(buf);
1144 #else
1145 struct in6_drlist dr;
1146 int s, i;
1147 struct timeval time;
1148
1149 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1150 err(1, "socket");
1151 /* NOTREACHED */
1152 }
1153 bzero(&dr, sizeof(dr));
1154 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1155 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1156 err(1, "ioctl(SIOCGDRLST_IN6)");
1157 /* NOTREACHED */
1158 }
1159 #define DR dr.defrouter[i]
1160 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1161 struct sockaddr_in6 sin6;
1162
1163 bzero(&sin6, sizeof(sin6));
1164 sin6.sin6_family = AF_INET6;
1165 sin6.sin6_len = sizeof(sin6);
1166 sin6.sin6_addr = DR.rtaddr;
1167 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1168 sizeof(host_buf), NULL, 0,
1169 (nflag ? NI_NUMERICHOST : 0));
1170
1171 printf("%s if=%s", host_buf,
1172 if_indextoname(DR.if_index, ifix_buf));
1173 printf(", flags=%s%s",
1174 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1175 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1176 gettimeofday(&time, 0);
1177 if (DR.expire == 0)
1178 printf(", expire=Never\n");
1179 else
1180 printf(", expire=%s\n",
1181 sec2str(DR.expire - time.tv_sec));
1182 }
1183 #undef DR
1184 close(s);
1185 #endif
1186 }
1187
1188 void
1189 plist()
1190 {
1191 #ifdef ICMPV6CTL_ND6_PRLIST
1192 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1193 char *buf;
1194 struct in6_prefix *p, *ep, *n;
1195 struct sockaddr_in6 *advrtr;
1196 size_t l;
1197 struct timeval time;
1198 const int niflags = NI_NUMERICHOST;
1199 int ninflags = nflag ? NI_NUMERICHOST : 0;
1200 char namebuf[NI_MAXHOST];
1201
1202 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1203 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1204 /*NOTREACHED*/
1205 }
1206 buf = malloc(l);
1207 if (!buf) {
1208 err(1, "malloc");
1209 /*NOTREACHED*/
1210 }
1211 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1212 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1213 /*NOTREACHED*/
1214 }
1215
1216 ep = (struct in6_prefix *)(buf + l);
1217 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1218 advrtr = (struct sockaddr_in6 *)(p + 1);
1219 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1220
1221 if (getnameinfo((struct sockaddr *)&p->prefix,
1222 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1223 NULL, 0, niflags) != 0)
1224 strlcpy(namebuf, "?", sizeof(namebuf));
1225 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1226 if_indextoname(p->if_index, ifix_buf));
1227
1228 gettimeofday(&time, 0);
1229 /*
1230 * meaning of fields, especially flags, is very different
1231 * by origin. notify the difference to the users.
1232 */
1233 printf("flags=%s%s%s%s%s",
1234 p->raflags.onlink ? "L" : "",
1235 p->raflags.autonomous ? "A" : "",
1236 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1237 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1238 #ifdef NDPRF_HOME
1239 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1240 #else
1241 ""
1242 #endif
1243 );
1244 if (p->vltime == ND6_INFINITE_LIFETIME)
1245 printf(" vltime=infinity");
1246 else
1247 printf(" vltime=%lu", (unsigned long)p->vltime);
1248 if (p->pltime == ND6_INFINITE_LIFETIME)
1249 printf(", pltime=infinity");
1250 else
1251 printf(", pltime=%lu", (unsigned long)p->pltime);
1252 if (p->expire == 0)
1253 printf(", expire=Never");
1254 else if (p->expire >= time.tv_sec)
1255 printf(", expire=%s",
1256 sec2str(p->expire - time.tv_sec));
1257 else
1258 printf(", expired");
1259 printf(", ref=%d", p->refcnt);
1260 printf("\n");
1261 /*
1262 * "advertising router" list is meaningful only if the prefix
1263 * information is from RA.
1264 */
1265 if (p->advrtrs) {
1266 int j;
1267 struct sockaddr_in6 *sin6;
1268
1269 sin6 = advrtr;
1270 printf(" advertised by\n");
1271 for (j = 0; j < p->advrtrs; j++) {
1272 struct in6_nbrinfo *nbi;
1273
1274 if (getnameinfo((struct sockaddr *)sin6,
1275 sin6->sin6_len, namebuf, sizeof(namebuf),
1276 NULL, 0, ninflags) != 0)
1277 strlcpy(namebuf, "?", sizeof(namebuf));
1278 printf(" %s", namebuf);
1279
1280 nbi = getnbrinfo(&sin6->sin6_addr,
1281 p->if_index, 0);
1282 if (nbi) {
1283 switch (nbi->state) {
1284 case ND6_LLINFO_REACHABLE:
1285 case ND6_LLINFO_STALE:
1286 case ND6_LLINFO_DELAY:
1287 case ND6_LLINFO_PROBE:
1288 printf(" (reachable)\n");
1289 break;
1290 default:
1291 printf(" (unreachable)\n");
1292 }
1293 } else
1294 printf(" (no neighbor state)\n");
1295 sin6++;
1296 }
1297 } else
1298 printf(" No advertising router\n");
1299 }
1300 free(buf);
1301 #else
1302 struct in6_prlist pr;
1303 int s, i;
1304 struct timeval time;
1305
1306 gettimeofday(&time, 0);
1307
1308 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1309 err(1, "socket");
1310 /* NOTREACHED */
1311 }
1312 bzero(&pr, sizeof(pr));
1313 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1314 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1315 err(1, "ioctl(SIOCGPRLST_IN6)");
1316 /* NOTREACHED */
1317 }
1318 #define PR pr.prefix[i]
1319 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1320 struct sockaddr_in6 p6;
1321 char namebuf[NI_MAXHOST];
1322 int niflags;
1323
1324 #ifdef NDPRF_ONLINK
1325 p6 = PR.prefix;
1326 #else
1327 memset(&p6, 0, sizeof(p6));
1328 p6.sin6_family = AF_INET6;
1329 p6.sin6_len = sizeof(p6);
1330 p6.sin6_addr = PR.prefix;
1331 #endif
1332
1333 niflags = NI_NUMERICHOST;
1334 if (getnameinfo((struct sockaddr *)&p6,
1335 sizeof(p6), namebuf, sizeof(namebuf),
1336 NULL, 0, niflags)) {
1337 warnx("getnameinfo failed");
1338 continue;
1339 }
1340 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1341 if_indextoname(PR.if_index, ifix_buf));
1342
1343 gettimeofday(&time, 0);
1344 /*
1345 * meaning of fields, especially flags, is very different
1346 * by origin. notify the difference to the users.
1347 */
1348 #if 0
1349 printf(" %s",
1350 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1351 #endif
1352 #ifdef NDPRF_ONLINK
1353 printf("flags=%s%s%s%s%s",
1354 PR.raflags.onlink ? "L" : "",
1355 PR.raflags.autonomous ? "A" : "",
1356 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1357 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1358 #ifdef NDPRF_HOME
1359 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1360 #else
1361 ""
1362 #endif
1363 );
1364 #else
1365 printf("flags=%s%s",
1366 PR.raflags.onlink ? "L" : "",
1367 PR.raflags.autonomous ? "A" : "");
1368 #endif
1369 if (PR.vltime == ND6_INFINITE_LIFETIME)
1370 printf(" vltime=infinity");
1371 else
1372 printf(" vltime=%lu", PR.vltime);
1373 if (PR.pltime == ND6_INFINITE_LIFETIME)
1374 printf(", pltime=infinity");
1375 else
1376 printf(", pltime=%lu", PR.pltime);
1377 if (PR.expire == 0)
1378 printf(", expire=Never");
1379 else if (PR.expire >= time.tv_sec)
1380 printf(", expire=%s",
1381 sec2str(PR.expire - time.tv_sec));
1382 else
1383 printf(", expired");
1384 #ifdef NDPRF_ONLINK
1385 printf(", ref=%d", PR.refcnt);
1386 #endif
1387 #if 0
1388 switch (PR.origin) {
1389 case PR_ORIG_RA:
1390 printf(", origin=RA");
1391 break;
1392 case PR_ORIG_RR:
1393 printf(", origin=RR");
1394 break;
1395 case PR_ORIG_STATIC:
1396 printf(", origin=static");
1397 break;
1398 case PR_ORIG_KERNEL:
1399 printf(", origin=kernel");
1400 break;
1401 default:
1402 printf(", origin=?");
1403 break;
1404 }
1405 #endif
1406 printf("\n");
1407 /*
1408 * "advertising router" list is meaningful only if the prefix
1409 * information is from RA.
1410 */
1411 if (0 && /* prefix origin is almost obsolted */
1412 PR.origin != PR_ORIG_RA)
1413 ;
1414 else if (PR.advrtrs) {
1415 int j;
1416 printf(" advertised by\n");
1417 for (j = 0; j < PR.advrtrs; j++) {
1418 struct sockaddr_in6 sin6;
1419 struct in6_nbrinfo *nbi;
1420
1421 bzero(&sin6, sizeof(sin6));
1422 sin6.sin6_family = AF_INET6;
1423 sin6.sin6_len = sizeof(sin6);
1424 sin6.sin6_addr = PR.advrtr[j];
1425 sin6.sin6_scope_id = PR.if_index; /* XXX */
1426 getnameinfo((struct sockaddr *)&sin6,
1427 sin6.sin6_len, host_buf,
1428 sizeof(host_buf), NULL, 0,
1429 (nflag ? NI_NUMERICHOST : 0));
1430 printf(" %s", host_buf);
1431
1432 nbi = getnbrinfo(&sin6.sin6_addr,
1433 PR.if_index, 0);
1434 if (nbi) {
1435 switch (nbi->state) {
1436 case ND6_LLINFO_REACHABLE:
1437 case ND6_LLINFO_STALE:
1438 case ND6_LLINFO_DELAY:
1439 case ND6_LLINFO_PROBE:
1440 printf(" (reachable)\n");
1441 break;
1442 default:
1443 printf(" (unreachable)\n");
1444 }
1445 } else
1446 printf(" (no neighbor state)\n");
1447 }
1448 if (PR.advrtrs > DRLSTSIZ)
1449 printf(" and %d routers\n",
1450 PR.advrtrs - DRLSTSIZ);
1451 } else
1452 printf(" No advertising router\n");
1453 }
1454 #undef PR
1455 close(s);
1456 #endif
1457 }
1458
1459 void
1460 pfx_flush()
1461 {
1462 char dummyif[IFNAMSIZ+8];
1463 int s;
1464
1465 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1466 err(1, "socket");
1467 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1468 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1469 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1470 close(s);
1471 }
1472
1473 void
1474 rtr_flush()
1475 {
1476 char dummyif[IFNAMSIZ+8];
1477 int s;
1478
1479 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1480 err(1, "socket");
1481 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1482 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1483 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1484
1485 close(s);
1486 }
1487
1488 void
1489 harmonize_rtr()
1490 {
1491 char dummyif[IFNAMSIZ+8];
1492 int s;
1493
1494 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1495 err(1, "socket");
1496 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1497 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1498 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1499
1500 close(s);
1501 }
1502
1503 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1504 static void
1505 setdefif(ifname)
1506 char *ifname;
1507 {
1508 struct in6_ndifreq ndifreq;
1509 unsigned int ifindex;
1510
1511 if (strcasecmp(ifname, "delete") == 0)
1512 ifindex = 0;
1513 else {
1514 if ((ifindex = if_nametoindex(ifname)) == 0)
1515 err(1, "failed to resolve i/f index for %s", ifname);
1516 }
1517
1518 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1519 err(1, "socket");
1520
1521 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1522 ndifreq.ifindex = ifindex;
1523
1524 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1525 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1526
1527 close(s);
1528 }
1529
1530 static void
1531 getdefif()
1532 {
1533 struct in6_ndifreq ndifreq;
1534 char ifname[IFNAMSIZ+8];
1535
1536 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1537 err(1, "socket");
1538
1539 memset(&ndifreq, 0, sizeof(ndifreq));
1540 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1541
1542 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1543 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1544
1545 if (ndifreq.ifindex == 0)
1546 printf("No default interface.\n");
1547 else {
1548 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1549 err(1, "failed to resolve ifname for index %lu",
1550 ndifreq.ifindex);
1551 printf("ND default interface = %s\n", ifname);
1552 }
1553
1554 close(s);
1555 }
1556 #endif
1557
1558 static char *
1559 sec2str(total)
1560 time_t total;
1561 {
1562 static char result[256];
1563 int days, hours, mins, secs;
1564 int first = 1;
1565 char *p = result;
1566 char *ep = &result[sizeof(result)];
1567 int n;
1568
1569 days = total / 3600 / 24;
1570 hours = (total / 3600) % 24;
1571 mins = (total / 60) % 60;
1572 secs = total % 60;
1573
1574 if (days) {
1575 first = 0;
1576 n = snprintf(p, ep - p, "%dd", days);
1577 if (n < 0 || n >= ep - p)
1578 return "?";
1579 p += n;
1580 }
1581 if (!first || hours) {
1582 first = 0;
1583 n = snprintf(p, ep - p, "%dh", hours);
1584 if (n < 0 || n >= ep - p)
1585 return "?";
1586 p += n;
1587 }
1588 if (!first || mins) {
1589 first = 0;
1590 n = snprintf(p, ep - p, "%dm", mins);
1591 if (n < 0 || n >= ep - p)
1592 return "?";
1593 p += n;
1594 }
1595 snprintf(p, ep - p, "%ds", secs);
1596
1597 return(result);
1598 }
1599
1600 /*
1601 * Print the timestamp
1602 * from tcpdump/util.c
1603 */
1604 static void
1605 ts_print(tvp)
1606 const struct timeval *tvp;
1607 {
1608 int s;
1609
1610 /* Default */
1611 s = (tvp->tv_sec + thiszone) % 86400;
1612 (void)printf("%02d:%02d:%02d.%06u ",
1613 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1614 }
1615