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