GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/route/show.c Lines: 180 315 57.1 %
Date: 2016-12-06 Branches: 122 262 46.6 %

Line Branch Exec Source
1
/*	$OpenBSD: show.c,v 1.104 2016/07/13 08:40:46 mpi Exp $	*/
2
/*	$NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $	*/
3
4
/*
5
 * Copyright (c) 1983, 1988, 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_types.h>
39
#include <net/route.h>
40
#include <netinet/in.h>
41
#include <netinet/if_ether.h>
42
#include <netmpls/mpls.h>
43
#include <arpa/inet.h>
44
45
#include <err.h>
46
#include <errno.h>
47
#include <netdb.h>
48
#include <stdio.h>
49
#include <stddef.h>
50
#include <stdlib.h>
51
#include <string.h>
52
#include <unistd.h>
53
#include <limits.h>
54
55
#include "show.h"
56
57
char	*any_ntoa(const struct sockaddr *);
58
char	*link_print(struct sockaddr *);
59
char	*label_print(struct sockaddr *);
60
61
#define ROUNDUP(a) \
62
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
63
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
64
65
/*
66
 * Definitions for showing gateway flags.
67
 */
68
struct bits {
69
	int	b_mask;
70
	char	b_val;
71
};
72
static const struct bits bits[] = {
73
	{ RTF_UP,	'U' },
74
	{ RTF_GATEWAY,	'G' },
75
	{ RTF_HOST,	'H' },
76
	{ RTF_REJECT,	'R' },
77
	{ RTF_BLACKHOLE, 'B' },
78
	{ RTF_DYNAMIC,	'D' },
79
	{ RTF_MODIFIED,	'M' },
80
	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
81
	{ RTF_CLONING,	'C' },
82
	{ RTF_MULTICAST,'m' },
83
	{ RTF_LLINFO,	'L' },
84
	{ RTF_STATIC,	'S' },
85
	{ RTF_PROTO1,	'1' },
86
	{ RTF_PROTO2,	'2' },
87
	{ RTF_PROTO3,	'3' },
88
	{ RTF_CLONED,	'c' },
89
	{ RTF_MPATH,	'P' },
90
	{ RTF_MPLS,	'T' },
91
	{ RTF_LOCAL,	'l' },
92
	{ RTF_BROADCAST, 'b' },
93
	{ 0 }
94
};
95
96
int	 WID_DST(int);
97
void	 pr_rthdr(int);
98
void	 p_rtentry(struct rt_msghdr *);
99
void	 pr_family(int);
100
void	 p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
101
void	 p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
102
void	 p_flags(int, char *);
103
char	*routename4(in_addr_t);
104
char	*routename6(struct sockaddr_in6 *);
105
char	*netname4(in_addr_t, struct sockaddr_in *);
106
char	*netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
107
108
/*
109
 * Print routing tables.
110
 */
111
void
112
p_rttables(int af, u_int tableid, int hastable, char prio)
113
5
{
114
	struct rt_msghdr *rtm;
115
5
	char *buf = NULL, *next, *lim = NULL;
116
	size_t needed;
117
	int mib[7], mcnt;
118
	struct sockaddr *sa;
119
120
5
	mib[0] = CTL_NET;
121
5
	mib[1] = PF_ROUTE;
122
5
	mib[2] = 0;
123
5
	mib[3] = af;
124
5
	mib[4] = NET_RT_DUMP;
125
5
	mib[5] = prio;
126
5
	if (hastable) {
127
5
		mib[6] = tableid;
128
5
		mcnt = 7;
129
	} else
130
		mcnt = 6;
131
132
	while (1) {
133
5
		if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
134
			err(1, "route-sysctl-estimate");
135
5
		if (needed == 0)
136
			break;
137
5
		if ((buf = realloc(buf, needed)) == NULL)
138
			err(1, NULL);
139
5
		if (sysctl(mib, mcnt, buf, &needed, NULL, 0) == -1) {
140
			if (errno == ENOMEM)
141
				continue;
142
			err(1, "sysctl of routing table");
143
		}
144
5
		lim = buf + needed;
145
5
		break;
146
	}
147
148
5
	if (pledge("stdio rpath dns cpath wpath", NULL) == -1)
149
		err(1, "pledge");
150
151
5
	printf("Routing tables\n");
152
153
5
	if (buf) {
154
82
		for (next = buf; next < lim; next += rtm->rtm_msglen) {
155
77
			rtm = (struct rt_msghdr *)next;
156
77
			if (rtm->rtm_version != RTM_VERSION)
157
				continue;
158
77
			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
159
77
			p_rtentry(rtm);
160
		}
161
5
		free(buf);
162
5
		buf = NULL;
163
	}
164
5
}
165
166
/*
167
 * column widths; each followed by one space
168
 * width of destination/gateway column
169
 * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
170
 */
