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