arp.c revision 1.23.2.2 1 /* $NetBSD: arp.c,v 1.23.2.2 2001/04/01 15:01:19 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.2.2 2001/04/01 15:01:19 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 #include <sys/ioctl.h>
62
63 #include <net/if.h>
64 #include <net/if_dl.h>
65 #include <net/if_ether.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/if_inarp.h>
70 #include <arpa/inet.h>
71
72 #include <err.h>
73 #include <errno.h>
74 #include <netdb.h>
75 #include <nlist.h>
76 #include <paths.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81
82 int delete __P((const char *, const char *));
83 void dump __P((u_long));
84 void sdl_print __P((const struct sockaddr_dl *));
85 int getifname __P((u_int16_t, char *));
86 int atosdl __P((const char *s, struct sockaddr_dl *sdl));
87 int file __P((char *));
88 void get __P((const char *));
89 int getinetaddr __P((const char *, struct in_addr *));
90 void getsocket __P((void));
91 int main __P((int, char **));
92 int rtmsg __P((int));
93 int set __P((int, char **));
94 void usage __P((void));
95
96 static int pid;
97 static int nflag, vflag;
98 static int s = -1;
99 static int is = -1;
100
101 static struct ifconf ifc;
102 static char ifconfbuf[8192];
103 static char *progname;
104
105 int delete __P((const char *, const char *));
106 void dump __P((u_long));
107 void ether_print __P((const u_char *));
108 int file __P((char *));
109 void get __P((const char *));
110 int getinetaddr __P((const char *, struct in_addr *));
111 void getsocket __P((void));
112 int rtmsg __P((int));
113 int set __P((int, char **));
114 void usage __P((void));
115
116 int
117 main(argc, argv)
118 int argc;
119 char **argv;
120 {
121 int ch;
122 int op = 0;
123
124 pid = getpid();
125 progname = ((progname = strrchr(argv[0], '/')) ? progname + 1 : argv[0]);
126
127 while ((ch = getopt(argc, argv, "andsfv")) != -1)
128 switch((char)ch) {
129 case 'a':
130 case 'd':
131 case 's':
132 case 'f':
133 if (op)
134 usage();
135 op = ch;
136 break;
137 case 'n':
138 nflag = 1;
139 break;
140 case 'v':
141 vflag = 1;
142 break;
143 default:
144 usage();
145 }
146 argc -= optind;
147 argv += optind;
148
149 switch((char)op) {
150 case 'a':
151 dump(0);
152 break;
153 case 'd':
154 if (argc < 1 || argc > 2)
155 usage();
156 (void)delete(argv[0], argv[1]);
157 break;
158 case 's':
159 if (argc < 2 || argc > 5)
160 usage();
161 return (set(argc, argv) ? 1 : 0);
162 case 'f':
163 if (argc != 1)
164 usage();
165 return (file(argv[0]));
166 default:
167 if (argc != 1)
168 usage();
169 get(argv[0]);
170 break;
171 }
172 return (0);
173 }
174
175 /*
176 * Process a file to set standard arp entries
177 */
178 int
179 file(name)
180 char *name;
181 {
182 char line[100], arg[5][50], *args[5];
183 int i, retval;
184 FILE *fp;
185
186 if ((fp = fopen(name, "r")) == NULL)
187 err(1, "cannot open %s", name);
188 args[0] = &arg[0][0];
189 args[1] = &arg[1][0];
190 args[2] = &arg[2][0];
191 args[3] = &arg[3][0];
192 args[4] = &arg[4][0];
193 retval = 0;
194 while (fgets(line, 100, fp) != NULL) {
195 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
196 arg[3], arg[4]);
197 if (i < 2) {
198 warnx("bad line: %s", line);
199 retval = 1;
200 continue;
201 }
202 if (set(i, args))
203 retval = 1;
204 }
205 fclose(fp);
206 return (retval);
207 }
208
209 void
210 getsocket()
211 {
212 if (s >= 0)
213 return;
214 s = socket(PF_ROUTE, SOCK_RAW, 0);
215 if (s < 0)
216 err(1, "socket");
217 }
218
219 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
220 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
221 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
222 int expire_time, flags, export_only, doing_proxy, found_entry;
223 struct {
224 struct rt_msghdr m_rtm;
225 char m_space[512];
226 } m_rtmsg;
227
228 /*
229 * Set an individual arp entry
230 */
231 int
232 set(argc, argv)
233 int argc;
234 char **argv;
235 {
236 struct sockaddr_inarp *sin;
237 struct sockaddr_dl *sdl;
238 struct rt_msghdr *rtm;
239 char *host = argv[0], *eaddr;
240 int rval;
241
242 sin = &sin_m;
243 rtm = &(m_rtmsg.m_rtm);
244 eaddr = argv[1];
245
246 getsocket();
247 argc -= 2;
248 argv += 2;
249 sdl_m = blank_sdl; /* struct copy */
250 sin_m = blank_sin; /* struct copy */
251 if (getinetaddr(host, &sin->sin_addr) == -1)
252 return (1);
253 if (atosdl(eaddr, &sdl_m))
254 warnx("invalid link-level address '%s'", eaddr);
255 doing_proxy = flags = export_only = expire_time = 0;
256 while (argc-- > 0) {
257 if (strncmp(argv[0], "temp", 4) == 0) {
258 struct timeval time;
259 (void)gettimeofday(&time, 0);
260 expire_time = time.tv_sec + 20 * 60;
261 }
262 else if (strncmp(argv[0], "pub", 3) == 0) {
263 flags |= RTF_ANNOUNCE;
264 doing_proxy = SIN_PROXY;
265 } else if (strncmp(argv[0], "trail", 5) == 0) {
266 (void)printf(
267 "%s: Sending trailers is no longer supported\n",
268 host);
269 }
270 argv++;
271 }
272 tryagain:
273 if (rtmsg(RTM_GET) < 0) {
274 warn("%s", host);
275 return (1);
276 }
277 sin = (struct sockaddr_inarp *)(rtm + 1);
278 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
279 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
280 if (sdl->sdl_family == AF_LINK &&
281 (rtm->rtm_flags & RTF_LLINFO) &&
282 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
283 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
284 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
285 goto overwrite;
286 }
287 if (doing_proxy == 0) {
288 (void)printf("set: can only proxy for %s\n", host);
289 return (1);
290 }
291 if (sin_m.sin_other & SIN_PROXY) {
292 (void)printf(
293 "set: proxy entry exists for non 802 device\n");
294 return (1);
295 }
296 sin_m.sin_other = SIN_PROXY;
297 export_only = 1;
298 goto tryagain;
299 }
300 overwrite:
301 if (sdl->sdl_family != AF_LINK) {
302 (void)printf("cannot intuit interface index and type for %s\n",
303 host);
304 return (1);
305 }
306 sdl_m.sdl_type = sdl->sdl_type;
307 sdl_m.sdl_index = sdl->sdl_index;
308 rval = rtmsg(RTM_ADD);
309 if (vflag)
310 (void)printf("%s (%s) added\n", host, eaddr);
311 return (rval);
312 }
313
314 /*
315 * Display an individual arp entry
316 */
317 void
318 get(host)
319 const char *host;
320 {
321 struct sockaddr_inarp *sin;
322
323 sin = &sin_m;
324 sin_m = blank_sin; /* struct copy */
325 if (getinetaddr(host, &sin->sin_addr) == -1)
326 exit(1);
327 dump(sin->sin_addr.s_addr);
328 if (found_entry == 0) {
329 (void)printf("%s (%s) -- no entry\n", host,
330 inet_ntoa(sin->sin_addr));
331 exit(1);
332 }
333 }
334
335 /*
336 * Delete an arp entry
337 */
338 int
339 delete(host, info)
340 const char *host;
341 const char *info;
342 {
343 struct sockaddr_inarp *sin;
344 struct rt_msghdr *rtm;
345 struct sockaddr_dl *sdl;
346
347 sin = &sin_m;
348 rtm = &m_rtmsg.m_rtm;
349
350 if (info && strncmp(info, "pro", 3) )
351 export_only = 1;
352 getsocket();
353 sin_m = blank_sin; /* struct copy */
354 if (getinetaddr(host, &sin->sin_addr) == -1)
355 return (1);
356 tryagain:
357 if (rtmsg(RTM_GET) < 0) {
358 warn("%s", host);
359 return (1);
360 }
361 sin = (struct sockaddr_inarp *)(rtm + 1);
362 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
363 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
364 if (sdl->sdl_family == AF_LINK &&
365 (rtm->rtm_flags & RTF_LLINFO) &&
366 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
367 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
368 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
369 goto delete;
370 }
371 }
372 if (sin_m.sin_other & SIN_PROXY) {
373 warnx("delete: can't locate %s", host);
374 return (1);
375 } else {
376 sin_m.sin_other = SIN_PROXY;
377 goto tryagain;
378 }
379 delete:
380 if (sdl->sdl_family != AF_LINK) {
381 (void)printf("cannot locate %s\n", host);
382 return (1);
383 }
384 if (rtmsg(RTM_DELETE))
385 return (1);
386 if (vflag)
387 (void)printf("%s (%s) deleted\n", host,
388 inet_ntoa(sin->sin_addr));
389 return (0);
390 }
391
392 /*
393 * Dump the entire arp table
394 */
395 void
396 dump(addr)
397 u_long addr;
398 {
399 int mib[6];
400 size_t needed;
401 char ifname[IFNAMSIZ];
402 char *host, *lim, *buf, *next;
403 struct rt_msghdr *rtm;
404 struct sockaddr_inarp *sin;
405 struct sockaddr_dl *sdl;
406 extern int h_errno;
407 struct hostent *hp;
408
409 mib[0] = CTL_NET;
410 mib[1] = PF_ROUTE;
411 mib[2] = 0;
412 mib[3] = AF_INET;
413 mib[4] = NET_RT_FLAGS;
414 mib[5] = RTF_LLINFO;
415 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
416 err(1, "route-sysctl-estimate");
417 if (needed == 0)
418 return;
419 if ((buf = malloc(needed)) == NULL)
420 err(1, "malloc");
421 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
422 err(1, "actual retrieval of routing table");
423 lim = buf + needed;
424 for (next = buf; next < lim; next += rtm->rtm_msglen) {
425 rtm = (struct rt_msghdr *)next;
426 sin = (struct sockaddr_inarp *)(rtm + 1);
427 sdl = (struct sockaddr_dl *)(sin + 1);
428 if (addr) {
429 if (addr != sin->sin_addr.s_addr)
430 continue;
431 found_entry = 1;
432 }
433 if (nflag == 0)
434 hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
435 sizeof sin->sin_addr, AF_INET);
436 else
437 hp = 0;
438 if (hp)
439 host = hp->h_name;
440 else {
441 host = "?";
442 if (h_errno == TRY_AGAIN)
443 nflag = 1;
444 }
445 (void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
446 if (sdl->sdl_alen)
447 sdl_print(sdl);
448 else
449 (void)printf("(incomplete)");
450
451 if (sdl->sdl_index) {
452 if (getifname(sdl->sdl_index, ifname) == 0)
453 printf(" on %s", ifname);
454 }
455
456 if (rtm->rtm_rmx.rmx_expire == 0)
457 (void)printf(" permanent");
458 if (sin->sin_other & SIN_PROXY)
459 (void)printf(" published (proxy only)");
460 if (rtm->rtm_addrs & RTA_NETMASK) {
461 sin = (struct sockaddr_inarp *)
462 (sdl->sdl_len + (char *)sdl);
463 if (sin->sin_addr.s_addr == 0xffffffff)
464 (void)printf(" published");
465 if (sin->sin_len != 8)
466 (void)printf("(wierd)");
467 }
468 (void)printf("\n");
469 }
470 }
471
472 void
473 sdl_print(sdl)
474 const struct sockaddr_dl *sdl;
475 {
476 int i;
477 u_int8_t *p;
478
479 i = sdl->sdl_alen;
480 p = LLADDR(sdl);
481
482 (void)printf("%02x", *p);
483 while (--i > 0) {
484 putchar(':');
485 (void)printf("%02x", *++p);
486 }
487 }
488
489 int
490 atosdl(s, sdl)
491 const char *s;
492 struct sockaddr_dl *sdl;
493 {
494 int i;
495 long b;
496 caddr_t endp;
497 caddr_t p;
498 char *t, *r;
499
500 p = LLADDR(sdl);
501 endp = ((caddr_t)sdl) + sdl->sdl_len;
502 i = 0;
503
504 b = strtol(s, &t, 16);
505 if (t == s)
506 return 1;
507
508 *p++ = b;
509 ++i;
510 while ((p < endp) && (*t++ == ':')) {
511 b = strtol(t, &r, 16);
512 if (r == t)
513 break;
514 *p++ = b;
515 ++i;
516 t = r;
517 }
518 sdl->sdl_alen = i;
519
520 return 0;
521 }
522
523 void
524 usage()
525 {
526
527 (void)fprintf(stderr, "usage: %s [-n] hostname\n", progname);
528 (void)fprintf(stderr, "usage: %s [-n] -a\n", progname);
529 (void)fprintf(stderr, "usage: %s -d hostname\n", progname);
530 (void)fprintf(stderr,
531 "usage: %s -s hostname ether_addr [temp] [pub]\n", progname);
532 (void)fprintf(stderr, "usage: %s -f filename\n", progname);
533 exit(1);
534 }
535
536 int
537 rtmsg(cmd)
538 int cmd;
539 {
540 static int seq;
541 int rlen;
542 struct rt_msghdr *rtm;
543 char *cp;
544 int l;
545
546 rtm = &m_rtmsg.m_rtm;
547 cp = m_rtmsg.m_space;
548 errno = 0;
549
550 if (cmd == RTM_DELETE)
551 goto doit;
552 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
553 rtm->rtm_flags = flags;
554 rtm->rtm_version = RTM_VERSION;
555
556 switch (cmd) {
557 default:
558 errx(1, "internal wrong cmd");
559 /*NOTREACHED*/
560 case RTM_ADD:
561 rtm->rtm_addrs |= RTA_GATEWAY;
562 rtm->rtm_rmx.rmx_expire = expire_time;
563 rtm->rtm_inits = RTV_EXPIRE;
564 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
565 sin_m.sin_other = 0;
566 if (doing_proxy) {
567 if (export_only)
568 sin_m.sin_other = SIN_PROXY;
569 else {
570 rtm->rtm_addrs |= RTA_NETMASK;
571 rtm->rtm_flags &= ~RTF_HOST;
572 }
573 }
574 /* FALLTHROUGH */
575 case RTM_GET:
576 rtm->rtm_addrs |= RTA_DST;
577 }
578 #define NEXTADDR(w, s) \
579 if (rtm->rtm_addrs & (w)) { \
580 (void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
581
582 NEXTADDR(RTA_DST, sin_m);
583 NEXTADDR(RTA_GATEWAY, sdl_m);
584 NEXTADDR(RTA_NETMASK, so_mask);
585
586 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
587 doit:
588 l = rtm->rtm_msglen;
589 rtm->rtm_seq = ++seq;
590 rtm->rtm_type = cmd;
591 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
592 if (errno != ESRCH || cmd != RTM_DELETE) {
593 warn("writing to routing socket");
594 return (-1);
595 }
596 }
597 do {
598 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
599 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
600 if (l < 0)
601 warn("read from routing socket");
602 return (0);
603 }
604
605 int
606 getinetaddr(host, inap)
607 const char *host;
608 struct in_addr *inap;
609 {
610 struct hostent *hp;
611
612 if (inet_aton(host, inap) == 1)
613 return (0);
614 if ((hp = gethostbyname(host)) == NULL) {
615 warnx("%s: %s\n", host, hstrerror(h_errno));
616 return (-1);
617 }
618 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
619 return (0);
620 }
621
622 int
623 getifname(ifindex, ifname)
624 u_int16_t ifindex;
625 char* ifname;
626 {
627 int i, idx, siz;
628 char ifrbuf[8192];
629 struct ifreq ifreq, *ifr;
630 const struct sockaddr_dl *sdl = NULL;
631
632 if (is < 0) {
633 is = socket(PF_INET, SOCK_DGRAM, 0);
634 if (is < 0)
635 err(1, "socket");
636
637 ifc.ifc_len = sizeof(ifconfbuf);
638 ifc.ifc_buf = ifconfbuf;
639
640 if (ioctl(is, SIOCGIFCONF, &ifc) < 0) {
641 close(is);
642 err(1, "SIOCGIFCONF");
643 is = -1;
644 }
645 }
646
647 ifr = ifc.ifc_req;
648 ifreq.ifr_name[0] = '\0';
649 for (i = 0, idx = 0; i < ifc.ifc_len; ) {
650 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
651 siz = sizeof(ifr->ifr_name) +
652 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
653 ? ifr->ifr_addr.sa_len
654 : sizeof(struct sockaddr));
655 i += siz;
656 /* avoid alignment issue */
657 if (sizeof(ifrbuf) < siz)
658 errx(1, "ifr too big");
659
660 memcpy(ifrbuf, ifr, siz);
661 ifr = (struct ifreq *)ifrbuf;
662
663 if (ifr->ifr_addr.sa_family != AF_LINK)
664 continue;
665
666 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
667 if (sdl && sdl->sdl_index == ifindex) {
668 (void) strncpy(ifname, ifr->ifr_name, IFNAMSIZ);
669 return 0;
670 }
671 }
672
673 return -1;
674 }
675