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