Annotation of src/usr.bin/netstat/show.c, Revision 1.37
1.37 ! deraadt 1: /* $OpenBSD: show.c,v 1.36 2012/03/17 10:16:41 dlg 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/param.h>
34: #include <sys/protosw.h>
35: #include <sys/socket.h>
36: #include <sys/sysctl.h>
37:
38: #include <net/if.h>
39: #include <net/if_dl.h>
40: #include <net/if_types.h>
41: #include <net/pfkeyv2.h>
42: #include <net/route.h>
43: #include <netinet/in.h>
44: #include <netinet/if_ether.h>
45: #include <netinet/ip_ipsp.h>
1.13 claudio 46: #include <netmpls/mpls.h>
1.1 claudio 47: #include <arpa/inet.h>
48:
49: #include <err.h>
50: #include <errno.h>
51: #include <netdb.h>
52: #include <stdio.h>
53: #include <stddef.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <unistd.h>
57:
58: #include "netstat.h"
59:
60: char *any_ntoa(const struct sockaddr *);
61: char *link_print(struct sockaddr *);
1.30 claudio 62: char *label_print(struct sockaddr *);
1.1 claudio 63:
64: #define ROUNDUP(a) \
65: ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
66: #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
67:
68: #define PFKEYV2_CHUNK sizeof(u_int64_t)
69:
70: /*
71: * Definitions for showing gateway flags.
72: */
73: struct bits {
74: int b_mask;
75: char b_val;
76: };
77: static const struct bits bits[] = {
78: { RTF_UP, 'U' },
79: { RTF_GATEWAY, 'G' },
80: { RTF_HOST, 'H' },
81: { RTF_REJECT, 'R' },
82: { RTF_BLACKHOLE, 'B' },
83: { RTF_DYNAMIC, 'D' },
84: { RTF_MODIFIED, 'M' },
85: { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
86: { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
87: { RTF_CLONING, 'C' },
88: { RTF_XRESOLVE, 'X' },
89: { RTF_LLINFO, 'L' },
90: { RTF_STATIC, 'S' },
91: { RTF_PROTO1, '1' },
92: { RTF_PROTO2, '2' },
93: { RTF_PROTO3, '3' },
94: { RTF_CLONED, 'c' },
1.18 claudio 95: { RTF_MPATH, 'P' },
1.25 michele 96: { RTF_MPLS, 'T' },
1.1 claudio 97: { 0 }
98: };
99:
100: void p_rtentry(struct rt_msghdr *);
101: void p_pfkentry(struct sadb_msg *);
102: void pr_family(int);
103: void p_encap(struct sockaddr *, struct sockaddr *, int);
104: void p_protocol(struct sadb_protocol *, struct sockaddr *, struct
1.13 claudio 105: sadb_protocol *, int);
1.1 claudio 106: void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
1.17 michele 107: void p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
1.1 claudio 108: void p_flags(int, char *);
109: char *routename4(in_addr_t);
110: char *routename6(struct sockaddr_in6 *);
111: void index_pfk(struct sadb_msg *, void **);
112:
113: /*
114: * Print routing tables.
115: */
116: void
1.35 mikeb 117: p_rttables(int af, u_int tableid)
1.1 claudio 118: {
119: struct rt_msghdr *rtm;
120: struct sadb_msg *msg;
121: char *buf = NULL, *next, *lim = NULL;
122: size_t needed;
1.34 claudio 123: int mib[7], mcnt;
1.1 claudio 124: struct sockaddr *sa;
125:
126: mib[0] = CTL_NET;
127: mib[1] = PF_ROUTE;
128: mib[2] = 0;
129: mib[3] = af;
130: mib[4] = NET_RT_DUMP;
131: mib[5] = 0;
1.35 mikeb 132: mib[6] = tableid;
133: mcnt = 7;
1.10 claudio 134:
1.34 claudio 135: if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) < 0)
1.1 claudio 136: err(1, "route-sysctl-estimate");
137: if (needed > 0) {
138: if ((buf = malloc(needed)) == 0)
139: err(1, NULL);
1.34 claudio 140: if (sysctl(mib, mcnt, buf, &needed, NULL, 0) < 0)
1.1 claudio 141: err(1, "sysctl of routing table");
142: lim = buf + needed;
143: }
144:
145: printf("Routing tables\n");
146:
147: if (buf) {
148: for (next = buf; next < lim; next += rtm->rtm_msglen) {
149: rtm = (struct rt_msghdr *)next;
1.8 claudio 150: if (rtm->rtm_version != RTM_VERSION)
151: continue;
1.23 chris 152: sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
1.1 claudio 153: if (af != AF_UNSPEC && sa->sa_family != af)
154: continue;
155: p_rtentry(rtm);
156: }
157: free(buf);
158: buf = NULL;
159: }
160:
161: if (af != 0 && af != PF_KEY)
162: return;
163:
164: mib[0] = CTL_NET;
165: mib[1] = PF_KEY;
166: mib[2] = PF_KEY_V2;
167: mib[3] = NET_KEY_SPD_DUMP;
168: mib[4] = mib[5] = 0;
169:
170: if (sysctl(mib, 4, NULL, &needed, NULL, 0) == -1) {
171: if (errno == ENOPROTOOPT)
172: return;
173: err(1, "spd-sysctl-estimate");
174: }
175: if (needed > 0) {
176: if ((buf = malloc(needed)) == 0)
177: err(1, NULL);
178: if (sysctl(mib, 4, buf, &needed, NULL, 0) == -1)
179: err(1,"sysctl of spd");
180: lim = buf + needed;
181: }
182:
183: if (buf) {
184: printf("\nEncap:\n");
185:
186: for (next = buf; next < lim; next += msg->sadb_msg_len *
187: PFKEYV2_CHUNK) {
188: msg = (struct sadb_msg *)next;
189: if (msg->sadb_msg_len == 0)
190: break;
191: p_pfkentry(msg);
192: }
193: free(buf);
194: buf = NULL;
195: }
196: }
197:
1.12 deraadt 198: /*
1.1 claudio 199: * column widths; each followed by one space
200: * width of destination/gateway column
201: * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
202: */
203: #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
204:
1.13 claudio 205: int
206: WID_DST(int af)
207: {
208:
209: if (nflag)
210: switch (af) {
211: case AF_MPLS:
1.30 claudio 212: return 9;
1.13 claudio 213: case AF_INET6:
214: return 34;
215: default:
216: return 18;
217: }
218: else
219: switch (af) {
220: case AF_MPLS:
1.30 claudio 221: return 9;
1.13 claudio 222: default:
223: return 18;
224: }
225: }
226:
1.1 claudio 227: /*
228: * Print header for routing table columns.
229: */
230: void
231: pr_rthdr(int af, int Aflag)
232: {
233: if (Aflag)
234: printf("%-*.*s ", PLEN, PLEN, "Address");
1.13 claudio 235: switch (af) {
236: case PF_KEY:
1.1 claudio 237: printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
238: "Source", "Port", "Destination",
239: "Port", "Proto", "SA(Address/Proto/Type/Direction)");
1.13 claudio 240: break;
241: case PF_MPLS:
1.30 claudio 242: printf("%-9s %-9s %-6s %-18s %-6.6s %5.5s %8.8s %5.5s %4.4s %s\n",
1.13 claudio 243: "In label", "Out label", "Op", "Gateway",
244: "Flags", "Refs", "Use", "Mtu", "Prio", "Interface");
245: break;
246: default:
1.32 bluhm 247: printf("%-*.*s %-*.*s %-6.6s %5.5s %8.8s %5.5s %4.4s %s",
1.13 claudio 248: WID_DST(af), WID_DST(af), "Destination",
249: WID_GW(af), WID_GW(af), "Gateway",
250: "Flags", "Refs", "Use", "Mtu", "Prio", "Iface");
1.33 bluhm 251: if (vflag && !Aflag)
1.32 bluhm 252: printf(" %s", "Label");
253: putchar('\n');
1.13 claudio 254: break;
255: }
1.1 claudio 256: }
257:
258: static void
259: get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
260: {
261: int i;
262:
263: for (i = 0; i < RTAX_MAX; i++) {
264: if (addrs & (1 << i)) {
265: rti_info[i] = sa;
266: sa = (struct sockaddr *)((char *)(sa) +
267: ROUNDUP(sa->sa_len));
268: } else
269: rti_info[i] = NULL;
270: }
271: }
272:
273: /*
274: * Print a routing table entry.
275: */
276: void
277: p_rtentry(struct rt_msghdr *rtm)
278: {
279: static int old_af = -1;
1.22 chris 280: struct sockaddr *sa = (struct sockaddr *)((char *)rtm + rtm->rtm_hdrlen);
1.1 claudio 281: struct sockaddr *mask, *rti_info[RTAX_MAX];
282: char ifbuf[IF_NAMESIZE];
283:
284: if (sa->sa_family == AF_KEY)
285: return;
286:
1.5 pyr 287: get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
288: if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
289: return;
290: }
1.1 claudio 291: if (old_af != sa->sa_family) {
292: old_af = sa->sa_family;
293: pr_family(sa->sa_family);
294: pr_rthdr(sa->sa_family, 0);
295: }
296:
297: mask = rti_info[RTAX_NETMASK];
298: if ((sa = rti_info[RTAX_DST]) == NULL)
299: return;
300:
301: p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family));
1.19 michele 302: p_sockaddr_mpls(sa, rti_info[RTAX_SRC], rtm->rtm_mpls,
1.17 michele 303: WID_DST(sa->sa_family));
304:
1.1 claudio 305: p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
306: WID_GW(sa->sa_family));
1.30 claudio 307:
1.1 claudio 308: p_flags(rtm->rtm_flags, "%-6.6s ");
1.13 claudio 309: printf("%5u %8llu ", rtm->rtm_rmx.rmx_refcnt,
1.1 claudio 310: rtm->rtm_rmx.rmx_pksent);
311: if (rtm->rtm_rmx.rmx_mtu)
1.13 claudio 312: printf("%5u ", rtm->rtm_rmx.rmx_mtu);
1.1 claudio 313: else
1.13 claudio 314: printf("%5s ", "-");
1.1 claudio 315: putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
1.32 bluhm 316: printf(" %2d %-5.16s", rtm->rtm_priority,
1.13 claudio 317: if_indextoname(rtm->rtm_index, ifbuf));
1.32 bluhm 318: if (vflag && rti_info[RTAX_LABEL])
319: printf(" %s", ((struct sockaddr_rtlabel *)
320: rti_info[RTAX_LABEL])->sr_label);
1.1 claudio 321: putchar('\n');
322: }
323:
324: /*
325: * Print a pfkey/encap entry.
326: */
327: void
328: p_pfkentry(struct sadb_msg *msg)
329: {
1.13 claudio 330: static int old = 0;
1.1 claudio 331: struct sadb_address *saddr;
332: struct sadb_protocol *sap, *saft;
333: struct sockaddr *sa, *mask;
334: void *headers[SADB_EXT_MAX + 1];
335:
336: if (!old) {
337: pr_rthdr(PF_KEY, 0);
338: old++;
339: }
340:
341: bzero(headers, sizeof(headers));
342: index_pfk(msg, headers);
343:
344: /* These are always set */
345: saddr = headers[SADB_X_EXT_SRC_FLOW];
346: sa = (struct sockaddr *)(saddr + 1);
347: saddr = headers[SADB_X_EXT_SRC_MASK];
348: mask = (struct sockaddr *)(saddr + 1);
349: p_encap(sa, mask, WID_DST(sa->sa_family));
350:
351: /* These are always set, too. */
352: saddr = headers[SADB_X_EXT_DST_FLOW];
353: sa = (struct sockaddr *)(saddr + 1);
354: saddr = headers[SADB_X_EXT_DST_MASK];
355: mask = (struct sockaddr *)(saddr + 1);
356: p_encap(sa, mask, WID_DST(sa->sa_family));
357:
358: /* Bypass and deny flows do not set SADB_EXT_ADDRESS_DST! */
359: sap = headers[SADB_X_EXT_PROTOCOL];
360: saft = headers[SADB_X_EXT_FLOW_TYPE];
361: saddr = headers[SADB_EXT_ADDRESS_DST];
362: if (saddr)
363: sa = (struct sockaddr *)(saddr + 1);
364: else
365: sa = NULL;
366: p_protocol(sap, sa, saft, msg->sadb_msg_satype);
367:
368: printf("\n");
369: }
370:
371: /*
372: * Print address family header before a section of the routing table.
373: */
374: void
375: pr_family(int af)
376: {
377: char *afname;
378:
379: switch (af) {
380: case AF_INET:
381: afname = "Internet";
382: break;
383: case AF_INET6:
384: afname = "Internet6";
385: break;
386: case PF_KEY:
387: afname = "Encap";
388: break;
1.13 claudio 389: case AF_MPLS:
390: afname = "MPLS";
391: break;
1.1 claudio 392: case AF_APPLETALK:
393: afname = "AppleTalk";
394: break;
395: default:
396: afname = NULL;
397: break;
398: }
399: if (afname)
400: printf("\n%s:\n", afname);
401: else
402: printf("\nProtocol Family %d:\n", af);
403: }
404:
405: void
406: p_addr(struct sockaddr *sa, struct sockaddr *mask, int flags)
407: {
408: p_sockaddr(sa, mask, flags, WID_DST(sa->sa_family));
409: }
410:
411: void
412: p_gwaddr(struct sockaddr *sa, int af)
413: {
414: p_sockaddr(sa, 0, RTF_HOST, WID_GW(af));
415: }
416:
417: void
418: p_encap(struct sockaddr *sa, struct sockaddr *mask, int width)
419: {
1.13 claudio 420: char *cp;
421: unsigned short port = 0;
1.1 claudio 422:
1.2 todd 423: if (mask)
424: cp = netname(sa, mask);
425: else
426: cp = routename(sa);
1.1 claudio 427: switch (sa->sa_family) {
1.9 claudio 428: case AF_INET:
429: port = ntohs(((struct sockaddr_in *)sa)->sin_port);
430: break;
1.2 todd 431: case AF_INET6:
432: port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
1.1 claudio 433: break;
434: }
435: if (width < 0)
436: printf("%s", cp);
437: else {
438: if (nflag)
439: printf("%-*s %-5u ", width, cp, port);
440: else
441: printf("%-*.*s %-5u ", width, width, cp, port);
442: }
443: }
444:
445: void
446: p_protocol(struct sadb_protocol *sap, struct sockaddr *sa, struct sadb_protocol
447: *saft, int proto)
448: {
449: printf("%-6u", sap->sadb_protocol_proto);
1.9 claudio 450:
1.1 claudio 451: if (sa)
452: p_sockaddr(sa, NULL, 0, -1);
453: else
454: printf("none");
455:
456: switch (proto) {
457: case SADB_SATYPE_ESP:
458: printf("/esp");
459: break;
460: case SADB_SATYPE_AH:
461: printf("/ah");
462: break;
463: case SADB_X_SATYPE_IPCOMP:
464: printf("/ipcomp");
465: break;
466: case SADB_X_SATYPE_IPIP:
467: printf("/ipip");
468: break;
469: default:
470: printf("/<unknown>");
471: }
472:
473: switch(saft->sadb_protocol_proto) {
474: case SADB_X_FLOW_TYPE_USE:
475: printf("/use");
476: break;
477: case SADB_X_FLOW_TYPE_REQUIRE:
478: printf("/require");
479: break;
480: case SADB_X_FLOW_TYPE_ACQUIRE:
481: printf("/acquire");
482: break;
483: case SADB_X_FLOW_TYPE_DENY:
484: printf("/deny");
485: break;
486: case SADB_X_FLOW_TYPE_BYPASS:
487: printf("/bypass");
488: break;
489: case SADB_X_FLOW_TYPE_DONTACQ:
490: printf("/dontacq");
491: break;
492: default:
493: printf("/<unknown type>");
494: }
495:
496: switch(saft->sadb_protocol_direction) {
497: case IPSP_DIRECTION_IN:
498: printf("/in");
499: break;
500: case IPSP_DIRECTION_OUT:
501: printf("/out");
502: break;
503: default:
504: printf("/<unknown>");
505: }
506: }
507:
508: void
509: p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
510: {
511: char *cp;
512:
513: switch (sa->sa_family) {
514: case AF_INET6:
515: {
516: struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
517: struct in6_addr *in6 = &sa6->sin6_addr;
518:
519: /*
520: * XXX: This is a special workaround for KAME kernels.
521: * sin6_scope_id field of SA should be set in the future.
522: */
523: if (IN6_IS_ADDR_LINKLOCAL(in6) ||
1.3 itojun 524: IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
525: IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
1.1 claudio 526: /* XXX: override is ok? */
527: sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
528: &in6->s6_addr[2]);
529: *(u_short *)&in6->s6_addr[2] = 0;
530: }
531: if (flags & RTF_HOST)
532: cp = routename((struct sockaddr *)sa6);
533: else
534: cp = netname((struct sockaddr *)sa6, mask);
535: break;
536: }
1.16 michele 537: case AF_MPLS:
1.17 michele 538: return;
1.1 claudio 539: default:
540: if ((flags & RTF_HOST) || mask == NULL)
541: cp = routename(sa);
542: else
543: cp = netname(sa, mask);
544: break;
545: }
546: if (width < 0)
547: printf("%s", cp);
548: else {
549: if (nflag)
550: printf("%-*s ", width, cp);
551: else
552: printf("%-*.*s ", width, width, cp);
553: }
554: }
555:
1.17 michele 556: static char line[MAXHOSTNAMELEN];
557: static char domain[MAXHOSTNAMELEN];
558:
559: void
560: p_sockaddr_mpls(struct sockaddr *in, struct sockaddr *out, int flags, int width)
561: {
562: if (in->sa_family != AF_MPLS)
563: return;
564:
1.30 claudio 565: if (flags & MPLS_OP_POP || flags == MPLS_OP_LOCAL) {
566: printf("%-*s ", width, label_print(in));
567: printf("%-*s ", width, label_print(NULL));
568: } else {
569: printf("%-*s ", width, label_print(in));
570: printf("%-*s ", width, label_print(out));
571: }
1.17 michele 572:
1.30 claudio 573: printf("%-6s ", mpls_op(flags));
1.17 michele 574: }
575:
1.1 claudio 576: void
577: p_flags(int f, char *format)
578: {
579: char name[33], *flags;
580: const struct bits *p = bits;
581:
582: for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
583: if (p->b_mask & f)
584: *flags++ = p->b_val;
585: *flags = '\0';
586: printf(format, name);
587: }
588:
589: char *
590: routename(struct sockaddr *sa)
591: {
592: char *cp = NULL;
593: static int first = 1;
594:
595: if (first) {
596: first = 0;
597: if (gethostname(domain, sizeof(domain)) == 0 &&
598: (cp = strchr(domain, '.')))
599: (void)strlcpy(domain, cp + 1, sizeof(domain));
600: else
601: domain[0] = '\0';
602: cp = NULL;
603: }
604:
605: if (sa->sa_len == 0) {
606: (void)strlcpy(line, "default", sizeof(line));
607: return (line);
608: }
609:
610: switch (sa->sa_family) {
611: case AF_INET:
612: return
613: (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
614:
615: case AF_INET6:
616: {
617: struct sockaddr_in6 sin6;
618:
619: memset(&sin6, 0, sizeof(sin6));
620: memcpy(&sin6, sa, sa->sa_len);
621: sin6.sin6_len = sizeof(struct sockaddr_in6);
622: sin6.sin6_family = AF_INET6;
623: if (sa->sa_len == sizeof(struct sockaddr_in6) &&
624: (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3 itojun 625: IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
626: IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1 claudio 627: sin6.sin6_scope_id == 0) {
628: sin6.sin6_scope_id =
629: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
630: sin6.sin6_addr.s6_addr[2] = 0;
631: sin6.sin6_addr.s6_addr[3] = 0;
632: }
633: return (routename6(&sin6));
634: }
635:
636: case AF_LINK:
637: return (link_print(sa));
1.13 claudio 638: case AF_MPLS:
1.30 claudio 639: return (label_print(sa));
1.1 claudio 640: case AF_UNSPEC:
641: if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
642: static char name[RTLABEL_LEN];
643: struct sockaddr_rtlabel *sr;
644:
645: sr = (struct sockaddr_rtlabel *)sa;
1.9 claudio 646: (void)strlcpy(name, sr->sr_label, sizeof(name));
1.1 claudio 647: return (name);
648: }
649: /* FALLTHROUGH */
650: default:
651: (void)snprintf(line, sizeof(line), "(%d) %s",
652: sa->sa_family, any_ntoa(sa));
653: break;
654: }
655: return (line);
656: }
657:
658: char *
659: routename4(in_addr_t in)
660: {
661: char *cp = NULL;
662: struct in_addr ina;
663: struct hostent *hp;
664:
665: if (in == INADDR_ANY)
666: cp = "default";
667: if (!cp && !nflag) {
668: if ((hp = gethostbyaddr((char *)&in,
669: sizeof(in), AF_INET)) != NULL) {
670: if ((cp = strchr(hp->h_name, '.')) &&
671: !strcmp(cp + 1, domain))
672: *cp = '\0';
673: cp = hp->h_name;
674: }
675: }
676: ina.s_addr = in;
677: strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
678:
679: return (line);
680: }
681:
682: char *
683: routename6(struct sockaddr_in6 *sin6)
684: {
685: int niflags = 0;
686:
687: if (nflag)
688: niflags |= NI_NUMERICHOST;
689: else
690: niflags |= NI_NOFQDN;
691:
692: if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
693: line, sizeof(line), NULL, 0, niflags) != 0)
694: strncpy(line, "invalid", sizeof(line));
695:
696: return (line);
697: }
698:
699: /*
700: * Return the name of the network whose address is given.
701: * The address is assumed to be that of a net or subnet, not a host.
702: */
703: char *
704: netname4(in_addr_t in, in_addr_t mask)
705: {
706: char *cp = NULL;
707: struct netent *np = NULL;
708: int mbits;
709:
710: in = ntohl(in);
711: mask = ntohl(mask);
712: if (!nflag && in != INADDR_ANY) {
713: if ((np = getnetbyaddr(in, AF_INET)) != NULL)
714: cp = np->n_name;
715: }
1.28 dlg 716: if (in == INADDR_ANY && mask == INADDR_ANY)
1.1 claudio 717: cp = "default";
718: mbits = mask ? 33 - ffs(mask) : 0;
719: if (cp)
720: strlcpy(line, cp, sizeof(line));
721: #define C(x) ((x) & 0xff)
722: else if (mbits < 9)
723: snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
724: else if (mbits < 17)
725: snprintf(line, sizeof(line), "%u.%u/%d",
726: C(in >> 24) , C(in >> 16), mbits);
727: else if (mbits < 25)
728: snprintf(line, sizeof(line), "%u.%u.%u/%d",
729: C(in >> 24), C(in >> 16), C(in >> 8), mbits);
730: else
731: snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
732: C(in >> 16), C(in >> 8), C(in), mbits);
733: #undef C
734: return (line);
735: }
736:
737: char *
738: netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
739: {
740: struct sockaddr_in6 sin6;
741: u_char *p;
742: int masklen, final = 0, illegal = 0;
743: int i, lim, flag, error;
744: char hbuf[NI_MAXHOST];
745:
746: sin6 = *sa6;
747:
748: flag = 0;
749: masklen = 0;
750: if (mask) {
751: lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1.4 claudio 752: lim = lim < (int)sizeof(struct in6_addr) ?
1.1 claudio 753: lim : sizeof(struct in6_addr);
754: for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
755: if (final && *p) {
756: illegal++;
757: sin6.sin6_addr.s6_addr[i++] = 0x00;
758: continue;
759: }
760:
761: switch (*p & 0xff) {
762: case 0xff:
763: masklen += 8;
764: break;
765: case 0xfe:
766: masklen += 7;
767: final++;
768: break;
769: case 0xfc:
770: masklen += 6;
771: final++;
772: break;
773: case 0xf8:
774: masklen += 5;
775: final++;
776: break;
777: case 0xf0:
778: masklen += 4;
779: final++;
780: break;
781: case 0xe0:
782: masklen += 3;
783: final++;
784: break;
785: case 0xc0:
786: masklen += 2;
787: final++;
788: break;
789: case 0x80:
790: masklen += 1;
791: final++;
792: break;
793: case 0x00:
794: final++;
795: break;
796: default:
797: final++;
798: illegal++;
799: break;
800: }
801:
802: if (!illegal)
803: sin6.sin6_addr.s6_addr[i++] &= *p;
804: else
805: sin6.sin6_addr.s6_addr[i++] = 0x00;
806: }
807: while (i < sizeof(struct in6_addr))
808: sin6.sin6_addr.s6_addr[i++] = 0x00;
809: } else
810: masklen = 128;
811:
812: if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
813: return ("default");
814:
815: if (illegal)
816: warnx("illegal prefixlen");
817:
818: if (nflag)
819: flag |= NI_NUMERICHOST;
820: error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
821: hbuf, sizeof(hbuf), NULL, 0, flag);
822: if (error)
823: snprintf(hbuf, sizeof(hbuf), "invalid");
824:
825: snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
826: return (line);
827: }
828:
829: /*
830: * Return the name of the network whose address is given.
831: * The address is assumed to be that of a net or subnet, not a host.
832: */
833: char *
834: netname(struct sockaddr *sa, struct sockaddr *mask)
835: {
836: switch (sa->sa_family) {
837: case AF_INET:
838: return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
1.29 claudio 839: mask->sa_len == 0 ? 0 :
1.1 claudio 840: ((struct sockaddr_in *)mask)->sin_addr.s_addr);
841: case AF_INET6:
842: return netname6((struct sockaddr_in6 *)sa,
843: (struct sockaddr_in6 *)mask);
844: case AF_LINK:
845: return (link_print(sa));
1.13 claudio 846: case AF_MPLS:
1.30 claudio 847: return (label_print(sa));
1.1 claudio 848: default:
849: snprintf(line, sizeof(line), "af %d: %s",
850: sa->sa_family, any_ntoa(sa));
851: break;
852: }
853: return (line);
854: }
855:
856: static const char hexlist[] = "0123456789abcdef";
857:
858: char *
859: any_ntoa(const struct sockaddr *sa)
860: {
861: static char obuf[240];
862: const char *in = sa->sa_data;
863: char *out = obuf;
864: int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
865:
866: *out++ = 'Q';
867: do {
868: *out++ = hexlist[(*in >> 4) & 15];
869: *out++ = hexlist[(*in++) & 15];
870: *out++ = '.';
871: } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
872: out[-1] = '\0';
873: return (obuf);
874: }
875:
876: char *
877: link_print(struct sockaddr *sa)
878: {
879: struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
880: u_char *lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
881:
882: if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
883: sdl->sdl_slen == 0) {
884: (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
885: return (line);
886: }
887: switch (sdl->sdl_type) {
888: case IFT_ETHER:
889: case IFT_CARP:
890: return (ether_ntoa((struct ether_addr *)lla));
891: default:
892: return (link_ntoa(sdl));
893: }
1.13 claudio 894: }
895:
896: char *
1.30 claudio 897: mpls_op(u_int32_t type)
1.13 claudio 898: {
1.16 michele 899: switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
1.21 michele 900: case MPLS_OP_LOCAL:
901: return ("LOCAL");
1.13 claudio 902: case MPLS_OP_POP:
903: return ("POP");
904: case MPLS_OP_SWAP:
905: return ("SWAP");
906: case MPLS_OP_PUSH:
907: return ("PUSH");
908: default:
909: return ("?");
910: }
911: }
912:
913: char *
1.30 claudio 914: label_print(struct sockaddr *sa)
1.13 claudio 915: {
1.30 claudio 916: struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
1.13 claudio 917:
1.30 claudio 918: if (smpls)
919: (void)snprintf(line, sizeof(line), "%u",
920: ntohl(smpls->smpls_label) >> MPLS_LABEL_OFFSET);
921: else
922: (void)snprintf(line, sizeof(line), "-");
1.13 claudio 923:
924: return (line);
1.1 claudio 925: }
926:
927: void
928: index_pfk(struct sadb_msg *msg, void **headers)
929: {
930: struct sadb_ext *ext;
931:
932: for (ext = (struct sadb_ext *)(msg + 1);
933: (size_t)((u_int8_t *)ext - (u_int8_t *)msg) <
934: msg->sadb_msg_len * PFKEYV2_CHUNK && ext->sadb_ext_len > 0;
935: ext = (struct sadb_ext *)((u_int8_t *)ext +
936: ext->sadb_ext_len * PFKEYV2_CHUNK)) {
937: switch (ext->sadb_ext_type) {
938: case SADB_EXT_ADDRESS_SRC:
939: headers[SADB_EXT_ADDRESS_SRC] = (void *)ext;
940: break;
941: case SADB_EXT_ADDRESS_DST:
942: headers[SADB_EXT_ADDRESS_DST] = (void *)ext;
943: break;
944: case SADB_X_EXT_PROTOCOL:
945: headers[SADB_X_EXT_PROTOCOL] = (void *)ext;
946: break;
947: case SADB_X_EXT_SRC_FLOW:
948: headers[SADB_X_EXT_SRC_FLOW] = (void *)ext;
949: break;
950: case SADB_X_EXT_DST_FLOW:
951: headers[SADB_X_EXT_DST_FLOW] = (void *)ext;
952: break;
953: case SADB_X_EXT_SRC_MASK:
954: headers[SADB_X_EXT_SRC_MASK] = (void *)ext;
955: break;
956: case SADB_X_EXT_DST_MASK:
957: headers[SADB_X_EXT_DST_MASK] = (void *)ext;
958: break;
959: case SADB_X_EXT_FLOW_TYPE:
960: headers[SADB_X_EXT_FLOW_TYPE] = (void *)ext;
1.26 deraadt 961: break;
1.1 claudio 962: default:
963: /* Ignore. */
964: break;
965: }
966: }
967: }