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