GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/netstat/show.c Lines: 0 282 0.0 %
Date: 2016-12-06 Branches: 0 250 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: show.c,v 1.50 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 "netstat.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	 p_rtentry(struct rt_msghdr *);
98
void	 pr_family(int);
99
void	 p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
100
void	 p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
101
void	 p_flags(int, char *);
102
char	*routename4(in_addr_t);
103
char	*routename6(struct sockaddr_in6 *);
104
105
size_t
106
get_sysctl(const int *mib, u_int mcnt, char **buf)
107
{
108
	size_t needed;
109
110
	while (1) {
111
		if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
112
			err(1, "sysctl-estimate");
113
		if (needed == 0)
114
			break;
115
		if ((*buf = realloc(*buf, needed)) == NULL)
116
			err(1, NULL);
117
		if (sysctl(mib, mcnt, *buf, &needed, NULL, 0) == -1) {
118
			if (errno == ENOMEM)
119
				continue;
120
			err(1, "sysctl");
121
		}
122
		break;
123
	}
124
125
	return needed;
126
}
127
128
/*
129
 * Print routing tables.
130
 */
131
void
132
p_rttables(int af, u_int tableid)
133
{
134
	struct rt_msghdr *rtm;
135
	char *buf = NULL, *next, *lim = NULL;
136
	size_t needed;
137
	int mib[7], mcnt;
138
	struct sockaddr *sa;
139
140
	mib[0] = CTL_NET;
141
	mib[1] = PF_ROUTE;
142
	mib[2] = 0;
143
	mib[3] = af;
144
	mib[4] = NET_RT_DUMP;
145
	mib[5] = 0;
146
	mib[6] = tableid;
147
	mcnt = 7;
148
149
	needed = get_sysctl(mib, mcnt, &buf);
150
	lim = buf + needed;
151
152
	printf("Routing tables\n");
153
154
	if (buf) {
155
		for (next = buf; next < lim; next += rtm->rtm_msglen) {
156
			rtm = (struct rt_msghdr *)next;
157
			if (rtm->rtm_version != RTM_VERSION)
158
				continue;
159
			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
160
			p_rtentry(rtm);
161
		}
162
	}
163
	free(buf);
164
}
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
{
176
177
	if (nflag)
178
		switch (af) {
179
		case AF_MPLS:
180
			return 9;
181
		case AF_INET6:
182
			return 34;
183
		default:
184
			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, int Aflag)
200
{
201
	if (Aflag)
202
		printf("%-*.*s ", PLEN, PLEN, "Address");
203
	switch (af) {
204
	case PF_KEY:
205
		printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
206
		    "Source", "Port", "Destination",
207
		    "Port", "Proto", "SA(Address/Proto/Type/Direction)");
208
		break;
209
	case PF_MPLS:
210
		printf("%-9s %-9s %-6s %-18s %-6.6s %5.5s %8.8s %5.5s  %4.4s %s\n",
211
		    "In label", "Out label", "Op", "Gateway",
212
		    "Flags", "Refs", "Use", "Mtu", "Prio", "Interface");
213
		break;
214
	default:
215
		printf("%-*.*s %-*.*s %-6.6s %5.5s %8.8s %5.5s  %4.4s %s",
216
		    WID_DST(af), WID_DST(af), "Destination",
217
		    WID_GW(af), WID_GW(af), "Gateway",
218
		    "Flags", "Refs", "Use", "Mtu", "Prio", "Iface");
219
		if (vflag && !Aflag)
220
			printf(" %s", "Label");
221
		putchar('\n');
222
		break;
223
	}
224
}
225
226
static void
227
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
228
{
229
	int	i;
230
231
	for (i = 0; i < RTAX_MAX; i++) {
232
		if (addrs & (1 << i)) {
233
			rti_info[i] = sa;
234
			sa = (struct sockaddr *)((char *)(sa) +
235
			    ROUNDUP(sa->sa_len));
236
		} else
237
			rti_info[i] = NULL;
238
	}
239
}
240
241
/*
242
 * Print a routing table entry.
243
 */
244
void
245
p_rtentry(struct rt_msghdr *rtm)
246
{
247
	static int	 old_af = -1;
248
	struct sockaddr	*sa = (struct sockaddr *)((char *)rtm + rtm->rtm_hdrlen);
249
	struct sockaddr	*mask, *rti_info[RTAX_MAX];
250
	char		 ifbuf[IF_NAMESIZE];
251
252
	if (sa->sa_family == AF_KEY)
253
		return;
254
255
	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
256
257
	if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
258
		return;
259
	}
260
	if (old_af != sa->sa_family) {
261
		old_af = sa->sa_family;
262
		pr_family(sa->sa_family);
263
		pr_rthdr(sa->sa_family, 0);
264
	}
265
266
	mask = rti_info[RTAX_NETMASK];
