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