1 /*
2 * Copyright (c) 1984 Regents of the University of California.
3 * 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 char copyright[] =
39 "@(#) Copyright (c) 1984 Regents of the University of California.\n\
40 All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 static char sccsid[] = "@(#)arp.c 5.11.1.1 (Berkeley) 7/22/91";
45 #endif /* not lint */
46
47 /*
48 * arp - display, set, and delete arp table entries
49 */
50
51 #include <sys/param.h>
52 #include <sys/file.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55
56 #include <netdb.h>
57 #include <netinet/in.h>
58 #include <net/if.h>
59 #include <netinet/if_ether.h>
60
61 #include <errno.h>
62 #include <nlist.h>
63 #include <kvm.h>
64 #include <stdio.h>
65 #include <paths.h>
66
67 extern int errno;
68
main(argc,argv)69 main(argc, argv)
70 int argc;
71 char **argv;
72 {
73 int ch;
74
75 while ((ch = getopt(argc, argv, "adsf")) != EOF)
76 switch((char)ch) {
77 case 'a': {
78 char *mem = 0;
79
80 if (argc > 4)
81 usage();
82 if (argc == 4) {
83 mem = argv[3];
84 }
85 dump((argc >= 3) ? argv[2] : _PATH_UNIX, mem);
86 exit(0);
87 }
88 case 'd':
89 if (argc != 3)
90 usage();
91 delete(argv[2]);
92 exit(0);
93 case 's':
94 if (argc < 4 || argc > 7)
95 usage();
96 exit(set(argc-2, &argv[2]) ? 1 : 0);
97 case 'f':
98 if (argc != 3)
99 usage();
100 exit (file(argv[2]) ? 1 : 0);
101 case '?':
102 default:
103 usage();
104 }
105 if (argc != 2)
106 usage();
107 get(argv[1]);
108 exit(0);
109 }
110
111 /*
112 * Process a file to set standard arp entries
113 */
file(name)114 file(name)
115 char *name;
116 {
117 FILE *fp;
118 int i, retval;
119 char line[100], arg[5][50], *args[5];
120
121 if ((fp = fopen(name, "r")) == NULL) {
122 fprintf(stderr, "arp: cannot open %s\n", name);
123 exit(1);
124 }
125 args[0] = &arg[0][0];
126 args[1] = &arg[1][0];
127 args[2] = &arg[2][0];
128 args[3] = &arg[3][0];
129 args[4] = &arg[4][0];
130 retval = 0;
131 while(fgets(line, 100, fp) != NULL) {
132 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
133 arg[3], arg[4]);
134 if (i < 2) {
135 fprintf(stderr, "arp: bad line: %s\n", line);
136 retval = 1;
137 continue;
138 }
139 if (set(i, args))
140 retval = 1;
141 }
142 fclose(fp);
143 return (retval);
144 }
145
146 /*
147 * Set an individual arp entry
148 */
set(argc,argv)149 set(argc, argv)
150 int argc;
151 char **argv;
152 {
153 struct arpreq ar;
154 struct hostent *hp;
155 struct sockaddr_in *sin;
156 u_char *ea;
157 int s;
158 char *host = argv[0], *eaddr = argv[1];
159
160 argc -= 2;
161 argv += 2;
162 bzero((caddr_t)&ar, sizeof ar);
163 sin = (struct sockaddr_in *)&ar.arp_pa;
164 sin->sin_family = AF_INET;
165 sin->sin_addr.s_addr = inet_addr(host);
166 if (sin->sin_addr.s_addr == -1) {
167 if (!(hp = gethostbyname(host))) {
168 fprintf(stderr, "arp: %s: ", host);
169 herror((char *)NULL);
170 return (1);
171 }
172 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
173 sizeof sin->sin_addr);
174 }
175 ea = (u_char *)ar.arp_ha.sa_data;
176 if (ether_aton(eaddr, ea))
177 return (1);
178 ar.arp_flags = ATF_PERM;
179 while (argc-- > 0) {
180 if (strncmp(argv[0], "temp", 4) == 0)
181 ar.arp_flags &= ~ATF_PERM;
182 else if (strncmp(argv[0], "pub", 3) == 0)
183 ar.arp_flags |= ATF_PUBL;
184 else if (strncmp(argv[0], "trail", 5) == 0)
185 ar.arp_flags |= ATF_USETRAILERS;
186 argv++;
187 }
188
189 s = socket(AF_INET, SOCK_DGRAM, 0);
190 if (s < 0) {
191 perror("arp: socket");
192 exit(1);
193 }
194 if (ioctl(s, SIOCSARP, (caddr_t)&ar) < 0) {
195 perror(host);
196 exit(1);
197 }
198 close(s);
199 return (0);
200 }
201
202 /*
203 * Display an individual arp entry
204 */
get(host)205 get(host)
206 char *host;
207 {
208 struct arpreq ar;
209 struct hostent *hp;
210 struct sockaddr_in *sin;
211 u_char *ea;
212 int s;
213
214 bzero((caddr_t)&ar, sizeof ar);
215 ar.arp_pa.sa_family = AF_INET;
216 sin = (struct sockaddr_in *)&ar.arp_pa;
217 sin->sin_family = AF_INET;
218 sin->sin_addr.s_addr = inet_addr(host);
219 if (sin->sin_addr.s_addr == -1) {
220 if (!(hp = gethostbyname(host))) {
221 fprintf(stderr, "arp: %s: ", host);
222 herror((char *)NULL);
223 exit(1);
224 }
225 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
226 sizeof sin->sin_addr);
227 }
228 s = socket(AF_INET, SOCK_DGRAM, 0);
229 if (s < 0) {
230 perror("arp: socket");
231 exit(1);
232 }
233 if (ioctl(s, SIOCGARP, (caddr_t)&ar) < 0) {
234 if (errno == ENXIO)
235 printf("%s (%s) -- no entry\n",
236 host, inet_ntoa(sin->sin_addr));
237 else
238 perror("SIOCGARP");
239 exit(1);
240 }
241 close(s);
242 ea = (u_char *)ar.arp_ha.sa_data;
243 printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
244 if (ar.arp_flags & ATF_COM)
245 ether_print(ea);
246 else
247 printf("(incomplete)");
248 if (ar.arp_flags & ATF_PERM)
249 printf(" permanent");
250 if (ar.arp_flags & ATF_PUBL)
251 printf(" published");
252 if (ar.arp_flags & ATF_USETRAILERS)
253 printf(" trailers");
254 printf("\n");
255 }
256
257 /*
258 * Delete an arp entry
259 */
delete(host)260 delete(host)
261 char *host;
262 {
263 struct arpreq ar;
264 struct hostent *hp;
265 struct sockaddr_in *sin;
266 int s;
267
268 bzero((caddr_t)&ar, sizeof ar);
269 ar.arp_pa.sa_family = AF_INET;
270 sin = (struct sockaddr_in *)&ar.arp_pa;
271 sin->sin_family = AF_INET;
272 sin->sin_addr.s_addr = inet_addr(host);
273 if (sin->sin_addr.s_addr == -1) {
274 if (!(hp = gethostbyname(host))) {
275 fprintf(stderr, "arp: %s: ", host);
276 herror((char *)NULL);
277 exit(1);
278 }
279 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
280 sizeof sin->sin_addr);
281 }
282 s = socket(AF_INET, SOCK_DGRAM, 0);
283 if (s < 0) {
284 perror("arp: socket");
285 exit(1);
286 }
287 if (ioctl(s, SIOCDARP, (caddr_t)&ar) < 0) {
288 if (errno == ENXIO)
289 printf("%s (%s) -- no entry\n",
290 host, inet_ntoa(sin->sin_addr));
291 else
292 perror("SIOCDARP");
293 exit(1);
294 }
295 close(s);
296 printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
297 }
298
299 struct nlist nl[] = {
300 #define X_ARPTAB 0
301 { "_arptab" },
302 #define X_ARPTAB_SIZE 1
303 { "_arptab_size" },
304 { "" },
305 };
306
307 /*
308 * Dump the entire arp table
309 */
dump(kernel,mem)310 dump(kernel, mem)
311 char *kernel, *mem;
312 {
313 extern int h_errno;
314 struct arptab *at;
315 struct hostent *hp;
316 int bynumber, mf, arptab_size, sz;
317 char *host, *malloc();
318 off_t lseek();
319
320 if (kvm_openfiles(kernel, mem, NULL) == -1) {
321 fprintf(stderr, "arp: kvm_openfiles: %s\n", kvm_geterr());
322 exit(1);
323 }
324 if (kvm_nlist(nl) < 0 || nl[X_ARPTAB_SIZE].n_type == 0) {
325 fprintf(stderr, "arp: %s: bad namelist\n", kernel);
326 exit(1);
327 }
328 if (kvm_read((void *)(nl[X_ARPTAB_SIZE].n_value),
329 &arptab_size, sizeof arptab_size) == -1 ||
330 arptab_size <= 0 || arptab_size > 1000) {
331 fprintf(stderr, "arp: %s: namelist wrong\n", kernel);
332 exit(1);
333 }
334 sz = arptab_size * sizeof (struct arptab);
335 at = (struct arptab *)malloc((u_int)sz);
336 if (at == NULL) {
337 fputs("arp: can't get memory for arptab.\n", stderr);
338 exit(1);
339 }
340 if (kvm_read((void *)(nl[X_ARPTAB].n_value), (char *)at, sz) == -1) {
341 perror("arp: error reading arptab");
342 exit(1);
343 }
344 for (bynumber = 0; arptab_size-- > 0; at++) {
345 if (at->at_iaddr.s_addr == 0 || at->at_flags == 0)
346 continue;
347 if (bynumber == 0)
348 hp = gethostbyaddr((caddr_t)&at->at_iaddr,
349 sizeof at->at_iaddr, AF_INET);
350 else
351 hp = 0;
352 if (hp)
353 host = hp->h_name;
354 else {
355 host = "?";
356 if (h_errno == TRY_AGAIN)
357 bynumber = 1;
358 }
359 printf("%s (%s) at ", host, inet_ntoa(at->at_iaddr));
360 if (at->at_flags & ATF_COM)
361 ether_print(at->at_enaddr);
362 else
363 printf("(incomplete)");
364 if (at->at_flags & ATF_PERM)
365 printf(" permanent");
366 if (at->at_flags & ATF_PUBL)
367 printf(" published");
368 if (at->at_flags & ATF_USETRAILERS)
369 printf(" trailers");
370 printf("\n");
371 }
372 }
373
ether_print(cp)374 ether_print(cp)
375 u_char *cp;
376 {
377 printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
378 }
379
ether_aton(a,n)380 ether_aton(a, n)
381 char *a;
382 u_char *n;
383 {
384 int i, o[6];
385
386 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
387 &o[3], &o[4], &o[5]);
388 if (i != 6) {
389 fprintf(stderr, "arp: invalid Ethernet address '%s'\n", a);
390 return (1);
391 }
392 for (i=0; i<6; i++)
393 n[i] = o[i];
394 return (0);
395 }
396
usage()397 usage()
398 {
399 printf("usage: arp hostname\n");
400 printf(" arp -a [kernel] [kernel_memory]\n");
401 printf(" arp -d hostname\n");
402 printf(" arp -s hostname ether_addr [temp] [pub] [trail]\n");
403 printf(" arp -f filename\n");
404 exit(1);
405 }
406