ndp.c revision 1.8 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun /*
30 1.1 itojun * Copyright (c) 1984, 1993
31 1.1 itojun * The Regents of the University of California. All rights reserved.
32 1.1 itojun *
33 1.1 itojun * This code is derived from software contributed to Berkeley by
34 1.1 itojun * Sun Microsystems, Inc.
35 1.1 itojun *
36 1.1 itojun * Redistribution and use in source and binary forms, with or without
37 1.1 itojun * modification, are permitted provided that the following conditions
38 1.1 itojun * are met:
39 1.1 itojun * 1. Redistributions of source code must retain the above copyright
40 1.1 itojun * notice, this list of conditions and the following disclaimer.
41 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 itojun * notice, this list of conditions and the following disclaimer in the
43 1.1 itojun * documentation and/or other materials provided with the distribution.
44 1.1 itojun * 3. All advertising materials mentioning features or use of this software
45 1.1 itojun * must display the following acknowledgement:
46 1.1 itojun * This product includes software developed by the University of
47 1.1 itojun * California, Berkeley and its contributors.
48 1.1 itojun * 4. Neither the name of the University nor the names of its contributors
49 1.1 itojun * may be used to endorse or promote products derived from this software
50 1.1 itojun * without specific prior written permission.
51 1.1 itojun *
52 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 itojun * SUCH DAMAGE.
63 1.1 itojun */
64 1.1 itojun
65 1.1 itojun /*
66 1.1 itojun * Based on:
67 1.1 itojun * "@(#) Copyright (c) 1984, 1993\n\
68 1.1 itojun * The Regents of the University of California. All rights reserved.\n";
69 1.1 itojun *
70 1.1 itojun * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
71 1.1 itojun */
72 1.1 itojun
73 1.1 itojun /*
74 1.1 itojun * ndp - display, set, delete and flush neighbor cache
75 1.1 itojun */
76 1.1 itojun
77 1.1 itojun
78 1.1 itojun #include <sys/param.h>
79 1.1 itojun #include <sys/file.h>
80 1.1 itojun #include <sys/ioctl.h>
81 1.1 itojun #include <sys/socket.h>
82 1.1 itojun #include <sys/sysctl.h>
83 1.1 itojun #include <sys/time.h>
84 1.1 itojun
85 1.1 itojun #include <net/if.h>
86 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
87 1.1 itojun #include <net/if_var.h>
88 1.1 itojun #endif /* __FreeBSD__ >= 3 */
89 1.1 itojun #include <net/if_dl.h>
90 1.1 itojun #include <net/if_types.h>
91 1.1 itojun #include <net/route.h>
92 1.1 itojun
93 1.1 itojun #include <netinet/in.h>
94 1.1 itojun #ifndef __NetBSD__
95 1.1 itojun #include <netinet/if_ether.h>
96 1.1 itojun #endif
97 1.1 itojun
98 1.1 itojun #include <netinet/icmp6.h>
99 1.1 itojun #include <netinet6/in6_var.h>
100 1.1 itojun #include <netinet6/nd6.h>
101 1.1 itojun
102 1.1 itojun #include <arpa/inet.h>
103 1.1 itojun
104 1.1 itojun #include <netdb.h>
105 1.1 itojun #include <errno.h>
106 1.1 itojun #include <nlist.h>
107 1.1 itojun #include <stdio.h>
108 1.1 itojun #include <string.h>
109 1.1 itojun #include <paths.h>
110 1.1 itojun #include <err.h>
111 1.1 itojun #include <stdlib.h>
112 1.1 itojun #include <fcntl.h>
113 1.1 itojun #include <unistd.h>
114 1.1 itojun #include "gmt2local.h"
115 1.1 itojun
116 1.4 itojun #ifndef NI_WITHSCOPEID
117 1.4 itojun #define NI_WITHSCOPEID 0
118 1.4 itojun #endif
119 1.4 itojun
120 1.3 itojun /* packing rule for routing socket */
121 1.3 itojun #define ROUNDUP(a) \
122 1.3 itojun ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
123 1.3 itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
124 1.3 itojun
125 1.1 itojun static int pid;
126 1.1 itojun static int fflag;
127 1.1 itojun static int nflag;
128 1.1 itojun static int tflag;
129 1.1 itojun static int32_t thiszone; /* time difference with gmt */
130 1.1 itojun static int s = -1;
131 1.1 itojun static int repeat = 0;
132 1.4 itojun static int lflag = 0;
133 1.1 itojun
134 1.1 itojun char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
135 1.4 itojun char host_buf[NI_MAXHOST]; /* getnameinfo() */
136 1.1 itojun char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
137 1.1 itojun
138 1.1 itojun int main __P((int, char **));
139 1.1 itojun int file __P((char *));
140 1.1 itojun void getsocket __P((void));
141 1.1 itojun int set __P((int, char **));
142 1.1 itojun void get __P((char *));
143 1.1 itojun int delete __P((char *));
144 1.1 itojun void dump __P((struct in6_addr *));
145 1.4 itojun static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
146 1.4 itojun int ifindex, int));
147 1.1 itojun static char *ether_str __P((struct sockaddr_dl *));
148 1.1 itojun int ndp_ether_aton __P((char *, u_char *));
149 1.1 itojun void usage __P((void));
150 1.1 itojun int rtmsg __P((int));
151 1.8 itojun void ifinfo __P((int, char **));
152 1.1 itojun void rtrlist __P((void));
153 1.1 itojun void plist __P((void));
154 1.1 itojun void pfx_flush __P((void));
155 1.1 itojun void rtr_flush __P((void));
156 1.1 itojun void harmonize_rtr __P((void));
157 1.4 itojun #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
158 1.4 itojun static void getdefif __P((void));
159 1.4 itojun static void setdefif __P((char *));
160 1.4 itojun #endif
161 1.1 itojun static char *sec2str __P((time_t t));
162 1.1 itojun static char *ether_str __P((struct sockaddr_dl *sdl));
163 1.1 itojun static void ts_print __P((const struct timeval *));
164 1.1 itojun
165 1.1 itojun int
166 1.1 itojun main(argc, argv)
167 1.1 itojun int argc;
168 1.1 itojun char **argv;
169 1.1 itojun {
170 1.1 itojun int ch;
171 1.1 itojun int aflag = 0, cflag = 0, dflag = 0, sflag = 0, Hflag = 0,
172 1.1 itojun pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
173 1.1 itojun
174 1.1 itojun pid = getpid();
175 1.1 itojun thiszone = gmt2local(0);
176 1.4 itojun while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != EOF)
177 1.1 itojun switch ((char)ch) {
178 1.1 itojun case 'a':
179 1.1 itojun aflag = 1;
180 1.1 itojun break;
181 1.1 itojun case 'c':
182 1.1 itojun fflag = 1;
183 1.1 itojun cflag = 1;
184 1.1 itojun break;
185 1.1 itojun case 'd':
186 1.1 itojun dflag = 1;
187 1.1 itojun break;
188 1.4 itojun case 'I':
189 1.4 itojun #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
190 1.4 itojun if (argc > 2)
191 1.4 itojun setdefif(argv[2]);
192 1.4 itojun getdefif(); /* always call it to print the result */
193 1.4 itojun exit(0);
194 1.4 itojun #else
195 1.4 itojun errx(1, "not supported yet");
196 1.4 itojun /*NOTREACHED*/
197 1.4 itojun #endif
198 1.1 itojun case 'i' :
199 1.8 itojun argc -= optind;
200 1.8 itojun argv += optind;
201 1.8 itojun if (argc < 1)
202 1.1 itojun usage();
203 1.8 itojun ifinfo(argc, argv);
204 1.1 itojun exit(0);
205 1.1 itojun case 'n':
206 1.1 itojun nflag = 1;
207 1.1 itojun continue;
208 1.1 itojun case 'p':
209 1.1 itojun pflag = 1;
210 1.1 itojun break;
211 1.1 itojun case 'f' :
212 1.1 itojun if (argc != 3)
213 1.1 itojun usage();
214 1.1 itojun file(argv[2]);
215 1.1 itojun exit(0);
216 1.4 itojun case 'l' :
217 1.4 itojun lflag = 1;
218 1.4 itojun break;
219 1.1 itojun case 'r' :
220 1.1 itojun rflag = 1;
221 1.1 itojun break;
222 1.1 itojun case 's':
223 1.1 itojun sflag = 1;
224 1.1 itojun break;
225 1.1 itojun case 't':
226 1.1 itojun tflag = 1;
227 1.1 itojun break;
228 1.1 itojun case 'A':
229 1.1 itojun aflag = 1;
230 1.1 itojun repeat = atoi(optarg);
231 1.1 itojun if (repeat < 0)
232 1.1 itojun usage();
233 1.1 itojun break;
234 1.1 itojun case 'H' :
235 1.1 itojun Hflag = 1;
236 1.1 itojun break;
237 1.1 itojun case 'P':
238 1.1 itojun Pflag = 1;
239 1.1 itojun break;
240 1.1 itojun case 'R':
241 1.1 itojun Rflag = 1;
242 1.1 itojun break;
243 1.1 itojun default:
244 1.1 itojun usage();
245 1.1 itojun }
246 1.1 itojun
247 1.1 itojun argc -= optind;
248 1.1 itojun argv += optind;
249 1.1 itojun
250 1.1 itojun if (aflag || cflag) {
251 1.1 itojun dump(0);
252 1.1 itojun exit(0);
253 1.1 itojun }
254 1.1 itojun if (dflag) {
255 1.1 itojun if (argc != 1)
256 1.1 itojun usage();
257 1.1 itojun delete(argv[0]);
258 1.1 itojun }
259 1.1 itojun if (pflag) {
260 1.1 itojun plist();
261 1.1 itojun exit(0);
262 1.1 itojun }
263 1.1 itojun if (rflag) {
264 1.1 itojun rtrlist();
265 1.1 itojun exit(0);
266 1.1 itojun }
267 1.1 itojun if (sflag) {
268 1.1 itojun if (argc < 2 || argc > 4)
269 1.1 itojun usage();
270 1.1 itojun exit(set(argc, argv) ? 1 : 0);
271 1.1 itojun }
272 1.1 itojun if (Hflag) {
273 1.1 itojun harmonize_rtr();
274 1.1 itojun exit(0);
275 1.1 itojun }
276 1.1 itojun if (Pflag) {
277 1.1 itojun pfx_flush();
278 1.1 itojun exit(0);
279 1.1 itojun }
280 1.1 itojun if (Rflag) {
281 1.1 itojun rtr_flush();
282 1.1 itojun exit(0);
283 1.1 itojun }
284 1.1 itojun
285 1.1 itojun if (argc != 1)
286 1.1 itojun usage();
287 1.1 itojun get(argv[0]);
288 1.1 itojun exit(0);
289 1.1 itojun }
290 1.1 itojun
291 1.1 itojun /*
292 1.1 itojun * Process a file to set standard ndp entries
293 1.1 itojun */
294 1.1 itojun int
295 1.1 itojun file(name)
296 1.1 itojun char *name;
297 1.1 itojun {
298 1.1 itojun FILE *fp;
299 1.1 itojun int i, retval;
300 1.1 itojun char line[100], arg[5][50], *args[5];
301 1.1 itojun
302 1.1 itojun if ((fp = fopen(name, "r")) == NULL) {
303 1.1 itojun fprintf(stderr, "ndp: cannot open %s\n", name);
304 1.1 itojun exit(1);
305 1.1 itojun }
306 1.1 itojun args[0] = &arg[0][0];
307 1.1 itojun args[1] = &arg[1][0];
308 1.1 itojun args[2] = &arg[2][0];
309 1.1 itojun args[3] = &arg[3][0];
310 1.1 itojun args[4] = &arg[4][0];
311 1.1 itojun retval = 0;
312 1.1 itojun while(fgets(line, 100, fp) != NULL) {
313 1.1 itojun i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
314 1.1 itojun arg[3], arg[4]);
315 1.1 itojun if (i < 2) {
316 1.1 itojun fprintf(stderr, "ndp: bad line: %s\n", line);
317 1.1 itojun retval = 1;
318 1.1 itojun continue;
319 1.1 itojun }
320 1.1 itojun if (set(i, args))
321 1.1 itojun retval = 1;
322 1.1 itojun }
323 1.1 itojun fclose(fp);
324 1.1 itojun return (retval);
325 1.1 itojun }
326 1.1 itojun
327 1.1 itojun void
328 1.1 itojun getsocket()
329 1.1 itojun {
330 1.1 itojun if (s < 0) {
331 1.1 itojun s = socket(PF_ROUTE, SOCK_RAW, 0);
332 1.1 itojun if (s < 0) {
333 1.1 itojun perror("ndp: socket");
334 1.1 itojun exit(1);
335 1.1 itojun }
336 1.1 itojun }
337 1.1 itojun }
338 1.1 itojun
339 1.6 itojun struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
340 1.1 itojun struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
341 1.1 itojun struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
342 1.1 itojun int expire_time, flags, found_entry;
343 1.1 itojun struct {
344 1.1 itojun struct rt_msghdr m_rtm;
345 1.1 itojun char m_space[512];
346 1.1 itojun } m_rtmsg;
347 1.1 itojun
348 1.1 itojun /*
349 1.1 itojun * Set an individual neighbor cache entry
350 1.1 itojun */
351 1.1 itojun int
352 1.1 itojun set(argc, argv)
353 1.1 itojun int argc;
354 1.1 itojun char **argv;
355 1.1 itojun {
356 1.1 itojun register struct sockaddr_in6 *sin = &sin_m;
357 1.1 itojun register struct sockaddr_dl *sdl;
358 1.1 itojun register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
359 1.1 itojun struct addrinfo hints, *res;
360 1.1 itojun int gai_error;
361 1.1 itojun u_char *ea;
362 1.1 itojun char *host = argv[0], *eaddr = argv[1];
363 1.1 itojun
364 1.1 itojun getsocket();
365 1.1 itojun argc -= 2;
366 1.1 itojun argv += 2;
367 1.1 itojun sdl_m = blank_sdl;
368 1.1 itojun sin_m = blank_sin;
369 1.1 itojun
370 1.1 itojun bzero(&hints, sizeof(hints));
371 1.1 itojun hints.ai_family = AF_INET6;
372 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
373 1.1 itojun if (gai_error) {
374 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
375 1.1 itojun gai_strerror(gai_error));
376 1.1 itojun return 1;
377 1.1 itojun }
378 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
379 1.1 itojun ea = (u_char *)LLADDR(&sdl_m);
380 1.1 itojun if (ndp_ether_aton(eaddr, ea) == 0)
381 1.1 itojun sdl_m.sdl_alen = 6;
382 1.1 itojun flags = expire_time = 0;
383 1.1 itojun while (argc-- > 0) {
384 1.1 itojun if (strncmp(argv[0], "temp", 4) == 0) {
385 1.1 itojun struct timeval time;
386 1.1 itojun gettimeofday(&time, 0);
387 1.1 itojun expire_time = time.tv_sec + 20 * 60;
388 1.6 itojun } else if (strncmp(argv[0], "proxy", 5) == 0)
389 1.6 itojun flags |= RTF_ANNOUNCE;
390 1.1 itojun argv++;
391 1.1 itojun }
392 1.1 itojun tryagain:
393 1.1 itojun if (rtmsg(RTM_GET) < 0) {
394 1.1 itojun perror(host);
395 1.1 itojun return (1);
396 1.1 itojun }
397 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
398 1.3 itojun sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
399 1.1 itojun if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
400 1.1 itojun if (sdl->sdl_family == AF_LINK &&
401 1.1 itojun (rtm->rtm_flags & RTF_LLINFO) &&
402 1.1 itojun !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
403 1.1 itojun case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
404 1.1 itojun case IFT_ISO88024: case IFT_ISO88025:
405 1.1 itojun goto overwrite;
406 1.1 itojun }
407 1.1 itojun goto tryagain;
408 1.1 itojun }
409 1.1 itojun overwrite:
410 1.1 itojun if (sdl->sdl_family != AF_LINK) {
411 1.1 itojun printf("cannot intuit interface index and type for %s\n", host);
412 1.1 itojun return (1);
413 1.1 itojun }
414 1.1 itojun sdl_m.sdl_type = sdl->sdl_type;
415 1.1 itojun sdl_m.sdl_index = sdl->sdl_index;
416 1.1 itojun return (rtmsg(RTM_ADD));
417 1.1 itojun }
418 1.1 itojun
419 1.1 itojun /*
420 1.1 itojun * Display an individual neighbor cache entry
421 1.1 itojun */
422 1.1 itojun void
423 1.1 itojun get(host)
424 1.1 itojun char *host;
425 1.1 itojun {
426 1.1 itojun struct sockaddr_in6 *sin = &sin_m;
427 1.1 itojun struct addrinfo hints, *res;
428 1.1 itojun int gai_error;
429 1.1 itojun
430 1.1 itojun sin_m = blank_sin;
431 1.1 itojun bzero(&hints, sizeof(hints));
432 1.1 itojun hints.ai_family = AF_INET6;
433 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
434 1.1 itojun if (gai_error) {
435 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
436 1.1 itojun gai_strerror(gai_error));
437 1.1 itojun return;
438 1.1 itojun }
439 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
440 1.1 itojun dump(&sin->sin6_addr);
441 1.1 itojun if (found_entry == 0) {
442 1.4 itojun getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
443 1.4 itojun sizeof(host_buf), NULL ,0,
444 1.4 itojun NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
445 1.4 itojun printf("%s (%s) -- no entry\n", host, host_buf);
446 1.1 itojun exit(1);
447 1.1 itojun }
448 1.1 itojun }
449 1.1 itojun
450 1.1 itojun /*
451 1.1 itojun * Delete a neighbor cache entry
452 1.1 itojun */
453 1.1 itojun int
454 1.1 itojun delete(host)
455 1.1 itojun char *host;
456 1.1 itojun {
457 1.4 itojun struct sockaddr_in6 *sin = &sin_m;
458 1.1 itojun register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
459 1.1 itojun struct sockaddr_dl *sdl;
460 1.1 itojun struct addrinfo hints, *res;
461 1.1 itojun int gai_error;
462 1.1 itojun
463 1.1 itojun getsocket();
464 1.1 itojun sin_m = blank_sin;
465 1.1 itojun
466 1.1 itojun bzero(&hints, sizeof(hints));
467 1.1 itojun hints.ai_family = AF_INET6;
468 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
469 1.1 itojun if (gai_error) {
470 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
471 1.1 itojun gai_strerror(gai_error));
472 1.1 itojun return 1;
473 1.1 itojun }
474 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
475 1.1 itojun /*tryagain:*/
476 1.1 itojun if (rtmsg(RTM_GET) < 0) {
477 1.1 itojun perror(host);
478 1.1 itojun return (1);
479 1.1 itojun }
480 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
481 1.3 itojun sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
482 1.1 itojun if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
483 1.1 itojun if (sdl->sdl_family == AF_LINK &&
484 1.1 itojun (rtm->rtm_flags & RTF_LLINFO) &&
485 1.8 itojun !(rtm->rtm_flags & RTF_GATEWAY)) {
486 1.8 itojun switch (sdl->sdl_type) {
487 1.8 itojun case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
488 1.8 itojun case IFT_ISO88024: case IFT_ISO88025:
489 1.8 itojun goto delete;
490 1.8 itojun }
491 1.1 itojun }
492 1.1 itojun }
493 1.8 itojun return 0;
494 1.8 itojun
495 1.1 itojun delete:
496 1.1 itojun if (sdl->sdl_family != AF_LINK) {
497 1.1 itojun printf("cannot locate %s\n", host);
498 1.1 itojun return (1);
499 1.1 itojun }
500 1.4 itojun if (rtmsg(RTM_DELETE) == 0) {
501 1.4 itojun getnameinfo((struct sockaddr *)sin,
502 1.4 itojun sin->sin6_len, host_buf,
503 1.4 itojun sizeof(host_buf), NULL, 0,
504 1.4 itojun NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
505 1.4 itojun printf("%s (%s) deleted\n", host, host_buf);
506 1.4 itojun }
507 1.4 itojun
508 1.1 itojun return 0;
509 1.1 itojun }
510 1.1 itojun
511 1.1 itojun /*
512 1.1 itojun * Dump the entire neighbor cache
513 1.1 itojun */
514 1.1 itojun void
515 1.1 itojun dump(addr)
516 1.1 itojun struct in6_addr *addr;
517 1.1 itojun {
518 1.1 itojun int mib[6];
519 1.1 itojun size_t needed;
520 1.4 itojun char *lim, *buf, *next;
521 1.1 itojun struct rt_msghdr *rtm;
522 1.1 itojun struct sockaddr_in6 *sin;
523 1.1 itojun struct sockaddr_dl *sdl;
524 1.1 itojun struct in6_nbrinfo *nbi;
525 1.1 itojun struct timeval time;
526 1.4 itojun int addrwidth;
527 1.4 itojun char flgbuf[8];
528 1.1 itojun
529 1.1 itojun /* Print header */
530 1.1 itojun if (!tflag)
531 1.4 itojun printf("%-31.31s %-17.17s %6.6s %-9.9s %2s %4s %4s\n",
532 1.1 itojun "Neighbor", "Linklayer Address", "Netif", "Expire",
533 1.1 itojun "St", "Flgs", "Prbs");
534 1.1 itojun
535 1.1 itojun again:;
536 1.1 itojun mib[0] = CTL_NET;
537 1.1 itojun mib[1] = PF_ROUTE;
538 1.1 itojun mib[2] = 0;
539 1.1 itojun mib[3] = AF_INET6;
540 1.1 itojun mib[4] = NET_RT_FLAGS;
541 1.1 itojun mib[5] = RTF_LLINFO;
542 1.1 itojun if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
543 1.3 itojun err(1, "sysctl(PF_ROUTE estimate)");
544 1.3 itojun if (needed > 0) {
545 1.3 itojun if ((buf = malloc(needed)) == NULL)
546 1.3 itojun errx(1, "malloc");
547 1.3 itojun if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
548 1.3 itojun err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
549 1.3 itojun lim = buf + needed;
550 1.3 itojun } else
551 1.3 itojun buf = lim = NULL;
552 1.1 itojun
553 1.3 itojun for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
554 1.1 itojun int isrouter = 0, prbs = 0;
555 1.1 itojun
556 1.1 itojun rtm = (struct rt_msghdr *)next;
557 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
558 1.3 itojun sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
559 1.1 itojun if (addr) {
560 1.1 itojun if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
561 1.1 itojun continue;
562 1.1 itojun found_entry = 1;
563 1.1 itojun } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
564 1.1 itojun continue;
565 1.1 itojun if (fflag == 1) {
566 1.1 itojun delete((char *)inet_ntop(AF_INET6, &sin->sin6_addr,
567 1.4 itojun ntop_buf, sizeof(ntop_buf)));
568 1.1 itojun continue;
569 1.1 itojun }
570 1.4 itojun
571 1.4 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
572 1.4 itojun IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
573 1.4 itojun /* XXX: should scope id be filled in the kernel? */
574 1.4 itojun if (sin->sin6_scope_id == 0)
575 1.4 itojun sin->sin6_scope_id = sdl->sdl_index;
576 1.4 itojun
577 1.4 itojun /* XXX: KAME specific hack; removed the embedded id */
578 1.4 itojun *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
579 1.1 itojun }
580 1.4 itojun getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
581 1.4 itojun sizeof(host_buf), NULL, 0,
582 1.4 itojun NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
583 1.1 itojun gettimeofday(&time, 0);
584 1.1 itojun if (tflag)
585 1.1 itojun ts_print(&time);
586 1.1 itojun
587 1.4 itojun if (lflag) {
588 1.4 itojun addrwidth = strlen(host_buf);
589 1.4 itojun if (addrwidth < 31)
590 1.4 itojun addrwidth = 31;
591 1.4 itojun } else
592 1.4 itojun addrwidth = 31;
593 1.4 itojun
594 1.4 itojun printf("%-*.*s %-17.17s %6.6s", addrwidth, addrwidth, host_buf,
595 1.1 itojun ether_str(sdl),
596 1.1 itojun if_indextoname(sdl->sdl_index, ifix_buf));
597 1.1 itojun
598 1.1 itojun /* Print neighbor discovery specific informations */
599 1.4 itojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
600 1.1 itojun if (nbi) {
601 1.1 itojun if (nbi->expire > time.tv_sec) {
602 1.1 itojun printf(" %-9.9s",
603 1.1 itojun sec2str(nbi->expire - time.tv_sec));
604 1.1 itojun }
605 1.1 itojun else if (nbi->expire == 0)
606 1.1 itojun printf(" %-9.9s", "permanent");
607 1.1 itojun else
608 1.1 itojun printf(" %-9.9s", "expired");
609 1.1 itojun
610 1.1 itojun switch(nbi->state) {
611 1.1 itojun case ND6_LLINFO_NOSTATE:
612 1.1 itojun printf(" N");
613 1.1 itojun break;
614 1.1 itojun case ND6_LLINFO_WAITDELETE:
615 1.1 itojun printf(" W");
616 1.1 itojun break;
617 1.1 itojun case ND6_LLINFO_INCOMPLETE:
618 1.1 itojun printf(" I");
619 1.1 itojun break;
620 1.1 itojun case ND6_LLINFO_REACHABLE:
621 1.1 itojun printf(" R");
622 1.1 itojun break;
623 1.1 itojun case ND6_LLINFO_STALE:
624 1.1 itojun printf(" S");
625 1.1 itojun break;
626 1.1 itojun case ND6_LLINFO_DELAY:
627 1.1 itojun printf(" D");
628 1.1 itojun break;
629 1.1 itojun case ND6_LLINFO_PROBE:
630 1.1 itojun printf(" P");
631 1.1 itojun break;
632 1.1 itojun default:
633 1.1 itojun printf(" ?");
634 1.1 itojun break;
635 1.1 itojun }
636 1.1 itojun
637 1.1 itojun isrouter = nbi->isrouter;
638 1.1 itojun prbs = nbi->asked;
639 1.1 itojun }
640 1.1 itojun else {
641 1.1 itojun warnx("failed to get neighbor information");
642 1.1 itojun printf(" ");
643 1.1 itojun }
644 1.1 itojun putchar(' ');
645 1.1 itojun
646 1.4 itojun /*
647 1.4 itojun * other flags. R: router, P: proxy, W: ??
648 1.4 itojun */
649 1.4 itojun if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
650 1.6 itojun snprintf(flgbuf, sizeof(flgbuf), "%s%s",
651 1.6 itojun isrouter ? "R" : "",
652 1.6 itojun (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
653 1.4 itojun } else {
654 1.4 itojun sin = (struct sockaddr_in6 *)
655 1.4 itojun (sdl->sdl_len + (char *)sdl);
656 1.6 itojun snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
657 1.4 itojun isrouter ? "R" : "",
658 1.4 itojun !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
659 1.4 itojun ? "P" : "",
660 1.4 itojun (sin->sin6_len != sizeof(struct sockaddr_in6))
661 1.6 itojun ? "W" : "",
662 1.6 itojun (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
663 1.1 itojun }
664 1.4 itojun printf(" %-4.4s", flgbuf);
665 1.1 itojun
666 1.1 itojun if (prbs)
667 1.4 itojun printf(" %4d", prbs);
668 1.1 itojun
669 1.1 itojun printf("\n");
670 1.1 itojun }
671 1.1 itojun
672 1.1 itojun if (repeat) {
673 1.1 itojun printf("\n");
674 1.1 itojun sleep(repeat);
675 1.1 itojun goto again;
676 1.1 itojun }
677 1.1 itojun }
678 1.1 itojun
679 1.1 itojun static struct in6_nbrinfo *
680 1.4 itojun getnbrinfo(addr, ifindex, warning)
681 1.1 itojun struct in6_addr *addr;
682 1.1 itojun int ifindex;
683 1.4 itojun int warning;
684 1.1 itojun {
685 1.1 itojun static struct in6_nbrinfo nbi;
686 1.1 itojun int s;
687 1.1 itojun
688 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
689 1.1 itojun err(1, "socket");
690 1.1 itojun
691 1.1 itojun bzero(&nbi, sizeof(nbi));
692 1.1 itojun if_indextoname(ifindex, nbi.ifname);
693 1.1 itojun nbi.addr = *addr;
694 1.1 itojun if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
695 1.4 itojun if (warning)
696 1.8 itojun warn("ioctl(SIOCGNBRINFO_IN6)");
697 1.1 itojun close(s);
698 1.1 itojun return(NULL);
699 1.1 itojun }
700 1.1 itojun
701 1.1 itojun close(s);
702 1.1 itojun return(&nbi);
703 1.1 itojun }
704 1.1 itojun
705 1.1 itojun static char *
706 1.1 itojun ether_str(sdl)
707 1.1 itojun struct sockaddr_dl *sdl;
708 1.1 itojun {
709 1.1 itojun static char ebuf[32];
710 1.1 itojun u_char *cp;
711 1.1 itojun
712 1.1 itojun if (sdl->sdl_alen) {
713 1.1 itojun cp = (u_char *)LLADDR(sdl);
714 1.1 itojun sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
715 1.1 itojun cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
716 1.1 itojun }
717 1.1 itojun else {
718 1.1 itojun sprintf(ebuf, "(incomplete)");
719 1.1 itojun }
720 1.1 itojun
721 1.1 itojun return(ebuf);
722 1.1 itojun }
723 1.1 itojun
724 1.1 itojun int
725 1.1 itojun ndp_ether_aton(a, n)
726 1.1 itojun char *a;
727 1.1 itojun u_char *n;
728 1.1 itojun {
729 1.1 itojun int i, o[6];
730 1.1 itojun
731 1.1 itojun i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
732 1.1 itojun &o[3], &o[4], &o[5]);
733 1.1 itojun if (i != 6) {
734 1.1 itojun fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
735 1.1 itojun return (1);
736 1.1 itojun }
737 1.1 itojun for (i=0; i<6; i++)
738 1.1 itojun n[i] = o[i];
739 1.1 itojun return (0);
740 1.1 itojun }
741 1.1 itojun
742 1.1 itojun void
743 1.1 itojun usage()
744 1.1 itojun {
745 1.1 itojun printf("usage: ndp hostname\n");
746 1.4 itojun printf(" ndp -a[ntl]\n");
747 1.4 itojun printf(" ndp [-ntl] -A wait\n");
748 1.1 itojun printf(" ndp -c[nt]\n");
749 1.1 itojun printf(" ndp -d[nt] hostname\n");
750 1.1 itojun printf(" ndp -f[nt] filename\n");
751 1.8 itojun printf(" ndp -i interface [flags...]\n");
752 1.4 itojun #ifdef SIOCSDEFIFACE_IN6
753 1.4 itojun printf(" ndp -I [interface|delete]\n");
754 1.4 itojun #endif
755 1.1 itojun printf(" ndp -p\n");
756 1.1 itojun printf(" ndp -r\n");
757 1.6 itojun printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
758 1.1 itojun printf(" ndp -H\n");
759 1.1 itojun printf(" ndp -P\n");
760 1.1 itojun printf(" ndp -R\n");
761 1.1 itojun exit(1);
762 1.1 itojun }
763 1.1 itojun
764 1.1 itojun int
765 1.1 itojun rtmsg(cmd)
766 1.1 itojun int cmd;
767 1.1 itojun {
768 1.1 itojun static int seq;
769 1.1 itojun int rlen;
770 1.1 itojun register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
771 1.1 itojun register char *cp = m_rtmsg.m_space;
772 1.1 itojun register int l;
773 1.1 itojun
774 1.1 itojun errno = 0;
775 1.1 itojun if (cmd == RTM_DELETE)
776 1.1 itojun goto doit;
777 1.1 itojun bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
778 1.1 itojun rtm->rtm_flags = flags;
779 1.1 itojun rtm->rtm_version = RTM_VERSION;
780 1.1 itojun
781 1.1 itojun switch (cmd) {
782 1.1 itojun default:
783 1.1 itojun fprintf(stderr, "ndp: internal wrong cmd\n");
784 1.1 itojun exit(1);
785 1.1 itojun case RTM_ADD:
786 1.1 itojun rtm->rtm_addrs |= RTA_GATEWAY;
787 1.1 itojun rtm->rtm_rmx.rmx_expire = expire_time;
788 1.1 itojun rtm->rtm_inits = RTV_EXPIRE;
789 1.1 itojun rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
790 1.6 itojun if (rtm->rtm_flags & RTF_ANNOUNCE) {
791 1.6 itojun rtm->rtm_flags &= ~RTF_HOST;
792 1.6 itojun rtm->rtm_flags |= RTA_NETMASK;
793 1.6 itojun }
794 1.1 itojun /* FALLTHROUGH */
795 1.1 itojun case RTM_GET:
796 1.1 itojun rtm->rtm_addrs |= RTA_DST;
797 1.1 itojun }
798 1.1 itojun #define NEXTADDR(w, s) \
799 1.1 itojun if (rtm->rtm_addrs & (w)) { \
800 1.1 itojun bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
801 1.1 itojun
802 1.1 itojun NEXTADDR(RTA_DST, sin_m);
803 1.1 itojun NEXTADDR(RTA_GATEWAY, sdl_m);
804 1.6 itojun memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
805 1.1 itojun NEXTADDR(RTA_NETMASK, so_mask);
806 1.1 itojun
807 1.1 itojun rtm->rtm_msglen = cp - (char *)&m_rtmsg;
808 1.1 itojun doit:
809 1.1 itojun l = rtm->rtm_msglen;
810 1.1 itojun rtm->rtm_seq = ++seq;
811 1.1 itojun rtm->rtm_type = cmd;
812 1.1 itojun if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
813 1.1 itojun if (errno != ESRCH || cmd != RTM_DELETE) {
814 1.1 itojun perror("writing to routing socket");
815 1.1 itojun return (-1);
816 1.1 itojun }
817 1.1 itojun }
818 1.1 itojun do {
819 1.1 itojun l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
820 1.1 itojun } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
821 1.1 itojun if (l < 0)
822 1.1 itojun (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
823 1.1 itojun strerror(errno));
824 1.1 itojun return (0);
825 1.1 itojun }
826 1.1 itojun
827 1.1 itojun void
828 1.8 itojun ifinfo(argc, argv)
829 1.8 itojun int argc;
830 1.8 itojun char **argv;
831 1.1 itojun {
832 1.1 itojun struct in6_ndireq nd;
833 1.8 itojun int i, s;
834 1.8 itojun char *ifname = argv[0];
835 1.8 itojun u_int32_t newflags;
836 1.1 itojun
837 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
838 1.1 itojun perror("ndp: socket");
839 1.1 itojun exit(1);
840 1.1 itojun }
841 1.1 itojun bzero(&nd, sizeof(nd));
842 1.1 itojun strcpy(nd.ifname, ifname);
843 1.1 itojun if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
844 1.1 itojun perror("ioctl (SIOCGIFINFO_IN6)");
845 1.1 itojun exit(1);
846 1.1 itojun }
847 1.1 itojun #define ND nd.ndi
848 1.8 itojun newflags = ND.flags;
849 1.8 itojun for (i = 1; i < argc; i++) {
850 1.8 itojun int clear = 0;
851 1.8 itojun char *cp = argv[i];
852 1.8 itojun
853 1.8 itojun if (*cp == '-') {
854 1.8 itojun clear = 1;
855 1.8 itojun cp++;
856 1.8 itojun }
857 1.8 itojun
858 1.8 itojun #define SETFLAG(s, f) \
859 1.8 itojun do {\
860 1.8 itojun if (strcmp(cp, (s)) == 0) {\
861 1.8 itojun if (clear)\
862 1.8 itojun newflags &= ~(f);\
863 1.8 itojun else\
864 1.8 itojun newflags |= (f);\
865 1.8 itojun }\
866 1.8 itojun } while (0)
867 1.8 itojun SETFLAG("nud", ND6_IFF_PERFORMNUD);
868 1.8 itojun
869 1.8 itojun ND.flags = newflags;
870 1.8 itojun if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
871 1.8 itojun perror("ioctl(SIOCSIFINFO_FLAGS)");
872 1.8 itojun exit(1);
873 1.8 itojun }
874 1.8 itojun #undef SETFLAG
875 1.8 itojun }
876 1.8 itojun
877 1.1 itojun printf("linkmtu=%d", ND.linkmtu);
878 1.1 itojun printf(", curhlim=%d", ND.chlim);
879 1.1 itojun printf(", basereachable=%ds%dms",
880 1.1 itojun ND.basereachable / 1000, ND.basereachable % 1000);
881 1.1 itojun printf(", reachable=%ds", ND.reachable);
882 1.8 itojun printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
883 1.8 itojun if (ND.flags) {
884 1.8 itojun printf("\nFlags: ");
885 1.8 itojun if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
886 1.8 itojun printf("PERFORMNUD ");
887 1.8 itojun }
888 1.8 itojun putc('\n', stdout);
889 1.1 itojun #undef ND
890 1.8 itojun
891 1.1 itojun close(s);
892 1.1 itojun }
893 1.1 itojun
894 1.1 itojun void
895 1.1 itojun rtrlist()
896 1.1 itojun {
897 1.1 itojun struct in6_drlist dr;
898 1.1 itojun int s, i;
899 1.1 itojun struct timeval time;
900 1.1 itojun
901 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
902 1.1 itojun perror("ndp: socket");
903 1.1 itojun exit(1);
904 1.1 itojun }
905 1.1 itojun bzero(&dr, sizeof(dr));
906 1.1 itojun strcpy(dr.ifname, "lo0"); /* dummy */
907 1.1 itojun if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
908 1.1 itojun perror("ioctl (SIOCGDRLST_IN6)");
909 1.1 itojun exit(1);
910 1.1 itojun }
911 1.1 itojun #define DR dr.defrouter[i]
912 1.1 itojun for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
913 1.4 itojun struct sockaddr_in6 sin6;
914 1.4 itojun
915 1.4 itojun bzero(&sin6, sizeof(sin6));
916 1.4 itojun sin6.sin6_family = AF_INET6;
917 1.4 itojun sin6.sin6_len = sizeof(sin6);
918 1.4 itojun sin6.sin6_addr = DR.rtaddr;
919 1.4 itojun getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
920 1.4 itojun sizeof(host_buf), NULL, 0,
921 1.4 itojun NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
922 1.4 itojun
923 1.4 itojun printf("%s if=%s", host_buf,
924 1.1 itojun if_indextoname(DR.if_index, ifix_buf));
925 1.1 itojun printf(", flags=%s%s",
926 1.1 itojun DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
927 1.1 itojun DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
928 1.1 itojun gettimeofday(&time, 0);
929 1.1 itojun if (DR.expire == 0)
930 1.1 itojun printf(", expire=Never\n");
931 1.1 itojun else
932 1.1 itojun printf(", expire=%s\n",
933 1.1 itojun sec2str(DR.expire - time.tv_sec));
934 1.1 itojun }
935 1.1 itojun #undef DR
936 1.1 itojun close(s);
937 1.1 itojun }
938 1.1 itojun
939 1.1 itojun void
940 1.1 itojun plist()
941 1.1 itojun {
942 1.1 itojun struct in6_prlist pr;
943 1.1 itojun int s, i;
944 1.1 itojun struct timeval time;
945 1.1 itojun
946 1.1 itojun gettimeofday(&time, 0);
947 1.1 itojun
948 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
949 1.1 itojun perror("ndp: socket");
950 1.1 itojun exit(1);
951 1.1 itojun }
952 1.1 itojun bzero(&pr, sizeof(pr));
953 1.1 itojun strcpy(pr.ifname, "lo0"); /* dummy */
954 1.1 itojun if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
955 1.1 itojun perror("ioctl (SIOCGPRLST_IN6)");
956 1.1 itojun exit(1);
957 1.1 itojun }
958 1.1 itojun #define PR pr.prefix[i]
959 1.1 itojun for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
960 1.1 itojun printf("%s/%d if=%s\n",
961 1.1 itojun inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
962 1.1 itojun sizeof(ntop_buf)), PR.prefixlen,
963 1.1 itojun if_indextoname(PR.if_index, ifix_buf));
964 1.1 itojun gettimeofday(&time, 0);
965 1.6 itojun /*
966 1.6 itojun * meaning of fields, especially flags, is very different
967 1.6 itojun * by origin. notify the difference to the users.
968 1.6 itojun */
969 1.6 itojun printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: ");
970 1.6 itojun printf("flags=%s%s",
971 1.1 itojun PR.raflags.onlink ? "L" : "",
972 1.1 itojun PR.raflags.autonomous ? "A" : "");
973 1.1 itojun if (PR.vltime == ND6_INFINITE_LIFETIME)
974 1.1 itojun printf(" vltime=infinity");
975 1.1 itojun else
976 1.1 itojun printf(" vltime=%ld", (long)PR.vltime);
977 1.1 itojun if (PR.pltime == ND6_INFINITE_LIFETIME)
978 1.1 itojun printf(", pltime=infinity");
979 1.1 itojun else
980 1.1 itojun printf(", pltime=%ld", (long)PR.pltime);
981 1.1 itojun if (PR.expire == 0)
982 1.6 itojun printf(", expire=Never");
983 1.1 itojun else if (PR.expire >= time.tv_sec)
984 1.6 itojun printf(", expire=%s",
985 1.1 itojun sec2str(PR.expire - time.tv_sec));
986 1.1 itojun else
987 1.6 itojun printf(", expired");
988 1.6 itojun switch (PR.origin) {
989 1.6 itojun case PR_ORIG_RA:
990 1.6 itojun printf(", origin=RA");
991 1.6 itojun break;
992 1.6 itojun case PR_ORIG_RR:
993 1.6 itojun printf(", origin=RR");
994 1.6 itojun break;
995 1.6 itojun case PR_ORIG_STATIC:
996 1.6 itojun printf(", origin=static");
997 1.6 itojun break;
998 1.6 itojun case PR_ORIG_KERNEL:
999 1.6 itojun printf(", origin=kernel");
1000 1.6 itojun break;
1001 1.6 itojun default:
1002 1.6 itojun printf(", origin=?");
1003 1.6 itojun break;
1004 1.6 itojun }
1005 1.6 itojun printf("\n");
1006 1.6 itojun /*
1007 1.6 itojun * "advertising router" list is meaningful only if the prefix
1008 1.6 itojun * information is from RA.
1009 1.6 itojun */
1010 1.6 itojun if (PR.origin != PR_ORIG_RA)
1011 1.6 itojun ;
1012 1.6 itojun else if (PR.advrtrs) {
1013 1.1 itojun int j;
1014 1.1 itojun printf(" advertised by\n");
1015 1.1 itojun for (j = 0; j < PR.advrtrs; j++) {
1016 1.4 itojun struct sockaddr_in6 sin6;
1017 1.4 itojun struct in6_nbrinfo *nbi;
1018 1.4 itojun
1019 1.4 itojun bzero(&sin6, sizeof(sin6));
1020 1.4 itojun sin6.sin6_family = AF_INET6;
1021 1.4 itojun sin6.sin6_len = sizeof(sin6);
1022 1.4 itojun sin6.sin6_addr = PR.advrtr[j];
1023 1.4 itojun sin6.sin6_scope_id = PR.if_index; /* XXX */
1024 1.4 itojun getnameinfo((struct sockaddr *)&sin6,
1025 1.4 itojun sin6.sin6_len, host_buf,
1026 1.4 itojun sizeof(host_buf), NULL, 0,
1027 1.4 itojun NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1028 1.4 itojun printf(" %s", host_buf);
1029 1.4 itojun
1030 1.4 itojun nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1031 1.4 itojun 0);
1032 1.4 itojun if (nbi) {
1033 1.4 itojun switch(nbi->state) {
1034 1.4 itojun case ND6_LLINFO_REACHABLE:
1035 1.4 itojun case ND6_LLINFO_STALE:
1036 1.4 itojun case ND6_LLINFO_DELAY:
1037 1.4 itojun case ND6_LLINFO_PROBE:
1038 1.4 itojun printf(" (reachable)\n");
1039 1.4 itojun break;
1040 1.4 itojun default:
1041 1.4 itojun printf(" (unreachable)\n");
1042 1.4 itojun }
1043 1.4 itojun }
1044 1.4 itojun else
1045 1.4 itojun printf(" (no neighbor state)\n");
1046 1.1 itojun }
1047 1.1 itojun if (PR.advrtrs > DRLSTSIZ)
1048 1.1 itojun printf(" and %d routers\n",
1049 1.1 itojun PR.advrtrs - DRLSTSIZ);
1050 1.6 itojun } else
1051 1.1 itojun printf(" No advertising router\n");
1052 1.1 itojun }
1053 1.1 itojun #undef PR
1054 1.1 itojun close(s);
1055 1.1 itojun }
1056 1.1 itojun
1057 1.1 itojun void
1058 1.1 itojun pfx_flush()
1059 1.1 itojun {
1060 1.1 itojun char dummyif[IFNAMSIZ+8];
1061 1.1 itojun int s;
1062 1.1 itojun
1063 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1064 1.1 itojun err(1, "socket");
1065 1.1 itojun strcpy(dummyif, "lo0"); /* dummy */
1066 1.1 itojun if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1067 1.1 itojun err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1068 1.1 itojun }
1069 1.1 itojun
1070 1.1 itojun void
1071 1.1 itojun rtr_flush()
1072 1.1 itojun {
1073 1.1 itojun char dummyif[IFNAMSIZ+8];
1074 1.1 itojun int s;
1075 1.1 itojun
1076 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1077 1.1 itojun err(1, "socket");
1078 1.1 itojun strcpy(dummyif, "lo0"); /* dummy */
1079 1.1 itojun if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1080 1.1 itojun err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1081 1.4 itojun
1082 1.4 itojun close(s);
1083 1.1 itojun }
1084 1.1 itojun
1085 1.1 itojun void
1086 1.1 itojun harmonize_rtr()
1087 1.1 itojun {
1088 1.1 itojun char dummyif[IFNAMSIZ+8];
1089 1.1 itojun int s;
1090 1.1 itojun
1091 1.4 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1092 1.4 itojun err(1, "socket");
1093 1.4 itojun strcpy(dummyif, "lo0"); /* dummy */
1094 1.4 itojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1095 1.4 itojun err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1096 1.4 itojun
1097 1.4 itojun close(s);
1098 1.4 itojun }
1099 1.4 itojun
1100 1.4 itojun #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1101 1.4 itojun static void
1102 1.4 itojun setdefif(ifname)
1103 1.4 itojun char *ifname;
1104 1.4 itojun {
1105 1.4 itojun struct in6_ndifreq ndifreq;
1106 1.4 itojun unsigned int ifindex;
1107 1.4 itojun
1108 1.4 itojun if (strcasecmp(ifname, "delete") == 0)
1109 1.4 itojun ifindex = 0;
1110 1.4 itojun else {
1111 1.4 itojun if ((ifindex = if_nametoindex(ifname)) == 0)
1112 1.4 itojun err(1, "failed to resolve i/f index for %s", ifname);
1113 1.4 itojun }
1114 1.4 itojun
1115 1.4 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1116 1.4 itojun err(1, "socket");
1117 1.4 itojun
1118 1.4 itojun strcpy(ndifreq.ifname, "lo0"); /* dummy */
1119 1.4 itojun ndifreq.ifindex = ifindex;
1120 1.4 itojun
1121 1.4 itojun if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1122 1.4 itojun err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1123 1.4 itojun
1124 1.4 itojun close(s);
1125 1.4 itojun }
1126 1.4 itojun
1127 1.4 itojun static void
1128 1.4 itojun getdefif()
1129 1.4 itojun {
1130 1.4 itojun struct in6_ndifreq ndifreq;
1131 1.4 itojun char ifname[IFNAMSIZ+8];
1132 1.4 itojun
1133 1.4 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1134 1.4 itojun err(1, "socket");
1135 1.4 itojun
1136 1.4 itojun memset(&ndifreq, 0, sizeof(ndifreq));
1137 1.4 itojun strcpy(ndifreq.ifname, "lo0"); /* dummy */
1138 1.4 itojun
1139 1.4 itojun if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1140 1.4 itojun err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1141 1.4 itojun
1142 1.4 itojun if (ndifreq.ifindex == 0)
1143 1.4 itojun printf("No default interface.\n");
1144 1.4 itojun else {
1145 1.4 itojun if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1146 1.4 itojun err(1, "failed to resolve ifname for index %lu",
1147 1.4 itojun ndifreq.ifindex);
1148 1.4 itojun printf("ND default interface = %s\n", ifname);
1149 1.1 itojun }
1150 1.4 itojun
1151 1.4 itojun close(s);
1152 1.1 itojun }
1153 1.4 itojun #endif
1154 1.1 itojun
1155 1.1 itojun static char *
1156 1.1 itojun sec2str(total)
1157 1.1 itojun time_t total;
1158 1.1 itojun {
1159 1.1 itojun static char result[256];
1160 1.1 itojun int days, hours, mins, secs;
1161 1.1 itojun int first = 1;
1162 1.1 itojun char *p = result;
1163 1.1 itojun
1164 1.1 itojun days = total / 3600 / 24;
1165 1.1 itojun hours = (total / 3600) % 24;
1166 1.1 itojun mins = (total / 60) % 60;
1167 1.1 itojun secs = total % 60;
1168 1.1 itojun
1169 1.1 itojun if (days) {
1170 1.1 itojun first = 0;
1171 1.1 itojun p += sprintf(p, "%dd", days);
1172 1.1 itojun }
1173 1.1 itojun if (!first || hours) {
1174 1.1 itojun first = 0;
1175 1.1 itojun p += sprintf(p, "%dh", hours);
1176 1.1 itojun }
1177 1.1 itojun if (!first || mins) {
1178 1.1 itojun first = 0;
1179 1.1 itojun p += sprintf(p, "%dm", mins);
1180 1.1 itojun }
1181 1.1 itojun sprintf(p, "%ds", secs);
1182 1.1 itojun
1183 1.1 itojun return(result);
1184 1.1 itojun }
1185 1.1 itojun
1186 1.1 itojun /*
1187 1.1 itojun * Print the timestamp
1188 1.1 itojun * from tcpdump/util.c
1189 1.1 itojun */
1190 1.1 itojun static void
1191 1.1 itojun ts_print(tvp)
1192 1.1 itojun const struct timeval *tvp;
1193 1.1 itojun {
1194 1.1 itojun int s;
1195 1.1 itojun
1196 1.1 itojun /* Default */
1197 1.1 itojun s = (tvp->tv_sec + thiszone) % 86400;
1198 1.1 itojun (void)printf("%02d:%02d:%02d.%06u ",
1199 1.1 itojun s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1200 1.1 itojun }
1201