171
#define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
172
173
int
174
WID_DST(int af)
175
164
{
176
177
164
	if (nflag)
178
164
		switch (af) {
179
		case AF_MPLS:
180
			return 9;
181
		case AF_INET6:
182
28
			return 34;
183
		default:
184
136
			return 18;
185
		}
186
	else
187
		switch (af) {
188
		case AF_MPLS:
189
			return 9;
190
 		default:
191
			return 18;
192
		}
193
}
194
195
/*
196
 * Print header for routing table columns.
197
 */
198
void
199
pr_rthdr(int af)
200
5
{
201
5
	switch (af) {
202
	case PF_KEY:
203
		printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
204
		    "Source", "Port", "Destination",
205
		    "Port", "Proto", "SA(Address/Proto/Type/Direction)");
206
		break;
207
	case PF_MPLS:
208
		printf("%-9s %-9s %-6s %-18s %-6.6s %5.5s %8.8s %5.5s  %4.4s %s\n",
209
		    "In label", "Out label", "Op", "Gateway",
210
		    "Flags", "Refs", "Use", "Mtu", "Prio", "Interface");
211
		break;
212
	default:
213


5
		printf("%-*.*s %-*.*s %-6.6s %5.5s %8.8s %5.5s  %4.4s %s",
214
		    WID_DST(af), WID_DST(af), "Destination",
215
		    WID_GW(af), WID_GW(af), "Gateway",
216
		    "Flags", "Refs", "Use", "Mtu", "Prio", "Iface");
217
5
		if (verbose)
218
			printf(" %s", "Label");
219
5
		putchar('\n');
220
		break;
221
	}
222
5
}
223
224
static void
225
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
226
77
{
227
	int	i;
228
229
924
	for (i = 0; i < RTAX_MAX; i++) {
230
847
		if (addrs & (1 << i)) {
231
385
			rti_info[i] = sa;
232
385
			sa = (struct sockaddr *)((char *)(sa) +
233
			    ROUNDUP(sa->sa_len));
234
		} else
235
462
			rti_info[i] = NULL;
236
	}
237
77
}
238
239
/*
240
 * Print a routing table entry.
241
 */
242
void
243
p_rtentry(struct rt_msghdr *rtm)
244
77
{
245
	static int	 old_af = -1;
246
77
	struct sockaddr	*sa = (struct sockaddr *)((char *)rtm + rtm->rtm_hdrlen);
247
	struct sockaddr	*mask, *rti_info[RTAX_MAX];
248
	char		 ifbuf[IF_NAMESIZE];
249
	char		*label;
250
251
77
	if (sa->sa_family == AF_KEY)
252
		return;
253
254
77
	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
255
256

77
	if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
257
		return;
258
	}
259
260
77
	if (strlen(so_label.rtlabel.sr_label)) {
261
		if (!rti_info[RTAX_LABEL])
262
			return;
263
		label = ((struct sockaddr_rtlabel *)rti_info[RTAX_LABEL])->
264
		    sr_label;
265
		if (strcmp(label, so_label.rtlabel.sr_label))
266
			return;
267
	}
