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