arp.c revision 1.23.10.2 1 /* $NetBSD: arp.c,v 1.23.10.2 2001/05/01 12:43:30 he Exp $ */
2
3 /*
4 * Copyright (c) 1984, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Sun Microsystems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\n\
42 The Regents of the University of California. All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
48 #else
49 __RCSID("$NetBSD: arp.c,v 1.23.10.2 2001/05/01 12:43:30 he Exp $");
50 #endif
51 #endif /* not lint */
52
53 /*
54 * arp - display, set, and delete arp table entries
55 */
56
57 #include <sys/param.h>
58 #include <sys/file.h>
59 #include <sys/socket.h>
60 #include <sys/sysctl.h>
61
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/if_ether.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/if_inarp.h>
69 #include <arpa/inet.h>
70
71 #include <err.h>
72 #include <errno.h>
73 #include <netdb.h>
74 #include <nlist.h>
75 #include <paths.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80
81 int delete __P((const char *, const char *));
82 void dump __P((u_long));
83 void delete_all __P((void));
84 void sdl_print __P((const struct sockaddr_dl *));
85 int atosdl __P((const char *s, struct sockaddr_dl *sdl));
86 int file __P((char *));
87 void get __P((const char *));
88 int getinetaddr __P((const char *, struct in_addr *));
89 void getsocket __P((void));
90 int main __P((int, char **));
91 int rtmsg __P((int));
92 int set __P((int, char **));
93 void usage __P((void));
94
95 static int pid;
96 static int aflag, nflag, vflag;
97 static int s = -1;
98
99 int delete __P((const char *, const char *));
100 void dump __P((u_long));
101 void ether_print __P((const u_char *));
102 int file __P((char *));
103 void get __P((const char *));
104 int getinetaddr __P((const char *, struct in_addr *));
105 void getsocket __P((void));
106 int rtmsg __P((int));
107 int set __P((int, char **));
108 void usage __P((void));
109
110 int
111 main(argc, argv)
112 int argc;
113 char **argv;
114 {
115 int ch;
116 int op = 0;
117
118 pid = getpid();
119
120 while ((ch = getopt(argc, argv, "andsfv")) != -1)
121 switch((char)ch) {
122 case 'a':
123 aflag = 1;
124 break;
125 case 'd':
126 case 's':
127 case 'f':
128 if (op)
129 usage();
130 op = ch;
131 break;
132 case 'n':
133 nflag = 1;
134 break;
135 case 'v':
136 vflag = 1;
137 break;
138 default:
139 usage();
140 }
141 argc -= optind;
142 argv += optind;
143
144 if (!op && aflag)
145 op = 'a';
146
147 switch((char)op) {
148 case 'a':
149 dump(0);
150 break;
151 case 'd':
152 if (aflag && argc == 0)
153 delete_all();
154 else {
155 if (aflag || argc < 1 || argc > 2)
156 usage();
157 (void)delete(argv[0], argv[1]);
158 }
159 break;
160 case 's':
161 if (argc < 2 || argc > 5)
162 usage();
163 return (set(argc, argv) ? 1 : 0);
164 case 'f':
165 if (argc != 1)
166 usage();
167 return (file(argv[0]));
168 default:
169 if (argc != 1)
170 usage();
171 get(argv[0]);
172 break;
173 }
174 return (0);
175 }
176
177 /*
178 * Process a file to set standard arp entries
179 */
180 int
181 file(name)
182 char *name;
183 {
184 char line[100], arg[5][50], *args[5];
185 int i, retval;
186 FILE *fp;
187
188 if ((fp = fopen(name, "r")) == NULL)
189 err(1, "cannot open %s", name);
190 args[0] = &arg[0][0];
191 args[1] = &arg[1][0];
192 args[2] = &arg[2][0];
193 args[3] = &arg[3][0];
194 args[4] = &arg[4][0];
195 retval = 0;
196 while (fgets(line, 100, fp) != NULL) {
197 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
198 arg[3], arg[4]);
199 if (i < 2) {
200 warnx("bad line: %s", line);
201 retval = 1;
202 continue;
203 }
204 if (set(i, args))
205 retval = 1;
206 }
207 fclose(fp);
208 return (retval);
209 }
210
211 void
212 getsocket()
213 {
214 if (s >= 0)
215 return;
216 s = socket(PF_ROUTE, SOCK_RAW, 0);
217 if (s < 0)
218 err(1, "socket");
219 }
220
221 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
222 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
223 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
224 int expire_time, flags, export_only, doing_proxy, found_entry;
225 struct {
226 struct rt_msghdr m_rtm;
227 char m_space[512];
228 } m_rtmsg;
229
230 /*
231 * Set an individual arp entry
232 */
233 int
234 set(argc, argv)
235 int argc;
236 char **argv;
237 {
238 struct sockaddr_inarp *sin;
239 struct sockaddr_dl *sdl;
240 struct rt_msghdr *rtm;
241 char *host = argv[0], *eaddr;
242 int rval;
243
244 sin = &sin_m;
245 rtm = &(m_rtmsg.m_rtm);
246 eaddr = argv[1];
247
248 getsocket();
249 argc -= 2;
250 argv += 2;
251 sdl_m = blank_sdl; /* struct copy */
252 sin_m = blank_sin; /* struct copy */
253 if (getinetaddr(host, &sin->sin_addr) == -1)
254 return (1);
255 if (atosdl(eaddr, &sdl_m))
256 warnx("invalid link-level address '%s'", eaddr);
257 doing_proxy = flags = export_only = expire_time = 0;
258 while (argc-- > 0) {
259 if (strncmp(argv[0], "temp", 4) == 0) {
260 struct timeval time;
261 (void)gettimeofday(&time, 0);
262 expire_time = time.tv_sec + 20 * 60;
263 }
264 else if (strncmp(argv[0], "pub", 3) == 0) {
265 flags |= RTF_ANNOUNCE;
266 doing_proxy = SIN_PROXY;
267 } else if (strncmp(argv[0], "trail", 5) == 0) {
268 (void)printf(
269 "%s: Sending trailers is no longer supported\n",
270 host);
271 }
272 argv++;
273 }
274 tryagain:
275 if (rtmsg(RTM_GET) < 0) {
276 warn("%s", host);
277 return (1);
278 }
279 sin = (struct sockaddr_inarp *)(rtm + 1);
280 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
281 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
282 if (sdl->sdl_family == AF_LINK &&
283 (rtm->rtm_flags & RTF_LLINFO) &&
284 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
285 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
286 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
287 goto overwrite;
288 }
289 if (doing_proxy == 0) {
290 (void)printf("set: can only proxy for %s\n", host);
291 return (1);
292 }
293 if (sin_m.sin_other & SIN_PROXY) {
294 (void)printf(
295 "set: proxy entry exists for non 802 device\n");
296 return (1);
297 }
298 sin_m.sin_other = SIN_PROXY;
299 export_only = 1;
300 goto tryagain;
301 }
302 overwrite:
303 if (sdl->sdl_family != AF_LINK) {
304 (void)printf("cannot intuit interface index and type for %s\n",
305 host);
306 return (1);
307 }
308 sdl_m.sdl_type = sdl->sdl_type;
309 sdl_m.sdl_index = sdl->sdl_index;
310 rval = rtmsg(RTM_ADD);
311 if (vflag)
312 (void)printf("%s (%s) added\n", host, eaddr);
313 return (rval);
314 }
315
316 /*
317 * Display an individual arp entry
318 */
319 void
320 get(host)
321 const char *host;
322 {
323 struct sockaddr_inarp *sin;
324
325 sin = &sin_m;
326 sin_m = blank_sin; /* struct copy */
327 if (getinetaddr(host, &sin->sin_addr) == -1)
328 exit(1);
329 dump(sin->sin_addr.s_addr);
330 if (found_entry == 0) {
331 (void)printf("%s (%s) -- no entry\n", host,
332 inet_ntoa(sin->sin_addr));
333 exit(1);
334 }
335 }
336
337 /*
338 * Delete an arp entry
339 */
340 int
341 delete(host, info)
342 const char *host;
343 const char *info;
344 {
345 struct sockaddr_inarp *sin;
346 struct rt_msghdr *rtm;
347 struct sockaddr_dl *sdl;
348
349 sin = &sin_m;
350 rtm = &m_rtmsg.m_rtm;
351
352 if (info && strncmp(info, "pro", 3) )
353 export_only = 1;
354 getsocket();
355 sin_m = blank_sin; /* struct copy */
356 if (getinetaddr(host, &sin->sin_addr) == -1)
357 return (1);
358 tryagain:
359 if (rtmsg(RTM_GET) < 0) {
360 warn("%s", host);
361 return (1);
362 }
363 sin = (struct sockaddr_inarp *)(rtm + 1);
364 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
365 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
366 if (sdl->sdl_family == AF_LINK &&
367 (rtm->rtm_flags & RTF_LLINFO) &&
368 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
369 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
370 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
371 goto delete;
372 }
373 }
374 if (sin_m.sin_other & SIN_PROXY) {
375 warnx("delete: can't locate %s", host);
376 return (1);
377 } else {
378 sin_m.sin_other = SIN_PROXY;
379 goto tryagain;
380 }
381 delete:
382 if (sdl->sdl_family != AF_LINK) {
383 (void)printf("cannot locate %s\n", host);
384 return (1);
385 }
386 if (rtmsg(RTM_DELETE))
387 return (1);
388 if (vflag)
389 (void)printf("%s (%s) deleted\n", host,
390 inet_ntoa(sin->sin_addr));
391 return (0);
392 }
393
394 /*
395 * Dump the entire arp table
396 */
397 void
398 dump(addr)
399 u_long addr;
400 {
401 int mib[6];
402 size_t needed;
403 char *host, *lim, *buf, *next;
404 struct rt_msghdr *rtm;
405 struct sockaddr_inarp *sin;
406 struct sockaddr_dl *sdl;
407 extern int h_errno;
408 struct hostent *hp;
409
410 mib[0] = CTL_NET;
411 mib[1] = PF_ROUTE;
412 mib[2] = 0;
413 mib[3] = AF_INET;
414 mib[4] = NET_RT_FLAGS;
415 mib[5] = RTF_LLINFO;
416 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
417 err(1, "route-sysctl-estimate");
418 if (needed == 0)
419 return;
420 if ((buf = malloc(needed)) == NULL)
421 err(1, "malloc");
422 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
423 err(1, "actual retrieval of routing table");
424 lim = buf + needed;
425 for (next = buf; next < lim; next += rtm->rtm_msglen) {
426 rtm = (struct rt_msghdr *)next;
427 sin = (struct sockaddr_inarp *)(rtm + 1);
428 sdl = (struct sockaddr_dl *)(sin + 1);
429 if (addr) {
430 if (addr != sin->sin_addr.s_addr)
431 continue;
432 found_entry = 1;
433 }
434 if (nflag == 0)
435 hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
436 sizeof sin->sin_addr, AF_INET);
437 else
438 hp = 0;
439 if (hp)
440 host = hp->h_name;
441 else {
442 host = "?";
443 if (h_errno == TRY_AGAIN)
444 nflag = 1;
445 }
446 (void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
447 if (sdl->sdl_alen)
448 sdl_print(sdl);
449 else
450 (void)printf("(incomplete)");
451 if (rtm->rtm_rmx.rmx_expire == 0)
452 (void)printf(" permanent");
453 if (sin->sin_other & SIN_PROXY)
454 (void)printf(" published (proxy only)");
455 if (rtm->rtm_addrs & RTA_NETMASK) {
456 sin = (struct sockaddr_inarp *)
457 (sdl->sdl_len + (char *)sdl);
458 if (sin->sin_addr.s_addr == 0xffffffff)
459 (void)printf(" published");
460 if (sin->sin_len != 8)
461 (void)printf("(wierd)");
462 }
463 (void)printf("\n");
464 }
465 }
466
467 /*
468 * Delete the entire arp table
469 */
470 void
471 delete_all(void)
472 {
473 int mib[6];
474 size_t needed;
475 char addr[sizeof("000.000.000.000\0")];
476 char *lim, *buf, *next;
477 struct rt_msghdr *rtm;
478 struct sockaddr_inarp *sin;
479 struct sockaddr_dl *sdl;
480
481 mib[0] = CTL_NET;
482 mib[1] = PF_ROUTE;
483 mib[2] = 0;
484 mib[3] = AF_INET;
485 mib[4] = NET_RT_FLAGS;
486 mib[5] = RTF_LLINFO;
487 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
488 err(1, "route-sysctl-estimate");
489 if (needed == 0)
490 return;
491 if ((buf = malloc(needed)) == NULL)
492 err(1, "malloc");
493 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
494 err(1, "actual retrieval of routing table");
495 lim = buf + needed;
496 for (next = buf; next < lim; next += rtm->rtm_msglen) {
497 rtm = (struct rt_msghdr *)next;
498 sin = (struct sockaddr_inarp *)(rtm + 1);
499 sdl = (struct sockaddr_dl *)(sin + 1);
500 snprintf(addr, sizeof(addr), "%s", inet_ntoa(sin->sin_addr));
501 delete(addr, NULL);
502 }
503 }
504
505 void
506 sdl_print(sdl)
507 const struct sockaddr_dl *sdl;
508 {
509 int i;
510 u_int8_t *p;
511
512 i = sdl->sdl_alen;
513 p = LLADDR(sdl);
514
515 (void)printf("%02x", *p);
516 while (--i > 0) {
517 putchar(':');
518 (void)printf("%02x", *++p);
519 }
520 }
521
522 int
523 atosdl(s, sdl)
524 const char *s;
525 struct sockaddr_dl *sdl;
526 {
527 int i;
528 long b;
529 caddr_t endp;
530 caddr_t p;
531 char *t, *r;
532
533 p = LLADDR(sdl);
534 endp = ((caddr_t)sdl) + sdl->sdl_len;
535 i = 0;
536
537 b = strtol(s, &t, 16);
538 if (t == s)
539 return 1;
540
541 *p++ = b;
542 ++i;
543 while ((p < endp) && (*t++ == ':')) {
544 b = strtol(t, &r, 16);
545 if (r == t)
546 break;
547 *p++ = b;
548 ++i;
549 t = r;
550 }
551 sdl->sdl_alen = i;
552
553 return 0;
554 }
555
556 void
557 usage()
558 {
559 extern char *__progname;
560
561 (void)fprintf(stderr, "usage: %s [-n] hostname\n", __progname);
562 (void)fprintf(stderr, "usage: %s [-n] -a\n", __progname);
563 (void)fprintf(stderr, "usage: %s -d [-a|hostname]\n", __progname);
564 (void)fprintf(stderr,
565 "usage: %s -s hostname ether_addr [temp] [pub]\n", __progname);
566 (void)fprintf(stderr, "usage: %s -f filename\n", __progname);
567 exit(1);
568 }
569
570 int
571 rtmsg(cmd)
572 int cmd;
573 {
574 static int seq;
575 int rlen;
576 struct rt_msghdr *rtm;
577 char *cp;
578 int l;
579
580 rtm = &m_rtmsg.m_rtm;
581 cp = m_rtmsg.m_space;
582 errno = 0;
583
584 if (cmd == RTM_DELETE)
585 goto doit;
586 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
587 rtm->rtm_flags = flags;
588 rtm->rtm_version = RTM_VERSION;
589
590 switch (cmd) {
591 default:
592 errx(1, "internal wrong cmd");
593 /*NOTREACHED*/
594 case RTM_ADD:
595 rtm->rtm_addrs |= RTA_GATEWAY;
596 rtm->rtm_rmx.rmx_expire = expire_time;
597 rtm->rtm_inits = RTV_EXPIRE;
598 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
599 sin_m.sin_other = 0;
600 if (doing_proxy) {
601 if (export_only)
602 sin_m.sin_other = SIN_PROXY;
603 else {
604 rtm->rtm_addrs |= RTA_NETMASK;
605 rtm->rtm_flags &= ~RTF_HOST;
606 }
607 }
608 /* FALLTHROUGH */
609 case RTM_GET:
610 rtm->rtm_addrs |= RTA_DST;
611 }
612 #define NEXTADDR(w, s) \
613 if (rtm->rtm_addrs & (w)) { \
614 (void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
615
616 NEXTADDR(RTA_DST, sin_m);
617 NEXTADDR(RTA_GATEWAY, sdl_m);
618 NEXTADDR(RTA_NETMASK, so_mask);
619
620 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
621 doit:
622 l = rtm->rtm_msglen;
623 rtm->rtm_seq = ++seq;
624 rtm->rtm_type = cmd;
625 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
626 if (errno != ESRCH || cmd != RTM_DELETE) {
627 warn("writing to routing socket");
628 return (-1);
629 }
630 }
631 do {
632 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
633 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
634 if (l < 0)
635 warn("read from routing socket");
636 return (0);
637 }
638
639 int
640 getinetaddr(host, inap)
641 const char *host;
642 struct in_addr *inap;
643 {
644 struct hostent *hp;
645
646 if (inet_aton(host, inap) == 1)
647 return (0);
648 if ((hp = gethostbyname(host)) == NULL) {
649 warnx("%s: %s\n", host, hstrerror(h_errno));
650 return (-1);
651 }
652 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
653 return (0);
654 }
655