ndp.c revision 288297
162590Sitojun/* $FreeBSD: head/usr.sbin/ndp/ndp.c 288297 2015-09-27 04:54:29Z melifaro $ */ 2122615Sume/* $KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $ */ 362590Sitojun 455505Sshin/* 555505Sshin * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 655505Sshin * All rights reserved. 755505Sshin * 855505Sshin * Redistribution and use in source and binary forms, with or without 955505Sshin * modification, are permitted provided that the following conditions 1055505Sshin * are met: 1155505Sshin * 1. Redistributions of source code must retain the above copyright 1255505Sshin * notice, this list of conditions and the following disclaimer. 1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright 1455505Sshin * notice, this list of conditions and the following disclaimer in the 1555505Sshin * documentation and/or other materials provided with the distribution. 1655505Sshin * 3. Neither the name of the project nor the names of its contributors 1755505Sshin * may be used to endorse or promote products derived from this software 1855505Sshin * without specific prior written permission. 1955505Sshin * 2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2355505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3055505Sshin * SUCH DAMAGE. 3155505Sshin */ 3255505Sshin/* 3355505Sshin * Copyright (c) 1984, 1993 3455505Sshin * The Regents of the University of California. All rights reserved. 3555505Sshin * 3655505Sshin * This code is derived from software contributed to Berkeley by 3755505Sshin * Sun Microsystems, Inc. 3855505Sshin * 3955505Sshin * Redistribution and use in source and binary forms, with or without 4055505Sshin * modification, are permitted provided that the following conditions 4155505Sshin * are met: 4255505Sshin * 1. Redistributions of source code must retain the above copyright 4355505Sshin * notice, this list of conditions and the following disclaimer. 4455505Sshin * 2. Redistributions in binary form must reproduce the above copyright 4555505Sshin * notice, this list of conditions and the following disclaimer in the 4655505Sshin * documentation and/or other materials provided with the distribution. 4755505Sshin * 4. Neither the name of the University nor the names of its contributors 4855505Sshin * may be used to endorse or promote products derived from this software 4955505Sshin * without specific prior written permission. 5055505Sshin * 5155505Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5255505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5355505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5455505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5555505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5655505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5755505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5855505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5955505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6055505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6155505Sshin * SUCH DAMAGE. 6255505Sshin */ 6355505Sshin 6455505Sshin/* 6555505Sshin * Based on: 6655505Sshin * "@(#) Copyright (c) 1984, 1993\n\ 6755505Sshin * The Regents of the University of California. All rights reserved.\n"; 6855505Sshin * 6955505Sshin * "@(#)arp.c 8.2 (Berkeley) 1/2/94"; 7055505Sshin */ 7155505Sshin 7255505Sshin/* 7355505Sshin * ndp - display, set, delete and flush neighbor cache 7455505Sshin */ 7555505Sshin 7655505Sshin 7755505Sshin#include <sys/param.h> 7855505Sshin#include <sys/file.h> 7955505Sshin#include <sys/ioctl.h> 8055505Sshin#include <sys/socket.h> 8155505Sshin#include <sys/sysctl.h> 82253999Shrs#include <sys/time.h> 8378064Sume#include <sys/queue.h> 8455505Sshin 8555505Sshin#include <net/if.h> 8655505Sshin#include <net/if_dl.h> 8755505Sshin#include <net/if_types.h> 8855505Sshin#include <net/route.h> 8955505Sshin 9055505Sshin#include <netinet/in.h> 9155505Sshin#include <netinet/if_ether.h> 9255505Sshin 9355505Sshin#include <netinet/icmp6.h> 9455505Sshin#include <netinet6/in6_var.h> 9555505Sshin#include <netinet6/nd6.h> 9655505Sshin 9755505Sshin#include <arpa/inet.h> 9855505Sshin 99265778Smelifaro#include <ctype.h> 10055505Sshin#include <netdb.h> 10155505Sshin#include <errno.h> 10255505Sshin#include <nlist.h> 10355505Sshin#include <stdio.h> 10455505Sshin#include <string.h> 10555505Sshin#include <paths.h> 10655505Sshin#include <err.h> 10755505Sshin#include <stdlib.h> 10855505Sshin#include <fcntl.h> 10955505Sshin#include <unistd.h> 11055505Sshin#include "gmt2local.h" 11155505Sshin 112186119Sqingli#define NEXTADDR(w, s) \ 113186119Sqingli if (rtm->rtm_addrs & (w)) { \ 114196866Sbz bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);} 115186119Sqingli 116186119Sqingli 117100650Sjmallettstatic pid_t pid; 11862590Sitojunstatic int nflag; 11962590Sitojunstatic int tflag; 12062590Sitojunstatic int32_t thiszone; /* time difference with gmt */ 12162590Sitojunstatic int s = -1; 12262590Sitojunstatic int repeat = 0; 12355505Sshin 124287097Shrsstatic char host_buf[NI_MAXHOST]; /* getnameinfo() */ 125287097Shrsstatic char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 12655505Sshin 127265778Smelifarostatic int file(char *); 128287097Shrsstatic void getsocket(void); 129287097Shrsstatic int set(int, char **); 130287097Shrsstatic void get(char *); 131287097Shrsstatic int delete(char *); 132287097Shrsstatic void dump(struct sockaddr_in6 *, int); 133173412Skevlostatic struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int); 134173412Skevlostatic char *ether_str(struct sockaddr_dl *); 135287097Shrsstatic int ndp_ether_aton(char *, u_char *); 136287097Shrsstatic void usage(void); 137287097Shrsstatic int rtmsg(int); 138287097Shrsstatic void ifinfo(char *, int, char **); 139287097Shrsstatic void rtrlist(void); 140287097Shrsstatic void plist(void); 141287097Shrsstatic void pfx_flush(void); 142287097Shrsstatic void rtr_flush(void); 143287097Shrsstatic void harmonize_rtr(void); 14462590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 145173412Skevlostatic void getdefif(void); 146173412Skevlostatic void setdefif(char *); 14762590Sitojun#endif 148173412Skevlostatic char *sec2str(time_t); 149253999Shrsstatic void ts_print(const struct timeval *); 15055505Sshin 15178064Sumestatic char *rtpref_str[] = { 15278064Sume "medium", /* 00 */ 15378064Sume "high", /* 01 */ 15478064Sume "rsv", /* 10 */ 15578064Sume "low" /* 11 */ 15678064Sume}; 15778064Sume 15855505Sshinint 159259169Saemain(int argc, char **argv) 16055505Sshin{ 161287097Shrs int ch, mode = 0; 162287097Shrs char *arg = NULL; 16355505Sshin 16455505Sshin pid = getpid(); 16555505Sshin thiszone = gmt2local(0); 166122615Sume while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1) 167121156Sume switch (ch) { 16855505Sshin case 'a': 16955505Sshin case 'c': 170122615Sume case 'p': 171122615Sume case 'r': 172122615Sume case 'H': 173122615Sume case 'P': 174122615Sume case 'R': 175122615Sume case 's': 176122615Sume case 'I': 177122615Sume if (mode) { 178122615Sume usage(); 179122615Sume /*NOTREACHED*/ 180122615Sume } 181122615Sume mode = ch; 182122615Sume arg = NULL; 18355505Sshin break; 184122615Sume case 'f': 185265778Smelifaro exit(file(optarg) ? 1 : 0); 186268827Speter case 'd': 187265778Smelifaro case 'i': 188122615Sume if (mode) { 18955505Sshin usage(); 190122615Sume /*NOTREACHED*/ 191122615Sume } 192122615Sume mode = ch; 193122615Sume arg = optarg; 194122615Sume break; 19555505Sshin case 'n': 19655505Sshin nflag = 1; 19755505Sshin break; 19855505Sshin case 't': 19955505Sshin tflag = 1; 20055505Sshin break; 20155505Sshin case 'A': 202122615Sume if (mode) { 203122615Sume usage(); 204122615Sume /*NOTREACHED*/ 205122615Sume } 206122615Sume mode = 'a'; 20755505Sshin repeat = atoi(optarg); 208122615Sume if (repeat < 0) { 20955505Sshin usage(); 210122615Sume /*NOTREACHED*/ 211122615Sume } 21255505Sshin break; 21355505Sshin default: 21455505Sshin usage(); 21555505Sshin } 21655505Sshin 21755505Sshin argc -= optind; 21855505Sshin argv += optind; 21955505Sshin 220122615Sume switch (mode) { 221122615Sume case 'a': 222122615Sume case 'c': 223122615Sume if (argc != 0) { 22455505Sshin usage(); 225122615Sume /*NOTREACHED*/ 226122615Sume } 227122615Sume dump(0, mode == 'c'); 228122615Sume break; 229122615Sume case 'd': 230122615Sume if (argc != 0) { 231122615Sume usage(); 232122615Sume /*NOTREACHED*/ 233122615Sume } 234122615Sume delete(arg); 235122615Sume break; 236122615Sume case 'I': 237122615Sume#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 238122615Sume if (argc > 1) { 239122615Sume usage(); 240122615Sume /*NOTREACHED*/ 241122615Sume } else if (argc == 1) { 242122615Sume if (strcmp(*argv, "delete") == 0 || 243122615Sume if_nametoindex(*argv)) 244122615Sume setdefif(*argv); 245122615Sume else 246122615Sume errx(1, "invalid interface %s", *argv); 247122615Sume } 248122615Sume getdefif(); /* always call it to print the result */ 249122615Sume break; 250122615Sume#else 251122615Sume errx(1, "not supported yet"); 252122615Sume /*NOTREACHED*/ 253122615Sume#endif 254122615Sume case 'p': 255122615Sume if (argc != 0) { 256122615Sume usage(); 257122615Sume /*NOTREACHED*/ 258122615Sume } 25955505Sshin plist(); 260122615Sume break; 261122615Sume case 'i': 262122615Sume ifinfo(arg, argc, argv); 263122615Sume break; 264122615Sume case 'r': 265122615Sume if (argc != 0) { 266122615Sume usage(); 267122615Sume /*NOTREACHED*/ 268122615Sume } 26955505Sshin rtrlist(); 270122615Sume break; 271122615Sume case 's': 27255505Sshin if (argc < 2 || argc > 4) 27355505Sshin usage(); 27455505Sshin exit(set(argc, argv) ? 1 : 0); 275122615Sume case 'H': 276122615Sume if (argc != 0) { 277122615Sume usage(); 278122615Sume /*NOTREACHED*/ 279122615Sume } 28055505Sshin harmonize_rtr(); 281122615Sume break; 282122615Sume case 'P': 283122615Sume if (argc != 0) { 284122615Sume usage(); 285122615Sume /*NOTREACHED*/ 286122615Sume } 28755505Sshin pfx_flush(); 288122615Sume break; 289122615Sume case 'R': 290122615Sume if (argc != 0) { 291122615Sume usage(); 292122615Sume /*NOTREACHED*/ 293122615Sume } 29455505Sshin rtr_flush(); 295122615Sume break; 296122615Sume case 0: 297122615Sume if (argc != 1) { 298122615Sume usage(); 299122615Sume /*NOTREACHED*/ 300122615Sume } 301122615Sume get(argv[0]); 302122615Sume break; 30355505Sshin } 30455505Sshin exit(0); 30555505Sshin} 30655505Sshin 30755505Sshin/* 30855505Sshin * Process a file to set standard ndp entries 30955505Sshin */ 310265778Smelifarostatic int 311259169Saefile(char *name) 31255505Sshin{ 31355505Sshin FILE *fp; 31455505Sshin int i, retval; 315265778Smelifaro char line[100], arg[5][50], *args[5], *p; 31655505Sshin 317265778Smelifaro if ((fp = fopen(name, "r")) == NULL) 318265778Smelifaro err(1, "cannot open %s", name); 31955505Sshin args[0] = &arg[0][0]; 32055505Sshin args[1] = &arg[1][0]; 32155505Sshin args[2] = &arg[2][0]; 32255505Sshin args[3] = &arg[3][0]; 32355505Sshin args[4] = &arg[4][0]; 32455505Sshin retval = 0; 325167260Skevlo while (fgets(line, sizeof(line), fp) != NULL) { 326265778Smelifaro if ((p = strchr(line, '#')) != NULL) 327265778Smelifaro *p = '\0'; 328265778Smelifaro for (p = line; isblank(*p); p++); 329265778Smelifaro if (*p == '\n' || *p == '\0') 330265778Smelifaro continue; 331122615Sume i = sscanf(line, "%49s %49s %49s %49s %49s", 332122615Sume arg[0], arg[1], arg[2], arg[3], arg[4]); 33355505Sshin if (i < 2) { 334265778Smelifaro warnx("bad line: %s", line); 33555505Sshin retval = 1; 33655505Sshin continue; 33755505Sshin } 33855505Sshin if (set(i, args)) 33955505Sshin retval = 1; 34055505Sshin } 34155505Sshin fclose(fp); 34255505Sshin return (retval); 34355505Sshin} 34455505Sshin 345287097Shrsstatic void 34655505Sshingetsocket() 34755505Sshin{ 34855505Sshin if (s < 0) { 34955505Sshin s = socket(PF_ROUTE, SOCK_RAW, 0); 35055505Sshin if (s < 0) { 351121156Sume err(1, "socket"); 352121156Sume /* NOTREACHED */ 35355505Sshin } 35455505Sshin } 35555505Sshin} 35655505Sshin 357287097Shrsstatic struct sockaddr_in6 so_mask = { 358287097Shrs .sin6_len = sizeof(so_mask), 359287097Shrs .sin6_family = AF_INET6 360287097Shrs}; 361287097Shrsstatic struct sockaddr_in6 blank_sin = { 362287097Shrs .sin6_len = sizeof(blank_sin), 363287097Shrs .sin6_family = AF_INET6 364287097Shrs}; 365287097Shrsstatic struct sockaddr_in6 sin_m; 366287097Shrsstatic struct sockaddr_dl blank_sdl = { 367287097Shrs .sdl_len = sizeof(blank_sdl), 368287097Shrs .sdl_family = AF_LINK 369287097Shrs}; 370287097Shrsstatic struct sockaddr_dl sdl_m; 371287097Shrsstatic time_t expire_time; 372287097Shrsstatic int flags, found_entry; 373287097Shrsstatic struct { 37455505Sshin struct rt_msghdr m_rtm; 37555505Sshin char m_space[512]; 376287097Shrs} m_rtmsg; 37755505Sshin 37855505Sshin/* 37955505Sshin * Set an individual neighbor cache entry 38055505Sshin */ 381287097Shrsstatic int 382259169Saeset(int argc, char **argv) 38355505Sshin{ 38455505Sshin register struct sockaddr_in6 *sin = &sin_m; 38555505Sshin register struct sockaddr_dl *sdl; 38655505Sshin register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 38755505Sshin struct addrinfo hints, *res; 38855505Sshin int gai_error; 38955505Sshin u_char *ea; 39055505Sshin char *host = argv[0], *eaddr = argv[1]; 39155505Sshin 39255505Sshin getsocket(); 39355505Sshin argc -= 2; 39455505Sshin argv += 2; 39555505Sshin sdl_m = blank_sdl; 39655505Sshin sin_m = blank_sin; 39755505Sshin 39855505Sshin bzero(&hints, sizeof(hints)); 39955505Sshin hints.ai_family = AF_INET6; 40055505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 40155505Sshin if (gai_error) { 40255505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 40355505Sshin gai_strerror(gai_error)); 40455505Sshin return 1; 40555505Sshin } 40655505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 407243903Shrs sin->sin6_scope_id = 408243903Shrs ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 40955505Sshin ea = (u_char *)LLADDR(&sdl_m); 41055505Sshin if (ndp_ether_aton(eaddr, ea) == 0) 41155505Sshin sdl_m.sdl_alen = 6; 41255505Sshin flags = expire_time = 0; 41355505Sshin while (argc-- > 0) { 41455505Sshin if (strncmp(argv[0], "temp", 4) == 0) { 415253999Shrs struct timeval now; 416121156Sume 417253999Shrs gettimeofday(&now, 0); 418253970Shrs expire_time = now.tv_sec + 20 * 60; 41962590Sitojun } else if (strncmp(argv[0], "proxy", 5) == 0) 42062590Sitojun flags |= RTF_ANNOUNCE; 42155505Sshin argv++; 42255505Sshin } 42355505Sshin if (rtmsg(RTM_GET) < 0) { 424121156Sume errx(1, "RTM_GET(%s) failed", host); 425121156Sume /* NOTREACHED */ 42655505Sshin } 42755505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 428259171Sae sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin); 42955505Sshin if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 43055505Sshin if (sdl->sdl_family == AF_LINK && 431122615Sume !(rtm->rtm_flags & RTF_GATEWAY)) { 432122615Sume switch (sdl->sdl_type) { 433122615Sume case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 434122615Sume case IFT_ISO88024: case IFT_ISO88025: 435210936Sjhb case IFT_L2VLAN: case IFT_BRIDGE: 436122615Sume goto overwrite; 437122615Sume } 43855505Sshin } 43962590Sitojun fprintf(stderr, "set: cannot configure a new entry\n"); 44062590Sitojun return 1; 44155505Sshin } 44262590Sitojun 44355505Sshinoverwrite: 44455505Sshin if (sdl->sdl_family != AF_LINK) { 44555505Sshin printf("cannot intuit interface index and type for %s\n", host); 44655505Sshin return (1); 44755505Sshin } 44855505Sshin sdl_m.sdl_type = sdl->sdl_type; 44955505Sshin sdl_m.sdl_index = sdl->sdl_index; 45055505Sshin return (rtmsg(RTM_ADD)); 45155505Sshin} 45255505Sshin 45355505Sshin/* 45455505Sshin * Display an individual neighbor cache entry 45555505Sshin */ 456287097Shrsstatic void 457259169Saeget(char *host) 45855505Sshin{ 45955505Sshin struct sockaddr_in6 *sin = &sin_m; 46055505Sshin struct addrinfo hints, *res; 46155505Sshin int gai_error; 46255505Sshin 46355505Sshin sin_m = blank_sin; 46455505Sshin bzero(&hints, sizeof(hints)); 46555505Sshin hints.ai_family = AF_INET6; 46655505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 46755505Sshin if (gai_error) { 46855505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 469121156Sume gai_strerror(gai_error)); 47055505Sshin return; 47155505Sshin } 47255505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 473259176Sae sin->sin6_scope_id = 474259176Sae ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 475259176Sae dump(sin, 0); 47655505Sshin if (found_entry == 0) { 47755505Sshin getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 478121156Sume sizeof(host_buf), NULL ,0, 479121156Sume (nflag ? NI_NUMERICHOST : 0)); 48055505Sshin printf("%s (%s) -- no entry\n", host, host_buf); 48155505Sshin exit(1); 48255505Sshin } 48355505Sshin} 48455505Sshin 48555505Sshin/* 48655505Sshin * Delete a neighbor cache entry 48755505Sshin */ 488287097Shrsstatic int 489259169Saedelete(char *host) 49055505Sshin{ 49155505Sshin struct sockaddr_in6 *sin = &sin_m; 49255505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 493186119Sqingli register char *cp = m_rtmsg.m_space; 49455505Sshin struct sockaddr_dl *sdl; 49555505Sshin struct addrinfo hints, *res; 49655505Sshin int gai_error; 49755505Sshin 49855505Sshin getsocket(); 49955505Sshin sin_m = blank_sin; 50055505Sshin 50155505Sshin bzero(&hints, sizeof(hints)); 50255505Sshin hints.ai_family = AF_INET6; 50355505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 50455505Sshin if (gai_error) { 50555505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 506121156Sume gai_strerror(gai_error)); 50755505Sshin return 1; 50855505Sshin } 50955505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 510243903Shrs sin->sin6_scope_id = 511243903Shrs ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 51255505Sshin if (rtmsg(RTM_GET) < 0) { 513121156Sume errx(1, "RTM_GET(%s) failed", host); 514121156Sume /* NOTREACHED */ 51555505Sshin } 51655505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 517259171Sae sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin); 51855505Sshin if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 51955505Sshin if (sdl->sdl_family == AF_LINK && 52062590Sitojun !(rtm->rtm_flags & RTF_GATEWAY)) { 52178064Sume goto delete; 52255505Sshin } 52362590Sitojun fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 52462590Sitojun return 1; 52555505Sshin } 52662590Sitojun 52755505Sshindelete: 52855505Sshin if (sdl->sdl_family != AF_LINK) { 52955505Sshin printf("cannot locate %s\n", host); 53055505Sshin return (1); 53155505Sshin } 532186119Sqingli /* 533186119Sqingli * need to reinit the field because it has rt_key 534186119Sqingli * but we want the actual address 535186119Sqingli */ 536186119Sqingli NEXTADDR(RTA_DST, sin_m); 537186500Sqingli rtm->rtm_flags |= RTF_LLDATA; 53855505Sshin if (rtmsg(RTM_DELETE) == 0) { 539243903Shrs getnameinfo((struct sockaddr *)sin, 540243903Shrs sin->sin6_len, host_buf, 541121156Sume sizeof(host_buf), NULL, 0, 542121156Sume (nflag ? NI_NUMERICHOST : 0)); 54355505Sshin printf("%s (%s) deleted\n", host, host_buf); 54455505Sshin } 54555505Sshin 54655505Sshin return 0; 54755505Sshin} 54855505Sshin 549122615Sume#define W_ADDR 36 55078064Sume#define W_LL 17 55178064Sume#define W_IF 6 55278064Sume 55355505Sshin/* 55455505Sshin * Dump the entire neighbor cache 55555505Sshin */ 556287097Shrsstatic void 557259176Saedump(struct sockaddr_in6 *addr, int cflag) 55855505Sshin{ 55955505Sshin int mib[6]; 56055505Sshin size_t needed; 56162590Sitojun char *lim, *buf, *next; 56255505Sshin struct rt_msghdr *rtm; 56355505Sshin struct sockaddr_in6 *sin; 56455505Sshin struct sockaddr_dl *sdl; 56555505Sshin extern int h_errno; 56655505Sshin struct in6_nbrinfo *nbi; 567253999Shrs struct timeval now; 56855505Sshin int addrwidth; 56978064Sume int llwidth; 57078064Sume int ifwidth; 57162590Sitojun char flgbuf[8]; 57278064Sume char *ifname; 57355505Sshin 57455505Sshin /* Print header */ 57566865Ssumikawa if (!tflag && !cflag) 576122615Sume printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 57778064Sume W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 578122615Sume W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 57955505Sshin 58055505Sshinagain:; 58155505Sshin mib[0] = CTL_NET; 58255505Sshin mib[1] = PF_ROUTE; 58355505Sshin mib[2] = 0; 58455505Sshin mib[3] = AF_INET6; 58555505Sshin mib[4] = NET_RT_FLAGS; 586186119Sqingli#ifdef RTF_LLINFO 58755505Sshin mib[5] = RTF_LLINFO; 588186119Sqingli#else 589186119Sqingli mib[5] = 0; 590186119Sqingli#endif 59155505Sshin if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 59255505Sshin err(1, "sysctl(PF_ROUTE estimate)"); 59355505Sshin if (needed > 0) { 59455505Sshin if ((buf = malloc(needed)) == NULL) 595121156Sume err(1, "malloc"); 59655505Sshin if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 59755505Sshin err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 59855505Sshin lim = buf + needed; 59955505Sshin } else 60055505Sshin buf = lim = NULL; 60155505Sshin 60255505Sshin for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 60355505Sshin int isrouter = 0, prbs = 0; 60455505Sshin 60555505Sshin rtm = (struct rt_msghdr *)next; 60655505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 607259171Sae sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len)); 60878064Sume 60978064Sume /* 61078064Sume * Some OSes can produce a route that has the LINK flag but 61178064Sume * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 61278064Sume * and BSD/OS, where xx is not the interface identifier on 61378064Sume * lo0). Such routes entry would annoy getnbrinfo() below, 61478064Sume * so we skip them. 61578064Sume * XXX: such routes should have the GATEWAY flag, not the 616121156Sume * LINK flag. However, there is rotten routing software 61778064Sume * that advertises all routes that have the GATEWAY flag. 61878064Sume * Thus, KAME kernel intentionally does not set the LINK flag. 61978064Sume * What is to be fixed is not ndp, but such routing software 62078064Sume * (and the kernel workaround)... 62178064Sume */ 62278064Sume if (sdl->sdl_family != AF_LINK) 62378064Sume continue; 62478064Sume 625122615Sume if (!(rtm->rtm_flags & RTF_HOST)) 626122615Sume continue; 627122615Sume 62855505Sshin if (addr) { 629259176Sae if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, 630259176Sae &sin->sin6_addr) == 0 || 631259176Sae addr->sin6_scope_id != sin->sin6_scope_id) 63255505Sshin continue; 63355505Sshin found_entry = 1; 63455505Sshin } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 63555505Sshin continue; 63655505Sshin if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 63755505Sshin IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 63855505Sshin /* XXX: should scope id be filled in the kernel? */ 63955505Sshin if (sin->sin6_scope_id == 0) 64055505Sshin sin->sin6_scope_id = sdl->sdl_index; 64155505Sshin } 64255505Sshin getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 643121156Sume sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 644122615Sume if (cflag) { 64581366Ssumikawa#ifdef RTF_WASCLONED 64681366Ssumikawa if (rtm->rtm_flags & RTF_WASCLONED) 64781366Ssumikawa delete(host_buf); 648122615Sume#elif defined(RTF_CLONED) 649122615Sume if (rtm->rtm_flags & RTF_CLONED) 650122615Sume delete(host_buf); 65181366Ssumikawa#else 652288297Smelifaro if (rtm->rtm_flags & RTF_PINNED) 653288297Smelifaro continue; 65466865Ssumikawa delete(host_buf); 65581366Ssumikawa#endif 65666865Ssumikawa continue; 65766865Ssumikawa } 658253999Shrs gettimeofday(&now, 0); 65955505Sshin if (tflag) 660253970Shrs ts_print(&now); 66155505Sshin 66278064Sume addrwidth = strlen(host_buf); 66378064Sume if (addrwidth < W_ADDR) 66478064Sume addrwidth = W_ADDR; 66578064Sume llwidth = strlen(ether_str(sdl)); 66678064Sume if (W_ADDR + W_LL - addrwidth > llwidth) 66778064Sume llwidth = W_ADDR + W_LL - addrwidth; 66878064Sume ifname = if_indextoname(sdl->sdl_index, ifix_buf); 66978064Sume if (!ifname) 67078064Sume ifname = "?"; 67178064Sume ifwidth = strlen(ifname); 67278064Sume if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 67378064Sume ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 67455505Sshin 67578064Sume printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 67678064Sume llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 67755505Sshin 67855505Sshin /* Print neighbor discovery specific informations */ 67962590Sitojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 68055505Sshin if (nbi) { 681253970Shrs if (nbi->expire > now.tv_sec) { 68255505Sshin printf(" %-9.9s", 683253970Shrs sec2str(nbi->expire - now.tv_sec)); 68478064Sume } else if (nbi->expire == 0) 68555505Sshin printf(" %-9.9s", "permanent"); 68655505Sshin else 68755505Sshin printf(" %-9.9s", "expired"); 68855505Sshin 689121156Sume switch (nbi->state) { 690121156Sume case ND6_LLINFO_NOSTATE: 69155505Sshin printf(" N"); 69255505Sshin break; 69378064Sume#ifdef ND6_LLINFO_WAITDELETE 694121156Sume case ND6_LLINFO_WAITDELETE: 69555505Sshin printf(" W"); 69655505Sshin break; 69778064Sume#endif 698121156Sume case ND6_LLINFO_INCOMPLETE: 69955505Sshin printf(" I"); 70055505Sshin break; 701121156Sume case ND6_LLINFO_REACHABLE: 70255505Sshin printf(" R"); 70355505Sshin break; 704121156Sume case ND6_LLINFO_STALE: 70555505Sshin printf(" S"); 70655505Sshin break; 707121156Sume case ND6_LLINFO_DELAY: 70855505Sshin printf(" D"); 70955505Sshin break; 710121156Sume case ND6_LLINFO_PROBE: 71155505Sshin printf(" P"); 71255505Sshin break; 713121156Sume default: 71455505Sshin printf(" ?"); 71555505Sshin break; 71655505Sshin } 71755505Sshin 71855505Sshin isrouter = nbi->isrouter; 71955505Sshin prbs = nbi->asked; 72078064Sume } else { 72155505Sshin warnx("failed to get neighbor information"); 72255505Sshin printf(" "); 72355505Sshin } 72455505Sshin 72562590Sitojun /* 72662590Sitojun * other flags. R: router, P: proxy, W: ?? 72762590Sitojun */ 72862590Sitojun if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 72962590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s", 730121156Sume isrouter ? "R" : "", 731121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 73262590Sitojun } else { 733287995Sdelphij#if 0 /* W and P are mystery even for us */ 73462590Sitojun sin = (struct sockaddr_in6 *) 735121156Sume (sdl->sdl_len + (char *)sdl); 73662590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 737121156Sume isrouter ? "R" : "", 738121156Sume !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 739121156Sume (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 740121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 741122615Sume#else 742122615Sume snprintf(flgbuf, sizeof(flgbuf), "%s%s", 743122615Sume isrouter ? "R" : "", 744122615Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 745122615Sume#endif 74655505Sshin } 747122615Sume printf(" %s", flgbuf); 74855505Sshin 74955505Sshin if (prbs) 750122615Sume printf(" %d", prbs); 75155505Sshin 75255505Sshin printf("\n"); 75355505Sshin } 75478064Sume if (buf != NULL) 75578064Sume free(buf); 75655505Sshin 75755505Sshin if (repeat) { 75855505Sshin printf("\n"); 759125675Ssumikawa fflush(stdout); 76055505Sshin sleep(repeat); 76155505Sshin goto again; 76255505Sshin } 76355505Sshin} 76455505Sshin 76555505Sshinstatic struct in6_nbrinfo * 766259169Saegetnbrinfo(struct in6_addr *addr, int ifindex, int warning) 76755505Sshin{ 76855505Sshin static struct in6_nbrinfo nbi; 76955505Sshin int s; 77055505Sshin 77155505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 77255505Sshin err(1, "socket"); 77355505Sshin 77455505Sshin bzero(&nbi, sizeof(nbi)); 77555505Sshin if_indextoname(ifindex, nbi.ifname); 77655505Sshin nbi.addr = *addr; 77755505Sshin if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 77862590Sitojun if (warning) 77962590Sitojun warn("ioctl(SIOCGNBRINFO_IN6)"); 78055505Sshin close(s); 78155505Sshin return(NULL); 78255505Sshin } 78355505Sshin 78455505Sshin close(s); 78555505Sshin return(&nbi); 78655505Sshin} 78755505Sshin 78855505Sshinstatic char * 789217140Sdelphijether_str(struct sockaddr_dl *sdl) 79055505Sshin{ 791121156Sume static char hbuf[NI_MAXHOST]; 79255505Sshin 793219819Sjeff if (sdl->sdl_alen == ETHER_ADDR_LEN) { 794217140Sdelphij strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)), 795217140Sdelphij sizeof(hbuf)); 796219819Sjeff } else if (sdl->sdl_alen) { 797219819Sjeff int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 798219819Sjeff snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n); 799219819Sjeff } else 800121156Sume snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 80155505Sshin 802121156Sume return(hbuf); 80355505Sshin} 80455505Sshin 805287097Shrsstatic int 806259169Saendp_ether_aton(char *a, u_char *n) 80755505Sshin{ 80855505Sshin int i, o[6]; 80955505Sshin 81055505Sshin i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 811121156Sume &o[3], &o[4], &o[5]); 81255505Sshin if (i != 6) { 81355505Sshin fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 81455505Sshin return (1); 81555505Sshin } 816121156Sume for (i = 0; i < 6; i++) 81755505Sshin n[i] = o[i]; 81855505Sshin return (0); 81955505Sshin} 82055505Sshin 821287097Shrsstatic void 82255505Sshinusage() 82355505Sshin{ 824122615Sume printf("usage: ndp [-nt] hostname\n"); 825122615Sume printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 82678064Sume printf(" ndp [-nt] -A wait\n"); 827122615Sume printf(" ndp [-nt] -d hostname\n"); 828122615Sume printf(" ndp [-nt] -f filename\n"); 829122615Sume printf(" ndp [-nt] -i interface [flags...]\n"); 83062590Sitojun#ifdef SIOCSDEFIFACE_IN6 831122615Sume printf(" ndp [-nt] -I [interface|delete]\n"); 83262590Sitojun#endif 833122615Sume printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 83455505Sshin exit(1); 83555505Sshin} 83655505Sshin 837287097Shrsstatic int 838259169Saertmsg(int cmd) 83955505Sshin{ 84055505Sshin static int seq; 84155505Sshin int rlen; 84255505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 84355505Sshin register char *cp = m_rtmsg.m_space; 84455505Sshin register int l; 84555505Sshin 84655505Sshin errno = 0; 84755505Sshin if (cmd == RTM_DELETE) 84855505Sshin goto doit; 84955505Sshin bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 85055505Sshin rtm->rtm_flags = flags; 85155505Sshin rtm->rtm_version = RTM_VERSION; 85255505Sshin 85355505Sshin switch (cmd) { 85455505Sshin default: 85555505Sshin fprintf(stderr, "ndp: internal wrong cmd\n"); 85655505Sshin exit(1); 85755505Sshin case RTM_ADD: 85855505Sshin rtm->rtm_addrs |= RTA_GATEWAY; 859122615Sume if (expire_time) { 860122615Sume rtm->rtm_rmx.rmx_expire = expire_time; 861122615Sume rtm->rtm_inits = RTV_EXPIRE; 862122615Sume } 863186500Sqingli rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 864151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 86562590Sitojun if (rtm->rtm_flags & RTF_ANNOUNCE) { 86662590Sitojun rtm->rtm_flags &= ~RTF_HOST; 867124241Ssuz rtm->rtm_addrs |= RTA_NETMASK; 86862590Sitojun } 869151473Ssuz#endif 87055505Sshin /* FALLTHROUGH */ 87155505Sshin case RTM_GET: 87255505Sshin rtm->rtm_addrs |= RTA_DST; 87355505Sshin } 87455505Sshin 87555505Sshin NEXTADDR(RTA_DST, sin_m); 87655505Sshin NEXTADDR(RTA_GATEWAY, sdl_m); 877151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 87862590Sitojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 87955505Sshin NEXTADDR(RTA_NETMASK, so_mask); 880151473Ssuz#endif 88155505Sshin 88255505Sshin rtm->rtm_msglen = cp - (char *)&m_rtmsg; 88355505Sshindoit: 88455505Sshin l = rtm->rtm_msglen; 88555505Sshin rtm->rtm_seq = ++seq; 88655505Sshin rtm->rtm_type = cmd; 88755505Sshin if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 88855505Sshin if (errno != ESRCH || cmd != RTM_DELETE) { 889121156Sume err(1, "writing to routing socket"); 890121156Sume /* NOTREACHED */ 89155505Sshin } 89255505Sshin } 89355505Sshin do { 89455505Sshin l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 89555505Sshin } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 89655505Sshin if (l < 0) 89755505Sshin (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 89855505Sshin strerror(errno)); 89955505Sshin return (0); 90055505Sshin} 90155505Sshin 902287097Shrsstatic void 903259169Saeifinfo(char *ifname, int argc, char **argv) 90455505Sshin{ 90555505Sshin struct in6_ndireq nd; 90662590Sitojun int i, s; 90762590Sitojun u_int32_t newflags; 90878064Sume#ifdef IPV6CTL_USETEMPADDR 90978064Sume u_int8_t nullbuf[8]; 91078064Sume#endif 91155505Sshin 91255505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 913121156Sume err(1, "socket"); 914121156Sume /* NOTREACHED */ 91555505Sshin } 91655505Sshin bzero(&nd, sizeof(nd)); 917121156Sume strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 91855505Sshin if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 919121156Sume err(1, "ioctl(SIOCGIFINFO_IN6)"); 920121156Sume /* NOTREACHED */ 921122615Sume } 92262590Sitojun#define ND nd.ndi 92362590Sitojun newflags = ND.flags; 924122615Sume for (i = 0; i < argc; i++) { 92562590Sitojun int clear = 0; 92662590Sitojun char *cp = argv[i]; 92762590Sitojun 92862590Sitojun if (*cp == '-') { 92962590Sitojun clear = 1; 93062590Sitojun cp++; 93162590Sitojun } 93262590Sitojun 93362590Sitojun#define SETFLAG(s, f) \ 93462590Sitojun do {\ 93562590Sitojun if (strcmp(cp, (s)) == 0) {\ 93662590Sitojun if (clear)\ 93762590Sitojun newflags &= ~(f);\ 93862590Sitojun else\ 93962590Sitojun newflags |= (f);\ 94062590Sitojun }\ 94162590Sitojun } while (0) 942151468Ssuz/* 943151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against 944151468Ssuz * "nudbogus". But we just let it go since this is minor. 945151468Ssuz */ 946151468Ssuz#define SETVALUE(f, v) \ 947151468Ssuz do { \ 948151468Ssuz char *valptr; \ 949151468Ssuz unsigned long newval; \ 950151468Ssuz v = 0; /* unspecified */ \ 951151468Ssuz if (strncmp(cp, f, strlen(f)) == 0) { \ 952151468Ssuz valptr = strchr(cp, '='); \ 953151468Ssuz if (valptr == NULL) \ 954151468Ssuz err(1, "syntax error in %s field", (f)); \ 955151468Ssuz errno = 0; \ 956151468Ssuz newval = strtoul(++valptr, NULL, 0); \ 957151468Ssuz if (errno) \ 958151468Ssuz err(1, "syntax error in %s's value", (f)); \ 959151468Ssuz v = newval; \ 960151468Ssuz } \ 961151468Ssuz } while (0) 962151468Ssuz 963151474Ssuz SETFLAG("disabled", ND6_IFF_IFDISABLED); 96462590Sitojun SETFLAG("nud", ND6_IFF_PERFORMNUD); 965118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 966118498Sume SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 967118498Sume#endif 968197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 969197138Shrs SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 970197138Shrs#endif 971245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 972245230Sume SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE); 973245230Sume#endif 974151468Ssuz SETVALUE("basereachable", ND.basereachable); 975151468Ssuz SETVALUE("retrans", ND.retrans); 976151468Ssuz SETVALUE("curhlim", ND.chlim); 97762590Sitojun 97862590Sitojun ND.flags = newflags; 979151468Ssuz if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 980151468Ssuz err(1, "ioctl(SIOCSIFINFO_IN6)"); 981121156Sume /* NOTREACHED */ 98262590Sitojun } 98362590Sitojun#undef SETFLAG 984151468Ssuz#undef SETVALUE 98562590Sitojun } 98662590Sitojun 987121162Sume if (!ND.initialized) { 988121162Sume errx(1, "%s: not initialized yet", ifname); 989121162Sume /* NOTREACHED */ 990121162Sume } 991121162Sume 992151468Ssuz if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 993151468Ssuz err(1, "ioctl(SIOCGIFINFO_IN6)"); 994151468Ssuz /* NOTREACHED */ 995151468Ssuz } 99655505Sshin printf("linkmtu=%d", ND.linkmtu); 997121471Sume printf(", maxmtu=%d", ND.maxmtu); 99855505Sshin printf(", curhlim=%d", ND.chlim); 99955505Sshin printf(", basereachable=%ds%dms", 1000121156Sume ND.basereachable / 1000, ND.basereachable % 1000); 100155505Sshin printf(", reachable=%ds", ND.reachable); 100262590Sitojun printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 100378064Sume#ifdef IPV6CTL_USETEMPADDR 100478064Sume memset(nullbuf, 0, sizeof(nullbuf)); 100578064Sume if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 100678064Sume int j; 100778064Sume u_int8_t *rbuf; 100878064Sume 100978064Sume for (i = 0; i < 3; i++) { 1010121156Sume switch (i) { 101178064Sume case 0: 101278064Sume printf("\nRandom seed(0): "); 101378064Sume rbuf = ND.randomseed0; 101478064Sume break; 101578064Sume case 1: 101678064Sume printf("\nRandom seed(1): "); 101778064Sume rbuf = ND.randomseed1; 101878064Sume break; 101978064Sume case 2: 102078064Sume printf("\nRandom ID: "); 102178064Sume rbuf = ND.randomid; 102278064Sume break; 1023151472Ssuz default: 1024151472Ssuz errx(1, "impossible case for tempaddr display"); 102578064Sume } 102678064Sume for (j = 0; j < 8; j++) 102778064Sume printf("%02x", rbuf[j]); 102878064Sume } 102978064Sume } 103078064Sume#endif 103162590Sitojun if (ND.flags) { 103262590Sitojun printf("\nFlags: "); 1033151474Ssuz#ifdef ND6_IFF_IFDISABLED 1034151474Ssuz if ((ND.flags & ND6_IFF_IFDISABLED)) 1035151474Ssuz printf("disabled "); 1036151474Ssuz#endif 1037118498Sume if ((ND.flags & ND6_IFF_PERFORMNUD)) 1038118498Sume printf("nud "); 1039118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 1040118498Sume if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1041118498Sume printf("accept_rtadv "); 1042118498Sume#endif 1043197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 1044197138Shrs if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 1045197138Shrs printf("auto_linklocal "); 1046197138Shrs#endif 1047245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 1048245230Sume if ((ND.flags & ND6_IFF_NO_PREFER_IFACE)) 1049245230Sume printf("no_prefer_iface "); 1050245230Sume#endif 1051122615Sume } 105262590Sitojun putc('\n', stdout); 105355505Sshin#undef ND 1054121156Sume 105555505Sshin close(s); 105655505Sshin} 105755505Sshin 105878064Sume#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 105978064Sume#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 106078064Sume#endif 106178064Sume 1062287097Shrsstatic void 106355505Sshinrtrlist() 106455505Sshin{ 106578064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 106678064Sume char *buf; 106778064Sume struct in6_defrouter *p, *ep; 106878064Sume size_t l; 1069253999Shrs struct timeval now; 107078064Sume 107178064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 107278064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 107378064Sume /*NOTREACHED*/ 107478064Sume } 1075151472Ssuz if (l == 0) 1076151472Ssuz return; 107778064Sume buf = malloc(l); 107878064Sume if (!buf) { 1079121156Sume err(1, "malloc"); 108078064Sume /*NOTREACHED*/ 108178064Sume } 108278064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 108378064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 108478064Sume /*NOTREACHED*/ 108578064Sume } 108678064Sume 108778064Sume ep = (struct in6_defrouter *)(buf + l); 108878064Sume for (p = (struct in6_defrouter *)buf; p < ep; p++) { 108978064Sume int rtpref; 109078064Sume 109178064Sume if (getnameinfo((struct sockaddr *)&p->rtaddr, 109278064Sume p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1093121156Sume (nflag ? NI_NUMERICHOST : 0)) != 0) 109478064Sume strlcpy(host_buf, "?", sizeof(host_buf)); 1095121156Sume 109678064Sume printf("%s if=%s", host_buf, 1097121156Sume if_indextoname(p->if_index, ifix_buf)); 109878064Sume printf(", flags=%s%s", 1099121156Sume p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1100121156Sume p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 110178064Sume rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 110278064Sume printf(", pref=%s", rtpref_str[rtpref]); 1103121156Sume 1104253999Shrs gettimeofday(&now, 0); 110578064Sume if (p->expire == 0) 110678064Sume printf(", expire=Never\n"); 110778064Sume else 110878064Sume printf(", expire=%s\n", 1109253970Shrs sec2str(p->expire - now.tv_sec)); 111078064Sume } 111178064Sume free(buf); 111255505Sshin} 111355505Sshin 1114287097Shrsstatic void 111555505Sshinplist() 111655505Sshin{ 111778064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 111878064Sume char *buf; 111978064Sume struct in6_prefix *p, *ep, *n; 112078064Sume struct sockaddr_in6 *advrtr; 112178064Sume size_t l; 1122253999Shrs struct timeval now; 112378064Sume const int niflags = NI_NUMERICHOST; 112478064Sume int ninflags = nflag ? NI_NUMERICHOST : 0; 112578064Sume char namebuf[NI_MAXHOST]; 112678064Sume 112778064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 112878064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 112978064Sume /*NOTREACHED*/ 113078064Sume } 113178064Sume buf = malloc(l); 113278064Sume if (!buf) { 1133121156Sume err(1, "malloc"); 113478064Sume /*NOTREACHED*/ 113578064Sume } 113678064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 113778064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 113878064Sume /*NOTREACHED*/ 113978064Sume } 114078064Sume 114178064Sume ep = (struct in6_prefix *)(buf + l); 114278064Sume for (p = (struct in6_prefix *)buf; p < ep; p = n) { 114378064Sume advrtr = (struct sockaddr_in6 *)(p + 1); 114478064Sume n = (struct in6_prefix *)&advrtr[p->advrtrs]; 114578064Sume 114678064Sume if (getnameinfo((struct sockaddr *)&p->prefix, 114778064Sume p->prefix.sin6_len, namebuf, sizeof(namebuf), 114878064Sume NULL, 0, niflags) != 0) 114978064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 115078064Sume printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1151121156Sume if_indextoname(p->if_index, ifix_buf)); 115278064Sume 1153253999Shrs gettimeofday(&now, 0); 115478064Sume /* 115578064Sume * meaning of fields, especially flags, is very different 115678064Sume * by origin. notify the difference to the users. 115778064Sume */ 115878064Sume printf("flags=%s%s%s%s%s", 1159121156Sume p->raflags.onlink ? "L" : "", 1160121156Sume p->raflags.autonomous ? "A" : "", 1161121156Sume (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1162121156Sume (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 116378064Sume#ifdef NDPRF_HOME 1164121156Sume (p->flags & NDPRF_HOME) != 0 ? "H" : "" 116578064Sume#else 1166121156Sume "" 116778064Sume#endif 1168121156Sume ); 116978064Sume if (p->vltime == ND6_INFINITE_LIFETIME) 117078064Sume printf(" vltime=infinity"); 117178064Sume else 1172122615Sume printf(" vltime=%lu", (unsigned long)p->vltime); 117378064Sume if (p->pltime == ND6_INFINITE_LIFETIME) 117478064Sume printf(", pltime=infinity"); 117578064Sume else 1176122615Sume printf(", pltime=%lu", (unsigned long)p->pltime); 117778064Sume if (p->expire == 0) 117878064Sume printf(", expire=Never"); 1179253970Shrs else if (p->expire >= now.tv_sec) 118078064Sume printf(", expire=%s", 1181253970Shrs sec2str(p->expire - now.tv_sec)); 118278064Sume else 118378064Sume printf(", expired"); 118478064Sume printf(", ref=%d", p->refcnt); 118578064Sume printf("\n"); 118678064Sume /* 118778064Sume * "advertising router" list is meaningful only if the prefix 118878064Sume * information is from RA. 118978064Sume */ 119078064Sume if (p->advrtrs) { 119178064Sume int j; 119278064Sume struct sockaddr_in6 *sin6; 119378064Sume 1194122615Sume sin6 = advrtr; 119578064Sume printf(" advertised by\n"); 119678064Sume for (j = 0; j < p->advrtrs; j++) { 119778064Sume struct in6_nbrinfo *nbi; 119878064Sume 119978064Sume if (getnameinfo((struct sockaddr *)sin6, 120078064Sume sin6->sin6_len, namebuf, sizeof(namebuf), 120178064Sume NULL, 0, ninflags) != 0) 120278064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 120378064Sume printf(" %s", namebuf); 120478064Sume 1205121156Sume nbi = getnbrinfo(&sin6->sin6_addr, 1206121156Sume p->if_index, 0); 120778064Sume if (nbi) { 1208121156Sume switch (nbi->state) { 120978064Sume case ND6_LLINFO_REACHABLE: 121078064Sume case ND6_LLINFO_STALE: 121178064Sume case ND6_LLINFO_DELAY: 121278064Sume case ND6_LLINFO_PROBE: 121378064Sume printf(" (reachable)\n"); 121478064Sume break; 121578064Sume default: 121678064Sume printf(" (unreachable)\n"); 121778064Sume } 121878064Sume } else 121978064Sume printf(" (no neighbor state)\n"); 122078064Sume sin6++; 122178064Sume } 122278064Sume } else 122378064Sume printf(" No advertising router\n"); 122478064Sume } 122578064Sume free(buf); 122655505Sshin} 122755505Sshin 1228287097Shrsstatic void 122955505Sshinpfx_flush() 123055505Sshin{ 123155505Sshin char dummyif[IFNAMSIZ+8]; 123255505Sshin int s; 123355505Sshin 123455505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 123555505Sshin err(1, "socket"); 1236121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 123755505Sshin if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1238121156Sume err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 123955505Sshin} 124055505Sshin 1241287097Shrsstatic void 124255505Sshinrtr_flush() 124355505Sshin{ 124455505Sshin char dummyif[IFNAMSIZ+8]; 124555505Sshin int s; 124655505Sshin 124755505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 124855505Sshin err(1, "socket"); 1249121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 125055505Sshin if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1251121156Sume err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 125262590Sitojun 125362590Sitojun close(s); 125455505Sshin} 125555505Sshin 1256287097Shrsstatic void 125755505Sshinharmonize_rtr() 125855505Sshin{ 125955505Sshin char dummyif[IFNAMSIZ+8]; 126055505Sshin int s; 126155505Sshin 126262590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 126362590Sitojun err(1, "socket"); 1264121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 126562590Sitojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1266121156Sume err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 126762590Sitojun 126862590Sitojun close(s); 126955505Sshin} 127055505Sshin 127162590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 127262590Sitojunstatic void 1273259169Saesetdefif(char *ifname) 127462590Sitojun{ 127562590Sitojun struct in6_ndifreq ndifreq; 127662590Sitojun unsigned int ifindex; 127762590Sitojun 127862590Sitojun if (strcasecmp(ifname, "delete") == 0) 127962590Sitojun ifindex = 0; 128062590Sitojun else { 128162590Sitojun if ((ifindex = if_nametoindex(ifname)) == 0) 128262590Sitojun err(1, "failed to resolve i/f index for %s", ifname); 128362590Sitojun } 128462590Sitojun 128562590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 128662590Sitojun err(1, "socket"); 128762590Sitojun 1288121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 128962590Sitojun ndifreq.ifindex = ifindex; 129062590Sitojun 129162590Sitojun if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1292121156Sume err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 129362590Sitojun 129462590Sitojun close(s); 129562590Sitojun} 129662590Sitojun 129762590Sitojunstatic void 129862590Sitojungetdefif() 129962590Sitojun{ 130062590Sitojun struct in6_ndifreq ndifreq; 130162590Sitojun char ifname[IFNAMSIZ+8]; 130262590Sitojun 130362590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 130462590Sitojun err(1, "socket"); 130562590Sitojun 130662590Sitojun memset(&ndifreq, 0, sizeof(ndifreq)); 1307121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 130862590Sitojun 130962590Sitojun if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1310121156Sume err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 131162590Sitojun 131262590Sitojun if (ndifreq.ifindex == 0) 131362590Sitojun printf("No default interface.\n"); 131462590Sitojun else { 131562590Sitojun if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 131662590Sitojun err(1, "failed to resolve ifname for index %lu", 131762590Sitojun ndifreq.ifindex); 131862590Sitojun printf("ND default interface = %s\n", ifname); 131962590Sitojun } 132062590Sitojun 132162590Sitojun close(s); 132262590Sitojun} 132362590Sitojun#endif 132462590Sitojun 132555505Sshinstatic char * 1326259169Saesec2str(time_t total) 132755505Sshin{ 132855505Sshin static char result[256]; 132955505Sshin int days, hours, mins, secs; 133055505Sshin int first = 1; 133155505Sshin char *p = result; 1332121156Sume char *ep = &result[sizeof(result)]; 1333121156Sume int n; 133455505Sshin 133555505Sshin days = total / 3600 / 24; 133655505Sshin hours = (total / 3600) % 24; 133755505Sshin mins = (total / 60) % 60; 133855505Sshin secs = total % 60; 133955505Sshin 134055505Sshin if (days) { 134155505Sshin first = 0; 1342121156Sume n = snprintf(p, ep - p, "%dd", days); 1343121156Sume if (n < 0 || n >= ep - p) 1344121156Sume return "?"; 1345121156Sume p += n; 134655505Sshin } 134755505Sshin if (!first || hours) { 134855505Sshin first = 0; 1349121156Sume n = snprintf(p, ep - p, "%dh", hours); 1350121156Sume if (n < 0 || n >= ep - p) 1351121156Sume return "?"; 1352121156Sume p += n; 135355505Sshin } 135455505Sshin if (!first || mins) { 135555505Sshin first = 0; 1356121156Sume n = snprintf(p, ep - p, "%dm", mins); 1357121156Sume if (n < 0 || n >= ep - p) 1358121156Sume return "?"; 1359121156Sume p += n; 136055505Sshin } 1361121156Sume snprintf(p, ep - p, "%ds", secs); 136255505Sshin 136355505Sshin return(result); 136455505Sshin} 136555505Sshin 136655505Sshin/* 136755505Sshin * Print the timestamp 136855505Sshin * from tcpdump/util.c 136955505Sshin */ 137055505Sshinstatic void 1371259169Saets_print(const struct timeval *tvp) 137255505Sshin{ 137355505Sshin int s; 137455505Sshin 137555505Sshin /* Default */ 1376253999Shrs s = (tvp->tv_sec + thiszone) % 86400; 137755505Sshin (void)printf("%02d:%02d:%02d.%06u ", 1378253999Shrs s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 137955505Sshin} 1380186119Sqingli 1381186119Sqingli#undef NEXTADDR 1382