ndp.c revision 287097
162590Sitojun/* $FreeBSD: head/usr.sbin/ndp/ndp.c 287097 2015-08-24 05:38:05Z hrs $ */ 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 65266865Ssumikawa delete(host_buf); 65381366Ssumikawa#endif 65466865Ssumikawa continue; 65566865Ssumikawa } 656253999Shrs gettimeofday(&now, 0); 65755505Sshin if (tflag) 658253970Shrs ts_print(&now); 65955505Sshin 66078064Sume addrwidth = strlen(host_buf); 66178064Sume if (addrwidth < W_ADDR) 66278064Sume addrwidth = W_ADDR; 66378064Sume llwidth = strlen(ether_str(sdl)); 66478064Sume if (W_ADDR + W_LL - addrwidth > llwidth) 66578064Sume llwidth = W_ADDR + W_LL - addrwidth; 66678064Sume ifname = if_indextoname(sdl->sdl_index, ifix_buf); 66778064Sume if (!ifname) 66878064Sume ifname = "?"; 66978064Sume ifwidth = strlen(ifname); 67078064Sume if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 67178064Sume ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 67255505Sshin 67378064Sume printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 67478064Sume llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 67555505Sshin 67655505Sshin /* Print neighbor discovery specific informations */ 67762590Sitojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 67855505Sshin if (nbi) { 679253970Shrs if (nbi->expire > now.tv_sec) { 68055505Sshin printf(" %-9.9s", 681253970Shrs sec2str(nbi->expire - now.tv_sec)); 68278064Sume } else if (nbi->expire == 0) 68355505Sshin printf(" %-9.9s", "permanent"); 68455505Sshin else 68555505Sshin printf(" %-9.9s", "expired"); 68655505Sshin 687121156Sume switch (nbi->state) { 688121156Sume case ND6_LLINFO_NOSTATE: 68955505Sshin printf(" N"); 69055505Sshin break; 69178064Sume#ifdef ND6_LLINFO_WAITDELETE 692121156Sume case ND6_LLINFO_WAITDELETE: 69355505Sshin printf(" W"); 69455505Sshin break; 69578064Sume#endif 696121156Sume case ND6_LLINFO_INCOMPLETE: 69755505Sshin printf(" I"); 69855505Sshin break; 699121156Sume case ND6_LLINFO_REACHABLE: 70055505Sshin printf(" R"); 70155505Sshin break; 702121156Sume case ND6_LLINFO_STALE: 70355505Sshin printf(" S"); 70455505Sshin break; 705121156Sume case ND6_LLINFO_DELAY: 70655505Sshin printf(" D"); 70755505Sshin break; 708121156Sume case ND6_LLINFO_PROBE: 70955505Sshin printf(" P"); 71055505Sshin break; 711121156Sume default: 71255505Sshin printf(" ?"); 71355505Sshin break; 71455505Sshin } 71555505Sshin 71655505Sshin isrouter = nbi->isrouter; 71755505Sshin prbs = nbi->asked; 71878064Sume } else { 71955505Sshin warnx("failed to get neighbor information"); 72055505Sshin printf(" "); 72155505Sshin } 72255505Sshin 72362590Sitojun /* 72462590Sitojun * other flags. R: router, P: proxy, W: ?? 72562590Sitojun */ 72662590Sitojun if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 72762590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s", 728121156Sume isrouter ? "R" : "", 729121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 73062590Sitojun } else { 73162590Sitojun sin = (struct sockaddr_in6 *) 732121156Sume (sdl->sdl_len + (char *)sdl); 733122615Sume#if 0 /* W and P are mystery even for us */ 73462590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 735121156Sume isrouter ? "R" : "", 736121156Sume !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 737121156Sume (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 738121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 739122615Sume#else 740122615Sume snprintf(flgbuf, sizeof(flgbuf), "%s%s", 741122615Sume isrouter ? "R" : "", 742122615Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 743122615Sume#endif 74455505Sshin } 745122615Sume printf(" %s", flgbuf); 74655505Sshin 74755505Sshin if (prbs) 748122615Sume printf(" %d", prbs); 74955505Sshin 75055505Sshin printf("\n"); 75155505Sshin } 75278064Sume if (buf != NULL) 75378064Sume free(buf); 75455505Sshin 75555505Sshin if (repeat) { 75655505Sshin printf("\n"); 757125675Ssumikawa fflush(stdout); 75855505Sshin sleep(repeat); 75955505Sshin goto again; 76055505Sshin } 76155505Sshin} 76255505Sshin 76355505Sshinstatic struct in6_nbrinfo * 764259169Saegetnbrinfo(struct in6_addr *addr, int ifindex, int warning) 76555505Sshin{ 76655505Sshin static struct in6_nbrinfo nbi; 76755505Sshin int s; 76855505Sshin 76955505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 77055505Sshin err(1, "socket"); 77155505Sshin 77255505Sshin bzero(&nbi, sizeof(nbi)); 77355505Sshin if_indextoname(ifindex, nbi.ifname); 77455505Sshin nbi.addr = *addr; 77555505Sshin if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 77662590Sitojun if (warning) 77762590Sitojun warn("ioctl(SIOCGNBRINFO_IN6)"); 77855505Sshin close(s); 77955505Sshin return(NULL); 78055505Sshin } 78155505Sshin 78255505Sshin close(s); 78355505Sshin return(&nbi); 78455505Sshin} 78555505Sshin 78655505Sshinstatic char * 787217140Sdelphijether_str(struct sockaddr_dl *sdl) 78855505Sshin{ 789121156Sume static char hbuf[NI_MAXHOST]; 79055505Sshin 791219819Sjeff if (sdl->sdl_alen == ETHER_ADDR_LEN) { 792217140Sdelphij strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)), 793217140Sdelphij sizeof(hbuf)); 794219819Sjeff } else if (sdl->sdl_alen) { 795219819Sjeff int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 796219819Sjeff snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n); 797219819Sjeff } else 798121156Sume snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 79955505Sshin 800121156Sume return(hbuf); 80155505Sshin} 80255505Sshin 803287097Shrsstatic int 804259169Saendp_ether_aton(char *a, u_char *n) 80555505Sshin{ 80655505Sshin int i, o[6]; 80755505Sshin 80855505Sshin i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 809121156Sume &o[3], &o[4], &o[5]); 81055505Sshin if (i != 6) { 81155505Sshin fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 81255505Sshin return (1); 81355505Sshin } 814121156Sume for (i = 0; i < 6; i++) 81555505Sshin n[i] = o[i]; 81655505Sshin return (0); 81755505Sshin} 81855505Sshin 819287097Shrsstatic void 82055505Sshinusage() 82155505Sshin{ 822122615Sume printf("usage: ndp [-nt] hostname\n"); 823122615Sume printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 82478064Sume printf(" ndp [-nt] -A wait\n"); 825122615Sume printf(" ndp [-nt] -d hostname\n"); 826122615Sume printf(" ndp [-nt] -f filename\n"); 827122615Sume printf(" ndp [-nt] -i interface [flags...]\n"); 82862590Sitojun#ifdef SIOCSDEFIFACE_IN6 829122615Sume printf(" ndp [-nt] -I [interface|delete]\n"); 83062590Sitojun#endif 831122615Sume printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 83255505Sshin exit(1); 83355505Sshin} 83455505Sshin 835287097Shrsstatic int 836259169Saertmsg(int cmd) 83755505Sshin{ 83855505Sshin static int seq; 83955505Sshin int rlen; 84055505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 84155505Sshin register char *cp = m_rtmsg.m_space; 84255505Sshin register int l; 84355505Sshin 84455505Sshin errno = 0; 84555505Sshin if (cmd == RTM_DELETE) 84655505Sshin goto doit; 84755505Sshin bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 84855505Sshin rtm->rtm_flags = flags; 84955505Sshin rtm->rtm_version = RTM_VERSION; 85055505Sshin 85155505Sshin switch (cmd) { 85255505Sshin default: 85355505Sshin fprintf(stderr, "ndp: internal wrong cmd\n"); 85455505Sshin exit(1); 85555505Sshin case RTM_ADD: 85655505Sshin rtm->rtm_addrs |= RTA_GATEWAY; 857122615Sume if (expire_time) { 858122615Sume rtm->rtm_rmx.rmx_expire = expire_time; 859122615Sume rtm->rtm_inits = RTV_EXPIRE; 860122615Sume } 861186500Sqingli rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 862151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 86362590Sitojun if (rtm->rtm_flags & RTF_ANNOUNCE) { 86462590Sitojun rtm->rtm_flags &= ~RTF_HOST; 865124241Ssuz rtm->rtm_addrs |= RTA_NETMASK; 86662590Sitojun } 867151473Ssuz#endif 86855505Sshin /* FALLTHROUGH */ 86955505Sshin case RTM_GET: 87055505Sshin rtm->rtm_addrs |= RTA_DST; 87155505Sshin } 87255505Sshin 87355505Sshin NEXTADDR(RTA_DST, sin_m); 87455505Sshin NEXTADDR(RTA_GATEWAY, sdl_m); 875151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */ 87662590Sitojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 87755505Sshin NEXTADDR(RTA_NETMASK, so_mask); 878151473Ssuz#endif 87955505Sshin 88055505Sshin rtm->rtm_msglen = cp - (char *)&m_rtmsg; 88155505Sshindoit: 88255505Sshin l = rtm->rtm_msglen; 88355505Sshin rtm->rtm_seq = ++seq; 88455505Sshin rtm->rtm_type = cmd; 88555505Sshin if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 88655505Sshin if (errno != ESRCH || cmd != RTM_DELETE) { 887121156Sume err(1, "writing to routing socket"); 888121156Sume /* NOTREACHED */ 88955505Sshin } 89055505Sshin } 89155505Sshin do { 89255505Sshin l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 89355505Sshin } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 89455505Sshin if (l < 0) 89555505Sshin (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 89655505Sshin strerror(errno)); 89755505Sshin return (0); 89855505Sshin} 89955505Sshin 900287097Shrsstatic void 901259169Saeifinfo(char *ifname, int argc, char **argv) 90255505Sshin{ 90355505Sshin struct in6_ndireq nd; 90462590Sitojun int i, s; 90562590Sitojun u_int32_t newflags; 90678064Sume#ifdef IPV6CTL_USETEMPADDR 90778064Sume u_int8_t nullbuf[8]; 90878064Sume#endif 90955505Sshin 91055505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 911121156Sume err(1, "socket"); 912121156Sume /* NOTREACHED */ 91355505Sshin } 91455505Sshin bzero(&nd, sizeof(nd)); 915121156Sume strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 91655505Sshin if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 917121156Sume err(1, "ioctl(SIOCGIFINFO_IN6)"); 918121156Sume /* NOTREACHED */ 919122615Sume } 92062590Sitojun#define ND nd.ndi 92162590Sitojun newflags = ND.flags; 922122615Sume for (i = 0; i < argc; i++) { 92362590Sitojun int clear = 0; 92462590Sitojun char *cp = argv[i]; 92562590Sitojun 92662590Sitojun if (*cp == '-') { 92762590Sitojun clear = 1; 92862590Sitojun cp++; 92962590Sitojun } 93062590Sitojun 93162590Sitojun#define SETFLAG(s, f) \ 93262590Sitojun do {\ 93362590Sitojun if (strcmp(cp, (s)) == 0) {\ 93462590Sitojun if (clear)\ 93562590Sitojun newflags &= ~(f);\ 93662590Sitojun else\ 93762590Sitojun newflags |= (f);\ 93862590Sitojun }\ 93962590Sitojun } while (0) 940151468Ssuz/* 941151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against 942151468Ssuz * "nudbogus". But we just let it go since this is minor. 943151468Ssuz */ 944151468Ssuz#define SETVALUE(f, v) \ 945151468Ssuz do { \ 946151468Ssuz char *valptr; \ 947151468Ssuz unsigned long newval; \ 948151468Ssuz v = 0; /* unspecified */ \ 949151468Ssuz if (strncmp(cp, f, strlen(f)) == 0) { \ 950151468Ssuz valptr = strchr(cp, '='); \ 951151468Ssuz if (valptr == NULL) \ 952151468Ssuz err(1, "syntax error in %s field", (f)); \ 953151468Ssuz errno = 0; \ 954151468Ssuz newval = strtoul(++valptr, NULL, 0); \ 955151468Ssuz if (errno) \ 956151468Ssuz err(1, "syntax error in %s's value", (f)); \ 957151468Ssuz v = newval; \ 958151468Ssuz } \ 959151468Ssuz } while (0) 960151468Ssuz 961151474Ssuz SETFLAG("disabled", ND6_IFF_IFDISABLED); 96262590Sitojun SETFLAG("nud", ND6_IFF_PERFORMNUD); 963118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 964118498Sume SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 965118498Sume#endif 966197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 967197138Shrs SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 968197138Shrs#endif 969245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 970245230Sume SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE); 971245230Sume#endif 972151468Ssuz SETVALUE("basereachable", ND.basereachable); 973151468Ssuz SETVALUE("retrans", ND.retrans); 974151468Ssuz SETVALUE("curhlim", ND.chlim); 97562590Sitojun 97662590Sitojun ND.flags = newflags; 977151468Ssuz if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 978151468Ssuz err(1, "ioctl(SIOCSIFINFO_IN6)"); 979121156Sume /* NOTREACHED */ 98062590Sitojun } 98162590Sitojun#undef SETFLAG 982151468Ssuz#undef SETVALUE 98362590Sitojun } 98462590Sitojun 985121162Sume if (!ND.initialized) { 986121162Sume errx(1, "%s: not initialized yet", ifname); 987121162Sume /* NOTREACHED */ 988121162Sume } 989121162Sume 990151468Ssuz if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 991151468Ssuz err(1, "ioctl(SIOCGIFINFO_IN6)"); 992151468Ssuz /* NOTREACHED */ 993151468Ssuz } 99455505Sshin printf("linkmtu=%d", ND.linkmtu); 995121471Sume printf(", maxmtu=%d", ND.maxmtu); 99655505Sshin printf(", curhlim=%d", ND.chlim); 99755505Sshin printf(", basereachable=%ds%dms", 998121156Sume ND.basereachable / 1000, ND.basereachable % 1000); 99955505Sshin printf(", reachable=%ds", ND.reachable); 100062590Sitojun printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 100178064Sume#ifdef IPV6CTL_USETEMPADDR 100278064Sume memset(nullbuf, 0, sizeof(nullbuf)); 100378064Sume if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 100478064Sume int j; 100578064Sume u_int8_t *rbuf; 100678064Sume 100778064Sume for (i = 0; i < 3; i++) { 1008121156Sume switch (i) { 100978064Sume case 0: 101078064Sume printf("\nRandom seed(0): "); 101178064Sume rbuf = ND.randomseed0; 101278064Sume break; 101378064Sume case 1: 101478064Sume printf("\nRandom seed(1): "); 101578064Sume rbuf = ND.randomseed1; 101678064Sume break; 101778064Sume case 2: 101878064Sume printf("\nRandom ID: "); 101978064Sume rbuf = ND.randomid; 102078064Sume break; 1021151472Ssuz default: 1022151472Ssuz errx(1, "impossible case for tempaddr display"); 102378064Sume } 102478064Sume for (j = 0; j < 8; j++) 102578064Sume printf("%02x", rbuf[j]); 102678064Sume } 102778064Sume } 102878064Sume#endif 102962590Sitojun if (ND.flags) { 103062590Sitojun printf("\nFlags: "); 1031151474Ssuz#ifdef ND6_IFF_IFDISABLED 1032151474Ssuz if ((ND.flags & ND6_IFF_IFDISABLED)) 1033151474Ssuz printf("disabled "); 1034151474Ssuz#endif 1035118498Sume if ((ND.flags & ND6_IFF_PERFORMNUD)) 1036118498Sume printf("nud "); 1037118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 1038118498Sume if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1039118498Sume printf("accept_rtadv "); 1040118498Sume#endif 1041197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL 1042197138Shrs if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 1043197138Shrs printf("auto_linklocal "); 1044197138Shrs#endif 1045245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE 1046245230Sume if ((ND.flags & ND6_IFF_NO_PREFER_IFACE)) 1047245230Sume printf("no_prefer_iface "); 1048245230Sume#endif 1049122615Sume } 105062590Sitojun putc('\n', stdout); 105155505Sshin#undef ND 1052121156Sume 105355505Sshin close(s); 105455505Sshin} 105555505Sshin 105678064Sume#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 105778064Sume#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 105878064Sume#endif 105978064Sume 1060287097Shrsstatic void 106155505Sshinrtrlist() 106255505Sshin{ 106378064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 106478064Sume char *buf; 106578064Sume struct in6_defrouter *p, *ep; 106678064Sume size_t l; 1067253999Shrs struct timeval now; 106878064Sume 106978064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 107078064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 107178064Sume /*NOTREACHED*/ 107278064Sume } 1073151472Ssuz if (l == 0) 1074151472Ssuz return; 107578064Sume buf = malloc(l); 107678064Sume if (!buf) { 1077121156Sume err(1, "malloc"); 107878064Sume /*NOTREACHED*/ 107978064Sume } 108078064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 108178064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 108278064Sume /*NOTREACHED*/ 108378064Sume } 108478064Sume 108578064Sume ep = (struct in6_defrouter *)(buf + l); 108678064Sume for (p = (struct in6_defrouter *)buf; p < ep; p++) { 108778064Sume int rtpref; 108878064Sume 108978064Sume if (getnameinfo((struct sockaddr *)&p->rtaddr, 109078064Sume p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1091121156Sume (nflag ? NI_NUMERICHOST : 0)) != 0) 109278064Sume strlcpy(host_buf, "?", sizeof(host_buf)); 1093121156Sume 109478064Sume printf("%s if=%s", host_buf, 1095121156Sume if_indextoname(p->if_index, ifix_buf)); 109678064Sume printf(", flags=%s%s", 1097121156Sume p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1098121156Sume p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 109978064Sume rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 110078064Sume printf(", pref=%s", rtpref_str[rtpref]); 1101121156Sume 1102253999Shrs gettimeofday(&now, 0); 110378064Sume if (p->expire == 0) 110478064Sume printf(", expire=Never\n"); 110578064Sume else 110678064Sume printf(", expire=%s\n", 1107253970Shrs sec2str(p->expire - now.tv_sec)); 110878064Sume } 110978064Sume free(buf); 111055505Sshin} 111155505Sshin 1112287097Shrsstatic void 111355505Sshinplist() 111455505Sshin{ 111578064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 111678064Sume char *buf; 111778064Sume struct in6_prefix *p, *ep, *n; 111878064Sume struct sockaddr_in6 *advrtr; 111978064Sume size_t l; 1120253999Shrs struct timeval now; 112178064Sume const int niflags = NI_NUMERICHOST; 112278064Sume int ninflags = nflag ? NI_NUMERICHOST : 0; 112378064Sume char namebuf[NI_MAXHOST]; 112478064Sume 112578064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 112678064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 112778064Sume /*NOTREACHED*/ 112878064Sume } 112978064Sume buf = malloc(l); 113078064Sume if (!buf) { 1131121156Sume err(1, "malloc"); 113278064Sume /*NOTREACHED*/ 113378064Sume } 113478064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 113578064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 113678064Sume /*NOTREACHED*/ 113778064Sume } 113878064Sume 113978064Sume ep = (struct in6_prefix *)(buf + l); 114078064Sume for (p = (struct in6_prefix *)buf; p < ep; p = n) { 114178064Sume advrtr = (struct sockaddr_in6 *)(p + 1); 114278064Sume n = (struct in6_prefix *)&advrtr[p->advrtrs]; 114378064Sume 114478064Sume if (getnameinfo((struct sockaddr *)&p->prefix, 114578064Sume p->prefix.sin6_len, namebuf, sizeof(namebuf), 114678064Sume NULL, 0, niflags) != 0) 114778064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 114878064Sume printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1149121156Sume if_indextoname(p->if_index, ifix_buf)); 115078064Sume 1151253999Shrs gettimeofday(&now, 0); 115278064Sume /* 115378064Sume * meaning of fields, especially flags, is very different 115478064Sume * by origin. notify the difference to the users. 115578064Sume */ 115678064Sume printf("flags=%s%s%s%s%s", 1157121156Sume p->raflags.onlink ? "L" : "", 1158121156Sume p->raflags.autonomous ? "A" : "", 1159121156Sume (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1160121156Sume (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 116178064Sume#ifdef NDPRF_HOME 1162121156Sume (p->flags & NDPRF_HOME) != 0 ? "H" : "" 116378064Sume#else 1164121156Sume "" 116578064Sume#endif 1166121156Sume ); 116778064Sume if (p->vltime == ND6_INFINITE_LIFETIME) 116878064Sume printf(" vltime=infinity"); 116978064Sume else 1170122615Sume printf(" vltime=%lu", (unsigned long)p->vltime); 117178064Sume if (p->pltime == ND6_INFINITE_LIFETIME) 117278064Sume printf(", pltime=infinity"); 117378064Sume else 1174122615Sume printf(", pltime=%lu", (unsigned long)p->pltime); 117578064Sume if (p->expire == 0) 117678064Sume printf(", expire=Never"); 1177253970Shrs else if (p->expire >= now.tv_sec) 117878064Sume printf(", expire=%s", 1179253970Shrs sec2str(p->expire - now.tv_sec)); 118078064Sume else 118178064Sume printf(", expired"); 118278064Sume printf(", ref=%d", p->refcnt); 118378064Sume printf("\n"); 118478064Sume /* 118578064Sume * "advertising router" list is meaningful only if the prefix 118678064Sume * information is from RA. 118778064Sume */ 118878064Sume if (p->advrtrs) { 118978064Sume int j; 119078064Sume struct sockaddr_in6 *sin6; 119178064Sume 1192122615Sume sin6 = advrtr; 119378064Sume printf(" advertised by\n"); 119478064Sume for (j = 0; j < p->advrtrs; j++) { 119578064Sume struct in6_nbrinfo *nbi; 119678064Sume 119778064Sume if (getnameinfo((struct sockaddr *)sin6, 119878064Sume sin6->sin6_len, namebuf, sizeof(namebuf), 119978064Sume NULL, 0, ninflags) != 0) 120078064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 120178064Sume printf(" %s", namebuf); 120278064Sume 1203121156Sume nbi = getnbrinfo(&sin6->sin6_addr, 1204121156Sume p->if_index, 0); 120578064Sume if (nbi) { 1206121156Sume switch (nbi->state) { 120778064Sume case ND6_LLINFO_REACHABLE: 120878064Sume case ND6_LLINFO_STALE: 120978064Sume case ND6_LLINFO_DELAY: 121078064Sume case ND6_LLINFO_PROBE: 121178064Sume printf(" (reachable)\n"); 121278064Sume break; 121378064Sume default: 121478064Sume printf(" (unreachable)\n"); 121578064Sume } 121678064Sume } else 121778064Sume printf(" (no neighbor state)\n"); 121878064Sume sin6++; 121978064Sume } 122078064Sume } else 122178064Sume printf(" No advertising router\n"); 122278064Sume } 122378064Sume free(buf); 122455505Sshin} 122555505Sshin 1226287097Shrsstatic void 122755505Sshinpfx_flush() 122855505Sshin{ 122955505Sshin char dummyif[IFNAMSIZ+8]; 123055505Sshin int s; 123155505Sshin 123255505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 123355505Sshin err(1, "socket"); 1234121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 123555505Sshin if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1236121156Sume err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 123755505Sshin} 123855505Sshin 1239287097Shrsstatic void 124055505Sshinrtr_flush() 124155505Sshin{ 124255505Sshin char dummyif[IFNAMSIZ+8]; 124355505Sshin int s; 124455505Sshin 124555505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 124655505Sshin err(1, "socket"); 1247121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 124855505Sshin if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1249121156Sume err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 125062590Sitojun 125162590Sitojun close(s); 125255505Sshin} 125355505Sshin 1254287097Shrsstatic void 125555505Sshinharmonize_rtr() 125655505Sshin{ 125755505Sshin char dummyif[IFNAMSIZ+8]; 125855505Sshin int s; 125955505Sshin 126062590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 126162590Sitojun err(1, "socket"); 1262121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 126362590Sitojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1264121156Sume err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 126562590Sitojun 126662590Sitojun close(s); 126755505Sshin} 126855505Sshin 126962590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 127062590Sitojunstatic void 1271259169Saesetdefif(char *ifname) 127262590Sitojun{ 127362590Sitojun struct in6_ndifreq ndifreq; 127462590Sitojun unsigned int ifindex; 127562590Sitojun 127662590Sitojun if (strcasecmp(ifname, "delete") == 0) 127762590Sitojun ifindex = 0; 127862590Sitojun else { 127962590Sitojun if ((ifindex = if_nametoindex(ifname)) == 0) 128062590Sitojun err(1, "failed to resolve i/f index for %s", ifname); 128162590Sitojun } 128262590Sitojun 128362590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 128462590Sitojun err(1, "socket"); 128562590Sitojun 1286121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 128762590Sitojun ndifreq.ifindex = ifindex; 128862590Sitojun 128962590Sitojun if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1290121156Sume err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 129162590Sitojun 129262590Sitojun close(s); 129362590Sitojun} 129462590Sitojun 129562590Sitojunstatic void 129662590Sitojungetdefif() 129762590Sitojun{ 129862590Sitojun struct in6_ndifreq ndifreq; 129962590Sitojun char ifname[IFNAMSIZ+8]; 130062590Sitojun 130162590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 130262590Sitojun err(1, "socket"); 130362590Sitojun 130462590Sitojun memset(&ndifreq, 0, sizeof(ndifreq)); 1305121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 130662590Sitojun 130762590Sitojun if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1308121156Sume err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 130962590Sitojun 131062590Sitojun if (ndifreq.ifindex == 0) 131162590Sitojun printf("No default interface.\n"); 131262590Sitojun else { 131362590Sitojun if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 131462590Sitojun err(1, "failed to resolve ifname for index %lu", 131562590Sitojun ndifreq.ifindex); 131662590Sitojun printf("ND default interface = %s\n", ifname); 131762590Sitojun } 131862590Sitojun 131962590Sitojun close(s); 132062590Sitojun} 132162590Sitojun#endif 132262590Sitojun 132355505Sshinstatic char * 1324259169Saesec2str(time_t total) 132555505Sshin{ 132655505Sshin static char result[256]; 132755505Sshin int days, hours, mins, secs; 132855505Sshin int first = 1; 132955505Sshin char *p = result; 1330121156Sume char *ep = &result[sizeof(result)]; 1331121156Sume int n; 133255505Sshin 133355505Sshin days = total / 3600 / 24; 133455505Sshin hours = (total / 3600) % 24; 133555505Sshin mins = (total / 60) % 60; 133655505Sshin secs = total % 60; 133755505Sshin 133855505Sshin if (days) { 133955505Sshin first = 0; 1340121156Sume n = snprintf(p, ep - p, "%dd", days); 1341121156Sume if (n < 0 || n >= ep - p) 1342121156Sume return "?"; 1343121156Sume p += n; 134455505Sshin } 134555505Sshin if (!first || hours) { 134655505Sshin first = 0; 1347121156Sume n = snprintf(p, ep - p, "%dh", hours); 1348121156Sume if (n < 0 || n >= ep - p) 1349121156Sume return "?"; 1350121156Sume p += n; 135155505Sshin } 135255505Sshin if (!first || mins) { 135355505Sshin first = 0; 1354121156Sume n = snprintf(p, ep - p, "%dm", mins); 1355121156Sume if (n < 0 || n >= ep - p) 1356121156Sume return "?"; 1357121156Sume p += n; 135855505Sshin } 1359121156Sume snprintf(p, ep - p, "%ds", secs); 136055505Sshin 136155505Sshin return(result); 136255505Sshin} 136355505Sshin 136455505Sshin/* 136555505Sshin * Print the timestamp 136655505Sshin * from tcpdump/util.c 136755505Sshin */ 136855505Sshinstatic void 1369259169Saets_print(const struct timeval *tvp) 137055505Sshin{ 137155505Sshin int s; 137255505Sshin 137355505Sshin /* Default */ 1374253999Shrs s = (tvp->tv_sec + thiszone) % 86400; 137555505Sshin (void)printf("%02d:%02d:%02d.%06u ", 1376253999Shrs s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 137755505Sshin} 1378186119Sqingli 1379186119Sqingli#undef NEXTADDR 1380