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