GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/route/route.c Lines: 395 1143 34.6 %
Date: 2017-11-07 Branches: 251 802 31.3 %

Line Branch Exec Source
1
/*	$OpenBSD: route.c,v 1.203 2017/09/06 20:21:22 benno Exp $	*/
2
/*	$NetBSD: route.c,v 1.16 1996/04/15 18:27:05 cgd Exp $	*/
3
4
/*
5
 * Copyright (c) 1983, 1989, 1991, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/socket.h>
34
#include <sys/sysctl.h>
35
36
#include <net/if.h>
37
#include <net/if_dl.h>
38
#include <net/if_media.h>
39
#include <net/if_types.h>
40
#include <net/route.h>
41
#include <netinet/in.h>
42
#include <netmpls/mpls.h>
43
44
#ifdef BFD
45
#include <sys/time.h>
46
#include <net/bfd.h>
47
#endif
48
49
#include <arpa/inet.h>
50
#include <netdb.h>
51
52
#include <errno.h>
53
#include <fcntl.h>
54
#include <unistd.h>
55
#include <limits.h>
56
#include <stdio.h>
57
#include <ctype.h>
58
#include <stddef.h>
59
#include <stdlib.h>
60
#include <string.h>
61
#include <time.h>
62
#include <paths.h>
63
#include <err.h>
64
65
#include "keywords.h"
66
#include "show.h"
67
68
const struct if_status_description
69
			if_status_descriptions[] = LINK_STATE_DESCRIPTIONS;
70
71
union sockunion so_dst, so_gate, so_mask, so_ifa, so_ifp, so_label, so_src;
72
73
typedef union sockunion *sup;
74
pid_t	pid;
75
int	rtm_addrs, s;
76
int	forcehost, forcenet, Fflag, nflag, af, qflag, tflag, Tflag;
77
int	iflag, verbose, aflen = sizeof(struct sockaddr_in);
78
int	locking, lockrest, debugonly;
79
u_long	mpls_flags = MPLS_OP_LOCAL;
80
u_long	rtm_inits;
81
uid_t	uid;
82
u_int	tableid;
83
84
struct rt_metrics	rt_metrics;
85
86
int	 flushroutes(int, char **);
87
int	 newroute(int, char **);
88
int	 show(int, char *[]);
89
int	 keycmp(const void *, const void *);
90
int	 keyword(char *);
91
void	 monitor(int, char *[]);
92
int	 prefixlen(char *);
93
void	 sockaddr(char *, struct sockaddr *);
94
void	 sodump(sup, char *);
95
char	*priorityname(uint8_t);
96
uint8_t	 getpriority(char *);
97
void	 print_getmsg(struct rt_msghdr *, int);
98
#ifdef BFD
99
const char *bfd_state(unsigned int);
100
const char *bfd_diag(unsigned int);
101
const char *bfd_calc_uptime(time_t);
102
void	 print_bfdmsg(struct rt_msghdr *);
103
void	 print_sabfd(struct sockaddr_bfd *, int);
104
#endif
105
const char *get_linkstate(int, int);
106
void	 print_rtmsg(struct rt_msghdr *, int);
107
void	 pmsg_common(struct rt_msghdr *);
108
void	 pmsg_addrs(char *, int);
109
void	 bprintf(FILE *, int, char *);
110
void	 mask_addr(union sockunion *, union sockunion *, int);
111
int	 inet6_makenetandmask(struct sockaddr_in6 *, char *);
112
int	 getaddr(int, char *, struct hostent **);
113
void	 getmplslabel(char *, int);
114
int	 rtmsg(int, int, int, uint8_t);
115
__dead void usage(char *);
116
void	 set_metric(char *, int);
117
void	 inet_makenetandmask(u_int32_t, struct sockaddr_in *, int);
118
void	 interfaces(void);
119
void	 getlabel(char *);
120
int	 gettable(const char *);
121
int	 rdomain(int, char **);
122
void	 print_rtdns(struct sockaddr_rtdns *);
123
void	 print_rtstatic(struct sockaddr_rtstatic *);
124
void	 print_rtsearch(struct sockaddr_rtsearch *);
125
126
__dead void
127
usage(char *cp)
128
{
129
	extern char *__progname;
130
131
	if (cp)
132
		warnx("botched keyword: %s", cp);
133
	fprintf(stderr,
134
	    "usage: %s [-dnqtv] [-T tableid] command [[modifiers] args]\n",
135
	    __progname);
136
	fprintf(stderr,
137
	    "commands: add, change, delete, exec, flush, get, monitor, show\n");
138
	exit(1);
139
}
140
141
#define ROUNDUP(a) \
142
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
143
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
144
145
int
146
main(int argc, char **argv)
147
{
148
	int ch;
149
	int rval = 0;
150
	int kw;
151
	int Terr = 0;
152
153
2250
	if (argc < 2)
154
		usage(NULL);
155
156
1125
	tableid = getrtable();
157
6912
	while ((ch = getopt(argc, argv, "dnqtT:v")) != -1)
158

4653
		switch (ch) {
159
		case 'n':
160
			nflag = 1;
161
1125
			break;
162
		case 'q':
163
			qflag = 1;
164
			break;
165
		case 'v':
166
			verbose = 1;
167
			break;
168
		case 't':
169
			tflag = 1;
170
72
			break;
171
		case 'T':
172
1125
			Terr = gettable(optarg);
173
			Tflag = 1;
174
1125
			break;
175
		case 'd':
176
			debugonly = 1;
177
			break;
178
		default:
179
			usage(NULL);
180
			/* NOTREACHED */
181
		}
182
1125
	argc -= optind;
183
1125
	argv += optind;
184
185
1125
	pid = getpid();
186
1125
	uid = geteuid();
187
1125
	if (*argv == NULL)
188
		usage(NULL);
189
190
1125
	kw = keyword(*argv);
191
1125
	if (Tflag && Terr != 0 && kw != K_ADD) {
192
		errno = Terr;
193
		err(1, "routing table %d", tableid);
194
	}
195
1125
	if (kw == K_EXEC)
196
		exit(rdomain(argc - 1, argv + 1));
197
198
1125
	s = socket(PF_ROUTE, SOCK_RAW, 0);
199
1125
	if (s == -1)
200
		err(1, "socket");
201
1125
	if (kw == K_MONITOR) {
202
		unsigned int filter = 0;
203
		int af = 0;
204
205
		while (--argc > 0) {
206
			if (**(++argv)== '-')
207
				switch (keyword(*argv + 1)) {
208
				case K_INET:
209
					af = AF_INET;
210
					break;
211
				case K_INET6:
212
					af = AF_INET6;
213
					break;
214
				case K_IFACE:
215
				case K_INTERFACE:
216
					filter = ROUTE_FILTER(RTM_IFINFO) |
217
					    ROUTE_FILTER(RTM_IFANNOUNCE);
218
					break;
219
				default:
220
					usage(*argv);
221
					/* NOTREACHED */
222
				}
223
			else
224
				usage(*argv);
225
		}
226
		if (setsockopt(s, AF_ROUTE, ROUTE_MSGFILTER, &filter,
227
		    sizeof(filter)) == -1)
228
			err(1, "setsockopt(ROUTE_MSGFILTER)");
229
	}
230
	/* force socket onto table user requested */
231

2250
	if (Tflag == 1 && Terr == 0 &&
232
1125
	    setsockopt(s, AF_ROUTE, ROUTE_TABLEFILTER,
233
1125
	    &tableid, sizeof(tableid)) == -1)
234
		err(1, "setsockopt(ROUTE_TABLEFILTER)");
235
236
1125
	switch (kw) {
237
	case K_SHOW:
238
		uid = 0;
239
		exit(show(argc, argv));
240
		break;
241
	case K_FLUSH:
242
		exit(flushroutes(argc, argv));
243
		break;
244
	}
245
246
900
	if (pledge("stdio rpath dns flock cpath wpath", NULL) == -1)
247
		err(1, "pledge");
248
249

909
	switch (kw) {
250
	case K_GET:
251
9
		uid = 0;
252
		/* FALLTHROUGH */
253
	case K_CHANGE:
254
	case K_ADD:
255
	case K_DEL:
256
	case K_DELETE:
257
900
		rval = newroute(argc, argv);
258
900
		break;
259
	case K_MONITOR:
260
		monitor(argc, argv);
261
		break;
262
	default:
263
		usage(*argv);
264
		/* NOTREACHED */
265
	}
266
	exit(rval);
267
}
268
269
/*
270
 * Purge all entries in the routing tables not
271
 * associated with network interfaces.
272
 */
273
int
274
flushroutes(int argc, char **argv)
275
{
276
	size_t needed;
277
	int mib[7], rlen, seqno;
278
	char *buf = NULL, *next, *lim = NULL;
279
	struct rt_msghdr *rtm;
280
	struct sockaddr *sa;
281
	uint8_t prio = 0;
282
	unsigned int ifindex = 0;
283
284
	if (uid)
285
		errx(1, "must be root to alter routing table");
286
	shutdown(s, SHUT_RD); /* Don't want to read back our messages */
287
	while (--argc > 0) {
288
		if (**(++argv) == '-')
289
			switch (keyword(*argv + 1)) {
290
			case K_INET:
291
				af = AF_INET;
292
				break;
293
			case K_INET6:
294
				af = AF_INET6;
295
				break;
296
			case K_LINK:
297
				af = AF_LINK;
298
				break;
299
			case K_MPLS:
300
				af = AF_MPLS;
301
				break;
302
			case K_IFACE:
303
			case K_INTERFACE:
304
				if (!--argc)
305
					usage(1+*argv);
306
				ifindex = if_nametoindex(*++argv);
307
				if (ifindex == 0)
308
					errx(1, "no such interface %s", *argv);
309
				break;
310
			case K_PRIORITY:
311
				if (!--argc)
312
					usage(1+*argv);
313
				prio = getpriority(*++argv);
314
				break;
315
			default:
316
				usage(*argv);
317
				/* NOTREACHED */
318
			}
319
		else
320
			usage(*argv);
321
	}
322
	mib[0] = CTL_NET;
323
	mib[1] = PF_ROUTE;
324
	mib[2] = 0;		/* protocol */
325
	mib[3] = 0;		/* wildcard address family */
326
	mib[4] = NET_RT_DUMP;
327
	mib[5] = 0;		/* no flags */
328
	mib[6] = tableid;
329
	while (1) {
330
		if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1)
331
			err(1, "route-sysctl-estimate");
332
		if (needed == 0)
333
			break;
334
		if ((buf = realloc(buf, needed)) == NULL)
335
			err(1, "realloc");
336
		if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) {
337
			if (errno == ENOMEM)
338
				continue;
339
			err(1, "actual retrieval of routing table");
340
		}
