ndp.c revision 292465
162590Sitojun/* $FreeBSD: head/usr.sbin/ndp/ndp.c 292465 2015-12-19 09:18:01Z ume $ */ 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; 566253999Shrs struct timeval now; 567292333Smelifaro u_long expire; 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 678289677Seadler /* Print neighbor discovery specific information */ 679292333Smelifaro expire = rtm->rtm_rmx.rmx_expire; 680292333Smelifaro if (expire > now.tv_sec) 681292333Smelifaro printf(" %-9.9s", sec2str(expire - now.tv_sec)); 682292333Smelifaro else if (expire == 0) 683292333Smelifaro printf(" %-9.9s", "permanent"); 684292333Smelifaro else 685292333Smelifaro printf(" %-9.9s", "expired"); 68655505Sshin 687292333Smelifaro switch (rtm->rtm_rmx.rmx_state) { 688292333Smelifaro case ND6_LLINFO_NOSTATE: 689292465Sume printf(" N"); 690292465Sume break; 69178064Sume#ifdef ND6_LLINFO_WAITDELETE 692292333Smelifaro case ND6_LLINFO_WAITDELETE: 693292465Sume printf(" W"); 694292465Sume break; 69578064Sume#endif 696292333Smelifaro case ND6_LLINFO_INCOMPLETE: 697292465Sume printf(" I"); 698292465Sume break; 699292333Smelifaro case ND6_LLINFO_REACHABLE: 700292465Sume printf(" R"); 701292465Sume break; 702292333Smelifaro case ND6_LLINFO_STALE: 703292465Sume printf(" S"); 704292465Sume break; 705292333Smelifaro case ND6_LLINFO_DELAY: 706292465Sume printf(" D"); 707292465Sume break; 708292333Smelifaro case ND6_LLINFO_PROBE: 709292465Sume printf(" P"); 710292465Sume break; 711292333Smelifaro default: 712292465Sume printf(" ?"); 713292465Sume break; 71455505Sshin } 71555505Sshin 716292333Smelifaro isrouter = rtm->rtm_flags & RTF_GATEWAY; 717292333Smelifaro prbs = rtm->rtm_rmx.rmx_pksent; 718292333Smelifaro 71962590Sitojun /* 72062590Sitojun * other flags. R: router, P: proxy, W: ?? 72162590Sitojun */ 72262590Sitojun if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 72362590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s", 724121156Sume isrouter ? "R" : "", 725121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 72662590Sitojun } else { 727287995Sdelphij#if 0 /* W and P are mystery even for us */ 72862590Sitojun sin = (struct sockaddr_in6 *) 729121156Sume (sdl->sdl_len + (char *)sdl); 73062590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 731121156Sume isrouter ? "R" : "", 732121156Sume !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 733121156Sume (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 734121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 735122615Sume#else 736122615Sume snprintf(flgbuf, sizeof(flgbuf), "%s%s", 737122615Sume isrouter ? "R" : "", 738122615Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 739122615Sume#endif 74055505Sshin } 741122615Sume printf(" %s", flgbuf); 74255505Sshin 74355505Sshin if (prbs) 744122615Sume printf(" %d", prbs); 74555505Sshin 74655505Sshin printf("\n"); 74755505Sshin } 74878064Sume if (buf != NULL) 74978064Sume free(buf); 75055505Sshin 75155505Sshin if (repeat) { 75255505Sshin printf("\n"); 753125675Ssumikawa fflush(stdout); 75455505Sshin sleep(repeat); 75555505Sshin goto again; 75655505Sshin } 75755505Sshin} 75855505Sshin 75955505Sshinstatic struct in6_nbrinfo * 760259169Saegetnbrinfo(struct in6_addr *addr, int ifindex, int warning) 76155505Sshin{ 76255505Sshin static struct in6_nbrinfo nbi; 76355505Sshin int s; 76455505Sshin 76555505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 76655505Sshin err(1, "socket"); 76755505Sshin 76855505Sshin bzero(&nbi, sizeof(nbi)); 76955505Sshin if_indextoname(ifindex, nbi.ifname); 77055505Sshin nbi.addr = *addr; 77155505Sshin if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 77262590Sitojun if (warning) 77362590Sitojun warn("ioctl(SIOCGNBRINFO_IN6)"); 77455505Sshin close(s); 77555505Sshin return(NULL); 77655505Sshin } 77755505Sshin 77855505Sshin close(s); 77955505Sshin return(&nbi); 78055505Sshin} 78155505Sshin 78255505Sshinstatic char * 783217140Sdelphijether_str(struct sockaddr_dl *sdl) 78455505Sshin{ 785121156Sume static char hbuf[NI_MAXHOST]; 78655505Sshin 787219819Sjeff if (sdl->sdl_alen == ETHER_ADDR_LEN) { 788217140Sdelphij strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)), 789217140Sdelphij sizeof(hbuf)); 790219819Sjeff } else if (sdl->sdl_alen) { 791219819Sjeff int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 792219819Sjeff snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n); 793219819Sjeff } else 794121156Sume snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 79555505Sshin 796121156Sume return(hbuf); 79755505Sshin} 79855505Sshin 799287097Shrsstatic int 800259169Saendp_ether_aton(char *a, u_char *n) 80155505Sshin{ 80255505Sshin int i, o[6]; 80355505Sshin 80455505Sshin i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 805121156Sume &o[3], &o[4], &o[5]); 80655505Sshin if (i != 6) { 80755505Sshin fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 80855505Sshin return (1); 80955505Sshin } 810121156Sume for (i = 0; i < 6; i++) 81155505Sshin n[i] = o[i]; 81255505Sshin return (0); 81355505Sshin} 81455505Sshin 815287097Shrsstatic void 81655505Sshinusage() 81755505Sshin{ 818122615Sume printf("usage: ndp [-nt] hostname\n"); 819122615Sume printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 82078064Sume printf(" ndp [-nt] -A wait\n"); 821122615Sume printf(" ndp [-nt] -d hostname\n"); 822122615Sume printf(" ndp [-nt] -f filename\n"); 823122615Sume printf(" ndp [-nt] -i interface [flags...]\n"); 82462590Sitojun#ifdef SIOCSDEFIFACE_IN6 825122615Sume printf(" ndp [-nt] -I [interface|delete]\n"); 82662590Sitojun#endif 827122615Sume printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 82855505Sshin exit(1); 82955505Sshin} 83055505Sshin 831287097Shrsstatic int 832259169Saertmsg(int cmd) 83355505Sshin{ 83455505Sshin static int seq; 83555505Sshin int rlen; 83655505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 83755505Sshin register char *cp = m_rtmsg.m_space; 83855505Sshin register int l; 83955505Sshin 84055505Sshin errno = 0; 84155505Sshin if (cmd == RTM_DELETE) 84255505Sshin goto doit; 84355505Sshin bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 84455505Sshin rtm->rtm_flags = flags; 84555505Sshin rtm->rtm_version = RTM_VERSION; 84655505Sshin 84755505Sshin switch (cmd) { 84855505Sshin default: 84955505Sshin fprintf(stderr, "ndp: internal wrong cmd\n"); 85055505Sshin exit(1); 85155505Sshin case RTM_ADD: 85255505Sshin rtm->rtm_addrs |= RTA_GATEWAY; 853122615Sume if (expire_time) { 854122615Sume rtm->rtm_rmx.rmx_expire = expire_time; 855122615Sume rtm->rtm_inits = RTV_EXPIRE; 856122615Sume } 857186500Sqingli rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 858151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 85962590Sitojun if (rtm->rtm_flags & RTF_ANNOUNCE) { 86062590Sitojun rtm->rtm_flags &= ~RTF_HOST; 861124241Ssuz rtm->rtm_addrs |= RTA_NETMASK; 86262590Sitojun } 863151473Ssuz#endif 86455505Sshin /* FALLTHROUGH */ 86555505Sshin case RTM_GET: 86655505Sshin rtm->rtm_addrs |= RTA_DST; 86755505Sshin } 86855505Sshin 86955505Sshin NEXTADDR(RTA_DST, sin_m); 87055505Sshin NEXTADDR(RTA_GATEWAY, sdl_m); 871151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 87262590Sitojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 87355505Sshin NEXTADDR(RTA_NETMASK, so_mask); 874151473Ssuz#endif 87555505Sshin 87655505Sshin rtm->rtm_msglen = cp - (char *)&m_rtmsg; 87755505Sshindoit: 87855505Sshin l = rtm->rtm_msglen; 87955505Sshin rtm->rtm_seq = ++seq; 88055505Sshin rtm->rtm_type = cmd; 88155505Sshin if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 88255505Sshin if (errno != ESRCH || cmd != RTM_DELETE) { 883121156Sume err(1, "writing to routing socket"); 884121156Sume /* NOTREACHED */ 88555505Sshin } 88655505Sshin } 88755505Sshin do { 88855505Sshin l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 88955505Sshin } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 89055505Sshin if (l < 0) 89155505Sshin (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 89255505Sshin strerror(errno)); 89355505Sshin return (0); 89455505Sshin} 89555505Sshin 896287097Shrsstatic void 897259169Saeifinfo(char *ifname, int argc, char **argv) 89855505Sshin{ 89955505Sshin struct in6_ndireq nd; 90062590Sitojun int i, s; 90162590Sitojun u_int32_t newflags; 90278064Sume#ifdef IPV6CTL_USETEMPADDR 90378064Sume u_int8_t nullbuf[8]; 90478064Sume#endif 90555505Sshin 90655505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 907121156Sume err(1, "socket"); 908121156Sume /* NOTREACHED */ 90955505Sshin } 91055505Sshin bzero(&nd, sizeof(nd)); 911121156Sume strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 91255505Sshin if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 913121156Sume err(1, "ioctl(SIOCGIFINFO_IN6)"); 914121156Sume /* NOTREACHED */ 915122615Sume } 91662590Sitojun#define ND nd.ndi 91762590Sitojun newflags = ND.flags; 918122615Sume for (i = 0; i < argc; i++) { 91962590Sitojun int clear = 0; 92062590Sitojun char *cp = argv[i]; 92162590Sitojun 92262590Sitojun if (*cp == '-') { 92362590Sitojun clear = 1; 92462590Sitojun cp++; 92562590Sitojun } 92662590Sitojun 92762590Sitojun#define SETFLAG(s, f) \ 92862590Sitojun do {\ 92962590Sitojun if (strcmp(cp, (s)) == 0) {\ 93062590Sitojun if (clear)\ 93162590Sitojun newflags &= ~(f);\ 93262590Sitojun else\ 93362590Sitojun newflags |= (f);\ 93462590Sitojun }\ 93562590Sitojun } while (0) 936151468Ssuz/* 937151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against 938151468Ssuz * "nudbogus". But we just let it go since this is minor. 939151468Ssuz */ 940151468Ssuz#define SETVALUE(f, v) \ 941151468Ssuz do { \ 942151468Ssuz char *valptr; \ 943151468Ssuz unsigned long newval; \ 944151468Ssuz v = 0; /* unspecified */ \ 945151468Ssuz if (strncmp(cp, f, strlen(f)) == 0) { \ 946151468Ssuz valptr = strchr(cp, '='); \ 947151468Ssuz if (valptr == NULL) \ 948151468Ssuz err(1, "syntax error in %s field", (f)); \ 949151468Ssuz errno = 0; \ 950151468Ssuz newval = strtoul(++valptr, NULL, 0); \ 951151468Ssuz if (errno) \ 952151468Ssuz err(1, "syntax error in %s's value", (f)); \ 953151468Ssuz v = newval; \ 954151468Ssuz } \ 955151468Ssuz } while (0) 956151468Ssuz 957151474Ssuz SETFLAG("disabled", ND6_IFF_IFDISABLED); 95862590Sitojun SETFLAG("nud", ND6_IFF_PERFORMNUD); 959118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 960118498Sume SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 961118498Sume#endif 962197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 963197138Shrs SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 964197138Shrs#endif 965245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 966245230Sume SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE); 967245230Sume#endif 968151468Ssuz SETVALUE("basereachable", ND.basereachable); 969151468Ssuz SETVALUE("retrans", ND.retrans); 970151468Ssuz SETVALUE("curhlim", ND.chlim); 97162590Sitojun 97262590Sitojun ND.flags = newflags; 973151468Ssuz if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 974151468Ssuz err(1, "ioctl(SIOCSIFINFO_IN6)"); 975121156Sume /* NOTREACHED */ 97662590Sitojun } 97762590Sitojun#undef SETFLAG 978151468Ssuz#undef SETVALUE 97962590Sitojun } 98062590Sitojun 981121162Sume if (!ND.initialized) { 982121162Sume errx(1, "%s: not initialized yet", ifname); 983121162Sume /* NOTREACHED */ 984121162Sume } 985121162Sume 986151468Ssuz if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 987151468Ssuz err(1, "ioctl(SIOCGIFINFO_IN6)"); 988151468Ssuz /* NOTREACHED */ 989151468Ssuz } 99055505Sshin printf("linkmtu=%d", ND.linkmtu); 991121471Sume printf(", maxmtu=%d", ND.maxmtu); 99255505Sshin printf(", curhlim=%d", ND.chlim); 99355505Sshin printf(", basereachable=%ds%dms", 994121156Sume ND.basereachable / 1000, ND.basereachable % 1000); 99555505Sshin printf(", reachable=%ds", ND.reachable); 99662590Sitojun printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 99778064Sume#ifdef IPV6CTL_USETEMPADDR 99878064Sume memset(nullbuf, 0, sizeof(nullbuf)); 99978064Sume if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 100078064Sume int j; 100178064Sume u_int8_t *rbuf; 100278064Sume 100378064Sume for (i = 0; i < 3; i++) { 1004121156Sume switch (i) { 100578064Sume case 0: 100678064Sume printf("\nRandom seed(0): "); 100778064Sume rbuf = ND.randomseed0; 100878064Sume break; 100978064Sume case 1: 101078064Sume printf("\nRandom seed(1): "); 101178064Sume rbuf = ND.randomseed1; 101278064Sume break; 101378064Sume case 2: 101478064Sume printf("\nRandom ID: "); 101578064Sume rbuf = ND.randomid; 101678064Sume break; 1017151472Ssuz default: 1018151472Ssuz errx(1, "impossible case for tempaddr display"); 101978064Sume } 102078064Sume for (j = 0; j < 8; j++) 102178064Sume printf("%02x", rbuf[j]); 102278064Sume } 102378064Sume } 102478064Sume#endif 102562590Sitojun if (ND.flags) { 102662590Sitojun printf("\nFlags: "); 1027151474Ssuz#ifdef ND6_IFF_IFDISABLED 1028151474Ssuz if ((ND.flags & ND6_IFF_IFDISABLED)) 1029151474Ssuz printf("disabled "); 1030151474Ssuz#endif 1031118498Sume if ((ND.flags & ND6_IFF_PERFORMNUD)) 1032118498Sume printf("nud "); 1033118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 1034118498Sume if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1035118498Sume printf("accept_rtadv "); 1036118498Sume#endif 1037197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 1038197138Shrs if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 1039197138Shrs printf("auto_linklocal "); 1040197138Shrs#endif 1041245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 1042245230Sume if ((ND.flags & ND6_IFF_NO_PREFER_IFACE)) 1043245230Sume printf("no_prefer_iface "); 1044245230Sume#endif 1045122615Sume } 104662590Sitojun putc('\n', stdout); 104755505Sshin#undef ND 1048121156Sume 104955505Sshin close(s); 105055505Sshin} 105155505Sshin 105278064Sume#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 105378064Sume#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 105478064Sume#endif 105578064Sume 1056287097Shrsstatic void 105755505Sshinrtrlist() 105855505Sshin{ 105978064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 106078064Sume char *buf; 106178064Sume struct in6_defrouter *p, *ep; 106278064Sume size_t l; 1063253999Shrs struct timeval now; 106478064Sume 106578064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 106678064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 106778064Sume /*NOTREACHED*/ 106878064Sume } 1069151472Ssuz if (l == 0) 1070151472Ssuz return; 107178064Sume buf = malloc(l); 107278064Sume if (!buf) { 1073121156Sume err(1, "malloc"); 107478064Sume /*NOTREACHED*/ 107578064Sume } 107678064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 107778064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 107878064Sume /*NOTREACHED*/ 107978064Sume } 108078064Sume 108178064Sume ep = (struct in6_defrouter *)(buf + l); 108278064Sume for (p = (struct in6_defrouter *)buf; p < ep; p++) { 108378064Sume int rtpref; 108478064Sume 108578064Sume if (getnameinfo((struct sockaddr *)&p->rtaddr, 108678064Sume p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1087121156Sume (nflag ? NI_NUMERICHOST : 0)) != 0) 108878064Sume strlcpy(host_buf, "?", sizeof(host_buf)); 1089121156Sume 109078064Sume printf("%s if=%s", host_buf, 1091121156Sume if_indextoname(p->if_index, ifix_buf)); 109278064Sume printf(", flags=%s%s", 1093121156Sume p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1094121156Sume p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 109578064Sume rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 109678064Sume printf(", pref=%s", rtpref_str[rtpref]); 1097121156Sume 1098253999Shrs gettimeofday(&now, 0); 109978064Sume if (p->expire == 0) 110078064Sume printf(", expire=Never\n"); 110178064Sume else 110278064Sume printf(", expire=%s\n", 1103253970Shrs sec2str(p->expire - now.tv_sec)); 110478064Sume } 110578064Sume free(buf); 110655505Sshin} 110755505Sshin 1108287097Shrsstatic void 110955505Sshinplist() 111055505Sshin{ 111178064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 111278064Sume char *buf; 111378064Sume struct in6_prefix *p, *ep, *n; 111478064Sume struct sockaddr_in6 *advrtr; 111578064Sume size_t l; 1116253999Shrs struct timeval now; 111778064Sume const int niflags = NI_NUMERICHOST; 111878064Sume int ninflags = nflag ? NI_NUMERICHOST : 0; 111978064Sume char namebuf[NI_MAXHOST]; 112078064Sume 112178064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 112278064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 112378064Sume /*NOTREACHED*/ 112478064Sume } 112578064Sume buf = malloc(l); 112678064Sume if (!buf) { 1127121156Sume err(1, "malloc"); 112878064Sume /*NOTREACHED*/ 112978064Sume } 113078064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 113178064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 113278064Sume /*NOTREACHED*/ 113378064Sume } 113478064Sume 113578064Sume ep = (struct in6_prefix *)(buf + l); 113678064Sume for (p = (struct in6_prefix *)buf; p < ep; p = n) { 113778064Sume advrtr = (struct sockaddr_in6 *)(p + 1); 113878064Sume n = (struct in6_prefix *)&advrtr[p->advrtrs]; 113978064Sume 114078064Sume if (getnameinfo((struct sockaddr *)&p->prefix, 114178064Sume p->prefix.sin6_len, namebuf, sizeof(namebuf), 114278064Sume NULL, 0, niflags) != 0) 114378064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 114478064Sume printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1145121156Sume if_indextoname(p->if_index, ifix_buf)); 114678064Sume 1147253999Shrs gettimeofday(&now, 0); 114878064Sume /* 114978064Sume * meaning of fields, especially flags, is very different 115078064Sume * by origin. notify the difference to the users. 115178064Sume */ 115278064Sume printf("flags=%s%s%s%s%s", 1153121156Sume p->raflags.onlink ? "L" : "", 1154121156Sume p->raflags.autonomous ? "A" : "", 1155121156Sume (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1156121156Sume (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 115778064Sume#ifdef NDPRF_HOME 1158121156Sume (p->flags & NDPRF_HOME) != 0 ? "H" : "" 115978064Sume#else 1160121156Sume "" 116178064Sume#endif 1162121156Sume ); 116378064Sume if (p->vltime == ND6_INFINITE_LIFETIME) 116478064Sume printf(" vltime=infinity"); 116578064Sume else 1166122615Sume printf(" vltime=%lu", (unsigned long)p->vltime); 116778064Sume if (p->pltime == ND6_INFINITE_LIFETIME) 116878064Sume printf(", pltime=infinity"); 116978064Sume else 1170122615Sume printf(", pltime=%lu", (unsigned long)p->pltime); 117178064Sume if (p->expire == 0) 117278064Sume printf(", expire=Never"); 1173253970Shrs else if (p->expire >= now.tv_sec) 117478064Sume printf(", expire=%s", 1175253970Shrs sec2str(p->expire - now.tv_sec)); 117678064Sume else 117778064Sume printf(", expired"); 117878064Sume printf(", ref=%d", p->refcnt); 117978064Sume printf("\n"); 118078064Sume /* 118178064Sume * "advertising router" list is meaningful only if the prefix 118278064Sume * information is from RA. 118378064Sume */ 118478064Sume if (p->advrtrs) { 118578064Sume int j; 118678064Sume struct sockaddr_in6 *sin6; 118778064Sume 1188122615Sume sin6 = advrtr; 118978064Sume printf(" advertised by\n"); 119078064Sume for (j = 0; j < p->advrtrs; j++) { 119178064Sume struct in6_nbrinfo *nbi; 119278064Sume 119378064Sume if (getnameinfo((struct sockaddr *)sin6, 119478064Sume sin6->sin6_len, namebuf, sizeof(namebuf), 119578064Sume NULL, 0, ninflags) != 0) 119678064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 119778064Sume printf(" %s", namebuf); 119878064Sume 1199121156Sume nbi = getnbrinfo(&sin6->sin6_addr, 1200121156Sume p->if_index, 0); 120178064Sume if (nbi) { 1202121156Sume switch (nbi->state) { 120378064Sume case ND6_LLINFO_REACHABLE: 120478064Sume case ND6_LLINFO_STALE: 120578064Sume case ND6_LLINFO_DELAY: 120678064Sume case ND6_LLINFO_PROBE: 120778064Sume printf(" (reachable)\n"); 120878064Sume break; 120978064Sume default: 121078064Sume printf(" (unreachable)\n"); 121178064Sume } 121278064Sume } else 121378064Sume printf(" (no neighbor state)\n"); 121478064Sume sin6++; 121578064Sume } 121678064Sume } else 121778064Sume printf(" No advertising router\n"); 121878064Sume } 121978064Sume free(buf); 122055505Sshin} 122155505Sshin 1222287097Shrsstatic void 122355505Sshinpfx_flush() 122455505Sshin{ 122555505Sshin char dummyif[IFNAMSIZ+8]; 122655505Sshin int s; 122755505Sshin 122855505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 122955505Sshin err(1, "socket"); 1230121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 123155505Sshin if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1232121156Sume err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 123355505Sshin} 123455505Sshin 1235287097Shrsstatic void 123655505Sshinrtr_flush() 123755505Sshin{ 123855505Sshin char dummyif[IFNAMSIZ+8]; 123955505Sshin int s; 124055505Sshin 124155505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 124255505Sshin err(1, "socket"); 1243121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 124455505Sshin if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1245121156Sume err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 124662590Sitojun 124762590Sitojun close(s); 124855505Sshin} 124955505Sshin 1250287097Shrsstatic void 125155505Sshinharmonize_rtr() 125255505Sshin{ 125355505Sshin char dummyif[IFNAMSIZ+8]; 125455505Sshin int s; 125555505Sshin 125662590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 125762590Sitojun err(1, "socket"); 1258121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 125962590Sitojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1260121156Sume err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 126162590Sitojun 126262590Sitojun close(s); 126355505Sshin} 126455505Sshin 126562590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 126662590Sitojunstatic void 1267259169Saesetdefif(char *ifname) 126862590Sitojun{ 126962590Sitojun struct in6_ndifreq ndifreq; 127062590Sitojun unsigned int ifindex; 127162590Sitojun 127262590Sitojun if (strcasecmp(ifname, "delete") == 0) 127362590Sitojun ifindex = 0; 127462590Sitojun else { 127562590Sitojun if ((ifindex = if_nametoindex(ifname)) == 0) 127662590Sitojun err(1, "failed to resolve i/f index for %s", ifname); 127762590Sitojun } 127862590Sitojun 127962590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 128062590Sitojun err(1, "socket"); 128162590Sitojun 1282121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 128362590Sitojun ndifreq.ifindex = ifindex; 128462590Sitojun 128562590Sitojun if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1286121156Sume err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 128762590Sitojun 128862590Sitojun close(s); 128962590Sitojun} 129062590Sitojun 129162590Sitojunstatic void 129262590Sitojungetdefif() 129362590Sitojun{ 129462590Sitojun struct in6_ndifreq ndifreq; 129562590Sitojun char ifname[IFNAMSIZ+8]; 129662590Sitojun 129762590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 129862590Sitojun err(1, "socket"); 129962590Sitojun 130062590Sitojun memset(&ndifreq, 0, sizeof(ndifreq)); 1301121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 130262590Sitojun 130362590Sitojun if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1304121156Sume err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 130562590Sitojun 130662590Sitojun if (ndifreq.ifindex == 0) 130762590Sitojun printf("No default interface.\n"); 130862590Sitojun else { 130962590Sitojun if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 131062590Sitojun err(1, "failed to resolve ifname for index %lu", 131162590Sitojun ndifreq.ifindex); 131262590Sitojun printf("ND default interface = %s\n", ifname); 131362590Sitojun } 131462590Sitojun 131562590Sitojun close(s); 131662590Sitojun} 131762590Sitojun#endif 131862590Sitojun 131955505Sshinstatic char * 1320259169Saesec2str(time_t total) 132155505Sshin{ 132255505Sshin static char result[256]; 132355505Sshin int days, hours, mins, secs; 132455505Sshin int first = 1; 132555505Sshin char *p = result; 1326121156Sume char *ep = &result[sizeof(result)]; 1327121156Sume int n; 132855505Sshin 132955505Sshin days = total / 3600 / 24; 133055505Sshin hours = (total / 3600) % 24; 133155505Sshin mins = (total / 60) % 60; 133255505Sshin secs = total % 60; 133355505Sshin 133455505Sshin if (days) { 133555505Sshin first = 0; 1336121156Sume n = snprintf(p, ep - p, "%dd", days); 1337121156Sume if (n < 0 || n >= ep - p) 1338121156Sume return "?"; 1339121156Sume p += n; 134055505Sshin } 134155505Sshin if (!first || hours) { 134255505Sshin first = 0; 1343121156Sume n = snprintf(p, ep - p, "%dh", hours); 1344121156Sume if (n < 0 || n >= ep - p) 1345121156Sume return "?"; 1346121156Sume p += n; 134755505Sshin } 134855505Sshin if (!first || mins) { 134955505Sshin first = 0; 1350121156Sume n = snprintf(p, ep - p, "%dm", mins); 1351121156Sume if (n < 0 || n >= ep - p) 1352121156Sume return "?"; 1353121156Sume p += n; 135455505Sshin } 1355121156Sume snprintf(p, ep - p, "%ds", secs); 135655505Sshin 135755505Sshin return(result); 135855505Sshin} 135955505Sshin 136055505Sshin/* 136155505Sshin * Print the timestamp 136255505Sshin * from tcpdump/util.c 136355505Sshin */ 136455505Sshinstatic void 1365259169Saets_print(const struct timeval *tvp) 136655505Sshin{ 136755505Sshin int s; 136855505Sshin 136955505Sshin /* Default */ 1370253999Shrs s = (tvp->tv_sec + thiszone) % 86400; 137155505Sshin (void)printf("%02d:%02d:%02d.%06u ", 1372253999Shrs s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 137355505Sshin} 1374186119Sqingli 1375186119Sqingli#undef NEXTADDR 1376