267
	if ((sa = rti_info[RTAX_DST]) == NULL)
268
		return;
269
270
	p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family));
271
	p_sockaddr_mpls(sa, rti_info[RTAX_SRC], rtm->rtm_mpls,
272
	    WID_DST(sa->sa_family));
273
274
	p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
275
	    WID_GW(sa->sa_family));
276
277
	p_flags(rtm->rtm_flags, "%-6.6s ");
278
	printf("%5u %8llu ", rtm->rtm_rmx.rmx_refcnt,
279
	    rtm->rtm_rmx.rmx_pksent);
280
	if (rtm->rtm_rmx.rmx_mtu)
281
		printf("%5u ", rtm->rtm_rmx.rmx_mtu);
282
	else
283
		printf("%5s ", "-");
284
	putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
285
	printf("  %2d %-5.16s", rtm->rtm_priority,
286
	    if_indextoname(rtm->rtm_index, ifbuf));
287
	if (vflag && rti_info[RTAX_LABEL])
288
		printf(" %s", ((struct sockaddr_rtlabel *)
289
		    rti_info[RTAX_LABEL])->sr_label);
290
	putchar('\n');
291
}
292
293
/*
294
 * Print address family header before a section of the routing table.
295
 */
296
void
297
pr_family(int af)
298
{
299
	char *afname;
300
301
	switch (af) {
302
	case AF_INET:
303
		afname = "Internet";
304
		break;
305
	case AF_INET6:
306
		afname = "Internet6";
307
		break;
308
	case PF_KEY:
309
		afname = "Encap";
310
		break;
311
	case AF_MPLS:
312
		afname = "MPLS";
313
		break;
314
	default:
315
		afname = NULL;
316
		break;
317
	}
318
	if (afname)
319
		printf("\n%s:\n", afname);
320
	else
321
		printf("\nProtocol Family %d:\n", af);
322
}
323
324
void
325
p_addr(struct sockaddr *sa, struct sockaddr *mask, int flags)
326
{
327
	p_sockaddr(sa, mask, flags, WID_DST(sa->sa_family));
328
}
329
330
void
331
p_gwaddr(struct sockaddr *sa, int af)
332
{
333
	p_sockaddr(sa, 0, RTF_HOST, WID_GW(af));
334
}
335
336
void
337
p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
338
{
339
	char *cp;
340
341
	switch (sa->sa_family) {
342
	case AF_INET6:
343
	    {
344
		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
345
		struct in6_addr *in6 = &sa6->sin6_addr;
346
347
		/*
348
		 * XXX: This is a special workaround for KAME kernels.
349
		 * sin6_scope_id field of SA should be set in the future.
350
		 */
351
		if (IN6_IS_ADDR_LINKLOCAL(in6) ||
352
		    IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
353
		    IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
354
			/* XXX: override is ok? */
355
			sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
356
			    &in6->s6_addr[2]);
357
			*(u_short *)&in6->s6_addr[2] = 0;
358
		}
359
		if (flags & RTF_HOST)
360
			cp = routename((struct sockaddr *)sa6);
361
		else
362
			cp = netname((struct sockaddr *)sa6, mask);
363
		break;
364
	    }
365
	case AF_MPLS:
366
		return;
367
	default:
368
		if ((flags & RTF_HOST) || mask == NULL)
369
			cp = routename(sa);
370
		else
371
			cp = netname(sa, mask);
372
		break;
373
	}
374
	if (width < 0)
375
		printf("%s", cp);
376
	else {
377
		if (nflag)
378
			printf("%-*s ", width, cp);
379
		else
380
			printf("%-*.*s ", width, width, cp);
381
	}
