ndp.c revision 1.9 1 /* $NetBSD: ndp.c,v 1.9 2000/06/20 22:23:02 itojun Exp $ */
2 /* $KAME: ndp.c,v 1.40 2000/06/20 21:50:17 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32 /*
33 * Copyright (c) 1984, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Sun Microsystems, Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 /*
69 * Based on:
70 * "@(#) Copyright (c) 1984, 1993\n\
71 * The Regents of the University of California. All rights reserved.\n";
72 *
73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
74 */
75
76 /*
77 * ndp - display, set, delete and flush neighbor cache
78 */
79
80
81 #include <sys/param.h>
82 #include <sys/file.h>
83 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
86 #include <sys/time.h>
87
88 #include <net/if.h>
89 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
90 #include <net/if_var.h>
91 #endif /* __FreeBSD__ >= 3 */
92 #include <net/if_dl.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
95
96 #include <netinet/in.h>
97 #ifndef __NetBSD__
98 #include <netinet/if_ether.h>
99 #endif
100
101 #include <netinet/icmp6.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet6/nd6.h>
104
105 #include <arpa/inet.h>
106
107 #include <netdb.h>
108 #include <errno.h>
109 #include <nlist.h>
110 #include <stdio.h>
111 #include <string.h>
112 #include <paths.h>
113 #include <err.h>
114 #include <stdlib.h>
115 #include <fcntl.h>
116 #include <unistd.h>
117 #include "gmt2local.h"
118
119 #ifndef NI_WITHSCOPEID
120 #define NI_WITHSCOPEID 0
121 #endif
122
123 /* packing rule for routing socket */
124 #define ROUNDUP(a) \
125 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
126 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
127
128 static int pid;
129 static int fflag;
130 static int nflag;
131 static int tflag;
132 static int32_t thiszone; /* time difference with gmt */
133 static int s = -1;
134 static int repeat = 0;
135 static int lflag = 0;
136
137 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
138 char host_buf[NI_MAXHOST]; /* getnameinfo() */
139 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
140
141 int main __P((int, char **));
142 int file __P((char *));
143 void getsocket __P((void));
144 int set __P((int, char **));
145 void get __P((char *));
146 int delete __P((char *));
147 void dump __P((struct in6_addr *));
148 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
149 int ifindex, int));
150 static char *ether_str __P((struct sockaddr_dl *));
151 int ndp_ether_aton __P((char *, u_char *));
152 void usage __P((void));
153 int rtmsg __P((int));
154 void ifinfo __P((int, char **));
155 void rtrlist __P((void));
156 void plist __P((void));
157 void pfx_flush __P((void));
158 void rtrlist __P((void));
159 void rtr_flush __P((void));
160 void harmonize_rtr __P((void));
161 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
162 static void getdefif __P((void));
163 static void setdefif __P((char *));
164 #endif
165 static char *sec2str __P((time_t t));
166 static char *ether_str __P((struct sockaddr_dl *sdl));
167 static void ts_print __P((const struct timeval *));
168
169 int
170 main(argc, argv)
171 int argc;
172 char **argv;
173 {
174 int ch;
175 int aflag = 0, cflag = 0, dflag = 0, sflag = 0, Hflag = 0,
176 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
177
178 pid = getpid();
179 thiszone = gmt2local(0);
180 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != EOF)
181 switch ((char)ch) {
182 case 'a':
183 aflag = 1;
184 break;
185 case 'c':
186 fflag = 1;
187 cflag = 1;
188 break;
189 case 'd':
190 dflag = 1;
191 break;
192 case 'I':
193 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
194 if (argc > 2)
195 setdefif(argv[2]);
196 getdefif(); /* always call it to print the result */
197 exit(0);
198 #else
199 errx(1, "not supported yet");
200 /*NOTREACHED*/
201 #endif
202 case 'i' :
203 argc -= optind;
204 argv += optind;
205 if (argc < 1)
206 usage();
207 ifinfo(argc, argv);
208 exit(0);
209 case 'n':
210 nflag = 1;
211 continue;
212 case 'p':
213 pflag = 1;
214 break;
215 case 'f' :
216 if (argc != 3)
217 usage();
218 file(argv[2]);
219 exit(0);
220 case 'l' :
221 lflag = 1;
222 break;
223 case 'r' :
224 rflag = 1;
225 break;
226 case 's':
227 sflag = 1;
228 break;
229 case 't':
230 tflag = 1;
231 break;
232 case 'A':
233 aflag = 1;
234 repeat = atoi(optarg);
235 if (repeat < 0)
236 usage();
237 break;
238 case 'H' :
239 Hflag = 1;
240 break;
241 case 'P':
242 Pflag = 1;
243 break;
244 case 'R':
245 Rflag = 1;
246 break;
247 default:
248 usage();
249 }
250
251 argc -= optind;
252 argv += optind;
253
254 if (aflag || cflag) {
255 dump(0);
256 exit(0);
257 }
258 if (dflag) {
259 if (argc != 1)
260 usage();
261 delete(argv[0]);
262 exit(0);
263 }
264 if (pflag) {
265 plist();
266 exit(0);
267 }
268 if (rflag) {
269 rtrlist();
270 exit(0);
271 }
272 if (sflag) {
273 if (argc < 2 || argc > 4)
274 usage();
275 exit(set(argc, argv) ? 1 : 0);
276 }
277 if (Hflag) {
278 harmonize_rtr();
279 exit(0);
280 }
281 if (Pflag) {
282 pfx_flush();
283 exit(0);
284 }
285 if (Rflag) {
286 rtr_flush();
287 exit(0);
288 }
289
290 if (argc != 1)
291 usage();
292 get(argv[0]);
293 exit(0);
294 }
295
296 /*
297 * Process a file to set standard ndp entries
298 */
299 int
300 file(name)
301 char *name;
302 {
303 FILE *fp;
304 int i, retval;
305 char line[100], arg[5][50], *args[5];
306
307 if ((fp = fopen(name, "r")) == NULL) {
308 fprintf(stderr, "ndp: cannot open %s\n", name);
309 exit(1);
310 }
311 args[0] = &arg[0][0];
312 args[1] = &arg[1][0];
313 args[2] = &arg[2][0];
314 args[3] = &arg[3][0];
315 args[4] = &arg[4][0];
316 retval = 0;
317 while(fgets(line, 100, fp) != NULL) {
318 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
319 arg[3], arg[4]);
320 if (i < 2) {
321 fprintf(stderr, "ndp: bad line: %s\n", line);
322 retval = 1;
323 continue;
324 }
325 if (set(i, args))
326 retval = 1;
327 }
328 fclose(fp);
329 return (retval);
330 }
331
332 void
333 getsocket()
334 {
335 if (s < 0) {
336 s = socket(PF_ROUTE, SOCK_RAW, 0);
337 if (s < 0) {
338 perror("ndp: socket");
339 exit(1);
340 }
341 }
342 }
343
344 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
345 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
346 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
347 int expire_time, flags, found_entry;
348 struct {
349 struct rt_msghdr m_rtm;
350 char m_space[512];
351 } m_rtmsg;
352
353 /*
354 * Set an individual neighbor cache entry
355 */
356 int
357 set(argc, argv)
358 int argc;
359 char **argv;
360 {
361 register struct sockaddr_in6 *sin = &sin_m;
362 register struct sockaddr_dl *sdl;
363 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
364 struct addrinfo hints, *res;
365 int gai_error;
366 u_char *ea;
367 char *host = argv[0], *eaddr = argv[1];
368
369 getsocket();
370 argc -= 2;
371 argv += 2;
372 sdl_m = blank_sdl;
373 sin_m = blank_sin;
374
375 bzero(&hints, sizeof(hints));
376 hints.ai_family = AF_INET6;
377 gai_error = getaddrinfo(host, NULL, &hints, &res);
378 if (gai_error) {
379 fprintf(stderr, "ndp: %s: %s\n", host,
380 gai_strerror(gai_error));
381 return 1;
382 }
383 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
384 #ifdef __KAME__
385 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
386 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
387 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
388 }
389 #endif
390 ea = (u_char *)LLADDR(&sdl_m);
391 if (ndp_ether_aton(eaddr, ea) == 0)
392 sdl_m.sdl_alen = 6;
393 flags = expire_time = 0;
394 while (argc-- > 0) {
395 if (strncmp(argv[0], "temp", 4) == 0) {
396 struct timeval time;
397 gettimeofday(&time, 0);
398 expire_time = time.tv_sec + 20 * 60;
399 } else if (strncmp(argv[0], "proxy", 5) == 0)
400 flags |= RTF_ANNOUNCE;
401 argv++;
402 }
403 if (rtmsg(RTM_GET) < 0) {
404 perror(host);
405 return (1);
406 }
407 sin = (struct sockaddr_in6 *)(rtm + 1);
408 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
409 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
410 if (sdl->sdl_family == AF_LINK &&
411 (rtm->rtm_flags & RTF_LLINFO) &&
412 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
413 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
414 case IFT_ISO88024: case IFT_ISO88025:
415 goto overwrite;
416 }
417 /*
418 * IPv4 arp command retries with sin_other = SIN_PROXY here.
419 */
420 fprintf(stderr, "set: cannot configure a new entry\n");
421 return 1;
422 }
423
424 overwrite:
425 if (sdl->sdl_family != AF_LINK) {
426 printf("cannot intuit interface index and type for %s\n", host);
427 return (1);
428 }
429 sdl_m.sdl_type = sdl->sdl_type;
430 sdl_m.sdl_index = sdl->sdl_index;
431 return (rtmsg(RTM_ADD));
432 }
433
434 /*
435 * Display an individual neighbor cache entry
436 */
437 void
438 get(host)
439 char *host;
440 {
441 struct sockaddr_in6 *sin = &sin_m;
442 struct addrinfo hints, *res;
443 int gai_error;
444
445 sin_m = blank_sin;
446 bzero(&hints, sizeof(hints));
447 hints.ai_family = AF_INET6;
448 gai_error = getaddrinfo(host, NULL, &hints, &res);
449 if (gai_error) {
450 fprintf(stderr, "ndp: %s: %s\n", host,
451 gai_strerror(gai_error));
452 return;
453 }
454 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
455 #ifdef __KAME__
456 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
457 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
458 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
459 }
460 #endif
461 dump(&sin->sin6_addr);
462 if (found_entry == 0) {
463 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
464 sizeof(host_buf), NULL ,0,
465 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
466 printf("%s (%s) -- no entry\n", host, host_buf);
467 exit(1);
468 }
469 }
470
471 /*
472 * Delete a neighbor cache entry
473 */
474 int
475 delete(host)
476 char *host;
477 {
478 struct sockaddr_in6 *sin = &sin_m;
479 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
480 struct sockaddr_dl *sdl;
481 struct addrinfo hints, *res;
482 int gai_error;
483
484 getsocket();
485 sin_m = blank_sin;
486
487 bzero(&hints, sizeof(hints));
488 hints.ai_family = AF_INET6;
489 gai_error = getaddrinfo(host, NULL, &hints, &res);
490 if (gai_error) {
491 fprintf(stderr, "ndp: %s: %s\n", host,
492 gai_strerror(gai_error));
493 return 1;
494 }
495 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
496 #ifdef __KAME__
497 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
498 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
499 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
500 }
501 #endif
502 if (rtmsg(RTM_GET) < 0) {
503 perror(host);
504 return (1);
505 }
506 sin = (struct sockaddr_in6 *)(rtm + 1);
507 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
508 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
509 if (sdl->sdl_family == AF_LINK &&
510 (rtm->rtm_flags & RTF_LLINFO) &&
511 !(rtm->rtm_flags & RTF_GATEWAY)) {
512 switch (sdl->sdl_type) {
513 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
514 case IFT_ISO88024: case IFT_ISO88025:
515 goto delete;
516 }
517 }
518 /*
519 * IPv4 arp command retries with sin_other = SIN_PROXY here.
520 */
521 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
522 return 1;
523 }
524
525 delete:
526 if (sdl->sdl_family != AF_LINK) {
527 printf("cannot locate %s\n", host);
528 return (1);
529 }
530 if (rtmsg(RTM_DELETE) == 0) {
531 getnameinfo((struct sockaddr *)sin,
532 sin->sin6_len, host_buf,
533 sizeof(host_buf), NULL, 0,
534 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
535 printf("%s (%s) deleted\n", host, host_buf);
536 }
537
538 return 0;
539 }
540
541 /*
542 * Dump the entire neighbor cache
543 */
544 void
545 dump(addr)
546 struct in6_addr *addr;
547 {
548 int mib[6];
549 size_t needed;
550 char *lim, *buf, *next;
551 struct rt_msghdr *rtm;
552 struct sockaddr_in6 *sin;
553 struct sockaddr_dl *sdl;
554 extern int h_errno;
555 struct in6_nbrinfo *nbi;
556 struct timeval time;
557 int addrwidth;
558 char flgbuf[8];
559
560 /* Print header */
561 if (!tflag)
562 printf("%-31.31s %-17.17s %6.6s %-9.9s %2s %4s %4s\n",
563 "Neighbor", "Linklayer Address", "Netif", "Expire",
564 "St", "Flgs", "Prbs");
565
566 again:;
567 mib[0] = CTL_NET;
568 mib[1] = PF_ROUTE;
569 mib[2] = 0;
570 mib[3] = AF_INET6;
571 mib[4] = NET_RT_FLAGS;
572 mib[5] = RTF_LLINFO;
573 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
574 err(1, "sysctl(PF_ROUTE estimate)");
575 if (needed > 0) {
576 if ((buf = malloc(needed)) == NULL)
577 errx(1, "malloc");
578 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
579 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
580 lim = buf + needed;
581 } else
582 buf = lim = NULL;
583
584 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
585 int isrouter = 0, prbs = 0;
586
587 rtm = (struct rt_msghdr *)next;
588 sin = (struct sockaddr_in6 *)(rtm + 1);
589 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
590 if (addr) {
591 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
592 continue;
593 found_entry = 1;
594 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
595 continue;
596 if (fflag == 1) {
597 delete((char *)inet_ntop(AF_INET6, &sin->sin6_addr,
598 ntop_buf, sizeof(ntop_buf)));
599 continue;
600 }
601
602 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
603 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
604 /* XXX: should scope id be filled in the kernel? */
605 if (sin->sin6_scope_id == 0)
606 sin->sin6_scope_id = sdl->sdl_index;
607
608 /* XXX: KAME specific hack; removed the embedded id */
609 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
610 }
611 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
612 sizeof(host_buf), NULL, 0,
613 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
614 gettimeofday(&time, 0);
615 if (tflag)
616 ts_print(&time);
617
618 if (lflag) {
619 addrwidth = strlen(host_buf);
620 if (addrwidth < 31)
621 addrwidth = 31;
622 } else
623 addrwidth = 31;
624
625 printf("%-*.*s %-17.17s %6.6s", addrwidth, addrwidth, host_buf,
626 ether_str(sdl),
627 if_indextoname(sdl->sdl_index, ifix_buf));
628
629 /* Print neighbor discovery specific informations */
630 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
631 if (nbi) {
632 if (nbi->expire > time.tv_sec) {
633 printf(" %-9.9s",
634 sec2str(nbi->expire - time.tv_sec));
635 }
636 else if (nbi->expire == 0)
637 printf(" %-9.9s", "permanent");
638 else
639 printf(" %-9.9s", "expired");
640
641 switch(nbi->state) {
642 case ND6_LLINFO_NOSTATE:
643 printf(" N");
644 break;
645 case ND6_LLINFO_WAITDELETE:
646 printf(" W");
647 break;
648 case ND6_LLINFO_INCOMPLETE:
649 printf(" I");
650 break;
651 case ND6_LLINFO_REACHABLE:
652 printf(" R");
653 break;
654 case ND6_LLINFO_STALE:
655 printf(" S");
656 break;
657 case ND6_LLINFO_DELAY:
658 printf(" D");
659 break;
660 case ND6_LLINFO_PROBE:
661 printf(" P");
662 break;
663 default:
664 printf(" ?");
665 break;
666 }
667
668 isrouter = nbi->isrouter;
669 prbs = nbi->asked;
670 }
671 else {
672 warnx("failed to get neighbor information");
673 printf(" ");
674 }
675 putchar(' ');
676
677 /*
678 * other flags. R: router, P: proxy, W: ??
679 */
680 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
681 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
682 isrouter ? "R" : "",
683 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
684 } else {
685 sin = (struct sockaddr_in6 *)
686 (sdl->sdl_len + (char *)sdl);
687 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
688 isrouter ? "R" : "",
689 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
690 ? "P" : "",
691 (sin->sin6_len != sizeof(struct sockaddr_in6))
692 ? "W" : "",
693 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
694 }
695 printf(" %-4.4s", flgbuf);
696
697 if (prbs)
698 printf(" %4d", prbs);
699
700 printf("\n");
701 }
702
703 if (repeat) {
704 printf("\n");
705 sleep(repeat);
706 goto again;
707 }
708 }
709
710 static struct in6_nbrinfo *
711 getnbrinfo(addr, ifindex, warning)
712 struct in6_addr *addr;
713 int ifindex;
714 int warning;
715 {
716 static struct in6_nbrinfo nbi;
717 int s;
718
719 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
720 err(1, "socket");
721
722 bzero(&nbi, sizeof(nbi));
723 if_indextoname(ifindex, nbi.ifname);
724 nbi.addr = *addr;
725 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
726 if (warning)
727 warn("ioctl(SIOCGNBRINFO_IN6)");
728 close(s);
729 return(NULL);
730 }
731
732 close(s);
733 return(&nbi);
734 }
735
736 static char *
737 ether_str(sdl)
738 struct sockaddr_dl *sdl;
739 {
740 static char ebuf[32];
741 u_char *cp;
742
743 if (sdl->sdl_alen) {
744 cp = (u_char *)LLADDR(sdl);
745 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
746 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
747 }
748 else {
749 sprintf(ebuf, "(incomplete)");
750 }
751
752 return(ebuf);
753 }
754
755 int
756 ndp_ether_aton(a, n)
757 char *a;
758 u_char *n;
759 {
760 int i, o[6];
761
762 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
763 &o[3], &o[4], &o[5]);
764 if (i != 6) {
765 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
766 return (1);
767 }
768 for (i=0; i<6; i++)
769 n[i] = o[i];
770 return (0);
771 }
772
773 void
774 usage()
775 {
776 printf("usage: ndp hostname\n");
777 printf(" ndp -a[ntl]\n");
778 printf(" ndp [-ntl] -A wait\n");
779 printf(" ndp -c[nt]\n");
780 printf(" ndp -d[nt] hostname\n");
781 printf(" ndp -f[nt] filename\n");
782 printf(" ndp -i interface [flags...]\n");
783 #ifdef SIOCSDEFIFACE_IN6
784 printf(" ndp -I [interface|delete]\n");
785 #endif
786 printf(" ndp -p\n");
787 printf(" ndp -r\n");
788 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
789 printf(" ndp -H\n");
790 printf(" ndp -P\n");
791 printf(" ndp -R\n");
792 exit(1);
793 }
794
795 int
796 rtmsg(cmd)
797 int cmd;
798 {
799 static int seq;
800 int rlen;
801 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
802 register char *cp = m_rtmsg.m_space;
803 register int l;
804
805 errno = 0;
806 if (cmd == RTM_DELETE)
807 goto doit;
808 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
809 rtm->rtm_flags = flags;
810 rtm->rtm_version = RTM_VERSION;
811
812 switch (cmd) {
813 default:
814 fprintf(stderr, "ndp: internal wrong cmd\n");
815 exit(1);
816 case RTM_ADD:
817 rtm->rtm_addrs |= RTA_GATEWAY;
818 rtm->rtm_rmx.rmx_expire = expire_time;
819 rtm->rtm_inits = RTV_EXPIRE;
820 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
821 if (rtm->rtm_flags & RTF_ANNOUNCE) {
822 rtm->rtm_flags &= ~RTF_HOST;
823 rtm->rtm_flags |= RTA_NETMASK;
824 }
825 /* FALLTHROUGH */
826 case RTM_GET:
827 rtm->rtm_addrs |= RTA_DST;
828 }
829 #define NEXTADDR(w, s) \
830 if (rtm->rtm_addrs & (w)) { \
831 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
832
833 NEXTADDR(RTA_DST, sin_m);
834 NEXTADDR(RTA_GATEWAY, sdl_m);
835 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
836 NEXTADDR(RTA_NETMASK, so_mask);
837
838 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
839 doit:
840 l = rtm->rtm_msglen;
841 rtm->rtm_seq = ++seq;
842 rtm->rtm_type = cmd;
843 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
844 if (errno != ESRCH || cmd != RTM_DELETE) {
845 perror("writing to routing socket");
846 return (-1);
847 }
848 }
849 do {
850 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
851 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
852 if (l < 0)
853 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
854 strerror(errno));
855 return (0);
856 }
857
858 void
859 ifinfo(argc, argv)
860 int argc;
861 char **argv;
862 {
863 struct in6_ndireq nd;
864 int i, s;
865 char *ifname = argv[0];
866 u_int32_t newflags;
867
868 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
869 perror("ndp: socket");
870 exit(1);
871 }
872 bzero(&nd, sizeof(nd));
873 strcpy(nd.ifname, ifname);
874 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
875 perror("ioctl (SIOCGIFINFO_IN6)");
876 exit(1);
877 }
878 #define ND nd.ndi
879 newflags = ND.flags;
880 for (i = 1; i < argc; i++) {
881 int clear = 0;
882 char *cp = argv[i];
883
884 if (*cp == '-') {
885 clear = 1;
886 cp++;
887 }
888
889 #define SETFLAG(s, f) \
890 do {\
891 if (strcmp(cp, (s)) == 0) {\
892 if (clear)\
893 newflags &= ~(f);\
894 else\
895 newflags |= (f);\
896 }\
897 } while (0)
898 SETFLAG("nud", ND6_IFF_PERFORMNUD);
899
900 ND.flags = newflags;
901 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
902 perror("ioctl(SIOCSIFINFO_FLAGS)");
903 exit(1);
904 }
905 #undef SETFLAG
906 }
907
908 printf("linkmtu=%d", ND.linkmtu);
909 printf(", curhlim=%d", ND.chlim);
910 printf(", basereachable=%ds%dms",
911 ND.basereachable / 1000, ND.basereachable % 1000);
912 printf(", reachable=%ds", ND.reachable);
913 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
914 if (ND.flags) {
915 printf("\nFlags: ");
916 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
917 printf("PERFORMNUD ");
918 }
919 putc('\n', stdout);
920 #undef ND
921
922 close(s);
923 }
924
925 void
926 rtrlist()
927 {
928 struct in6_drlist dr;
929 int s, i;
930 struct timeval time;
931
932 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
933 perror("ndp: socket");
934 exit(1);
935 }
936 bzero(&dr, sizeof(dr));
937 strcpy(dr.ifname, "lo0"); /* dummy */
938 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
939 perror("ioctl (SIOCGDRLST_IN6)");
940 exit(1);
941 }
942 #define DR dr.defrouter[i]
943 for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
944 struct sockaddr_in6 sin6;
945
946 bzero(&sin6, sizeof(sin6));
947 sin6.sin6_family = AF_INET6;
948 sin6.sin6_len = sizeof(sin6);
949 sin6.sin6_addr = DR.rtaddr;
950 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
951 sizeof(host_buf), NULL, 0,
952 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
953
954 printf("%s if=%s", host_buf,
955 if_indextoname(DR.if_index, ifix_buf));
956 printf(", flags=%s%s",
957 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
958 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
959 gettimeofday(&time, 0);
960 if (DR.expire == 0)
961 printf(", expire=Never\n");
962 else
963 printf(", expire=%s\n",
964 sec2str(DR.expire - time.tv_sec));
965 }
966 #undef DR
967 close(s);
968 }
969
970 void
971 plist()
972 {
973 struct in6_prlist pr;
974 int s, i;
975 struct timeval time;
976
977 gettimeofday(&time, 0);
978
979 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
980 perror("ndp: socket");
981 exit(1);
982 }
983 bzero(&pr, sizeof(pr));
984 strcpy(pr.ifname, "lo0"); /* dummy */
985 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
986 perror("ioctl (SIOCGPRLST_IN6)");
987 exit(1);
988 }
989 #define PR pr.prefix[i]
990 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
991 printf("%s/%d if=%s\n",
992 inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
993 sizeof(ntop_buf)), PR.prefixlen,
994 if_indextoname(PR.if_index, ifix_buf));
995 gettimeofday(&time, 0);
996 /*
997 * meaning of fields, especially flags, is very different
998 * by origin. notify the difference to the users.
999 */
1000 printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1001 printf("flags=%s%s",
1002 PR.raflags.onlink ? "L" : "",
1003 PR.raflags.autonomous ? "A" : "");
1004 if (PR.vltime == ND6_INFINITE_LIFETIME)
1005 printf(" vltime=infinity");
1006 else
1007 printf(" vltime=%ld", (long)PR.vltime);
1008 if (PR.pltime == ND6_INFINITE_LIFETIME)
1009 printf(", pltime=infinity");
1010 else
1011 printf(", pltime=%ld", (long)PR.pltime);
1012 if (PR.expire == 0)
1013 printf(", expire=Never");
1014 else if (PR.expire >= time.tv_sec)
1015 printf(", expire=%s",
1016 sec2str(PR.expire - time.tv_sec));
1017 else
1018 printf(", expired");
1019 switch (PR.origin) {
1020 case PR_ORIG_RA:
1021 printf(", origin=RA");
1022 break;
1023 case PR_ORIG_RR:
1024 printf(", origin=RR");
1025 break;
1026 case PR_ORIG_STATIC:
1027 printf(", origin=static");
1028 break;
1029 case PR_ORIG_KERNEL:
1030 printf(", origin=kernel");
1031 break;
1032 default:
1033 printf(", origin=?");
1034 break;
1035 }
1036 printf("\n");
1037 /*
1038 * "advertising router" list is meaningful only if the prefix
1039 * information is from RA.
1040 */
1041 if (PR.origin != PR_ORIG_RA)
1042 ;
1043 else if (PR.advrtrs) {
1044 int j;
1045 printf(" advertised by\n");
1046 for (j = 0; j < PR.advrtrs; j++) {
1047 struct sockaddr_in6 sin6;
1048 struct in6_nbrinfo *nbi;
1049
1050 bzero(&sin6, sizeof(sin6));
1051 sin6.sin6_family = AF_INET6;
1052 sin6.sin6_len = sizeof(sin6);
1053 sin6.sin6_addr = PR.advrtr[j];
1054 sin6.sin6_scope_id = PR.if_index; /* XXX */
1055 getnameinfo((struct sockaddr *)&sin6,
1056 sin6.sin6_len, host_buf,
1057 sizeof(host_buf), NULL, 0,
1058 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1059 printf(" %s", host_buf);
1060
1061 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1062 0);
1063 if (nbi) {
1064 switch(nbi->state) {
1065 case ND6_LLINFO_REACHABLE:
1066 case ND6_LLINFO_STALE:
1067 case ND6_LLINFO_DELAY:
1068 case ND6_LLINFO_PROBE:
1069 printf(" (reachable)\n");
1070 break;
1071 default:
1072 printf(" (unreachable)\n");
1073 }
1074 }
1075 else
1076 printf(" (no neighbor state)\n");
1077 }
1078 if (PR.advrtrs > DRLSTSIZ)
1079 printf(" and %d routers\n",
1080 PR.advrtrs - DRLSTSIZ);
1081 } else
1082 printf(" No advertising router\n");
1083 }
1084 #undef PR
1085 close(s);
1086 }
1087
1088 void
1089 pfx_flush()
1090 {
1091 char dummyif[IFNAMSIZ+8];
1092 int s;
1093
1094 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1095 err(1, "socket");
1096 strcpy(dummyif, "lo0"); /* dummy */
1097 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1098 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1099 }
1100
1101 void
1102 rtr_flush()
1103 {
1104 char dummyif[IFNAMSIZ+8];
1105 int s;
1106
1107 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1108 err(1, "socket");
1109 strcpy(dummyif, "lo0"); /* dummy */
1110 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1111 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1112
1113 close(s);
1114 }
1115
1116 void
1117 harmonize_rtr()
1118 {
1119 char dummyif[IFNAMSIZ+8];
1120 int s;
1121
1122 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1123 err(1, "socket");
1124 strcpy(dummyif, "lo0"); /* dummy */
1125 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1126 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1127
1128 close(s);
1129 }
1130
1131 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1132 static void
1133 setdefif(ifname)
1134 char *ifname;
1135 {
1136 struct in6_ndifreq ndifreq;
1137 unsigned int ifindex;
1138
1139 if (strcasecmp(ifname, "delete") == 0)
1140 ifindex = 0;
1141 else {
1142 if ((ifindex = if_nametoindex(ifname)) == 0)
1143 err(1, "failed to resolve i/f index for %s", ifname);
1144 }
1145
1146 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1147 err(1, "socket");
1148
1149 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1150 ndifreq.ifindex = ifindex;
1151
1152 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1153 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1154
1155 close(s);
1156 }
1157
1158 static void
1159 getdefif()
1160 {
1161 struct in6_ndifreq ndifreq;
1162 char ifname[IFNAMSIZ+8];
1163
1164 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1165 err(1, "socket");
1166
1167 memset(&ndifreq, 0, sizeof(ndifreq));
1168 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1169
1170 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1171 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1172
1173 if (ndifreq.ifindex == 0)
1174 printf("No default interface.\n");
1175 else {
1176 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1177 err(1, "failed to resolve ifname for index %lu",
1178 ndifreq.ifindex);
1179 printf("ND default interface = %s\n", ifname);
1180 }
1181
1182 close(s);
1183 }
1184 #endif
1185
1186 static char *
1187 sec2str(total)
1188 time_t total;
1189 {
1190 static char result[256];
1191 int days, hours, mins, secs;
1192 int first = 1;
1193 char *p = result;
1194
1195 days = total / 3600 / 24;
1196 hours = (total / 3600) % 24;
1197 mins = (total / 60) % 60;
1198 secs = total % 60;
1199
1200 if (days) {
1201 first = 0;
1202 p += sprintf(p, "%dd", days);
1203 }
1204 if (!first || hours) {
1205 first = 0;
1206 p += sprintf(p, "%dh", hours);
1207 }
1208 if (!first || mins) {
1209 first = 0;
1210 p += sprintf(p, "%dm", mins);
1211 }
1212 sprintf(p, "%ds", secs);
1213
1214 return(result);
1215 }
1216
1217 /*
1218 * Print the timestamp
1219 * from tcpdump/util.c
1220 */
1221 static void
1222 ts_print(tvp)
1223 const struct timeval *tvp;
1224 {
1225 int s;
1226
1227 /* Default */
1228 s = (tvp->tv_sec + thiszone) % 86400;
1229 (void)printf("%02d:%02d:%02d.%06u ",
1230 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1231 }
1232