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