Annotation of src/usr.bin/netstat/show.c, Revision 1.3
1.3 ! itojun 1: /* $OpenBSD: show.c,v 1.2 2006/05/30 19:42:58 todd 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) ||
1.3 ! itojun 475: IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
! 476: IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
1.1 claudio 477: /* XXX: override is ok? */
478: sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
479: &in6->s6_addr[2]);
480: *(u_short *)&in6->s6_addr[2] = 0;
481: }
482: if (flags & RTF_HOST)
483: cp = routename((struct sockaddr *)sa6);
484: else
485: cp = netname((struct sockaddr *)sa6, mask);
486: break;
487: }
488: default:
489: if ((flags & RTF_HOST) || mask == NULL)
490: cp = routename(sa);
491: else
492: cp = netname(sa, mask);
493: break;
494: }
495: if (width < 0)
496: printf("%s", cp);
497: else {
498: if (nflag)
499: printf("%-*s ", width, cp);
500: else
501: printf("%-*.*s ", width, width, cp);
502: }
503: }
504:
505: void
506: p_flags(int f, char *format)
507: {
508: char name[33], *flags;
509: const struct bits *p = bits;
510:
511: for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
512: if (p->b_mask & f)
513: *flags++ = p->b_val;
514: *flags = '\0';
515: printf(format, name);
516: }
517:
518: static char line[MAXHOSTNAMELEN];
519: static char domain[MAXHOSTNAMELEN];
520:
521: char *
522: routename(struct sockaddr *sa)
523: {
524: char *cp = NULL;
525: static int first = 1;
526:
527: if (first) {
528: first = 0;
529: if (gethostname(domain, sizeof(domain)) == 0 &&
530: (cp = strchr(domain, '.')))
531: (void)strlcpy(domain, cp + 1, sizeof(domain));
532: else
533: domain[0] = '\0';
534: cp = NULL;
535: }
536:
537: if (sa->sa_len == 0) {
538: (void)strlcpy(line, "default", sizeof(line));
539: return (line);
540: }
541:
542: switch (sa->sa_family) {
543: case AF_INET:
544: return
545: (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
546:
547: case AF_INET6:
548: {
549: struct sockaddr_in6 sin6;
550:
551: memset(&sin6, 0, sizeof(sin6));
552: memcpy(&sin6, sa, sa->sa_len);
553: sin6.sin6_len = sizeof(struct sockaddr_in6);
554: sin6.sin6_family = AF_INET6;
555: if (sa->sa_len == sizeof(struct sockaddr_in6) &&
556: (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3 ! itojun 557: IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
! 558: IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1 claudio 559: sin6.sin6_scope_id == 0) {
560: sin6.sin6_scope_id =
561: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
562: sin6.sin6_addr.s6_addr[2] = 0;
563: sin6.sin6_addr.s6_addr[3] = 0;
564: }
565: return (routename6(&sin6));
566: }
567:
568: case AF_IPX:
569: return (ipx_print(sa));
570:
571: case AF_LINK:
572: return (link_print(sa));
573:
574: case AF_UNSPEC:
575: if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
576: static char name[RTLABEL_LEN];
577: struct sockaddr_rtlabel *sr;
578:
579: sr = (struct sockaddr_rtlabel *)sa;
580: strlcpy(name, sr->sr_label, sizeof(name));
581: return (name);
582: }
583: /* FALLTHROUGH */
584: default:
585: (void)snprintf(line, sizeof(line), "(%d) %s",
586: sa->sa_family, any_ntoa(sa));
587: break;
588: }
589: return (line);
590: }
591:
592: char *
593: routename4(in_addr_t in)
594: {
595: char *cp = NULL;
596: struct in_addr ina;
597: struct hostent *hp;
598:
599: if (in == INADDR_ANY)
600: cp = "default";
601: if (!cp && !nflag) {
602: if ((hp = gethostbyaddr((char *)&in,
603: sizeof(in), AF_INET)) != NULL) {
604: if ((cp = strchr(hp->h_name, '.')) &&
605: !strcmp(cp + 1, domain))
606: *cp = '\0';
607: cp = hp->h_name;
608: }
609: }
610: ina.s_addr = in;
611: strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
612:
613: return (line);
614: }
615:
616: char *
617: routename6(struct sockaddr_in6 *sin6)
618: {
619: int niflags = 0;
620:
621: if (nflag)
622: niflags |= NI_NUMERICHOST;
623: else
624: niflags |= NI_NOFQDN;
625:
626: if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
627: line, sizeof(line), NULL, 0, niflags) != 0)
628: strncpy(line, "invalid", sizeof(line));
629:
630: return (line);
631: }
632:
633: /*
634: * Return the name of the network whose address is given.
635: * The address is assumed to be that of a net or subnet, not a host.
636: */
637: char *
638: netname4(in_addr_t in, in_addr_t mask)
639: {
640: char *cp = NULL;
641: struct netent *np = NULL;
642: int mbits;
643:
644: in = ntohl(in);
645: mask = ntohl(mask);
646: if (!nflag && in != INADDR_ANY) {
647: if ((np = getnetbyaddr(in, AF_INET)) != NULL)
648: cp = np->n_name;
649: }
650: if (in == INADDR_ANY)
651: cp = "default";
652: mbits = mask ? 33 - ffs(mask) : 0;
653: if (cp)
654: strlcpy(line, cp, sizeof(line));
655: #define C(x) ((x) & 0xff)
656: else if (mbits < 9)
657: snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
658: else if (mbits < 17)
659: snprintf(line, sizeof(line), "%u.%u/%d",
660: C(in >> 24) , C(in >> 16), mbits);
661: else if (mbits < 25)
662: snprintf(line, sizeof(line), "%u.%u.%u/%d",
663: C(in >> 24), C(in >> 16), C(in >> 8), mbits);
664: else
665: snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
666: C(in >> 16), C(in >> 8), C(in), mbits);
667: #undef C
668: return (line);
669: }
670:
671: char *
672: netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
673: {
674: struct sockaddr_in6 sin6;
675: u_char *p;
676: int masklen, final = 0, illegal = 0;
677: int i, lim, flag, error;
678: char hbuf[NI_MAXHOST];
679:
680: sin6 = *sa6;
681:
682: flag = 0;
683: masklen = 0;
684: if (mask) {
685: lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
686: lim = lim < sizeof(struct in6_addr) ?
687: lim : sizeof(struct in6_addr);
688: for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
689: if (final && *p) {
690: illegal++;
691: sin6.sin6_addr.s6_addr[i++] = 0x00;
692: continue;
693: }
694:
695: switch (*p & 0xff) {
696: case 0xff:
697: masklen += 8;
698: break;
699: case 0xfe:
700: masklen += 7;
701: final++;
702: break;
703: case 0xfc:
704: masklen += 6;
705: final++;
706: break;
707: case 0xf8:
708: masklen += 5;
709: final++;
710: break;
711: case 0xf0:
712: masklen += 4;
713: final++;
714: break;
715: case 0xe0:
716: masklen += 3;
717: final++;
718: break;
719: case 0xc0:
720: masklen += 2;
721: final++;
722: break;
723: case 0x80:
724: masklen += 1;
725: final++;
726: break;
727: case 0x00:
728: final++;
729: break;
730: default:
731: final++;
732: illegal++;
733: break;
734: }
735:
736: if (!illegal)
737: sin6.sin6_addr.s6_addr[i++] &= *p;
738: else
739: sin6.sin6_addr.s6_addr[i++] = 0x00;
740: }
741: while (i < sizeof(struct in6_addr))
742: sin6.sin6_addr.s6_addr[i++] = 0x00;
743: } else
744: masklen = 128;
745:
746: if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
747: return ("default");
748:
749: if (illegal)
750: warnx("illegal prefixlen");
751:
752: if (nflag)
753: flag |= NI_NUMERICHOST;
754: error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
755: hbuf, sizeof(hbuf), NULL, 0, flag);
756: if (error)
757: snprintf(hbuf, sizeof(hbuf), "invalid");
758:
759: snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
760: return (line);
761: }
762:
763: /*
764: * Return the name of the network whose address is given.
765: * The address is assumed to be that of a net or subnet, not a host.
766: */
767: char *
768: netname(struct sockaddr *sa, struct sockaddr *mask)
769: {
770: switch (sa->sa_family) {
771:
772: case AF_INET:
773: return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
774: ((struct sockaddr_in *)mask)->sin_addr.s_addr);
775: case AF_INET6:
776: return netname6((struct sockaddr_in6 *)sa,
777: (struct sockaddr_in6 *)mask);
778: case AF_IPX:
779: return (ipx_print(sa));
780: case AF_LINK:
781: return (link_print(sa));
782: default:
783: snprintf(line, sizeof(line), "af %d: %s",
784: sa->sa_family, any_ntoa(sa));
785: break;
786: }
787: return (line);
788: }
789:
790: static const char hexlist[] = "0123456789abcdef";
791:
792: char *
793: any_ntoa(const struct sockaddr *sa)
794: {
795: static char obuf[240];
796: const char *in = sa->sa_data;
797: char *out = obuf;
798: int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
799:
800: *out++ = 'Q';
801: do {
802: *out++ = hexlist[(*in >> 4) & 15];
803: *out++ = hexlist[(*in++) & 15];
804: *out++ = '.';
805: } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
806: out[-1] = '\0';
807: return (obuf);
808: }
809:
810: short ipx_nullh[] = {0,0,0};
811: short ipx_bh[] = {-1,-1,-1};
812:
813: char *
814: ipx_print(struct sockaddr *sa)
815: {
816: struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
817: struct ipx_addr work;
818: union {
819: union ipx_net net_e;
820: u_int32_t long_e;
821: } net;
822: u_short port;
823: static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
824: char *host = "";
825: char *p;
826: u_char *q;
827:
828: work = sipx->sipx_addr;
829: port = ntohs(work.ipx_port);
830: work.ipx_port = 0;
831: net.net_e = work.ipx_net;
832: if (ipx_nullhost(work) && net.long_e == 0) {
833: if (!port)
834: return ("*.*");
835: (void)snprintf(mybuf, sizeof(mybuf), "*.0x%XH", port);
836: return (mybuf);
837: }
838:
839: if (memcmp(ipx_bh, work.ipx_host.c_host, 6) == 0)
840: host = "any";
841: else if (memcmp(ipx_nullh, work.ipx_host.c_host, 6) == 0)
842: host = "*";
843: else {
844: q = work.ipx_host.c_host;
845: (void)snprintf(chost, sizeof(chost), "%02X%02X%02X%02X%02X%02XH",
846: q[0], q[1], q[2], q[3], q[4], q[5]);
847: for (p = chost; *p == '0' && p < chost + 12; p++)
848: /* void */;
849: host = p;
850: }
851: if (port)
852: (void)snprintf(cport, sizeof(cport), ".%XH", htons(port));
853: else
854: *cport = '\0';
855:
856: (void)snprintf(mybuf, sizeof(mybuf), "%XH.%s%s",
857: ntohl(net.long_e), host, cport);
858: return (mybuf);
859: }
860:
861: char *
862: link_print(struct sockaddr *sa)
863: {
864: struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
865: u_char *lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
866:
867: if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
868: sdl->sdl_slen == 0) {
869: (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
870: return (line);
871: }
872: switch (sdl->sdl_type) {
873: case IFT_ETHER:
874: case IFT_CARP:
875: return (ether_ntoa((struct ether_addr *)lla));
876: default:
877: return (link_ntoa(sdl));
878: }
879: }
880:
881: void
882: index_pfk(struct sadb_msg *msg, void **headers)
883: {
884: struct sadb_ext *ext;
885:
886: for (ext = (struct sadb_ext *)(msg + 1);
887: (size_t)((u_int8_t *)ext - (u_int8_t *)msg) <
888: msg->sadb_msg_len * PFKEYV2_CHUNK && ext->sadb_ext_len > 0;
889: ext = (struct sadb_ext *)((u_int8_t *)ext +
890: ext->sadb_ext_len * PFKEYV2_CHUNK)) {
891: switch (ext->sadb_ext_type) {
892: case SADB_EXT_ADDRESS_SRC:
893: headers[SADB_EXT_ADDRESS_SRC] = (void *)ext;
894: break;
895: case SADB_EXT_ADDRESS_DST:
896: headers[SADB_EXT_ADDRESS_DST] = (void *)ext;
897: break;
898: case SADB_X_EXT_PROTOCOL:
899: headers[SADB_X_EXT_PROTOCOL] = (void *)ext;
900: break;
901: case SADB_X_EXT_SRC_FLOW:
902: headers[SADB_X_EXT_SRC_FLOW] = (void *)ext;
903: break;
904: case SADB_X_EXT_DST_FLOW:
905: headers[SADB_X_EXT_DST_FLOW] = (void *)ext;
906: break;
907: case SADB_X_EXT_SRC_MASK:
908: headers[SADB_X_EXT_SRC_MASK] = (void *)ext;
909: break;
910: case SADB_X_EXT_DST_MASK:
911: headers[SADB_X_EXT_DST_MASK] = (void *)ext;
912: break;
913: case SADB_X_EXT_FLOW_TYPE:
914: headers[SADB_X_EXT_FLOW_TYPE] = (void *)ext;
915: default:
916: /* Ignore. */
917: break;
918: }
919: }
920: }