341
		lim = buf + needed;
342
		break;
343
	}
344
345
	if (pledge("stdio rpath dns flock cpath wpath", NULL) == -1)
346
		err(1, "pledge");
347
348
	if (verbose) {
349
		printf("Examining routing table from sysctl\n");
350
		if (af)
351
			printf("(address family %s)\n", (*argv + 1));
352
	}
353
	if (buf == NULL)
354
		return (1);
355
356
	seqno = 0;
357
	for (next = buf; next < lim; next += rtm->rtm_msglen) {
358
		rtm = (struct rt_msghdr *)next;
359
		if (rtm->rtm_version != RTM_VERSION)
360
			continue;
361
		if (verbose)
362
			print_rtmsg(rtm, rtm->rtm_msglen);
363
		if ((rtm->rtm_flags & (RTF_GATEWAY|RTF_STATIC|RTF_LLINFO)) == 0)
364
			continue;
365
		if ((rtm->rtm_flags & (RTF_LOCAL|RTF_BROADCAST)) != 0)
366
			continue;
367
		sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
368
		if (af && sa->sa_family != af)
369
			continue;
370
		if (ifindex && rtm->rtm_index != ifindex)
371
			continue;
372
		if (prio && rtm->rtm_priority != prio)
373
			continue;
374
		if (sa->sa_family == AF_KEY)
375
			continue;  /* Don't flush SPD */
376
		if (debugonly)
377
			continue;
378
		rtm->rtm_type = RTM_DELETE;
379
		rtm->rtm_seq = seqno;
380
		rtm->rtm_tableid = tableid;
381
		rlen = write(s, next, rtm->rtm_msglen);
382
		if (rlen < (int)rtm->rtm_msglen) {
383
			warn("write to routing socket");
384
			printf("got only %d for rlen\n", rlen);
385
			break;
386
		}
387
		seqno++;
388
		if (qflag)
389
			continue;
390
		if (verbose)
391
			print_rtmsg(rtm, rlen);
392
		else {
393
			struct sockaddr	*mask, *rti_info[RTAX_MAX];
394
395
			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
396
397
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
398
399
			sa = rti_info[RTAX_DST];
400
			mask = rti_info[RTAX_NETMASK];
401
402
			p_sockaddr(sa, mask, rtm->rtm_flags, 20);
403
			p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST, 20);
404
			printf("done\n");
405
		}
406
	}
407
	free(buf);
408
	return (0);
409
}
410
411
void
412
set_metric(char *value, int key)
413
{
414
	long long relative_expire;
415
144
	const char *errstr;
416
	int flag = 0;
417
418


72
	switch (key) {
419
	case K_MTU:
420
63
		rt_metrics.rmx_mtu = strtonum(value, 0, UINT_MAX, &errstr);
421
63
		if (errstr)
422
			errx(1, "set_metric mtu: %s is %s", value, errstr);
423
		flag = RTV_MTU;
424
63
		break;
425
	case K_EXPIRE:
426
9
		relative_expire = strtonum(value, 0, INT_MAX, &errstr);
427
9
		if (errstr)
428
			errx(1, "set_metric expire: %s is %s", value, errstr);
429
27
		rt_metrics.rmx_expire = relative_expire ?
430
9
		    relative_expire + time(NULL) : 0;
431
		flag = RTV_EXPIRE;
432
9
		break;
433
	case K_HOPCOUNT:
434
	case K_RECVPIPE:
435
	case K_SENDPIPE:
436
	case K_SSTHRESH:
437
	case K_RTT:
438
	case K_RTTVAR:
439
		/* no longer used, only for compatibility */
440
		return;
441
	default:
442
		errx(1, "king bula sez: set_metric with invalid key");
443
	}
444
72
	rtm_inits |= flag;
445
72
	if (lockrest || locking)
446
9
		rt_metrics.rmx_locks |= flag;
447
72
	if (locking)
448
9
		locking = 0;
449
144
}
450
451
int
452
newroute(int argc, char **argv)
453
{
454
	char *cmd, *dest = "", *gateway = "", *error;
455
	int ishost = 0, ret = 0, attempts, oerrno, flags = RTF_STATIC;
456
	int fmask = 0;
457
	int key;
458
	uint8_t prio = 0;
459
1800
	struct hostent *hp = NULL;
460
461
900
	if (uid)
462
		errx(1, "must be root to alter routing table");
463
900
	cmd = argv[0];
464
900
	if (*cmd != 'g')
465
891
		shutdown(s, SHUT_RD); /* Don't want to read back our messages */
466
3573
	while (--argc > 0) {
467
2673
		if (**(++argv)== '-') {
468











1044
			switch (key = keyword(1 + *argv)) {
469
			case K_LINK:
470
				af = AF_LINK;
471
				aflen = sizeof(struct sockaddr_dl);
472
				break;
473
			case K_INET:
474
				af = AF_INET;
475
				aflen = sizeof(struct sockaddr_in);
476
				break;
477
			case K_INET6:
478
45
				af = AF_INET6;
479
45
				aflen = sizeof(struct sockaddr_in6);
480
45
				break;
481
			case K_SA:
482
				af = PF_ROUTE;
483
				aflen = sizeof(union sockunion);
484
				break;
485
			case K_MPLS:
486
				af = AF_MPLS;
487
				aflen = sizeof(struct sockaddr_mpls);
488
				fmask |= RTF_MPLS;
489
				break;
490
			case K_MPLSLABEL:
491
				if (!--argc)
492
					usage(1+*argv);
493
				if (af != AF_INET && af != AF_INET6)
494
					errx(1, "-mplslabel requires "
495
					    "-inet or -inet6");
496
				getmplslabel(*++argv, 0);
497
				mpls_flags = MPLS_OP_PUSH;
498
				flags |= RTF_MPLS;
499
				break;
500
			case K_IN:
501
				if (!--argc)
502
					usage(1+*argv);
503
				if (af != AF_MPLS)
504
					errx(1, "-in requires -mpls");
505
				getmplslabel(*++argv, 1);
506
				break;
507
			case K_OUT:
508
				if (!--argc)
509
					usage(1+*argv);
510
				if (af != AF_MPLS)
511
					errx(1, "-out requires -mpls");
512
				if (mpls_flags == MPLS_OP_LOCAL)
513
					errx(1, "-out requires -push, -pop, "
514
					    "-swap");
515
				getmplslabel(*++argv, 0);
516
				flags |= RTF_MPLS;
517
				break;
518
			case K_POP:
519
				if (af != AF_MPLS)
520
					errx(1, "-pop requires -mpls");
521
				mpls_flags = MPLS_OP_POP;
522
				break;
523
			case K_PUSH:
524
				if (af != AF_MPLS)
525
					errx(1, "-push requires -mpls");
526
				mpls_flags = MPLS_OP_PUSH;
527
				break;
528
			case K_SWAP:
529
				if (af != AF_MPLS)
530
					errx(1, "-swap requires -mpls");
531
				mpls_flags = MPLS_OP_SWAP;
532
				break;
533
			case K_IFACE:
534
			case K_INTERFACE:
535
				iflag++;
536
				break;
537
			case K_NOSTATIC:
538
				flags &= ~RTF_STATIC;
539
				break;
540
			case K_LLINFO:
541
				flags |= RTF_LLINFO;
542
				break;
543
			case K_LOCK:
544
9
				locking = 1;
545
9
				break;
546
			case K_LOCKREST:
547
				lockrest = 1;
548
				break;
549
			case K_HOST:
550
9
				forcehost++;
551
9
				break;
552
			case K_REJECT:
553
9
				flags |= RTF_REJECT;
554
9
				break;
555
			case K_BLACKHOLE:
556
				flags |= RTF_BLACKHOLE;
557
				break;
558
			case K_PROTO1:
559
				flags |= RTF_PROTO1;
560
				break;
561
			case K_PROTO2:
562
				flags |= RTF_PROTO2;
563
				break;
564
			case K_CLONING:
565
				flags |= RTF_CLONING;
566
				break;
567
			case K_STATIC:
568
				flags |= RTF_STATIC;
569
				break;
570
			case K_IFA:
571
9
				if (!--argc)
572
					usage(1+*argv);
573
9
				getaddr(RTA_IFA, *++argv, NULL);
574
9
				break;
575
			case K_IFP:
576
9
				if (!--argc)
577
					usage(1+*argv);
578
9
				getaddr(RTA_IFP, *++argv, NULL);
579
9
				break;
580
			case K_GATEWAY:
581
27
				if (!--argc)
582
					usage(1+*argv);
583
27
				getaddr(RTA_GATEWAY, *++argv, NULL);
584
27
				gateway = *argv;
585
27
				break;
586
			case K_DST:
587
36
				if (!--argc)
588
					usage(1+*argv);
589
36
				ishost = getaddr(RTA_DST, *++argv, &hp);
590
36
				dest = *argv;
591
36
				break;
592
			case K_LABEL:
593
				if (!--argc)
594
					usage(1+*argv);
595
				getlabel(*++argv);
596
				break;
597
			case K_NETMASK:
598
36
				if (!--argc)
599
					usage(1+*argv);
600
36
				getaddr(RTA_NETMASK, *++argv, NULL);
601
				/* FALLTHROUGH */
602
			case K_NET:
603
72
				forcenet++;
604
72
				break;
605
			case K_PREFIXLEN:
606
				if (!--argc)
607
					usage(1+*argv);
608
				ishost = prefixlen(*++argv);
609
				break;
610
			case K_MPATH:
611
387
				flags |= RTF_MPATH;
612
387
				break;
613
			case K_MTU:
614
			case K_HOPCOUNT:
615
			case K_EXPIRE:
616
			case K_RECVPIPE:
617
			case K_SENDPIPE:
618
			case K_SSTHRESH:
619
			case K_RTT:
620
			case K_RTTVAR:
621
72
				if (!--argc)
622
					usage(1+*argv);
623
72
				set_metric(*++argv, key);
624
72
				break;
625
			case K_PRIORITY:
626
324
				if (!--argc)
627
					usage(1+*argv);
628
324
				prio = getpriority(*++argv);
629
324
				break;
630
			case K_BFD:
631
				flags |= RTF_BFD;
632
				fmask |= RTF_BFD;
633
				break;
634
			case K_NOBFD:
635
				flags &= ~RTF_BFD;
636
				fmask |= RTF_BFD;
637
				break;
638
			default:
639
				usage(1+*argv);
640
				/* NOTREACHED */
641
			}
642
		} else {
643
1665
			if ((rtm_addrs & RTA_DST) == 0) {
644
				dest = *argv;
645
864
				ishost = getaddr(RTA_DST, *argv, &hp);
646
1665
			} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
647
				gateway = *argv;
648
801
				getaddr(RTA_GATEWAY, *argv, &hp);
649
			} else
650
				usage(NULL);
651
		}
652
	}
