ndp.c revision 1.11 1 /* $NetBSD: ndp.c,v 1.11 2000/10/10 10:14:36 itojun Exp $ */
2 /* $KAME: ndp.c,v 1.48 2000/10/09 22:08:21 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 #include <sys/queue.h>
88
89 #include <net/if.h>
90 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
91 #include <net/if_var.h>
92 #endif /* __FreeBSD__ >= 3 */
93 #include <net/if_dl.h>
94 #include <net/if_types.h>
95 #include <net/route.h>
96
97 #include <netinet/in.h>
98 #ifndef __NetBSD__
99 #include <netinet/if_ether.h>
100 #endif
101
102 #include <netinet/icmp6.h>
103 #include <netinet6/in6_var.h>
104 #include <netinet6/nd6.h>
105
106 #include <arpa/inet.h>
107
108 #include <netdb.h>
109 #include <errno.h>
110 #include <nlist.h>
111 #include <stdio.h>
112 #include <string.h>
113 #include <paths.h>
114 #include <err.h>
115 #include <stdlib.h>
116 #include <fcntl.h>
117 #include <unistd.h>
118 #include "gmt2local.h"
119
120 #ifndef NI_WITHSCOPEID
121 #define NI_WITHSCOPEID 0
122 #endif
123
124 /* packing rule for routing socket */
125 #define ROUNDUP(a) \
126 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
127 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
128
129 static int pid;
130 static int cflag;
131 static int nflag;
132 static int tflag;
133 static int32_t thiszone; /* time difference with gmt */
134 static int s = -1;
135 static int repeat = 0;
136 static int lflag = 0;
137
138 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
139 char host_buf[NI_MAXHOST]; /* getnameinfo() */
140 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
141
142 int main __P((int, char **));
143 int file __P((char *));
144 void getsocket __P((void));
145 int set __P((int, char **));
146 void get __P((char *));
147 int delete __P((char *));
148 void dump __P((struct in6_addr *));
149 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
150 int ifindex, int));
151 static char *ether_str __P((struct sockaddr_dl *));
152 int ndp_ether_aton __P((char *, u_char *));
153 void usage __P((void));
154 int rtmsg __P((int));
155 void ifinfo __P((int, char **));
156 void rtrlist __P((void));
157 void plist __P((void));
158 void pfx_flush __P((void));
159 void rtrlist __P((void));
160 void rtr_flush __P((void));
161 void harmonize_rtr __P((void));
162 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
163 static void getdefif __P((void));
164 static void setdefif __P((char *));
165 #endif
166 static char *sec2str __P((time_t t));
167 static char *ether_str __P((struct sockaddr_dl *sdl));
168 static void ts_print __P((const struct timeval *));
169
170 int
171 main(argc, argv)
172 int argc;
173 char **argv;
174 {
175 int ch;
176 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
177 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
178
179 pid = getpid();
180 thiszone = gmt2local(0);
181 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != EOF)
182 switch ((char)ch) {
183 case 'a':
184 aflag = 1;
185 break;
186 case 'c':
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 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
532
533 #ifdef __KAME__
534 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
535 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
536 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
537 }
538 #endif
539 getnameinfo((struct sockaddr *)&s6,
540 s6.sin6_len, host_buf,
541 sizeof(host_buf), NULL, 0,
542 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
543 printf("%s (%s) deleted\n", host, host_buf);
544 }
545
546 return 0;
547 }
548
549 /*
550 * Dump the entire neighbor cache
551 */
552 void
553 dump(addr)
554 struct in6_addr *addr;
555 {
556 int mib[6];
557 size_t needed;
558 char *lim, *buf, *next;
559 struct rt_msghdr *rtm;
560 struct sockaddr_in6 *sin;
561 struct sockaddr_dl *sdl;
562 extern int h_errno;
563 struct in6_nbrinfo *nbi;
564 struct timeval time;
565 int addrwidth;
566 char flgbuf[8];
567
568 /* Print header */
569 if (!tflag && !cflag)
570 printf("%-31.31s %-17.17s %6.6s %-9.9s %2s %4s %4s\n",
571 "Neighbor", "Linklayer Address", "Netif", "Expire",
572 "St", "Flgs", "Prbs");
573
574 again:;
575 mib[0] = CTL_NET;
576 mib[1] = PF_ROUTE;
577 mib[2] = 0;
578 mib[3] = AF_INET6;
579 mib[4] = NET_RT_FLAGS;
580 mib[5] = RTF_LLINFO;
581 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
582 err(1, "sysctl(PF_ROUTE estimate)");
583 if (needed > 0) {
584 if ((buf = malloc(needed)) == NULL)
585 errx(1, "malloc");
586 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
587 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
588 lim = buf + needed;
589 } else
590 buf = lim = NULL;
591
592 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
593 int isrouter = 0, prbs = 0;
594
595 rtm = (struct rt_msghdr *)next;
596 sin = (struct sockaddr_in6 *)(rtm + 1);
597 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
598 if (addr) {
599 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
600 continue;
601 found_entry = 1;
602 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
603 continue;
604 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
605 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
606 /* XXX: should scope id be filled in the kernel? */
607 if (sin->sin6_scope_id == 0)
608 sin->sin6_scope_id = sdl->sdl_index;
609 #ifdef __KAME__
610 /* KAME specific hack; removed the embedded id */
611 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
612 #endif
613 }
614 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
615 sizeof(host_buf), NULL, 0,
616 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
617 if (cflag == 1) {
618 delete(host_buf);
619 continue;
620 }
621 gettimeofday(&time, 0);
622 if (tflag)
623 ts_print(&time);
624
625 if (lflag) {
626 addrwidth = strlen(host_buf);
627 if (addrwidth < 31)
628 addrwidth = 31;
629 } else
630 addrwidth = 31;
631
632 printf("%-*.*s %-17.17s %6.6s", addrwidth, addrwidth, host_buf,
633 ether_str(sdl),
634 if_indextoname(sdl->sdl_index, ifix_buf));
635
636 /* Print neighbor discovery specific informations */
637 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
638 if (nbi) {
639 if (nbi->expire > time.tv_sec) {
640 printf(" %-9.9s",
641 sec2str(nbi->expire - time.tv_sec));
642 } else if (nbi->expire == 0)
643 printf(" %-9.9s", "permanent");
644 else
645 printf(" %-9.9s", "expired");
646
647 switch(nbi->state) {
648 case ND6_LLINFO_NOSTATE:
649 printf(" N");
650 break;
651 #ifdef ND6_LLINFO_WAITDELETE
652 case ND6_LLINFO_WAITDELETE:
653 printf(" W");
654 break;
655 #endif
656 case ND6_LLINFO_INCOMPLETE:
657 printf(" I");
658 break;
659 case ND6_LLINFO_REACHABLE:
660 printf(" R");
661 break;
662 case ND6_LLINFO_STALE:
663 printf(" S");
664 break;
665 case ND6_LLINFO_DELAY:
666 printf(" D");
667 break;
668 case ND6_LLINFO_PROBE:
669 printf(" P");
670 break;
671 default:
672 printf(" ?");
673 break;
674 }
675
676 isrouter = nbi->isrouter;
677 prbs = nbi->asked;
678 } else {
679 warnx("failed to get neighbor information");
680 printf(" ");
681 }
682 putchar(' ');
683
684 /*
685 * other flags. R: router, P: proxy, W: ??
686 */
687 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
688 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
689 isrouter ? "R" : "",
690 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
691 } else {
692 sin = (struct sockaddr_in6 *)
693 (sdl->sdl_len + (char *)sdl);
694 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
695 isrouter ? "R" : "",
696 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
697 ? "P" : "",
698 (sin->sin6_len != sizeof(struct sockaddr_in6))
699 ? "W" : "",
700 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
701 }
702 printf(" %-4.4s", flgbuf);
703
704 if (prbs)
705 printf(" %4d", prbs);
706
707 printf("\n");
708 }
709
710 if (repeat) {
711 printf("\n");
712 sleep(repeat);
713 goto again;
714 }
715 }
716
717 static struct in6_nbrinfo *
718 getnbrinfo(addr, ifindex, warning)
719 struct in6_addr *addr;
720 int ifindex;
721 int warning;
722 {
723 static struct in6_nbrinfo nbi;
724 int s;
725
726 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
727 err(1, "socket");
728
729 bzero(&nbi, sizeof(nbi));
730 if_indextoname(ifindex, nbi.ifname);
731 nbi.addr = *addr;
732 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
733 if (warning)
734 warn("ioctl(SIOCGNBRINFO_IN6)");
735 close(s);
736 return(NULL);
737 }
738
739 close(s);
740 return(&nbi);
741 }
742
743 static char *
744 ether_str(sdl)
745 struct sockaddr_dl *sdl;
746 {
747 static char ebuf[32];
748 u_char *cp;
749
750 if (sdl->sdl_alen) {
751 cp = (u_char *)LLADDR(sdl);
752 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
753 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
754 } else {
755 sprintf(ebuf, "(incomplete)");
756 }
757
758 return(ebuf);
759 }
760
761 int
762 ndp_ether_aton(a, n)
763 char *a;
764 u_char *n;
765 {
766 int i, o[6];
767
768 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
769 &o[3], &o[4], &o[5]);
770 if (i != 6) {
771 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
772 return (1);
773 }
774 for (i=0; i<6; i++)
775 n[i] = o[i];
776 return (0);
777 }
778
779 void
780 usage()
781 {
782 printf("usage: ndp hostname\n");
783 printf(" ndp -a[ntl]\n");
784 printf(" ndp [-ntl] -A wait\n");
785 printf(" ndp -c[nt]\n");
786 printf(" ndp -d[nt] hostname\n");
787 printf(" ndp -f[nt] filename\n");
788 printf(" ndp -i interface [flags...]\n");
789 #ifdef SIOCSDEFIFACE_IN6
790 printf(" ndp -I [interface|delete]\n");
791 #endif
792 printf(" ndp -p\n");
793 printf(" ndp -r\n");
794 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
795 printf(" ndp -H\n");
796 printf(" ndp -P\n");
797 printf(" ndp -R\n");
798 exit(1);
799 }
800
801 int
802 rtmsg(cmd)
803 int cmd;
804 {
805 static int seq;
806 int rlen;
807 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
808 register char *cp = m_rtmsg.m_space;
809 register int l;
810
811 errno = 0;
812 if (cmd == RTM_DELETE)
813 goto doit;
814 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
815 rtm->rtm_flags = flags;
816 rtm->rtm_version = RTM_VERSION;
817
818 switch (cmd) {
819 default:
820 fprintf(stderr, "ndp: internal wrong cmd\n");
821 exit(1);
822 case RTM_ADD:
823 rtm->rtm_addrs |= RTA_GATEWAY;
824 rtm->rtm_rmx.rmx_expire = expire_time;
825 rtm->rtm_inits = RTV_EXPIRE;
826 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
827 if (rtm->rtm_flags & RTF_ANNOUNCE) {
828 rtm->rtm_flags &= ~RTF_HOST;
829 rtm->rtm_flags |= RTA_NETMASK;
830 }
831 /* FALLTHROUGH */
832 case RTM_GET:
833 rtm->rtm_addrs |= RTA_DST;
834 }
835 #define NEXTADDR(w, s) \
836 if (rtm->rtm_addrs & (w)) { \
837 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
838
839 NEXTADDR(RTA_DST, sin_m);
840 NEXTADDR(RTA_GATEWAY, sdl_m);
841 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
842 NEXTADDR(RTA_NETMASK, so_mask);
843
844 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
845 doit:
846 l = rtm->rtm_msglen;
847 rtm->rtm_seq = ++seq;
848 rtm->rtm_type = cmd;
849 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
850 if (errno != ESRCH || cmd != RTM_DELETE) {
851 perror("writing to routing socket");
852 return (-1);
853 }
854 }
855 do {
856 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
857 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
858 if (l < 0)
859 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
860 strerror(errno));
861 return (0);
862 }
863
864 void
865 ifinfo(argc, argv)
866 int argc;
867 char **argv;
868 {
869 struct in6_ndireq nd;
870 int i, s;
871 char *ifname = argv[0];
872 u_int32_t newflags;
873
874 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
875 perror("ndp: socket");
876 exit(1);
877 }
878 bzero(&nd, sizeof(nd));
879 strcpy(nd.ifname, ifname);
880 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
881 perror("ioctl (SIOCGIFINFO_IN6)");
882 exit(1);
883 }
884 #define ND nd.ndi
885 newflags = ND.flags;
886 for (i = 1; i < argc; i++) {
887 int clear = 0;
888 char *cp = argv[i];
889
890 if (*cp == '-') {
891 clear = 1;
892 cp++;
893 }
894
895 #define SETFLAG(s, f) \
896 do {\
897 if (strcmp(cp, (s)) == 0) {\
898 if (clear)\
899 newflags &= ~(f);\
900 else\
901 newflags |= (f);\
902 }\
903 } while (0)
904 SETFLAG("nud", ND6_IFF_PERFORMNUD);
905
906 ND.flags = newflags;
907 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
908 perror("ioctl(SIOCSIFINFO_FLAGS)");
909 exit(1);
910 }
911 #undef SETFLAG
912 }
913
914 printf("linkmtu=%d", ND.linkmtu);
915 printf(", curhlim=%d", ND.chlim);
916 printf(", basereachable=%ds%dms",
917 ND.basereachable / 1000, ND.basereachable % 1000);
918 printf(", reachable=%ds", ND.reachable);
919 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
920 if (ND.flags) {
921 printf("\nFlags: ");
922 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
923 printf("PERFORMNUD ");
924 }
925 putc('\n', stdout);
926 #undef ND
927
928 close(s);
929 }
930
931 void
932 rtrlist()
933 {
934 struct in6_drlist dr;
935 int s, i;
936 struct timeval time;
937
938 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
939 perror("ndp: socket");
940 exit(1);
941 }
942 bzero(&dr, sizeof(dr));
943 strcpy(dr.ifname, "lo0"); /* dummy */
944 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
945 perror("ioctl (SIOCGDRLST_IN6)");
946 exit(1);
947 }
948 #define DR dr.defrouter[i]
949 for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
950 struct sockaddr_in6 sin6;
951
952 bzero(&sin6, sizeof(sin6));
953 sin6.sin6_family = AF_INET6;
954 sin6.sin6_len = sizeof(sin6);
955 sin6.sin6_addr = DR.rtaddr;
956 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
957 sizeof(host_buf), NULL, 0,
958 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
959
960 printf("%s if=%s", host_buf,
961 if_indextoname(DR.if_index, ifix_buf));
962 printf(", flags=%s%s",
963 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
964 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
965 gettimeofday(&time, 0);
966 if (DR.expire == 0)
967 printf(", expire=Never\n");
968 else
969 printf(", expire=%s\n",
970 sec2str(DR.expire - time.tv_sec));
971 }
972 #undef DR
973 close(s);
974 }
975
976 void
977 plist()
978 {
979 struct in6_prlist pr;
980 int s, i;
981 struct timeval time;
982
983 gettimeofday(&time, 0);
984
985 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
986 perror("ndp: socket");
987 exit(1);
988 }
989 bzero(&pr, sizeof(pr));
990 strcpy(pr.ifname, "lo0"); /* dummy */
991 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
992 perror("ioctl (SIOCGPRLST_IN6)");
993 exit(1);
994 }
995 #define PR pr.prefix[i]
996 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
997 printf("%s/%d if=%s\n",
998 inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
999 sizeof(ntop_buf)), PR.prefixlen,
1000 if_indextoname(PR.if_index, ifix_buf));
1001 gettimeofday(&time, 0);
1002 /*
1003 * meaning of fields, especially flags, is very different
1004 * by origin. notify the difference to the users.
1005 */
1006 printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1007 printf("flags=%s%s",
1008 PR.raflags.onlink ? "L" : "",
1009 PR.raflags.autonomous ? "A" : "");
1010 if (PR.vltime == ND6_INFINITE_LIFETIME)
1011 printf(" vltime=infinity");
1012 else
1013 printf(" vltime=%ld", (long)PR.vltime);
1014 if (PR.pltime == ND6_INFINITE_LIFETIME)
1015 printf(", pltime=infinity");
1016 else
1017 printf(", pltime=%ld", (long)PR.pltime);
1018 if (PR.expire == 0)
1019 printf(", expire=Never");
1020 else if (PR.expire >= time.tv_sec)
1021 printf(", expire=%s",
1022 sec2str(PR.expire - time.tv_sec));
1023 else
1024 printf(", expired");
1025 switch (PR.origin) {
1026 case PR_ORIG_RA:
1027 printf(", origin=RA");
1028 break;
1029 case PR_ORIG_RR:
1030 printf(", origin=RR");
1031 break;
1032 case PR_ORIG_STATIC:
1033 printf(", origin=static");
1034 break;
1035 case PR_ORIG_KERNEL:
1036 printf(", origin=kernel");
1037 break;
1038 default:
1039 printf(", origin=?");
1040 break;
1041 }
1042 printf("\n");
1043 /*
1044 * "advertising router" list is meaningful only if the prefix
1045 * information is from RA.
1046 */
1047 if (PR.origin != PR_ORIG_RA)
1048 ;
1049 else if (PR.advrtrs) {
1050 int j;
1051 printf(" advertised by\n");
1052 for (j = 0; j < PR.advrtrs; j++) {
1053 struct sockaddr_in6 sin6;
1054 struct in6_nbrinfo *nbi;
1055
1056 bzero(&sin6, sizeof(sin6));
1057 sin6.sin6_family = AF_INET6;
1058 sin6.sin6_len = sizeof(sin6);
1059 sin6.sin6_addr = PR.advrtr[j];
1060 sin6.sin6_scope_id = PR.if_index; /* XXX */
1061 getnameinfo((struct sockaddr *)&sin6,
1062 sin6.sin6_len, host_buf,
1063 sizeof(host_buf), NULL, 0,
1064 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1065 printf(" %s", host_buf);
1066
1067 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1068 0);
1069 if (nbi) {
1070 switch(nbi->state) {
1071 case ND6_LLINFO_REACHABLE:
1072 case ND6_LLINFO_STALE:
1073 case ND6_LLINFO_DELAY:
1074 case ND6_LLINFO_PROBE:
1075 printf(" (reachable)\n");
1076 break;
1077 default:
1078 printf(" (unreachable)\n");
1079 }
1080 } else
1081 printf(" (no neighbor state)\n");
1082 }
1083 if (PR.advrtrs > DRLSTSIZ)
1084 printf(" and %d routers\n",
1085 PR.advrtrs - DRLSTSIZ);
1086 } else
1087 printf(" No advertising router\n");
1088 }
1089 #undef PR
1090 close(s);
1091 }
1092
1093 void
1094 pfx_flush()
1095 {
1096 char dummyif[IFNAMSIZ+8];
1097 int s;
1098
1099 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1100 err(1, "socket");
1101 strcpy(dummyif, "lo0"); /* dummy */
1102 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1103 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1104 }
1105
1106 void
1107 rtr_flush()
1108 {
1109 char dummyif[IFNAMSIZ+8];
1110 int s;
1111
1112 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1113 err(1, "socket");
1114 strcpy(dummyif, "lo0"); /* dummy */
1115 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1116 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1117
1118 close(s);
1119 }
1120
1121 void
1122 harmonize_rtr()
1123 {
1124 char dummyif[IFNAMSIZ+8];
1125 int s;
1126
1127 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1128 err(1, "socket");
1129 strcpy(dummyif, "lo0"); /* dummy */
1130 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1131 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1132
1133 close(s);
1134 }
1135
1136 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1137 static void
1138 setdefif(ifname)
1139 char *ifname;
1140 {
1141 struct in6_ndifreq ndifreq;
1142 unsigned int ifindex;
1143
1144 if (strcasecmp(ifname, "delete") == 0)
1145 ifindex = 0;
1146 else {
1147 if ((ifindex = if_nametoindex(ifname)) == 0)
1148 err(1, "failed to resolve i/f index for %s", ifname);
1149 }
1150
1151 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1152 err(1, "socket");
1153
1154 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1155 ndifreq.ifindex = ifindex;
1156
1157 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1158 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1159
1160 close(s);
1161 }
1162
1163 static void
1164 getdefif()
1165 {
1166 struct in6_ndifreq ndifreq;
1167 char ifname[IFNAMSIZ+8];
1168
1169 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1170 err(1, "socket");
1171
1172 memset(&ndifreq, 0, sizeof(ndifreq));
1173 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1174
1175 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1176 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1177
1178 if (ndifreq.ifindex == 0)
1179 printf("No default interface.\n");
1180 else {
1181 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1182 err(1, "failed to resolve ifname for index %lu",
1183 ndifreq.ifindex);
1184 printf("ND default interface = %s\n", ifname);
1185 }
1186
1187 close(s);
1188 }
1189 #endif
1190
1191 static char *
1192 sec2str(total)
1193 time_t total;
1194 {
1195 static char result[256];
1196 int days, hours, mins, secs;
1197 int first = 1;
1198 char *p = result;
1199
1200 days = total / 3600 / 24;
1201 hours = (total / 3600) % 24;
1202 mins = (total / 60) % 60;
1203 secs = total % 60;
1204
1205 if (days) {
1206 first = 0;
1207 p += sprintf(p, "%dd", days);
1208 }
1209 if (!first || hours) {
1210 first = 0;
1211 p += sprintf(p, "%dh", hours);
1212 }
1213 if (!first || mins) {
1214 first = 0;
1215 p += sprintf(p, "%dm", mins);
1216 }
1217 sprintf(p, "%ds", secs);
1218
1219 return(result);
1220 }
1221
1222 /*
1223 * Print the timestamp
1224 * from tcpdump/util.c
1225 */
1226 static void
1227 ts_print(tvp)
1228 const struct timeval *tvp;
1229 {
1230 int s;
1231
1232 /* Default */
1233 s = (tvp->tv_sec + thiszone) % 86400;
1234 (void)printf("%02d:%02d:%02d.%06u ",
1235 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1236 }
1237