ndp.c revision 1.14 1 /* $NetBSD: ndp.c,v 1.14 2001/03/21 15:28:29 itojun Exp $ */
2 /* $KAME: ndp.c,v 1.62 2001/03/21 15:26:32 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
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, 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 cflag = 1;
187 break;
188 case 'd':
189 dflag = 1;
190 break;
191 case 'I':
192 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
193 if (argc > 2)
194 setdefif(argv[2]);
195 getdefif(); /* always call it to print the result */
196 exit(0);
197 #else
198 errx(1, "not supported yet");
199 /*NOTREACHED*/
200 #endif
201 case 'i' :
202 argc -= optind;
203 argv += optind;
204 if (argc < 1)
205 usage();
206 ifinfo(argc, argv);
207 exit(0);
208 case 'n':
209 nflag = 1;
210 continue;
211 case 'p':
212 pflag = 1;
213 break;
214 case 'f' :
215 if (argc != 3)
216 usage();
217 file(argv[2]);
218 exit(0);
219 case 'l' :
220 /* obsolete, ignored */
221 break;
222 case 'r' :
223 rflag = 1;
224 break;
225 case 's':
226 sflag = 1;
227 break;
228 case 't':
229 tflag = 1;
230 break;
231 case 'A':
232 aflag = 1;
233 repeat = atoi(optarg);
234 if (repeat < 0)
235 usage();
236 break;
237 case 'H' :
238 Hflag = 1;
239 break;
240 case 'P':
241 Pflag = 1;
242 break;
243 case 'R':
244 Rflag = 1;
245 break;
246 default:
247 usage();
248 }
249
250 argc -= optind;
251 argv += optind;
252
253 if (aflag || cflag) {
254 dump(0);
255 exit(0);
256 }
257 if (dflag) {
258 if (argc != 1)
259 usage();
260 delete(argv[0]);
261 exit(0);
262 }
263 if (pflag) {
264 plist();
265 exit(0);
266 }
267 if (rflag) {
268 rtrlist();
269 exit(0);
270 }
271 if (sflag) {
272 if (argc < 2 || argc > 4)
273 usage();
274 exit(set(argc, argv) ? 1 : 0);
275 }
276 if (Hflag) {
277 harmonize_rtr();
278 exit(0);
279 }
280 if (Pflag) {
281 pfx_flush();
282 exit(0);
283 }
284 if (Rflag) {
285 rtr_flush();
286 exit(0);
287 }
288
289 if (argc != 1)
290 usage();
291 get(argv[0]);
292 exit(0);
293 }
294
295 /*
296 * Process a file to set standard ndp entries
297 */
298 int
299 file(name)
300 char *name;
301 {
302 FILE *fp;
303 int i, retval;
304 char line[100], arg[5][50], *args[5];
305
306 if ((fp = fopen(name, "r")) == NULL) {
307 fprintf(stderr, "ndp: cannot open %s\n", name);
308 exit(1);
309 }
310 args[0] = &arg[0][0];
311 args[1] = &arg[1][0];
312 args[2] = &arg[2][0];
313 args[3] = &arg[3][0];
314 args[4] = &arg[4][0];
315 retval = 0;
316 while(fgets(line, 100, fp) != NULL) {
317 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
318 arg[3], arg[4]);
319 if (i < 2) {
320 fprintf(stderr, "ndp: bad line: %s\n", line);
321 retval = 1;
322 continue;
323 }
324 if (set(i, args))
325 retval = 1;
326 }
327 fclose(fp);
328 return (retval);
329 }
330
331 void
332 getsocket()
333 {
334 if (s < 0) {
335 s = socket(PF_ROUTE, SOCK_RAW, 0);
336 if (s < 0) {
337 perror("ndp: socket");
338 exit(1);
339 }
340 }
341 }
342
343 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
344 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
345 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
346 int expire_time, flags, found_entry;
347 struct {
348 struct rt_msghdr m_rtm;
349 char m_space[512];
350 } m_rtmsg;
351
352 /*
353 * Set an individual neighbor cache entry
354 */
355 int
356 set(argc, argv)
357 int argc;
358 char **argv;
359 {
360 register struct sockaddr_in6 *sin = &sin_m;
361 register struct sockaddr_dl *sdl;
362 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
363 struct addrinfo hints, *res;
364 int gai_error;
365 u_char *ea;
366 char *host = argv[0], *eaddr = argv[1];
367
368 getsocket();
369 argc -= 2;
370 argv += 2;
371 sdl_m = blank_sdl;
372 sin_m = blank_sin;
373
374 bzero(&hints, sizeof(hints));
375 hints.ai_family = AF_INET6;
376 gai_error = getaddrinfo(host, NULL, &hints, &res);
377 if (gai_error) {
378 fprintf(stderr, "ndp: %s: %s\n", host,
379 gai_strerror(gai_error));
380 return 1;
381 }
382 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
383 #ifdef __KAME__
384 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
385 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
386 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
387 }
388 #endif
389 ea = (u_char *)LLADDR(&sdl_m);
390 if (ndp_ether_aton(eaddr, ea) == 0)
391 sdl_m.sdl_alen = 6;
392 flags = expire_time = 0;
393 while (argc-- > 0) {
394 if (strncmp(argv[0], "temp", 4) == 0) {
395 struct timeval time;
396 gettimeofday(&time, 0);
397 expire_time = time.tv_sec + 20 * 60;
398 } else if (strncmp(argv[0], "proxy", 5) == 0)
399 flags |= RTF_ANNOUNCE;
400 argv++;
401 }
402 if (rtmsg(RTM_GET) < 0) {
403 perror(host);
404 return (1);
405 }
406 sin = (struct sockaddr_in6 *)(rtm + 1);
407 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
408 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
409 if (sdl->sdl_family == AF_LINK &&
410 (rtm->rtm_flags & RTF_LLINFO) &&
411 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
412 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
413 case IFT_ISO88024: case IFT_ISO88025:
414 goto overwrite;
415 }
416 /*
417 * IPv4 arp command retries with sin_other = SIN_PROXY here.
418 */
419 fprintf(stderr, "set: cannot configure a new entry\n");
420 return 1;
421 }
422
423 overwrite:
424 if (sdl->sdl_family != AF_LINK) {
425 printf("cannot intuit interface index and type for %s\n", host);
426 return (1);
427 }
428 sdl_m.sdl_type = sdl->sdl_type;
429 sdl_m.sdl_index = sdl->sdl_index;
430 return (rtmsg(RTM_ADD));
431 }
432
433 /*
434 * Display an individual neighbor cache entry
435 */
436 void
437 get(host)
438 char *host;
439 {
440 struct sockaddr_in6 *sin = &sin_m;
441 struct addrinfo hints, *res;
442 int gai_error;
443
444 sin_m = blank_sin;
445 bzero(&hints, sizeof(hints));
446 hints.ai_family = AF_INET6;
447 gai_error = getaddrinfo(host, NULL, &hints, &res);
448 if (gai_error) {
449 fprintf(stderr, "ndp: %s: %s\n", host,
450 gai_strerror(gai_error));
451 return;
452 }
453 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
454 #ifdef __KAME__
455 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
456 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
457 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
458 }
459 #endif
460 dump(&sin->sin6_addr);
461 if (found_entry == 0) {
462 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
463 sizeof(host_buf), NULL ,0,
464 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
465 printf("%s (%s) -- no entry\n", host, host_buf);
466 exit(1);
467 }
468 }
469
470 /*
471 * Delete a neighbor cache entry
472 */
473 int
474 delete(host)
475 char *host;
476 {
477 struct sockaddr_in6 *sin = &sin_m;
478 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
479 struct sockaddr_dl *sdl;
480 struct addrinfo hints, *res;
481 int gai_error;
482
483 getsocket();
484 sin_m = blank_sin;
485
486 bzero(&hints, sizeof(hints));
487 hints.ai_family = AF_INET6;
488 gai_error = getaddrinfo(host, NULL, &hints, &res);
489 if (gai_error) {
490 fprintf(stderr, "ndp: %s: %s\n", host,
491 gai_strerror(gai_error));
492 return 1;
493 }
494 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
495 #ifdef __KAME__
496 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
497 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
498 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
499 }
500 #endif
501 if (rtmsg(RTM_GET) < 0) {
502 perror(host);
503 return (1);
504 }
505 sin = (struct sockaddr_in6 *)(rtm + 1);
506 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
507 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
508 if (sdl->sdl_family == AF_LINK &&
509 (rtm->rtm_flags & RTF_LLINFO) &&
510 !(rtm->rtm_flags & RTF_GATEWAY)) {
511 goto delete;
512 }
513 /*
514 * IPv4 arp command retries with sin_other = SIN_PROXY here.
515 */
516 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
517 return 1;
518 }
519
520 delete:
521 if (sdl->sdl_family != AF_LINK) {
522 printf("cannot locate %s\n", host);
523 return (1);
524 }
525 if (rtmsg(RTM_DELETE) == 0) {
526 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
527
528 #ifdef __KAME__
529 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
530 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
531 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
532 }
533 #endif
534 getnameinfo((struct sockaddr *)&s6,
535 s6.sin6_len, host_buf,
536 sizeof(host_buf), NULL, 0,
537 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
538 printf("%s (%s) deleted\n", host, host_buf);
539 }
540
541 return 0;
542 }
543
544 #define W_ADDR 31
545 #define W_LL 17
546 #define W_IF 6
547
548 /*
549 * Dump the entire neighbor cache
550 */
551 void
552 dump(addr)
553 struct in6_addr *addr;
554 {
555 int mib[6];
556 size_t needed;
557 char *lim, *buf, *next;
558 struct rt_msghdr *rtm;
559 struct sockaddr_in6 *sin;
560 struct sockaddr_dl *sdl;
561 extern int h_errno;
562 struct in6_nbrinfo *nbi;
563 struct timeval time;
564 int addrwidth;
565 int llwidth;
566 int ifwidth;
567 char flgbuf[8];
568 char *ifname;
569
570 /* Print header */
571 if (!tflag && !cflag)
572 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
573 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
574 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
575
576 again:;
577 mib[0] = CTL_NET;
578 mib[1] = PF_ROUTE;
579 mib[2] = 0;
580 mib[3] = AF_INET6;
581 mib[4] = NET_RT_FLAGS;
582 mib[5] = RTF_LLINFO;
583 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
584 err(1, "sysctl(PF_ROUTE estimate)");
585 if (needed > 0) {
586 if ((buf = malloc(needed)) == NULL)
587 errx(1, "malloc");
588 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
589 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
590 lim = buf + needed;
591 } else
592 buf = lim = NULL;
593
594 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
595 int isrouter = 0, prbs = 0;
596
597 rtm = (struct rt_msghdr *)next;
598 sin = (struct sockaddr_in6 *)(rtm + 1);
599 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
600 if (addr) {
601 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
602 continue;
603 found_entry = 1;
604 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
605 continue;
606 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
607 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
608 /* XXX: should scope id be filled in the kernel? */
609 if (sin->sin6_scope_id == 0)
610 sin->sin6_scope_id = sdl->sdl_index;
611 #ifdef __KAME__
612 /* KAME specific hack; removed the embedded id */
613 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
614 #endif
615 }
616 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
617 sizeof(host_buf), NULL, 0,
618 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
619 if (cflag == 1) {
620 delete(host_buf);
621 continue;
622 }
623 gettimeofday(&time, 0);
624 if (tflag)
625 ts_print(&time);
626
627 addrwidth = strlen(host_buf);
628 if (addrwidth < W_ADDR)
629 addrwidth = W_ADDR;
630 llwidth = strlen(ether_str(sdl));
631 if (W_ADDR + W_LL - addrwidth > llwidth)
632 llwidth = W_ADDR + W_LL - addrwidth;
633 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
634 if (!ifname)
635 ifname = "?";
636 ifwidth = strlen(ifname);
637 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
638 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
639
640 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
641 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
642
643 /* Print neighbor discovery specific informations */
644 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
645 if (nbi) {
646 if (nbi->expire > time.tv_sec) {
647 printf(" %-9.9s",
648 sec2str(nbi->expire - time.tv_sec));
649 } else if (nbi->expire == 0)
650 printf(" %-9.9s", "permanent");
651 else
652 printf(" %-9.9s", "expired");
653
654 switch(nbi->state) {
655 case ND6_LLINFO_NOSTATE:
656 printf(" N");
657 break;
658 #ifdef ND6_LLINFO_WAITDELETE
659 case ND6_LLINFO_WAITDELETE:
660 printf(" W");
661 break;
662 #endif
663 case ND6_LLINFO_INCOMPLETE:
664 printf(" I");
665 break;
666 case ND6_LLINFO_REACHABLE:
667 printf(" R");
668 break;
669 case ND6_LLINFO_STALE:
670 printf(" S");
671 break;
672 case ND6_LLINFO_DELAY:
673 printf(" D");
674 break;
675 case ND6_LLINFO_PROBE:
676 printf(" P");
677 break;
678 default:
679 printf(" ?");
680 break;
681 }
682
683 isrouter = nbi->isrouter;
684 prbs = nbi->asked;
685 } else {
686 warnx("failed to get neighbor information");
687 printf(" ");
688 }
689 putchar(' ');
690
691 /*
692 * other flags. R: router, P: proxy, W: ??
693 */
694 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
695 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
696 isrouter ? "R" : "",
697 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
698 } else {
699 sin = (struct sockaddr_in6 *)
700 (sdl->sdl_len + (char *)sdl);
701 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
702 isrouter ? "R" : "",
703 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
704 ? "P" : "",
705 (sin->sin6_len != sizeof(struct sockaddr_in6))
706 ? "W" : "",
707 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
708 }
709 printf(" %-4.4s", flgbuf);
710
711 if (prbs)
712 printf(" %4d", prbs);
713
714 printf("\n");
715 }
716 if (buf != NULL)
717 free(buf);
718
719 if (repeat) {
720 printf("\n");
721 sleep(repeat);
722 goto again;
723 }
724 }
725
726 static struct in6_nbrinfo *
727 getnbrinfo(addr, ifindex, warning)
728 struct in6_addr *addr;
729 int ifindex;
730 int warning;
731 {
732 static struct in6_nbrinfo nbi;
733 int s;
734
735 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
736 err(1, "socket");
737
738 bzero(&nbi, sizeof(nbi));
739 if_indextoname(ifindex, nbi.ifname);
740 nbi.addr = *addr;
741 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
742 if (warning)
743 warn("ioctl(SIOCGNBRINFO_IN6)");
744 close(s);
745 return(NULL);
746 }
747
748 close(s);
749 return(&nbi);
750 }
751
752 static char *
753 ether_str(sdl)
754 struct sockaddr_dl *sdl;
755 {
756 static char ebuf[32];
757 u_char *cp;
758
759 if (sdl->sdl_alen) {
760 cp = (u_char *)LLADDR(sdl);
761 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
762 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
763 } else {
764 sprintf(ebuf, "(incomplete)");
765 }
766
767 return(ebuf);
768 }
769
770 int
771 ndp_ether_aton(a, n)
772 char *a;
773 u_char *n;
774 {
775 int i, o[6];
776
777 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
778 &o[3], &o[4], &o[5]);
779 if (i != 6) {
780 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
781 return (1);
782 }
783 for (i=0; i<6; i++)
784 n[i] = o[i];
785 return (0);
786 }
787
788 void
789 usage()
790 {
791 printf("usage: ndp hostname\n");
792 printf(" ndp -a[nt]\n");
793 printf(" ndp [-nt] -A wait\n");
794 printf(" ndp -c[nt]\n");
795 printf(" ndp -d[nt] hostname\n");
796 printf(" ndp -f[nt] filename\n");
797 printf(" ndp -i interface [flags...]\n");
798 #ifdef SIOCSDEFIFACE_IN6
799 printf(" ndp -I [interface|delete]\n");
800 #endif
801 printf(" ndp -p\n");
802 printf(" ndp -r\n");
803 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
804 printf(" ndp -H\n");
805 printf(" ndp -P\n");
806 printf(" ndp -R\n");
807 exit(1);
808 }
809
810 int
811 rtmsg(cmd)
812 int cmd;
813 {
814 static int seq;
815 int rlen;
816 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
817 register char *cp = m_rtmsg.m_space;
818 register int l;
819
820 errno = 0;
821 if (cmd == RTM_DELETE)
822 goto doit;
823 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
824 rtm->rtm_flags = flags;
825 rtm->rtm_version = RTM_VERSION;
826
827 switch (cmd) {
828 default:
829 fprintf(stderr, "ndp: internal wrong cmd\n");
830 exit(1);
831 case RTM_ADD:
832 rtm->rtm_addrs |= RTA_GATEWAY;
833 rtm->rtm_rmx.rmx_expire = expire_time;
834 rtm->rtm_inits = RTV_EXPIRE;
835 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
836 if (rtm->rtm_flags & RTF_ANNOUNCE) {
837 rtm->rtm_flags &= ~RTF_HOST;
838 rtm->rtm_flags |= RTA_NETMASK;
839 }
840 /* FALLTHROUGH */
841 case RTM_GET:
842 rtm->rtm_addrs |= RTA_DST;
843 }
844 #define NEXTADDR(w, s) \
845 if (rtm->rtm_addrs & (w)) { \
846 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
847
848 NEXTADDR(RTA_DST, sin_m);
849 NEXTADDR(RTA_GATEWAY, sdl_m);
850 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
851 NEXTADDR(RTA_NETMASK, so_mask);
852
853 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
854 doit:
855 l = rtm->rtm_msglen;
856 rtm->rtm_seq = ++seq;
857 rtm->rtm_type = cmd;
858 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
859 if (errno != ESRCH || cmd != RTM_DELETE) {
860 perror("writing to routing socket");
861 return (-1);
862 }
863 }
864 do {
865 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
866 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
867 if (l < 0)
868 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
869 strerror(errno));
870 return (0);
871 }
872
873 void
874 ifinfo(argc, argv)
875 int argc;
876 char **argv;
877 {
878 struct in6_ndireq nd;
879 int i, s;
880 char *ifname = argv[0];
881 u_int32_t newflags;
882 #ifdef IPV6CTL_USETEMPADDR
883 u_int8_t nullbuf[8];
884 #endif
885
886 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
887 perror("ndp: socket");
888 exit(1);
889 }
890 bzero(&nd, sizeof(nd));
891 strcpy(nd.ifname, ifname);
892 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
893 perror("ioctl (SIOCGIFINFO_IN6)");
894 exit(1);
895 }
896 #define ND nd.ndi
897 newflags = ND.flags;
898 for (i = 1; i < argc; i++) {
899 int clear = 0;
900 char *cp = argv[i];
901
902 if (*cp == '-') {
903 clear = 1;
904 cp++;
905 }
906
907 #define SETFLAG(s, f) \
908 do {\
909 if (strcmp(cp, (s)) == 0) {\
910 if (clear)\
911 newflags &= ~(f);\
912 else\
913 newflags |= (f);\
914 }\
915 } while (0)
916 SETFLAG("nud", ND6_IFF_PERFORMNUD);
917
918 ND.flags = newflags;
919 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
920 perror("ioctl(SIOCSIFINFO_FLAGS)");
921 exit(1);
922 }
923 #undef SETFLAG
924 }
925
926 printf("linkmtu=%d", ND.linkmtu);
927 printf(", curhlim=%d", ND.chlim);
928 printf(", basereachable=%ds%dms",
929 ND.basereachable / 1000, ND.basereachable % 1000);
930 printf(", reachable=%ds", ND.reachable);
931 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
932 #ifdef IPV6CTL_USETEMPADDR
933 memset(nullbuf, 0, sizeof(nullbuf));
934 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
935 int j;
936 u_int8_t *rbuf;
937
938 for (i = 0; i < 3; i++) {
939 switch(i) {
940 case 0:
941 printf("\nRandom seed(0): ");
942 rbuf = ND.randomseed0;
943 break;
944 case 1:
945 printf("\nRandom seed(1): ");
946 rbuf = ND.randomseed1;
947 break;
948 case 2:
949 printf("\nRandom ID: ");
950 rbuf = ND.randomid;
951 break;
952 }
953 for (j = 0; j < 8; j++)
954 printf("%02x", rbuf[j]);
955 }
956 }
957 #endif
958 if (ND.flags) {
959 printf("\nFlags: ");
960 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
961 printf("PERFORMNUD ");
962 }
963 putc('\n', stdout);
964 #undef ND
965
966 close(s);
967 }
968
969 void
970 rtrlist()
971 {
972 struct in6_drlist dr;
973 int s, i;
974 struct timeval time;
975
976 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
977 perror("ndp: socket");
978 exit(1);
979 }
980 bzero(&dr, sizeof(dr));
981 strcpy(dr.ifname, "lo0"); /* dummy */
982 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
983 perror("ioctl (SIOCGDRLST_IN6)");
984 exit(1);
985 }
986 #define DR dr.defrouter[i]
987 for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
988 struct sockaddr_in6 sin6;
989
990 bzero(&sin6, sizeof(sin6));
991 sin6.sin6_family = AF_INET6;
992 sin6.sin6_len = sizeof(sin6);
993 sin6.sin6_addr = DR.rtaddr;
994 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
995 sizeof(host_buf), NULL, 0,
996 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
997
998 printf("%s if=%s", host_buf,
999 if_indextoname(DR.if_index, ifix_buf));
1000 printf(", flags=%s%s",
1001 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1002 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1003 gettimeofday(&time, 0);
1004 if (DR.expire == 0)
1005 printf(", expire=Never\n");
1006 else
1007 printf(", expire=%s\n",
1008 sec2str(DR.expire - time.tv_sec));
1009 }
1010 #undef DR
1011 close(s);
1012 }
1013
1014 void
1015 plist()
1016 {
1017 struct in6_prlist pr;
1018 int s, i;
1019 struct timeval time;
1020
1021 gettimeofday(&time, 0);
1022
1023 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1024 perror("ndp: socket");
1025 exit(1);
1026 }
1027 bzero(&pr, sizeof(pr));
1028 strcpy(pr.ifname, "lo0"); /* dummy */
1029 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1030 perror("ioctl (SIOCGPRLST_IN6)");
1031 exit(1);
1032 }
1033 #define PR pr.prefix[i]
1034 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1035 struct sockaddr_in6 p6;
1036 char namebuf[NI_MAXHOST];
1037 int niflags;
1038
1039 #ifdef NDPRF_ONLINK
1040 p6 = PR.prefix;
1041 #else
1042 memset(&p6, 0, sizeof(p6));
1043 p6.sin6_family = AF_INET6;
1044 p6.sin6_len = sizeof(p6);
1045 p6.sin6_addr = PR.prefix;
1046 #endif
1047
1048 /*
1049 * copy link index to sin6_scope_id field.
1050 * XXX: KAME specific.
1051 */
1052 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1053 u_int16_t linkid;
1054
1055 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1056 sizeof(linkid));
1057 linkid = ntohs(linkid);
1058 p6.sin6_scope_id = linkid;
1059 p6.sin6_addr.s6_addr[2] = 0;
1060 p6.sin6_addr.s6_addr[3] = 0;
1061 }
1062
1063 niflags = NI_NUMERICHOST;
1064 #ifdef __KAME__
1065 niflags |= NI_WITHSCOPEID;
1066 #endif
1067 if (getnameinfo((struct sockaddr *)&p6,
1068 sizeof(p6), namebuf, sizeof(namebuf),
1069 NULL, 0, niflags)) {
1070 warnx("getnameinfo failed");
1071 continue;
1072 }
1073 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1074 if_indextoname(PR.if_index, ifix_buf));
1075
1076 gettimeofday(&time, 0);
1077 /*
1078 * meaning of fields, especially flags, is very different
1079 * by origin. notify the difference to the users.
1080 */
1081 printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1082 #ifdef NDPRF_ONLINK
1083 printf("flags=%s%s%s%s",
1084 PR.raflags.onlink ? "L" : "",
1085 PR.raflags.autonomous ? "A" : "",
1086 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1087 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "");
1088 #else
1089 printf("flags=%s%s",
1090 PR.raflags.onlink ? "L" : "",
1091 PR.raflags.autonomous ? "A" : "");
1092 #endif
1093 if (PR.vltime == ND6_INFINITE_LIFETIME)
1094 printf(" vltime=infinity");
1095 else
1096 printf(" vltime=%ld", (long)PR.vltime);
1097 if (PR.pltime == ND6_INFINITE_LIFETIME)
1098 printf(", pltime=infinity");
1099 else
1100 printf(", pltime=%ld", (long)PR.pltime);
1101 if (PR.expire == 0)
1102 printf(", expire=Never");
1103 else if (PR.expire >= time.tv_sec)
1104 printf(", expire=%s",
1105 sec2str(PR.expire - time.tv_sec));
1106 else
1107 printf(", expired");
1108 #ifdef NDPRF_ONLINK
1109 printf(", ref=%d", PR.refcnt);
1110 #endif
1111 switch (PR.origin) {
1112 case PR_ORIG_RA:
1113 printf(", origin=RA");
1114 break;
1115 case PR_ORIG_RR:
1116 printf(", origin=RR");
1117 break;
1118 case PR_ORIG_STATIC:
1119 printf(", origin=static");
1120 break;
1121 case PR_ORIG_KERNEL:
1122 printf(", origin=kernel");
1123 break;
1124 default:
1125 printf(", origin=?");
1126 break;
1127 }
1128 printf("\n");
1129 /*
1130 * "advertising router" list is meaningful only if the prefix
1131 * information is from RA.
1132 */
1133 if (PR.origin != PR_ORIG_RA)
1134 ;
1135 else if (PR.advrtrs) {
1136 int j;
1137 printf(" advertised by\n");
1138 for (j = 0; j < PR.advrtrs; j++) {
1139 struct sockaddr_in6 sin6;
1140 struct in6_nbrinfo *nbi;
1141
1142 bzero(&sin6, sizeof(sin6));
1143 sin6.sin6_family = AF_INET6;
1144 sin6.sin6_len = sizeof(sin6);
1145 sin6.sin6_addr = PR.advrtr[j];
1146 sin6.sin6_scope_id = PR.if_index; /* XXX */
1147 getnameinfo((struct sockaddr *)&sin6,
1148 sin6.sin6_len, host_buf,
1149 sizeof(host_buf), NULL, 0,
1150 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1151 printf(" %s", host_buf);
1152
1153 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1154 0);
1155 if (nbi) {
1156 switch(nbi->state) {
1157 case ND6_LLINFO_REACHABLE:
1158 case ND6_LLINFO_STALE:
1159 case ND6_LLINFO_DELAY:
1160 case ND6_LLINFO_PROBE:
1161 printf(" (reachable)\n");
1162 break;
1163 default:
1164 printf(" (unreachable)\n");
1165 }
1166 } else
1167 printf(" (no neighbor state)\n");
1168 }
1169 if (PR.advrtrs > DRLSTSIZ)
1170 printf(" and %d routers\n",
1171 PR.advrtrs - DRLSTSIZ);
1172 } else
1173 printf(" No advertising router\n");
1174 }
1175 #undef PR
1176 close(s);
1177 }
1178
1179 void
1180 pfx_flush()
1181 {
1182 char dummyif[IFNAMSIZ+8];
1183 int s;
1184
1185 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1186 err(1, "socket");
1187 strcpy(dummyif, "lo0"); /* dummy */
1188 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1189 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1190 }
1191
1192 void
1193 rtr_flush()
1194 {
1195 char dummyif[IFNAMSIZ+8];
1196 int s;
1197
1198 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1199 err(1, "socket");
1200 strcpy(dummyif, "lo0"); /* dummy */
1201 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1202 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1203
1204 close(s);
1205 }
1206
1207 void
1208 harmonize_rtr()
1209 {
1210 char dummyif[IFNAMSIZ+8];
1211 int s;
1212
1213 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1214 err(1, "socket");
1215 strcpy(dummyif, "lo0"); /* dummy */
1216 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1217 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1218
1219 close(s);
1220 }
1221
1222 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1223 static void
1224 setdefif(ifname)
1225 char *ifname;
1226 {
1227 struct in6_ndifreq ndifreq;
1228 unsigned int ifindex;
1229
1230 if (strcasecmp(ifname, "delete") == 0)
1231 ifindex = 0;
1232 else {
1233 if ((ifindex = if_nametoindex(ifname)) == 0)
1234 err(1, "failed to resolve i/f index for %s", ifname);
1235 }
1236
1237 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1238 err(1, "socket");
1239
1240 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1241 ndifreq.ifindex = ifindex;
1242
1243 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1244 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1245
1246 close(s);
1247 }
1248
1249 static void
1250 getdefif()
1251 {
1252 struct in6_ndifreq ndifreq;
1253 char ifname[IFNAMSIZ+8];
1254
1255 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1256 err(1, "socket");
1257
1258 memset(&ndifreq, 0, sizeof(ndifreq));
1259 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1260
1261 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1262 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1263
1264 if (ndifreq.ifindex == 0)
1265 printf("No default interface.\n");
1266 else {
1267 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1268 err(1, "failed to resolve ifname for index %lu",
1269 ndifreq.ifindex);
1270 printf("ND default interface = %s\n", ifname);
1271 }
1272
1273 close(s);
1274 }
1275 #endif
1276
1277 static char *
1278 sec2str(total)
1279 time_t total;
1280 {
1281 static char result[256];
1282 int days, hours, mins, secs;
1283 int first = 1;
1284 char *p = result;
1285
1286 days = total / 3600 / 24;
1287 hours = (total / 3600) % 24;
1288 mins = (total / 60) % 60;
1289 secs = total % 60;
1290
1291 if (days) {
1292 first = 0;
1293 p += sprintf(p, "%dd", days);
1294 }
1295 if (!first || hours) {
1296 first = 0;
1297 p += sprintf(p, "%dh", hours);
1298 }
1299 if (!first || mins) {
1300 first = 0;
1301 p += sprintf(p, "%dm", mins);
1302 }
1303 sprintf(p, "%ds", secs);
1304
1305 return(result);
1306 }
1307
1308 /*
1309 * Print the timestamp
1310 * from tcpdump/util.c
1311 */
1312 static void
1313 ts_print(tvp)
1314 const struct timeval *tvp;
1315 {
1316 int s;
1317
1318 /* Default */
1319 s = (tvp->tv_sec + thiszone) % 86400;
1320 (void)printf("%02d:%02d:%02d.%06u ",
1321 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1322 }
1323