268
269
77
	if (old_af != sa->sa_family) {
270
5
		old_af = sa->sa_family;
271
5
		pr_family(sa->sa_family);
272
5
		pr_rthdr(sa->sa_family);
273
	}
274
275
77
	mask = rti_info[RTAX_NETMASK];
276
77
	if ((sa = rti_info[RTAX_DST]) == NULL)
277
		return;
278
279
77
	p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family));
280
77
	p_sockaddr_mpls(sa, rti_info[RTAX_SRC], rtm->rtm_mpls,
281
	    WID_DST(sa->sa_family));
282
283

77
	p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
284
	    WID_GW(sa->sa_family));
285
286
77
	p_flags(rtm->rtm_flags, "%-6.6s ");
287
77
	printf("%5u %8llu ", rtm->rtm_rmx.rmx_refcnt,
288
	    rtm->rtm_rmx.rmx_pksent);
289
77
	if (rtm->rtm_rmx.rmx_mtu)
290
77
		printf("%5u ", rtm->rtm_rmx.rmx_mtu);
291
	else
292
		printf("%5s ", "-");
293

77
	putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
294
77
	printf("  %2d %-5.16s", rtm->rtm_priority,
295
	    if_indextoname(rtm->rtm_index, ifbuf));
296

77
	if (verbose && rti_info[RTAX_LABEL])
297
		printf(" %s", ((struct sockaddr_rtlabel *)
298
		    rti_info[RTAX_LABEL])->sr_label);
299
77
	putchar('\n');
300
}
301
302
/*
303
 * Print address family header before a section of the routing table.
304
 */
305
void
306
pr_family(int af)
307
5
{
308
	char *afname;
309
310

5
	switch (af) {
311
	case AF_INET:
312
4
		afname = "Internet";
313
4
		break;
314
	case AF_INET6:
315
1
		afname = "Internet6";
316
1
		break;
317
	case PF_KEY:
318
		afname = "Encap";
319
		break;
320
	case AF_MPLS:
321
		afname = "MPLS";
322
		break;
323
	default:
324
		afname = NULL;
325
		break;
326
	}
327
5
	if (afname)
328
5
		printf("\n%s:\n", afname);
329
	else
330
		printf("\nProtocol Family %d:\n", af);
331
5
}
332
333
void
334
p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
335
154
{
336
	char *cp;
337
338
154
	switch (sa->sa_family) {
339
	case AF_INET6:
340
	    {
341
26
		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
342
26
		struct in6_addr *in6 = &sa6->sin6_addr;
343
344
		/*
345
		 * XXX: This is a special workaround for KAME kernels.
346
		 * sin6_scope_id field of SA should be set in the future.
347
		 */
348



26
		if (IN6_IS_ADDR_LINKLOCAL(in6) ||
349
		    IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
350
		    IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
351
			/* XXX: override is ok? */
352
12
			sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
353
			    &in6->s6_addr[2]);
354
12
			*(u_short *)&in6->s6_addr[2] = 0;
355
		}
356
26
		if (flags & RTF_HOST)
357
20
			cp = routename((struct sockaddr *)sa6);
358
		else
359
6
			cp = netname((struct sockaddr *)sa6, mask);
360
		break;
361
	    }
362
	case AF_MPLS:
363
		return;
364
	default:
365
128
		if ((flags & RTF_HOST) || mask == NULL)
366
80
			cp = routename(sa);
367
		else
368
48
			cp = netname(sa, mask);
369
		break;
370
	}
371
154
	if (width < 0)
372
		printf("%s", cp);
373
	else {
374
154
		if (nflag)
375
154
			printf("%-*s ", width, cp);
376
		else
377
			printf("%-*.*s ", width, width, cp);
378
	}
