1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2015, 2016 by Delphix. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inet/mib2.h>
#include <sys/debug.h>
#include <sys/stropts.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inet/tcp.h>
#include <arpa/inet.h>
#include <ofmt.h>
#include <sys/time.h>
#include "connstat_mib.h"
#include "connstat_tcp.h"
/*
* The byte order of some of the fields in this code can be a bit confusing.
* When using sockaddr_in(6) structs, the address and ports are always in
* Network Byte Order (Big Endian), as required by sockaddr(3SOCKET).
*
* When using the structs mib2_tcpConnEntry_t and mib2_tcp6ConnEntry_t, the
* address fields (tcp(6)ConnLocalAddress and tcp(6)ConnRemAdddress) are in
* Network Byte Order. Note, however, that the port fields ARE NOT, but are
* instead in Host Byte Order. This isn't a problem though, since the ports
* we filter on from the command-line (ca_lport and ca_rport) are kept in
* Host Byte Order after parsing.
*
* Since the t_lport and t_rport fields come from the MIB structs, they are
* likewise stored in Host Byte Order (and need to be for printing). The
* t_laddr and t_raddr fields are string representations of the addresses,
* so they don't require any special attention.
*
* All of the statistics (such as bytes read and written, current window
* sizes, etc.) are in Host Byte Order.
*/
typedef struct tcp_fields_buf_s {
char t_laddr[INET6_ADDRSTRLEN];
char t_raddr[INET6_ADDRSTRLEN];
uint16_t t_lport;
uint16_t t_rport;
uint64_t t_inbytes;
uint64_t t_insegs;
uint64_t t_inunorderbytes;
uint64_t t_inunordersegs;
uint64_t t_outbytes;
uint64_t t_outsegs;
uint64_t t_retransbytes;
uint64_t t_retranssegs;
uint32_t t_suna;
uint32_t t_unsent;
uint32_t t_swnd;
uint32_t t_cwnd;
uint32_t t_rwnd;
uint32_t t_mss;
uint32_t t_rto;
uint32_t t_rtt_cnt;
uint64_t t_rtt_sum;
int t_state;
uint64_t t_rtt;
} tcp_fields_buf_t;
static boolean_t print_tcp_state(ofmt_arg_t *, char *, uint_t);
static ofmt_field_t tcp_fields[] = {
{ "LADDR", 26,
offsetof(tcp_fields_buf_t, t_laddr), print_string },
{ "RADDR", 26,
offsetof(tcp_fields_buf_t, t_raddr), print_string },
{ "LPORT", 6,
offsetof(tcp_fields_buf_t, t_lport), print_uint16 },
{ "RPORT", 6,
offsetof(tcp_fields_buf_t, t_rport), print_uint16 },
{ "INBYTES", 11,
offsetof(tcp_fields_buf_t, t_inbytes), print_uint64 },
{ "INSEGS", 11,
offsetof(tcp_fields_buf_t, t_insegs), print_uint64 },
{ "INUNORDERBYTES", 15,
offsetof(tcp_fields_buf_t, t_inunorderbytes), print_uint64 },
{ "INUNORDERSEGS", 14,
offsetof(tcp_fields_buf_t, t_inunordersegs), print_uint64 },
{ "OUTBYTES", 11,
offsetof(tcp_fields_buf_t, t_outbytes), print_uint64 },
{ "OUTSEGS", 11,
offsetof(tcp_fields_buf_t, t_outsegs), print_uint64 },
{ "RETRANSBYTES", 13,
offsetof(tcp_fields_buf_t, t_retransbytes), print_uint64 },
{ "RETRANSSEGS", 12,
offsetof(tcp_fields_buf_t, t_retranssegs), print_uint64 },
{ "SUNA", 11,
offsetof(tcp_fields_buf_t, t_suna), print_uint32 },
{ "UNSENT", 11,
offsetof(tcp_fields_buf_t, t_unsent), print_uint32 },
{ "SWND", 11,
offsetof(tcp_fields_buf_t, t_swnd), print_uint32 },
{ "CWND", 11,
offsetof(tcp_fields_buf_t, t_cwnd), print_uint32 },
{ "RWND", 11,
offsetof(tcp_fields_buf_t, t_rwnd), print_uint32 },
{ "MSS", 6,
offsetof(tcp_fields_buf_t, t_mss), print_uint32 },
{ "RTO", 8,
offsetof(tcp_fields_buf_t, t_rto), print_uint32 },
{ "RTT", 8,
offsetof(tcp_fields_buf_t, t_rtt), print_uint64 },
{ "RTTS", 8,
offsetof(tcp_fields_buf_t, t_rtt_sum), print_uint64 },
{ "RTTC", 11,
offsetof(tcp_fields_buf_t, t_rtt_cnt), print_uint32 },
{ "STATE", 12,
offsetof(tcp_fields_buf_t, t_state), print_tcp_state },
{ NULL, 0, 0, NULL}
};
static tcp_fields_buf_t fields_buf;
typedef struct tcp_state_info_s {
int tsi_state;
const char *tsi_string;
} tcp_state_info_t;
tcp_state_info_t tcp_state_info[] = {
{ TCPS_CLOSED, "CLOSED" },
{ TCPS_IDLE, "IDLE" },
{ TCPS_BOUND, "BOUND" },
{ TCPS_LISTEN, "LISTEN" },
{ TCPS_SYN_SENT, "SYN_SENT" },
{ TCPS_SYN_RCVD, "SYN_RCVD" },
{ TCPS_ESTABLISHED, "ESTABLISHED" },
{ TCPS_CLOSE_WAIT, "CLOSE_WAIT" },
{ TCPS_FIN_WAIT_1, "FIN_WAIT_1" },
{ TCPS_CLOSING, "CLOSING" },
{ TCPS_LAST_ACK, "LAST_ACK" },
{ TCPS_FIN_WAIT_2, "FIN_WAIT_2" },
{ TCPS_TIME_WAIT, "TIME_WAIT" },
{ TCPS_CLOSED - 1, NULL }
};
ofmt_field_t *
tcp_get_fields(void)
{
return (tcp_fields);
}
/*
* Extract information from the connection info structure into the global
* output buffer.
*/
static void
tcp_ci2buf(struct tcpConnEntryInfo_s *ci)
{
fields_buf.t_inbytes =
ci->ce_in_data_inorder_bytes + ci->ce_in_data_unorder_bytes;
fields_buf.t_insegs =
ci->ce_in_data_inorder_segs + ci->ce_in_data_unorder_segs;
fields_buf.t_inunorderbytes = ci->ce_in_data_unorder_bytes;
fields_buf.t_inunordersegs = ci->ce_in_data_unorder_segs;
fields_buf.t_outbytes = ci->ce_out_data_bytes;
fields_buf.t_outsegs = ci->ce_out_data_segs;
fields_buf.t_retransbytes = ci->ce_out_retrans_bytes;
fields_buf.t_retranssegs = ci->ce_out_retrans_segs;
fields_buf.t_suna = ci->ce_snxt - ci->ce_suna;
fields_buf.t_unsent = ci->ce_unsent;
fields_buf.t_swnd = ci->ce_swnd;
fields_buf.t_cwnd = ci->ce_cwnd;
fields_buf.t_rwnd = ci->ce_rwnd;
fields_buf.t_mss = ci->ce_mss;
fields_buf.t_rto = ci->ce_rto;
fields_buf.t_rtt = (ci->ce_out_data_segs == 0 ? 0 : ci->ce_rtt_sa);
fields_buf.t_rtt_sum = ci->ce_rtt_sum;
fields_buf.t_rtt_cnt = ci->ce_rtt_cnt;
fields_buf.t_state = ci->ce_state;
}
/*
* Extract information from the connection entry into the global output
* buffer.
*/
static void
tcp_ipv4_ce2buf(mib2_tcpConnEntry_t *ce)
{
VERIFY3P(inet_ntop(AF_INET, (void *)&ce->tcpConnLocalAddress,
fields_buf.t_laddr, sizeof (fields_buf.t_laddr)), !=, NULL);
VERIFY3P(inet_ntop(AF_INET, (void *)&ce->tcpConnRemAddress,
fields_buf.t_raddr, sizeof (fields_buf.t_raddr)), !=, NULL);
fields_buf.t_lport = ce->tcpConnLocalPort;
fields_buf.t_rport = ce->tcpConnRemPort;
tcp_ci2buf(&ce->tcpConnEntryInfo);
}
static void
tcp_ipv6_ce2buf(mib2_tcp6ConnEntry_t *ce)
{
VERIFY3P(inet_ntop(AF_INET6, (void *)&ce->tcp6ConnLocalAddress,
fields_buf.t_laddr, sizeof (fields_buf.t_laddr)), !=, NULL);
VERIFY3P(inet_ntop(AF_INET6, (void *)&ce->tcp6ConnRemAddress,
fields_buf.t_raddr, sizeof (fields_buf.t_raddr)), !=, NULL);
fields_buf.t_lport = ce->tcp6ConnLocalPort;
fields_buf.t_rport = ce->tcp6ConnRemPort;
tcp_ci2buf(&ce->tcp6ConnEntryInfo);
}
/*
* Print a single IPv4 connection entry, taking into account possible
* filters that have been set in state.
*/
static void
tcp_ipv4_print(mib2_tcpConnEntry_t *ce, conn_walk_state_t *state)
{
if (!(state->cws_flags & CS_LOOPBACK) &&
ntohl(ce->tcpConnLocalAddress) == INADDR_LOOPBACK) {
return;
}
if (state->cws_flags & CS_LADDR) {
struct sockaddr_in *sin =
(struct sockaddr_in *)&state->cws_filter.ca_laddr;
if (ce->tcpConnLocalAddress != sin->sin_addr.s_addr) {
return;
}
}
if (state->cws_flags & CS_RADDR) {
struct sockaddr_in *sin =
(struct sockaddr_in *)&state->cws_filter.ca_raddr;
if (ce->tcpConnRemAddress != sin->sin_addr.s_addr) {
return;
}
}
if (state->cws_flags & CS_LPORT) {
if (ce->tcpConnLocalPort != state->cws_filter.ca_lport) {
return;
}
}
if (state->cws_flags & CS_RPORT) {
if (ce->tcpConnRemPort != state->cws_filter.ca_rport) {
return;
}
}
if ((state->cws_flags & CS_STATE) &&
ce->tcpConnEntryInfo.ce_state != state->cws_filter.ca_state) {
return;
}
tcp_ipv4_ce2buf(ce);
ofmt_print(state->cws_ofmt, &fields_buf);
}
/*
* Print a single IPv6 connection entry, taking into account possible
* filters that have been set in state.
*/
static void
tcp_ipv6_print(mib2_tcp6ConnEntry_t *ce, conn_walk_state_t *state)
{
if (!(state->cws_flags & CS_LOOPBACK) &&
IN6_IS_ADDR_LOOPBACK(
(struct in6_addr *)&ce->tcp6ConnLocalAddress)) {
return;
}
if (state->cws_flags & CS_LADDR) {
struct sockaddr_in6 *sin6 =
(struct sockaddr_in6 *)&state->cws_filter.ca_laddr;
if (!IN6_ARE_ADDR_EQUAL(
(struct in6_addr *)&ce->tcp6ConnLocalAddress,
&sin6->sin6_addr)) {
return;
}
}
if (state->cws_flags & CS_RADDR) {
struct sockaddr_in6 *sin6 =
(struct sockaddr_in6 *)&state->cws_filter.ca_raddr;
if (!IN6_ARE_ADDR_EQUAL(
(struct in6_addr *)&ce->tcp6ConnRemAddress,
&sin6->sin6_addr)) {
return;
}
}
if (state->cws_flags & CS_LPORT) {
if (ce->tcp6ConnLocalPort != state->cws_filter.ca_lport) {
return;
}
}
if (state->cws_flags & CS_RPORT) {
if (ce->tcp6ConnRemPort != state->cws_filter.ca_rport) {
return;
}
}
if ((state->cws_flags & CS_STATE) &&
ce->tcp6ConnEntryInfo.ce_state != state->cws_filter.ca_state) {
return;
}
tcp_ipv6_ce2buf(ce);
ofmt_print(state->cws_ofmt, &fields_buf);
}
void
tcp_walk_ipv4(struct strbuf *dbuf, conn_walk_state_t *state)
{
uint_t nconns = (dbuf->len / sizeof (mib2_tcpConnEntry_t));
/* LINTED E_BAD_PTR_CAST_ALIGN */
mib2_tcpConnEntry_t *ce = (mib2_tcpConnEntry_t *)dbuf->buf;
for (; nconns > 0; ce++, nconns--) {
tcp_ipv4_print(ce, state);
}
}
void
tcp_walk_ipv6(struct strbuf *dbuf, conn_walk_state_t *state)
{
uint_t nconns = (dbuf->len / sizeof (mib2_tcp6ConnEntry_t));
/* LINTED E_BAD_PTR_CAST_ALIGN */
mib2_tcp6ConnEntry_t *ce = (mib2_tcp6ConnEntry_t *)dbuf->buf;
for (; nconns > 0; ce++, nconns--) {
tcp_ipv6_print(ce, state);
}
}
static tcp_state_info_t *
tcp_stateinfobystate(int state)
{
tcp_state_info_t *sip;
for (sip = tcp_state_info; sip->tsi_string != NULL; sip++) {
if (sip->tsi_state == state) {
return (sip);
}
}
return (NULL);
}
static tcp_state_info_t *
tcp_stateinfobystr(const char *statestr)
{
tcp_state_info_t *sip;
for (sip = tcp_state_info; sip->tsi_string != NULL; sip++) {
if (strncasecmp(statestr, sip->tsi_string,
strlen(sip->tsi_string)) == 0) {
return (sip);
}
}
return (NULL);
}
int
tcp_str2state(const char *statestr)
{
tcp_state_info_t *sip = tcp_stateinfobystr(statestr);
return (sip == NULL ? TCPS_CLOSED - 1 : sip->tsi_state);
}
static const char *
tcp_state2str(int state)
{
tcp_state_info_t *sip = tcp_stateinfobystate(state);
return (sip == NULL ? NULL : sip->tsi_string);
}
static boolean_t
print_tcp_state(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
{
/* LINTED E_BAD_PTR_CAST_ALIGN */
int state = *(int *)((char *)ofarg->ofmt_cbarg + ofarg->ofmt_id);
const char *statestr = tcp_state2str(state);
if (statestr != NULL) {
(void) strlcpy(buf, statestr, bufsize);
} else {
(void) snprintf(buf, bufsize, "UNKNOWN(%d)", state);
}
return (B_TRUE);
}
|