]> git.saurik.com Git - apple/network_cmds.git/blame - arp.tproj/arp.c
network_cmds-245.16.tar.gz
[apple/network_cmds.git] / arp.tproj / arp.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1984, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Sun Microsystems, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
ac2f15b3 38static char const copyright[] =
b7080c8e
A
39"@(#) Copyright (c) 1984, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
ac2f15b3
A
44#if 0
45static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94";
46#endif
47static const char rcsid[] =
48 "$FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.10 2002/06/18 00:16:59 kbyanc Exp $";
b7080c8e
A
49#endif /* not lint */
50
51/*
52 * arp - display, set, and delete arp table entries
53 */
54
55
56#include <sys/param.h>
57#include <sys/file.h>
58#include <sys/socket.h>
ac2f15b3 59#include <sys/sockio.h>
b7080c8e 60#include <sys/sysctl.h>
ac2f15b3
A
61#include <sys/ioctl.h>
62#include <sys/time.h>
b7080c8e
A
63
64#include <net/if.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67#include <net/route.h>
68
69#include <netinet/in.h>
70#include <netinet/if_ether.h>
71
72#include <arpa/inet.h>
73
74#include <err.h>
75#include <errno.h>
76#include <netdb.h>
77#include <nlist.h>
78#include <paths.h>
79#include <stdio.h>
80#include <stdlib.h>
ac2f15b3 81#include <strings.h>
b7080c8e
A
82#include <unistd.h>
83
ac2f15b3
A
84#include <sys/types.h>
85#include <sys/socket.h>
86#include <net/ethernet.h>
87
88void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
89 struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
90void print_entry(struct sockaddr_dl *sdl,
91 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
92void nuke_entry(struct sockaddr_dl *sdl,
93 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
94int delete(char *host, char *info);
95void usage(void);
96int set(int argc, char **argv);
97int get(char *host);
98int file(char *name);
99void getsocket(void);
100int my_ether_aton(char *a, struct ether_addr *n);
101int rtmsg(int cmd);
102int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
103
b7080c8e 104static int pid;
ac2f15b3
A
105static int nflag; /* no reverse dns lookups */
106static int aflag; /* do it for all entries */
b7080c8e
A
107static int s = -1;
108
ac2f15b3
A
109struct sockaddr_in so_mask;
110struct sockaddr_inarp blank_sin, sin_m;
111struct sockaddr_dl blank_sdl, sdl_m;
112int expire_time, flags, doing_proxy, proxy_only, found_entry;
113struct {
114 struct rt_msghdr m_rtm;
115 char m_space[512];
116} m_rtmsg;
117
118/* which function we're supposed to do */
119#define F_GET 1
120#define F_SET 2
121#define F_FILESET 3
122#define F_REPLACE 4
123#define F_DELETE 5
124
125#define ROUNDUP(a) \
126 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
127#define SETFUNC(f) { if (func) usage(); func = (f); }
b7080c8e
A
128
129int
ac2f15b3 130main(int argc, char *argv[])
b7080c8e 131{
ac2f15b3
A
132 int ch, func = 0;
133 int rtn = 0;
b7080c8e
A
134
135 pid = getpid();
ac2f15b3 136 while ((ch = getopt(argc, argv, "andfsS")) != -1)
b7080c8e
A
137 switch((char)ch) {
138 case 'a':
8052502f
A
139 aflag = 1;
140 break;
b7080c8e 141 case 'd':
ac2f15b3
A
142 SETFUNC(F_DELETE);
143 break;
b7080c8e
A
144 case 'n':
145 nflag = 1;
8052502f 146 break;
ac2f15b3
A
147 case 'S':
148 SETFUNC(F_REPLACE);
149 break;
b7080c8e 150 case 's':
ac2f15b3
A
151 SETFUNC(F_SET);
152 break;
153 case 'f' :
154 SETFUNC(F_FILESET);
155 break;
b7080c8e
A
156 case '?':
157 default:
158 usage();
159 }
ac2f15b3
A
160 argc -= optind;
161 argv += optind;
162
163 bzero(&so_mask, sizeof(so_mask));
164 so_mask.sin_len = 8;
165 so_mask.sin_addr.s_addr = 0xffffffff;
166 bzero(&blank_sin, sizeof(blank_sin));
167 blank_sin.sin_len = sizeof(blank_sin);
168 blank_sin.sin_family = AF_INET;
169 bzero(&blank_sdl, sizeof(blank_sdl));
170 blank_sdl.sdl_len = sizeof(blank_sdl);
171 blank_sdl.sdl_family = AF_LINK;
172
173 if (!func)
174 func = F_GET;
175 switch (func) {
176 case F_GET:
177 if (aflag) {
178 if (argc != 0)
179 usage();
180 search(0, print_entry);
181 } else {
182 if (argc != 1)
183 usage();
184 get(argv[0]);
185 }
186 break;
187 case F_SET:
188 case F_REPLACE:
189 if (argc < 2 || argc > 6)
190 usage();
191 if (func == F_REPLACE)
192 (void) delete(argv[0], NULL);
193 rtn = set(argc, argv) ? 1 : 0;
194 break;
195 case F_DELETE:
196 if (aflag) {
197 if (argc != 0)
198 usage();
199 search(0, nuke_entry);
200 } else {
201 if (argc < 1 || argc > 2)
202 usage();
203 rtn = delete(argv[0], argv[1]);
204 }
205 break;
206 case F_FILESET:
207 if (argc != 1)
208 usage();
209 rtn = file(argv[0]);
210 break;
8052502f 211 }
ac2f15b3
A
212
213 return(rtn);
b7080c8e
A
214}
215
216/*
217 * Process a file to set standard arp entries
218 */
219int
ac2f15b3 220file(char *name)
b7080c8e
A
221{
222 FILE *fp;
223 int i, retval;
224 char line[100], arg[5][50], *args[5];
225
ac2f15b3
A
226 if ((fp = fopen(name, "r")) == NULL)
227 errx(1, "cannot open %s", name);
b7080c8e
A
228 args[0] = &arg[0][0];
229 args[1] = &arg[1][0];
230 args[2] = &arg[2][0];
231 args[3] = &arg[3][0];
232 args[4] = &arg[4][0];
233 retval = 0;
234 while(fgets(line, 100, fp) != NULL) {
ac2f15b3
A
235 i = sscanf(line, "%49s %49s %49s %49s %49s", arg[0], arg[1],
236 arg[2], arg[3], arg[4]);
b7080c8e 237 if (i < 2) {
ac2f15b3 238 warnx("bad line: %s", line);
b7080c8e
A
239 retval = 1;
240 continue;
241 }
242 if (set(i, args))
243 retval = 1;
244 }
245 fclose(fp);
246 return (retval);
247}
248
249void
ac2f15b3
A
250getsocket(void)
251{
b7080c8e
A
252 if (s < 0) {
253 s = socket(PF_ROUTE, SOCK_RAW, 0);
ac2f15b3
A
254 if (s < 0)
255 err(1, "socket");
b7080c8e
A
256 }
257}
258
b7080c8e 259/*
ac2f15b3 260 * Set an individual arp entry
b7080c8e
A
261 */
262int
ac2f15b3 263set(int argc, char **argv)
b7080c8e
A
264{
265 struct hostent *hp;
ac2f15b3 266 register struct sockaddr_inarp *addr = &sin_m;
b7080c8e
A
267 register struct sockaddr_dl *sdl;
268 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
ac2f15b3 269 struct ether_addr *ea;
b7080c8e
A
270 char *host = argv[0], *eaddr = argv[1];
271
272 getsocket();
273 argc -= 2;
274 argv += 2;
275 sdl_m = blank_sdl;
276 sin_m = blank_sin;
ac2f15b3
A
277 addr->sin_addr.s_addr = inet_addr(host);
278 if (addr->sin_addr.s_addr == INADDR_NONE) {
b7080c8e 279 if (!(hp = gethostbyname(host))) {
ac2f15b3 280 warnx("%s: %s", host, hstrerror(h_errno));
b7080c8e
A
281 return (1);
282 }
ac2f15b3
A
283 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
284 sizeof addr->sin_addr);
b7080c8e 285 }
ac2f15b3 286 doing_proxy = flags = proxy_only = expire_time = 0;
b7080c8e
A
287 while (argc-- > 0) {
288 if (strncmp(argv[0], "temp", 4) == 0) {
ac2f15b3
A
289 struct timeval tv;
290 gettimeofday(&tv, 0);
291 expire_time = tv.tv_sec + 20 * 60;
b7080c8e
A
292 }
293 else if (strncmp(argv[0], "pub", 3) == 0) {
294 flags |= RTF_ANNOUNCE;
ac2f15b3
A
295 doing_proxy = 1;
296 if (argc && strncmp(argv[1], "only", 3) == 0) {
297 proxy_only = 1;
298 sin_m.sin_other = SIN_PROXY;
299 argc--; argv++;
300 }
b7080c8e
A
301 } else if (strncmp(argv[0], "trail", 5) == 0) {
302 printf("%s: Sending trailers is no longer supported\n",
303 host);
304 }
305 argv++;
306 }
ac2f15b3
A
307 ea = (struct ether_addr *)LLADDR(&sdl_m);
308 if (doing_proxy && !strcmp(eaddr, "auto")) {
309 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
310 printf("no interface found for %s\n",
311 inet_ntoa(addr->sin_addr));
312 return (1);
313 }
314 sdl_m.sdl_alen = ETHER_ADDR_LEN;
315 } else {
316 if (my_ether_aton(eaddr, ea) == 0)
317 sdl_m.sdl_alen = ETHER_ADDR_LEN;
318 }
b7080c8e
A
319tryagain:
320 if (rtmsg(RTM_GET) < 0) {
ac2f15b3 321 warn("%s", host);
b7080c8e
A
322 return (1);
323 }
ac2f15b3
A
324 addr = (struct sockaddr_inarp *)(rtm + 1);
325 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
326 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
b7080c8e
A
327 if (sdl->sdl_family == AF_LINK &&
328 (rtm->rtm_flags & RTF_LLINFO) &&
329 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
330 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
ac2f15b3 331 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
b7080c8e
A
332 goto overwrite;
333 }
334 if (doing_proxy == 0) {
335 printf("set: can only proxy for %s\n", host);
336 return (1);
337 }
338 if (sin_m.sin_other & SIN_PROXY) {
339 printf("set: proxy entry exists for non 802 device\n");
340 return(1);
341 }
342 sin_m.sin_other = SIN_PROXY;
ac2f15b3 343 proxy_only = 1;
b7080c8e
A
344 goto tryagain;
345 }
346overwrite:
347 if (sdl->sdl_family != AF_LINK) {
348 printf("cannot intuit interface index and type for %s\n", host);
349 return (1);
350 }
351 sdl_m.sdl_type = sdl->sdl_type;
352 sdl_m.sdl_index = sdl->sdl_index;
353 return (rtmsg(RTM_ADD));
354}
355
356/*
357 * Display an individual arp entry
358 */
ac2f15b3
A
359int
360get(char *host)
b7080c8e
A
361{
362 struct hostent *hp;
ac2f15b3 363 struct sockaddr_inarp *addr = &sin_m;
b7080c8e
A
364
365 sin_m = blank_sin;
ac2f15b3
A
366 addr->sin_addr.s_addr = inet_addr(host);
367 if (addr->sin_addr.s_addr == INADDR_NONE) {
368 if (!(hp = gethostbyname(host)))
369 errx(1, "%s: %s", host, hstrerror(h_errno));
370 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
371 sizeof addr->sin_addr);
b7080c8e 372 }
ac2f15b3 373 search(addr->sin_addr.s_addr, print_entry);
b7080c8e
A
374 if (found_entry == 0) {
375 printf("%s (%s) -- no entry\n",
ac2f15b3
A
376 host, inet_ntoa(addr->sin_addr));
377 return(1);
b7080c8e 378 }
ac2f15b3 379 return(0);
b7080c8e
A
380}
381
382/*
ac2f15b3 383 * Delete an arp entry
b7080c8e
A
384 */
385int
ac2f15b3 386delete(char *host, char *info)
b7080c8e
A
387{
388 struct hostent *hp;
ac2f15b3 389 register struct sockaddr_inarp *addr = &sin_m;
b7080c8e
A
390 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
391 struct sockaddr_dl *sdl;
392
b7080c8e
A
393 getsocket();
394 sin_m = blank_sin;
ac2f15b3
A
395 if (info) {
396 if (strncmp(info, "pub", 3) == 0)
397 sin_m.sin_other = SIN_PROXY;
398 else
399 usage();
400 }
401 addr->sin_addr.s_addr = inet_addr(host);
402 if (addr->sin_addr.s_addr == INADDR_NONE) {
b7080c8e 403 if (!(hp = gethostbyname(host))) {
ac2f15b3 404 warnx("%s: %s", host, hstrerror(h_errno));
b7080c8e
A
405 return (1);
406 }
ac2f15b3
A
407 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
408 sizeof addr->sin_addr);
b7080c8e
A
409 }
410tryagain:
411 if (rtmsg(RTM_GET) < 0) {
ac2f15b3 412 warn("%s", host);
b7080c8e
A
413 return (1);
414 }
ac2f15b3
A
415 addr = (struct sockaddr_inarp *)(rtm + 1);
416 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
417 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
b7080c8e
A
418 if (sdl->sdl_family == AF_LINK &&
419 (rtm->rtm_flags & RTF_LLINFO) &&
420 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
421 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
ac2f15b3 422 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
b7080c8e
A
423 goto delete;
424 }
425 }
426 if (sin_m.sin_other & SIN_PROXY) {
427 fprintf(stderr, "delete: can't locate %s\n",host);
428 return (1);
429 } else {
430 sin_m.sin_other = SIN_PROXY;
431 goto tryagain;
432 }
433delete:
434 if (sdl->sdl_family != AF_LINK) {
435 printf("cannot locate %s\n", host);
436 return (1);
437 }
ac2f15b3
A
438 if (rtmsg(RTM_DELETE) == 0) {
439 printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
440 return (0);
441 }
442 return (1);
b7080c8e
A
443}
444
445/*
ac2f15b3 446 * Search the arp table and do some action on matching entries
b7080c8e
A
447 */
448void
ac2f15b3
A
449search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
450 struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
b7080c8e
A
451{
452 int mib[6];
453 size_t needed;
ac2f15b3 454 char *lim, *buf, *next;
b7080c8e 455 struct rt_msghdr *rtm;
ac2f15b3 456 struct sockaddr_inarp *sin2;
b7080c8e 457 struct sockaddr_dl *sdl;
b7080c8e
A
458
459 mib[0] = CTL_NET;
460 mib[1] = PF_ROUTE;
461 mib[2] = 0;
462 mib[3] = AF_INET;
463 mib[4] = NET_RT_FLAGS;
464 mib[5] = RTF_LLINFO;
465 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
ac2f15b3 466 errx(1, "route-sysctl-estimate");
b7080c8e 467 if ((buf = malloc(needed)) == NULL)
ac2f15b3 468 errx(1, "malloc");
b7080c8e 469 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
ac2f15b3 470 errx(1, "actual retrieval of routing table");
b7080c8e
A
471 lim = buf + needed;
472 for (next = buf; next < lim; next += rtm->rtm_msglen) {
473 rtm = (struct rt_msghdr *)next;
ac2f15b3 474 sin2 = (struct sockaddr_inarp *)(rtm + 1);
2b484d24 475 sdl = (struct sockaddr_dl*)((char*)sin2 + ROUNDUP(sin2->sin_len));
b7080c8e 476 if (addr) {
ac2f15b3 477 if (addr != sin2->sin_addr.s_addr)
b7080c8e
A
478 continue;
479 found_entry = 1;
480 }
ac2f15b3 481 (*action)(sdl, sin2, rtm);
b7080c8e 482 }
ac2f15b3 483 free(buf);
b7080c8e
A
484}
485
ac2f15b3
A
486
487/*
488 * Stolen and adapted from ifconfig
489 */
490static void
491print_lladdr(struct sockaddr_dl *sdl)
492{
493 char *cp;
494 int n;
495
496 cp = (char *)LLADDR(sdl);
497 if ((n = sdl->sdl_alen) > 0) {
498 while (--n >= 0)
499 printf("%x%s",*cp++ & 0xff, n>0? ":" : "");
500 }
501}
502
503
504/*
505 * Display an arp entry
506 */
b7080c8e 507void
ac2f15b3
A
508print_entry(struct sockaddr_dl *sdl,
509 struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
b7080c8e 510{
ac2f15b3
A
511 const char *host;
512 struct hostent *hp;
513 struct iso88025_sockaddr_dl_data *trld;
514 char ifname[IF_NAMESIZE];
515 int seg;
516
517 if (nflag == 0)
518 hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
519 sizeof addr->sin_addr, AF_INET);
520 else
521 hp = 0;
522 if (hp)
523 host = hp->h_name;
524 else {
525 host = "?";
526 if (h_errno == TRY_AGAIN)
527 nflag = 1;
528 }
529 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
530 if (sdl->sdl_alen) {
531 print_lladdr(sdl);
532 } else
533 printf("(incomplete)");
534 if (if_indextoname(sdl->sdl_index, ifname) != NULL)
535 printf(" on %s", ifname);
536 if (rtm->rtm_rmx.rmx_expire == 0)
537 printf(" permanent");
538 if (addr->sin_other & SIN_PROXY)
539 printf(" published (proxy only)");
540 if (rtm->rtm_addrs & RTA_NETMASK) {
541 addr = (struct sockaddr_inarp *)
542 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
543 if (addr->sin_addr.s_addr == 0xffffffff)
544 printf(" published");
545 if (addr->sin_len != 8)
546 printf("(weird)");
547 }
548 switch(sdl->sdl_type) {
549 case IFT_ETHER:
550 printf(" [ethernet]");
551 break;
552 case IFT_ISO88025:
553#ifndef __APPLE__
554 printf(" [token-ring]");
555 trld = SDL_ISO88025(sdl);
556 if (trld->trld_rcf != 0) {
557 printf(" rt=%x", ntohs(trld->trld_rcf));
558 for (seg = 0;
559 seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
560 seg++)
561 printf(":%x", ntohs(*(trld->trld_route[seg])));
562 }
563#endif
564 break;
565 case IFT_L2VLAN:
566 printf(" [vlan]");
567 break;
568 case IFT_IEEE1394:
569 printf(" [firewire]");
570 break;
571 default:
2b484d24 572 break;
ac2f15b3
A
573 }
574
575 printf("\n");
576
577}
578
579/*
580 * Nuke an arp entry
581 */
582void
583nuke_entry(struct sockaddr_dl *sdl,
584 struct sockaddr_inarp *addr, struct rt_msghdr *rtm )
585{
586 char ip[20];
587
588 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
589 delete(ip, NULL);
b7080c8e
A
590}
591
592int
ac2f15b3 593my_ether_aton(char *a, struct ether_addr *n)
b7080c8e 594{
ac2f15b3 595 struct ether_addr *ea;
b7080c8e 596
ac2f15b3
A
597 if ((ea = ether_aton(a)) == NULL) {
598 warnx("invalid Ethernet address '%s'", a);
b7080c8e
A
599 return (1);
600 }
ac2f15b3 601 *n = *ea;
b7080c8e
A
602 return (0);
603}
604
605void
ac2f15b3 606usage(void)
b7080c8e 607{
ac2f15b3
A
608 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
609 "usage: arp [-n] hostname",
610 " arp [-n] -a",
611 " arp -d hostname [pub]",
612 " arp -d -a",
613 " arp -s hostname ether_addr [temp] [pub]",
614 " arp -S hostname ether_addr [temp] [pub]",
615 " arp -f filename");
b7080c8e
A
616 exit(1);
617}
618
619int
ac2f15b3 620rtmsg(int cmd)
b7080c8e
A
621{
622 static int seq;
623 int rlen;
624 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
625 register char *cp = m_rtmsg.m_space;
626 register int l;
627
628 errno = 0;
629 if (cmd == RTM_DELETE)
630 goto doit;
631 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
632 rtm->rtm_flags = flags;
633 rtm->rtm_version = RTM_VERSION;
634
635 switch (cmd) {
636 default:
ac2f15b3 637 errx(1, "internal wrong cmd");
b7080c8e
A
638 case RTM_ADD:
639 rtm->rtm_addrs |= RTA_GATEWAY;
640 rtm->rtm_rmx.rmx_expire = expire_time;
641 rtm->rtm_inits = RTV_EXPIRE;
642 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
643 sin_m.sin_other = 0;
644 if (doing_proxy) {
ac2f15b3 645 if (proxy_only)
b7080c8e
A
646 sin_m.sin_other = SIN_PROXY;
647 else {
648 rtm->rtm_addrs |= RTA_NETMASK;
649 rtm->rtm_flags &= ~RTF_HOST;
650 }
651 }
652 /* FALLTHROUGH */
653 case RTM_GET:
654 rtm->rtm_addrs |= RTA_DST;
655 }
656#define NEXTADDR(w, s) \
657 if (rtm->rtm_addrs & (w)) { \
ac2f15b3 658 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
b7080c8e
A
659
660 NEXTADDR(RTA_DST, sin_m);
661 NEXTADDR(RTA_GATEWAY, sdl_m);
662 NEXTADDR(RTA_NETMASK, so_mask);
663
664 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
665doit:
666 l = rtm->rtm_msglen;
667 rtm->rtm_seq = ++seq;
668 rtm->rtm_type = cmd;
669 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
670 if (errno != ESRCH || cmd != RTM_DELETE) {
ac2f15b3 671 warn("writing to routing socket");
b7080c8e
A
672 return (-1);
673 }
674 }
675 do {
676 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
677 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
678 if (l < 0)
ac2f15b3 679 warn("read from routing socket");
b7080c8e
A
680 return (0);
681}
ac2f15b3
A
682
683/*
684 * get_ether_addr - get the hardware address of an interface on the
685 * the same subnet as ipaddr.
686 */
687#define MAX_IFS 32
688
689int
690get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
691{
692 struct ifreq *ifr, *ifend, *ifp;
693 u_int32_t ina, mask;
694 struct sockaddr_dl *dla;
695 struct ifreq ifreq;
696 struct ifconf ifc;
697 struct ifreq ifs[MAX_IFS];
698 int sock;
699
700 sock = socket(AF_INET, SOCK_DGRAM, 0);
701 if (sock < 0)
702 err(1, "socket");
703
704 ifc.ifc_len = sizeof(ifs);
705 ifc.ifc_req = ifs;
706 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
707 warnx("ioctl(SIOCGIFCONF)");
708 close(sock);
709 return 0;
710 }
711
712 /*
713 * Scan through looking for an interface with an Internet
714 * address on the same subnet as `ipaddr'.
715 */
716 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
717 for (ifr = ifc.ifc_req; ifr < ifend; ) {
718 if (ifr->ifr_addr.sa_family == AF_INET) {
719 ina = ((struct sockaddr_in *)
720 &ifr->ifr_addr)->sin_addr.s_addr;
721 strncpy(ifreq.ifr_name, ifr->ifr_name,
722 sizeof(ifreq.ifr_name));
723 /*
724 * Check that the interface is up,
725 * and not point-to-point or loopback.
726 */
727 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
728 continue;
729 if ((ifreq.ifr_flags &
730 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
731 IFF_LOOPBACK|IFF_NOARP))
732 != (IFF_UP|IFF_BROADCAST))
733 goto nextif;
734 /*
735 * Get its netmask and check that it's on
736 * the right subnet.
737 */
738 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
739 continue;
740 mask = ((struct sockaddr_in *)
741 &ifreq.ifr_addr)->sin_addr.s_addr;
742 if ((ipaddr & mask) != (ina & mask))
743 goto nextif;
744 break;
745 }
746nextif:
747 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
748 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
749 }
750
751 if (ifr >= ifend) {
752 close(sock);
753 return 0;
754 }
755
756 /*
757 * Now scan through again looking for a link-level address
758 * for this interface.
759 */
760 ifp = ifr;
761 for (ifr = ifc.ifc_req; ifr < ifend; ) {
762 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
763 && ifr->ifr_addr.sa_family == AF_LINK) {
764 /*
765 * Found the link-level address - copy it out
766 */
767 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
768 close (sock);
769 printf("using interface %s for proxy with address ",
770 ifp->ifr_name);
771 print_lladdr(dla);
772 printf("\n");
773 return dla->sdl_alen;
774 }
775 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
776 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
777 }
778 return 0;
779}