Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Copyright (c) 1983, 1989, 1991, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | */ | |
56 | ||
57 | #ifndef lint | |
58 | static const char copyright[] = | |
59 | "@(#) Copyright (c) 1983, 1989, 1991, 1993\n\ | |
60 | The Regents of the University of California. All rights reserved.\n"; | |
61 | #endif /* not lint */ | |
62 | ||
63 | #ifndef lint | |
64 | #if 0 | |
65 | static char sccsid[] = "@(#)route.c 8.3 (Berkeley) 3/19/94"; | |
66 | #endif | |
67 | static const char rcsid[] = | |
68 | "$Id: route.c,v 1.1.1.2 2000/01/11 01:49:18 wsanchez Exp $"; | |
69 | #endif /* not lint */ | |
70 | ||
71 | #include <sys/param.h> | |
72 | #include <sys/file.h> | |
73 | #include <sys/socket.h> | |
74 | #include <sys/ioctl.h> | |
75 | #include <sys/sysctl.h> | |
76 | ||
77 | #include <net/if.h> | |
78 | #include <net/route.h> | |
79 | #include <net/if_dl.h> | |
80 | #include <netinet/in.h> | |
81 | #ifdef AT | |
82 | #include <netatalk/at.h> | |
83 | #endif | |
84 | #ifdef NS | |
85 | #include <netns/ns.h> | |
86 | #endif | |
87 | #include <arpa/inet.h> | |
88 | #include <netdb.h> | |
89 | ||
90 | #include <ctype.h> | |
91 | #include <err.h> | |
92 | #include <errno.h> | |
93 | #include <stdio.h> | |
94 | #include <stdlib.h> | |
95 | #include <string.h> | |
96 | #include <sysexits.h> | |
97 | #include <unistd.h> | |
98 | ||
99 | struct keytab { | |
100 | char *kt_cp; | |
101 | int kt_i; | |
102 | } keywords[] = { | |
103 | #include "keywords.h" | |
104 | {0, 0} | |
105 | }; | |
106 | ||
107 | struct ortentry route; | |
108 | union sockunion { | |
109 | struct sockaddr sa; | |
110 | struct sockaddr_in sin; | |
111 | #ifdef AT | |
112 | struct sockaddr_at sat; | |
113 | #endif | |
114 | #ifdef NS | |
115 | struct sockaddr_ns sns; | |
116 | #endif | |
117 | struct sockaddr_dl sdl; | |
118 | } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp; | |
119 | ||
120 | typedef union sockunion *sup; | |
121 | int pid, rtm_addrs, uid; | |
122 | int s; | |
123 | int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword(); | |
124 | int iflag, verbose, aflen = sizeof (struct sockaddr_in); | |
125 | int locking, lockrest, debugonly; | |
126 | struct rt_metrics rt_metrics; | |
127 | u_long rtm_inits; | |
128 | struct in_addr inet_makeaddr(); | |
129 | #ifdef AT | |
130 | int atalk_aton __P((const char *, struct at_addr *)); | |
131 | char *atalk_ntoa __P((struct at_addr)); | |
132 | #endif | |
133 | char *routename(), *netname(); | |
134 | void flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf(); | |
135 | void print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr(); | |
136 | int getaddr(), rtmsg(), x25_makemask(); | |
137 | extern char *inet_ntoa(), *iso_ntoa(), *link_ntoa(); | |
138 | ||
139 | void usage __P((const char *)); | |
140 | ||
141 | void | |
142 | usage(cp) | |
143 | const char *cp; | |
144 | { | |
145 | if (cp) | |
146 | warnx("bad keyword: %s", cp); | |
147 | (void) fprintf(stderr, | |
148 | "usage: route [-dnqtv] command [[modifiers] args]\n"); | |
149 | exit(EX_USAGE); | |
150 | /* NOTREACHED */ | |
151 | } | |
152 | ||
153 | #define ROUNDUP(a) \ | |
154 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
155 | #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) | |
156 | ||
157 | int | |
158 | main(argc, argv) | |
159 | int argc; | |
160 | char **argv; | |
161 | { | |
162 | int ch; | |
163 | ||
164 | if (argc < 2) | |
165 | usage((char *)NULL); | |
166 | ||
167 | while ((ch = getopt(argc, argv, "nqdtv")) != -1) | |
168 | switch(ch) { | |
169 | case 'n': | |
170 | nflag = 1; | |
171 | break; | |
172 | case 'q': | |
173 | qflag = 1; | |
174 | break; | |
175 | case 'v': | |
176 | verbose = 1; | |
177 | break; | |
178 | case 't': | |
179 | tflag = 1; | |
180 | break; | |
181 | case 'd': | |
182 | debugonly = 1; | |
183 | break; | |
184 | case '?': | |
185 | default: | |
186 | usage((char *)NULL); | |
187 | } | |
188 | argc -= optind; | |
189 | argv += optind; | |
190 | ||
191 | pid = getpid(); | |
192 | uid = getuid(); | |
193 | if (tflag) | |
194 | s = open("/dev/null", O_WRONLY, 0); | |
195 | else | |
196 | s = socket(PF_ROUTE, SOCK_RAW, 0); | |
197 | if (s < 0) | |
198 | err(EX_OSERR, "socket"); | |
199 | setuid(uid); | |
200 | if (*argv) | |
201 | switch (keyword(*argv)) { | |
202 | case K_GET: | |
203 | uid = 0; | |
204 | /* FALLTHROUGH */ | |
205 | ||
206 | case K_CHANGE: | |
207 | case K_ADD: | |
208 | case K_DELETE: | |
209 | newroute(argc, argv); | |
210 | exit(0); | |
211 | /* NOTREACHED */ | |
212 | ||
213 | case K_MONITOR: | |
214 | monitor(); | |
215 | /* NOTREACHED */ | |
216 | ||
217 | case K_FLUSH: | |
218 | flushroutes(argc, argv); | |
219 | exit(0); | |
220 | /* NOTREACHED */ | |
221 | } | |
222 | usage(*argv); | |
223 | /* NOTREACHED */ | |
224 | } | |
225 | ||
226 | /* | |
227 | * Purge all entries in the routing tables not | |
228 | * associated with network interfaces. | |
229 | */ | |
230 | void | |
231 | flushroutes(argc, argv) | |
232 | int argc; | |
233 | char *argv[]; | |
234 | { | |
235 | size_t needed; | |
236 | int mib[6], rlen, seqno; | |
237 | char *buf, *next, *lim; | |
238 | register struct rt_msghdr *rtm; | |
239 | ||
240 | if (uid) { | |
241 | errx(EX_NOPERM, "must be root to alter routing table"); | |
242 | } | |
243 | shutdown(s, 0); /* Don't want to read back our messages */ | |
244 | if (argc > 1) { | |
245 | argv++; | |
246 | if (argc == 2 && **argv == '-') | |
247 | switch (keyword(*argv + 1)) { | |
248 | case K_INET: | |
249 | af = AF_INET; | |
250 | break; | |
251 | ||
252 | #ifdef | |
253 | case K_ATALK: | |
254 | af = AF_APPLETALK; | |
255 | break; | |
256 | #endif | |
257 | #ifdef NS | |
258 | case K_XNS: | |
259 | af = AF_NS; | |
260 | break; | |
261 | #endif | |
262 | case K_LINK: | |
263 | af = AF_LINK; | |
264 | break; | |
265 | default: | |
266 | goto bad; | |
267 | } else | |
268 | bad: usage(*argv); | |
269 | } | |
270 | mib[0] = CTL_NET; | |
271 | mib[1] = PF_ROUTE; | |
272 | mib[2] = 0; /* protocol */ | |
273 | mib[3] = 0; /* wildcard address family */ | |
274 | mib[4] = NET_RT_DUMP; | |
275 | mib[5] = 0; /* no flags */ | |
276 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) | |
277 | err(EX_OSERR, "route-sysctl-estimate"); | |
278 | if ((buf = malloc(needed)) == NULL) | |
279 | errx(EX_OSERR, "malloc failed"); | |
280 | if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) | |
281 | err(EX_OSERR, "route-sysctl-get"); | |
282 | lim = buf + needed; | |
283 | if (verbose) | |
284 | (void) printf("Examining routing table from sysctl\n"); | |
285 | seqno = 0; /* ??? */ | |
286 | for (next = buf; next < lim; next += rtm->rtm_msglen) { | |
287 | rtm = (struct rt_msghdr *)next; | |
288 | if (verbose) | |
289 | print_rtmsg(rtm, rtm->rtm_msglen); | |
290 | if ((rtm->rtm_flags & RTF_GATEWAY) == 0) | |
291 | continue; | |
292 | if (af) { | |
293 | struct sockaddr *sa = (struct sockaddr *)(rtm + 1); | |
294 | ||
295 | if (sa->sa_family != af) | |
296 | continue; | |
297 | } | |
298 | if (debugonly) | |
299 | continue; | |
300 | rtm->rtm_type = RTM_DELETE; | |
301 | rtm->rtm_seq = seqno; | |
302 | rlen = write(s, next, rtm->rtm_msglen); | |
303 | if (rlen < (int)rtm->rtm_msglen) { | |
304 | warn("write to routing socket"); | |
305 | (void) printf("got only %d for rlen\n", rlen); | |
306 | break; | |
307 | } | |
308 | seqno++; | |
309 | if (qflag) | |
310 | continue; | |
311 | if (verbose) | |
312 | print_rtmsg(rtm, rlen); | |
313 | else { | |
314 | struct sockaddr *sa = (struct sockaddr *)(rtm + 1); | |
315 | (void) printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ? | |
316 | routename(sa) : netname(sa)); | |
317 | sa = (struct sockaddr *)(sa->sa_len + (char *)sa); | |
318 | (void) printf("%-20.20s ", routename(sa)); | |
319 | (void) printf("done\n"); | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | char * | |
325 | routename(sa) | |
326 | struct sockaddr *sa; | |
327 | { | |
328 | register char *cp; | |
329 | static char line[MAXHOSTNAMELEN + 1]; | |
330 | struct hostent *hp; | |
331 | static char domain[MAXHOSTNAMELEN + 1]; | |
332 | static int first = 1; | |
333 | #ifdef NS | |
334 | char *ns_print(); | |
335 | #endif | |
336 | ||
337 | if (first) { | |
338 | first = 0; | |
339 | if (gethostname(domain, MAXHOSTNAMELEN) == 0 && | |
340 | (cp = index(domain, '.'))) { | |
341 | domain[MAXHOSTNAMELEN] = '\0'; | |
342 | (void) strcpy(domain, cp + 1); | |
343 | } else | |
344 | domain[0] = 0; | |
345 | } | |
346 | ||
347 | if (sa->sa_len == 0) | |
348 | strcpy(line, "default"); | |
349 | else switch (sa->sa_family) { | |
350 | ||
351 | case AF_INET: | |
352 | { struct in_addr in; | |
353 | in = ((struct sockaddr_in *)sa)->sin_addr; | |
354 | ||
355 | cp = 0; | |
356 | if (in.s_addr == INADDR_ANY || sa->sa_len < 4) | |
357 | cp = "default"; | |
358 | if (cp == 0 && !nflag) { | |
359 | hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), | |
360 | AF_INET); | |
361 | if (hp) { | |
362 | if ((cp = index(hp->h_name, '.')) && | |
363 | !strcmp(cp + 1, domain)) | |
364 | *cp = 0; | |
365 | cp = hp->h_name; | |
366 | } | |
367 | } | |
368 | if (cp) { | |
369 | strncpy(line, cp, sizeof(line) - 1); | |
370 | line[sizeof(line) - 1] = '\0'; | |
371 | } else { | |
372 | /* XXX - why not inet_ntoa()? */ | |
373 | #define C(x) (unsigned)((x) & 0xff) | |
374 | in.s_addr = ntohl(in.s_addr); | |
375 | (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), | |
376 | C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr)); | |
377 | } | |
378 | break; | |
379 | } | |
380 | #ifdef AT | |
381 | case AF_APPLETALK: | |
382 | (void) snprintf(line, sizeof(line), "atalk %s", | |
383 | atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr)); | |
384 | break; | |
385 | #endif | |
386 | #ifdef NS | |
387 | case AF_NS: | |
388 | return (ns_print((struct sockaddr_ns *)sa)); | |
389 | #endif | |
390 | ||
391 | case AF_LINK: | |
392 | return (link_ntoa((struct sockaddr_dl *)sa)); | |
393 | ||
394 | default: | |
395 | { u_short *s = (u_short *)sa; | |
396 | u_short *slim = s + ((sa->sa_len + 1) >> 1); | |
397 | char *cp = line + sprintf(line, "(%d)", sa->sa_family); | |
398 | char *cpe = line + sizeof(line); | |
399 | ||
400 | while (++s < slim && cp < cpe) /* start with sa->sa_data */ | |
401 | cp += snprintf(cp, cpe - cp, " %x", *s); | |
402 | break; | |
403 | } | |
404 | } | |
405 | return (line); | |
406 | } | |
407 | ||
408 | /* | |
409 | * Return the name of the network whose address is given. | |
410 | * The address is assumed to be that of a net or subnet, not a host. | |
411 | */ | |
412 | char * | |
413 | netname(sa) | |
414 | struct sockaddr *sa; | |
415 | { | |
416 | char *cp = 0; | |
417 | static char line[MAXHOSTNAMELEN + 1]; | |
418 | struct netent *np = 0; | |
419 | u_long net, mask; | |
420 | register u_long i; | |
421 | int subnetshift; | |
422 | #ifdef NS | |
423 | char *ns_print(); | |
424 | #endif | |
425 | ||
426 | switch (sa->sa_family) { | |
427 | ||
428 | case AF_INET: | |
429 | { struct in_addr in; | |
430 | in = ((struct sockaddr_in *)sa)->sin_addr; | |
431 | ||
432 | i = in.s_addr = ntohl(in.s_addr); | |
433 | if (in.s_addr == 0) | |
434 | cp = "default"; | |
435 | else if (!nflag) { | |
436 | if (IN_CLASSA(i)) { | |
437 | mask = IN_CLASSA_NET; | |
438 | subnetshift = 8; | |
439 | } else if (IN_CLASSB(i)) { | |
440 | mask = IN_CLASSB_NET; | |
441 | subnetshift = 8; | |
442 | } else { | |
443 | mask = IN_CLASSC_NET; | |
444 | subnetshift = 4; | |
445 | } | |
446 | /* | |
447 | * If there are more bits than the standard mask | |
448 | * would suggest, subnets must be in use. | |
449 | * Guess at the subnet mask, assuming reasonable | |
450 | * width subnet fields. | |
451 | */ | |
452 | while (in.s_addr &~ mask) | |
453 | mask = (long)mask >> subnetshift; | |
454 | net = in.s_addr & mask; | |
455 | while ((mask & 1) == 0) | |
456 | mask >>= 1, net >>= 1; | |
457 | np = getnetbyaddr(net, AF_INET); | |
458 | if (np) | |
459 | cp = np->n_name; | |
460 | } | |
461 | if (cp) | |
462 | strncpy(line, cp, sizeof(line)); | |
463 | else if ((in.s_addr & 0xffffff) == 0) | |
464 | (void) sprintf(line, "%u", C(in.s_addr >> 24)); | |
465 | else if ((in.s_addr & 0xffff) == 0) | |
466 | (void) sprintf(line, "%u.%u", C(in.s_addr >> 24), | |
467 | C(in.s_addr >> 16)); | |
468 | else if ((in.s_addr & 0xff) == 0) | |
469 | (void) sprintf(line, "%u.%u.%u", C(in.s_addr >> 24), | |
470 | C(in.s_addr >> 16), C(in.s_addr >> 8)); | |
471 | else | |
472 | (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), | |
473 | C(in.s_addr >> 16), C(in.s_addr >> 8), | |
474 | C(in.s_addr)); | |
475 | break; | |
476 | } | |
477 | #ifdef AT | |
478 | case AF_APPLETALK: | |
479 | (void) snprintf(line, sizeof(line), "atalk %s", | |
480 | atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr)); | |
481 | break; | |
482 | #endif | |
483 | #ifdef NS | |
484 | case AF_NS: | |
485 | return (ns_print((struct sockaddr_ns *)sa)); | |
486 | break; | |
487 | #endif | |
488 | ||
489 | case AF_LINK: | |
490 | return (link_ntoa((struct sockaddr_dl *)sa)); | |
491 | ||
492 | ||
493 | default: | |
494 | { u_short *s = (u_short *)sa->sa_data; | |
495 | u_short *slim = s + ((sa->sa_len + 1)>>1); | |
496 | char *cp = line + sprintf(line, "af %d:", sa->sa_family); | |
497 | char *cpe = line + sizeof(line); | |
498 | ||
499 | while (s < slim && cp < cpe) | |
500 | cp += snprintf(cp, cpe - cp, " %x", *s++); | |
501 | break; | |
502 | } | |
503 | } | |
504 | return (line); | |
505 | } | |
506 | ||
507 | void | |
508 | set_metric(value, key) | |
509 | char *value; | |
510 | int key; | |
511 | { | |
512 | int flag = 0; | |
513 | u_long noval, *valp = &noval; | |
514 | ||
515 | switch (key) { | |
516 | #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break | |
517 | caseof(K_MTU, RTV_MTU, rmx_mtu); | |
518 | caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount); | |
519 | caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire); | |
520 | caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe); | |
521 | caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe); | |
522 | caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh); | |
523 | caseof(K_RTT, RTV_RTT, rmx_rtt); | |
524 | caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar); | |
525 | } | |
526 | rtm_inits |= flag; | |
527 | if (lockrest || locking) | |
528 | rt_metrics.rmx_locks |= flag; | |
529 | if (locking) | |
530 | locking = 0; | |
531 | *valp = atoi(value); | |
532 | } | |
533 | ||
534 | void | |
535 | newroute(argc, argv) | |
536 | int argc; | |
537 | register char **argv; | |
538 | { | |
539 | char *cmd, *dest = "", *gateway = "", *err; | |
540 | int ishost = 0, ret, attempts, oerrno, flags = RTF_STATIC; | |
541 | int key; | |
542 | struct hostent *hp = 0; | |
543 | ||
544 | if (uid) { | |
545 | errx(EX_NOPERM, "must be root to alter routing table"); | |
546 | } | |
547 | cmd = argv[0]; | |
548 | if (*cmd != 'g') | |
549 | shutdown(s, 0); /* Don't want to read back our messages */ | |
550 | while (--argc > 0) { | |
551 | if (**(++argv)== '-') { | |
552 | switch (key = keyword(1 + *argv)) { | |
553 | case K_LINK: | |
554 | af = AF_LINK; | |
555 | aflen = sizeof(struct sockaddr_dl); | |
556 | break; | |
557 | case K_INET: | |
558 | af = AF_INET; | |
559 | aflen = sizeof(struct sockaddr_in); | |
560 | break; | |
561 | #ifdef AT | |
562 | case K_ATALK: | |
563 | af = AF_APPLETALK; | |
564 | aflen = sizeof(struct sockaddr_at); | |
565 | break; | |
566 | #endif | |
567 | case K_SA: | |
568 | af = PF_ROUTE; | |
569 | aflen = sizeof(union sockunion); | |
570 | break; | |
571 | #ifdef NS | |
572 | case K_XNS: | |
573 | af = AF_NS; | |
574 | aflen = sizeof(struct sockaddr_ns); | |
575 | break; | |
576 | #endif | |
577 | case K_IFACE: | |
578 | case K_INTERFACE: | |
579 | iflag++; | |
580 | break; | |
581 | case K_NOSTATIC: | |
582 | flags &= ~RTF_STATIC; | |
583 | break; | |
584 | ||
585 | case K_LLINFO: | |
586 | flags |= RTF_LLINFO; | |
587 | break; | |
588 | ||
589 | case K_LOCK: | |
590 | locking = 1; | |
591 | break; | |
592 | case K_LOCKREST: | |
593 | lockrest = 1; | |
594 | break; | |
595 | case K_HOST: | |
596 | forcehost++; | |
597 | break; | |
598 | case K_REJECT: | |
599 | flags |= RTF_REJECT; | |
600 | break; | |
601 | case K_BLACKHOLE: | |
602 | flags |= RTF_BLACKHOLE; | |
603 | break; | |
604 | case K_PROTO1: | |
605 | flags |= RTF_PROTO1; | |
606 | break; | |
607 | case K_PROTO2: | |
608 | flags |= RTF_PROTO2; | |
609 | break; | |
610 | case K_CLONING: | |
611 | flags |= RTF_CLONING; | |
612 | break; | |
613 | case K_XRESOLVE: | |
614 | flags |= RTF_XRESOLVE; | |
615 | break; | |
616 | case K_STATIC: | |
617 | flags |= RTF_STATIC; | |
618 | break; | |
619 | case K_IFA: | |
620 | argc--; | |
621 | (void) getaddr(RTA_IFA, *++argv, 0); | |
622 | break; | |
623 | case K_IFP: | |
624 | argc--; | |
625 | (void) getaddr(RTA_IFP, *++argv, 0); | |
626 | break; | |
627 | case K_GENMASK: | |
628 | argc--; | |
629 | (void) getaddr(RTA_GENMASK, *++argv, 0); | |
630 | break; | |
631 | case K_GATEWAY: | |
632 | argc--; | |
633 | (void) getaddr(RTA_GATEWAY, *++argv, 0); | |
634 | break; | |
635 | case K_DST: | |
636 | argc--; | |
637 | ishost = getaddr(RTA_DST, *++argv, &hp); | |
638 | dest = *argv; | |
639 | break; | |
640 | case K_NETMASK: | |
641 | argc--; | |
642 | (void) getaddr(RTA_NETMASK, *++argv, 0); | |
643 | /* FALLTHROUGH */ | |
644 | case K_NET: | |
645 | forcenet++; | |
646 | break; | |
647 | case K_MTU: | |
648 | case K_HOPCOUNT: | |
649 | case K_EXPIRE: | |
650 | case K_RECVPIPE: | |
651 | case K_SENDPIPE: | |
652 | case K_SSTHRESH: | |
653 | case K_RTT: | |
654 | case K_RTTVAR: | |
655 | argc--; | |
656 | set_metric(*++argv, key); | |
657 | break; | |
658 | default: | |
659 | usage(1+*argv); | |
660 | } | |
661 | } else { | |
662 | if ((rtm_addrs & RTA_DST) == 0) { | |
663 | dest = *argv; | |
664 | ishost = getaddr(RTA_DST, *argv, &hp); | |
665 | } else if ((rtm_addrs & RTA_GATEWAY) == 0) { | |
666 | gateway = *argv; | |
667 | (void) getaddr(RTA_GATEWAY, *argv, &hp); | |
668 | } else { | |
669 | (void) getaddr(RTA_NETMASK, *argv, 0); | |
670 | } | |
671 | } | |
672 | } | |
673 | if (forcehost) | |
674 | ishost = 1; | |
675 | if (forcenet) | |
676 | ishost = 0; | |
677 | flags |= RTF_UP; | |
678 | if (ishost) | |
679 | flags |= RTF_HOST; | |
680 | if (iflag == 0) | |
681 | flags |= RTF_GATEWAY; | |
682 | for (attempts = 1; ; attempts++) { | |
683 | errno = 0; | |
684 | if ((ret = rtmsg(*cmd, flags)) == 0) | |
685 | break; | |
686 | if (errno != ENETUNREACH && errno != ESRCH) | |
687 | break; | |
688 | if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) { | |
689 | hp->h_addr_list++; | |
690 | bcopy(hp->h_addr_list[0], &so_gate.sin.sin_addr, | |
691 | MIN(hp->h_length, sizeof(so_gate.sin.sin_addr))); | |
692 | } else | |
693 | break; | |
694 | } | |
695 | if (*cmd == 'g') | |
696 | exit(0); | |
697 | oerrno = errno; | |
698 | (void) printf("%s %s %s", cmd, ishost? "host" : "net", dest); | |
699 | if (*gateway) { | |
700 | (void) printf(": gateway %s", gateway); | |
701 | if (attempts > 1 && ret == 0 && af == AF_INET) | |
702 | (void) printf(" (%s)", | |
703 | inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr)); | |
704 | } | |
705 | if (ret == 0) | |
706 | (void) printf("\n"); | |
707 | else { | |
708 | switch (oerrno) { | |
709 | case ESRCH: | |
710 | err = "not in table"; | |
711 | break; | |
712 | case EBUSY: | |
713 | err = "entry in use"; | |
714 | break; | |
715 | case ENOBUFS: | |
716 | err = "routing table overflow"; | |
717 | break; | |
718 | default: | |
719 | err = strerror(oerrno); | |
720 | break; | |
721 | } | |
722 | (void) printf(": %s\n", err); | |
723 | } | |
724 | } | |
725 | ||
726 | void | |
727 | inet_makenetandmask(net, sin, bits) | |
728 | u_long net, bits; | |
729 | register struct sockaddr_in *sin; | |
730 | { | |
731 | u_long addr, mask = 0; | |
732 | register char *cp; | |
733 | ||
734 | rtm_addrs |= RTA_NETMASK; | |
735 | if (net == 0) | |
736 | mask = addr = 0; | |
737 | else if (bits) { | |
738 | addr = net; | |
739 | mask = 0xffffffff << (32 - bits); | |
740 | } else if (net < 128) { | |
741 | addr = net << IN_CLASSA_NSHIFT; | |
742 | mask = IN_CLASSA_NET; | |
743 | } else if (net < 65536) { | |
744 | addr = net << IN_CLASSB_NSHIFT; | |
745 | mask = IN_CLASSB_NET; | |
746 | } else if (net < 16777216L) { | |
747 | addr = net << IN_CLASSC_NSHIFT; | |
748 | mask = IN_CLASSC_NET; | |
749 | } else { | |
750 | addr = net; | |
751 | if ((addr & IN_CLASSA_HOST) == 0) | |
752 | mask = IN_CLASSA_NET; | |
753 | else if ((addr & IN_CLASSB_HOST) == 0) | |
754 | mask = IN_CLASSB_NET; | |
755 | else if ((addr & IN_CLASSC_HOST) == 0) | |
756 | mask = IN_CLASSC_NET; | |
757 | else | |
758 | mask = -1; | |
759 | } | |
760 | sin->sin_addr.s_addr = htonl(addr); | |
761 | sin = &so_mask.sin; | |
762 | sin->sin_addr.s_addr = htonl(mask); | |
763 | sin->sin_len = 0; | |
764 | sin->sin_family = 0; | |
765 | cp = (char *)(&sin->sin_addr + 1); | |
766 | while (*--cp == 0 && cp > (char *)sin) | |
767 | ; | |
768 | sin->sin_len = 1 + cp - (char *)sin; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Interpret an argument as a network address of some kind, | |
773 | * returning 1 if a host address, 0 if a network address. | |
774 | */ | |
775 | int | |
776 | getaddr(which, s, hpp) | |
777 | int which; | |
778 | char *s; | |
779 | struct hostent **hpp; | |
780 | { | |
781 | register sup su; | |
782 | #ifdef NS | |
783 | struct ns_addr ns_addr(); | |
784 | #endif | |
785 | struct hostent *hp; | |
786 | struct netent *np; | |
787 | u_long val; | |
788 | char *q,qs; | |
789 | ||
790 | if (af == 0) { | |
791 | af = AF_INET; | |
792 | aflen = sizeof(struct sockaddr_in); | |
793 | } | |
794 | rtm_addrs |= which; | |
795 | switch (which) { | |
796 | case RTA_DST: | |
797 | su = &so_dst; | |
798 | break; | |
799 | case RTA_GATEWAY: | |
800 | su = &so_gate; | |
801 | if (iflag) { | |
802 | #define MAX_IFACES 400 | |
803 | int sock; | |
804 | struct ifreq iflist[MAX_IFACES]; | |
805 | struct ifconf ifconf; | |
806 | struct ifreq *ifr, *ifr_end; | |
807 | struct sockaddr_dl *dl, *sdl = NULL; | |
808 | ||
809 | /* Get socket */ | |
810 | if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | |
811 | err(1, "socket"); | |
812 | ||
813 | /* Get interface list */ | |
814 | ifconf.ifc_req = iflist; | |
815 | ifconf.ifc_len = sizeof(iflist); | |
816 | if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) | |
817 | err(1, "ioctl(SIOCGIFCONF)"); | |
818 | close(sock); | |
819 | ||
820 | /* Look for this interface in the list */ | |
821 | for (ifr = ifconf.ifc_req, | |
822 | ifr_end = (struct ifreq *) | |
823 | (ifconf.ifc_buf + ifconf.ifc_len); | |
824 | ifr < ifr_end; | |
825 | ifr = (struct ifreq *) ((char *) &ifr->ifr_addr | |
826 | + ifr->ifr_addr.sa_len)) { | |
827 | dl = (struct sockaddr_dl *)&ifr->ifr_addr; | |
828 | if (ifr->ifr_addr.sa_family == AF_LINK | |
829 | && (ifr->ifr_flags & IFF_POINTOPOINT) | |
830 | && !strncmp(s, dl->sdl_data, dl->sdl_nlen) | |
831 | && s[dl->sdl_nlen] == 0) { | |
832 | sdl = dl; | |
833 | break; | |
834 | } | |
835 | } | |
836 | ||
837 | /* If we found it, then use it */ | |
838 | if (sdl) { | |
839 | su->sdl = *sdl; | |
840 | return(1); | |
841 | } | |
842 | } | |
843 | break; | |
844 | case RTA_NETMASK: | |
845 | su = &so_mask; | |
846 | break; | |
847 | case RTA_GENMASK: | |
848 | su = &so_genmask; | |
849 | break; | |
850 | case RTA_IFP: | |
851 | su = &so_ifp; | |
852 | break; | |
853 | case RTA_IFA: | |
854 | su = &so_ifa; | |
855 | break; | |
856 | default: | |
857 | usage("internal error"); | |
858 | /*NOTREACHED*/ | |
859 | } | |
860 | su->sa.sa_len = aflen; | |
861 | su->sa.sa_family = af; /* cases that don't want it have left already */ | |
862 | if (strcmp(s, "default") == 0) { | |
863 | /* | |
864 | * Default is net 0.0.0.0/0 | |
865 | */ | |
866 | switch (which) { | |
867 | case RTA_DST: | |
868 | forcenet++; | |
869 | /* bzero(su, sizeof(*su)); *//* for readability */ | |
870 | (void) getaddr(RTA_NETMASK, s, 0); | |
871 | break; | |
872 | #if 0 | |
873 | case RTA_NETMASK: | |
874 | case RTA_GENMASK: | |
875 | /* bzero(su, sizeof(*su)); *//* for readability */ | |
876 | #endif | |
877 | } | |
878 | return (0); | |
879 | } | |
880 | switch (af) { | |
881 | #ifdef NS | |
882 | case AF_NS: | |
883 | if (which == RTA_DST) { | |
884 | extern short ns_bh[3]; | |
885 | struct sockaddr_ns *sms = &(so_mask.sns); | |
886 | bzero((char *)sms, sizeof(*sms)); | |
887 | sms->sns_family = 0; | |
888 | sms->sns_len = 6; | |
889 | sms->sns_addr.x_net = *(union ns_net *)ns_bh; | |
890 | rtm_addrs |= RTA_NETMASK; | |
891 | } | |
892 | su->sns.sns_addr = ns_addr(s); | |
893 | return (!ns_nullhost(su->sns.sns_addr)); | |
894 | #endif | |
895 | ||
896 | #ifdef AT | |
897 | case AF_APPLETALK: | |
898 | if (!atalk_aton(s, &su->sat.sat_addr)) | |
899 | errx(EX_NOHOST, "bad address: %s", s); | |
900 | rtm_addrs |= RTA_NETMASK; | |
901 | return(forcehost || su->sat.sat_addr.s_node != 0); | |
902 | #endif | |
903 | case AF_LINK: | |
904 | link_addr(s, &su->sdl); | |
905 | return (1); | |
906 | ||
907 | ||
908 | case PF_ROUTE: | |
909 | su->sa.sa_len = sizeof(*su); | |
910 | sockaddr(s, &su->sa); | |
911 | return (1); | |
912 | ||
913 | case AF_INET: | |
914 | default: | |
915 | break; | |
916 | } | |
917 | ||
918 | if (hpp == NULL) | |
919 | hpp = &hp; | |
920 | *hpp = NULL; | |
921 | ||
922 | q = strchr(s,'/'); | |
923 | if (q && which == RTA_DST) { | |
924 | qs = *q; | |
925 | *q = '\0'; | |
926 | if (((val = inet_addr(s)) != INADDR_NONE)) { | |
927 | inet_makenetandmask( | |
928 | htonl(val), &su->sin, strtoul(q+1, 0, 0)); | |
929 | return (0); | |
930 | } | |
931 | *q =qs; | |
932 | } | |
933 | if (((val = inet_addr(s)) != INADDR_NONE) && | |
934 | (which != RTA_DST || forcenet == 0)) { | |
935 | su->sin.sin_addr.s_addr = val; | |
936 | if (inet_lnaof(su->sin.sin_addr) != INADDR_ANY) | |
937 | return (1); | |
938 | else { | |
939 | val = ntohl(val); | |
940 | goto netdone; | |
941 | } | |
942 | } | |
943 | if ((val = inet_network(s)) != INADDR_NONE || | |
944 | (forcehost == 0 && (np = getnetbyname(s)) != NULL && | |
945 | (val = np->n_net) != 0)) { | |
946 | netdone: | |
947 | if (which == RTA_DST) | |
948 | inet_makenetandmask(val, &su->sin, 0); | |
949 | return (0); | |
950 | } | |
951 | hp = gethostbyname(s); | |
952 | if (hp) { | |
953 | *hpp = hp; | |
954 | su->sin.sin_family = hp->h_addrtype; | |
955 | bcopy(hp->h_addr, (char *)&su->sin.sin_addr, | |
956 | MIN(hp->h_length, sizeof(su->sin.sin_addr))); | |
957 | return (1); | |
958 | } | |
959 | errx(EX_NOHOST, "bad address: %s", s); | |
960 | } | |
961 | ||
962 | ||
963 | #ifdef NS | |
964 | short ns_nullh[] = {0,0,0}; | |
965 | short ns_bh[] = {-1,-1,-1}; | |
966 | ||
967 | char * | |
968 | ns_print(sns) | |
969 | struct sockaddr_ns *sns; | |
970 | { | |
971 | struct ns_addr work; | |
972 | union { union ns_net net_e; u_long long_e; } net; | |
973 | u_short port; | |
974 | static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25]; | |
975 | char *host = ""; | |
976 | register char *p; | |
977 | register u_char *q; | |
978 | ||
979 | work = sns->sns_addr; | |
980 | port = ntohs(work.x_port); | |
981 | work.x_port = 0; | |
982 | net.net_e = work.x_net; | |
983 | if (ns_nullhost(work) && net.long_e == 0) { | |
984 | if (!port) | |
985 | return ("*.*"); | |
986 | (void) sprintf(mybuf, "*.%XH", port); | |
987 | return (mybuf); | |
988 | } | |
989 | ||
990 | if (bcmp((char *)ns_bh, (char *)work.x_host.c_host, 6) == 0) | |
991 | host = "any"; | |
992 | else if (bcmp((char *)ns_nullh, (char *)work.x_host.c_host, 6) == 0) | |
993 | host = "*"; | |
994 | else { | |
995 | q = work.x_host.c_host; | |
996 | (void) sprintf(chost, "%02X%02X%02X%02X%02X%02XH", | |
997 | q[0], q[1], q[2], q[3], q[4], q[5]); | |
998 | for (p = chost; *p == '0' && p < chost + 12; p++) | |
999 | /* void */; | |
1000 | host = p; | |
1001 | } | |
1002 | if (port) | |
1003 | (void) sprintf(cport, ".%XH", htons(port)); | |
1004 | else | |
1005 | *cport = 0; | |
1006 | ||
1007 | (void) snprintf(mybuf, sizeof(mybuf), "%lxH.%s%s", | |
1008 | (unsigned long)ntohl(net.long_e), | |
1009 | host, cport); | |
1010 | return (mybuf); | |
1011 | } | |
1012 | #endif | |
1013 | ||
1014 | void | |
1015 | interfaces() | |
1016 | { | |
1017 | size_t needed; | |
1018 | int mib[6]; | |
1019 | char *buf, *lim, *next; | |
1020 | register struct rt_msghdr *rtm; | |
1021 | ||
1022 | mib[0] = CTL_NET; | |
1023 | mib[1] = PF_ROUTE; | |
1024 | mib[2] = 0; /* protocol */ | |
1025 | mib[3] = 0; /* wildcard address family */ | |
1026 | mib[4] = NET_RT_IFLIST; | |
1027 | mib[5] = 0; /* no flags */ | |
1028 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) | |
1029 | err(EX_OSERR, "route-sysctl-estimate"); | |
1030 | if ((buf = malloc(needed)) == NULL) | |
1031 | errx(EX_OSERR, "malloc failed"); | |
1032 | if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) | |
1033 | err(EX_OSERR, "actual retrieval of interface table"); | |
1034 | lim = buf + needed; | |
1035 | for (next = buf; next < lim; next += rtm->rtm_msglen) { | |
1036 | rtm = (struct rt_msghdr *)next; | |
1037 | print_rtmsg(rtm, rtm->rtm_msglen); | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | void | |
1042 | monitor() | |
1043 | { | |
1044 | int n; | |
1045 | char msg[2048]; | |
1046 | ||
1047 | verbose = 1; | |
1048 | if (debugonly) { | |
1049 | interfaces(); | |
1050 | exit(0); | |
1051 | } | |
1052 | for(;;) { | |
1053 | n = read(s, msg, 2048); | |
1054 | (void) printf("got message of size %d\n", n); | |
1055 | print_rtmsg((struct rt_msghdr *)msg, n); | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | struct { | |
1060 | struct rt_msghdr m_rtm; | |
1061 | char m_space[512]; | |
1062 | } m_rtmsg; | |
1063 | ||
1064 | int | |
1065 | rtmsg(cmd, flags) | |
1066 | int cmd, flags; | |
1067 | { | |
1068 | static int seq; | |
1069 | int rlen; | |
1070 | register char *cp = m_rtmsg.m_space; | |
1071 | register int l; | |
1072 | ||
1073 | #define NEXTADDR(w, u) \ | |
1074 | if (rtm_addrs & (w)) {\ | |
1075 | l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\ | |
1076 | if (verbose) sodump(&(u),"u");\ | |
1077 | } | |
1078 | ||
1079 | errno = 0; | |
1080 | bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); | |
1081 | if (cmd == 'a') | |
1082 | cmd = RTM_ADD; | |
1083 | else if (cmd == 'c') | |
1084 | cmd = RTM_CHANGE; | |
1085 | else if (cmd == 'g') { | |
1086 | cmd = RTM_GET; | |
1087 | if (so_ifp.sa.sa_family == 0) { | |
1088 | so_ifp.sa.sa_family = AF_LINK; | |
1089 | so_ifp.sa.sa_len = sizeof(struct sockaddr_dl); | |
1090 | rtm_addrs |= RTA_IFP; | |
1091 | } | |
1092 | } else | |
1093 | cmd = RTM_DELETE; | |
1094 | #define rtm m_rtmsg.m_rtm | |
1095 | rtm.rtm_type = cmd; | |
1096 | rtm.rtm_flags = flags; | |
1097 | rtm.rtm_version = RTM_VERSION; | |
1098 | rtm.rtm_seq = ++seq; | |
1099 | rtm.rtm_addrs = rtm_addrs; | |
1100 | rtm.rtm_rmx = rt_metrics; | |
1101 | rtm.rtm_inits = rtm_inits; | |
1102 | ||
1103 | if (rtm_addrs & RTA_NETMASK) | |
1104 | mask_addr(); | |
1105 | NEXTADDR(RTA_DST, so_dst); | |
1106 | NEXTADDR(RTA_GATEWAY, so_gate); | |
1107 | NEXTADDR(RTA_NETMASK, so_mask); | |
1108 | NEXTADDR(RTA_GENMASK, so_genmask); | |
1109 | NEXTADDR(RTA_IFP, so_ifp); | |
1110 | NEXTADDR(RTA_IFA, so_ifa); | |
1111 | rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; | |
1112 | if (verbose) | |
1113 | print_rtmsg(&rtm, l); | |
1114 | if (debugonly) | |
1115 | return (0); | |
1116 | if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { | |
1117 | warn("writing to routing socket"); | |
1118 | return (-1); | |
1119 | } | |
1120 | if (cmd == RTM_GET) { | |
1121 | do { | |
1122 | l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); | |
1123 | } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); | |
1124 | if (l < 0) | |
1125 | warn("read from routing socket"); | |
1126 | else | |
1127 | print_getmsg(&rtm, l); | |
1128 | } | |
1129 | #undef rtm | |
1130 | return (0); | |
1131 | } | |
1132 | ||
1133 | void | |
1134 | mask_addr() | |
1135 | { | |
1136 | int olen = so_mask.sa.sa_len; | |
1137 | register char *cp1 = olen + (char *)&so_mask, *cp2; | |
1138 | ||
1139 | for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; ) | |
1140 | if (*--cp1 != 0) { | |
1141 | so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask; | |
1142 | break; | |
1143 | } | |
1144 | if ((rtm_addrs & RTA_DST) == 0) | |
1145 | return; | |
1146 | switch (so_dst.sa.sa_family) { | |
1147 | #ifdef NS | |
1148 | case AF_NS: | |
1149 | #endif | |
1150 | case AF_INET: | |
1151 | case AF_APPLETALK: | |
1152 | case 0: | |
1153 | return; | |
1154 | } | |
1155 | cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst; | |
1156 | cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst; | |
1157 | while (cp2 > cp1) | |
1158 | *--cp2 = 0; | |
1159 | cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask; | |
1160 | while (cp1 > so_dst.sa.sa_data) | |
1161 | *--cp1 &= *--cp2; | |
1162 | } | |
1163 | ||
1164 | char *msgtypes[] = { | |
1165 | "", | |
1166 | "RTM_ADD: Add Route", | |
1167 | "RTM_DELETE: Delete Route", | |
1168 | "RTM_CHANGE: Change Metrics or flags", | |
1169 | "RTM_GET: Report Metrics", | |
1170 | "RTM_LOSING: Kernel Suspects Partitioning", | |
1171 | "RTM_REDIRECT: Told to use different route", | |
1172 | "RTM_MISS: Lookup failed on this address", | |
1173 | "RTM_LOCK: fix specified metrics", | |
1174 | "RTM_OLDADD: caused by SIOCADDRT", | |
1175 | "RTM_OLDDEL: caused by SIOCDELRT", | |
1176 | "RTM_RESOLVE: Route created by cloning", | |
1177 | "RTM_NEWADDR: address being added to iface", | |
1178 | "RTM_DELADDR: address being removed from iface", | |
1179 | "RTM_IFINFO: iface status change", | |
1180 | "RTM_NEWMADDR: new multicast group membership on iface", | |
1181 | "RTM_DELMADDR: multicast group membership removed from iface", | |
1182 | 0, | |
1183 | }; | |
1184 | ||
1185 | char metricnames[] = | |
1186 | "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount" | |
1187 | "\1mtu"; | |
1188 | char routeflags[] = | |
1189 | "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT" | |
1190 | "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016" | |
1191 | "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE" | |
1192 | "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST"; | |
1193 | char ifnetflags[] = | |
1194 | "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP" | |
1195 | "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1" | |
1196 | "\017LINK2\020MULTICAST"; | |
1197 | char addrnames[] = | |
1198 | "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"; | |
1199 | ||
1200 | void | |
1201 | print_rtmsg(rtm, msglen) | |
1202 | register struct rt_msghdr *rtm; | |
1203 | int msglen; | |
1204 | { | |
1205 | struct if_msghdr *ifm; | |
1206 | struct ifa_msghdr *ifam; | |
1207 | #ifdef RTM_NEWMADDR | |
1208 | struct ifma_msghdr *ifmam; | |
1209 | #endif | |
1210 | ||
1211 | if (verbose == 0) | |
1212 | return; | |
1213 | if (rtm->rtm_version != RTM_VERSION) { | |
1214 | (void) printf("routing message version %d not understood\n", | |
1215 | rtm->rtm_version); | |
1216 | return; | |
1217 | } | |
1218 | (void)printf("%s: len %d, ", msgtypes[rtm->rtm_type], rtm->rtm_msglen); | |
1219 | switch (rtm->rtm_type) { | |
1220 | case RTM_IFINFO: | |
1221 | ifm = (struct if_msghdr *)rtm; | |
1222 | (void) printf("if# %d, flags:", ifm->ifm_index); | |
1223 | bprintf(stdout, ifm->ifm_flags, ifnetflags); | |
1224 | pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs); | |
1225 | break; | |
1226 | case RTM_NEWADDR: | |
1227 | case RTM_DELADDR: | |
1228 | ifam = (struct ifa_msghdr *)rtm; | |
1229 | (void) printf("metric %d, flags:", ifam->ifam_metric); | |
1230 | bprintf(stdout, ifam->ifam_flags, routeflags); | |
1231 | pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs); | |
1232 | break; | |
1233 | #ifdef RTM_NEWMADDR | |
1234 | case RTM_NEWMADDR: | |
1235 | case RTM_DELMADDR: | |
1236 | ifmam = (struct ifma_msghdr *)rtm; | |
1237 | pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs); | |
1238 | break; | |
1239 | #endif | |
1240 | default: | |
1241 | (void) printf("pid: %ld, seq %d, errno %d, flags:", | |
1242 | (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno); | |
1243 | bprintf(stdout, rtm->rtm_flags, routeflags); | |
1244 | pmsg_common(rtm); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | void | |
1249 | print_getmsg(rtm, msglen) | |
1250 | register struct rt_msghdr *rtm; | |
1251 | int msglen; | |
1252 | { | |
1253 | struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL; | |
1254 | struct sockaddr_dl *ifp = NULL; | |
1255 | register struct sockaddr *sa; | |
1256 | register char *cp; | |
1257 | register int i; | |
1258 | ||
1259 | (void) printf(" route to: %s\n", routename(&so_dst)); | |
1260 | if (rtm->rtm_version != RTM_VERSION) { | |
1261 | warnx("routing message version %d not understood", | |
1262 | rtm->rtm_version); | |
1263 | return; | |
1264 | } | |
1265 | if (rtm->rtm_msglen > msglen) { | |
1266 | warnx("message length mismatch, in packet %d, returned %d", | |
1267 | rtm->rtm_msglen, msglen); | |
1268 | } | |
1269 | if (rtm->rtm_errno) { | |
1270 | errno = rtm->rtm_errno; | |
1271 | warn("message indicates error %d", errno); | |
1272 | return; | |
1273 | } | |
1274 | cp = ((char *)(rtm + 1)); | |
1275 | if (rtm->rtm_addrs) | |
1276 | for (i = 1; i; i <<= 1) | |
1277 | if (i & rtm->rtm_addrs) { | |
1278 | sa = (struct sockaddr *)cp; | |
1279 | switch (i) { | |
1280 | case RTA_DST: | |
1281 | dst = sa; | |
1282 | break; | |
1283 | case RTA_GATEWAY: | |
1284 | gate = sa; | |
1285 | break; | |
1286 | case RTA_NETMASK: | |
1287 | mask = sa; | |
1288 | break; | |
1289 | case RTA_IFP: | |
1290 | if (sa->sa_family == AF_LINK && | |
1291 | ((struct sockaddr_dl *)sa)->sdl_nlen) | |
1292 | ifp = (struct sockaddr_dl *)sa; | |
1293 | break; | |
1294 | } | |
1295 | ADVANCE(cp, sa); | |
1296 | } | |
1297 | if (dst && mask) | |
1298 | mask->sa_family = dst->sa_family; /* XXX */ | |
1299 | if (dst) | |
1300 | (void)printf("destination: %s\n", routename(dst)); | |
1301 | if (mask) { | |
1302 | int savenflag = nflag; | |
1303 | ||
1304 | nflag = 1; | |
1305 | (void)printf(" mask: %s\n", routename(mask)); | |
1306 | nflag = savenflag; | |
1307 | } | |
1308 | if (gate && rtm->rtm_flags & RTF_GATEWAY) | |
1309 | (void)printf(" gateway: %s\n", routename(gate)); | |
1310 | if (ifp) | |
1311 | (void)printf(" interface: %.*s\n", | |
1312 | ifp->sdl_nlen, ifp->sdl_data); | |
1313 | (void)printf(" flags: "); | |
1314 | bprintf(stdout, rtm->rtm_flags, routeflags); | |
1315 | ||
1316 | #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ') | |
1317 | #define msec(u) (((u) + 500) / 1000) /* usec to msec */ | |
1318 | ||
1319 | (void) printf("\n%s\n", "\ | |
1320 | recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire"); | |
1321 | printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE)); | |
1322 | printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE)); | |
1323 | printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH)); | |
1324 | printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT)); | |
1325 | printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR)); | |
1326 | printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT)); | |
1327 | printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU)); | |
1328 | if (rtm->rtm_rmx.rmx_expire) | |
1329 | rtm->rtm_rmx.rmx_expire -= time(0); | |
1330 | printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE)); | |
1331 | #undef lock | |
1332 | #undef msec | |
1333 | #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD) | |
1334 | if (verbose) | |
1335 | pmsg_common(rtm); | |
1336 | else if (rtm->rtm_addrs &~ RTA_IGN) { | |
1337 | (void) printf("sockaddrs: "); | |
1338 | bprintf(stdout, rtm->rtm_addrs, addrnames); | |
1339 | putchar('\n'); | |
1340 | } | |
1341 | #undef RTA_IGN | |
1342 | } | |
1343 | ||
1344 | void | |
1345 | pmsg_common(rtm) | |
1346 | register struct rt_msghdr *rtm; | |
1347 | { | |
1348 | (void) printf("\nlocks: "); | |
1349 | bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames); | |
1350 | (void) printf(" inits: "); | |
1351 | bprintf(stdout, rtm->rtm_inits, metricnames); | |
1352 | pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs); | |
1353 | } | |
1354 | ||
1355 | void | |
1356 | pmsg_addrs(cp, addrs) | |
1357 | char *cp; | |
1358 | int addrs; | |
1359 | { | |
1360 | register struct sockaddr *sa; | |
1361 | int i; | |
1362 | ||
1363 | if (addrs == 0) | |
1364 | return; | |
1365 | (void) printf("\nsockaddrs: "); | |
1366 | bprintf(stdout, addrs, addrnames); | |
1367 | (void) putchar('\n'); | |
1368 | for (i = 1; i; i <<= 1) | |
1369 | if (i & addrs) { | |
1370 | sa = (struct sockaddr *)cp; | |
1371 | (void) printf(" %s", routename(sa)); | |
1372 | ADVANCE(cp, sa); | |
1373 | } | |
1374 | (void) putchar('\n'); | |
1375 | (void) fflush(stdout); | |
1376 | } | |
1377 | ||
1378 | void | |
1379 | bprintf(fp, b, s) | |
1380 | register FILE *fp; | |
1381 | register int b; | |
1382 | register u_char *s; | |
1383 | { | |
1384 | register int i; | |
1385 | int gotsome = 0; | |
1386 | ||
1387 | if (b == 0) | |
1388 | return; | |
1389 | while ((i = *s++) != 0) { | |
1390 | if (b & (1 << (i-1))) { | |
1391 | if (gotsome == 0) | |
1392 | i = '<'; | |
1393 | else | |
1394 | i = ','; | |
1395 | (void) putc(i, fp); | |
1396 | gotsome = 1; | |
1397 | for (; (i = *s) > 32; s++) | |
1398 | (void) putc(i, fp); | |
1399 | } else | |
1400 | while (*s > 32) | |
1401 | s++; | |
1402 | } | |
1403 | if (gotsome) | |
1404 | (void) putc('>', fp); | |
1405 | } | |
1406 | ||
1407 | int | |
1408 | keyword(cp) | |
1409 | char *cp; | |
1410 | { | |
1411 | register struct keytab *kt = keywords; | |
1412 | ||
1413 | while (kt->kt_cp && strcmp(kt->kt_cp, cp)) | |
1414 | kt++; | |
1415 | return kt->kt_i; | |
1416 | } | |
1417 | ||
1418 | void | |
1419 | sodump(su, which) | |
1420 | register sup su; | |
1421 | char *which; | |
1422 | { | |
1423 | switch (su->sa.sa_family) { | |
1424 | case AF_LINK: | |
1425 | (void) printf("%s: link %s; ", | |
1426 | which, link_ntoa(&su->sdl)); | |
1427 | break; | |
1428 | case AF_INET: | |
1429 | (void) printf("%s: inet %s; ", | |
1430 | which, inet_ntoa(su->sin.sin_addr)); | |
1431 | break; | |
1432 | #ifdef AT | |
1433 | case AF_APPLETALK: | |
1434 | (void) printf("%s: atalk %s; ", | |
1435 | which, atalk_ntoa(su->sat.sat_addr)); | |
1436 | break; | |
1437 | #endif | |
1438 | #ifdef NS | |
1439 | case AF_NS: | |
1440 | (void) printf("%s: xns %s; ", | |
1441 | which, ns_ntoa(su->sns.sns_addr)); | |
1442 | break; | |
1443 | #endif | |
1444 | } | |
1445 | (void) fflush(stdout); | |
1446 | } | |
1447 | ||
1448 | /* States*/ | |
1449 | #define VIRGIN 0 | |
1450 | #define GOTONE 1 | |
1451 | #define GOTTWO 2 | |
1452 | /* Inputs */ | |
1453 | #define DIGIT (4*0) | |
1454 | #define END (4*1) | |
1455 | #define DELIM (4*2) | |
1456 | ||
1457 | void | |
1458 | sockaddr(addr, sa) | |
1459 | register char *addr; | |
1460 | register struct sockaddr *sa; | |
1461 | { | |
1462 | register char *cp = (char *)sa; | |
1463 | int size = sa->sa_len; | |
1464 | char *cplim = cp + size; | |
1465 | register int byte = 0, state = VIRGIN, new = 0 /* foil gcc */; | |
1466 | ||
1467 | bzero(cp, size); | |
1468 | cp++; | |
1469 | do { | |
1470 | if ((*addr >= '0') && (*addr <= '9')) { | |
1471 | new = *addr - '0'; | |
1472 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | |
1473 | new = *addr - 'a' + 10; | |
1474 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | |
1475 | new = *addr - 'A' + 10; | |
1476 | } else if (*addr == 0) | |
1477 | state |= END; | |
1478 | else | |
1479 | state |= DELIM; | |
1480 | addr++; | |
1481 | switch (state /* | INPUT */) { | |
1482 | case GOTTWO | DIGIT: | |
1483 | *cp++ = byte; /*FALLTHROUGH*/ | |
1484 | case VIRGIN | DIGIT: | |
1485 | state = GOTONE; byte = new; continue; | |
1486 | case GOTONE | DIGIT: | |
1487 | state = GOTTWO; byte = new + (byte << 4); continue; | |
1488 | default: /* | DELIM */ | |
1489 | state = VIRGIN; *cp++ = byte; byte = 0; continue; | |
1490 | case GOTONE | END: | |
1491 | case GOTTWO | END: | |
1492 | *cp++ = byte; /* FALLTHROUGH */ | |
1493 | case VIRGIN | END: | |
1494 | break; | |
1495 | } | |
1496 | break; | |
1497 | } while (cp < cplim); | |
1498 | sa->sa_len = cp - (char *)sa; | |
1499 | } | |
1500 | ||
1501 | #ifdef AT | |
1502 | int | |
1503 | atalk_aton(const char *text, struct at_addr *addr) | |
1504 | { | |
1505 | u_int net, node; | |
1506 | ||
1507 | if (sscanf(text, "%u.%u", &net, &node) != 2 | |
1508 | || net > 0xffff || node > 0xff) | |
1509 | return(0); | |
1510 | addr->s_net = htons(net); | |
1511 | addr->s_node = node; | |
1512 | return(1); | |
1513 | } | |
1514 | ||
1515 | char * | |
1516 | atalk_ntoa(struct at_addr at) | |
1517 | { | |
1518 | static char buf[20]; | |
1519 | ||
1520 | (void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node); | |
1521 | return(buf); | |
1522 | } | |
1523 | #endif |