653
900
	if (forcehost)
654
9
		ishost = 1;
655
900
	if (forcenet)
656
63
		ishost = 0;
657

963
	if (forcenet && !(rtm_addrs & RTA_NETMASK))
658
		errx(1, "netmask missing");
659
900
	flags |= RTF_UP;
660
900
	if (ishost)
661
54
		flags |= RTF_HOST;
662
900
	if (iflag == 0)
663
900
		flags |= RTF_GATEWAY;
664
900
	for (attempts = 1; ; attempts++) {
665
900
		errno = 0;
666
900
		if ((ret = rtmsg(*cmd, flags, fmask, prio)) == 0)
667
			break;
668

243
		if (errno != ENETUNREACH && errno != ESRCH)
669
			break;
670

108
		if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
671
			hp->h_addr_list++;
672
			memcpy(&so_gate.sin.sin_addr, hp->h_addr_list[0],
673
			    hp->h_length);
674
		} else
675
			break;
676
	}
677
900
	if (*cmd == 'g') {
678
9
		if (ret != 0 && qflag == 0)
679
			warn("writing to routing socket");
680
		exit(0);
681
	}
682
891
	oerrno = errno;
683
891
	if (!qflag) {
684
882
		printf("%s %s %s", cmd, ishost ? "host" : "net", dest);
685
882
		if (*gateway) {
686
819
			printf(": gateway %s", gateway);
687
819
			if (attempts > 1 && ret == 0 && af == AF_INET)
688
			    printf(" (%s)", inet_ntoa(so_gate.sin.sin_addr));
689
		}
690
882
		if (ret == 0)
691
801
			printf("\n");
692
882
		if (ret != 0) {
693

81
			switch (oerrno) {
694
			case ESRCH:
695
				error = "not in table";
696
27
				break;
697
			case EBUSY:
698
				error = "entry in use";
699
				break;
700
			case ENOBUFS:
701
				error = "routing table overflow";
702
				break;
703
			default:
704
54
				error = strerror(oerrno);
705
54
				break;
706
			}
707
81
			printf(": %s\n", error);
708
81
		}
709
	}
710
1782
	return (ret != 0);
711
891
}
712
713
int
714
show(int argc, char *argv[])
715
{
716
	int		 af = 0;
717
	char		 prio = 0;
718
719
927
	while (--argc > 0) {
720
252
		if (**(++argv)== '-')
721


252
			switch (keyword(*argv + 1)) {
722
			case K_INET:
723
				af = AF_INET;
724
207
				break;
725
			case K_INET6:
726
				af = AF_INET6;
727
18
				break;
728
			case K_LINK:
729
				af = AF_LINK;
730
				break;
731
			case K_MPLS:
732
				af = AF_MPLS;
733
				break;
734
			case K_GATEWAY:
735
27
				Fflag = 1;
736
27
				break;
737
			case K_LABEL:
738
				if (!--argc)
739
					usage(1+*argv);
740
				getlabel(*++argv);
741
				break;
742
			case K_PRIORITY:
743
				if (!--argc)
744
					usage(1+*argv);
745
				prio = getpriority(*++argv);
746
				break;
747
			default:
748
				usage(*argv);
749
				/* NOTREACHED */
750
			}
751
		else
752
			usage(*argv);
753
	}
754
755
225
	p_rttables(af, tableid, Tflag, prio);
756
225
	return (0);
757
}
758
759
void
760
inet_makenetandmask(u_int32_t net, struct sockaddr_in *sin, int bits)
761
{
762
	u_int32_t addr, mask = 0;
763
	char *cp;
764
765
1548
	rtm_addrs |= RTA_NETMASK;
766
774
	if (net == 0 && bits == 0)
767
		mask = addr = 0;
768
774
	else if (bits) {
769
		addr = net;
770
774
		mask = 0xffffffff << (32 - bits);
771
774
	} else if (net < 128) {
772
		addr = net << IN_CLASSA_NSHIFT;
773
		mask = IN_CLASSA_NET;
774
	} else if (net < 65536) {
775
		addr = net << IN_CLASSB_NSHIFT;
776
		mask = IN_CLASSB_NET;
777
	} else if (net < 16777216L) {
778
		addr = net << IN_CLASSC_NSHIFT;
779
		mask = IN_CLASSC_NET;
780
	} else {
781
		addr = net;
782
		if ((addr & IN_CLASSA_HOST) == 0)
783
			mask = IN_CLASSA_NET;
784
		else if ((addr & IN_CLASSB_HOST) == 0)
785
			mask = IN_CLASSB_NET;
786
		else if ((addr & IN_CLASSC_HOST) == 0)
787
			mask = IN_CLASSC_NET;
788
		else
789
			mask = 0xffffffff;
790
	}
791
774
	addr &= mask;
792
774
	sin->sin_addr.s_addr = htonl(addr);
793
	sin = &so_mask.sin;
794
774
	sin->sin_addr.s_addr = htonl(mask);
795
774
	sin->sin_len = 0;
796
774
	sin->sin_family = 0;
797
	cp = (char *)(&sin->sin_addr + 1);
798

3150
	while (*--cp == '\0' && cp > (char *)sin)
799
		continue;
800
774
	sin->sin_len = 1 + cp - (char *)sin;
801
774
}
802
803
/*
804
 * XXX the function may need more improvement...
805
 */
806
int
807
inet6_makenetandmask(struct sockaddr_in6 *sin6, char *plen)
808
{
809
126
	struct in6_addr in6;
810
63
	const char *errstr;
811
	int i, len, q, r;
812
813
63
	if (NULL==plen) {
814


36
		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
815
		    sin6->sin6_scope_id == 0) {
816
			plen = "0";
817
36
		} else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
818
			/* aggregatable global unicast - RFC2374 */
819
			memset(&in6, 0, sizeof(in6));
820
			if (!memcmp(&sin6->sin6_addr.s6_addr[8],
821
			    &in6.s6_addr[8], 8))
822
				plen = "64";
823
		}
824
	}
825
826

90
	if (!plen || strcmp(plen, "128") == 0)
827
36
		return (1);
828
	else {
829
27
		rtm_addrs |= RTA_NETMASK;
830
27
		prefixlen(plen);
831
832
27
		len = strtonum(plen, 0, 128, &errstr);
833
27
		if (errstr)
834
			errx(1, "prefixlen %s is %s", plen, errstr);
835
836
27
		q = (128-len) >> 3;
837
27
		r = (128-len) & 7;
838
		i = 15;
839
840
486
		while (q-- > 0)
841
216
			sin6->sin6_addr.s6_addr[i--] = 0;
842
27
		if (r > 0)
843
			sin6->sin6_addr.s6_addr[i] &= 0xff << r;
844
845
27
		return (0);
846
	}
847
63
}
848
849
/*
850
 * Interpret an argument as a network address of some kind,
851
 * returning 1 if a host address, 0 if a network address.
852
 */
853
int
854
getaddr(int which, char *s, struct hostent **hpp)
855
{
856
	sup su = NULL;
857
	struct hostent *hp;
858
	struct netent *np;
859
	int afamily, bits;
860
861
3582
	if (af == 0) {
862
855
		if (strchr(s, ':') != NULL) {
863
			af = AF_INET6;
864
			aflen = sizeof(struct sockaddr_in6);
865
		} else {
866
			af = AF_INET;
867
			aflen = sizeof(struct sockaddr_in);
868
		}
869
855
	}
870
1791
	afamily = af;	/* local copy of af so we can change it */
871
872
1791
	rtm_addrs |= which;
873

1791
	switch (which) {
874
	case RTA_DST:
875
		su = &so_dst;
876
900
		break;
877
	case RTA_GATEWAY:
878
		su = &so_gate;
879
828
		break;
880
	case RTA_NETMASK:
881
		su = &so_mask;
882
45
		break;
883
	case RTA_IFP:
884
		su = &so_ifp;
885
		afamily = AF_LINK;
886
9
		break;
887
	case RTA_IFA:
888
		su = &so_ifa;
889
9
		break;
890
	default:
891
		errx(1, "internal error");
892
		/* NOTREACHED */
893
	}
894
1791
	su->sa.sa_len = aflen;
895
1791
	su->sa.sa_family = afamily;
896
897
1791
	if (strcmp(s, "default") == 0) {
898
36
		switch (which) {
899
		case RTA_DST:
900
9
			forcenet++;
901
9
			getaddr(RTA_NETMASK, s, NULL);
902
9
			break;
903
		case RTA_NETMASK:
904
9
			su->sa.sa_len = 0;
905
9
		}
906
18
		return (0);
907
	}
908
909

1773
	switch (afamily) {
910
	case AF_INET6:
911
	    {
912
99
		struct addrinfo hints, *res;
913
99
		char            buf[
914
		   sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")
915
		];
916
		char           *sep;
917
918
99
		if (strlcpy(buf, s, sizeof buf) >= sizeof buf) {
919
			errx(1, "%s: bad value", s);
920
		}
921
922
99
		sep = strchr(buf, '/');
923
99
		if (sep != NULL)
924
27
			*sep++ = '\0';
925
99
		memset(&hints, 0, sizeof(hints));
926
99
		hints.ai_family = afamily;	/*AF_INET6*/
927
99
		hints.ai_flags = AI_NUMERICHOST;
928
99
		hints.ai_socktype = SOCK_DGRAM;		/*dummy*/
929
99
		if (getaddrinfo(buf, "0", &hints, &res) != 0) {
930
			hints.ai_flags = 0;
931
			if (getaddrinfo(buf, "0", &hints, &res) != 0)
932
				errx(1, "%s: bad value", s);
933
		}
934
99
		if (sizeof(su->sin6) != res->ai_addrlen)
935
			errx(1, "%s: bad value", s);
936
99
		if (res->ai_next)
937
			errx(1, "%s: resolved to multiple values", s);
938
99
		memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
939
99
		freeaddrinfo(res);
940

99
		if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
941

99
		     IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr) ||
942

99
		     IN6_IS_ADDR_MC_INTFACELOCAL(&su->sin6.sin6_addr)) &&
943
		    su->sin6.sin6_scope_id) {
944
			*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
945
				htons(su->sin6.sin6_scope_id);
946
			su->sin6.sin6_scope_id = 0;
947
		}
