File: | src/usr.sbin/ndp/ndp.c |
Warning: | line 801, column 7 Although the value stored to 'rlen' is used in the enclosing expression, the value is never actually read from 'rlen' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: ndp.c,v 1.109 2023/04/05 13:56:41 bluhm Exp $ */ |
2 | /* $KAME: ndp.c,v 1.101 2002/07/17 08:46:33 itojun Exp $ */ |
3 | |
4 | /* |
5 | * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. Neither the name of the project nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | * SUCH DAMAGE. |
31 | */ |
32 | /* |
33 | * Copyright (c) 1984, 1993 |
34 | * The Regents of the University of California. All rights reserved. |
35 | * |
36 | * This code is derived from software contributed to Berkeley by |
37 | * Sun Microsystems, Inc. |
38 | * |
39 | * Redistribution and use in source and binary forms, with or without |
40 | * modification, are permitted provided that the following conditions |
41 | * are met: |
42 | * 1. Redistributions of source code must retain the above copyright |
43 | * notice, this list of conditions and the following disclaimer. |
44 | * 2. Redistributions in binary form must reproduce the above copyright |
45 | * notice, this list of conditions and the following disclaimer in the |
46 | * documentation and/or other materials provided with the distribution. |
47 | * 3. Neither the name of the University nor the names of its contributors |
48 | * may be used to endorse or promote products derived from this software |
49 | * without specific prior written permission. |
50 | * |
51 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
61 | * SUCH DAMAGE. |
62 | */ |
63 | |
64 | /* |
65 | * Based on: |
66 | * "@(#) Copyright (c) 1984, 1993\n\ |
67 | * The Regents of the University of California. All rights reserved.\n"; |
68 | * |
69 | * "@(#)arp.c 8.2 (Berkeley) 1/2/94"; |
70 | */ |
71 | |
72 | /* |
73 | * ndp - display, set, delete and flush neighbor cache |
74 | */ |
75 | |
76 | |
77 | #include <sys/ioctl.h> |
78 | #include <sys/socket.h> |
79 | #include <sys/sysctl.h> |
80 | #include <sys/time.h> |
81 | #include <sys/queue.h> |
82 | |
83 | #include <net/if.h> |
84 | #include <net/if_dl.h> |
85 | #include <net/if_types.h> |
86 | #include <net/route.h> |
87 | |
88 | #include <netinet/in.h> |
89 | |
90 | #include <netinet/icmp6.h> |
91 | #include <netinet6/in6_var.h> |
92 | #include <netinet6/nd6.h> |
93 | |
94 | #include <arpa/inet.h> |
95 | |
96 | #include <stdio.h> |
97 | #include <errno(*__errno()).h> |
98 | #include <fcntl.h> |
99 | #include <netdb.h> |
100 | #include <stdlib.h> |
101 | #include <string.h> |
102 | #include <unistd.h> |
103 | #include <limits.h> |
104 | #include <err.h> |
105 | |
106 | /* packing rule for routing socket */ |
107 | #define ROUNDUP(a)((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof (long)) \ |
108 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
109 | #define ADVANCE(x, n)(x += (((n)->sa_len) > 0 ? (1 + ((((n)->sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))) (x += ROUNDUP((n)->sa_len)(((n)->sa_len) > 0 ? (1 + ((((n)->sa_len) - 1) | (sizeof (long) - 1))) : sizeof(long))) |
110 | |
111 | static pid_t pid; |
112 | static int nflag; |
113 | static int tflag; |
114 | static int rtsock = -1; |
115 | static int repeat = 0; |
116 | |
117 | char host_buf[NI_MAXHOST256]; /* getnameinfo() */ |
118 | char ifix_buf[IFNAMSIZ16]; /* if_indextoname() */ |
119 | |
120 | int file(char *); |
121 | void getsocket(void); |
122 | int parse_host(const char *, struct sockaddr_in6 *); |
123 | int set(int, char **); |
124 | void get(const char *); |
125 | int delete(const char *); |
126 | void dump(struct sockaddr_in6 *, int); |
127 | static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int); |
128 | static char *ether_str(struct sockaddr_dl *); |
129 | int ndp_ether_aton(const char *, u_char *); |
130 | void usage(void); |
131 | int rtmsg(int); |
132 | int rtget(struct sockaddr_in6 **, struct sockaddr_dl **); |
133 | void ifinfo(const char *); |
134 | static char *sec2str(time_t); |
135 | static int rdomain; |
136 | |
137 | int |
138 | main(int argc, char *argv[]) |
139 | { |
140 | int ch, mode = 0, error = 0; |
141 | char *arg = NULL((void *)0); |
142 | const char *errstr; |
143 | |
144 | pid = getpid(); |
145 | rdomain = getrtable(); |
146 | while ((ch = getopt(argc, argv, "acd:f:i:nstA:V:")) != -1) { |
147 | switch (ch) { |
148 | case 'a': |
149 | case 'c': |
150 | case 'p': |
151 | case 'r': |
152 | case 'P': |
153 | case 's': |
154 | if (mode) { |
155 | usage(); |
156 | } |
157 | mode = ch; |
158 | arg = NULL((void *)0); |
159 | break; |
160 | case 'd': |
161 | case 'f': |
162 | case 'i' : |
163 | if (mode) { |
164 | usage(); |
165 | } |
166 | mode = ch; |
167 | arg = optarg; |
168 | break; |
169 | case 'n': |
170 | nflag = 1; |
171 | break; |
172 | case 't': |
173 | tflag = 1; |
174 | break; |
175 | case 'A': |
176 | if (mode) { |
177 | usage(); |
178 | } |
179 | mode = 'a'; |
180 | repeat = strtonum(optarg, 1, INT_MAX0x7fffffff, &errstr); |
181 | if (errstr) { |
182 | usage(); |
183 | } |
184 | break; |
185 | case 'V': |
186 | rdomain = strtonum(optarg, 0, RT_TABLEID_MAX255, &errstr); |
187 | if (errstr != NULL((void *)0)) { |
188 | warn("bad rdomain: %s", errstr); |
189 | usage(); |
190 | } |
191 | break; |
192 | default: |
193 | usage(); |
194 | } |
195 | } |
196 | argc -= optind; |
197 | argv += optind; |
198 | |
199 | switch (mode) { |
200 | case 'a': |
201 | case 'c': |
202 | if (argc != 0) { |
203 | usage(); |
204 | } |
205 | dump(NULL((void *)0), mode == 'c'); |
206 | break; |
207 | case 'd': |
208 | if (argc != 0) { |
209 | usage(); |
210 | } |
211 | error = delete(arg); |
212 | break; |
213 | case 'f': |
214 | if (argc != 0) |
215 | usage(); |
216 | file(arg); |
217 | break; |
218 | case 'i': |
219 | if (argc != 0) |
220 | usage(); |
221 | ifinfo(arg); |
222 | break; |
223 | case 's': |
224 | if (argc < 2 || argc > 4) |
225 | usage(); |
226 | exit(set(argc, argv) ? 1 : 0); |
227 | case 0: |
228 | if (argc != 1) { |
229 | usage(); |
230 | } |
231 | get(argv[0]); |
232 | break; |
233 | } |
234 | return (error); |
235 | } |
236 | |
237 | /* |
238 | * Process a file to set standard ndp entries |
239 | */ |
240 | int |
241 | file(char *name) |
242 | { |
243 | FILE *fp; |
244 | int i, retval; |
245 | char line[100], arg[5][50], *args[5]; |
246 | |
247 | if ((fp = fopen(name, "r")) == NULL((void *)0)) { |
248 | err(1, "cannot open %s", name); |
249 | } |
250 | args[0] = &arg[0][0]; |
251 | args[1] = &arg[1][0]; |
252 | args[2] = &arg[2][0]; |
253 | args[3] = &arg[3][0]; |
254 | args[4] = &arg[4][0]; |
255 | retval = 0; |
256 | while (fgets(line, sizeof(line), fp) != NULL((void *)0)) { |
257 | i = sscanf(line, "%49s %49s %49s %49s %49s", |
258 | arg[0], arg[1], arg[2], arg[3], arg[4]); |
259 | if (i < 2) { |
260 | warnx("bad line: %s", line); |
261 | retval = 1; |
262 | continue; |
263 | } |
264 | if (set(i, args)) |
265 | retval = 1; |
266 | } |
267 | fclose(fp); |
268 | return (retval); |
269 | } |
270 | |
271 | void |
272 | getsocket(void) |
273 | { |
274 | socklen_t len = sizeof(rdomain); |
275 | |
276 | if (rtsock >= 0) |
277 | return; |
278 | rtsock = socket(AF_ROUTE17, SOCK_RAW3, 0); |
279 | if (rtsock == -1) |
280 | err(1, "routing socket"); |
281 | if (setsockopt(rtsock, AF_ROUTE17, ROUTE_TABLEFILTER2, &rdomain, len) == -1) |
282 | err(1, "ROUTE_TABLEFILTER"); |
283 | |
284 | if (pledge("stdio dns", NULL((void *)0)) == -1) |
285 | err(1, "pledge"); |
286 | } |
287 | |
288 | int |
289 | parse_host(const char *host, struct sockaddr_in6 *sin6) |
290 | { |
291 | struct addrinfo hints, *res; |
292 | int gai_error; |
293 | |
294 | bzero(&hints, sizeof(hints)); |
295 | hints.ai_family = AF_INET624; |
296 | if (nflag) |
297 | hints.ai_flags = AI_NUMERICHOST4; |
298 | |
299 | gai_error = getaddrinfo(host, NULL((void *)0), &hints, &res); |
300 | if (gai_error) { |
301 | warnx("%s: %s", host, gai_strerror(gai_error)); |
302 | return 1; |
303 | } |
304 | *sin6 = *(struct sockaddr_in6 *)res->ai_addr; |
305 | freeaddrinfo(res); |
306 | return 0; |
307 | } |
308 | |
309 | struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET624 }; |
310 | struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET624 }, sin_m; |
311 | struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK18 }, sdl_m; |
312 | struct sockaddr_dl ifp_m = { sizeof(ifp_m), AF_LINK18 }; |
313 | time_t expire_time; |
314 | int flags, found_entry; |
315 | struct { |
316 | struct rt_msghdr m_rtm; |
317 | char m_space[512]; |
318 | } m_rtmsg; |
319 | |
320 | /* |
321 | * Set an individual neighbor cache entry |
322 | */ |
323 | int |
324 | set(int argc, char *argv[]) |
325 | { |
326 | struct sockaddr_in6 *sin = &sin_m; |
327 | struct sockaddr_dl *sdl; |
328 | struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); |
329 | u_char *ea; |
330 | const char *host = argv[0], *eaddr = argv[1]; |
331 | |
332 | getsocket(); |
333 | argc -= 2; |
334 | argv += 2; |
335 | sdl_m = blank_sdl; |
336 | sin_m = blank_sin; |
337 | |
338 | if (parse_host(host, sin)) |
339 | return 1; |
340 | ea = (u_char *)LLADDR(&sdl_m)((caddr_t)((&sdl_m)->sdl_data + (&sdl_m)->sdl_nlen )); |
341 | if (ndp_ether_aton(eaddr, ea) == 0) |
342 | sdl_m.sdl_alen = 6; |
343 | expire_time = 0; |
344 | flags = 0; |
345 | while (argc-- > 0) { |
346 | if (strncmp(argv[0], "temp", 4) == 0) { |
347 | struct timeval now; |
348 | |
349 | gettimeofday(&now, 0); |
350 | expire_time = now.tv_sec + 20 * 60; |
351 | } else if (strncmp(argv[0], "proxy", 5) == 0) |
352 | flags |= RTF_ANNOUNCE0x4000; |
353 | argv++; |
354 | } |
355 | |
356 | if (rtget(&sin, &sdl)) { |
357 | errx(1, "RTM_GET(%s) failed", host); |
358 | } |
359 | |
360 | if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)(memcmp(&(&sin->sin6_addr)->__u6_addr.__u6_addr8 [0], &(&sin_m.sin6_addr)->__u6_addr.__u6_addr8[0], sizeof(struct in6_addr)) == 0) && |
361 | sin->sin6_scope_id == sin_m.sin6_scope_id) { |
362 | if (sdl->sdl_family == AF_LINK18 && |
363 | (rtm->rtm_flags & RTF_LLINFO0x400) && |
364 | !(rtm->rtm_flags & RTF_GATEWAY0x2)) { |
365 | switch (sdl->sdl_type) { |
366 | case IFT_ETHER0x06: case IFT_FDDI0x0f: case IFT_ISO880230x07: |
367 | case IFT_ISO880240x08: case IFT_ISO880250x09: |
368 | goto overwrite; |
369 | } |
370 | } |
371 | /* |
372 | * IPv4 arp command retries with sin_other = SIN_PROXY here. |
373 | */ |
374 | warnx("set: cannot configure a new entry"); |
375 | return 1; |
376 | } |
377 | |
378 | overwrite: |
379 | if (sdl->sdl_family != AF_LINK18) { |
380 | printf("cannot intuit interface index and type for %s\n", host); |
381 | return (1); |
382 | } |
383 | sdl_m.sdl_type = sdl->sdl_type; |
384 | sdl_m.sdl_index = sdl->sdl_index; |
385 | return (rtmsg(RTM_ADD0x1)); |
386 | } |
387 | |
388 | /* |
389 | * Display an individual neighbor cache entry |
390 | */ |
391 | void |
392 | get(const char *host) |
393 | { |
394 | struct sockaddr_in6 *sin = &sin_m; |
395 | |
396 | sin_m = blank_sin; |
397 | if (parse_host(host, sin)) |
398 | return; |
399 | |
400 | dump(sin, 0); |
401 | if (found_entry == 0) { |
402 | getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, |
403 | sizeof(host_buf), NULL((void *)0) ,0, |
404 | (nflag ? NI_NUMERICHOST1 : 0)); |
405 | printf("%s (%s) -- no entry\n", host, host_buf); |
406 | exit(1); |
407 | } |
408 | } |
409 | |
410 | /* |
411 | * Delete a neighbor cache entry |
412 | */ |
413 | int |
414 | delete(const char *host) |
415 | { |
416 | struct sockaddr_in6 *sin; |
417 | struct rt_msghdr *rtm; |
418 | struct sockaddr_dl *sdl; |
419 | |
420 | sin = &sin_m; |
421 | rtm = &m_rtmsg.m_rtm; |
422 | |
423 | getsocket(); |
424 | sin_m = blank_sin; /* struct copy */ |
425 | if (parse_host(host, sin)) |
426 | return 1; |
427 | if (rtget(&sin, &sdl)) { |
428 | warn("%s", host); |
429 | return 1; |
430 | } |
431 | if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)(memcmp(&(&sin->sin6_addr)->__u6_addr.__u6_addr8 [0], &(&sin_m.sin6_addr)->__u6_addr.__u6_addr8[0], sizeof(struct in6_addr)) == 0) && |
432 | sin->sin6_scope_id == sin_m.sin6_scope_id) { |
433 | if (sdl->sdl_family == AF_LINK18 && rtm->rtm_flags & RTF_LLINFO0x400) { |
434 | if (rtm->rtm_flags & RTF_LOCAL0x200000) |
435 | return 0; |
436 | if ((rtm->rtm_flags & RTF_GATEWAY0x2) == 0) |
437 | goto delete; |
438 | } |
439 | } |
440 | |
441 | /* |
442 | * IPv4 arp command retries with sin_other = SIN_PROXY here. |
443 | */ |
444 | warnx("delete: cannot locate %s", host); |
445 | return 1; |
446 | |
447 | delete: |
448 | if (sdl->sdl_family != AF_LINK18) { |
449 | printf("cannot locate %s\n", host); |
450 | return 1; |
451 | } |
452 | if (rtmsg(RTM_DELETE0x2)) |
453 | return 1; |
454 | getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, |
455 | sizeof(host_buf), NULL((void *)0), 0, (nflag ? NI_NUMERICHOST1 : 0)); |
456 | printf("%s (%s) deleted\n", host, host_buf); |
457 | return 0; |
458 | } |
459 | |
460 | /* |
461 | * strlen("2001:0db8:3333:4444:5555:6666:7777:8888") == 39 |
462 | */ |
463 | #define W_ADDR39 39 |
464 | #define W_LL17 17 |
465 | #define W_IF7 7 |
466 | |
467 | /* |
468 | * Dump the entire neighbor cache |
469 | */ |
470 | void |
471 | dump(struct sockaddr_in6 *addr, int cflag) |
472 | { |
473 | int mib[7]; |
474 | size_t needed; |
475 | char *lim, *buf = NULL((void *)0), *next; |
476 | struct rt_msghdr *rtm; |
477 | struct sockaddr_in6 *sin; |
478 | struct sockaddr_dl *sdl; |
479 | struct in6_nbrinfo *nbi; |
480 | struct timeval now; |
481 | int addrwidth; |
482 | int llwidth; |
483 | int ifwidth; |
484 | char *ifname; |
485 | |
486 | /* Print header */ |
487 | if (!tflag && !cflag) |
488 | printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", |
489 | W_ADDR39, W_ADDR39, "Neighbor", W_LL17, W_LL17, "Linklayer Address", |
490 | W_IF7, W_IF7, "Netif", "Expire", "S", "Flags"); |
491 | |
492 | again:; |
493 | lim = NULL((void *)0); |
494 | mib[0] = CTL_NET4; |
495 | mib[1] = PF_ROUTE17; |
496 | mib[2] = 0; |
497 | mib[3] = AF_INET624; |
498 | mib[4] = NET_RT_FLAGS2; |
499 | mib[5] = RTF_LLINFO0x400; |
500 | mib[6] = rdomain; |
501 | while (1) { |
502 | if (sysctl(mib, 7, NULL((void *)0), &needed, NULL((void *)0), 0) == -1) |
503 | err(1, "sysctl(PF_ROUTE estimate)"); |
504 | if (needed == 0) |
505 | break; |
506 | if ((buf = realloc(buf, needed)) == NULL((void *)0)) |
507 | err(1, "realloc"); |
508 | if (sysctl(mib, 7, buf, &needed, NULL((void *)0), 0) == -1) { |
509 | if (errno(*__errno()) == ENOMEM12) |
510 | continue; |
511 | err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); |
512 | } |
513 | lim = buf + needed; |
514 | break; |
515 | } |
516 | |
517 | for (next = buf; next && lim && next < lim; next += rtm->rtm_msglen) { |
518 | int isrouter = 0, prbs = 0; |
519 | |
520 | rtm = (struct rt_msghdr *)next; |
521 | if (rtm->rtm_version != RTM_VERSION5) |
522 | continue; |
523 | sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen); |
524 | #ifdef __KAME__ |
525 | { |
526 | struct in6_addr *in6 = &sin->sin6_addr; |
527 | if ((IN6_IS_ADDR_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xfe) && (((in6 )->__u6_addr.__u6_addr8[1] & 0xc0) == 0x80)) || |
528 | IN6_IS_ADDR_MC_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x02)) || |
529 | IN6_IS_ADDR_MC_INTFACELOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x01))) && |
530 | sin->sin6_scope_id == 0) { |
531 | sin->sin6_scope_id = (u_int32_t) |
532 | ntohs(*(u_short *)&in6->s6_addr[2])(__uint16_t)(__builtin_constant_p(*(u_short *)&in6->__u6_addr .__u6_addr8[2]) ? (__uint16_t)(((__uint16_t)(*(u_short *)& in6->__u6_addr.__u6_addr8[2]) & 0xffU) << 8 | (( __uint16_t)(*(u_short *)&in6->__u6_addr.__u6_addr8[2]) & 0xff00U) >> 8) : __swap16md(*(u_short *)&in6 ->__u6_addr.__u6_addr8[2])); |
533 | *(u_short *)&in6->s6_addr__u6_addr.__u6_addr8[2] = 0; |
534 | } |
535 | } |
536 | #endif |
537 | sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)((sin->sin6_len) > 0 ? (1 + (((sin->sin6_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); |
538 | |
539 | /* |
540 | * Some OSes can produce a route that has the LINK flag but |
541 | * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD |
542 | * and BSD/OS, where xx is not the interface identifier on |
543 | * lo0). Such routes entry would annoy getnbrinfo() below, |
544 | * so we skip them. |
545 | * XXX: such routes should have the GATEWAY flag, not the |
546 | * LINK flag. However, there is rotten routing software |
547 | * that advertises all routes that have the GATEWAY flag. |
548 | * Thus, KAME kernel intentionally does not set the LINK flag. |
549 | * What is to be fixed is not ndp, but such routing software |
550 | * (and the kernel workaround)... |
551 | */ |
552 | if (sdl->sdl_family != AF_LINK18) |
553 | continue; |
554 | |
555 | if (!(rtm->rtm_flags & RTF_HOST0x4)) |
556 | continue; |
557 | |
558 | if (addr) { |
559 | if (!IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,(memcmp(&(&addr->sin6_addr)->__u6_addr.__u6_addr8 [0], &(&sin->sin6_addr)->__u6_addr.__u6_addr8[0 ], sizeof(struct in6_addr)) == 0) |
560 | &sin->sin6_addr)(memcmp(&(&addr->sin6_addr)->__u6_addr.__u6_addr8 [0], &(&sin->sin6_addr)->__u6_addr.__u6_addr8[0 ], sizeof(struct in6_addr)) == 0) || addr->sin6_scope_id != |
561 | sin->sin6_scope_id) |
562 | continue; |
563 | found_entry = 1; |
564 | } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)((&sin->sin6_addr)->__u6_addr.__u6_addr8[0] == 0xff )) |
565 | continue; |
566 | getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, |
567 | sizeof(host_buf), NULL((void *)0), 0, (nflag ? NI_NUMERICHOST1 : 0)); |
568 | if (cflag) { |
569 | if (rtm->rtm_flags & RTF_CLONED0x10000) |
570 | delete(host_buf); |
571 | continue; |
572 | } |
573 | gettimeofday(&now, 0); |
574 | if (tflag) { |
575 | char buf[sizeof("00:00:00")]; |
576 | struct tm *tm; |
577 | |
578 | tm = localtime(&now.tv_sec); |
579 | if (tm != NULL((void *)0)) { |
580 | strftime(buf, sizeof(buf), "%H:%M:%S", tm); |
581 | printf("%s.%06ld ", buf, now.tv_usec); |
582 | } |
583 | } |
584 | |
585 | addrwidth = strlen(host_buf); |
586 | if (addrwidth < W_ADDR39) |
587 | addrwidth = W_ADDR39; |
588 | llwidth = strlen(ether_str(sdl)); |
589 | if (W_ADDR39 + W_LL17 - addrwidth > llwidth) |
590 | llwidth = W_ADDR39 + W_LL17 - addrwidth; |
591 | ifname = if_indextoname(sdl->sdl_index, ifix_buf); |
592 | if (!ifname) |
593 | ifname = "?"; |
594 | ifwidth = strlen(ifname); |
595 | if (W_ADDR39 + W_LL17 + W_IF7 - addrwidth - llwidth > ifwidth) |
596 | ifwidth = W_ADDR39 + W_LL17 + W_IF7 - addrwidth - llwidth; |
597 | |
598 | printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, |
599 | llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); |
600 | |
601 | /* Print neighbor discovery specific information */ |
602 | nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); |
603 | if (nbi) { |
604 | if (nbi->expire > now.tv_sec) { |
605 | printf(" %-9.9s", |
606 | sec2str(nbi->expire - now.tv_sec)); |
607 | } else if (nbi->expire == 0) |
608 | printf(" %-9.9s", "permanent"); |
609 | else |
610 | printf(" %-9.9s", "expired"); |
611 | |
612 | switch (nbi->state) { |
613 | case ND6_LLINFO_NOSTATE-2: |
614 | printf(" N"); |
615 | break; |
616 | case ND6_LLINFO_INCOMPLETE0: |
617 | printf(" I"); |
618 | break; |
619 | case ND6_LLINFO_REACHABLE1: |
620 | printf(" R"); |
621 | break; |
622 | case ND6_LLINFO_STALE2: |
623 | printf(" S"); |
624 | break; |
625 | case ND6_LLINFO_DELAY3: |
626 | printf(" D"); |
627 | break; |
628 | case ND6_LLINFO_PROBE4: |
629 | printf(" P"); |
630 | break; |
631 | default: |
632 | printf(" ?"); |
633 | break; |
634 | } |
635 | |
636 | isrouter = nbi->isrouter; |
637 | prbs = nbi->asked; |
638 | } else { |
639 | warnx("failed to get neighbor information"); |
640 | printf(" "); |
641 | } |
642 | |
643 | printf(" %s%s%s", |
644 | (rtm->rtm_flags & RTF_LOCAL0x200000) ? "l" : "", |
645 | isrouter ? "R" : "", |
646 | (rtm->rtm_flags & RTF_ANNOUNCE0x4000) ? "p" : ""); |
647 | |
648 | if (prbs) |
649 | printf(" %d", prbs); |
650 | |
651 | printf("\n"); |
652 | } |
653 | |
654 | if (repeat) { |
655 | printf("\n"); |
656 | fflush(stdout(&__sF[1])); |
657 | sleep(repeat); |
658 | goto again; |
659 | } |
660 | |
661 | free(buf); |
662 | } |
663 | |
664 | static struct in6_nbrinfo * |
665 | getnbrinfo(struct in6_addr *addr, int ifindex, int warning) |
666 | { |
667 | static struct in6_nbrinfo nbi; |
668 | int s; |
669 | |
670 | if ((s = socket(AF_INET624, SOCK_DGRAM2, 0)) == -1) |
671 | err(1, "socket"); |
672 | |
673 | bzero(&nbi, sizeof(nbi)); |
674 | if_indextoname(ifindex, nbi.ifname); |
675 | nbi.addr = *addr; |
676 | if (ioctl(s, SIOCGNBRINFO_IN6(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof (struct in6_nbrinfo) & 0x1fff) << 16) | ((('i')) << 8) | ((78))), (caddr_t)&nbi) == -1) { |
677 | if (warning) |
678 | warn("ioctl(SIOCGNBRINFO_IN6)"); |
679 | close(s); |
680 | return(NULL((void *)0)); |
681 | } |
682 | |
683 | close(s); |
684 | return(&nbi); |
685 | } |
686 | |
687 | static char * |
688 | ether_str(struct sockaddr_dl *sdl) |
689 | { |
690 | static char hbuf[NI_MAXHOST256]; |
691 | u_char *cp; |
692 | |
693 | if (sdl->sdl_alen) { |
694 | cp = (u_char *)LLADDR(sdl)((caddr_t)((sdl)->sdl_data + (sdl)->sdl_nlen)); |
695 | snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x", |
696 | cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); |
697 | } else |
698 | snprintf(hbuf, sizeof(hbuf), "(incomplete)"); |
699 | |
700 | return(hbuf); |
701 | } |
702 | |
703 | int |
704 | ndp_ether_aton(const char *a, u_char *n) |
705 | { |
706 | int i, o[6]; |
707 | |
708 | i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], |
709 | &o[3], &o[4], &o[5]); |
710 | if (i != 6) { |
711 | warnx("invalid Ethernet address '%s'", a); |
712 | return (1); |
713 | } |
714 | for (i = 0; i < 6; i++) |
715 | n[i] = o[i]; |
716 | return (0); |
717 | } |
718 | |
719 | void |
720 | usage(void) |
721 | { |
722 | printf("usage: ndp [-acnt] "); |
723 | printf("[-A wait] [-d hostname] [-f filename] [-i interface]\n"); |
724 | printf("\t[-s nodename ether_addr [temp] [proxy]] "); |
725 | printf("[-V rdomain] [hostname]\n"); |
726 | exit(1); |
727 | } |
728 | |
729 | int |
730 | rtmsg(int cmd) |
731 | { |
732 | static int seq; |
733 | int rlen; |
734 | struct rt_msghdr *rtm = &m_rtmsg.m_rtm; |
735 | char *cp = m_rtmsg.m_space; |
736 | int l; |
737 | |
738 | errno(*__errno()) = 0; |
739 | if (cmd == RTM_DELETE0x2) |
740 | goto doit; |
741 | bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); |
742 | rtm->rtm_flags = flags; |
743 | rtm->rtm_version = RTM_VERSION5; |
744 | rtm->rtm_tableid = rdomain; |
745 | |
746 | switch (cmd) { |
747 | default: |
748 | errx(1, "internal wrong cmd"); |
749 | case RTM_ADD0x1: |
750 | rtm->rtm_addrs |= RTA_GATEWAY0x2; |
751 | if (expire_time) { |
752 | rtm->rtm_rmx.rmx_expire = expire_time; |
753 | rtm->rtm_inits = RTV_EXPIRE0x4; |
754 | } |
755 | rtm->rtm_flags |= (RTF_HOST0x4 | RTF_STATIC0x800); |
756 | #if 0 /* we don't support ipv6addr/128 type proxying. */ |
757 | if (rtm->rtm_flags & RTF_ANNOUNCE0x4000) { |
758 | rtm->rtm_flags &= ~RTF_HOST0x4; |
759 | rtm->rtm_addrs |= RTA_NETMASK0x4; |
760 | } |
761 | #endif |
762 | /* FALLTHROUGH */ |
763 | case RTM_GET0x4: |
764 | rtm->rtm_addrs |= (RTA_DST0x1 | RTA_IFP0x10); |
765 | } |
766 | |
767 | #define NEXTADDR(w, s)if (rtm->rtm_addrs & (w)) { memcpy(cp, &(s), sizeof (s)); (cp += ((((struct sockaddr *)&(s))->sa_len) > 0 ? (1 + (((((struct sockaddr *)&(s))->sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); } \ |
768 | if (rtm->rtm_addrs & (w)) { \ |
769 | memcpy(cp, &(s), sizeof(s)); \ |
770 | ADVANCE(cp, (struct sockaddr *)&(s))(cp += ((((struct sockaddr *)&(s))->sa_len) > 0 ? ( 1 + (((((struct sockaddr *)&(s))->sa_len) - 1) | (sizeof (long) - 1))) : sizeof(long))); \ |
771 | } |
772 | |
773 | #ifdef __KAME__ |
774 | { |
775 | struct sockaddr_in6 sin6 = sin_m; |
776 | struct in6_addr *in6 = &sin6.sin6_addr; |
777 | if (IN6_IS_ADDR_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xfe) && (((in6 )->__u6_addr.__u6_addr8[1] & 0xc0) == 0x80)) || |
778 | IN6_IS_ADDR_MC_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x02)) || |
779 | IN6_IS_ADDR_MC_INTFACELOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x01))) { |
780 | *(u_int16_t *)& in6->s6_addr__u6_addr.__u6_addr8[2] = |
781 | htons(sin6.sin6_scope_id)(__uint16_t)(__builtin_constant_p(sin6.sin6_scope_id) ? (__uint16_t )(((__uint16_t)(sin6.sin6_scope_id) & 0xffU) << 8 | ((__uint16_t)(sin6.sin6_scope_id) & 0xff00U) >> 8) : __swap16md(sin6.sin6_scope_id)); |
782 | sin6.sin6_scope_id = 0; |
783 | } |
784 | NEXTADDR(RTA_DST, sin6)if (rtm->rtm_addrs & (0x1)) { memcpy(cp, &(sin6), sizeof (sin6)); (cp += ((((struct sockaddr *)&(sin6))->sa_len ) > 0 ? (1 + (((((struct sockaddr *)&(sin6))->sa_len ) - 1) | (sizeof(long) - 1))) : sizeof(long))); }; |
785 | } |
786 | #else |
787 | NEXTADDR(RTA_DST, sin_m)if (rtm->rtm_addrs & (0x1)) { memcpy(cp, &(sin_m), sizeof(sin_m)); (cp += ((((struct sockaddr *)&(sin_m))-> sa_len) > 0 ? (1 + (((((struct sockaddr *)&(sin_m))-> sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); }; |
788 | #endif |
789 | NEXTADDR(RTA_GATEWAY, sdl_m)if (rtm->rtm_addrs & (0x2)) { memcpy(cp, &(sdl_m), sizeof(sdl_m)); (cp += ((((struct sockaddr *)&(sdl_m))-> sa_len) > 0 ? (1 + (((((struct sockaddr *)&(sdl_m))-> sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); }; |
790 | #if 0 /* we don't support ipv6addr/128 type proxying. */ |
791 | memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); |
792 | NEXTADDR(RTA_NETMASK, so_mask)if (rtm->rtm_addrs & (0x4)) { memcpy(cp, &(so_mask ), sizeof(so_mask)); (cp += ((((struct sockaddr *)&(so_mask ))->sa_len) > 0 ? (1 + (((((struct sockaddr *)&(so_mask ))->sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); }; |
793 | #endif |
794 | NEXTADDR(RTA_IFP, ifp_m)if (rtm->rtm_addrs & (0x10)) { memcpy(cp, &(ifp_m) , sizeof(ifp_m)); (cp += ((((struct sockaddr *)&(ifp_m))-> sa_len) > 0 ? (1 + (((((struct sockaddr *)&(ifp_m))-> sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); }; |
795 | |
796 | rtm->rtm_msglen = cp - (char *)&m_rtmsg; |
797 | doit: |
798 | l = rtm->rtm_msglen; |
799 | rtm->rtm_seq = ++seq; |
800 | rtm->rtm_type = cmd; |
801 | if ((rlen = write(rtsock, (char *)&m_rtmsg, l)) == -1) { |
Although the value stored to 'rlen' is used in the enclosing expression, the value is never actually read from 'rlen' | |
802 | if (errno(*__errno()) != ESRCH3 || cmd != RTM_DELETE0x2) { |
803 | err(1, "writing to routing socket"); |
804 | } |
805 | } |
806 | do { |
807 | l = read(rtsock, (char *)&m_rtmsg, sizeof(m_rtmsg)); |
808 | } while (l > 0 && (rtm->rtm_version != RTM_VERSION5 || |
809 | rtm->rtm_seq != seq || rtm->rtm_pid != pid)); |
810 | if (l == -1) |
811 | warn("read from routing socket"); |
812 | return (0); |
813 | } |
814 | |
815 | int |
816 | rtget(struct sockaddr_in6 **sinp, struct sockaddr_dl **sdlp) |
817 | { |
818 | struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); |
819 | struct sockaddr_in6 *sin = NULL((void *)0); |
820 | struct sockaddr_dl *sdl = NULL((void *)0); |
821 | struct sockaddr *sa; |
822 | char *cp; |
823 | unsigned int i; |
824 | |
825 | if (rtmsg(RTM_GET0x4) < 0) |
826 | return (1); |
827 | |
828 | if (rtm->rtm_addrs) { |
829 | cp = ((char *)rtm + rtm->rtm_hdrlen); |
830 | for (i = 1; i; i <<= 1) { |
831 | if (i & rtm->rtm_addrs) { |
832 | sa = (struct sockaddr *)cp; |
833 | switch (i) { |
834 | case RTA_DST0x1: |
835 | sin = (struct sockaddr_in6 *)sa; |
836 | break; |
837 | case RTA_IFP0x10: |
838 | sdl = (struct sockaddr_dl *)sa; |
839 | break; |
840 | default: |
841 | break; |
842 | } |
843 | ADVANCE(cp, sa)(cp += (((sa)->sa_len) > 0 ? (1 + ((((sa)->sa_len) - 1) | (sizeof(long) - 1))) : sizeof(long))); |
844 | } |
845 | } |
846 | } |
847 | |
848 | if (sin == NULL((void *)0) || sdl == NULL((void *)0)) |
849 | return (1); |
850 | |
851 | #ifdef __KAME__ |
852 | { |
853 | static struct sockaddr_in6 ksin; |
854 | struct in6_addr *in6; |
855 | |
856 | /* do not damage the route message, we need it for delete */ |
857 | ksin = *sin; |
858 | sin = &ksin; |
859 | in6 = &sin->sin6_addr; |
860 | |
861 | if ((IN6_IS_ADDR_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xfe) && (((in6 )->__u6_addr.__u6_addr8[1] & 0xc0) == 0x80)) || |
862 | IN6_IS_ADDR_MC_LINKLOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x02)) || |
863 | IN6_IS_ADDR_MC_INTFACELOCAL(in6)(((in6)->__u6_addr.__u6_addr8[0] == 0xff) && (((in6 )->__u6_addr.__u6_addr8[1] & 0x0f) == 0x01))) && |
864 | sin->sin6_scope_id == 0) { |
865 | sin->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)(__uint16_t)(__builtin_constant_p(*(u_short *) &in6->__u6_addr .__u6_addr8[2]) ? (__uint16_t)(((__uint16_t)(*(u_short *) & in6->__u6_addr.__u6_addr8[2]) & 0xffU) << 8 | (( __uint16_t)(*(u_short *) &in6->__u6_addr.__u6_addr8[2] ) & 0xff00U) >> 8) : __swap16md(*(u_short *) &in6 ->__u6_addr.__u6_addr8[2])) |
866 | &in6->s6_addr[2])(__uint16_t)(__builtin_constant_p(*(u_short *) &in6->__u6_addr .__u6_addr8[2]) ? (__uint16_t)(((__uint16_t)(*(u_short *) & in6->__u6_addr.__u6_addr8[2]) & 0xffU) << 8 | (( __uint16_t)(*(u_short *) &in6->__u6_addr.__u6_addr8[2] ) & 0xff00U) >> 8) : __swap16md(*(u_short *) &in6 ->__u6_addr.__u6_addr8[2])); |
867 | *(u_short *)&in6->s6_addr__u6_addr.__u6_addr8[2] = 0; |
868 | } |
869 | } |
870 | #endif |
871 | *sinp = sin; |
872 | *sdlp = sdl; |
873 | |
874 | return (0); |
875 | } |
876 | |
877 | void |
878 | ifinfo(const char *ifname) |
879 | { |
880 | struct in6_ndireq nd; |
881 | int s; |
882 | |
883 | if ((s = socket(AF_INET624, SOCK_DGRAM2, 0)) == -1) { |
884 | err(1, "socket"); |
885 | } |
886 | bzero(&nd, sizeof(nd)); |
887 | strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); |
888 | if (ioctl(s, SIOCGIFINFO_IN6(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof (struct in6_ndireq) & 0x1fff) << 16) | ((('i')) << 8) | ((108))), (caddr_t)&nd) == -1) |
889 | err(1, "ioctl(SIOCGIFINFO_IN6)"); |
890 | |
891 | printf("reachable=%ds\n", nd.ndi.reachable); |
892 | |
893 | close(s); |
894 | } |
895 | |
896 | static char * |
897 | sec2str(time_t total) |
898 | { |
899 | static char result[256]; |
900 | int days, hours, mins, secs; |
901 | int first = 1; |
902 | char *p = result; |
903 | char *ep = &result[sizeof(result)]; |
904 | int n; |
905 | |
906 | days = total / 3600 / 24; |
907 | hours = (total / 3600) % 24; |
908 | mins = (total / 60) % 60; |
909 | secs = total % 60; |
910 | |
911 | if (days) { |
912 | first = 0; |
913 | n = snprintf(p, ep - p, "%dd", days); |
914 | if (n < 0 || n >= ep - p) |
915 | return "?"; |
916 | p += n; |
917 | } |
918 | if (!first || hours) { |
919 | first = 0; |
920 | n = snprintf(p, ep - p, "%dh", hours); |
921 | if (n < 0 || n >= ep - p) |
922 | return "?"; |
923 | p += n; |
924 | } |
925 | if (!first || mins) { |
926 | first = 0; |
927 | n = snprintf(p, ep - p, "%dm", mins); |
928 | if (n < 0 || n >= ep - p) |
929 | return "?"; |
930 | p += n; |
931 | } |
932 | snprintf(p, ep - p, "%ds", secs); |
933 | |
934 | return(result); |
935 | } |