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