Annotation of src/usr.bin/netstat/show.c, Revision 1.58
1.58 ! florian 1: /* $OpenBSD: show.c,v 1.57 2021/01/16 17:42:52 claudio 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_DYNAMIC, 'D' },
78: { RTF_MODIFIED, 'M' },
79: { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
80: { RTF_CLONING, 'C' },
1.50 mpi 81: { RTF_MULTICAST,'m' },
1.1 claudio 82: { RTF_LLINFO, 'L' },
83: { RTF_STATIC, 'S' },
1.51 bluhm 84: { RTF_BLACKHOLE,'B' },
85: { RTF_PROTO3, '3' },
86: { RTF_PROTO2, '2' },
1.1 claudio 87: { RTF_PROTO1, '1' },
88: { RTF_CLONED, 'c' },
1.51 bluhm 89: { RTF_CACHED, 'h' },
1.18 claudio 90: { RTF_MPATH, 'P' },
1.25 michele 91: { RTF_MPLS, 'T' },
1.42 mpi 92: { RTF_LOCAL, 'l' },
1.52 phessler 93: { RTF_BFD, 'F' },
1.51 bluhm 94: { RTF_BROADCAST,'b' },
95: { RTF_CONNECTED,'n' },
1.1 claudio 96: { 0 }
97: };
98:
1.41 claudio 99: int WID_DST(int);
1.1 claudio 100: void p_rtentry(struct rt_msghdr *);
101: void pr_family(int);
102: void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
1.17 michele 103: void p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
1.1 claudio 104: void p_flags(int, char *);
105: char *routename4(in_addr_t);
106: char *routename6(struct sockaddr_in6 *);
107:
1.46 claudio 108: size_t
109: get_sysctl(const int *mib, u_int mcnt, char **buf)
110: {
111: size_t needed;
112:
113: while (1) {
114: if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
115: err(1, "sysctl-estimate");
116: if (needed == 0)
117: break;
118: if ((*buf = realloc(*buf, needed)) == NULL)
119: err(1, NULL);
120: if (sysctl(mib, mcnt, *buf, &needed, NULL, 0) == -1) {
121: if (errno == ENOMEM)
122: continue;
123: err(1, "sysctl");
124: }
125: break;
126: }
127:
128: return needed;
129: }
130:
1.1 claudio 131: /*
132: * Print routing tables.
133: */
134: void
1.35 mikeb 135: p_rttables(int af, u_int tableid)
1.1 claudio 136: {
137: struct rt_msghdr *rtm;
138: char *buf = NULL, *next, *lim = NULL;
139: size_t needed;
1.34 claudio 140: int mib[7], mcnt;
1.1 claudio 141:
142: mib[0] = CTL_NET;
143: mib[1] = PF_ROUTE;
144: mib[2] = 0;
145: mib[3] = af;
146: mib[4] = NET_RT_DUMP;
147: mib[5] = 0;
1.35 mikeb 148: mib[6] = tableid;
149: mcnt = 7;
1.46 claudio 150:
151: needed = get_sysctl(mib, mcnt, &buf);
152: lim = buf + needed;
1.1 claudio 153:
154: printf("Routing tables\n");
155:
156: if (buf) {
157: for (next = buf; next < lim; next += rtm->rtm_msglen) {
158: rtm = (struct rt_msghdr *)next;
1.8 claudio 159: if (rtm->rtm_version != RTM_VERSION)
160: continue;
1.1 claudio 161: p_rtentry(rtm);
162: }
163: }
1.47 claudio 164: free(buf);
1.1 claudio 165: }
166:
1.12 deraadt 167: /*
1.1 claudio 168: * column widths; each followed by one space
169: * width of destination/gateway column
170: * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
171: */
1.56 pamela 172: #define WID_GW(af) ((af) == AF_INET6 ? 30 : 18)
1.1 claudio 173:
1.41 claudio 174: int
1.13 claudio 175: WID_DST(int af)
176: {
177:
1.56 pamela 178: switch (af) {
179: case AF_MPLS:
180: return 9;
181: case AF_INET6:
182: return 34;
183: default:
184: return 18;
185: }
1.13 claudio 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;
1.58 ! florian 338: #ifdef __KAME__
1.1 claudio 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: */
1.57 claudio 345: if ((IN6_IS_ADDR_LINKLOCAL(in6) ||
1.3 itojun 346: IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
1.57 claudio 347: IN6_IS_ADDR_MC_INTFACELOCAL(in6)) &&
348: sa6->sin6_scope_id == 0) {
1.1 claudio 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: }
1.58 ! florian 354: #endif
1.1 claudio 355: if (flags & RTF_HOST)
356: cp = routename((struct sockaddr *)sa6);
357: else
358: cp = netname((struct sockaddr *)sa6, mask);
359: break;
360: }
1.16 michele 361: case AF_MPLS:
1.17 michele 362: return;
1.1 claudio 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:
1.44 deraadt 380: static char line[HOST_NAME_MAX+1];
381: static char domain[HOST_NAME_MAX+1];
1.17 michele 382:
383: void
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:
1.30 claudio 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: }
1.17 michele 396:
1.30 claudio 397: printf("%-6s ", mpls_op(flags));
1.17 michele 398: }
399:
1.1 claudio 400: void
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 *
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;
1.58 ! florian 446: #ifdef __KAME__
1.1 claudio 447: if (sa->sa_len == sizeof(struct sockaddr_in6) &&
448: (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3 itojun 449: IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
450: IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1 claudio 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: }
1.58 ! florian 457: #endif
1.1 claudio 458: return (routename6(&sin6));
459: }
460:
461: case AF_LINK:
462: return (link_print(sa));
1.13 claudio 463: case AF_MPLS:
1.30 claudio 464: return (label_print(sa));
1.1 claudio 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;
1.9 claudio 471: (void)strlcpy(name, sr->sr_label, sizeof(name));
1.1 claudio 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: {
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 *
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:
1.53 guenther 515: if (getnameinfo((struct sockaddr *)sin6, sizeof(*sin6),
1.1 claudio 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 *
527: netname4(in_addr_t in, in_addr_t mask)
528: {
529: int mbits;
530:
531: in = ntohl(in);
532: mask = ntohl(mask);
1.43 schwarze 533: mbits = mask ? 33 - ffs(mask) : 0;
1.28 dlg 534: if (in == INADDR_ANY && mask == INADDR_ANY)
1.43 schwarze 535: strlcpy(line, "default", sizeof(line));
1.1 claudio 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 *
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);
1.4 claudio 567: lim = lim < (int)sizeof(struct in6_addr) ?
1.41 claudio 568: lim : (int)sizeof(struct in6_addr);
1.1 claudio 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: }
1.41 claudio 622: while (i < (int)sizeof(struct in6_addr))
1.1 claudio 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;
1.53 guenther 635: error = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
1.1 claudio 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 *
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,
1.29 claudio 654: mask->sa_len == 0 ? 0 :
1.1 claudio 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));
1.13 claudio 661: case AF_MPLS:
1.30 claudio 662: return (label_print(sa));
1.1 claudio 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 *
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 *
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: }
1.13 claudio 709: }
710:
711: char *
1.30 claudio 712: mpls_op(u_int32_t type)
1.13 claudio 713: {
1.16 michele 714: switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
1.21 michele 715: case MPLS_OP_LOCAL:
716: return ("LOCAL");
1.13 claudio 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 *
1.30 claudio 729: label_print(struct sockaddr *sa)
1.13 claudio 730: {
1.30 claudio 731: struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
1.13 claudio 732:
1.30 claudio 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), "-");
1.13 claudio 738:
739: return (line);
1.1 claudio 740: }