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