1*15967fb2Sbluhm /* $OpenBSD: ndp.c,v 1.109 2023/04/05 13:56:41 bluhm Exp $ */
25d6447e8Sitojun /* $KAME: ndp.c,v 1.101 2002/07/17 08:46:33 itojun Exp $ */
32ded093fSitojun
43a06b6c9Sitojun /*
53a06b6c9Sitojun * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
63a06b6c9Sitojun * All rights reserved.
73a06b6c9Sitojun *
83a06b6c9Sitojun * Redistribution and use in source and binary forms, with or without
93a06b6c9Sitojun * modification, are permitted provided that the following conditions
103a06b6c9Sitojun * are met:
113a06b6c9Sitojun * 1. Redistributions of source code must retain the above copyright
123a06b6c9Sitojun * notice, this list of conditions and the following disclaimer.
133a06b6c9Sitojun * 2. Redistributions in binary form must reproduce the above copyright
143a06b6c9Sitojun * notice, this list of conditions and the following disclaimer in the
153a06b6c9Sitojun * documentation and/or other materials provided with the distribution.
163a06b6c9Sitojun * 3. Neither the name of the project nor the names of its contributors
173a06b6c9Sitojun * may be used to endorse or promote products derived from this software
183a06b6c9Sitojun * without specific prior written permission.
193a06b6c9Sitojun *
203a06b6c9Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
213a06b6c9Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
223a06b6c9Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
233a06b6c9Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
243a06b6c9Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
253a06b6c9Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
263a06b6c9Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
273a06b6c9Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
283a06b6c9Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
293a06b6c9Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
303a06b6c9Sitojun * SUCH DAMAGE.
313a06b6c9Sitojun */
323a06b6c9Sitojun /*
333a06b6c9Sitojun * Copyright (c) 1984, 1993
343a06b6c9Sitojun * The Regents of the University of California. All rights reserved.
353a06b6c9Sitojun *
363a06b6c9Sitojun * This code is derived from software contributed to Berkeley by
373a06b6c9Sitojun * Sun Microsystems, Inc.
383a06b6c9Sitojun *
393a06b6c9Sitojun * Redistribution and use in source and binary forms, with or without
403a06b6c9Sitojun * modification, are permitted provided that the following conditions
413a06b6c9Sitojun * are met:
423a06b6c9Sitojun * 1. Redistributions of source code must retain the above copyright
433a06b6c9Sitojun * notice, this list of conditions and the following disclaimer.
443a06b6c9Sitojun * 2. Redistributions in binary form must reproduce the above copyright
453a06b6c9Sitojun * notice, this list of conditions and the following disclaimer in the
463a06b6c9Sitojun * documentation and/or other materials provided with the distribution.
4729295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
483a06b6c9Sitojun * may be used to endorse or promote products derived from this software
493a06b6c9Sitojun * without specific prior written permission.
503a06b6c9Sitojun *
513a06b6c9Sitojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
523a06b6c9Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
533a06b6c9Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
543a06b6c9Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
553a06b6c9Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
563a06b6c9Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
573a06b6c9Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
583a06b6c9Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
593a06b6c9Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
603a06b6c9Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
613a06b6c9Sitojun * SUCH DAMAGE.
623a06b6c9Sitojun */
633a06b6c9Sitojun
643a06b6c9Sitojun /*
653a06b6c9Sitojun * Based on:
663a06b6c9Sitojun * "@(#) Copyright (c) 1984, 1993\n\
673a06b6c9Sitojun * The Regents of the University of California. All rights reserved.\n";
683a06b6c9Sitojun *
693a06b6c9Sitojun * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
703a06b6c9Sitojun */
713a06b6c9Sitojun
723a06b6c9Sitojun /*
733a06b6c9Sitojun * ndp - display, set, delete and flush neighbor cache
743a06b6c9Sitojun */
753a06b6c9Sitojun
763a06b6c9Sitojun
773a06b6c9Sitojun #include <sys/ioctl.h>
783a06b6c9Sitojun #include <sys/socket.h>
793a06b6c9Sitojun #include <sys/sysctl.h>
803a06b6c9Sitojun #include <sys/time.h>
81cbe0ea82Sitojun #include <sys/queue.h>
823a06b6c9Sitojun
833a06b6c9Sitojun #include <net/if.h>
843a06b6c9Sitojun #include <net/if_dl.h>
853a06b6c9Sitojun #include <net/if_types.h>
863a06b6c9Sitojun #include <net/route.h>
873a06b6c9Sitojun
883a06b6c9Sitojun #include <netinet/in.h>
893a06b6c9Sitojun
903a06b6c9Sitojun #include <netinet/icmp6.h>
913a06b6c9Sitojun #include <netinet6/in6_var.h>
923a06b6c9Sitojun #include <netinet6/nd6.h>
933a06b6c9Sitojun
943a06b6c9Sitojun #include <arpa/inet.h>
953a06b6c9Sitojun
963a06b6c9Sitojun #include <stdio.h>
97ba8eb024Sderaadt #include <errno.h>
983a06b6c9Sitojun #include <fcntl.h>
99ba8eb024Sderaadt #include <netdb.h>
100ba8eb024Sderaadt #include <stdlib.h>
101ba8eb024Sderaadt #include <string.h>
1023a06b6c9Sitojun #include <unistd.h>
103a47b6461Sderaadt #include <limits.h>
104e66b2787Sitojun #include <err.h>
105ba8eb024Sderaadt
1063a06b6c9Sitojun /* packing rule for routing socket */
1073a06b6c9Sitojun #define ROUNDUP(a) \
1083a06b6c9Sitojun ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
109fd4782b8Sbluhm #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
1103a06b6c9Sitojun
11149a8054fSderaadt static pid_t pid;
1123a06b6c9Sitojun static int nflag;
1133a06b6c9Sitojun static int tflag;
114fa8923c4Sjca static int rtsock = -1;
1153a06b6c9Sitojun static int repeat = 0;
1163a06b6c9Sitojun
1173a06b6c9Sitojun char host_buf[NI_MAXHOST]; /* getnameinfo() */
1183a06b6c9Sitojun char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
1193a06b6c9Sitojun
120c72b5b24Smillert int file(char *);
121c72b5b24Smillert void getsocket(void);
122f775b7d7Sflorian int parse_host(const char *, struct sockaddr_in6 *);
123c72b5b24Smillert int set(int, char **);
124b19e7985Skn void get(const char *);
125b19e7985Skn int delete(const char *);
126f775b7d7Sflorian void dump(struct sockaddr_in6 *, int);
127ce93bbcbSitojun static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
128c72b5b24Smillert static char *ether_str(struct sockaddr_dl *);
129b19e7985Skn int ndp_ether_aton(const char *, u_char *);
130c72b5b24Smillert void usage(void);
131c72b5b24Smillert int rtmsg(int);
132f3b11628Smpi int rtget(struct sockaddr_in6 **, struct sockaddr_dl **);
133b19e7985Skn void ifinfo(const char *);
134ce93bbcbSitojun static char *sec2str(time_t);
135e3e2e38bSbluhm static int rdomain;
1363a06b6c9Sitojun
1373a06b6c9Sitojun int
main(int argc,char * argv[])138ae1436b2Sderaadt main(int argc, char *argv[])
1393a06b6c9Sitojun {
140fba40a11Sbluhm int ch, mode = 0, error = 0;
141207c9f8aSjca char *arg = NULL;
14285aa3881Sphessler const char *errstr;
1433a06b6c9Sitojun
1443a06b6c9Sitojun pid = getpid();
145e3e2e38bSbluhm rdomain = getrtable();
1464ce76853Sflorian while ((ch = getopt(argc, argv, "acd:f:i:nstA:V:")) != -1) {
147fba84816Sitojun switch (ch) {
1483a06b6c9Sitojun case 'a':
1493a06b6c9Sitojun case 'c':
150fba84816Sitojun case 'p':
151fba84816Sitojun case 'r':
152fba84816Sitojun case 'P':
153fba84816Sitojun case 's':
154fba84816Sitojun if (mode) {
155fba84816Sitojun usage();
156fba84816Sitojun }
157fba84816Sitojun mode = ch;
158fba84816Sitojun arg = NULL;
1593a06b6c9Sitojun break;
1603a06b6c9Sitojun case 'd':
161fba84816Sitojun case 'f':
1623a06b6c9Sitojun case 'i' :
163fba84816Sitojun if (mode) {
1643a06b6c9Sitojun usage();
165fba84816Sitojun }
166fba84816Sitojun mode = ch;
167fba84816Sitojun arg = optarg;
168fba84816Sitojun break;
1693a06b6c9Sitojun case 'n':
1703a06b6c9Sitojun nflag = 1;
1713a06b6c9Sitojun break;
1723a06b6c9Sitojun case 't':
1733a06b6c9Sitojun tflag = 1;
1743a06b6c9Sitojun break;
1753a06b6c9Sitojun case 'A':
176fba84816Sitojun if (mode) {
1773a06b6c9Sitojun usage();
178fba84816Sitojun }
179fba84816Sitojun mode = 'a';
180a47b6461Sderaadt repeat = strtonum(optarg, 1, INT_MAX, &errstr);
181a47b6461Sderaadt if (errstr) {
182fba84816Sitojun usage();
183fba84816Sitojun }
1843a06b6c9Sitojun break;
18585aa3881Sphessler case 'V':
18685aa3881Sphessler rdomain = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
18785aa3881Sphessler if (errstr != NULL) {
18885aa3881Sphessler warn("bad rdomain: %s", errstr);
18985aa3881Sphessler usage();
19085aa3881Sphessler }
19185aa3881Sphessler break;
1923a06b6c9Sitojun default:
1933a06b6c9Sitojun usage();
1943a06b6c9Sitojun }
195e3e2e38bSbluhm }
1963a06b6c9Sitojun argc -= optind;
1973a06b6c9Sitojun argv += optind;
1983a06b6c9Sitojun
199fba84816Sitojun switch (mode) {
200fba84816Sitojun case 'a':
201fba84816Sitojun case 'c':
202fba84816Sitojun if (argc != 0) {
2033a06b6c9Sitojun usage();
2043a06b6c9Sitojun }
2053c570657Sflorian dump(NULL, mode == 'c');
206fba84816Sitojun break;
207fba84816Sitojun case 'd':
208fba84816Sitojun if (argc != 0) {
209fba84816Sitojun usage();
210fba84816Sitojun }
211fba40a11Sbluhm error = delete(arg);
212fba84816Sitojun break;
2131e1c2156Sjca case 'f':
2141e1c2156Sjca if (argc != 0)
2151e1c2156Sjca usage();
2161e1c2156Sjca file(arg);
2171e1c2156Sjca break;
218fba84816Sitojun case 'i':
2190e39def2Sflorian if (argc != 0)
2200e39def2Sflorian usage();
2210e39def2Sflorian ifinfo(arg);
222fba84816Sitojun break;
223fba84816Sitojun case 's':
2243a06b6c9Sitojun if (argc < 2 || argc > 4)
2253a06b6c9Sitojun usage();
2263a06b6c9Sitojun exit(set(argc, argv) ? 1 : 0);
227fba84816Sitojun case 0:
228fba84816Sitojun if (argc != 1) {
229fba84816Sitojun usage();
230fba84816Sitojun }
2313a06b6c9Sitojun get(argv[0]);
232fba84816Sitojun break;
233fba84816Sitojun }
234fba40a11Sbluhm return (error);
2353a06b6c9Sitojun }
2363a06b6c9Sitojun
2373a06b6c9Sitojun /*
2383a06b6c9Sitojun * Process a file to set standard ndp entries
2393a06b6c9Sitojun */
2403a06b6c9Sitojun int
file(char * name)241ae1436b2Sderaadt file(char *name)
2423a06b6c9Sitojun {
2433a06b6c9Sitojun FILE *fp;
2443a06b6c9Sitojun int i, retval;
2453a06b6c9Sitojun char line[100], arg[5][50], *args[5];
2463a06b6c9Sitojun
2473a06b6c9Sitojun if ((fp = fopen(name, "r")) == NULL) {
24823a16c1cSkn err(1, "cannot open %s", name);
2493a06b6c9Sitojun }
2503a06b6c9Sitojun args[0] = &arg[0][0];
2513a06b6c9Sitojun args[1] = &arg[1][0];
2523a06b6c9Sitojun args[2] = &arg[2][0];
2533a06b6c9Sitojun args[3] = &arg[3][0];
2543a06b6c9Sitojun args[4] = &arg[4][0];
2553a06b6c9Sitojun retval = 0;
256b3c11e64Sray while (fgets(line, sizeof(line), fp) != NULL) {
25707412504Sitojun i = sscanf(line, "%49s %49s %49s %49s %49s",
25807412504Sitojun arg[0], arg[1], arg[2], arg[3], arg[4]);
2593a06b6c9Sitojun if (i < 2) {
26023a16c1cSkn warnx("bad line: %s", line);
2613a06b6c9Sitojun retval = 1;
2623a06b6c9Sitojun continue;
2633a06b6c9Sitojun }
2643a06b6c9Sitojun if (set(i, args))
2653a06b6c9Sitojun retval = 1;
2663a06b6c9Sitojun }
2673a06b6c9Sitojun fclose(fp);
2683a06b6c9Sitojun return (retval);
2693a06b6c9Sitojun }
2703a06b6c9Sitojun
2713a06b6c9Sitojun void
getsocket(void)272ae1436b2Sderaadt getsocket(void)
2733a06b6c9Sitojun {
2742ab086daSbluhm socklen_t len = sizeof(rdomain);
2752ab086daSbluhm
2762ab086daSbluhm if (rtsock >= 0)
2772ab086daSbluhm return;
278b47fcd70Skrw rtsock = socket(AF_ROUTE, SOCK_RAW, 0);
279df69c215Sderaadt if (rtsock == -1)
2802ab086daSbluhm err(1, "routing socket");
281df69c215Sderaadt if (setsockopt(rtsock, AF_ROUTE, ROUTE_TABLEFILTER, &rdomain, len) == -1)
2822ab086daSbluhm err(1, "ROUTE_TABLEFILTER");
2832ab086daSbluhm
2842ab086daSbluhm if (pledge("stdio dns", NULL) == -1)
2852ab086daSbluhm err(1, "pledge");
2863a06b6c9Sitojun }
2873a06b6c9Sitojun
288e9c45431Skn int
parse_host(const char * host,struct sockaddr_in6 * sin6)289f775b7d7Sflorian parse_host(const char *host, struct sockaddr_in6 *sin6)
290e9c45431Skn {
291e9c45431Skn struct addrinfo hints, *res;
292e9c45431Skn int gai_error;
293e9c45431Skn
294e9c45431Skn bzero(&hints, sizeof(hints));
295e9c45431Skn hints.ai_family = AF_INET6;
296e9c45431Skn if (nflag)
297e9c45431Skn hints.ai_flags = AI_NUMERICHOST;
298e9c45431Skn
299e9c45431Skn gai_error = getaddrinfo(host, NULL, &hints, &res);
300e9c45431Skn if (gai_error) {
301e9c45431Skn warnx("%s: %s", host, gai_strerror(gai_error));
302e9c45431Skn return 1;
303e9c45431Skn }
304f775b7d7Sflorian *sin6 = *(struct sockaddr_in6 *)res->ai_addr;
305e9c45431Skn freeaddrinfo(res);
306e9c45431Skn return 0;
307e9c45431Skn }
308e9c45431Skn
309a0f7b357Sitojun struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
3103a06b6c9Sitojun struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
3113a06b6c9Sitojun struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
312fd4782b8Sbluhm struct sockaddr_dl ifp_m = { sizeof(ifp_m), AF_LINK };
31367a14d93Sbluhm time_t expire_time;
31467a14d93Sbluhm int flags, found_entry;
3153a06b6c9Sitojun struct {
3163a06b6c9Sitojun struct rt_msghdr m_rtm;
3173a06b6c9Sitojun char m_space[512];
3183a06b6c9Sitojun } m_rtmsg;
3193a06b6c9Sitojun
3203a06b6c9Sitojun /*
3213a06b6c9Sitojun * Set an individual neighbor cache entry
3223a06b6c9Sitojun */
3233a06b6c9Sitojun int
set(int argc,char * argv[])324b19e7985Skn set(int argc, char *argv[])
3253a06b6c9Sitojun {
3264b6e8b27Sderaadt struct sockaddr_in6 *sin = &sin_m;
3274b6e8b27Sderaadt struct sockaddr_dl *sdl;
3284b6e8b27Sderaadt struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
3293a06b6c9Sitojun u_char *ea;
330b19e7985Skn const char *host = argv[0], *eaddr = argv[1];
3313a06b6c9Sitojun
3323a06b6c9Sitojun getsocket();
3333a06b6c9Sitojun argc -= 2;
3343a06b6c9Sitojun argv += 2;
3353a06b6c9Sitojun sdl_m = blank_sdl;
3363a06b6c9Sitojun sin_m = blank_sin;
3373a06b6c9Sitojun
338f775b7d7Sflorian if (parse_host(host, sin))
3393a06b6c9Sitojun return 1;
3403a06b6c9Sitojun ea = (u_char *)LLADDR(&sdl_m);
3413a06b6c9Sitojun if (ndp_ether_aton(eaddr, ea) == 0)
3423a06b6c9Sitojun sdl_m.sdl_alen = 6;
34367a14d93Sbluhm expire_time = 0;
34467a14d93Sbluhm flags = 0;
3453a06b6c9Sitojun while (argc-- > 0) {
3463a06b6c9Sitojun if (strncmp(argv[0], "temp", 4) == 0) {
3473b91be6eSbluhm struct timeval now;
348626954f6Sderaadt
3493b91be6eSbluhm gettimeofday(&now, 0);
3503b91be6eSbluhm expire_time = now.tv_sec + 20 * 60;
351a0f7b357Sitojun } else if (strncmp(argv[0], "proxy", 5) == 0)
352a0f7b357Sitojun flags |= RTF_ANNOUNCE;
3533a06b6c9Sitojun argv++;
3543a06b6c9Sitojun }
355f3b11628Smpi
356f3b11628Smpi if (rtget(&sin, &sdl)) {
357e66b2787Sitojun errx(1, "RTM_GET(%s) failed", host);
3583a06b6c9Sitojun }
359f3b11628Smpi
360f775b7d7Sflorian if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr) &&
361f775b7d7Sflorian sin->sin6_scope_id == sin_m.sin6_scope_id) {
3623a06b6c9Sitojun if (sdl->sdl_family == AF_LINK &&
3633a06b6c9Sitojun (rtm->rtm_flags & RTF_LLINFO) &&
364fba84816Sitojun !(rtm->rtm_flags & RTF_GATEWAY)) {
365fba84816Sitojun switch (sdl->sdl_type) {
3663a06b6c9Sitojun case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
3673a06b6c9Sitojun case IFT_ISO88024: case IFT_ISO88025:
3683a06b6c9Sitojun goto overwrite;
3693a06b6c9Sitojun }
370fba84816Sitojun }
371742030deSitojun /*
372742030deSitojun * IPv4 arp command retries with sin_other = SIN_PROXY here.
373742030deSitojun */
37423a16c1cSkn warnx("set: cannot configure a new entry");
375742030deSitojun return 1;
3763a06b6c9Sitojun }
377742030deSitojun
3783a06b6c9Sitojun overwrite:
3793a06b6c9Sitojun if (sdl->sdl_family != AF_LINK) {
3803a06b6c9Sitojun printf("cannot intuit interface index and type for %s\n", host);
3813a06b6c9Sitojun return (1);
3823a06b6c9Sitojun }
3833a06b6c9Sitojun sdl_m.sdl_type = sdl->sdl_type;
3843a06b6c9Sitojun sdl_m.sdl_index = sdl->sdl_index;
3853a06b6c9Sitojun return (rtmsg(RTM_ADD));
3863a06b6c9Sitojun }
3873a06b6c9Sitojun
3883a06b6c9Sitojun /*
3893a06b6c9Sitojun * Display an individual neighbor cache entry
3903a06b6c9Sitojun */
3913a06b6c9Sitojun void
get(const char * host)392b19e7985Skn get(const char *host)
3933a06b6c9Sitojun {
3943a06b6c9Sitojun struct sockaddr_in6 *sin = &sin_m;
3953a06b6c9Sitojun
3963a06b6c9Sitojun sin_m = blank_sin;
397f775b7d7Sflorian if (parse_host(host, sin))
3983a06b6c9Sitojun return;
399e9c45431Skn
400f775b7d7Sflorian dump(sin, 0);
4013a06b6c9Sitojun if (found_entry == 0) {
4023a06b6c9Sitojun getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
4033a06b6c9Sitojun sizeof(host_buf), NULL ,0,
404fae7be87Sitojun (nflag ? NI_NUMERICHOST : 0));
4053a06b6c9Sitojun printf("%s (%s) -- no entry\n", host, host_buf);
4063a06b6c9Sitojun exit(1);
4073a06b6c9Sitojun }
4083a06b6c9Sitojun }
4093a06b6c9Sitojun
4103a06b6c9Sitojun /*
4113a06b6c9Sitojun * Delete a neighbor cache entry
4123a06b6c9Sitojun */
4133a06b6c9Sitojun int
delete(const char * host)414b19e7985Skn delete(const char *host)
4153a06b6c9Sitojun {
416fba40a11Sbluhm struct sockaddr_in6 *sin;
417fba40a11Sbluhm struct rt_msghdr *rtm;
4183a06b6c9Sitojun struct sockaddr_dl *sdl;
4193a06b6c9Sitojun
420fba40a11Sbluhm sin = &sin_m;
421fba40a11Sbluhm rtm = &m_rtmsg.m_rtm;
422fba40a11Sbluhm
4233a06b6c9Sitojun getsocket();
424fba40a11Sbluhm sin_m = blank_sin; /* struct copy */
425f775b7d7Sflorian if (parse_host(host, sin))
4263a06b6c9Sitojun return 1;
427f3b11628Smpi if (rtget(&sin, &sdl)) {
428fba40a11Sbluhm warn("%s", host);
429fba40a11Sbluhm return 1;
4303a06b6c9Sitojun }
431f775b7d7Sflorian if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr) &&
432f775b7d7Sflorian sin->sin6_scope_id == sin_m.sin6_scope_id) {
43338c8fc57Smpi if (sdl->sdl_family == AF_LINK && rtm->rtm_flags & RTF_LLINFO) {
4341fa97ce3Smpi if (rtm->rtm_flags & RTF_LOCAL)
435fba40a11Sbluhm return 0;
436fba40a11Sbluhm if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
4373a06b6c9Sitojun goto delete;
4383a06b6c9Sitojun }
439*15967fb2Sbluhm }
440*15967fb2Sbluhm
441742030deSitojun /*
442742030deSitojun * IPv4 arp command retries with sin_other = SIN_PROXY here.
443742030deSitojun */
444fba40a11Sbluhm warnx("delete: cannot locate %s", host);
445742030deSitojun return 1;
446d374aaacSitojun
4473a06b6c9Sitojun delete:
4483a06b6c9Sitojun if (sdl->sdl_family != AF_LINK) {
4493a06b6c9Sitojun printf("cannot locate %s\n", host);
450fba40a11Sbluhm return 1;
4513a06b6c9Sitojun }
452fba40a11Sbluhm if (rtmsg(RTM_DELETE))
453fba40a11Sbluhm return 1;
454fba40a11Sbluhm getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
455fba40a11Sbluhm sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
4563a06b6c9Sitojun printf("%s (%s) deleted\n", host, host_buf);
4573a06b6c9Sitojun return 0;
4583a06b6c9Sitojun }
4593a06b6c9Sitojun
4602150c9baSkn /*
4612150c9baSkn * strlen("2001:0db8:3333:4444:5555:6666:7777:8888") == 39
4622150c9baSkn */
4632150c9baSkn #define W_ADDR 39
464f76e25adSitojun #define W_LL 17
4659dd4e201Smpi #define W_IF 7
466f76e25adSitojun
4673a06b6c9Sitojun /*
4683a06b6c9Sitojun * Dump the entire neighbor cache
4693a06b6c9Sitojun */
4703a06b6c9Sitojun void
dump(struct sockaddr_in6 * addr,int cflag)471f775b7d7Sflorian dump(struct sockaddr_in6 *addr, int cflag)
4723a06b6c9Sitojun {
47385aa3881Sphessler int mib[7];
4743a06b6c9Sitojun size_t needed;
4758ae2d342Sderaadt char *lim, *buf = NULL, *next;
4763a06b6c9Sitojun struct rt_msghdr *rtm;
4773a06b6c9Sitojun struct sockaddr_in6 *sin;
4783a06b6c9Sitojun struct sockaddr_dl *sdl;
4793a06b6c9Sitojun struct in6_nbrinfo *nbi;
4803b91be6eSbluhm struct timeval now;
4813a06b6c9Sitojun int addrwidth;
482f76e25adSitojun int llwidth;
483f76e25adSitojun int ifwidth;
484054a08cbSitojun char *ifname;
4853a06b6c9Sitojun
4863a06b6c9Sitojun /* Print header */
487cbe0ea82Sitojun if (!tflag && !cflag)
488fba84816Sitojun printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
489f76e25adSitojun W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
490fba84816Sitojun W_IF, W_IF, "Netif", "Expire", "S", "Flags");
4913a06b6c9Sitojun
4923a06b6c9Sitojun again:;
4939c991b9fSjca lim = NULL;
4943a06b6c9Sitojun mib[0] = CTL_NET;
4953a06b6c9Sitojun mib[1] = PF_ROUTE;
4963a06b6c9Sitojun mib[2] = 0;
4973a06b6c9Sitojun mib[3] = AF_INET6;
4983a06b6c9Sitojun mib[4] = NET_RT_FLAGS;
4993a06b6c9Sitojun mib[5] = RTF_LLINFO;
50085aa3881Sphessler mib[6] = rdomain;
5018ae2d342Sderaadt while (1) {
50285aa3881Sphessler if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1)
5033a06b6c9Sitojun err(1, "sysctl(PF_ROUTE estimate)");
5048ae2d342Sderaadt if (needed == 0)
5058ae2d342Sderaadt break;
5068ae2d342Sderaadt if ((buf = realloc(buf, needed)) == NULL)
5078ae2d342Sderaadt err(1, "realloc");
50885aa3881Sphessler if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) {
5098ae2d342Sderaadt if (errno == ENOMEM)
5108ae2d342Sderaadt continue;
5113a06b6c9Sitojun err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
5128ae2d342Sderaadt }
5133a06b6c9Sitojun lim = buf + needed;
5148ae2d342Sderaadt break;
5158ae2d342Sderaadt }
5163a06b6c9Sitojun
5179c991b9fSjca for (next = buf; next && lim && next < lim; next += rtm->rtm_msglen) {
5183a06b6c9Sitojun int isrouter = 0, prbs = 0;
5193a06b6c9Sitojun
5203a06b6c9Sitojun rtm = (struct rt_msghdr *)next;
521e60a43bfSclaudio if (rtm->rtm_version != RTM_VERSION)
522e60a43bfSclaudio continue;
5237a13f2d5Schris sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen);
524f775b7d7Sflorian #ifdef __KAME__
525f775b7d7Sflorian {
526f775b7d7Sflorian struct in6_addr *in6 = &sin->sin6_addr;
527f775b7d7Sflorian if ((IN6_IS_ADDR_LINKLOCAL(in6) ||
528f775b7d7Sflorian IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
529f775b7d7Sflorian IN6_IS_ADDR_MC_INTFACELOCAL(in6)) &&
530f775b7d7Sflorian sin->sin6_scope_id == 0) {
531f775b7d7Sflorian sin->sin6_scope_id = (u_int32_t)
532f775b7d7Sflorian ntohs(*(u_short *)&in6->s6_addr[2]);
533f775b7d7Sflorian *(u_short *)&in6->s6_addr[2] = 0;
534f775b7d7Sflorian }
535f775b7d7Sflorian }
536f775b7d7Sflorian #endif
5373a06b6c9Sitojun sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
538e66b2787Sitojun
539e66b2787Sitojun /*
540e66b2787Sitojun * Some OSes can produce a route that has the LINK flag but
541e66b2787Sitojun * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
542e66b2787Sitojun * and BSD/OS, where xx is not the interface identifier on
543e66b2787Sitojun * lo0). Such routes entry would annoy getnbrinfo() below,
544e66b2787Sitojun * so we skip them.
545e66b2787Sitojun * XXX: such routes should have the GATEWAY flag, not the
546e66b2787Sitojun * LINK flag. However, there is rotten routing software
547e66b2787Sitojun * that advertises all routes that have the GATEWAY flag.
548e66b2787Sitojun * Thus, KAME kernel intentionally does not set the LINK flag.
549e66b2787Sitojun * What is to be fixed is not ndp, but such routing software
550e66b2787Sitojun * (and the kernel workaround)...
551e66b2787Sitojun */
552e66b2787Sitojun if (sdl->sdl_family != AF_LINK)
553e66b2787Sitojun continue;
554e66b2787Sitojun
555fae7be87Sitojun if (!(rtm->rtm_flags & RTF_HOST))
556fae7be87Sitojun continue;
557fae7be87Sitojun
5583a06b6c9Sitojun if (addr) {
559f775b7d7Sflorian if (!IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
560f775b7d7Sflorian &sin->sin6_addr) || addr->sin6_scope_id !=
561f775b7d7Sflorian sin->sin6_scope_id)
5623a06b6c9Sitojun continue;
5633a06b6c9Sitojun found_entry = 1;
5643a06b6c9Sitojun } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
5653a06b6c9Sitojun continue;
5663a06b6c9Sitojun getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
567fba84816Sitojun sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
568fba84816Sitojun if (cflag) {
569fae7be87Sitojun if (rtm->rtm_flags & RTF_CLONED)
570fae7be87Sitojun delete(host_buf);
571cbe0ea82Sitojun continue;
572cbe0ea82Sitojun }
5733b91be6eSbluhm gettimeofday(&now, 0);
574567a5b7eSflorian if (tflag) {
575567a5b7eSflorian char buf[sizeof("00:00:00")];
576567a5b7eSflorian struct tm *tm;
577567a5b7eSflorian
578567a5b7eSflorian tm = localtime(&now.tv_sec);
579567a5b7eSflorian if (tm != NULL) {
580567a5b7eSflorian strftime(buf, sizeof(buf), "%H:%M:%S", tm);
581567a5b7eSflorian printf("%s.%06ld ", buf, now.tv_usec);
582567a5b7eSflorian }
583567a5b7eSflorian }
5843a06b6c9Sitojun
5853a06b6c9Sitojun addrwidth = strlen(host_buf);
586f76e25adSitojun if (addrwidth < W_ADDR)
587f76e25adSitojun addrwidth = W_ADDR;
588f76e25adSitojun llwidth = strlen(ether_str(sdl));
589f76e25adSitojun if (W_ADDR + W_LL - addrwidth > llwidth)
590f76e25adSitojun llwidth = W_ADDR + W_LL - addrwidth;
591054a08cbSitojun ifname = if_indextoname(sdl->sdl_index, ifix_buf);
592054a08cbSitojun if (!ifname)
593054a08cbSitojun ifname = "?";
594054a08cbSitojun ifwidth = strlen(ifname);
595f76e25adSitojun if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
596f76e25adSitojun ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
5973a06b6c9Sitojun
598f76e25adSitojun printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
599054a08cbSitojun llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
6003a06b6c9Sitojun
6013a50f0a9Sjmc /* Print neighbor discovery specific information */
6023a06b6c9Sitojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
6033a06b6c9Sitojun if (nbi) {
6043b91be6eSbluhm if (nbi->expire > now.tv_sec) {
6053a06b6c9Sitojun printf(" %-9.9s",
6063b91be6eSbluhm sec2str(nbi->expire - now.tv_sec));
607cbe0ea82Sitojun } else if (nbi->expire == 0)
6083a06b6c9Sitojun printf(" %-9.9s", "permanent");
6093a06b6c9Sitojun else
6103a06b6c9Sitojun printf(" %-9.9s", "expired");
6113a06b6c9Sitojun
6123a06b6c9Sitojun switch (nbi->state) {
6133a06b6c9Sitojun case ND6_LLINFO_NOSTATE:
6143a06b6c9Sitojun printf(" N");
6153a06b6c9Sitojun break;
6163a06b6c9Sitojun case ND6_LLINFO_INCOMPLETE:
6173a06b6c9Sitojun printf(" I");
6183a06b6c9Sitojun break;
6193a06b6c9Sitojun case ND6_LLINFO_REACHABLE:
6203a06b6c9Sitojun printf(" R");
6213a06b6c9Sitojun break;
6223a06b6c9Sitojun case ND6_LLINFO_STALE:
6233a06b6c9Sitojun printf(" S");
6243a06b6c9Sitojun break;
6253a06b6c9Sitojun case ND6_LLINFO_DELAY:
6263a06b6c9Sitojun printf(" D");
6273a06b6c9Sitojun break;
6283a06b6c9Sitojun case ND6_LLINFO_PROBE:
6293a06b6c9Sitojun printf(" P");
6303a06b6c9Sitojun break;
6313a06b6c9Sitojun default:
6323a06b6c9Sitojun printf(" ?");
6333a06b6c9Sitojun break;
6343a06b6c9Sitojun }
6353a06b6c9Sitojun
6363a06b6c9Sitojun isrouter = nbi->isrouter;
6373a06b6c9Sitojun prbs = nbi->asked;
638cbe0ea82Sitojun } else {
6393a06b6c9Sitojun warnx("failed to get neighbor information");
6403a06b6c9Sitojun printf(" ");
6413a06b6c9Sitojun }
6423a06b6c9Sitojun
643d8f8e4e4Sbluhm printf(" %s%s%s",
64448ccdf10Smpi (rtm->rtm_flags & RTF_LOCAL) ? "l" : "",
645a0f7b357Sitojun isrouter ? "R" : "",
646a0f7b357Sitojun (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
6473a06b6c9Sitojun
6483a06b6c9Sitojun if (prbs)
649fae7be87Sitojun printf(" %d", prbs);
6503a06b6c9Sitojun
6513a06b6c9Sitojun printf("\n");
6523a06b6c9Sitojun }
6533a06b6c9Sitojun
6543a06b6c9Sitojun if (repeat) {
6553a06b6c9Sitojun printf("\n");
656a2718418Sitojun fflush(stdout);
6573a06b6c9Sitojun sleep(repeat);
6583a06b6c9Sitojun goto again;
6593a06b6c9Sitojun }
6609c357217Sjca
6619c357217Sjca free(buf);
6623a06b6c9Sitojun }
6633a06b6c9Sitojun
6643a06b6c9Sitojun static struct in6_nbrinfo *
getnbrinfo(struct in6_addr * addr,int ifindex,int warning)665ae1436b2Sderaadt getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
6663a06b6c9Sitojun {
6673a06b6c9Sitojun static struct in6_nbrinfo nbi;
6683a06b6c9Sitojun int s;
6693a06b6c9Sitojun
670df69c215Sderaadt if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
6713a06b6c9Sitojun err(1, "socket");
6723a06b6c9Sitojun
6733a06b6c9Sitojun bzero(&nbi, sizeof(nbi));
6743a06b6c9Sitojun if_indextoname(ifindex, nbi.ifname);
6753a06b6c9Sitojun nbi.addr = *addr;
676df69c215Sderaadt if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) == -1) {
6773a06b6c9Sitojun if (warning)
678d374aaacSitojun warn("ioctl(SIOCGNBRINFO_IN6)");
6793a06b6c9Sitojun close(s);
6803a06b6c9Sitojun return(NULL);
6813a06b6c9Sitojun }
6823a06b6c9Sitojun
6833a06b6c9Sitojun close(s);
6843a06b6c9Sitojun return(&nbi);
6853a06b6c9Sitojun }
6863a06b6c9Sitojun
6873a06b6c9Sitojun static char *
ether_str(struct sockaddr_dl * sdl)688ae1436b2Sderaadt ether_str(struct sockaddr_dl *sdl)
6893a06b6c9Sitojun {
69040e2a752Sitojun static char hbuf[NI_MAXHOST];
6913a06b6c9Sitojun u_char *cp;
6923a06b6c9Sitojun
6933a06b6c9Sitojun if (sdl->sdl_alen) {
6943a06b6c9Sitojun cp = (u_char *)LLADDR(sdl);
695743faed6Ssthen snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
6963a06b6c9Sitojun cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
69740e2a752Sitojun } else
69840e2a752Sitojun snprintf(hbuf, sizeof(hbuf), "(incomplete)");
6993a06b6c9Sitojun
70040e2a752Sitojun return(hbuf);
7013a06b6c9Sitojun }
7023a06b6c9Sitojun
7033a06b6c9Sitojun int
ndp_ether_aton(const char * a,u_char * n)704b19e7985Skn ndp_ether_aton(const char *a, u_char *n)
7053a06b6c9Sitojun {
7063a06b6c9Sitojun int i, o[6];
7073a06b6c9Sitojun
7083a06b6c9Sitojun i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
7093a06b6c9Sitojun &o[3], &o[4], &o[5]);
7103a06b6c9Sitojun if (i != 6) {
71123a16c1cSkn warnx("invalid Ethernet address '%s'", a);
7123a06b6c9Sitojun return (1);
7133a06b6c9Sitojun }
7143a06b6c9Sitojun for (i = 0; i < 6; i++)
7153a06b6c9Sitojun n[i] = o[i];
7163a06b6c9Sitojun return (0);
7173a06b6c9Sitojun }
7183a06b6c9Sitojun
7193a06b6c9Sitojun void
usage(void)720ae1436b2Sderaadt usage(void)
7213a06b6c9Sitojun {
7224ce76853Sflorian printf("usage: ndp [-acnt] ");
723036cd9efSjmc printf("[-A wait] [-d hostname] [-f filename] [-i interface]\n");
724036cd9efSjmc printf("\t[-s nodename ether_addr [temp] [proxy]] ");
725036cd9efSjmc printf("[-V rdomain] [hostname]\n");
7263a06b6c9Sitojun exit(1);
7273a06b6c9Sitojun }
7283a06b6c9Sitojun
7293a06b6c9Sitojun int
rtmsg(int cmd)730ae1436b2Sderaadt rtmsg(int cmd)
7313a06b6c9Sitojun {
7323a06b6c9Sitojun static int seq;
7333a06b6c9Sitojun int rlen;
7344b6e8b27Sderaadt struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
7354b6e8b27Sderaadt char *cp = m_rtmsg.m_space;
7364b6e8b27Sderaadt int l;
7373a06b6c9Sitojun
7383a06b6c9Sitojun errno = 0;
7393a06b6c9Sitojun if (cmd == RTM_DELETE)
7403a06b6c9Sitojun goto doit;
7413a06b6c9Sitojun bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
7423a06b6c9Sitojun rtm->rtm_flags = flags;
7433a06b6c9Sitojun rtm->rtm_version = RTM_VERSION;
74485aa3881Sphessler rtm->rtm_tableid = rdomain;
7453a06b6c9Sitojun
7463a06b6c9Sitojun switch (cmd) {
7473a06b6c9Sitojun default:
74823a16c1cSkn errx(1, "internal wrong cmd");
7493a06b6c9Sitojun case RTM_ADD:
7503a06b6c9Sitojun rtm->rtm_addrs |= RTA_GATEWAY;
751fae7be87Sitojun if (expire_time) {
7523a06b6c9Sitojun rtm->rtm_rmx.rmx_expire = expire_time;
7533a06b6c9Sitojun rtm->rtm_inits = RTV_EXPIRE;
754fae7be87Sitojun }
7553a06b6c9Sitojun rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
756389d9112Smarkus #if 0 /* we don't support ipv6addr/128 type proxying. */
757a0f7b357Sitojun if (rtm->rtm_flags & RTF_ANNOUNCE) {
758a0f7b357Sitojun rtm->rtm_flags &= ~RTF_HOST;
75908db349cSitojun rtm->rtm_addrs |= RTA_NETMASK;
760a0f7b357Sitojun }
761389d9112Smarkus #endif
7623a06b6c9Sitojun /* FALLTHROUGH */
7633a06b6c9Sitojun case RTM_GET:
764f3b11628Smpi rtm->rtm_addrs |= (RTA_DST | RTA_IFP);
7653a06b6c9Sitojun }
766fd4782b8Sbluhm
7673a06b6c9Sitojun #define NEXTADDR(w, s) \
7683a06b6c9Sitojun if (rtm->rtm_addrs & (w)) { \
769fd4782b8Sbluhm memcpy(cp, &(s), sizeof(s)); \
770fd4782b8Sbluhm ADVANCE(cp, (struct sockaddr *)&(s)); \
771fd4782b8Sbluhm }
7723a06b6c9Sitojun
773f775b7d7Sflorian #ifdef __KAME__
774f775b7d7Sflorian {
775f775b7d7Sflorian struct sockaddr_in6 sin6 = sin_m;
776f775b7d7Sflorian struct in6_addr *in6 = &sin6.sin6_addr;
777f775b7d7Sflorian if (IN6_IS_ADDR_LINKLOCAL(in6) ||
778f775b7d7Sflorian IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
779f775b7d7Sflorian IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
780f775b7d7Sflorian *(u_int16_t *)& in6->s6_addr[2] =
781f775b7d7Sflorian htons(sin6.sin6_scope_id);
782f775b7d7Sflorian sin6.sin6_scope_id = 0;
783f775b7d7Sflorian }
784f775b7d7Sflorian NEXTADDR(RTA_DST, sin6);
785f775b7d7Sflorian }
786f775b7d7Sflorian #else
7873a06b6c9Sitojun NEXTADDR(RTA_DST, sin_m);
788f775b7d7Sflorian #endif
7893a06b6c9Sitojun NEXTADDR(RTA_GATEWAY, sdl_m);
790389d9112Smarkus #if 0 /* we don't support ipv6addr/128 type proxying. */
791a0f7b357Sitojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
7923a06b6c9Sitojun NEXTADDR(RTA_NETMASK, so_mask);
793389d9112Smarkus #endif
794f3b11628Smpi NEXTADDR(RTA_IFP, ifp_m);
7953a06b6c9Sitojun
7963a06b6c9Sitojun rtm->rtm_msglen = cp - (char *)&m_rtmsg;
7973a06b6c9Sitojun doit:
7983a06b6c9Sitojun l = rtm->rtm_msglen;
7993a06b6c9Sitojun rtm->rtm_seq = ++seq;
8003a06b6c9Sitojun rtm->rtm_type = cmd;
801df69c215Sderaadt if ((rlen = write(rtsock, (char *)&m_rtmsg, l)) == -1) {
8023a06b6c9Sitojun if (errno != ESRCH || cmd != RTM_DELETE) {
803e66b2787Sitojun err(1, "writing to routing socket");
8043a06b6c9Sitojun }
8053a06b6c9Sitojun }
8063a06b6c9Sitojun do {
807fa8923c4Sjca l = read(rtsock, (char *)&m_rtmsg, sizeof(m_rtmsg));
80835b77220Sclaudio } while (l > 0 && (rtm->rtm_version != RTM_VERSION ||
80935b77220Sclaudio rtm->rtm_seq != seq || rtm->rtm_pid != pid));
810df69c215Sderaadt if (l == -1)
81123a16c1cSkn warn("read from routing socket");
8123a06b6c9Sitojun return (0);
8133a06b6c9Sitojun }
8143a06b6c9Sitojun
815f3b11628Smpi int
rtget(struct sockaddr_in6 ** sinp,struct sockaddr_dl ** sdlp)816f3b11628Smpi rtget(struct sockaddr_in6 **sinp, struct sockaddr_dl **sdlp)
817f3b11628Smpi {
818f3b11628Smpi struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
819f3b11628Smpi struct sockaddr_in6 *sin = NULL;
820f3b11628Smpi struct sockaddr_dl *sdl = NULL;
821f3b11628Smpi struct sockaddr *sa;
822f3b11628Smpi char *cp;
823cac6055cSmmcc unsigned int i;
824f3b11628Smpi
825f3b11628Smpi if (rtmsg(RTM_GET) < 0)
826f3b11628Smpi return (1);
827f3b11628Smpi
828f3b11628Smpi if (rtm->rtm_addrs) {
829f3b11628Smpi cp = ((char *)rtm + rtm->rtm_hdrlen);
830f3b11628Smpi for (i = 1; i; i <<= 1) {
831f3b11628Smpi if (i & rtm->rtm_addrs) {
832f3b11628Smpi sa = (struct sockaddr *)cp;
833f3b11628Smpi switch (i) {
834f3b11628Smpi case RTA_DST:
835f3b11628Smpi sin = (struct sockaddr_in6 *)sa;
836f3b11628Smpi break;
837f3b11628Smpi case RTA_IFP:
838f3b11628Smpi sdl = (struct sockaddr_dl *)sa;
839f3b11628Smpi break;
840f3b11628Smpi default:
841f3b11628Smpi break;
842f3b11628Smpi }
843fd4782b8Sbluhm ADVANCE(cp, sa);
844f3b11628Smpi }
845f3b11628Smpi }
846f3b11628Smpi }
847f3b11628Smpi
848f3b11628Smpi if (sin == NULL || sdl == NULL)
849f3b11628Smpi return (1);
850f3b11628Smpi
851f775b7d7Sflorian #ifdef __KAME__
852f775b7d7Sflorian {
853f775b7d7Sflorian static struct sockaddr_in6 ksin;
854f775b7d7Sflorian struct in6_addr *in6;
855f775b7d7Sflorian
856f775b7d7Sflorian /* do not damage the route message, we need it for delete */
857f775b7d7Sflorian ksin = *sin;
858f775b7d7Sflorian sin = &ksin;
859f775b7d7Sflorian in6 = &sin->sin6_addr;
860f775b7d7Sflorian
861f775b7d7Sflorian if ((IN6_IS_ADDR_LINKLOCAL(in6) ||
862f775b7d7Sflorian IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
863f775b7d7Sflorian IN6_IS_ADDR_MC_INTFACELOCAL(in6)) &&
864f775b7d7Sflorian sin->sin6_scope_id == 0) {
865f775b7d7Sflorian sin->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
866f775b7d7Sflorian &in6->s6_addr[2]);
867f775b7d7Sflorian *(u_short *)&in6->s6_addr[2] = 0;
868f775b7d7Sflorian }
869f775b7d7Sflorian }
870f775b7d7Sflorian #endif
871f3b11628Smpi *sinp = sin;
872f3b11628Smpi *sdlp = sdl;
873f3b11628Smpi
874f3b11628Smpi return (0);
875f3b11628Smpi }
876f3b11628Smpi
8773a06b6c9Sitojun void
ifinfo(const char * ifname)878b19e7985Skn ifinfo(const char *ifname)
8793a06b6c9Sitojun {
8803a06b6c9Sitojun struct in6_ndireq nd;
881e2a0fd57Skrw int s;
8823a06b6c9Sitojun
883df69c215Sderaadt if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
884e66b2787Sitojun err(1, "socket");
8853a06b6c9Sitojun }
8863a06b6c9Sitojun bzero(&nd, sizeof(nd));
8876de5763fSderaadt strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
888df69c215Sderaadt if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) == -1)
889e66b2787Sitojun err(1, "ioctl(SIOCGIFINFO_IN6)");
890d374aaacSitojun
891abcde9bcSkn printf("reachable=%ds\n", nd.ndi.reachable);
892d374aaacSitojun
8933a06b6c9Sitojun close(s);
8943a06b6c9Sitojun }
8953a06b6c9Sitojun
8963a06b6c9Sitojun static char *
sec2str(time_t total)897ae1436b2Sderaadt sec2str(time_t total)
8983a06b6c9Sitojun {
8993a06b6c9Sitojun static char result[256];
9003a06b6c9Sitojun int days, hours, mins, secs;
9013a06b6c9Sitojun int first = 1;
9023a06b6c9Sitojun char *p = result;
903fae7be87Sitojun char *ep = &result[sizeof(result)];
904fae7be87Sitojun int n;
9053a06b6c9Sitojun
9063a06b6c9Sitojun days = total / 3600 / 24;
9073a06b6c9Sitojun hours = (total / 3600) % 24;
9083a06b6c9Sitojun mins = (total / 60) % 60;
9093a06b6c9Sitojun secs = total % 60;
9103a06b6c9Sitojun
9113a06b6c9Sitojun if (days) {
9123a06b6c9Sitojun first = 0;
913fae7be87Sitojun n = snprintf(p, ep - p, "%dd", days);
914fae7be87Sitojun if (n < 0 || n >= ep - p)
915fae7be87Sitojun return "?";
916fae7be87Sitojun p += n;
9173a06b6c9Sitojun }
9183a06b6c9Sitojun if (!first || hours) {
9193a06b6c9Sitojun first = 0;
920fae7be87Sitojun n = snprintf(p, ep - p, "%dh", hours);
921fae7be87Sitojun if (n < 0 || n >= ep - p)
922fae7be87Sitojun return "?";
923fae7be87Sitojun p += n;
9243a06b6c9Sitojun }
9253a06b6c9Sitojun if (!first || mins) {
9263a06b6c9Sitojun first = 0;
927fae7be87Sitojun n = snprintf(p, ep - p, "%dm", mins);
928fae7be87Sitojun if (n < 0 || n >= ep - p)
929fae7be87Sitojun return "?";
930fae7be87Sitojun p += n;
9313a06b6c9Sitojun }
932fae7be87Sitojun snprintf(p, ep - p, "%ds", secs);
9333a06b6c9Sitojun
9343a06b6c9Sitojun return(result);
9353a06b6c9Sitojun }
936