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