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