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