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