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