382
}
383
384
static char line[HOST_NAME_MAX+1];
385
static char domain[HOST_NAME_MAX+1];
386
387
void
388
p_sockaddr_mpls(struct sockaddr *in, struct sockaddr *out, int flags, int width)
389
{
390
	if (in->sa_family != AF_MPLS)
391
		return;
392
393
	if (flags & MPLS_OP_POP || flags == MPLS_OP_LOCAL) {
394
		printf("%-*s ", width, label_print(in));
395
		printf("%-*s ", width, label_print(NULL));
396
	} else {
397
		printf("%-*s ", width, label_print(in));
398
		printf("%-*s ", width, label_print(out));
399
	}
400
401
	printf("%-6s ", mpls_op(flags));
402
}
403
404
void
405
p_flags(int f, char *format)
406
{
407
	char name[33], *flags;
408
	const struct bits *p = bits;
409
410
	for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
411
		if (p->b_mask & f)
412
			*flags++ = p->b_val;
413
	*flags = '\0';
414
	printf(format, name);
415
}
416
417
char *
418
routename(struct sockaddr *sa)
419
{
420
	char *cp = NULL;
421
	static int first = 1;
422
423
	if (first) {
424
		first = 0;
425
		if (gethostname(domain, sizeof(domain)) == 0 &&
426
		    (cp = strchr(domain, '.')))
427
			(void)strlcpy(domain, cp + 1, sizeof(domain));
428
		else
429
			domain[0] = '\0';
430
		cp = NULL;
431
	}
432
433
	if (sa->sa_len == 0) {
434
		(void)strlcpy(line, "default", sizeof(line));
435
		return (line);
436
	}
437
438
	switch (sa->sa_family) {
439
	case AF_INET:
440
		return
441
		    (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
442
443
	case AF_INET6:
444
	    {
445
		struct sockaddr_in6 sin6;
446
447
		memset(&sin6, 0, sizeof(sin6));
448
		memcpy(&sin6, sa, sa->sa_len);
449
		sin6.sin6_len = sizeof(struct sockaddr_in6);
450
		sin6.sin6_family = AF_INET6;
451
		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
452
		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
453
		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
454
		     IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
455
		    sin6.sin6_scope_id == 0) {
456
			sin6.sin6_scope_id =
457
			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
458
			sin6.sin6_addr.s6_addr[2] = 0;
459
			sin6.sin6_addr.s6_addr[3] = 0;
460
		}
461
		return (routename6(&sin6));
462
	    }
463
464
	case AF_LINK:
465
		return (link_print(sa));
466
	case AF_MPLS:
467
		return (label_print(sa));
468
	case AF_UNSPEC:
469
		if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
470
			static char name[RTLABEL_LEN];
471
			struct sockaddr_rtlabel *sr;
472
473
			sr = (struct sockaddr_rtlabel *)sa;
474
			(void)strlcpy(name, sr->sr_label, sizeof(name));
475
			return (name);
476
		}
477
		/* FALLTHROUGH */
478
	default:
479
		(void)snprintf(line, sizeof(line), "(%d) %s",
480
		    sa->sa_family, any_ntoa(sa));
481
		break;
482
	}
483
	return (line);
484
}
485
486
char *
487
routename4(in_addr_t in)
488
{
489
	char		*cp = NULL;
490
	struct in_addr	 ina;
491
	struct hostent	*hp;
492
493
	if (in == INADDR_ANY)
494
		cp = "default";
495
	if (!cp && !nflag) {
496
		if ((hp = gethostbyaddr((char *)&in,
497
		    sizeof(in), AF_INET)) != NULL) {
498
			if ((cp = strchr(hp->h_name, '.')) &&
499
			    !strcmp(cp + 1, domain))
500
				*cp = '\0';
501
			cp = hp->h_name;
502
		}
503
	}
504
	ina.s_addr = in;
505
	strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
506
507
	return (line);
508
}
509
510
char *
511
routename6(struct sockaddr_in6 *sin6)
512
{
513
	int	 niflags = 0;
514
515
	if (nflag)
516
		niflags |= NI_NUMERICHOST;
517
	else
518
		niflags |= NI_NOFQDN;
519
520
	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
521
	    line, sizeof(line), NULL, 0, niflags) != 0)
522
		strncpy(line, "invalid", sizeof(line));
523
524
	return (line);
525
}
526
527
/*
528
 * Return the name of the network whose address is given.
529
 * The address is assumed to be that of a net or subnet, not a host.
530
 */
531
char *
532
netname4(in_addr_t in, in_addr_t mask)
533
{
534
	int mbits;
535
536
	in = ntohl(in);
537
	mask = ntohl(mask);
538
	mbits = mask ? 33 - ffs(mask) : 0;
539
	if (in == INADDR_ANY && mask == INADDR_ANY)
540
		strlcpy(line, "default", sizeof(line));
541
#define C(x)	((x) & 0xff)
542
	else if (mbits < 9)
543
		snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
544
	else if (mbits < 17)
545
		snprintf(line, sizeof(line), "%u.%u/%d",
546
		    C(in >> 24) , C(in >> 16), mbits);
547
	else if (mbits < 25)
548
		snprintf(line, sizeof(line), "%u.%u.%u/%d",
549
		    C(in >> 24), C(in >> 16), C(in >> 8), mbits);
550
	else
551
		snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
552
		    C(in >> 16), C(in >> 8), C(in), mbits);
553
#undef C
554
	return (line);
