1/*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifdef __APPLE__
30#include <TargetConditionals.h>
31#endif
32
33#if TARGET_OS_EMBEDDED
34#include <stdio.h>
35#include <err.h>
36#include <stdlib.h>
37#include <strings.h>
38
39#include <sys/errno.h>
40#include <sys/types.h>
41#include <sys/sysctl.h>
42
43#include <netinet/in.h>
44#include <netinet/tcp.h>
45#include <netinet/mptcp_var.h>
46
47#include <arpa/inet.h>
48
49#include "netstat.h"
50
51/* XXX we can't include tcp_fsm.h because inet.c already includes it. */
52static const char *tcpstates[] = {
53        "CLOSED",       "LISTEN",       "SYN_SENT",     "SYN_RCVD",
54        "ESTABLISHED",  "CLOSE_WAIT",   "FIN_WAIT_1",   "CLOSING",
55        "LAST_ACK",     "FIN_WAIT_2",   "TIME_WAIT"
56};
57
58static const char *mptcpstates[] = {
59	"CLOSED", "LISTEN", "ESTABLISHED", "CLOSE_WAIT", "FIN_WAIT_1",
60	"CLOSING", "LAST_ACK", "FIN_WAIT_2", "TIME_WAIT", "FASTCLOSE_WAIT"
61};
62
63int mptcp_done = 0;
64extern void inetprint (struct in_addr *, int, char *, int);
65extern void inet6print (struct in6_addr *, int, char *, int);
66
67static void
68printmptcp(int id, conninfo_mptcp_t *mptcp)
69{
70	int i;
71	conninfo_tcp_t *tcpci;
72	struct sockaddr_storage *src, *dst;
73	mptcp_flow_t *flow;
74	int af;
75
76	printf("mptcp/%-2.2d %69s\n", id,
77	    mptcpstates[mptcp->mptcpci_state]);
78	for (i = 0; i < mptcp->mptcpci_nflows; i++) {
79		flow = &mptcp->mptcpci_flows[i];
80		src = &flow->flow_src;
81		dst = &flow->flow_dst;
82		af = src->ss_family;
83		printf(" tcp%d/%-2.2d  ", af == AF_INET ? 4 : 6,
84		    flow->flow_cid);
85		printf("%-8.8x   ", flow->flow_flags);
86#define SIN(x) ((struct sockaddr_in *)(x))
87#define SIN6(x) ((struct sockaddr_in6 *)(x))
88		switch (af) {
89		case AF_INET:
90			inetprint(&SIN(src)->sin_addr, SIN(src)->sin_port,
91			    "tcp", nflag);
92			inetprint(&SIN(dst)->sin_addr, SIN(dst)->sin_port,
93			    "tcp", nflag);
94			break;
95#ifdef INET6
96		case AF_INET6:
97			inet6print(&SIN6(src)->sin6_addr, SIN6(src)->sin6_port,
98			    "tcp", nflag);
99			inet6print(&SIN6(dst)->sin6_addr, SIN6(dst)->sin6_port,
100			    "tcp", nflag);
101			break;
102		}
103#endif
104#undef SIN
105#undef SIN6
106		tcpci = &flow->flow_ci;
107		printf("%s\n", tcpstates[tcpci->tcpci_tcp_info.tcpi_state]);
108	}
109}
110
111void
112mptcppr(uint32_t off, char *name, int af)
113{
114#pragma unused(off, name, af)
115	const char *mibvar = "net.inet.mptcp.pcblist";
116	size_t len = 0;
117	conninfo_mptcp_t *mptcp;
118	char *buf, *bufp;
119	int id = 0;
120
121	if (Lflag || Aflag || mptcp_done)
122		return;
123	mptcp_done = 1;
124
125	if (sysctlbyname(mibvar, 0, &len, NULL, 0) < 0) {
126		if (errno != ENOENT)
127			warn("sysctl: %s", mibvar);
128		return;
129	}
130	if ((buf = malloc(len)) == NULL) {
131		warn("malloc");
132		return;
133	}
134	if (sysctlbyname(mibvar, buf, &len, NULL, 0) < 0) {
135		warn("sysctl: %s", mibvar);
136		free(buf);
137		return;
138	}
139
140	printf("Active Multipath Internet connections\n");
141	printf("%-8.8s  %-9.9s  %-22.22s %-22.22s %-11.11s\n",
142		"Proto/ID", "Flags",
143		"Local Address", "Foreign Address",
144		"(state)");
145
146	bufp = buf;
147	while (bufp < buf + len) {
148		/* Sanity check */
149		if (buf + len - bufp < sizeof(conninfo_mptcp_t))
150			break;
151		mptcp = (conninfo_mptcp_t *)bufp;
152		printmptcp(id++, mptcp);
153		bufp += mptcp->mptcpci_len;
154	}
155	free(buf);
156}
157#endif /* TARGET_OS_EMBEDDED */
158