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