Annotation of src/usr.bin/netstat/show.c, Revision 1.45
1.45 ! reyk 1: /* $OpenBSD: show.c,v 1.44 2015/01/16 06:40:10 deraadt Exp $ */
1.1 claudio 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>
1.13 claudio 42: #include <netmpls/mpls.h>
1.1 claudio 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>
1.44 deraadt 53: #include <limits.h>
1.1 claudio 54:
55: #include "netstat.h"
56:
57: char *any_ntoa(const struct sockaddr *);
58: char *link_print(struct sockaddr *);
1.30 claudio 59: char *label_print(struct sockaddr *);
1.1 claudio 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_MASK, 'm' }, /* Mask Present -- for routing messages only */
82: { RTF_CLONING, 'C' },
83: { RTF_XRESOLVE, 'X' },
84: { RTF_LLINFO, 'L' },
85: { RTF_STATIC, 'S' },
86: { RTF_PROTO1, '1' },
87: { RTF_PROTO2, '2' },
88: { RTF_PROTO3, '3' },
89: { RTF_CLONED, 'c' },
1.18 claudio 90: { RTF_MPATH, 'P' },
1.25 michele 91: { RTF_MPLS, 'T' },
1.42 mpi 92: { RTF_LOCAL, 'l' },
93: { RTF_BROADCAST, 'b' },
1.1 claudio 94: { 0 }
95: };
96:
1.41 claudio 97: int WID_DST(int);
1.1 claudio 98: void p_rtentry(struct rt_msghdr *);
99: void pr_family(int);
100: void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
1.17 michele 101: void p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
1.1 claudio 102: void p_flags(int, char *);
103: char *routename4(in_addr_t);
104: char *routename6(struct sockaddr_in6 *);
105:
106: /*
107: * Print routing tables.
108: */
109: void
1.35 mikeb 110: p_rttables(int af, u_int tableid)
1.1 claudio 111: {
112: struct rt_msghdr *rtm;
113: char *buf = NULL, *next, *lim = NULL;
114: size_t needed;
1.34 claudio 115: int mib[7], mcnt;
1.1 claudio 116: struct sockaddr *sa;
117:
118: mib[0] = CTL_NET;
119: mib[1] = PF_ROUTE;
120: mib[2] = 0;
121: mib[3] = af;
122: mib[4] = NET_RT_DUMP;
123: mib[5] = 0;
1.35 mikeb 124: mib[6] = tableid;
125: mcnt = 7;
1.38 deraadt 126: while (1) {
127: if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
128: err(1, "route-sysctl-estimate");
129: if (needed == 0)
130: break;
131: if ((buf = realloc(buf, needed)) == NULL)
1.1 claudio 132: err(1, NULL);
1.38 deraadt 133: if (sysctl(mib, mcnt, buf, &needed, NULL, 0) == -1) {
134: if (errno == ENOMEM)
135: continue;
1.1 claudio 136: err(1, "sysctl of routing table");
1.38 deraadt 137: }
1.1 claudio 138: lim = buf + needed;
1.38 deraadt 139: break;
1.1 claudio 140: }
141:
142: printf("Routing tables\n");
143:
144: if (buf) {
145: for (next = buf; next < lim; next += rtm->rtm_msglen) {
146: rtm = (struct rt_msghdr *)next;
1.8 claudio 147: if (rtm->rtm_version != RTM_VERSION)
148: continue;
1.23 chris 149: sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1.1 claudio 150: if (af != AF_UNSPEC && sa->sa_family != af)
151: continue;
152: p_rtentry(rtm);
153: }
154: free(buf);
155: buf = NULL;
156: }
157: }
158:
1.12 deraadt 159: /*
1.1 claudio 160: * column widths; each followed by one space
161: * width of destination/gateway column
162: * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
163: */
164: #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
165:
1.41 claudio 166: int
1.13 claudio 167: WID_DST(int af)
168: {
169:
170: if (nflag)
171: switch (af) {
172: case AF_MPLS:
1.30 claudio 173: return 9;
1.13 claudio 174: case AF_INET6:
175: return 34;
176: default:
177: return 18;
178: }
179: else
180: switch (af) {
181: case AF_MPLS:
1.30 claudio 182: return 9;
1.13 claudio 183: default:
184: return 18;
185: }
186: }
187:
1.1 claudio 188: /*
189: * Print header for routing table columns.
190: */
191: void
192: pr_rthdr(int af, int Aflag)
193: {
194: if (Aflag)
195: printf("%-*.*s ", PLEN, PLEN, "Address");
1.13 claudio 196: switch (af) {
197: case PF_KEY:
1.1 claudio 198: printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
199: "Source", "Port", "Destination",
200: "Port", "Proto", "SA(Address/Proto/Type/Direction)");
1.13 claudio 201: break;
202: case PF_MPLS:
1.30 claudio 203: printf("%-9s %-9s %-6s %-18s %-6.6s %5.5s %8.8s %5.5s %4.4s %s\n",
1.13 claudio 204: "In label", "Out label", "Op", "Gateway",
205: "Flags", "Refs", "Use", "Mtu", "Prio", "Interface");
206: break;
207: default:
1.32 bluhm 208: printf("%-*.*s %-*.*s %-6.6s %5.5s %8.8s %5.5s %4.4s %s",
1.13 claudio 209: WID_DST(af), WID_DST(af), "Destination",
210: WID_GW(af), WID_GW(af), "Gateway",
211: "Flags", "Refs", "Use", "Mtu", "Prio", "Iface");
1.33 bluhm 212: if (vflag && !Aflag)
1.32 bluhm 213: printf(" %s", "Label");
214: putchar('\n');
1.13 claudio 215: break;
216: }
1.1 claudio 217: }
218:
219: static void
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
238: p_rtentry(struct rt_msghdr *rtm)
239: {
240: static int old_af = -1;
1.22 chris 241: struct sockaddr *sa = (struct sockaddr *)((char *)rtm + rtm->rtm_hdrlen);
1.1 claudio 242: struct sockaddr *mask, *rti_info[RTAX_MAX];
243: char ifbuf[IF_NAMESIZE];
244:
245: if (sa->sa_family == AF_KEY)
246: return;
247:
1.5 pyr 248: get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1.41 claudio 249:
1.5 pyr 250: if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
251: return;
252: }
1.1 claudio 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));
1.19 michele 264: p_sockaddr_mpls(sa, rti_info[RTAX_SRC], rtm->rtm_mpls,
1.17 michele 265: WID_DST(sa->sa_family));
266:
1.1 claudio 267: p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
268: WID_GW(sa->sa_family));
1.30 claudio 269:
1.1 claudio 270: p_flags(rtm->rtm_flags, "%-6.6s ");
1.13 claudio 271: printf("%5u %8llu ", rtm->rtm_rmx.rmx_refcnt,
1.1 claudio 272: rtm->rtm_rmx.rmx_pksent);
273: if (rtm->rtm_rmx.rmx_mtu)
1.13 claudio 274: printf("%5u ", rtm->rtm_rmx.rmx_mtu);
1.1 claudio 275: else
1.13 claudio 276: printf("%5s ", "-");
1.1 claudio 277: putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
1.32 bluhm 278: printf(" %2d %-5.16s", rtm->rtm_priority,
1.13 claudio 279: if_indextoname(rtm->rtm_index, ifbuf));
1.32 bluhm 280: if (vflag && rti_info[RTAX_LABEL])
281: printf(" %s", ((struct sockaddr_rtlabel *)
282: rti_info[RTAX_LABEL])->sr_label);
1.1 claudio 283: putchar('\n');
284: }
285:
286: /*
287: * Print address family header before a section of the routing table.
288: */
289: void
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;
1.13 claudio 304: case AF_MPLS:
305: afname = "MPLS";
306: break;
1.1 claudio 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
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
324: p_gwaddr(struct sockaddr *sa, int af)
325: {
326: p_sockaddr(sa, 0, RTF_HOST, WID_GW(af));
327: }
328:
329: void
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: struct in6_addr *in6 = &sa6->sin6_addr;
339:
340: /*
341: * XXX: This is a special workaround for KAME kernels.
342: * sin6_scope_id field of SA should be set in the future.
343: */
344: if (IN6_IS_ADDR_LINKLOCAL(in6) ||
1.3 itojun 345: IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
346: IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
1.1 claudio 347: /* XXX: override is ok? */
348: sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
349: &in6->s6_addr[2]);
350: *(u_short *)&in6->s6_addr[2] = 0;
351: }
352: if (flags & RTF_HOST)
353: cp = routename((struct sockaddr *)sa6);
354: else
355: cp = netname((struct sockaddr *)sa6, mask);
356: break;
357: }
1.16 michele 358: case AF_MPLS:
1.17 michele 359: return;
1.1 claudio 360: default:
361: if ((flags & RTF_HOST) || mask == NULL)
362: cp = routename(sa);
363: else
364: cp = netname(sa, mask);
365: break;
366: }
367: if (width < 0)
368: printf("%s", cp);
369: else {
370: if (nflag)
371: printf("%-*s ", width, cp);
372: else
373: printf("%-*.*s ", width, width, cp);
374: }
375: }
376:
1.44 deraadt 377: static char line[HOST_NAME_MAX+1];
378: static char domain[HOST_NAME_MAX+1];
1.17 michele 379:
380: void
381: p_sockaddr_mpls(struct sockaddr *in, struct sockaddr *out, int flags, int width)
382: {
383: if (in->sa_family != AF_MPLS)
384: return;
385:
1.30 claudio 386: if (flags & MPLS_OP_POP || flags == MPLS_OP_LOCAL) {
387: printf("%-*s ", width, label_print(in));
388: printf("%-*s ", width, label_print(NULL));
389: } else {
390: printf("%-*s ", width, label_print(in));
391: printf("%-*s ", width, label_print(out));
392: }
1.17 michele 393:
1.30 claudio 394: printf("%-6s ", mpls_op(flags));
1.17 michele 395: }
396:
1.1 claudio 397: void
398: p_flags(int f, char *format)
399: {
400: char name[33], *flags;
401: const struct bits *p = bits;
402:
403: for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
404: if (p->b_mask & f)
405: *flags++ = p->b_val;
406: *flags = '\0';
407: printf(format, name);
408: }
409:
410: char *
411: routename(struct sockaddr *sa)
412: {
413: char *cp = NULL;
414: static int first = 1;
415:
416: if (first) {
417: first = 0;
418: if (gethostname(domain, sizeof(domain)) == 0 &&
419: (cp = strchr(domain, '.')))
420: (void)strlcpy(domain, cp + 1, sizeof(domain));
421: else
422: domain[0] = '\0';
423: cp = NULL;
424: }
425:
426: if (sa->sa_len == 0) {
427: (void)strlcpy(line, "default", sizeof(line));
428: return (line);
429: }
430:
431: switch (sa->sa_family) {
432: case AF_INET:
433: return
434: (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
435:
436: case AF_INET6:
437: {
438: struct sockaddr_in6 sin6;
439:
440: memset(&sin6, 0, sizeof(sin6));
441: memcpy(&sin6, sa, sa->sa_len);
442: sin6.sin6_len = sizeof(struct sockaddr_in6);
443: sin6.sin6_family = AF_INET6;
444: if (sa->sa_len == sizeof(struct sockaddr_in6) &&
445: (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3 itojun 446: IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
447: IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1 claudio 448: sin6.sin6_scope_id == 0) {
449: sin6.sin6_scope_id =
450: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
451: sin6.sin6_addr.s6_addr[2] = 0;
452: sin6.sin6_addr.s6_addr[3] = 0;
453: }
454: return (routename6(&sin6));
455: }
456:
457: case AF_LINK:
458: return (link_print(sa));
1.13 claudio 459: case AF_MPLS:
1.30 claudio 460: return (label_print(sa));
1.1 claudio 461: case AF_UNSPEC:
462: if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
463: static char name[RTLABEL_LEN];
464: struct sockaddr_rtlabel *sr;
465:
466: sr = (struct sockaddr_rtlabel *)sa;
1.9 claudio 467: (void)strlcpy(name, sr->sr_label, sizeof(name));
1.1 claudio 468: return (name);
469: }
470: /* FALLTHROUGH */
471: default:
472: (void)snprintf(line, sizeof(line), "(%d) %s",
473: sa->sa_family, any_ntoa(sa));
474: break;
475: }
476: return (line);
477: }
478:
479: char *
480: routename4(in_addr_t in)
481: {
482: char *cp = NULL;
483: struct in_addr ina;
484: struct hostent *hp;
485:
486: if (in == INADDR_ANY)
487: cp = "default";
488: if (!cp && !nflag) {
489: if ((hp = gethostbyaddr((char *)&in,
490: sizeof(in), AF_INET)) != NULL) {
491: if ((cp = strchr(hp->h_name, '.')) &&
492: !strcmp(cp + 1, domain))
493: *cp = '\0';
494: cp = hp->h_name;
495: }
496: }
497: ina.s_addr = in;
498: strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
499:
500: return (line);
501: }
502:
503: char *
504: routename6(struct sockaddr_in6 *sin6)
505: {
506: int niflags = 0;
507:
508: if (nflag)
509: niflags |= NI_NUMERICHOST;
510: else
511: niflags |= NI_NOFQDN;
512:
513: if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
514: line, sizeof(line), NULL, 0, niflags) != 0)
515: strncpy(line, "invalid", sizeof(line));
516:
517: return (line);
518: }
519:
520: /*
521: * Return the name of the network whose address is given.
522: * The address is assumed to be that of a net or subnet, not a host.
523: */
524: char *
525: netname4(in_addr_t in, in_addr_t mask)
526: {
527: int mbits;
528:
529: in = ntohl(in);
530: mask = ntohl(mask);
1.43 schwarze 531: mbits = mask ? 33 - ffs(mask) : 0;
1.28 dlg 532: if (in == INADDR_ANY && mask == INADDR_ANY)
1.43 schwarze 533: strlcpy(line, "default", sizeof(line));
1.1 claudio 534: #define C(x) ((x) & 0xff)
535: else if (mbits < 9)
536: snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
537: else if (mbits < 17)
538: snprintf(line, sizeof(line), "%u.%u/%d",
539: C(in >> 24) , C(in >> 16), mbits);
540: else if (mbits < 25)
541: snprintf(line, sizeof(line), "%u.%u.%u/%d",
542: C(in >> 24), C(in >> 16), C(in >> 8), mbits);
543: else
544: snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
545: C(in >> 16), C(in >> 8), C(in), mbits);
546: #undef C
547: return (line);
548: }
549:
550: char *
551: netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
552: {
553: struct sockaddr_in6 sin6;
554: u_char *p;
555: int masklen, final = 0, illegal = 0;
556: int i, lim, flag, error;
557: char hbuf[NI_MAXHOST];
558:
559: sin6 = *sa6;
560:
561: flag = 0;
562: masklen = 0;
563: if (mask) {
564: lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1.4 claudio 565: lim = lim < (int)sizeof(struct in6_addr) ?
1.41 claudio 566: lim : (int)sizeof(struct in6_addr);
1.1 claudio 567: for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
568: if (final && *p) {
569: illegal++;
570: sin6.sin6_addr.s6_addr[i++] = 0x00;
571: continue;
572: }
573:
574: switch (*p & 0xff) {
575: case 0xff:
576: masklen += 8;
577: break;
578: case 0xfe:
579: masklen += 7;
580: final++;
581: break;
582: case 0xfc:
583: masklen += 6;
584: final++;
585: break;
586: case 0xf8:
587: masklen += 5;
588: final++;
589: break;
590: case 0xf0:
591: masklen += 4;
592: final++;
593: break;
594: case 0xe0:
595: masklen += 3;
596: final++;
597: break;
598: case 0xc0:
599: masklen += 2;
600: final++;
601: break;
602: case 0x80:
603: masklen += 1;
604: final++;
605: break;
606: case 0x00:
607: final++;
608: break;
609: default:
610: final++;
611: illegal++;
612: break;
613: }
614:
615: if (!illegal)
616: sin6.sin6_addr.s6_addr[i++] &= *p;
617: else
618: sin6.sin6_addr.s6_addr[i++] = 0x00;
619: }
1.41 claudio 620: while (i < (int)sizeof(struct in6_addr))
1.1 claudio 621: sin6.sin6_addr.s6_addr[i++] = 0x00;
622: } else
623: masklen = 128;
624:
625: if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
626: return ("default");
627:
628: if (illegal)
629: warnx("illegal prefixlen");
630:
631: if (nflag)
632: flag |= NI_NUMERICHOST;
633: error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
634: hbuf, sizeof(hbuf), NULL, 0, flag);
635: if (error)
636: snprintf(hbuf, sizeof(hbuf), "invalid");
637:
638: snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
639: return (line);
640: }
641:
642: /*
643: * Return the name of the network whose address is given.
644: * The address is assumed to be that of a net or subnet, not a host.
645: */
646: char *
647: netname(struct sockaddr *sa, struct sockaddr *mask)
648: {
649: switch (sa->sa_family) {
650: case AF_INET:
651: return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
1.29 claudio 652: mask->sa_len == 0 ? 0 :
1.1 claudio 653: ((struct sockaddr_in *)mask)->sin_addr.s_addr);
654: case AF_INET6:
655: return netname6((struct sockaddr_in6 *)sa,
656: (struct sockaddr_in6 *)mask);
657: case AF_LINK:
658: return (link_print(sa));
1.13 claudio 659: case AF_MPLS:
1.30 claudio 660: return (label_print(sa));
1.1 claudio 661: default:
662: snprintf(line, sizeof(line), "af %d: %s",
663: sa->sa_family, any_ntoa(sa));
664: break;
665: }
666: return (line);
667: }
668:
669: static const char hexlist[] = "0123456789abcdef";
670:
671: char *
672: any_ntoa(const struct sockaddr *sa)
673: {
674: static char obuf[240];
675: const char *in = sa->sa_data;
676: char *out = obuf;
677: int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
678:
679: *out++ = 'Q';
680: do {
681: *out++ = hexlist[(*in >> 4) & 15];
682: *out++ = hexlist[(*in++) & 15];
683: *out++ = '.';
684: } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
685: out[-1] = '\0';
686: return (obuf);
687: }
688:
689: char *
690: link_print(struct sockaddr *sa)
691: {
692: struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
693: u_char *lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
694:
695: if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
696: sdl->sdl_slen == 0) {
697: (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
698: return (line);
699: }
700: switch (sdl->sdl_type) {
701: case IFT_ETHER:
702: case IFT_CARP:
703: return (ether_ntoa((struct ether_addr *)lla));
704: default:
705: return (link_ntoa(sdl));
706: }
1.13 claudio 707: }
708:
709: char *
1.30 claudio 710: mpls_op(u_int32_t type)
1.13 claudio 711: {
1.16 michele 712: switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
1.21 michele 713: case MPLS_OP_LOCAL:
714: return ("LOCAL");
1.13 claudio 715: case MPLS_OP_POP:
716: return ("POP");
717: case MPLS_OP_SWAP:
718: return ("SWAP");
719: case MPLS_OP_PUSH:
720: return ("PUSH");
721: default:
722: return ("?");
723: }
724: }
725:
726: char *
1.30 claudio 727: label_print(struct sockaddr *sa)
1.13 claudio 728: {
1.30 claudio 729: struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
1.13 claudio 730:
1.30 claudio 731: if (smpls)
732: (void)snprintf(line, sizeof(line), "%u",
733: ntohl(smpls->smpls_label) >> MPLS_LABEL_OFFSET);
734: else
735: (void)snprintf(line, sizeof(line), "-");
1.13 claudio 736:
737: return (line);
1.1 claudio 738: }