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