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