379
}
380
381
static char line[HOST_NAME_MAX+1];
382
static char domain[HOST_NAME_MAX+1];
383
384
void
385
p_sockaddr_mpls(struct sockaddr *in, struct sockaddr *out, int flags, int width)
386
77
{
387
77
	if (in->sa_family != AF_MPLS)
388
77
		return;
389
390
	if (flags & MPLS_OP_POP || flags == MPLS_OP_LOCAL) {
391
		printf("%-*s ", width, label_print(in));
392
		printf("%-*s ", width, label_print(NULL));
393
	} else {
394
		printf("%-*s ", width, label_print(in));
395
		printf("%-*s ", width, label_print(out));
396
	}
397
398
	printf("%-6s ", mpls_op(flags));
399
}
400
401
void
402
p_flags(int f, char *format)
403
77
{
404
	char name[33], *flags;
405
77
	const struct bits *p = bits;
406
407

1617
	for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
408
1540
		if (p->b_mask & f)
409
237
			*flags++ = p->b_val;
410
77
	*flags = '\0';
411
77
	printf(format, name);
412
77
}
413
414
char *
415
routename(struct sockaddr *sa)
416
105
{
417
105
	char *cp = NULL;
418
	static int first = 1;
419
420
105
	if (first) {
421
6
		first = 0;
422

12
		if (gethostname(domain, sizeof(domain)) == 0 &&
423
		    (cp = strchr(domain, '.')))
424
6
			(void)strlcpy(domain, cp + 1, sizeof(domain));
425
		else
426
			domain[0] = '\0';
427
6
		cp = NULL;
428
	}
429
430
105
	if (sa->sa_len == 0) {
431
		(void)strlcpy(line, "default", sizeof(line));
432
		return (line);
433
	}
434
435

105
	switch (sa->sa_family) {
436
	case AF_INET:
437
80
		return
438
		    (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
439
440
	case AF_INET6:
441
	    {
442
		struct sockaddr_in6 sin6;
443
444
25
		memset(&sin6, 0, sizeof(sin6));
445
25
		memcpy(&sin6, sa, sa->sa_len);
446
25
		sin6.sin6_len = sizeof(struct sockaddr_in6);
447
25
		sin6.sin6_family = AF_INET6;
448




25
		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
449
		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
450
		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
451
		     IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
452
		    sin6.sin6_scope_id == 0) {
453
			sin6.sin6_scope_id =
454
			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
455
			sin6.sin6_addr.s6_addr[2] = 0;
456
			sin6.sin6_addr.s6_addr[3] = 0;
457
		}
458
25
		return (routename6(&sin6));
459
	    }
460
461
	case AF_LINK:
462
		return (link_print(sa));
463
	case AF_MPLS:
464
		return (label_print(sa));
465
	case AF_UNSPEC:
466
		if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
467
			static char name[RTLABEL_LEN];
468
			struct sockaddr_rtlabel *sr;
469
470
			sr = (struct sockaddr_rtlabel *)sa;
471
			(void)strlcpy(name, sr->sr_label, sizeof(name));
472
			return (name);
473
		}
474
		/* FALLTHROUGH */
475
	default:
476
		(void)snprintf(line, sizeof(line), "(%d) %s",
477
		    sa->sa_family, any_ntoa(sa));
478
		break;
479
	}
480
	return (line);