948
99
		if (hints.ai_flags == AI_NUMERICHOST) {
949
99
			if (which == RTA_DST)
950
63
				return (inet6_makenetandmask(&su->sin6, sep));
951
36
			return (0);
952
		} else
953
			return (1);
954
99
	    }
955
956
	case AF_LINK:
957
9
		su->sdl.sdl_index = if_nametoindex(s);
958
9
		memset(&su->sdl.sdl_data, 0, sizeof(su->sdl.sdl_data));
959
9
		return (1);
960
	case AF_MPLS:
961
		errx(1, "mpls labels require -in or -out switch");
962
	case PF_ROUTE:
963
		su->sa.sa_len = sizeof(*su);
964
		sockaddr(s, &su->sa);
965
		return (1);
966
967
	case AF_INET:
968
1665
		if (hpp != NULL)
969
1593
			*hpp = NULL;
970
1665
		if (which == RTA_DST && !forcehost) {
971
828
			bits = inet_net_pton(AF_INET, s, &su->sin.sin_addr,
972
			    sizeof(su->sin.sin_addr));
973
828
			if (bits == 32)
974
54
				return (1);
975
774
			if (bits >= 0) {
976
774
				inet_makenetandmask(ntohl(
977
				    su->sin.sin_addr.s_addr),
978
				    &su->sin, bits);
979
774
				return (0);
980
			}
981
			np = getnetbyname(s);
982
			if (np != NULL && np->n_net != 0) {
983
				inet_makenetandmask(np->n_net, &su->sin, 0);
984
				return (0);
985
			}
986
			if (forcenet)
987
				errx(1, "%s: not a network", s);
988
		}
989
837
		if (inet_pton(AF_INET, s, &su->sin.sin_addr) == 1)
990
837
			return (1);
991
		hp = gethostbyname(s);
992
		if (hp != NULL) {
993
			if (hpp != NULL)
994
				*hpp = hp;
995
			su->sin.sin_addr = *(struct in_addr *)hp->h_addr;
996
			return (1);
997
		}
998
		errx(1, "%s: bad address", s);
999
		/* NOTREACHED */
1000
1001
	default:
1002
		errx(1, "%d: bad address family", afamily);
1003
		/* NOTREACHED */
1004
	}
