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