ndp.c revision 151472
162590Sitojun/* $FreeBSD: head/usr.sbin/ndp/ndp.c 151472 2005-10-19 15:52:00Z suz $ */ 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> 8255505Sshin#include <sys/time.h> 8378064Sume#include <sys/queue.h> 8455505Sshin 8555505Sshin#include <net/if.h> 8655505Sshin#include <net/if_var.h> 8755505Sshin#include <net/if_dl.h> 8855505Sshin#include <net/if_types.h> 8955505Sshin#include <net/route.h> 9055505Sshin 9155505Sshin#include <netinet/in.h> 9255505Sshin#include <netinet/if_ether.h> 9355505Sshin 9455505Sshin#include <netinet/icmp6.h> 9555505Sshin#include <netinet6/in6_var.h> 9655505Sshin#include <netinet6/nd6.h> 9755505Sshin 9855505Sshin#include <arpa/inet.h> 9955505Sshin 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> 110122615Sume#include <err.h> 11155505Sshin#include "gmt2local.h" 11255505Sshin 11355505Sshin/* packing rule for routing socket */ 11462590Sitojun#define ROUNDUP(a) \ 11555505Sshin ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 11662590Sitojun#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 11755505Sshin 118100650Sjmallettstatic pid_t pid; 11962590Sitojunstatic int nflag; 12062590Sitojunstatic int tflag; 12162590Sitojunstatic int32_t thiszone; /* time difference with gmt */ 12262590Sitojunstatic int s = -1; 12362590Sitojunstatic int repeat = 0; 12455505Sshin 12562590Sitojunchar ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */ 12662590Sitojunchar host_buf[NI_MAXHOST]; /* getnameinfo() */ 12762590Sitojunchar ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 12855505Sshin 12962590Sitojunint main __P((int, char **)); 13062590Sitojunint file __P((char *)); 13162590Sitojunvoid getsocket __P((void)); 13262590Sitojunint set __P((int, char **)); 13362590Sitojunvoid get __P((char *)); 13462590Sitojunint delete __P((char *)); 135122615Sumevoid dump __P((struct in6_addr *, int)); 136122615Sumestatic struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int)); 13762590Sitojunstatic char *ether_str __P((struct sockaddr_dl *)); 13862590Sitojunint ndp_ether_aton __P((char *, u_char *)); 13962590Sitojunvoid usage __P((void)); 14062590Sitojunint rtmsg __P((int)); 141122615Sumevoid ifinfo __P((char *, int, char **)); 14262590Sitojunvoid rtrlist __P((void)); 14362590Sitojunvoid plist __P((void)); 14462590Sitojunvoid pfx_flush __P((void)); 14562590Sitojunvoid rtr_flush __P((void)); 14662590Sitojunvoid harmonize_rtr __P((void)); 14762590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 14862590Sitojunstatic void getdefif __P((void)); 14962590Sitojunstatic void setdefif __P((char *)); 15062590Sitojun#endif 151122615Sumestatic char *sec2str __P((time_t)); 152122615Sumestatic char *ether_str __P((struct sockaddr_dl *)); 15362590Sitojunstatic void ts_print __P((const struct timeval *)); 15455505Sshin 155122615Sume#ifdef ICMPV6CTL_ND6_DRLIST 15678064Sumestatic char *rtpref_str[] = { 15778064Sume "medium", /* 00 */ 15878064Sume "high", /* 01 */ 15978064Sume "rsv", /* 10 */ 16078064Sume "low" /* 11 */ 16178064Sume}; 162122615Sume#endif 16378064Sume 164122615Sumeint mode = 0; 165122615Sumechar *arg = NULL; 166122615Sume 16755505Sshinint 16855505Sshinmain(argc, argv) 16955505Sshin int argc; 17055505Sshin char **argv; 17155505Sshin{ 17255505Sshin int ch; 17355505Sshin 17455505Sshin pid = getpid(); 17555505Sshin thiszone = gmt2local(0); 176122615Sume while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1) 177121156Sume switch (ch) { 17855505Sshin case 'a': 17955505Sshin case 'c': 180122615Sume case 'p': 181122615Sume case 'r': 182122615Sume case 'H': 183122615Sume case 'P': 184122615Sume case 'R': 185122615Sume case 's': 186122615Sume case 'I': 187122615Sume if (mode) { 188122615Sume usage(); 189122615Sume /*NOTREACHED*/ 190122615Sume } 191122615Sume mode = ch; 192122615Sume arg = NULL; 19355505Sshin break; 19455505Sshin case 'd': 195122615Sume case 'f': 19655505Sshin case 'i' : 197122615Sume if (mode) { 19855505Sshin usage(); 199122615Sume /*NOTREACHED*/ 200122615Sume } 201122615Sume mode = ch; 202122615Sume arg = optarg; 203122615Sume break; 20455505Sshin case 'n': 20555505Sshin nflag = 1; 20655505Sshin break; 20755505Sshin case 't': 20855505Sshin tflag = 1; 20955505Sshin break; 21055505Sshin case 'A': 211122615Sume if (mode) { 212122615Sume usage(); 213122615Sume /*NOTREACHED*/ 214122615Sume } 215122615Sume mode = 'a'; 21655505Sshin repeat = atoi(optarg); 217122615Sume if (repeat < 0) { 21855505Sshin usage(); 219122615Sume /*NOTREACHED*/ 220122615Sume } 22155505Sshin break; 22255505Sshin default: 22355505Sshin usage(); 22455505Sshin } 22555505Sshin 22655505Sshin argc -= optind; 22755505Sshin argv += optind; 22855505Sshin 229122615Sume switch (mode) { 230122615Sume case 'a': 231122615Sume case 'c': 232122615Sume if (argc != 0) { 23355505Sshin usage(); 234122615Sume /*NOTREACHED*/ 235122615Sume } 236122615Sume dump(0, mode == 'c'); 237122615Sume break; 238122615Sume case 'd': 239122615Sume if (argc != 0) { 240122615Sume usage(); 241122615Sume /*NOTREACHED*/ 242122615Sume } 243122615Sume delete(arg); 244122615Sume break; 245122615Sume case 'I': 246122615Sume#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 247122615Sume if (argc > 1) { 248122615Sume usage(); 249122615Sume /*NOTREACHED*/ 250122615Sume } else if (argc == 1) { 251122615Sume if (strcmp(*argv, "delete") == 0 || 252122615Sume if_nametoindex(*argv)) 253122615Sume setdefif(*argv); 254122615Sume else 255122615Sume errx(1, "invalid interface %s", *argv); 256122615Sume } 257122615Sume getdefif(); /* always call it to print the result */ 258122615Sume break; 259122615Sume#else 260122615Sume errx(1, "not supported yet"); 261122615Sume /*NOTREACHED*/ 262122615Sume#endif 263122615Sume case 'p': 264122615Sume if (argc != 0) { 265122615Sume usage(); 266122615Sume /*NOTREACHED*/ 267122615Sume } 26855505Sshin plist(); 269122615Sume break; 270122615Sume case 'i': 271122615Sume ifinfo(arg, argc, argv); 272122615Sume break; 273122615Sume case 'r': 274122615Sume if (argc != 0) { 275122615Sume usage(); 276122615Sume /*NOTREACHED*/ 277122615Sume } 27855505Sshin rtrlist(); 279122615Sume break; 280122615Sume case 's': 28155505Sshin if (argc < 2 || argc > 4) 28255505Sshin usage(); 28355505Sshin exit(set(argc, argv) ? 1 : 0); 284122615Sume case 'H': 285122615Sume if (argc != 0) { 286122615Sume usage(); 287122615Sume /*NOTREACHED*/ 288122615Sume } 28955505Sshin harmonize_rtr(); 290122615Sume break; 291122615Sume case 'P': 292122615Sume if (argc != 0) { 293122615Sume usage(); 294122615Sume /*NOTREACHED*/ 295122615Sume } 29655505Sshin pfx_flush(); 297122615Sume break; 298122615Sume case 'R': 299122615Sume if (argc != 0) { 300122615Sume usage(); 301122615Sume /*NOTREACHED*/ 302122615Sume } 30355505Sshin rtr_flush(); 304122615Sume break; 305122615Sume case 0: 306122615Sume if (argc != 1) { 307122615Sume usage(); 308122615Sume /*NOTREACHED*/ 309122615Sume } 310122615Sume get(argv[0]); 311122615Sume break; 31255505Sshin } 31355505Sshin exit(0); 31455505Sshin} 31555505Sshin 31655505Sshin/* 31755505Sshin * Process a file to set standard ndp entries 31855505Sshin */ 31955505Sshinint 32055505Sshinfile(name) 32155505Sshin char *name; 32255505Sshin{ 32355505Sshin FILE *fp; 32455505Sshin int i, retval; 32555505Sshin char line[100], arg[5][50], *args[5]; 32655505Sshin 32755505Sshin if ((fp = fopen(name, "r")) == NULL) { 32855505Sshin fprintf(stderr, "ndp: cannot open %s\n", name); 32955505Sshin exit(1); 33055505Sshin } 33155505Sshin args[0] = &arg[0][0]; 33255505Sshin args[1] = &arg[1][0]; 33355505Sshin args[2] = &arg[2][0]; 33455505Sshin args[3] = &arg[3][0]; 33555505Sshin args[4] = &arg[4][0]; 33655505Sshin retval = 0; 337121156Sume while (fgets(line, 100, fp) != NULL) { 338122615Sume i = sscanf(line, "%49s %49s %49s %49s %49s", 339122615Sume arg[0], arg[1], arg[2], arg[3], arg[4]); 34055505Sshin if (i < 2) { 34155505Sshin fprintf(stderr, "ndp: bad line: %s\n", line); 34255505Sshin retval = 1; 34355505Sshin continue; 34455505Sshin } 34555505Sshin if (set(i, args)) 34655505Sshin retval = 1; 34755505Sshin } 34855505Sshin fclose(fp); 34955505Sshin return (retval); 35055505Sshin} 35155505Sshin 35255505Sshinvoid 35355505Sshingetsocket() 35455505Sshin{ 35555505Sshin if (s < 0) { 35655505Sshin s = socket(PF_ROUTE, SOCK_RAW, 0); 35755505Sshin if (s < 0) { 358121156Sume err(1, "socket"); 359121156Sume /* NOTREACHED */ 36055505Sshin } 36155505Sshin } 36255505Sshin} 36355505Sshin 36462590Sitojunstruct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 }; 36555505Sshinstruct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m; 36655505Sshinstruct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 36755505Sshinint expire_time, flags, found_entry; 36855505Sshinstruct { 36955505Sshin struct rt_msghdr m_rtm; 37055505Sshin char m_space[512]; 37155505Sshin} m_rtmsg; 37255505Sshin 37355505Sshin/* 37455505Sshin * Set an individual neighbor cache entry 37555505Sshin */ 37655505Sshinint 37755505Sshinset(argc, argv) 37855505Sshin int argc; 37955505Sshin char **argv; 38055505Sshin{ 38155505Sshin register struct sockaddr_in6 *sin = &sin_m; 38255505Sshin register struct sockaddr_dl *sdl; 38355505Sshin register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 38455505Sshin struct addrinfo hints, *res; 38555505Sshin int gai_error; 38655505Sshin u_char *ea; 38755505Sshin char *host = argv[0], *eaddr = argv[1]; 38855505Sshin 38955505Sshin getsocket(); 39055505Sshin argc -= 2; 39155505Sshin argv += 2; 39255505Sshin sdl_m = blank_sdl; 39355505Sshin sin_m = blank_sin; 39455505Sshin 39555505Sshin bzero(&hints, sizeof(hints)); 39655505Sshin hints.ai_family = AF_INET6; 39755505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 39855505Sshin if (gai_error) { 39955505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 40055505Sshin gai_strerror(gai_error)); 40155505Sshin return 1; 40255505Sshin } 40355505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 40462590Sitojun#ifdef __KAME__ 40562590Sitojun if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 40662590Sitojun *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 407121156Sume htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 40862590Sitojun } 40962590Sitojun#endif 41055505Sshin ea = (u_char *)LLADDR(&sdl_m); 41155505Sshin if (ndp_ether_aton(eaddr, ea) == 0) 41255505Sshin sdl_m.sdl_alen = 6; 41355505Sshin flags = expire_time = 0; 41455505Sshin while (argc-- > 0) { 41555505Sshin if (strncmp(argv[0], "temp", 4) == 0) { 41655505Sshin struct timeval time; 417121156Sume 41855505Sshin gettimeofday(&time, 0); 41955505Sshin expire_time = time.tv_sec + 20 * 60; 42062590Sitojun } else if (strncmp(argv[0], "proxy", 5) == 0) 42162590Sitojun flags |= RTF_ANNOUNCE; 42255505Sshin argv++; 42355505Sshin } 42455505Sshin if (rtmsg(RTM_GET) < 0) { 425121156Sume errx(1, "RTM_GET(%s) failed", host); 426121156Sume /* NOTREACHED */ 42755505Sshin } 42855505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 42955505Sshin sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 43055505Sshin if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 43155505Sshin if (sdl->sdl_family == AF_LINK && 43255505Sshin (rtm->rtm_flags & RTF_LLINFO) && 433122615Sume !(rtm->rtm_flags & RTF_GATEWAY)) { 434122615Sume switch (sdl->sdl_type) { 435122615Sume case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 436122615Sume case IFT_ISO88024: case IFT_ISO88025: 437122615Sume goto overwrite; 438122615Sume } 43955505Sshin } 44062590Sitojun /* 44162590Sitojun * IPv4 arp command retries with sin_other = SIN_PROXY here. 44262590Sitojun */ 44362590Sitojun fprintf(stderr, "set: cannot configure a new entry\n"); 44462590Sitojun return 1; 44555505Sshin } 44662590Sitojun 44755505Sshinoverwrite: 44855505Sshin if (sdl->sdl_family != AF_LINK) { 44955505Sshin printf("cannot intuit interface index and type for %s\n", host); 45055505Sshin return (1); 45155505Sshin } 45255505Sshin sdl_m.sdl_type = sdl->sdl_type; 45355505Sshin sdl_m.sdl_index = sdl->sdl_index; 45455505Sshin return (rtmsg(RTM_ADD)); 45555505Sshin} 45655505Sshin 45755505Sshin/* 45855505Sshin * Display an individual neighbor cache entry 45955505Sshin */ 46055505Sshinvoid 46155505Sshinget(host) 46255505Sshin char *host; 46355505Sshin{ 46455505Sshin struct sockaddr_in6 *sin = &sin_m; 46555505Sshin struct addrinfo hints, *res; 46655505Sshin int gai_error; 46755505Sshin 46855505Sshin sin_m = blank_sin; 46955505Sshin bzero(&hints, sizeof(hints)); 47055505Sshin hints.ai_family = AF_INET6; 47155505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 47255505Sshin if (gai_error) { 47355505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 474121156Sume gai_strerror(gai_error)); 47555505Sshin return; 47655505Sshin } 47755505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 47862590Sitojun#ifdef __KAME__ 47962590Sitojun if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 48062590Sitojun *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 481121156Sume htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 48262590Sitojun } 48362590Sitojun#endif 484122615Sume dump(&sin->sin6_addr, 0); 48555505Sshin if (found_entry == 0) { 48655505Sshin getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 487121156Sume sizeof(host_buf), NULL ,0, 488121156Sume (nflag ? NI_NUMERICHOST : 0)); 48955505Sshin printf("%s (%s) -- no entry\n", host, host_buf); 49055505Sshin exit(1); 49155505Sshin } 49255505Sshin} 49355505Sshin 49455505Sshin/* 49555505Sshin * Delete a neighbor cache entry 49655505Sshin */ 49755505Sshinint 49855505Sshindelete(host) 49955505Sshin char *host; 50055505Sshin{ 50155505Sshin struct sockaddr_in6 *sin = &sin_m; 50255505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 50355505Sshin struct sockaddr_dl *sdl; 50455505Sshin struct addrinfo hints, *res; 50555505Sshin int gai_error; 50655505Sshin 50755505Sshin getsocket(); 50855505Sshin sin_m = blank_sin; 50955505Sshin 51055505Sshin bzero(&hints, sizeof(hints)); 51155505Sshin hints.ai_family = AF_INET6; 51255505Sshin gai_error = getaddrinfo(host, NULL, &hints, &res); 51355505Sshin if (gai_error) { 51455505Sshin fprintf(stderr, "ndp: %s: %s\n", host, 515121156Sume gai_strerror(gai_error)); 51655505Sshin return 1; 51755505Sshin } 51855505Sshin sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 51962590Sitojun#ifdef __KAME__ 52062590Sitojun if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 52162590Sitojun *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 522121156Sume htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 52362590Sitojun } 52462590Sitojun#endif 52555505Sshin if (rtmsg(RTM_GET) < 0) { 526121156Sume errx(1, "RTM_GET(%s) failed", host); 527121156Sume /* NOTREACHED */ 52855505Sshin } 52955505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 53055505Sshin sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 53155505Sshin if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 53255505Sshin if (sdl->sdl_family == AF_LINK && 53355505Sshin (rtm->rtm_flags & RTF_LLINFO) && 53462590Sitojun !(rtm->rtm_flags & RTF_GATEWAY)) { 53578064Sume goto delete; 53655505Sshin } 53762590Sitojun /* 53862590Sitojun * IPv4 arp command retries with sin_other = SIN_PROXY here. 53962590Sitojun */ 54062590Sitojun fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 54162590Sitojun return 1; 54255505Sshin } 54362590Sitojun 54455505Sshindelete: 54555505Sshin if (sdl->sdl_family != AF_LINK) { 54655505Sshin printf("cannot locate %s\n", host); 54755505Sshin return (1); 54855505Sshin } 54955505Sshin if (rtmsg(RTM_DELETE) == 0) { 55062590Sitojun struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 55162590Sitojun 55262590Sitojun#ifdef __KAME__ 55362590Sitojun if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 55462590Sitojun s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 55562590Sitojun *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 55662590Sitojun } 55762590Sitojun#endif 55862590Sitojun getnameinfo((struct sockaddr *)&s6, 559121156Sume s6.sin6_len, host_buf, 560121156Sume sizeof(host_buf), NULL, 0, 561121156Sume (nflag ? NI_NUMERICHOST : 0)); 56255505Sshin printf("%s (%s) deleted\n", host, host_buf); 56355505Sshin } 56455505Sshin 56555505Sshin return 0; 56655505Sshin} 56755505Sshin 568122615Sume#define W_ADDR 36 56978064Sume#define W_LL 17 57078064Sume#define W_IF 6 57178064Sume 57255505Sshin/* 57355505Sshin * Dump the entire neighbor cache 57455505Sshin */ 57555505Sshinvoid 576122615Sumedump(addr, cflag) 57755505Sshin struct in6_addr *addr; 578122615Sume int cflag; 57955505Sshin{ 58055505Sshin int mib[6]; 58155505Sshin size_t needed; 58262590Sitojun char *lim, *buf, *next; 58355505Sshin struct rt_msghdr *rtm; 58455505Sshin struct sockaddr_in6 *sin; 58555505Sshin struct sockaddr_dl *sdl; 58655505Sshin extern int h_errno; 58755505Sshin struct in6_nbrinfo *nbi; 58855505Sshin struct timeval time; 58955505Sshin int addrwidth; 59078064Sume int llwidth; 59178064Sume int ifwidth; 59262590Sitojun char flgbuf[8]; 59378064Sume char *ifname; 59455505Sshin 59555505Sshin /* Print header */ 59666865Ssumikawa if (!tflag && !cflag) 597122615Sume printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 59878064Sume W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 599122615Sume W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 60055505Sshin 60155505Sshinagain:; 60255505Sshin mib[0] = CTL_NET; 60355505Sshin mib[1] = PF_ROUTE; 60455505Sshin mib[2] = 0; 60555505Sshin mib[3] = AF_INET6; 60655505Sshin mib[4] = NET_RT_FLAGS; 60755505Sshin mib[5] = RTF_LLINFO; 60855505Sshin if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 60955505Sshin err(1, "sysctl(PF_ROUTE estimate)"); 61055505Sshin if (needed > 0) { 61155505Sshin if ((buf = malloc(needed)) == NULL) 612121156Sume err(1, "malloc"); 61355505Sshin if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 61455505Sshin err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 61555505Sshin lim = buf + needed; 61655505Sshin } else 61755505Sshin buf = lim = NULL; 61855505Sshin 61955505Sshin for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 62055505Sshin int isrouter = 0, prbs = 0; 62155505Sshin 62255505Sshin rtm = (struct rt_msghdr *)next; 62355505Sshin sin = (struct sockaddr_in6 *)(rtm + 1); 62455505Sshin sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 62578064Sume 62678064Sume /* 62778064Sume * Some OSes can produce a route that has the LINK flag but 62878064Sume * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 62978064Sume * and BSD/OS, where xx is not the interface identifier on 63078064Sume * lo0). Such routes entry would annoy getnbrinfo() below, 63178064Sume * so we skip them. 63278064Sume * XXX: such routes should have the GATEWAY flag, not the 633121156Sume * LINK flag. However, there is rotten routing software 63478064Sume * that advertises all routes that have the GATEWAY flag. 63578064Sume * Thus, KAME kernel intentionally does not set the LINK flag. 63678064Sume * What is to be fixed is not ndp, but such routing software 63778064Sume * (and the kernel workaround)... 63878064Sume */ 63978064Sume if (sdl->sdl_family != AF_LINK) 64078064Sume continue; 64178064Sume 642122615Sume if (!(rtm->rtm_flags & RTF_HOST)) 643122615Sume continue; 644122615Sume 64555505Sshin if (addr) { 64655505Sshin if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 64755505Sshin continue; 64855505Sshin found_entry = 1; 64955505Sshin } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 65055505Sshin continue; 65155505Sshin if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 65255505Sshin IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 65355505Sshin /* XXX: should scope id be filled in the kernel? */ 65455505Sshin if (sin->sin6_scope_id == 0) 65555505Sshin sin->sin6_scope_id = sdl->sdl_index; 65666865Ssumikawa#ifdef __KAME__ 65766865Ssumikawa /* KAME specific hack; removed the embedded id */ 65855505Sshin *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 65966865Ssumikawa#endif 66055505Sshin } 66155505Sshin getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 662121156Sume sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 663122615Sume if (cflag) { 66481366Ssumikawa#ifdef RTF_WASCLONED 66581366Ssumikawa if (rtm->rtm_flags & RTF_WASCLONED) 66681366Ssumikawa delete(host_buf); 667122615Sume#elif defined(RTF_CLONED) 668122615Sume if (rtm->rtm_flags & RTF_CLONED) 669122615Sume delete(host_buf); 67081366Ssumikawa#else 67166865Ssumikawa delete(host_buf); 67281366Ssumikawa#endif 67366865Ssumikawa continue; 67466865Ssumikawa } 67555505Sshin gettimeofday(&time, 0); 67655505Sshin if (tflag) 67755505Sshin ts_print(&time); 67855505Sshin 67978064Sume addrwidth = strlen(host_buf); 68078064Sume if (addrwidth < W_ADDR) 68178064Sume addrwidth = W_ADDR; 68278064Sume llwidth = strlen(ether_str(sdl)); 68378064Sume if (W_ADDR + W_LL - addrwidth > llwidth) 68478064Sume llwidth = W_ADDR + W_LL - addrwidth; 68578064Sume ifname = if_indextoname(sdl->sdl_index, ifix_buf); 68678064Sume if (!ifname) 68778064Sume ifname = "?"; 68878064Sume ifwidth = strlen(ifname); 68978064Sume if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 69078064Sume ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 69155505Sshin 69278064Sume printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 69378064Sume llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 69455505Sshin 69555505Sshin /* Print neighbor discovery specific informations */ 69662590Sitojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 69755505Sshin if (nbi) { 69855505Sshin if (nbi->expire > time.tv_sec) { 69955505Sshin printf(" %-9.9s", 700121156Sume sec2str(nbi->expire - time.tv_sec)); 70178064Sume } else if (nbi->expire == 0) 70255505Sshin printf(" %-9.9s", "permanent"); 70355505Sshin else 70455505Sshin printf(" %-9.9s", "expired"); 70555505Sshin 706121156Sume switch (nbi->state) { 707121156Sume case ND6_LLINFO_NOSTATE: 70855505Sshin printf(" N"); 70955505Sshin break; 71078064Sume#ifdef ND6_LLINFO_WAITDELETE 711121156Sume case ND6_LLINFO_WAITDELETE: 71255505Sshin printf(" W"); 71355505Sshin break; 71478064Sume#endif 715121156Sume case ND6_LLINFO_INCOMPLETE: 71655505Sshin printf(" I"); 71755505Sshin break; 718121156Sume case ND6_LLINFO_REACHABLE: 71955505Sshin printf(" R"); 72055505Sshin break; 721121156Sume case ND6_LLINFO_STALE: 72255505Sshin printf(" S"); 72355505Sshin break; 724121156Sume case ND6_LLINFO_DELAY: 72555505Sshin printf(" D"); 72655505Sshin break; 727121156Sume case ND6_LLINFO_PROBE: 72855505Sshin printf(" P"); 72955505Sshin break; 730121156Sume default: 73155505Sshin printf(" ?"); 73255505Sshin break; 73355505Sshin } 73455505Sshin 73555505Sshin isrouter = nbi->isrouter; 73655505Sshin prbs = nbi->asked; 73778064Sume } else { 73855505Sshin warnx("failed to get neighbor information"); 73955505Sshin printf(" "); 74055505Sshin } 74155505Sshin 74262590Sitojun /* 74362590Sitojun * other flags. R: router, P: proxy, W: ?? 74462590Sitojun */ 74562590Sitojun if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 74662590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s", 747121156Sume isrouter ? "R" : "", 748121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 74962590Sitojun } else { 75062590Sitojun sin = (struct sockaddr_in6 *) 751121156Sume (sdl->sdl_len + (char *)sdl); 752122615Sume#if 0 /* W and P are mystery even for us */ 75362590Sitojun snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 754121156Sume isrouter ? "R" : "", 755121156Sume !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 756121156Sume (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 757121156Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 758122615Sume#else 759122615Sume snprintf(flgbuf, sizeof(flgbuf), "%s%s", 760122615Sume isrouter ? "R" : "", 761122615Sume (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 762122615Sume#endif 76355505Sshin } 764122615Sume printf(" %s", flgbuf); 76555505Sshin 76655505Sshin if (prbs) 767122615Sume printf(" %d", prbs); 76855505Sshin 76955505Sshin printf("\n"); 77055505Sshin } 77178064Sume if (buf != NULL) 77278064Sume free(buf); 77355505Sshin 77455505Sshin if (repeat) { 77555505Sshin printf("\n"); 776125675Ssumikawa fflush(stdout); 77755505Sshin sleep(repeat); 77855505Sshin goto again; 77955505Sshin } 78055505Sshin} 78155505Sshin 78255505Sshinstatic struct in6_nbrinfo * 78362590Sitojungetnbrinfo(addr, ifindex, warning) 78455505Sshin struct in6_addr *addr; 78555505Sshin int ifindex; 78662590Sitojun int warning; 78755505Sshin{ 78855505Sshin static struct in6_nbrinfo nbi; 78955505Sshin int s; 79055505Sshin 79155505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 79255505Sshin err(1, "socket"); 79355505Sshin 79455505Sshin bzero(&nbi, sizeof(nbi)); 79555505Sshin if_indextoname(ifindex, nbi.ifname); 79655505Sshin nbi.addr = *addr; 79755505Sshin if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 79862590Sitojun if (warning) 79962590Sitojun warn("ioctl(SIOCGNBRINFO_IN6)"); 80055505Sshin close(s); 80155505Sshin return(NULL); 80255505Sshin } 80355505Sshin 80455505Sshin close(s); 80555505Sshin return(&nbi); 80655505Sshin} 80755505Sshin 80855505Sshinstatic char * 80955505Sshinether_str(sdl) 81055505Sshin struct sockaddr_dl *sdl; 81155505Sshin{ 812121156Sume static char hbuf[NI_MAXHOST]; 81355505Sshin u_char *cp; 81455505Sshin 81555505Sshin if (sdl->sdl_alen) { 81655505Sshin cp = (u_char *)LLADDR(sdl); 817121156Sume snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x", 818121156Sume cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 819121156Sume } else 820121156Sume snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 82155505Sshin 822121156Sume return(hbuf); 82355505Sshin} 82455505Sshin 82555505Sshinint 82655505Sshinndp_ether_aton(a, n) 82755505Sshin char *a; 82855505Sshin u_char *n; 82955505Sshin{ 83055505Sshin int i, o[6]; 83155505Sshin 83255505Sshin i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 833121156Sume &o[3], &o[4], &o[5]); 83455505Sshin if (i != 6) { 83555505Sshin fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 83655505Sshin return (1); 83755505Sshin } 838121156Sume for (i = 0; i < 6; i++) 83955505Sshin n[i] = o[i]; 84055505Sshin return (0); 84155505Sshin} 84255505Sshin 84355505Sshinvoid 84455505Sshinusage() 84555505Sshin{ 846122615Sume printf("usage: ndp [-nt] hostname\n"); 847122615Sume printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 84878064Sume printf(" ndp [-nt] -A wait\n"); 849122615Sume printf(" ndp [-nt] -d hostname\n"); 850122615Sume printf(" ndp [-nt] -f filename\n"); 851122615Sume printf(" ndp [-nt] -i interface [flags...]\n"); 85262590Sitojun#ifdef SIOCSDEFIFACE_IN6 853122615Sume printf(" ndp [-nt] -I [interface|delete]\n"); 85462590Sitojun#endif 855122615Sume printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 85655505Sshin exit(1); 85755505Sshin} 85855505Sshin 85955505Sshinint 86055505Sshinrtmsg(cmd) 86155505Sshin int cmd; 86255505Sshin{ 86355505Sshin static int seq; 86455505Sshin int rlen; 86555505Sshin register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 86655505Sshin register char *cp = m_rtmsg.m_space; 86755505Sshin register int l; 86855505Sshin 86955505Sshin errno = 0; 87055505Sshin if (cmd == RTM_DELETE) 87155505Sshin goto doit; 87255505Sshin bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 87355505Sshin rtm->rtm_flags = flags; 87455505Sshin rtm->rtm_version = RTM_VERSION; 87555505Sshin 87655505Sshin switch (cmd) { 87755505Sshin default: 87855505Sshin fprintf(stderr, "ndp: internal wrong cmd\n"); 87955505Sshin exit(1); 88055505Sshin case RTM_ADD: 88155505Sshin rtm->rtm_addrs |= RTA_GATEWAY; 882122615Sume if (expire_time) { 883122615Sume rtm->rtm_rmx.rmx_expire = expire_time; 884122615Sume rtm->rtm_inits = RTV_EXPIRE; 885122615Sume } 88655505Sshin rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 88762590Sitojun if (rtm->rtm_flags & RTF_ANNOUNCE) { 88862590Sitojun rtm->rtm_flags &= ~RTF_HOST; 889124241Ssuz rtm->rtm_addrs |= RTA_NETMASK; 89062590Sitojun } 89155505Sshin /* FALLTHROUGH */ 89255505Sshin case RTM_GET: 89355505Sshin rtm->rtm_addrs |= RTA_DST; 89455505Sshin } 89562590Sitojun#define NEXTADDR(w, s) \ 89655505Sshin if (rtm->rtm_addrs & (w)) { \ 89755505Sshin bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);} 89855505Sshin 89955505Sshin NEXTADDR(RTA_DST, sin_m); 90055505Sshin NEXTADDR(RTA_GATEWAY, sdl_m); 90162590Sitojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 90255505Sshin NEXTADDR(RTA_NETMASK, so_mask); 90355505Sshin 90455505Sshin rtm->rtm_msglen = cp - (char *)&m_rtmsg; 90555505Sshindoit: 90655505Sshin l = rtm->rtm_msglen; 90755505Sshin rtm->rtm_seq = ++seq; 90855505Sshin rtm->rtm_type = cmd; 90955505Sshin if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 91055505Sshin if (errno != ESRCH || cmd != RTM_DELETE) { 911121156Sume err(1, "writing to routing socket"); 912121156Sume /* NOTREACHED */ 91355505Sshin } 91455505Sshin } 91555505Sshin do { 91655505Sshin l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 91755505Sshin } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 91855505Sshin if (l < 0) 91955505Sshin (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 92055505Sshin strerror(errno)); 92155505Sshin return (0); 92255505Sshin} 92355505Sshin 92455505Sshinvoid 925122615Sumeifinfo(ifname, argc, argv) 926122615Sume char *ifname; 92762590Sitojun int argc; 92862590Sitojun char **argv; 92955505Sshin{ 93055505Sshin struct in6_ndireq nd; 93162590Sitojun int i, s; 93262590Sitojun u_int32_t newflags; 93378064Sume#ifdef IPV6CTL_USETEMPADDR 93478064Sume u_int8_t nullbuf[8]; 93578064Sume#endif 93655505Sshin 93755505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 938121156Sume err(1, "socket"); 939121156Sume /* NOTREACHED */ 94055505Sshin } 94155505Sshin bzero(&nd, sizeof(nd)); 942121156Sume strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 94355505Sshin if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 944121156Sume err(1, "ioctl(SIOCGIFINFO_IN6)"); 945121156Sume /* NOTREACHED */ 946122615Sume } 94762590Sitojun#define ND nd.ndi 94862590Sitojun newflags = ND.flags; 949122615Sume for (i = 0; i < argc; i++) { 95062590Sitojun int clear = 0; 95162590Sitojun char *cp = argv[i]; 95262590Sitojun 95362590Sitojun if (*cp == '-') { 95462590Sitojun clear = 1; 95562590Sitojun cp++; 95662590Sitojun } 95762590Sitojun 95862590Sitojun#define SETFLAG(s, f) \ 95962590Sitojun do {\ 96062590Sitojun if (strcmp(cp, (s)) == 0) {\ 96162590Sitojun if (clear)\ 96262590Sitojun newflags &= ~(f);\ 96362590Sitojun else\ 96462590Sitojun newflags |= (f);\ 96562590Sitojun }\ 96662590Sitojun } while (0) 967151468Ssuz/* 968151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against 969151468Ssuz * "nudbogus". But we just let it go since this is minor. 970151468Ssuz */ 971151468Ssuz#define SETVALUE(f, v) \ 972151468Ssuz do { \ 973151468Ssuz char *valptr; \ 974151468Ssuz unsigned long newval; \ 975151468Ssuz v = 0; /* unspecified */ \ 976151468Ssuz if (strncmp(cp, f, strlen(f)) == 0) { \ 977151468Ssuz valptr = strchr(cp, '='); \ 978151468Ssuz if (valptr == NULL) \ 979151468Ssuz err(1, "syntax error in %s field", (f)); \ 980151468Ssuz errno = 0; \ 981151468Ssuz newval = strtoul(++valptr, NULL, 0); \ 982151468Ssuz if (errno) \ 983151468Ssuz err(1, "syntax error in %s's value", (f)); \ 984151468Ssuz v = newval; \ 985151468Ssuz } \ 986151468Ssuz } while (0) 987151468Ssuz 98862590Sitojun SETFLAG("nud", ND6_IFF_PERFORMNUD); 989118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 990118498Sume SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 991118498Sume#endif 992122615Sume#ifdef ND6_IFF_PREFER_SOURCE 993122615Sume SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE); 994122615Sume#endif 995151468Ssuz SETVALUE("basereachable", ND.basereachable); 996151468Ssuz SETVALUE("retrans", ND.retrans); 997151468Ssuz SETVALUE("curhlim", ND.chlim); 99862590Sitojun 99962590Sitojun ND.flags = newflags; 1000151468Ssuz if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 1001151468Ssuz err(1, "ioctl(SIOCSIFINFO_IN6)"); 1002121156Sume /* NOTREACHED */ 100362590Sitojun } 100462590Sitojun#undef SETFLAG 1005151468Ssuz#undef SETVALUE 100662590Sitojun } 100762590Sitojun 1008121162Sume if (!ND.initialized) { 1009121162Sume errx(1, "%s: not initialized yet", ifname); 1010121162Sume /* NOTREACHED */ 1011121162Sume } 1012121162Sume 1013151468Ssuz if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 1014151468Ssuz err(1, "ioctl(SIOCGIFINFO_IN6)"); 1015151468Ssuz /* NOTREACHED */ 1016151468Ssuz } 101755505Sshin printf("linkmtu=%d", ND.linkmtu); 1018121471Sume printf(", maxmtu=%d", ND.maxmtu); 101955505Sshin printf(", curhlim=%d", ND.chlim); 102055505Sshin printf(", basereachable=%ds%dms", 1021121156Sume ND.basereachable / 1000, ND.basereachable % 1000); 102255505Sshin printf(", reachable=%ds", ND.reachable); 102362590Sitojun printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 102478064Sume#ifdef IPV6CTL_USETEMPADDR 102578064Sume memset(nullbuf, 0, sizeof(nullbuf)); 102678064Sume if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 102778064Sume int j; 102878064Sume u_int8_t *rbuf; 102978064Sume 103078064Sume for (i = 0; i < 3; i++) { 1031121156Sume switch (i) { 103278064Sume case 0: 103378064Sume printf("\nRandom seed(0): "); 103478064Sume rbuf = ND.randomseed0; 103578064Sume break; 103678064Sume case 1: 103778064Sume printf("\nRandom seed(1): "); 103878064Sume rbuf = ND.randomseed1; 103978064Sume break; 104078064Sume case 2: 104178064Sume printf("\nRandom ID: "); 104278064Sume rbuf = ND.randomid; 104378064Sume break; 1044151472Ssuz default: 1045151472Ssuz errx(1, "impossible case for tempaddr display"); 104678064Sume } 104778064Sume for (j = 0; j < 8; j++) 104878064Sume printf("%02x", rbuf[j]); 104978064Sume } 105078064Sume } 105178064Sume#endif 105262590Sitojun if (ND.flags) { 105362590Sitojun printf("\nFlags: "); 1054118498Sume if ((ND.flags & ND6_IFF_PERFORMNUD)) 1055118498Sume printf("nud "); 1056118498Sume#ifdef ND6_IFF_ACCEPT_RTADV 1057118498Sume if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1058118498Sume printf("accept_rtadv "); 1059118498Sume#endif 1060122615Sume#ifdef ND6_IFF_PREFER_SOURCE 1061122615Sume if ((ND.flags & ND6_IFF_PREFER_SOURCE)) 1062122615Sume printf("prefer_source "); 1063122615Sume#endif 1064122615Sume } 106562590Sitojun putc('\n', stdout); 106655505Sshin#undef ND 1067121156Sume 106855505Sshin close(s); 106955505Sshin} 107055505Sshin 107178064Sume#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 107278064Sume#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 107378064Sume#endif 107478064Sume 107555505Sshinvoid 107655505Sshinrtrlist() 107755505Sshin{ 107878064Sume#ifdef ICMPV6CTL_ND6_DRLIST 107978064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 108078064Sume char *buf; 108178064Sume struct in6_defrouter *p, *ep; 108278064Sume size_t l; 108378064Sume struct timeval time; 108478064Sume 108578064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 108678064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 108778064Sume /*NOTREACHED*/ 108878064Sume } 1089151472Ssuz if (l == 0) 1090151472Ssuz return; 109178064Sume buf = malloc(l); 109278064Sume if (!buf) { 1093121156Sume err(1, "malloc"); 109478064Sume /*NOTREACHED*/ 109578064Sume } 109678064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 109778064Sume err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 109878064Sume /*NOTREACHED*/ 109978064Sume } 110078064Sume 110178064Sume ep = (struct in6_defrouter *)(buf + l); 110278064Sume for (p = (struct in6_defrouter *)buf; p < ep; p++) { 110378064Sume int rtpref; 110478064Sume 110578064Sume if (getnameinfo((struct sockaddr *)&p->rtaddr, 110678064Sume p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1107121156Sume (nflag ? NI_NUMERICHOST : 0)) != 0) 110878064Sume strlcpy(host_buf, "?", sizeof(host_buf)); 1109121156Sume 111078064Sume printf("%s if=%s", host_buf, 1111121156Sume if_indextoname(p->if_index, ifix_buf)); 111278064Sume printf(", flags=%s%s", 1113121156Sume p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1114121156Sume p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 111578064Sume rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 111678064Sume printf(", pref=%s", rtpref_str[rtpref]); 1117121156Sume 111878064Sume gettimeofday(&time, 0); 111978064Sume if (p->expire == 0) 112078064Sume printf(", expire=Never\n"); 112178064Sume else 112278064Sume printf(", expire=%s\n", 1123121156Sume sec2str(p->expire - time.tv_sec)); 112478064Sume } 112578064Sume free(buf); 112678064Sume#else 112755505Sshin struct in6_drlist dr; 112855505Sshin int s, i; 112955505Sshin struct timeval time; 113055505Sshin 113155505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1132121156Sume err(1, "socket"); 1133121156Sume /* NOTREACHED */ 113455505Sshin } 113555505Sshin bzero(&dr, sizeof(dr)); 1136121156Sume strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */ 113755505Sshin if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 1138121156Sume err(1, "ioctl(SIOCGDRLST_IN6)"); 1139121156Sume /* NOTREACHED */ 1140121156Sume } 114162590Sitojun#define DR dr.defrouter[i] 114278064Sume for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) { 114355505Sshin struct sockaddr_in6 sin6; 114455505Sshin 114555505Sshin bzero(&sin6, sizeof(sin6)); 114655505Sshin sin6.sin6_family = AF_INET6; 114755505Sshin sin6.sin6_len = sizeof(sin6); 114855505Sshin sin6.sin6_addr = DR.rtaddr; 114955505Sshin getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf, 1150121156Sume sizeof(host_buf), NULL, 0, 1151121156Sume (nflag ? NI_NUMERICHOST : 0)); 1152121156Sume 115355505Sshin printf("%s if=%s", host_buf, 1154121156Sume if_indextoname(DR.if_index, ifix_buf)); 115555505Sshin printf(", flags=%s%s", 1156121156Sume DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 1157121156Sume DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 115855505Sshin gettimeofday(&time, 0); 115955505Sshin if (DR.expire == 0) 116055505Sshin printf(", expire=Never\n"); 116155505Sshin else 116255505Sshin printf(", expire=%s\n", 1163121156Sume sec2str(DR.expire - time.tv_sec)); 116455505Sshin } 116555505Sshin#undef DR 116655505Sshin close(s); 116778064Sume#endif 116855505Sshin} 116955505Sshin 117055505Sshinvoid 117155505Sshinplist() 117255505Sshin{ 117378064Sume#ifdef ICMPV6CTL_ND6_PRLIST 117478064Sume int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 117578064Sume char *buf; 117678064Sume struct in6_prefix *p, *ep, *n; 117778064Sume struct sockaddr_in6 *advrtr; 117878064Sume size_t l; 117978064Sume struct timeval time; 118078064Sume const int niflags = NI_NUMERICHOST; 118178064Sume int ninflags = nflag ? NI_NUMERICHOST : 0; 118278064Sume char namebuf[NI_MAXHOST]; 118378064Sume 118478064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 118578064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 118678064Sume /*NOTREACHED*/ 118778064Sume } 118878064Sume buf = malloc(l); 118978064Sume if (!buf) { 1190121156Sume err(1, "malloc"); 119178064Sume /*NOTREACHED*/ 119278064Sume } 119378064Sume if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 119478064Sume err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 119578064Sume /*NOTREACHED*/ 119678064Sume } 119778064Sume 119878064Sume ep = (struct in6_prefix *)(buf + l); 119978064Sume for (p = (struct in6_prefix *)buf; p < ep; p = n) { 120078064Sume advrtr = (struct sockaddr_in6 *)(p + 1); 120178064Sume n = (struct in6_prefix *)&advrtr[p->advrtrs]; 120278064Sume 120378064Sume if (getnameinfo((struct sockaddr *)&p->prefix, 120478064Sume p->prefix.sin6_len, namebuf, sizeof(namebuf), 120578064Sume NULL, 0, niflags) != 0) 120678064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 120778064Sume printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1208121156Sume if_indextoname(p->if_index, ifix_buf)); 120978064Sume 121078064Sume gettimeofday(&time, 0); 121178064Sume /* 121278064Sume * meaning of fields, especially flags, is very different 121378064Sume * by origin. notify the difference to the users. 121478064Sume */ 121578064Sume printf("flags=%s%s%s%s%s", 1216121156Sume p->raflags.onlink ? "L" : "", 1217121156Sume p->raflags.autonomous ? "A" : "", 1218121156Sume (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1219121156Sume (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 122078064Sume#ifdef NDPRF_HOME 1221121156Sume (p->flags & NDPRF_HOME) != 0 ? "H" : "" 122278064Sume#else 1223121156Sume "" 122478064Sume#endif 1225121156Sume ); 122678064Sume if (p->vltime == ND6_INFINITE_LIFETIME) 122778064Sume printf(" vltime=infinity"); 122878064Sume else 1229122615Sume printf(" vltime=%lu", (unsigned long)p->vltime); 123078064Sume if (p->pltime == ND6_INFINITE_LIFETIME) 123178064Sume printf(", pltime=infinity"); 123278064Sume else 1233122615Sume printf(", pltime=%lu", (unsigned long)p->pltime); 123478064Sume if (p->expire == 0) 123578064Sume printf(", expire=Never"); 123678064Sume else if (p->expire >= time.tv_sec) 123778064Sume printf(", expire=%s", 1238121156Sume sec2str(p->expire - time.tv_sec)); 123978064Sume else 124078064Sume printf(", expired"); 124178064Sume printf(", ref=%d", p->refcnt); 124278064Sume printf("\n"); 124378064Sume /* 124478064Sume * "advertising router" list is meaningful only if the prefix 124578064Sume * information is from RA. 124678064Sume */ 124778064Sume if (p->advrtrs) { 124878064Sume int j; 124978064Sume struct sockaddr_in6 *sin6; 125078064Sume 1251122615Sume sin6 = advrtr; 125278064Sume printf(" advertised by\n"); 125378064Sume for (j = 0; j < p->advrtrs; j++) { 125478064Sume struct in6_nbrinfo *nbi; 125578064Sume 125678064Sume if (getnameinfo((struct sockaddr *)sin6, 125778064Sume sin6->sin6_len, namebuf, sizeof(namebuf), 125878064Sume NULL, 0, ninflags) != 0) 125978064Sume strlcpy(namebuf, "?", sizeof(namebuf)); 126078064Sume printf(" %s", namebuf); 126178064Sume 1262121156Sume nbi = getnbrinfo(&sin6->sin6_addr, 1263121156Sume p->if_index, 0); 126478064Sume if (nbi) { 1265121156Sume switch (nbi->state) { 126678064Sume case ND6_LLINFO_REACHABLE: 126778064Sume case ND6_LLINFO_STALE: 126878064Sume case ND6_LLINFO_DELAY: 126978064Sume case ND6_LLINFO_PROBE: 127078064Sume printf(" (reachable)\n"); 127178064Sume break; 127278064Sume default: 127378064Sume printf(" (unreachable)\n"); 127478064Sume } 127578064Sume } else 127678064Sume printf(" (no neighbor state)\n"); 127778064Sume sin6++; 127878064Sume } 127978064Sume } else 128078064Sume printf(" No advertising router\n"); 128178064Sume } 128278064Sume free(buf); 128378064Sume#else 128455505Sshin struct in6_prlist pr; 128555505Sshin int s, i; 128655505Sshin struct timeval time; 128755505Sshin 128855505Sshin gettimeofday(&time, 0); 128955505Sshin 129055505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1291121156Sume err(1, "socket"); 1292121156Sume /* NOTREACHED */ 129355505Sshin } 129455505Sshin bzero(&pr, sizeof(pr)); 1295121156Sume strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */ 129655505Sshin if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 1297121156Sume err(1, "ioctl(SIOCGPRLST_IN6)"); 1298121156Sume /* NOTREACHED */ 1299121156Sume } 130062590Sitojun#define PR pr.prefix[i] 130155505Sshin for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 130278064Sume struct sockaddr_in6 p6; 130378064Sume char namebuf[NI_MAXHOST]; 130478064Sume int niflags; 130578064Sume 130678064Sume#ifdef NDPRF_ONLINK 130778064Sume p6 = PR.prefix; 130878064Sume#else 130978064Sume memset(&p6, 0, sizeof(p6)); 131078064Sume p6.sin6_family = AF_INET6; 131178064Sume p6.sin6_len = sizeof(p6); 131278064Sume p6.sin6_addr = PR.prefix; 131378064Sume#endif 131478064Sume 131578064Sume /* 131678064Sume * copy link index to sin6_scope_id field. 131778064Sume * XXX: KAME specific. 131878064Sume */ 131978064Sume if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) { 132078064Sume u_int16_t linkid; 132178064Sume 132278064Sume memcpy(&linkid, &p6.sin6_addr.s6_addr[2], 1323121156Sume sizeof(linkid)); 132478064Sume linkid = ntohs(linkid); 132578064Sume p6.sin6_scope_id = linkid; 132678064Sume p6.sin6_addr.s6_addr[2] = 0; 132778064Sume p6.sin6_addr.s6_addr[3] = 0; 132878064Sume } 132978064Sume 133078064Sume niflags = NI_NUMERICHOST; 133178064Sume if (getnameinfo((struct sockaddr *)&p6, 1332121156Sume sizeof(p6), namebuf, sizeof(namebuf), 1333121156Sume NULL, 0, niflags)) { 133478064Sume warnx("getnameinfo failed"); 133578064Sume continue; 133678064Sume } 133778064Sume printf("%s/%d if=%s\n", namebuf, PR.prefixlen, 1338121156Sume if_indextoname(PR.if_index, ifix_buf)); 133978064Sume 134055505Sshin gettimeofday(&time, 0); 134162590Sitojun /* 134262590Sitojun * meaning of fields, especially flags, is very different 134362590Sitojun * by origin. notify the difference to the users. 134462590Sitojun */ 134578064Sume#if 0 134678064Sume printf(" %s", 1347121156Sume PR.origin == PR_ORIG_RA ? "" : "advertise: "); 134878064Sume#endif 134978064Sume#ifdef NDPRF_ONLINK 135078064Sume printf("flags=%s%s%s%s%s", 1351121156Sume PR.raflags.onlink ? "L" : "", 1352121156Sume PR.raflags.autonomous ? "A" : "", 1353121156Sume (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1354121156Sume (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "", 135578064Sume#ifdef NDPRF_HOME 1356121156Sume (PR.flags & NDPRF_HOME) != 0 ? "H" : "" 135778064Sume#else 1358121156Sume "" 135978064Sume#endif 1360121156Sume ); 136178064Sume#else 136262590Sitojun printf("flags=%s%s", 1363121156Sume PR.raflags.onlink ? "L" : "", 1364121156Sume PR.raflags.autonomous ? "A" : ""); 136578064Sume#endif 136655505Sshin if (PR.vltime == ND6_INFINITE_LIFETIME) 136755505Sshin printf(" vltime=infinity"); 136855505Sshin else 1369122615Sume printf(" vltime=%lu", PR.vltime); 137055505Sshin if (PR.pltime == ND6_INFINITE_LIFETIME) 137155505Sshin printf(", pltime=infinity"); 137255505Sshin else 1373122615Sume printf(", pltime=%lu", PR.pltime); 137455505Sshin if (PR.expire == 0) 137562590Sitojun printf(", expire=Never"); 137655505Sshin else if (PR.expire >= time.tv_sec) 137762590Sitojun printf(", expire=%s", 1378121156Sume sec2str(PR.expire - time.tv_sec)); 137955505Sshin else 138062590Sitojun printf(", expired"); 138178064Sume#ifdef NDPRF_ONLINK 138278064Sume printf(", ref=%d", PR.refcnt); 138378064Sume#endif 138478064Sume#if 0 138562590Sitojun switch (PR.origin) { 138662590Sitojun case PR_ORIG_RA: 138762590Sitojun printf(", origin=RA"); 138862590Sitojun break; 138962590Sitojun case PR_ORIG_RR: 139062590Sitojun printf(", origin=RR"); 139162590Sitojun break; 139262590Sitojun case PR_ORIG_STATIC: 139362590Sitojun printf(", origin=static"); 139462590Sitojun break; 139562590Sitojun case PR_ORIG_KERNEL: 139662590Sitojun printf(", origin=kernel"); 139762590Sitojun break; 139862590Sitojun default: 139962590Sitojun printf(", origin=?"); 140062590Sitojun break; 140162590Sitojun } 140278064Sume#endif 140362590Sitojun printf("\n"); 140462590Sitojun /* 140562590Sitojun * "advertising router" list is meaningful only if the prefix 140662590Sitojun * information is from RA. 140762590Sitojun */ 140878064Sume if (0 && /* prefix origin is almost obsolted */ 140978064Sume PR.origin != PR_ORIG_RA) 141062590Sitojun ; 141162590Sitojun else if (PR.advrtrs) { 141255505Sshin int j; 141355505Sshin printf(" advertised by\n"); 141455505Sshin for (j = 0; j < PR.advrtrs; j++) { 141555505Sshin struct sockaddr_in6 sin6; 141662590Sitojun struct in6_nbrinfo *nbi; 141755505Sshin 141855505Sshin bzero(&sin6, sizeof(sin6)); 141955505Sshin sin6.sin6_family = AF_INET6; 142055505Sshin sin6.sin6_len = sizeof(sin6); 142155505Sshin sin6.sin6_addr = PR.advrtr[j]; 142262590Sitojun sin6.sin6_scope_id = PR.if_index; /* XXX */ 142355505Sshin getnameinfo((struct sockaddr *)&sin6, 1424121156Sume sin6.sin6_len, host_buf, 1425121156Sume sizeof(host_buf), NULL, 0, 1426121156Sume (nflag ? NI_NUMERICHOST : 0)); 142762590Sitojun printf(" %s", host_buf); 142855505Sshin 1429121156Sume nbi = getnbrinfo(&sin6.sin6_addr, 1430121156Sume PR.if_index, 0); 143162590Sitojun if (nbi) { 1432121156Sume switch (nbi->state) { 1433121156Sume case ND6_LLINFO_REACHABLE: 1434121156Sume case ND6_LLINFO_STALE: 1435121156Sume case ND6_LLINFO_DELAY: 1436121156Sume case ND6_LLINFO_PROBE: 143762590Sitojun printf(" (reachable)\n"); 143862590Sitojun break; 1439121156Sume default: 144062590Sitojun printf(" (unreachable)\n"); 144162590Sitojun } 144278064Sume } else 144362590Sitojun printf(" (no neighbor state)\n"); 144455505Sshin } 144555505Sshin if (PR.advrtrs > DRLSTSIZ) 144655505Sshin printf(" and %d routers\n", 1447121156Sume PR.advrtrs - DRLSTSIZ); 144862590Sitojun } else 144955505Sshin printf(" No advertising router\n"); 145055505Sshin } 145155505Sshin#undef PR 145255505Sshin close(s); 145378064Sume#endif 145455505Sshin} 145555505Sshin 145655505Sshinvoid 145755505Sshinpfx_flush() 145855505Sshin{ 145955505Sshin char dummyif[IFNAMSIZ+8]; 146055505Sshin int s; 146155505Sshin 146255505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 146355505Sshin err(1, "socket"); 1464121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 146555505Sshin if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1466121156Sume err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 146755505Sshin} 146855505Sshin 146955505Sshinvoid 147055505Sshinrtr_flush() 147155505Sshin{ 147255505Sshin char dummyif[IFNAMSIZ+8]; 147355505Sshin int s; 147455505Sshin 147555505Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 147655505Sshin err(1, "socket"); 1477121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 147855505Sshin if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1479121156Sume err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 148062590Sitojun 148162590Sitojun close(s); 148255505Sshin} 148355505Sshin 148455505Sshinvoid 148555505Sshinharmonize_rtr() 148655505Sshin{ 148755505Sshin char dummyif[IFNAMSIZ+8]; 148855505Sshin int s; 148955505Sshin 149062590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 149162590Sitojun err(1, "socket"); 1492121156Sume strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 149362590Sitojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1494121156Sume err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 149562590Sitojun 149662590Sitojun close(s); 149755505Sshin} 149855505Sshin 149962590Sitojun#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 150062590Sitojunstatic void 150162590Sitojunsetdefif(ifname) 150262590Sitojun char *ifname; 150362590Sitojun{ 150462590Sitojun struct in6_ndifreq ndifreq; 150562590Sitojun unsigned int ifindex; 150662590Sitojun 150762590Sitojun if (strcasecmp(ifname, "delete") == 0) 150862590Sitojun ifindex = 0; 150962590Sitojun else { 151062590Sitojun if ((ifindex = if_nametoindex(ifname)) == 0) 151162590Sitojun err(1, "failed to resolve i/f index for %s", ifname); 151262590Sitojun } 151362590Sitojun 151462590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 151562590Sitojun err(1, "socket"); 151662590Sitojun 1517121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 151862590Sitojun ndifreq.ifindex = ifindex; 151962590Sitojun 152062590Sitojun if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1521121156Sume err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 152262590Sitojun 152362590Sitojun close(s); 152462590Sitojun} 152562590Sitojun 152662590Sitojunstatic void 152762590Sitojungetdefif() 152862590Sitojun{ 152962590Sitojun struct in6_ndifreq ndifreq; 153062590Sitojun char ifname[IFNAMSIZ+8]; 153162590Sitojun 153262590Sitojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 153362590Sitojun err(1, "socket"); 153462590Sitojun 153562590Sitojun memset(&ndifreq, 0, sizeof(ndifreq)); 1536121156Sume strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 153762590Sitojun 153862590Sitojun if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1539121156Sume err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 154062590Sitojun 154162590Sitojun if (ndifreq.ifindex == 0) 154262590Sitojun printf("No default interface.\n"); 154362590Sitojun else { 154462590Sitojun if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 154562590Sitojun err(1, "failed to resolve ifname for index %lu", 154662590Sitojun ndifreq.ifindex); 154762590Sitojun printf("ND default interface = %s\n", ifname); 154862590Sitojun } 154962590Sitojun 155062590Sitojun close(s); 155162590Sitojun} 155262590Sitojun#endif 155362590Sitojun 155455505Sshinstatic char * 155555505Sshinsec2str(total) 155655505Sshin time_t total; 155755505Sshin{ 155855505Sshin static char result[256]; 155955505Sshin int days, hours, mins, secs; 156055505Sshin int first = 1; 156155505Sshin char *p = result; 1562121156Sume char *ep = &result[sizeof(result)]; 1563121156Sume int n; 156455505Sshin 156555505Sshin days = total / 3600 / 24; 156655505Sshin hours = (total / 3600) % 24; 156755505Sshin mins = (total / 60) % 60; 156855505Sshin secs = total % 60; 156955505Sshin 157055505Sshin if (days) { 157155505Sshin first = 0; 1572121156Sume n = snprintf(p, ep - p, "%dd", days); 1573121156Sume if (n < 0 || n >= ep - p) 1574121156Sume return "?"; 1575121156Sume p += n; 157655505Sshin } 157755505Sshin if (!first || hours) { 157855505Sshin first = 0; 1579121156Sume n = snprintf(p, ep - p, "%dh", hours); 1580121156Sume if (n < 0 || n >= ep - p) 1581121156Sume return "?"; 1582121156Sume p += n; 158355505Sshin } 158455505Sshin if (!first || mins) { 158555505Sshin first = 0; 1586121156Sume n = snprintf(p, ep - p, "%dm", mins); 1587121156Sume if (n < 0 || n >= ep - p) 1588121156Sume return "?"; 1589121156Sume p += n; 159055505Sshin } 1591121156Sume snprintf(p, ep - p, "%ds", secs); 159255505Sshin 159355505Sshin return(result); 159455505Sshin} 159555505Sshin 159655505Sshin/* 159755505Sshin * Print the timestamp 159855505Sshin * from tcpdump/util.c 159955505Sshin */ 160055505Sshinstatic void 160155505Sshints_print(tvp) 160255505Sshin const struct timeval *tvp; 160355505Sshin{ 160455505Sshin int s; 160555505Sshin 160655505Sshin /* Default */ 160755505Sshin s = (tvp->tv_sec + thiszone) % 86400; 160855505Sshin (void)printf("%02d:%02d:%02d.%06u ", 160955505Sshin s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 161055505Sshin} 1611