1005
1791
}
1006
1007
void
1008
getmplslabel(char *s, int in)
1009
{
1010
	sup su = NULL;
1011
	const char *errstr;
1012
	u_int32_t label;
1013
1014
	label = strtonum(s, 0, 0x000fffff, &errstr);
1015
	if (errstr)
1016
		errx(1, "bad label: %s is %s", s, errstr);
1017
	if (in) {
1018
		rtm_addrs |= RTA_DST;
1019
		su = &so_dst;
1020
		su->smpls.smpls_label = htonl(label << MPLS_LABEL_OFFSET);
1021
	} else {
1022
		rtm_addrs |= RTA_SRC;
1023
		su = &so_src;
1024
		su->smpls.smpls_label = htonl(label << MPLS_LABEL_OFFSET);
1025
	}
1026
1027
	su->sa.sa_len = sizeof(struct sockaddr_mpls);
1028
	su->sa.sa_family = AF_MPLS;
1029
}
1030
1031
int
1032
prefixlen(char *s)
1033
{
1034
54
	const char *errstr;
1035
	int len, q, r;
1036
	int max;
1037
1038
27
	switch (af) {
1039
	case AF_INET:
1040
		max = sizeof(struct in_addr) * 8;
1041
		break;
1042
	case AF_INET6:
1043
		max = sizeof(struct in6_addr) * 8;
1044
27
		break;
1045
	default:
1046
		errx(1, "prefixlen is not supported with af %d", af);
1047
		/* NOTREACHED */
1048
	}
1049
1050
27
	rtm_addrs |= RTA_NETMASK;
1051
27
	len = strtonum(s, 0, max, &errstr);
1052
27
	if (errstr)
1053
		errx(1, "prefixlen %s is %s", s, errstr);
1054
1055
54
	q = len >> 3;
1056
54
	r = len & 7;
1057
54
	switch (af) {
1058
	case AF_INET:
1059
		memset(&so_mask, 0, sizeof(so_mask));
1060
		so_mask.sin.sin_family = AF_INET;
1061
		so_mask.sin.sin_len = sizeof(struct sockaddr_in);
1062
		so_mask.sin.sin_addr.s_addr = htonl(0xffffffff << (32 - len));
1063
		break;
1064
	case AF_INET6:
1065
27
		so_mask.sin6.sin6_family = AF_INET6;
1066
27
		so_mask.sin6.sin6_len = sizeof(struct sockaddr_in6);
1067
27
		memset((void *)&so_mask.sin6.sin6_addr, 0,
1068
			sizeof(so_mask.sin6.sin6_addr));
1069
27
		if (q > 0)
1070
27
			memset((void *)&so_mask.sin6.sin6_addr, 0xff, q);
1071
27
		if (r > 0)
1072
			*((u_char *)&so_mask.sin6.sin6_addr + q) =
1073
			    (0xff00 >> r) & 0xff;
1074
		break;
1075
	}
1076
54
	return (len == max);
1077
27
}
1078
1079
void
1080
interfaces(void)
1081
{
1082
	size_t needed;
1083
	int mib[6];
1084
	char *buf = NULL, *lim, *next;
1085
	struct rt_msghdr *rtm;
1086
1087
	mib[0] = CTL_NET;
1088
	mib[1] = PF_ROUTE;
1089
	mib[2] = 0;		/* protocol */
1090
	mib[3] = 0;		/* wildcard address family */
1091
	mib[4] = NET_RT_IFLIST;
1092
	mib[5] = 0;		/* no flags */
1093
	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1094
		err(1, "route-sysctl-estimate");
1095
	if (needed) {
1096
		if ((buf = malloc(needed)) == NULL)
1097
			err(1, "malloc");
1098
		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1099
			err(1, "actual retrieval of interface table");
1100
		lim = buf + needed;
1101
		for (next = buf; next < lim; next += rtm->rtm_msglen) {
1102
			rtm = (struct rt_msghdr *)next;
1103
			print_rtmsg(rtm, rtm->rtm_msglen);
1104
		}
1105
		free(buf);
1106
	}
1107
}
1108
1109
void
1110
monitor(int argc, char *argv[])
1111
{
1112
	int n;
1113
	char msg[2048];
1114
	time_t now;
1115
1116
	if (pledge("stdio rpath dns flock cpath wpath", NULL) == -1)
1117
		err(1, "pledge");
1118
1119
	verbose = 1;
1120
	if (debugonly) {
1121
		interfaces();
1122
		exit(0);
1123
	}
1124
	for (;;) {
1125
		if ((n = read(s, msg, sizeof(msg))) == -1) {
1126
			if (errno == EINTR)
1127
				continue;
1128
			err(1, "read");
1129
		}
1130
		now = time(NULL);
1131
		printf("got message of size %d on %s", n, ctime(&now));
1132
		print_rtmsg((struct rt_msghdr *)msg, n);
1133
	}
1134
}
1135
1136
struct {
1137
	struct rt_msghdr	m_rtm;
1138
	char			m_space[512];
1139
} m_rtmsg;
1140
1141
int
1142
rtmsg(int cmd, int flags, int fmask, uint8_t prio)
1143
{
1144
	static int seq;
1145
	char *cp = m_rtmsg.m_space;
1146
	int l;
1147
1148
#define NEXTADDR(w, u)				\
1149
	if (rtm_addrs & (w)) {			\
1150
		l = ROUNDUP(u.sa.sa_len);	\
1151
		memcpy(cp, &(u), l);		\
1152
		cp += l;			\
1153
		if (verbose)			\
1154
			sodump(&(u), #u);	\
1155
	}
1156
1157
1800
	errno = 0;
1158
900
	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
1159
900
	if (cmd == 'a')
1160
693
		cmd = RTM_ADD;
1161
207
	else if (cmd == 'c')
1162
108
		cmd = RTM_CHANGE;
1163
99
	else if (cmd == 'g') {
1164
		cmd = RTM_GET;
1165
9
		if (so_ifp.sa.sa_family == 0) {
1166
9
			so_ifp.sa.sa_family = AF_LINK;
1167
9
			so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1168
9
			rtm_addrs |= RTA_IFP;
1169
9
		}
1170
	} else
1171
		cmd = RTM_DELETE;
1172
#define rtm m_rtmsg.m_rtm
1173
900
	rtm.rtm_type = cmd;
1174
900
	rtm.rtm_flags = flags;
1175
900
	rtm.rtm_fmask = fmask;
1176
900
	rtm.rtm_version = RTM_VERSION;
1177
900
	rtm.rtm_seq = ++seq;
1178
900
	rtm.rtm_addrs = rtm_addrs;
1179
900
	rtm.rtm_rmx = rt_metrics;
1180
900
	rtm.rtm_inits = rtm_inits;
1181
900
	rtm.rtm_tableid = tableid;
1182
900
	rtm.rtm_priority = prio;
1183
900
	rtm.rtm_mpls = mpls_flags;
1184
900
	rtm.rtm_hdrlen = sizeof(rtm);
1185
1186
900
	if (rtm_addrs & RTA_NETMASK)
1187
846
		mask_addr(&so_dst, &so_mask, RTA_DST);
1188

3600
	NEXTADDR(RTA_DST, so_dst);
1189

3384
	NEXTADDR(RTA_GATEWAY, so_gate);
1190

3429
	NEXTADDR(RTA_NETMASK, so_mask);
1191

954
	NEXTADDR(RTA_IFP, so_ifp);
1192

927
	NEXTADDR(RTA_IFA, so_ifa);
1193

900
	NEXTADDR(RTA_LABEL, so_label);
1194

900
	NEXTADDR(RTA_SRC, so_src);
1195
900
	rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1196
900
	if (verbose)
1197
		print_rtmsg(&rtm, l);
1198
900
	if (debugonly)
1199
		return (0);
1200
900
	if (write(s, &m_rtmsg, l) != l) {
1201
81
		return (-1);
1202
	}
1203
819
	if (cmd == RTM_GET) {
1204
		do {
1205
9
			l = read(s, &m_rtmsg, sizeof(m_rtmsg));
1206

27
		} while (l > 0 && (rtm.rtm_version != RTM_VERSION ||
1207
18
		    rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1208
9
		if (l == -1)
1209
			warn("read from routing socket");
1210
		else
1211
9
			print_getmsg(&rtm, l);
1212
	}
1213
#undef rtm
1214
819
	return (0);
1215
900
}
1216
1217
void
1218
mask_addr(union sockunion *addr, union sockunion *mask, int which)
1219
{
1220
1692
	int olen = mask->sa.sa_len;
1221
846
	char *cp1 = olen + (char *)mask, *cp2;
1222
1223
2340
	for (mask->sa.sa_len = 0; cp1 > (char *)mask; )
1224
1485
		if (*--cp1 != '\0') {
1225
837
			mask->sa.sa_len = 1 + cp1 - (char *)mask;
1226
837
			break;
1227
		}
1228
846
	if ((rtm_addrs & which) == 0)
1229
		return;
1230

846
	switch (addr->sa.sa_family) {
1231
	case AF_INET:
1232
	case AF_INET6:
1233
	case 0:
1234
846
		return;
1235
	}
1236
	cp1 = mask->sa.sa_len + 1 + (char *)addr;
1237
	cp2 = addr->sa.sa_len + 1 + (char *)addr;
1238
	while (cp2 > cp1)
1239
		*--cp2 = '\0';
1240
	cp2 = mask->sa.sa_len + 1 + (char *)mask;
1241
	while (cp1 > addr->sa.sa_data)
1242
		*--cp1 &= *--cp2;
1243
846
}
1244
1245
char *msgtypes[] = {
1246
	"",
1247
	"RTM_ADD: Add Route",
1248
	"RTM_DELETE: Delete Route",
1249
	"RTM_CHANGE: Change Metrics or flags",
1250
	"RTM_GET: Report Metrics",
1251
	"RTM_LOSING: Kernel Suspects Partitioning",
1252
	"RTM_REDIRECT: Told to use different route",
1253
	"RTM_MISS: Lookup failed on this address",
1254
	"RTM_LOCK: fix specified metrics",
1255
	"RTM_OLDADD: caused by SIOCADDRT",
1256
	"RTM_OLDDEL: caused by SIOCDELRT",
1257
	"RTM_RESOLVE: Route created by cloning",
1258
	"RTM_NEWADDR: address being added to iface",
1259
	"RTM_DELADDR: address being removed from iface",
1260
	"RTM_IFINFO: iface status change",
1261
	"RTM_IFANNOUNCE: iface arrival/departure",
1262
	"RTM_DESYNC: route socket overflow",
1263
	"RTM_INVALIDATE: invalidate cache of L2 route",
1264
	"RTM_BFD: bidirectional forwarding detection",
1265
	"RTM_PROPOSAL: config proposal"
1266
};
1267
1268
char metricnames[] =
1269
"\011priority\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount\1mtu";
1270
char routeflags[] =
1271
"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010XMASK_PRESENT"
1272
"\011CLONING\012MULTICAST\013LLINFO\014STATIC\015BLACKHOLE\016PROTO3\017PROTO2"
1273
"\020PROTO1\021CLONED\022CACHED\023MPATH\025MPLS\026LOCAL\027BROADCAST"
1274
"\030CONNECTED\031BFD";
1275
char ifnetflags[] =
1276
"\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6NOTRAILERS\7RUNNING\010NOARP\011PPROMISC"
1277
"\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1\017LINK2\020MULTICAST";
1278
char addrnames[] =
1279
"\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD\011SRC\012SRCMASK\013LABEL\014BFD\015DNS\016STATIC\017SEARCH";
1280
1281
const char *
1282
get_linkstate(int mt, int link_state)
1283
{
1284
	const struct if_status_description *p;
1285
	static char buf[8];
1286
1287
	for (p = if_status_descriptions; p->ifs_string != NULL; p++) {
1288
		if (LINK_STATE_DESC_MATCH(p, mt, link_state))
1289
			return (p->ifs_string);
1290
	}
1291
	snprintf(buf, sizeof(buf), "[#%d]", link_state);
1292
	return buf;
1293
}
1294
1295
void
1296
print_rtmsg(struct rt_msghdr *rtm, int msglen)
1297
{
1298
	long long relative_expire;
1299
	struct if_msghdr *ifm;
1300
	struct ifa_msghdr *ifam;
1301
	struct if_announcemsghdr *ifan;
1302
	char ifname[IF_NAMESIZE];
1303
1304
	if (verbose == 0)
1305
		return;
1306
	if (rtm->rtm_version != RTM_VERSION) {
1307
		warnx("routing message version %d not understood",
1308
		    rtm->rtm_version);
1309
		return;
1310
	}
1311
	if (rtm->rtm_type > 0 &&
1312
	    rtm->rtm_type < sizeof(msgtypes)/sizeof(msgtypes[0]))
1313
		printf("%s", msgtypes[rtm->rtm_type]);
1314
	else
1315
		printf("[rtm_type %d out of range]", rtm->rtm_type);
1316
1317
	printf(": len %d", rtm->rtm_msglen);
1318
	switch (rtm->rtm_type) {
1319
	case RTM_DESYNC:
1320
		printf("\n");
1321
		break;
1322
	case RTM_IFINFO:
1323
		ifm = (struct if_msghdr *)rtm;
1324
		(void) printf(", if# %d, ", ifm->ifm_index);
1325
		if (if_indextoname(ifm->ifm_index, ifname) != NULL)
1326
			printf("name: %s, ", ifname);
1327
		printf("link: %s, mtu: %u, flags:",
1328
		    get_linkstate(ifm->ifm_data.ifi_type,
1329
		        ifm->ifm_data.ifi_link_state),
1330
		    ifm->ifm_data.ifi_mtu);
1331
		bprintf(stdout, ifm->ifm_flags, ifnetflags);
1332
		pmsg_addrs((char *)ifm + ifm->ifm_hdrlen, ifm->ifm_addrs);
1333
		break;
1334
	case RTM_NEWADDR:
1335
	case RTM_DELADDR:
1336
		ifam = (struct ifa_msghdr *)rtm;
1337
		printf(", metric %d, flags:", ifam->ifam_metric);
1338
		bprintf(stdout, ifam->ifam_flags, routeflags);
1339
		pmsg_addrs((char *)ifam + ifam->ifam_hdrlen, ifam->ifam_addrs);
1340
		break;
1341
	case RTM_IFANNOUNCE:
1342
		ifan = (struct if_announcemsghdr *)rtm;
1343
		printf(", if# %d, name %s, what: ",
1344
		    ifan->ifan_index, ifan->ifan_name);
1345
		switch (ifan->ifan_what) {
1346
		case IFAN_ARRIVAL:
1347
			printf("arrival");
1348
			break;
1349
		case IFAN_DEPARTURE:
1350
			printf("departure");
1351
			break;
1352
		default:
1353
			printf("#%d", ifan->ifan_what);
1354
			break;
1355
		}
1356
		printf("\n");
1357
		break;
1358
#ifdef BFD
1359
	case RTM_BFD:
1360
		print_bfdmsg(rtm);
1361
		break;
1362
#endif
1363
	case RTM_PROPOSAL:
1364
		printf(", source ");
1365
		switch (rtm->rtm_priority) {
1366
		case RTP_PROPOSAL_STATIC:
1367
			printf("static");
1368
			break;
1369
		case RTP_PROPOSAL_DHCLIENT:
1370
			printf("dhcp");
1371
			break;
1372
		case RTP_PROPOSAL_SLAAC:
1373
			printf("slaac");
1374
			break;
1375
		default:
1376
			printf("unknown");
1377
			break;
1378
		}
1379
		printf(" table %u, ifidx %u, ",
1380
		    rtm->rtm_tableid, rtm->rtm_index);
1381
		printf("pid: %ld, seq %d, errno %d\nflags:",
1382
		    (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1383
		bprintf(stdout, rtm->rtm_flags, routeflags);
1384
		printf("\nfmask:");
1385
		bprintf(stdout, rtm->rtm_fmask, routeflags);
1386
		if (verbose) {
1387
#define lock(f)	((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1388
			relative_expire = rtm->rtm_rmx.rmx_expire ?
1389
			    rtm->rtm_rmx.rmx_expire - time(NULL) : 0;
1390
			printf("\nuse: %8llu   mtu: %8u%c   expire: %8lld%c",
1391
			    rtm->rtm_rmx.rmx_pksent,
1392
			    rtm->rtm_rmx.rmx_mtu, lock(MTU),
1393
			    relative_expire, lock(EXPIRE));
1394
#undef lock
1395
		}
1396
		printf("\nlocks: ");
1397
		bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1398
		printf(" inits: ");
1399
		bprintf(stdout, rtm->rtm_inits, metricnames);
1400
		pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen),
1401
		   rtm->rtm_addrs & ~(RTA_STATIC | RTA_SEARCH | RTA_DNS));
1402
		printf("Static Routes:\n");
1403
		if (rtm->rtm_addrs & RTA_STATIC) {
1404
			char *next = (char *)rtm + rtm->rtm_hdrlen;
1405
			struct sockaddr	*sa, *rti_info[RTAX_MAX];
1406
			struct sockaddr_rtstatic *rtstatic;
1407
			sa = (struct sockaddr *)next;
1408
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1409
			rtstatic = (struct sockaddr_rtstatic *)
1410
			    rti_info[RTAX_STATIC];
1411
			if (rtstatic != NULL) {
1412
				printf(" ");
1413
				print_rtstatic(rtstatic);
1414
			}
1415
		}
1416
		printf("Domain search:\n");
1417
		if (rtm->rtm_addrs & RTA_SEARCH) {
1418
			char *next = (char *)rtm + rtm->rtm_hdrlen;
1419
			struct sockaddr	*sa, *rti_info[RTAX_MAX];
1420
			struct sockaddr_rtsearch *rtsearch;
1421
			sa = (struct sockaddr *)next;
1422
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1423
			rtsearch = (struct sockaddr_rtsearch *)
1424
			    rti_info[RTAX_SEARCH];
1425
			if (rtsearch != NULL) {
1426
				printf(" ");
1427
				print_rtsearch(rtsearch);
1428
			}
1429
		}
1430
		printf("Domain Name Servers:\n");
1431
		if (rtm->rtm_addrs & RTA_DNS) {
1432
			char *next = (char *)rtm + rtm->rtm_hdrlen;
1433
			struct sockaddr	*sa, *rti_info[RTAX_MAX];
1434
			struct sockaddr_rtdns *rtdns;
1435
			sa = (struct sockaddr *)next;
1436
			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1437
			rtdns = (struct sockaddr_rtdns *)rti_info[RTAX_DNS];
1438
			if (rtdns != NULL) {
1439
				printf(" ");
1440
				print_rtdns(rtdns);
1441
			}
1442
		}
1443
		break;
1444
	default:
1445
		printf(", priority %d, table %u, ifidx %u, ",
1446
		    rtm->rtm_priority, rtm->rtm_tableid, rtm->rtm_index);
1447
		printf("pid: %ld, seq %d, errno %d\nflags:",
1448
		    (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1449
		bprintf(stdout, rtm->rtm_flags, routeflags);
1450
		printf("\nfmask:");
1451
		bprintf(stdout, rtm->rtm_fmask, routeflags);
1452
		if (verbose) {
1453
#define lock(f)	((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1454
			relative_expire = rtm->rtm_rmx.rmx_expire ?
1455
			    rtm->rtm_rmx.rmx_expire - time(NULL) : 0;
1456
			printf("\nuse: %8llu   mtu: %8u%c   expire: %8lld%c",
1457
			    rtm->rtm_rmx.rmx_pksent,
1458
			    rtm->rtm_rmx.rmx_mtu, lock(MTU),
1459
			    relative_expire, lock(EXPIRE));
1460
#undef lock
1461
		}
1462
		pmsg_common(rtm);
1463
	}
1464
}
1465
1466
char *
1467
priorityname(uint8_t prio)
1468
{
1469


18
	switch (prio) {
1470
	case RTP_NONE:
1471
		return ("none");
1472
	case RTP_LOCAL:
1473
		return ("local");
1474
	case RTP_CONNECTED:
1475
		return ("connected");
1476
	case RTP_STATIC:
1477
9
		return ("static");
1478
	case RTP_OSPF:
1479
		return ("ospf");
1480
	case RTP_ISIS:
1481
		return ("is-is");
1482
	case RTP_RIP:
1483
		return ("rip");
1484
	case RTP_BGP:
1485
		return ("bgp");
1486
	case RTP_DEFAULT:
1487
		return ("default");
1488
	default:
1489
		return ("");
1490
	}
1491
9
}
1492
1493
uint8_t
1494
getpriority(char *priostr)
1495
{
1496
648
	const char *errstr;
1497
	uint8_t prio;
1498
1499

324
	switch (keyword(priostr)) {
1500
	case K_LOCAL:
1501
		prio = RTP_LOCAL;
1502
		break;
1503
	case K_CONNECTED:
1504
		prio = RTP_CONNECTED;
1505
		break;
1506
	case K_STATIC:
1507
		prio = RTP_STATIC;
1508
		break;
1509
	case K_OSPF:
1510
		prio = RTP_OSPF;
1511
		break;
1512
	case K_RIP:
1513
		prio = RTP_RIP;
1514
		break;
1515
	case K_BGP:
1516
		prio = RTP_BGP;
1517
		break;
1518
	default:
1519
324
		prio = strtonum(priostr, -RTP_MAX, RTP_MAX, &errstr);
1520
324
		if (errstr)
1521
			errx(1, "priority is %s: %s", errstr, priostr);
1522
	}
1523
1524
324
	return (prio);
1525
324
}
1526
1527
void
1528
print_getmsg(struct rt_msghdr *rtm, int msglen)
1529
{
1530
	long long relative_expire;
1531
	struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL, *ifa = NULL;
1532
	struct sockaddr_dl *ifp = NULL;
1533
	struct sockaddr_rtlabel *sa_rl = NULL;
1534
#ifdef BFD
1535
	struct sockaddr_bfd *sa_bfd = NULL;
1536
#endif
1537
	struct sockaddr *mpls = NULL;
1538
	struct sockaddr *sa;
1539
	char *cp;
1540
	int i;
1541
1542
18
	printf("   route to: %s\n", routename(&so_dst.sa));
1543
9
	if (rtm->rtm_version != RTM_VERSION) {
1544
		warnx("routing message version %d not understood",
1545
		    rtm->rtm_version);
1546
		return;
1547
	}
1548
9
	if (rtm->rtm_msglen > msglen)
1549
		warnx("message length mismatch, in packet %d, returned %d",
1550
		    rtm->rtm_msglen, msglen);
1551
9
	if (rtm->rtm_errno) {
1552
		warnx("RTM_GET: %s (errno %d)",
1553
		    strerror(rtm->rtm_errno), rtm->rtm_errno);
1554
		return;
1555
	}
1556
9
	cp = ((char *)rtm + rtm->rtm_hdrlen);
1557
9
	if (rtm->rtm_addrs)
1558
594
		for (i = 1; i; i <<= 1)
1559
288
			if (i & rtm->rtm_addrs) {
1560
90
				sa = (struct sockaddr *)cp;
1561


90
				switch (i) {
1562
				case RTA_DST:
1563
					dst = sa;
1564
9
					break;
1565
				case RTA_GATEWAY:
1566
					gate = sa;
1567
9
					break;
1568
				case RTA_NETMASK:
1569
					mask = sa;
1570
9
					break;
1571
				case RTA_IFA:
1572
					ifa = sa;
1573
9
					break;
1574
				case RTA_IFP:
1575

18
					if (sa->sa_family == AF_LINK &&
1576
9
					   ((struct sockaddr_dl *)sa)->sdl_nlen)
1577
9
						ifp = (struct sockaddr_dl *)sa;
1578
					break;
1579
				case RTA_SRC:
1580
					mpls = sa;
1581
					break;
1582
				case RTA_LABEL:
1583
					sa_rl = (struct sockaddr_rtlabel *)sa;
1584
					break;
1585
#ifdef BFD
1586
				case RTA_BFD:
1587
					sa_bfd = (struct sockaddr_bfd *)sa;
1588
					break;
1589
#endif
1590
				}
1591
135
				ADVANCE(cp, sa);
1592
45
			}
1593
9
	if (dst && mask)
1594
9
		mask->sa_family = dst->sa_family;	/* XXX */
1595
9
	if (dst)
1596
9
		printf("destination: %s\n", routename(dst));
1597
9
	if (mask) {
1598
9
		int savenflag = nflag;
1599
1600
9
		nflag = 1;
1601
9
		printf("       mask: %s\n", routename(mask));
1602
9
		nflag = savenflag;
1603
9
	}
1604

18
	if (gate && rtm->rtm_flags & RTF_GATEWAY)
1605
9
		printf("    gateway: %s\n", routename(gate));
1606
9
	if (ifp)
1607
9
		printf("  interface: %.*s\n",
1608
9
		    ifp->sdl_nlen, ifp->sdl_data);
1609
9
	if (ifa)
1610
9
		printf(" if address: %s\n", routename(ifa));
1611
9
	if (mpls) {
1612
		printf(" mpls label: %s %s\n", mpls_op(rtm->rtm_mpls),
1613
		    routename(mpls));
1614
	}
1615
18
	printf("   priority: %u (%s)\n", rtm->rtm_priority,
1616
9
	   priorityname(rtm->rtm_priority));
1617
9
	printf("      flags: ");
1618
9
	bprintf(stdout, rtm->rtm_flags, routeflags);
1619
9
	printf("\n");
1620
9
	if (sa_rl != NULL)
1621
		printf("      label: %s\n", sa_rl->sr_label);
1622
#ifdef BFD
1623
9
	if (sa_bfd)
1624
		print_sabfd(sa_bfd, rtm->rtm_fmask);
1625
#endif
1626
1627
#define lock(f)	((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1628
18
	relative_expire = rtm->rtm_rmx.rmx_expire ?
1629
	    rtm->rtm_rmx.rmx_expire - time(NULL) : 0;
1630
9
	printf("     use       mtu    expire\n");
1631
9
	printf("%8llu  %8u%c %8lld%c\n",
1632
9
	    rtm->rtm_rmx.rmx_pksent,
1633
9
	    rtm->rtm_rmx.rmx_mtu, lock(MTU),
1634
9
	    relative_expire, lock(EXPIRE));
1635
#undef lock
1636
#define	RTA_IGN	(RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1637
9
	if (verbose)
1638
		pmsg_common(rtm);
1639
9
	else if (rtm->rtm_addrs &~ RTA_IGN) {
1640
		printf("sockaddrs: ");
1641
		bprintf(stdout, rtm->rtm_addrs, addrnames);
1642
		putchar('\n');
1643
	}
1644
#undef	RTA_IGN
1645
18
}
1646
1647
#ifdef BFD
1648
const char *
1649
bfd_state(unsigned int state)
1650
{
1651
	switch (state) {
1652
	case BFD_STATE_ADMINDOWN:
1653
		return("admindown");
1654
		break;
1655
	case BFD_STATE_DOWN:
1656
		return("down");
1657
		break;
1658
	case BFD_STATE_INIT:
1659
		return("init");
1660
		break;
1661
	case BFD_STATE_UP:
1662
		return("up");
1663
		break;
1664
	}
1665
	return "invalid";
1666
}
1667
1668
const char *
1669
bfd_diag(unsigned int diag)
1670
{
1671
	switch (diag) {
1672
	case BFD_DIAG_NONE:
1673
		return("none");
1674
		break;
1675
	case BFD_DIAG_EXPIRED:
1676
		return("expired");
1677
		break;
1678
	case BFD_DIAG_ECHO_FAILED:
1679
		return("echo-failed");
1680
		break;
1681
	case BFD_DIAG_NEIGHBOR_SIGDOWN:
1682
		return("neighbor-down");
1683
		break;
1684
	case BFD_DIAG_FIB_RESET:
1685
		return("fib-reset");
1686
		break;
1687
	case BFD_DIAG_PATH_DOWN:
1688
		return("path-down");
1689
		break;
1690
	case BFD_DIAG_CONCAT_PATH_DOWN:
1691
		return("concat-path-down");
1692
		break;
1693
	case BFD_DIAG_ADMIN_DOWN:
1694
		return("admindown");
1695
		break;
1696
	case BFD_DIAG_CONCAT_REVERSE_DOWN:
1697
		return("concat-reverse-down");
1698
		break;
1699
	}
1700
	return "invalid";
1701
}
1702
1703
const char *
1704
bfd_calc_uptime(time_t time)
1705
{
1706
	static char buf[256];
1707
	struct tm *tp;
1708
	const char *fmt;
1709
1710
	if (time > 2*86400)
1711
		fmt = "%dd%kh%Mm%Ss";
1712
	else if (time > 2*3600)
1713
		fmt = "%kh%Mm%Ss";
1714
	else if (time > 2*60)
1715
		fmt = "%Mm%Ss";
1716
	else
1717
		fmt = "%Ss";
1718
1719
	tp = localtime(&time);
1720
	(void)strftime(buf, sizeof(buf), fmt, tp);
1721
	return (buf);
1722
}
1723
1724
void
1725
print_bfdmsg(struct rt_msghdr *rtm)
1726
{
1727
	struct bfd_msghdr *bfdm = (struct bfd_msghdr *)rtm;
1728
1729
	printf("\n");
1730
	print_sabfd(&bfdm->bm_sa, rtm->rtm_fmask);
1731
	pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen), rtm->rtm_addrs);
1732
}
1733
1734
void
1735
print_sabfd(struct sockaddr_bfd *sa_bfd, int fmask)
1736
{
1737
	struct timeval tv;
1738
1739
	gettimeofday(&tv, NULL);
1740
1741
	printf("        BFD:");
1742
1743
	/* only show the state, unless verbose or -bfd */
1744
	if (!verbose && ((fmask & RTF_BFD) != RTF_BFD)) {
1745
		printf(" %s\n", bfd_state(sa_bfd->bs_state));
1746
		return;
1747
	}
1748
1749
	switch (sa_bfd->bs_mode) {
1750
	case BFD_MODE_ASYNC:
1751
		printf(" async");
1752
		break;
1753
	case BFD_MODE_DEMAND:
1754
		printf(" demand");
1755
		break;
1756
	default:
1757
		printf(" unknown %u", sa_bfd->bs_mode);
1758
		break;
1759
	}
1760
1761
	printf(" state %s", bfd_state(sa_bfd->bs_state));
1762
	printf(" remote %s", bfd_state(sa_bfd->bs_remotestate));
1763
	printf(" laststate %s", bfd_state(sa_bfd->bs_laststate));
1764
1765
	printf(" error %d", sa_bfd->bs_error);
1766
	printf("\n            ");
1767
	printf(" diag %s", bfd_diag(sa_bfd->bs_localdiag));
1768
	printf(" remote %s", bfd_diag(sa_bfd->bs_remotediag));
1769
	printf("\n            ");
1770
	printf(" discr %u", sa_bfd->bs_localdiscr);
1771
	printf(" remote %u", sa_bfd->bs_remotediscr);
1772
	printf("\n            ");
1773
	printf(" uptime %s", bfd_calc_uptime(tv.tv_sec - sa_bfd->bs_uptime));
1774
	if (sa_bfd->bs_lastuptime)
1775
		printf(" last state time %s",
1776
		    bfd_calc_uptime(sa_bfd->bs_lastuptime));
1777
	printf("\n            ");
1778
	printf(" mintx %u", sa_bfd->bs_mintx);
1779
	printf(" minrx %u", sa_bfd->bs_minrx);
1780
	printf(" minecho %u", sa_bfd->bs_minecho);
1781
	printf(" multiplier %u", sa_bfd->bs_multiplier);
1782
	printf("\n");
1783
}
1784
#endif /* BFD */
1785
1786
void
1787
pmsg_common(struct rt_msghdr *rtm)
1788
{
1789
	printf("\nlocks: ");
1790
	bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1791
	printf(" inits: ");
1792
	bprintf(stdout, rtm->rtm_inits, metricnames);
1793
	pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen), rtm->rtm_addrs);
1794
}
1795
1796
void
1797
pmsg_addrs(char *cp, int addrs)
1798
{
1799
	struct sockaddr *sa;
1800
	int family = AF_UNSPEC;
1801
	int i;
1802
	char *p;
1803
1804
	if (addrs != 0) {
1805
		printf("\nsockaddrs: ");
1806
		bprintf(stdout, addrs, addrnames);
1807
		putchar('\n');
1808
		/* first run, search for address family */
1809
		p = cp;
1810
		for (i = 1; i; i <<= 1)
1811
			if (i & addrs) {
1812
				sa = (struct sockaddr *)p;
1813
				if (family == AF_UNSPEC)
1814
					switch (i) {
1815
					case RTA_DST:
1816
					case RTA_IFA:
1817
						family = sa->sa_family;
1818
					}
1819
				ADVANCE(p, sa);
1820
			}
1821
		/* second run, set address family for mask and print */
1822
		p = cp;
1823
		for (i = 1; i; i <<= 1)
1824
			if (i & addrs) {
1825
				sa = (struct sockaddr *)p;
1826
				if (family != AF_UNSPEC)
1827
					switch (i) {
1828
					case RTA_NETMASK:
1829
						sa->sa_family = family;
1830
					}
1831
				printf(" %s", routename(sa));
1832
				ADVANCE(p, sa);
1833
			}
1834
	}
1835
	putchar('\n');
1836
	fflush(stdout);
1837
}
1838
1839
void
1840
bprintf(FILE *fp, int b, char *s)
1841
{
1842
	int i;
1843
	int gotsome = 0;
1844
1845
18
	if (b == 0)
1846
		return;
1847
225
	while ((i = *s++)) {
1848
216
		if ((b & (1 << (i-1)))) {
1849
45
			if (gotsome == 0)
1850
9
				i = '<';
1851
			else
1852
				i = ',';
1853
90
			putc(i, fp);
1854
			gotsome = 1;
1855
522
			for (; (i = *s) > 32; s++)
1856
432
				putc(i, fp);
1857
		} else
1858
2493
			while (*s > 32)
1859
1161
				s++;
1860
	}
1861
9
	if (gotsome)
1862
18
		putc('>', fp);
1863
18
}
1864
1865
int
1866
keycmp(const void *key, const void *kt)
1867
{
1868
28368
	return (strcmp(key, ((struct keytab *)kt)->kt_cp));
1869
}
1870
1871
int
1872
keyword(char *cp)
1873
{
1874
	struct keytab *kt;
1875
1876
5418
	kt = bsearch(cp, keywords, sizeof(keywords)/sizeof(keywords[0]),
1877
	    sizeof(keywords[0]), keycmp);
1878
2709
	if (!kt)
1879
324
		return (0);
1880
1881
2385
	return (kt->kt_i);
1882
2709
}
1883
1884
void
1885
sodump(sup su, char *which)
1886
{
1887
	switch (su->sa.sa_family) {
1888
	case AF_LINK:
1889
		printf("%s: link %s; ", which, link_ntoa(&su->sdl));
1890
		break;
1891
	case AF_INET:
1892
		printf("%s: inet %s; ", which, inet_ntoa(su->sin.sin_addr));
1893
		break;
1894
	case AF_INET6:
1895
	    {
1896
		char ntop_buf[NI_MAXHOST];
1897
1898
		printf("%s: inet6 %s; ",
1899
		    which, inet_ntop(AF_INET6, &su->sin6.sin6_addr,
1900
		    ntop_buf, sizeof(ntop_buf)));
1901
		break;
1902
	    }
1903
	}
1904
	fflush(stdout);
1905
}
1906
1907
/* States*/
1908
#define VIRGIN	0
1909
#define GOTONE	1
1910
#define GOTTWO	2
1911
/* Inputs */
1912
#define	DIGIT	(4*0)
1913
#define	END	(4*1)
1914
#define DELIM	(4*2)
1915
1916
void
1917
sockaddr(char *addr, struct sockaddr *sa)
1918
{
1919
	char *cp = (char *)sa;
1920
	int size = sa->sa_len;
1921
	char *cplim = cp + size;
1922
	int byte = 0, state = VIRGIN, new = 0;
1923
1924
	memset(cp, 0, size);
1925
	cp++;
1926
	do {
1927
		if ((*addr >= '0') && (*addr <= '9')) {
1928
			new = *addr - '0';
1929
		} else if ((*addr >= 'a') && (*addr <= 'f')) {
1930
			new = *addr - 'a' + 10;
1931
		} else if ((*addr >= 'A') && (*addr <= 'F')) {
1932
			new = *addr - 'A' + 10;
1933
		} else if (*addr == '\0')
1934
			state |= END;
1935
		else
1936
			state |= DELIM;
1937
		addr++;
1938
		switch (state /* | INPUT */) {
1939
		case GOTTWO | DIGIT:
1940
			*cp++ = byte; /*FALLTHROUGH*/
1941
		case VIRGIN | DIGIT:
1942
			state = GOTONE; byte = new; continue;
1943
		case GOTONE | DIGIT:
1944
			state = GOTTWO; byte = new + (byte << 4); continue;
1945
		default: /* | DELIM */
1946
			state = VIRGIN; *cp++ = byte; byte = 0; continue;
1947
		case GOTONE | END:
1948
		case GOTTWO | END:
1949
			*cp++ = byte; /* FALLTHROUGH */
1950
		case VIRGIN | END:
1951
			break;
1952
		}
1953
		break;
1954
	} while (cp < cplim);
1955
	sa->sa_len = cp - (char *)sa;
1956
}
1957
1958
void
1959
getlabel(char *name)
1960
{
1961
	so_label.rtlabel.sr_len = sizeof(so_label.rtlabel);
1962
	so_label.rtlabel.sr_family = AF_UNSPEC;
1963
	if (strlcpy(so_label.rtlabel.sr_label, name,
1964
	    sizeof(so_label.rtlabel.sr_label)) >=
1965
	    sizeof(so_label.rtlabel.sr_label))
1966
		errx(1, "label too long");
1967
	rtm_addrs |= RTA_LABEL;
1968
}
1969
1970
int
1971
gettable(const char *s)
1972
{
1973
2250
	const char		*errstr;
1974
1125
	struct rt_tableinfo      info;
1975
1125
	int			 mib[6];
1976
1125
	size_t			 len;
1977
1978
1125
	tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr);
1979
1125
	if (errstr)
1980
		errx(1, "invalid table id: %s", errstr);
1981
1982
1125
	mib[0] = CTL_NET;
1983
1125
	mib[1] = PF_ROUTE;
1984
1125
	mib[2] = 0;
1985
1125
	mib[3] = 0;
1986
1125
	mib[4] = NET_RT_TABLE;
1987
1125
	mib[5] = tableid;
1988
1989
1125
	len = sizeof(info);
1990
1125
	if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
1991
		return (errno);
1992
	else
1993
1125
		return (0);
1994
1125
}
1995
1996
int
1997
rdomain(int argc, char **argv)
1998
{
1999
	if (!argc)
2000
		usage(NULL);
2001
	if (setrtable(tableid) == -1)
2002
		err(1, "setrtable");
2003
	execvp(*argv, argv);
2004
	warn("%s", argv[0]);
2005
	return (errno == ENOENT ? 127 : 126);
2006
}
2007
2008
/*
2009
 * Print RTM_PROPOSAL DNS server addresses.
2010
 */
