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