481
}
482
483
char *
484
routename4(in_addr_t in)
485
80
{
486
80
	char		*cp = NULL;
487
	struct in_addr	 ina;
488
	struct hostent	*hp;
489
490
80
	if (in == INADDR_ANY)
491
		cp = "default";
492

80
	if (!cp && !nflag) {
493
		if ((hp = gethostbyaddr((char *)&in,
494
		    sizeof(in), AF_INET)) != NULL) {
495
			if ((cp = strchr(hp->h_name, '.')) &&
496
			    !strcmp(cp + 1, domain))
497
				*cp = '\0';
498
			cp = hp->h_name;
499
		}
500
	}
501
80
	ina.s_addr = in;
502
80
	strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
503
504
80
	return (line);
505
}
506
507
char *
508
routename6(struct sockaddr_in6 *sin6)
509
25
{
510
25
	int	 niflags = 0;
511
512
25
	if (nflag)
513
25
		niflags |= NI_NUMERICHOST;
514
	else
515
		niflags |= NI_NOFQDN;
516
517
25
	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
518
	    line, sizeof(line), NULL, 0, niflags) != 0)
519
		strncpy(line, "invalid", sizeof(line));
520
521
25
	return (line);
522
}
523
524
/*
525
 * Return the name of the network whose address is given.
526
 * The address is assumed to be that of a net or subnet, not a host.
527
 */
528
char *
529
netname4(in_addr_t in, struct sockaddr_in *maskp)
530
48
{
531
48
	char *cp = NULL;
532
48
	struct netent *np = NULL;
533
	in_addr_t mask;
534
	int mbits;
535
536
48
	in = ntohl(in);
537

48
	mask = maskp && maskp->sin_len != 0 ? ntohl(maskp->sin_addr.s_addr) : 0;
538
48
	if (!nflag && in != INADDR_ANY) {
539
		if ((np = getnetbyaddr(in, AF_INET)) != NULL)
540
			cp = np->n_name;
541
	}
542
48
	if (in == INADDR_ANY && mask == INADDR_ANY)
543
		cp = "default";
544
48
	mbits = mask ? 33 - ffs(mask) : 0;
545
48
	if (cp)
546
		strlcpy(line, cp, sizeof(line));
547
#define C(x)	((x) & 0xff)
548
48
	else if (mbits < 9)
549
4
		snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
550
44
	else if (mbits < 17)
551
12
		snprintf(line, sizeof(line), "%u.%u/%d",
552
		    C(in >> 24) , C(in >> 16), mbits);
553
32
	else if (mbits < 25)
554
32
		snprintf(line, sizeof(line), "%u.%u.%u/%d",
555
		    C(in >> 24), C(in >> 16), C(in >> 8), mbits);
556
	else
557
		snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
558
		    C(in >> 16), C(in >> 8), C(in), mbits);
559
#undef C
560
48
	return (line);
561
}
562
563
char *
564
netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
565
6
{
566
	struct sockaddr_in6 sin6;
567
	u_char *p;
568
6
	int masklen, final = 0, illegal = 0;
569
	int i, lim, flag, error;
570
	char hbuf[NI_MAXHOST];
571
572
6
	sin6 = *sa6;
573
574
6
	flag = 0;
575
6
	masklen = 0;
576
6
	if (mask) {
577
6
		lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
578
6
		lim = lim < (int)sizeof(struct in6_addr) ?
579
		    lim : (int)sizeof(struct in6_addr);
580
102
		for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
581

96
			if (final && *p) {
582
				illegal++;
583
				sin6.sin6_addr.s6_addr[i++] = 0x00;
584
				continue;
585
			}
586
587


96
			switch (*p & 0xff) {
588
			case 0xff:
589
24
				masklen += 8;
590
24
				break;
591
			case 0xfe:
592
				masklen += 7;
593
				final++;
594
				break;
595
			case 0xfc:
596
				masklen += 6;
597
				final++;
598
				break;
599
			case 0xf8:
600
				masklen += 5;
601
				final++;
602
				break;
603
			case 0xf0:
604
				masklen += 4;
605
				final++;
606
				break;
607
			case 0xe0:
608
				masklen += 3;
609
				final++;
610
				break;
611
			case 0xc0:
612
				masklen += 2;
613
				final++;
614
				break;
615
			case 0x80:
616
				masklen += 1;
617
				final++;
618
				break;
619
			case 0x00:
620
72
				final++;
621
72
				break;
622
			default:
623
				final++;
624
				illegal++;
625
				break;
626
			}
627
628
96
			if (!illegal)
629
96
				sin6.sin6_addr.s6_addr[i++] &= *p;
630
			else
631
				sin6.sin6_addr.s6_addr[i++] = 0x00;
632
		}
633
6
		while (i < (int)sizeof(struct in6_addr))
634
			sin6.sin6_addr.s6_addr[i++] = 0x00;
635
	} else