2011
void
2012
print_rtdns(struct sockaddr_rtdns *rtdns)
2013
{
2014
	struct in_addr	 server;
2015
	struct in6_addr	 in6;
2016
	size_t		 srclen, offset;
2017
	unsigned int	 servercnt;
2018
	int		 i;
2019
	char		*src = rtdns->sr_dns;
2020
	char		 ntopbuf[INET6_ADDRSTRLEN];
2021
2022
	offset = offsetof(struct sockaddr_rtdns, sr_dns);
2023
	if (rtdns->sr_len <= offset) {
2024
		printf("<invalid sr_len (%d <= %zu)>\n", rtdns->sr_len,
2025
		    offset);
2026
		return;
2027
	}
2028
	srclen = rtdns->sr_len - offset;
2029
	if (srclen > sizeof(rtdns->sr_dns)) {
2030
		printf("<invalid sr_len (%zu > %zu)>\n", srclen,
2031
		    sizeof(rtdns->sr_dns));
2032
		return;
2033
	}
2034
2035
	switch (rtdns->sr_family) {
2036
	case AF_INET:
2037
		/* An array of IPv4 addresses. */
2038
		servercnt = srclen / sizeof(struct in_addr);
2039
		if (servercnt * sizeof(struct in_addr) != srclen) {
2040
			printf("<invalid server count>\n");
2041
			return;
2042
		}
2043
		for (i = 0; i < servercnt; i++) {
2044
			memcpy(&server.s_addr, src, sizeof(server.s_addr));
2045
			printf("%s ", inet_ntoa(server));
2046
			src += sizeof(struct in_addr);
2047
		}
2048
		break;
2049
	case AF_INET6:
2050
		servercnt = srclen / sizeof(struct in6_addr);
2051
		if (servercnt * sizeof(struct in6_addr) != srclen) {
2052
			printf("<invalid server count>\n");
2053
			return;
2054
		}
2055
		for (i = 0; i < servercnt; i++) {
2056
			memcpy(&in6, src, sizeof(in6));
2057
			src += sizeof(in6);
2058
			printf("%s ", inet_ntop(AF_INET6, &in6, ntopbuf,
2059
			    INET6_ADDRSTRLEN));
2060
		}
2061
		break;
2062
	default:
2063
		break;
2064
	}
2065
	printf("\n");
2066
}
2067
2068
/*
2069
 * Print RTM_PROPOSAL static routes.
2070
 */
