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