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