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