636
		masklen = 128;
637
638


6
	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
639
		return ("default");
640
641
6
	if (illegal)
642
		warnx("illegal prefixlen");
643
644
6
	if (nflag)
645
6
		flag |= NI_NUMERICHOST;
646
6
	error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
647
	    hbuf, sizeof(hbuf), NULL, 0, flag);
648
6
	if (error)
649
		snprintf(hbuf, sizeof(hbuf), "invalid");
650
651
6
	snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
652
6
	return (line);
653
}
654
655
/*
656
 * Return the name of the network whose address is given.
657
 * The address is assumed to be that of a net or subnet, not a host.
658
 */
659
char *
660
netname(struct sockaddr *sa, struct sockaddr *mask)
661
54
{
662

54
	switch (sa->sa_family) {
663
	case AF_INET:
664
48
		return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
665
		    (struct sockaddr_in *)mask);
666
	case AF_INET6:
667
6
		return netname6((struct sockaddr_in6 *)sa,
668
		    (struct sockaddr_in6 *)mask);
669
	case AF_LINK:
670
		return (link_print(sa));
671
	case AF_MPLS:
672
		return (label_print(sa));
673
	default:
674
		snprintf(line, sizeof(line), "af %d: %s",
675
		    sa->sa_family, any_ntoa(sa));
676
		break;
677
	}
678
	return (line);
679
}
680
681
static const char hexlist[] = "0123456789abcdef";
682
683
char *
684
any_ntoa(const struct sockaddr *sa)
685
{
686
	static char obuf[240];
687
	const char *in = sa->sa_data;
688
	char *out = obuf;
689
	int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
690
691
	*out++ = 'Q';
692
	do {
693
		*out++ = hexlist[(*in >> 4) & 15];
694
		*out++ = hexlist[(*in++)    & 15];
695
		*out++ = '.';
696
	} while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
697
	out[-1] = '\0';
698
	return (obuf);
699
}
700
701
char *
702
link_print(struct sockaddr *sa)
703
{
704
	struct sockaddr_dl	*sdl = (struct sockaddr_dl *)sa;
705
	u_char			*lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
706
707
	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
708
	    sdl->sdl_slen == 0) {
709
		(void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
710
		return (line);
711
	}
712
	switch (sdl->sdl_type) {
713
	case IFT_ETHER:
714
	case IFT_CARP:
715
		return (ether_ntoa((struct ether_addr *)lla));
716
	default:
717
		return (link_ntoa(sdl));
718
	}
719
}
720
721
char *
722
mpls_op(u_int32_t type)
723
{
724
	switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
725
	case MPLS_OP_LOCAL:
726
		return ("LOCAL");
727
	case MPLS_OP_POP:
728
		return ("POP");
729
	case MPLS_OP_SWAP:
730
		return ("SWAP");
731
	case MPLS_OP_PUSH:
732
		return ("PUSH");
733
	default:
734
		return ("?");
735
	}
736
}
737
738
char *
739
label_print(struct sockaddr *sa)
740
{
741
	struct sockaddr_mpls	*smpls = (struct sockaddr_mpls *)sa;
742
743
	if (smpls)
744
		(void)snprintf(line, sizeof(line), "%u",
745
		    ntohl(smpls->smpls_label) >> MPLS_LABEL_OFFSET);
746
	else
747
		(void)snprintf(line, sizeof(line), "-");
748
749
	return (line);
750
}