2071
void
2072
print_rtstatic(struct sockaddr_rtstatic *rtstatic)
2073
{
2074
	struct sockaddr_in6	 gateway6;
2075
	struct in6_addr		 prefix;
2076
	struct in_addr		 dest, gateway;
2077
	size_t			 srclen, offset;
2078
	int			 bits, bytes, error;
2079
	uint8_t			 prefixlen;
2080
	unsigned char		*src = rtstatic->sr_static;
2081
	char			 ntoabuf[INET_ADDRSTRLEN];
2082
	char			 hbuf[NI_MAXHOST];
2083
	char			 ntopbuf[INET6_ADDRSTRLEN];
2084
2085
	offset = offsetof(struct sockaddr_rtstatic, sr_static);
2086
	if (rtstatic->sr_len <= offset) {
2087
		printf("<invalid sr_len (%d <= %zu)>\n", rtstatic->sr_len,
2088
		    offset);
2089
		return;
2090
	}
2091
	srclen = rtstatic->sr_len - offset;
2092
	if (srclen > sizeof(rtstatic->sr_static)) {
2093
		printf("<invalid sr_len (%zu > %zu)>\n", srclen,
2094
		    sizeof(rtstatic->sr_static));
2095
		return;
2096
	}
2097
2098
	switch (rtstatic->sr_family) {
2099
	case AF_INET:
2100
		/* AF_INET -> RFC 3442 encoded static routes. */
2101
		while (srclen) {
2102
			bits = *src;
2103
			src++;
2104
			srclen--;
2105
			bytes = (bits + 7) / 8;
2106
			if (srclen < bytes || bytes > sizeof(dest.s_addr))
2107
				break;
2108
			memset(&dest, 0, sizeof(dest));
2109
			memcpy(&dest.s_addr, src, bytes);
2110
			src += bytes;
2111
			srclen -= bytes;
2112
			strlcpy(ntoabuf, inet_ntoa(dest), sizeof(ntoabuf));
2113
			if (srclen < sizeof(gateway.s_addr))
2114
				break;
2115
			memcpy(&gateway.s_addr, src, sizeof(gateway.s_addr));
2116
			src += sizeof(gateway.s_addr);
2117
			srclen -= sizeof(gateway.s_addr);
2118
			printf("%s/%u %s ", ntoabuf, bits, inet_ntoa(gateway));
2119
		}
2120
		break;
2121
	case AF_INET6:
2122
		while (srclen >= sizeof(prefixlen) + sizeof(prefix) +
2123
		    sizeof(gateway6)) {
2124
			memcpy(&prefixlen, src, sizeof(prefixlen));
2125
			srclen -= sizeof(prefixlen);
2126
			src += sizeof(prefixlen);
2127
2128
			memcpy(&prefix, src, sizeof(prefix));
2129
			srclen -= sizeof(prefix);
2130
			src += sizeof(prefix);
2131
2132
			memcpy(&gateway6, src, sizeof(gateway6));
2133
			srclen -= sizeof(gateway6);
2134
			src += sizeof(gateway6);
2135
2136
			if ((error = getnameinfo((struct sockaddr *)&gateway6,
2137
			    gateway6.sin6_len, hbuf, sizeof(hbuf), NULL, 0,
2138
			    NI_NUMERICHOST | NI_NUMERICSERV))) {
2139
				warnx("cannot get gateway address: %s",
2140
				    gai_strerror(error));
2141
				return;
2142
			}
2143
			printf("%s/%u %s ", inet_ntop(AF_INET6, &prefix,
2144
			    ntopbuf, INET6_ADDRSTRLEN), prefixlen, hbuf);
2145
		}
2146
		break;
2147
	default:
2148
		printf("<unknown address family %d>", rtstatic->sr_family);
2149
		break;
2150
	}
2151
	printf("\n");
2152
}
2153
2154
/*
2155
 * Print RTM_PROPOSAL domain search list.
2156
 */
2157
void
2158
print_rtsearch(struct sockaddr_rtsearch *rtsearch)
2159
{
2160
	char	*src = rtsearch->sr_search;
2161
	size_t	 srclen, offset;
2162
2163
	offset = offsetof(struct sockaddr_rtsearch, sr_search);
2164
	if (rtsearch->sr_len <= offset) {
2165
		printf("<invalid sr_len (%d <= %zu)>\n", rtsearch->sr_len,
2166
		    offset);
2167
		return;
2168
	}
2169
	srclen = rtsearch->sr_len - offset;
2170
	if (srclen > sizeof(rtsearch->sr_search)) {
2171
		printf("<invalid sr_len (%zu > %zu)>\n", srclen,
2172
		    sizeof(rtsearch->sr_search));
2173
		return;
2174
	}
2175
2176
	printf("%.*s\n", (int)srclen, src);
2177
}