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