]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - misc/ss.c
TCPDIAG_VEGASINFO is not a #define, its an enum
[lisovros/iproute2_canprio.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 <resolv.h>
26 #include <dirent.h>
27 #include <fnmatch.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 <asm/byteorder.h>
36 #include <linux/tcp.h>
37 #include <linux/tcp_diag.h>
38
39 int resolve_hosts = 0;
40 int resolve_services = 1;
41 int preferred_family = AF_UNSPEC;
42 int show_options = 0;
43 int show_details = 0;
44 int show_users = 0;
45 int show_mem = 0;
46 int show_tcpinfo = 0;
47
48 int netid_width;
49 int state_width;
50 int addrp_width;
51 int addr_width;
52 int serv_width;
53 int screen_width;
54
55 static const char *TCP_PROTO = "tcp";
56 static const char *UDP_PROTO = "udp";
57 static const char *RAW_PROTO = "raw";
58 static const char *dg_proto = NULL;
59
60 enum
61 {
62         TCP_DB,
63         UDP_DB,
64         RAW_DB,
65         UNIX_DG_DB,
66         UNIX_ST_DB,
67         PACKET_DG_DB,
68         PACKET_R_DB,
69         NETLINK_DB,
70         MAX_DB
71 };
72
73 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
74 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB))
75 #define ALL_DB ((1<<MAX_DB)-1)
76
77 enum {
78         SS_UNKNOWN,
79         SS_ESTABLISHED,
80         SS_SYN_SENT,
81         SS_SYN_RECV,
82         SS_FIN_WAIT1,
83         SS_FIN_WAIT2,
84         SS_TIME_WAIT,
85         SS_CLOSE,
86         SS_CLOSE_WAIT,
87         SS_LAST_ACK,
88         SS_LISTEN,
89         SS_CLOSING,
90         SS_MAX
91 };
92
93 #define SS_ALL ((1<<SS_MAX)-1)
94
95 #include "ssfilter.h"
96
97 struct filter
98 {
99         int dbs;
100         int states;
101         int families;
102         struct ssfilter *f;
103 };
104
105 struct filter default_filter = {
106         dbs: (1<<TCP_DB),
107         states: SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
108         families: (1<<AF_INET)|(1<<AF_INET6),
109 };
110
111 struct filter current_filter;
112
113 int generic_proc_open(char *env, char *name)
114 {
115         char store[128];
116         char *p = getenv(env);
117         if (!p) {
118                 p = getenv("PROC_ROOT") ? : "/proc";
119                 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
120                 p = store;
121         }
122         return open(store, O_RDONLY);
123 }
124
125 int net_tcp_open(void)
126 {
127         return generic_proc_open("PROC_NET_TCP", "net/tcp");
128 }
129
130 int net_tcp6_open(void)
131 {
132         return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
133 }
134
135 int net_udp_open(void)
136 {
137         return generic_proc_open("PROC_NET_UDP", "net/udp");
138 }
139
140 int net_udp6_open(void)
141 {
142         return generic_proc_open("PROC_NET_UDP6", "net/udp6");
143 }
144
145 int net_raw_open(void)
146 {
147         return generic_proc_open("PROC_NET_RAW", "net/raw");
148 }
149
150 int net_raw6_open(void)
151 {
152         return generic_proc_open("PROC_NET_RAW6", "net/raw6");
153 }
154
155 int net_unix_open(void)
156 {
157         return generic_proc_open("PROC_NET_UNIX", "net/unix");
158 }
159
160 int net_packet_open(void)
161 {
162         return generic_proc_open("PROC_NET_PACKET", "net/packet");
163 }
164
165 int net_netlink_open(void)
166 {
167         return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
168 }
169
170 int slabinfo_open(void)
171 {
172         return generic_proc_open("PROC_SLABINFO", "slabinfo");
173 }
174
175 int net_sockstat_open(void)
176 {
177         return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
178 }
179
180 int net_sockstat6_open(void)
181 {
182         return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
183 }
184
185 int net_snmp_open(void)
186 {
187         return generic_proc_open("PROC_NET_SNMP", "net/snmp");
188 }
189
190 int net_netstat_open(void)
191 {
192         return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
193 }
194
195 int ephemeral_ports_open(void)
196 {
197         return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
198 }
199
200 int find_users(int ino, char *buf, int buflen)
201 {
202         char pattern[64];
203         int  pattern_len;
204         char *ptr = buf;
205         char name[1024];
206         DIR *dir;
207         struct dirent *d;
208         int cnt = 0;
209         int nameoff;
210
211         if (!ino)
212                 return 0;
213
214         sprintf(pattern, "socket:[%d]", ino);
215         pattern_len = strlen(pattern);
216
217         strncpy(name, getenv("PROC_ROOT") ? : "/proc/", sizeof(name)/2);
218         name[sizeof(name)/2] = 0;
219         if (strlen(name) == 0 ||
220             name[strlen(name)-1] != '/')
221                 strcat(name, "/");
222         nameoff = strlen(name);
223         if ((dir = opendir(name)) == NULL)
224                 return 0;
225
226         while ((d = readdir(dir)) != NULL) {
227                 DIR *dir1;
228                 struct dirent *d1;
229                 int pid;
230                 int pos;
231                 char crap;
232                 char process[16];
233
234                 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
235                         continue;
236
237                 sprintf(name+nameoff, "%d/fd/", pid);
238                 pos = strlen(name);
239                 if ((dir1 = opendir(name)) == NULL)
240                         continue;
241
242                 process[0] = 0;
243
244                 while ((d1 = readdir(dir1)) != NULL) {
245                         int fd, n;
246                         char lnk[64];
247
248                         if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
249                                 continue;
250
251                         sprintf(name+pos, "%d", fd);
252                         n = readlink(name, lnk, sizeof(lnk)-1);
253                         if (n != pattern_len ||
254                             memcmp(lnk, pattern, n))
255                                 continue;
256
257                         if (ptr-buf >= buflen-1)
258                                 break;
259
260                         if (process[0] == 0) {
261                                 char tmp[1024];
262                                 FILE *fp;
263                                 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
264                                          getenv("PROC_ROOT") ? : "/proc", pid);
265                                 if ((fp = fopen(tmp, "r")) != NULL) {
266                                         fscanf(fp, "%*d (%[^)])", process);
267                                         fclose(fp);
268                                 }
269                         }
270
271                         snprintf(ptr, buflen-(ptr-buf), "(\"%s\",%d,%d),", process, pid, fd);
272                         ptr += strlen(ptr);
273                         cnt++;
274                 }
275                 closedir(dir1);
276         }
277         closedir(dir);
278         if (ptr != buf)
279                 ptr[-1] = 0;
280         return cnt;
281 }
282
283
284 /* Get stats from slab */
285
286 struct slabstat
287 {
288         int socks;
289         int tcp_ports;
290         int tcp_tws;
291         int tcp_syns;
292         int skbs;
293 };
294
295 struct slabstat slabstat;
296
297 const char *slabstat_ids[] = 
298 {
299         "sock",
300         "tcp_bind_bucket",
301         "tcp_tw_bucket",
302         "tcp_open_request",
303         "skbuff_head_cache",
304 };
305
306 int get_slabstat(struct slabstat *s)
307 {
308         char buf[256];
309         FILE *fp;
310         int cnt;
311
312         memset(s, 0, sizeof(*s));
313
314         if ((fp = fdopen(slabinfo_open(), "r")) == NULL)
315                 return -1;
316
317         cnt = sizeof(*s)/sizeof(int);
318
319         fgets(buf, sizeof(buf), fp);
320         while(fgets(buf, sizeof(buf), fp) != NULL) {
321                 int i;
322                 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
323                         if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
324                                 sscanf(buf, "%*s%d", ((int *)s) + i);
325                                 cnt--;
326                                 break;
327                         }
328                 }
329                 if (cnt <= 0)
330                         break;
331         }
332
333         fclose(fp);
334         return 0;
335 }
336
337 static const char *sstate_name[] = {
338         "UNKNOWN",
339         [TCP_ESTABLISHED] = "ESTAB",
340         [TCP_SYN_SENT] = "SYN-SENT",
341         [TCP_SYN_RECV] = "SYN-RECV",
342         [TCP_FIN_WAIT1] = "FIN-WAIT-1",
343         [TCP_FIN_WAIT2] = "FIN-WAIT-2",
344         [TCP_TIME_WAIT] = "TIME-WAIT",
345         [TCP_CLOSE] = "UNCONN",
346         [TCP_CLOSE_WAIT] = "CLOSE-WAIT",
347         [TCP_LAST_ACK] = "LAST-ACK",
348         [TCP_LISTEN] =  "LISTEN",
349         [TCP_CLOSING] = "CLOSING",
350 };
351
352 static const char *sstate_namel[] = {
353         "UNKNOWN",
354         [TCP_ESTABLISHED] = "established",
355         [TCP_SYN_SENT] = "syn-sent",
356         [TCP_SYN_RECV] = "syn-recv",
357         [TCP_FIN_WAIT1] = "fin-wait-1",
358         [TCP_FIN_WAIT2] = "fin-wait-2",
359         [TCP_TIME_WAIT] = "time-wait",
360         [TCP_CLOSE] = "unconnected",
361         [TCP_CLOSE_WAIT] = "close-wait",
362         [TCP_LAST_ACK] = "last-ack",
363         [TCP_LISTEN] =  "listening",
364         [TCP_CLOSING] = "closing",
365 };
366
367 struct tcpstat
368 {
369         inet_prefix     local;
370         inet_prefix     remote;
371         int             lport;
372         int             rport;
373         int             state;
374         int             rq, wq;
375         int             timer;
376         int             timeout;
377         int             retrs;
378         int             ino;
379         int             probes;
380         int             uid;
381         int             refcnt;
382         unsigned long long sk;
383         int             rto, ato, qack, cwnd, ssthresh;
384 };
385
386 static const char *tmr_name[] = {
387         "off",
388         "on",
389         "keepalive",
390         "timewait",
391         "persist",
392         "unknown"
393 };
394
395 char *print_ms_timer(int timeout)
396 {
397         static char buf[64];
398         int secs, msecs, minutes;
399         if (timeout < 0)
400                 timeout = 0;
401         secs = timeout/1000;
402         minutes = secs/60;
403         secs = secs%60;
404         msecs = timeout%1000;
405         buf[0] = 0;
406         if (minutes) {
407                 msecs = 0;
408                 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
409                 if (minutes > 9)
410                         secs = 0;
411         }
412         if (secs) {
413                 if (secs > 9)
414                         msecs = 0;
415                 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
416         }
417         if (msecs)
418                 sprintf(buf+strlen(buf), "%03dms", msecs);
419         return buf;
420 };
421
422 char *print_hz_timer(int timeout)
423 {
424         int hz = get_hz();
425         return print_ms_timer(((timeout*1000) + hz-1)/hz);
426 };
427
428 struct scache
429 {
430         struct scache *next;
431         int port;
432         char *name;
433         const char *proto;
434 };
435
436 struct scache *rlist;
437
438 void init_service_resolver(void)
439 {
440         char buf[128];
441         FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
442         if (fp) {
443                 fgets(buf, sizeof(buf), fp);
444                 while (fgets(buf, sizeof(buf), fp) != NULL) {
445                         unsigned int progn, port;
446                         char proto[128], prog[128];
447                         if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
448                                    &port, prog+4) == 4) {
449                                 struct scache *c = malloc(sizeof(*c));
450                                 if (c) {
451                                         c->port = port;
452                                         memcpy(prog, "rpc.", 4);
453                                         c->name = strdup(prog);
454                                         if (strcmp(proto, TCP_PROTO) == 0)
455                                                 c->proto = TCP_PROTO;
456                                         else if (strcmp(proto, UDP_PROTO) == 0)
457                                                 c->proto = UDP_PROTO;
458                                         else
459                                                 c->proto = NULL;
460                                         c->next = rlist;
461                                         rlist = c;
462                                 }
463                         }
464                 }
465         }
466 }
467
468 const char *__resolve_service(int port)
469 {
470         struct scache *c;
471
472         for (c = rlist; c; c = c->next) {
473                 if (c->port == port && c->proto == dg_proto)
474                         return c->name;
475         }
476
477         /* Even do not try default linux ephemeral port ranges:
478          * default /etc/services contains so much of useless crap
479          * wouldbe "allocated" to this area that resolution
480          * is really harmful. I shrug each time when seeing
481          * "socks" or "cfinger" in dumps.
482          */
483         if (port < 32768 && (port < 1024 || port > 4999)) {
484                 static int notfirst;
485                 struct servent *se;
486                 if (!notfirst) {
487                         setservent(1);
488                         notfirst = 1;
489                 } 
490                 se = getservbyport(htons(port), dg_proto);
491                 if (se)
492                         return se->s_name;
493         }
494
495         return NULL;
496 }
497
498
499 const char *resolve_service(int port)
500 {
501         static char buf[128];
502         static struct scache cache[256];
503
504         if (port == 0) {
505                 buf[0] = '*';
506                 buf[1] = 0;
507                 return buf;
508         }
509
510         if (resolve_services) {
511                 if (dg_proto == RAW_PROTO) {
512                         return inet_proto_n2a(port, buf, sizeof(buf));
513                 } else {
514                         struct scache *c;
515                         const char *res;
516                         int hash = (port^(((unsigned long)dg_proto)>>2))&255;
517
518                         for (c = &cache[hash]; c; c = c->next) { 
519                                 if (c->port == port &&
520                                     c->proto == dg_proto) {
521                                         if (c->name)
522                                                 return c->name;
523                                         goto do_numeric;
524                                 }
525                         }
526
527                         if ((res = __resolve_service(port)) != NULL) {
528                                 if ((c = malloc(sizeof(*c))) == NULL)
529                                         goto do_numeric;
530                         } else {
531                                 c = &cache[hash];
532                                 if (c->name)
533                                         free(c->name);
534                         }
535                         c->port = port;
536                         c->name = NULL;
537                         c->proto = dg_proto;
538                         if (res) {
539                                 c->name = strdup(res);
540                                 c->next = cache[hash].next;
541                                 cache[hash].next = c;
542                         }
543                         if (c->name)
544                                 return c->name;
545                 }
546         }
547
548         do_numeric:
549         sprintf(buf, "%u", port);
550         return buf;
551 }
552
553 void formatted_print(const inet_prefix *a, int port)
554 {
555         char buf[1024];
556         const char *ap = buf;
557         int est_len;
558
559         est_len = addr_width;
560
561         if (a->family == AF_INET) {
562                 if (a->data[0] == 0) {
563                         buf[0] = '*';
564                         buf[1] = 0;
565                 } else {
566                         ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
567                 }
568         } else {
569                 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
570                 est_len = strlen(ap);
571                 if (est_len <= addr_width)
572                         est_len = addr_width;
573                 else
574                         est_len = addr_width + ((est_len-addr_width+3)/4)*4;
575         }
576         printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
577 }
578
579 struct aafilter
580 {
581         inet_prefix     addr;
582         int             port;
583         struct aafilter *next;
584 };
585
586 int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, int plen)
587 {
588         if (!inet_addr_match(a, p, plen))
589                 return 0;
590
591         /* Cursed "v4 mapped" addresses: v4 mapped socket matches
592          * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
593          * sockets. Fair? */
594         if (p->family == AF_INET && a->family == AF_INET6) {
595                 if (a->data[0] == 0 && a->data[1] == 0 &&
596                     a->data[2] == htonl(0xffff)) {
597                         inet_prefix tmp = *a;
598                         tmp.data[0] = a->data[3];
599                         return inet_addr_match(&tmp, p, plen);
600                 }
601         }
602         return 1;
603 }
604
605 int unix_match(const inet_prefix *a, const inet_prefix *p)
606 {
607         char *addr, *pattern;
608         memcpy(&addr, a->data, sizeof(addr));
609         memcpy(&pattern, p->data, sizeof(pattern));
610         if (pattern == NULL)
611                 return 1;
612         if (addr == NULL)
613                 addr = "";
614         return !fnmatch(pattern, addr, 0);
615 }
616
617 int run_ssfilter(struct ssfilter *f, struct tcpstat *s)
618 {
619         switch (f->type) {
620                 case SSF_S_AUTO:
621         {
622                 static int low, high=65535;
623
624                 if (s->local.family == AF_UNIX) {
625                         char *p;
626                         memcpy(&p, s->local.data, sizeof(p));
627                         return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
628                                              strspn(p+1, "0123456789abcdef") == 5); 
629                 }
630                 if (s->local.family == AF_PACKET)
631                         return s->lport == 0 && s->local.data == 0;
632                 if (s->local.family == AF_NETLINK)
633                         return s->lport < 0;
634
635                 if (!low) {
636                         FILE *fp = fdopen(ephemeral_ports_open(), "r");
637                         if (fp) {
638                                 fscanf(fp, "%d%d", &low, &high);
639                                 fclose(fp);
640                         }
641                 }
642                 return s->lport >= low && s->lport <= high;
643         }
644                 case SSF_DCOND:
645         {
646                 struct aafilter *a = (void*)f->pred;
647                 if (a->addr.family == AF_UNIX)
648                         return unix_match(&s->remote, &a->addr);
649                 if (a->port != -1 && a->port != s->rport)
650                         return 0;
651                 if (a->addr.bitlen) {
652                         do {
653                                 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
654                                         return 1;
655                         } while ((a = a->next) != NULL);
656                         return 0;
657                 }
658                 return 1;
659         }
660                 case SSF_SCOND:
661         {
662                 struct aafilter *a = (void*)f->pred;
663                 if (a->addr.family == AF_UNIX)
664                         return unix_match(&s->local, &a->addr);
665                 if (a->port != -1 && a->port != s->lport)
666                         return 0;
667                 if (a->addr.bitlen) {
668                         do {
669                                 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
670                                         return 1;
671                         } while ((a = a->next) != NULL); 
672                         return 0;
673                 }
674                 return 1;
675         }
676                 case SSF_D_GE:
677         {
678                 struct aafilter *a = (void*)f->pred;
679                 return s->rport >= a->port;
680         }
681                 case SSF_D_LE:
682         {
683                 struct aafilter *a = (void*)f->pred;
684                 return s->rport <= a->port;
685         }
686                 case SSF_S_GE:
687         {
688                 struct aafilter *a = (void*)f->pred;
689                 return s->lport >= a->port;
690         }
691                 case SSF_S_LE:
692         {
693                 struct aafilter *a = (void*)f->pred;
694                 return s->lport <= a->port;
695         }
696
697                 /* Yup. It is recursion. Sorry. */
698                 case SSF_AND:
699                 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
700                 case SSF_OR:
701                 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
702                 case SSF_NOT:
703                 return !run_ssfilter(f->pred, s);
704                 default:
705                 abort();
706         }
707 }
708
709 /* Relocate external jumps by reloc. */ 
710 void ssfilter_patch(char *a, int len, int reloc)
711 {
712         while (len > 0) {
713                 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)a;
714                 if (op->no == len+4)
715                         op->no += reloc;
716                 len -= op->yes;
717                 a += op->yes;
718         }
719         if (len < 0)
720                 abort();
721 }
722
723 int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
724 {
725         switch (f->type) {
726                 case SSF_S_AUTO:
727         {
728                 if (!(*bytecode=malloc(4))) abort();
729                 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_AUTO, 4, 8 };
730                 return 8;
731         }
732                 case SSF_DCOND:
733                 case SSF_SCOND:
734         {
735                 struct aafilter *a = (void*)f->pred;
736                 struct aafilter *b;
737                 char *ptr;
738                 int  code = (f->type == SSF_DCOND ? TCPDIAG_BC_D_COND : TCPDIAG_BC_S_COND);
739                 int len = 0;
740
741                 for (b=a; b; b=b->next) {
742                         len += 4 + sizeof(struct tcpdiag_hostcond);
743                         if (a->addr.family == AF_INET6)
744                                 len += 16;
745                         else
746                                 len += 4;
747                         if (b->next)
748                                 len += 4;
749                 }
750                 if (!(ptr = malloc(len))) abort();
751                 *bytecode = ptr;
752                 for (b=a; b; b=b->next) {
753                         struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op *)ptr;
754                         int alen = (a->addr.family == AF_INET6 ? 16 : 4);
755                         int oplen = alen + 4 + sizeof(struct tcpdiag_hostcond);
756                         struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(ptr+4);
757
758                         *op = (struct tcpdiag_bc_op){ code, oplen, oplen+4 };
759                         cond->family = a->addr.family;
760                         cond->port = a->port;
761                         cond->prefix_len = a->addr.bitlen;
762                         memcpy(cond->addr, a->addr.data, alen);
763                         ptr += oplen;
764                         if (b->next) {
765                                 op = (struct tcpdiag_bc_op *)ptr;
766                                 *op = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, len - (ptr-*bytecode)};
767                                 ptr += 4;
768                         }
769                 }
770                 return ptr - *bytecode;
771         }
772                 case SSF_D_GE:
773         {
774                 struct aafilter *x = (void*)f->pred;
775                 if (!(*bytecode=malloc(8))) abort();
776                 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_D_GE, 8, 12 };
777                 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
778                 return 8;
779         }
780                 case SSF_D_LE:
781         {
782                 struct aafilter *x = (void*)f->pred;
783                 if (!(*bytecode=malloc(8))) abort();
784                 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_D_LE, 8, 12 };
785                 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
786                 return 8;
787         }
788                 case SSF_S_GE:
789         {
790                 struct aafilter *x = (void*)f->pred;
791                 if (!(*bytecode=malloc(8))) abort();
792                 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_S_GE, 8, 12 };
793                 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
794                 return 8;
795         }
796                 case SSF_S_LE:
797         {
798                 struct aafilter *x = (void*)f->pred;
799                 if (!(*bytecode=malloc(8))) abort();
800                 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_S_LE, 8, 12 };
801                 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
802                 return 8;
803         }
804
805                 case SSF_AND:
806         {
807                 char *a1, *a2, *a, l1, l2;
808                 l1 = ssfilter_bytecompile(f->pred, &a1);
809                 l2 = ssfilter_bytecompile(f->post, &a2);
810                 if (!(a = malloc(l1+l2))) abort();
811                 memcpy(a, a1, l1);
812                 memcpy(a+l1, a2, l2);
813                 free(a1); free(a2);
814                 ssfilter_patch(a, l1, l2);
815                 *bytecode = a;
816                 return l1+l2;
817         }
818                 case SSF_OR:
819         {
820                 char *a1, *a2, *a, l1, l2;
821                 l1 = ssfilter_bytecompile(f->pred, &a1);
822                 l2 = ssfilter_bytecompile(f->post, &a2);
823                 if (!(a = malloc(l1+l2+4))) abort();
824                 memcpy(a, a1, l1);
825                 memcpy(a+l1+4, a2, l2);
826                 free(a1); free(a2);
827                 *(struct tcpdiag_bc_op*)(a+l1) = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, l2+4 };
828                 *bytecode = a;
829                 return l1+l2+4;
830         }
831                 case SSF_NOT:
832         {
833                 char *a1, *a, l1;
834                 l1 = ssfilter_bytecompile(f->pred, &a1);
835                 if (!(a = malloc(l1+4))) abort();
836                 memcpy(a, a1, l1);
837                 free(a1);
838                 *(struct tcpdiag_bc_op*)(a+l1) = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, 8 };
839                 *bytecode = a;
840                 return l1+4;
841         }
842                 default:
843                 abort();
844         }
845 }
846
847 int remember_he(struct aafilter *a, struct hostent *he)
848 {
849         char **ptr = he->h_addr_list; 
850         int cnt = 0;
851         int len;
852
853         if (he->h_addrtype == AF_INET)
854                 len = 4;
855         else if (he->h_addrtype == AF_INET6)
856                 len = 16;
857         else
858                 return 0;
859
860         while (*ptr) {
861                 struct aafilter *b = a;
862                 if (a->addr.bitlen) {
863                         if ((b = malloc(sizeof(*b))) == NULL)
864                                 return cnt;
865                         *b = *a;
866                         b->next = a->next;
867                         a->next = b;
868                 }
869                 memcpy(b->addr.data, *ptr, len);
870                 b->addr.bytelen = len;
871                 b->addr.bitlen = len*8;
872                 b->addr.family = he->h_addrtype;
873                 ptr++;
874                 cnt++;
875         }
876         return cnt;
877 }
878
879 int get_dns_host(struct aafilter *a, char *addr, int fam)
880 {
881         static int notfirst;
882         int cnt = 0;
883         struct hostent *he;
884
885         a->addr.bitlen = 0;
886         if (!notfirst) {
887                 sethostent(1);
888                 notfirst = 1;
889         }
890         he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
891         if (he)
892                 cnt = remember_he(a, he);
893         if (fam == AF_UNSPEC) {
894                 he = gethostbyname2(addr, AF_INET6);
895                 if (he)
896                         cnt += remember_he(a, he);
897         }
898         return !cnt;
899 }
900
901 int xll_initted = 0;
902
903 void xll_init(void)
904 {
905         struct rtnl_handle rth;
906         rtnl_open(&rth, 0);
907         ll_init_map(&rth);
908         rtnl_close(&rth);
909         xll_initted = 1;
910 }
911
912 const char *xll_index_to_name(int index)
913 {
914         if (!xll_initted)
915                 xll_init();
916         return ll_index_to_name(index);
917 }
918
919 int xll_name_to_index(char *dev)
920 {
921         if (!xll_initted)
922                 xll_init();
923         return ll_name_to_index(dev);
924 }
925
926 void *parse_hostcond(char *addr)
927 {
928         char *port = NULL;
929         struct aafilter a;
930         struct aafilter *res;
931         int fam = preferred_family;
932
933         memset(&a, 0, sizeof(a));
934         a.port = -1;
935
936         if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
937                 char *p;
938                 a.addr.family = AF_UNIX;
939                 if (strncmp(addr, "unix:", 5) == 0)
940                         addr+=5;
941                 p = strdup(addr);
942                 a.addr.bitlen = 8*strlen(p);
943                 memcpy(a.addr.data, &p, sizeof(p));
944                 goto out;
945         }
946
947         if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
948                 a.addr.family = AF_PACKET;
949                 a.addr.bitlen = 0;
950                 if (strncmp(addr, "link:", 5) == 0)
951                         addr+=5;
952                 port = strchr(addr, ':');
953                 if (port) {
954                         *port = 0;
955                         if (port[1] && strcmp(port+1, "*")) {
956                                 if (get_integer(&a.port, port+1, 0)) {
957                                         if ((a.port = xll_name_to_index(port+1)) <= 0)
958                                                 return NULL;
959                                 }
960                         }
961                 }
962                 if (addr[0] && strcmp(addr, "*")) {
963                         unsigned short tmp;
964                         a.addr.bitlen = 32;
965                         if (ll_proto_a2n(&tmp, addr))
966                                 return NULL;
967                         a.addr.data[0] = ntohs(tmp);
968                 }
969                 goto out;
970         }
971
972         if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
973                 a.addr.family = AF_NETLINK;
974                 a.addr.bitlen = 0;
975                 if (strncmp(addr, "netlink:", 8) == 0)
976                         addr+=8;
977                 port = strchr(addr, ':');
978                 if (port) {
979                         *port = 0;
980                         if (port[1] && strcmp(port+1, "*")) {
981                                 if (get_integer(&a.port, port+1, 0)) {
982                                         if (strcmp(port+1, "kernel") == 0)
983                                                 a.port = 0;
984                                         else
985                                                 return NULL;
986                                 }
987                         }
988                 }
989                 if (addr[0] && strcmp(addr, "*")) {
990                         a.addr.bitlen = 32;
991                         if (get_u32(a.addr.data, addr, 0)) {
992                                 if (strcmp(addr, "rtnl") == 0)
993                                         a.addr.data[0] = 0;
994                                 else if (strcmp(addr, "fw") == 0)
995                                         a.addr.data[0] = 3;
996                                 else if (strcmp(addr, "tcpdiag") == 0)
997                                         a.addr.data[0] = 4;
998                                 else
999                                         return NULL;
1000                         }
1001                 }
1002                 goto out;
1003         }
1004
1005         if (strncmp(addr, "inet:", 5) == 0) {
1006                 addr += 5;
1007                 fam = AF_INET;
1008         } else if (strncmp(addr, "inet6:", 6) == 0) {
1009                 addr += 6;
1010                 fam = AF_INET6;
1011         }
1012
1013         /* URL-like literal [] */
1014         if (addr[0] == '[') {
1015                 addr++;
1016                 if ((port = strchr(addr, ']')) == NULL)
1017                         return NULL;
1018                 *port++ = 0;
1019         } else if (addr[0] == '*') {
1020                 port = addr+1;
1021         } else {
1022                 port = strrchr(strchr(addr, '/') ? : addr, ':');
1023         }
1024         if (port && *port) {
1025                 if (*port != ':')
1026                         return NULL;
1027                 *port++ = 0;
1028                 if (*port && *port != '*') {
1029                         if (get_integer(&a.port, port, 0)) {
1030                                 struct servent *se1 = NULL;
1031                                 struct servent *se2 = NULL;
1032                                 if (current_filter.dbs&(1<<UDP_DB))
1033                                         se1 = getservbyname(port, UDP_PROTO);
1034                                 if (current_filter.dbs&(1<<TCP_DB))
1035                                         se2 = getservbyname(port, TCP_PROTO);
1036                                 if (se1 && se2 && se1->s_port != se2->s_port) {
1037                                         fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1038                                         return NULL;
1039                                 }
1040                                 if (!se1)
1041                                         se1 = se2;
1042                                 if (se1) {
1043                                         a.port = ntohs(se1->s_port);
1044                                 } else {
1045                                         struct scache *s;
1046                                         for (s = rlist; s; s = s->next) {
1047                                                 if ((s->proto == UDP_PROTO &&
1048                                                      (current_filter.dbs&(1<<UDP_DB))) ||
1049                                                     (s->proto == TCP_PROTO &&
1050                                                      (current_filter.dbs&(1<<TCP_DB)))) {
1051                                                         if (s->name && strcmp(s->name, port) == 0) {
1052                                                                 if (a.port > 0 && a.port != s->port) {
1053                                                                         fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1054                                                                         return NULL;
1055                                                                 }
1056                                                                 a.port = s->port;
1057                                                         }
1058                                                 }
1059                                         }
1060                                         if (a.port <= 0) {
1061                                                 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1062                                                 return NULL;
1063                                         }
1064                                 }
1065                         }
1066                 }
1067         }
1068         if (addr && *addr && *addr != '*') {
1069                 if (get_prefix_1(&a.addr, addr, fam)) {
1070                         if (get_dns_host(&a, addr, fam)) {
1071                                 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1072                                 return NULL;
1073                         }
1074                 }
1075         }
1076
1077         out:
1078         res = malloc(sizeof(*res));
1079         if (res)
1080                 memcpy(res, &a, sizeof(a));
1081         return res;
1082 }
1083
1084 int tcp_show_line(char *line, struct filter *f, int family)
1085 {
1086         struct tcpstat s;
1087         char *loc, *rem, *data;
1088         char opt[256];
1089         int n;
1090         char *p;
1091         
1092         if ((p = strchr(line, ':')) == NULL)
1093                 return -1;
1094         loc = p+2;
1095         
1096         if ((p = strchr(loc, ':')) == NULL)
1097                 return -1;
1098         p[5] = 0;
1099         rem = p+6;
1100         
1101         if ((p = strchr(rem, ':')) == NULL)
1102                 return -1;
1103         p[5] = 0;
1104         data = p+6;
1105         
1106         do {
1107                 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1108
1109                 if (!(f->states & (1<<state)))
1110                         return 0;
1111         } while (0);
1112         
1113         s.local.family = s.remote.family = family;
1114         if (family == AF_INET) {
1115                 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1116                 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1117                 s.local.bytelen = s.remote.bytelen = 4;
1118         } else {
1119                 sscanf(loc, "%08x%08x%08x%08x:%x",
1120                        s.local.data,
1121                        s.local.data+1,
1122                        s.local.data+2,
1123                        s.local.data+3,
1124                        &s.lport);
1125                 sscanf(rem, "%08x%08x%08x%08x:%x",
1126                        s.remote.data,
1127                        s.remote.data+1,
1128                        s.remote.data+2,
1129                        s.remote.data+3,
1130                        &s.rport);
1131                 s.local.bytelen = s.remote.bytelen = 16;
1132         }
1133         
1134         if (f->f && run_ssfilter(f->f, &s) == 0)
1135                 return 0;
1136         
1137         opt[0] = 0;
1138         n = sscanf(data, "%x %x:%x %x:%x %x %d %d %d %d %llx %d %d %d %d %d %[^\n]\n",
1139                    &s.state, &s.wq, &s.rq,
1140                    &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino,
1141                    &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack,
1142                    &s.cwnd, &s.ssthresh, opt);
1143         
1144         if (n < 17)
1145                 opt[0] = 0;
1146         
1147         if (n < 12) {
1148                 s.rto = 0;
1149                 s.cwnd = 2;
1150                 s.ssthresh = -1;
1151                 s.ato = s.qack = 0;
1152         }
1153         
1154         if (netid_width)
1155                 printf("%-*s ", netid_width, "tcp");
1156         if (state_width)
1157                 printf("%-*s ", state_width, sstate_name[s.state]);
1158         
1159         printf("%-6d %-6d ", s.rq, s.wq);
1160         
1161         formatted_print(&s.local, s.lport);
1162         formatted_print(&s.remote, s.rport);
1163         
1164         if (show_options) {
1165                 if (s.timer) {
1166                         if (s.timer > 4)
1167                                 s.timer = 5;
1168                         printf(" timer:(%s,%s,%d)",
1169                                tmr_name[s.timer],
1170                                print_hz_timer(s.timeout),
1171                                s.timer != 1 ? s.probes : s.retrs);
1172                 }
1173         }
1174         if (show_tcpinfo) {
1175                 if (s.rto && s.rto != 3*get_hz())
1176                         printf(" rto:%g", (double)s.rto/get_hz());
1177                 if (s.ato)
1178                         printf(" ato:%g", (double)s.ato/get_hz());
1179                 if (s.cwnd != 2)
1180                         printf(" cwnd:%d", s.cwnd);
1181                 if (s.ssthresh != -1)
1182                         printf(" ssthresh:%d", s.ssthresh);
1183                 if (s.qack/2)
1184                         printf(" qack:%d", s.qack/2);
1185                 if (s.qack&1)
1186                         printf(" bidir");
1187         }
1188         if (show_users) {
1189                 char ubuf[4096];
1190                 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1191                         printf(" users:(%s)", ubuf);
1192         }
1193         if (show_details) {
1194                 if (s.uid)
1195                         printf(" uid:%u", (unsigned)s.uid);
1196                 printf(" ino:%u", (unsigned)s.ino);
1197                 printf(" sk:%llx", s.sk);
1198                 if (opt[0])
1199                         printf(" opt:\"%s\"", opt);
1200         }
1201         printf("\n");
1202
1203         return 0;
1204 }
1205
1206 int generic_record_read(int fd, char *buf, int bufsize,
1207                         int (*worker)(char*, struct filter *, int),
1208                         struct filter *f, int fam)
1209 {
1210         int n;
1211         int recsize;
1212         int eof = 0;
1213         char *p;
1214
1215         /* Load the first chunk and calculate record length from it. */
1216         n = read(fd, buf, bufsize);
1217         if (n < 0)
1218                 goto outerr;
1219         /* I _know_ that this is wrong, do not remind. :-)
1220          * But this works nowadays. */
1221         if (n < bufsize)
1222                 eof = 1;
1223         p = memchr(buf, '\n', n);
1224         if (p == NULL || (p-buf) >= n)
1225                 goto outwrongformat;
1226         recsize = (p-buf)+1;
1227         p = buf+recsize;
1228
1229         for (;;) {
1230                 while ((p+recsize) - buf <= n) {
1231                         if (p[recsize-1] != '\n')
1232                                 goto outwrongformat;
1233                         p[recsize-1] = 0;
1234                         if (worker(p, f, fam) < 0)
1235                                 goto done;
1236                         p += recsize;
1237                 }
1238                 if (!eof) {
1239                         int remains = (buf+bufsize) - p;
1240                         memcpy(buf, p, remains);
1241                         p = buf+remains;
1242                         n = read(fd, p, (buf+bufsize) - p);
1243                         if (n < 0)
1244                                 goto outerr;
1245                         if (n < (buf+bufsize) - p) {
1246                                 eof = 1;
1247                                 if (n == 0) {
1248                                         if (remains)
1249                                                 goto outwrongformat;
1250                                         goto done;
1251                                 }
1252                         }
1253                         n += remains;
1254                         p = buf;
1255                 } else {
1256                         if (p != buf+n)
1257                                 goto outwrongformat;
1258                         goto done;
1259                 }
1260         }
1261 done:
1262         return 0;
1263
1264 outwrongformat:
1265         errno = EINVAL;
1266 outerr:
1267         return -1;
1268 }
1269
1270 void tcp_show_info(struct nlmsghdr *nlh, struct tcpdiagmsg *r)
1271 {
1272         struct rtattr * tb[TCPDIAG_MAX+1];
1273         const struct tcpdiag_meminfo *minfo = NULL;
1274         const struct tcp_info *info = NULL;
1275         const struct tcpvegas_info *vinfo = NULL;
1276
1277         memset(tb, 0, sizeof(tb));
1278         parse_rtattr(tb, TCPDIAG_MAX, (struct rtattr*)(r+1),
1279                      nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
1280
1281         if (tb[TCPDIAG_MEMINFO])
1282                 minfo = RTA_DATA(tb[TCPDIAG_MEMINFO]);
1283         if (tb[TCPDIAG_INFO])
1284                 info = RTA_DATA(tb[TCPDIAG_INFO]);
1285         if (tb[TCPDIAG_VEGASINFO]) 
1286                 vinfo = RTA_DATA(tb[TCPDIAG_VEGASINFO]);
1287
1288         if (minfo) {
1289                 printf(" mem:(r%u,w%u,f%u,t%u)",
1290                        minfo->tcpdiag_rmem,
1291                        minfo->tcpdiag_wmem,
1292                        minfo->tcpdiag_fmem,
1293                        minfo->tcpdiag_tmem);
1294         }
1295
1296         if (info) {
1297 #ifdef TCP_INFO
1298                 if (info->tcpi_rto && info->tcpi_rto != 3000000)
1299                         printf(" rto:%g", (double)info->tcpi_rto/1000);
1300                 if (info->tcpi_rtt)
1301                         printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000,
1302                                (double)info->tcpi_rttvar/1000);
1303                 if (info->tcpi_ato)
1304                         printf(" ato:%g", (double)info->tcpi_ato/1000);
1305                 if (info->tcpi_snd_cwnd != 2)
1306                         printf(" cwnd:%d", info->tcpi_snd_cwnd);
1307                 if (info->tcpi_snd_ssthresh < 0xFFFF)
1308                         printf(" ssthresh:%d", info->tcpi_snd_ssthresh);
1309
1310                 if (vinfo) {
1311                         if (vinfo->tcpv_enabled)
1312                                 printf(" vegas");
1313                         if (vinfo->tcpv_rtt && 
1314                             vinfo->tcpv_rtt != 0x7fffffff &&
1315                             info->tcpi_snd_mss && 
1316                             info->tcpi_snd_cwnd) {
1317                                 printf(" bw:%g", 
1318                                        (double) info->tcpi_snd_cwnd *
1319                                        (double) info->tcpi_snd_mss *
1320                                        8000000. / (double) vinfo->tcpv_rtt);
1321                         }
1322                 }
1323         }
1324 #else
1325 #warning No TCP_INFO. Please, do not repeat this experiment, use right kernel.
1326         printf(" MORE_INFO_PROVIDED_YOU_COMPILED_SS_RIGHT");
1327 #endif
1328 }
1329
1330 int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f)
1331 {
1332         struct tcpdiagmsg *r = NLMSG_DATA(nlh);
1333         struct tcpstat s;
1334
1335         s.state = r->tcpdiag_state;
1336         s.local.family = s.remote.family = r->tcpdiag_family;
1337         s.lport = ntohs(r->id.tcpdiag_sport);
1338         s.rport = ntohs(r->id.tcpdiag_dport);
1339         if (s.local.family == AF_INET) {
1340                 s.local.bytelen = s.remote.bytelen = 4;
1341         } else {
1342                 s.local.bytelen = s.remote.bytelen = 16;
1343         }
1344         memcpy(s.local.data, r->id.tcpdiag_src, s.local.bytelen);
1345         memcpy(s.remote.data, r->id.tcpdiag_dst, s.local.bytelen);
1346
1347         if (f && f->f && run_ssfilter(f->f, &s) == 0)
1348                 return 0;
1349
1350         if (netid_width)
1351                 printf("%-*s ", netid_width, "tcp");
1352         if (state_width)
1353                 printf("%-*s ", state_width, sstate_name[s.state]);
1354
1355         printf("%-6d %-6d ", r->tcpdiag_rqueue, r->tcpdiag_wqueue);
1356
1357         formatted_print(&s.local, s.lport);
1358         formatted_print(&s.remote, s.rport);
1359
1360         if (show_options) {
1361                 if (r->tcpdiag_timer) {
1362                         if (r->tcpdiag_timer > 4)
1363                                 r->tcpdiag_timer = 5;
1364                         printf(" timer:(%s,%s,%d)",
1365                                tmr_name[r->tcpdiag_timer],
1366                                print_ms_timer(r->tcpdiag_expires),
1367                                r->tcpdiag_retrans);
1368                 }
1369         }
1370         if (show_users) {
1371                 char ubuf[4096];
1372                 if (find_users(r->tcpdiag_inode, ubuf, sizeof(ubuf)) > 0)
1373                         printf(" users:(%s)", ubuf);
1374         }
1375         if (show_details) {
1376                 if (r->tcpdiag_uid)
1377                         printf(" uid:%u", (unsigned)r->tcpdiag_uid);
1378                 printf(" ino:%u", (unsigned)r->tcpdiag_inode);
1379                 printf(" sk:%08x", r->id.tcpdiag_cookie[0]);
1380                 if (r->id.tcpdiag_cookie[1] != 0)
1381                         printf("%08x", r->id.tcpdiag_cookie[1]);
1382         }
1383         if (show_mem || show_tcpinfo) {
1384                 printf("\n\t");
1385                 tcp_show_info(nlh, r);
1386         }
1387
1388         printf("\n");
1389
1390         return 0;
1391
1392 }
1393
1394 int tcp_show_netlink(struct filter *f, FILE *dump_fp)
1395 {
1396         int fd;
1397         struct sockaddr_nl nladdr;
1398         struct {
1399                 struct nlmsghdr nlh;
1400                 struct tcpdiagreq r;
1401         } req;
1402         char    *bc = NULL;
1403         int     bclen;
1404         struct msghdr msg;
1405         struct rtattr rta;
1406         char    buf[8192];
1407         struct iovec iov[3];
1408
1409         if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TCPDIAG)) < 0)
1410                 return -1;
1411
1412         memset(&nladdr, 0, sizeof(nladdr));
1413         nladdr.nl_family = AF_NETLINK;
1414
1415         req.nlh.nlmsg_len = sizeof(req);
1416         req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
1417         req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1418         req.nlh.nlmsg_pid = 0;
1419         req.nlh.nlmsg_seq = 123456;
1420         memset(&req.r, 0, sizeof(req.r));
1421         req.r.tcpdiag_family = AF_INET;
1422         req.r.tcpdiag_states = f->states;
1423         if (show_mem)
1424                 req.r.tcpdiag_ext |= (1<<(TCPDIAG_MEMINFO-1)); 
1425         if (show_tcpinfo) {
1426                 req.r.tcpdiag_ext |= (1<<(TCPDIAG_INFO-1));
1427 #ifdef TCPDIAG_VEGASINFO
1428                 req.r.tcpdiag_ext |= (1<<(TCPDIAG_VEGASINFO-1));
1429 #endif
1430         }
1431
1432         iov[0] = (struct iovec){ &req, sizeof(req) };
1433         if (f->f) {
1434                 bclen = ssfilter_bytecompile(f->f, &bc);
1435                 rta.rta_type = TCPDIAG_REQ_BYTECODE;
1436                 rta.rta_len = RTA_LENGTH(bclen);
1437                 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1438                 iov[2] = (struct iovec){ bc, bclen };
1439                 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1440         }
1441
1442         msg = (struct msghdr) {
1443                 (void*)&nladdr, sizeof(nladdr),
1444                 iov,    f->f ? 3 : 1,
1445                 NULL,   0,
1446                 0
1447         };
1448
1449         if (sendmsg(fd, &msg, 0) < 0)
1450                 return -1;
1451
1452
1453         iov[0] = (struct iovec){ buf, sizeof(buf) };
1454
1455         while (1) {
1456                 int status;
1457                 struct nlmsghdr *h;
1458
1459                 msg = (struct msghdr) {
1460                         (void*)&nladdr, sizeof(nladdr),
1461                         iov,    1,
1462                         NULL,   0,
1463                         0
1464                 };
1465
1466                 status = recvmsg(fd, &msg, 0);
1467
1468                 if (status < 0) {
1469                         if (errno == EINTR)
1470                                 continue;
1471                         perror("OVERRUN");
1472                         continue;
1473                 }
1474                 if (status == 0) {
1475                         fprintf(stderr, "EOF on netlink\n");
1476                         return 0;
1477                 }
1478
1479                 if (dump_fp)
1480                         fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
1481
1482                 h = (struct nlmsghdr*)buf;
1483                 while (NLMSG_OK(h, status)) {
1484                         int err;
1485
1486                         if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
1487                             h->nlmsg_seq != 123456)
1488                                 goto skip_it;
1489
1490                         if (h->nlmsg_type == NLMSG_DONE)
1491                                 return 0;
1492                         if (h->nlmsg_type == NLMSG_ERROR) {
1493                                 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1494                                 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1495                                         fprintf(stderr, "ERROR truncated\n");
1496                                 } else {
1497                                         errno = -err->error;
1498                                         perror("TCPDIAG answers");
1499                                 }
1500                                 return 0;
1501                         }
1502                         if (!dump_fp) {
1503                                 err = tcp_show_sock(h, NULL);
1504                                 if (err < 0)
1505                                         return err;
1506                         }
1507
1508 skip_it:
1509                         h = NLMSG_NEXT(h, status);
1510                 }
1511                 if (msg.msg_flags & MSG_TRUNC) {
1512                         fprintf(stderr, "Message truncated\n");
1513                         continue;
1514                 }
1515                 if (status) {
1516                         fprintf(stderr, "!!!Remnant of size %d\n", status);
1517                         exit(1);
1518                 }
1519         }
1520         return 0;
1521 }
1522
1523 int tcp_show_netlink_file(struct filter *f)
1524 {
1525         FILE    *fp;
1526         char    buf[8192];
1527
1528         if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
1529                 perror("fopen($TCPDIAG_FILE)");
1530                 return -1;
1531         }
1532
1533         while (1) {
1534                 int status, err;
1535                 struct nlmsghdr *h = (struct nlmsghdr*)buf;
1536
1537                 status = fread(buf, 1, sizeof(*h), fp);
1538                 if (status < 0) {
1539                         perror("Reading header from $TCPDIAG_FILE");
1540                         return -1;
1541                 }
1542                 if (status != sizeof(*h)) {
1543                         perror("Unexpected EOF reading $TCPDIAG_FILE");
1544                         return -1;
1545                 }
1546
1547                 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
1548
1549                 if (status < 0) {
1550                         perror("Reading $TCPDIAG_FILE");
1551                         return -1;
1552                 }
1553                 if (status + sizeof(*h) < h->nlmsg_len) {
1554                         perror("Unexpected EOF reading $TCPDIAG_FILE");
1555                         return -1;
1556                 }
1557
1558                 /* The only legal exit point */
1559                 if (h->nlmsg_type == NLMSG_DONE)
1560                         return 0;
1561
1562                 if (h->nlmsg_type == NLMSG_ERROR) {
1563                         struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1564                         if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1565                                 fprintf(stderr, "ERROR truncated\n");
1566                         } else {
1567                                 errno = -err->error;
1568                                 perror("TCPDIAG answered");
1569                         }
1570                         return -1;
1571                 }
1572
1573                 err = tcp_show_sock(h, f);
1574                 if (err < 0)
1575                         return err;
1576         }
1577 }
1578
1579 int tcp_show(struct filter *f)
1580 {
1581         int fd = -1;
1582         char *buf = NULL;
1583         int bufsize = 64*1024;
1584
1585         dg_proto = TCP_PROTO;
1586
1587         if (getenv("TCPDIAG_FILE"))
1588                 return tcp_show_netlink_file(f);
1589
1590         if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
1591             && tcp_show_netlink(f, NULL) == 0)
1592                 return 0;
1593
1594         /* Sigh... We have to parse /proc/net/tcp... */
1595
1596         /* Estimate amount of sockets and try to allocate
1597          * huge buffer to read all the table at one read.
1598          * Limit it by 16MB though. The assumption is: as soon as
1599          * kernel was able to hold information about N connections,
1600          * it is able to give us some memory for snapshot.
1601          */
1602         if (1) {
1603                 int guess = slabstat.socks+slabstat.tcp_syns;
1604                 if (f->states&(1<<SS_TIME_WAIT))
1605                         guess += slabstat.tcp_tws;
1606                 if (guess > (16*1024*1024)/128)
1607                         guess = (16*1024*1024)/128;
1608                 guess *= 128;
1609                 if (guess > bufsize)
1610                         bufsize = guess;
1611         }
1612         while (bufsize >= 64*1024) {
1613                 if ((buf = malloc(bufsize)) != NULL)
1614                         break;
1615                 bufsize /= 2;
1616         }
1617         if (buf == NULL) {
1618                 errno = ENOMEM;
1619                 return -1;
1620         }
1621
1622         if (f->families & (1<<AF_INET)) {
1623                 if ((fd = net_tcp_open()) < 0)
1624                         goto outerr;
1625                 if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET))
1626                         goto outerr;
1627                 close(fd);
1628         }
1629
1630         if ((f->families & (1<<AF_INET6)) &&
1631             (fd = net_tcp6_open()) >= 0) {
1632                 if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET6))
1633                         goto outerr;
1634                 close(fd);
1635         }
1636
1637         free(buf);
1638         return 0;
1639
1640 outerr:
1641         do {
1642                 int saved_errno = errno;
1643                 if (buf)
1644                         free(buf);
1645                 if (fd >= 0)
1646                         close(fd);
1647                 errno = saved_errno;
1648                 return -1;
1649         } while (0);
1650 }
1651
1652
1653 int dgram_show_line(char *line, struct filter *f, int family)
1654 {
1655         struct tcpstat s;
1656         char *loc, *rem, *data;
1657         char opt[256];
1658         int n;
1659         char *p;
1660
1661         if ((p = strchr(line, ':')) == NULL)
1662                 return -1;
1663         loc = p+2;
1664
1665         if ((p = strchr(loc, ':')) == NULL)
1666                 return -1;
1667         p[5] = 0;
1668         rem = p+6;
1669
1670         if ((p = strchr(rem, ':')) == NULL)
1671                 return -1;
1672         p[5] = 0;
1673         data = p+6;
1674
1675         do {
1676                 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1677
1678                 if (!(f->states & (1<<state)))
1679                         return 0;
1680         } while (0);
1681
1682         s.local.family = s.remote.family = family;
1683         if (family == AF_INET) {
1684                 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1685                 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1686                 s.local.bytelen = s.remote.bytelen = 4;
1687         } else {
1688                 sscanf(loc, "%08x%08x%08x%08x:%x",
1689                        s.local.data,
1690                        s.local.data+1,
1691                        s.local.data+2,
1692                        s.local.data+3,
1693                        &s.lport);
1694                 sscanf(rem, "%08x%08x%08x%08x:%x",
1695                        s.remote.data,
1696                        s.remote.data+1,
1697                        s.remote.data+2,
1698                        s.remote.data+3,
1699                        &s.rport);
1700                 s.local.bytelen = s.remote.bytelen = 16;
1701         }
1702
1703         if (f->f && run_ssfilter(f->f, &s) == 0)
1704                 return 0;
1705
1706         opt[0] = 0;
1707         n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %d %d %llx %[^\n]\n",
1708                &s.state, &s.wq, &s.rq,
1709                &s.uid, &s.ino,
1710                &s.refcnt, &s.sk, opt);
1711
1712         if (n < 9)
1713                 opt[0] = 0;
1714
1715         if (netid_width)
1716                 printf("%-*s ", netid_width, dg_proto);
1717         if (state_width)
1718                 printf("%-*s ", state_width, sstate_name[s.state]);
1719
1720         printf("%-6d %-6d ", s.rq, s.wq);
1721
1722         formatted_print(&s.local, s.lport);
1723         formatted_print(&s.remote, s.rport);
1724
1725         if (show_users) {
1726                 char ubuf[4096];
1727                 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1728                         printf(" users:(%s)", ubuf);
1729         }
1730
1731         if (show_details) {
1732                 if (s.uid)
1733                         printf(" uid=%u", (unsigned)s.uid);
1734                 printf(" ino=%u", (unsigned)s.ino);
1735                 printf(" sk=%llx", s.sk);
1736                 if (opt[0])
1737                         printf(" opt:\"%s\"", opt);
1738         }
1739         printf("\n");
1740
1741         return 0;
1742 }
1743
1744
1745 int udp_show(struct filter *f)
1746 {
1747         int fd = -1;
1748         char buf[8192];
1749         int  bufsize = sizeof(buf);
1750
1751         dg_proto = UDP_PROTO;
1752
1753         if (f->families&(1<<AF_INET)) {
1754                 if ((fd = net_udp_open()) < 0)
1755                         goto outerr;
1756                 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1757                         goto outerr;
1758                 close(fd);
1759         }
1760
1761         if ((f->families&(1<<AF_INET6)) &&
1762             (fd = net_udp6_open()) >= 0) {
1763                 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1764                         goto outerr;
1765                 close(fd);
1766         }
1767         return 0;
1768
1769 outerr:
1770         do {
1771                 int saved_errno = errno;
1772                 if (fd >= 0)
1773                         close(fd);
1774                 errno = saved_errno;
1775                 return -1;
1776         } while (0);
1777 }
1778
1779 int raw_show(struct filter *f)
1780 {
1781         int fd = -1;
1782         char buf[8192];
1783         int  bufsize = sizeof(buf);
1784
1785         dg_proto = RAW_PROTO;
1786
1787         if (f->families&(1<<AF_INET)) {
1788                 if ((fd = net_raw_open()) < 0)
1789                         goto outerr;
1790                 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1791                         goto outerr;
1792                 close(fd);
1793         }
1794
1795         if ((f->families&(1<<AF_INET6)) &&
1796             (fd = net_raw6_open()) >= 0) {
1797                 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1798                         goto outerr;
1799                 close(fd);
1800         }
1801         return 0;
1802
1803 outerr:
1804         do {
1805                 int saved_errno = errno;
1806                 if (fd >= 0)
1807                         close(fd);
1808                 errno = saved_errno;
1809                 return -1;
1810         } while (0);
1811 }
1812
1813
1814 struct unixstat
1815 {
1816         struct unixstat *next;
1817         int ino;
1818         int peer;
1819         int rq;
1820         int wq;
1821         int state;
1822         int type;
1823         char *name;
1824 };
1825
1826
1827
1828 int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
1829                          SS_ESTABLISHED, SS_CLOSING };
1830
1831
1832 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat))
1833
1834 void unix_list_free(struct unixstat *list)
1835 {
1836         while (list) {
1837                 struct unixstat *s = list;
1838                 list = list->next;
1839                 if (s->name)
1840                         free(s->name);
1841                 free(s);
1842         }
1843 }
1844
1845 void unix_list_print(struct unixstat *list, struct filter *f)
1846 {
1847         struct unixstat *s;
1848         char *peer;
1849
1850         for (s = list; s; s = s->next) {
1851                 if (!(f->states & (1<<s->state)))
1852                         continue;
1853                 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
1854                         continue;
1855                 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
1856                         continue;
1857
1858                 peer = "*";
1859                 if (s->peer) {
1860                         struct unixstat *p;
1861                         for (p = list; p; p = p->next) {
1862                                 if (s->peer == p->ino)
1863                                         break;
1864                         }
1865                         if (!p) {
1866                                 peer = "?";
1867                         } else {
1868                                 peer = p->name ? : "*";
1869                         }
1870                 }
1871
1872                 if (f->f) {
1873                         struct tcpstat tst;
1874                         tst.local.family = AF_UNIX;
1875                         tst.remote.family = AF_UNIX;
1876                         memcpy(tst.local.data, &s->name, sizeof(s->name));
1877                         if (strcmp(peer, "*") == 0)
1878                                 memset(tst.remote.data, 0, sizeof(peer));
1879                         else
1880                                 memcpy(tst.remote.data, &peer, sizeof(peer));  
1881                         if (run_ssfilter(f->f, &tst) == 0)
1882                                 continue;
1883                 }
1884
1885                 if (netid_width)
1886                         printf("%-*s ", netid_width, 
1887                                s->type == SOCK_STREAM ? "u_str" : "u_dgr");
1888                 if (state_width)
1889                         printf("%-*s ", state_width, sstate_name[s->state]);
1890                 printf("%-6d %-6d ", s->rq, s->wq);
1891                 printf("%*s %-*d %*s %-*d",
1892                        addr_width, s->name ? : "*", serv_width, s->ino,
1893                        addr_width, peer, serv_width, s->peer);
1894                 if (show_users) {
1895                         char ubuf[4096];
1896                         if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0)
1897                                 printf(" users:(%s)", ubuf);
1898                 }
1899                 printf("\n");
1900         }
1901 }
1902
1903 int unix_show(struct filter *f)
1904 {
1905         FILE *fp;
1906         char buf[256];
1907         char name[128];
1908         int  newformat = 0;
1909         int  cnt;
1910         struct unixstat *list = NULL;
1911
1912         if ((fp = fdopen(net_unix_open(), "r")) == NULL)
1913                 return -1;
1914         fgets(buf, sizeof(buf)-1, fp);
1915
1916         if (memcmp(buf, "Peer", 4) == 0) 
1917                 newformat = 1;
1918         cnt = 0;
1919
1920         while (fgets(buf, sizeof(buf)-1, fp)) {
1921                 struct unixstat *u, **insp;
1922                 int flags;
1923
1924                 if (!(u = malloc(sizeof(*u))))
1925                         break;
1926                 u->name = NULL;
1927
1928                 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
1929                            &u->peer, &u->rq, &u->wq, &flags, &u->type,
1930                            &u->state, &u->ino, name) < 8)
1931                         name[0] = 0;
1932
1933                 if (flags&(1<<16)) {
1934                         u->state = SS_LISTEN;
1935                 } else {
1936                         u->state = unix_state_map[u->state-1];
1937                         if (u->type == SOCK_DGRAM &&
1938                             u->state == SS_CLOSE &&
1939                             u->peer)
1940                                 u->state = SS_ESTABLISHED;
1941                 }
1942
1943                 if (!newformat) {
1944                         u->peer = 0;
1945                         u->rq = 0;
1946                         u->wq = 0;
1947                 }
1948
1949                 insp = &list;
1950                 while (*insp) {
1951                         if (u->type < (*insp)->type ||
1952                             (u->type == (*insp)->type &&
1953                              u->ino < (*insp)->ino))
1954                                 break;
1955                         insp = &(*insp)->next;
1956                 }
1957                 u->next = *insp;
1958                 *insp = u;
1959
1960                 if (name[0]) {
1961                         if ((u->name = malloc(strlen(name)+1)) == NULL)
1962                                 break;
1963                         strcpy(u->name, name);
1964                 }
1965                 if (++cnt > MAX_UNIX_REMEMBER) {
1966                         unix_list_print(list, f);
1967                         unix_list_free(list);
1968                         list = NULL;
1969                         cnt = 0;
1970                 }
1971         }
1972
1973         if (list) {
1974                 unix_list_print(list, f);
1975                 unix_list_free(list);
1976                 list = NULL;
1977                 cnt = 0;
1978         }
1979
1980         return 0;
1981 }
1982
1983
1984 int packet_show(struct filter *f)
1985 {
1986         FILE *fp;
1987         char buf[256];
1988         int type;
1989         int prot;
1990         int iface;
1991         int state;
1992         int rq;
1993         int uid;
1994         int ino;
1995         unsigned long long sk;
1996
1997         if (!(f->states & (1<<SS_CLOSE)))
1998                 return 0;
1999
2000         if ((fp = fdopen(net_packet_open(), "r")) == NULL)
2001                 return -1;
2002         fgets(buf, sizeof(buf)-1, fp);
2003
2004         while (fgets(buf, sizeof(buf)-1, fp)) {
2005                 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
2006                        &sk,
2007                        &type, &prot, &iface, &state,
2008                        &rq, &uid, &ino);
2009
2010                 if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
2011                         continue;
2012                 if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
2013                         continue;
2014                 if (f->f) {
2015                         struct tcpstat tst;
2016                         tst.local.family = AF_PACKET;
2017                         tst.remote.family = AF_PACKET;
2018                         tst.rport = 0;
2019                         tst.lport = iface;
2020                         tst.local.data[0] = prot;
2021                         tst.remote.data[0] = 0;
2022                         if (run_ssfilter(f->f, &tst) == 0)
2023                                 continue;
2024                 }
2025
2026                 if (netid_width)
2027                         printf("%-*s ", netid_width, 
2028                                type == SOCK_RAW ? "p_raw" : "p_dgr");
2029                 if (state_width)
2030                         printf("%-*s ", state_width, "UNCONN");
2031                 printf("%-6d %-6d ", rq, 0);
2032                 if (prot == 3) {
2033                         printf("%*s:", addr_width, "*");
2034                 } else {
2035                         char tb[16];
2036                         printf("%*s:", addr_width, 
2037                                ll_proto_n2a(htons(prot), tb, sizeof(tb)));
2038                 }
2039                 if (iface == 0) {
2040                         printf("%-*s ", serv_width, "*");
2041                 } else {
2042                         printf("%-*s ", serv_width, xll_index_to_name(iface));
2043                 }
2044                 printf("%*s*%-*s",
2045                        addr_width, "", serv_width, "");
2046
2047                 if (show_users) {
2048                         char ubuf[4096];
2049                         if (find_users(ino, ubuf, sizeof(ubuf)) > 0)
2050                                 printf(" users:(%s)", ubuf);
2051                 }
2052                 if (show_details) {
2053                         printf(" ino=%u uid=%u sk=%llx", ino, uid, sk);
2054                 }
2055                 printf("\n");
2056         }
2057
2058         return 0;
2059 }
2060
2061 int netlink_show(struct filter *f)
2062 {
2063         FILE *fp;
2064         char buf[256];
2065         int prot, pid;
2066         unsigned groups;
2067         int rq, wq, rc;
2068         unsigned long long sk, cb;
2069
2070         if (!(f->states & (1<<SS_CLOSE)))
2071                 return 0;
2072
2073         if ((fp = fdopen(net_netlink_open(), "r")) == NULL)
2074                 return -1;
2075         fgets(buf, sizeof(buf)-1, fp);
2076
2077         while (fgets(buf, sizeof(buf)-1, fp)) {
2078                 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
2079                        &sk,
2080                        &prot, &pid, &groups, &rq, &wq, &cb, &rc);
2081
2082                 if (f->f) {
2083                         struct tcpstat tst;
2084                         tst.local.family = AF_NETLINK;
2085                         tst.remote.family = AF_NETLINK;
2086                         tst.rport = -1;
2087                         tst.lport = pid;
2088                         tst.local.data[0] = prot;
2089                         tst.remote.data[0] = 0;
2090                         if (run_ssfilter(f->f, &tst) == 0)
2091                                 continue;
2092                 }
2093
2094                 if (netid_width)
2095                         printf("%-*s ", netid_width, "nl"); 
2096                 if (state_width)
2097                         printf("%-*s ", state_width, "UNCONN");
2098                 printf("%-6d %-6d ", rq, wq);
2099                 if (resolve_services && prot == 0)
2100                         printf("%*s:", addr_width, "rtnl");
2101                 else if (resolve_services && prot == 3)
2102                         printf("%*s:", addr_width, "fw");
2103                 else if (resolve_services && prot == 4)
2104                         printf("%*s:", addr_width, "tcpdiag");
2105                 else
2106                         printf("%*d:", addr_width, prot);
2107                 if (pid == -1) {
2108                         printf("%-*s ", serv_width, "*");
2109                 } else if (resolve_services) {
2110                         int done = 0;
2111                         if (!pid) {
2112                                 done = 1;
2113                                 printf("%-*s ", serv_width, "kernel");
2114                         } else if (pid > 0) {
2115                                 char procname[64];
2116                                 FILE *fp;
2117                                 sprintf(procname, "%s/%d/stat",
2118                                         getenv("PROC_ROOT") ? : "/proc", pid);
2119                                 if ((fp = fopen(procname, "r")) != NULL) {
2120                                         if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
2121                                                 sprintf(procname+strlen(procname), "/%d", pid);  
2122                                                 printf("%-*s ", serv_width, procname);
2123                                                 done = 1;
2124                                         }
2125                                         fclose(fp);
2126                                 }
2127                         }
2128                         if (!done)
2129                                 printf("%-*d ", serv_width, pid);
2130                 } else {
2131                         printf("%-*d ", serv_width, pid);
2132                 }
2133                 printf("%*s*%-*s",
2134                        addr_width, "", serv_width, "");
2135
2136                 if (show_details) {
2137                         printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
2138                 }
2139                 printf("\n");
2140         }
2141
2142         return 0;
2143 }
2144
2145 struct snmpstat
2146 {
2147         int tcp_estab;
2148 };
2149
2150 int get_snmp_int(char *proto, char *key, int *result)
2151 {
2152         char buf[1024];
2153         FILE *fp;
2154         int protolen = strlen(proto);
2155         int keylen = strlen(key);
2156
2157         *result = 0;
2158
2159         if ((fp = fdopen(net_snmp_open(), "r")) == NULL)
2160                 return -1;
2161
2162         while (fgets(buf, sizeof(buf), fp) != NULL) {
2163                 char *p = buf;
2164                 int  pos = 0;
2165                 if (memcmp(buf, proto, protolen))
2166                         continue;
2167                 while ((p = strchr(p, ' ')) != NULL) {
2168                         pos++;
2169                         p++;
2170                         if (memcmp(p, key, keylen) == 0 &&
2171                             (p[keylen] == ' ' || p[keylen] == '\n'))
2172                                 break;
2173                 }
2174                 if (fgets(buf, sizeof(buf), fp) == NULL)
2175                         break;
2176                 if (memcmp(buf, proto, protolen))
2177                         break;
2178                 p = buf;
2179                 while ((p = strchr(p, ' ')) != NULL) {
2180                         p++;
2181                         if (--pos == 0) {
2182                                 sscanf(p, "%d", result);
2183                                 fclose(fp);
2184                                 return 0;
2185                         }
2186                 }
2187         }
2188
2189         fclose(fp);
2190         errno = ESRCH;
2191         return -1;
2192 }
2193
2194
2195 /* Get stats from sockstat */
2196
2197 struct sockstat
2198 {
2199         int socks;
2200         int tcp_mem;
2201         int tcp_total;
2202         int tcp_orphans;
2203         int tcp_tws;
2204         int tcp4_hashed;
2205         int udp4;
2206         int raw4;
2207         int frag4;
2208         int frag4_mem;
2209         int tcp6_hashed;
2210         int udp6;
2211         int raw6;
2212         int frag6;
2213         int frag6_mem;
2214 };
2215
2216 static void get_sockstat_line(char *line, struct sockstat *s)
2217 {
2218         char id[256], rem[256];
2219
2220         if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
2221                 return;
2222
2223         if (strcmp(id, "sockets:") == 0)
2224                 sscanf(rem, "%*s%d", &s->socks);
2225         else if (strcmp(id, "UDP:") == 0)
2226                 sscanf(rem, "%*s%d", &s->udp4);
2227         else if (strcmp(id, "UDP6:") == 0)
2228                 sscanf(rem, "%*s%d", &s->udp6);
2229         else if (strcmp(id, "RAW:") == 0)
2230                 sscanf(rem, "%*s%d", &s->raw4);
2231         else if (strcmp(id, "RAW6:") == 0)
2232                 sscanf(rem, "%*s%d", &s->raw6);
2233         else if (strcmp(id, "TCP6:") == 0)
2234                 sscanf(rem, "%*s%d", &s->tcp6_hashed);
2235         else if (strcmp(id, "FRAG:") == 0)
2236                 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
2237         else if (strcmp(id, "FRAG6:") == 0)
2238                 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
2239         else if (strcmp(id, "TCP:") == 0)
2240                 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
2241                        &s->tcp4_hashed,
2242                        &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
2243 }
2244
2245 int get_sockstat(struct sockstat *s)
2246 {
2247         char buf[256];
2248         FILE *fp;
2249
2250         memset(s, 0, sizeof(*s));
2251
2252         if ((fp = fdopen(net_sockstat_open(), "r")) == NULL)
2253                 return -1;
2254         while(fgets(buf, sizeof(buf), fp) != NULL)
2255                 get_sockstat_line(buf, s);
2256         fclose(fp);
2257
2258         if ((fp = fdopen(net_sockstat6_open(), "r")) == NULL)
2259                 return 0;
2260         while(fgets(buf, sizeof(buf), fp) != NULL)
2261                 get_sockstat_line(buf, s);
2262         fclose(fp);
2263
2264         return 0;
2265 }
2266
2267 int print_summary(void)
2268 {
2269         struct sockstat s;
2270         struct snmpstat sn;
2271
2272         if (get_sockstat(&s) < 0)
2273                 perror("ss: get_sockstat");
2274         if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
2275                 perror("ss: get_snmpstat");
2276
2277         printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
2278
2279         printf("TCP:   %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
2280                s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
2281                sn.tcp_estab,
2282                s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
2283                s.tcp_orphans,
2284                slabstat.tcp_syns,
2285                s.tcp_tws, slabstat.tcp_tws,
2286                slabstat.tcp_ports
2287                );
2288
2289         printf("\n");
2290         printf("Transport Total     IP        IPv6\n");
2291         printf("*         %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
2292         printf("RAW       %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
2293         printf("UDP       %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
2294         printf("TCP       %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
2295         printf("INET      %-9d %-9d %-9d\n", 
2296                s.raw4+s.udp4+s.tcp4_hashed+
2297                s.raw6+s.udp6+s.tcp6_hashed,
2298                s.raw4+s.udp4+s.tcp4_hashed,
2299                s.raw6+s.udp6+s.tcp6_hashed);
2300         printf("FRAG      %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
2301
2302         printf("\n");
2303
2304         return 0;
2305 }
2306
2307
2308 static void usage(void) __attribute__((noreturn));
2309
2310 static void usage(void)
2311 {
2312         fprintf(stderr,
2313 "Usage: ss [ OPTIONS ]\n"
2314 "       ss [ OPTIONS ] [ FILTER ]\n"
2315 "where  OPTIONS := { -h[elp] | -V[ersion] | -n[umeric] | -r[esolve] |\n"
2316 "                    -a[ll] -l[istening] -o[ptions] -e[xtended] -p[rocesses]\n"
2317 "                    -A QUERY -i[nfo] } -s[ummary]\n"
2318 "                    -f[amily] { inet | inet6 | link | unix } }\n"
2319 "       QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
2320 "       FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
2321 );
2322         exit(-1);
2323 }
2324
2325
2326 int scan_state(const char *state)
2327 {
2328         int i;
2329         if (strcasecmp(state, "close") == 0 ||
2330             strcasecmp(state, "closed") == 0)
2331                 return (1<<SS_CLOSE);
2332         if (strcasecmp(state, "syn-rcv") == 0)
2333                 return (1<<SS_SYN_RECV);
2334         if (matches(state, "established") == 0)
2335                 return (1<<SS_ESTABLISHED);
2336         if (strcasecmp(state, "all") == 0)
2337                 return SS_ALL;
2338         if (strcasecmp(state, "connected") == 0)
2339                 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
2340         if (matches(state, "synchronized") == 0)
2341                 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
2342         if (strcasecmp(state, "bucket") == 0)
2343                 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
2344         if (strcasecmp(state, "big") == 0)
2345                 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
2346         for (i=0; i<SS_MAX; i++) {
2347                 if (matches(state, sstate_namel[i]) == 0)
2348                         return (1<<i);
2349         }
2350         return 0;
2351 }
2352
2353 int main(int argc, char *argv[])
2354 {
2355         int do_default = 1;
2356         int saw_states = 0;
2357         int saw_query = 0;
2358         int do_summary = 0;
2359         const char *dump_tcpdiag = NULL;
2360         FILE *filter_fp = NULL;
2361         int ch;
2362
2363         memset(&current_filter, 0, sizeof(current_filter));
2364
2365         current_filter.states = default_filter.states;
2366
2367         while ((ch = getopt(argc, argv, "h?aletuwxnro460spf:miA:D:F:vV")) != EOF) {
2368                 switch(ch) {
2369                 case 'n':
2370                         resolve_services = 0;
2371                         break;
2372                 case 'r':
2373                         resolve_hosts = 1;
2374                         break;
2375                 case 'o':
2376                         show_options = 1;
2377                         break;
2378                 case 'e':
2379                         show_options = 1;
2380                         show_details++;
2381                         break;
2382                 case 'm':
2383                         show_mem = 1;
2384                         break;
2385                 case 'i':
2386                         show_tcpinfo = 1;
2387                         break;
2388                 case 'p':
2389                         show_users++;
2390                         break;
2391                 case 't':
2392                         current_filter.dbs |= (1<<TCP_DB);
2393                         do_default = 0;
2394                         break;
2395                 case 'u':
2396                         current_filter.dbs |= (1<<UDP_DB);
2397                         do_default = 0;
2398                         break;
2399                 case 'w':
2400                         current_filter.dbs |= (1<<RAW_DB);
2401                         do_default = 0;
2402                         break;
2403                 case 'x':
2404                         current_filter.dbs |= UNIX_DBM;
2405                         do_default = 0;
2406                         break;
2407                 case 'a':
2408                         current_filter.states = SS_ALL;
2409                         break;
2410                 case 'l':
2411                         current_filter.states = (1<<SS_LISTEN);
2412                         break;
2413                 case '4':
2414                         preferred_family = AF_INET;
2415                         break;
2416                 case '6':
2417                         preferred_family = AF_INET6;
2418                         break;
2419                 case '0':
2420                         preferred_family = AF_PACKET;
2421                         break;
2422                 case 'f':
2423                         if (strcmp(optarg, "inet") == 0)
2424                                 preferred_family = AF_INET;
2425                         else if (strcmp(optarg, "inet6") == 0)
2426                                 preferred_family = AF_INET6;
2427                         else if (strcmp(optarg, "link") == 0)
2428                                 preferred_family = AF_PACKET;
2429                         else if (strcmp(optarg, "unix") == 0)
2430                                 preferred_family = AF_UNIX;
2431                         else if (strcmp(optarg, "netlink") == 0)
2432                                 preferred_family = AF_NETLINK;
2433                         else if (strcmp(optarg, "help") == 0)
2434                                 usage();
2435                         else {
2436                                 fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
2437                                 usage();
2438                         }
2439                         break;
2440                 case 'A':
2441                 {
2442                         char *p, *p1;
2443                         if (!saw_query) {
2444                                 current_filter.dbs = 0;
2445                                 saw_query = 1;
2446                                 do_default = 0;
2447                         }
2448                         p = p1 = optarg;
2449                         do {
2450                                 if ((p1 = strchr(p, ',')) != NULL)
2451                                         *p1 = 0; 
2452                                 if (strcmp(p, "all") == 0) {
2453                                         current_filter.dbs = ALL_DB;
2454                                 } else if (strcmp(p, "inet") == 0) {
2455                                         current_filter.dbs |= (1<<TCP_DB)|(1<<UDP_DB)|(1<<RAW_DB);
2456                                 } else if (strcmp(p, "udp") == 0) {
2457                                         current_filter.dbs |= (1<<UDP_DB);
2458                                 } else if (strcmp(p, "tcp") == 0) {
2459                                         current_filter.dbs |= (1<<TCP_DB);
2460                                 } else if (strcmp(p, "raw") == 0) {
2461                                         current_filter.dbs |= (1<<RAW_DB);
2462                                 } else if (strcmp(p, "unix") == 0) {
2463                                         current_filter.dbs |= UNIX_DBM;
2464                                 } else if (matches(p, "unix_stream") == 0 ||
2465                                            strcmp(p, "u_str") == 0) {
2466                                         current_filter.dbs |= (1<<UNIX_ST_DB);
2467                                 } else if (matches(p, "unix_dgram") == 0 ||
2468                                            strcmp(p, "u_dgr") == 0) {
2469                                         current_filter.dbs |= (1<<UNIX_DG_DB);
2470                                 } else if (strcmp(p, "packet") == 0) {
2471                                         current_filter.dbs |= PACKET_DBM;
2472                                 } else if (strcmp(p, "packet_raw") == 0 ||
2473                                            strcmp(p, "p_raw") == 0) {
2474                                         current_filter.dbs |= (1<<PACKET_R_DB);
2475                                 } else if (strcmp(p, "packet_dgram") == 0 ||
2476                                            strcmp(p, "p_dgr") == 0) {
2477                                         current_filter.dbs |= (1<<PACKET_DG_DB);
2478                                 } else if (strcmp(p, "netlink") == 0) {
2479                                         current_filter.dbs |= (1<<NETLINK_DB);
2480                                 } else {
2481                                         fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
2482                                         usage();
2483                                 }
2484                                 p = p1 + 1;
2485                         } while (p1);
2486                         break;
2487                 }
2488                 case 's':
2489                         do_summary = 1;
2490                         break;
2491                 case 'D':
2492                         dump_tcpdiag = optarg;
2493                         break;
2494                 case 'F':
2495                         if (filter_fp) {
2496                                 fprintf(stderr, "More than one filter file\n");
2497                                 exit(-1);
2498                         }
2499                         if (optarg[0] == '-')
2500                                 filter_fp = stdin;
2501                         else
2502                                 filter_fp = fopen(optarg, "r");
2503                         if (!filter_fp) {
2504                                 perror("fopen filter file");
2505                                 exit(-1);
2506                         }
2507                         break;
2508                 case 'v':
2509                 case 'V':
2510                         printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
2511                         exit(0);
2512                 case 'h':
2513                 case '?':
2514                 default:
2515                         usage();
2516                 }
2517         }
2518
2519         argc -= optind;
2520         argv += optind;
2521
2522         get_slabstat(&slabstat);
2523
2524         if (do_summary) {
2525                 print_summary();
2526                 if (do_default && argc == 0)
2527                         exit(0);
2528         }
2529
2530         if (do_default)
2531                 current_filter.dbs = default_filter.dbs;
2532
2533         if (preferred_family == AF_UNSPEC) {
2534                 if (!(current_filter.dbs&~UNIX_DBM))
2535                         preferred_family = AF_UNIX;
2536                 else if (!(current_filter.dbs&~PACKET_DBM))
2537                         preferred_family = AF_PACKET;
2538                 else if (!(current_filter.dbs&~(1<<NETLINK_DB)))
2539                         preferred_family = AF_NETLINK;
2540         }
2541
2542         if (preferred_family != AF_UNSPEC) {
2543                 int mask2;
2544                 if (preferred_family == AF_INET ||
2545                     preferred_family == AF_INET6) {
2546                         mask2= (1<<TCP_DB);
2547                         if (!do_default)
2548                                 mask2 = (1<<UDP_DB)|(1<<RAW_DB);
2549                 } else if (preferred_family == AF_PACKET) {
2550                         mask2 = PACKET_DBM;
2551                 } else if (preferred_family == AF_UNIX) {
2552                         mask2 = UNIX_DBM;
2553                 } else if (preferred_family == AF_NETLINK) {
2554                         mask2 = (1<<NETLINK_DB);
2555                 } else {
2556                         mask2 = 0;
2557                 }
2558
2559                 if (do_default)
2560                         current_filter.dbs = mask2;
2561                 else
2562                         current_filter.dbs &= mask2;
2563                 current_filter.families = (1<<preferred_family);
2564         } else {
2565                 if (!do_default)
2566                         current_filter.families = ~0;
2567                 else
2568                         current_filter.families = default_filter.families;
2569         }
2570         if (current_filter.dbs == 0) {
2571                 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
2572                 exit(0);
2573         }
2574         if (current_filter.families == 0) {
2575                 fprintf(stderr, "ss: no families to show with such filter.\n");
2576                 exit(0);
2577         }
2578
2579         if (resolve_services && resolve_hosts &&
2580             (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB))))
2581                 init_service_resolver();
2582
2583         /* Now parse filter... */
2584         if (argc == 0 && filter_fp) {
2585                 if (ssfilter_parse(&current_filter.f, 0, NULL, filter_fp))
2586                         usage();
2587         }
2588
2589         while (argc > 0) {
2590                 if (strcmp(*argv, "state") == 0) {
2591                         NEXT_ARG();
2592                         if (!saw_states)
2593                                 current_filter.states = 0;
2594                         current_filter.states |= scan_state(*argv);
2595                         saw_states = 1;
2596                 } else if (strcmp(*argv, "exclude") == 0 ||
2597                            strcmp(*argv, "excl") == 0) {
2598                         NEXT_ARG();
2599                         if (!saw_states)
2600                                 current_filter.states = SS_ALL;
2601                         current_filter.states &= ~scan_state(*argv);
2602                         saw_states = 1;
2603                 } else {
2604                         if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
2605                                 usage();
2606                         break;
2607                 }
2608                 argc--; argv++;
2609         }
2610
2611         if (current_filter.states == 0) {
2612                 fprintf(stderr, "ss: no socket states to show with such filter.\n");
2613                 exit(0);
2614         }
2615
2616         if (dump_tcpdiag) {
2617                 FILE *dump_fp = stdout;
2618                 if (!(current_filter.dbs & (1<<TCP_DB))) {
2619                         fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
2620                         exit(0);
2621                 }
2622                 if (dump_tcpdiag[0] != '-') {
2623                         dump_fp = fopen(dump_tcpdiag, "w");
2624                         if (!dump_tcpdiag) {
2625                                 perror("fopen dump file");
2626                                 exit(-1);
2627                         }
2628                 }
2629                 tcp_show_netlink(&current_filter, dump_fp);
2630                 fflush(dump_fp);
2631                 exit(0);
2632         }
2633
2634         netid_width = 0;
2635         if (current_filter.dbs&(current_filter.dbs-1))
2636                 netid_width = 5;
2637
2638         state_width = 0;
2639         if (current_filter.states&(current_filter.states-1))
2640                 state_width = 10;
2641
2642         screen_width = 80;
2643         if (isatty(STDOUT_FILENO)) {
2644                 struct winsize w;
2645
2646                 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
2647                         if (w.ws_col > 0)
2648                                 screen_width = w.ws_col;
2649                 }
2650         }
2651
2652         addrp_width = screen_width;
2653         addrp_width -= netid_width+1;
2654         addrp_width -= state_width+1;
2655         addrp_width -= 14;
2656
2657         if (addrp_width&1) {
2658                 if (netid_width)
2659                         netid_width++;
2660                 else if (state_width)
2661                         state_width++;
2662         }
2663
2664         addrp_width /= 2;
2665         addrp_width--;
2666
2667         serv_width = resolve_services ? 7 : 5;
2668
2669         if (addrp_width < 15+serv_width+1)
2670                 addrp_width = 15+serv_width+1;
2671
2672         addr_width = addrp_width - serv_width - 1; 
2673
2674         if (netid_width)
2675                 printf("%-*s ", netid_width, "Netid");
2676         if (state_width)
2677                 printf("%-*s ", state_width, "State");
2678         printf("%-6s %-6s ", "Recv-Q", "Send-Q");
2679
2680         printf("%*s:%-*s %*s:%-*s\n",
2681                addr_width, "Local Address", serv_width, "Port",
2682                addr_width, "Peer Address", serv_width, "Port");
2683
2684 //printf("%08x %08x %08x\n", current_filter.dbs, current_filter.states, current_filter.families);
2685         fflush(stdout);
2686
2687         if (current_filter.dbs & (1<<NETLINK_DB))
2688                 netlink_show(&current_filter);
2689         if (current_filter.dbs & PACKET_DBM)
2690                 packet_show(&current_filter);
2691         if (current_filter.dbs & UNIX_DBM)
2692                 unix_show(&current_filter);
2693         if (current_filter.dbs & (1<<RAW_DB))
2694                 raw_show(&current_filter);
2695         if (current_filter.dbs & (1<<UDP_DB))
2696                 udp_show(&current_filter);
2697         if (current_filter.dbs & (1<<TCP_DB))
2698                 tcp_show(&current_filter);
2699         return 0;
2700 }