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