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