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