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