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