]>
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> | |
15 | #include <syslog.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/ioctl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <sys/uio.h> | |
20 | #include <netinet/in.h> | |
21 | #include <string.h> | |
22 | #include <errno.h> | |
23 | #include <netdb.h> | |
24 | #include <arpa/inet.h> | |
aba5acdf SH |
25 | #include <dirent.h> |
26 | #include <fnmatch.h> | |
ab61159a | 27 | #include <getopt.h> |
bf4ceee6 | 28 | #include <stdbool.h> |
6b376ebd | 29 | #include <limits.h> |
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> |
aba5acdf | 46 | |
8a4025f6 | 47 | #define MAGIC_SEQ 123456 |
48 | ||
5fb421d4 | 49 | #define DIAG_REQUEST(_req, _r) \ |
50 | struct { \ | |
51 | struct nlmsghdr nlh; \ | |
52 | _r; \ | |
53 | } _req = { \ | |
54 | .nlh = { \ | |
55 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, \ | |
56 | .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\ | |
8a4025f6 | 57 | .nlmsg_seq = MAGIC_SEQ, \ |
5fb421d4 | 58 | .nlmsg_len = sizeof(_req), \ |
59 | }, \ | |
60 | } | |
61 | ||
116ac927 RH |
62 | #if HAVE_SELINUX |
63 | #include <selinux/selinux.h> | |
64 | #else | |
65 | /* Stubs for SELinux functions */ | |
66 | static int is_selinux_enabled(void) | |
67 | { | |
68 | return -1; | |
69 | } | |
70 | ||
71 | static int getpidcon(pid_t pid, char **context) | |
72 | { | |
73 | *context = NULL; | |
74 | return -1; | |
75 | } | |
76 | ||
77 | static int getfilecon(char *path, char **context) | |
78 | { | |
79 | *context = NULL; | |
80 | return -1; | |
81 | } | |
82 | ||
83 | static int security_get_initial_context(char *name, char **context) | |
84 | { | |
85 | *context = NULL; | |
86 | return -1; | |
87 | } | |
88 | #endif | |
89 | ||
acd1e437 | 90 | int resolve_hosts; |
aba5acdf SH |
91 | int resolve_services = 1; |
92 | int preferred_family = AF_UNSPEC; | |
acd1e437 SH |
93 | int show_options; |
94 | int show_details; | |
95 | int show_users; | |
96 | int show_mem; | |
97 | int show_tcpinfo; | |
98 | int show_bpf; | |
99 | int show_proc_ctx; | |
100 | int show_sock_ctx; | |
7a4559f6 | 101 | int show_header = 1; |
116ac927 | 102 | /* If show_users & show_proc_ctx only do user_ent_hash_build() once */ |
acd1e437 SH |
103 | int user_ent_hash_build_init; |
104 | int follow_events; | |
aba5acdf SH |
105 | |
106 | int netid_width; | |
107 | int state_width; | |
108 | int addrp_width; | |
109 | int addr_width; | |
110 | int serv_width; | |
111 | int screen_width; | |
112 | ||
113 | static const char *TCP_PROTO = "tcp"; | |
114 | static const char *UDP_PROTO = "udp"; | |
115 | static const char *RAW_PROTO = "raw"; | |
acd1e437 | 116 | static const char *dg_proto; |
aba5acdf | 117 | |
acd1e437 | 118 | enum { |
aba5acdf | 119 | TCP_DB, |
351efcde | 120 | DCCP_DB, |
aba5acdf SH |
121 | UDP_DB, |
122 | RAW_DB, | |
123 | UNIX_DG_DB, | |
124 | UNIX_ST_DB, | |
30b669d7 | 125 | UNIX_SQ_DB, |
aba5acdf SH |
126 | PACKET_DG_DB, |
127 | PACKET_R_DB, | |
128 | NETLINK_DB, | |
129 | MAX_DB | |
130 | }; | |
131 | ||
132 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) | |
30b669d7 | 133 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB)) |
aba5acdf | 134 | #define ALL_DB ((1<<MAX_DB)-1) |
9db7bf15 | 135 | #define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB)) |
aba5acdf SH |
136 | |
137 | enum { | |
7d105b56 SH |
138 | SS_UNKNOWN, |
139 | SS_ESTABLISHED, | |
140 | SS_SYN_SENT, | |
141 | SS_SYN_RECV, | |
142 | SS_FIN_WAIT1, | |
143 | SS_FIN_WAIT2, | |
144 | SS_TIME_WAIT, | |
145 | SS_CLOSE, | |
146 | SS_CLOSE_WAIT, | |
147 | SS_LAST_ACK, | |
148 | SS_LISTEN, | |
149 | SS_CLOSING, | |
150 | SS_MAX | |
aba5acdf SH |
151 | }; |
152 | ||
9db7bf15 VK |
153 | #define SS_ALL ((1 << SS_MAX) - 1) |
154 | #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV))) | |
aba5acdf SH |
155 | |
156 | #include "ssfilter.h" | |
157 | ||
acd1e437 | 158 | struct filter { |
aba5acdf SH |
159 | int dbs; |
160 | int states; | |
161 | int families; | |
162 | struct ssfilter *f; | |
fb2594c1 | 163 | bool kill; |
aba5acdf SH |
164 | }; |
165 | ||
9db7bf15 VK |
166 | static const struct filter default_dbs[MAX_DB] = { |
167 | [TCP_DB] = { | |
168 | .states = SS_CONN, | |
169 | .families = (1 << AF_INET) | (1 << AF_INET6), | |
170 | }, | |
171 | [DCCP_DB] = { | |
172 | .states = SS_CONN, | |
173 | .families = (1 << AF_INET) | (1 << AF_INET6), | |
174 | }, | |
175 | [UDP_DB] = { | |
f42a4574 | 176 | .states = (1 << SS_ESTABLISHED), |
9db7bf15 VK |
177 | .families = (1 << AF_INET) | (1 << AF_INET6), |
178 | }, | |
179 | [RAW_DB] = { | |
f42a4574 | 180 | .states = (1 << SS_ESTABLISHED), |
9db7bf15 VK |
181 | .families = (1 << AF_INET) | (1 << AF_INET6), |
182 | }, | |
183 | [UNIX_DG_DB] = { | |
184 | .states = (1 << SS_CLOSE), | |
185 | .families = (1 << AF_UNIX), | |
186 | }, | |
187 | [UNIX_ST_DB] = { | |
188 | .states = SS_CONN, | |
189 | .families = (1 << AF_UNIX), | |
190 | }, | |
191 | [UNIX_SQ_DB] = { | |
192 | .states = SS_CONN, | |
193 | .families = (1 << AF_UNIX), | |
194 | }, | |
195 | [PACKET_DG_DB] = { | |
196 | .states = (1 << SS_CLOSE), | |
197 | .families = (1 << AF_PACKET), | |
198 | }, | |
199 | [PACKET_R_DB] = { | |
200 | .states = (1 << SS_CLOSE), | |
201 | .families = (1 << AF_PACKET), | |
202 | }, | |
203 | [NETLINK_DB] = { | |
204 | .states = (1 << SS_CLOSE), | |
205 | .families = (1 << AF_NETLINK), | |
206 | }, | |
aba5acdf SH |
207 | }; |
208 | ||
9db7bf15 VK |
209 | static const struct filter default_afs[AF_MAX] = { |
210 | [AF_INET] = { | |
211 | .dbs = INET_DBM, | |
212 | .states = SS_CONN, | |
213 | }, | |
214 | [AF_INET6] = { | |
215 | .dbs = INET_DBM, | |
216 | .states = SS_CONN, | |
217 | }, | |
218 | [AF_UNIX] = { | |
219 | .dbs = UNIX_DBM, | |
220 | .states = SS_CONN, | |
221 | }, | |
222 | [AF_PACKET] = { | |
223 | .dbs = PACKET_DBM, | |
224 | .states = (1 << SS_CLOSE), | |
225 | }, | |
226 | [AF_NETLINK] = { | |
227 | .dbs = (1 << NETLINK_DB), | |
228 | .states = (1 << SS_CLOSE), | |
229 | }, | |
230 | }; | |
231 | ||
232 | static int do_default = 1; | |
233 | static struct filter current_filter; | |
234 | ||
235 | static void filter_db_set(struct filter *f, int db) | |
236 | { | |
237 | f->states |= default_dbs[db].states; | |
9db7bf15 VK |
238 | f->dbs |= 1 << db; |
239 | do_default = 0; | |
240 | } | |
241 | ||
242 | static void filter_af_set(struct filter *f, int af) | |
243 | { | |
1527a17e VK |
244 | f->states |= default_afs[af].states; |
245 | f->families |= 1 << af; | |
246 | do_default = 0; | |
247 | preferred_family = af; | |
9db7bf15 VK |
248 | } |
249 | ||
250 | static int filter_af_get(struct filter *f, int af) | |
251 | { | |
252 | return f->families & (1 << af); | |
253 | } | |
254 | ||
255 | static void filter_default_dbs(struct filter *f) | |
256 | { | |
257 | filter_db_set(f, UDP_DB); | |
258 | filter_db_set(f, DCCP_DB); | |
259 | filter_db_set(f, TCP_DB); | |
260 | filter_db_set(f, RAW_DB); | |
261 | filter_db_set(f, UNIX_ST_DB); | |
262 | filter_db_set(f, UNIX_DG_DB); | |
263 | filter_db_set(f, UNIX_SQ_DB); | |
264 | filter_db_set(f, PACKET_R_DB); | |
265 | filter_db_set(f, PACKET_DG_DB); | |
266 | filter_db_set(f, NETLINK_DB); | |
267 | } | |
268 | ||
57ff5a10 | 269 | static void filter_states_set(struct filter *f, int states) |
9db7bf15 | 270 | { |
57ff5a10 | 271 | if (states) |
9d320e1e | 272 | f->states = states; |
57ff5a10 | 273 | } |
9db7bf15 | 274 | |
57ff5a10 VK |
275 | static void filter_merge_defaults(struct filter *f) |
276 | { | |
277 | int db; | |
278 | int af; | |
9db7bf15 | 279 | |
57ff5a10 VK |
280 | for (db = 0; db < MAX_DB; db++) { |
281 | if (!(f->dbs & (1 << db))) | |
282 | continue; | |
9db7bf15 | 283 | |
57ff5a10 VK |
284 | if (!(default_dbs[db].families & f->families)) |
285 | f->families |= default_dbs[db].families; | |
286 | } | |
287 | for (af = 0; af < AF_MAX; af++) { | |
288 | if (!(f->families & (1 << af))) | |
289 | continue; | |
290 | ||
291 | if (!(default_afs[af].dbs & f->dbs)) | |
292 | f->dbs |= default_afs[af].dbs; | |
293 | } | |
9db7bf15 | 294 | } |
aba5acdf | 295 | |
ab01dbbb | 296 | static FILE *generic_proc_open(const char *env, const char *name) |
aba5acdf | 297 | { |
ab01dbbb | 298 | const char *p = getenv(env); |
aba5acdf | 299 | char store[128]; |
ab01dbbb | 300 | |
aba5acdf SH |
301 | if (!p) { |
302 | p = getenv("PROC_ROOT") ? : "/proc"; | |
303 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
304 | p = store; | |
305 | } | |
ab01dbbb SH |
306 | |
307 | return fopen(p, "r"); | |
aba5acdf SH |
308 | } |
309 | ||
ab01dbbb | 310 | static FILE *net_tcp_open(void) |
aba5acdf SH |
311 | { |
312 | return generic_proc_open("PROC_NET_TCP", "net/tcp"); | |
313 | } | |
314 | ||
ab01dbbb | 315 | static FILE *net_tcp6_open(void) |
aba5acdf SH |
316 | { |
317 | return generic_proc_open("PROC_NET_TCP6", "net/tcp6"); | |
318 | } | |
319 | ||
ab01dbbb | 320 | static FILE *net_udp_open(void) |
aba5acdf SH |
321 | { |
322 | return generic_proc_open("PROC_NET_UDP", "net/udp"); | |
323 | } | |
324 | ||
ab01dbbb | 325 | static FILE *net_udp6_open(void) |
aba5acdf SH |
326 | { |
327 | return generic_proc_open("PROC_NET_UDP6", "net/udp6"); | |
328 | } | |
329 | ||
ab01dbbb | 330 | static FILE *net_raw_open(void) |
aba5acdf SH |
331 | { |
332 | return generic_proc_open("PROC_NET_RAW", "net/raw"); | |
333 | } | |
334 | ||
ab01dbbb | 335 | static FILE *net_raw6_open(void) |
aba5acdf SH |
336 | { |
337 | return generic_proc_open("PROC_NET_RAW6", "net/raw6"); | |
338 | } | |
339 | ||
ab01dbbb | 340 | static FILE *net_unix_open(void) |
aba5acdf SH |
341 | { |
342 | return generic_proc_open("PROC_NET_UNIX", "net/unix"); | |
343 | } | |
344 | ||
ab01dbbb | 345 | static FILE *net_packet_open(void) |
aba5acdf SH |
346 | { |
347 | return generic_proc_open("PROC_NET_PACKET", "net/packet"); | |
348 | } | |
349 | ||
ab01dbbb | 350 | static FILE *net_netlink_open(void) |
aba5acdf SH |
351 | { |
352 | return generic_proc_open("PROC_NET_NETLINK", "net/netlink"); | |
353 | } | |
354 | ||
ab01dbbb | 355 | static FILE *slabinfo_open(void) |
aba5acdf SH |
356 | { |
357 | return generic_proc_open("PROC_SLABINFO", "slabinfo"); | |
358 | } | |
359 | ||
ab01dbbb | 360 | static FILE *net_sockstat_open(void) |
aba5acdf SH |
361 | { |
362 | return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat"); | |
363 | } | |
364 | ||
ab01dbbb | 365 | static FILE *net_sockstat6_open(void) |
aba5acdf SH |
366 | { |
367 | return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6"); | |
368 | } | |
369 | ||
ab01dbbb | 370 | static FILE *net_snmp_open(void) |
aba5acdf SH |
371 | { |
372 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); | |
373 | } | |
374 | ||
ab01dbbb | 375 | static FILE *ephemeral_ports_open(void) |
aba5acdf SH |
376 | { |
377 | return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range"); | |
378 | } | |
379 | ||
fbc0f876 SF |
380 | struct user_ent { |
381 | struct user_ent *next; | |
382 | unsigned int ino; | |
383 | int pid; | |
384 | int fd; | |
116ac927 RH |
385 | char *process; |
386 | char *process_ctx; | |
387 | char *socket_ctx; | |
fbc0f876 SF |
388 | }; |
389 | ||
390 | #define USER_ENT_HASH_SIZE 256 | |
391 | struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; | |
392 | ||
393 | static int user_ent_hashfn(unsigned int ino) | |
aba5acdf | 394 | { |
fbc0f876 | 395 | int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino; |
aba5acdf | 396 | |
fbc0f876 SF |
397 | return val & (USER_ENT_HASH_SIZE - 1); |
398 | } | |
399 | ||
116ac927 RH |
400 | static void user_ent_add(unsigned int ino, char *process, |
401 | int pid, int fd, | |
402 | char *proc_ctx, | |
403 | char *sock_ctx) | |
fbc0f876 SF |
404 | { |
405 | struct user_ent *p, **pp; | |
aba5acdf | 406 | |
116ac927 RH |
407 | p = malloc(sizeof(struct user_ent)); |
408 | if (!p) { | |
409 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
fbc0f876 | 410 | abort(); |
116ac927 | 411 | } |
fbc0f876 SF |
412 | p->next = NULL; |
413 | p->ino = ino; | |
414 | p->pid = pid; | |
415 | p->fd = fd; | |
116ac927 RH |
416 | p->process = strdup(process); |
417 | p->process_ctx = strdup(proc_ctx); | |
418 | p->socket_ctx = strdup(sock_ctx); | |
fbc0f876 SF |
419 | |
420 | pp = &user_ent_hash[user_ent_hashfn(ino)]; | |
421 | p->next = *pp; | |
422 | *pp = p; | |
423 | } | |
aba5acdf | 424 | |
116ac927 RH |
425 | static void user_ent_destroy(void) |
426 | { | |
427 | struct user_ent *p, *p_next; | |
428 | int cnt = 0; | |
429 | ||
430 | while (cnt != USER_ENT_HASH_SIZE) { | |
431 | p = user_ent_hash[cnt]; | |
432 | while (p) { | |
433 | free(p->process); | |
434 | free(p->process_ctx); | |
435 | free(p->socket_ctx); | |
436 | p_next = p->next; | |
437 | free(p); | |
438 | p = p_next; | |
439 | } | |
440 | cnt++; | |
441 | } | |
442 | } | |
443 | ||
fbc0f876 SF |
444 | static void user_ent_hash_build(void) |
445 | { | |
446 | const char *root = getenv("PROC_ROOT") ? : "/proc/"; | |
447 | struct dirent *d; | |
448 | char name[1024]; | |
449 | int nameoff; | |
450 | DIR *dir; | |
116ac927 RH |
451 | char *pid_context; |
452 | char *sock_context; | |
453 | const char *no_ctx = "unavailable"; | |
454 | ||
455 | /* If show_users & show_proc_ctx set only do this once */ | |
456 | if (user_ent_hash_build_init != 0) | |
457 | return; | |
458 | ||
459 | user_ent_hash_build_init = 1; | |
fbc0f876 | 460 | |
0ee9052f | 461 | strncpy(name, root, sizeof(name)-1); |
462 | name[sizeof(name)-1] = 0; | |
463 | ||
fbc0f876 | 464 | if (strlen(name) == 0 || name[strlen(name)-1] != '/') |
aba5acdf | 465 | strcat(name, "/"); |
fbc0f876 | 466 | |
aba5acdf | 467 | nameoff = strlen(name); |
fbc0f876 SF |
468 | |
469 | dir = opendir(name); | |
470 | if (!dir) | |
471 | return; | |
aba5acdf SH |
472 | |
473 | while ((d = readdir(dir)) != NULL) { | |
aba5acdf | 474 | struct dirent *d1; |
aba5acdf | 475 | char process[16]; |
116ac927 | 476 | char *p; |
fbc0f876 SF |
477 | int pid, pos; |
478 | DIR *dir1; | |
479 | char crap; | |
aba5acdf SH |
480 | |
481 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) | |
482 | continue; | |
483 | ||
116ac927 RH |
484 | if (getpidcon(pid, &pid_context) != 0) |
485 | pid_context = strdup(no_ctx); | |
486 | ||
0ee9052f | 487 | snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); |
aba5acdf | 488 | pos = strlen(name); |
a02371fb PS |
489 | if ((dir1 = opendir(name)) == NULL) { |
490 | free(pid_context); | |
aba5acdf | 491 | continue; |
a02371fb | 492 | } |
aba5acdf | 493 | |
fbc0f876 | 494 | process[0] = '\0'; |
116ac927 | 495 | p = process; |
aba5acdf SH |
496 | |
497 | while ((d1 = readdir(dir1)) != NULL) { | |
fbc0f876 SF |
498 | const char *pattern = "socket:["; |
499 | unsigned int ino; | |
aba5acdf | 500 | char lnk[64]; |
18445b3e | 501 | int fd; |
788731b3 | 502 | ssize_t link_len; |
116ac927 | 503 | char tmp[1024]; |
aba5acdf SH |
504 | |
505 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) | |
506 | continue; | |
507 | ||
0ee9052f | 508 | snprintf(name+pos, sizeof(name) - pos, "%d", fd); |
788731b3 TJ |
509 | |
510 | link_len = readlink(name, lnk, sizeof(lnk)-1); | |
511 | if (link_len == -1) | |
512 | continue; | |
513 | lnk[link_len] = '\0'; | |
514 | ||
515 | if (strncmp(lnk, pattern, strlen(pattern))) | |
aba5acdf SH |
516 | continue; |
517 | ||
fbc0f876 | 518 | sscanf(lnk, "socket:[%u]", &ino); |
aba5acdf | 519 | |
116ac927 RH |
520 | snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s", |
521 | root, pid, d1->d_name); | |
522 | ||
523 | if (getfilecon(tmp, &sock_context) <= 0) | |
524 | sock_context = strdup(no_ctx); | |
525 | ||
526 | if (*p == '\0') { | |
aba5acdf | 527 | FILE *fp; |
fbc0f876 | 528 | |
116ac927 RH |
529 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", |
530 | root, pid); | |
aba5acdf | 531 | if ((fp = fopen(tmp, "r")) != NULL) { |
d572ed4d PS |
532 | if (fscanf(fp, "%*d (%[^)])", p) < 1) |
533 | ; /* ignore */ | |
aba5acdf SH |
534 | fclose(fp); |
535 | } | |
536 | } | |
116ac927 RH |
537 | user_ent_add(ino, p, pid, fd, |
538 | pid_context, sock_context); | |
539 | free(sock_context); | |
aba5acdf | 540 | } |
116ac927 | 541 | free(pid_context); |
aba5acdf SH |
542 | closedir(dir1); |
543 | } | |
544 | closedir(dir); | |
fbc0f876 SF |
545 | } |
546 | ||
116ac927 RH |
547 | enum entry_types { |
548 | USERS, | |
549 | PROC_CTX, | |
550 | PROC_SOCK_CTX | |
551 | }; | |
552 | ||
553 | #define ENTRY_BUF_SIZE 512 | |
acd1e437 | 554 | static int find_entry(unsigned int ino, char **buf, int type) |
fbc0f876 SF |
555 | { |
556 | struct user_ent *p; | |
557 | int cnt = 0; | |
558 | char *ptr; | |
532ca40a | 559 | char *new_buf; |
116ac927 RH |
560 | int len, new_buf_len; |
561 | int buf_used = 0; | |
562 | int buf_len = 0; | |
fbc0f876 SF |
563 | |
564 | if (!ino) | |
565 | return 0; | |
566 | ||
567 | p = user_ent_hash[user_ent_hashfn(ino)]; | |
116ac927 | 568 | ptr = *buf = NULL; |
fbc0f876 SF |
569 | while (p) { |
570 | if (p->ino != ino) | |
571 | goto next; | |
572 | ||
116ac927 RH |
573 | while (1) { |
574 | ptr = *buf + buf_used; | |
575 | switch (type) { | |
576 | case USERS: | |
577 | len = snprintf(ptr, buf_len - buf_used, | |
578 | "(\"%s\",pid=%d,fd=%d),", | |
579 | p->process, p->pid, p->fd); | |
580 | break; | |
581 | case PROC_CTX: | |
582 | len = snprintf(ptr, buf_len - buf_used, | |
583 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),", | |
584 | p->process, p->pid, | |
585 | p->process_ctx, p->fd); | |
586 | break; | |
587 | case PROC_SOCK_CTX: | |
588 | len = snprintf(ptr, buf_len - buf_used, | |
589 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),", | |
590 | p->process, p->pid, | |
591 | p->process_ctx, p->fd, | |
592 | p->socket_ctx); | |
593 | break; | |
594 | default: | |
595 | fprintf(stderr, "ss: invalid type: %d\n", type); | |
596 | abort(); | |
597 | } | |
fbc0f876 | 598 | |
116ac927 RH |
599 | if (len < 0 || len >= buf_len - buf_used) { |
600 | new_buf_len = buf_len + ENTRY_BUF_SIZE; | |
532ca40a | 601 | new_buf = realloc(*buf, new_buf_len); |
116ac927 RH |
602 | if (!new_buf) { |
603 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
604 | abort(); | |
605 | } | |
532ca40a | 606 | *buf = new_buf; |
116ac927 RH |
607 | buf_len = new_buf_len; |
608 | continue; | |
609 | } else { | |
610 | buf_used += len; | |
611 | break; | |
612 | } | |
613 | } | |
fbc0f876 | 614 | cnt++; |
116ac927 | 615 | next: |
fbc0f876 SF |
616 | p = p->next; |
617 | } | |
116ac927 RH |
618 | if (buf_used) { |
619 | ptr = *buf + buf_used; | |
fbc0f876 | 620 | ptr[-1] = '\0'; |
116ac927 | 621 | } |
aba5acdf SH |
622 | return cnt; |
623 | } | |
624 | ||
aba5acdf SH |
625 | /* Get stats from slab */ |
626 | ||
acd1e437 | 627 | struct slabstat { |
aba5acdf SH |
628 | int socks; |
629 | int tcp_ports; | |
630 | int tcp_tws; | |
631 | int tcp_syns; | |
632 | int skbs; | |
633 | }; | |
634 | ||
a221d621 | 635 | static struct slabstat slabstat; |
aba5acdf | 636 | |
acd1e437 SH |
637 | static const char *slabstat_ids[] = { |
638 | ||
aba5acdf SH |
639 | "sock", |
640 | "tcp_bind_bucket", | |
641 | "tcp_tw_bucket", | |
642 | "tcp_open_request", | |
643 | "skbuff_head_cache", | |
644 | }; | |
645 | ||
d1f28cf1 | 646 | static int get_slabstat(struct slabstat *s) |
aba5acdf SH |
647 | { |
648 | char buf[256]; | |
649 | FILE *fp; | |
650 | int cnt; | |
a221d621 BL |
651 | static int slabstat_valid; |
652 | ||
653 | if (slabstat_valid) | |
654 | return 0; | |
aba5acdf SH |
655 | |
656 | memset(s, 0, sizeof(*s)); | |
657 | ||
ab01dbbb SH |
658 | fp = slabinfo_open(); |
659 | if (!fp) | |
aba5acdf SH |
660 | return -1; |
661 | ||
662 | cnt = sizeof(*s)/sizeof(int); | |
663 | ||
d572ed4d PS |
664 | if (!fgets(buf, sizeof(buf), fp)) { |
665 | fclose(fp); | |
666 | return -1; | |
667 | } | |
acd1e437 | 668 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
aba5acdf | 669 | int i; |
acd1e437 | 670 | |
62000e51 | 671 | for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) { |
aba5acdf SH |
672 | if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) { |
673 | sscanf(buf, "%*s%d", ((int *)s) + i); | |
674 | cnt--; | |
675 | break; | |
676 | } | |
677 | } | |
678 | if (cnt <= 0) | |
679 | break; | |
680 | } | |
681 | ||
a221d621 BL |
682 | slabstat_valid = 1; |
683 | ||
aba5acdf SH |
684 | fclose(fp); |
685 | return 0; | |
686 | } | |
687 | ||
2e7e805d | 688 | static unsigned long long cookie_sk_get(const uint32_t *cookie) |
f1b39e1b | 689 | { |
2e7e805d | 690 | return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0]; |
f1b39e1b VK |
691 | } |
692 | ||
7d105b56 SH |
693 | static const char *sstate_name[] = { |
694 | "UNKNOWN", | |
9cb1eccf ED |
695 | [SS_ESTABLISHED] = "ESTAB", |
696 | [SS_SYN_SENT] = "SYN-SENT", | |
697 | [SS_SYN_RECV] = "SYN-RECV", | |
698 | [SS_FIN_WAIT1] = "FIN-WAIT-1", | |
699 | [SS_FIN_WAIT2] = "FIN-WAIT-2", | |
700 | [SS_TIME_WAIT] = "TIME-WAIT", | |
701 | [SS_CLOSE] = "UNCONN", | |
702 | [SS_CLOSE_WAIT] = "CLOSE-WAIT", | |
703 | [SS_LAST_ACK] = "LAST-ACK", | |
acd1e437 | 704 | [SS_LISTEN] = "LISTEN", |
9cb1eccf | 705 | [SS_CLOSING] = "CLOSING", |
aba5acdf SH |
706 | }; |
707 | ||
7d105b56 SH |
708 | static const char *sstate_namel[] = { |
709 | "UNKNOWN", | |
9cb1eccf ED |
710 | [SS_ESTABLISHED] = "established", |
711 | [SS_SYN_SENT] = "syn-sent", | |
712 | [SS_SYN_RECV] = "syn-recv", | |
713 | [SS_FIN_WAIT1] = "fin-wait-1", | |
714 | [SS_FIN_WAIT2] = "fin-wait-2", | |
715 | [SS_TIME_WAIT] = "time-wait", | |
716 | [SS_CLOSE] = "unconnected", | |
717 | [SS_CLOSE_WAIT] = "close-wait", | |
718 | [SS_LAST_ACK] = "last-ack", | |
acd1e437 | 719 | [SS_LISTEN] = "listening", |
9cb1eccf | 720 | [SS_CLOSING] = "closing", |
aba5acdf SH |
721 | }; |
722 | ||
acd1e437 | 723 | struct sockstat { |
ec4d0d8a VK |
724 | struct sockstat *next; |
725 | unsigned int type; | |
89f634f9 | 726 | uint16_t prot; |
8250bc9f VK |
727 | inet_prefix local; |
728 | inet_prefix remote; | |
729 | int lport; | |
730 | int rport; | |
731 | int state; | |
732 | int rq, wq; | |
acd1e437 SH |
733 | unsigned int ino; |
734 | unsigned int uid; | |
8250bc9f VK |
735 | int refcnt; |
736 | unsigned int iface; | |
737 | unsigned long long sk; | |
99bb68ff VK |
738 | char *name; |
739 | char *peer_name; | |
055840f2 VK |
740 | }; |
741 | ||
acd1e437 | 742 | struct dctcpstat { |
055840f2 VK |
743 | unsigned int ce_state; |
744 | unsigned int alpha; | |
745 | unsigned int ab_ecn; | |
746 | unsigned int ab_tot; | |
747 | bool enabled; | |
748 | }; | |
749 | ||
acd1e437 | 750 | struct tcpstat { |
055840f2 | 751 | struct sockstat ss; |
8250bc9f VK |
752 | int timer; |
753 | int timeout; | |
754 | int probes; | |
d2055ea5 | 755 | char cong_alg[16]; |
8250bc9f VK |
756 | double rto, ato, rtt, rttvar; |
757 | int qack, cwnd, ssthresh, backoff; | |
758 | double send_bps; | |
759 | int snd_wscale; | |
760 | int rcv_wscale; | |
761 | int mss; | |
762 | unsigned int lastsnd; | |
763 | unsigned int lastrcv; | |
764 | unsigned int lastack; | |
765 | double pacing_rate; | |
766 | double pacing_rate_max; | |
1a4dda71 ED |
767 | unsigned long long bytes_acked; |
768 | unsigned long long bytes_received; | |
ecb435ea CG |
769 | unsigned int segs_out; |
770 | unsigned int segs_in; | |
414aeec9 MKL |
771 | unsigned int data_segs_out; |
772 | unsigned int data_segs_in; | |
8250bc9f VK |
773 | unsigned int unacked; |
774 | unsigned int retrans; | |
775 | unsigned int retrans_total; | |
776 | unsigned int lost; | |
777 | unsigned int sacked; | |
778 | unsigned int fackets; | |
779 | unsigned int reordering; | |
9e99e495 | 780 | unsigned int not_sent; |
8250bc9f | 781 | double rcv_rtt; |
9e99e495 | 782 | double min_rtt; |
8250bc9f VK |
783 | int rcv_space; |
784 | bool has_ts_opt; | |
785 | bool has_sack_opt; | |
786 | bool has_ecn_opt; | |
787 | bool has_ecnseen_opt; | |
788 | bool has_fastopen_opt; | |
789 | bool has_wscale_opt; | |
790 | struct dctcpstat *dctcp; | |
2f0f9aef | 791 | struct tcp_bbr_info *bbr_info; |
aba5acdf SH |
792 | }; |
793 | ||
2d791bc8 VK |
794 | static void sock_state_print(struct sockstat *s, const char *sock_name) |
795 | { | |
796 | if (netid_width) | |
797 | printf("%-*s ", netid_width, sock_name); | |
798 | if (state_width) | |
799 | printf("%-*s ", state_width, sstate_name[s->state]); | |
800 | ||
801 | printf("%-6d %-6d ", s->rq, s->wq); | |
802 | } | |
803 | ||
f1b39e1b VK |
804 | static void sock_details_print(struct sockstat *s) |
805 | { | |
806 | if (s->uid) | |
807 | printf(" uid:%u", s->uid); | |
808 | ||
809 | printf(" ino:%u", s->ino); | |
810 | printf(" sk:%llx", s->sk); | |
811 | } | |
812 | ||
b217df10 VK |
813 | static void sock_addr_print_width(int addr_len, const char *addr, char *delim, |
814 | int port_len, const char *port, const char *ifname) | |
815 | { | |
816 | if (ifname) { | |
817 | printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim, | |
818 | port_len, port); | |
acd1e437 | 819 | } else { |
b217df10 VK |
820 | printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port); |
821 | } | |
822 | } | |
823 | ||
824 | static void sock_addr_print(const char *addr, char *delim, const char *port, | |
825 | const char *ifname) | |
826 | { | |
827 | sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname); | |
828 | } | |
829 | ||
7d105b56 | 830 | static const char *tmr_name[] = { |
aba5acdf SH |
831 | "off", |
832 | "on", | |
833 | "keepalive", | |
834 | "timewait", | |
835 | "persist", | |
836 | "unknown" | |
837 | }; | |
838 | ||
d1f28cf1 | 839 | static const char *print_ms_timer(int timeout) |
aba5acdf SH |
840 | { |
841 | static char buf[64]; | |
842 | int secs, msecs, minutes; | |
acd1e437 | 843 | |
aba5acdf SH |
844 | if (timeout < 0) |
845 | timeout = 0; | |
846 | secs = timeout/1000; | |
847 | minutes = secs/60; | |
848 | secs = secs%60; | |
849 | msecs = timeout%1000; | |
850 | buf[0] = 0; | |
851 | if (minutes) { | |
852 | msecs = 0; | |
853 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); | |
854 | if (minutes > 9) | |
855 | secs = 0; | |
856 | } | |
857 | if (secs) { | |
858 | if (secs > 9) | |
859 | msecs = 0; | |
860 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); | |
861 | } | |
862 | if (msecs) | |
863 | sprintf(buf+strlen(buf), "%03dms", msecs); | |
864 | return buf; | |
e7113c61 | 865 | } |
aba5acdf | 866 | |
22588a0e | 867 | struct scache { |
aba5acdf SH |
868 | struct scache *next; |
869 | int port; | |
870 | char *name; | |
871 | const char *proto; | |
872 | }; | |
873 | ||
874 | struct scache *rlist; | |
875 | ||
d1f28cf1 | 876 | static void init_service_resolver(void) |
aba5acdf SH |
877 | { |
878 | char buf[128]; | |
879 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); | |
596307ea PS |
880 | |
881 | if (!fp) | |
882 | return; | |
883 | ||
884 | if (!fgets(buf, sizeof(buf), fp)) { | |
2bcc3c16 | 885 | pclose(fp); |
596307ea PS |
886 | return; |
887 | } | |
888 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
889 | unsigned int progn, port; | |
890 | char proto[128], prog[128] = "rpc."; | |
891 | struct scache *c; | |
892 | ||
893 | if (sscanf(buf, "%u %*d %s %u %s", | |
acd1e437 | 894 | &progn, proto, &port, prog+4) != 4) |
596307ea PS |
895 | continue; |
896 | ||
897 | if (!(c = malloc(sizeof(*c)))) | |
898 | continue; | |
899 | ||
900 | c->port = port; | |
901 | c->name = strdup(prog); | |
902 | if (strcmp(proto, TCP_PROTO) == 0) | |
903 | c->proto = TCP_PROTO; | |
904 | else if (strcmp(proto, UDP_PROTO) == 0) | |
905 | c->proto = UDP_PROTO; | |
906 | else | |
907 | c->proto = NULL; | |
908 | c->next = rlist; | |
909 | rlist = c; | |
aba5acdf | 910 | } |
596307ea | 911 | pclose(fp); |
aba5acdf SH |
912 | } |
913 | ||
ab61159a SH |
914 | /* Even do not try default linux ephemeral port ranges: |
915 | * default /etc/services contains so much of useless crap | |
916 | * wouldbe "allocated" to this area that resolution | |
917 | * is really harmful. I shrug each time when seeing | |
918 | * "socks" or "cfinger" in dumps. | |
919 | */ | |
920 | static int is_ephemeral(int port) | |
921 | { | |
acd1e437 | 922 | static int min = 0, max; |
c29d3792 PS |
923 | |
924 | if (!min) { | |
ab01dbbb | 925 | FILE *f = ephemeral_ports_open(); |
acd1e437 | 926 | |
c29d3792 PS |
927 | if (!f || fscanf(f, "%d %d", &min, &max) < 2) { |
928 | min = 1024; | |
929 | max = 4999; | |
ab61159a | 930 | } |
c29d3792 PS |
931 | if (f) |
932 | fclose(f); | |
ab61159a | 933 | } |
c29d3792 | 934 | return port >= min && port <= max; |
ab61159a SH |
935 | } |
936 | ||
937 | ||
d1f28cf1 | 938 | static const char *__resolve_service(int port) |
aba5acdf SH |
939 | { |
940 | struct scache *c; | |
941 | ||
942 | for (c = rlist; c; c = c->next) { | |
943 | if (c->port == port && c->proto == dg_proto) | |
944 | return c->name; | |
945 | } | |
946 | ||
ab61159a | 947 | if (!is_ephemeral(port)) { |
aba5acdf SH |
948 | static int notfirst; |
949 | struct servent *se; | |
acd1e437 | 950 | |
aba5acdf SH |
951 | if (!notfirst) { |
952 | setservent(1); | |
953 | notfirst = 1; | |
ae665a52 | 954 | } |
aba5acdf SH |
955 | se = getservbyport(htons(port), dg_proto); |
956 | if (se) | |
957 | return se->s_name; | |
958 | } | |
959 | ||
960 | return NULL; | |
961 | } | |
962 | ||
22588a0e ED |
963 | #define SCACHE_BUCKETS 1024 |
964 | static struct scache *cache_htab[SCACHE_BUCKETS]; | |
aba5acdf | 965 | |
d1f28cf1 | 966 | static const char *resolve_service(int port) |
aba5acdf SH |
967 | { |
968 | static char buf[128]; | |
22588a0e ED |
969 | struct scache *c; |
970 | const char *res; | |
971 | int hash; | |
aba5acdf SH |
972 | |
973 | if (port == 0) { | |
974 | buf[0] = '*'; | |
975 | buf[1] = 0; | |
976 | return buf; | |
977 | } | |
978 | ||
22588a0e ED |
979 | if (!resolve_services) |
980 | goto do_numeric; | |
aba5acdf | 981 | |
22588a0e ED |
982 | if (dg_proto == RAW_PROTO) |
983 | return inet_proto_n2a(port, buf, sizeof(buf)); | |
984 | ||
985 | ||
986 | hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS; | |
987 | ||
988 | for (c = cache_htab[hash]; c; c = c->next) { | |
989 | if (c->port == port && c->proto == dg_proto) | |
990 | goto do_cache; | |
aba5acdf SH |
991 | } |
992 | ||
22588a0e ED |
993 | c = malloc(sizeof(*c)); |
994 | if (!c) | |
995 | goto do_numeric; | |
996 | res = __resolve_service(port); | |
997 | c->port = port; | |
998 | c->name = res ? strdup(res) : NULL; | |
999 | c->proto = dg_proto; | |
1000 | c->next = cache_htab[hash]; | |
1001 | cache_htab[hash] = c; | |
1002 | ||
1003 | do_cache: | |
1004 | if (c->name) | |
1005 | return c->name; | |
1006 | ||
1007 | do_numeric: | |
aba5acdf SH |
1008 | sprintf(buf, "%u", port); |
1009 | return buf; | |
1010 | } | |
1011 | ||
b217df10 | 1012 | static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex) |
aba5acdf SH |
1013 | { |
1014 | char buf[1024]; | |
1015 | const char *ap = buf; | |
b217df10 VK |
1016 | int est_len = addr_width; |
1017 | const char *ifname = NULL; | |
aba5acdf SH |
1018 | |
1019 | if (a->family == AF_INET) { | |
1020 | if (a->data[0] == 0) { | |
1021 | buf[0] = '*'; | |
1022 | buf[1] = 0; | |
1023 | } else { | |
a418e451 | 1024 | ap = format_host(AF_INET, 4, a->data); |
aba5acdf SH |
1025 | } |
1026 | } else { | |
a418e451 | 1027 | ap = format_host(a->family, 16, a->data); |
aba5acdf SH |
1028 | est_len = strlen(ap); |
1029 | if (est_len <= addr_width) | |
1030 | est_len = addr_width; | |
1031 | else | |
1032 | est_len = addr_width + ((est_len-addr_width+3)/4)*4; | |
1033 | } | |
b217df10 | 1034 | |
7c8a3cfb | 1035 | if (ifindex) { |
b217df10 VK |
1036 | ifname = ll_index_to_name(ifindex); |
1037 | est_len -= strlen(ifname) + 1; /* +1 for percent char */ | |
4fcfb6bc MS |
1038 | if (est_len < 0) |
1039 | est_len = 0; | |
b217df10 | 1040 | } |
7c8a3cfb | 1041 | |
b217df10 VK |
1042 | sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port), |
1043 | ifname); | |
aba5acdf SH |
1044 | } |
1045 | ||
acd1e437 | 1046 | struct aafilter { |
aba5acdf SH |
1047 | inet_prefix addr; |
1048 | int port; | |
2d293212 | 1049 | unsigned int iface; |
aba5acdf SH |
1050 | struct aafilter *next; |
1051 | }; | |
1052 | ||
d1f28cf1 SH |
1053 | static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, |
1054 | int plen) | |
aba5acdf SH |
1055 | { |
1056 | if (!inet_addr_match(a, p, plen)) | |
1057 | return 0; | |
7d105b56 | 1058 | |
aba5acdf SH |
1059 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches |
1060 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped | |
1061 | * sockets. Fair? */ | |
1062 | if (p->family == AF_INET && a->family == AF_INET6) { | |
1063 | if (a->data[0] == 0 && a->data[1] == 0 && | |
1064 | a->data[2] == htonl(0xffff)) { | |
1065 | inet_prefix tmp = *a; | |
acd1e437 | 1066 | |
aba5acdf SH |
1067 | tmp.data[0] = a->data[3]; |
1068 | return inet_addr_match(&tmp, p, plen); | |
1069 | } | |
1070 | } | |
1071 | return 1; | |
1072 | } | |
1073 | ||
d1f28cf1 | 1074 | static int unix_match(const inet_prefix *a, const inet_prefix *p) |
aba5acdf | 1075 | { |
99bb68ff | 1076 | char *addr, *pattern; |
acd1e437 | 1077 | |
99bb68ff VK |
1078 | memcpy(&addr, a->data, sizeof(addr)); |
1079 | memcpy(&pattern, p->data, sizeof(pattern)); | |
aba5acdf SH |
1080 | if (pattern == NULL) |
1081 | return 1; | |
1082 | if (addr == NULL) | |
1083 | addr = ""; | |
1084 | return !fnmatch(pattern, addr, 0); | |
1085 | } | |
1086 | ||
055840f2 | 1087 | static int run_ssfilter(struct ssfilter *f, struct sockstat *s) |
aba5acdf SH |
1088 | { |
1089 | switch (f->type) { | |
1090 | case SSF_S_AUTO: | |
1091 | { | |
aba5acdf | 1092 | if (s->local.family == AF_UNIX) { |
99bb68ff | 1093 | char *p; |
acd1e437 | 1094 | |
99bb68ff | 1095 | memcpy(&p, s->local.data, sizeof(p)); |
aba5acdf | 1096 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && |
ae665a52 | 1097 | strspn(p+1, "0123456789abcdef") == 5); |
aba5acdf SH |
1098 | } |
1099 | if (s->local.family == AF_PACKET) | |
bbd303d1 | 1100 | return s->lport == 0 && s->local.data[0] == 0; |
aba5acdf SH |
1101 | if (s->local.family == AF_NETLINK) |
1102 | return s->lport < 0; | |
1103 | ||
c29d3792 | 1104 | return is_ephemeral(s->lport); |
aba5acdf SH |
1105 | } |
1106 | case SSF_DCOND: | |
1107 | { | |
acd1e437 SH |
1108 | struct aafilter *a = (void *)f->pred; |
1109 | ||
aba5acdf SH |
1110 | if (a->addr.family == AF_UNIX) |
1111 | return unix_match(&s->remote, &a->addr); | |
1112 | if (a->port != -1 && a->port != s->rport) | |
1113 | return 0; | |
1114 | if (a->addr.bitlen) { | |
1115 | do { | |
1116 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) | |
1117 | return 1; | |
1118 | } while ((a = a->next) != NULL); | |
1119 | return 0; | |
1120 | } | |
1121 | return 1; | |
1122 | } | |
1123 | case SSF_SCOND: | |
1124 | { | |
acd1e437 SH |
1125 | struct aafilter *a = (void *)f->pred; |
1126 | ||
aba5acdf SH |
1127 | if (a->addr.family == AF_UNIX) |
1128 | return unix_match(&s->local, &a->addr); | |
1129 | if (a->port != -1 && a->port != s->lport) | |
1130 | return 0; | |
1131 | if (a->addr.bitlen) { | |
1132 | do { | |
1133 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) | |
1134 | return 1; | |
ae665a52 | 1135 | } while ((a = a->next) != NULL); |
aba5acdf SH |
1136 | return 0; |
1137 | } | |
1138 | return 1; | |
1139 | } | |
1140 | case SSF_D_GE: | |
1141 | { | |
acd1e437 SH |
1142 | struct aafilter *a = (void *)f->pred; |
1143 | ||
aba5acdf SH |
1144 | return s->rport >= a->port; |
1145 | } | |
1146 | case SSF_D_LE: | |
1147 | { | |
acd1e437 SH |
1148 | struct aafilter *a = (void *)f->pred; |
1149 | ||
aba5acdf SH |
1150 | return s->rport <= a->port; |
1151 | } | |
1152 | case SSF_S_GE: | |
1153 | { | |
acd1e437 SH |
1154 | struct aafilter *a = (void *)f->pred; |
1155 | ||
aba5acdf SH |
1156 | return s->lport >= a->port; |
1157 | } | |
1158 | case SSF_S_LE: | |
1159 | { | |
acd1e437 SH |
1160 | struct aafilter *a = (void *)f->pred; |
1161 | ||
aba5acdf SH |
1162 | return s->lport <= a->port; |
1163 | } | |
2d293212 DA |
1164 | case SSF_DEVCOND: |
1165 | { | |
1166 | struct aafilter *a = (void *)f->pred; | |
aba5acdf | 1167 | |
2d293212 DA |
1168 | return s->iface == a->iface; |
1169 | } | |
aba5acdf SH |
1170 | /* Yup. It is recursion. Sorry. */ |
1171 | case SSF_AND: | |
1172 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); | |
1173 | case SSF_OR: | |
1174 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); | |
1175 | case SSF_NOT: | |
1176 | return !run_ssfilter(f->pred, s); | |
1177 | default: | |
1178 | abort(); | |
1179 | } | |
1180 | } | |
1181 | ||
ae665a52 | 1182 | /* Relocate external jumps by reloc. */ |
b4b0b7d5 | 1183 | static void ssfilter_patch(char *a, int len, int reloc) |
aba5acdf SH |
1184 | { |
1185 | while (len > 0) { | |
acd1e437 SH |
1186 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a; |
1187 | ||
aba5acdf SH |
1188 | if (op->no == len+4) |
1189 | op->no += reloc; | |
1190 | len -= op->yes; | |
1191 | a += op->yes; | |
1192 | } | |
1193 | if (len < 0) | |
1194 | abort(); | |
1195 | } | |
1196 | ||
b4b0b7d5 | 1197 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
aba5acdf SH |
1198 | { |
1199 | switch (f->type) { | |
1200 | case SSF_S_AUTO: | |
1201 | { | |
acd1e437 SH |
1202 | if (!(*bytecode = malloc(4))) abort(); |
1203 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; | |
df39de8d | 1204 | return 4; |
aba5acdf SH |
1205 | } |
1206 | case SSF_DCOND: | |
1207 | case SSF_SCOND: | |
1208 | { | |
acd1e437 | 1209 | struct aafilter *a = (void *)f->pred; |
aba5acdf SH |
1210 | struct aafilter *b; |
1211 | char *ptr; | |
351efcde | 1212 | int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); |
aba5acdf SH |
1213 | int len = 0; |
1214 | ||
acd1e437 | 1215 | for (b = a; b; b = b->next) { |
351efcde | 1216 | len += 4 + sizeof(struct inet_diag_hostcond); |
aba5acdf SH |
1217 | if (a->addr.family == AF_INET6) |
1218 | len += 16; | |
1219 | else | |
1220 | len += 4; | |
1221 | if (b->next) | |
1222 | len += 4; | |
1223 | } | |
1224 | if (!(ptr = malloc(len))) abort(); | |
1225 | *bytecode = ptr; | |
acd1e437 | 1226 | for (b = a; b; b = b->next) { |
351efcde | 1227 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; |
aba5acdf | 1228 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
351efcde | 1229 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
acd1e437 | 1230 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4); |
aba5acdf | 1231 | |
351efcde | 1232 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
aba5acdf SH |
1233 | cond->family = a->addr.family; |
1234 | cond->port = a->port; | |
1235 | cond->prefix_len = a->addr.bitlen; | |
1236 | memcpy(cond->addr, a->addr.data, alen); | |
1237 | ptr += oplen; | |
1238 | if (b->next) { | |
351efcde SH |
1239 | op = (struct inet_diag_bc_op *)ptr; |
1240 | *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; | |
aba5acdf SH |
1241 | ptr += 4; |
1242 | } | |
1243 | } | |
1244 | return ptr - *bytecode; | |
1245 | } | |
1246 | case SSF_D_GE: | |
1247 | { | |
acd1e437 SH |
1248 | struct aafilter *x = (void *)f->pred; |
1249 | ||
1250 | if (!(*bytecode = malloc(8))) abort(); | |
1251 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; | |
1252 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1253 | return 8; |
1254 | } | |
1255 | case SSF_D_LE: | |
1256 | { | |
acd1e437 SH |
1257 | struct aafilter *x = (void *)f->pred; |
1258 | ||
1259 | if (!(*bytecode = malloc(8))) abort(); | |
1260 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; | |
1261 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1262 | return 8; |
1263 | } | |
1264 | case SSF_S_GE: | |
1265 | { | |
acd1e437 SH |
1266 | struct aafilter *x = (void *)f->pred; |
1267 | ||
1268 | if (!(*bytecode = malloc(8))) abort(); | |
1269 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; | |
1270 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1271 | return 8; |
1272 | } | |
1273 | case SSF_S_LE: | |
1274 | { | |
acd1e437 SH |
1275 | struct aafilter *x = (void *)f->pred; |
1276 | ||
1277 | if (!(*bytecode = malloc(8))) abort(); | |
1278 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; | |
1279 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
1280 | return 8; |
1281 | } | |
1282 | ||
1283 | case SSF_AND: | |
1284 | { | |
376fb868 | 1285 | char *a1 = NULL, *a2 = NULL, *a; |
2a4fa1c3 | 1286 | int l1, l2; |
acd1e437 | 1287 | |
aba5acdf SH |
1288 | l1 = ssfilter_bytecompile(f->pred, &a1); |
1289 | l2 = ssfilter_bytecompile(f->post, &a2); | |
376fb868 DA |
1290 | if (!l1 || !l2) { |
1291 | free(a1); | |
1292 | free(a2); | |
1293 | return 0; | |
1294 | } | |
aba5acdf SH |
1295 | if (!(a = malloc(l1+l2))) abort(); |
1296 | memcpy(a, a1, l1); | |
1297 | memcpy(a+l1, a2, l2); | |
1298 | free(a1); free(a2); | |
1299 | ssfilter_patch(a, l1, l2); | |
1300 | *bytecode = a; | |
1301 | return l1+l2; | |
1302 | } | |
1303 | case SSF_OR: | |
1304 | { | |
376fb868 | 1305 | char *a1 = NULL, *a2 = NULL, *a; |
2a4fa1c3 | 1306 | int l1, l2; |
acd1e437 | 1307 | |
aba5acdf SH |
1308 | l1 = ssfilter_bytecompile(f->pred, &a1); |
1309 | l2 = ssfilter_bytecompile(f->post, &a2); | |
376fb868 DA |
1310 | if (!l1 || !l2) { |
1311 | free(a1); | |
1312 | free(a2); | |
1313 | return 0; | |
1314 | } | |
aba5acdf SH |
1315 | if (!(a = malloc(l1+l2+4))) abort(); |
1316 | memcpy(a, a1, l1); | |
1317 | memcpy(a+l1+4, a2, l2); | |
1318 | free(a1); free(a2); | |
acd1e437 | 1319 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; |
aba5acdf SH |
1320 | *bytecode = a; |
1321 | return l1+l2+4; | |
1322 | } | |
1323 | case SSF_NOT: | |
1324 | { | |
376fb868 | 1325 | char *a1 = NULL, *a; |
2a4fa1c3 | 1326 | int l1; |
acd1e437 | 1327 | |
aba5acdf | 1328 | l1 = ssfilter_bytecompile(f->pred, &a1); |
376fb868 DA |
1329 | if (!l1) { |
1330 | free(a1); | |
1331 | return 0; | |
1332 | } | |
aba5acdf SH |
1333 | if (!(a = malloc(l1+4))) abort(); |
1334 | memcpy(a, a1, l1); | |
1335 | free(a1); | |
acd1e437 | 1336 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; |
aba5acdf SH |
1337 | *bytecode = a; |
1338 | return l1+4; | |
2d293212 DA |
1339 | } |
1340 | case SSF_DEVCOND: | |
1341 | { | |
1342 | /* bytecompile for SSF_DEVCOND not supported yet */ | |
1343 | return 0; | |
aba5acdf SH |
1344 | } |
1345 | default: | |
1346 | abort(); | |
1347 | } | |
1348 | } | |
1349 | ||
b4b0b7d5 | 1350 | static int remember_he(struct aafilter *a, struct hostent *he) |
aba5acdf | 1351 | { |
ae665a52 | 1352 | char **ptr = he->h_addr_list; |
aba5acdf SH |
1353 | int cnt = 0; |
1354 | int len; | |
1355 | ||
1356 | if (he->h_addrtype == AF_INET) | |
1357 | len = 4; | |
1358 | else if (he->h_addrtype == AF_INET6) | |
1359 | len = 16; | |
1360 | else | |
1361 | return 0; | |
1362 | ||
1363 | while (*ptr) { | |
1364 | struct aafilter *b = a; | |
acd1e437 | 1365 | |
aba5acdf SH |
1366 | if (a->addr.bitlen) { |
1367 | if ((b = malloc(sizeof(*b))) == NULL) | |
1368 | return cnt; | |
1369 | *b = *a; | |
1370 | b->next = a->next; | |
1371 | a->next = b; | |
1372 | } | |
1373 | memcpy(b->addr.data, *ptr, len); | |
1374 | b->addr.bytelen = len; | |
1375 | b->addr.bitlen = len*8; | |
1376 | b->addr.family = he->h_addrtype; | |
1377 | ptr++; | |
1378 | cnt++; | |
1379 | } | |
1380 | return cnt; | |
1381 | } | |
1382 | ||
b4b0b7d5 | 1383 | static int get_dns_host(struct aafilter *a, const char *addr, int fam) |
aba5acdf SH |
1384 | { |
1385 | static int notfirst; | |
1386 | int cnt = 0; | |
1387 | struct hostent *he; | |
1388 | ||
1389 | a->addr.bitlen = 0; | |
1390 | if (!notfirst) { | |
1391 | sethostent(1); | |
1392 | notfirst = 1; | |
1393 | } | |
1394 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); | |
1395 | if (he) | |
1396 | cnt = remember_he(a, he); | |
1397 | if (fam == AF_UNSPEC) { | |
1398 | he = gethostbyname2(addr, AF_INET6); | |
1399 | if (he) | |
1400 | cnt += remember_he(a, he); | |
1401 | } | |
1402 | return !cnt; | |
1403 | } | |
1404 | ||
acd1e437 | 1405 | static int xll_initted; |
aba5acdf | 1406 | |
b4b0b7d5 | 1407 | static void xll_init(void) |
aba5acdf SH |
1408 | { |
1409 | struct rtnl_handle rth; | |
acd1e437 | 1410 | |
d2468da0 SH |
1411 | if (rtnl_open(&rth, 0) < 0) |
1412 | exit(1); | |
1413 | ||
aba5acdf SH |
1414 | ll_init_map(&rth); |
1415 | rtnl_close(&rth); | |
1416 | xll_initted = 1; | |
1417 | } | |
1418 | ||
b4b0b7d5 | 1419 | static const char *xll_index_to_name(int index) |
aba5acdf SH |
1420 | { |
1421 | if (!xll_initted) | |
1422 | xll_init(); | |
1423 | return ll_index_to_name(index); | |
1424 | } | |
1425 | ||
b4b0b7d5 | 1426 | static int xll_name_to_index(const char *dev) |
aba5acdf SH |
1427 | { |
1428 | if (!xll_initted) | |
1429 | xll_init(); | |
1430 | return ll_name_to_index(dev); | |
1431 | } | |
1432 | ||
2d293212 DA |
1433 | void *parse_devcond(char *name) |
1434 | { | |
1435 | struct aafilter a = { .iface = 0 }; | |
1436 | struct aafilter *res; | |
1437 | ||
1438 | a.iface = xll_name_to_index(name); | |
1439 | if (a.iface == 0) { | |
1440 | char *end; | |
930d3f28 | 1441 | unsigned long n; |
2d293212 | 1442 | |
930d3f28 DA |
1443 | n = strtoul(name, &end, 0); |
1444 | if (!end || end == name || *end || n > UINT_MAX) | |
2d293212 | 1445 | return NULL; |
930d3f28 DA |
1446 | |
1447 | a.iface = n; | |
2d293212 DA |
1448 | } |
1449 | ||
1450 | res = malloc(sizeof(*res)); | |
1451 | *res = a; | |
1452 | ||
1453 | return res; | |
1454 | } | |
1455 | ||
7871f7db | 1456 | void *parse_hostcond(char *addr, bool is_port) |
aba5acdf SH |
1457 | { |
1458 | char *port = NULL; | |
1527a17e | 1459 | struct aafilter a = { .port = -1 }; |
aba5acdf | 1460 | struct aafilter *res; |
1527a17e | 1461 | int fam = preferred_family; |
9db7bf15 | 1462 | struct filter *f = ¤t_filter; |
aba5acdf | 1463 | |
1527a17e | 1464 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { |
aba5acdf | 1465 | char *p; |
acd1e437 | 1466 | |
aba5acdf SH |
1467 | a.addr.family = AF_UNIX; |
1468 | if (strncmp(addr, "unix:", 5) == 0) | |
acd1e437 | 1469 | addr += 5; |
aba5acdf SH |
1470 | p = strdup(addr); |
1471 | a.addr.bitlen = 8*strlen(p); | |
99bb68ff | 1472 | memcpy(a.addr.data, &p, sizeof(p)); |
9db7bf15 | 1473 | fam = AF_UNIX; |
aba5acdf SH |
1474 | goto out; |
1475 | } | |
1476 | ||
1527a17e | 1477 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { |
aba5acdf SH |
1478 | a.addr.family = AF_PACKET; |
1479 | a.addr.bitlen = 0; | |
1480 | if (strncmp(addr, "link:", 5) == 0) | |
acd1e437 | 1481 | addr += 5; |
aba5acdf SH |
1482 | port = strchr(addr, ':'); |
1483 | if (port) { | |
1484 | *port = 0; | |
1485 | if (port[1] && strcmp(port+1, "*")) { | |
1486 | if (get_integer(&a.port, port+1, 0)) { | |
1487 | if ((a.port = xll_name_to_index(port+1)) <= 0) | |
1488 | return NULL; | |
1489 | } | |
1490 | } | |
1491 | } | |
1492 | if (addr[0] && strcmp(addr, "*")) { | |
1493 | unsigned short tmp; | |
acd1e437 | 1494 | |
aba5acdf SH |
1495 | a.addr.bitlen = 32; |
1496 | if (ll_proto_a2n(&tmp, addr)) | |
1497 | return NULL; | |
1498 | a.addr.data[0] = ntohs(tmp); | |
1499 | } | |
9db7bf15 | 1500 | fam = AF_PACKET; |
aba5acdf SH |
1501 | goto out; |
1502 | } | |
1503 | ||
1527a17e | 1504 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { |
aba5acdf SH |
1505 | a.addr.family = AF_NETLINK; |
1506 | a.addr.bitlen = 0; | |
1507 | if (strncmp(addr, "netlink:", 8) == 0) | |
acd1e437 | 1508 | addr += 8; |
aba5acdf SH |
1509 | port = strchr(addr, ':'); |
1510 | if (port) { | |
1511 | *port = 0; | |
1512 | if (port[1] && strcmp(port+1, "*")) { | |
1513 | if (get_integer(&a.port, port+1, 0)) { | |
1514 | if (strcmp(port+1, "kernel") == 0) | |
1515 | a.port = 0; | |
1516 | else | |
1517 | return NULL; | |
1518 | } | |
1519 | } | |
1520 | } | |
1521 | if (addr[0] && strcmp(addr, "*")) { | |
1522 | a.addr.bitlen = 32; | |
b00daf6a | 1523 | if (nl_proto_a2n(&a.addr.data[0], addr) == -1) |
1524 | return NULL; | |
aba5acdf | 1525 | } |
9db7bf15 | 1526 | fam = AF_NETLINK; |
aba5acdf SH |
1527 | goto out; |
1528 | } | |
1529 | ||
1527a17e | 1530 | if (fam == AF_INET || !strncmp(addr, "inet:", 5)) { |
aba5acdf | 1531 | fam = AF_INET; |
1527a17e VK |
1532 | if (!strncmp(addr, "inet:", 5)) |
1533 | addr += 5; | |
1534 | } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) { | |
aba5acdf | 1535 | fam = AF_INET6; |
1527a17e VK |
1536 | if (!strncmp(addr, "inet6:", 6)) |
1537 | addr += 6; | |
aba5acdf SH |
1538 | } |
1539 | ||
1540 | /* URL-like literal [] */ | |
1541 | if (addr[0] == '[') { | |
1542 | addr++; | |
1543 | if ((port = strchr(addr, ']')) == NULL) | |
1544 | return NULL; | |
1545 | *port++ = 0; | |
1546 | } else if (addr[0] == '*') { | |
1547 | port = addr+1; | |
1548 | } else { | |
1549 | port = strrchr(strchr(addr, '/') ? : addr, ':'); | |
1550 | } | |
7871f7db VK |
1551 | |
1552 | if (is_port) | |
1553 | port = addr; | |
1554 | ||
aba5acdf | 1555 | if (port && *port) { |
7871f7db VK |
1556 | if (*port == ':') |
1557 | *port++ = 0; | |
1558 | ||
aba5acdf SH |
1559 | if (*port && *port != '*') { |
1560 | if (get_integer(&a.port, port, 0)) { | |
1561 | struct servent *se1 = NULL; | |
1562 | struct servent *se2 = NULL; | |
acd1e437 | 1563 | |
aba5acdf SH |
1564 | if (current_filter.dbs&(1<<UDP_DB)) |
1565 | se1 = getservbyname(port, UDP_PROTO); | |
1566 | if (current_filter.dbs&(1<<TCP_DB)) | |
1567 | se2 = getservbyname(port, TCP_PROTO); | |
1568 | if (se1 && se2 && se1->s_port != se2->s_port) { | |
1569 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
1570 | return NULL; | |
1571 | } | |
1572 | if (!se1) | |
1573 | se1 = se2; | |
1574 | if (se1) { | |
1575 | a.port = ntohs(se1->s_port); | |
1576 | } else { | |
1577 | struct scache *s; | |
acd1e437 | 1578 | |
aba5acdf SH |
1579 | for (s = rlist; s; s = s->next) { |
1580 | if ((s->proto == UDP_PROTO && | |
1581 | (current_filter.dbs&(1<<UDP_DB))) || | |
1582 | (s->proto == TCP_PROTO && | |
1583 | (current_filter.dbs&(1<<TCP_DB)))) { | |
1584 | if (s->name && strcmp(s->name, port) == 0) { | |
1585 | if (a.port > 0 && a.port != s->port) { | |
1586 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
1587 | return NULL; | |
1588 | } | |
1589 | a.port = s->port; | |
1590 | } | |
1591 | } | |
1592 | } | |
1593 | if (a.port <= 0) { | |
1594 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); | |
1595 | return NULL; | |
1596 | } | |
1597 | } | |
1598 | } | |
1599 | } | |
1600 | } | |
7871f7db | 1601 | if (!is_port && addr && *addr && *addr != '*') { |
aba5acdf SH |
1602 | if (get_prefix_1(&a.addr, addr, fam)) { |
1603 | if (get_dns_host(&a, addr, fam)) { | |
1604 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); | |
1605 | return NULL; | |
1606 | } | |
1607 | } | |
1608 | } | |
1609 | ||
9db7bf15 | 1610 | out: |
1527a17e | 1611 | if (fam != AF_UNSPEC) { |
e56a959e | 1612 | int states = f->states; |
1527a17e | 1613 | f->families = 0; |
9db7bf15 | 1614 | filter_af_set(f, fam); |
e56a959e | 1615 | filter_states_set(f, states); |
1527a17e | 1616 | } |
9db7bf15 | 1617 | |
aba5acdf SH |
1618 | res = malloc(sizeof(*res)); |
1619 | if (res) | |
1620 | memcpy(res, &a, sizeof(a)); | |
1621 | return res; | |
1622 | } | |
1623 | ||
8250bc9f VK |
1624 | static char *proto_name(int protocol) |
1625 | { | |
1626 | switch (protocol) { | |
235c4453 NA |
1627 | case 0: |
1628 | return "raw"; | |
8250bc9f VK |
1629 | case IPPROTO_UDP: |
1630 | return "udp"; | |
1631 | case IPPROTO_TCP: | |
1632 | return "tcp"; | |
1633 | case IPPROTO_DCCP: | |
1634 | return "dccp"; | |
1635 | } | |
1636 | ||
1637 | return "???"; | |
1638 | } | |
1639 | ||
055840f2 | 1640 | static void inet_stats_print(struct sockstat *s, int protocol) |
8250bc9f VK |
1641 | { |
1642 | char *buf = NULL; | |
1643 | ||
2d791bc8 | 1644 | sock_state_print(s, proto_name(protocol)); |
8250bc9f | 1645 | |
b217df10 VK |
1646 | inet_addr_print(&s->local, s->lport, s->iface); |
1647 | inet_addr_print(&s->remote, s->rport, 0); | |
8250bc9f | 1648 | |
8250bc9f VK |
1649 | if (show_proc_ctx || show_sock_ctx) { |
1650 | if (find_entry(s->ino, &buf, | |
1651 | (show_proc_ctx & show_sock_ctx) ? | |
1652 | PROC_SOCK_CTX : PROC_CTX) > 0) { | |
1653 | printf(" users:(%s)", buf); | |
1654 | free(buf); | |
1655 | } | |
1656 | } else if (show_users) { | |
1657 | if (find_entry(s->ino, &buf, USERS) > 0) { | |
1658 | printf(" users:(%s)", buf); | |
1659 | free(buf); | |
1660 | } | |
1661 | } | |
1662 | } | |
1663 | ||
055840f2 | 1664 | static int proc_parse_inet_addr(char *loc, char *rem, int family, struct |
acd1e437 | 1665 | sockstat * s) |
8250bc9f VK |
1666 | { |
1667 | s->local.family = s->remote.family = family; | |
1668 | if (family == AF_INET) { | |
acd1e437 SH |
1669 | sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport); |
1670 | sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport); | |
8250bc9f VK |
1671 | s->local.bytelen = s->remote.bytelen = 4; |
1672 | return 0; | |
1673 | } else { | |
1674 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
1675 | s->local.data, | |
1676 | s->local.data + 1, | |
1677 | s->local.data + 2, | |
1678 | s->local.data + 3, | |
1679 | &s->lport); | |
1680 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
1681 | s->remote.data, | |
1682 | s->remote.data + 1, | |
1683 | s->remote.data + 2, | |
1684 | s->remote.data + 3, | |
1685 | &s->rport); | |
1686 | s->local.bytelen = s->remote.bytelen = 16; | |
1687 | return 0; | |
1688 | } | |
1689 | return -1; | |
1690 | } | |
1691 | ||
1692 | static int proc_inet_split_line(char *line, char **loc, char **rem, char **data) | |
aba5acdf | 1693 | { |
aba5acdf | 1694 | char *p; |
ae665a52 | 1695 | |
aba5acdf SH |
1696 | if ((p = strchr(line, ':')) == NULL) |
1697 | return -1; | |
ae665a52 | 1698 | |
8250bc9f VK |
1699 | *loc = p+2; |
1700 | if ((p = strchr(*loc, ':')) == NULL) | |
aba5acdf | 1701 | return -1; |
ae665a52 | 1702 | |
8250bc9f VK |
1703 | p[5] = 0; |
1704 | *rem = p+6; | |
1705 | if ((p = strchr(*rem, ':')) == NULL) | |
aba5acdf | 1706 | return -1; |
8250bc9f | 1707 | |
aba5acdf | 1708 | p[5] = 0; |
8250bc9f VK |
1709 | *data = p+6; |
1710 | return 0; | |
1711 | } | |
ae665a52 | 1712 | |
8250bc9f VK |
1713 | static char *sprint_bw(char *buf, double bw) |
1714 | { | |
1715 | if (bw > 1000000.) | |
acd1e437 | 1716 | sprintf(buf, "%.1fM", bw / 1000000.); |
8250bc9f | 1717 | else if (bw > 1000.) |
acd1e437 | 1718 | sprintf(buf, "%.1fK", bw / 1000.); |
8250bc9f VK |
1719 | else |
1720 | sprintf(buf, "%g", bw); | |
aba5acdf | 1721 | |
8250bc9f VK |
1722 | return buf; |
1723 | } | |
ae665a52 | 1724 | |
8250bc9f VK |
1725 | static void tcp_stats_print(struct tcpstat *s) |
1726 | { | |
1727 | char b1[64]; | |
1728 | ||
1729 | if (s->has_ts_opt) | |
1730 | printf(" ts"); | |
1731 | if (s->has_sack_opt) | |
1732 | printf(" sack"); | |
1733 | if (s->has_ecn_opt) | |
1734 | printf(" ecn"); | |
1735 | if (s->has_ecnseen_opt) | |
1736 | printf(" ecnseen"); | |
1737 | if (s->has_fastopen_opt) | |
1738 | printf(" fastopen"); | |
d2055ea5 | 1739 | if (s->cong_alg[0]) |
8250bc9f VK |
1740 | printf(" %s", s->cong_alg); |
1741 | if (s->has_wscale_opt) | |
1742 | printf(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale); | |
1743 | if (s->rto) | |
1744 | printf(" rto:%g", s->rto); | |
1745 | if (s->backoff) | |
1746 | printf(" backoff:%u", s->backoff); | |
1747 | if (s->rtt) | |
1748 | printf(" rtt:%g/%g", s->rtt, s->rttvar); | |
1749 | if (s->ato) | |
1750 | printf(" ato:%g", s->ato); | |
1751 | ||
1752 | if (s->qack) | |
1753 | printf(" qack:%d", s->qack); | |
1754 | if (s->qack & 1) | |
1755 | printf(" bidir"); | |
1756 | ||
1757 | if (s->mss) | |
1758 | printf(" mss:%d", s->mss); | |
3bf5445c | 1759 | if (s->cwnd) |
8250bc9f VK |
1760 | printf(" cwnd:%d", s->cwnd); |
1761 | if (s->ssthresh) | |
1762 | printf(" ssthresh:%d", s->ssthresh); | |
1763 | ||
1a4dda71 ED |
1764 | if (s->bytes_acked) |
1765 | printf(" bytes_acked:%llu", s->bytes_acked); | |
1766 | if (s->bytes_received) | |
1767 | printf(" bytes_received:%llu", s->bytes_received); | |
ecb435ea CG |
1768 | if (s->segs_out) |
1769 | printf(" segs_out:%u", s->segs_out); | |
1770 | if (s->segs_in) | |
1771 | printf(" segs_in:%u", s->segs_in); | |
414aeec9 MKL |
1772 | if (s->data_segs_out) |
1773 | printf(" data_segs_out:%u", s->data_segs_out); | |
1774 | if (s->data_segs_in) | |
1775 | printf(" data_segs_in:%u", s->data_segs_in); | |
1a4dda71 | 1776 | |
8250bc9f VK |
1777 | if (s->dctcp && s->dctcp->enabled) { |
1778 | struct dctcpstat *dctcp = s->dctcp; | |
1779 | ||
3bf5445c | 1780 | printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)", |
8250bc9f VK |
1781 | dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn, |
1782 | dctcp->ab_tot); | |
1783 | } else if (s->dctcp) { | |
3bf5445c | 1784 | printf(" dctcp:fallback_mode"); |
8250bc9f VK |
1785 | } |
1786 | ||
2f0f9aef NC |
1787 | if (s->bbr_info) { |
1788 | __u64 bw; | |
1789 | ||
1790 | bw = s->bbr_info->bbr_bw_hi; | |
1791 | bw <<= 32; | |
1792 | bw |= s->bbr_info->bbr_bw_lo; | |
1793 | ||
1794 | printf(" bbr:(bw:%sbps,mrtt:%g", | |
1795 | sprint_bw(b1, bw * 8.0), | |
1796 | (double)s->bbr_info->bbr_min_rtt / 1000.0); | |
1797 | if (s->bbr_info->bbr_pacing_gain) | |
1798 | printf(",pacing_gain:%g", | |
1799 | (double)s->bbr_info->bbr_pacing_gain / 256.0); | |
1800 | if (s->bbr_info->bbr_cwnd_gain) | |
1801 | printf(",cwnd_gain:%g", | |
1802 | (double)s->bbr_info->bbr_cwnd_gain / 256.0); | |
1803 | printf(")"); | |
1804 | } | |
1805 | ||
8250bc9f VK |
1806 | if (s->send_bps) |
1807 | printf(" send %sbps", sprint_bw(b1, s->send_bps)); | |
1808 | if (s->lastsnd) | |
1809 | printf(" lastsnd:%u", s->lastsnd); | |
1810 | if (s->lastrcv) | |
1811 | printf(" lastrcv:%u", s->lastrcv); | |
1812 | if (s->lastack) | |
1813 | printf(" lastack:%u", s->lastack); | |
1814 | ||
1815 | if (s->pacing_rate) { | |
1816 | printf(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate)); | |
1817 | if (s->pacing_rate_max) | |
1818 | printf("/%sbps", sprint_bw(b1, | |
1819 | s->pacing_rate_max)); | |
1820 | } | |
1821 | ||
1822 | if (s->unacked) | |
1823 | printf(" unacked:%u", s->unacked); | |
1824 | if (s->retrans || s->retrans_total) | |
1825 | printf(" retrans:%u/%u", s->retrans, s->retrans_total); | |
1826 | if (s->lost) | |
1827 | printf(" lost:%u", s->lost); | |
055840f2 | 1828 | if (s->sacked && s->ss.state != SS_LISTEN) |
8250bc9f VK |
1829 | printf(" sacked:%u", s->sacked); |
1830 | if (s->fackets) | |
1831 | printf(" fackets:%u", s->fackets); | |
1832 | if (s->reordering != 3) | |
1833 | printf(" reordering:%d", s->reordering); | |
1834 | if (s->rcv_rtt) | |
1835 | printf(" rcv_rtt:%g", s->rcv_rtt); | |
1836 | if (s->rcv_space) | |
1837 | printf(" rcv_space:%d", s->rcv_space); | |
9e99e495 SH |
1838 | if (s->not_sent) |
1839 | printf(" notsent:%u", s->not_sent); | |
1840 | if (s->min_rtt) | |
1841 | printf(" minrtt:%g", s->min_rtt); | |
8250bc9f VK |
1842 | } |
1843 | ||
055840f2 VK |
1844 | static void tcp_timer_print(struct tcpstat *s) |
1845 | { | |
1846 | if (s->timer) { | |
1847 | if (s->timer > 4) | |
1848 | s->timer = 5; | |
1849 | printf(" timer:(%s,%s,%d)", | |
1850 | tmr_name[s->timer], | |
1851 | print_ms_timer(s->timeout), | |
1852 | s->retrans); | |
1853 | } | |
1854 | } | |
1855 | ||
8250bc9f VK |
1856 | static int tcp_show_line(char *line, const struct filter *f, int family) |
1857 | { | |
1858 | int rto = 0, ato = 0; | |
1859 | struct tcpstat s = {}; | |
1860 | char *loc, *rem, *data; | |
1861 | char opt[256]; | |
1862 | int n; | |
1863 | int hz = get_user_hz(); | |
1864 | ||
1865 | if (proc_inet_split_line(line, &loc, &rem, &data)) | |
1866 | return -1; | |
1867 | ||
1868 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
acd1e437 | 1869 | |
8250bc9f VK |
1870 | if (!(f->states & (1 << state))) |
1871 | return 0; | |
1872 | ||
055840f2 | 1873 | proc_parse_inet_addr(loc, rem, family, &s.ss); |
ae665a52 | 1874 | |
055840f2 | 1875 | if (f->f && run_ssfilter(f->f, &s.ss) == 0) |
aba5acdf | 1876 | return 0; |
ae665a52 | 1877 | |
aba5acdf | 1878 | opt[0] = 0; |
e7113c61 | 1879 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n", |
055840f2 VK |
1880 | &s.ss.state, &s.ss.wq, &s.ss.rq, |
1881 | &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, | |
1882 | &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, | |
1883 | &s.ssthresh, opt); | |
ae665a52 | 1884 | |
aba5acdf SH |
1885 | if (n < 17) |
1886 | opt[0] = 0; | |
ae665a52 | 1887 | |
aba5acdf | 1888 | if (n < 12) { |
8250bc9f | 1889 | rto = 0; |
aba5acdf SH |
1890 | s.cwnd = 2; |
1891 | s.ssthresh = -1; | |
8250bc9f | 1892 | ato = s.qack = 0; |
aba5acdf | 1893 | } |
ae665a52 | 1894 | |
8250bc9f VK |
1895 | s.retrans = s.timer != 1 ? s.probes : s.retrans; |
1896 | s.timeout = (s.timeout * 1000 + hz - 1) / hz; | |
1897 | s.ato = (double)ato / hz; | |
1898 | s.qack /= 2; | |
1899 | s.rto = (double)rto; | |
1900 | s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; | |
1901 | s.rto = s.rto != 3 * hz ? s.rto / hz : 0; | |
ae665a52 | 1902 | |
055840f2 VK |
1903 | inet_stats_print(&s.ss, IPPROTO_TCP); |
1904 | ||
1905 | if (show_options) | |
1906 | tcp_timer_print(&s); | |
116ac927 | 1907 | |
aba5acdf | 1908 | if (show_details) { |
f1b39e1b | 1909 | sock_details_print(&s.ss); |
aba5acdf SH |
1910 | if (opt[0]) |
1911 | printf(" opt:\"%s\"", opt); | |
1912 | } | |
aba5acdf | 1913 | |
8250bc9f VK |
1914 | if (show_tcpinfo) |
1915 | tcp_stats_print(&s); | |
1916 | ||
1917 | printf("\n"); | |
aba5acdf SH |
1918 | return 0; |
1919 | } | |
1920 | ||
ab01dbbb SH |
1921 | static int generic_record_read(FILE *fp, |
1922 | int (*worker)(char*, const struct filter *, int), | |
1923 | const struct filter *f, int fam) | |
aba5acdf | 1924 | { |
ab01dbbb | 1925 | char line[256]; |
aba5acdf | 1926 | |
ab01dbbb SH |
1927 | /* skip header */ |
1928 | if (fgets(line, sizeof(line), fp) == NULL) | |
aba5acdf | 1929 | goto outerr; |
ab01dbbb SH |
1930 | |
1931 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1932 | int n = strlen(line); | |
acd1e437 | 1933 | |
ab01dbbb SH |
1934 | if (n == 0 || line[n-1] != '\n') { |
1935 | errno = -EINVAL; | |
1936 | return -1; | |
aba5acdf | 1937 | } |
ab01dbbb | 1938 | line[n-1] = 0; |
aba5acdf | 1939 | |
ab01dbbb SH |
1940 | if (worker(line, f, fam) < 0) |
1941 | return 0; | |
1942 | } | |
aba5acdf | 1943 | outerr: |
ab01dbbb SH |
1944 | |
1945 | return ferror(fp) ? -1 : 0; | |
aba5acdf | 1946 | } |
ae665a52 | 1947 | |
51ff9f24 HFS |
1948 | static void print_skmeminfo(struct rtattr *tb[], int attrtype) |
1949 | { | |
1950 | const __u32 *skmeminfo; | |
db08bdb8 VK |
1951 | |
1952 | if (!tb[attrtype]) { | |
1953 | if (attrtype == INET_DIAG_SKMEMINFO) { | |
1954 | if (!tb[INET_DIAG_MEMINFO]) | |
1955 | return; | |
1956 | ||
1957 | const struct inet_diag_meminfo *minfo = | |
1958 | RTA_DATA(tb[INET_DIAG_MEMINFO]); | |
1959 | ||
1960 | printf(" mem:(r%u,w%u,f%u,t%u)", | |
1961 | minfo->idiag_rmem, | |
1962 | minfo->idiag_wmem, | |
1963 | minfo->idiag_fmem, | |
1964 | minfo->idiag_tmem); | |
1965 | } | |
51ff9f24 | 1966 | return; |
db08bdb8 VK |
1967 | } |
1968 | ||
51ff9f24 HFS |
1969 | skmeminfo = RTA_DATA(tb[attrtype]); |
1970 | ||
1971 | printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u", | |
1972 | skmeminfo[SK_MEMINFO_RMEM_ALLOC], | |
1973 | skmeminfo[SK_MEMINFO_RCVBUF], | |
1974 | skmeminfo[SK_MEMINFO_WMEM_ALLOC], | |
1975 | skmeminfo[SK_MEMINFO_SNDBUF], | |
1976 | skmeminfo[SK_MEMINFO_FWD_ALLOC], | |
1977 | skmeminfo[SK_MEMINFO_WMEM_QUEUED], | |
1978 | skmeminfo[SK_MEMINFO_OPTMEM]); | |
1979 | ||
1980 | if (RTA_PAYLOAD(tb[attrtype]) >= | |
1981 | (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32)) | |
1982 | printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]); | |
1983 | ||
6df9c7a0 ED |
1984 | if (RTA_PAYLOAD(tb[attrtype]) >= |
1985 | (SK_MEMINFO_DROPS + 1) * sizeof(__u32)) | |
1986 | printf(",d%u", skmeminfo[SK_MEMINFO_DROPS]); | |
1987 | ||
51ff9f24 HFS |
1988 | printf(")"); |
1989 | } | |
1990 | ||
8250bc9f VK |
1991 | #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt)) |
1992 | ||
5b816047 PE |
1993 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
1994 | struct rtattr *tb[]) | |
7d105b56 | 1995 | { |
b4b0b7d5 | 1996 | double rtt = 0; |
8250bc9f | 1997 | struct tcpstat s = {}; |
7d105b56 | 1998 | |
055840f2 VK |
1999 | s.ss.state = r->idiag_state; |
2000 | ||
db08bdb8 | 2001 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
7d105b56 | 2002 | |
351efcde | 2003 | if (tb[INET_DIAG_INFO]) { |
05e18118 | 2004 | struct tcp_info *info; |
351efcde | 2005 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
05e18118 SH |
2006 | |
2007 | /* workaround for older kernels with less fields */ | |
2008 | if (len < sizeof(*info)) { | |
2009 | info = alloca(sizeof(*info)); | |
351efcde | 2010 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
656e8fdd | 2011 | memset((char *)info + len, 0, sizeof(*info) - len); |
05e18118 | 2012 | } else |
351efcde | 2013 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
05e18118 | 2014 | |
b4b0b7d5 | 2015 | if (show_options) { |
8250bc9f VK |
2016 | s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS); |
2017 | s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK); | |
2018 | s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN); | |
2019 | s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN); | |
2020 | s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA); | |
b4b0b7d5 | 2021 | } |
52d5ac3f | 2022 | |
d2055ea5 ED |
2023 | if (tb[INET_DIAG_CONG]) |
2024 | strncpy(s.cong_alg, | |
2025 | rta_getattr_str(tb[INET_DIAG_CONG]), | |
2026 | sizeof(s.cong_alg) - 1); | |
8250bc9f VK |
2027 | |
2028 | if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) { | |
2029 | s.has_wscale_opt = true; | |
2030 | s.snd_wscale = info->tcpi_snd_wscale; | |
2031 | s.rcv_wscale = info->tcpi_rcv_wscale; | |
2032 | } | |
ea8fc104 | 2033 | |
7d105b56 | 2034 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
8250bc9f VK |
2035 | s.rto = (double)info->tcpi_rto / 1000; |
2036 | ||
2037 | s.backoff = info->tcpi_backoff; | |
2038 | s.rtt = (double)info->tcpi_rtt / 1000; | |
2039 | s.rttvar = (double)info->tcpi_rttvar / 1000; | |
11ba90fc | 2040 | s.ato = (double)info->tcpi_ato / 1000; |
8250bc9f VK |
2041 | s.mss = info->tcpi_snd_mss; |
2042 | s.rcv_space = info->tcpi_rcv_space; | |
2043 | s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000; | |
2044 | s.lastsnd = info->tcpi_last_data_sent; | |
2045 | s.lastrcv = info->tcpi_last_data_recv; | |
2046 | s.lastack = info->tcpi_last_ack_recv; | |
2047 | s.unacked = info->tcpi_unacked; | |
2048 | s.retrans = info->tcpi_retrans; | |
2049 | s.retrans_total = info->tcpi_total_retrans; | |
2050 | s.lost = info->tcpi_lost; | |
2051 | s.sacked = info->tcpi_sacked; | |
2052 | s.reordering = info->tcpi_reordering; | |
2053 | s.rcv_space = info->tcpi_rcv_space; | |
2054 | s.cwnd = info->tcpi_snd_cwnd; | |
2055 | ||
7d105b56 | 2056 | if (info->tcpi_snd_ssthresh < 0xFFFF) |
8250bc9f | 2057 | s.ssthresh = info->tcpi_snd_ssthresh; |
52d5ac3f | 2058 | |
b4b0b7d5 | 2059 | rtt = (double) info->tcpi_rtt; |
351efcde | 2060 | if (tb[INET_DIAG_VEGASINFO]) { |
05e18118 | 2061 | const struct tcpvegas_info *vinfo |
351efcde | 2062 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
7d105b56 | 2063 | |
ae665a52 | 2064 | if (vinfo->tcpv_enabled && |
8250bc9f | 2065 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
ea8fc104 | 2066 | rtt = vinfo->tcpv_rtt; |
b4b0b7d5 SH |
2067 | } |
2068 | ||
907e1aca | 2069 | if (tb[INET_DIAG_DCTCPINFO]) { |
8250bc9f VK |
2070 | struct dctcpstat *dctcp = malloc(sizeof(struct |
2071 | dctcpstat)); | |
2072 | ||
907e1aca DB |
2073 | const struct tcp_dctcp_info *dinfo |
2074 | = RTA_DATA(tb[INET_DIAG_DCTCPINFO]); | |
2075 | ||
8250bc9f VK |
2076 | dctcp->enabled = !!dinfo->dctcp_enabled; |
2077 | dctcp->ce_state = dinfo->dctcp_ce_state; | |
2078 | dctcp->alpha = dinfo->dctcp_alpha; | |
2079 | dctcp->ab_ecn = dinfo->dctcp_ab_ecn; | |
2080 | dctcp->ab_tot = dinfo->dctcp_ab_tot; | |
2081 | s.dctcp = dctcp; | |
907e1aca DB |
2082 | } |
2083 | ||
2f0f9aef NC |
2084 | if (tb[INET_DIAG_BBRINFO]) { |
2085 | const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]); | |
2086 | int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]), | |
2087 | sizeof(*s.bbr_info)); | |
2088 | ||
2089 | s.bbr_info = calloc(1, sizeof(*s.bbr_info)); | |
2090 | if (s.bbr_info && bbr_info) | |
2091 | memcpy(s.bbr_info, bbr_info, len); | |
2092 | } | |
2093 | ||
b4b0b7d5 | 2094 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { |
8250bc9f VK |
2095 | s.send_bps = (double) info->tcpi_snd_cwnd * |
2096 | (double)info->tcpi_snd_mss * 8000000. / rtt; | |
7d105b56 | 2097 | } |
b4b0b7d5 | 2098 | |
eb6028b2 | 2099 | if (info->tcpi_pacing_rate && |
8250bc9f VK |
2100 | info->tcpi_pacing_rate != ~0ULL) { |
2101 | s.pacing_rate = info->tcpi_pacing_rate * 8.; | |
eb6028b2 ED |
2102 | |
2103 | if (info->tcpi_max_pacing_rate && | |
8250bc9f VK |
2104 | info->tcpi_max_pacing_rate != ~0ULL) |
2105 | s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.; | |
eb6028b2 | 2106 | } |
1a4dda71 ED |
2107 | s.bytes_acked = info->tcpi_bytes_acked; |
2108 | s.bytes_received = info->tcpi_bytes_received; | |
ecb435ea CG |
2109 | s.segs_out = info->tcpi_segs_out; |
2110 | s.segs_in = info->tcpi_segs_in; | |
414aeec9 MKL |
2111 | s.data_segs_out = info->tcpi_data_segs_out; |
2112 | s.data_segs_in = info->tcpi_data_segs_in; | |
9e99e495 | 2113 | s.not_sent = info->tcpi_notsent_bytes; |
d9ba887e ED |
2114 | if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U) |
2115 | s.min_rtt = (double) info->tcpi_min_rtt / 1000; | |
8250bc9f | 2116 | tcp_stats_print(&s); |
92de1c2c | 2117 | free(s.dctcp); |
2f0f9aef | 2118 | free(s.bbr_info); |
77a8ca81 | 2119 | } |
77a8ca81 PE |
2120 | } |
2121 | ||
82d73ea0 | 2122 | static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s) |
aba5acdf | 2123 | { |
acd1e437 | 2124 | struct rtattr *tb[INET_DIAG_MAX+1]; |
351efcde | 2125 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
aba5acdf | 2126 | |
acd1e437 | 2127 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), |
5b816047 PE |
2128 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
2129 | ||
82d73ea0 DA |
2130 | s->state = r->idiag_state; |
2131 | s->local.family = s->remote.family = r->idiag_family; | |
2132 | s->lport = ntohs(r->id.idiag_sport); | |
2133 | s->rport = ntohs(r->id.idiag_dport); | |
2134 | s->wq = r->idiag_wqueue; | |
2135 | s->rq = r->idiag_rqueue; | |
2136 | s->ino = r->idiag_inode; | |
2137 | s->uid = r->idiag_uid; | |
2138 | s->iface = r->id.idiag_if; | |
2139 | s->sk = cookie_sk_get(&r->id.idiag_cookie[0]); | |
2140 | ||
2141 | if (s->local.family == AF_INET) | |
2142 | s->local.bytelen = s->remote.bytelen = 4; | |
2143 | else | |
2144 | s->local.bytelen = s->remote.bytelen = 16; | |
8250bc9f | 2145 | |
82d73ea0 DA |
2146 | memcpy(s->local.data, r->id.idiag_src, s->local.bytelen); |
2147 | memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen); | |
2148 | } | |
aba5acdf | 2149 | |
82d73ea0 DA |
2150 | static int inet_show_sock(struct nlmsghdr *nlh, |
2151 | struct sockstat *s, | |
2152 | int protocol) | |
2153 | { | |
2154 | struct rtattr *tb[INET_DIAG_MAX+1]; | |
2155 | struct inet_diag_msg *r = NLMSG_DATA(nlh); | |
2156 | ||
2157 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), | |
2158 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
aba5acdf | 2159 | |
6885e3bf CG |
2160 | if (tb[INET_DIAG_PROTOCOL]) |
2161 | protocol = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]); | |
2162 | ||
82d73ea0 | 2163 | inet_stats_print(s, protocol); |
116ac927 | 2164 | |
055840f2 VK |
2165 | if (show_options) { |
2166 | struct tcpstat t = {}; | |
2167 | ||
2168 | t.timer = r->idiag_timer; | |
2169 | t.timeout = r->idiag_expires; | |
2170 | t.retrans = r->idiag_retrans; | |
2171 | tcp_timer_print(&t); | |
2172 | } | |
2173 | ||
aba5acdf | 2174 | if (show_details) { |
82d73ea0 DA |
2175 | sock_details_print(s); |
2176 | if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) { | |
f32dc746 | 2177 | unsigned char v6only; |
acd1e437 | 2178 | |
f32dc746 PS |
2179 | v6only = *(__u8 *)RTA_DATA(tb[INET_DIAG_SKV6ONLY]); |
2180 | printf(" v6only:%u", v6only); | |
2181 | } | |
5b816047 PE |
2182 | if (tb[INET_DIAG_SHUTDOWN]) { |
2183 | unsigned char mask; | |
acd1e437 | 2184 | |
5b816047 PE |
2185 | mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]); |
2186 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
2187 | } | |
aba5acdf | 2188 | } |
8250bc9f | 2189 | |
aba5acdf | 2190 | if (show_mem || show_tcpinfo) { |
7d105b56 | 2191 | printf("\n\t"); |
5b816047 | 2192 | tcp_show_info(nlh, r, tb); |
aba5acdf | 2193 | } |
7d105b56 | 2194 | |
aba5acdf | 2195 | printf("\n"); |
aba5acdf | 2196 | return 0; |
aba5acdf SH |
2197 | } |
2198 | ||
746a695f | 2199 | static int tcpdiag_send(int fd, int protocol, struct filter *f) |
aba5acdf | 2200 | { |
d17b136f | 2201 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
aba5acdf SH |
2202 | struct { |
2203 | struct nlmsghdr nlh; | |
351efcde | 2204 | struct inet_diag_req r; |
d17b136f PS |
2205 | } req = { |
2206 | .nlh.nlmsg_len = sizeof(req), | |
2207 | .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST, | |
2208 | .nlh.nlmsg_seq = MAGIC_SEQ, | |
2209 | .r.idiag_family = AF_INET, | |
2210 | .r.idiag_states = f->states, | |
2211 | }; | |
aba5acdf SH |
2212 | char *bc = NULL; |
2213 | int bclen; | |
2214 | struct msghdr msg; | |
2215 | struct rtattr rta; | |
aba5acdf | 2216 | struct iovec iov[3]; |
376fb868 | 2217 | int iovlen = 1; |
aba5acdf | 2218 | |
346f8ca8 PE |
2219 | if (protocol == IPPROTO_UDP) |
2220 | return -1; | |
2221 | ||
3fe5b534 PE |
2222 | if (protocol == IPPROTO_TCP) |
2223 | req.nlh.nlmsg_type = TCPDIAG_GETSOCK; | |
2224 | else | |
2225 | req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; | |
910b0397 | 2226 | if (show_mem) { |
ae665a52 | 2227 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
910b0397 SW |
2228 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
2229 | } | |
b4b0b7d5 | 2230 | |
7d105b56 | 2231 | if (show_tcpinfo) { |
351efcde SH |
2232 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
2233 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
2234 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
7d105b56 | 2235 | } |
aba5acdf | 2236 | |
ae665a52 SH |
2237 | iov[0] = (struct iovec){ |
2238 | .iov_base = &req, | |
2239 | .iov_len = sizeof(req) | |
ea8fc104 | 2240 | }; |
aba5acdf SH |
2241 | if (f->f) { |
2242 | bclen = ssfilter_bytecompile(f->f, &bc); | |
376fb868 DA |
2243 | if (bclen) { |
2244 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
2245 | rta.rta_len = RTA_LENGTH(bclen); | |
2246 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
2247 | iov[2] = (struct iovec){ bc, bclen }; | |
2248 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
2249 | iovlen = 3; | |
2250 | } | |
aba5acdf SH |
2251 | } |
2252 | ||
2253 | msg = (struct msghdr) { | |
acd1e437 | 2254 | .msg_name = (void *)&nladdr, |
ea8fc104 | 2255 | .msg_namelen = sizeof(nladdr), |
ae665a52 | 2256 | .msg_iov = iov, |
376fb868 | 2257 | .msg_iovlen = iovlen, |
aba5acdf SH |
2258 | }; |
2259 | ||
930a75f9 ED |
2260 | if (sendmsg(fd, &msg, 0) < 0) { |
2261 | close(fd); | |
aba5acdf | 2262 | return -1; |
930a75f9 | 2263 | } |
aba5acdf | 2264 | |
746a695f PE |
2265 | return 0; |
2266 | } | |
2267 | ||
886d19d6 PE |
2268 | static int sockdiag_send(int family, int fd, int protocol, struct filter *f) |
2269 | { | |
d17b136f | 2270 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
5fb421d4 | 2271 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
886d19d6 PE |
2272 | char *bc = NULL; |
2273 | int bclen; | |
2274 | struct msghdr msg; | |
2275 | struct rtattr rta; | |
2276 | struct iovec iov[3]; | |
376fb868 | 2277 | int iovlen = 1; |
886d19d6 PE |
2278 | |
2279 | if (family == PF_UNSPEC) | |
2280 | return tcpdiag_send(fd, protocol, f); | |
2281 | ||
886d19d6 PE |
2282 | memset(&req.r, 0, sizeof(req.r)); |
2283 | req.r.sdiag_family = family; | |
2284 | req.r.sdiag_protocol = protocol; | |
2285 | req.r.idiag_states = f->states; | |
2286 | if (show_mem) { | |
2287 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); | |
2288 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); | |
2289 | } | |
2290 | ||
2291 | if (show_tcpinfo) { | |
2292 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); | |
2293 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
2294 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
2295 | } | |
2296 | ||
2297 | iov[0] = (struct iovec){ | |
2298 | .iov_base = &req, | |
2299 | .iov_len = sizeof(req) | |
2300 | }; | |
2301 | if (f->f) { | |
2302 | bclen = ssfilter_bytecompile(f->f, &bc); | |
376fb868 DA |
2303 | if (bclen) { |
2304 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
2305 | rta.rta_len = RTA_LENGTH(bclen); | |
2306 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
2307 | iov[2] = (struct iovec){ bc, bclen }; | |
2308 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
2309 | iovlen = 3; | |
2310 | } | |
886d19d6 PE |
2311 | } |
2312 | ||
2313 | msg = (struct msghdr) { | |
acd1e437 | 2314 | .msg_name = (void *)&nladdr, |
886d19d6 PE |
2315 | .msg_namelen = sizeof(nladdr), |
2316 | .msg_iov = iov, | |
376fb868 | 2317 | .msg_iovlen = iovlen, |
886d19d6 PE |
2318 | }; |
2319 | ||
2320 | if (sendmsg(fd, &msg, 0) < 0) { | |
2321 | close(fd); | |
2322 | return -1; | |
2323 | } | |
2324 | ||
2325 | return 0; | |
2326 | } | |
2327 | ||
486ccd99 VK |
2328 | struct inet_diag_arg { |
2329 | struct filter *f; | |
2330 | int protocol; | |
fb2594c1 | 2331 | struct rtnl_handle *rth; |
486ccd99 | 2332 | }; |
aba5acdf | 2333 | |
b38e7409 | 2334 | static int kill_inet_sock(struct nlmsghdr *h, void *arg) |
fb2594c1 LC |
2335 | { |
2336 | struct inet_diag_msg *d = NLMSG_DATA(h); | |
2337 | struct inet_diag_arg *diag_arg = arg; | |
2338 | struct rtnl_handle *rth = diag_arg->rth; | |
acd1e437 | 2339 | |
fb2594c1 LC |
2340 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
2341 | ||
2342 | req.nlh.nlmsg_type = SOCK_DESTROY; | |
2343 | req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; | |
2344 | req.nlh.nlmsg_seq = ++rth->seq; | |
2345 | req.r.sdiag_family = d->idiag_family; | |
2346 | req.r.sdiag_protocol = diag_arg->protocol; | |
2347 | req.r.id = d->id; | |
2348 | ||
2349 | return rtnl_talk(rth, &req.nlh, NULL, 0); | |
2350 | } | |
2351 | ||
486ccd99 VK |
2352 | static int show_one_inet_sock(const struct sockaddr_nl *addr, |
2353 | struct nlmsghdr *h, void *arg) | |
2354 | { | |
2355 | int err; | |
2356 | struct inet_diag_arg *diag_arg = arg; | |
2357 | struct inet_diag_msg *r = NLMSG_DATA(h); | |
82d73ea0 | 2358 | struct sockstat s = {}; |
aba5acdf | 2359 | |
486ccd99 VK |
2360 | if (!(diag_arg->f->families & (1 << r->idiag_family))) |
2361 | return 0; | |
82d73ea0 DA |
2362 | |
2363 | parse_diag_msg(h, &s); | |
2364 | ||
2365 | if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0) | |
2366 | return 0; | |
2367 | ||
b38e7409 | 2368 | if (diag_arg->f->kill && kill_inet_sock(h, arg) != 0) { |
fb2594c1 LC |
2369 | if (errno == EOPNOTSUPP || errno == ENOENT) { |
2370 | /* Socket can't be closed, or is already closed. */ | |
2371 | return 0; | |
2372 | } else { | |
2373 | perror("SOCK_DESTROY answers"); | |
2374 | return -1; | |
2375 | } | |
2376 | } | |
82d73ea0 DA |
2377 | |
2378 | err = inet_show_sock(h, &s, diag_arg->protocol); | |
2379 | if (err < 0) | |
486ccd99 | 2380 | return err; |
aba5acdf | 2381 | |
486ccd99 VK |
2382 | return 0; |
2383 | } | |
886d19d6 | 2384 | |
486ccd99 VK |
2385 | static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) |
2386 | { | |
2387 | int err = 0; | |
fb2594c1 | 2388 | struct rtnl_handle rth, rth2; |
486ccd99 VK |
2389 | int family = PF_INET; |
2390 | struct inet_diag_arg arg = { .f = f, .protocol = protocol }; | |
886d19d6 | 2391 | |
486ccd99 VK |
2392 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
2393 | return -1; | |
fb2594c1 LC |
2394 | |
2395 | if (f->kill) { | |
2396 | if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) { | |
2397 | rtnl_close(&rth); | |
2398 | return -1; | |
2399 | } | |
2400 | arg.rth = &rth2; | |
2401 | } | |
2402 | ||
486ccd99 VK |
2403 | rth.dump = MAGIC_SEQ; |
2404 | rth.dump_fp = dump_fp; | |
518af1e0 ED |
2405 | if (preferred_family == PF_INET6) |
2406 | family = PF_INET6; | |
886d19d6 | 2407 | |
486ccd99 VK |
2408 | again: |
2409 | if ((err = sockdiag_send(family, rth.fd, protocol, f))) | |
2410 | goto Exit; | |
aba5acdf | 2411 | |
486ccd99 VK |
2412 | if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) { |
2413 | if (family != PF_UNSPEC) { | |
2414 | family = PF_UNSPEC; | |
2415 | goto again; | |
aba5acdf | 2416 | } |
486ccd99 | 2417 | goto Exit; |
aba5acdf | 2418 | } |
518af1e0 | 2419 | if (family == PF_INET && preferred_family != PF_INET) { |
886d19d6 PE |
2420 | family = PF_INET6; |
2421 | goto again; | |
2422 | } | |
2423 | ||
486ccd99 VK |
2424 | Exit: |
2425 | rtnl_close(&rth); | |
fb2594c1 LC |
2426 | if (arg.rth) |
2427 | rtnl_close(arg.rth); | |
486ccd99 | 2428 | return err; |
aba5acdf SH |
2429 | } |
2430 | ||
ab01dbbb | 2431 | static int tcp_show_netlink_file(struct filter *f) |
aba5acdf SH |
2432 | { |
2433 | FILE *fp; | |
e557212e | 2434 | char buf[16384]; |
aba5acdf SH |
2435 | |
2436 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { | |
2437 | perror("fopen($TCPDIAG_FILE)"); | |
2438 | return -1; | |
2439 | } | |
2440 | ||
2441 | while (1) { | |
2442 | int status, err; | |
acd1e437 | 2443 | struct nlmsghdr *h = (struct nlmsghdr *)buf; |
82d73ea0 | 2444 | struct sockstat s = {}; |
aba5acdf SH |
2445 | |
2446 | status = fread(buf, 1, sizeof(*h), fp); | |
2447 | if (status < 0) { | |
2448 | perror("Reading header from $TCPDIAG_FILE"); | |
2449 | return -1; | |
2450 | } | |
2451 | if (status != sizeof(*h)) { | |
2452 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
2453 | return -1; | |
2454 | } | |
2455 | ||
2456 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); | |
2457 | ||
2458 | if (status < 0) { | |
2459 | perror("Reading $TCPDIAG_FILE"); | |
2460 | return -1; | |
2461 | } | |
2462 | if (status + sizeof(*h) < h->nlmsg_len) { | |
2463 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
2464 | return -1; | |
2465 | } | |
2466 | ||
2467 | /* The only legal exit point */ | |
2468 | if (h->nlmsg_type == NLMSG_DONE) | |
2469 | return 0; | |
2470 | ||
2471 | if (h->nlmsg_type == NLMSG_ERROR) { | |
acd1e437 SH |
2472 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
2473 | ||
aba5acdf SH |
2474 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
2475 | fprintf(stderr, "ERROR truncated\n"); | |
2476 | } else { | |
2477 | errno = -err->error; | |
2478 | perror("TCPDIAG answered"); | |
2479 | } | |
2480 | return -1; | |
2481 | } | |
2482 | ||
82d73ea0 DA |
2483 | parse_diag_msg(h, &s); |
2484 | ||
2485 | if (f && f->f && run_ssfilter(f->f, &s) == 0) | |
2486 | continue; | |
2487 | ||
2488 | err = inet_show_sock(h, &s, IPPROTO_TCP); | |
aba5acdf SH |
2489 | if (err < 0) |
2490 | return err; | |
2491 | } | |
2492 | } | |
2493 | ||
ab01dbbb | 2494 | static int tcp_show(struct filter *f, int socktype) |
aba5acdf | 2495 | { |
ab01dbbb | 2496 | FILE *fp = NULL; |
aba5acdf SH |
2497 | char *buf = NULL; |
2498 | int bufsize = 64*1024; | |
2499 | ||
1527a17e VK |
2500 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
2501 | return 0; | |
2502 | ||
aba5acdf SH |
2503 | dg_proto = TCP_PROTO; |
2504 | ||
2505 | if (getenv("TCPDIAG_FILE")) | |
2506 | return tcp_show_netlink_file(f); | |
2507 | ||
2508 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") | |
3fe5b534 | 2509 | && inet_show_netlink(f, NULL, socktype) == 0) |
aba5acdf SH |
2510 | return 0; |
2511 | ||
2512 | /* Sigh... We have to parse /proc/net/tcp... */ | |
2513 | ||
ab01dbbb | 2514 | |
aba5acdf SH |
2515 | /* Estimate amount of sockets and try to allocate |
2516 | * huge buffer to read all the table at one read. | |
2517 | * Limit it by 16MB though. The assumption is: as soon as | |
2518 | * kernel was able to hold information about N connections, | |
2519 | * it is able to give us some memory for snapshot. | |
2520 | */ | |
2521 | if (1) { | |
a221d621 BL |
2522 | get_slabstat(&slabstat); |
2523 | ||
aba5acdf | 2524 | int guess = slabstat.socks+slabstat.tcp_syns; |
acd1e437 | 2525 | |
aba5acdf SH |
2526 | if (f->states&(1<<SS_TIME_WAIT)) |
2527 | guess += slabstat.tcp_tws; | |
2528 | if (guess > (16*1024*1024)/128) | |
2529 | guess = (16*1024*1024)/128; | |
2530 | guess *= 128; | |
2531 | if (guess > bufsize) | |
2532 | bufsize = guess; | |
2533 | } | |
2534 | while (bufsize >= 64*1024) { | |
2535 | if ((buf = malloc(bufsize)) != NULL) | |
2536 | break; | |
2537 | bufsize /= 2; | |
2538 | } | |
2539 | if (buf == NULL) { | |
2540 | errno = ENOMEM; | |
2541 | return -1; | |
2542 | } | |
2543 | ||
2544 | if (f->families & (1<<AF_INET)) { | |
69cae645 | 2545 | if ((fp = net_tcp_open()) == NULL) |
aba5acdf | 2546 | goto outerr; |
ab01dbbb SH |
2547 | |
2548 | setbuffer(fp, buf, bufsize); | |
2549 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) | |
aba5acdf | 2550 | goto outerr; |
ab01dbbb | 2551 | fclose(fp); |
aba5acdf SH |
2552 | } |
2553 | ||
2554 | if ((f->families & (1<<AF_INET6)) && | |
69cae645 | 2555 | (fp = net_tcp6_open()) != NULL) { |
ab01dbbb SH |
2556 | setbuffer(fp, buf, bufsize); |
2557 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) | |
aba5acdf | 2558 | goto outerr; |
ab01dbbb | 2559 | fclose(fp); |
aba5acdf SH |
2560 | } |
2561 | ||
2562 | free(buf); | |
2563 | return 0; | |
2564 | ||
2565 | outerr: | |
2566 | do { | |
2567 | int saved_errno = errno; | |
acd1e437 | 2568 | |
92de1c2c | 2569 | free(buf); |
ab01dbbb SH |
2570 | if (fp) |
2571 | fclose(fp); | |
aba5acdf SH |
2572 | errno = saved_errno; |
2573 | return -1; | |
2574 | } while (0); | |
2575 | } | |
2576 | ||
2577 | ||
d1f28cf1 | 2578 | static int dgram_show_line(char *line, const struct filter *f, int family) |
aba5acdf | 2579 | { |
055840f2 | 2580 | struct sockstat s = {}; |
aba5acdf SH |
2581 | char *loc, *rem, *data; |
2582 | char opt[256]; | |
2583 | int n; | |
aba5acdf | 2584 | |
8250bc9f | 2585 | if (proc_inet_split_line(line, &loc, &rem, &data)) |
aba5acdf | 2586 | return -1; |
aba5acdf | 2587 | |
8250bc9f | 2588 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
acd1e437 | 2589 | |
8250bc9f VK |
2590 | if (!(f->states & (1 << state))) |
2591 | return 0; | |
aba5acdf | 2592 | |
8250bc9f | 2593 | proc_parse_inet_addr(loc, rem, family, &s); |
aba5acdf SH |
2594 | |
2595 | if (f->f && run_ssfilter(f->f, &s) == 0) | |
2596 | return 0; | |
2597 | ||
2598 | opt[0] = 0; | |
e7113c61 | 2599 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
aba5acdf SH |
2600 | &s.state, &s.wq, &s.rq, |
2601 | &s.uid, &s.ino, | |
2602 | &s.refcnt, &s.sk, opt); | |
2603 | ||
2604 | if (n < 9) | |
2605 | opt[0] = 0; | |
2606 | ||
235c4453 | 2607 | inet_stats_print(&s, dg_proto == UDP_PROTO ? IPPROTO_UDP : 0); |
aba5acdf | 2608 | |
f1b39e1b VK |
2609 | if (show_details && opt[0]) |
2610 | printf(" opt:\"%s\"", opt); | |
aba5acdf | 2611 | |
8250bc9f | 2612 | printf("\n"); |
aba5acdf SH |
2613 | return 0; |
2614 | } | |
2615 | ||
d1f28cf1 | 2616 | static int udp_show(struct filter *f) |
aba5acdf | 2617 | { |
ab01dbbb | 2618 | FILE *fp = NULL; |
aba5acdf | 2619 | |
1527a17e VK |
2620 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
2621 | return 0; | |
2622 | ||
ace5cb31 VK |
2623 | dg_proto = UDP_PROTO; |
2624 | ||
346f8ca8 PE |
2625 | if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") |
2626 | && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) | |
2627 | return 0; | |
2628 | ||
aba5acdf | 2629 | if (f->families&(1<<AF_INET)) { |
69cae645 | 2630 | if ((fp = net_udp_open()) == NULL) |
aba5acdf | 2631 | goto outerr; |
ab01dbbb | 2632 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 2633 | goto outerr; |
ab01dbbb | 2634 | fclose(fp); |
aba5acdf SH |
2635 | } |
2636 | ||
2637 | if ((f->families&(1<<AF_INET6)) && | |
69cae645 | 2638 | (fp = net_udp6_open()) != NULL) { |
ab01dbbb | 2639 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 2640 | goto outerr; |
ab01dbbb | 2641 | fclose(fp); |
aba5acdf SH |
2642 | } |
2643 | return 0; | |
2644 | ||
2645 | outerr: | |
2646 | do { | |
2647 | int saved_errno = errno; | |
acd1e437 | 2648 | |
ab01dbbb SH |
2649 | if (fp) |
2650 | fclose(fp); | |
aba5acdf SH |
2651 | errno = saved_errno; |
2652 | return -1; | |
2653 | } while (0); | |
2654 | } | |
2655 | ||
d1f28cf1 | 2656 | static int raw_show(struct filter *f) |
aba5acdf | 2657 | { |
ab01dbbb | 2658 | FILE *fp = NULL; |
aba5acdf | 2659 | |
1527a17e VK |
2660 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
2661 | return 0; | |
2662 | ||
aba5acdf SH |
2663 | dg_proto = RAW_PROTO; |
2664 | ||
2665 | if (f->families&(1<<AF_INET)) { | |
69cae645 | 2666 | if ((fp = net_raw_open()) == NULL) |
aba5acdf | 2667 | goto outerr; |
ab01dbbb | 2668 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 2669 | goto outerr; |
ab01dbbb | 2670 | fclose(fp); |
aba5acdf SH |
2671 | } |
2672 | ||
2673 | if ((f->families&(1<<AF_INET6)) && | |
69cae645 | 2674 | (fp = net_raw6_open()) != NULL) { |
ab01dbbb | 2675 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 2676 | goto outerr; |
ab01dbbb | 2677 | fclose(fp); |
aba5acdf SH |
2678 | } |
2679 | return 0; | |
2680 | ||
2681 | outerr: | |
2682 | do { | |
2683 | int saved_errno = errno; | |
acd1e437 | 2684 | |
ab01dbbb SH |
2685 | if (fp) |
2686 | fclose(fp); | |
aba5acdf SH |
2687 | errno = saved_errno; |
2688 | return -1; | |
2689 | } while (0); | |
2690 | } | |
2691 | ||
aba5acdf SH |
2692 | int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, |
2693 | SS_ESTABLISHED, SS_CLOSING }; | |
2694 | ||
ec4d0d8a | 2695 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) |
aba5acdf | 2696 | |
ec4d0d8a | 2697 | static void unix_list_free(struct sockstat *list) |
aba5acdf SH |
2698 | { |
2699 | while (list) { | |
ec4d0d8a | 2700 | struct sockstat *s = list; |
ec4d0d8a | 2701 | |
aba5acdf | 2702 | list = list->next; |
99bb68ff | 2703 | free(s->name); |
aba5acdf SH |
2704 | free(s); |
2705 | } | |
2706 | } | |
2707 | ||
30b669d7 MY |
2708 | static const char *unix_netid_name(int type) |
2709 | { | |
2710 | const char *netid; | |
2711 | ||
2712 | switch (type) { | |
2713 | case SOCK_STREAM: | |
2714 | netid = "u_str"; | |
2715 | break; | |
2716 | case SOCK_SEQPACKET: | |
2717 | netid = "u_seq"; | |
2718 | break; | |
2719 | case SOCK_DGRAM: | |
2720 | default: | |
2721 | netid = "u_dgr"; | |
2722 | break; | |
2723 | } | |
2724 | return netid; | |
2725 | } | |
2726 | ||
ec4d0d8a | 2727 | static bool unix_type_skip(struct sockstat *s, struct filter *f) |
bf4ceee6 VK |
2728 | { |
2729 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) | |
2730 | return true; | |
2731 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) | |
2732 | return true; | |
2733 | if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB))) | |
2734 | return true; | |
2735 | return false; | |
2736 | } | |
2737 | ||
ec4d0d8a VK |
2738 | static bool unix_use_proc(void) |
2739 | { | |
2740 | return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT"); | |
2741 | } | |
2742 | ||
2743 | static void unix_stats_print(struct sockstat *list, struct filter *f) | |
aba5acdf | 2744 | { |
ec4d0d8a | 2745 | struct sockstat *s; |
99bb68ff | 2746 | char *peer; |
b217df10 | 2747 | char *ctx_buf = NULL; |
ec4d0d8a | 2748 | bool use_proc = unix_use_proc(); |
b217df10 | 2749 | char port_name[30] = {}; |
aba5acdf SH |
2750 | |
2751 | for (s = list; s; s = s->next) { | |
ec4d0d8a | 2752 | if (!(f->states & (1 << s->state))) |
aba5acdf | 2753 | continue; |
bf4ceee6 | 2754 | if (unix_type_skip(s, f)) |
30b669d7 | 2755 | continue; |
aba5acdf | 2756 | |
99bb68ff VK |
2757 | peer = "*"; |
2758 | if (s->peer_name) | |
2759 | peer = s->peer_name; | |
ec4d0d8a VK |
2760 | |
2761 | if (s->rport && use_proc) { | |
2762 | struct sockstat *p; | |
bf4ceee6 | 2763 | |
aba5acdf | 2764 | for (p = list; p; p = p->next) { |
ec4d0d8a | 2765 | if (s->rport == p->lport) |
aba5acdf SH |
2766 | break; |
2767 | } | |
ec4d0d8a | 2768 | |
aba5acdf SH |
2769 | if (!p) { |
2770 | peer = "?"; | |
2771 | } else { | |
99bb68ff | 2772 | peer = p->name ? : "*"; |
aba5acdf SH |
2773 | } |
2774 | } | |
2775 | ||
29999b0f | 2776 | if (use_proc && f->f) { |
d17b136f PS |
2777 | struct sockstat st = { |
2778 | .local.family = AF_UNIX, | |
2779 | .remote.family = AF_UNIX, | |
2780 | }; | |
acd1e437 | 2781 | |
99bb68ff | 2782 | memcpy(st.local.data, &s->name, sizeof(s->name)); |
d17b136f | 2783 | if (strcmp(peer, "*")) |
99bb68ff VK |
2784 | memcpy(st.remote.data, &peer, sizeof(peer)); |
2785 | if (run_ssfilter(f->f, &st) == 0) | |
aba5acdf SH |
2786 | continue; |
2787 | } | |
2788 | ||
2d791bc8 VK |
2789 | sock_state_print(s, unix_netid_name(s->type)); |
2790 | ||
99bb68ff | 2791 | sock_addr_print(s->name ?: "*", " ", |
b217df10 VK |
2792 | int_to_str(s->lport, port_name), NULL); |
2793 | sock_addr_print(peer, " ", int_to_str(s->rport, port_name), | |
2794 | NULL); | |
116ac927 RH |
2795 | |
2796 | if (show_proc_ctx || show_sock_ctx) { | |
b217df10 | 2797 | if (find_entry(s->ino, &ctx_buf, |
116ac927 RH |
2798 | (show_proc_ctx & show_sock_ctx) ? |
2799 | PROC_SOCK_CTX : PROC_CTX) > 0) { | |
b217df10 VK |
2800 | printf(" users:(%s)", ctx_buf); |
2801 | free(ctx_buf); | |
116ac927 RH |
2802 | } |
2803 | } else if (show_users) { | |
b217df10 VK |
2804 | if (find_entry(s->ino, &ctx_buf, USERS) > 0) { |
2805 | printf(" users:(%s)", ctx_buf); | |
2806 | free(ctx_buf); | |
116ac927 | 2807 | } |
aba5acdf SH |
2808 | } |
2809 | printf("\n"); | |
2810 | } | |
2811 | } | |
2812 | ||
8a4025f6 | 2813 | static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, |
2814 | void *arg) | |
dfbaa90d | 2815 | { |
8a4025f6 | 2816 | struct filter *f = (struct filter *)arg; |
dfbaa90d PE |
2817 | struct unix_diag_msg *r = NLMSG_DATA(nlh); |
2818 | struct rtattr *tb[UNIX_DIAG_MAX+1]; | |
99bb68ff VK |
2819 | char name[128]; |
2820 | struct sockstat stat = { .name = "*", .peer_name = "*" }; | |
dfbaa90d | 2821 | |
acd1e437 | 2822 | parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1), |
dfbaa90d PE |
2823 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
2824 | ||
bf4ceee6 VK |
2825 | stat.type = r->udiag_type; |
2826 | stat.state = r->udiag_state; | |
ec4d0d8a VK |
2827 | stat.ino = stat.lport = r->udiag_ino; |
2828 | stat.local.family = stat.remote.family = AF_UNIX; | |
0d2e01c5 | 2829 | |
bf4ceee6 VK |
2830 | if (unix_type_skip(&stat, f)) |
2831 | return 0; | |
dfbaa90d | 2832 | |
defd61ca HFS |
2833 | if (tb[UNIX_DIAG_RQLEN]) { |
2834 | struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); | |
acd1e437 | 2835 | |
bf4ceee6 VK |
2836 | stat.rq = rql->udiag_rqueue; |
2837 | stat.wq = rql->udiag_wqueue; | |
defd61ca | 2838 | } |
dfbaa90d PE |
2839 | if (tb[UNIX_DIAG_NAME]) { |
2840 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); | |
2841 | ||
2842 | memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); | |
2843 | name[len] = '\0'; | |
2844 | if (name[0] == '\0') | |
2845 | name[0] = '@'; | |
99bb68ff VK |
2846 | stat.name = &name[0]; |
2847 | memcpy(stat.local.data, &stat.name, sizeof(stat.name)); | |
bf4ceee6 | 2848 | } |
dfbaa90d | 2849 | if (tb[UNIX_DIAG_PEER]) |
ec4d0d8a | 2850 | stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); |
dfbaa90d | 2851 | |
29999b0f VK |
2852 | if (f->f && run_ssfilter(f->f, &stat) == 0) |
2853 | return 0; | |
2854 | ||
bf4ceee6 | 2855 | unix_stats_print(&stat, f); |
dfbaa90d | 2856 | |
51ff9f24 | 2857 | if (show_mem) { |
bf4ceee6 | 2858 | printf("\t"); |
51ff9f24 HFS |
2859 | print_skmeminfo(tb, UNIX_DIAG_MEMINFO); |
2860 | } | |
5b816047 PE |
2861 | if (show_details) { |
2862 | if (tb[UNIX_DIAG_SHUTDOWN]) { | |
2863 | unsigned char mask; | |
acd1e437 | 2864 | |
5b816047 PE |
2865 | mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]); |
2866 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
2867 | } | |
2868 | } | |
bf4ceee6 VK |
2869 | if (show_mem || show_details) |
2870 | printf("\n"); | |
ec4d0d8a | 2871 | |
dfbaa90d PE |
2872 | return 0; |
2873 | } | |
2874 | ||
8a4025f6 | 2875 | static int handle_netlink_request(struct filter *f, struct nlmsghdr *req, |
2876 | size_t size, rtnl_filter_t show_one_sock) | |
dfbaa90d | 2877 | { |
8a4025f6 | 2878 | int ret = -1; |
2879 | struct rtnl_handle rth; | |
dfbaa90d | 2880 | |
8a4025f6 | 2881 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
dfbaa90d PE |
2882 | return -1; |
2883 | ||
8a4025f6 | 2884 | rth.dump = MAGIC_SEQ; |
dfbaa90d | 2885 | |
8a4025f6 | 2886 | if (rtnl_send(&rth, req, size) < 0) |
2887 | goto Exit; | |
dfbaa90d | 2888 | |
8a4025f6 | 2889 | if (rtnl_dump_filter(&rth, show_one_sock, f)) |
2890 | goto Exit; | |
dfbaa90d | 2891 | |
8a4025f6 | 2892 | ret = 0; |
2893 | Exit: | |
2894 | rtnl_close(&rth); | |
2895 | return ret; | |
dfbaa90d PE |
2896 | } |
2897 | ||
8a4025f6 | 2898 | static int unix_show_netlink(struct filter *f) |
d8402b96 | 2899 | { |
5fb421d4 | 2900 | DIAG_REQUEST(req, struct unix_diag_req r); |
d8402b96 AV |
2901 | |
2902 | req.r.sdiag_family = AF_UNIX; | |
2903 | req.r.udiag_states = f->states; | |
2904 | req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; | |
2905 | if (show_mem) | |
2906 | req.r.udiag_show |= UDIAG_SHOW_MEMINFO; | |
2907 | ||
8a4025f6 | 2908 | return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock); |
d8402b96 AV |
2909 | } |
2910 | ||
d1f28cf1 | 2911 | static int unix_show(struct filter *f) |
aba5acdf SH |
2912 | { |
2913 | FILE *fp; | |
2914 | char buf[256]; | |
2915 | char name[128]; | |
2916 | int newformat = 0; | |
2917 | int cnt; | |
ec4d0d8a | 2918 | struct sockstat *list = NULL; |
aba5acdf | 2919 | |
9db7bf15 VK |
2920 | if (!filter_af_get(f, AF_UNIX)) |
2921 | return 0; | |
2922 | ||
ec4d0d8a | 2923 | if (!unix_use_proc() && unix_show_netlink(f) == 0) |
dfbaa90d PE |
2924 | return 0; |
2925 | ||
ab01dbbb | 2926 | if ((fp = net_unix_open()) == NULL) |
aba5acdf | 2927 | return -1; |
61170fd8 | 2928 | if (!fgets(buf, sizeof(buf), fp)) { |
d572ed4d PS |
2929 | fclose(fp); |
2930 | return -1; | |
2931 | } | |
aba5acdf | 2932 | |
ae665a52 | 2933 | if (memcmp(buf, "Peer", 4) == 0) |
aba5acdf SH |
2934 | newformat = 1; |
2935 | cnt = 0; | |
2936 | ||
61170fd8 | 2937 | while (fgets(buf, sizeof(buf), fp)) { |
ec4d0d8a | 2938 | struct sockstat *u, **insp; |
aba5acdf SH |
2939 | int flags; |
2940 | ||
0ee9052f | 2941 | if (!(u = calloc(1, sizeof(*u)))) |
aba5acdf | 2942 | break; |
99bb68ff VK |
2943 | u->name = NULL; |
2944 | u->peer_name = NULL; | |
aba5acdf SH |
2945 | |
2946 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", | |
ec4d0d8a | 2947 | &u->rport, &u->rq, &u->wq, &flags, &u->type, |
aba5acdf SH |
2948 | &u->state, &u->ino, name) < 8) |
2949 | name[0] = 0; | |
2950 | ||
ec4d0d8a VK |
2951 | u->lport = u->ino; |
2952 | u->local.family = u->remote.family = AF_UNIX; | |
2953 | ||
2954 | if (flags & (1 << 16)) { | |
aba5acdf SH |
2955 | u->state = SS_LISTEN; |
2956 | } else { | |
2957 | u->state = unix_state_map[u->state-1]; | |
ec4d0d8a | 2958 | if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) |
aba5acdf SH |
2959 | u->state = SS_ESTABLISHED; |
2960 | } | |
2961 | ||
2962 | if (!newformat) { | |
ec4d0d8a | 2963 | u->rport = 0; |
aba5acdf SH |
2964 | u->rq = 0; |
2965 | u->wq = 0; | |
2966 | } | |
2967 | ||
2968 | insp = &list; | |
2969 | while (*insp) { | |
2970 | if (u->type < (*insp)->type || | |
2971 | (u->type == (*insp)->type && | |
2972 | u->ino < (*insp)->ino)) | |
2973 | break; | |
2974 | insp = &(*insp)->next; | |
2975 | } | |
2976 | u->next = *insp; | |
2977 | *insp = u; | |
2978 | ||
2979 | if (name[0]) { | |
99bb68ff VK |
2980 | if ((u->name = malloc(strlen(name)+1)) == NULL) |
2981 | break; | |
2982 | strcpy(u->name, name); | |
aba5acdf SH |
2983 | } |
2984 | if (++cnt > MAX_UNIX_REMEMBER) { | |
bf4ceee6 | 2985 | unix_stats_print(list, f); |
aba5acdf SH |
2986 | unix_list_free(list); |
2987 | list = NULL; | |
2988 | cnt = 0; | |
2989 | } | |
2990 | } | |
a3fd8e58 | 2991 | fclose(fp); |
aba5acdf | 2992 | if (list) { |
bf4ceee6 | 2993 | unix_stats_print(list, f); |
aba5acdf SH |
2994 | unix_list_free(list); |
2995 | list = NULL; | |
2996 | cnt = 0; | |
2997 | } | |
2998 | ||
2999 | return 0; | |
3000 | } | |
3001 | ||
89f634f9 | 3002 | static int packet_stats_print(struct sockstat *s, const struct filter *f) |
4a0053b6 VK |
3003 | { |
3004 | char *buf = NULL; | |
b217df10 VK |
3005 | const char *addr, *port; |
3006 | char ll_name[16]; | |
372c30d2 | 3007 | |
4a0053b6 | 3008 | if (f->f) { |
89f634f9 VK |
3009 | s->local.family = AF_PACKET; |
3010 | s->remote.family = AF_PACKET; | |
89f634f9 | 3011 | s->local.data[0] = s->prot; |
89f634f9 | 3012 | if (run_ssfilter(f->f, s) == 0) |
4a0053b6 VK |
3013 | return 1; |
3014 | } | |
372c30d2 | 3015 | |
2d791bc8 | 3016 | sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr"); |
372c30d2 | 3017 | |
b217df10 VK |
3018 | if (s->prot == 3) |
3019 | addr = "*"; | |
3020 | else | |
3021 | addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); | |
3022 | ||
3023 | if (s->iface == 0) | |
3024 | port = "*"; | |
3025 | else | |
3026 | port = xll_index_to_name(s->iface); | |
372c30d2 | 3027 | |
b217df10 VK |
3028 | sock_addr_print(addr, ":", port, NULL); |
3029 | sock_addr_print("", "*", "", NULL); | |
116ac927 RH |
3030 | |
3031 | if (show_proc_ctx || show_sock_ctx) { | |
4a0053b6 VK |
3032 | if (find_entry(s->ino, &buf, |
3033 | (show_proc_ctx & show_sock_ctx) ? | |
3034 | PROC_SOCK_CTX : PROC_CTX) > 0) { | |
116ac927 RH |
3035 | printf(" users:(%s)", buf); |
3036 | free(buf); | |
3037 | } | |
3038 | } else if (show_users) { | |
4a0053b6 | 3039 | if (find_entry(s->ino, &buf, USERS) > 0) { |
116ac927 RH |
3040 | printf(" users:(%s)", buf); |
3041 | free(buf); | |
3042 | } | |
372c30d2 | 3043 | } |
116ac927 | 3044 | |
f1b39e1b VK |
3045 | if (show_details) |
3046 | sock_details_print(s); | |
3047 | ||
4a0053b6 VK |
3048 | return 0; |
3049 | } | |
3050 | ||
2631b856 VK |
3051 | static void packet_show_ring(struct packet_diag_ring *ring) |
3052 | { | |
3053 | printf("blk_size:%d", ring->pdr_block_size); | |
3054 | printf(",blk_nr:%d", ring->pdr_block_nr); | |
3055 | printf(",frm_size:%d", ring->pdr_frame_size); | |
3056 | printf(",frm_nr:%d", ring->pdr_frame_nr); | |
3057 | printf(",tmo:%d", ring->pdr_retire_tmo); | |
3058 | printf(",features:0x%x", ring->pdr_features); | |
3059 | } | |
3060 | ||
4a0053b6 VK |
3061 | static int packet_show_sock(const struct sockaddr_nl *addr, |
3062 | struct nlmsghdr *nlh, void *arg) | |
3063 | { | |
3064 | const struct filter *f = arg; | |
3065 | struct packet_diag_msg *r = NLMSG_DATA(nlh); | |
2631b856 VK |
3066 | struct packet_diag_info *pinfo = NULL; |
3067 | struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL; | |
4a0053b6 | 3068 | struct rtattr *tb[PACKET_DIAG_MAX+1]; |
89f634f9 | 3069 | struct sockstat stat = {}; |
2631b856 VK |
3070 | uint32_t fanout = 0; |
3071 | bool has_fanout = false; | |
4a0053b6 | 3072 | |
acd1e437 | 3073 | parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1), |
4a0053b6 VK |
3074 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
3075 | ||
3076 | /* use /proc/net/packet if all info are not available */ | |
3077 | if (!tb[PACKET_DIAG_MEMINFO]) | |
3078 | return -1; | |
3079 | ||
2d791bc8 VK |
3080 | stat.type = r->pdiag_type; |
3081 | stat.prot = r->pdiag_num; | |
3082 | stat.ino = r->pdiag_ino; | |
3083 | stat.state = SS_CLOSE; | |
f1b39e1b | 3084 | stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); |
4a0053b6 VK |
3085 | |
3086 | if (tb[PACKET_DIAG_MEMINFO]) { | |
3087 | __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); | |
acd1e437 | 3088 | |
4a0053b6 VK |
3089 | stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
3090 | } | |
3091 | ||
3092 | if (tb[PACKET_DIAG_INFO]) { | |
2631b856 | 3093 | pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); |
2d791bc8 | 3094 | stat.lport = stat.iface = pinfo->pdi_index; |
4a0053b6 VK |
3095 | } |
3096 | ||
f1b39e1b VK |
3097 | if (tb[PACKET_DIAG_UID]) |
3098 | stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]); | |
3099 | ||
2631b856 VK |
3100 | if (tb[PACKET_DIAG_RX_RING]) |
3101 | ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]); | |
3102 | ||
3103 | if (tb[PACKET_DIAG_TX_RING]) | |
3104 | ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]); | |
3105 | ||
3106 | if (tb[PACKET_DIAG_FANOUT]) { | |
3107 | has_fanout = true; | |
3108 | fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]); | |
3109 | } | |
3110 | ||
4a0053b6 VK |
3111 | if (packet_stats_print(&stat, f)) |
3112 | return 0; | |
3113 | ||
2631b856 VK |
3114 | if (show_details) { |
3115 | if (pinfo) { | |
3116 | printf("\n\tver:%d", pinfo->pdi_version); | |
3117 | printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh); | |
3118 | printf(" flags( "); | |
3119 | if (pinfo->pdi_flags & PDI_RUNNING) | |
3120 | printf("running"); | |
3121 | if (pinfo->pdi_flags & PDI_AUXDATA) | |
3122 | printf(" auxdata"); | |
3123 | if (pinfo->pdi_flags & PDI_ORIGDEV) | |
3124 | printf(" origdev"); | |
3125 | if (pinfo->pdi_flags & PDI_VNETHDR) | |
3126 | printf(" vnethdr"); | |
3127 | if (pinfo->pdi_flags & PDI_LOSS) | |
3128 | printf(" loss"); | |
3129 | if (!pinfo->pdi_flags) | |
3130 | printf("0"); | |
3131 | printf(" )"); | |
3132 | } | |
3133 | if (ring_rx) { | |
3134 | printf("\n\tring_rx("); | |
3135 | packet_show_ring(ring_rx); | |
3136 | printf(")"); | |
3137 | } | |
3138 | if (ring_tx) { | |
3139 | printf("\n\tring_tx("); | |
3140 | packet_show_ring(ring_tx); | |
3141 | printf(")"); | |
3142 | } | |
3143 | if (has_fanout) { | |
3144 | uint16_t type = (fanout >> 16) & 0xffff; | |
3145 | ||
3146 | printf("\n\tfanout("); | |
3147 | printf("id:%d,", fanout & 0xffff); | |
3148 | printf("type:"); | |
3149 | ||
3150 | if (type == 0) | |
3151 | printf("hash"); | |
3152 | else if (type == 1) | |
3153 | printf("lb"); | |
3154 | else if (type == 2) | |
3155 | printf("cpu"); | |
3156 | else if (type == 3) | |
3157 | printf("roll"); | |
3158 | else if (type == 4) | |
3159 | printf("random"); | |
3160 | else if (type == 5) | |
3161 | printf("qm"); | |
3162 | else | |
3163 | printf("0x%x", type); | |
3164 | ||
3165 | printf(")"); | |
3166 | } | |
3167 | } | |
3168 | ||
372c30d2 ND |
3169 | if (show_bpf && tb[PACKET_DIAG_FILTER]) { |
3170 | struct sock_filter *fil = | |
3171 | RTA_DATA(tb[PACKET_DIAG_FILTER]); | |
3172 | int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / | |
3173 | sizeof(struct sock_filter); | |
3174 | ||
3175 | printf("\n\tbpf filter (%d): ", num); | |
3176 | while (num) { | |
3177 | printf(" 0x%02x %u %u %u,", | |
3178 | fil->code, fil->jt, fil->jf, fil->k); | |
3179 | num--; | |
3180 | fil++; | |
3181 | } | |
3182 | } | |
3183 | printf("\n"); | |
3184 | return 0; | |
3185 | } | |
3186 | ||
8a4025f6 | 3187 | static int packet_show_netlink(struct filter *f) |
372c30d2 | 3188 | { |
5fb421d4 | 3189 | DIAG_REQUEST(req, struct packet_diag_req r); |
372c30d2 | 3190 | |
372c30d2 | 3191 | req.r.sdiag_family = AF_PACKET; |
2631b856 VK |
3192 | req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | |
3193 | PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT; | |
372c30d2 | 3194 | |
8a4025f6 | 3195 | return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock); |
372c30d2 ND |
3196 | } |
3197 | ||
4a0053b6 VK |
3198 | static int packet_show_line(char *buf, const struct filter *f, int fam) |
3199 | { | |
3200 | unsigned long long sk; | |
89f634f9 | 3201 | struct sockstat stat = {}; |
4a0053b6 VK |
3202 | int type, prot, iface, state, rq, uid, ino; |
3203 | ||
3204 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", | |
3205 | &sk, | |
3206 | &type, &prot, &iface, &state, | |
3207 | &rq, &uid, &ino); | |
3208 | ||
3209 | if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) | |
3210 | return 0; | |
3211 | if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) | |
3212 | return 0; | |
3213 | ||
3214 | stat.type = type; | |
3215 | stat.prot = prot; | |
2d791bc8 | 3216 | stat.lport = stat.iface = iface; |
4a0053b6 VK |
3217 | stat.state = state; |
3218 | stat.rq = rq; | |
3219 | stat.uid = uid; | |
3220 | stat.ino = ino; | |
2d791bc8 VK |
3221 | stat.state = SS_CLOSE; |
3222 | ||
4a0053b6 VK |
3223 | if (packet_stats_print(&stat, f)) |
3224 | return 0; | |
3225 | ||
4a0053b6 VK |
3226 | printf("\n"); |
3227 | return 0; | |
3228 | } | |
aba5acdf | 3229 | |
d1f28cf1 | 3230 | static int packet_show(struct filter *f) |
aba5acdf SH |
3231 | { |
3232 | FILE *fp; | |
b95d28c3 | 3233 | int rc = 0; |
3d0b7439 | 3234 | |
9db7bf15 | 3235 | if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE))) |
b9ea445d | 3236 | return 0; |
aba5acdf | 3237 | |
4a0053b6 VK |
3238 | if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") && |
3239 | packet_show_netlink(f) == 0) | |
372c30d2 ND |
3240 | return 0; |
3241 | ||
ab01dbbb | 3242 | if ((fp = net_packet_open()) == NULL) |
aba5acdf | 3243 | return -1; |
4a0053b6 | 3244 | if (generic_record_read(fp, packet_show_line, f, AF_PACKET)) |
b95d28c3 | 3245 | rc = -1; |
aba5acdf | 3246 | |
b95d28c3 PS |
3247 | fclose(fp); |
3248 | return rc; | |
aba5acdf SH |
3249 | } |
3250 | ||
5f24ec0e | 3251 | static int netlink_show_one(struct filter *f, |
acd1e437 SH |
3252 | int prot, int pid, unsigned int groups, |
3253 | int state, int dst_pid, unsigned int dst_group, | |
129709ae AV |
3254 | int rq, int wq, |
3255 | unsigned long long sk, unsigned long long cb) | |
3256 | { | |
2d791bc8 | 3257 | struct sockstat st; |
acd1e437 | 3258 | |
b217df10 VK |
3259 | SPRINT_BUF(prot_buf) = {}; |
3260 | const char *prot_name; | |
3261 | char procname[64] = {}; | |
eef43b50 | 3262 | |
2d791bc8 VK |
3263 | st.state = SS_CLOSE; |
3264 | st.rq = rq; | |
3265 | st.wq = wq; | |
3266 | ||
129709ae | 3267 | if (f->f) { |
055840f2 VK |
3268 | st.local.family = AF_NETLINK; |
3269 | st.remote.family = AF_NETLINK; | |
3270 | st.rport = -1; | |
3271 | st.lport = pid; | |
3272 | st.local.data[0] = prot; | |
055840f2 | 3273 | if (run_ssfilter(f->f, &st) == 0) |
5f24ec0e | 3274 | return 1; |
129709ae AV |
3275 | } |
3276 | ||
2d791bc8 | 3277 | sock_state_print(&st, "nl"); |
eef43b50 | 3278 | |
b217df10 VK |
3279 | if (resolve_services) |
3280 | prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); | |
3281 | else | |
3282 | prot_name = int_to_str(prot, prot_buf); | |
eef43b50 | 3283 | |
129709ae | 3284 | if (pid == -1) { |
b217df10 | 3285 | procname[0] = '*'; |
129709ae AV |
3286 | } else if (resolve_services) { |
3287 | int done = 0; | |
acd1e437 | 3288 | |
129709ae AV |
3289 | if (!pid) { |
3290 | done = 1; | |
b217df10 | 3291 | strncpy(procname, "kernel", 6); |
129709ae | 3292 | } else if (pid > 0) { |
129709ae | 3293 | FILE *fp; |
acd1e437 | 3294 | |
0ee9052f | 3295 | snprintf(procname, sizeof(procname), "%s/%d/stat", |
129709ae AV |
3296 | getenv("PROC_ROOT") ? : "/proc", pid); |
3297 | if ((fp = fopen(procname, "r")) != NULL) { | |
3298 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { | |
0ee9052f | 3299 | snprintf(procname+strlen(procname), |
3300 | sizeof(procname)-strlen(procname), | |
3301 | "/%d", pid); | |
129709ae AV |
3302 | done = 1; |
3303 | } | |
3304 | fclose(fp); | |
3305 | } | |
3306 | } | |
3307 | if (!done) | |
b217df10 | 3308 | int_to_str(pid, procname); |
129709ae | 3309 | } else { |
b217df10 | 3310 | int_to_str(pid, procname); |
129709ae | 3311 | } |
f271fe01 | 3312 | |
b217df10 VK |
3313 | sock_addr_print(prot_name, ":", procname, NULL); |
3314 | ||
f271fe01 | 3315 | if (state == NETLINK_CONNECTED) { |
b217df10 VK |
3316 | char dst_group_buf[30]; |
3317 | char dst_pid_buf[30]; | |
acd1e437 | 3318 | |
b217df10 VK |
3319 | sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", |
3320 | int_to_str(dst_pid, dst_pid_buf), NULL); | |
f271fe01 | 3321 | } else { |
b217df10 | 3322 | sock_addr_print("", "*", "", NULL); |
f271fe01 | 3323 | } |
129709ae | 3324 | |
116ac927 | 3325 | char *pid_context = NULL; |
acd1e437 | 3326 | |
116ac927 RH |
3327 | if (show_proc_ctx) { |
3328 | /* The pid value will either be: | |
3329 | * 0 if destination kernel - show kernel initial context. | |
3330 | * A valid process pid - use getpidcon. | |
3331 | * A unique value allocated by the kernel or netlink user | |
3332 | * to the process - show context as "not available". | |
3333 | */ | |
3334 | if (!pid) | |
3335 | security_get_initial_context("kernel", &pid_context); | |
3336 | else if (pid > 0) | |
3337 | getpidcon(pid, &pid_context); | |
3338 | ||
3339 | if (pid_context != NULL) { | |
3340 | printf("proc_ctx=%-*s ", serv_width, pid_context); | |
3341 | free(pid_context); | |
3342 | } else { | |
3343 | printf("proc_ctx=%-*s ", serv_width, "unavailable"); | |
3344 | } | |
3345 | } | |
3346 | ||
129709ae AV |
3347 | if (show_details) { |
3348 | printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); | |
3349 | } | |
3350 | printf("\n"); | |
3351 | ||
5f24ec0e | 3352 | return 0; |
129709ae AV |
3353 | } |
3354 | ||
8a4025f6 | 3355 | static int netlink_show_sock(const struct sockaddr_nl *addr, |
3356 | struct nlmsghdr *nlh, void *arg) | |
ecb928c8 | 3357 | { |
8a4025f6 | 3358 | struct filter *f = (struct filter *)arg; |
ecb928c8 AV |
3359 | struct netlink_diag_msg *r = NLMSG_DATA(nlh); |
3360 | struct rtattr *tb[NETLINK_DIAG_MAX+1]; | |
3361 | int rq = 0, wq = 0; | |
3362 | unsigned long groups = 0; | |
3363 | ||
acd1e437 | 3364 | parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1), |
ecb928c8 AV |
3365 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
3366 | ||
3367 | if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS])) | |
3368 | groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]); | |
3369 | ||
3370 | if (tb[NETLINK_DIAG_MEMINFO]) { | |
3371 | const __u32 *skmeminfo; | |
acd1e437 | 3372 | |
ecb928c8 AV |
3373 | skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]); |
3374 | ||
3375 | rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
3376 | wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC]; | |
3377 | } | |
3378 | ||
5f24ec0e | 3379 | if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups, |
ecb928c8 | 3380 | r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group, |
5f24ec0e VK |
3381 | rq, wq, 0, 0)) { |
3382 | return 0; | |
3383 | } | |
ecb928c8 AV |
3384 | |
3385 | if (show_mem) { | |
3386 | printf("\t"); | |
3387 | print_skmeminfo(tb, NETLINK_DIAG_MEMINFO); | |
3388 | printf("\n"); | |
3389 | } | |
3390 | ||
3391 | return 0; | |
3392 | } | |
3393 | ||
8a4025f6 | 3394 | static int netlink_show_netlink(struct filter *f) |
ecb928c8 | 3395 | { |
5fb421d4 | 3396 | DIAG_REQUEST(req, struct netlink_diag_req r); |
ecb928c8 AV |
3397 | |
3398 | req.r.sdiag_family = AF_NETLINK; | |
3399 | req.r.sdiag_protocol = NDIAG_PROTO_ALL; | |
3400 | req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO; | |
3401 | ||
8a4025f6 | 3402 | return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock); |
ecb928c8 AV |
3403 | } |
3404 | ||
d1f28cf1 | 3405 | static int netlink_show(struct filter *f) |
aba5acdf SH |
3406 | { |
3407 | FILE *fp; | |
3408 | char buf[256]; | |
3409 | int prot, pid; | |
acd1e437 | 3410 | unsigned int groups; |
aba5acdf SH |
3411 | int rq, wq, rc; |
3412 | unsigned long long sk, cb; | |
3413 | ||
9db7bf15 | 3414 | if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE))) |
b9ea445d VK |
3415 | return 0; |
3416 | ||
129709ae | 3417 | if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") && |
8a4025f6 | 3418 | netlink_show_netlink(f) == 0) |
129709ae AV |
3419 | return 0; |
3420 | ||
ab01dbbb | 3421 | if ((fp = net_netlink_open()) == NULL) |
aba5acdf | 3422 | return -1; |
61170fd8 | 3423 | if (!fgets(buf, sizeof(buf), fp)) { |
d572ed4d PS |
3424 | fclose(fp); |
3425 | return -1; | |
3426 | } | |
aba5acdf | 3427 | |
61170fd8 | 3428 | while (fgets(buf, sizeof(buf), fp)) { |
aba5acdf SH |
3429 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", |
3430 | &sk, | |
3431 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); | |
3432 | ||
f271fe01 | 3433 | netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb); |
aba5acdf SH |
3434 | } |
3435 | ||
b95d28c3 | 3436 | fclose(fp); |
aba5acdf SH |
3437 | return 0; |
3438 | } | |
3439 | ||
6885e3bf CG |
3440 | struct sock_diag_msg { |
3441 | __u8 sdiag_family; | |
3442 | }; | |
3443 | ||
3444 | static int generic_show_sock(const struct sockaddr_nl *addr, | |
3445 | struct nlmsghdr *nlh, void *arg) | |
3446 | { | |
3447 | struct sock_diag_msg *r = NLMSG_DATA(nlh); | |
3448 | struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX }; | |
3449 | ||
3450 | switch (r->sdiag_family) { | |
3451 | case AF_INET: | |
3452 | case AF_INET6: | |
3453 | return show_one_inet_sock(addr, nlh, &inet_arg); | |
3454 | case AF_UNIX: | |
3455 | return unix_show_sock(addr, nlh, arg); | |
3456 | case AF_PACKET: | |
3457 | return packet_show_sock(addr, nlh, arg); | |
3458 | case AF_NETLINK: | |
3459 | return netlink_show_sock(addr, nlh, arg); | |
3460 | default: | |
3461 | return -1; | |
3462 | } | |
3463 | } | |
3464 | ||
3465 | static int handle_follow_request(struct filter *f) | |
3466 | { | |
3467 | int ret = -1; | |
3468 | int groups = 0; | |
3469 | struct rtnl_handle rth; | |
3470 | ||
3471 | if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB)) | |
3472 | groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1); | |
3473 | if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB)) | |
3474 | groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1); | |
3475 | if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB)) | |
3476 | groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1); | |
3477 | if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB)) | |
3478 | groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1); | |
3479 | ||
3480 | if (groups == 0) | |
3481 | return -1; | |
3482 | ||
3483 | if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG)) | |
3484 | return -1; | |
3485 | ||
3486 | rth.dump = 0; | |
3487 | rth.local.nl_pid = 0; | |
3488 | ||
3489 | if (rtnl_dump_filter(&rth, generic_show_sock, f)) | |
3490 | goto Exit; | |
3491 | ||
3492 | ret = 0; | |
3493 | Exit: | |
3494 | rtnl_close(&rth); | |
3495 | return ret; | |
3496 | } | |
3497 | ||
acd1e437 | 3498 | struct snmpstat { |
aba5acdf SH |
3499 | int tcp_estab; |
3500 | }; | |
3501 | ||
d1f28cf1 | 3502 | static int get_snmp_int(char *proto, char *key, int *result) |
aba5acdf SH |
3503 | { |
3504 | char buf[1024]; | |
3505 | FILE *fp; | |
3506 | int protolen = strlen(proto); | |
3507 | int keylen = strlen(key); | |
3508 | ||
3509 | *result = 0; | |
3510 | ||
ab01dbbb | 3511 | if ((fp = net_snmp_open()) == NULL) |
aba5acdf SH |
3512 | return -1; |
3513 | ||
3514 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
3515 | char *p = buf; | |
3516 | int pos = 0; | |
acd1e437 | 3517 | |
aba5acdf SH |
3518 | if (memcmp(buf, proto, protolen)) |
3519 | continue; | |
3520 | while ((p = strchr(p, ' ')) != NULL) { | |
3521 | pos++; | |
3522 | p++; | |
3523 | if (memcmp(p, key, keylen) == 0 && | |
3524 | (p[keylen] == ' ' || p[keylen] == '\n')) | |
3525 | break; | |
3526 | } | |
3527 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
3528 | break; | |
3529 | if (memcmp(buf, proto, protolen)) | |
3530 | break; | |
3531 | p = buf; | |
3532 | while ((p = strchr(p, ' ')) != NULL) { | |
3533 | p++; | |
3534 | if (--pos == 0) { | |
3535 | sscanf(p, "%d", result); | |
3536 | fclose(fp); | |
3537 | return 0; | |
3538 | } | |
3539 | } | |
3540 | } | |
3541 | ||
3542 | fclose(fp); | |
3543 | errno = ESRCH; | |
3544 | return -1; | |
3545 | } | |
3546 | ||
3547 | ||
3548 | /* Get stats from sockstat */ | |
3549 | ||
acd1e437 | 3550 | struct ssummary { |
aba5acdf SH |
3551 | int socks; |
3552 | int tcp_mem; | |
3553 | int tcp_total; | |
3554 | int tcp_orphans; | |
3555 | int tcp_tws; | |
3556 | int tcp4_hashed; | |
3557 | int udp4; | |
3558 | int raw4; | |
3559 | int frag4; | |
3560 | int frag4_mem; | |
3561 | int tcp6_hashed; | |
3562 | int udp6; | |
3563 | int raw6; | |
3564 | int frag6; | |
3565 | int frag6_mem; | |
3566 | }; | |
3567 | ||
055840f2 | 3568 | static void get_sockstat_line(char *line, struct ssummary *s) |
aba5acdf SH |
3569 | { |
3570 | char id[256], rem[256]; | |
3571 | ||
3572 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) | |
3573 | return; | |
3574 | ||
3575 | if (strcmp(id, "sockets:") == 0) | |
3576 | sscanf(rem, "%*s%d", &s->socks); | |
3577 | else if (strcmp(id, "UDP:") == 0) | |
3578 | sscanf(rem, "%*s%d", &s->udp4); | |
3579 | else if (strcmp(id, "UDP6:") == 0) | |
3580 | sscanf(rem, "%*s%d", &s->udp6); | |
3581 | else if (strcmp(id, "RAW:") == 0) | |
3582 | sscanf(rem, "%*s%d", &s->raw4); | |
3583 | else if (strcmp(id, "RAW6:") == 0) | |
3584 | sscanf(rem, "%*s%d", &s->raw6); | |
3585 | else if (strcmp(id, "TCP6:") == 0) | |
3586 | sscanf(rem, "%*s%d", &s->tcp6_hashed); | |
3587 | else if (strcmp(id, "FRAG:") == 0) | |
3588 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); | |
3589 | else if (strcmp(id, "FRAG6:") == 0) | |
3590 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); | |
3591 | else if (strcmp(id, "TCP:") == 0) | |
3592 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", | |
3593 | &s->tcp4_hashed, | |
3594 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); | |
3595 | } | |
3596 | ||
055840f2 | 3597 | static int get_sockstat(struct ssummary *s) |
aba5acdf SH |
3598 | { |
3599 | char buf[256]; | |
3600 | FILE *fp; | |
3601 | ||
3602 | memset(s, 0, sizeof(*s)); | |
3603 | ||
ab01dbbb | 3604 | if ((fp = net_sockstat_open()) == NULL) |
aba5acdf | 3605 | return -1; |
acd1e437 | 3606 | while (fgets(buf, sizeof(buf), fp) != NULL) |
aba5acdf SH |
3607 | get_sockstat_line(buf, s); |
3608 | fclose(fp); | |
3609 | ||
ab01dbbb | 3610 | if ((fp = net_sockstat6_open()) == NULL) |
aba5acdf | 3611 | return 0; |
acd1e437 | 3612 | while (fgets(buf, sizeof(buf), fp) != NULL) |
aba5acdf SH |
3613 | get_sockstat_line(buf, s); |
3614 | fclose(fp); | |
3615 | ||
3616 | return 0; | |
3617 | } | |
3618 | ||
d1f28cf1 | 3619 | static int print_summary(void) |
aba5acdf | 3620 | { |
055840f2 | 3621 | struct ssummary s; |
aba5acdf SH |
3622 | struct snmpstat sn; |
3623 | ||
3624 | if (get_sockstat(&s) < 0) | |
3625 | perror("ss: get_sockstat"); | |
3626 | if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0) | |
3627 | perror("ss: get_snmpstat"); | |
3628 | ||
a221d621 BL |
3629 | get_slabstat(&slabstat); |
3630 | ||
aba5acdf SH |
3631 | printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks); |
3632 | ||
3633 | printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n", | |
3634 | s.tcp_total + slabstat.tcp_syns + s.tcp_tws, | |
3635 | sn.tcp_estab, | |
3636 | s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws), | |
3637 | s.tcp_orphans, | |
3638 | slabstat.tcp_syns, | |
3639 | s.tcp_tws, slabstat.tcp_tws, | |
3640 | slabstat.tcp_ports | |
3641 | ); | |
3642 | ||
3643 | printf("\n"); | |
3644 | printf("Transport Total IP IPv6\n"); | |
3645 | printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-"); | |
3646 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); | |
3647 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); | |
3648 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); | |
ae665a52 | 3649 | printf("INET %-9d %-9d %-9d\n", |
aba5acdf SH |
3650 | s.raw4+s.udp4+s.tcp4_hashed+ |
3651 | s.raw6+s.udp6+s.tcp6_hashed, | |
3652 | s.raw4+s.udp4+s.tcp4_hashed, | |
3653 | s.raw6+s.udp6+s.tcp6_hashed); | |
3654 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); | |
3655 | ||
3656 | printf("\n"); | |
3657 | ||
3658 | return 0; | |
3659 | } | |
3660 | ||
7a96e199 | 3661 | static void _usage(FILE *dest) |
aba5acdf | 3662 | { |
7a96e199 | 3663 | fprintf(dest, |
aba5acdf SH |
3664 | "Usage: ss [ OPTIONS ]\n" |
3665 | " ss [ OPTIONS ] [ FILTER ]\n" | |
ff041f16 VK |
3666 | " -h, --help this message\n" |
3667 | " -V, --version output version information\n" | |
3668 | " -n, --numeric don't resolve service names\n" | |
ab61159a | 3669 | " -r, --resolve resolve host names\n" |
ff041f16 VK |
3670 | " -a, --all display all sockets\n" |
3671 | " -l, --listening display listening sockets\n" | |
ab61159a SH |
3672 | " -o, --options show timer information\n" |
3673 | " -e, --extended show detailed socket information\n" | |
3674 | " -m, --memory show socket memory usage\n" | |
ff041f16 VK |
3675 | " -p, --processes show process using socket\n" |
3676 | " -i, --info show internal TCP information\n" | |
3677 | " -s, --summary show socket usage summary\n" | |
b0f01cf6 | 3678 | " -b, --bpf show bpf filter socket information\n" |
6885e3bf | 3679 | " -E, --events continually display sockets as they are destroyed\n" |
ff041f16 VK |
3680 | " -Z, --context display process SELinux security contexts\n" |
3681 | " -z, --contexts display process and socket SELinux security contexts\n" | |
95ce04bc | 3682 | " -N, --net switch to the specified network namespace name\n" |
ab61159a SH |
3683 | "\n" |
3684 | " -4, --ipv4 display only IP version 4 sockets\n" | |
3685 | " -6, --ipv6 display only IP version 6 sockets\n" | |
ff041f16 VK |
3686 | " -0, --packet display PACKET sockets\n" |
3687 | " -t, --tcp display only TCP sockets\n" | |
3688 | " -u, --udp display only UDP sockets\n" | |
3689 | " -d, --dccp display only DCCP sockets\n" | |
3690 | " -w, --raw display only RAW sockets\n" | |
3691 | " -x, --unix display only Unix domain sockets\n" | |
ab61159a | 3692 | " -f, --family=FAMILY display sockets of type FAMILY\n" |
ebef3174 | 3693 | " FAMILY := {inet|inet6|link|unix|netlink|help}\n" |
ab61159a | 3694 | "\n" |
fb2594c1 | 3695 | " -K, --kill forcibly close sockets, display what was closed\n" |
7a4559f6 | 3696 | " -H, --no-header Suppress header line\n" |
fb2594c1 | 3697 | "\n" |
583de149 | 3698 | " -A, --query=QUERY, --socket=QUERY\n" |
56dee73e | 3699 | " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n" |
ab61159a | 3700 | "\n" |
583de149 | 3701 | " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" |
ab61159a | 3702 | " -F, --filter=FILE read filter information from FILE\n" |
ff041f16 VK |
3703 | " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n" |
3704 | " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n" | |
3705 | " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n" | |
3706 | " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" | |
3707 | " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" | |
3708 | " bucket := {syn-recv|time-wait}\n" | |
3709 | " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n" | |
ab61159a | 3710 | ); |
7a96e199 AH |
3711 | } |
3712 | ||
3713 | static void help(void) __attribute__((noreturn)); | |
3714 | static void help(void) | |
3715 | { | |
3716 | _usage(stdout); | |
3717 | exit(0); | |
3718 | } | |
3719 | ||
3720 | static void usage(void) __attribute__((noreturn)); | |
3721 | static void usage(void) | |
3722 | { | |
3723 | _usage(stderr); | |
aba5acdf SH |
3724 | exit(-1); |
3725 | } | |
3726 | ||
3727 | ||
d1f28cf1 | 3728 | static int scan_state(const char *state) |
aba5acdf SH |
3729 | { |
3730 | int i; | |
acd1e437 | 3731 | |
aba5acdf SH |
3732 | if (strcasecmp(state, "close") == 0 || |
3733 | strcasecmp(state, "closed") == 0) | |
3734 | return (1<<SS_CLOSE); | |
3735 | if (strcasecmp(state, "syn-rcv") == 0) | |
3736 | return (1<<SS_SYN_RECV); | |
1a5bad5a | 3737 | if (strcasecmp(state, "established") == 0) |
aba5acdf SH |
3738 | return (1<<SS_ESTABLISHED); |
3739 | if (strcasecmp(state, "all") == 0) | |
3740 | return SS_ALL; | |
3741 | if (strcasecmp(state, "connected") == 0) | |
3742 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); | |
1a5bad5a | 3743 | if (strcasecmp(state, "synchronized") == 0) |
aba5acdf SH |
3744 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
3745 | if (strcasecmp(state, "bucket") == 0) | |
3746 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); | |
3747 | if (strcasecmp(state, "big") == 0) | |
3748 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); | |
acd1e437 | 3749 | for (i = 0; i < SS_MAX; i++) { |
1a5bad5a | 3750 | if (strcasecmp(state, sstate_namel[i]) == 0) |
aba5acdf SH |
3751 | return (1<<i); |
3752 | } | |
9db7bf15 VK |
3753 | |
3754 | fprintf(stderr, "ss: wrong state name: %s\n", state); | |
3755 | exit(-1); | |
aba5acdf SH |
3756 | } |
3757 | ||
ab61159a SH |
3758 | static const struct option long_opts[] = { |
3759 | { "numeric", 0, 0, 'n' }, | |
3760 | { "resolve", 0, 0, 'r' }, | |
3761 | { "options", 0, 0, 'o' }, | |
3762 | { "extended", 0, 0, 'e' }, | |
3763 | { "memory", 0, 0, 'm' }, | |
3764 | { "info", 0, 0, 'i' }, | |
3765 | { "processes", 0, 0, 'p' }, | |
372c30d2 | 3766 | { "bpf", 0, 0, 'b' }, |
6885e3bf | 3767 | { "events", 0, 0, 'E' }, |
351efcde | 3768 | { "dccp", 0, 0, 'd' }, |
ab61159a SH |
3769 | { "tcp", 0, 0, 't' }, |
3770 | { "udp", 0, 0, 'u' }, | |
3771 | { "raw", 0, 0, 'w' }, | |
3772 | { "unix", 0, 0, 'x' }, | |
3773 | { "all", 0, 0, 'a' }, | |
3774 | { "listening", 0, 0, 'l' }, | |
3775 | { "ipv4", 0, 0, '4' }, | |
3776 | { "ipv6", 0, 0, '6' }, | |
3777 | { "packet", 0, 0, '0' }, | |
3778 | { "family", 1, 0, 'f' }, | |
3779 | { "socket", 1, 0, 'A' }, | |
583de149 | 3780 | { "query", 1, 0, 'A' }, |
c3f346b0 | 3781 | { "summary", 0, 0, 's' }, |
583de149 | 3782 | { "diag", 1, 0, 'D' }, |
ab61159a SH |
3783 | { "filter", 1, 0, 'F' }, |
3784 | { "version", 0, 0, 'V' }, | |
3785 | { "help", 0, 0, 'h' }, | |
116ac927 RH |
3786 | { "context", 0, 0, 'Z' }, |
3787 | { "contexts", 0, 0, 'z' }, | |
95ce04bc | 3788 | { "net", 1, 0, 'N' }, |
fb2594c1 | 3789 | { "kill", 0, 0, 'K' }, |
7a4559f6 | 3790 | { "no-header", 0, 0, 'H' }, |
ab61159a | 3791 | { 0 } |
ae665a52 | 3792 | |
ab61159a SH |
3793 | }; |
3794 | ||
aba5acdf SH |
3795 | int main(int argc, char *argv[]) |
3796 | { | |
aba5acdf SH |
3797 | int saw_states = 0; |
3798 | int saw_query = 0; | |
3799 | int do_summary = 0; | |
7d105b56 | 3800 | const char *dump_tcpdiag = NULL; |
aba5acdf SH |
3801 | FILE *filter_fp = NULL; |
3802 | int ch; | |
9db7bf15 | 3803 | int state_filter = 0; |
aba5acdf | 3804 | |
7a4559f6 | 3805 | while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KH", |
ab61159a | 3806 | long_opts, NULL)) != EOF) { |
acd1e437 | 3807 | switch (ch) { |
aba5acdf SH |
3808 | case 'n': |
3809 | resolve_services = 0; | |
3810 | break; | |
3811 | case 'r': | |
3812 | resolve_hosts = 1; | |
3813 | break; | |
3814 | case 'o': | |
3815 | show_options = 1; | |
3816 | break; | |
3817 | case 'e': | |
3818 | show_options = 1; | |
3819 | show_details++; | |
3820 | break; | |
3821 | case 'm': | |
3822 | show_mem = 1; | |
3823 | break; | |
3824 | case 'i': | |
3825 | show_tcpinfo = 1; | |
3826 | break; | |
3827 | case 'p': | |
3828 | show_users++; | |
fbc0f876 | 3829 | user_ent_hash_build(); |
aba5acdf | 3830 | break; |
372c30d2 ND |
3831 | case 'b': |
3832 | show_options = 1; | |
3833 | show_bpf++; | |
3834 | break; | |
6885e3bf CG |
3835 | case 'E': |
3836 | follow_events = 1; | |
3837 | break; | |
351efcde | 3838 | case 'd': |
57ff5a10 | 3839 | filter_db_set(¤t_filter, DCCP_DB); |
351efcde | 3840 | break; |
aba5acdf | 3841 | case 't': |
57ff5a10 | 3842 | filter_db_set(¤t_filter, TCP_DB); |
aba5acdf SH |
3843 | break; |
3844 | case 'u': | |
57ff5a10 | 3845 | filter_db_set(¤t_filter, UDP_DB); |
aba5acdf SH |
3846 | break; |
3847 | case 'w': | |
57ff5a10 | 3848 | filter_db_set(¤t_filter, RAW_DB); |
aba5acdf SH |
3849 | break; |
3850 | case 'x': | |
9db7bf15 | 3851 | filter_af_set(¤t_filter, AF_UNIX); |
aba5acdf SH |
3852 | break; |
3853 | case 'a': | |
9db7bf15 | 3854 | state_filter = SS_ALL; |
aba5acdf SH |
3855 | break; |
3856 | case 'l': | |
9db7bf15 | 3857 | state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE); |
aba5acdf SH |
3858 | break; |
3859 | case '4': | |
9db7bf15 | 3860 | filter_af_set(¤t_filter, AF_INET); |
aba5acdf SH |
3861 | break; |
3862 | case '6': | |
9db7bf15 | 3863 | filter_af_set(¤t_filter, AF_INET6); |
aba5acdf SH |
3864 | break; |
3865 | case '0': | |
9db7bf15 | 3866 | filter_af_set(¤t_filter, AF_PACKET); |
aba5acdf SH |
3867 | break; |
3868 | case 'f': | |
3869 | if (strcmp(optarg, "inet") == 0) | |
9db7bf15 | 3870 | filter_af_set(¤t_filter, AF_INET); |
aba5acdf | 3871 | else if (strcmp(optarg, "inet6") == 0) |
9db7bf15 | 3872 | filter_af_set(¤t_filter, AF_INET6); |
aba5acdf | 3873 | else if (strcmp(optarg, "link") == 0) |
9db7bf15 | 3874 | filter_af_set(¤t_filter, AF_PACKET); |
aba5acdf | 3875 | else if (strcmp(optarg, "unix") == 0) |
9db7bf15 | 3876 | filter_af_set(¤t_filter, AF_UNIX); |
aba5acdf | 3877 | else if (strcmp(optarg, "netlink") == 0) |
9db7bf15 | 3878 | filter_af_set(¤t_filter, AF_NETLINK); |
aba5acdf | 3879 | else if (strcmp(optarg, "help") == 0) |
7a96e199 | 3880 | help(); |
aba5acdf | 3881 | else { |
9db7bf15 VK |
3882 | fprintf(stderr, "ss: \"%s\" is invalid family\n", |
3883 | optarg); | |
aba5acdf SH |
3884 | usage(); |
3885 | } | |
3886 | break; | |
3887 | case 'A': | |
3888 | { | |
3889 | char *p, *p1; | |
acd1e437 | 3890 | |
aba5acdf SH |
3891 | if (!saw_query) { |
3892 | current_filter.dbs = 0; | |
7f9dddbe | 3893 | state_filter = state_filter ? |
acd1e437 | 3894 | state_filter : SS_CONN; |
aba5acdf SH |
3895 | saw_query = 1; |
3896 | do_default = 0; | |
3897 | } | |
3898 | p = p1 = optarg; | |
3899 | do { | |
3900 | if ((p1 = strchr(p, ',')) != NULL) | |
ae665a52 | 3901 | *p1 = 0; |
aba5acdf | 3902 | if (strcmp(p, "all") == 0) { |
57ff5a10 | 3903 | filter_default_dbs(¤t_filter); |
aba5acdf | 3904 | } else if (strcmp(p, "inet") == 0) { |
57ff5a10 VK |
3905 | filter_db_set(¤t_filter, UDP_DB); |
3906 | filter_db_set(¤t_filter, DCCP_DB); | |
3907 | filter_db_set(¤t_filter, TCP_DB); | |
3908 | filter_db_set(¤t_filter, RAW_DB); | |
aba5acdf | 3909 | } else if (strcmp(p, "udp") == 0) { |
57ff5a10 | 3910 | filter_db_set(¤t_filter, UDP_DB); |
351efcde | 3911 | } else if (strcmp(p, "dccp") == 0) { |
57ff5a10 | 3912 | filter_db_set(¤t_filter, DCCP_DB); |
aba5acdf | 3913 | } else if (strcmp(p, "tcp") == 0) { |
57ff5a10 | 3914 | filter_db_set(¤t_filter, TCP_DB); |
aba5acdf | 3915 | } else if (strcmp(p, "raw") == 0) { |
57ff5a10 | 3916 | filter_db_set(¤t_filter, RAW_DB); |
aba5acdf | 3917 | } else if (strcmp(p, "unix") == 0) { |
57ff5a10 VK |
3918 | filter_db_set(¤t_filter, UNIX_ST_DB); |
3919 | filter_db_set(¤t_filter, UNIX_DG_DB); | |
3920 | filter_db_set(¤t_filter, UNIX_SQ_DB); | |
1a5bad5a | 3921 | } else if (strcasecmp(p, "unix_stream") == 0 || |
aba5acdf | 3922 | strcmp(p, "u_str") == 0) { |
57ff5a10 | 3923 | filter_db_set(¤t_filter, UNIX_ST_DB); |
1a5bad5a | 3924 | } else if (strcasecmp(p, "unix_dgram") == 0 || |
aba5acdf | 3925 | strcmp(p, "u_dgr") == 0) { |
57ff5a10 | 3926 | filter_db_set(¤t_filter, UNIX_DG_DB); |
30b669d7 MY |
3927 | } else if (strcasecmp(p, "unix_seqpacket") == 0 || |
3928 | strcmp(p, "u_seq") == 0) { | |
57ff5a10 | 3929 | filter_db_set(¤t_filter, UNIX_SQ_DB); |
aba5acdf | 3930 | } else if (strcmp(p, "packet") == 0) { |
57ff5a10 VK |
3931 | filter_db_set(¤t_filter, PACKET_R_DB); |
3932 | filter_db_set(¤t_filter, PACKET_DG_DB); | |
aba5acdf SH |
3933 | } else if (strcmp(p, "packet_raw") == 0 || |
3934 | strcmp(p, "p_raw") == 0) { | |
57ff5a10 | 3935 | filter_db_set(¤t_filter, PACKET_R_DB); |
aba5acdf SH |
3936 | } else if (strcmp(p, "packet_dgram") == 0 || |
3937 | strcmp(p, "p_dgr") == 0) { | |
57ff5a10 | 3938 | filter_db_set(¤t_filter, PACKET_DG_DB); |
aba5acdf | 3939 | } else if (strcmp(p, "netlink") == 0) { |
57ff5a10 | 3940 | filter_db_set(¤t_filter, NETLINK_DB); |
aba5acdf SH |
3941 | } else { |
3942 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); | |
3943 | usage(); | |
3944 | } | |
3945 | p = p1 + 1; | |
3946 | } while (p1); | |
3947 | break; | |
3948 | } | |
3949 | case 's': | |
3950 | do_summary = 1; | |
3951 | break; | |
3952 | case 'D': | |
3953 | dump_tcpdiag = optarg; | |
3954 | break; | |
3955 | case 'F': | |
3956 | if (filter_fp) { | |
3957 | fprintf(stderr, "More than one filter file\n"); | |
3958 | exit(-1); | |
3959 | } | |
3960 | if (optarg[0] == '-') | |
3961 | filter_fp = stdin; | |
3962 | else | |
3963 | filter_fp = fopen(optarg, "r"); | |
3964 | if (!filter_fp) { | |
3965 | perror("fopen filter file"); | |
3966 | exit(-1); | |
3967 | } | |
3968 | break; | |
3969 | case 'v': | |
3970 | case 'V': | |
3971 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); | |
3972 | exit(0); | |
116ac927 RH |
3973 | case 'z': |
3974 | show_sock_ctx++; | |
3975 | case 'Z': | |
3976 | if (is_selinux_enabled() <= 0) { | |
3977 | fprintf(stderr, "ss: SELinux is not enabled.\n"); | |
3978 | exit(1); | |
3979 | } | |
3980 | show_proc_ctx++; | |
3981 | user_ent_hash_build(); | |
3982 | break; | |
95ce04bc VK |
3983 | case 'N': |
3984 | if (netns_switch(optarg)) | |
3985 | exit(1); | |
3986 | break; | |
fb2594c1 LC |
3987 | case 'K': |
3988 | current_filter.kill = 1; | |
3989 | break; | |
7a4559f6 DA |
3990 | case 'H': |
3991 | show_header = 0; | |
3992 | break; | |
aba5acdf | 3993 | case 'h': |
7a96e199 | 3994 | help(); |
f73105ab | 3995 | case '?': |
aba5acdf SH |
3996 | default: |
3997 | usage(); | |
3998 | } | |
3999 | } | |
4000 | ||
4001 | argc -= optind; | |
4002 | argv += optind; | |
4003 | ||
aba5acdf SH |
4004 | if (do_summary) { |
4005 | print_summary(); | |
4006 | if (do_default && argc == 0) | |
4007 | exit(0); | |
4008 | } | |
4009 | ||
aba5acdf SH |
4010 | while (argc > 0) { |
4011 | if (strcmp(*argv, "state") == 0) { | |
4012 | NEXT_ARG(); | |
4013 | if (!saw_states) | |
9db7bf15 VK |
4014 | state_filter = 0; |
4015 | state_filter |= scan_state(*argv); | |
aba5acdf SH |
4016 | saw_states = 1; |
4017 | } else if (strcmp(*argv, "exclude") == 0 || | |
4018 | strcmp(*argv, "excl") == 0) { | |
4019 | NEXT_ARG(); | |
4020 | if (!saw_states) | |
9db7bf15 VK |
4021 | state_filter = SS_ALL; |
4022 | state_filter &= ~scan_state(*argv); | |
aba5acdf SH |
4023 | saw_states = 1; |
4024 | } else { | |
aba5acdf SH |
4025 | break; |
4026 | } | |
4027 | argc--; argv++; | |
4028 | } | |
4029 | ||
9db7bf15 VK |
4030 | if (do_default) { |
4031 | state_filter = state_filter ? state_filter : SS_CONN; | |
4032 | filter_default_dbs(¤t_filter); | |
9db7bf15 VK |
4033 | } |
4034 | ||
57ff5a10 VK |
4035 | filter_states_set(¤t_filter, state_filter); |
4036 | filter_merge_defaults(¤t_filter); | |
4037 | ||
9db7bf15 VK |
4038 | if (resolve_services && resolve_hosts && |
4039 | (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)))) | |
4040 | init_service_resolver(); | |
4041 | ||
4042 | ||
4043 | if (current_filter.dbs == 0) { | |
4044 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); | |
4045 | exit(0); | |
4046 | } | |
4047 | if (current_filter.families == 0) { | |
4048 | fprintf(stderr, "ss: no families to show with such filter.\n"); | |
4049 | exit(0); | |
4050 | } | |
aba5acdf SH |
4051 | if (current_filter.states == 0) { |
4052 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); | |
4053 | exit(0); | |
4054 | } | |
4055 | ||
4056 | if (dump_tcpdiag) { | |
4057 | FILE *dump_fp = stdout; | |
acd1e437 | 4058 | |
aba5acdf SH |
4059 | if (!(current_filter.dbs & (1<<TCP_DB))) { |
4060 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); | |
4061 | exit(0); | |
4062 | } | |
4063 | if (dump_tcpdiag[0] != '-') { | |
4064 | dump_fp = fopen(dump_tcpdiag, "w"); | |
4065 | if (!dump_tcpdiag) { | |
4066 | perror("fopen dump file"); | |
4067 | exit(-1); | |
4068 | } | |
4069 | } | |
3fe5b534 | 4070 | inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); |
aba5acdf SH |
4071 | fflush(dump_fp); |
4072 | exit(0); | |
4073 | } | |
4074 | ||
1527a17e VK |
4075 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) |
4076 | usage(); | |
4077 | ||
aba5acdf SH |
4078 | netid_width = 0; |
4079 | if (current_filter.dbs&(current_filter.dbs-1)) | |
4080 | netid_width = 5; | |
4081 | ||
4082 | state_width = 0; | |
4083 | if (current_filter.states&(current_filter.states-1)) | |
4084 | state_width = 10; | |
4085 | ||
4086 | screen_width = 80; | |
4087 | if (isatty(STDOUT_FILENO)) { | |
4088 | struct winsize w; | |
4089 | ||
4090 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { | |
4091 | if (w.ws_col > 0) | |
4092 | screen_width = w.ws_col; | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | addrp_width = screen_width; | |
4097 | addrp_width -= netid_width+1; | |
4098 | addrp_width -= state_width+1; | |
4099 | addrp_width -= 14; | |
4100 | ||
4101 | if (addrp_width&1) { | |
4102 | if (netid_width) | |
4103 | netid_width++; | |
4104 | else if (state_width) | |
4105 | state_width++; | |
4106 | } | |
4107 | ||
4108 | addrp_width /= 2; | |
4109 | addrp_width--; | |
4110 | ||
4111 | serv_width = resolve_services ? 7 : 5; | |
4112 | ||
4113 | if (addrp_width < 15+serv_width+1) | |
4114 | addrp_width = 15+serv_width+1; | |
4115 | ||
ae665a52 | 4116 | addr_width = addrp_width - serv_width - 1; |
aba5acdf | 4117 | |
7a4559f6 DA |
4118 | if (show_header) { |
4119 | if (netid_width) | |
4120 | printf("%-*s ", netid_width, "Netid"); | |
4121 | if (state_width) | |
4122 | printf("%-*s ", state_width, "State"); | |
4123 | printf("%-6s %-6s ", "Recv-Q", "Send-Q"); | |
4124 | } | |
aba5acdf | 4125 | |
d68e00f7 | 4126 | /* Make enough space for the local/remote port field */ |
4127 | addr_width -= 13; | |
4128 | serv_width += 13; | |
2dc85485 | 4129 | |
7a4559f6 DA |
4130 | if (show_header) { |
4131 | printf("%*s:%-*s %*s:%-*s\n", | |
4132 | addr_width, "Local Address", serv_width, "Port", | |
4133 | addr_width, "Peer Address", serv_width, "Port"); | |
4134 | } | |
aba5acdf | 4135 | |
aba5acdf SH |
4136 | fflush(stdout); |
4137 | ||
6885e3bf CG |
4138 | if (follow_events) |
4139 | exit(handle_follow_request(¤t_filter)); | |
4140 | ||
aba5acdf SH |
4141 | if (current_filter.dbs & (1<<NETLINK_DB)) |
4142 | netlink_show(¤t_filter); | |
4143 | if (current_filter.dbs & PACKET_DBM) | |
4144 | packet_show(¤t_filter); | |
4145 | if (current_filter.dbs & UNIX_DBM) | |
4146 | unix_show(¤t_filter); | |
4147 | if (current_filter.dbs & (1<<RAW_DB)) | |
4148 | raw_show(¤t_filter); | |
4149 | if (current_filter.dbs & (1<<UDP_DB)) | |
4150 | udp_show(¤t_filter); | |
4151 | if (current_filter.dbs & (1<<TCP_DB)) | |
3fe5b534 | 4152 | tcp_show(¤t_filter, IPPROTO_TCP); |
351efcde | 4153 | if (current_filter.dbs & (1<<DCCP_DB)) |
3fe5b534 | 4154 | tcp_show(¤t_filter, IPPROTO_DCCP); |
116ac927 RH |
4155 | |
4156 | if (show_users || show_proc_ctx || show_sock_ctx) | |
4157 | user_ent_destroy(); | |
4158 | ||
aba5acdf SH |
4159 | return 0; |
4160 | } |