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