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