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