arp.c revision 1.49 1 1.49 dholland /* $NetBSD: arp.c,v 1.49 2010/06/10 06:03:20 dholland Exp $ */
2 1.10 chopps
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1984, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Sun Microsystems, Inc.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.38 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.47 lukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
38 1.47 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.19 lukem #if 0
43 1.19 lukem static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
44 1.19 lukem #else
45 1.49 dholland __RCSID("$NetBSD: arp.c,v 1.49 2010/06/10 06:03:20 dholland Exp $");
46 1.19 lukem #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * arp - display, set, and delete arp table entries
51 1.1 cgd */
52 1.1 cgd
53 1.1 cgd #include <sys/param.h>
54 1.1 cgd #include <sys/file.h>
55 1.1 cgd #include <sys/socket.h>
56 1.5 mycroft #include <sys/sysctl.h>
57 1.29 fair #include <sys/ioctl.h>
58 1.5 mycroft
59 1.5 mycroft #include <net/if.h>
60 1.5 mycroft #include <net/if_dl.h>
61 1.17 is #include <net/if_ether.h>
62 1.5 mycroft #include <net/if_types.h>
63 1.5 mycroft #include <net/route.h>
64 1.1 cgd #include <netinet/in.h>
65 1.17 is #include <netinet/if_inarp.h>
66 1.2 mycroft #include <arpa/inet.h>
67 1.1 cgd
68 1.15 mikel #include <err.h>
69 1.15 mikel #include <errno.h>
70 1.5 mycroft #include <netdb.h>
71 1.1 cgd #include <nlist.h>
72 1.15 mikel #include <paths.h>
73 1.1 cgd #include <stdio.h>
74 1.8 chopps #include <stdlib.h>
75 1.12 cgd #include <string.h>
76 1.15 mikel #include <unistd.h>
77 1.35 rafal #include <ifaddrs.h>
78 1.1 cgd
79 1.43 dyoung static int is_llinfo(const struct sockaddr_dl *, int);
80 1.44 christos static int delete(const char *, const char *);
81 1.44 christos static void dump(uint32_t);
82 1.44 christos static void delete_all(void);
83 1.44 christos static void sdl_print(const struct sockaddr_dl *);
84 1.44 christos static int getifname(u_int16_t, char *, size_t);
85 1.44 christos static int atosdl(const char *s, struct sockaddr_dl *sdl);
86 1.44 christos static int file(const char *);
87 1.44 christos static void get(const char *);
88 1.44 christos static int getinetaddr(const char *, struct in_addr *);
89 1.44 christos static void getsocket(void);
90 1.44 christos static int rtmsg(int);
91 1.44 christos static int set(int, char **);
92 1.45 perry static void usage(void) __dead;
93 1.17 is
94 1.44 christos static pid_t pid;
95 1.30 atatat static int aflag, nflag, vflag;
96 1.5 mycroft static int s = -1;
97 1.35 rafal static struct ifaddrs* ifaddrs = NULL;
98 1.44 christos static struct sockaddr_in so_mask = {
99 1.44 christos .sin_len = 8,
100 1.44 christos .sin_addr = {
101 1.44 christos .s_addr = 0xffffffff
102 1.44 christos }
103 1.44 christos };
104 1.44 christos static struct sockaddr_inarp blank_sin = {
105 1.44 christos .sin_len = sizeof(blank_sin),
106 1.44 christos .sin_family = AF_INET
107 1.44 christos };
108 1.44 christos static struct sockaddr_inarp sin_m;
109 1.44 christos static struct sockaddr_dl blank_sdl = {
110 1.44 christos .sdl_len = sizeof(blank_sdl),
111 1.44 christos .sdl_family = AF_LINK
112 1.44 christos };
113 1.44 christos static struct sockaddr_dl sdl_m;
114 1.44 christos
115 1.44 christos static int expire_time, flags, export_only, doing_proxy, found_entry;
116 1.44 christos static struct {
117 1.44 christos struct rt_msghdr m_rtm;
118 1.44 christos char m_space[512];
119 1.44 christos } m_rtmsg;
120 1.16 mikel
121 1.8 chopps int
122 1.41 xtraeme main(int argc, char **argv)
123 1.1 cgd {
124 1.1 cgd int ch;
125 1.15 mikel int op = 0;
126 1.34 tv
127 1.34 tv setprogname(argv[0]);
128 1.1 cgd
129 1.5 mycroft pid = getpid();
130 1.15 mikel
131 1.23 mrg while ((ch = getopt(argc, argv, "andsfv")) != -1)
132 1.1 cgd switch((char)ch) {
133 1.5 mycroft case 'a':
134 1.30 atatat aflag = 1;
135 1.30 atatat break;
136 1.1 cgd case 'd':
137 1.15 mikel case 's':
138 1.15 mikel case 'f':
139 1.15 mikel if (op)
140 1.1 cgd usage();
141 1.15 mikel op = ch;
142 1.15 mikel break;
143 1.5 mycroft case 'n':
144 1.5 mycroft nflag = 1;
145 1.8 chopps break;
146 1.23 mrg case 'v':
147 1.23 mrg vflag = 1;
148 1.23 mrg break;
149 1.1 cgd default:
150 1.1 cgd usage();
151 1.1 cgd }
152 1.15 mikel argc -= optind;
153 1.15 mikel argv += optind;
154 1.15 mikel
155 1.30 atatat if (!op && aflag)
156 1.30 atatat op = 'a';
157 1.30 atatat
158 1.15 mikel switch((char)op) {
159 1.15 mikel case 'a':
160 1.15 mikel dump(0);
161 1.15 mikel break;
162 1.15 mikel case 'd':
163 1.30 atatat if (aflag && argc == 0)
164 1.30 atatat delete_all();
165 1.30 atatat else {
166 1.30 atatat if (aflag || argc < 1 || argc > 2)
167 1.30 atatat usage();
168 1.30 atatat (void)delete(argv[0], argv[1]);
169 1.30 atatat }
170 1.15 mikel break;
171 1.15 mikel case 's':
172 1.15 mikel if (argc < 2 || argc > 5)
173 1.15 mikel usage();
174 1.15 mikel return (set(argc, argv) ? 1 : 0);
175 1.15 mikel case 'f':
176 1.15 mikel if (argc != 1)
177 1.15 mikel usage();
178 1.15 mikel return (file(argv[0]));
179 1.15 mikel default:
180 1.15 mikel if (argc != 1)
181 1.15 mikel usage();
182 1.15 mikel get(argv[0]);
183 1.15 mikel break;
184 1.15 mikel }
185 1.8 chopps return (0);
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd /*
189 1.1 cgd * Process a file to set standard arp entries
190 1.1 cgd */
191 1.44 christos static int
192 1.44 christos file(const char *name)
193 1.1 cgd {
194 1.44 christos char *line, *argv[5];
195 1.8 chopps int i, retval;
196 1.1 cgd FILE *fp;
197 1.1 cgd
198 1.49 dholland if (!strcmp(name, "-")) {
199 1.49 dholland fp = stdin;
200 1.49 dholland } else {
201 1.49 dholland fp = fopen(name, "r");
202 1.49 dholland if (fp == NULL) {
203 1.49 dholland err(1, "Cannot open %s", name);
204 1.49 dholland }
205 1.49 dholland }
206 1.1 cgd retval = 0;
207 1.44 christos for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
208 1.44 christos char **ap, *inputstring;
209 1.44 christos
210 1.44 christos inputstring = line;
211 1.44 christos for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
212 1.44 christos (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
213 1.44 christos if (**ap != '\0')
214 1.44 christos ap++;
215 1.44 christos }
216 1.44 christos i = ap - argv;
217 1.1 cgd if (i < 2) {
218 1.8 chopps warnx("bad line: %s", line);
219 1.1 cgd retval = 1;
220 1.1 cgd continue;
221 1.1 cgd }
222 1.44 christos if (set(i, argv))
223 1.1 cgd retval = 1;
224 1.1 cgd }
225 1.49 dholland if (fp != stdin)
226 1.49 dholland (void)fclose(fp);
227 1.44 christos return retval;
228 1.1 cgd }
229 1.1 cgd
230 1.44 christos static void
231 1.41 xtraeme getsocket(void)
232 1.8 chopps {
233 1.8 chopps if (s >= 0)
234 1.8 chopps return;
235 1.8 chopps s = socket(PF_ROUTE, SOCK_RAW, 0);
236 1.8 chopps if (s < 0)
237 1.8 chopps err(1, "socket");
238 1.5 mycroft }
239 1.5 mycroft
240 1.1 cgd /*
241 1.1 cgd * Set an individual arp entry
242 1.1 cgd */
243 1.44 christos static int
244 1.41 xtraeme set(int argc, char **argv)
245 1.1 cgd {
246 1.41 xtraeme struct sockaddr_inarp *sina;
247 1.19 lukem struct sockaddr_dl *sdl;
248 1.19 lukem struct rt_msghdr *rtm;
249 1.8 chopps char *host = argv[0], *eaddr;
250 1.23 mrg int rval;
251 1.8 chopps
252 1.41 xtraeme sina = &sin_m;
253 1.8 chopps rtm = &(m_rtmsg.m_rtm);
254 1.8 chopps eaddr = argv[1];
255 1.1 cgd
256 1.5 mycroft getsocket();
257 1.1 cgd argc -= 2;
258 1.1 cgd argv += 2;
259 1.8 chopps sdl_m = blank_sdl; /* struct copy */
260 1.8 chopps sin_m = blank_sin; /* struct copy */
261 1.41 xtraeme if (getinetaddr(host, &sina->sin_addr) == -1)
262 1.8 chopps return (1);
263 1.17 is if (atosdl(eaddr, &sdl_m))
264 1.17 is warnx("invalid link-level address '%s'", eaddr);
265 1.5 mycroft doing_proxy = flags = export_only = expire_time = 0;
266 1.1 cgd while (argc-- > 0) {
267 1.5 mycroft if (strncmp(argv[0], "temp", 4) == 0) {
268 1.41 xtraeme struct timeval timev;
269 1.41 xtraeme (void)gettimeofday(&timev, 0);
270 1.41 xtraeme expire_time = timev.tv_sec + 20 * 60;
271 1.5 mycroft }
272 1.5 mycroft else if (strncmp(argv[0], "pub", 3) == 0) {
273 1.5 mycroft flags |= RTF_ANNOUNCE;
274 1.5 mycroft doing_proxy = SIN_PROXY;
275 1.46 seanb if (argc && strncmp(argv[1], "pro", 3) == 0) {
276 1.46 seanb export_only = 1;
277 1.46 seanb argc--; argv++;
278 1.46 seanb }
279 1.5 mycroft } else if (strncmp(argv[0], "trail", 5) == 0) {
280 1.44 christos warnx("%s: Sending trailers is no longer supported",
281 1.44 christos host);
282 1.5 mycroft }
283 1.1 cgd argv++;
284 1.1 cgd }
285 1.5 mycroft tryagain:
286 1.5 mycroft if (rtmsg(RTM_GET) < 0) {
287 1.8 chopps warn("%s", host);
288 1.5 mycroft return (1);
289 1.1 cgd }
290 1.44 christos sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
291 1.48 christos sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
292 1.44 christos (char *)(void *)sina);
293 1.41 xtraeme if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
294 1.43 dyoung if (is_llinfo(sdl, rtm->rtm_flags))
295 1.5 mycroft goto overwrite;
296 1.5 mycroft if (doing_proxy == 0) {
297 1.44 christos warnx("set: can only proxy for %s", host);
298 1.5 mycroft return (1);
299 1.5 mycroft }
300 1.5 mycroft if (sin_m.sin_other & SIN_PROXY) {
301 1.44 christos warnx("set: proxy entry exists for non 802 device");
302 1.8 chopps return (1);
303 1.5 mycroft }
304 1.5 mycroft sin_m.sin_other = SIN_PROXY;
305 1.5 mycroft export_only = 1;
306 1.5 mycroft goto tryagain;
307 1.5 mycroft }
308 1.5 mycroft overwrite:
309 1.5 mycroft if (sdl->sdl_family != AF_LINK) {
310 1.44 christos warnx("cannot intuit interface index and type for %s",
311 1.8 chopps host);
312 1.5 mycroft return (1);
313 1.1 cgd }
314 1.5 mycroft sdl_m.sdl_type = sdl->sdl_type;
315 1.5 mycroft sdl_m.sdl_index = sdl->sdl_index;
316 1.23 mrg rval = rtmsg(RTM_ADD);
317 1.23 mrg if (vflag)
318 1.23 mrg (void)printf("%s (%s) added\n", host, eaddr);
319 1.23 mrg return (rval);
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd /*
323 1.1 cgd * Display an individual arp entry
324 1.1 cgd */
325 1.44 christos static void
326 1.41 xtraeme get(const char *host)
327 1.1 cgd {
328 1.41 xtraeme struct sockaddr_inarp *sina;
329 1.1 cgd
330 1.41 xtraeme sina = &sin_m;
331 1.8 chopps sin_m = blank_sin; /* struct copy */
332 1.41 xtraeme if (getinetaddr(host, &sina->sin_addr) == -1)
333 1.8 chopps exit(1);
334 1.41 xtraeme dump(sina->sin_addr.s_addr);
335 1.44 christos if (found_entry == 0)
336 1.44 christos errx(1, "%s (%s) -- no entry", host, inet_ntoa(sina->sin_addr));
337 1.1 cgd }
338 1.1 cgd
339 1.43 dyoung
340 1.43 dyoung static int
341 1.43 dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
342 1.43 dyoung {
343 1.43 dyoung if (sdl->sdl_family != AF_LINK ||
344 1.43 dyoung (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
345 1.43 dyoung return 0;
346 1.43 dyoung
347 1.43 dyoung switch (sdl->sdl_type) {
348 1.43 dyoung case IFT_ETHER:
349 1.43 dyoung case IFT_FDDI:
350 1.43 dyoung case IFT_ISO88023:
351 1.43 dyoung case IFT_ISO88024:
352 1.43 dyoung case IFT_ISO88025:
353 1.43 dyoung case IFT_ARCNET:
354 1.43 dyoung return 1;
355 1.43 dyoung default:
356 1.43 dyoung return 0;
357 1.43 dyoung }
358 1.43 dyoung }
359 1.43 dyoung
360 1.1 cgd /*
361 1.1 cgd * Delete an arp entry
362 1.1 cgd */
363 1.8 chopps int
364 1.41 xtraeme delete(const char *host, const char *info)
365 1.1 cgd {
366 1.41 xtraeme struct sockaddr_inarp *sina;
367 1.19 lukem struct rt_msghdr *rtm;
368 1.5 mycroft struct sockaddr_dl *sdl;
369 1.1 cgd
370 1.41 xtraeme sina = &sin_m;
371 1.8 chopps rtm = &m_rtmsg.m_rtm;
372 1.8 chopps
373 1.5 mycroft getsocket();
374 1.8 chopps sin_m = blank_sin; /* struct copy */
375 1.46 seanb if (info && strncmp(info, "pro", 3) == 0)
376 1.46 seanb sina->sin_other = SIN_PROXY;
377 1.41 xtraeme if (getinetaddr(host, &sina->sin_addr) == -1)
378 1.8 chopps return (1);
379 1.5 mycroft tryagain:
380 1.5 mycroft if (rtmsg(RTM_GET) < 0) {
381 1.8 chopps warn("%s", host);
382 1.5 mycroft return (1);
383 1.5 mycroft }
384 1.44 christos sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
385 1.48 christos sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
386 1.44 christos (char *)(void *)sina);
387 1.43 dyoung if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
388 1.43 dyoung is_llinfo(sdl, rtm->rtm_flags))
389 1.43 dyoung goto delete;
390 1.5 mycroft if (sin_m.sin_other & SIN_PROXY) {
391 1.8 chopps warnx("delete: can't locate %s", host);
392 1.5 mycroft return (1);
393 1.5 mycroft } else {
394 1.5 mycroft sin_m.sin_other = SIN_PROXY;
395 1.5 mycroft goto tryagain;
396 1.5 mycroft }
397 1.5 mycroft delete:
398 1.5 mycroft if (sdl->sdl_family != AF_LINK) {
399 1.44 christos (void)warnx("cannot locate %s", host);
400 1.5 mycroft return (1);
401 1.1 cgd }
402 1.8 chopps if (rtmsg(RTM_DELETE))
403 1.8 chopps return (1);
404 1.23 mrg if (vflag)
405 1.23 mrg (void)printf("%s (%s) deleted\n", host,
406 1.41 xtraeme inet_ntoa(sina->sin_addr));
407 1.8 chopps return (0);
408 1.1 cgd }
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * Dump the entire arp table
412 1.1 cgd */
413 1.8 chopps void
414 1.44 christos dump(uint32_t addr)
415 1.1 cgd {
416 1.5 mycroft int mib[6];
417 1.5 mycroft size_t needed;
418 1.29 fair char ifname[IFNAMSIZ];
419 1.41 xtraeme char *lim, *buf, *next;
420 1.41 xtraeme const char *host;
421 1.5 mycroft struct rt_msghdr *rtm;
422 1.41 xtraeme struct sockaddr_inarp *sina;
423 1.5 mycroft struct sockaddr_dl *sdl;
424 1.1 cgd struct hostent *hp;
425 1.1 cgd
426 1.5 mycroft mib[0] = CTL_NET;
427 1.5 mycroft mib[1] = PF_ROUTE;
428 1.5 mycroft mib[2] = 0;
429 1.5 mycroft mib[3] = AF_INET;
430 1.5 mycroft mib[4] = NET_RT_FLAGS;
431 1.5 mycroft mib[5] = RTF_LLINFO;
432 1.5 mycroft if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
433 1.8 chopps err(1, "route-sysctl-estimate");
434 1.22 fvdl if (needed == 0)
435 1.22 fvdl return;
436 1.5 mycroft if ((buf = malloc(needed)) == NULL)
437 1.8 chopps err(1, "malloc");
438 1.5 mycroft if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
439 1.8 chopps err(1, "actual retrieval of routing table");
440 1.5 mycroft lim = buf + needed;
441 1.5 mycroft for (next = buf; next < lim; next += rtm->rtm_msglen) {
442 1.44 christos rtm = (struct rt_msghdr *)(void *)next;
443 1.44 christos sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
444 1.44 christos sdl = (struct sockaddr_dl *)(void *)
445 1.48 christos (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
446 1.5 mycroft if (addr) {
447 1.41 xtraeme if (addr != sina->sin_addr.s_addr)
448 1.5 mycroft continue;
449 1.5 mycroft found_entry = 1;
450 1.5 mycroft }
451 1.5 mycroft if (nflag == 0)
452 1.44 christos hp = gethostbyaddr((const char *)(void *)
453 1.44 christos &(sina->sin_addr),
454 1.41 xtraeme sizeof sina->sin_addr, AF_INET);
455 1.1 cgd else
456 1.27 christos hp = NULL;
457 1.27 christos
458 1.27 christos host = hp ? hp->h_name : "?";
459 1.27 christos
460 1.41 xtraeme (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
461 1.5 mycroft if (sdl->sdl_alen)
462 1.17 is sdl_print(sdl);
463 1.1 cgd else
464 1.8 chopps (void)printf("(incomplete)");
465 1.29 fair
466 1.29 fair if (sdl->sdl_index) {
467 1.37 itojun if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
468 1.44 christos (void)printf(" on %s", ifname);
469 1.29 fair }
470 1.29 fair
471 1.5 mycroft if (rtm->rtm_rmx.rmx_expire == 0)
472 1.8 chopps (void)printf(" permanent");
473 1.41 xtraeme if (sina->sin_other & SIN_PROXY)
474 1.8 chopps (void)printf(" published (proxy only)");
475 1.5 mycroft if (rtm->rtm_addrs & RTA_NETMASK) {
476 1.44 christos sina = (struct sockaddr_inarp *)(void *)
477 1.48 christos (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
478 1.41 xtraeme if (sina->sin_addr.s_addr == 0xffffffff)
479 1.8 chopps (void)printf(" published");
480 1.41 xtraeme if (sina->sin_len != 8)
481 1.25 erh (void)printf("(weird)");
482 1.5 mycroft }
483 1.8 chopps (void)printf("\n");
484 1.1 cgd }
485 1.39 itojun free(buf);
486 1.1 cgd }
487 1.1 cgd
488 1.30 atatat /*
489 1.30 atatat * Delete the entire arp table
490 1.30 atatat */
491 1.30 atatat void
492 1.30 atatat delete_all(void)
493 1.30 atatat {
494 1.30 atatat int mib[6];
495 1.30 atatat size_t needed;
496 1.30 atatat char addr[sizeof("000.000.000.000\0")];
497 1.30 atatat char *lim, *buf, *next;
498 1.30 atatat struct rt_msghdr *rtm;
499 1.41 xtraeme struct sockaddr_inarp *sina;
500 1.30 atatat
501 1.30 atatat mib[0] = CTL_NET;
502 1.30 atatat mib[1] = PF_ROUTE;
503 1.30 atatat mib[2] = 0;
504 1.30 atatat mib[3] = AF_INET;
505 1.30 atatat mib[4] = NET_RT_FLAGS;
506 1.30 atatat mib[5] = RTF_LLINFO;
507 1.30 atatat if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
508 1.30 atatat err(1, "route-sysctl-estimate");
509 1.30 atatat if (needed == 0)
510 1.30 atatat return;
511 1.30 atatat if ((buf = malloc(needed)) == NULL)
512 1.30 atatat err(1, "malloc");
513 1.30 atatat if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
514 1.30 atatat err(1, "actual retrieval of routing table");
515 1.30 atatat lim = buf + needed;
516 1.30 atatat for (next = buf; next < lim; next += rtm->rtm_msglen) {
517 1.44 christos rtm = (struct rt_msghdr *)(void *)next;
518 1.44 christos sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
519 1.44 christos (void)snprintf(addr, sizeof(addr), "%s",
520 1.44 christos inet_ntoa(sina->sin_addr));
521 1.44 christos (void)delete(addr, NULL);
522 1.30 atatat }
523 1.39 itojun free(buf);
524 1.30 atatat }
525 1.30 atatat
526 1.8 chopps void
527 1.41 xtraeme sdl_print(const struct sockaddr_dl *sdl)
528 1.17 is {
529 1.32 bjh21 char hbuf[NI_MAXHOST];
530 1.17 is
531 1.44 christos if (getnameinfo((const struct sockaddr *)(const void *)sdl,
532 1.44 christos (socklen_t)sdl->sdl_len,
533 1.32 bjh21 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
534 1.44 christos (void)printf("<invalid>");
535 1.32 bjh21 else
536 1.44 christos (void)printf("%s", hbuf);
537 1.17 is }
538 1.17 is
539 1.44 christos static int
540 1.41 xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
541 1.1 cgd {
542 1.17 is int i;
543 1.44 christos unsigned long b;
544 1.44 christos char *endp;
545 1.44 christos char *p;
546 1.17 is char *t, *r;
547 1.17 is
548 1.17 is p = LLADDR(sdl);
549 1.44 christos endp = ((char *)(void *)sdl) + sdl->sdl_len;
550 1.17 is i = 0;
551 1.17 is
552 1.44 christos b = strtoul(ss, &t, 16);
553 1.44 christos if (b > 255 || t == ss)
554 1.17 is return 1;
555 1.17 is
556 1.44 christos *p++ = (char)b;
557 1.17 is ++i;
558 1.17 is while ((p < endp) && (*t++ == ':')) {
559 1.44 christos b = strtoul(t, &r, 16);
560 1.44 christos if (b > 255 || r == t)
561 1.17 is break;
562 1.44 christos *p++ = (char)b;
563 1.17 is ++i;
564 1.17 is t = r;
565 1.17 is }
566 1.17 is sdl->sdl_alen = i;
567 1.17 is
568 1.17 is return 0;
569 1.1 cgd }
570 1.1 cgd
571 1.44 christos static void
572 1.41 xtraeme usage(void)
573 1.1 cgd {
574 1.33 pooka const char *progname;
575 1.20 lukem
576 1.33 pooka progname = getprogname();
577 1.44 christos (void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
578 1.44 christos (void)fprintf(stderr, " %s [-nv] -a\n", progname);
579 1.46 seanb (void)fprintf(stderr, " %s [-v] -d [-a|hostname [pub [proxy]]]\n",
580 1.44 christos progname);
581 1.46 seanb (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub [proxy]]\n",
582 1.44 christos progname);
583 1.44 christos (void)fprintf(stderr, " %s -f filename\n", progname);
584 1.5 mycroft exit(1);
585 1.5 mycroft }
586 1.5 mycroft
587 1.44 christos static int
588 1.41 xtraeme rtmsg(int cmd)
589 1.5 mycroft {
590 1.5 mycroft static int seq;
591 1.19 lukem struct rt_msghdr *rtm;
592 1.19 lukem char *cp;
593 1.19 lukem int l;
594 1.5 mycroft
595 1.8 chopps rtm = &m_rtmsg.m_rtm;
596 1.8 chopps cp = m_rtmsg.m_space;
597 1.5 mycroft errno = 0;
598 1.8 chopps
599 1.5 mycroft if (cmd == RTM_DELETE)
600 1.5 mycroft goto doit;
601 1.8 chopps (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
602 1.5 mycroft rtm->rtm_flags = flags;
603 1.5 mycroft rtm->rtm_version = RTM_VERSION;
604 1.5 mycroft
605 1.5 mycroft switch (cmd) {
606 1.5 mycroft default:
607 1.8 chopps errx(1, "internal wrong cmd");
608 1.8 chopps /*NOTREACHED*/
609 1.5 mycroft case RTM_ADD:
610 1.5 mycroft rtm->rtm_addrs |= RTA_GATEWAY;
611 1.5 mycroft rtm->rtm_rmx.rmx_expire = expire_time;
612 1.5 mycroft rtm->rtm_inits = RTV_EXPIRE;
613 1.5 mycroft rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
614 1.5 mycroft sin_m.sin_other = 0;
615 1.5 mycroft if (doing_proxy) {
616 1.5 mycroft if (export_only)
617 1.5 mycroft sin_m.sin_other = SIN_PROXY;
618 1.5 mycroft else {
619 1.5 mycroft rtm->rtm_addrs |= RTA_NETMASK;
620 1.5 mycroft rtm->rtm_flags &= ~RTF_HOST;
621 1.5 mycroft }
622 1.5 mycroft }
623 1.5 mycroft /* FALLTHROUGH */
624 1.5 mycroft case RTM_GET:
625 1.5 mycroft rtm->rtm_addrs |= RTA_DST;
626 1.5 mycroft }
627 1.25 erh
628 1.5 mycroft #define NEXTADDR(w, s) \
629 1.5 mycroft if (rtm->rtm_addrs & (w)) { \
630 1.44 christos (void)memcpy(cp, &s, \
631 1.44 christos (size_t)((struct sockaddr *)(void *)&s)->sa_len); \
632 1.48 christos RT_ADVANCE(cp, ((struct sockaddr *)(void *)&s)); \
633 1.25 erh }
634 1.5 mycroft
635 1.5 mycroft NEXTADDR(RTA_DST, sin_m);
636 1.5 mycroft NEXTADDR(RTA_GATEWAY, sdl_m);
637 1.5 mycroft NEXTADDR(RTA_NETMASK, so_mask);
638 1.5 mycroft
639 1.44 christos rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
640 1.5 mycroft doit:
641 1.5 mycroft l = rtm->rtm_msglen;
642 1.5 mycroft rtm->rtm_seq = ++seq;
643 1.5 mycroft rtm->rtm_type = cmd;
644 1.44 christos if (write(s, &m_rtmsg, (size_t)l) < 0) {
645 1.5 mycroft if (errno != ESRCH || cmd != RTM_DELETE) {
646 1.8 chopps warn("writing to routing socket");
647 1.5 mycroft return (-1);
648 1.5 mycroft }
649 1.5 mycroft }
650 1.5 mycroft do {
651 1.44 christos l = read(s, &m_rtmsg, sizeof(m_rtmsg));
652 1.5 mycroft } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
653 1.5 mycroft if (l < 0)
654 1.8 chopps warn("read from routing socket");
655 1.5 mycroft return (0);
656 1.5 mycroft }
657 1.5 mycroft
658 1.44 christos static int
659 1.41 xtraeme getinetaddr(const char *host, struct in_addr *inap)
660 1.5 mycroft {
661 1.8 chopps struct hostent *hp;
662 1.8 chopps
663 1.9 chopps if (inet_aton(host, inap) == 1)
664 1.8 chopps return (0);
665 1.8 chopps if ((hp = gethostbyname(host)) == NULL) {
666 1.31 chs warnx("%s: %s", host, hstrerror(h_errno));
667 1.8 chopps return (-1);
668 1.8 chopps }
669 1.8 chopps (void)memcpy(inap, hp->h_addr, sizeof(*inap));
670 1.8 chopps return (0);
671 1.29 fair }
672 1.29 fair
673 1.44 christos static int
674 1.41 xtraeme getifname(u_int16_t ifindex, char *ifname, size_t l)
675 1.29 fair {
676 1.35 rafal int i;
677 1.35 rafal struct ifaddrs *addr;
678 1.29 fair const struct sockaddr_dl *sdl = NULL;
679 1.29 fair
680 1.35 rafal if (ifaddrs == NULL) {
681 1.35 rafal i = getifaddrs(&ifaddrs);
682 1.35 rafal if (i != 0)
683 1.35 rafal err(1, "getifaddrs");
684 1.29 fair }
685 1.29 fair
686 1.35 rafal for (addr = ifaddrs; addr; addr = addr->ifa_next) {
687 1.35 rafal if (addr->ifa_addr == NULL ||
688 1.35 rafal addr->ifa_addr->sa_family != AF_LINK)
689 1.29 fair continue;
690 1.29 fair
691 1.44 christos sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
692 1.29 fair if (sdl && sdl->sdl_index == ifindex) {
693 1.37 itojun (void) strlcpy(ifname, addr->ifa_name, l);
694 1.29 fair return 0;
695 1.29 fair }
696 1.29 fair }
697 1.29 fair
698 1.29 fair return -1;
699 1.1 cgd }
700