]> git.saurik.com Git - apple/network_cmds.git/blame - route.tproj/route.c
network_cmds-115.tar.gz
[apple/network_cmds.git] / route.tproj / route.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1983, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#ifndef lint
58static 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
65static char sccsid[] = "@(#)route.c 8.3 (Berkeley) 3/19/94";
66#endif
67static const char rcsid[] =
7ba0088d 68 "$Id: route.c,v 1.2 2002/03/05 20:35:16 lindak Exp $";
b7080c8e
A
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>
7ba0088d 76#include <sys/types.h>
b7080c8e
A
77
78#include <net/if.h>
79#include <net/route.h>
80#include <net/if_dl.h>
81#include <netinet/in.h>
7ba0088d 82#ifndef __APPLE__
b7080c8e
A
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>
7ba0088d 94#include <paths.h>
b7080c8e
A
95#include <stdio.h>
96#include <stdlib.h>
97#include <string.h>
98#include <sysexits.h>
99#include <unistd.h>
7ba0088d 100#include <ifaddrs.h>
b7080c8e
A
101
102struct keytab {
103 char *kt_cp;
104 int kt_i;
105} keywords[] = {
106#include "keywords.h"
107 {0, 0}
108};
109
110struct ortentry route;
111union sockunion {
112 struct sockaddr sa;
113 struct sockaddr_in sin;
7ba0088d
A
114#ifdef INET6
115 struct sockaddr_in6 sin6;
116#endif
117#ifndef __APPLE__
b7080c8e
A
118 struct sockaddr_at sat;
119#endif
120#ifdef NS
121 struct sockaddr_ns sns;
122#endif
123 struct sockaddr_dl sdl;
7ba0088d 124 struct sockaddr_storage ss; /* added to avoid memory overrun */
b7080c8e
A
125} so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
126
127typedef union sockunion *sup;
128int pid, rtm_addrs, uid;
129int s;
130int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword();
131int iflag, verbose, aflen = sizeof (struct sockaddr_in);
132int locking, lockrest, debugonly;
133struct rt_metrics rt_metrics;
134u_long rtm_inits;
7ba0088d 135#ifndef __APPLE__
b7080c8e
A
136int atalk_aton __P((const char *, struct at_addr *));
137char *atalk_ntoa __P((struct at_addr));
138#endif
7ba0088d 139const char *routename(), *netname();
b7080c8e
A
140void flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
141void print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
142int getaddr(), rtmsg(), x25_makemask();
7ba0088d
A
143int prefixlen();
144extern char *iso_ntoa();
b7080c8e
A
145
146void usage __P((const char *));
147
148void
149usage(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
164int
165main(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)
7ba0088d 201 s = open(_PATH_DEVNULL, O_WRONLY, 0);
b7080c8e
A
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 */
237void
238flushroutes(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;
7ba0088d
A
258#ifdef INET6
259 case K_INET6:
260 af = AF_INET6;
261 break;
262#endif
263#ifndef __APPLE__
b7080c8e
A
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
279bad: 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));
7ba0088d 328 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
b7080c8e
A
329 (void) printf("%-20.20s ", routename(sa));
330 (void) printf("done\n");
331 }
332 }
333}
334
7ba0088d 335const char *
b7080c8e
A
336routename(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 }
7ba0088d
A
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__
b7080c8e
A
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
7ba0088d 431
b7080c8e
A
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 */
7ba0088d 458const char *
b7080c8e
A
459netname(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 }
7ba0088d
A
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__
b7080c8e
A
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
7ba0088d 564
b7080c8e
A
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
589void
590set_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
616void
617newroute(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;
7ba0088d
A
643#ifdef INET6
644 case K_INET6:
645 af = AF_INET6;
646 aflen = sizeof(struct sockaddr_in6);
647 break;
648#endif
649#ifndef __APPLE__
b7080c8e
A
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;
b7080c8e
A
672 case K_LLINFO:
673 flags |= RTF_LLINFO;
674 break;
b7080c8e
A
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:
7ba0088d
A
706 if (!--argc)
707 usage((char *)NULL);
b7080c8e
A
708 (void) getaddr(RTA_IFA, *++argv, 0);
709 break;
710 case K_IFP:
7ba0088d
A
711 if (!--argc)
712 usage((char *)NULL);
b7080c8e
A
713 (void) getaddr(RTA_IFP, *++argv, 0);
714 break;
715 case K_GENMASK:
7ba0088d
A
716 if (!--argc)
717 usage((char *)NULL);
b7080c8e
A
718 (void) getaddr(RTA_GENMASK, *++argv, 0);
719 break;
720 case K_GATEWAY:
7ba0088d
A
721 if (!--argc)
722 usage((char *)NULL);
b7080c8e
A
723 (void) getaddr(RTA_GATEWAY, *++argv, 0);
724 break;
725 case K_DST:
7ba0088d
A
726 if (!--argc)
727 usage((char *)NULL);
b7080c8e
A
728 ishost = getaddr(RTA_DST, *++argv, &hp);
729 dest = *argv;
730 break;
731 case K_NETMASK:
7ba0088d
A
732 if (!--argc)
733 usage((char *)NULL);
b7080c8e
A
734 (void) getaddr(RTA_NETMASK, *++argv, 0);
735 /* FALLTHROUGH */
736 case K_NET:
737 forcenet++;
738 break;
7ba0088d
A
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;
b7080c8e
A
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:
7ba0088d
A
758 if (!--argc)
759 usage((char *)NULL);
b7080c8e
A
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 }
7ba0088d 777 if (forcehost) {
b7080c8e 778 ishost = 1;
7ba0088d
A
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 }
b7080c8e
A
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 for (attempts = 1; ; attempts++) {
794 errno = 0;
795 if ((ret = rtmsg(*cmd, flags)) == 0)
796 break;
797 if (errno != ENETUNREACH && errno != ESRCH)
798 break;
799 if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
800 hp->h_addr_list++;
801 bcopy(hp->h_addr_list[0], &so_gate.sin.sin_addr,
802 MIN(hp->h_length, sizeof(so_gate.sin.sin_addr)));
803 } else
804 break;
805 }
806 if (*cmd == 'g')
807 exit(0);
808 oerrno = errno;
809 (void) printf("%s %s %s", cmd, ishost? "host" : "net", dest);
810 if (*gateway) {
811 (void) printf(": gateway %s", gateway);
812 if (attempts > 1 && ret == 0 && af == AF_INET)
813 (void) printf(" (%s)",
814 inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
815 }
816 if (ret == 0)
817 (void) printf("\n");
818 else {
819 switch (oerrno) {
820 case ESRCH:
821 err = "not in table";
822 break;
823 case EBUSY:
824 err = "entry in use";
825 break;
826 case ENOBUFS:
827 err = "routing table overflow";
828 break;
829 default:
830 err = strerror(oerrno);
831 break;
832 }
833 (void) printf(": %s\n", err);
834 }
835}
836
837void
838inet_makenetandmask(net, sin, bits)
839 u_long net, bits;
840 register struct sockaddr_in *sin;
841{
842 u_long addr, mask = 0;
843 register char *cp;
844
845 rtm_addrs |= RTA_NETMASK;
7ba0088d 846 if (bits) {
b7080c8e
A
847 addr = net;
848 mask = 0xffffffff << (32 - bits);
7ba0088d
A
849 } else if (net == 0)
850 mask = addr = 0;
851 else if (net < 128) {
b7080c8e
A
852 addr = net << IN_CLASSA_NSHIFT;
853 mask = IN_CLASSA_NET;
854 } else if (net < 65536) {
855 addr = net << IN_CLASSB_NSHIFT;
856 mask = IN_CLASSB_NET;
857 } else if (net < 16777216L) {
858 addr = net << IN_CLASSC_NSHIFT;
859 mask = IN_CLASSC_NET;
860 } else {
861 addr = net;
862 if ((addr & IN_CLASSA_HOST) == 0)
863 mask = IN_CLASSA_NET;
864 else if ((addr & IN_CLASSB_HOST) == 0)
865 mask = IN_CLASSB_NET;
866 else if ((addr & IN_CLASSC_HOST) == 0)
867 mask = IN_CLASSC_NET;
868 else
869 mask = -1;
870 }
871 sin->sin_addr.s_addr = htonl(addr);
872 sin = &so_mask.sin;
873 sin->sin_addr.s_addr = htonl(mask);
874 sin->sin_len = 0;
875 sin->sin_family = 0;
876 cp = (char *)(&sin->sin_addr + 1);
877 while (*--cp == 0 && cp > (char *)sin)
878 ;
879 sin->sin_len = 1 + cp - (char *)sin;
880}
881
882/*
883 * Interpret an argument as a network address of some kind,
884 * returning 1 if a host address, 0 if a network address.
885 */
886int
887getaddr(which, s, hpp)
888 int which;
889 char *s;
890 struct hostent **hpp;
891{
892 register sup su;
b7080c8e
A
893 struct hostent *hp;
894 struct netent *np;
895 u_long val;
7ba0088d
A
896 char *q;
897 int afamily; /* local copy of af so we can change it */
b7080c8e
A
898
899 if (af == 0) {
900 af = AF_INET;
901 aflen = sizeof(struct sockaddr_in);
902 }
7ba0088d 903 afamily = af;
b7080c8e
A
904 rtm_addrs |= which;
905 switch (which) {
906 case RTA_DST:
907 su = &so_dst;
908 break;
909 case RTA_GATEWAY:
910 su = &so_gate;
911 if (iflag) {
7ba0088d
A
912 struct ifaddrs *ifap, *ifa;
913 struct sockaddr_dl *sdl = NULL;
914
915 if (getifaddrs(&ifap))
916 err(1, "getifaddrs");
917
918 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
919 if (ifa->ifa_addr->sa_family != AF_LINK)
920 continue;
921
922 if (strcmp(s, ifa->ifa_name))
923 continue;
b7080c8e 924
7ba0088d
A
925 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
926 }
b7080c8e
A
927 /* If we found it, then use it */
928 if (sdl) {
7ba0088d
A
929 /*
930 * Copy is safe since we have a
931 * sockaddr_storage member in sockunion{}.
932 * Note that we need to copy before calling
933 * freeifaddrs().
934 */
935 memcpy(&su->sdl, sdl, sdl->sdl_len);
b7080c8e 936 }
7ba0088d
A
937 freeifaddrs(ifap);
938 if (sdl)
939 return(1);
b7080c8e
A
940 }
941 break;
942 case RTA_NETMASK:
943 su = &so_mask;
944 break;
945 case RTA_GENMASK:
946 su = &so_genmask;
947 break;
948 case RTA_IFP:
949 su = &so_ifp;
7ba0088d 950 afamily = AF_LINK;
b7080c8e
A
951 break;
952 case RTA_IFA:
953 su = &so_ifa;
954 break;
955 default:
956 usage("internal error");
957 /*NOTREACHED*/
958 }
959 su->sa.sa_len = aflen;
7ba0088d 960 su->sa.sa_family = afamily; /* cases that don't want it have left already */
b7080c8e
A
961 if (strcmp(s, "default") == 0) {
962 /*
963 * Default is net 0.0.0.0/0
964 */
965 switch (which) {
966 case RTA_DST:
967 forcenet++;
968 /* bzero(su, sizeof(*su)); *//* for readability */
969 (void) getaddr(RTA_NETMASK, s, 0);
970 break;
b7080c8e
A
971 case RTA_NETMASK:
972 case RTA_GENMASK:
973 /* bzero(su, sizeof(*su)); *//* for readability */
7ba0088d 974 break;
b7080c8e
A
975 }
976 return (0);
977 }
7ba0088d
A
978 switch (afamily) {
979#ifdef INET6
980 case AF_INET6:
981 {
982 struct addrinfo hints, *res;
983
984 memset(&hints, 0, sizeof(hints));
985 hints.ai_family = afamily; /*AF_INET6*/
986 hints.ai_flags = AI_NUMERICHOST;
987 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
988 if (getaddrinfo(s, "0", &hints, &res) != 0 ||
989 res->ai_family != AF_INET6 ||
990 res->ai_addrlen != sizeof(su->sin6)) {
991 (void) fprintf(stderr, "%s: bad value\n", s);
992 exit(1);
993 }
994 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
995#ifdef __KAME__
996 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
997 IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr)) &&
998 su->sin6.sin6_scope_id) {
999 *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
1000 htons(su->sin6.sin6_scope_id);
1001 su->sin6.sin6_scope_id = 0;
1002 }
1003#endif
1004 freeaddrinfo(res);
1005 return (0);
1006 }
1007#endif /* INET6 */
1008
b7080c8e
A
1009#ifdef NS
1010 case AF_NS:
1011 if (which == RTA_DST) {
1012 extern short ns_bh[3];
1013 struct sockaddr_ns *sms = &(so_mask.sns);
1014 bzero((char *)sms, sizeof(*sms));
1015 sms->sns_family = 0;
1016 sms->sns_len = 6;
1017 sms->sns_addr.x_net = *(union ns_net *)ns_bh;
1018 rtm_addrs |= RTA_NETMASK;
1019 }
1020 su->sns.sns_addr = ns_addr(s);
1021 return (!ns_nullhost(su->sns.sns_addr));
1022#endif
1023
7ba0088d
A
1024
1025#ifndef __APPLE__
b7080c8e
A
1026 case AF_APPLETALK:
1027 if (!atalk_aton(s, &su->sat.sat_addr))
1028 errx(EX_NOHOST, "bad address: %s", s);
1029 rtm_addrs |= RTA_NETMASK;
1030 return(forcehost || su->sat.sat_addr.s_node != 0);
1031#endif
7ba0088d 1032
b7080c8e
A
1033 case AF_LINK:
1034 link_addr(s, &su->sdl);
1035 return (1);
1036
1037
1038 case PF_ROUTE:
1039 su->sa.sa_len = sizeof(*su);
1040 sockaddr(s, &su->sa);
1041 return (1);
1042
1043 case AF_INET:
1044 default:
1045 break;
1046 }
1047
1048 if (hpp == NULL)
1049 hpp = &hp;
1050 *hpp = NULL;
1051
1052 q = strchr(s,'/');
1053 if (q && which == RTA_DST) {
b7080c8e 1054 *q = '\0';
7ba0088d 1055 if ((val = inet_addr(s)) != INADDR_NONE) {
b7080c8e 1056 inet_makenetandmask(
7ba0088d 1057 ntohl(val), &su->sin, strtoul(q+1, 0, 0));
b7080c8e
A
1058 return (0);
1059 }
7ba0088d 1060 *q = '/';
b7080c8e 1061 }
7ba0088d
A
1062 if ((which != RTA_DST || forcenet == 0) &&
1063 (val = inet_addr(s)) != INADDR_NONE) {
b7080c8e 1064 su->sin.sin_addr.s_addr = val;
7ba0088d
A
1065 if (which != RTA_DST ||
1066 inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
b7080c8e
A
1067 return (1);
1068 else {
1069 val = ntohl(val);
1070 goto netdone;
1071 }
1072 }
7ba0088d
A
1073 if (which == RTA_DST && forcehost == 0 &&
1074 ((val = inet_network(s)) != INADDR_NONE ||
1075 ((np = getnetbyname(s)) != NULL && (val = np->n_net) != 0))) {
b7080c8e 1076netdone:
7ba0088d 1077 inet_makenetandmask(val, &su->sin, 0);
b7080c8e
A
1078 return (0);
1079 }
1080 hp = gethostbyname(s);
1081 if (hp) {
1082 *hpp = hp;
1083 su->sin.sin_family = hp->h_addrtype;
1084 bcopy(hp->h_addr, (char *)&su->sin.sin_addr,
1085 MIN(hp->h_length, sizeof(su->sin.sin_addr)));
1086 return (1);
1087 }
1088 errx(EX_NOHOST, "bad address: %s", s);
1089}
1090
7ba0088d
A
1091int
1092prefixlen(s)
1093 char *s;
1094{
1095 int len = atoi(s), q, r;
1096 int max;
1097 char *p;
1098
1099 rtm_addrs |= RTA_NETMASK;
1100 switch (af) {
1101#ifdef INET6
1102 case AF_INET6:
1103 max = 128;
1104 p = (char *)&so_mask.sin6.sin6_addr;
1105 break;
1106#endif
1107 case AF_INET:
1108 max = 32;
1109 p = (char *)&so_mask.sin.sin_addr;
1110 break;
1111 default:
1112 (void) fprintf(stderr, "prefixlen not supported in this af\n");
1113 exit(1);
1114 /*NOTREACHED*/
1115 }
1116
1117 if (len < 0 || max < len) {
1118 (void) fprintf(stderr, "%s: bad value\n", s);
1119 exit(1);
1120 }
1121
1122 q = len >> 3;
1123 r = len & 7;
1124 so_mask.sa.sa_family = af;
1125 so_mask.sa.sa_len = aflen;
1126 memset((void *)p, 0, max / 8);
1127 if (q > 0)
1128 memset((void *)p, 0xff, q);
1129 if (r > 0)
1130 *((u_char *)p + q) = (0xff00 >> r) & 0xff;
1131 if (len == max)
1132 return -1;
1133 else
1134 return len;
1135}
b7080c8e
A
1136
1137#ifdef NS
1138short ns_nullh[] = {0,0,0};
1139short ns_bh[] = {-1,-1,-1};
1140
1141char *
1142ns_print(sns)
1143 struct sockaddr_ns *sns;
1144{
1145 struct ns_addr work;
1146 union { union ns_net net_e; u_long long_e; } net;
1147 u_short port;
1148 static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
1149 char *host = "";
1150 register char *p;
1151 register u_char *q;
1152
1153 work = sns->sns_addr;
1154 port = ntohs(work.x_port);
1155 work.x_port = 0;
1156 net.net_e = work.x_net;
1157 if (ns_nullhost(work) && net.long_e == 0) {
1158 if (!port)
1159 return ("*.*");
1160 (void) sprintf(mybuf, "*.%XH", port);
1161 return (mybuf);
1162 }
1163
1164 if (bcmp((char *)ns_bh, (char *)work.x_host.c_host, 6) == 0)
1165 host = "any";
1166 else if (bcmp((char *)ns_nullh, (char *)work.x_host.c_host, 6) == 0)
1167 host = "*";
1168 else {
1169 q = work.x_host.c_host;
1170 (void) sprintf(chost, "%02X%02X%02X%02X%02X%02XH",
1171 q[0], q[1], q[2], q[3], q[4], q[5]);
1172 for (p = chost; *p == '0' && p < chost + 12; p++)
1173 /* void */;
1174 host = p;
1175 }
1176 if (port)
1177 (void) sprintf(cport, ".%XH", htons(port));
1178 else
1179 *cport = 0;
1180
7ba0088d
A
1181 (void) snprintf(mybuf, sizeof(mybuf), "%lxH.%s%s",
1182 (unsigned long)ntohl(net.long_e),
1183 host, cport);
b7080c8e
A
1184 return (mybuf);
1185}
1186#endif
1187
1188void
1189interfaces()
1190{
1191 size_t needed;
1192 int mib[6];
1193 char *buf, *lim, *next;
1194 register struct rt_msghdr *rtm;
1195
1196 mib[0] = CTL_NET;
1197 mib[1] = PF_ROUTE;
1198 mib[2] = 0; /* protocol */
1199 mib[3] = 0; /* wildcard address family */
1200 mib[4] = NET_RT_IFLIST;
1201 mib[5] = 0; /* no flags */
1202 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1203 err(EX_OSERR, "route-sysctl-estimate");
1204 if ((buf = malloc(needed)) == NULL)
1205 errx(EX_OSERR, "malloc failed");
1206 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1207 err(EX_OSERR, "actual retrieval of interface table");
1208 lim = buf + needed;
1209 for (next = buf; next < lim; next += rtm->rtm_msglen) {
1210 rtm = (struct rt_msghdr *)next;
1211 print_rtmsg(rtm, rtm->rtm_msglen);
1212 }
1213}
1214
1215void
1216monitor()
1217{
1218 int n;
1219 char msg[2048];
1220
1221 verbose = 1;
1222 if (debugonly) {
1223 interfaces();
1224 exit(0);
1225 }
1226 for(;;) {
7ba0088d 1227 time_t now;
b7080c8e 1228 n = read(s, msg, 2048);
7ba0088d
A
1229 now = time(NULL);
1230 (void) printf("\ngot message of size %d on %s", n, ctime(&now));
b7080c8e
A
1231 print_rtmsg((struct rt_msghdr *)msg, n);
1232 }
1233}
1234
1235struct {
1236 struct rt_msghdr m_rtm;
1237 char m_space[512];
1238} m_rtmsg;
1239
1240int
1241rtmsg(cmd, flags)
1242 int cmd, flags;
1243{
1244 static int seq;
1245 int rlen;
1246 register char *cp = m_rtmsg.m_space;
1247 register int l;
1248
1249#define NEXTADDR(w, u) \
1250 if (rtm_addrs & (w)) {\
1251 l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\
1252 if (verbose) sodump(&(u),"u");\
1253 }
1254
1255 errno = 0;
1256 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
1257 if (cmd == 'a')
1258 cmd = RTM_ADD;
1259 else if (cmd == 'c')
1260 cmd = RTM_CHANGE;
1261 else if (cmd == 'g') {
1262 cmd = RTM_GET;
1263 if (so_ifp.sa.sa_family == 0) {
1264 so_ifp.sa.sa_family = AF_LINK;
1265 so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1266 rtm_addrs |= RTA_IFP;
1267 }
1268 } else
1269 cmd = RTM_DELETE;
1270#define rtm m_rtmsg.m_rtm
1271 rtm.rtm_type = cmd;
1272 rtm.rtm_flags = flags;
1273 rtm.rtm_version = RTM_VERSION;
1274 rtm.rtm_seq = ++seq;
1275 rtm.rtm_addrs = rtm_addrs;
1276 rtm.rtm_rmx = rt_metrics;
1277 rtm.rtm_inits = rtm_inits;
1278
1279 if (rtm_addrs & RTA_NETMASK)
1280 mask_addr();
1281 NEXTADDR(RTA_DST, so_dst);
1282 NEXTADDR(RTA_GATEWAY, so_gate);
1283 NEXTADDR(RTA_NETMASK, so_mask);
1284 NEXTADDR(RTA_GENMASK, so_genmask);
1285 NEXTADDR(RTA_IFP, so_ifp);
1286 NEXTADDR(RTA_IFA, so_ifa);
1287 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1288 if (verbose)
1289 print_rtmsg(&rtm, l);
1290 if (debugonly)
1291 return (0);
1292 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1293 warn("writing to routing socket");
1294 return (-1);
1295 }
1296 if (cmd == RTM_GET) {
1297 do {
1298 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1299 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1300 if (l < 0)
1301 warn("read from routing socket");
1302 else
1303 print_getmsg(&rtm, l);
1304 }
1305#undef rtm
1306 return (0);
1307}
1308
1309void
1310mask_addr()
1311{
1312 int olen = so_mask.sa.sa_len;
1313 register char *cp1 = olen + (char *)&so_mask, *cp2;
1314
1315 for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1316 if (*--cp1 != 0) {
1317 so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1318 break;
1319 }
1320 if ((rtm_addrs & RTA_DST) == 0)
1321 return;
1322 switch (so_dst.sa.sa_family) {
1323#ifdef NS
1324 case AF_NS:
1325#endif
1326 case AF_INET:
7ba0088d
A
1327#ifdef INET6
1328 case AF_INET6:
1329#endif
b7080c8e
A
1330 case AF_APPLETALK:
1331 case 0:
1332 return;
1333 }
1334 cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1335 cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1336 while (cp2 > cp1)
1337 *--cp2 = 0;
1338 cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1339 while (cp1 > so_dst.sa.sa_data)
1340 *--cp1 &= *--cp2;
1341}
1342
1343char *msgtypes[] = {
1344 "",
1345 "RTM_ADD: Add Route",
1346 "RTM_DELETE: Delete Route",
1347 "RTM_CHANGE: Change Metrics or flags",
1348 "RTM_GET: Report Metrics",
1349 "RTM_LOSING: Kernel Suspects Partitioning",
1350 "RTM_REDIRECT: Told to use different route",
1351 "RTM_MISS: Lookup failed on this address",
1352 "RTM_LOCK: fix specified metrics",
1353 "RTM_OLDADD: caused by SIOCADDRT",
1354 "RTM_OLDDEL: caused by SIOCDELRT",
1355 "RTM_RESOLVE: Route created by cloning",
1356 "RTM_NEWADDR: address being added to iface",
1357 "RTM_DELADDR: address being removed from iface",
1358 "RTM_IFINFO: iface status change",
1359 "RTM_NEWMADDR: new multicast group membership on iface",
1360 "RTM_DELMADDR: multicast group membership removed from iface",
1361 0,
1362};
1363
1364char metricnames[] =
1365"\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1366"\1mtu";
1367char routeflags[] =
1368"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1369"\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1370"\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1371"\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1372char ifnetflags[] =
1373"\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1374"\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1375"\017LINK2\020MULTICAST";
1376char addrnames[] =
1377"\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1378
1379void
1380print_rtmsg(rtm, msglen)
1381 register struct rt_msghdr *rtm;
1382 int msglen;
1383{
1384 struct if_msghdr *ifm;
1385 struct ifa_msghdr *ifam;
1386#ifdef RTM_NEWMADDR
1387 struct ifma_msghdr *ifmam;
1388#endif
1389
1390 if (verbose == 0)
1391 return;
1392 if (rtm->rtm_version != RTM_VERSION) {
1393 (void) printf("routing message version %d not understood\n",
1394 rtm->rtm_version);
1395 return;
1396 }
1397 (void)printf("%s: len %d, ", msgtypes[rtm->rtm_type], rtm->rtm_msglen);
1398 switch (rtm->rtm_type) {
1399 case RTM_IFINFO:
1400 ifm = (struct if_msghdr *)rtm;
1401 (void) printf("if# %d, flags:", ifm->ifm_index);
1402 bprintf(stdout, ifm->ifm_flags, ifnetflags);
1403 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1404 break;
1405 case RTM_NEWADDR:
1406 case RTM_DELADDR:
1407 ifam = (struct ifa_msghdr *)rtm;
1408 (void) printf("metric %d, flags:", ifam->ifam_metric);
1409 bprintf(stdout, ifam->ifam_flags, routeflags);
1410 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1411 break;
1412#ifdef RTM_NEWMADDR
1413 case RTM_NEWMADDR:
1414 case RTM_DELMADDR:
1415 ifmam = (struct ifma_msghdr *)rtm;
1416 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1417 break;
1418#endif
1419 default:
1420 (void) printf("pid: %ld, seq %d, errno %d, flags:",
1421 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1422 bprintf(stdout, rtm->rtm_flags, routeflags);
1423 pmsg_common(rtm);
1424 }
1425}
1426
1427void
1428print_getmsg(rtm, msglen)
1429 register struct rt_msghdr *rtm;
1430 int msglen;
1431{
1432 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1433 struct sockaddr_dl *ifp = NULL;
1434 register struct sockaddr *sa;
1435 register char *cp;
1436 register int i;
1437
1438 (void) printf(" route to: %s\n", routename(&so_dst));
1439 if (rtm->rtm_version != RTM_VERSION) {
1440 warnx("routing message version %d not understood",
1441 rtm->rtm_version);
1442 return;
1443 }
1444 if (rtm->rtm_msglen > msglen) {
1445 warnx("message length mismatch, in packet %d, returned %d",
1446 rtm->rtm_msglen, msglen);
1447 }
1448 if (rtm->rtm_errno) {
1449 errno = rtm->rtm_errno;
1450 warn("message indicates error %d", errno);
1451 return;
1452 }
1453 cp = ((char *)(rtm + 1));
1454 if (rtm->rtm_addrs)
1455 for (i = 1; i; i <<= 1)
1456 if (i & rtm->rtm_addrs) {
1457 sa = (struct sockaddr *)cp;
1458 switch (i) {
1459 case RTA_DST:
1460 dst = sa;
1461 break;
1462 case RTA_GATEWAY:
1463 gate = sa;
1464 break;
1465 case RTA_NETMASK:
1466 mask = sa;
1467 break;
1468 case RTA_IFP:
1469 if (sa->sa_family == AF_LINK &&
1470 ((struct sockaddr_dl *)sa)->sdl_nlen)
1471 ifp = (struct sockaddr_dl *)sa;
1472 break;
1473 }
1474 ADVANCE(cp, sa);
1475 }
1476 if (dst && mask)
1477 mask->sa_family = dst->sa_family; /* XXX */
1478 if (dst)
1479 (void)printf("destination: %s\n", routename(dst));
1480 if (mask) {
1481 int savenflag = nflag;
1482
1483 nflag = 1;
1484 (void)printf(" mask: %s\n", routename(mask));
1485 nflag = savenflag;
1486 }
1487 if (gate && rtm->rtm_flags & RTF_GATEWAY)
1488 (void)printf(" gateway: %s\n", routename(gate));
1489 if (ifp)
1490 (void)printf(" interface: %.*s\n",
1491 ifp->sdl_nlen, ifp->sdl_data);
1492 (void)printf(" flags: ");
1493 bprintf(stdout, rtm->rtm_flags, routeflags);
1494
1495#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1496#define msec(u) (((u) + 500) / 1000) /* usec to msec */
1497
1498 (void) printf("\n%s\n", "\
1499 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1500 printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1501 printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1502 printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1503 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1504 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1505 printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1506 printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
1507 if (rtm->rtm_rmx.rmx_expire)
1508 rtm->rtm_rmx.rmx_expire -= time(0);
1509 printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
1510#undef lock
1511#undef msec
1512#define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1513 if (verbose)
1514 pmsg_common(rtm);
1515 else if (rtm->rtm_addrs &~ RTA_IGN) {
1516 (void) printf("sockaddrs: ");
1517 bprintf(stdout, rtm->rtm_addrs, addrnames);
1518 putchar('\n');
1519 }
1520#undef RTA_IGN
1521}
1522
1523void
1524pmsg_common(rtm)
1525 register struct rt_msghdr *rtm;
1526{
1527 (void) printf("\nlocks: ");
1528 bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1529 (void) printf(" inits: ");
1530 bprintf(stdout, rtm->rtm_inits, metricnames);
1531 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1532}
1533
1534void
1535pmsg_addrs(cp, addrs)
1536 char *cp;
1537 int addrs;
1538{
1539 register struct sockaddr *sa;
1540 int i;
1541
7ba0088d
A
1542 if (addrs == 0) {
1543 (void) putchar('\n');
b7080c8e 1544 return;
7ba0088d 1545 }
b7080c8e
A
1546 (void) printf("\nsockaddrs: ");
1547 bprintf(stdout, addrs, addrnames);
1548 (void) putchar('\n');
1549 for (i = 1; i; i <<= 1)
1550 if (i & addrs) {
1551 sa = (struct sockaddr *)cp;
1552 (void) printf(" %s", routename(sa));
1553 ADVANCE(cp, sa);
1554 }
1555 (void) putchar('\n');
1556 (void) fflush(stdout);
1557}
1558
1559void
1560bprintf(fp, b, s)
1561 register FILE *fp;
1562 register int b;
1563 register u_char *s;
1564{
1565 register int i;
1566 int gotsome = 0;
1567
1568 if (b == 0)
1569 return;
1570 while ((i = *s++) != 0) {
1571 if (b & (1 << (i-1))) {
1572 if (gotsome == 0)
1573 i = '<';
1574 else
1575 i = ',';
1576 (void) putc(i, fp);
1577 gotsome = 1;
1578 for (; (i = *s) > 32; s++)
1579 (void) putc(i, fp);
1580 } else
1581 while (*s > 32)
1582 s++;
1583 }
1584 if (gotsome)
1585 (void) putc('>', fp);
1586}
1587
1588int
1589keyword(cp)
1590 char *cp;
1591{
1592 register struct keytab *kt = keywords;
1593
1594 while (kt->kt_cp && strcmp(kt->kt_cp, cp))
1595 kt++;
1596 return kt->kt_i;
1597}
1598
1599void
1600sodump(su, which)
1601 register sup su;
1602 char *which;
1603{
1604 switch (su->sa.sa_family) {
1605 case AF_LINK:
1606 (void) printf("%s: link %s; ",
1607 which, link_ntoa(&su->sdl));
1608 break;
1609 case AF_INET:
1610 (void) printf("%s: inet %s; ",
1611 which, inet_ntoa(su->sin.sin_addr));
1612 break;
7ba0088d 1613#ifndef __APPLE__
b7080c8e
A
1614 case AF_APPLETALK:
1615 (void) printf("%s: atalk %s; ",
1616 which, atalk_ntoa(su->sat.sat_addr));
1617 break;
1618#endif
1619#ifdef NS
1620 case AF_NS:
1621 (void) printf("%s: xns %s; ",
1622 which, ns_ntoa(su->sns.sns_addr));
1623 break;
1624#endif
1625 }
1626 (void) fflush(stdout);
1627}
1628
1629/* States*/
1630#define VIRGIN 0
1631#define GOTONE 1
1632#define GOTTWO 2
1633/* Inputs */
1634#define DIGIT (4*0)
1635#define END (4*1)
1636#define DELIM (4*2)
1637
1638void
1639sockaddr(addr, sa)
1640 register char *addr;
1641 register struct sockaddr *sa;
1642{
1643 register char *cp = (char *)sa;
1644 int size = sa->sa_len;
1645 char *cplim = cp + size;
1646 register int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
1647
1648 bzero(cp, size);
1649 cp++;
1650 do {
1651 if ((*addr >= '0') && (*addr <= '9')) {
1652 new = *addr - '0';
1653 } else if ((*addr >= 'a') && (*addr <= 'f')) {
1654 new = *addr - 'a' + 10;
1655 } else if ((*addr >= 'A') && (*addr <= 'F')) {
1656 new = *addr - 'A' + 10;
1657 } else if (*addr == 0)
1658 state |= END;
1659 else
1660 state |= DELIM;
1661 addr++;
1662 switch (state /* | INPUT */) {
1663 case GOTTWO | DIGIT:
1664 *cp++ = byte; /*FALLTHROUGH*/
1665 case VIRGIN | DIGIT:
1666 state = GOTONE; byte = new; continue;
1667 case GOTONE | DIGIT:
1668 state = GOTTWO; byte = new + (byte << 4); continue;
1669 default: /* | DELIM */
1670 state = VIRGIN; *cp++ = byte; byte = 0; continue;
1671 case GOTONE | END:
1672 case GOTTWO | END:
1673 *cp++ = byte; /* FALLTHROUGH */
1674 case VIRGIN | END:
1675 break;
1676 }
1677 break;
1678 } while (cp < cplim);
1679 sa->sa_len = cp - (char *)sa;
1680}
1681
7ba0088d 1682#ifndef __APPLE__
b7080c8e
A
1683int
1684atalk_aton(const char *text, struct at_addr *addr)
1685{
1686 u_int net, node;
1687
1688 if (sscanf(text, "%u.%u", &net, &node) != 2
1689 || net > 0xffff || node > 0xff)
1690 return(0);
1691 addr->s_net = htons(net);
1692 addr->s_node = node;
1693 return(1);
1694}
1695
1696char *
1697atalk_ntoa(struct at_addr at)
1698{
1699 static char buf[20];
1700
1701 (void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
1702 return(buf);
1703}
1704#endif