555
}
556
557
char *
558
netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
559
{
560
	struct sockaddr_in6 sin6;
561
	u_char *p;
562
	int masklen, final = 0, illegal = 0;
563
	int i, lim, flag, error;
564
	char hbuf[NI_MAXHOST];
565
566
	sin6 = *sa6;
567
568
	flag = 0;
569
	masklen = 0;
570
	if (mask) {
571
		lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
572
		lim = lim < (int)sizeof(struct in6_addr) ?
573
		    lim : (int)sizeof(struct in6_addr);
574
		for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
575
			if (final && *p) {
576
				illegal++;
577
				sin6.sin6_addr.s6_addr[i++] = 0x00;
578
				continue;
579
			}
580
581
			switch (*p & 0xff) {
582
			case 0xff:
583
				masklen += 8;
584
				break;
585
			case 0xfe:
586
				masklen += 7;
587
				final++;
588
				break;
589
			case 0xfc:
590
				masklen += 6;
591
				final++;
592
				break;
593
			case 0xf8:
594
				masklen += 5;
595
				final++;
596
				break;
597
			case 0xf0:
598
				masklen += 4;
599
				final++;
600
				break;
601
			case 0xe0:
602
				masklen += 3;
603
				final++;
604
				break;
605
			case 0xc0:
606
				masklen += 2;
607
				final++;
608
				break;
609
			case 0x80:
610
				masklen += 1;
611
				final++;
612
				break;
613
			case 0x00:
614
				final++;
615
				break;
616
			default:
617
				final++;
618
				illegal++;
619
				break;
620
			}
621
622
			if (!illegal)
623
				sin6.sin6_addr.s6_addr[i++] &= *p;
624
			else
625
				sin6.sin6_addr.s6_addr[i++] = 0x00;
626
		}
627
		while (i < (int)sizeof(struct in6_addr))
628
			sin6.sin6_addr.s6_addr[i++] = 0x00;
629
	} else
630
		masklen = 128;
631
632
	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
633
		return ("default");
634
635
	if (illegal)
636
		warnx("illegal prefixlen");
637
638
	if (nflag)
639
		flag |= NI_NUMERICHOST;
640
	error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
641
	    hbuf, sizeof(hbuf), NULL, 0, flag);
642
	if (error)
643
		snprintf(hbuf, sizeof(hbuf), "invalid");
644
645
	snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
646
	return (line);
647
}
648
649
/*
650
 * Return the name of the network whose address is given.
651
 * The address is assumed to be that of a net or subnet, not a host.
652
 */
653
char *
654
netname(struct sockaddr *sa, struct sockaddr *mask)
655
{
656
	switch (sa->sa_family) {
657
	case AF_INET:
658
		return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
659
		    mask->sa_len == 0 ? 0 :
660
		    ((struct sockaddr_in *)mask)->sin_addr.s_addr);
661
	case AF_INET6:
662
		return netname6((struct sockaddr_in6 *)sa,
663
		    (struct sockaddr_in6 *)mask);
664
	case AF_LINK:
665
		return (link_print(sa));
666
	case AF_MPLS:
667
		return (label_print(sa));
668
	default:
669
		snprintf(line, sizeof(line), "af %d: %s",
670
		    sa->sa_family, any_ntoa(sa));
671
		break;
672
	}
673
	return (line);
674
}
675
676
static const char hexlist[] = "0123456789abcdef";
677
678
char *
679
any_ntoa(const struct sockaddr *sa)
680
{
681
	static char obuf[240];
682
	const char *in = sa->sa_data;
683
	char *out = obuf;
684
	int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
685
686
	*out++ = 'Q';
687
	do {
688
		*out++ = hexlist[(*in >> 4) & 15];
689
		*out++ = hexlist[(*in++)    & 15];
690
		*out++ = '.';
691
	} while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
692
	out[-1] = '\0';
693
	return (obuf);
694
}
695
696
char *
697
link_print(struct sockaddr *sa)
698
{
699
	struct sockaddr_dl	*sdl = (struct sockaddr_dl *)sa;
700
	u_char			*lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
701
702
	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
703
	    sdl->sdl_slen == 0) {
704
		(void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
705
		return (line);
706
	}
707
	switch (sdl->sdl_type) {
708
	case IFT_ETHER:
709
	case IFT_CARP:
710
		return (ether_ntoa((struct ether_addr *)lla));
711
	default:
712
		return (link_ntoa(sdl));
713
	}
714
}
715
716
char *
717
mpls_op(u_int32_t type)
718
{
719
	switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
720
	case MPLS_OP_LOCAL:
721
		return ("LOCAL");
722
	case MPLS_OP_POP:
723
		return ("POP");
724
	case MPLS_OP_SWAP:
725
		return ("SWAP");
726
	case MPLS_OP_PUSH:
727
		return ("PUSH");
728
	default:
729
		return ("?");
730
	}
731
}
732
733
char *
734
label_print(struct sockaddr *sa)
735
{
736
	struct sockaddr_mpls	*smpls = (struct sockaddr_mpls *)sa;
737
738
	if (smpls)
739
		(void)snprintf(line, sizeof(line), "%u",
740
		    ntohl(smpls->smpls_label) >> MPLS_LABEL_OFFSET);
741
	else
742
		(void)snprintf(line, sizeof(line), "-");
743
744
	return (line);
745
}