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