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