]>
Commit | Line | Data |
---|---|---|
b9de3ecf | 1 | /* |
aba5acdf SH |
2 | * ss.c "sockstat", socket statistics |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
aba5acdf SH |
15 | #include <fcntl.h> |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/socket.h> | |
18 | #include <sys/uio.h> | |
0bab7630 | 19 | #include <sys/sysmacros.h> |
aba5acdf SH |
20 | #include <netinet/in.h> |
21 | #include <string.h> | |
22 | #include <errno.h> | |
23 | #include <netdb.h> | |
24 | #include <arpa/inet.h> | |
aba5acdf SH |
25 | #include <dirent.h> |
26 | #include <fnmatch.h> | |
ab61159a | 27 | #include <getopt.h> |
bf4ceee6 | 28 | #include <stdbool.h> |
6b376ebd | 29 | #include <limits.h> |
90351722 | 30 | #include <stdarg.h> |
aba5acdf | 31 | |
7bd91885 | 32 | #include "ss_util.h" |
aba5acdf SH |
33 | #include "utils.h" |
34 | #include "rt_names.h" | |
35 | #include "ll_map.h" | |
36 | #include "libnetlink.h" | |
95ce04bc | 37 | #include "namespace.h" |
fbef6555 | 38 | #include "version.h" |
31f45088 | 39 | #include "rt_names.h" |
14f4bda5 | 40 | #include "cg_map.h" |
aba5acdf | 41 | |
9cb1eccf | 42 | #include <linux/tcp.h> |
dfbaa90d | 43 | #include <linux/unix_diag.h> |
372c30d2 ND |
44 | #include <linux/netdevice.h> /* for MAX_ADDR_LEN */ |
45 | #include <linux/filter.h> | |
2abc3d76 | 46 | #include <linux/xdp_diag.h> |
372c30d2 | 47 | #include <linux/packet_diag.h> |
ecb928c8 | 48 | #include <linux/netlink_diag.h> |
f89d46ad | 49 | #include <linux/sctp.h> |
c759116a | 50 | #include <linux/vm_sockets_diag.h> |
5caf79a0 GM |
51 | #include <linux/net.h> |
52 | #include <linux/tipc.h> | |
53 | #include <linux/tipc_netlink.h> | |
54 | #include <linux/tipc_sockets_diag.h> | |
14cadc70 | 55 | #include <linux/tls.h> |
712fdd98 | 56 | #include <linux/mptcp.h> |
aba5acdf | 57 | |
9700927a TDS |
58 | /* AF_VSOCK/PF_VSOCK is only provided since glibc 2.18 */ |
59 | #ifndef PF_VSOCK | |
60 | #define PF_VSOCK 40 | |
61 | #endif | |
62 | #ifndef AF_VSOCK | |
63 | #define AF_VSOCK PF_VSOCK | |
64 | #endif | |
65 | ||
9c3be2c0 PA |
66 | #ifndef IPPROTO_MPTCP |
67 | #define IPPROTO_MPTCP 262 | |
68 | #endif | |
69 | ||
aa5bd6a2 SB |
70 | #define BUF_CHUNK (1024 * 1024) /* Buffer chunk allocation size */ |
71 | #define BUF_CHUNKS_MAX 5 /* Maximum number of allocated buffer chunks */ | |
691bd854 | 72 | #define LEN_ALIGN(x) (((x) + 1) & ~1) |
8a4025f6 | 73 | |
116ac927 RH |
74 | #if HAVE_SELINUX |
75 | #include <selinux/selinux.h> | |
76 | #else | |
77 | /* Stubs for SELinux functions */ | |
78 | static int is_selinux_enabled(void) | |
79 | { | |
80 | return -1; | |
81 | } | |
82 | ||
83 | static int getpidcon(pid_t pid, char **context) | |
84 | { | |
85 | *context = NULL; | |
86 | return -1; | |
87 | } | |
88 | ||
89 | static int getfilecon(char *path, char **context) | |
90 | { | |
91 | *context = NULL; | |
92 | return -1; | |
93 | } | |
94 | ||
95 | static int security_get_initial_context(char *name, char **context) | |
96 | { | |
97 | *context = NULL; | |
98 | return -1; | |
99 | } | |
100 | #endif | |
101 | ||
aba5acdf | 102 | int preferred_family = AF_UNSPEC; |
3f7bd0fd | 103 | static int show_options; |
acd1e437 | 104 | int show_details; |
3f7bd0fd SH |
105 | static int show_users; |
106 | static int show_mem; | |
107 | static int show_tcpinfo; | |
108 | static int show_bpf; | |
109 | static int show_proc_ctx; | |
110 | static int show_sock_ctx; | |
111 | static int show_header = 1; | |
112 | static int follow_events; | |
113 | static int sctp_ino; | |
114 | static int show_tipcinfo; | |
0f3f0ca3 | 115 | static int show_tos; |
14f4bda5 | 116 | static int show_cgroup; |
ad34d5fa | 117 | static int show_inet_sockopt; |
296b5de7 | 118 | int oneline; |
aba5acdf | 119 | |
59f46b7b SB |
120 | enum col_id { |
121 | COL_NETID, | |
122 | COL_STATE, | |
123 | COL_RECVQ, | |
124 | COL_SENDQ, | |
125 | COL_ADDR, | |
126 | COL_SERV, | |
127 | COL_RADDR, | |
128 | COL_RSERV, | |
129 | COL_EXT, | |
5883c6eb | 130 | COL_PROC, |
59f46b7b SB |
131 | COL_MAX |
132 | }; | |
133 | ||
134 | enum col_align { | |
135 | ALIGN_LEFT, | |
136 | ALIGN_CENTER, | |
137 | ALIGN_RIGHT | |
138 | }; | |
139 | ||
140 | struct column { | |
141 | const enum col_align align; | |
142 | const char *header; | |
143 | const char *ldelim; | |
87b1a7ae SB |
144 | int disabled; |
145 | int width; /* Calculated, including additional layout spacing */ | |
146 | int max_len; /* Measured maximum field length in this column */ | |
59f46b7b SB |
147 | }; |
148 | ||
149 | static struct column columns[] = { | |
87b1a7ae SB |
150 | { ALIGN_LEFT, "Netid", "", 0, 0, 0 }, |
151 | { ALIGN_LEFT, "State", " ", 0, 0, 0 }, | |
152 | { ALIGN_LEFT, "Recv-Q", " ", 0, 0, 0 }, | |
153 | { ALIGN_LEFT, "Send-Q", " ", 0, 0, 0 }, | |
154 | { ALIGN_RIGHT, "Local Address:", " ", 0, 0, 0 }, | |
155 | { ALIGN_LEFT, "Port", "", 0, 0, 0 }, | |
156 | { ALIGN_RIGHT, "Peer Address:", " ", 0, 0, 0 }, | |
157 | { ALIGN_LEFT, "Port", "", 0, 0, 0 }, | |
5883c6eb | 158 | { ALIGN_LEFT, "Process", "", 0, 0, 0 }, |
87b1a7ae | 159 | { ALIGN_LEFT, "", "", 0, 0, 0 }, |
59f46b7b SB |
160 | }; |
161 | ||
162 | static struct column *current_field = columns; | |
691bd854 SB |
163 | |
164 | /* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to | |
165 | * the buffer as a variable size token. A token consists of a 16 bits length | |
166 | * field, followed by a string which is not NULL-terminated. | |
167 | * | |
168 | * A new chunk is allocated and linked when the current chunk doesn't have | |
169 | * enough room to store the current token as a whole. | |
170 | */ | |
171 | struct buf_chunk { | |
172 | struct buf_chunk *next; /* Next chained chunk */ | |
173 | char *end; /* Current end of content */ | |
174 | char data[0]; | |
175 | }; | |
176 | ||
177 | struct buf_token { | |
178 | uint16_t len; /* Data length, excluding length descriptor */ | |
179 | char data[0]; | |
180 | }; | |
181 | ||
182 | static struct { | |
183 | struct buf_token *cur; /* Position of current token in chunk */ | |
184 | struct buf_chunk *head; /* First chunk */ | |
185 | struct buf_chunk *tail; /* Current chunk */ | |
aa5bd6a2 | 186 | int chunks; /* Number of allocated chunks */ |
691bd854 | 187 | } buffer; |
aba5acdf SH |
188 | |
189 | static const char *TCP_PROTO = "tcp"; | |
f89d46ad | 190 | static const char *SCTP_PROTO = "sctp"; |
aba5acdf SH |
191 | static const char *UDP_PROTO = "udp"; |
192 | static const char *RAW_PROTO = "raw"; | |
acd1e437 | 193 | static const char *dg_proto; |
aba5acdf | 194 | |
acd1e437 | 195 | enum { |
aba5acdf | 196 | TCP_DB, |
9c3be2c0 | 197 | MPTCP_DB, |
351efcde | 198 | DCCP_DB, |
aba5acdf SH |
199 | UDP_DB, |
200 | RAW_DB, | |
201 | UNIX_DG_DB, | |
202 | UNIX_ST_DB, | |
30b669d7 | 203 | UNIX_SQ_DB, |
aba5acdf SH |
204 | PACKET_DG_DB, |
205 | PACKET_R_DB, | |
206 | NETLINK_DB, | |
f89d46ad | 207 | SCTP_DB, |
c759116a SH |
208 | VSOCK_ST_DB, |
209 | VSOCK_DG_DB, | |
5caf79a0 | 210 | TIPC_DB, |
2abc3d76 | 211 | XDP_DB, |
aba5acdf SH |
212 | MAX_DB |
213 | }; | |
214 | ||
215 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) | |
30b669d7 | 216 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB)) |
aba5acdf | 217 | #define ALL_DB ((1<<MAX_DB)-1) |
9c3be2c0 | 218 | #define INET_L4_DBM ((1<<TCP_DB)|(1<<MPTCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB)) |
168d97f9 | 219 | #define INET_DBM (INET_L4_DBM | (1<<RAW_DB)) |
c759116a | 220 | #define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB)) |
aba5acdf SH |
221 | |
222 | enum { | |
7d105b56 SH |
223 | SS_UNKNOWN, |
224 | SS_ESTABLISHED, | |
225 | SS_SYN_SENT, | |
226 | SS_SYN_RECV, | |
227 | SS_FIN_WAIT1, | |
228 | SS_FIN_WAIT2, | |
229 | SS_TIME_WAIT, | |
230 | SS_CLOSE, | |
231 | SS_CLOSE_WAIT, | |
232 | SS_LAST_ACK, | |
233 | SS_LISTEN, | |
234 | SS_CLOSING, | |
235 | SS_MAX | |
aba5acdf SH |
236 | }; |
237 | ||
f89d46ad PS |
238 | enum { |
239 | SCTP_STATE_CLOSED = 0, | |
240 | SCTP_STATE_COOKIE_WAIT = 1, | |
241 | SCTP_STATE_COOKIE_ECHOED = 2, | |
242 | SCTP_STATE_ESTABLISHED = 3, | |
243 | SCTP_STATE_SHUTDOWN_PENDING = 4, | |
244 | SCTP_STATE_SHUTDOWN_SENT = 5, | |
245 | SCTP_STATE_SHUTDOWN_RECEIVED = 6, | |
246 | SCTP_STATE_SHUTDOWN_ACK_SENT = 7, | |
247 | }; | |
248 | ||
9db7bf15 VK |
249 | #define SS_ALL ((1 << SS_MAX) - 1) |
250 | #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV))) | |
5caf79a0 | 251 | #define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE)) |
aba5acdf SH |
252 | |
253 | #include "ssfilter.h" | |
254 | ||
acd1e437 | 255 | struct filter { |
aba5acdf SH |
256 | int dbs; |
257 | int states; | |
b338a3e7 | 258 | uint64_t families; |
aba5acdf | 259 | struct ssfilter *f; |
fb2594c1 | 260 | bool kill; |
97352f1b | 261 | struct rtnl_handle *rth_for_killing; |
aba5acdf SH |
262 | }; |
263 | ||
b338a3e7 SH |
264 | #define FAMILY_MASK(family) ((uint64_t)1 << (family)) |
265 | ||
9db7bf15 VK |
266 | static const struct filter default_dbs[MAX_DB] = { |
267 | [TCP_DB] = { | |
268 | .states = SS_CONN, | |
b338a3e7 | 269 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
9db7bf15 | 270 | }, |
9c3be2c0 PA |
271 | [MPTCP_DB] = { |
272 | .states = SS_CONN, | |
273 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
274 | }, | |
9db7bf15 VK |
275 | [DCCP_DB] = { |
276 | .states = SS_CONN, | |
b338a3e7 | 277 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
9db7bf15 VK |
278 | }, |
279 | [UDP_DB] = { | |
f42a4574 | 280 | .states = (1 << SS_ESTABLISHED), |
b338a3e7 | 281 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
9db7bf15 VK |
282 | }, |
283 | [RAW_DB] = { | |
f42a4574 | 284 | .states = (1 << SS_ESTABLISHED), |
b338a3e7 | 285 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
9db7bf15 VK |
286 | }, |
287 | [UNIX_DG_DB] = { | |
288 | .states = (1 << SS_CLOSE), | |
b338a3e7 | 289 | .families = FAMILY_MASK(AF_UNIX), |
9db7bf15 VK |
290 | }, |
291 | [UNIX_ST_DB] = { | |
292 | .states = SS_CONN, | |
b338a3e7 | 293 | .families = FAMILY_MASK(AF_UNIX), |
9db7bf15 VK |
294 | }, |
295 | [UNIX_SQ_DB] = { | |
296 | .states = SS_CONN, | |
b338a3e7 | 297 | .families = FAMILY_MASK(AF_UNIX), |
9db7bf15 VK |
298 | }, |
299 | [PACKET_DG_DB] = { | |
300 | .states = (1 << SS_CLOSE), | |
b338a3e7 | 301 | .families = FAMILY_MASK(AF_PACKET), |
9db7bf15 VK |
302 | }, |
303 | [PACKET_R_DB] = { | |
304 | .states = (1 << SS_CLOSE), | |
b338a3e7 | 305 | .families = FAMILY_MASK(AF_PACKET), |
9db7bf15 VK |
306 | }, |
307 | [NETLINK_DB] = { | |
308 | .states = (1 << SS_CLOSE), | |
b338a3e7 | 309 | .families = FAMILY_MASK(AF_NETLINK), |
9db7bf15 | 310 | }, |
f89d46ad PS |
311 | [SCTP_DB] = { |
312 | .states = SS_CONN, | |
b338a3e7 | 313 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
f89d46ad | 314 | }, |
c759116a SH |
315 | [VSOCK_ST_DB] = { |
316 | .states = SS_CONN, | |
317 | .families = FAMILY_MASK(AF_VSOCK), | |
318 | }, | |
319 | [VSOCK_DG_DB] = { | |
320 | .states = SS_CONN, | |
321 | .families = FAMILY_MASK(AF_VSOCK), | |
322 | }, | |
5caf79a0 GM |
323 | [TIPC_DB] = { |
324 | .states = TIPC_SS_CONN, | |
325 | .families = FAMILY_MASK(AF_TIPC), | |
326 | }, | |
2abc3d76 BT |
327 | [XDP_DB] = { |
328 | .states = (1 << SS_CLOSE), | |
329 | .families = FAMILY_MASK(AF_XDP), | |
330 | }, | |
aba5acdf SH |
331 | }; |
332 | ||
9db7bf15 VK |
333 | static const struct filter default_afs[AF_MAX] = { |
334 | [AF_INET] = { | |
335 | .dbs = INET_DBM, | |
336 | .states = SS_CONN, | |
337 | }, | |
338 | [AF_INET6] = { | |
339 | .dbs = INET_DBM, | |
340 | .states = SS_CONN, | |
341 | }, | |
342 | [AF_UNIX] = { | |
343 | .dbs = UNIX_DBM, | |
344 | .states = SS_CONN, | |
345 | }, | |
346 | [AF_PACKET] = { | |
347 | .dbs = PACKET_DBM, | |
348 | .states = (1 << SS_CLOSE), | |
349 | }, | |
350 | [AF_NETLINK] = { | |
351 | .dbs = (1 << NETLINK_DB), | |
352 | .states = (1 << SS_CLOSE), | |
353 | }, | |
c759116a SH |
354 | [AF_VSOCK] = { |
355 | .dbs = VSOCK_DBM, | |
356 | .states = SS_CONN, | |
357 | }, | |
5caf79a0 GM |
358 | [AF_TIPC] = { |
359 | .dbs = (1 << TIPC_DB), | |
360 | .states = TIPC_SS_CONN, | |
361 | }, | |
2abc3d76 BT |
362 | [AF_XDP] = { |
363 | .dbs = (1 << XDP_DB), | |
364 | .states = (1 << SS_CLOSE), | |
365 | }, | |
9db7bf15 VK |
366 | }; |
367 | ||
368 | static int do_default = 1; | |
369 | static struct filter current_filter; | |
370 | ||
c121111e | 371 | static void filter_db_set(struct filter *f, int db, bool enable) |
9db7bf15 | 372 | { |
c121111e PS |
373 | if (enable) { |
374 | f->states |= default_dbs[db].states; | |
375 | f->dbs |= 1 << db; | |
376 | } else { | |
377 | f->dbs &= ~(1 << db); | |
378 | } | |
9db7bf15 VK |
379 | do_default = 0; |
380 | } | |
381 | ||
67d5fd55 PS |
382 | static int filter_db_parse(struct filter *f, const char *s) |
383 | { | |
384 | const struct { | |
385 | const char *name; | |
386 | int dbs[MAX_DB + 1]; | |
387 | } db_name_tbl[] = { | |
388 | #define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } } | |
9c3be2c0 | 389 | ENTRY(all, UDP_DB, DCCP_DB, TCP_DB, MPTCP_DB, RAW_DB, |
67d5fd55 PS |
390 | UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB, |
391 | PACKET_R_DB, PACKET_DG_DB, NETLINK_DB, | |
2abc3d76 | 392 | SCTP_DB, VSOCK_ST_DB, VSOCK_DG_DB, XDP_DB), |
9c3be2c0 | 393 | ENTRY(inet, UDP_DB, DCCP_DB, TCP_DB, MPTCP_DB, SCTP_DB, RAW_DB), |
67d5fd55 PS |
394 | ENTRY(udp, UDP_DB), |
395 | ENTRY(dccp, DCCP_DB), | |
396 | ENTRY(tcp, TCP_DB), | |
9c3be2c0 | 397 | ENTRY(mptcp, MPTCP_DB), |
67d5fd55 PS |
398 | ENTRY(sctp, SCTP_DB), |
399 | ENTRY(raw, RAW_DB), | |
400 | ENTRY(unix, UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB), | |
401 | ENTRY(unix_stream, UNIX_ST_DB), | |
402 | ENTRY(u_str, UNIX_ST_DB), /* alias for unix_stream */ | |
403 | ENTRY(unix_dgram, UNIX_DG_DB), | |
404 | ENTRY(u_dgr, UNIX_DG_DB), /* alias for unix_dgram */ | |
405 | ENTRY(unix_seqpacket, UNIX_SQ_DB), | |
406 | ENTRY(u_seq, UNIX_SQ_DB), /* alias for unix_seqpacket */ | |
407 | ENTRY(packet, PACKET_R_DB, PACKET_DG_DB), | |
408 | ENTRY(packet_raw, PACKET_R_DB), | |
409 | ENTRY(p_raw, PACKET_R_DB), /* alias for packet_raw */ | |
410 | ENTRY(packet_dgram, PACKET_DG_DB), | |
411 | ENTRY(p_dgr, PACKET_DG_DB), /* alias for packet_dgram */ | |
412 | ENTRY(netlink, NETLINK_DB), | |
413 | ENTRY(vsock, VSOCK_ST_DB, VSOCK_DG_DB), | |
414 | ENTRY(vsock_stream, VSOCK_ST_DB), | |
415 | ENTRY(v_str, VSOCK_ST_DB), /* alias for vsock_stream */ | |
416 | ENTRY(vsock_dgram, VSOCK_DG_DB), | |
417 | ENTRY(v_dgr, VSOCK_DG_DB), /* alias for vsock_dgram */ | |
2abc3d76 | 418 | ENTRY(xdp, XDP_DB), |
67d5fd55 PS |
419 | #undef ENTRY |
420 | }; | |
421 | bool enable = true; | |
422 | unsigned int i; | |
423 | const int *dbp; | |
424 | ||
425 | if (s[0] == '!') { | |
426 | enable = false; | |
427 | s++; | |
428 | } | |
429 | for (i = 0; i < ARRAY_SIZE(db_name_tbl); i++) { | |
430 | if (strcmp(s, db_name_tbl[i].name)) | |
431 | continue; | |
432 | for (dbp = db_name_tbl[i].dbs; *dbp != MAX_DB; dbp++) | |
433 | filter_db_set(f, *dbp, enable); | |
434 | return 0; | |
435 | } | |
436 | return -1; | |
437 | } | |
438 | ||
9db7bf15 VK |
439 | static void filter_af_set(struct filter *f, int af) |
440 | { | |
1527a17e | 441 | f->states |= default_afs[af].states; |
b338a3e7 | 442 | f->families |= FAMILY_MASK(af); |
1527a17e VK |
443 | do_default = 0; |
444 | preferred_family = af; | |
9db7bf15 VK |
445 | } |
446 | ||
447 | static int filter_af_get(struct filter *f, int af) | |
448 | { | |
b338a3e7 | 449 | return !!(f->families & FAMILY_MASK(af)); |
9db7bf15 VK |
450 | } |
451 | ||
57ff5a10 | 452 | static void filter_states_set(struct filter *f, int states) |
9db7bf15 | 453 | { |
57ff5a10 | 454 | if (states) |
9d320e1e | 455 | f->states = states; |
57ff5a10 | 456 | } |
9db7bf15 | 457 | |
57ff5a10 VK |
458 | static void filter_merge_defaults(struct filter *f) |
459 | { | |
460 | int db; | |
461 | int af; | |
9db7bf15 | 462 | |
57ff5a10 VK |
463 | for (db = 0; db < MAX_DB; db++) { |
464 | if (!(f->dbs & (1 << db))) | |
465 | continue; | |
9db7bf15 | 466 | |
57ff5a10 VK |
467 | if (!(default_dbs[db].families & f->families)) |
468 | f->families |= default_dbs[db].families; | |
469 | } | |
470 | for (af = 0; af < AF_MAX; af++) { | |
b338a3e7 | 471 | if (!(f->families & FAMILY_MASK(af))) |
57ff5a10 VK |
472 | continue; |
473 | ||
474 | if (!(default_afs[af].dbs & f->dbs)) | |
475 | f->dbs |= default_afs[af].dbs; | |
476 | } | |
9db7bf15 | 477 | } |
aba5acdf | 478 | |
ab01dbbb | 479 | static FILE *generic_proc_open(const char *env, const char *name) |
aba5acdf | 480 | { |
ab01dbbb | 481 | const char *p = getenv(env); |
aba5acdf | 482 | char store[128]; |
ab01dbbb | 483 | |
aba5acdf SH |
484 | if (!p) { |
485 | p = getenv("PROC_ROOT") ? : "/proc"; | |
486 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
487 | p = store; | |
488 | } | |
ab01dbbb SH |
489 | |
490 | return fopen(p, "r"); | |
aba5acdf | 491 | } |
0cb74a86 PS |
492 | #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp") |
493 | #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6") | |
494 | #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp") | |
495 | #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6") | |
496 | #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw") | |
497 | #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6") | |
498 | #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix") | |
499 | #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \ | |
500 | "net/packet") | |
501 | #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \ | |
502 | "net/netlink") | |
0cb74a86 PS |
503 | #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \ |
504 | "net/sockstat") | |
505 | #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \ | |
506 | "net/sockstat6") | |
507 | #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp") | |
508 | #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \ | |
509 | "sys/net/ipv4/ip_local_port_range") | |
aba5acdf | 510 | |
fbc0f876 SF |
511 | struct user_ent { |
512 | struct user_ent *next; | |
513 | unsigned int ino; | |
514 | int pid; | |
515 | int fd; | |
116ac927 RH |
516 | char *process; |
517 | char *process_ctx; | |
518 | char *socket_ctx; | |
fbc0f876 SF |
519 | }; |
520 | ||
521 | #define USER_ENT_HASH_SIZE 256 | |
3f7bd0fd | 522 | static struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; |
fbc0f876 SF |
523 | |
524 | static int user_ent_hashfn(unsigned int ino) | |
aba5acdf | 525 | { |
fbc0f876 | 526 | int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino; |
aba5acdf | 527 | |
fbc0f876 SF |
528 | return val & (USER_ENT_HASH_SIZE - 1); |
529 | } | |
530 | ||
116ac927 RH |
531 | static void user_ent_add(unsigned int ino, char *process, |
532 | int pid, int fd, | |
533 | char *proc_ctx, | |
534 | char *sock_ctx) | |
fbc0f876 SF |
535 | { |
536 | struct user_ent *p, **pp; | |
aba5acdf | 537 | |
116ac927 RH |
538 | p = malloc(sizeof(struct user_ent)); |
539 | if (!p) { | |
540 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
fbc0f876 | 541 | abort(); |
116ac927 | 542 | } |
fbc0f876 SF |
543 | p->next = NULL; |
544 | p->ino = ino; | |
545 | p->pid = pid; | |
546 | p->fd = fd; | |
116ac927 RH |
547 | p->process = strdup(process); |
548 | p->process_ctx = strdup(proc_ctx); | |
549 | p->socket_ctx = strdup(sock_ctx); | |
fbc0f876 SF |
550 | |
551 | pp = &user_ent_hash[user_ent_hashfn(ino)]; | |
552 | p->next = *pp; | |
553 | *pp = p; | |
554 | } | |
aba5acdf | 555 | |
116ac927 RH |
556 | static void user_ent_destroy(void) |
557 | { | |
558 | struct user_ent *p, *p_next; | |
559 | int cnt = 0; | |
560 | ||
561 | while (cnt != USER_ENT_HASH_SIZE) { | |
562 | p = user_ent_hash[cnt]; | |
563 | while (p) { | |
564 | free(p->process); | |
565 | free(p->process_ctx); | |
566 | free(p->socket_ctx); | |
567 | p_next = p->next; | |
568 | free(p); | |
569 | p = p_next; | |
570 | } | |
571 | cnt++; | |
572 | } | |
573 | } | |
574 | ||
fbc0f876 SF |
575 | static void user_ent_hash_build(void) |
576 | { | |
577 | const char *root = getenv("PROC_ROOT") ? : "/proc/"; | |
578 | struct dirent *d; | |
579 | char name[1024]; | |
580 | int nameoff; | |
581 | DIR *dir; | |
116ac927 RH |
582 | char *pid_context; |
583 | char *sock_context; | |
584 | const char *no_ctx = "unavailable"; | |
b25bad2f | 585 | static int user_ent_hash_build_init; |
116ac927 RH |
586 | |
587 | /* If show_users & show_proc_ctx set only do this once */ | |
588 | if (user_ent_hash_build_init != 0) | |
589 | return; | |
590 | ||
591 | user_ent_hash_build_init = 1; | |
fbc0f876 | 592 | |
18f156bf | 593 | strlcpy(name, root, sizeof(name)); |
0ee9052f | 594 | |
fbc0f876 | 595 | if (strlen(name) == 0 || name[strlen(name)-1] != '/') |
aba5acdf | 596 | strcat(name, "/"); |
fbc0f876 | 597 | |
aba5acdf | 598 | nameoff = strlen(name); |
fbc0f876 SF |
599 | |
600 | dir = opendir(name); | |
601 | if (!dir) | |
602 | return; | |
aba5acdf SH |
603 | |
604 | while ((d = readdir(dir)) != NULL) { | |
aba5acdf | 605 | struct dirent *d1; |
aba5acdf | 606 | char process[16]; |
116ac927 | 607 | char *p; |
fbc0f876 SF |
608 | int pid, pos; |
609 | DIR *dir1; | |
610 | char crap; | |
aba5acdf SH |
611 | |
612 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) | |
613 | continue; | |
614 | ||
116ac927 RH |
615 | if (getpidcon(pid, &pid_context) != 0) |
616 | pid_context = strdup(no_ctx); | |
617 | ||
0ee9052f | 618 | snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); |
aba5acdf | 619 | pos = strlen(name); |
a02371fb PS |
620 | if ((dir1 = opendir(name)) == NULL) { |
621 | free(pid_context); | |
aba5acdf | 622 | continue; |
a02371fb | 623 | } |
aba5acdf | 624 | |
fbc0f876 | 625 | process[0] = '\0'; |
116ac927 | 626 | p = process; |
aba5acdf SH |
627 | |
628 | while ((d1 = readdir(dir1)) != NULL) { | |
fbc0f876 SF |
629 | const char *pattern = "socket:["; |
630 | unsigned int ino; | |
aba5acdf | 631 | char lnk[64]; |
18445b3e | 632 | int fd; |
788731b3 | 633 | ssize_t link_len; |
116ac927 | 634 | char tmp[1024]; |
aba5acdf SH |
635 | |
636 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) | |
637 | continue; | |
638 | ||
0ee9052f | 639 | snprintf(name+pos, sizeof(name) - pos, "%d", fd); |
788731b3 TJ |
640 | |
641 | link_len = readlink(name, lnk, sizeof(lnk)-1); | |
642 | if (link_len == -1) | |
643 | continue; | |
644 | lnk[link_len] = '\0'; | |
645 | ||
646 | if (strncmp(lnk, pattern, strlen(pattern))) | |
aba5acdf SH |
647 | continue; |
648 | ||
fbc0f876 | 649 | sscanf(lnk, "socket:[%u]", &ino); |
aba5acdf | 650 | |
116ac927 RH |
651 | snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s", |
652 | root, pid, d1->d_name); | |
653 | ||
654 | if (getfilecon(tmp, &sock_context) <= 0) | |
655 | sock_context = strdup(no_ctx); | |
656 | ||
657 | if (*p == '\0') { | |
aba5acdf | 658 | FILE *fp; |
fbc0f876 | 659 | |
116ac927 RH |
660 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", |
661 | root, pid); | |
aba5acdf | 662 | if ((fp = fopen(tmp, "r")) != NULL) { |
d572ed4d PS |
663 | if (fscanf(fp, "%*d (%[^)])", p) < 1) |
664 | ; /* ignore */ | |
aba5acdf SH |
665 | fclose(fp); |
666 | } | |
667 | } | |
116ac927 RH |
668 | user_ent_add(ino, p, pid, fd, |
669 | pid_context, sock_context); | |
670 | free(sock_context); | |
aba5acdf | 671 | } |
116ac927 | 672 | free(pid_context); |
aba5acdf SH |
673 | closedir(dir1); |
674 | } | |
675 | closedir(dir); | |
fbc0f876 SF |
676 | } |
677 | ||
116ac927 RH |
678 | enum entry_types { |
679 | USERS, | |
680 | PROC_CTX, | |
681 | PROC_SOCK_CTX | |
682 | }; | |
683 | ||
684 | #define ENTRY_BUF_SIZE 512 | |
acd1e437 | 685 | static int find_entry(unsigned int ino, char **buf, int type) |
fbc0f876 SF |
686 | { |
687 | struct user_ent *p; | |
688 | int cnt = 0; | |
689 | char *ptr; | |
532ca40a | 690 | char *new_buf; |
116ac927 RH |
691 | int len, new_buf_len; |
692 | int buf_used = 0; | |
693 | int buf_len = 0; | |
fbc0f876 SF |
694 | |
695 | if (!ino) | |
696 | return 0; | |
697 | ||
698 | p = user_ent_hash[user_ent_hashfn(ino)]; | |
116ac927 | 699 | ptr = *buf = NULL; |
fbc0f876 SF |
700 | while (p) { |
701 | if (p->ino != ino) | |
702 | goto next; | |
703 | ||
116ac927 RH |
704 | while (1) { |
705 | ptr = *buf + buf_used; | |
706 | switch (type) { | |
707 | case USERS: | |
708 | len = snprintf(ptr, buf_len - buf_used, | |
709 | "(\"%s\",pid=%d,fd=%d),", | |
710 | p->process, p->pid, p->fd); | |
711 | break; | |
712 | case PROC_CTX: | |
713 | len = snprintf(ptr, buf_len - buf_used, | |
714 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),", | |
715 | p->process, p->pid, | |
716 | p->process_ctx, p->fd); | |
717 | break; | |
718 | case PROC_SOCK_CTX: | |
719 | len = snprintf(ptr, buf_len - buf_used, | |
720 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),", | |
721 | p->process, p->pid, | |
722 | p->process_ctx, p->fd, | |
723 | p->socket_ctx); | |
724 | break; | |
725 | default: | |
726 | fprintf(stderr, "ss: invalid type: %d\n", type); | |
727 | abort(); | |
728 | } | |
fbc0f876 | 729 | |
116ac927 RH |
730 | if (len < 0 || len >= buf_len - buf_used) { |
731 | new_buf_len = buf_len + ENTRY_BUF_SIZE; | |
532ca40a | 732 | new_buf = realloc(*buf, new_buf_len); |
116ac927 RH |
733 | if (!new_buf) { |
734 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
735 | abort(); | |
736 | } | |
532ca40a | 737 | *buf = new_buf; |
116ac927 RH |
738 | buf_len = new_buf_len; |
739 | continue; | |
740 | } else { | |
741 | buf_used += len; | |
742 | break; | |
743 | } | |
744 | } | |
fbc0f876 | 745 | cnt++; |
116ac927 | 746 | next: |
fbc0f876 SF |
747 | p = p->next; |
748 | } | |
116ac927 RH |
749 | if (buf_used) { |
750 | ptr = *buf + buf_used; | |
fbc0f876 | 751 | ptr[-1] = '\0'; |
116ac927 | 752 | } |
aba5acdf SH |
753 | return cnt; |
754 | } | |
755 | ||
2e7e805d | 756 | static unsigned long long cookie_sk_get(const uint32_t *cookie) |
f1b39e1b | 757 | { |
2e7e805d | 758 | return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0]; |
f1b39e1b VK |
759 | } |
760 | ||
f89d46ad PS |
761 | static const char *sctp_sstate_name[] = { |
762 | [SCTP_STATE_CLOSED] = "CLOSED", | |
763 | [SCTP_STATE_COOKIE_WAIT] = "COOKIE_WAIT", | |
764 | [SCTP_STATE_COOKIE_ECHOED] = "COOKIE_ECHOED", | |
765 | [SCTP_STATE_ESTABLISHED] = "ESTAB", | |
766 | [SCTP_STATE_SHUTDOWN_PENDING] = "SHUTDOWN_PENDING", | |
767 | [SCTP_STATE_SHUTDOWN_SENT] = "SHUTDOWN_SENT", | |
768 | [SCTP_STATE_SHUTDOWN_RECEIVED] = "SHUTDOWN_RECEIVED", | |
769 | [SCTP_STATE_SHUTDOWN_ACK_SENT] = "ACK_SENT", | |
770 | }; | |
771 | ||
5caf79a0 GM |
772 | static const char * const stype_nameg[] = { |
773 | "UNKNOWN", | |
774 | [SOCK_STREAM] = "STREAM", | |
775 | [SOCK_DGRAM] = "DGRAM", | |
776 | [SOCK_RDM] = "RDM", | |
777 | [SOCK_SEQPACKET] = "SEQPACKET", | |
778 | }; | |
779 | ||
acd1e437 | 780 | struct sockstat { |
ec4d0d8a VK |
781 | struct sockstat *next; |
782 | unsigned int type; | |
89f634f9 | 783 | uint16_t prot; |
41fe6c34 | 784 | uint16_t raw_prot; |
8250bc9f VK |
785 | inet_prefix local; |
786 | inet_prefix remote; | |
787 | int lport; | |
788 | int rport; | |
789 | int state; | |
790 | int rq, wq; | |
acd1e437 SH |
791 | unsigned int ino; |
792 | unsigned int uid; | |
8250bc9f VK |
793 | int refcnt; |
794 | unsigned int iface; | |
795 | unsigned long long sk; | |
99bb68ff VK |
796 | char *name; |
797 | char *peer_name; | |
ec75249b | 798 | __u32 mark; |
14f4bda5 | 799 | __u64 cgroup_id; |
055840f2 VK |
800 | }; |
801 | ||
acd1e437 | 802 | struct dctcpstat { |
055840f2 VK |
803 | unsigned int ce_state; |
804 | unsigned int alpha; | |
805 | unsigned int ab_ecn; | |
806 | unsigned int ab_tot; | |
807 | bool enabled; | |
808 | }; | |
809 | ||
acd1e437 | 810 | struct tcpstat { |
055840f2 | 811 | struct sockstat ss; |
4cbf5224 PS |
812 | unsigned int timer; |
813 | unsigned int timeout; | |
8250bc9f | 814 | int probes; |
d2055ea5 | 815 | char cong_alg[16]; |
8250bc9f | 816 | double rto, ato, rtt, rttvar; |
d1f338b3 | 817 | int qack, ssthresh, backoff; |
8250bc9f VK |
818 | double send_bps; |
819 | int snd_wscale; | |
820 | int rcv_wscale; | |
821 | int mss; | |
38e6dbc4 ED |
822 | int rcv_mss; |
823 | int advmss; | |
3d791a32 | 824 | unsigned int pmtu; |
d1f338b3 | 825 | unsigned int cwnd; |
8250bc9f VK |
826 | unsigned int lastsnd; |
827 | unsigned int lastrcv; | |
828 | unsigned int lastack; | |
829 | double pacing_rate; | |
830 | double pacing_rate_max; | |
2f579872 | 831 | double delivery_rate; |
1a4dda71 ED |
832 | unsigned long long bytes_acked; |
833 | unsigned long long bytes_received; | |
ecb435ea CG |
834 | unsigned int segs_out; |
835 | unsigned int segs_in; | |
414aeec9 MKL |
836 | unsigned int data_segs_out; |
837 | unsigned int data_segs_in; | |
8250bc9f VK |
838 | unsigned int unacked; |
839 | unsigned int retrans; | |
840 | unsigned int retrans_total; | |
841 | unsigned int lost; | |
842 | unsigned int sacked; | |
843 | unsigned int fackets; | |
844 | unsigned int reordering; | |
9e99e495 | 845 | unsigned int not_sent; |
2f4d834b ED |
846 | unsigned int delivered; |
847 | unsigned int delivered_ce; | |
5eead627 ED |
848 | unsigned int dsack_dups; |
849 | unsigned int reord_seen; | |
8250bc9f | 850 | double rcv_rtt; |
9e99e495 | 851 | double min_rtt; |
8250bc9f | 852 | int rcv_space; |
00ac78d3 | 853 | unsigned int rcv_ssthresh; |
b6c7fc61 YC |
854 | unsigned long long busy_time; |
855 | unsigned long long rwnd_limited; | |
856 | unsigned long long sndbuf_limited; | |
5eead627 ED |
857 | unsigned long long bytes_sent; |
858 | unsigned long long bytes_retrans; | |
8250bc9f VK |
859 | bool has_ts_opt; |
860 | bool has_sack_opt; | |
861 | bool has_ecn_opt; | |
862 | bool has_ecnseen_opt; | |
863 | bool has_fastopen_opt; | |
864 | bool has_wscale_opt; | |
2f579872 | 865 | bool app_limited; |
8250bc9f | 866 | struct dctcpstat *dctcp; |
2f0f9aef | 867 | struct tcp_bbr_info *bbr_info; |
aba5acdf SH |
868 | }; |
869 | ||
f89d46ad PS |
870 | /* SCTP assocs share the same inode number with their parent endpoint. So if we |
871 | * have seen the inode number before, it must be an assoc instead of the next | |
872 | * endpoint. */ | |
873 | static bool is_sctp_assoc(struct sockstat *s, const char *sock_name) | |
874 | { | |
875 | if (strcmp(sock_name, "sctp")) | |
876 | return false; | |
877 | if (!sctp_ino || sctp_ino != s->ino) | |
878 | return false; | |
879 | return true; | |
880 | } | |
881 | ||
be7e4d20 PS |
882 | static const char *unix_netid_name(int type) |
883 | { | |
884 | switch (type) { | |
885 | case SOCK_STREAM: | |
886 | return "u_str"; | |
887 | case SOCK_SEQPACKET: | |
888 | return "u_seq"; | |
889 | case SOCK_DGRAM: | |
890 | default: | |
891 | return "u_dgr"; | |
892 | } | |
893 | } | |
894 | ||
895 | static const char *proto_name(int protocol) | |
2d791bc8 | 896 | { |
be7e4d20 PS |
897 | switch (protocol) { |
898 | case 0: | |
899 | return "raw"; | |
900 | case IPPROTO_UDP: | |
901 | return "udp"; | |
902 | case IPPROTO_TCP: | |
903 | return "tcp"; | |
9c3be2c0 PA |
904 | case IPPROTO_MPTCP: |
905 | return "mptcp"; | |
be7e4d20 PS |
906 | case IPPROTO_SCTP: |
907 | return "sctp"; | |
908 | case IPPROTO_DCCP: | |
909 | return "dccp"; | |
572e8936 PS |
910 | case IPPROTO_ICMPV6: |
911 | return "icmp6"; | |
be7e4d20 PS |
912 | } |
913 | ||
914 | return "???"; | |
915 | } | |
916 | ||
c759116a SH |
917 | static const char *vsock_netid_name(int type) |
918 | { | |
919 | switch (type) { | |
920 | case SOCK_STREAM: | |
921 | return "v_str"; | |
922 | case SOCK_DGRAM: | |
923 | return "v_dgr"; | |
924 | default: | |
925 | return "???"; | |
926 | } | |
927 | } | |
928 | ||
5caf79a0 GM |
929 | static const char *tipc_netid_name(int type) |
930 | { | |
931 | switch (type) { | |
932 | case SOCK_STREAM: | |
933 | return "ti_st"; | |
934 | case SOCK_DGRAM: | |
935 | return "ti_dg"; | |
936 | case SOCK_RDM: | |
937 | return "ti_rd"; | |
938 | case SOCK_SEQPACKET: | |
939 | return "ti_sq"; | |
940 | default: | |
941 | return "???"; | |
942 | } | |
943 | } | |
944 | ||
691bd854 SB |
945 | /* Allocate and initialize a new buffer chunk */ |
946 | static struct buf_chunk *buf_chunk_new(void) | |
947 | { | |
948 | struct buf_chunk *new = malloc(BUF_CHUNK); | |
949 | ||
950 | if (!new) | |
951 | abort(); | |
952 | ||
953 | new->next = NULL; | |
954 | ||
955 | /* This is also the last block */ | |
956 | buffer.tail = new; | |
957 | ||
958 | /* Next token will be stored at the beginning of chunk data area, and | |
959 | * its initial length is zero. | |
960 | */ | |
961 | buffer.cur = (struct buf_token *)new->data; | |
962 | buffer.cur->len = 0; | |
963 | ||
964 | new->end = buffer.cur->data; | |
965 | ||
aa5bd6a2 SB |
966 | buffer.chunks++; |
967 | ||
691bd854 SB |
968 | return new; |
969 | } | |
970 | ||
971 | /* Return available tail room in given chunk */ | |
972 | static int buf_chunk_avail(struct buf_chunk *chunk) | |
973 | { | |
974 | return BUF_CHUNK - offsetof(struct buf_chunk, data) - | |
975 | (chunk->end - chunk->data); | |
976 | } | |
977 | ||
978 | /* Update end pointer and token length, link new chunk if we hit the end of the | |
979 | * current one. Return -EAGAIN if we got a new chunk, caller has to print again. | |
980 | */ | |
981 | static int buf_update(int len) | |
982 | { | |
983 | struct buf_chunk *chunk = buffer.tail; | |
984 | struct buf_token *t = buffer.cur; | |
985 | ||
986 | /* Claim success if new content fits in the current chunk, and anyway | |
987 | * if this is the first token in the chunk: in the latter case, | |
988 | * allocating a new chunk won't help, so we'll just cut the output. | |
989 | */ | |
990 | if ((len < buf_chunk_avail(chunk) && len != -1 /* glibc < 2.0.6 */) || | |
991 | t == (struct buf_token *)chunk->data) { | |
992 | len = min(len, buf_chunk_avail(chunk)); | |
993 | ||
994 | /* Total field length can't exceed 2^16 bytes, cut as needed */ | |
995 | len = min(len, USHRT_MAX - t->len); | |
996 | ||
997 | chunk->end += len; | |
998 | t->len += len; | |
999 | return 0; | |
1000 | } | |
1001 | ||
1002 | /* Content truncated, time to allocate more */ | |
1003 | chunk->next = buf_chunk_new(); | |
1004 | ||
1005 | /* Copy current token over to new chunk, including length descriptor */ | |
1006 | memcpy(chunk->next->data, t, sizeof(t->len) + t->len); | |
1007 | chunk->next->end += t->len; | |
1008 | ||
1009 | /* Discard partially written field in old chunk */ | |
1010 | chunk->end -= t->len + sizeof(t->len); | |
1011 | ||
1012 | return -EAGAIN; | |
1013 | } | |
1014 | ||
1015 | /* Append content to buffer as part of the current field */ | |
5d5586b0 | 1016 | __attribute__((format(printf, 1, 2))) |
90351722 SB |
1017 | static void out(const char *fmt, ...) |
1018 | { | |
59f46b7b | 1019 | struct column *f = current_field; |
90351722 | 1020 | va_list args; |
691bd854 SB |
1021 | char *pos; |
1022 | int len; | |
1023 | ||
87b1a7ae | 1024 | if (f->disabled) |
691bd854 SB |
1025 | return; |
1026 | ||
1027 | if (!buffer.head) | |
1028 | buffer.head = buf_chunk_new(); | |
1029 | ||
1030 | again: /* Append to buffer: if we have a new chunk, print again */ | |
90351722 | 1031 | |
691bd854 | 1032 | pos = buffer.cur->data + buffer.cur->len; |
90351722 | 1033 | va_start(args, fmt); |
691bd854 SB |
1034 | |
1035 | /* Limit to tail room. If we hit the limit, buf_update() will tell us */ | |
1036 | len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, args); | |
90351722 | 1037 | va_end(args); |
691bd854 SB |
1038 | |
1039 | if (buf_update(len)) | |
1040 | goto again; | |
90351722 SB |
1041 | } |
1042 | ||
691bd854 | 1043 | static int print_left_spacing(struct column *f, int stored, int printed) |
59f46b7b SB |
1044 | { |
1045 | int s; | |
1046 | ||
87b1a7ae | 1047 | if (!f->width || f->align == ALIGN_LEFT) |
59f46b7b SB |
1048 | return 0; |
1049 | ||
691bd854 | 1050 | s = f->width - stored - printed; |
59f46b7b SB |
1051 | if (f->align == ALIGN_CENTER) |
1052 | /* If count of total spacing is odd, shift right by one */ | |
1053 | s = (s + 1) / 2; | |
1054 | ||
1055 | if (s > 0) | |
1056 | return printf("%*c", s, ' '); | |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
691bd854 | 1061 | static void print_right_spacing(struct column *f, int printed) |
59f46b7b SB |
1062 | { |
1063 | int s; | |
1064 | ||
87b1a7ae | 1065 | if (!f->width || f->align == ALIGN_RIGHT) |
59f46b7b SB |
1066 | return; |
1067 | ||
691bd854 | 1068 | s = f->width - printed; |
59f46b7b SB |
1069 | if (f->align == ALIGN_CENTER) |
1070 | s /= 2; | |
1071 | ||
1072 | if (s > 0) | |
1073 | printf("%*c", s, ' '); | |
1074 | } | |
1075 | ||
691bd854 | 1076 | /* Done with field: update buffer pointer, start new token after current one */ |
59f46b7b SB |
1077 | static void field_flush(struct column *f) |
1078 | { | |
192be8fc AQ |
1079 | struct buf_chunk *chunk; |
1080 | unsigned int pad; | |
691bd854 | 1081 | |
87b1a7ae | 1082 | if (f->disabled) |
59f46b7b SB |
1083 | return; |
1084 | ||
192be8fc AQ |
1085 | chunk = buffer.tail; |
1086 | pad = buffer.cur->len % 2; | |
1087 | ||
87b1a7ae SB |
1088 | if (buffer.cur->len > f->max_len) |
1089 | f->max_len = buffer.cur->len; | |
1090 | ||
691bd854 SB |
1091 | /* We need a new chunk if we can't store the next length descriptor. |
1092 | * Mind the gap between end of previous token and next aligned position | |
1093 | * for length descriptor. | |
1094 | */ | |
1095 | if (buf_chunk_avail(chunk) - pad < sizeof(buffer.cur->len)) { | |
1096 | chunk->end += pad; | |
1097 | chunk->next = buf_chunk_new(); | |
1098 | return; | |
1099 | } | |
59f46b7b | 1100 | |
691bd854 SB |
1101 | buffer.cur = (struct buf_token *)(buffer.cur->data + |
1102 | LEN_ALIGN(buffer.cur->len)); | |
1103 | buffer.cur->len = 0; | |
1104 | buffer.tail->end = buffer.cur->data; | |
59f46b7b SB |
1105 | } |
1106 | ||
1107 | static int field_is_last(struct column *f) | |
1108 | { | |
1109 | return f - columns == COL_MAX - 1; | |
1110 | } | |
1111 | ||
691bd854 SB |
1112 | /* Get the next available token in the buffer starting from the current token */ |
1113 | static struct buf_token *buf_token_next(struct buf_token *cur) | |
1114 | { | |
1115 | struct buf_chunk *chunk = buffer.tail; | |
1116 | ||
1117 | /* If we reached the end of chunk contents, get token from next chunk */ | |
1118 | if (cur->data + LEN_ALIGN(cur->len) == chunk->end) { | |
1119 | buffer.tail = chunk = chunk->next; | |
1120 | return chunk ? (struct buf_token *)chunk->data : NULL; | |
1121 | } | |
1122 | ||
1123 | return (struct buf_token *)(cur->data + LEN_ALIGN(cur->len)); | |
1124 | } | |
1125 | ||
1126 | /* Free up all allocated buffer chunks */ | |
1127 | static void buf_free_all(void) | |
1128 | { | |
1129 | struct buf_chunk *tmp; | |
1130 | ||
1131 | for (buffer.tail = buffer.head; buffer.tail; ) { | |
1132 | tmp = buffer.tail; | |
1133 | buffer.tail = buffer.tail->next; | |
1134 | free(tmp); | |
1135 | } | |
1136 | buffer.head = NULL; | |
aa5bd6a2 | 1137 | buffer.chunks = 0; |
691bd854 SB |
1138 | } |
1139 | ||
c4f58629 | 1140 | /* Get current screen width, returns -1 if TIOCGWINSZ fails */ |
32ea3d54 SB |
1141 | static int render_screen_width(void) |
1142 | { | |
c4f58629 | 1143 | int width = -1; |
32ea3d54 SB |
1144 | |
1145 | if (isatty(STDOUT_FILENO)) { | |
1146 | struct winsize w; | |
1147 | ||
1148 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { | |
1149 | if (w.ws_col > 0) | |
1150 | width = w.ws_col; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | return width; | |
1155 | } | |
1156 | ||
87b1a7ae SB |
1157 | /* Calculate column width from contents length. If columns don't fit on one |
1158 | * line, break them into the least possible amount of lines and keep them | |
1159 | * aligned across lines. Available screen space is equally spread between fields | |
1160 | * as additional spacing. | |
1161 | */ | |
32ea3d54 | 1162 | static void render_calc_width(void) |
87b1a7ae | 1163 | { |
c4f58629 | 1164 | int screen_width, first, len = 0, linecols = 0; |
87b1a7ae | 1165 | struct column *c, *eol = columns - 1; |
c4f58629 PJ |
1166 | bool compact_output = false; |
1167 | ||
1168 | screen_width = render_screen_width(); | |
1169 | if (screen_width == -1) { | |
1170 | screen_width = INT_MAX; | |
1171 | compact_output = true; | |
1172 | } | |
87b1a7ae SB |
1173 | |
1174 | /* First pass: set width for each column to measured content length */ | |
1175 | for (first = 1, c = columns; c - columns < COL_MAX; c++) { | |
1176 | if (c->disabled) | |
1177 | continue; | |
1178 | ||
1179 | if (!first && c->max_len) | |
1180 | c->width = c->max_len + strlen(c->ldelim); | |
1181 | else | |
1182 | c->width = c->max_len; | |
1183 | ||
1184 | /* But don't exceed screen size. If we exceed the screen size | |
1185 | * for even a single field, it will just start on a line of its | |
1186 | * own and then naturally wrap. | |
1187 | */ | |
1188 | c->width = min(c->width, screen_width); | |
1189 | ||
1190 | if (c->width) | |
1191 | first = 0; | |
1192 | } | |
1193 | ||
c4f58629 PJ |
1194 | if (compact_output) { |
1195 | /* Compact output, skip extending columns. */ | |
1196 | return; | |
1197 | } | |
1198 | ||
87b1a7ae SB |
1199 | /* Second pass: find out newlines and distribute available spacing */ |
1200 | for (c = columns; c - columns < COL_MAX; c++) { | |
1201 | int pad, spacing, rem, last; | |
1202 | struct column *tmp; | |
1203 | ||
1204 | if (!c->width) | |
1205 | continue; | |
1206 | ||
1207 | linecols++; | |
1208 | len += c->width; | |
1209 | ||
1210 | for (last = 1, tmp = c + 1; tmp - columns < COL_MAX; tmp++) { | |
1211 | if (tmp->width) { | |
1212 | last = 0; | |
1213 | break; | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | if (!last && len < screen_width) { | |
1218 | /* Columns fit on screen so far, nothing to do yet */ | |
1219 | continue; | |
1220 | } | |
1221 | ||
1222 | if (len == screen_width) { | |
1223 | /* Exact fit, just start with new line */ | |
1224 | goto newline; | |
1225 | } | |
1226 | ||
1227 | if (len > screen_width) { | |
1228 | /* Screen width exceeded: go back one column */ | |
1229 | len -= c->width; | |
1230 | c--; | |
1231 | linecols--; | |
1232 | } | |
1233 | ||
1234 | /* Distribute remaining space to columns on this line */ | |
1235 | pad = screen_width - len; | |
1236 | spacing = pad / linecols; | |
1237 | rem = pad % linecols; | |
1238 | for (tmp = c; tmp > eol; tmp--) { | |
1239 | if (!tmp->width) | |
1240 | continue; | |
1241 | ||
1242 | tmp->width += spacing; | |
1243 | if (rem) { | |
1244 | tmp->width++; | |
1245 | rem--; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | newline: | |
1250 | /* Line break: reset line counters, mark end-of-line */ | |
1251 | eol = c; | |
1252 | len = 0; | |
1253 | linecols = 0; | |
1254 | } | |
1255 | } | |
1256 | ||
691bd854 | 1257 | /* Render buffered output with spacing and delimiters, then free up buffers */ |
32ea3d54 | 1258 | static void render(void) |
691bd854 | 1259 | { |
eb8559ef | 1260 | struct buf_token *token; |
87b1a7ae | 1261 | int printed, line_started = 0; |
691bd854 SB |
1262 | struct column *f; |
1263 | ||
eb8559ef JPB |
1264 | if (!buffer.head) |
1265 | return; | |
1266 | ||
1267 | token = (struct buf_token *)buffer.head->data; | |
1268 | ||
691bd854 SB |
1269 | /* Ensure end alignment of last token, it wasn't necessarily flushed */ |
1270 | buffer.tail->end += buffer.cur->len % 2; | |
1271 | ||
32ea3d54 | 1272 | render_calc_width(); |
87b1a7ae | 1273 | |
691bd854 SB |
1274 | /* Rewind and replay */ |
1275 | buffer.tail = buffer.head; | |
1276 | ||
1277 | f = columns; | |
1278 | while (!f->width) | |
1279 | f++; | |
1280 | ||
1281 | while (token) { | |
1282 | /* Print left delimiter only if we already started a line */ | |
1283 | if (line_started++) | |
00240899 | 1284 | printed = printf("%s", f->ldelim); |
691bd854 SB |
1285 | else |
1286 | printed = 0; | |
1287 | ||
1288 | /* Print field content from token data with spacing */ | |
1289 | printed += print_left_spacing(f, token->len, printed); | |
1290 | printed += fwrite(token->data, 1, token->len, stdout); | |
1291 | print_right_spacing(f, printed); | |
1292 | ||
691bd854 SB |
1293 | /* Go to next non-empty field, deal with end-of-line */ |
1294 | do { | |
1295 | if (field_is_last(f)) { | |
87b1a7ae | 1296 | printf("\n"); |
691bd854 SB |
1297 | f = columns; |
1298 | line_started = 0; | |
1299 | } else { | |
1300 | f++; | |
1301 | } | |
87b1a7ae | 1302 | } while (f->disabled); |
691bd854 SB |
1303 | |
1304 | token = buf_token_next(token); | |
1305 | } | |
9eee92a4 BV |
1306 | /* Deal with final end-of-line when the last non-empty field printed |
1307 | * is not the last field. | |
1308 | */ | |
1309 | if (line_started) | |
1310 | printf("\n"); | |
691bd854 SB |
1311 | |
1312 | buf_free_all(); | |
32ea3d54 | 1313 | current_field = columns; |
691bd854 SB |
1314 | } |
1315 | ||
aa5bd6a2 SB |
1316 | /* Move to next field, and render buffer if we reached the maximum number of |
1317 | * chunks, at the last field in a line. | |
1318 | */ | |
1319 | static void field_next(void) | |
1320 | { | |
1321 | if (field_is_last(current_field) && buffer.chunks >= BUF_CHUNKS_MAX) { | |
1322 | render(); | |
1323 | return; | |
1324 | } | |
1325 | ||
1326 | field_flush(current_field); | |
1327 | if (field_is_last(current_field)) | |
1328 | current_field = columns; | |
1329 | else | |
1330 | current_field++; | |
1331 | } | |
1332 | ||
1333 | /* Walk through fields and flush them until we reach the desired one */ | |
1334 | static void field_set(enum col_id id) | |
1335 | { | |
1336 | while (id != current_field - columns) | |
1337 | field_next(); | |
1338 | } | |
1339 | ||
1340 | /* Print header for all non-empty columns */ | |
1341 | static void print_header(void) | |
1342 | { | |
1343 | while (!field_is_last(current_field)) { | |
1344 | if (!current_field->disabled) | |
1345 | out("%s", current_field->header); | |
1346 | field_next(); | |
1347 | } | |
1348 | } | |
1349 | ||
be7e4d20 PS |
1350 | static void sock_state_print(struct sockstat *s) |
1351 | { | |
1352 | const char *sock_name; | |
1882c0db PS |
1353 | static const char * const sstate_name[] = { |
1354 | "UNKNOWN", | |
1355 | [SS_ESTABLISHED] = "ESTAB", | |
1356 | [SS_SYN_SENT] = "SYN-SENT", | |
1357 | [SS_SYN_RECV] = "SYN-RECV", | |
1358 | [SS_FIN_WAIT1] = "FIN-WAIT-1", | |
1359 | [SS_FIN_WAIT2] = "FIN-WAIT-2", | |
1360 | [SS_TIME_WAIT] = "TIME-WAIT", | |
1361 | [SS_CLOSE] = "UNCONN", | |
1362 | [SS_CLOSE_WAIT] = "CLOSE-WAIT", | |
1363 | [SS_LAST_ACK] = "LAST-ACK", | |
1364 | [SS_LISTEN] = "LISTEN", | |
1365 | [SS_CLOSING] = "CLOSING", | |
1366 | }; | |
be7e4d20 PS |
1367 | |
1368 | switch (s->local.family) { | |
1369 | case AF_UNIX: | |
1370 | sock_name = unix_netid_name(s->type); | |
1371 | break; | |
1372 | case AF_INET: | |
1373 | case AF_INET6: | |
1374 | sock_name = proto_name(s->type); | |
1375 | break; | |
1376 | case AF_PACKET: | |
1377 | sock_name = s->type == SOCK_RAW ? "p_raw" : "p_dgr"; | |
1378 | break; | |
1379 | case AF_NETLINK: | |
1380 | sock_name = "nl"; | |
1381 | break; | |
5caf79a0 GM |
1382 | case AF_TIPC: |
1383 | sock_name = tipc_netid_name(s->type); | |
1384 | break; | |
c759116a SH |
1385 | case AF_VSOCK: |
1386 | sock_name = vsock_netid_name(s->type); | |
1387 | break; | |
2abc3d76 BT |
1388 | case AF_XDP: |
1389 | sock_name = "xdp"; | |
1390 | break; | |
be7e4d20 PS |
1391 | default: |
1392 | sock_name = "unknown"; | |
1393 | } | |
1394 | ||
59f46b7b SB |
1395 | if (is_sctp_assoc(s, sock_name)) { |
1396 | field_set(COL_STATE); /* Empty Netid field */ | |
1397 | out("`- %s", sctp_sstate_name[s->state]); | |
1398 | } else { | |
1399 | field_set(COL_NETID); | |
1400 | out("%s", sock_name); | |
1401 | field_set(COL_STATE); | |
1402 | out("%s", sstate_name[s->state]); | |
f89d46ad | 1403 | } |
2d791bc8 | 1404 | |
59f46b7b SB |
1405 | field_set(COL_RECVQ); |
1406 | out("%-6d", s->rq); | |
1407 | field_set(COL_SENDQ); | |
1408 | out("%-6d", s->wq); | |
1409 | field_set(COL_ADDR); | |
2d791bc8 VK |
1410 | } |
1411 | ||
f1b39e1b VK |
1412 | static void sock_details_print(struct sockstat *s) |
1413 | { | |
1414 | if (s->uid) | |
90351722 | 1415 | out(" uid:%u", s->uid); |
f1b39e1b | 1416 | |
90351722 SB |
1417 | out(" ino:%u", s->ino); |
1418 | out(" sk:%llx", s->sk); | |
ec75249b LC |
1419 | |
1420 | if (s->mark) | |
90351722 | 1421 | out(" fwmark:0x%x", s->mark); |
14f4bda5 DY |
1422 | |
1423 | if (s->cgroup_id) | |
1424 | out(" cgroup:%s", cg_id_to_path(s->cgroup_id)); | |
f1b39e1b VK |
1425 | } |
1426 | ||
b217df10 VK |
1427 | static void sock_addr_print(const char *addr, char *delim, const char *port, |
1428 | const char *ifname) | |
1429 | { | |
59f46b7b SB |
1430 | if (ifname) |
1431 | out("%s" "%%" "%s%s", addr, ifname, delim); | |
1432 | else | |
1433 | out("%s%s", addr, delim); | |
1434 | ||
1435 | field_next(); | |
1436 | out("%s", port); | |
1437 | field_next(); | |
b217df10 VK |
1438 | } |
1439 | ||
4cbf5224 | 1440 | static const char *print_ms_timer(unsigned int timeout) |
aba5acdf SH |
1441 | { |
1442 | static char buf[64]; | |
1443 | int secs, msecs, minutes; | |
acd1e437 | 1444 | |
aba5acdf SH |
1445 | secs = timeout/1000; |
1446 | minutes = secs/60; | |
1447 | secs = secs%60; | |
1448 | msecs = timeout%1000; | |
1449 | buf[0] = 0; | |
1450 | if (minutes) { | |
1451 | msecs = 0; | |
1452 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); | |
1453 | if (minutes > 9) | |
1454 | secs = 0; | |
1455 | } | |
1456 | if (secs) { | |
1457 | if (secs > 9) | |
1458 | msecs = 0; | |
1459 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); | |
1460 | } | |
1461 | if (msecs) | |
1462 | sprintf(buf+strlen(buf), "%03dms", msecs); | |
1463 | return buf; | |
e7113c61 | 1464 | } |
aba5acdf | 1465 | |
22588a0e | 1466 | struct scache { |
aba5acdf SH |
1467 | struct scache *next; |
1468 | int port; | |
1469 | char *name; | |
1470 | const char *proto; | |
1471 | }; | |
1472 | ||
3f7bd0fd | 1473 | static struct scache *rlist; |
aba5acdf | 1474 | |
d1f28cf1 | 1475 | static void init_service_resolver(void) |
aba5acdf SH |
1476 | { |
1477 | char buf[128]; | |
1478 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); | |
596307ea PS |
1479 | |
1480 | if (!fp) | |
1481 | return; | |
1482 | ||
1483 | if (!fgets(buf, sizeof(buf), fp)) { | |
2bcc3c16 | 1484 | pclose(fp); |
596307ea PS |
1485 | return; |
1486 | } | |
1487 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
1488 | unsigned int progn, port; | |
1489 | char proto[128], prog[128] = "rpc."; | |
1490 | struct scache *c; | |
1491 | ||
1492 | if (sscanf(buf, "%u %*d %s %u %s", | |
acd1e437 | 1493 | &progn, proto, &port, prog+4) != 4) |
596307ea PS |
1494 | continue; |
1495 | ||
1496 | if (!(c = malloc(sizeof(*c)))) | |
1497 | continue; | |
1498 | ||
1499 | c->port = port; | |
1500 | c->name = strdup(prog); | |
1501 | if (strcmp(proto, TCP_PROTO) == 0) | |
1502 | c->proto = TCP_PROTO; | |
1503 | else if (strcmp(proto, UDP_PROTO) == 0) | |
1504 | c->proto = UDP_PROTO; | |
f89d46ad PS |
1505 | else if (strcmp(proto, SCTP_PROTO) == 0) |
1506 | c->proto = SCTP_PROTO; | |
596307ea PS |
1507 | else |
1508 | c->proto = NULL; | |
1509 | c->next = rlist; | |
1510 | rlist = c; | |
aba5acdf | 1511 | } |
596307ea | 1512 | pclose(fp); |
aba5acdf SH |
1513 | } |
1514 | ||
ab61159a SH |
1515 | /* Even do not try default linux ephemeral port ranges: |
1516 | * default /etc/services contains so much of useless crap | |
1517 | * wouldbe "allocated" to this area that resolution | |
1518 | * is really harmful. I shrug each time when seeing | |
1519 | * "socks" or "cfinger" in dumps. | |
1520 | */ | |
1521 | static int is_ephemeral(int port) | |
1522 | { | |
acd1e437 | 1523 | static int min = 0, max; |
c29d3792 PS |
1524 | |
1525 | if (!min) { | |
ab01dbbb | 1526 | FILE *f = ephemeral_ports_open(); |
acd1e437 | 1527 | |
c29d3792 PS |
1528 | if (!f || fscanf(f, "%d %d", &min, &max) < 2) { |
1529 | min = 1024; | |
1530 | max = 4999; | |
ab61159a | 1531 | } |
c29d3792 PS |
1532 | if (f) |
1533 | fclose(f); | |
ab61159a | 1534 | } |
c29d3792 | 1535 | return port >= min && port <= max; |
ab61159a SH |
1536 | } |
1537 | ||
1538 | ||
d1f28cf1 | 1539 | static const char *__resolve_service(int port) |
aba5acdf SH |
1540 | { |
1541 | struct scache *c; | |
1542 | ||
1543 | for (c = rlist; c; c = c->next) { | |
1544 | if (c->port == port && c->proto == dg_proto) | |
1545 | return c->name; | |
1546 | } | |
1547 | ||
ab61159a | 1548 | if (!is_ephemeral(port)) { |
aba5acdf SH |
1549 | static int notfirst; |
1550 | struct servent *se; | |
acd1e437 | 1551 | |
aba5acdf SH |
1552 | if (!notfirst) { |
1553 | setservent(1); | |
1554 | notfirst = 1; | |
ae665a52 | 1555 | } |
aba5acdf SH |
1556 | se = getservbyport(htons(port), dg_proto); |
1557 | if (se) | |
1558 | return se->s_name; | |
1559 | } | |
1560 | ||
1561 | return NULL; | |
1562 | } | |
1563 | ||
22588a0e ED |
1564 | #define SCACHE_BUCKETS 1024 |
1565 | static struct scache *cache_htab[SCACHE_BUCKETS]; | |
aba5acdf | 1566 | |
d1f28cf1 | 1567 | static const char *resolve_service(int port) |
aba5acdf SH |
1568 | { |
1569 | static char buf[128]; | |
22588a0e ED |
1570 | struct scache *c; |
1571 | const char *res; | |
1572 | int hash; | |
aba5acdf SH |
1573 | |
1574 | if (port == 0) { | |
1575 | buf[0] = '*'; | |
1576 | buf[1] = 0; | |
1577 | return buf; | |
1578 | } | |
1579 | ||
ca697cee | 1580 | if (numeric) |
22588a0e | 1581 | goto do_numeric; |
aba5acdf | 1582 | |
22588a0e ED |
1583 | if (dg_proto == RAW_PROTO) |
1584 | return inet_proto_n2a(port, buf, sizeof(buf)); | |
1585 | ||
1586 | ||
1587 | hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS; | |
1588 | ||
1589 | for (c = cache_htab[hash]; c; c = c->next) { | |
1590 | if (c->port == port && c->proto == dg_proto) | |
1591 | goto do_cache; | |
aba5acdf SH |
1592 | } |
1593 | ||
22588a0e ED |
1594 | c = malloc(sizeof(*c)); |
1595 | if (!c) | |
1596 | goto do_numeric; | |
1597 | res = __resolve_service(port); | |
1598 | c->port = port; | |
1599 | c->name = res ? strdup(res) : NULL; | |
1600 | c->proto = dg_proto; | |
1601 | c->next = cache_htab[hash]; | |
1602 | cache_htab[hash] = c; | |
1603 | ||
1604 | do_cache: | |
1605 | if (c->name) | |
1606 | return c->name; | |
1607 | ||
1608 | do_numeric: | |
aba5acdf SH |
1609 | sprintf(buf, "%u", port); |
1610 | return buf; | |
1611 | } | |
1612 | ||
1267c0b9 PS |
1613 | static void inet_addr_print(const inet_prefix *a, int port, |
1614 | unsigned int ifindex, bool v6only) | |
aba5acdf SH |
1615 | { |
1616 | char buf[1024]; | |
1617 | const char *ap = buf; | |
b217df10 | 1618 | const char *ifname = NULL; |
aba5acdf SH |
1619 | |
1620 | if (a->family == AF_INET) { | |
1267c0b9 | 1621 | ap = format_host(AF_INET, 4, a->data); |
aba5acdf | 1622 | } else { |
1267c0b9 PS |
1623 | if (!v6only && |
1624 | !memcmp(a->data, &in6addr_any, sizeof(in6addr_any))) { | |
aba9c23a SH |
1625 | buf[0] = '*'; |
1626 | buf[1] = 0; | |
1627 | } else { | |
1628 | ap = format_host(a->family, 16, a->data); | |
1629 | ||
1630 | /* Numeric IPv6 addresses should be bracketed */ | |
1631 | if (strchr(ap, ':')) { | |
1632 | snprintf(buf, sizeof(buf), | |
1633 | "[%s]", ap); | |
1634 | ap = buf; | |
1635 | } | |
aba9c23a | 1636 | } |
aba5acdf | 1637 | } |
b217df10 | 1638 | |
59f46b7b SB |
1639 | if (ifindex) |
1640 | ifname = ll_index_to_name(ifindex); | |
7c8a3cfb | 1641 | |
59f46b7b | 1642 | sock_addr_print(ap, ":", resolve_service(port), ifname); |
aba5acdf SH |
1643 | } |
1644 | ||
acd1e437 | 1645 | struct aafilter { |
aba5acdf SH |
1646 | inet_prefix addr; |
1647 | int port; | |
2d293212 | 1648 | unsigned int iface; |
ec75249b LC |
1649 | __u32 mark; |
1650 | __u32 mask; | |
14f4bda5 | 1651 | __u64 cgroup_id; |
aba5acdf SH |
1652 | struct aafilter *next; |
1653 | }; | |
1654 | ||
d1f28cf1 SH |
1655 | static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, |
1656 | int plen) | |
aba5acdf SH |
1657 | { |
1658 | if (!inet_addr_match(a, p, plen)) | |
1659 | return 0; | |
7d105b56 | 1660 | |
aba5acdf SH |
1661 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches |
1662 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped | |
1663 | * sockets. Fair? */ | |
1664 | if (p->family == AF_INET && a->family == AF_INET6) { | |
1665 | if (a->data[0] == 0 && a->data[1] == 0 && | |
1666 | a->data[2] == htonl(0xffff)) { | |
1667 | inet_prefix tmp = *a; | |
acd1e437 | 1668 | |
aba5acdf SH |
1669 | tmp.data[0] = a->data[3]; |
1670 | return inet_addr_match(&tmp, p, plen); | |
1671 | } | |
1672 | } | |
1673 | return 1; | |
1674 | } | |
1675 | ||
d1f28cf1 | 1676 | static int unix_match(const inet_prefix *a, const inet_prefix *p) |
aba5acdf | 1677 | { |
99bb68ff | 1678 | char *addr, *pattern; |
acd1e437 | 1679 | |
99bb68ff VK |
1680 | memcpy(&addr, a->data, sizeof(addr)); |
1681 | memcpy(&pattern, p->data, sizeof(pattern)); | |
aba5acdf SH |
1682 | if (pattern == NULL) |
1683 | return 1; | |
1684 | if (addr == NULL) | |
1685 | addr = ""; | |
8f5a602f | 1686 | return !fnmatch(pattern, addr, FNM_CASEFOLD); |
aba5acdf SH |
1687 | } |
1688 | ||
055840f2 | 1689 | static int run_ssfilter(struct ssfilter *f, struct sockstat *s) |
aba5acdf SH |
1690 | { |
1691 | switch (f->type) { | |
1692 | case SSF_S_AUTO: | |
1693 | { | |
aba5acdf | 1694 | if (s->local.family == AF_UNIX) { |
99bb68ff | 1695 | char *p; |
acd1e437 | 1696 | |
99bb68ff | 1697 | memcpy(&p, s->local.data, sizeof(p)); |
aba5acdf | 1698 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && |
ae665a52 | 1699 | strspn(p+1, "0123456789abcdef") == 5); |
aba5acdf SH |
1700 | } |
1701 | if (s->local.family == AF_PACKET) | |
bbd303d1 | 1702 | return s->lport == 0 && s->local.data[0] == 0; |
aba5acdf SH |
1703 | if (s->local.family == AF_NETLINK) |
1704 | return s->lport < 0; | |
c759116a SH |
1705 | if (s->local.family == AF_VSOCK) |
1706 | return s->lport > 1023; | |
aba5acdf | 1707 | |
c29d3792 | 1708 | return is_ephemeral(s->lport); |
aba5acdf SH |
1709 | } |
1710 | case SSF_DCOND: | |
1711 | { | |
acd1e437 SH |
1712 | struct aafilter *a = (void *)f->pred; |
1713 | ||
aba5acdf SH |
1714 | if (a->addr.family == AF_UNIX) |
1715 | return unix_match(&s->remote, &a->addr); | |
1716 | if (a->port != -1 && a->port != s->rport) | |
1717 | return 0; | |
1718 | if (a->addr.bitlen) { | |
1719 | do { | |
1720 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) | |
1721 | return 1; | |
1722 | } while ((a = a->next) != NULL); | |
1723 | return 0; | |
1724 | } | |
1725 | return 1; | |
1726 | } | |
1727 | case SSF_SCOND: | |
1728 | { | |
acd1e437 SH |
1729 | struct aafilter *a = (void *)f->pred; |
1730 | ||
aba5acdf SH |
1731 | if (a->addr.family == AF_UNIX) |
1732 | return unix_match(&s->local, &a->addr); | |
1733 | if (a->port != -1 && a->port != s->lport) | |
1734 | return 0; | |
1735 | if (a->addr.bitlen) { | |
1736 | do { | |
1737 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) | |
1738 | return 1; | |
ae665a52 | 1739 | } while ((a = a->next) != NULL); |
aba5acdf SH |
1740 | return 0; |
1741 | } | |
1742 | return 1; | |
1743 | } | |
1744 | case SSF_D_GE: | |
1745 | { | |
acd1e437 SH |
1746 | struct aafilter *a = (void *)f->pred; |
1747 | ||
aba5acdf SH |
1748 | return s->rport >= a->port; |
1749 | } | |
1750 | case SSF_D_LE: | |
1751 | { | |
acd1e437 SH |
1752 | struct aafilter *a = (void *)f->pred; |
1753 | ||
aba5acdf SH |
1754 | return s->rport <= a->port; |
1755 | } | |
1756 | case SSF_S_GE: | |
1757 | { | |
acd1e437 SH |
1758 | struct aafilter *a = (void *)f->pred; |
1759 | ||
aba5acdf SH |
1760 | return s->lport >= a->port; |
1761 | } | |
1762 | case SSF_S_LE: | |
1763 | { | |
acd1e437 SH |
1764 | struct aafilter *a = (void *)f->pred; |
1765 | ||
aba5acdf SH |
1766 | return s->lport <= a->port; |
1767 | } | |
2d293212 DA |
1768 | case SSF_DEVCOND: |
1769 | { | |
1770 | struct aafilter *a = (void *)f->pred; | |
aba5acdf | 1771 | |
2d293212 | 1772 | return s->iface == a->iface; |
ec75249b LC |
1773 | } |
1774 | case SSF_MARKMASK: | |
1775 | { | |
1776 | struct aafilter *a = (void *)f->pred; | |
1777 | ||
1778 | return (s->mark & a->mask) == a->mark; | |
14f4bda5 DY |
1779 | } |
1780 | case SSF_CGROUPCOND: | |
1781 | { | |
1782 | struct aafilter *a = (void *)f->pred; | |
1783 | ||
1784 | return s->cgroup_id == a->cgroup_id; | |
2d293212 | 1785 | } |
aba5acdf SH |
1786 | /* Yup. It is recursion. Sorry. */ |
1787 | case SSF_AND: | |
1788 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); | |
1789 | case SSF_OR: | |
1790 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); | |
1791 | case SSF_NOT: | |
1792 | return !run_ssfilter(f->pred, s); | |
1793 | default: | |
1794 | abort(); | |
1795 | } | |
1796 | } | |
1797 | ||
ae665a52 | 1798 | /* Relocate external jumps by reloc. */ |
b4b0b7d5 | 1799 | static void ssfilter_patch(char *a, int len, int reloc) |
aba5acdf SH |
1800 | { |
1801 | while (len > 0) { | |
acd1e437 SH |
1802 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a; |
1803 | ||
aba5acdf SH |
1804 | if (op->no == len+4) |
1805 | op->no += reloc; | |
1806 | len -= op->yes; | |
1807 | a += op->yes; | |
1808 | } | |
1809 | if (len < 0) | |
1810 | abort(); | |
1811 | } | |
1812 | ||
b4b0b7d5 | 1813 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
aba5acdf SH |
1814 | { |
1815 | switch (f->type) { | |
1816 | case SSF_S_AUTO: | |
1817 | { | |
acd1e437 SH |
1818 | if (!(*bytecode = malloc(4))) abort(); |
1819 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; | |
df39de8d | 1820 | return 4; |
aba5acdf SH |
1821 | } |
1822 | case SSF_DCOND: | |
1823 | case SSF_SCOND: | |
1824 | { | |
acd1e437 | 1825 | struct aafilter *a = (void *)f->pred; |
aba5acdf SH |
1826 | struct aafilter *b; |
1827 | char *ptr; | |
351efcde | 1828 | int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); |
aba5acdf SH |
1829 | int len = 0; |
1830 | ||
acd1e437 | 1831 | for (b = a; b; b = b->next) { |
351efcde | 1832 | len += 4 + sizeof(struct inet_diag_hostcond); |
aba5acdf SH |
1833 | if (a->addr.family == AF_INET6) |
1834 | len += 16; | |
1835 | else | |
1836 | len += 4; | |
1837 | if (b->next) | |
1838 | len += 4; | |
1839 | } | |
1840 | if (!(ptr = malloc(len))) abort(); | |
1841 | *bytecode = ptr; | |
acd1e437 | 1842 | for (b = a; b; b = b->next) { |
351efcde | 1843 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; |
aba5acdf | 1844 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
351efcde | 1845 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
acd1e437 | 1846 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4); |
aba5acdf | 1847 | |
351efcde | 1848 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
aba5acdf SH |
1849 | cond->family = a->addr.family; |
1850 | cond->port = a->port; | |
1851 | cond->prefix_len = a->addr.bitlen; | |
1852 | memcpy(cond->addr, a->addr.data, alen); | |
1853 | ptr += oplen; | |
1854 | if (b->next) { | |
351efcde SH |
1855 | op = (struct inet_diag_bc_op *)ptr; |
1856 | *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; | |
aba5acdf SH |
1857 | ptr += 4; |
1858 | } | |
1859 | } | |
1860 | return ptr - *bytecode; | |
1861 | } | |
1862 | case SSF_D_GE: | |
1863 | { | |
acd1e437 SH |
1864 | struct aafilter *x = (void *)f->pred; |
1865 | ||
1866 | if (!(*bytecode = malloc(8))) abort(); | |
1867 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; | |
1868 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1869 | return 8; |
1870 | } | |
1871 | case SSF_D_LE: | |
1872 | { | |
acd1e437 SH |
1873 | struct aafilter *x = (void *)f->pred; |
1874 | ||
1875 | if (!(*bytecode = malloc(8))) abort(); | |
1876 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; | |
1877 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1878 | return 8; |
1879 | } | |
1880 | case SSF_S_GE: | |
1881 | { | |
acd1e437 SH |
1882 | struct aafilter *x = (void *)f->pred; |
1883 | ||
1884 | if (!(*bytecode = malloc(8))) abort(); | |
1885 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; | |
1886 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1887 | return 8; |
1888 | } | |
1889 | case SSF_S_LE: | |
1890 | { | |
acd1e437 SH |
1891 | struct aafilter *x = (void *)f->pred; |
1892 | ||
1893 | if (!(*bytecode = malloc(8))) abort(); | |
1894 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; | |
1895 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1896 | return 8; |
1897 | } | |
1898 | ||
1899 | case SSF_AND: | |
1900 | { | |
376fb868 | 1901 | char *a1 = NULL, *a2 = NULL, *a; |
2a4fa1c3 | 1902 | int l1, l2; |
acd1e437 | 1903 | |
aba5acdf SH |
1904 | l1 = ssfilter_bytecompile(f->pred, &a1); |
1905 | l2 = ssfilter_bytecompile(f->post, &a2); | |
376fb868 DA |
1906 | if (!l1 || !l2) { |
1907 | free(a1); | |
1908 | free(a2); | |
1909 | return 0; | |
1910 | } | |
aba5acdf SH |
1911 | if (!(a = malloc(l1+l2))) abort(); |
1912 | memcpy(a, a1, l1); | |
1913 | memcpy(a+l1, a2, l2); | |
1914 | free(a1); free(a2); | |
1915 | ssfilter_patch(a, l1, l2); | |
1916 | *bytecode = a; | |
1917 | return l1+l2; | |
1918 | } | |
1919 | case SSF_OR: | |
1920 | { | |
376fb868 | 1921 | char *a1 = NULL, *a2 = NULL, *a; |
2a4fa1c3 | 1922 | int l1, l2; |
acd1e437 | 1923 | |
aba5acdf SH |
1924 | l1 = ssfilter_bytecompile(f->pred, &a1); |
1925 | l2 = ssfilter_bytecompile(f->post, &a2); | |
376fb868 DA |
1926 | if (!l1 || !l2) { |
1927 | free(a1); | |
1928 | free(a2); | |
1929 | return 0; | |
1930 | } | |
aba5acdf SH |
1931 | if (!(a = malloc(l1+l2+4))) abort(); |
1932 | memcpy(a, a1, l1); | |
1933 | memcpy(a+l1+4, a2, l2); | |
1934 | free(a1); free(a2); | |
acd1e437 | 1935 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; |
aba5acdf SH |
1936 | *bytecode = a; |
1937 | return l1+l2+4; | |
1938 | } | |
1939 | case SSF_NOT: | |
1940 | { | |
376fb868 | 1941 | char *a1 = NULL, *a; |
2a4fa1c3 | 1942 | int l1; |
acd1e437 | 1943 | |
aba5acdf | 1944 | l1 = ssfilter_bytecompile(f->pred, &a1); |
376fb868 DA |
1945 | if (!l1) { |
1946 | free(a1); | |
1947 | return 0; | |
1948 | } | |
aba5acdf SH |
1949 | if (!(a = malloc(l1+4))) abort(); |
1950 | memcpy(a, a1, l1); | |
1951 | free(a1); | |
acd1e437 | 1952 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; |
aba5acdf SH |
1953 | *bytecode = a; |
1954 | return l1+4; | |
2d293212 DA |
1955 | } |
1956 | case SSF_DEVCOND: | |
1957 | { | |
1958 | /* bytecompile for SSF_DEVCOND not supported yet */ | |
1959 | return 0; | |
ec75249b LC |
1960 | } |
1961 | case SSF_MARKMASK: | |
1962 | { | |
1963 | struct aafilter *a = (void *)f->pred; | |
1964 | struct instr { | |
1965 | struct inet_diag_bc_op op; | |
1966 | struct inet_diag_markcond cond; | |
1967 | }; | |
1968 | int inslen = sizeof(struct instr); | |
1969 | ||
1970 | if (!(*bytecode = malloc(inslen))) abort(); | |
1971 | ((struct instr *)*bytecode)[0] = (struct instr) { | |
1972 | { INET_DIAG_BC_MARK_COND, inslen, inslen + 4 }, | |
1973 | { a->mark, a->mask}, | |
1974 | }; | |
1975 | ||
14f4bda5 DY |
1976 | return inslen; |
1977 | } | |
1978 | case SSF_CGROUPCOND: | |
1979 | { | |
1980 | struct aafilter *a = (void *)f->pred; | |
1981 | struct instr { | |
1982 | struct inet_diag_bc_op op; | |
1983 | __u64 cgroup_id; | |
1984 | } __attribute__((packed)); | |
1985 | int inslen = sizeof(struct instr); | |
1986 | ||
1987 | if (!(*bytecode = malloc(inslen))) abort(); | |
1988 | ((struct instr *)*bytecode)[0] = (struct instr) { | |
1989 | { INET_DIAG_BC_CGROUP_COND, inslen, inslen + 4 }, | |
1990 | a->cgroup_id, | |
1991 | }; | |
1992 | ||
ec75249b | 1993 | return inslen; |
aba5acdf SH |
1994 | } |
1995 | default: | |
1996 | abort(); | |
1997 | } | |
1998 | } | |
1999 | ||
b4b0b7d5 | 2000 | static int remember_he(struct aafilter *a, struct hostent *he) |
aba5acdf | 2001 | { |
ae665a52 | 2002 | char **ptr = he->h_addr_list; |
aba5acdf SH |
2003 | int cnt = 0; |
2004 | int len; | |
2005 | ||
2006 | if (he->h_addrtype == AF_INET) | |
2007 | len = 4; | |
2008 | else if (he->h_addrtype == AF_INET6) | |
2009 | len = 16; | |
2010 | else | |
2011 | return 0; | |
2012 | ||
2013 | while (*ptr) { | |
2014 | struct aafilter *b = a; | |
acd1e437 | 2015 | |
aba5acdf SH |
2016 | if (a->addr.bitlen) { |
2017 | if ((b = malloc(sizeof(*b))) == NULL) | |
2018 | return cnt; | |
2019 | *b = *a; | |
aba5acdf SH |
2020 | a->next = b; |
2021 | } | |
2022 | memcpy(b->addr.data, *ptr, len); | |
2023 | b->addr.bytelen = len; | |
2024 | b->addr.bitlen = len*8; | |
2025 | b->addr.family = he->h_addrtype; | |
2026 | ptr++; | |
2027 | cnt++; | |
2028 | } | |
2029 | return cnt; | |
2030 | } | |
2031 | ||
b4b0b7d5 | 2032 | static int get_dns_host(struct aafilter *a, const char *addr, int fam) |
aba5acdf SH |
2033 | { |
2034 | static int notfirst; | |
2035 | int cnt = 0; | |
2036 | struct hostent *he; | |
2037 | ||
2038 | a->addr.bitlen = 0; | |
2039 | if (!notfirst) { | |
2040 | sethostent(1); | |
2041 | notfirst = 1; | |
2042 | } | |
2043 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); | |
2044 | if (he) | |
2045 | cnt = remember_he(a, he); | |
2046 | if (fam == AF_UNSPEC) { | |
2047 | he = gethostbyname2(addr, AF_INET6); | |
2048 | if (he) | |
2049 | cnt += remember_he(a, he); | |
2050 | } | |
2051 | return !cnt; | |
2052 | } | |
2053 | ||
acd1e437 | 2054 | static int xll_initted; |
aba5acdf | 2055 | |
b4b0b7d5 | 2056 | static void xll_init(void) |
aba5acdf SH |
2057 | { |
2058 | struct rtnl_handle rth; | |
acd1e437 | 2059 | |
d2468da0 SH |
2060 | if (rtnl_open(&rth, 0) < 0) |
2061 | exit(1); | |
2062 | ||
aba5acdf SH |
2063 | ll_init_map(&rth); |
2064 | rtnl_close(&rth); | |
2065 | xll_initted = 1; | |
2066 | } | |
2067 | ||
b4b0b7d5 | 2068 | static const char *xll_index_to_name(int index) |
aba5acdf SH |
2069 | { |
2070 | if (!xll_initted) | |
2071 | xll_init(); | |
2072 | return ll_index_to_name(index); | |
2073 | } | |
2074 | ||
b4b0b7d5 | 2075 | static int xll_name_to_index(const char *dev) |
aba5acdf SH |
2076 | { |
2077 | if (!xll_initted) | |
2078 | xll_init(); | |
2079 | return ll_name_to_index(dev); | |
2080 | } | |
2081 | ||
2d293212 DA |
2082 | void *parse_devcond(char *name) |
2083 | { | |
2084 | struct aafilter a = { .iface = 0 }; | |
2085 | struct aafilter *res; | |
2086 | ||
2087 | a.iface = xll_name_to_index(name); | |
2088 | if (a.iface == 0) { | |
2089 | char *end; | |
930d3f28 | 2090 | unsigned long n; |
2d293212 | 2091 | |
930d3f28 DA |
2092 | n = strtoul(name, &end, 0); |
2093 | if (!end || end == name || *end || n > UINT_MAX) | |
2d293212 | 2094 | return NULL; |
930d3f28 DA |
2095 | |
2096 | a.iface = n; | |
2d293212 DA |
2097 | } |
2098 | ||
2099 | res = malloc(sizeof(*res)); | |
2100 | *res = a; | |
2101 | ||
2102 | return res; | |
2103 | } | |
2104 | ||
c759116a SH |
2105 | static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid) |
2106 | { | |
2107 | *a = (inet_prefix){ | |
2108 | .bytelen = sizeof(cid), | |
2109 | .family = AF_VSOCK, | |
2110 | }; | |
2111 | memcpy(a->data, &cid, sizeof(cid)); | |
2112 | } | |
2113 | ||
7871f7db | 2114 | void *parse_hostcond(char *addr, bool is_port) |
aba5acdf SH |
2115 | { |
2116 | char *port = NULL; | |
1527a17e | 2117 | struct aafilter a = { .port = -1 }; |
aba5acdf | 2118 | struct aafilter *res; |
1527a17e | 2119 | int fam = preferred_family; |
9db7bf15 | 2120 | struct filter *f = ¤t_filter; |
aba5acdf | 2121 | |
1527a17e | 2122 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { |
aba5acdf | 2123 | char *p; |
acd1e437 | 2124 | |
aba5acdf SH |
2125 | a.addr.family = AF_UNIX; |
2126 | if (strncmp(addr, "unix:", 5) == 0) | |
acd1e437 | 2127 | addr += 5; |
aba5acdf SH |
2128 | p = strdup(addr); |
2129 | a.addr.bitlen = 8*strlen(p); | |
99bb68ff | 2130 | memcpy(a.addr.data, &p, sizeof(p)); |
9db7bf15 | 2131 | fam = AF_UNIX; |
aba5acdf SH |
2132 | goto out; |
2133 | } | |
2134 | ||
1527a17e | 2135 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { |
aba5acdf SH |
2136 | a.addr.family = AF_PACKET; |
2137 | a.addr.bitlen = 0; | |
2138 | if (strncmp(addr, "link:", 5) == 0) | |
acd1e437 | 2139 | addr += 5; |
aba5acdf SH |
2140 | port = strchr(addr, ':'); |
2141 | if (port) { | |
2142 | *port = 0; | |
2143 | if (port[1] && strcmp(port+1, "*")) { | |
2144 | if (get_integer(&a.port, port+1, 0)) { | |
2145 | if ((a.port = xll_name_to_index(port+1)) <= 0) | |
2146 | return NULL; | |
2147 | } | |
2148 | } | |
2149 | } | |
2150 | if (addr[0] && strcmp(addr, "*")) { | |
2151 | unsigned short tmp; | |
acd1e437 | 2152 | |
aba5acdf SH |
2153 | a.addr.bitlen = 32; |
2154 | if (ll_proto_a2n(&tmp, addr)) | |
2155 | return NULL; | |
2156 | a.addr.data[0] = ntohs(tmp); | |
2157 | } | |
9db7bf15 | 2158 | fam = AF_PACKET; |
aba5acdf SH |
2159 | goto out; |
2160 | } | |
2161 | ||
1527a17e | 2162 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { |
aba5acdf SH |
2163 | a.addr.family = AF_NETLINK; |
2164 | a.addr.bitlen = 0; | |
2165 | if (strncmp(addr, "netlink:", 8) == 0) | |
acd1e437 | 2166 | addr += 8; |
aba5acdf SH |
2167 | port = strchr(addr, ':'); |
2168 | if (port) { | |
2169 | *port = 0; | |
2170 | if (port[1] && strcmp(port+1, "*")) { | |
2171 | if (get_integer(&a.port, port+1, 0)) { | |
2172 | if (strcmp(port+1, "kernel") == 0) | |
2173 | a.port = 0; | |
2174 | else | |
2175 | return NULL; | |
2176 | } | |
2177 | } | |
2178 | } | |
2179 | if (addr[0] && strcmp(addr, "*")) { | |
2180 | a.addr.bitlen = 32; | |
b00daf6a | 2181 | if (nl_proto_a2n(&a.addr.data[0], addr) == -1) |
2182 | return NULL; | |
aba5acdf | 2183 | } |
9db7bf15 | 2184 | fam = AF_NETLINK; |
aba5acdf SH |
2185 | goto out; |
2186 | } | |
2187 | ||
c759116a SH |
2188 | if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) { |
2189 | __u32 cid = ~(__u32)0; | |
2190 | ||
2191 | a.addr.family = AF_VSOCK; | |
2192 | if (strncmp(addr, "vsock:", 6) == 0) | |
2193 | addr += 6; | |
2194 | ||
2195 | if (is_port) | |
2196 | port = addr; | |
2197 | else { | |
2198 | port = strchr(addr, ':'); | |
2199 | if (port) { | |
2200 | *port = '\0'; | |
2201 | port++; | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | if (port && strcmp(port, "*") && | |
2206 | get_u32((__u32 *)&a.port, port, 0)) | |
2207 | return NULL; | |
2208 | ||
2209 | if (addr[0] && strcmp(addr, "*")) { | |
2210 | a.addr.bitlen = 32; | |
2211 | if (get_u32(&cid, addr, 0)) | |
2212 | return NULL; | |
2213 | } | |
2214 | vsock_set_inet_prefix(&a.addr, cid); | |
2215 | fam = AF_VSOCK; | |
2216 | goto out; | |
2217 | } | |
2218 | ||
1527a17e | 2219 | if (fam == AF_INET || !strncmp(addr, "inet:", 5)) { |
aba5acdf | 2220 | fam = AF_INET; |
1527a17e VK |
2221 | if (!strncmp(addr, "inet:", 5)) |
2222 | addr += 5; | |
2223 | } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) { | |
aba5acdf | 2224 | fam = AF_INET6; |
1527a17e VK |
2225 | if (!strncmp(addr, "inet6:", 6)) |
2226 | addr += 6; | |
aba5acdf SH |
2227 | } |
2228 | ||
2229 | /* URL-like literal [] */ | |
2230 | if (addr[0] == '[') { | |
2231 | addr++; | |
2232 | if ((port = strchr(addr, ']')) == NULL) | |
2233 | return NULL; | |
2234 | *port++ = 0; | |
2235 | } else if (addr[0] == '*') { | |
2236 | port = addr+1; | |
2237 | } else { | |
2238 | port = strrchr(strchr(addr, '/') ? : addr, ':'); | |
2239 | } | |
7871f7db VK |
2240 | |
2241 | if (is_port) | |
2242 | port = addr; | |
2243 | ||
aba5acdf | 2244 | if (port && *port) { |
7871f7db VK |
2245 | if (*port == ':') |
2246 | *port++ = 0; | |
2247 | ||
aba5acdf SH |
2248 | if (*port && *port != '*') { |
2249 | if (get_integer(&a.port, port, 0)) { | |
2250 | struct servent *se1 = NULL; | |
2251 | struct servent *se2 = NULL; | |
acd1e437 | 2252 | |
aba5acdf SH |
2253 | if (current_filter.dbs&(1<<UDP_DB)) |
2254 | se1 = getservbyname(port, UDP_PROTO); | |
2255 | if (current_filter.dbs&(1<<TCP_DB)) | |
2256 | se2 = getservbyname(port, TCP_PROTO); | |
2257 | if (se1 && se2 && se1->s_port != se2->s_port) { | |
2258 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
2259 | return NULL; | |
2260 | } | |
2261 | if (!se1) | |
2262 | se1 = se2; | |
2263 | if (se1) { | |
2264 | a.port = ntohs(se1->s_port); | |
2265 | } else { | |
2266 | struct scache *s; | |
acd1e437 | 2267 | |
aba5acdf SH |
2268 | for (s = rlist; s; s = s->next) { |
2269 | if ((s->proto == UDP_PROTO && | |
2270 | (current_filter.dbs&(1<<UDP_DB))) || | |
2271 | (s->proto == TCP_PROTO && | |
2272 | (current_filter.dbs&(1<<TCP_DB)))) { | |
2273 | if (s->name && strcmp(s->name, port) == 0) { | |
2274 | if (a.port > 0 && a.port != s->port) { | |
2275 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
2276 | return NULL; | |
2277 | } | |
2278 | a.port = s->port; | |
2279 | } | |
2280 | } | |
2281 | } | |
2282 | if (a.port <= 0) { | |
2283 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); | |
2284 | return NULL; | |
2285 | } | |
2286 | } | |
2287 | } | |
2288 | } | |
2289 | } | |
44448a90 | 2290 | if (!is_port && *addr && *addr != '*') { |
aba5acdf SH |
2291 | if (get_prefix_1(&a.addr, addr, fam)) { |
2292 | if (get_dns_host(&a, addr, fam)) { | |
2293 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); | |
2294 | return NULL; | |
2295 | } | |
2296 | } | |
2297 | } | |
2298 | ||
9db7bf15 | 2299 | out: |
1527a17e | 2300 | if (fam != AF_UNSPEC) { |
e56a959e | 2301 | int states = f->states; |
1527a17e | 2302 | f->families = 0; |
9db7bf15 | 2303 | filter_af_set(f, fam); |
e56a959e | 2304 | filter_states_set(f, states); |
1527a17e | 2305 | } |
9db7bf15 | 2306 | |
aba5acdf SH |
2307 | res = malloc(sizeof(*res)); |
2308 | if (res) | |
2309 | memcpy(res, &a, sizeof(a)); | |
2310 | return res; | |
2311 | } | |
2312 | ||
ec75249b LC |
2313 | void *parse_markmask(const char *markmask) |
2314 | { | |
2315 | struct aafilter a, *res; | |
2316 | ||
2317 | if (strchr(markmask, '/')) { | |
2318 | if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2) | |
2319 | return NULL; | |
2320 | } else { | |
2321 | a.mask = 0xffffffff; | |
2322 | if (sscanf(markmask, "%i", &a.mark) != 1) | |
2323 | return NULL; | |
2324 | } | |
2325 | ||
2326 | res = malloc(sizeof(*res)); | |
2327 | if (res) | |
2328 | memcpy(res, &a, sizeof(a)); | |
2329 | return res; | |
2330 | } | |
2331 | ||
14f4bda5 DY |
2332 | void *parse_cgroupcond(const char *path) |
2333 | { | |
2334 | struct aafilter *res; | |
2335 | __u64 id; | |
2336 | ||
2337 | id = get_cgroup2_id(path); | |
2338 | if (!id) | |
2339 | return NULL; | |
2340 | ||
2341 | res = malloc(sizeof(*res)); | |
2342 | if (res) | |
2343 | res->cgroup_id = id; | |
2344 | ||
2345 | return res; | |
2346 | } | |
2347 | ||
5f27ac1d | 2348 | static void proc_ctx_print(struct sockstat *s) |
8250bc9f | 2349 | { |
5f27ac1d | 2350 | char *buf; |
8250bc9f | 2351 | |
8250bc9f VK |
2352 | if (show_proc_ctx || show_sock_ctx) { |
2353 | if (find_entry(s->ino, &buf, | |
2354 | (show_proc_ctx & show_sock_ctx) ? | |
2355 | PROC_SOCK_CTX : PROC_CTX) > 0) { | |
90351722 | 2356 | out(" users:(%s)", buf); |
8250bc9f VK |
2357 | free(buf); |
2358 | } | |
2359 | } else if (show_users) { | |
2360 | if (find_entry(s->ino, &buf, USERS) > 0) { | |
90351722 | 2361 | out(" users:(%s)", buf); |
8250bc9f VK |
2362 | free(buf); |
2363 | } | |
2364 | } | |
2365 | } | |
2366 | ||
1267c0b9 | 2367 | static void inet_stats_print(struct sockstat *s, bool v6only) |
5f27ac1d PS |
2368 | { |
2369 | sock_state_print(s); | |
2370 | ||
1267c0b9 PS |
2371 | inet_addr_print(&s->local, s->lport, s->iface, v6only); |
2372 | inet_addr_print(&s->remote, s->rport, 0, v6only); | |
5f27ac1d PS |
2373 | |
2374 | proc_ctx_print(s); | |
2375 | } | |
2376 | ||
055840f2 | 2377 | static int proc_parse_inet_addr(char *loc, char *rem, int family, struct |
acd1e437 | 2378 | sockstat * s) |
8250bc9f VK |
2379 | { |
2380 | s->local.family = s->remote.family = family; | |
2381 | if (family == AF_INET) { | |
acd1e437 SH |
2382 | sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport); |
2383 | sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport); | |
8250bc9f VK |
2384 | s->local.bytelen = s->remote.bytelen = 4; |
2385 | return 0; | |
2386 | } else { | |
2387 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
2388 | s->local.data, | |
2389 | s->local.data + 1, | |
2390 | s->local.data + 2, | |
2391 | s->local.data + 3, | |
2392 | &s->lport); | |
2393 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
2394 | s->remote.data, | |
2395 | s->remote.data + 1, | |
2396 | s->remote.data + 2, | |
2397 | s->remote.data + 3, | |
2398 | &s->rport); | |
2399 | s->local.bytelen = s->remote.bytelen = 16; | |
2400 | return 0; | |
2401 | } | |
2402 | return -1; | |
2403 | } | |
2404 | ||
2405 | static int proc_inet_split_line(char *line, char **loc, char **rem, char **data) | |
aba5acdf | 2406 | { |
aba5acdf | 2407 | char *p; |
ae665a52 | 2408 | |
aba5acdf SH |
2409 | if ((p = strchr(line, ':')) == NULL) |
2410 | return -1; | |
ae665a52 | 2411 | |
8250bc9f VK |
2412 | *loc = p+2; |
2413 | if ((p = strchr(*loc, ':')) == NULL) | |
aba5acdf | 2414 | return -1; |
ae665a52 | 2415 | |
8250bc9f VK |
2416 | p[5] = 0; |
2417 | *rem = p+6; | |
2418 | if ((p = strchr(*rem, ':')) == NULL) | |
aba5acdf | 2419 | return -1; |
8250bc9f | 2420 | |
aba5acdf | 2421 | p[5] = 0; |
8250bc9f VK |
2422 | *data = p+6; |
2423 | return 0; | |
2424 | } | |
ae665a52 | 2425 | |
8142c762 SH |
2426 | /* |
2427 | * Display bandwidth in standard units | |
2428 | * See: https://en.wikipedia.org/wiki/Data-rate_units | |
2429 | * bw is in bits per second | |
2430 | */ | |
8250bc9f VK |
2431 | static char *sprint_bw(char *buf, double bw) |
2432 | { | |
a257456f | 2433 | if (numeric) |
d791e75d | 2434 | sprintf(buf, "%.0f", bw); |
8142c762 SH |
2435 | else if (bw >= 1e12) |
2436 | sprintf(buf, "%.3gT", bw / 1e12); | |
e133fa9c ED |
2437 | else if (bw >= 1e9) |
2438 | sprintf(buf, "%.3gG", bw / 1e9); | |
2439 | else if (bw >= 1e6) | |
2440 | sprintf(buf, "%.3gM", bw / 1e6); | |
2441 | else if (bw >= 1e3) | |
8142c762 | 2442 | sprintf(buf, "%.3gk", bw / 1e3); |
8250bc9f VK |
2443 | else |
2444 | sprintf(buf, "%g", bw); | |
aba5acdf | 2445 | |
8250bc9f VK |
2446 | return buf; |
2447 | } | |
ae665a52 | 2448 | |
f89d46ad PS |
2449 | static void sctp_stats_print(struct sctp_info *s) |
2450 | { | |
2451 | if (s->sctpi_tag) | |
90351722 | 2452 | out(" tag:%x", s->sctpi_tag); |
f89d46ad | 2453 | if (s->sctpi_state) |
90351722 | 2454 | out(" state:%s", sctp_sstate_name[s->sctpi_state]); |
f89d46ad | 2455 | if (s->sctpi_rwnd) |
90351722 | 2456 | out(" rwnd:%d", s->sctpi_rwnd); |
f89d46ad | 2457 | if (s->sctpi_unackdata) |
90351722 | 2458 | out(" unackdata:%d", s->sctpi_unackdata); |
f89d46ad | 2459 | if (s->sctpi_penddata) |
90351722 | 2460 | out(" penddata:%d", s->sctpi_penddata); |
f89d46ad | 2461 | if (s->sctpi_instrms) |
90351722 | 2462 | out(" instrms:%d", s->sctpi_instrms); |
f89d46ad | 2463 | if (s->sctpi_outstrms) |
90351722 | 2464 | out(" outstrms:%d", s->sctpi_outstrms); |
f89d46ad | 2465 | if (s->sctpi_inqueue) |
90351722 | 2466 | out(" inqueue:%d", s->sctpi_inqueue); |
f89d46ad | 2467 | if (s->sctpi_outqueue) |
90351722 | 2468 | out(" outqueue:%d", s->sctpi_outqueue); |
f89d46ad | 2469 | if (s->sctpi_overall_error) |
90351722 | 2470 | out(" overerr:%d", s->sctpi_overall_error); |
f89d46ad | 2471 | if (s->sctpi_max_burst) |
90351722 | 2472 | out(" maxburst:%d", s->sctpi_max_burst); |
f89d46ad | 2473 | if (s->sctpi_maxseg) |
90351722 | 2474 | out(" maxseg:%d", s->sctpi_maxseg); |
f89d46ad | 2475 | if (s->sctpi_peer_rwnd) |
90351722 | 2476 | out(" prwnd:%d", s->sctpi_peer_rwnd); |
f89d46ad | 2477 | if (s->sctpi_peer_tag) |
90351722 | 2478 | out(" ptag:%x", s->sctpi_peer_tag); |
f89d46ad | 2479 | if (s->sctpi_peer_capable) |
90351722 | 2480 | out(" pcapable:%d", s->sctpi_peer_capable); |
f89d46ad | 2481 | if (s->sctpi_peer_sack) |
90351722 | 2482 | out(" psack:%d", s->sctpi_peer_sack); |
f89d46ad | 2483 | if (s->sctpi_s_autoclose) |
90351722 | 2484 | out(" autoclose:%d", s->sctpi_s_autoclose); |
f89d46ad | 2485 | if (s->sctpi_s_adaptation_ind) |
90351722 | 2486 | out(" adapind:%d", s->sctpi_s_adaptation_ind); |
f89d46ad | 2487 | if (s->sctpi_s_pd_point) |
90351722 | 2488 | out(" pdpoint:%d", s->sctpi_s_pd_point); |
f89d46ad | 2489 | if (s->sctpi_s_nodelay) |
18db049f | 2490 | out(" nodelay:%d", s->sctpi_s_nodelay); |
f89d46ad | 2491 | if (s->sctpi_s_disable_fragments) |
90351722 | 2492 | out(" nofrag:%d", s->sctpi_s_disable_fragments); |
f89d46ad | 2493 | if (s->sctpi_s_v4mapped) |
90351722 | 2494 | out(" v4mapped:%d", s->sctpi_s_v4mapped); |
f89d46ad | 2495 | if (s->sctpi_s_frag_interleave) |
90351722 | 2496 | out(" fraginl:%d", s->sctpi_s_frag_interleave); |
f89d46ad PS |
2497 | } |
2498 | ||
8250bc9f VK |
2499 | static void tcp_stats_print(struct tcpstat *s) |
2500 | { | |
2501 | char b1[64]; | |
2502 | ||
2503 | if (s->has_ts_opt) | |
90351722 | 2504 | out(" ts"); |
8250bc9f | 2505 | if (s->has_sack_opt) |
90351722 | 2506 | out(" sack"); |
8250bc9f | 2507 | if (s->has_ecn_opt) |
90351722 | 2508 | out(" ecn"); |
8250bc9f | 2509 | if (s->has_ecnseen_opt) |
90351722 | 2510 | out(" ecnseen"); |
8250bc9f | 2511 | if (s->has_fastopen_opt) |
90351722 | 2512 | out(" fastopen"); |
d2055ea5 | 2513 | if (s->cong_alg[0]) |
90351722 | 2514 | out(" %s", s->cong_alg); |
8250bc9f | 2515 | if (s->has_wscale_opt) |
90351722 | 2516 | out(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale); |
8250bc9f | 2517 | if (s->rto) |
90351722 | 2518 | out(" rto:%g", s->rto); |
8250bc9f | 2519 | if (s->backoff) |
90351722 | 2520 | out(" backoff:%u", s->backoff); |
8250bc9f | 2521 | if (s->rtt) |
90351722 | 2522 | out(" rtt:%g/%g", s->rtt, s->rttvar); |
8250bc9f | 2523 | if (s->ato) |
90351722 | 2524 | out(" ato:%g", s->ato); |
8250bc9f VK |
2525 | |
2526 | if (s->qack) | |
90351722 | 2527 | out(" qack:%d", s->qack); |
8250bc9f | 2528 | if (s->qack & 1) |
90351722 | 2529 | out(" bidir"); |
8250bc9f VK |
2530 | |
2531 | if (s->mss) | |
90351722 | 2532 | out(" mss:%d", s->mss); |
3d791a32 RM |
2533 | if (s->pmtu) |
2534 | out(" pmtu:%u", s->pmtu); | |
38e6dbc4 | 2535 | if (s->rcv_mss) |
90351722 | 2536 | out(" rcvmss:%d", s->rcv_mss); |
38e6dbc4 | 2537 | if (s->advmss) |
90351722 | 2538 | out(" advmss:%d", s->advmss); |
3bf5445c | 2539 | if (s->cwnd) |
90351722 | 2540 | out(" cwnd:%u", s->cwnd); |
8250bc9f | 2541 | if (s->ssthresh) |
90351722 | 2542 | out(" ssthresh:%d", s->ssthresh); |
8250bc9f | 2543 | |
5eead627 ED |
2544 | if (s->bytes_sent) |
2545 | out(" bytes_sent:%llu", s->bytes_sent); | |
2546 | if (s->bytes_retrans) | |
2547 | out(" bytes_retrans:%llu", s->bytes_retrans); | |
1a4dda71 | 2548 | if (s->bytes_acked) |
90351722 | 2549 | out(" bytes_acked:%llu", s->bytes_acked); |
1a4dda71 | 2550 | if (s->bytes_received) |
90351722 | 2551 | out(" bytes_received:%llu", s->bytes_received); |
ecb435ea | 2552 | if (s->segs_out) |
90351722 | 2553 | out(" segs_out:%u", s->segs_out); |
ecb435ea | 2554 | if (s->segs_in) |
90351722 | 2555 | out(" segs_in:%u", s->segs_in); |
414aeec9 | 2556 | if (s->data_segs_out) |
90351722 | 2557 | out(" data_segs_out:%u", s->data_segs_out); |
414aeec9 | 2558 | if (s->data_segs_in) |
90351722 | 2559 | out(" data_segs_in:%u", s->data_segs_in); |
1a4dda71 | 2560 | |
8250bc9f VK |
2561 | if (s->dctcp && s->dctcp->enabled) { |
2562 | struct dctcpstat *dctcp = s->dctcp; | |
2563 | ||
90351722 SB |
2564 | out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)", |
2565 | dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn, | |
2566 | dctcp->ab_tot); | |
8250bc9f | 2567 | } else if (s->dctcp) { |
90351722 | 2568 | out(" dctcp:fallback_mode"); |
8250bc9f VK |
2569 | } |
2570 | ||
2f0f9aef NC |
2571 | if (s->bbr_info) { |
2572 | __u64 bw; | |
2573 | ||
2574 | bw = s->bbr_info->bbr_bw_hi; | |
2575 | bw <<= 32; | |
2576 | bw |= s->bbr_info->bbr_bw_lo; | |
2577 | ||
90351722 SB |
2578 | out(" bbr:(bw:%sbps,mrtt:%g", |
2579 | sprint_bw(b1, bw * 8.0), | |
2580 | (double)s->bbr_info->bbr_min_rtt / 1000.0); | |
2f0f9aef | 2581 | if (s->bbr_info->bbr_pacing_gain) |
90351722 SB |
2582 | out(",pacing_gain:%g", |
2583 | (double)s->bbr_info->bbr_pacing_gain / 256.0); | |
2f0f9aef | 2584 | if (s->bbr_info->bbr_cwnd_gain) |
90351722 SB |
2585 | out(",cwnd_gain:%g", |
2586 | (double)s->bbr_info->bbr_cwnd_gain / 256.0); | |
2587 | out(")"); | |
2f0f9aef NC |
2588 | } |
2589 | ||
8250bc9f | 2590 | if (s->send_bps) |
90351722 | 2591 | out(" send %sbps", sprint_bw(b1, s->send_bps)); |
8250bc9f | 2592 | if (s->lastsnd) |
90351722 | 2593 | out(" lastsnd:%u", s->lastsnd); |
8250bc9f | 2594 | if (s->lastrcv) |
90351722 | 2595 | out(" lastrcv:%u", s->lastrcv); |
8250bc9f | 2596 | if (s->lastack) |
90351722 | 2597 | out(" lastack:%u", s->lastack); |
8250bc9f VK |
2598 | |
2599 | if (s->pacing_rate) { | |
90351722 | 2600 | out(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate)); |
8250bc9f | 2601 | if (s->pacing_rate_max) |
90351722 | 2602 | out("/%sbps", sprint_bw(b1, s->pacing_rate_max)); |
8250bc9f VK |
2603 | } |
2604 | ||
2f579872 | 2605 | if (s->delivery_rate) |
90351722 | 2606 | out(" delivery_rate %sbps", sprint_bw(b1, s->delivery_rate)); |
2f4d834b ED |
2607 | if (s->delivered) |
2608 | out(" delivered:%u", s->delivered); | |
2609 | if (s->delivered_ce) | |
2610 | out(" delivered_ce:%u", s->delivered_ce); | |
2f579872 | 2611 | if (s->app_limited) |
90351722 | 2612 | out(" app_limited"); |
2f579872 | 2613 | |
b6c7fc61 | 2614 | if (s->busy_time) { |
90351722 | 2615 | out(" busy:%llums", s->busy_time / 1000); |
b6c7fc61 | 2616 | if (s->rwnd_limited) |
90351722 SB |
2617 | out(" rwnd_limited:%llums(%.1f%%)", |
2618 | s->rwnd_limited / 1000, | |
2619 | 100.0 * s->rwnd_limited / s->busy_time); | |
b6c7fc61 | 2620 | if (s->sndbuf_limited) |
90351722 SB |
2621 | out(" sndbuf_limited:%llums(%.1f%%)", |
2622 | s->sndbuf_limited / 1000, | |
2623 | 100.0 * s->sndbuf_limited / s->busy_time); | |
b6c7fc61 YC |
2624 | } |
2625 | ||
8250bc9f | 2626 | if (s->unacked) |
90351722 | 2627 | out(" unacked:%u", s->unacked); |
8250bc9f | 2628 | if (s->retrans || s->retrans_total) |
90351722 | 2629 | out(" retrans:%u/%u", s->retrans, s->retrans_total); |
8250bc9f | 2630 | if (s->lost) |
90351722 | 2631 | out(" lost:%u", s->lost); |
055840f2 | 2632 | if (s->sacked && s->ss.state != SS_LISTEN) |
90351722 | 2633 | out(" sacked:%u", s->sacked); |
5eead627 ED |
2634 | if (s->dsack_dups) |
2635 | out(" dsack_dups:%u", s->dsack_dups); | |
8250bc9f | 2636 | if (s->fackets) |
90351722 | 2637 | out(" fackets:%u", s->fackets); |
8250bc9f | 2638 | if (s->reordering != 3) |
90351722 | 2639 | out(" reordering:%d", s->reordering); |
5eead627 ED |
2640 | if (s->reord_seen) |
2641 | out(" reord_seen:%d", s->reord_seen); | |
8250bc9f | 2642 | if (s->rcv_rtt) |
90351722 | 2643 | out(" rcv_rtt:%g", s->rcv_rtt); |
8250bc9f | 2644 | if (s->rcv_space) |
90351722 | 2645 | out(" rcv_space:%d", s->rcv_space); |
00ac78d3 | 2646 | if (s->rcv_ssthresh) |
90351722 | 2647 | out(" rcv_ssthresh:%u", s->rcv_ssthresh); |
9e99e495 | 2648 | if (s->not_sent) |
90351722 | 2649 | out(" notsent:%u", s->not_sent); |
9e99e495 | 2650 | if (s->min_rtt) |
90351722 | 2651 | out(" minrtt:%g", s->min_rtt); |
8250bc9f VK |
2652 | } |
2653 | ||
055840f2 VK |
2654 | static void tcp_timer_print(struct tcpstat *s) |
2655 | { | |
86dfa1be PS |
2656 | static const char * const tmr_name[] = { |
2657 | "off", | |
2658 | "on", | |
2659 | "keepalive", | |
2660 | "timewait", | |
2661 | "persist", | |
2662 | "unknown" | |
2663 | }; | |
2664 | ||
055840f2 VK |
2665 | if (s->timer) { |
2666 | if (s->timer > 4) | |
2667 | s->timer = 5; | |
90351722 SB |
2668 | out(" timer:(%s,%s,%d)", |
2669 | tmr_name[s->timer], | |
2670 | print_ms_timer(s->timeout), | |
2671 | s->retrans); | |
055840f2 VK |
2672 | } |
2673 | } | |
2674 | ||
f89d46ad PS |
2675 | static void sctp_timer_print(struct tcpstat *s) |
2676 | { | |
2677 | if (s->timer) | |
90351722 SB |
2678 | out(" timer:(T3_RTX,%s,%d)", |
2679 | print_ms_timer(s->timeout), s->retrans); | |
f89d46ad PS |
2680 | } |
2681 | ||
8250bc9f VK |
2682 | static int tcp_show_line(char *line, const struct filter *f, int family) |
2683 | { | |
2684 | int rto = 0, ato = 0; | |
2685 | struct tcpstat s = {}; | |
2686 | char *loc, *rem, *data; | |
2687 | char opt[256]; | |
2688 | int n; | |
2689 | int hz = get_user_hz(); | |
2690 | ||
2691 | if (proc_inet_split_line(line, &loc, &rem, &data)) | |
2692 | return -1; | |
2693 | ||
2694 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
acd1e437 | 2695 | |
8250bc9f VK |
2696 | if (!(f->states & (1 << state))) |
2697 | return 0; | |
2698 | ||
055840f2 | 2699 | proc_parse_inet_addr(loc, rem, family, &s.ss); |
ae665a52 | 2700 | |
055840f2 | 2701 | if (f->f && run_ssfilter(f->f, &s.ss) == 0) |
aba5acdf | 2702 | return 0; |
ae665a52 | 2703 | |
aba5acdf | 2704 | opt[0] = 0; |
d1f338b3 | 2705 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n", |
055840f2 VK |
2706 | &s.ss.state, &s.ss.wq, &s.ss.rq, |
2707 | &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, | |
2708 | &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, | |
2709 | &s.ssthresh, opt); | |
ae665a52 | 2710 | |
aba5acdf SH |
2711 | if (n < 17) |
2712 | opt[0] = 0; | |
ae665a52 | 2713 | |
aba5acdf | 2714 | if (n < 12) { |
8250bc9f | 2715 | rto = 0; |
aba5acdf SH |
2716 | s.cwnd = 2; |
2717 | s.ssthresh = -1; | |
8250bc9f | 2718 | ato = s.qack = 0; |
aba5acdf | 2719 | } |
ae665a52 | 2720 | |
8250bc9f VK |
2721 | s.retrans = s.timer != 1 ? s.probes : s.retrans; |
2722 | s.timeout = (s.timeout * 1000 + hz - 1) / hz; | |
2723 | s.ato = (double)ato / hz; | |
2724 | s.qack /= 2; | |
2725 | s.rto = (double)rto; | |
2726 | s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; | |
2727 | s.rto = s.rto != 3 * hz ? s.rto / hz : 0; | |
be7e4d20 | 2728 | s.ss.type = IPPROTO_TCP; |
ae665a52 | 2729 | |
1267c0b9 | 2730 | inet_stats_print(&s.ss, false); |
055840f2 VK |
2731 | |
2732 | if (show_options) | |
2733 | tcp_timer_print(&s); | |
116ac927 | 2734 | |
aba5acdf | 2735 | if (show_details) { |
f1b39e1b | 2736 | sock_details_print(&s.ss); |
aba5acdf | 2737 | if (opt[0]) |
90351722 | 2738 | out(" opt:\"%s\"", opt); |
aba5acdf | 2739 | } |
aba5acdf | 2740 | |
8250bc9f VK |
2741 | if (show_tcpinfo) |
2742 | tcp_stats_print(&s); | |
2743 | ||
aba5acdf SH |
2744 | return 0; |
2745 | } | |
2746 | ||
ab01dbbb SH |
2747 | static int generic_record_read(FILE *fp, |
2748 | int (*worker)(char*, const struct filter *, int), | |
2749 | const struct filter *f, int fam) | |
aba5acdf | 2750 | { |
ab01dbbb | 2751 | char line[256]; |
aba5acdf | 2752 | |
ab01dbbb SH |
2753 | /* skip header */ |
2754 | if (fgets(line, sizeof(line), fp) == NULL) | |
aba5acdf | 2755 | goto outerr; |
ab01dbbb SH |
2756 | |
2757 | while (fgets(line, sizeof(line), fp) != NULL) { | |
2758 | int n = strlen(line); | |
acd1e437 | 2759 | |
ab01dbbb SH |
2760 | if (n == 0 || line[n-1] != '\n') { |
2761 | errno = -EINVAL; | |
2762 | return -1; | |
aba5acdf | 2763 | } |
ab01dbbb | 2764 | line[n-1] = 0; |
aba5acdf | 2765 | |
ab01dbbb SH |
2766 | if (worker(line, f, fam) < 0) |
2767 | return 0; | |
2768 | } | |
aba5acdf | 2769 | outerr: |
ab01dbbb SH |
2770 | |
2771 | return ferror(fp) ? -1 : 0; | |
aba5acdf | 2772 | } |
ae665a52 | 2773 | |
51ff9f24 HFS |
2774 | static void print_skmeminfo(struct rtattr *tb[], int attrtype) |
2775 | { | |
2776 | const __u32 *skmeminfo; | |
db08bdb8 VK |
2777 | |
2778 | if (!tb[attrtype]) { | |
2779 | if (attrtype == INET_DIAG_SKMEMINFO) { | |
2780 | if (!tb[INET_DIAG_MEMINFO]) | |
2781 | return; | |
2782 | ||
2783 | const struct inet_diag_meminfo *minfo = | |
2784 | RTA_DATA(tb[INET_DIAG_MEMINFO]); | |
2785 | ||
90351722 SB |
2786 | out(" mem:(r%u,w%u,f%u,t%u)", |
2787 | minfo->idiag_rmem, | |
2788 | minfo->idiag_wmem, | |
2789 | minfo->idiag_fmem, | |
2790 | minfo->idiag_tmem); | |
db08bdb8 | 2791 | } |
51ff9f24 | 2792 | return; |
db08bdb8 VK |
2793 | } |
2794 | ||
51ff9f24 HFS |
2795 | skmeminfo = RTA_DATA(tb[attrtype]); |
2796 | ||
90351722 SB |
2797 | out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u", |
2798 | skmeminfo[SK_MEMINFO_RMEM_ALLOC], | |
2799 | skmeminfo[SK_MEMINFO_RCVBUF], | |
2800 | skmeminfo[SK_MEMINFO_WMEM_ALLOC], | |
2801 | skmeminfo[SK_MEMINFO_SNDBUF], | |
2802 | skmeminfo[SK_MEMINFO_FWD_ALLOC], | |
2803 | skmeminfo[SK_MEMINFO_WMEM_QUEUED], | |
2804 | skmeminfo[SK_MEMINFO_OPTMEM]); | |
51ff9f24 HFS |
2805 | |
2806 | if (RTA_PAYLOAD(tb[attrtype]) >= | |
2807 | (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32)) | |
90351722 | 2808 | out(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]); |
51ff9f24 | 2809 | |
6df9c7a0 ED |
2810 | if (RTA_PAYLOAD(tb[attrtype]) >= |
2811 | (SK_MEMINFO_DROPS + 1) * sizeof(__u32)) | |
90351722 | 2812 | out(",d%u", skmeminfo[SK_MEMINFO_DROPS]); |
6df9c7a0 | 2813 | |
90351722 | 2814 | out(")"); |
51ff9f24 HFS |
2815 | } |
2816 | ||
da9cc6ab ID |
2817 | static void print_md5sig(struct tcp_diag_md5sig *sig) |
2818 | { | |
90351722 SB |
2819 | out("%s/%d=", |
2820 | format_host(sig->tcpm_family, | |
2821 | sig->tcpm_family == AF_INET6 ? 16 : 4, | |
2822 | &sig->tcpm_addr), | |
2823 | sig->tcpm_prefixlen); | |
da9cc6ab ID |
2824 | print_escape_buf(sig->tcpm_key, sig->tcpm_keylen, " ,"); |
2825 | } | |
2826 | ||
14cadc70 DC |
2827 | static void tcp_tls_version(struct rtattr *attr) |
2828 | { | |
2829 | u_int16_t val; | |
2830 | ||
2831 | if (!attr) | |
2832 | return; | |
2833 | val = rta_getattr_u16(attr); | |
2834 | ||
2835 | switch (val) { | |
2836 | case TLS_1_2_VERSION: | |
2837 | out(" version: 1.2"); | |
2838 | break; | |
2839 | case TLS_1_3_VERSION: | |
2840 | out(" version: 1.3"); | |
2841 | break; | |
2842 | default: | |
2843 | out(" version: unknown(%hu)", val); | |
2844 | break; | |
2845 | } | |
2846 | } | |
2847 | ||
2848 | static void tcp_tls_cipher(struct rtattr *attr) | |
2849 | { | |
2850 | u_int16_t val; | |
2851 | ||
2852 | if (!attr) | |
2853 | return; | |
2854 | val = rta_getattr_u16(attr); | |
2855 | ||
2856 | switch (val) { | |
2857 | case TLS_CIPHER_AES_GCM_128: | |
2858 | out(" cipher: aes-gcm-128"); | |
2859 | break; | |
2860 | case TLS_CIPHER_AES_GCM_256: | |
2861 | out(" cipher: aes-gcm-256"); | |
2862 | break; | |
2863 | } | |
2864 | } | |
2865 | ||
2866 | static void tcp_tls_conf(const char *name, struct rtattr *attr) | |
2867 | { | |
2868 | u_int16_t val; | |
2869 | ||
2870 | if (!attr) | |
2871 | return; | |
2872 | val = rta_getattr_u16(attr); | |
2873 | ||
2874 | switch (val) { | |
2875 | case TLS_CONF_BASE: | |
2876 | out(" %s: none", name); | |
2877 | break; | |
2878 | case TLS_CONF_SW: | |
2879 | out(" %s: sw", name); | |
2880 | break; | |
2881 | case TLS_CONF_HW: | |
2882 | out(" %s: hw", name); | |
2883 | break; | |
2884 | case TLS_CONF_HW_RECORD: | |
2885 | out(" %s: hw-record", name); | |
2886 | break; | |
2887 | default: | |
2888 | out(" %s: unknown(%hu)", name, val); | |
2889 | break; | |
2890 | } | |
2891 | } | |
2892 | ||
712fdd98 DC |
2893 | static void mptcp_subflow_info(struct rtattr *tb[]) |
2894 | { | |
2895 | u_int32_t flags = 0; | |
2896 | ||
2897 | if (tb[MPTCP_SUBFLOW_ATTR_FLAGS]) { | |
2898 | char caps[32 + 1] = { 0 }, *cap = &caps[0]; | |
2899 | ||
2900 | flags = rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_FLAGS]); | |
2901 | ||
2902 | if (flags & MPTCP_SUBFLOW_FLAG_MCAP_REM) | |
2903 | *cap++ = 'M'; | |
2904 | if (flags & MPTCP_SUBFLOW_FLAG_MCAP_LOC) | |
2905 | *cap++ = 'm'; | |
2906 | if (flags & MPTCP_SUBFLOW_FLAG_JOIN_REM) | |
2907 | *cap++ = 'J'; | |
2908 | if (flags & MPTCP_SUBFLOW_FLAG_JOIN_LOC) | |
2909 | *cap++ = 'j'; | |
2910 | if (flags & MPTCP_SUBFLOW_FLAG_BKUP_REM) | |
2911 | *cap++ = 'B'; | |
2912 | if (flags & MPTCP_SUBFLOW_FLAG_BKUP_LOC) | |
2913 | *cap++ = 'b'; | |
2914 | if (flags & MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED) | |
2915 | *cap++ = 'e'; | |
2916 | if (flags & MPTCP_SUBFLOW_FLAG_CONNECTED) | |
2917 | *cap++ = 'c'; | |
2918 | if (flags & MPTCP_SUBFLOW_FLAG_MAPVALID) | |
2919 | *cap++ = 'v'; | |
2920 | if (flags) | |
2921 | out(" flags:%s", caps); | |
2922 | } | |
2923 | if (tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM] && | |
2924 | tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC] && | |
2925 | tb[MPTCP_SUBFLOW_ATTR_ID_REM] && | |
2926 | tb[MPTCP_SUBFLOW_ATTR_ID_LOC]) | |
2927 | out(" token:%04x(id:%hhu)/%04x(id:%hhu)", | |
2928 | rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM]), | |
2929 | rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_REM]), | |
2930 | rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC]), | |
2931 | rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_LOC])); | |
2932 | if (tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ]) | |
2933 | out(" seq:%llx", | |
2934 | rta_getattr_u64(tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ])); | |
2935 | if (tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ]) | |
2936 | out(" sfseq:%x", | |
2937 | rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ])); | |
2938 | if (tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET]) | |
2939 | out(" ssnoff:%x", | |
2940 | rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET])); | |
2941 | if (tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN]) | |
2942 | out(" maplen:%x", | |
2943 | rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN])); | |
2944 | } | |
2945 | ||
8250bc9f VK |
2946 | #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt)) |
2947 | ||
5b816047 PE |
2948 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
2949 | struct rtattr *tb[]) | |
7d105b56 | 2950 | { |
b4b0b7d5 | 2951 | double rtt = 0; |
8250bc9f | 2952 | struct tcpstat s = {}; |
7d105b56 | 2953 | |
055840f2 VK |
2954 | s.ss.state = r->idiag_state; |
2955 | ||
db08bdb8 | 2956 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
7d105b56 | 2957 | |
351efcde | 2958 | if (tb[INET_DIAG_INFO]) { |
05e18118 | 2959 | struct tcp_info *info; |
351efcde | 2960 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
05e18118 SH |
2961 | |
2962 | /* workaround for older kernels with less fields */ | |
2963 | if (len < sizeof(*info)) { | |
2964 | info = alloca(sizeof(*info)); | |
351efcde | 2965 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
656e8fdd | 2966 | memset((char *)info + len, 0, sizeof(*info) - len); |
05e18118 | 2967 | } else |
351efcde | 2968 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
05e18118 | 2969 | |
b4b0b7d5 | 2970 | if (show_options) { |
8250bc9f VK |
2971 | s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS); |
2972 | s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK); | |
2973 | s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN); | |
2974 | s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN); | |
2975 | s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA); | |
b4b0b7d5 | 2976 | } |
52d5ac3f | 2977 | |
d2055ea5 ED |
2978 | if (tb[INET_DIAG_CONG]) |
2979 | strncpy(s.cong_alg, | |
2980 | rta_getattr_str(tb[INET_DIAG_CONG]), | |
2981 | sizeof(s.cong_alg) - 1); | |
8250bc9f VK |
2982 | |
2983 | if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) { | |
2984 | s.has_wscale_opt = true; | |
2985 | s.snd_wscale = info->tcpi_snd_wscale; | |
2986 | s.rcv_wscale = info->tcpi_rcv_wscale; | |
2987 | } | |
ea8fc104 | 2988 | |
7d105b56 | 2989 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
8250bc9f VK |
2990 | s.rto = (double)info->tcpi_rto / 1000; |
2991 | ||
2992 | s.backoff = info->tcpi_backoff; | |
2993 | s.rtt = (double)info->tcpi_rtt / 1000; | |
2994 | s.rttvar = (double)info->tcpi_rttvar / 1000; | |
11ba90fc | 2995 | s.ato = (double)info->tcpi_ato / 1000; |
8250bc9f | 2996 | s.mss = info->tcpi_snd_mss; |
38e6dbc4 ED |
2997 | s.rcv_mss = info->tcpi_rcv_mss; |
2998 | s.advmss = info->tcpi_advmss; | |
8250bc9f VK |
2999 | s.rcv_space = info->tcpi_rcv_space; |
3000 | s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000; | |
3001 | s.lastsnd = info->tcpi_last_data_sent; | |
3002 | s.lastrcv = info->tcpi_last_data_recv; | |
3003 | s.lastack = info->tcpi_last_ack_recv; | |
3004 | s.unacked = info->tcpi_unacked; | |
3005 | s.retrans = info->tcpi_retrans; | |
3006 | s.retrans_total = info->tcpi_total_retrans; | |
3007 | s.lost = info->tcpi_lost; | |
3008 | s.sacked = info->tcpi_sacked; | |
c4be5feb | 3009 | s.fackets = info->tcpi_fackets; |
8250bc9f | 3010 | s.reordering = info->tcpi_reordering; |
00ac78d3 | 3011 | s.rcv_ssthresh = info->tcpi_rcv_ssthresh; |
8250bc9f | 3012 | s.cwnd = info->tcpi_snd_cwnd; |
3d791a32 | 3013 | s.pmtu = info->tcpi_pmtu; |
8250bc9f | 3014 | |
7d105b56 | 3015 | if (info->tcpi_snd_ssthresh < 0xFFFF) |
8250bc9f | 3016 | s.ssthresh = info->tcpi_snd_ssthresh; |
52d5ac3f | 3017 | |
b4b0b7d5 | 3018 | rtt = (double) info->tcpi_rtt; |
351efcde | 3019 | if (tb[INET_DIAG_VEGASINFO]) { |
05e18118 | 3020 | const struct tcpvegas_info *vinfo |
351efcde | 3021 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
7d105b56 | 3022 | |
ae665a52 | 3023 | if (vinfo->tcpv_enabled && |
8250bc9f | 3024 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
ea8fc104 | 3025 | rtt = vinfo->tcpv_rtt; |
b4b0b7d5 SH |
3026 | } |
3027 | ||
907e1aca | 3028 | if (tb[INET_DIAG_DCTCPINFO]) { |
8250bc9f VK |
3029 | struct dctcpstat *dctcp = malloc(sizeof(struct |
3030 | dctcpstat)); | |
3031 | ||
907e1aca DB |
3032 | const struct tcp_dctcp_info *dinfo |
3033 | = RTA_DATA(tb[INET_DIAG_DCTCPINFO]); | |
3034 | ||
8250bc9f VK |
3035 | dctcp->enabled = !!dinfo->dctcp_enabled; |
3036 | dctcp->ce_state = dinfo->dctcp_ce_state; | |
3037 | dctcp->alpha = dinfo->dctcp_alpha; | |
3038 | dctcp->ab_ecn = dinfo->dctcp_ab_ecn; | |
3039 | dctcp->ab_tot = dinfo->dctcp_ab_tot; | |
3040 | s.dctcp = dctcp; | |
907e1aca DB |
3041 | } |
3042 | ||
2f0f9aef NC |
3043 | if (tb[INET_DIAG_BBRINFO]) { |
3044 | const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]); | |
3045 | int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]), | |
3046 | sizeof(*s.bbr_info)); | |
3047 | ||
3048 | s.bbr_info = calloc(1, sizeof(*s.bbr_info)); | |
3049 | if (s.bbr_info && bbr_info) | |
3050 | memcpy(s.bbr_info, bbr_info, len); | |
3051 | } | |
3052 | ||
b4b0b7d5 | 3053 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { |
8250bc9f VK |
3054 | s.send_bps = (double) info->tcpi_snd_cwnd * |
3055 | (double)info->tcpi_snd_mss * 8000000. / rtt; | |
7d105b56 | 3056 | } |
b4b0b7d5 | 3057 | |
eb6028b2 | 3058 | if (info->tcpi_pacing_rate && |
8250bc9f VK |
3059 | info->tcpi_pacing_rate != ~0ULL) { |
3060 | s.pacing_rate = info->tcpi_pacing_rate * 8.; | |
eb6028b2 ED |
3061 | |
3062 | if (info->tcpi_max_pacing_rate && | |
8250bc9f VK |
3063 | info->tcpi_max_pacing_rate != ~0ULL) |
3064 | s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.; | |
eb6028b2 | 3065 | } |
1a4dda71 ED |
3066 | s.bytes_acked = info->tcpi_bytes_acked; |
3067 | s.bytes_received = info->tcpi_bytes_received; | |
ecb435ea CG |
3068 | s.segs_out = info->tcpi_segs_out; |
3069 | s.segs_in = info->tcpi_segs_in; | |
414aeec9 MKL |
3070 | s.data_segs_out = info->tcpi_data_segs_out; |
3071 | s.data_segs_in = info->tcpi_data_segs_in; | |
9e99e495 | 3072 | s.not_sent = info->tcpi_notsent_bytes; |
d9ba887e ED |
3073 | if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U) |
3074 | s.min_rtt = (double) info->tcpi_min_rtt / 1000; | |
2f579872 NC |
3075 | s.delivery_rate = info->tcpi_delivery_rate * 8.; |
3076 | s.app_limited = info->tcpi_delivery_rate_app_limited; | |
b6c7fc61 YC |
3077 | s.busy_time = info->tcpi_busy_time; |
3078 | s.rwnd_limited = info->tcpi_rwnd_limited; | |
3079 | s.sndbuf_limited = info->tcpi_sndbuf_limited; | |
2f4d834b ED |
3080 | s.delivered = info->tcpi_delivered; |
3081 | s.delivered_ce = info->tcpi_delivered_ce; | |
5eead627 ED |
3082 | s.dsack_dups = info->tcpi_dsack_dups; |
3083 | s.reord_seen = info->tcpi_reord_seen; | |
3084 | s.bytes_sent = info->tcpi_bytes_sent; | |
3085 | s.bytes_retrans = info->tcpi_bytes_retrans; | |
8250bc9f | 3086 | tcp_stats_print(&s); |
92de1c2c | 3087 | free(s.dctcp); |
2f0f9aef | 3088 | free(s.bbr_info); |
77a8ca81 | 3089 | } |
da9cc6ab ID |
3090 | if (tb[INET_DIAG_MD5SIG]) { |
3091 | struct tcp_diag_md5sig *sig = RTA_DATA(tb[INET_DIAG_MD5SIG]); | |
3092 | int len = RTA_PAYLOAD(tb[INET_DIAG_MD5SIG]); | |
3093 | ||
90351722 | 3094 | out(" md5keys:"); |
da9cc6ab ID |
3095 | print_md5sig(sig++); |
3096 | for (len -= sizeof(*sig); len > 0; len -= sizeof(*sig)) { | |
90351722 | 3097 | out(","); |
da9cc6ab ID |
3098 | print_md5sig(sig++); |
3099 | } | |
3100 | } | |
14cadc70 DC |
3101 | if (tb[INET_DIAG_ULP_INFO]) { |
3102 | struct rtattr *ulpinfo[INET_ULP_INFO_MAX + 1] = { 0 }; | |
3103 | ||
3104 | parse_rtattr_nested(ulpinfo, INET_ULP_INFO_MAX, | |
3105 | tb[INET_DIAG_ULP_INFO]); | |
3106 | ||
3107 | if (ulpinfo[INET_ULP_INFO_NAME]) | |
3108 | out(" tcp-ulp-%s", | |
3109 | rta_getattr_str(ulpinfo[INET_ULP_INFO_NAME])); | |
3110 | ||
3111 | if (ulpinfo[INET_ULP_INFO_TLS]) { | |
3112 | struct rtattr *tlsinfo[TLS_INFO_MAX + 1] = { 0 }; | |
3113 | ||
3114 | parse_rtattr_nested(tlsinfo, TLS_INFO_MAX, | |
3115 | ulpinfo[INET_ULP_INFO_TLS]); | |
3116 | ||
3117 | tcp_tls_version(tlsinfo[TLS_INFO_VERSION]); | |
3118 | tcp_tls_cipher(tlsinfo[TLS_INFO_CIPHER]); | |
3119 | tcp_tls_conf("rxconf", tlsinfo[TLS_INFO_RXCONF]); | |
3120 | tcp_tls_conf("txconf", tlsinfo[TLS_INFO_TXCONF]); | |
3121 | } | |
712fdd98 DC |
3122 | if (ulpinfo[INET_ULP_INFO_MPTCP]) { |
3123 | struct rtattr *sfinfo[MPTCP_SUBFLOW_ATTR_MAX + 1] = | |
3124 | { 0 }; | |
3125 | ||
3126 | parse_rtattr_nested(sfinfo, MPTCP_SUBFLOW_ATTR_MAX, | |
3127 | ulpinfo[INET_ULP_INFO_MPTCP]); | |
3128 | mptcp_subflow_info(sfinfo); | |
3129 | } | |
14cadc70 | 3130 | } |
77a8ca81 PE |
3131 | } |
3132 | ||
9c3be2c0 PA |
3133 | static void mptcp_stats_print(struct mptcp_info *s) |
3134 | { | |
3135 | if (s->mptcpi_subflows) | |
3136 | out(" subflows:%d", s->mptcpi_subflows); | |
3137 | if (s->mptcpi_add_addr_signal) | |
3138 | out(" add_addr_signal:%d", s->mptcpi_add_addr_signal); | |
3139 | if (s->mptcpi_add_addr_signal) | |
3140 | out(" add_addr_accepted:%d", s->mptcpi_add_addr_accepted); | |
3141 | if (s->mptcpi_subflows_max) | |
3142 | out(" subflows_max:%d", s->mptcpi_subflows_max); | |
3143 | if (s->mptcpi_add_addr_signal_max) | |
3144 | out(" add_addr_signal_max:%d", s->mptcpi_add_addr_signal_max); | |
3145 | if (s->mptcpi_add_addr_accepted_max) | |
3146 | out(" add_addr_accepted_max:%d", s->mptcpi_add_addr_accepted_max); | |
3147 | if (s->mptcpi_flags & MPTCP_INFO_FLAG_FALLBACK) | |
3148 | out(" fallback"); | |
3149 | if (s->mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED) | |
3150 | out(" remote_key"); | |
3151 | if (s->mptcpi_token) | |
3152 | out(" token:%x", s->mptcpi_token); | |
3153 | if (s->mptcpi_write_seq) | |
3154 | out(" write_seq:%llx", s->mptcpi_write_seq); | |
3155 | if (s->mptcpi_snd_una) | |
3156 | out(" snd_una:%llx", s->mptcpi_snd_una); | |
3157 | if (s->mptcpi_rcv_nxt) | |
3158 | out(" rcv_nxt:%llx", s->mptcpi_rcv_nxt); | |
3159 | } | |
3160 | ||
3161 | static void mptcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, | |
3162 | struct rtattr *tb[]) | |
3163 | { | |
3164 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); | |
3165 | ||
3166 | if (tb[INET_DIAG_INFO]) { | |
3167 | struct mptcp_info *info; | |
3168 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); | |
3169 | ||
3170 | /* workaround for older kernels with less fields */ | |
3171 | if (len < sizeof(*info)) { | |
3172 | info = alloca(sizeof(*info)); | |
3173 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); | |
3174 | memset((char *)info + len, 0, sizeof(*info) - len); | |
3175 | } else | |
3176 | info = RTA_DATA(tb[INET_DIAG_INFO]); | |
3177 | ||
3178 | mptcp_stats_print(info); | |
3179 | } | |
3180 | } | |
3181 | ||
f89d46ad PS |
3182 | static const char *format_host_sa(struct sockaddr_storage *sa) |
3183 | { | |
3184 | union { | |
3185 | struct sockaddr_in sin; | |
3186 | struct sockaddr_in6 sin6; | |
3187 | } *saddr = (void *)sa; | |
3188 | ||
3189 | switch (sa->ss_family) { | |
3190 | case AF_INET: | |
3191 | return format_host(AF_INET, 4, &saddr->sin.sin_addr); | |
3192 | case AF_INET6: | |
3193 | return format_host(AF_INET6, 16, &saddr->sin6.sin6_addr); | |
3194 | default: | |
3195 | return ""; | |
3196 | } | |
3197 | } | |
3198 | ||
3199 | static void sctp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, | |
3200 | struct rtattr *tb[]) | |
3201 | { | |
3202 | struct sockaddr_storage *sa; | |
3203 | int len; | |
3204 | ||
3205 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); | |
3206 | ||
3207 | if (tb[INET_DIAG_LOCALS]) { | |
3208 | len = RTA_PAYLOAD(tb[INET_DIAG_LOCALS]); | |
3209 | sa = RTA_DATA(tb[INET_DIAG_LOCALS]); | |
3210 | ||
2d7cb222 | 3211 | out(" locals:%s", format_host_sa(sa)); |
f89d46ad | 3212 | for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) |
90351722 | 3213 | out(",%s", format_host_sa(sa)); |
f89d46ad PS |
3214 | |
3215 | } | |
3216 | if (tb[INET_DIAG_PEERS]) { | |
3217 | len = RTA_PAYLOAD(tb[INET_DIAG_PEERS]); | |
3218 | sa = RTA_DATA(tb[INET_DIAG_PEERS]); | |
3219 | ||
90351722 | 3220 | out(" peers:%s", format_host_sa(sa)); |
f89d46ad | 3221 | for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) |
90351722 | 3222 | out(",%s", format_host_sa(sa)); |
f89d46ad PS |
3223 | } |
3224 | if (tb[INET_DIAG_INFO]) { | |
3225 | struct sctp_info *info; | |
3226 | len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); | |
3227 | ||
3228 | /* workaround for older kernels with less fields */ | |
3229 | if (len < sizeof(*info)) { | |
3230 | info = alloca(sizeof(*info)); | |
3231 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); | |
3232 | memset((char *)info + len, 0, sizeof(*info) - len); | |
3233 | } else | |
3234 | info = RTA_DATA(tb[INET_DIAG_INFO]); | |
3235 | ||
3236 | sctp_stats_print(info); | |
3237 | } | |
3238 | } | |
3239 | ||
82d73ea0 | 3240 | static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s) |
aba5acdf | 3241 | { |
acd1e437 | 3242 | struct rtattr *tb[INET_DIAG_MAX+1]; |
351efcde | 3243 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
aba5acdf | 3244 | |
acd1e437 | 3245 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), |
5b816047 PE |
3246 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
3247 | ||
82d73ea0 DA |
3248 | s->state = r->idiag_state; |
3249 | s->local.family = s->remote.family = r->idiag_family; | |
3250 | s->lport = ntohs(r->id.idiag_sport); | |
3251 | s->rport = ntohs(r->id.idiag_dport); | |
3252 | s->wq = r->idiag_wqueue; | |
3253 | s->rq = r->idiag_rqueue; | |
3254 | s->ino = r->idiag_inode; | |
3255 | s->uid = r->idiag_uid; | |
3256 | s->iface = r->id.idiag_if; | |
3257 | s->sk = cookie_sk_get(&r->id.idiag_cookie[0]); | |
3258 | ||
ec75249b LC |
3259 | s->mark = 0; |
3260 | if (tb[INET_DIAG_MARK]) | |
9f1370c0 | 3261 | s->mark = rta_getattr_u32(tb[INET_DIAG_MARK]); |
14f4bda5 DY |
3262 | s->cgroup_id = 0; |
3263 | if (tb[INET_DIAG_CGROUP_ID]) | |
3264 | s->cgroup_id = rta_getattr_u64(tb[INET_DIAG_CGROUP_ID]); | |
41fe6c34 | 3265 | if (tb[INET_DIAG_PROTOCOL]) |
9f1370c0 | 3266 | s->raw_prot = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); |
41fe6c34 CG |
3267 | else |
3268 | s->raw_prot = 0; | |
ec75249b | 3269 | |
82d73ea0 DA |
3270 | if (s->local.family == AF_INET) |
3271 | s->local.bytelen = s->remote.bytelen = 4; | |
3272 | else | |
3273 | s->local.bytelen = s->remote.bytelen = 16; | |
8250bc9f | 3274 | |
82d73ea0 DA |
3275 | memcpy(s->local.data, r->id.idiag_src, s->local.bytelen); |
3276 | memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen); | |
3277 | } | |
aba5acdf | 3278 | |
82d73ea0 | 3279 | static int inet_show_sock(struct nlmsghdr *nlh, |
be7e4d20 | 3280 | struct sockstat *s) |
82d73ea0 DA |
3281 | { |
3282 | struct rtattr *tb[INET_DIAG_MAX+1]; | |
3283 | struct inet_diag_msg *r = NLMSG_DATA(nlh); | |
1267c0b9 | 3284 | unsigned char v6only = 0; |
82d73ea0 DA |
3285 | |
3286 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), | |
3287 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
aba5acdf | 3288 | |
6885e3bf | 3289 | if (tb[INET_DIAG_PROTOCOL]) |
9f1370c0 | 3290 | s->type = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); |
6885e3bf | 3291 | |
1267c0b9 PS |
3292 | if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) |
3293 | v6only = rta_getattr_u8(tb[INET_DIAG_SKV6ONLY]); | |
3294 | ||
3295 | inet_stats_print(s, v6only); | |
116ac927 | 3296 | |
055840f2 VK |
3297 | if (show_options) { |
3298 | struct tcpstat t = {}; | |
3299 | ||
3300 | t.timer = r->idiag_timer; | |
3301 | t.timeout = r->idiag_expires; | |
3302 | t.retrans = r->idiag_retrans; | |
be7e4d20 | 3303 | if (s->type == IPPROTO_SCTP) |
f89d46ad PS |
3304 | sctp_timer_print(&t); |
3305 | else | |
3306 | tcp_timer_print(&t); | |
055840f2 VK |
3307 | } |
3308 | ||
aba5acdf | 3309 | if (show_details) { |
82d73ea0 | 3310 | sock_details_print(s); |
1267c0b9 | 3311 | if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) |
90351722 | 3312 | out(" v6only:%u", v6only); |
1267c0b9 | 3313 | |
5b816047 PE |
3314 | if (tb[INET_DIAG_SHUTDOWN]) { |
3315 | unsigned char mask; | |
acd1e437 | 3316 | |
9f1370c0 | 3317 | mask = rta_getattr_u8(tb[INET_DIAG_SHUTDOWN]); |
90351722 SB |
3318 | out(" %c-%c", |
3319 | mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
5b816047 | 3320 | } |
aba5acdf | 3321 | } |
8250bc9f | 3322 | |
0f3f0ca3 KK |
3323 | if (show_tos) { |
3324 | if (tb[INET_DIAG_TOS]) | |
3325 | out(" tos:%#x", rta_getattr_u8(tb[INET_DIAG_TOS])); | |
3326 | if (tb[INET_DIAG_TCLASS]) | |
3327 | out(" tclass:%#x", rta_getattr_u8(tb[INET_DIAG_TCLASS])); | |
3328 | if (tb[INET_DIAG_CLASS_ID]) | |
3329 | out(" class_id:%#x", rta_getattr_u32(tb[INET_DIAG_CLASS_ID])); | |
3330 | } | |
3331 | ||
14f4bda5 DY |
3332 | if (show_cgroup) { |
3333 | if (tb[INET_DIAG_CGROUP_ID]) | |
3334 | out(" cgroup:%s", cg_id_to_path(rta_getattr_u64(tb[INET_DIAG_CGROUP_ID]))); | |
3335 | } | |
3336 | ||
ad34d5fa WW |
3337 | if (show_inet_sockopt) { |
3338 | if (tb[INET_DIAG_SOCKOPT] && RTA_PAYLOAD(tb[INET_DIAG_SOCKOPT]) >= | |
3339 | sizeof(struct inet_diag_sockopt)) { | |
3340 | const struct inet_diag_sockopt *sockopt = | |
3341 | RTA_DATA(tb[INET_DIAG_SOCKOPT]); | |
3342 | if (!oneline) | |
3343 | out("\n\tinet-sockopt: ("); | |
3344 | else | |
3345 | out(" inet-sockopt: ("); | |
3346 | if (sockopt->recverr) | |
3347 | out(" recverr"); | |
3348 | if (sockopt->is_icsk) | |
3349 | out(" is_icsk"); | |
3350 | if (sockopt->freebind) | |
3351 | out(" freebind"); | |
3352 | if (sockopt->hdrincl) | |
3353 | out(" hdrincl"); | |
3354 | if (sockopt->mc_loop) | |
3355 | out(" mc_loop"); | |
3356 | if (sockopt->transparent) | |
3357 | out(" transparent"); | |
3358 | if (sockopt->mc_all) | |
3359 | out(" mc_all"); | |
3360 | if (sockopt->nodefrag) | |
3361 | out(" nodefrag"); | |
3362 | if (sockopt->bind_address_no_port) | |
3363 | out(" bind_addr_no_port"); | |
3364 | if (sockopt->recverr_rfc4884) | |
3365 | out(" recverr_rfc4884"); | |
3366 | if (sockopt->defer_connect) | |
3367 | out(" defer_connect"); | |
3368 | out(")"); | |
3369 | } | |
3370 | } | |
3371 | ||
be7e4d20 | 3372 | if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) { |
296b5de7 JH |
3373 | if (!oneline) |
3374 | out("\n\t"); | |
be7e4d20 | 3375 | if (s->type == IPPROTO_SCTP) |
f89d46ad | 3376 | sctp_show_info(nlh, r, tb); |
9c3be2c0 PA |
3377 | else if (s->type == IPPROTO_MPTCP) |
3378 | mptcp_show_info(nlh, r, tb); | |
f89d46ad PS |
3379 | else |
3380 | tcp_show_info(nlh, r, tb); | |
aba5acdf | 3381 | } |
f89d46ad | 3382 | sctp_ino = s->ino; |
7d105b56 | 3383 | |
aba5acdf | 3384 | return 0; |
aba5acdf SH |
3385 | } |
3386 | ||
746a695f | 3387 | static int tcpdiag_send(int fd, int protocol, struct filter *f) |
aba5acdf | 3388 | { |
d17b136f | 3389 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
aba5acdf SH |
3390 | struct { |
3391 | struct nlmsghdr nlh; | |
351efcde | 3392 | struct inet_diag_req r; |
d17b136f PS |
3393 | } req = { |
3394 | .nlh.nlmsg_len = sizeof(req), | |
3395 | .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST, | |
3396 | .nlh.nlmsg_seq = MAGIC_SEQ, | |
3397 | .r.idiag_family = AF_INET, | |
3398 | .r.idiag_states = f->states, | |
3399 | }; | |
aba5acdf SH |
3400 | char *bc = NULL; |
3401 | int bclen; | |
3402 | struct msghdr msg; | |
3403 | struct rtattr rta; | |
aba5acdf | 3404 | struct iovec iov[3]; |
376fb868 | 3405 | int iovlen = 1; |
aba5acdf | 3406 | |
346f8ca8 PE |
3407 | if (protocol == IPPROTO_UDP) |
3408 | return -1; | |
3409 | ||
3fe5b534 PE |
3410 | if (protocol == IPPROTO_TCP) |
3411 | req.nlh.nlmsg_type = TCPDIAG_GETSOCK; | |
3412 | else | |
3413 | req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; | |
910b0397 | 3414 | if (show_mem) { |
ae665a52 | 3415 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
910b0397 SW |
3416 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
3417 | } | |
b4b0b7d5 | 3418 | |
7d105b56 | 3419 | if (show_tcpinfo) { |
351efcde SH |
3420 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
3421 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
3422 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
7d105b56 | 3423 | } |
aba5acdf | 3424 | |
0f3f0ca3 KK |
3425 | if (show_tos) { |
3426 | req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1)); | |
3427 | req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1)); | |
3428 | } | |
3429 | ||
ae665a52 SH |
3430 | iov[0] = (struct iovec){ |
3431 | .iov_base = &req, | |
3432 | .iov_len = sizeof(req) | |
ea8fc104 | 3433 | }; |
aba5acdf SH |
3434 | if (f->f) { |
3435 | bclen = ssfilter_bytecompile(f->f, &bc); | |
376fb868 DA |
3436 | if (bclen) { |
3437 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
3438 | rta.rta_len = RTA_LENGTH(bclen); | |
3439 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
3440 | iov[2] = (struct iovec){ bc, bclen }; | |
3441 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
3442 | iovlen = 3; | |
3443 | } | |
aba5acdf SH |
3444 | } |
3445 | ||
3446 | msg = (struct msghdr) { | |
acd1e437 | 3447 | .msg_name = (void *)&nladdr, |
ea8fc104 | 3448 | .msg_namelen = sizeof(nladdr), |
ae665a52 | 3449 | .msg_iov = iov, |
376fb868 | 3450 | .msg_iovlen = iovlen, |
aba5acdf SH |
3451 | }; |
3452 | ||
930a75f9 ED |
3453 | if (sendmsg(fd, &msg, 0) < 0) { |
3454 | close(fd); | |
aba5acdf | 3455 | return -1; |
930a75f9 | 3456 | } |
aba5acdf | 3457 | |
746a695f PE |
3458 | return 0; |
3459 | } | |
3460 | ||
886d19d6 PE |
3461 | static int sockdiag_send(int family, int fd, int protocol, struct filter *f) |
3462 | { | |
d17b136f | 3463 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
5fb421d4 | 3464 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
886d19d6 PE |
3465 | char *bc = NULL; |
3466 | int bclen; | |
9c3be2c0 | 3467 | __u32 proto; |
886d19d6 | 3468 | struct msghdr msg; |
9c3be2c0 PA |
3469 | struct rtattr rta_bc; |
3470 | struct rtattr rta_proto; | |
3471 | struct iovec iov[5]; | |
376fb868 | 3472 | int iovlen = 1; |
886d19d6 PE |
3473 | |
3474 | if (family == PF_UNSPEC) | |
3475 | return tcpdiag_send(fd, protocol, f); | |
3476 | ||
886d19d6 PE |
3477 | memset(&req.r, 0, sizeof(req.r)); |
3478 | req.r.sdiag_family = family; | |
3479 | req.r.sdiag_protocol = protocol; | |
3480 | req.r.idiag_states = f->states; | |
3481 | if (show_mem) { | |
3482 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); | |
3483 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); | |
3484 | } | |
3485 | ||
3486 | if (show_tcpinfo) { | |
3487 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); | |
3488 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
3489 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
3490 | } | |
3491 | ||
0f3f0ca3 KK |
3492 | if (show_tos) { |
3493 | req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1)); | |
3494 | req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1)); | |
3495 | } | |
3496 | ||
886d19d6 PE |
3497 | iov[0] = (struct iovec){ |
3498 | .iov_base = &req, | |
3499 | .iov_len = sizeof(req) | |
3500 | }; | |
3501 | if (f->f) { | |
3502 | bclen = ssfilter_bytecompile(f->f, &bc); | |
376fb868 | 3503 | if (bclen) { |
9c3be2c0 PA |
3504 | rta_bc.rta_type = INET_DIAG_REQ_BYTECODE; |
3505 | rta_bc.rta_len = RTA_LENGTH(bclen); | |
3506 | iov[1] = (struct iovec){ &rta_bc, sizeof(rta_bc) }; | |
376fb868 DA |
3507 | iov[2] = (struct iovec){ bc, bclen }; |
3508 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
3509 | iovlen = 3; | |
3510 | } | |
886d19d6 PE |
3511 | } |
3512 | ||
9c3be2c0 PA |
3513 | /* put extended protocol attribute, if required */ |
3514 | if (protocol > 255) { | |
3515 | rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL; | |
3516 | rta_proto.rta_len = RTA_LENGTH(sizeof(proto)); | |
3517 | proto = protocol; | |
3518 | iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto) }; | |
3519 | iov[iovlen + 1] = (struct iovec){ &proto, sizeof(proto) }; | |
3520 | req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto)); | |
3521 | iovlen += 2; | |
3522 | } | |
3523 | ||
886d19d6 | 3524 | msg = (struct msghdr) { |
acd1e437 | 3525 | .msg_name = (void *)&nladdr, |
886d19d6 PE |
3526 | .msg_namelen = sizeof(nladdr), |
3527 | .msg_iov = iov, | |
376fb868 | 3528 | .msg_iovlen = iovlen, |
886d19d6 PE |
3529 | }; |
3530 | ||
3531 | if (sendmsg(fd, &msg, 0) < 0) { | |
3532 | close(fd); | |
3533 | return -1; | |
3534 | } | |
3535 | ||
3536 | return 0; | |
3537 | } | |
3538 | ||
486ccd99 VK |
3539 | struct inet_diag_arg { |
3540 | struct filter *f; | |
3541 | int protocol; | |
fb2594c1 | 3542 | struct rtnl_handle *rth; |
486ccd99 | 3543 | }; |
aba5acdf | 3544 | |
41fe6c34 | 3545 | static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s) |
fb2594c1 LC |
3546 | { |
3547 | struct inet_diag_msg *d = NLMSG_DATA(h); | |
3548 | struct inet_diag_arg *diag_arg = arg; | |
3549 | struct rtnl_handle *rth = diag_arg->rth; | |
acd1e437 | 3550 | |
fb2594c1 LC |
3551 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
3552 | ||
3553 | req.nlh.nlmsg_type = SOCK_DESTROY; | |
3554 | req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; | |
3555 | req.nlh.nlmsg_seq = ++rth->seq; | |
3556 | req.r.sdiag_family = d->idiag_family; | |
3557 | req.r.sdiag_protocol = diag_arg->protocol; | |
3558 | req.r.id = d->id; | |
3559 | ||
41fe6c34 CG |
3560 | if (diag_arg->protocol == IPPROTO_RAW) { |
3561 | struct inet_diag_req_raw *raw = (void *)&req.r; | |
3562 | ||
3563 | BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw)); | |
3564 | raw->sdiag_raw_protocol = s->raw_prot; | |
3565 | } | |
3566 | ||
86bf43c7 | 3567 | return rtnl_talk(rth, &req.nlh, NULL); |
fb2594c1 LC |
3568 | } |
3569 | ||
cd554f2c | 3570 | static int show_one_inet_sock(struct nlmsghdr *h, void *arg) |
486ccd99 VK |
3571 | { |
3572 | int err; | |
3573 | struct inet_diag_arg *diag_arg = arg; | |
3574 | struct inet_diag_msg *r = NLMSG_DATA(h); | |
82d73ea0 | 3575 | struct sockstat s = {}; |
aba5acdf | 3576 | |
b338a3e7 | 3577 | if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family))) |
486ccd99 | 3578 | return 0; |
82d73ea0 DA |
3579 | |
3580 | parse_diag_msg(h, &s); | |
be7e4d20 | 3581 | s.type = diag_arg->protocol; |
82d73ea0 DA |
3582 | |
3583 | if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0) | |
3584 | return 0; | |
3585 | ||
41fe6c34 | 3586 | if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) { |
fb2594c1 LC |
3587 | if (errno == EOPNOTSUPP || errno == ENOENT) { |
3588 | /* Socket can't be closed, or is already closed. */ | |
3589 | return 0; | |
3590 | } else { | |
3591 | perror("SOCK_DESTROY answers"); | |
3592 | return -1; | |
3593 | } | |
3594 | } | |
82d73ea0 | 3595 | |
be7e4d20 | 3596 | err = inet_show_sock(h, &s); |
82d73ea0 | 3597 | if (err < 0) |
486ccd99 | 3598 | return err; |
aba5acdf | 3599 | |
486ccd99 VK |
3600 | return 0; |
3601 | } | |
886d19d6 | 3602 | |
486ccd99 VK |
3603 | static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) |
3604 | { | |
3605 | int err = 0; | |
fb2594c1 | 3606 | struct rtnl_handle rth, rth2; |
486ccd99 VK |
3607 | int family = PF_INET; |
3608 | struct inet_diag_arg arg = { .f = f, .protocol = protocol }; | |
886d19d6 | 3609 | |
486ccd99 VK |
3610 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
3611 | return -1; | |
fb2594c1 LC |
3612 | |
3613 | if (f->kill) { | |
3614 | if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) { | |
3615 | rtnl_close(&rth); | |
3616 | return -1; | |
3617 | } | |
3618 | arg.rth = &rth2; | |
3619 | } | |
3620 | ||
486ccd99 VK |
3621 | rth.dump = MAGIC_SEQ; |
3622 | rth.dump_fp = dump_fp; | |
518af1e0 ED |
3623 | if (preferred_family == PF_INET6) |
3624 | family = PF_INET6; | |
886d19d6 | 3625 | |
486ccd99 VK |
3626 | again: |
3627 | if ((err = sockdiag_send(family, rth.fd, protocol, f))) | |
3628 | goto Exit; | |
aba5acdf | 3629 | |
486ccd99 VK |
3630 | if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) { |
3631 | if (family != PF_UNSPEC) { | |
3632 | family = PF_UNSPEC; | |
3633 | goto again; | |
aba5acdf | 3634 | } |
486ccd99 | 3635 | goto Exit; |
aba5acdf | 3636 | } |
518af1e0 | 3637 | if (family == PF_INET && preferred_family != PF_INET) { |
886d19d6 PE |
3638 | family = PF_INET6; |
3639 | goto again; | |
3640 | } | |
3641 | ||
486ccd99 VK |
3642 | Exit: |
3643 | rtnl_close(&rth); | |
fb2594c1 LC |
3644 | if (arg.rth) |
3645 | rtnl_close(arg.rth); | |
486ccd99 | 3646 | return err; |
aba5acdf SH |
3647 | } |
3648 | ||
ab01dbbb | 3649 | static int tcp_show_netlink_file(struct filter *f) |
aba5acdf SH |
3650 | { |
3651 | FILE *fp; | |
e557212e | 3652 | char buf[16384]; |
4b45ae22 | 3653 | int err = -1; |
aba5acdf SH |
3654 | |
3655 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { | |
3656 | perror("fopen($TCPDIAG_FILE)"); | |
4b45ae22 | 3657 | return err; |
aba5acdf SH |
3658 | } |
3659 | ||
3660 | while (1) { | |
eca51239 MŁ |
3661 | int err2; |
3662 | size_t status, nitems; | |
acd1e437 | 3663 | struct nlmsghdr *h = (struct nlmsghdr *)buf; |
82d73ea0 | 3664 | struct sockstat s = {}; |
aba5acdf SH |
3665 | |
3666 | status = fread(buf, 1, sizeof(*h), fp); | |
aba5acdf | 3667 | if (status != sizeof(*h)) { |
eca51239 MŁ |
3668 | if (ferror(fp)) |
3669 | perror("Reading header from $TCPDIAG_FILE"); | |
3670 | if (feof(fp)) | |
3671 | fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE"); | |
4b45ae22 | 3672 | break; |
aba5acdf SH |
3673 | } |
3674 | ||
eca51239 MŁ |
3675 | nitems = NLMSG_ALIGN(h->nlmsg_len - sizeof(*h)); |
3676 | status = fread(h+1, 1, nitems, fp); | |
aba5acdf | 3677 | |
eca51239 MŁ |
3678 | if (status != nitems) { |
3679 | if (ferror(fp)) | |
3680 | perror("Reading $TCPDIAG_FILE"); | |
3681 | if (feof(fp)) | |
3682 | fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE"); | |
4b45ae22 | 3683 | break; |
aba5acdf SH |
3684 | } |
3685 | ||
3686 | /* The only legal exit point */ | |
4b45ae22 PS |
3687 | if (h->nlmsg_type == NLMSG_DONE) { |
3688 | err = 0; | |
3689 | break; | |
3690 | } | |
aba5acdf SH |
3691 | |
3692 | if (h->nlmsg_type == NLMSG_ERROR) { | |
acd1e437 SH |
3693 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
3694 | ||
aba5acdf SH |
3695 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
3696 | fprintf(stderr, "ERROR truncated\n"); | |
3697 | } else { | |
3698 | errno = -err->error; | |
3699 | perror("TCPDIAG answered"); | |
3700 | } | |
4b45ae22 | 3701 | break; |
aba5acdf SH |
3702 | } |
3703 | ||
82d73ea0 | 3704 | parse_diag_msg(h, &s); |
be7e4d20 | 3705 | s.type = IPPROTO_TCP; |
82d73ea0 DA |
3706 | |
3707 | if (f && f->f && run_ssfilter(f->f, &s) == 0) | |
3708 | continue; | |
3709 | ||
4b45ae22 PS |
3710 | err2 = inet_show_sock(h, &s); |
3711 | if (err2 < 0) { | |
3712 | err = err2; | |
3713 | break; | |
3714 | } | |
aba5acdf | 3715 | } |
4b45ae22 PS |
3716 | |
3717 | fclose(fp); | |
3718 | return err; | |
aba5acdf SH |
3719 | } |
3720 | ||
6c6bbc30 | 3721 | static int tcp_show(struct filter *f) |
aba5acdf | 3722 | { |
ab01dbbb | 3723 | FILE *fp = NULL; |
aba5acdf | 3724 | char *buf = NULL; |
10f68773 | 3725 | int bufsize = 1024*1024; |
aba5acdf | 3726 | |
1527a17e VK |
3727 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
3728 | return 0; | |
3729 | ||
aba5acdf SH |
3730 | dg_proto = TCP_PROTO; |
3731 | ||
3732 | if (getenv("TCPDIAG_FILE")) | |
3733 | return tcp_show_netlink_file(f); | |
3734 | ||
3735 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") | |
6c6bbc30 | 3736 | && inet_show_netlink(f, NULL, IPPROTO_TCP) == 0) |
aba5acdf SH |
3737 | return 0; |
3738 | ||
3739 | /* Sigh... We have to parse /proc/net/tcp... */ | |
aba5acdf SH |
3740 | while (bufsize >= 64*1024) { |
3741 | if ((buf = malloc(bufsize)) != NULL) | |
3742 | break; | |
3743 | bufsize /= 2; | |
3744 | } | |
3745 | if (buf == NULL) { | |
3746 | errno = ENOMEM; | |
3747 | return -1; | |
3748 | } | |
3749 | ||
b338a3e7 | 3750 | if (f->families & FAMILY_MASK(AF_INET)) { |
69cae645 | 3751 | if ((fp = net_tcp_open()) == NULL) |
aba5acdf | 3752 | goto outerr; |
ab01dbbb SH |
3753 | |
3754 | setbuffer(fp, buf, bufsize); | |
3755 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) | |
aba5acdf | 3756 | goto outerr; |
ab01dbbb | 3757 | fclose(fp); |
aba5acdf SH |
3758 | } |
3759 | ||
b338a3e7 | 3760 | if ((f->families & FAMILY_MASK(AF_INET6)) && |
69cae645 | 3761 | (fp = net_tcp6_open()) != NULL) { |
ab01dbbb SH |
3762 | setbuffer(fp, buf, bufsize); |
3763 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) | |
aba5acdf | 3764 | goto outerr; |
ab01dbbb | 3765 | fclose(fp); |
aba5acdf SH |
3766 | } |
3767 | ||
3768 | free(buf); | |
3769 | return 0; | |
3770 | ||
3771 | outerr: | |
3772 | do { | |
3773 | int saved_errno = errno; | |
acd1e437 | 3774 | |
92de1c2c | 3775 | free(buf); |
ab01dbbb SH |
3776 | if (fp) |
3777 | fclose(fp); | |
aba5acdf SH |
3778 | errno = saved_errno; |
3779 | return -1; | |
3780 | } while (0); | |
3781 | } | |
3782 | ||
9c3be2c0 PA |
3783 | static int mptcp_show(struct filter *f) |
3784 | { | |
3785 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3786 | return 0; | |
3787 | ||
3788 | if (!getenv("PROC_NET_MPTCP") && !getenv("PROC_ROOT") | |
3789 | && inet_show_netlink(f, NULL, IPPROTO_MPTCP) == 0) | |
3790 | return 0; | |
3791 | ||
3792 | return 0; | |
3793 | } | |
3794 | ||
6c6bbc30 PS |
3795 | static int dccp_show(struct filter *f) |
3796 | { | |
3797 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3798 | return 0; | |
3799 | ||
3800 | if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT") | |
3801 | && inet_show_netlink(f, NULL, IPPROTO_DCCP) == 0) | |
3802 | return 0; | |
3803 | ||
3804 | return 0; | |
3805 | } | |
3806 | ||
f89d46ad PS |
3807 | static int sctp_show(struct filter *f) |
3808 | { | |
3809 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3810 | return 0; | |
3811 | ||
3812 | if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT") | |
3813 | && inet_show_netlink(f, NULL, IPPROTO_SCTP) == 0) | |
3814 | return 0; | |
3815 | ||
3816 | return 0; | |
3817 | } | |
aba5acdf | 3818 | |
d1f28cf1 | 3819 | static int dgram_show_line(char *line, const struct filter *f, int family) |
aba5acdf | 3820 | { |
055840f2 | 3821 | struct sockstat s = {}; |
aba5acdf SH |
3822 | char *loc, *rem, *data; |
3823 | char opt[256]; | |
3824 | int n; | |
aba5acdf | 3825 | |
8250bc9f | 3826 | if (proc_inet_split_line(line, &loc, &rem, &data)) |
aba5acdf | 3827 | return -1; |
aba5acdf | 3828 | |
8250bc9f | 3829 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
acd1e437 | 3830 | |
8250bc9f VK |
3831 | if (!(f->states & (1 << state))) |
3832 | return 0; | |
aba5acdf | 3833 | |
8250bc9f | 3834 | proc_parse_inet_addr(loc, rem, family, &s); |
aba5acdf SH |
3835 | |
3836 | if (f->f && run_ssfilter(f->f, &s) == 0) | |
3837 | return 0; | |
3838 | ||
3839 | opt[0] = 0; | |
e7113c61 | 3840 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
aba5acdf SH |
3841 | &s.state, &s.wq, &s.rq, |
3842 | &s.uid, &s.ino, | |
3843 | &s.refcnt, &s.sk, opt); | |
3844 | ||
3845 | if (n < 9) | |
3846 | opt[0] = 0; | |
3847 | ||
be7e4d20 | 3848 | s.type = dg_proto == UDP_PROTO ? IPPROTO_UDP : 0; |
1267c0b9 | 3849 | inet_stats_print(&s, false); |
aba5acdf | 3850 | |
f1b39e1b | 3851 | if (show_details && opt[0]) |
90351722 | 3852 | out(" opt:\"%s\"", opt); |
aba5acdf SH |
3853 | |
3854 | return 0; | |
3855 | } | |
3856 | ||
d1f28cf1 | 3857 | static int udp_show(struct filter *f) |
aba5acdf | 3858 | { |
ab01dbbb | 3859 | FILE *fp = NULL; |
aba5acdf | 3860 | |
1527a17e VK |
3861 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
3862 | return 0; | |
3863 | ||
ace5cb31 VK |
3864 | dg_proto = UDP_PROTO; |
3865 | ||
346f8ca8 PE |
3866 | if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") |
3867 | && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) | |
3868 | return 0; | |
3869 | ||
b338a3e7 | 3870 | if (f->families&FAMILY_MASK(AF_INET)) { |
69cae645 | 3871 | if ((fp = net_udp_open()) == NULL) |
aba5acdf | 3872 | goto outerr; |
ab01dbbb | 3873 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 3874 | goto outerr; |
ab01dbbb | 3875 | fclose(fp); |
aba5acdf SH |
3876 | } |
3877 | ||
b338a3e7 | 3878 | if ((f->families&FAMILY_MASK(AF_INET6)) && |
69cae645 | 3879 | (fp = net_udp6_open()) != NULL) { |
ab01dbbb | 3880 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 3881 | goto outerr; |
ab01dbbb | 3882 | fclose(fp); |
aba5acdf SH |
3883 | } |
3884 | return 0; | |
3885 | ||
3886 | outerr: | |
3887 | do { | |
3888 | int saved_errno = errno; | |
acd1e437 | 3889 | |
ab01dbbb SH |
3890 | if (fp) |
3891 | fclose(fp); | |
aba5acdf SH |
3892 | errno = saved_errno; |
3893 | return -1; | |
3894 | } while (0); | |
3895 | } | |
3896 | ||
d1f28cf1 | 3897 | static int raw_show(struct filter *f) |
aba5acdf | 3898 | { |
ab01dbbb | 3899 | FILE *fp = NULL; |
aba5acdf | 3900 | |
1527a17e VK |
3901 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
3902 | return 0; | |
3903 | ||
aba5acdf SH |
3904 | dg_proto = RAW_PROTO; |
3905 | ||
41fe6c34 CG |
3906 | if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") && |
3907 | inet_show_netlink(f, NULL, IPPROTO_RAW) == 0) | |
3908 | return 0; | |
3909 | ||
b338a3e7 | 3910 | if (f->families&FAMILY_MASK(AF_INET)) { |
69cae645 | 3911 | if ((fp = net_raw_open()) == NULL) |
aba5acdf | 3912 | goto outerr; |
ab01dbbb | 3913 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 3914 | goto outerr; |
ab01dbbb | 3915 | fclose(fp); |
aba5acdf SH |
3916 | } |
3917 | ||
b338a3e7 | 3918 | if ((f->families&FAMILY_MASK(AF_INET6)) && |
69cae645 | 3919 | (fp = net_raw6_open()) != NULL) { |
ab01dbbb | 3920 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 3921 | goto outerr; |
ab01dbbb | 3922 | fclose(fp); |
aba5acdf SH |
3923 | } |
3924 | return 0; | |
3925 | ||
3926 | outerr: | |
3927 | do { | |
3928 | int saved_errno = errno; | |
acd1e437 | 3929 | |
ab01dbbb SH |
3930 | if (fp) |
3931 | fclose(fp); | |
aba5acdf SH |
3932 | errno = saved_errno; |
3933 | return -1; | |
3934 | } while (0); | |
3935 | } | |
3936 | ||
ec4d0d8a | 3937 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) |
aba5acdf | 3938 | |
2d0e538f | 3939 | static void unix_list_drop_first(struct sockstat **list) |
aba5acdf | 3940 | { |
2d0e538f | 3941 | struct sockstat *s = *list; |
ec4d0d8a | 3942 | |
2d0e538f PS |
3943 | (*list) = (*list)->next; |
3944 | free(s->name); | |
3945 | free(s); | |
aba5acdf SH |
3946 | } |
3947 | ||
ec4d0d8a | 3948 | static bool unix_type_skip(struct sockstat *s, struct filter *f) |
bf4ceee6 VK |
3949 | { |
3950 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) | |
3951 | return true; | |
3952 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) | |
3953 | return true; | |
3954 | if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB))) | |
3955 | return true; | |
3956 | return false; | |
3957 | } | |
3958 | ||
2d0e538f | 3959 | static void unix_stats_print(struct sockstat *s, struct filter *f) |
aba5acdf | 3960 | { |
b217df10 | 3961 | char port_name[30] = {}; |
aba5acdf | 3962 | |
2d0e538f | 3963 | sock_state_print(s); |
2d791bc8 | 3964 | |
2d0e538f PS |
3965 | sock_addr_print(s->name ?: "*", " ", |
3966 | int_to_str(s->lport, port_name), NULL); | |
3967 | sock_addr_print(s->peer_name ?: "*", " ", | |
3968 | int_to_str(s->rport, port_name), NULL); | |
116ac927 | 3969 | |
2d0e538f | 3970 | proc_ctx_print(s); |
aba5acdf SH |
3971 | } |
3972 | ||
cd554f2c | 3973 | static int unix_show_sock(struct nlmsghdr *nlh, void *arg) |
dfbaa90d | 3974 | { |
8a4025f6 | 3975 | struct filter *f = (struct filter *)arg; |
dfbaa90d PE |
3976 | struct unix_diag_msg *r = NLMSG_DATA(nlh); |
3977 | struct rtattr *tb[UNIX_DIAG_MAX+1]; | |
99bb68ff VK |
3978 | char name[128]; |
3979 | struct sockstat stat = { .name = "*", .peer_name = "*" }; | |
dfbaa90d | 3980 | |
acd1e437 | 3981 | parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1), |
dfbaa90d PE |
3982 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
3983 | ||
bf4ceee6 VK |
3984 | stat.type = r->udiag_type; |
3985 | stat.state = r->udiag_state; | |
ec4d0d8a VK |
3986 | stat.ino = stat.lport = r->udiag_ino; |
3987 | stat.local.family = stat.remote.family = AF_UNIX; | |
0d2e01c5 | 3988 | |
bf4ceee6 VK |
3989 | if (unix_type_skip(&stat, f)) |
3990 | return 0; | |
dfbaa90d | 3991 | |
defd61ca HFS |
3992 | if (tb[UNIX_DIAG_RQLEN]) { |
3993 | struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); | |
acd1e437 | 3994 | |
bf4ceee6 VK |
3995 | stat.rq = rql->udiag_rqueue; |
3996 | stat.wq = rql->udiag_wqueue; | |
defd61ca | 3997 | } |
dfbaa90d PE |
3998 | if (tb[UNIX_DIAG_NAME]) { |
3999 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); | |
4000 | ||
4001 | memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); | |
4002 | name[len] = '\0'; | |
1dd0cca7 SH |
4003 | if (name[0] == '\0') { |
4004 | int i; | |
4005 | for (i = 0; i < len; i++) | |
878dadc7 IB |
4006 | if (name[i] == '\0') |
4007 | name[i] = '@'; | |
1dd0cca7 | 4008 | } |
99bb68ff VK |
4009 | stat.name = &name[0]; |
4010 | memcpy(stat.local.data, &stat.name, sizeof(stat.name)); | |
bf4ceee6 | 4011 | } |
dfbaa90d | 4012 | if (tb[UNIX_DIAG_PEER]) |
ec4d0d8a | 4013 | stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); |
dfbaa90d | 4014 | |
29999b0f VK |
4015 | if (f->f && run_ssfilter(f->f, &stat) == 0) |
4016 | return 0; | |
4017 | ||
bf4ceee6 | 4018 | unix_stats_print(&stat, f); |
dfbaa90d | 4019 | |
45199997 | 4020 | if (show_mem) |
51ff9f24 | 4021 | print_skmeminfo(tb, UNIX_DIAG_MEMINFO); |
5b816047 PE |
4022 | if (show_details) { |
4023 | if (tb[UNIX_DIAG_SHUTDOWN]) { | |
4024 | unsigned char mask; | |
acd1e437 | 4025 | |
9f1370c0 | 4026 | mask = rta_getattr_u8(tb[UNIX_DIAG_SHUTDOWN]); |
90351722 SB |
4027 | out(" %c-%c", |
4028 | mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
5b816047 | 4029 | } |
0bab7630 CW |
4030 | if (tb[UNIX_DIAG_VFS]) { |
4031 | struct unix_diag_vfs *uv = RTA_DATA(tb[UNIX_DIAG_VFS]); | |
4032 | ||
4033 | out(" ino:%u dev:%u/%u", uv->udiag_vfs_ino, major(uv->udiag_vfs_dev), | |
4034 | minor(uv->udiag_vfs_dev)); | |
4035 | } | |
4036 | if (tb[UNIX_DIAG_ICONS]) { | |
4037 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_ICONS]); | |
4038 | __u32 *peers = RTA_DATA(tb[UNIX_DIAG_ICONS]); | |
4039 | int i; | |
4040 | ||
4041 | out(" peers:"); | |
4042 | for (i = 0; i < len / sizeof(__u32); i++) | |
4043 | out(" %u", peers[i]); | |
4044 | } | |
5b816047 | 4045 | } |
ec4d0d8a | 4046 | |
dfbaa90d PE |
4047 | return 0; |
4048 | } | |
4049 | ||
8a4025f6 | 4050 | static int handle_netlink_request(struct filter *f, struct nlmsghdr *req, |
4051 | size_t size, rtnl_filter_t show_one_sock) | |
dfbaa90d | 4052 | { |
8a4025f6 | 4053 | int ret = -1; |
4054 | struct rtnl_handle rth; | |
dfbaa90d | 4055 | |
8a4025f6 | 4056 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
dfbaa90d PE |
4057 | return -1; |
4058 | ||
8a4025f6 | 4059 | rth.dump = MAGIC_SEQ; |
dfbaa90d | 4060 | |
8a4025f6 | 4061 | if (rtnl_send(&rth, req, size) < 0) |
4062 | goto Exit; | |
dfbaa90d | 4063 | |
8a4025f6 | 4064 | if (rtnl_dump_filter(&rth, show_one_sock, f)) |
4065 | goto Exit; | |
dfbaa90d | 4066 | |
8a4025f6 | 4067 | ret = 0; |
4068 | Exit: | |
4069 | rtnl_close(&rth); | |
4070 | return ret; | |
dfbaa90d PE |
4071 | } |
4072 | ||
8a4025f6 | 4073 | static int unix_show_netlink(struct filter *f) |
d8402b96 | 4074 | { |
5fb421d4 | 4075 | DIAG_REQUEST(req, struct unix_diag_req r); |
d8402b96 AV |
4076 | |
4077 | req.r.sdiag_family = AF_UNIX; | |
4078 | req.r.udiag_states = f->states; | |
4079 | req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; | |
4080 | if (show_mem) | |
4081 | req.r.udiag_show |= UDIAG_SHOW_MEMINFO; | |
0bab7630 CW |
4082 | if (show_details) |
4083 | req.r.udiag_show |= UDIAG_SHOW_VFS | UDIAG_SHOW_ICONS; | |
d8402b96 | 4084 | |
8a4025f6 | 4085 | return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock); |
d8402b96 AV |
4086 | } |
4087 | ||
d1f28cf1 | 4088 | static int unix_show(struct filter *f) |
aba5acdf SH |
4089 | { |
4090 | FILE *fp; | |
4091 | char buf[256]; | |
4092 | char name[128]; | |
4093 | int newformat = 0; | |
4094 | int cnt; | |
ec4d0d8a | 4095 | struct sockstat *list = NULL; |
96d45daa PS |
4096 | const int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, |
4097 | SS_ESTABLISHED, SS_CLOSING }; | |
aba5acdf | 4098 | |
9db7bf15 VK |
4099 | if (!filter_af_get(f, AF_UNIX)) |
4100 | return 0; | |
4101 | ||
f25062e9 PS |
4102 | if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT") |
4103 | && unix_show_netlink(f) == 0) | |
dfbaa90d PE |
4104 | return 0; |
4105 | ||
ab01dbbb | 4106 | if ((fp = net_unix_open()) == NULL) |
aba5acdf | 4107 | return -1; |
61170fd8 | 4108 | if (!fgets(buf, sizeof(buf), fp)) { |
d572ed4d PS |
4109 | fclose(fp); |
4110 | return -1; | |
4111 | } | |
aba5acdf | 4112 | |
ae665a52 | 4113 | if (memcmp(buf, "Peer", 4) == 0) |
aba5acdf SH |
4114 | newformat = 1; |
4115 | cnt = 0; | |
4116 | ||
61170fd8 | 4117 | while (fgets(buf, sizeof(buf), fp)) { |
ec4d0d8a | 4118 | struct sockstat *u, **insp; |
aba5acdf SH |
4119 | int flags; |
4120 | ||
0ee9052f | 4121 | if (!(u = calloc(1, sizeof(*u)))) |
aba5acdf | 4122 | break; |
aba5acdf SH |
4123 | |
4124 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", | |
ec4d0d8a | 4125 | &u->rport, &u->rq, &u->wq, &flags, &u->type, |
aba5acdf SH |
4126 | &u->state, &u->ino, name) < 8) |
4127 | name[0] = 0; | |
4128 | ||
ec4d0d8a VK |
4129 | u->lport = u->ino; |
4130 | u->local.family = u->remote.family = AF_UNIX; | |
4131 | ||
4132 | if (flags & (1 << 16)) { | |
aba5acdf | 4133 | u->state = SS_LISTEN; |
0aa03350 PS |
4134 | } else if (u->state > 0 && |
4135 | u->state <= ARRAY_SIZE(unix_state_map)) { | |
aba5acdf | 4136 | u->state = unix_state_map[u->state-1]; |
ec4d0d8a | 4137 | if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) |
aba5acdf SH |
4138 | u->state = SS_ESTABLISHED; |
4139 | } | |
2d0e538f PS |
4140 | if (unix_type_skip(u, f) || |
4141 | !(f->states & (1 << u->state))) { | |
4142 | free(u); | |
4143 | continue; | |
4144 | } | |
aba5acdf SH |
4145 | |
4146 | if (!newformat) { | |
ec4d0d8a | 4147 | u->rport = 0; |
aba5acdf SH |
4148 | u->rq = 0; |
4149 | u->wq = 0; | |
4150 | } | |
4151 | ||
2d0e538f PS |
4152 | if (name[0]) { |
4153 | u->name = strdup(name); | |
46131577 PS |
4154 | if (!u->name) { |
4155 | free(u); | |
2d0e538f | 4156 | break; |
46131577 | 4157 | } |
2d0e538f PS |
4158 | } |
4159 | ||
4160 | if (u->rport) { | |
4161 | struct sockstat *p; | |
4162 | ||
4163 | for (p = list; p; p = p->next) { | |
4164 | if (u->rport == p->lport) | |
4165 | break; | |
4166 | } | |
4167 | if (!p) | |
4168 | u->peer_name = "?"; | |
4169 | else | |
4170 | u->peer_name = p->name ? : "*"; | |
4171 | } | |
4172 | ||
4173 | if (f->f) { | |
4174 | struct sockstat st = { | |
4175 | .local.family = AF_UNIX, | |
4176 | .remote.family = AF_UNIX, | |
4177 | }; | |
4178 | ||
4179 | memcpy(st.local.data, &u->name, sizeof(u->name)); | |
ebbb219c AQ |
4180 | /* when parsing the old format rport is set to 0 and |
4181 | * therefore peer_name remains NULL | |
4182 | */ | |
4183 | if (u->peer_name && strcmp(u->peer_name, "*")) | |
2d0e538f PS |
4184 | memcpy(st.remote.data, &u->peer_name, |
4185 | sizeof(u->peer_name)); | |
4186 | if (run_ssfilter(f->f, &st) == 0) { | |
4187 | free(u->name); | |
4188 | free(u); | |
4189 | continue; | |
4190 | } | |
4191 | } | |
4192 | ||
aba5acdf SH |
4193 | insp = &list; |
4194 | while (*insp) { | |
4195 | if (u->type < (*insp)->type || | |
4196 | (u->type == (*insp)->type && | |
4197 | u->ino < (*insp)->ino)) | |
4198 | break; | |
4199 | insp = &(*insp)->next; | |
4200 | } | |
4201 | u->next = *insp; | |
4202 | *insp = u; | |
4203 | ||
aba5acdf | 4204 | if (++cnt > MAX_UNIX_REMEMBER) { |
2d0e538f PS |
4205 | while (list) { |
4206 | unix_stats_print(list, f); | |
2d0e538f PS |
4207 | unix_list_drop_first(&list); |
4208 | } | |
aba5acdf SH |
4209 | cnt = 0; |
4210 | } | |
4211 | } | |
a3fd8e58 | 4212 | fclose(fp); |
2d0e538f | 4213 | while (list) { |
bf4ceee6 | 4214 | unix_stats_print(list, f); |
2d0e538f | 4215 | unix_list_drop_first(&list); |
aba5acdf SH |
4216 | } |
4217 | ||
4218 | return 0; | |
4219 | } | |
4220 | ||
89f634f9 | 4221 | static int packet_stats_print(struct sockstat *s, const struct filter *f) |
4a0053b6 | 4222 | { |
b217df10 VK |
4223 | const char *addr, *port; |
4224 | char ll_name[16]; | |
372c30d2 | 4225 | |
be7e4d20 PS |
4226 | s->local.family = s->remote.family = AF_PACKET; |
4227 | ||
4a0053b6 | 4228 | if (f->f) { |
89f634f9 | 4229 | s->local.data[0] = s->prot; |
89f634f9 | 4230 | if (run_ssfilter(f->f, s) == 0) |
4a0053b6 VK |
4231 | return 1; |
4232 | } | |
372c30d2 | 4233 | |
be7e4d20 | 4234 | sock_state_print(s); |
372c30d2 | 4235 | |
b217df10 VK |
4236 | if (s->prot == 3) |
4237 | addr = "*"; | |
4238 | else | |
4239 | addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); | |
4240 | ||
4241 | if (s->iface == 0) | |
4242 | port = "*"; | |
4243 | else | |
4244 | port = xll_index_to_name(s->iface); | |
372c30d2 | 4245 | |
b217df10 VK |
4246 | sock_addr_print(addr, ":", port, NULL); |
4247 | sock_addr_print("", "*", "", NULL); | |
116ac927 | 4248 | |
5f27ac1d | 4249 | proc_ctx_print(s); |
116ac927 | 4250 | |
f1b39e1b VK |
4251 | if (show_details) |
4252 | sock_details_print(s); | |
4253 | ||
4a0053b6 VK |
4254 | return 0; |
4255 | } | |
4256 | ||
2631b856 VK |
4257 | static void packet_show_ring(struct packet_diag_ring *ring) |
4258 | { | |
90351722 SB |
4259 | out("blk_size:%d", ring->pdr_block_size); |
4260 | out(",blk_nr:%d", ring->pdr_block_nr); | |
4261 | out(",frm_size:%d", ring->pdr_frame_size); | |
4262 | out(",frm_nr:%d", ring->pdr_frame_nr); | |
4263 | out(",tmo:%d", ring->pdr_retire_tmo); | |
4264 | out(",features:0x%x", ring->pdr_features); | |
2631b856 VK |
4265 | } |
4266 | ||
cd554f2c | 4267 | static int packet_show_sock(struct nlmsghdr *nlh, void *arg) |
4a0053b6 VK |
4268 | { |
4269 | const struct filter *f = arg; | |
4270 | struct packet_diag_msg *r = NLMSG_DATA(nlh); | |
2631b856 VK |
4271 | struct packet_diag_info *pinfo = NULL; |
4272 | struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL; | |
4a0053b6 | 4273 | struct rtattr *tb[PACKET_DIAG_MAX+1]; |
89f634f9 | 4274 | struct sockstat stat = {}; |
2631b856 VK |
4275 | uint32_t fanout = 0; |
4276 | bool has_fanout = false; | |
4a0053b6 | 4277 | |
acd1e437 | 4278 | parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1), |
4a0053b6 VK |
4279 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
4280 | ||
4281 | /* use /proc/net/packet if all info are not available */ | |
4282 | if (!tb[PACKET_DIAG_MEMINFO]) | |
4283 | return -1; | |
4284 | ||
2d791bc8 VK |
4285 | stat.type = r->pdiag_type; |
4286 | stat.prot = r->pdiag_num; | |
4287 | stat.ino = r->pdiag_ino; | |
4288 | stat.state = SS_CLOSE; | |
f1b39e1b | 4289 | stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); |
4a0053b6 VK |
4290 | |
4291 | if (tb[PACKET_DIAG_MEMINFO]) { | |
4292 | __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); | |
acd1e437 | 4293 | |
4a0053b6 VK |
4294 | stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
4295 | } | |
4296 | ||
4297 | if (tb[PACKET_DIAG_INFO]) { | |
2631b856 | 4298 | pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); |
2d791bc8 | 4299 | stat.lport = stat.iface = pinfo->pdi_index; |
4a0053b6 VK |
4300 | } |
4301 | ||
f1b39e1b | 4302 | if (tb[PACKET_DIAG_UID]) |
9f1370c0 | 4303 | stat.uid = rta_getattr_u32(tb[PACKET_DIAG_UID]); |
f1b39e1b | 4304 | |
2631b856 VK |
4305 | if (tb[PACKET_DIAG_RX_RING]) |
4306 | ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]); | |
4307 | ||
4308 | if (tb[PACKET_DIAG_TX_RING]) | |
4309 | ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]); | |
4310 | ||
4311 | if (tb[PACKET_DIAG_FANOUT]) { | |
4312 | has_fanout = true; | |
9f1370c0 | 4313 | fanout = rta_getattr_u32(tb[PACKET_DIAG_FANOUT]); |
2631b856 VK |
4314 | } |
4315 | ||
4a0053b6 VK |
4316 | if (packet_stats_print(&stat, f)) |
4317 | return 0; | |
4318 | ||
2631b856 VK |
4319 | if (show_details) { |
4320 | if (pinfo) { | |
296b5de7 JH |
4321 | if (oneline) |
4322 | out(" ver:%d", pinfo->pdi_version); | |
4323 | else | |
4324 | out("\n\tver:%d", pinfo->pdi_version); | |
90351722 SB |
4325 | out(" cpy_thresh:%d", pinfo->pdi_copy_thresh); |
4326 | out(" flags( "); | |
2631b856 | 4327 | if (pinfo->pdi_flags & PDI_RUNNING) |
90351722 | 4328 | out("running"); |
2631b856 | 4329 | if (pinfo->pdi_flags & PDI_AUXDATA) |
90351722 | 4330 | out(" auxdata"); |
2631b856 | 4331 | if (pinfo->pdi_flags & PDI_ORIGDEV) |
90351722 | 4332 | out(" origdev"); |
2631b856 | 4333 | if (pinfo->pdi_flags & PDI_VNETHDR) |
90351722 | 4334 | out(" vnethdr"); |
2631b856 | 4335 | if (pinfo->pdi_flags & PDI_LOSS) |
90351722 | 4336 | out(" loss"); |
2631b856 | 4337 | if (!pinfo->pdi_flags) |
90351722 SB |
4338 | out("0"); |
4339 | out(" )"); | |
2631b856 VK |
4340 | } |
4341 | if (ring_rx) { | |
296b5de7 JH |
4342 | if (oneline) |
4343 | out(" ring_rx("); | |
4344 | else | |
4345 | out("\n\tring_rx("); | |
2631b856 | 4346 | packet_show_ring(ring_rx); |
90351722 | 4347 | out(")"); |
2631b856 VK |
4348 | } |
4349 | if (ring_tx) { | |
296b5de7 JH |
4350 | if (oneline) |
4351 | out(" ring_tx("); | |
4352 | else | |
4353 | out("\n\tring_tx("); | |
2631b856 | 4354 | packet_show_ring(ring_tx); |
90351722 | 4355 | out(")"); |
2631b856 VK |
4356 | } |
4357 | if (has_fanout) { | |
4358 | uint16_t type = (fanout >> 16) & 0xffff; | |
4359 | ||
296b5de7 JH |
4360 | if (oneline) |
4361 | out(" fanout("); | |
4362 | else | |
4363 | out("\n\tfanout("); | |
90351722 SB |
4364 | out("id:%d,", fanout & 0xffff); |
4365 | out("type:"); | |
2631b856 VK |
4366 | |
4367 | if (type == 0) | |
90351722 | 4368 | out("hash"); |
2631b856 | 4369 | else if (type == 1) |
90351722 | 4370 | out("lb"); |
2631b856 | 4371 | else if (type == 2) |
90351722 | 4372 | out("cpu"); |
2631b856 | 4373 | else if (type == 3) |
90351722 | 4374 | out("roll"); |
2631b856 | 4375 | else if (type == 4) |
90351722 | 4376 | out("random"); |
2631b856 | 4377 | else if (type == 5) |
90351722 | 4378 | out("qm"); |
2631b856 | 4379 | else |
90351722 | 4380 | out("0x%x", type); |
2631b856 | 4381 | |
90351722 | 4382 | out(")"); |
2631b856 VK |
4383 | } |
4384 | } | |
4385 | ||
372c30d2 ND |
4386 | if (show_bpf && tb[PACKET_DIAG_FILTER]) { |
4387 | struct sock_filter *fil = | |
4388 | RTA_DATA(tb[PACKET_DIAG_FILTER]); | |
4389 | int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / | |
4390 | sizeof(struct sock_filter); | |
4391 | ||
296b5de7 JH |
4392 | if (oneline) |
4393 | out(" bpf filter (%d): ", num); | |
4394 | else | |
4395 | out("\n\tbpf filter (%d): ", num); | |
372c30d2 | 4396 | while (num) { |
90351722 SB |
4397 | out(" 0x%02x %u %u %u,", |
4398 | fil->code, fil->jt, fil->jf, fil->k); | |
372c30d2 ND |
4399 | num--; |
4400 | fil++; | |
4401 | } | |
4402 | } | |
430e05d3 RP |
4403 | |
4404 | if (show_mem) | |
4405 | print_skmeminfo(tb, PACKET_DIAG_MEMINFO); | |
372c30d2 ND |
4406 | return 0; |
4407 | } | |
4408 | ||
8a4025f6 | 4409 | static int packet_show_netlink(struct filter *f) |
372c30d2 | 4410 | { |
5fb421d4 | 4411 | DIAG_REQUEST(req, struct packet_diag_req r); |
372c30d2 | 4412 | |
372c30d2 | 4413 | req.r.sdiag_family = AF_PACKET; |
2631b856 VK |
4414 | req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | |
4415 | PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT; | |
372c30d2 | 4416 | |
8a4025f6 | 4417 | return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock); |
372c30d2 ND |
4418 | } |
4419 | ||
4a0053b6 VK |
4420 | static int packet_show_line(char *buf, const struct filter *f, int fam) |
4421 | { | |
4422 | unsigned long long sk; | |
89f634f9 | 4423 | struct sockstat stat = {}; |
4a0053b6 VK |
4424 | int type, prot, iface, state, rq, uid, ino; |
4425 | ||
4426 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", | |
4427 | &sk, | |
4428 | &type, &prot, &iface, &state, | |
4429 | &rq, &uid, &ino); | |
4430 | ||
4431 | if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) | |
4432 | return 0; | |
4433 | if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) | |
4434 | return 0; | |
4435 | ||
4436 | stat.type = type; | |
4437 | stat.prot = prot; | |
2d791bc8 | 4438 | stat.lport = stat.iface = iface; |
4a0053b6 VK |
4439 | stat.state = state; |
4440 | stat.rq = rq; | |
4441 | stat.uid = uid; | |
4442 | stat.ino = ino; | |
2d791bc8 VK |
4443 | stat.state = SS_CLOSE; |
4444 | ||
4a0053b6 VK |
4445 | if (packet_stats_print(&stat, f)) |
4446 | return 0; | |
4447 | ||
4a0053b6 VK |
4448 | return 0; |
4449 | } | |
aba5acdf | 4450 | |
d1f28cf1 | 4451 | static int packet_show(struct filter *f) |
aba5acdf SH |
4452 | { |
4453 | FILE *fp; | |
b95d28c3 | 4454 | int rc = 0; |
3d0b7439 | 4455 | |
9db7bf15 | 4456 | if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE))) |
b9ea445d | 4457 | return 0; |
aba5acdf | 4458 | |
4a0053b6 VK |
4459 | if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") && |
4460 | packet_show_netlink(f) == 0) | |
372c30d2 ND |
4461 | return 0; |
4462 | ||
ab01dbbb | 4463 | if ((fp = net_packet_open()) == NULL) |
aba5acdf | 4464 | return -1; |
4a0053b6 | 4465 | if (generic_record_read(fp, packet_show_line, f, AF_PACKET)) |
b95d28c3 | 4466 | rc = -1; |
aba5acdf | 4467 | |
b95d28c3 PS |
4468 | fclose(fp); |
4469 | return rc; | |
aba5acdf SH |
4470 | } |
4471 | ||
2abc3d76 BT |
4472 | static int xdp_stats_print(struct sockstat *s, const struct filter *f) |
4473 | { | |
4474 | const char *addr, *port; | |
4475 | char q_str[16]; | |
4476 | ||
4477 | s->local.family = s->remote.family = AF_XDP; | |
4478 | ||
4479 | if (f->f) { | |
4480 | if (run_ssfilter(f->f, s) == 0) | |
4481 | return 1; | |
4482 | } | |
4483 | ||
4484 | sock_state_print(s); | |
4485 | ||
4486 | if (s->iface) { | |
4487 | addr = xll_index_to_name(s->iface); | |
4488 | snprintf(q_str, sizeof(q_str), "q%d", s->lport); | |
4489 | port = q_str; | |
4490 | sock_addr_print(addr, ":", port, NULL); | |
4491 | } else { | |
4492 | sock_addr_print("", "*", "", NULL); | |
4493 | } | |
4494 | ||
4495 | sock_addr_print("", "*", "", NULL); | |
4496 | ||
4497 | proc_ctx_print(s); | |
4498 | ||
4499 | if (show_details) | |
4500 | sock_details_print(s); | |
4501 | ||
4502 | return 0; | |
4503 | } | |
4504 | ||
4505 | static void xdp_show_ring(const char *name, struct xdp_diag_ring *ring) | |
4506 | { | |
296b5de7 JH |
4507 | if (oneline) |
4508 | out(" %s(", name); | |
4509 | else | |
4510 | out("\n\t%s(", name); | |
2abc3d76 BT |
4511 | out("entries:%u", ring->entries); |
4512 | out(")"); | |
4513 | } | |
4514 | ||
4515 | static void xdp_show_umem(struct xdp_diag_umem *umem, struct xdp_diag_ring *fr, | |
4516 | struct xdp_diag_ring *cr) | |
4517 | { | |
296b5de7 JH |
4518 | if (oneline) |
4519 | out(" tumem("); | |
4520 | else | |
4521 | out("\n\tumem("); | |
2abc3d76 BT |
4522 | out("id:%u", umem->id); |
4523 | out(",size:%llu", umem->size); | |
4524 | out(",num_pages:%u", umem->num_pages); | |
4525 | out(",chunk_size:%u", umem->chunk_size); | |
4526 | out(",headroom:%u", umem->headroom); | |
4527 | out(",ifindex:%u", umem->ifindex); | |
4528 | out(",qid:%u", umem->queue_id); | |
4529 | out(",zc:%u", umem->flags & XDP_DU_F_ZEROCOPY); | |
4530 | out(",refs:%u", umem->refs); | |
4531 | out(")"); | |
4532 | ||
4533 | if (fr) | |
4534 | xdp_show_ring("fr", fr); | |
4535 | if (cr) | |
4536 | xdp_show_ring("cr", cr); | |
4537 | } | |
4538 | ||
d2be31d9 CL |
4539 | static void xdp_show_stats(struct xdp_diag_stats *stats) |
4540 | { | |
4541 | if (oneline) | |
4542 | out(" stats("); | |
4543 | else | |
4544 | out("\n\tstats("); | |
4545 | out("rx dropped:%llu", stats->n_rx_dropped); | |
4546 | out(",rx invalid:%llu", stats->n_rx_invalid); | |
4547 | out(",rx queue full:%llu", stats->n_rx_full); | |
4548 | out(",rx fill ring empty:%llu", stats->n_fill_ring_empty); | |
4549 | out(",tx invalid:%llu", stats->n_tx_invalid); | |
4550 | out(",tx ring empty:%llu", stats->n_tx_ring_empty); | |
4551 | out(")"); | |
4552 | } | |
4553 | ||
2abc3d76 BT |
4554 | static int xdp_show_sock(struct nlmsghdr *nlh, void *arg) |
4555 | { | |
4556 | struct xdp_diag_ring *rx = NULL, *tx = NULL, *fr = NULL, *cr = NULL; | |
4557 | struct xdp_diag_msg *msg = NLMSG_DATA(nlh); | |
4558 | struct rtattr *tb[XDP_DIAG_MAX + 1]; | |
4559 | struct xdp_diag_info *info = NULL; | |
4560 | struct xdp_diag_umem *umem = NULL; | |
d2be31d9 | 4561 | struct xdp_diag_stats *stats = NULL; |
2abc3d76 BT |
4562 | const struct filter *f = arg; |
4563 | struct sockstat stat = {}; | |
4564 | ||
4565 | parse_rtattr(tb, XDP_DIAG_MAX, (struct rtattr *)(msg + 1), | |
4566 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*msg))); | |
4567 | ||
4568 | stat.type = msg->xdiag_type; | |
4569 | stat.ino = msg->xdiag_ino; | |
4570 | stat.state = SS_CLOSE; | |
4571 | stat.sk = cookie_sk_get(&msg->xdiag_cookie[0]); | |
4572 | ||
4573 | if (tb[XDP_DIAG_INFO]) { | |
4574 | info = RTA_DATA(tb[XDP_DIAG_INFO]); | |
4575 | stat.iface = info->ifindex; | |
4576 | stat.lport = info->queue_id; | |
4577 | } | |
4578 | ||
4579 | if (tb[XDP_DIAG_UID]) | |
4580 | stat.uid = rta_getattr_u32(tb[XDP_DIAG_UID]); | |
4581 | if (tb[XDP_DIAG_RX_RING]) | |
4582 | rx = RTA_DATA(tb[XDP_DIAG_RX_RING]); | |
4583 | if (tb[XDP_DIAG_TX_RING]) | |
4584 | tx = RTA_DATA(tb[XDP_DIAG_TX_RING]); | |
4585 | if (tb[XDP_DIAG_UMEM]) | |
4586 | umem = RTA_DATA(tb[XDP_DIAG_UMEM]); | |
4587 | if (tb[XDP_DIAG_UMEM_FILL_RING]) | |
4588 | fr = RTA_DATA(tb[XDP_DIAG_UMEM_FILL_RING]); | |
4589 | if (tb[XDP_DIAG_UMEM_COMPLETION_RING]) | |
4590 | cr = RTA_DATA(tb[XDP_DIAG_UMEM_COMPLETION_RING]); | |
4591 | if (tb[XDP_DIAG_MEMINFO]) { | |
4592 | __u32 *skmeminfo = RTA_DATA(tb[XDP_DIAG_MEMINFO]); | |
4593 | ||
4594 | stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
4595 | } | |
d2be31d9 CL |
4596 | if (tb[XDP_DIAG_STATS]) |
4597 | stats = RTA_DATA(tb[XDP_DIAG_STATS]); | |
2abc3d76 BT |
4598 | |
4599 | if (xdp_stats_print(&stat, f)) | |
4600 | return 0; | |
4601 | ||
4602 | if (show_details) { | |
4603 | if (rx) | |
4604 | xdp_show_ring("rx", rx); | |
4605 | if (tx) | |
4606 | xdp_show_ring("tx", tx); | |
4607 | if (umem) | |
4608 | xdp_show_umem(umem, fr, cr); | |
d2be31d9 CL |
4609 | if (stats) |
4610 | xdp_show_stats(stats); | |
2abc3d76 BT |
4611 | } |
4612 | ||
4613 | if (show_mem) | |
4614 | print_skmeminfo(tb, XDP_DIAG_MEMINFO); // really? | |
4615 | ||
4616 | ||
4617 | return 0; | |
4618 | } | |
4619 | ||
4620 | static int xdp_show(struct filter *f) | |
4621 | { | |
4622 | DIAG_REQUEST(req, struct xdp_diag_req r); | |
4623 | ||
4624 | if (!filter_af_get(f, AF_XDP) || !(f->states & (1 << SS_CLOSE))) | |
4625 | return 0; | |
4626 | ||
4627 | req.r.sdiag_family = AF_XDP; | |
4628 | req.r.xdiag_show = XDP_SHOW_INFO | XDP_SHOW_RING_CFG | XDP_SHOW_UMEM | | |
d2be31d9 | 4629 | XDP_SHOW_MEMINFO | XDP_SHOW_STATS; |
2abc3d76 BT |
4630 | |
4631 | return handle_netlink_request(f, &req.nlh, sizeof(req), xdp_show_sock); | |
4632 | } | |
4633 | ||
5f24ec0e | 4634 | static int netlink_show_one(struct filter *f, |
acd1e437 SH |
4635 | int prot, int pid, unsigned int groups, |
4636 | int state, int dst_pid, unsigned int dst_group, | |
129709ae AV |
4637 | int rq, int wq, |
4638 | unsigned long long sk, unsigned long long cb) | |
4639 | { | |
301826be PS |
4640 | struct sockstat st = { |
4641 | .state = SS_CLOSE, | |
4642 | .rq = rq, | |
4643 | .wq = wq, | |
4644 | .local.family = AF_NETLINK, | |
4645 | .remote.family = AF_NETLINK, | |
4646 | }; | |
acd1e437 | 4647 | |
b217df10 VK |
4648 | SPRINT_BUF(prot_buf) = {}; |
4649 | const char *prot_name; | |
4650 | char procname[64] = {}; | |
eef43b50 | 4651 | |
129709ae | 4652 | if (f->f) { |
055840f2 VK |
4653 | st.rport = -1; |
4654 | st.lport = pid; | |
4655 | st.local.data[0] = prot; | |
055840f2 | 4656 | if (run_ssfilter(f->f, &st) == 0) |
5f24ec0e | 4657 | return 1; |
129709ae AV |
4658 | } |
4659 | ||
be7e4d20 | 4660 | sock_state_print(&st); |
eef43b50 | 4661 | |
ca697cee | 4662 | prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); |
eef43b50 | 4663 | |
129709ae | 4664 | if (pid == -1) { |
b217df10 | 4665 | procname[0] = '*'; |
ca697cee | 4666 | } else if (!numeric) { |
129709ae | 4667 | int done = 0; |
acd1e437 | 4668 | |
129709ae AV |
4669 | if (!pid) { |
4670 | done = 1; | |
da8034a0 | 4671 | strncpy(procname, "kernel", 7); |
129709ae | 4672 | } else if (pid > 0) { |
129709ae | 4673 | FILE *fp; |
acd1e437 | 4674 | |
0ee9052f | 4675 | snprintf(procname, sizeof(procname), "%s/%d/stat", |
129709ae AV |
4676 | getenv("PROC_ROOT") ? : "/proc", pid); |
4677 | if ((fp = fopen(procname, "r")) != NULL) { | |
4678 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { | |
0ee9052f | 4679 | snprintf(procname+strlen(procname), |
4680 | sizeof(procname)-strlen(procname), | |
4681 | "/%d", pid); | |
129709ae AV |
4682 | done = 1; |
4683 | } | |
4684 | fclose(fp); | |
4685 | } | |
4686 | } | |
4687 | if (!done) | |
b217df10 | 4688 | int_to_str(pid, procname); |
129709ae | 4689 | } else { |
b217df10 | 4690 | int_to_str(pid, procname); |
129709ae | 4691 | } |
f271fe01 | 4692 | |
b217df10 VK |
4693 | sock_addr_print(prot_name, ":", procname, NULL); |
4694 | ||
f271fe01 | 4695 | if (state == NETLINK_CONNECTED) { |
b217df10 VK |
4696 | char dst_group_buf[30]; |
4697 | char dst_pid_buf[30]; | |
acd1e437 | 4698 | |
b217df10 VK |
4699 | sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", |
4700 | int_to_str(dst_pid, dst_pid_buf), NULL); | |
f271fe01 | 4701 | } else { |
b217df10 | 4702 | sock_addr_print("", "*", "", NULL); |
f271fe01 | 4703 | } |
129709ae | 4704 | |
116ac927 | 4705 | char *pid_context = NULL; |
acd1e437 | 4706 | |
116ac927 RH |
4707 | if (show_proc_ctx) { |
4708 | /* The pid value will either be: | |
4709 | * 0 if destination kernel - show kernel initial context. | |
4710 | * A valid process pid - use getpidcon. | |
4711 | * A unique value allocated by the kernel or netlink user | |
4712 | * to the process - show context as "not available". | |
4713 | */ | |
4714 | if (!pid) | |
4715 | security_get_initial_context("kernel", &pid_context); | |
4716 | else if (pid > 0) | |
4717 | getpidcon(pid, &pid_context); | |
4718 | ||
90351722 | 4719 | out(" proc_ctx=%s", pid_context ? : "unavailable"); |
22658ff5 | 4720 | free(pid_context); |
116ac927 RH |
4721 | } |
4722 | ||
129709ae | 4723 | if (show_details) { |
90351722 | 4724 | out(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); |
129709ae | 4725 | } |
129709ae | 4726 | |
5f24ec0e | 4727 | return 0; |
129709ae AV |
4728 | } |
4729 | ||
cd554f2c | 4730 | static int netlink_show_sock(struct nlmsghdr *nlh, void *arg) |
ecb928c8 | 4731 | { |
8a4025f6 | 4732 | struct filter *f = (struct filter *)arg; |
ecb928c8 AV |
4733 | struct netlink_diag_msg *r = NLMSG_DATA(nlh); |
4734 | struct rtattr *tb[NETLINK_DIAG_MAX+1]; | |
4735 | int rq = 0, wq = 0; | |
4736 | unsigned long groups = 0; | |
4737 | ||
acd1e437 | 4738 | parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1), |
ecb928c8 AV |
4739 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
4740 | ||
4741 | if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS])) | |
4742 | groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]); | |
4743 | ||
4744 | if (tb[NETLINK_DIAG_MEMINFO]) { | |
4745 | const __u32 *skmeminfo; | |
acd1e437 | 4746 | |
ecb928c8 AV |
4747 | skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]); |
4748 | ||
4749 | rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
4750 | wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC]; | |
4751 | } | |
4752 | ||
5f24ec0e | 4753 | if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups, |
ecb928c8 | 4754 | r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group, |
5f24ec0e VK |
4755 | rq, wq, 0, 0)) { |
4756 | return 0; | |
4757 | } | |
ecb928c8 AV |
4758 | |
4759 | if (show_mem) { | |
90351722 | 4760 | out("\t"); |
ecb928c8 | 4761 | print_skmeminfo(tb, NETLINK_DIAG_MEMINFO); |
ecb928c8 AV |
4762 | } |
4763 | ||
4764 | return 0; | |
4765 | } | |
4766 | ||
8a4025f6 | 4767 | static int netlink_show_netlink(struct filter *f) |
ecb928c8 | 4768 | { |
5fb421d4 | 4769 | DIAG_REQUEST(req, struct netlink_diag_req r); |
ecb928c8 AV |
4770 | |
4771 | req.r.sdiag_family = AF_NETLINK; | |
4772 | req.r.sdiag_protocol = NDIAG_PROTO_ALL; | |
4773 | req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO; | |
4774 | ||
8a4025f6 | 4775 | return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock); |
ecb928c8 AV |
4776 | } |
4777 | ||
d1f28cf1 | 4778 | static int netlink_show(struct filter *f) |
aba5acdf SH |
4779 | { |
4780 | FILE *fp; | |
4781 | char buf[256]; | |
4782 | int prot, pid; | |
acd1e437 | 4783 | unsigned int groups; |
aba5acdf SH |
4784 | int rq, wq, rc; |
4785 | unsigned long long sk, cb; | |
4786 | ||
9db7bf15 | 4787 | if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE))) |
b9ea445d VK |
4788 | return 0; |
4789 | ||
129709ae | 4790 | if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") && |
8a4025f6 | 4791 | netlink_show_netlink(f) == 0) |
129709ae AV |
4792 | return 0; |
4793 | ||
ab01dbbb | 4794 | if ((fp = net_netlink_open()) == NULL) |
aba5acdf | 4795 | return -1; |
61170fd8 | 4796 | if (!fgets(buf, sizeof(buf), fp)) { |
d572ed4d PS |
4797 | fclose(fp); |
4798 | return -1; | |
4799 | } | |
aba5acdf | 4800 | |
61170fd8 | 4801 | while (fgets(buf, sizeof(buf), fp)) { |
aba5acdf SH |
4802 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", |
4803 | &sk, | |
4804 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); | |
4805 | ||
f271fe01 | 4806 | netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb); |
aba5acdf SH |
4807 | } |
4808 | ||
b95d28c3 | 4809 | fclose(fp); |
aba5acdf SH |
4810 | return 0; |
4811 | } | |
4812 | ||
c759116a SH |
4813 | static bool vsock_type_skip(struct sockstat *s, struct filter *f) |
4814 | { | |
4815 | if (s->type == SOCK_STREAM && !(f->dbs & (1 << VSOCK_ST_DB))) | |
4816 | return true; | |
4817 | if (s->type == SOCK_DGRAM && !(f->dbs & (1 << VSOCK_DG_DB))) | |
4818 | return true; | |
4819 | return false; | |
4820 | } | |
4821 | ||
4822 | static void vsock_addr_print(inet_prefix *a, __u32 port) | |
4823 | { | |
4824 | char cid_str[sizeof("4294967295")]; | |
4825 | char port_str[sizeof("4294967295")]; | |
4826 | __u32 cid; | |
4827 | ||
4828 | memcpy(&cid, a->data, sizeof(cid)); | |
4829 | ||
4830 | if (cid == ~(__u32)0) | |
4831 | snprintf(cid_str, sizeof(cid_str), "*"); | |
4832 | else | |
4833 | snprintf(cid_str, sizeof(cid_str), "%u", cid); | |
4834 | ||
4835 | if (port == ~(__u32)0) | |
4836 | snprintf(port_str, sizeof(port_str), "*"); | |
4837 | else | |
4838 | snprintf(port_str, sizeof(port_str), "%u", port); | |
4839 | ||
4840 | sock_addr_print(cid_str, ":", port_str, NULL); | |
4841 | } | |
4842 | ||
4843 | static void vsock_stats_print(struct sockstat *s, struct filter *f) | |
4844 | { | |
4845 | sock_state_print(s); | |
4846 | ||
4847 | vsock_addr_print(&s->local, s->lport); | |
4848 | vsock_addr_print(&s->remote, s->rport); | |
4849 | ||
4850 | proc_ctx_print(s); | |
c759116a SH |
4851 | } |
4852 | ||
cd554f2c | 4853 | static int vsock_show_sock(struct nlmsghdr *nlh, void *arg) |
c759116a SH |
4854 | { |
4855 | struct filter *f = (struct filter *)arg; | |
4856 | struct vsock_diag_msg *r = NLMSG_DATA(nlh); | |
4857 | struct sockstat stat = { | |
4858 | .type = r->vdiag_type, | |
4859 | .lport = r->vdiag_src_port, | |
4860 | .rport = r->vdiag_dst_port, | |
4861 | .state = r->vdiag_state, | |
4862 | .ino = r->vdiag_ino, | |
4863 | }; | |
4864 | ||
4865 | vsock_set_inet_prefix(&stat.local, r->vdiag_src_cid); | |
4866 | vsock_set_inet_prefix(&stat.remote, r->vdiag_dst_cid); | |
4867 | ||
4868 | if (vsock_type_skip(&stat, f)) | |
4869 | return 0; | |
4870 | ||
4871 | if (f->f && run_ssfilter(f->f, &stat) == 0) | |
4872 | return 0; | |
4873 | ||
4874 | vsock_stats_print(&stat, f); | |
4875 | ||
4876 | return 0; | |
4877 | } | |
4878 | ||
4879 | static int vsock_show(struct filter *f) | |
4880 | { | |
4881 | DIAG_REQUEST(req, struct vsock_diag_req r); | |
4882 | ||
4883 | if (!filter_af_get(f, AF_VSOCK)) | |
4884 | return 0; | |
4885 | ||
4886 | req.r.sdiag_family = AF_VSOCK; | |
4887 | req.r.vdiag_states = f->states; | |
4888 | ||
4889 | return handle_netlink_request(f, &req.nlh, sizeof(req), vsock_show_sock); | |
4890 | } | |
4891 | ||
5caf79a0 GM |
4892 | static void tipc_sock_addr_print(struct rtattr *net_addr, struct rtattr *id) |
4893 | { | |
4894 | uint32_t node = rta_getattr_u32(net_addr); | |
4895 | uint32_t identity = rta_getattr_u32(id); | |
4896 | ||
4897 | SPRINT_BUF(addr) = {}; | |
4898 | SPRINT_BUF(port) = {}; | |
4899 | ||
4900 | sprintf(addr, "%u", node); | |
4901 | sprintf(port, "%u", identity); | |
4902 | sock_addr_print(addr, ":", port, NULL); | |
4903 | ||
4904 | } | |
4905 | ||
cd554f2c | 4906 | static int tipc_show_sock(struct nlmsghdr *nlh, void *arg) |
5caf79a0 GM |
4907 | { |
4908 | struct rtattr *stat[TIPC_NLA_SOCK_STAT_MAX + 1] = {}; | |
4909 | struct rtattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {}; | |
4910 | struct rtattr *con[TIPC_NLA_CON_MAX + 1] = {}; | |
4911 | struct rtattr *info[TIPC_NLA_MAX + 1] = {}; | |
4912 | struct rtattr *msg_ref; | |
4913 | struct sockstat ss = {}; | |
4914 | ||
4915 | parse_rtattr(info, TIPC_NLA_MAX, NLMSG_DATA(nlh), | |
4916 | NLMSG_PAYLOAD(nlh, 0)); | |
4917 | ||
4918 | if (!info[TIPC_NLA_SOCK]) | |
4919 | return 0; | |
4920 | ||
4921 | msg_ref = info[TIPC_NLA_SOCK]; | |
4922 | parse_rtattr(attrs, TIPC_NLA_SOCK_MAX, RTA_DATA(msg_ref), | |
4923 | RTA_PAYLOAD(msg_ref)); | |
4924 | ||
4925 | msg_ref = attrs[TIPC_NLA_SOCK_STAT]; | |
4926 | parse_rtattr(stat, TIPC_NLA_SOCK_STAT_MAX, | |
4927 | RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); | |
4928 | ||
4929 | ||
4930 | ss.local.family = AF_TIPC; | |
4931 | ss.type = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TYPE]); | |
4932 | ss.state = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TIPC_STATE]); | |
4933 | ss.uid = rta_getattr_u32(attrs[TIPC_NLA_SOCK_UID]); | |
4934 | ss.ino = rta_getattr_u32(attrs[TIPC_NLA_SOCK_INO]); | |
4935 | ss.rq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_RCVQ]); | |
4936 | ss.wq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_SENDQ]); | |
4937 | ss.sk = rta_getattr_u64(attrs[TIPC_NLA_SOCK_COOKIE]); | |
4938 | ||
4939 | sock_state_print (&ss); | |
4940 | ||
4941 | tipc_sock_addr_print(attrs[TIPC_NLA_SOCK_ADDR], | |
4942 | attrs[TIPC_NLA_SOCK_REF]); | |
4943 | ||
4944 | msg_ref = attrs[TIPC_NLA_SOCK_CON]; | |
4945 | if (msg_ref) { | |
4946 | parse_rtattr(con, TIPC_NLA_CON_MAX, | |
4947 | RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); | |
4948 | ||
4949 | tipc_sock_addr_print(con[TIPC_NLA_CON_NODE], | |
4950 | con[TIPC_NLA_CON_SOCK]); | |
4951 | } else | |
4952 | sock_addr_print("", "-", "", NULL); | |
4953 | ||
4954 | if (show_details) | |
4955 | sock_details_print(&ss); | |
4956 | ||
4957 | proc_ctx_print(&ss); | |
4958 | ||
4959 | if (show_tipcinfo) { | |
296b5de7 JH |
4960 | if (oneline) |
4961 | out(" type:%s", stype_nameg[ss.type]); | |
4962 | else | |
4963 | out("\n type:%s", stype_nameg[ss.type]); | |
5caf79a0 GM |
4964 | out(" cong:%s ", |
4965 | stat[TIPC_NLA_SOCK_STAT_LINK_CONG] ? "link" : | |
4966 | stat[TIPC_NLA_SOCK_STAT_CONN_CONG] ? "conn" : "none"); | |
4967 | out(" drop:%d ", | |
4968 | rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_DROP])); | |
4969 | ||
4970 | if (attrs[TIPC_NLA_SOCK_HAS_PUBL]) | |
4971 | out(" publ"); | |
4972 | ||
4973 | if (con[TIPC_NLA_CON_FLAG]) | |
4974 | out(" via {%u,%u} ", | |
4975 | rta_getattr_u32(con[TIPC_NLA_CON_TYPE]), | |
4976 | rta_getattr_u32(con[TIPC_NLA_CON_INST])); | |
4977 | } | |
4978 | ||
4979 | return 0; | |
4980 | } | |
4981 | ||
4982 | static int tipc_show(struct filter *f) | |
4983 | { | |
4984 | DIAG_REQUEST(req, struct tipc_sock_diag_req r); | |
4985 | ||
4986 | memset(&req.r, 0, sizeof(req.r)); | |
4987 | req.r.sdiag_family = AF_TIPC; | |
4988 | req.r.tidiag_states = f->states; | |
4989 | ||
4990 | return handle_netlink_request(f, &req.nlh, sizeof(req), tipc_show_sock); | |
4991 | } | |
4992 | ||
6885e3bf CG |
4993 | struct sock_diag_msg { |
4994 | __u8 sdiag_family; | |
4995 | }; | |
4996 | ||
cd554f2c | 4997 | static int generic_show_sock(struct nlmsghdr *nlh, void *arg) |
6885e3bf CG |
4998 | { |
4999 | struct sock_diag_msg *r = NLMSG_DATA(nlh); | |
5000 | struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX }; | |
32ea3d54 | 5001 | int ret; |
6885e3bf CG |
5002 | |
5003 | switch (r->sdiag_family) { | |
5004 | case AF_INET: | |
5005 | case AF_INET6: | |
97352f1b | 5006 | inet_arg.rth = inet_arg.f->rth_for_killing; |
cd554f2c | 5007 | ret = show_one_inet_sock(nlh, &inet_arg); |
32ea3d54 | 5008 | break; |
6885e3bf | 5009 | case AF_UNIX: |
cd554f2c | 5010 | ret = unix_show_sock(nlh, arg); |
32ea3d54 | 5011 | break; |
6885e3bf | 5012 | case AF_PACKET: |
cd554f2c | 5013 | ret = packet_show_sock(nlh, arg); |
32ea3d54 | 5014 | break; |
6885e3bf | 5015 | case AF_NETLINK: |
cd554f2c | 5016 | ret = netlink_show_sock(nlh, arg); |
32ea3d54 | 5017 | break; |
c759116a | 5018 | case AF_VSOCK: |
cd554f2c | 5019 | ret = vsock_show_sock(nlh, arg); |
32ea3d54 | 5020 | break; |
2abc3d76 BT |
5021 | case AF_XDP: |
5022 | ret = xdp_show_sock(nlh, arg); | |
5023 | break; | |
6885e3bf | 5024 | default: |
32ea3d54 | 5025 | ret = -1; |
6885e3bf | 5026 | } |
32ea3d54 SB |
5027 | |
5028 | render(); | |
5029 | ||
5030 | return ret; | |
6885e3bf CG |
5031 | } |
5032 | ||
5033 | static int handle_follow_request(struct filter *f) | |
5034 | { | |
6b224dad | 5035 | int ret = 0; |
6885e3bf | 5036 | int groups = 0; |
97352f1b | 5037 | struct rtnl_handle rth, rth2; |
6885e3bf | 5038 | |
b338a3e7 | 5039 | if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << TCP_DB)) |
6885e3bf | 5040 | groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1); |
b338a3e7 | 5041 | if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << UDP_DB)) |
6885e3bf | 5042 | groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1); |
b338a3e7 | 5043 | if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << TCP_DB)) |
6885e3bf | 5044 | groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1); |
b338a3e7 | 5045 | if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << UDP_DB)) |
6885e3bf CG |
5046 | groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1); |
5047 | ||
5048 | if (groups == 0) | |
5049 | return -1; | |
5050 | ||
5051 | if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG)) | |
5052 | return -1; | |
5053 | ||
5054 | rth.dump = 0; | |
5055 | rth.local.nl_pid = 0; | |
5056 | ||
97352f1b MY |
5057 | if (f->kill) { |
5058 | if (rtnl_open_byproto(&rth2, groups, NETLINK_SOCK_DIAG)) { | |
5059 | rtnl_close(&rth); | |
5060 | return -1; | |
5061 | } | |
5062 | f->rth_for_killing = &rth2; | |
5063 | } | |
5064 | ||
6885e3bf | 5065 | if (rtnl_dump_filter(&rth, generic_show_sock, f)) |
6b224dad | 5066 | ret = -1; |
6885e3bf | 5067 | |
6885e3bf | 5068 | rtnl_close(&rth); |
97352f1b MY |
5069 | if (f->rth_for_killing) |
5070 | rtnl_close(f->rth_for_killing); | |
6885e3bf CG |
5071 | return ret; |
5072 | } | |
5073 | ||
d1f28cf1 | 5074 | static int get_snmp_int(char *proto, char *key, int *result) |
aba5acdf SH |
5075 | { |
5076 | char buf[1024]; | |
5077 | FILE *fp; | |
5078 | int protolen = strlen(proto); | |
5079 | int keylen = strlen(key); | |
5080 | ||
5081 | *result = 0; | |
5082 | ||
ab01dbbb | 5083 | if ((fp = net_snmp_open()) == NULL) |
aba5acdf SH |
5084 | return -1; |
5085 | ||
5086 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
5087 | char *p = buf; | |
5088 | int pos = 0; | |
acd1e437 | 5089 | |
aba5acdf SH |
5090 | if (memcmp(buf, proto, protolen)) |
5091 | continue; | |
5092 | while ((p = strchr(p, ' ')) != NULL) { | |
5093 | pos++; | |
5094 | p++; | |
5095 | if (memcmp(p, key, keylen) == 0 && | |
5096 | (p[keylen] == ' ' || p[keylen] == '\n')) | |
5097 | break; | |
5098 | } | |
5099 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
5100 | break; | |
5101 | if (memcmp(buf, proto, protolen)) | |
5102 | break; | |
5103 | p = buf; | |
5104 | while ((p = strchr(p, ' ')) != NULL) { | |
5105 | p++; | |
5106 | if (--pos == 0) { | |
5107 | sscanf(p, "%d", result); | |
5108 | fclose(fp); | |
5109 | return 0; | |
5110 | } | |
5111 | } | |
5112 | } | |
5113 | ||
5114 | fclose(fp); | |
5115 | errno = ESRCH; | |
5116 | return -1; | |
5117 | } | |
5118 | ||
5119 | ||
5120 | /* Get stats from sockstat */ | |
5121 | ||
acd1e437 | 5122 | struct ssummary { |
aba5acdf SH |
5123 | int socks; |
5124 | int tcp_mem; | |
5125 | int tcp_total; | |
5126 | int tcp_orphans; | |
5127 | int tcp_tws; | |
5128 | int tcp4_hashed; | |
5129 | int udp4; | |
5130 | int raw4; | |
5131 | int frag4; | |
5132 | int frag4_mem; | |
5133 | int tcp6_hashed; | |
5134 | int udp6; | |
5135 | int raw6; | |
5136 | int frag6; | |
5137 | int frag6_mem; | |
5138 | }; | |
5139 | ||
055840f2 | 5140 | static void get_sockstat_line(char *line, struct ssummary *s) |
aba5acdf SH |
5141 | { |
5142 | char id[256], rem[256]; | |
5143 | ||
5144 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) | |
5145 | return; | |
5146 | ||
5147 | if (strcmp(id, "sockets:") == 0) | |
5148 | sscanf(rem, "%*s%d", &s->socks); | |
5149 | else if (strcmp(id, "UDP:") == 0) | |
5150 | sscanf(rem, "%*s%d", &s->udp4); | |
5151 | else if (strcmp(id, "UDP6:") == 0) | |
5152 | sscanf(rem, "%*s%d", &s->udp6); | |
5153 | else if (strcmp(id, "RAW:") == 0) | |
5154 | sscanf(rem, "%*s%d", &s->raw4); | |
5155 | else if (strcmp(id, "RAW6:") == 0) | |
5156 | sscanf(rem, "%*s%d", &s->raw6); | |
5157 | else if (strcmp(id, "TCP6:") == 0) | |
5158 | sscanf(rem, "%*s%d", &s->tcp6_hashed); | |
5159 | else if (strcmp(id, "FRAG:") == 0) | |
5160 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); | |
5161 | else if (strcmp(id, "FRAG6:") == 0) | |
5162 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); | |
5163 | else if (strcmp(id, "TCP:") == 0) | |
5164 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", | |
5165 | &s->tcp4_hashed, | |
5166 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); | |
5167 | } | |
5168 | ||
055840f2 | 5169 | static int get_sockstat(struct ssummary *s) |
aba5acdf SH |
5170 | { |
5171 | char buf[256]; | |
5172 | FILE *fp; | |
5173 | ||
5174 | memset(s, 0, sizeof(*s)); | |
5175 | ||
ab01dbbb | 5176 | if ((fp = net_sockstat_open()) == NULL) |
aba5acdf | 5177 | return -1; |
acd1e437 | 5178 | while (fgets(buf, sizeof(buf), fp) != NULL) |
aba5acdf SH |
5179 | get_sockstat_line(buf, s); |
5180 | fclose(fp); | |
5181 | ||
ab01dbbb | 5182 | if ((fp = net_sockstat6_open()) == NULL) |
aba5acdf | 5183 | return 0; |
acd1e437 | 5184 | while (fgets(buf, sizeof(buf), fp) != NULL) |
aba5acdf SH |
5185 | get_sockstat_line(buf, s); |
5186 | fclose(fp); | |
5187 | ||
5188 | return 0; | |
5189 | } | |
5190 | ||
d1f28cf1 | 5191 | static int print_summary(void) |
aba5acdf | 5192 | { |
055840f2 | 5193 | struct ssummary s; |
2f938ce1 | 5194 | int tcp_estab; |
aba5acdf SH |
5195 | |
5196 | if (get_sockstat(&s) < 0) | |
5197 | perror("ss: get_sockstat"); | |
2f938ce1 | 5198 | if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab) < 0) |
aba5acdf SH |
5199 | perror("ss: get_snmpstat"); |
5200 | ||
10f68773 | 5201 | printf("Total: %d\n", s.socks); |
aba5acdf | 5202 | |
10f68773 SH |
5203 | printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n", |
5204 | s.tcp_total + s.tcp_tws, tcp_estab, | |
5205 | s.tcp_total - (s.tcp4_hashed + s.tcp6_hashed - s.tcp_tws), | |
5206 | s.tcp_orphans, s.tcp_tws); | |
aba5acdf SH |
5207 | |
5208 | printf("\n"); | |
5209 | printf("Transport Total IP IPv6\n"); | |
aba5acdf SH |
5210 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); |
5211 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); | |
5212 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); | |
ae665a52 | 5213 | printf("INET %-9d %-9d %-9d\n", |
aba5acdf SH |
5214 | s.raw4+s.udp4+s.tcp4_hashed+ |
5215 | s.raw6+s.udp6+s.tcp6_hashed, | |
5216 | s.raw4+s.udp4+s.tcp4_hashed, | |
5217 | s.raw6+s.udp6+s.tcp6_hashed); | |
5218 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); | |
5219 | ||
5220 | printf("\n"); | |
5221 | ||
5222 | return 0; | |
5223 | } | |
5224 | ||
7a96e199 | 5225 | static void _usage(FILE *dest) |
aba5acdf | 5226 | { |
7a96e199 | 5227 | fprintf(dest, |
aba5acdf SH |
5228 | "Usage: ss [ OPTIONS ]\n" |
5229 | " ss [ OPTIONS ] [ FILTER ]\n" | |
ff041f16 VK |
5230 | " -h, --help this message\n" |
5231 | " -V, --version output version information\n" | |
5232 | " -n, --numeric don't resolve service names\n" | |
ab61159a | 5233 | " -r, --resolve resolve host names\n" |
ff041f16 VK |
5234 | " -a, --all display all sockets\n" |
5235 | " -l, --listening display listening sockets\n" | |
ab61159a SH |
5236 | " -o, --options show timer information\n" |
5237 | " -e, --extended show detailed socket information\n" | |
5238 | " -m, --memory show socket memory usage\n" | |
ff041f16 VK |
5239 | " -p, --processes show process using socket\n" |
5240 | " -i, --info show internal TCP information\n" | |
5caf79a0 | 5241 | " --tipcinfo show internal tipc socket information\n" |
ff041f16 | 5242 | " -s, --summary show socket usage summary\n" |
0f3f0ca3 | 5243 | " --tos show tos and priority information\n" |
14f4bda5 | 5244 | " --cgroup show cgroup information\n" |
b0f01cf6 | 5245 | " -b, --bpf show bpf filter socket information\n" |
6885e3bf | 5246 | " -E, --events continually display sockets as they are destroyed\n" |
ff041f16 VK |
5247 | " -Z, --context display process SELinux security contexts\n" |
5248 | " -z, --contexts display process and socket SELinux security contexts\n" | |
95ce04bc | 5249 | " -N, --net switch to the specified network namespace name\n" |
ab61159a SH |
5250 | "\n" |
5251 | " -4, --ipv4 display only IP version 4 sockets\n" | |
5252 | " -6, --ipv6 display only IP version 6 sockets\n" | |
ff041f16 VK |
5253 | " -0, --packet display PACKET sockets\n" |
5254 | " -t, --tcp display only TCP sockets\n" | |
9c3be2c0 | 5255 | " -M, --mptcp display only MPTCP sockets\n" |
f89d46ad | 5256 | " -S, --sctp display only SCTP sockets\n" |
ff041f16 VK |
5257 | " -u, --udp display only UDP sockets\n" |
5258 | " -d, --dccp display only DCCP sockets\n" | |
5259 | " -w, --raw display only RAW sockets\n" | |
5260 | " -x, --unix display only Unix domain sockets\n" | |
5caf79a0 | 5261 | " --tipc display only TIPC sockets\n" |
c759116a | 5262 | " --vsock display only vsock sockets\n" |
ab61159a | 5263 | " -f, --family=FAMILY display sockets of type FAMILY\n" |
2abc3d76 | 5264 | " FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help}\n" |
ab61159a | 5265 | "\n" |
fb2594c1 | 5266 | " -K, --kill forcibly close sockets, display what was closed\n" |
7a4559f6 | 5267 | " -H, --no-header Suppress header line\n" |
296b5de7 | 5268 | " -O, --oneline socket's data printed on a single line\n" |
ad34d5fa | 5269 | " --inet-sockopt show various inet socket options\n" |
fb2594c1 | 5270 | "\n" |
583de149 | 5271 | " -A, --query=QUERY, --socket=QUERY\n" |
9c3be2c0 | 5272 | " QUERY := {all|inet|tcp|mptcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY]\n" |
ab61159a | 5273 | "\n" |
583de149 | 5274 | " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" |
ab61159a | 5275 | " -F, --filter=FILE read filter information from FILE\n" |
ff041f16 VK |
5276 | " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n" |
5277 | " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n" | |
ae4e21c9 | 5278 | " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n" |
ff041f16 VK |
5279 | " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" |
5280 | " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" | |
5281 | " bucket := {syn-recv|time-wait}\n" | |
ae4e21c9 | 5282 | " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n" |
ab61159a | 5283 | ); |
7a96e199 AH |
5284 | } |
5285 | ||
5286 | static void help(void) __attribute__((noreturn)); | |
5287 | static void help(void) | |
5288 | { | |
5289 | _usage(stdout); | |
5290 | exit(0); | |
5291 | } | |
5292 | ||
5293 | static void usage(void) __attribute__((noreturn)); | |
5294 | static void usage(void) | |
5295 | { | |
5296 | _usage(stderr); | |
aba5acdf SH |
5297 | exit(-1); |
5298 | } | |
5299 | ||
5300 | ||
d1f28cf1 | 5301 | static int scan_state(const char *state) |
aba5acdf | 5302 | { |
b710a722 PS |
5303 | static const char * const sstate_namel[] = { |
5304 | "UNKNOWN", | |
5305 | [SS_ESTABLISHED] = "established", | |
5306 | [SS_SYN_SENT] = "syn-sent", | |
5307 | [SS_SYN_RECV] = "syn-recv", | |
5308 | [SS_FIN_WAIT1] = "fin-wait-1", | |
5309 | [SS_FIN_WAIT2] = "fin-wait-2", | |
5310 | [SS_TIME_WAIT] = "time-wait", | |
5311 | [SS_CLOSE] = "unconnected", | |
5312 | [SS_CLOSE_WAIT] = "close-wait", | |
5313 | [SS_LAST_ACK] = "last-ack", | |
5314 | [SS_LISTEN] = "listening", | |
5315 | [SS_CLOSING] = "closing", | |
5316 | }; | |
aba5acdf | 5317 | int i; |
acd1e437 | 5318 | |
aba5acdf SH |
5319 | if (strcasecmp(state, "close") == 0 || |
5320 | strcasecmp(state, "closed") == 0) | |
5321 | return (1<<SS_CLOSE); | |
5322 | if (strcasecmp(state, "syn-rcv") == 0) | |
5323 | return (1<<SS_SYN_RECV); | |
1a5bad5a | 5324 | if (strcasecmp(state, "established") == 0) |
aba5acdf SH |
5325 | return (1<<SS_ESTABLISHED); |
5326 | if (strcasecmp(state, "all") == 0) | |
5327 | return SS_ALL; | |
5328 | if (strcasecmp(state, "connected") == 0) | |
5329 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); | |
1a5bad5a | 5330 | if (strcasecmp(state, "synchronized") == 0) |
aba5acdf SH |
5331 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
5332 | if (strcasecmp(state, "bucket") == 0) | |
5333 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); | |
5334 | if (strcasecmp(state, "big") == 0) | |
5335 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); | |
acd1e437 | 5336 | for (i = 0; i < SS_MAX; i++) { |
1a5bad5a | 5337 | if (strcasecmp(state, sstate_namel[i]) == 0) |
aba5acdf SH |
5338 | return (1<<i); |
5339 | } | |
9db7bf15 VK |
5340 | |
5341 | fprintf(stderr, "ss: wrong state name: %s\n", state); | |
5342 | exit(-1); | |
aba5acdf SH |
5343 | } |
5344 | ||
c759116a SH |
5345 | /* Values 'v' and 'V' are already used so a non-character is used */ |
5346 | #define OPT_VSOCK 256 | |
5347 | ||
5caf79a0 GM |
5348 | /* Values of 't' are already used so a non-character is used */ |
5349 | #define OPT_TIPCSOCK 257 | |
5350 | #define OPT_TIPCINFO 258 | |
5351 | ||
0f3f0ca3 KK |
5352 | #define OPT_TOS 259 |
5353 | ||
2abc3d76 | 5354 | /* Values of 'x' are already used so a non-character is used */ |
9f78e995 | 5355 | #define OPT_XDPSOCK 260 |
2abc3d76 | 5356 | |
14f4bda5 DY |
5357 | #define OPT_CGROUP 261 |
5358 | ||
ad34d5fa WW |
5359 | #define OPT_INET_SOCKOPT 262 |
5360 | ||
ab61159a SH |
5361 | static const struct option long_opts[] = { |
5362 | { "numeric", 0, 0, 'n' }, | |
5363 | { "resolve", 0, 0, 'r' }, | |
5364 | { "options", 0, 0, 'o' }, | |
5365 | { "extended", 0, 0, 'e' }, | |
5366 | { "memory", 0, 0, 'm' }, | |
5367 | { "info", 0, 0, 'i' }, | |
5368 | { "processes", 0, 0, 'p' }, | |
372c30d2 | 5369 | { "bpf", 0, 0, 'b' }, |
6885e3bf | 5370 | { "events", 0, 0, 'E' }, |
351efcde | 5371 | { "dccp", 0, 0, 'd' }, |
ab61159a | 5372 | { "tcp", 0, 0, 't' }, |
f89d46ad | 5373 | { "sctp", 0, 0, 'S' }, |
ab61159a SH |
5374 | { "udp", 0, 0, 'u' }, |
5375 | { "raw", 0, 0, 'w' }, | |
5376 | { "unix", 0, 0, 'x' }, | |
5caf79a0 | 5377 | { "tipc", 0, 0, OPT_TIPCSOCK}, |
c759116a | 5378 | { "vsock", 0, 0, OPT_VSOCK }, |
ab61159a SH |
5379 | { "all", 0, 0, 'a' }, |
5380 | { "listening", 0, 0, 'l' }, | |
5381 | { "ipv4", 0, 0, '4' }, | |
5382 | { "ipv6", 0, 0, '6' }, | |
5383 | { "packet", 0, 0, '0' }, | |
5384 | { "family", 1, 0, 'f' }, | |
5385 | { "socket", 1, 0, 'A' }, | |
583de149 | 5386 | { "query", 1, 0, 'A' }, |
c3f346b0 | 5387 | { "summary", 0, 0, 's' }, |
583de149 | 5388 | { "diag", 1, 0, 'D' }, |
ab61159a SH |
5389 | { "filter", 1, 0, 'F' }, |
5390 | { "version", 0, 0, 'V' }, | |
5391 | { "help", 0, 0, 'h' }, | |
116ac927 RH |
5392 | { "context", 0, 0, 'Z' }, |
5393 | { "contexts", 0, 0, 'z' }, | |
95ce04bc | 5394 | { "net", 1, 0, 'N' }, |
5caf79a0 | 5395 | { "tipcinfo", 0, 0, OPT_TIPCINFO}, |
0f3f0ca3 | 5396 | { "tos", 0, 0, OPT_TOS }, |
14f4bda5 | 5397 | { "cgroup", 0, 0, OPT_CGROUP }, |
fb2594c1 | 5398 | { "kill", 0, 0, 'K' }, |
7a4559f6 | 5399 | { "no-header", 0, 0, 'H' }, |
2abc3d76 | 5400 | { "xdp", 0, 0, OPT_XDPSOCK}, |
9c3be2c0 | 5401 | { "mptcp", 0, 0, 'M' }, |
296b5de7 | 5402 | { "oneline", 0, 0, 'O' }, |
ad34d5fa | 5403 | { "inet-sockopt", 0, 0, OPT_INET_SOCKOPT }, |
ab61159a | 5404 | { 0 } |
ae665a52 | 5405 | |
ab61159a SH |
5406 | }; |
5407 | ||
aba5acdf SH |
5408 | int main(int argc, char *argv[]) |
5409 | { | |
aba5acdf SH |
5410 | int saw_states = 0; |
5411 | int saw_query = 0; | |
5412 | int do_summary = 0; | |
7d105b56 | 5413 | const char *dump_tcpdiag = NULL; |
aba5acdf SH |
5414 | FILE *filter_fp = NULL; |
5415 | int ch; | |
9db7bf15 | 5416 | int state_filter = 0; |
aba5acdf | 5417 | |
168d97f9 | 5418 | while ((ch = getopt_long(argc, argv, |
9c3be2c0 | 5419 | "dhaletuwxnro460spbEf:mMiA:D:F:vVzZN:KHSO", |
ab61159a | 5420 | long_opts, NULL)) != EOF) { |
acd1e437 | 5421 | switch (ch) { |
aba5acdf | 5422 | case 'n': |
ca697cee | 5423 | numeric = 1; |
aba5acdf SH |
5424 | break; |
5425 | case 'r': | |
5426 | resolve_hosts = 1; | |
5427 | break; | |
5428 | case 'o': | |
5429 | show_options = 1; | |
5430 | break; | |
5431 | case 'e': | |
5432 | show_options = 1; | |
5433 | show_details++; | |
5434 | break; | |
5435 | case 'm': | |
5436 | show_mem = 1; | |
5437 | break; | |
5438 | case 'i': | |
5439 | show_tcpinfo = 1; | |
5440 | break; | |
5441 | case 'p': | |
5442 | show_users++; | |
fbc0f876 | 5443 | user_ent_hash_build(); |
aba5acdf | 5444 | break; |
372c30d2 ND |
5445 | case 'b': |
5446 | show_options = 1; | |
5447 | show_bpf++; | |
5448 | break; | |
6885e3bf CG |
5449 | case 'E': |
5450 | follow_events = 1; | |
5451 | break; | |
351efcde | 5452 | case 'd': |
c121111e | 5453 | filter_db_set(¤t_filter, DCCP_DB, true); |
351efcde | 5454 | break; |
aba5acdf | 5455 | case 't': |
c121111e | 5456 | filter_db_set(¤t_filter, TCP_DB, true); |
aba5acdf | 5457 | break; |
f89d46ad | 5458 | case 'S': |
c121111e | 5459 | filter_db_set(¤t_filter, SCTP_DB, true); |
f89d46ad | 5460 | break; |
aba5acdf | 5461 | case 'u': |
c121111e | 5462 | filter_db_set(¤t_filter, UDP_DB, true); |
aba5acdf SH |
5463 | break; |
5464 | case 'w': | |
c121111e | 5465 | filter_db_set(¤t_filter, RAW_DB, true); |
aba5acdf SH |
5466 | break; |
5467 | case 'x': | |
9db7bf15 | 5468 | filter_af_set(¤t_filter, AF_UNIX); |
aba5acdf | 5469 | break; |
c759116a SH |
5470 | case OPT_VSOCK: |
5471 | filter_af_set(¤t_filter, AF_VSOCK); | |
5472 | break; | |
5caf79a0 GM |
5473 | case OPT_TIPCSOCK: |
5474 | filter_af_set(¤t_filter, AF_TIPC); | |
5475 | break; | |
aba5acdf | 5476 | case 'a': |
9db7bf15 | 5477 | state_filter = SS_ALL; |
aba5acdf SH |
5478 | break; |
5479 | case 'l': | |
9db7bf15 | 5480 | state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE); |
aba5acdf SH |
5481 | break; |
5482 | case '4': | |
9db7bf15 | 5483 | filter_af_set(¤t_filter, AF_INET); |
aba5acdf SH |
5484 | break; |
5485 | case '6': | |
9db7bf15 | 5486 | filter_af_set(¤t_filter, AF_INET6); |
aba5acdf SH |
5487 | break; |
5488 | case '0': | |
9db7bf15 | 5489 | filter_af_set(¤t_filter, AF_PACKET); |
aba5acdf | 5490 | break; |
2abc3d76 BT |
5491 | case OPT_XDPSOCK: |
5492 | filter_af_set(¤t_filter, AF_XDP); | |
5493 | break; | |
9c3be2c0 PA |
5494 | case 'M': |
5495 | filter_db_set(¤t_filter, MPTCP_DB, true); | |
5496 | break; | |
aba5acdf SH |
5497 | case 'f': |
5498 | if (strcmp(optarg, "inet") == 0) | |
9db7bf15 | 5499 | filter_af_set(¤t_filter, AF_INET); |
aba5acdf | 5500 | else if (strcmp(optarg, "inet6") == 0) |
9db7bf15 | 5501 | filter_af_set(¤t_filter, AF_INET6); |
aba5acdf | 5502 | else if (strcmp(optarg, "link") == 0) |
9db7bf15 | 5503 | filter_af_set(¤t_filter, AF_PACKET); |
aba5acdf | 5504 | else if (strcmp(optarg, "unix") == 0) |
9db7bf15 | 5505 | filter_af_set(¤t_filter, AF_UNIX); |
aba5acdf | 5506 | else if (strcmp(optarg, "netlink") == 0) |
9db7bf15 | 5507 | filter_af_set(¤t_filter, AF_NETLINK); |
5caf79a0 GM |
5508 | else if (strcmp(optarg, "tipc") == 0) |
5509 | filter_af_set(¤t_filter, AF_TIPC); | |
c759116a SH |
5510 | else if (strcmp(optarg, "vsock") == 0) |
5511 | filter_af_set(¤t_filter, AF_VSOCK); | |
2abc3d76 BT |
5512 | else if (strcmp(optarg, "xdp") == 0) |
5513 | filter_af_set(¤t_filter, AF_XDP); | |
aba5acdf | 5514 | else if (strcmp(optarg, "help") == 0) |
7a96e199 | 5515 | help(); |
aba5acdf | 5516 | else { |
9db7bf15 VK |
5517 | fprintf(stderr, "ss: \"%s\" is invalid family\n", |
5518 | optarg); | |
aba5acdf SH |
5519 | usage(); |
5520 | } | |
5521 | break; | |
5522 | case 'A': | |
5523 | { | |
5524 | char *p, *p1; | |
acd1e437 | 5525 | |
aba5acdf SH |
5526 | if (!saw_query) { |
5527 | current_filter.dbs = 0; | |
7f9dddbe | 5528 | state_filter = state_filter ? |
acd1e437 | 5529 | state_filter : SS_CONN; |
aba5acdf SH |
5530 | saw_query = 1; |
5531 | do_default = 0; | |
5532 | } | |
5533 | p = p1 = optarg; | |
5534 | do { | |
5535 | if ((p1 = strchr(p, ',')) != NULL) | |
ae665a52 | 5536 | *p1 = 0; |
67d5fd55 | 5537 | if (filter_db_parse(¤t_filter, p)) { |
aba5acdf SH |
5538 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); |
5539 | usage(); | |
5540 | } | |
5541 | p = p1 + 1; | |
5542 | } while (p1); | |
5543 | break; | |
5544 | } | |
5545 | case 's': | |
5546 | do_summary = 1; | |
5547 | break; | |
5548 | case 'D': | |
5549 | dump_tcpdiag = optarg; | |
5550 | break; | |
5551 | case 'F': | |
5552 | if (filter_fp) { | |
5553 | fprintf(stderr, "More than one filter file\n"); | |
5554 | exit(-1); | |
5555 | } | |
5556 | if (optarg[0] == '-') | |
5557 | filter_fp = stdin; | |
5558 | else | |
5559 | filter_fp = fopen(optarg, "r"); | |
5560 | if (!filter_fp) { | |
5561 | perror("fopen filter file"); | |
5562 | exit(-1); | |
5563 | } | |
5564 | break; | |
5565 | case 'v': | |
5566 | case 'V': | |
fbef6555 | 5567 | printf("ss utility, iproute2-%s\n", version); |
aba5acdf | 5568 | exit(0); |
116ac927 RH |
5569 | case 'z': |
5570 | show_sock_ctx++; | |
36df1a6e | 5571 | /* fall through */ |
116ac927 RH |
5572 | case 'Z': |
5573 | if (is_selinux_enabled() <= 0) { | |
5574 | fprintf(stderr, "ss: SELinux is not enabled.\n"); | |
5575 | exit(1); | |
5576 | } | |
5577 | show_proc_ctx++; | |
5578 | user_ent_hash_build(); | |
5579 | break; | |
95ce04bc VK |
5580 | case 'N': |
5581 | if (netns_switch(optarg)) | |
5582 | exit(1); | |
5583 | break; | |
5caf79a0 GM |
5584 | case OPT_TIPCINFO: |
5585 | show_tipcinfo = 1; | |
5586 | break; | |
0f3f0ca3 KK |
5587 | case OPT_TOS: |
5588 | show_tos = 1; | |
5589 | break; | |
14f4bda5 DY |
5590 | case OPT_CGROUP: |
5591 | show_cgroup = 1; | |
5592 | break; | |
fb2594c1 LC |
5593 | case 'K': |
5594 | current_filter.kill = 1; | |
5595 | break; | |
7a4559f6 DA |
5596 | case 'H': |
5597 | show_header = 0; | |
5598 | break; | |
296b5de7 JH |
5599 | case 'O': |
5600 | oneline = 1; | |
5601 | break; | |
ad34d5fa WW |
5602 | case OPT_INET_SOCKOPT: |
5603 | show_inet_sockopt = 1; | |
5604 | break; | |
aba5acdf | 5605 | case 'h': |
7a96e199 | 5606 | help(); |
f73105ab | 5607 | case '?': |
aba5acdf SH |
5608 | default: |
5609 | usage(); | |
5610 | } | |
5611 | } | |
5612 | ||
5613 | argc -= optind; | |
5614 | argv += optind; | |
5615 | ||
aba5acdf SH |
5616 | if (do_summary) { |
5617 | print_summary(); | |
5618 | if (do_default && argc == 0) | |
5619 | exit(0); | |
5620 | } | |
5621 | ||
aba5acdf SH |
5622 | while (argc > 0) { |
5623 | if (strcmp(*argv, "state") == 0) { | |
5624 | NEXT_ARG(); | |
5625 | if (!saw_states) | |
9db7bf15 VK |
5626 | state_filter = 0; |
5627 | state_filter |= scan_state(*argv); | |
aba5acdf SH |
5628 | saw_states = 1; |
5629 | } else if (strcmp(*argv, "exclude") == 0 || | |
5630 | strcmp(*argv, "excl") == 0) { | |
5631 | NEXT_ARG(); | |
5632 | if (!saw_states) | |
9db7bf15 VK |
5633 | state_filter = SS_ALL; |
5634 | state_filter &= ~scan_state(*argv); | |
aba5acdf SH |
5635 | saw_states = 1; |
5636 | } else { | |
aba5acdf SH |
5637 | break; |
5638 | } | |
5639 | argc--; argv++; | |
5640 | } | |
5641 | ||
9db7bf15 VK |
5642 | if (do_default) { |
5643 | state_filter = state_filter ? state_filter : SS_CONN; | |
3e1652c9 | 5644 | filter_db_parse(¤t_filter, "all"); |
9db7bf15 VK |
5645 | } |
5646 | ||
57ff5a10 VK |
5647 | filter_states_set(¤t_filter, state_filter); |
5648 | filter_merge_defaults(¤t_filter); | |
5649 | ||
ca697cee | 5650 | if (!numeric && resolve_hosts && |
168d97f9 | 5651 | (current_filter.dbs & (UNIX_DBM|INET_L4_DBM))) |
9db7bf15 VK |
5652 | init_service_resolver(); |
5653 | ||
9db7bf15 VK |
5654 | if (current_filter.dbs == 0) { |
5655 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); | |
5656 | exit(0); | |
5657 | } | |
5658 | if (current_filter.families == 0) { | |
5659 | fprintf(stderr, "ss: no families to show with such filter.\n"); | |
5660 | exit(0); | |
5661 | } | |
aba5acdf SH |
5662 | if (current_filter.states == 0) { |
5663 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); | |
5664 | exit(0); | |
5665 | } | |
5666 | ||
5667 | if (dump_tcpdiag) { | |
5668 | FILE *dump_fp = stdout; | |
acd1e437 | 5669 | |
aba5acdf SH |
5670 | if (!(current_filter.dbs & (1<<TCP_DB))) { |
5671 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); | |
5672 | exit(0); | |
5673 | } | |
5674 | if (dump_tcpdiag[0] != '-') { | |
5675 | dump_fp = fopen(dump_tcpdiag, "w"); | |
5676 | if (!dump_tcpdiag) { | |
5677 | perror("fopen dump file"); | |
5678 | exit(-1); | |
5679 | } | |
5680 | } | |
3fe5b534 | 5681 | inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); |
aba5acdf SH |
5682 | fflush(dump_fp); |
5683 | exit(0); | |
5684 | } | |
5685 | ||
1527a17e VK |
5686 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) |
5687 | usage(); | |
5688 | ||
87b1a7ae SB |
5689 | if (!(current_filter.dbs & (current_filter.dbs - 1))) |
5690 | columns[COL_NETID].disabled = 1; | |
59f46b7b | 5691 | |
87b1a7ae SB |
5692 | if (!(current_filter.states & (current_filter.states - 1))) |
5693 | columns[COL_STATE].disabled = 1; | |
aba5acdf | 5694 | |
59f46b7b SB |
5695 | if (show_header) |
5696 | print_header(); | |
aba5acdf | 5697 | |
aba5acdf SH |
5698 | fflush(stdout); |
5699 | ||
6885e3bf CG |
5700 | if (follow_events) |
5701 | exit(handle_follow_request(¤t_filter)); | |
5702 | ||
aba5acdf SH |
5703 | if (current_filter.dbs & (1<<NETLINK_DB)) |
5704 | netlink_show(¤t_filter); | |
5705 | if (current_filter.dbs & PACKET_DBM) | |
5706 | packet_show(¤t_filter); | |
5707 | if (current_filter.dbs & UNIX_DBM) | |
5708 | unix_show(¤t_filter); | |
5709 | if (current_filter.dbs & (1<<RAW_DB)) | |
5710 | raw_show(¤t_filter); | |
5711 | if (current_filter.dbs & (1<<UDP_DB)) | |
5712 | udp_show(¤t_filter); | |
5713 | if (current_filter.dbs & (1<<TCP_DB)) | |
6c6bbc30 | 5714 | tcp_show(¤t_filter); |
351efcde | 5715 | if (current_filter.dbs & (1<<DCCP_DB)) |
6c6bbc30 | 5716 | dccp_show(¤t_filter); |
f89d46ad PS |
5717 | if (current_filter.dbs & (1<<SCTP_DB)) |
5718 | sctp_show(¤t_filter); | |
c759116a SH |
5719 | if (current_filter.dbs & VSOCK_DBM) |
5720 | vsock_show(¤t_filter); | |
5caf79a0 GM |
5721 | if (current_filter.dbs & (1<<TIPC_DB)) |
5722 | tipc_show(¤t_filter); | |
2abc3d76 BT |
5723 | if (current_filter.dbs & (1<<XDP_DB)) |
5724 | xdp_show(¤t_filter); | |
9c3be2c0 PA |
5725 | if (current_filter.dbs & (1<<MPTCP_DB)) |
5726 | mptcp_show(¤t_filter); | |
116ac927 RH |
5727 | |
5728 | if (show_users || show_proc_ctx || show_sock_ctx) | |
5729 | user_ent_destroy(); | |
5730 | ||
32ea3d54 | 5731 | render(); |
59f46b7b | 5732 | |
aba5acdf SH |
5733 | return 0; |
5734 | } |