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