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