]> git.saurik.com Git - apple/network_cmds.git/blob - route.tproj/route.c
network_cmds-176.3.5.tar.gz
[apple/network_cmds.git] / route.tproj / route.c
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.3 2003/01/07 21:03:05 bbraun 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 #include <sys/types.h>
77
78 #include <net/if.h>
79 #include <net/route.h>
80 #include <net/if_dl.h>
81 #include <netinet/in.h>
82 #ifndef __APPLE__
83 #include <netatalk/at.h>
84 #endif
85 #ifdef NS
86 #include <netns/ns.h>
87 #endif
88 #include <arpa/inet.h>
89 #include <netdb.h>
90
91 #include <ctype.h>
92 #include <err.h>
93 #include <errno.h>
94 #include <paths.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 #include <sysexits.h>
99 #include <unistd.h>
100 #include <ifaddrs.h>
101
102 struct keytab {
103 char *kt_cp;
104 int kt_i;
105 } keywords[] = {
106 #include "keywords.h"
107 {0, 0}
108 };
109
110 struct ortentry route;
111 union sockunion {
112 struct sockaddr sa;
113 struct sockaddr_in sin;
114 #ifdef INET6
115 struct sockaddr_in6 sin6;
116 #endif
117 #ifndef __APPLE__
118 struct sockaddr_at sat;
119 #endif
120 #ifdef NS
121 struct sockaddr_ns sns;
122 #endif
123 struct sockaddr_dl sdl;
124 struct sockaddr_storage ss; /* added to avoid memory overrun */
125 } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
126
127 typedef union sockunion *sup;
128 int pid, rtm_addrs, uid;
129 int s;
130 int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword();
131 int iflag, verbose, aflen = sizeof (struct sockaddr_in);
132 int locking, lockrest, debugonly;
133 struct rt_metrics rt_metrics;
134 u_long rtm_inits;
135 #ifndef __APPLE__
136 int atalk_aton __P((const char *, struct at_addr *));
137 char *atalk_ntoa __P((struct at_addr));
138 #endif
139 const char *routename(), *netname();
140 void flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
141 void print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
142 int getaddr(), rtmsg(), x25_makemask();
143 int prefixlen();
144 extern char *iso_ntoa();
145
146 void usage __P((const char *));
147
148 void
149 usage(cp)
150 const char *cp;
151 {
152 if (cp)
153 warnx("bad keyword: %s", cp);
154 (void) fprintf(stderr,
155 "usage: route [-dnqtv] command [[modifiers] args]\n");
156 exit(EX_USAGE);
157 /* NOTREACHED */
158 }
159
160 #define ROUNDUP(a) \
161 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
162 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
163
164 int
165 main(argc, argv)
166 int argc;
167 char **argv;
168 {
169 int ch;
170
171 if (argc < 2)
172 usage((char *)NULL);
173
174 while ((ch = getopt(argc, argv, "nqdtv")) != -1)
175 switch(ch) {
176 case 'n':
177 nflag = 1;
178 break;
179 case 'q':
180 qflag = 1;
181 break;
182 case 'v':
183 verbose = 1;
184 break;
185 case 't':
186 tflag = 1;
187 break;
188 case 'd':
189 debugonly = 1;
190 break;
191 case '?':
192 default:
193 usage((char *)NULL);
194 }
195 argc -= optind;
196 argv += optind;
197
198 pid = getpid();
199 uid = getuid();
200 if (tflag)
201 s = open(_PATH_DEVNULL, O_WRONLY, 0);
202 else
203 s = socket(PF_ROUTE, SOCK_RAW, 0);
204 if (s < 0)
205 err(EX_OSERR, "socket");
206 setuid(uid);
207 if (*argv)
208 switch (keyword(*argv)) {
209 case K_GET:
210 uid = 0;
211 /* FALLTHROUGH */
212
213 case K_CHANGE:
214 case K_ADD:
215 case K_DELETE:
216 newroute(argc, argv);
217 exit(0);
218 /* NOTREACHED */
219
220 case K_MONITOR:
221 monitor();
222 /* NOTREACHED */
223
224 case K_FLUSH:
225 flushroutes(argc, argv);
226 exit(0);
227 /* NOTREACHED */
228 }
229 usage(*argv);
230 /* NOTREACHED */
231 }
232
233 /*
234 * Purge all entries in the routing tables not
235 * associated with network interfaces.
236 */
237 void
238 flushroutes(argc, argv)
239 int argc;
240 char *argv[];
241 {
242 size_t needed;
243 int mib[6], rlen, seqno;
244 char *buf, *next, *lim;
245 register struct rt_msghdr *rtm;
246
247 if (uid) {
248 errx(EX_NOPERM, "must be root to alter routing table");
249 }
250 shutdown(s, 0); /* Don't want to read back our messages */
251 if (argc > 1) {
252 argv++;
253 if (argc == 2 && **argv == '-')
254 switch (keyword(*argv + 1)) {
255 case K_INET:
256 af = AF_INET;
257 break;
258 #ifdef INET6
259 case K_INET6:
260 af = AF_INET6;
261 break;
262 #endif
263 #ifndef __APPLE__
264 case K_ATALK:
265 af = AF_APPLETALK;
266 break;
267 #endif
268 #ifdef NS
269 case K_XNS:
270 af = AF_NS;
271 break;
272 #endif
273 case K_LINK:
274 af = AF_LINK;
275 break;
276 default:
277 goto bad;
278 } else
279 bad: usage(*argv);
280 }
281 mib[0] = CTL_NET;
282 mib[1] = PF_ROUTE;
283 mib[2] = 0; /* protocol */
284 mib[3] = 0; /* wildcard address family */
285 mib[4] = NET_RT_DUMP;
286 mib[5] = 0; /* no flags */
287 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
288 err(EX_OSERR, "route-sysctl-estimate");
289 if ((buf = malloc(needed)) == NULL)
290 errx(EX_OSERR, "malloc failed");
291 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
292 err(EX_OSERR, "route-sysctl-get");
293 lim = buf + needed;
294 if (verbose)
295 (void) printf("Examining routing table from sysctl\n");
296 seqno = 0; /* ??? */
297 for (next = buf; next < lim; next += rtm->rtm_msglen) {
298 rtm = (struct rt_msghdr *)next;
299 if (verbose)
300 print_rtmsg(rtm, rtm->rtm_msglen);
301 if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
302 continue;
303 if (af) {
304 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
305
306 if (sa->sa_family != af)
307 continue;
308 }
309 if (debugonly)
310 continue;
311 rtm->rtm_type = RTM_DELETE;
312 rtm->rtm_seq = seqno;
313 rlen = write(s, next, rtm->rtm_msglen);
314 if (rlen < (int)rtm->rtm_msglen) {
315 warn("write to routing socket");
316 (void) printf("got only %d for rlen\n", rlen);
317 break;
318 }
319 seqno++;
320 if (qflag)
321 continue;
322 if (verbose)
323 print_rtmsg(rtm, rlen);
324 else {
325 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
326 (void) printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
327 routename(sa) : netname(sa));
328 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
329 (void) printf("%-20.20s ", routename(sa));
330 (void) printf("done\n");
331 }
332 }
333 }
334
335 const char *
336 routename(sa)
337 struct sockaddr *sa;
338 {
339 register char *cp;
340 static char line[MAXHOSTNAMELEN + 1];
341 struct hostent *hp;
342 static char domain[MAXHOSTNAMELEN + 1];
343 static int first = 1;
344 #ifdef NS
345 char *ns_print();
346 #endif
347
348 if (first) {
349 first = 0;
350 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
351 (cp = index(domain, '.'))) {
352 domain[MAXHOSTNAMELEN] = '\0';
353 (void) strcpy(domain, cp + 1);
354 } else
355 domain[0] = 0;
356 }
357
358 if (sa->sa_len == 0)
359 strcpy(line, "default");
360 else switch (sa->sa_family) {
361
362 case AF_INET:
363 { struct in_addr in;
364 in = ((struct sockaddr_in *)sa)->sin_addr;
365
366 cp = 0;
367 if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
368 cp = "default";
369 if (cp == 0 && !nflag) {
370 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
371 AF_INET);
372 if (hp) {
373 if ((cp = index(hp->h_name, '.')) &&
374 !strcmp(cp + 1, domain))
375 *cp = 0;
376 cp = hp->h_name;
377 }
378 }
379 if (cp) {
380 strncpy(line, cp, sizeof(line) - 1);
381 line[sizeof(line) - 1] = '\0';
382 } else {
383 /* XXX - why not inet_ntoa()? */
384 #define C(x) (unsigned)((x) & 0xff)
385 in.s_addr = ntohl(in.s_addr);
386 (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
387 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
388 }
389 break;
390 }
391
392 #ifdef INET6
393 case AF_INET6:
394 {
395 struct sockaddr_in6 sin6; /* use static var for safety */
396 int niflags = 0;
397 #ifdef NI_WITHSCOPEID
398 niflags = NI_WITHSCOPEID;
399 #endif
400
401 memset(&sin6, 0, sizeof(sin6));
402 memcpy(&sin6, sa, sa->sa_len);
403 sin6.sin6_len = sizeof(struct sockaddr_in6);
404 sin6.sin6_family = AF_INET6;
405 #ifdef __KAME__
406 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
407 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
408 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
409 sin6.sin6_scope_id == 0) {
410 sin6.sin6_scope_id =
411 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
412 sin6.sin6_addr.s6_addr[2] = 0;
413 sin6.sin6_addr.s6_addr[3] = 0;
414 }
415 #endif
416 if (nflag)
417 niflags |= NI_NUMERICHOST;
418 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
419 line, sizeof(line), NULL, 0, niflags) != 0)
420 strncpy(line, "invalid", sizeof(line));
421
422 return(line);
423 }
424 #endif
425 #ifndef __APPLE__
426 case AF_APPLETALK:
427 (void) snprintf(line, sizeof(line), "atalk %s",
428 atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
429 break;
430 #endif
431
432 #ifdef NS
433 case AF_NS:
434 return (ns_print((struct sockaddr_ns *)sa));
435 #endif
436
437 case AF_LINK:
438 return (link_ntoa((struct sockaddr_dl *)sa));
439
440 default:
441 { u_short *s = (u_short *)sa;
442 u_short *slim = s + ((sa->sa_len + 1) >> 1);
443 char *cp = line + sprintf(line, "(%d)", sa->sa_family);
444 char *cpe = line + sizeof(line);
445
446 while (++s < slim && cp < cpe) /* start with sa->sa_data */
447 cp += snprintf(cp, cpe - cp, " %x", *s);
448 break;
449 }
450 }
451 return (line);
452 }
453
454 /*
455 * Return the name of the network whose address is given.
456 * The address is assumed to be that of a net or subnet, not a host.
457 */
458 const char *
459 netname(sa)
460 struct sockaddr *sa;
461 {
462 char *cp = 0;
463 static char line[MAXHOSTNAMELEN + 1];
464 struct netent *np = 0;
465 u_long net, mask;
466 register u_long i;
467 int subnetshift;
468 #ifdef NS
469 char *ns_print();
470 #endif
471
472 switch (sa->sa_family) {
473
474 case AF_INET:
475 { struct in_addr in;
476 in = ((struct sockaddr_in *)sa)->sin_addr;
477
478 i = in.s_addr = ntohl(in.s_addr);
479 if (in.s_addr == 0)
480 cp = "default";
481 else if (!nflag) {
482 if (IN_CLASSA(i)) {
483 mask = IN_CLASSA_NET;
484 subnetshift = 8;
485 } else if (IN_CLASSB(i)) {
486 mask = IN_CLASSB_NET;
487 subnetshift = 8;
488 } else {
489 mask = IN_CLASSC_NET;
490 subnetshift = 4;
491 }
492 /*
493 * If there are more bits than the standard mask
494 * would suggest, subnets must be in use.
495 * Guess at the subnet mask, assuming reasonable
496 * width subnet fields.
497 */
498 while (in.s_addr &~ mask)
499 mask = (long)mask >> subnetshift;
500 net = in.s_addr & mask;
501 while ((mask & 1) == 0)
502 mask >>= 1, net >>= 1;
503 np = getnetbyaddr(net, AF_INET);
504 if (np)
505 cp = np->n_name;
506 }
507 if (cp)
508 strncpy(line, cp, sizeof(line));
509 else if ((in.s_addr & 0xffffff) == 0)
510 (void) sprintf(line, "%u", C(in.s_addr >> 24));
511 else if ((in.s_addr & 0xffff) == 0)
512 (void) sprintf(line, "%u.%u", C(in.s_addr >> 24),
513 C(in.s_addr >> 16));
514 else if ((in.s_addr & 0xff) == 0)
515 (void) sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
516 C(in.s_addr >> 16), C(in.s_addr >> 8));
517 else
518 (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
519 C(in.s_addr >> 16), C(in.s_addr >> 8),
520 C(in.s_addr));
521 break;
522 }
523
524 #ifdef INET6
525 case AF_INET6:
526 {
527 struct sockaddr_in6 sin6; /* use static var for safety */
528 int niflags = 0;
529 #ifdef NI_WITHSCOPEID
530 niflags = NI_WITHSCOPEID;
531 #endif
532
533 memset(&sin6, 0, sizeof(sin6));
534 memcpy(&sin6, sa, sa->sa_len);
535 sin6.sin6_len = sizeof(struct sockaddr_in6);
536 sin6.sin6_family = AF_INET6;
537 #ifdef __KAME__
538 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
539 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
540 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
541 sin6.sin6_scope_id == 0) {
542 sin6.sin6_scope_id =
543 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
544 sin6.sin6_addr.s6_addr[2] = 0;
545 sin6.sin6_addr.s6_addr[3] = 0;
546 }
547 #endif
548 if (nflag)
549 niflags |= NI_NUMERICHOST;
550 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
551 line, sizeof(line), NULL, 0, niflags) != 0)
552 strncpy(line, "invalid", sizeof(line));
553
554 return(line);
555 }
556 #endif
557
558 #ifndef __APPLE__
559 case AF_APPLETALK:
560 (void) snprintf(line, sizeof(line), "atalk %s",
561 atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
562 break;
563 #endif
564
565 #ifdef NS
566 case AF_NS:
567 return (ns_print((struct sockaddr_ns *)sa));
568 break;
569 #endif
570
571 case AF_LINK:
572 return (link_ntoa((struct sockaddr_dl *)sa));
573
574
575 default:
576 { u_short *s = (u_short *)sa->sa_data;
577 u_short *slim = s + ((sa->sa_len + 1)>>1);
578 char *cp = line + sprintf(line, "af %d:", sa->sa_family);
579 char *cpe = line + sizeof(line);
580
581 while (s < slim && cp < cpe)
582 cp += snprintf(cp, cpe - cp, " %x", *s++);
583 break;
584 }
585 }
586 return (line);
587 }
588
589 void
590 set_metric(value, key)
591 char *value;
592 int key;
593 {
594 int flag = 0;
595 u_long noval, *valp = &noval;
596
597 switch (key) {
598 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
599 caseof(K_MTU, RTV_MTU, rmx_mtu);
600 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
601 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
602 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
603 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
604 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
605 caseof(K_RTT, RTV_RTT, rmx_rtt);
606 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
607 }
608 rtm_inits |= flag;
609 if (lockrest || locking)
610 rt_metrics.rmx_locks |= flag;
611 if (locking)
612 locking = 0;
613 *valp = atoi(value);
614 }
615
616 void
617 newroute(argc, argv)
618 int argc;
619 register char **argv;
620 {
621 char *cmd, *dest = "", *gateway = "", *err;
622 int ishost = 0, ret, attempts, oerrno, flags = RTF_STATIC;
623 int key;
624 struct hostent *hp = 0;
625
626 if (uid) {
627 errx(EX_NOPERM, "must be root to alter routing table");
628 }
629 cmd = argv[0];
630 if (*cmd != 'g')
631 shutdown(s, 0); /* Don't want to read back our messages */
632 while (--argc > 0) {
633 if (**(++argv)== '-') {
634 switch (key = keyword(1 + *argv)) {
635 case K_LINK:
636 af = AF_LINK;
637 aflen = sizeof(struct sockaddr_dl);
638 break;
639 case K_INET:
640 af = AF_INET;
641 aflen = sizeof(struct sockaddr_in);
642 break;
643 #ifdef INET6
644 case K_INET6:
645 af = AF_INET6;
646 aflen = sizeof(struct sockaddr_in6);
647 break;
648 #endif
649 #ifndef __APPLE__
650 case K_ATALK:
651 af = AF_APPLETALK;
652 aflen = sizeof(struct sockaddr_at);
653 break;
654 #endif
655 case K_SA:
656 af = PF_ROUTE;
657 aflen = sizeof(union sockunion);
658 break;
659 #ifdef NS
660 case K_XNS:
661 af = AF_NS;
662 aflen = sizeof(struct sockaddr_ns);
663 break;
664 #endif
665 case K_IFACE:
666 case K_INTERFACE:
667 iflag++;
668 break;
669 case K_NOSTATIC:
670 flags &= ~RTF_STATIC;
671 break;
672 case K_LLINFO:
673 flags |= RTF_LLINFO;
674 break;
675 case K_LOCK:
676 locking = 1;
677 break;
678 case K_LOCKREST:
679 lockrest = 1;
680 break;
681 case K_HOST:
682 forcehost++;
683 break;
684 case K_REJECT:
685 flags |= RTF_REJECT;
686 break;
687 case K_BLACKHOLE:
688 flags |= RTF_BLACKHOLE;
689 break;
690 case K_PROTO1:
691 flags |= RTF_PROTO1;
692 break;
693 case K_PROTO2:
694 flags |= RTF_PROTO2;
695 break;
696 case K_CLONING:
697 flags |= RTF_CLONING;
698 break;
699 case K_XRESOLVE:
700 flags |= RTF_XRESOLVE;
701 break;
702 case K_STATIC:
703 flags |= RTF_STATIC;
704 break;
705 case K_IFA:
706 if (!--argc)
707 usage((char *)NULL);
708 (void) getaddr(RTA_IFA, *++argv, 0);
709 break;
710 case K_IFP:
711 if (!--argc)
712 usage((char *)NULL);
713 (void) getaddr(RTA_IFP, *++argv, 0);
714 break;
715 case K_GENMASK:
716 if (!--argc)
717 usage((char *)NULL);
718 (void) getaddr(RTA_GENMASK, *++argv, 0);
719 break;
720 case K_GATEWAY:
721 if (!--argc)
722 usage((char *)NULL);
723 (void) getaddr(RTA_GATEWAY, *++argv, 0);
724 break;
725 case K_DST:
726 if (!--argc)
727 usage((char *)NULL);
728 ishost = getaddr(RTA_DST, *++argv, &hp);
729 dest = *argv;
730 break;
731 case K_NETMASK:
732 if (!--argc)
733 usage((char *)NULL);
734 (void) getaddr(RTA_NETMASK, *++argv, 0);
735 /* FALLTHROUGH */
736 case K_NET:
737 forcenet++;
738 break;
739 case K_PREFIXLEN:
740 if (!--argc)
741 usage((char *)NULL);
742 if (prefixlen(*++argv) == -1) {
743 forcenet = 0;
744 ishost = 1;
745 } else {
746 forcenet = 1;
747 ishost = 0;
748 }
749 break;
750 case K_MTU:
751 case K_HOPCOUNT:
752 case K_EXPIRE:
753 case K_RECVPIPE:
754 case K_SENDPIPE:
755 case K_SSTHRESH:
756 case K_RTT:
757 case K_RTTVAR:
758 if (!--argc)
759 usage((char *)NULL);
760 set_metric(*++argv, key);
761 break;
762 default:
763 usage(1+*argv);
764 }
765 } else {
766 if ((rtm_addrs & RTA_DST) == 0) {
767 dest = *argv;
768 ishost = getaddr(RTA_DST, *argv, &hp);
769 } else if ((rtm_addrs & RTA_GATEWAY) == 0) {
770 gateway = *argv;
771 (void) getaddr(RTA_GATEWAY, *argv, &hp);
772 } else {
773 (void) getaddr(RTA_NETMASK, *argv, 0);
774 }
775 }
776 }
777 if (forcehost) {
778 ishost = 1;
779 #ifdef INET6
780 if (af == AF_INET6) {
781 rtm_addrs &= ~RTA_NETMASK;
782 memset((void *)&so_mask, 0, sizeof(so_mask));
783 }
784 #endif
785 }
786 if (forcenet)
787 ishost = 0;
788 flags |= RTF_UP;
789 if (ishost)
790 flags |= RTF_HOST;
791 if (iflag == 0)
792 flags |= RTF_GATEWAY;
793 if (so_mask.sin.sin_family == AF_INET) {
794 // make sure the mask is contiguous
795 long i;
796 for (i = 0; i < 32; i++)
797 if (((so_mask.sin.sin_addr.s_addr) & ntohl((1 << i))) != 0)
798 break;
799 for (; i < 32; i++)
800 if (((so_mask.sin.sin_addr.s_addr) & ntohl((1 << i))) == 0)
801 errx(EX_NOHOST, "invalid mask: %s", inet_ntoa(so_mask.sin.sin_addr));
802 }
803 for (attempts = 1; ; attempts++) {
804 errno = 0;
805 if ((ret = rtmsg(*cmd, flags)) == 0)
806 break;
807 if (errno != ENETUNREACH && errno != ESRCH)
808 break;
809 if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
810 hp->h_addr_list++;
811 bcopy(hp->h_addr_list[0], &so_gate.sin.sin_addr,
812 MIN(hp->h_length, sizeof(so_gate.sin.sin_addr)));
813 } else
814 break;
815 }
816 if (*cmd == 'g')
817 exit(0);
818 oerrno = errno;
819 (void) printf("%s %s %s", cmd, ishost? "host" : "net", dest);
820 if (*gateway) {
821 (void) printf(": gateway %s", gateway);
822 if (attempts > 1 && ret == 0 && af == AF_INET)
823 (void) printf(" (%s)",
824 inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
825 }
826 if (ret == 0)
827 (void) printf("\n");
828 else {
829 switch (oerrno) {
830 case ESRCH:
831 err = "not in table";
832 break;
833 case EBUSY:
834 err = "entry in use";
835 break;
836 case ENOBUFS:
837 err = "routing table overflow";
838 break;
839 default:
840 err = strerror(oerrno);
841 break;
842 }
843 (void) printf(": %s\n", err);
844 }
845 }
846
847 void
848 inet_makenetandmask(net, sin, bits)
849 u_long net, bits;
850 register struct sockaddr_in *sin;
851 {
852 u_long addr, mask = 0;
853 register char *cp;
854
855 rtm_addrs |= RTA_NETMASK;
856 if (bits) {
857 addr = net;
858 mask = 0xffffffff << (32 - bits);
859 } else if (net == 0)
860 mask = addr = 0;
861 else if (net < 128) {
862 addr = net << IN_CLASSA_NSHIFT;
863 mask = IN_CLASSA_NET;
864 } else if (net < 65536) {
865 addr = net << IN_CLASSB_NSHIFT;
866 mask = IN_CLASSB_NET;
867 } else if (net < 16777216L) {
868 addr = net << IN_CLASSC_NSHIFT;
869 mask = IN_CLASSC_NET;
870 } else {
871 addr = net;
872 if ((addr & IN_CLASSA_HOST) == 0)
873 mask = IN_CLASSA_NET;
874 else if ((addr & IN_CLASSB_HOST) == 0)
875 mask = IN_CLASSB_NET;
876 else if ((addr & IN_CLASSC_HOST) == 0)
877 mask = IN_CLASSC_NET;
878 else
879 mask = -1;
880 }
881 sin->sin_addr.s_addr = htonl(addr);
882 sin = &so_mask.sin;
883 sin->sin_addr.s_addr = htonl(mask);
884 sin->sin_len = 0;
885 sin->sin_family = 0;
886 cp = (char *)(&sin->sin_addr + 1);
887 while (*--cp == 0 && cp > (char *)sin)
888 ;
889 sin->sin_len = 1 + cp - (char *)sin;
890 }
891
892 /*
893 * Interpret an argument as a network address of some kind,
894 * returning 1 if a host address, 0 if a network address.
895 */
896 int
897 getaddr(which, s, hpp)
898 int which;
899 char *s;
900 struct hostent **hpp;
901 {
902 register sup su;
903 struct hostent *hp;
904 struct netent *np;
905 u_long val;
906 char *q;
907 int afamily; /* local copy of af so we can change it */
908
909 if (af == 0) {
910 af = AF_INET;
911 aflen = sizeof(struct sockaddr_in);
912 }
913 afamily = af;
914 rtm_addrs |= which;
915 switch (which) {
916 case RTA_DST:
917 su = &so_dst;
918 break;
919 case RTA_GATEWAY:
920 su = &so_gate;
921 if (iflag) {
922 struct ifaddrs *ifap, *ifa;
923 struct sockaddr_dl *sdl = NULL;
924
925 if (getifaddrs(&ifap))
926 err(1, "getifaddrs");
927
928 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
929 if (ifa->ifa_addr->sa_family != AF_LINK)
930 continue;
931
932 if (strcmp(s, ifa->ifa_name))
933 continue;
934
935 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
936 }
937 /* If we found it, then use it */
938 if (sdl) {
939 /*
940 * Copy is safe since we have a
941 * sockaddr_storage member in sockunion{}.
942 * Note that we need to copy before calling
943 * freeifaddrs().
944 */
945 memcpy(&su->sdl, sdl, sdl->sdl_len);
946 }
947 freeifaddrs(ifap);
948 if (sdl)
949 return(1);
950 }
951 break;
952 case RTA_NETMASK:
953 su = &so_mask;
954 break;
955 case RTA_GENMASK:
956 su = &so_genmask;
957 break;
958 case RTA_IFP:
959 su = &so_ifp;
960 afamily = AF_LINK;
961 break;
962 case RTA_IFA:
963 su = &so_ifa;
964 break;
965 default:
966 usage("internal error");
967 /*NOTREACHED*/
968 }
969 su->sa.sa_len = aflen;
970 su->sa.sa_family = afamily; /* cases that don't want it have left already */
971 if (strcmp(s, "default") == 0) {
972 /*
973 * Default is net 0.0.0.0/0
974 */
975 switch (which) {
976 case RTA_DST:
977 forcenet++;
978 /* bzero(su, sizeof(*su)); *//* for readability */
979 (void) getaddr(RTA_NETMASK, s, 0);
980 break;
981 case RTA_NETMASK:
982 case RTA_GENMASK:
983 /* bzero(su, sizeof(*su)); *//* for readability */
984 break;
985 }
986 return (0);
987 }
988 switch (afamily) {
989 #ifdef INET6
990 case AF_INET6:
991 {
992 struct addrinfo hints, *res;
993
994 memset(&hints, 0, sizeof(hints));
995 hints.ai_family = afamily; /*AF_INET6*/
996 hints.ai_flags = AI_NUMERICHOST;
997 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
998 if (getaddrinfo(s, "0", &hints, &res) != 0 ||
999 res->ai_family != AF_INET6 ||
1000 res->ai_addrlen != sizeof(su->sin6)) {
1001 (void) fprintf(stderr, "%s: bad value\n", s);
1002 exit(1);
1003 }
1004 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
1005 #ifdef __KAME__
1006 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
1007 IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr)) &&
1008 su->sin6.sin6_scope_id) {
1009 *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
1010 htons(su->sin6.sin6_scope_id);
1011 su->sin6.sin6_scope_id = 0;
1012 }
1013 #endif
1014 freeaddrinfo(res);
1015 return (0);
1016 }
1017 #endif /* INET6 */
1018
1019 #ifdef NS
1020 case AF_NS:
1021 if (which == RTA_DST) {
1022 extern short ns_bh[3];
1023 struct sockaddr_ns *sms = &(so_mask.sns);
1024 bzero((char *)sms, sizeof(*sms));
1025 sms->sns_family = 0;
1026 sms->sns_len = 6;
1027 sms->sns_addr.x_net = *(union ns_net *)ns_bh;
1028 rtm_addrs |= RTA_NETMASK;
1029 }
1030 su->sns.sns_addr = ns_addr(s);
1031 return (!ns_nullhost(su->sns.sns_addr));
1032 #endif
1033
1034
1035 #ifndef __APPLE__
1036 case AF_APPLETALK:
1037 if (!atalk_aton(s, &su->sat.sat_addr))
1038 errx(EX_NOHOST, "bad address: %s", s);
1039 rtm_addrs |= RTA_NETMASK;
1040 return(forcehost || su->sat.sat_addr.s_node != 0);
1041 #endif
1042
1043 case AF_LINK:
1044 link_addr(s, &su->sdl);
1045 return (1);
1046
1047
1048 case PF_ROUTE:
1049 su->sa.sa_len = sizeof(*su);
1050 sockaddr(s, &su->sa);
1051 return (1);
1052
1053 case AF_INET:
1054 default:
1055 break;
1056 }
1057
1058 if (hpp == NULL)
1059 hpp = &hp;
1060 *hpp = NULL;
1061
1062 q = strchr(s,'/');
1063 if (q && which == RTA_DST) {
1064 *q = '\0';
1065 if ((val = inet_addr(s)) != INADDR_NONE) {
1066 inet_makenetandmask(
1067 ntohl(val), &su->sin, strtoul(q+1, 0, 0));
1068 return (0);
1069 }
1070 *q = '/';
1071 }
1072 if ((which != RTA_DST || forcenet == 0) &&
1073 (val = inet_addr(s)) != INADDR_NONE) {
1074 su->sin.sin_addr.s_addr = val;
1075 if (which != RTA_DST ||
1076 inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
1077 return (1);
1078 else {
1079 val = ntohl(val);
1080 goto netdone;
1081 }
1082 }
1083 if (which == RTA_DST && forcehost == 0 &&
1084 ((val = inet_network(s)) != INADDR_NONE ||
1085 ((np = getnetbyname(s)) != NULL && (val = np->n_net) != 0))) {
1086 netdone:
1087 inet_makenetandmask(val, &su->sin, 0);
1088 return (0);
1089 }
1090 hp = gethostbyname(s);
1091 if (hp) {
1092 *hpp = hp;
1093 su->sin.sin_family = hp->h_addrtype;
1094 bcopy(hp->h_addr, (char *)&su->sin.sin_addr,
1095 MIN(hp->h_length, sizeof(su->sin.sin_addr)));
1096 return (1);
1097 }
1098 errx(EX_NOHOST, "bad address: %s", s);
1099 }
1100
1101 int
1102 prefixlen(s)
1103 char *s;
1104 {
1105 int len = atoi(s), q, r;
1106 int max;
1107 char *p;
1108
1109 rtm_addrs |= RTA_NETMASK;
1110 switch (af) {
1111 #ifdef INET6
1112 case AF_INET6:
1113 max = 128;
1114 p = (char *)&so_mask.sin6.sin6_addr;
1115 break;
1116 #endif
1117 case AF_INET:
1118 max = 32;
1119 p = (char *)&so_mask.sin.sin_addr;
1120 break;
1121 default:
1122 (void) fprintf(stderr, "prefixlen not supported in this af\n");
1123 exit(1);
1124 /*NOTREACHED*/
1125 }
1126
1127 if (len < 0 || max < len) {
1128 (void) fprintf(stderr, "%s: bad value\n", s);
1129 exit(1);
1130 }
1131
1132 q = len >> 3;
1133 r = len & 7;
1134 so_mask.sa.sa_family = af;
1135 so_mask.sa.sa_len = aflen;
1136 memset((void *)p, 0, max / 8);
1137 if (q > 0)
1138 memset((void *)p, 0xff, q);
1139 if (r > 0)
1140 *((u_char *)p + q) = (0xff00 >> r) & 0xff;
1141 if (len == max)
1142 return -1;
1143 else
1144 return len;
1145 }
1146
1147 #ifdef NS
1148 short ns_nullh[] = {0,0,0};
1149 short ns_bh[] = {-1,-1,-1};
1150
1151 char *
1152 ns_print(sns)
1153 struct sockaddr_ns *sns;
1154 {
1155 struct ns_addr work;
1156 union { union ns_net net_e; u_long long_e; } net;
1157 u_short port;
1158 static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
1159 char *host = "";
1160 register char *p;
1161 register u_char *q;
1162
1163 work = sns->sns_addr;
1164 port = ntohs(work.x_port);
1165 work.x_port = 0;
1166 net.net_e = work.x_net;
1167 if (ns_nullhost(work) && net.long_e == 0) {
1168 if (!port)
1169 return ("*.*");
1170 (void) sprintf(mybuf, "*.%XH", port);
1171 return (mybuf);
1172 }
1173
1174 if (bcmp((char *)ns_bh, (char *)work.x_host.c_host, 6) == 0)
1175 host = "any";
1176 else if (bcmp((char *)ns_nullh, (char *)work.x_host.c_host, 6) == 0)
1177 host = "*";
1178 else {
1179 q = work.x_host.c_host;
1180 (void) sprintf(chost, "%02X%02X%02X%02X%02X%02XH",
1181 q[0], q[1], q[2], q[3], q[4], q[5]);
1182 for (p = chost; *p == '0' && p < chost + 12; p++)
1183 /* void */;
1184 host = p;
1185 }
1186 if (port)
1187 (void) sprintf(cport, ".%XH", htons(port));
1188 else
1189 *cport = 0;
1190
1191 (void) snprintf(mybuf, sizeof(mybuf), "%lxH.%s%s",
1192 (unsigned long)ntohl(net.long_e),
1193 host, cport);
1194 return (mybuf);
1195 }
1196 #endif
1197
1198 void
1199 interfaces()
1200 {
1201 size_t needed;
1202 int mib[6];
1203 char *buf, *lim, *next;
1204 register struct rt_msghdr *rtm;
1205
1206 mib[0] = CTL_NET;
1207 mib[1] = PF_ROUTE;
1208 mib[2] = 0; /* protocol */
1209 mib[3] = 0; /* wildcard address family */
1210 mib[4] = NET_RT_IFLIST;
1211 mib[5] = 0; /* no flags */
1212 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1213 err(EX_OSERR, "route-sysctl-estimate");
1214 if ((buf = malloc(needed)) == NULL)
1215 errx(EX_OSERR, "malloc failed");
1216 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1217 err(EX_OSERR, "actual retrieval of interface table");
1218 lim = buf + needed;
1219 for (next = buf; next < lim; next += rtm->rtm_msglen) {
1220 rtm = (struct rt_msghdr *)next;
1221 print_rtmsg(rtm, rtm->rtm_msglen);
1222 }
1223 }
1224
1225 void
1226 monitor()
1227 {
1228 int n;
1229 char msg[2048];
1230
1231 verbose = 1;
1232 if (debugonly) {
1233 interfaces();
1234 exit(0);
1235 }
1236 for(;;) {
1237 time_t now;
1238 n = read(s, msg, 2048);
1239 now = time(NULL);
1240 (void) printf("\ngot message of size %d on %s", n, ctime(&now));
1241 print_rtmsg((struct rt_msghdr *)msg, n);
1242 }
1243 }
1244
1245 struct {
1246 struct rt_msghdr m_rtm;
1247 char m_space[512];
1248 } m_rtmsg;
1249
1250 int
1251 rtmsg(cmd, flags)
1252 int cmd, flags;
1253 {
1254 static int seq;
1255 int rlen;
1256 register char *cp = m_rtmsg.m_space;
1257 register int l;
1258
1259 #define NEXTADDR(w, u) \
1260 if (rtm_addrs & (w)) {\
1261 l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\
1262 if (verbose) sodump(&(u),"u");\
1263 }
1264
1265 errno = 0;
1266 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
1267 if (cmd == 'a')
1268 cmd = RTM_ADD;
1269 else if (cmd == 'c')
1270 cmd = RTM_CHANGE;
1271 else if (cmd == 'g') {
1272 cmd = RTM_GET;
1273 if (so_ifp.sa.sa_family == 0) {
1274 so_ifp.sa.sa_family = AF_LINK;
1275 so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1276 rtm_addrs |= RTA_IFP;
1277 }
1278 } else
1279 cmd = RTM_DELETE;
1280 #define rtm m_rtmsg.m_rtm
1281 rtm.rtm_type = cmd;
1282 rtm.rtm_flags = flags;
1283 rtm.rtm_version = RTM_VERSION;
1284 rtm.rtm_seq = ++seq;
1285 rtm.rtm_addrs = rtm_addrs;
1286 rtm.rtm_rmx = rt_metrics;
1287 rtm.rtm_inits = rtm_inits;
1288
1289 if (rtm_addrs & RTA_NETMASK)
1290 mask_addr();
1291 NEXTADDR(RTA_DST, so_dst);
1292 NEXTADDR(RTA_GATEWAY, so_gate);
1293 NEXTADDR(RTA_NETMASK, so_mask);
1294 NEXTADDR(RTA_GENMASK, so_genmask);
1295 NEXTADDR(RTA_IFP, so_ifp);
1296 NEXTADDR(RTA_IFA, so_ifa);
1297 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1298 if (verbose)
1299 print_rtmsg(&rtm, l);
1300 if (debugonly)
1301 return (0);
1302 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1303 warn("writing to routing socket");
1304 return (-1);
1305 }
1306 if (cmd == RTM_GET) {
1307 do {
1308 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1309 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1310 if (l < 0)
1311 warn("read from routing socket");
1312 else
1313 print_getmsg(&rtm, l);
1314 }
1315 #undef rtm
1316 return (0);
1317 }
1318
1319 void
1320 mask_addr()
1321 {
1322 int olen = so_mask.sa.sa_len;
1323 register char *cp1 = olen + (char *)&so_mask, *cp2;
1324
1325 for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1326 if (*--cp1 != 0) {
1327 so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1328 break;
1329 }
1330 if ((rtm_addrs & RTA_DST) == 0)
1331 return;
1332 switch (so_dst.sa.sa_family) {
1333 #ifdef NS
1334 case AF_NS:
1335 #endif
1336 case AF_INET:
1337 #ifdef INET6
1338 case AF_INET6:
1339 #endif
1340 case AF_APPLETALK:
1341 case 0:
1342 return;
1343 }
1344 cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1345 cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1346 while (cp2 > cp1)
1347 *--cp2 = 0;
1348 cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1349 while (cp1 > so_dst.sa.sa_data)
1350 *--cp1 &= *--cp2;
1351 }
1352
1353 char *msgtypes[] = {
1354 "",
1355 "RTM_ADD: Add Route",
1356 "RTM_DELETE: Delete Route",
1357 "RTM_CHANGE: Change Metrics or flags",
1358 "RTM_GET: Report Metrics",
1359 "RTM_LOSING: Kernel Suspects Partitioning",
1360 "RTM_REDIRECT: Told to use different route",
1361 "RTM_MISS: Lookup failed on this address",
1362 "RTM_LOCK: fix specified metrics",
1363 "RTM_OLDADD: caused by SIOCADDRT",
1364 "RTM_OLDDEL: caused by SIOCDELRT",
1365 "RTM_RESOLVE: Route created by cloning",
1366 "RTM_NEWADDR: address being added to iface",
1367 "RTM_DELADDR: address being removed from iface",
1368 "RTM_IFINFO: iface status change",
1369 "RTM_NEWMADDR: new multicast group membership on iface",
1370 "RTM_DELMADDR: multicast group membership removed from iface",
1371 0,
1372 };
1373
1374 char metricnames[] =
1375 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1376 "\1mtu";
1377 char routeflags[] =
1378 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1379 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1380 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1381 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1382 char ifnetflags[] =
1383 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1384 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1385 "\017LINK2\020MULTICAST";
1386 char addrnames[] =
1387 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1388
1389 void
1390 print_rtmsg(rtm, msglen)
1391 register struct rt_msghdr *rtm;
1392 int msglen;
1393 {
1394 struct if_msghdr *ifm;
1395 struct ifa_msghdr *ifam;
1396 #ifdef RTM_NEWMADDR
1397 struct ifma_msghdr *ifmam;
1398 #endif
1399
1400 if (verbose == 0)
1401 return;
1402 if (rtm->rtm_version != RTM_VERSION) {
1403 (void) printf("routing message version %d not understood\n",
1404 rtm->rtm_version);
1405 return;
1406 }
1407 (void)printf("%s: len %d, ", msgtypes[rtm->rtm_type], rtm->rtm_msglen);
1408 switch (rtm->rtm_type) {
1409 case RTM_IFINFO:
1410 ifm = (struct if_msghdr *)rtm;
1411 (void) printf("if# %d, flags:", ifm->ifm_index);
1412 bprintf(stdout, ifm->ifm_flags, ifnetflags);
1413 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1414 break;
1415 case RTM_NEWADDR:
1416 case RTM_DELADDR:
1417 ifam = (struct ifa_msghdr *)rtm;
1418 (void) printf("metric %d, flags:", ifam->ifam_metric);
1419 bprintf(stdout, ifam->ifam_flags, routeflags);
1420 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1421 break;
1422 #ifdef RTM_NEWMADDR
1423 case RTM_NEWMADDR:
1424 case RTM_DELMADDR:
1425 ifmam = (struct ifma_msghdr *)rtm;
1426 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1427 break;
1428 #endif
1429 default:
1430 (void) printf("pid: %ld, seq %d, errno %d, flags:",
1431 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1432 bprintf(stdout, rtm->rtm_flags, routeflags);
1433 pmsg_common(rtm);
1434 }
1435 }
1436
1437 void
1438 print_getmsg(rtm, msglen)
1439 register struct rt_msghdr *rtm;
1440 int msglen;
1441 {
1442 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1443 struct sockaddr_dl *ifp = NULL;
1444 register struct sockaddr *sa;
1445 register char *cp;
1446 register int i;
1447
1448 (void) printf(" route to: %s\n", routename(&so_dst));
1449 if (rtm->rtm_version != RTM_VERSION) {
1450 warnx("routing message version %d not understood",
1451 rtm->rtm_version);
1452 return;
1453 }
1454 if (rtm->rtm_msglen > msglen) {
1455 warnx("message length mismatch, in packet %d, returned %d",
1456 rtm->rtm_msglen, msglen);
1457 }
1458 if (rtm->rtm_errno) {
1459 errno = rtm->rtm_errno;
1460 warn("message indicates error %d", errno);
1461 return;
1462 }
1463 cp = ((char *)(rtm + 1));
1464 if (rtm->rtm_addrs)
1465 for (i = 1; i; i <<= 1)
1466 if (i & rtm->rtm_addrs) {
1467 sa = (struct sockaddr *)cp;
1468 switch (i) {
1469 case RTA_DST:
1470 dst = sa;
1471 break;
1472 case RTA_GATEWAY:
1473 gate = sa;
1474 break;
1475 case RTA_NETMASK:
1476 mask = sa;
1477 break;
1478 case RTA_IFP:
1479 if (sa->sa_family == AF_LINK &&
1480 ((struct sockaddr_dl *)sa)->sdl_nlen)
1481 ifp = (struct sockaddr_dl *)sa;
1482 break;
1483 }
1484 ADVANCE(cp, sa);
1485 }
1486 if (dst && mask)
1487 mask->sa_family = dst->sa_family; /* XXX */
1488 if (dst)
1489 (void)printf("destination: %s\n", routename(dst));
1490 if (mask) {
1491 int savenflag = nflag;
1492
1493 nflag = 1;
1494 (void)printf(" mask: %s\n", routename(mask));
1495 nflag = savenflag;
1496 }
1497 if (gate && rtm->rtm_flags & RTF_GATEWAY)
1498 (void)printf(" gateway: %s\n", routename(gate));
1499 if (ifp)
1500 (void)printf(" interface: %.*s\n",
1501 ifp->sdl_nlen, ifp->sdl_data);
1502 (void)printf(" flags: ");
1503 bprintf(stdout, rtm->rtm_flags, routeflags);
1504
1505 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1506 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1507
1508 (void) printf("\n%s\n", "\
1509 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1510 printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1511 printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1512 printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1513 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1514 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1515 printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1516 printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
1517 if (rtm->rtm_rmx.rmx_expire)
1518 rtm->rtm_rmx.rmx_expire -= time(0);
1519 printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
1520 #undef lock
1521 #undef msec
1522 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1523 if (verbose)
1524 pmsg_common(rtm);
1525 else if (rtm->rtm_addrs &~ RTA_IGN) {
1526 (void) printf("sockaddrs: ");
1527 bprintf(stdout, rtm->rtm_addrs, addrnames);
1528 putchar('\n');
1529 }
1530 #undef RTA_IGN
1531 }
1532
1533 void
1534 pmsg_common(rtm)
1535 register struct rt_msghdr *rtm;
1536 {
1537 (void) printf("\nlocks: ");
1538 bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1539 (void) printf(" inits: ");
1540 bprintf(stdout, rtm->rtm_inits, metricnames);
1541 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1542 }
1543
1544 void
1545 pmsg_addrs(cp, addrs)
1546 char *cp;
1547 int addrs;
1548 {
1549 register struct sockaddr *sa;
1550 int i;
1551
1552 if (addrs == 0) {
1553 (void) putchar('\n');
1554 return;
1555 }
1556 (void) printf("\nsockaddrs: ");
1557 bprintf(stdout, addrs, addrnames);
1558 (void) putchar('\n');
1559 for (i = 1; i; i <<= 1)
1560 if (i & addrs) {
1561 sa = (struct sockaddr *)cp;
1562 (void) printf(" %s", routename(sa));
1563 ADVANCE(cp, sa);
1564 }
1565 (void) putchar('\n');
1566 (void) fflush(stdout);
1567 }
1568
1569 void
1570 bprintf(fp, b, s)
1571 register FILE *fp;
1572 register int b;
1573 register u_char *s;
1574 {
1575 register int i;
1576 int gotsome = 0;
1577
1578 if (b == 0)
1579 return;
1580 while ((i = *s++) != 0) {
1581 if (b & (1 << (i-1))) {
1582 if (gotsome == 0)
1583 i = '<';
1584 else
1585 i = ',';
1586 (void) putc(i, fp);
1587 gotsome = 1;
1588 for (; (i = *s) > 32; s++)
1589 (void) putc(i, fp);
1590 } else
1591 while (*s > 32)
1592 s++;
1593 }
1594 if (gotsome)
1595 (void) putc('>', fp);
1596 }
1597
1598 int
1599 keyword(cp)
1600 char *cp;
1601 {
1602 register struct keytab *kt = keywords;
1603
1604 while (kt->kt_cp && strcmp(kt->kt_cp, cp))
1605 kt++;
1606 return kt->kt_i;
1607 }
1608
1609 void
1610 sodump(su, which)
1611 register sup su;
1612 char *which;
1613 {
1614 switch (su->sa.sa_family) {
1615 case AF_LINK:
1616 (void) printf("%s: link %s; ",
1617 which, link_ntoa(&su->sdl));
1618 break;
1619 case AF_INET:
1620 (void) printf("%s: inet %s; ",
1621 which, inet_ntoa(su->sin.sin_addr));
1622 break;
1623 #ifndef __APPLE__
1624 case AF_APPLETALK:
1625 (void) printf("%s: atalk %s; ",
1626 which, atalk_ntoa(su->sat.sat_addr));
1627 break;
1628 #endif
1629 #ifdef NS
1630 case AF_NS:
1631 (void) printf("%s: xns %s; ",
1632 which, ns_ntoa(su->sns.sns_addr));
1633 break;
1634 #endif
1635 }
1636 (void) fflush(stdout);
1637 }
1638
1639 /* States*/
1640 #define VIRGIN 0
1641 #define GOTONE 1
1642 #define GOTTWO 2
1643 /* Inputs */
1644 #define DIGIT (4*0)
1645 #define END (4*1)
1646 #define DELIM (4*2)
1647
1648 void
1649 sockaddr(addr, sa)
1650 register char *addr;
1651 register struct sockaddr *sa;
1652 {
1653 register char *cp = (char *)sa;
1654 int size = sa->sa_len;
1655 char *cplim = cp + size;
1656 register int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
1657
1658 bzero(cp, size);
1659 cp++;
1660 do {
1661 if ((*addr >= '0') && (*addr <= '9')) {
1662 new = *addr - '0';
1663 } else if ((*addr >= 'a') && (*addr <= 'f')) {
1664 new = *addr - 'a' + 10;
1665 } else if ((*addr >= 'A') && (*addr <= 'F')) {
1666 new = *addr - 'A' + 10;
1667 } else if (*addr == 0)
1668 state |= END;
1669 else
1670 state |= DELIM;
1671 addr++;
1672 switch (state /* | INPUT */) {
1673 case GOTTWO | DIGIT:
1674 *cp++ = byte; /*FALLTHROUGH*/
1675 case VIRGIN | DIGIT:
1676 state = GOTONE; byte = new; continue;
1677 case GOTONE | DIGIT:
1678 state = GOTTWO; byte = new + (byte << 4); continue;
1679 default: /* | DELIM */
1680 state = VIRGIN; *cp++ = byte; byte = 0; continue;
1681 case GOTONE | END:
1682 case GOTTWO | END:
1683 *cp++ = byte; /* FALLTHROUGH */
1684 case VIRGIN | END:
1685 break;
1686 }
1687 break;
1688 } while (cp < cplim);
1689 sa->sa_len = cp - (char *)sa;
1690 }
1691
1692 #ifndef __APPLE__
1693 int
1694 atalk_aton(const char *text, struct at_addr *addr)
1695 {
1696 u_int net, node;
1697
1698 if (sscanf(text, "%u.%u", &net, &node) != 2
1699 || net > 0xffff || node > 0xff)
1700 return(0);
1701 addr->s_net = htons(net);
1702 addr->s_node = node;
1703 return(1);
1704 }
1705
1706 char *
1707 atalk_ntoa(struct at_addr at)
1708 {
1709 static char buf[20];
1710
1711 (void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
1712 return(buf);
1713 }
1714 #endif