]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/ss.c
ipnetconf: by default dump all entries
[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>
aba5acdf
SH
25#include <dirent.h>
26#include <fnmatch.h>
ab61159a 27#include <getopt.h>
aba5acdf
SH
28
29#include "utils.h"
30#include "rt_names.h"
31#include "ll_map.h"
32#include "libnetlink.h"
aba5acdf
SH
33#include "SNAPSHOT.h"
34
9cb1eccf 35#include <linux/tcp.h>
f6062360 36#include <linux/sock_diag.h>
351efcde 37#include <linux/inet_diag.h>
dfbaa90d 38#include <linux/unix_diag.h>
aba5acdf
SH
39
40int resolve_hosts = 0;
41int resolve_services = 1;
42int preferred_family = AF_UNSPEC;
43int show_options = 0;
44int show_details = 0;
45int show_users = 0;
46int show_mem = 0;
47int show_tcpinfo = 0;
48
49int netid_width;
50int state_width;
51int addrp_width;
52int addr_width;
53int serv_width;
54int screen_width;
55
56static const char *TCP_PROTO = "tcp";
57static const char *UDP_PROTO = "udp";
58static const char *RAW_PROTO = "raw";
59static const char *dg_proto = NULL;
60
61enum
62{
63 TCP_DB,
351efcde 64 DCCP_DB,
aba5acdf
SH
65 UDP_DB,
66 RAW_DB,
67 UNIX_DG_DB,
68 UNIX_ST_DB,
69 PACKET_DG_DB,
70 PACKET_R_DB,
71 NETLINK_DB,
72 MAX_DB
73};
74
75#define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
76#define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB))
77#define ALL_DB ((1<<MAX_DB)-1)
78
79enum {
7d105b56
SH
80 SS_UNKNOWN,
81 SS_ESTABLISHED,
82 SS_SYN_SENT,
83 SS_SYN_RECV,
84 SS_FIN_WAIT1,
85 SS_FIN_WAIT2,
86 SS_TIME_WAIT,
87 SS_CLOSE,
88 SS_CLOSE_WAIT,
89 SS_LAST_ACK,
90 SS_LISTEN,
91 SS_CLOSING,
92 SS_MAX
aba5acdf
SH
93};
94
95#define SS_ALL ((1<<SS_MAX)-1)
96
97#include "ssfilter.h"
98
99struct filter
100{
101 int dbs;
102 int states;
103 int families;
104 struct ssfilter *f;
105};
106
107struct filter default_filter = {
7de7e591 108 .dbs = ~0,
e7113c61
SH
109 .states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
110 .families= (1<<AF_INET)|(1<<AF_INET6),
aba5acdf
SH
111};
112
113struct filter current_filter;
114
ab01dbbb 115static FILE *generic_proc_open(const char *env, const char *name)
aba5acdf 116{
ab01dbbb 117 const char *p = getenv(env);
aba5acdf 118 char store[128];
ab01dbbb 119
aba5acdf
SH
120 if (!p) {
121 p = getenv("PROC_ROOT") ? : "/proc";
122 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
123 p = store;
124 }
ab01dbbb
SH
125
126 return fopen(p, "r");
aba5acdf
SH
127}
128
ab01dbbb 129static FILE *net_tcp_open(void)
aba5acdf
SH
130{
131 return generic_proc_open("PROC_NET_TCP", "net/tcp");
132}
133
ab01dbbb 134static FILE *net_tcp6_open(void)
aba5acdf
SH
135{
136 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
137}
138
ab01dbbb 139static FILE *net_udp_open(void)
aba5acdf
SH
140{
141 return generic_proc_open("PROC_NET_UDP", "net/udp");
142}
143
ab01dbbb 144static FILE *net_udp6_open(void)
aba5acdf
SH
145{
146 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
147}
148
ab01dbbb 149static FILE *net_raw_open(void)
aba5acdf
SH
150{
151 return generic_proc_open("PROC_NET_RAW", "net/raw");
152}
153
ab01dbbb 154static FILE *net_raw6_open(void)
aba5acdf
SH
155{
156 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
157}
158
ab01dbbb 159static FILE *net_unix_open(void)
aba5acdf
SH
160{
161 return generic_proc_open("PROC_NET_UNIX", "net/unix");
162}
163
ab01dbbb 164static FILE *net_packet_open(void)
aba5acdf
SH
165{
166 return generic_proc_open("PROC_NET_PACKET", "net/packet");
167}
168
ab01dbbb 169static FILE *net_netlink_open(void)
aba5acdf
SH
170{
171 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
172}
173
ab01dbbb 174static FILE *slabinfo_open(void)
aba5acdf
SH
175{
176 return generic_proc_open("PROC_SLABINFO", "slabinfo");
177}
178
ab01dbbb 179static FILE *net_sockstat_open(void)
aba5acdf
SH
180{
181 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
182}
183
ab01dbbb 184static FILE *net_sockstat6_open(void)
aba5acdf
SH
185{
186 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
187}
188
ab01dbbb 189static FILE *net_snmp_open(void)
aba5acdf
SH
190{
191 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
192}
193
ab01dbbb 194static FILE *ephemeral_ports_open(void)
aba5acdf
SH
195{
196 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
197}
198
fbc0f876
SF
199struct user_ent {
200 struct user_ent *next;
201 unsigned int ino;
202 int pid;
203 int fd;
204 char process[0];
205};
206
207#define USER_ENT_HASH_SIZE 256
208struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
209
210static int user_ent_hashfn(unsigned int ino)
aba5acdf 211{
fbc0f876 212 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
aba5acdf 213
fbc0f876
SF
214 return val & (USER_ENT_HASH_SIZE - 1);
215}
216
217static void user_ent_add(unsigned int ino, const char *process, int pid, int fd)
218{
219 struct user_ent *p, **pp;
220 int str_len;
aba5acdf 221
fbc0f876
SF
222 str_len = strlen(process) + 1;
223 p = malloc(sizeof(struct user_ent) + str_len);
224 if (!p)
225 abort();
226 p->next = NULL;
227 p->ino = ino;
228 p->pid = pid;
229 p->fd = fd;
230 strcpy(p->process, process);
231
232 pp = &user_ent_hash[user_ent_hashfn(ino)];
233 p->next = *pp;
234 *pp = p;
235}
aba5acdf 236
fbc0f876
SF
237static void user_ent_hash_build(void)
238{
239 const char *root = getenv("PROC_ROOT") ? : "/proc/";
240 struct dirent *d;
241 char name[1024];
242 int nameoff;
243 DIR *dir;
244
245 strcpy(name, root);
246 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
aba5acdf 247 strcat(name, "/");
fbc0f876 248
aba5acdf 249 nameoff = strlen(name);
fbc0f876
SF
250
251 dir = opendir(name);
252 if (!dir)
253 return;
aba5acdf
SH
254
255 while ((d = readdir(dir)) != NULL) {
aba5acdf 256 struct dirent *d1;
aba5acdf 257 char process[16];
fbc0f876
SF
258 int pid, pos;
259 DIR *dir1;
260 char crap;
aba5acdf
SH
261
262 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
263 continue;
264
fbc0f876 265 sprintf(name + nameoff, "%d/fd/", pid);
aba5acdf
SH
266 pos = strlen(name);
267 if ((dir1 = opendir(name)) == NULL)
268 continue;
269
fbc0f876 270 process[0] = '\0';
aba5acdf
SH
271
272 while ((d1 = readdir(dir1)) != NULL) {
fbc0f876
SF
273 const char *pattern = "socket:[";
274 unsigned int ino;
aba5acdf 275 char lnk[64];
18445b3e 276 int fd;
788731b3 277 ssize_t link_len;
aba5acdf
SH
278
279 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
280 continue;
281
282 sprintf(name+pos, "%d", fd);
788731b3
TJ
283
284 link_len = readlink(name, lnk, sizeof(lnk)-1);
285 if (link_len == -1)
286 continue;
287 lnk[link_len] = '\0';
288
289 if (strncmp(lnk, pattern, strlen(pattern)))
aba5acdf
SH
290 continue;
291
fbc0f876 292 sscanf(lnk, "socket:[%u]", &ino);
aba5acdf 293
fbc0f876 294 if (process[0] == '\0') {
aba5acdf
SH
295 char tmp[1024];
296 FILE *fp;
fbc0f876
SF
297
298 snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
aba5acdf
SH
299 if ((fp = fopen(tmp, "r")) != NULL) {
300 fscanf(fp, "%*d (%[^)])", process);
301 fclose(fp);
302 }
303 }
304
fbc0f876 305 user_ent_add(ino, process, pid, fd);
aba5acdf
SH
306 }
307 closedir(dir1);
308 }
309 closedir(dir);
fbc0f876
SF
310}
311
d1f28cf1 312static int find_users(unsigned ino, char *buf, int buflen)
fbc0f876
SF
313{
314 struct user_ent *p;
315 int cnt = 0;
316 char *ptr;
317
318 if (!ino)
319 return 0;
320
321 p = user_ent_hash[user_ent_hashfn(ino)];
322 ptr = buf;
323 while (p) {
324 if (p->ino != ino)
325 goto next;
326
327 if (ptr - buf >= buflen - 1)
328 break;
329
330 snprintf(ptr, buflen - (ptr - buf),
331 "(\"%s\",%d,%d),",
332 p->process, p->pid, p->fd);
333 ptr += strlen(ptr);
334 cnt++;
335
336 next:
337 p = p->next;
338 }
339
aba5acdf 340 if (ptr != buf)
fbc0f876
SF
341 ptr[-1] = '\0';
342
aba5acdf
SH
343 return cnt;
344}
345
aba5acdf
SH
346/* Get stats from slab */
347
348struct slabstat
349{
350 int socks;
351 int tcp_ports;
352 int tcp_tws;
353 int tcp_syns;
354 int skbs;
355};
356
357struct slabstat slabstat;
358
ae665a52 359static const char *slabstat_ids[] =
aba5acdf
SH
360{
361 "sock",
362 "tcp_bind_bucket",
363 "tcp_tw_bucket",
364 "tcp_open_request",
365 "skbuff_head_cache",
366};
367
d1f28cf1 368static int get_slabstat(struct slabstat *s)
aba5acdf
SH
369{
370 char buf[256];
371 FILE *fp;
372 int cnt;
373
374 memset(s, 0, sizeof(*s));
375
ab01dbbb
SH
376 fp = slabinfo_open();
377 if (!fp)
aba5acdf
SH
378 return -1;
379
380 cnt = sizeof(*s)/sizeof(int);
381
382 fgets(buf, sizeof(buf), fp);
383 while(fgets(buf, sizeof(buf), fp) != NULL) {
384 int i;
385 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
386 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
387 sscanf(buf, "%*s%d", ((int *)s) + i);
388 cnt--;
389 break;
390 }
391 }
392 if (cnt <= 0)
393 break;
394 }
395
396 fclose(fp);
397 return 0;
398}
399
7d105b56
SH
400static const char *sstate_name[] = {
401 "UNKNOWN",
9cb1eccf
ED
402 [SS_ESTABLISHED] = "ESTAB",
403 [SS_SYN_SENT] = "SYN-SENT",
404 [SS_SYN_RECV] = "SYN-RECV",
405 [SS_FIN_WAIT1] = "FIN-WAIT-1",
406 [SS_FIN_WAIT2] = "FIN-WAIT-2",
407 [SS_TIME_WAIT] = "TIME-WAIT",
408 [SS_CLOSE] = "UNCONN",
409 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
410 [SS_LAST_ACK] = "LAST-ACK",
411 [SS_LISTEN] = "LISTEN",
412 [SS_CLOSING] = "CLOSING",
aba5acdf
SH
413};
414
7d105b56
SH
415static const char *sstate_namel[] = {
416 "UNKNOWN",
9cb1eccf
ED
417 [SS_ESTABLISHED] = "established",
418 [SS_SYN_SENT] = "syn-sent",
419 [SS_SYN_RECV] = "syn-recv",
420 [SS_FIN_WAIT1] = "fin-wait-1",
421 [SS_FIN_WAIT2] = "fin-wait-2",
422 [SS_TIME_WAIT] = "time-wait",
423 [SS_CLOSE] = "unconnected",
424 [SS_CLOSE_WAIT] = "close-wait",
425 [SS_LAST_ACK] = "last-ack",
426 [SS_LISTEN] = "listening",
427 [SS_CLOSING] = "closing",
aba5acdf
SH
428};
429
430struct tcpstat
431{
432 inet_prefix local;
433 inet_prefix remote;
434 int lport;
435 int rport;
436 int state;
437 int rq, wq;
438 int timer;
439 int timeout;
440 int retrs;
e7113c61 441 unsigned ino;
aba5acdf 442 int probes;
e7113c61 443 unsigned uid;
aba5acdf
SH
444 int refcnt;
445 unsigned long long sk;
446 int rto, ato, qack, cwnd, ssthresh;
447};
448
7d105b56 449static const char *tmr_name[] = {
aba5acdf
SH
450 "off",
451 "on",
452 "keepalive",
453 "timewait",
454 "persist",
455 "unknown"
456};
457
d1f28cf1 458static const char *print_ms_timer(int timeout)
aba5acdf
SH
459{
460 static char buf[64];
461 int secs, msecs, minutes;
462 if (timeout < 0)
463 timeout = 0;
464 secs = timeout/1000;
465 minutes = secs/60;
466 secs = secs%60;
467 msecs = timeout%1000;
468 buf[0] = 0;
469 if (minutes) {
470 msecs = 0;
471 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
472 if (minutes > 9)
473 secs = 0;
474 }
475 if (secs) {
476 if (secs > 9)
477 msecs = 0;
478 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
479 }
480 if (msecs)
481 sprintf(buf+strlen(buf), "%03dms", msecs);
482 return buf;
e7113c61 483}
aba5acdf 484
d1f28cf1 485static const char *print_hz_timer(int timeout)
aba5acdf 486{
2d44be19 487 int hz = get_user_hz();
aba5acdf 488 return print_ms_timer(((timeout*1000) + hz-1)/hz);
e7113c61 489}
aba5acdf
SH
490
491struct scache
492{
493 struct scache *next;
494 int port;
495 char *name;
496 const char *proto;
497};
498
499struct scache *rlist;
500
d1f28cf1 501static void init_service_resolver(void)
aba5acdf
SH
502{
503 char buf[128];
504 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
505 if (fp) {
506 fgets(buf, sizeof(buf), fp);
507 while (fgets(buf, sizeof(buf), fp) != NULL) {
508 unsigned int progn, port;
509 char proto[128], prog[128];
510 if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
511 &port, prog+4) == 4) {
512 struct scache *c = malloc(sizeof(*c));
513 if (c) {
514 c->port = port;
515 memcpy(prog, "rpc.", 4);
516 c->name = strdup(prog);
517 if (strcmp(proto, TCP_PROTO) == 0)
518 c->proto = TCP_PROTO;
519 else if (strcmp(proto, UDP_PROTO) == 0)
520 c->proto = UDP_PROTO;
521 else
522 c->proto = NULL;
523 c->next = rlist;
524 rlist = c;
525 }
526 }
527 }
2bcc3c16 528 pclose(fp);
aba5acdf
SH
529 }
530}
531
ab61159a
SH
532static int ip_local_port_min, ip_local_port_max;
533
534/* Even do not try default linux ephemeral port ranges:
535 * default /etc/services contains so much of useless crap
536 * wouldbe "allocated" to this area that resolution
537 * is really harmful. I shrug each time when seeing
538 * "socks" or "cfinger" in dumps.
539 */
540static int is_ephemeral(int port)
541{
542 if (!ip_local_port_min) {
ab01dbbb 543 FILE *f = ephemeral_ports_open();
ab61159a 544 if (f) {
ae665a52 545 fscanf(f, "%d %d",
ab61159a
SH
546 &ip_local_port_min, &ip_local_port_max);
547 fclose(f);
548 } else {
549 ip_local_port_min = 1024;
550 ip_local_port_max = 4999;
551 }
552 }
553
554 return (port >= ip_local_port_min && port<= ip_local_port_max);
555}
556
557
d1f28cf1 558static const char *__resolve_service(int port)
aba5acdf
SH
559{
560 struct scache *c;
561
562 for (c = rlist; c; c = c->next) {
563 if (c->port == port && c->proto == dg_proto)
564 return c->name;
565 }
566
ab61159a 567 if (!is_ephemeral(port)) {
aba5acdf
SH
568 static int notfirst;
569 struct servent *se;
570 if (!notfirst) {
571 setservent(1);
572 notfirst = 1;
ae665a52 573 }
aba5acdf
SH
574 se = getservbyport(htons(port), dg_proto);
575 if (se)
576 return se->s_name;
577 }
578
579 return NULL;
580}
581
582
d1f28cf1 583static const char *resolve_service(int port)
aba5acdf
SH
584{
585 static char buf[128];
586 static struct scache cache[256];
587
588 if (port == 0) {
589 buf[0] = '*';
590 buf[1] = 0;
591 return buf;
592 }
593
594 if (resolve_services) {
595 if (dg_proto == RAW_PROTO) {
596 return inet_proto_n2a(port, buf, sizeof(buf));
597 } else {
598 struct scache *c;
599 const char *res;
600 int hash = (port^(((unsigned long)dg_proto)>>2))&255;
601
ae665a52 602 for (c = &cache[hash]; c; c = c->next) {
aba5acdf
SH
603 if (c->port == port &&
604 c->proto == dg_proto) {
605 if (c->name)
606 return c->name;
607 goto do_numeric;
608 }
609 }
610
611 if ((res = __resolve_service(port)) != NULL) {
612 if ((c = malloc(sizeof(*c))) == NULL)
613 goto do_numeric;
614 } else {
615 c = &cache[hash];
616 if (c->name)
617 free(c->name);
618 }
619 c->port = port;
620 c->name = NULL;
621 c->proto = dg_proto;
622 if (res) {
623 c->name = strdup(res);
624 c->next = cache[hash].next;
625 cache[hash].next = c;
626 }
627 if (c->name)
628 return c->name;
629 }
630 }
631
632 do_numeric:
633 sprintf(buf, "%u", port);
634 return buf;
635}
636
d1f28cf1 637static void formatted_print(const inet_prefix *a, int port)
aba5acdf
SH
638{
639 char buf[1024];
640 const char *ap = buf;
641 int est_len;
642
643 est_len = addr_width;
644
645 if (a->family == AF_INET) {
646 if (a->data[0] == 0) {
647 buf[0] = '*';
648 buf[1] = 0;
649 } else {
650 ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
651 }
652 } else {
653 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
654 est_len = strlen(ap);
655 if (est_len <= addr_width)
656 est_len = addr_width;
657 else
658 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
659 }
660 printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
661}
662
663struct aafilter
664{
665 inet_prefix addr;
666 int port;
667 struct aafilter *next;
668};
669
d1f28cf1
SH
670static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
671 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
d1f28cf1 690static int 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
d1f28cf1 702static int run_ssfilter(struct ssfilter *f, struct tcpstat *s)
aba5acdf
SH
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
51ff9f24
HFS
1330static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1331{
1332 const __u32 *skmeminfo;
1333 if (!tb[attrtype])
1334 return;
1335 skmeminfo = RTA_DATA(tb[attrtype]);
1336
1337 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
1338 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
1339 skmeminfo[SK_MEMINFO_RCVBUF],
1340 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
1341 skmeminfo[SK_MEMINFO_SNDBUF],
1342 skmeminfo[SK_MEMINFO_FWD_ALLOC],
1343 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
1344 skmeminfo[SK_MEMINFO_OPTMEM]);
1345
1346 if (RTA_PAYLOAD(tb[attrtype]) >=
1347 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
1348 printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
1349
1350 printf(")");
1351}
1352
351efcde 1353static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r)
7d105b56 1354{
351efcde 1355 struct rtattr * tb[INET_DIAG_MAX+1];
b4b0b7d5
SH
1356 char b1[64];
1357 double rtt = 0;
7d105b56 1358
351efcde 1359 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
7d105b56
SH
1360 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
1361
910b0397 1362 if (tb[INET_DIAG_SKMEMINFO]) {
51ff9f24 1363 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
c6d6c92c 1364 } else if (tb[INET_DIAG_MEMINFO]) {
351efcde
SH
1365 const struct inet_diag_meminfo *minfo
1366 = RTA_DATA(tb[INET_DIAG_MEMINFO]);
7d105b56 1367 printf(" mem:(r%u,w%u,f%u,t%u)",
351efcde
SH
1368 minfo->idiag_rmem,
1369 minfo->idiag_wmem,
1370 minfo->idiag_fmem,
1371 minfo->idiag_tmem);
7d105b56
SH
1372 }
1373
351efcde 1374 if (tb[INET_DIAG_INFO]) {
05e18118 1375 struct tcp_info *info;
351efcde 1376 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
05e18118
SH
1377
1378 /* workaround for older kernels with less fields */
1379 if (len < sizeof(*info)) {
1380 info = alloca(sizeof(*info));
1381 memset(info, 0, sizeof(*info));
351efcde 1382 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
05e18118 1383 } else
351efcde 1384 info = RTA_DATA(tb[INET_DIAG_INFO]);
05e18118 1385
b4b0b7d5
SH
1386 if (show_options) {
1387 if (info->tcpi_options & TCPI_OPT_TIMESTAMPS)
1388 printf(" ts");
1389 if (info->tcpi_options & TCPI_OPT_SACK)
1390 printf(" sack");
1391 if (info->tcpi_options & TCPI_OPT_ECN)
1392 printf(" ecn");
719b958b
ED
1393 if (info->tcpi_options & TCPI_OPT_ECN_SEEN)
1394 printf(" ecnseen");
9cb1eccf
ED
1395 if (info->tcpi_options & TCPI_OPT_SYN_DATA)
1396 printf(" fastopen");
b4b0b7d5 1397 }
52d5ac3f 1398
351efcde 1399 if (tb[INET_DIAG_CONG])
ff24746c 1400 printf(" %s", rta_getattr_str(tb[INET_DIAG_CONG]));
ea8fc104 1401
ae665a52 1402 if (info->tcpi_options & TCPI_OPT_WSCALE)
05e18118
SH
1403 printf(" wscale:%d,%d", info->tcpi_snd_wscale,
1404 info->tcpi_rcv_wscale);
7d105b56
SH
1405 if (info->tcpi_rto && info->tcpi_rto != 3000000)
1406 printf(" rto:%g", (double)info->tcpi_rto/1000);
1407 if (info->tcpi_rtt)
1408 printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000,
1409 (double)info->tcpi_rttvar/1000);
1410 if (info->tcpi_ato)
1411 printf(" ato:%g", (double)info->tcpi_ato/1000);
4d354347
BH
1412 if (info->tcpi_snd_mss)
1413 printf(" mss:%d", info->tcpi_snd_mss);
7d105b56
SH
1414 if (info->tcpi_snd_cwnd != 2)
1415 printf(" cwnd:%d", info->tcpi_snd_cwnd);
1416 if (info->tcpi_snd_ssthresh < 0xFFFF)
1417 printf(" ssthresh:%d", info->tcpi_snd_ssthresh);
52d5ac3f 1418
b4b0b7d5 1419 rtt = (double) info->tcpi_rtt;
351efcde 1420 if (tb[INET_DIAG_VEGASINFO]) {
05e18118 1421 const struct tcpvegas_info *vinfo
351efcde 1422 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
7d105b56 1423
ae665a52 1424 if (vinfo->tcpv_enabled &&
ea8fc104
SH
1425 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
1426 rtt = vinfo->tcpv_rtt;
b4b0b7d5
SH
1427 }
1428
1429 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
1430 printf(" send %sbps",
1431 sprint_bw(b1, (double) info->tcpi_snd_cwnd *
1432 (double) info->tcpi_snd_mss * 8000000.
1433 / rtt));
7d105b56 1434 }
b4b0b7d5
SH
1435
1436 if (info->tcpi_rcv_rtt)
1437 printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000);
1438 if (info->tcpi_rcv_space)
1439 printf(" rcv_space:%d", info->tcpi_rcv_space);
1440
7d105b56 1441 }
7d105b56 1442}
aba5acdf 1443
3fe5b534 1444static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f)
aba5acdf 1445{
351efcde 1446 struct inet_diag_msg *r = NLMSG_DATA(nlh);
aba5acdf
SH
1447 struct tcpstat s;
1448
351efcde
SH
1449 s.state = r->idiag_state;
1450 s.local.family = s.remote.family = r->idiag_family;
1451 s.lport = ntohs(r->id.idiag_sport);
1452 s.rport = ntohs(r->id.idiag_dport);
aba5acdf
SH
1453 if (s.local.family == AF_INET) {
1454 s.local.bytelen = s.remote.bytelen = 4;
1455 } else {
1456 s.local.bytelen = s.remote.bytelen = 16;
1457 }
351efcde
SH
1458 memcpy(s.local.data, r->id.idiag_src, s.local.bytelen);
1459 memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen);
aba5acdf
SH
1460
1461 if (f && f->f && run_ssfilter(f->f, &s) == 0)
1462 return 0;
1463
1464 if (netid_width)
1465 printf("%-*s ", netid_width, "tcp");
1466 if (state_width)
1467 printf("%-*s ", state_width, sstate_name[s.state]);
1468
351efcde 1469 printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue);
aba5acdf
SH
1470
1471 formatted_print(&s.local, s.lport);
1472 formatted_print(&s.remote, s.rport);
1473
1474 if (show_options) {
351efcde
SH
1475 if (r->idiag_timer) {
1476 if (r->idiag_timer > 4)
1477 r->idiag_timer = 5;
aba5acdf 1478 printf(" timer:(%s,%s,%d)",
351efcde
SH
1479 tmr_name[r->idiag_timer],
1480 print_ms_timer(r->idiag_expires),
1481 r->idiag_retrans);
aba5acdf
SH
1482 }
1483 }
1484 if (show_users) {
1485 char ubuf[4096];
351efcde 1486 if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0)
aba5acdf
SH
1487 printf(" users:(%s)", ubuf);
1488 }
1489 if (show_details) {
351efcde
SH
1490 if (r->idiag_uid)
1491 printf(" uid:%u", (unsigned)r->idiag_uid);
e7113c61 1492 printf(" ino:%u", r->idiag_inode);
bbe32053 1493 printf(" sk:");
351efcde
SH
1494 if (r->id.idiag_cookie[1] != 0)
1495 printf("%08x", r->id.idiag_cookie[1]);
bbe32053 1496 printf("%08x", r->id.idiag_cookie[0]);
aba5acdf
SH
1497 }
1498 if (show_mem || show_tcpinfo) {
7d105b56
SH
1499 printf("\n\t");
1500 tcp_show_info(nlh, r);
aba5acdf 1501 }
7d105b56 1502
aba5acdf
SH
1503 printf("\n");
1504
1505 return 0;
aba5acdf
SH
1506}
1507
746a695f 1508static int tcpdiag_send(int fd, int protocol, struct filter *f)
aba5acdf 1509{
aba5acdf
SH
1510 struct sockaddr_nl nladdr;
1511 struct {
1512 struct nlmsghdr nlh;
351efcde 1513 struct inet_diag_req r;
aba5acdf
SH
1514 } req;
1515 char *bc = NULL;
1516 int bclen;
1517 struct msghdr msg;
1518 struct rtattr rta;
aba5acdf
SH
1519 struct iovec iov[3];
1520
346f8ca8
PE
1521 if (protocol == IPPROTO_UDP)
1522 return -1;
1523
aba5acdf
SH
1524 memset(&nladdr, 0, sizeof(nladdr));
1525 nladdr.nl_family = AF_NETLINK;
1526
1527 req.nlh.nlmsg_len = sizeof(req);
3fe5b534
PE
1528 if (protocol == IPPROTO_TCP)
1529 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
1530 else
1531 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
aba5acdf
SH
1532 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1533 req.nlh.nlmsg_pid = 0;
1534 req.nlh.nlmsg_seq = 123456;
1535 memset(&req.r, 0, sizeof(req.r));
351efcde
SH
1536 req.r.idiag_family = AF_INET;
1537 req.r.idiag_states = f->states;
910b0397 1538 if (show_mem) {
ae665a52 1539 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
910b0397
SW
1540 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
1541 }
b4b0b7d5 1542
7d105b56 1543 if (show_tcpinfo) {
351efcde
SH
1544 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
1545 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
1546 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
7d105b56 1547 }
aba5acdf 1548
ae665a52
SH
1549 iov[0] = (struct iovec){
1550 .iov_base = &req,
1551 .iov_len = sizeof(req)
ea8fc104 1552 };
aba5acdf
SH
1553 if (f->f) {
1554 bclen = ssfilter_bytecompile(f->f, &bc);
351efcde 1555 rta.rta_type = INET_DIAG_REQ_BYTECODE;
aba5acdf
SH
1556 rta.rta_len = RTA_LENGTH(bclen);
1557 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1558 iov[2] = (struct iovec){ bc, bclen };
1559 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1560 }
1561
1562 msg = (struct msghdr) {
ae665a52 1563 .msg_name = (void*)&nladdr,
ea8fc104 1564 .msg_namelen = sizeof(nladdr),
ae665a52 1565 .msg_iov = iov,
ea8fc104 1566 .msg_iovlen = f->f ? 3 : 1,
aba5acdf
SH
1567 };
1568
930a75f9
ED
1569 if (sendmsg(fd, &msg, 0) < 0) {
1570 close(fd);
aba5acdf 1571 return -1;
930a75f9 1572 }
aba5acdf 1573
746a695f
PE
1574 return 0;
1575}
1576
886d19d6
PE
1577static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
1578{
1579 struct sockaddr_nl nladdr;
1580 struct {
1581 struct nlmsghdr nlh;
1582 struct inet_diag_req_v2 r;
1583 } req;
1584 char *bc = NULL;
1585 int bclen;
1586 struct msghdr msg;
1587 struct rtattr rta;
1588 struct iovec iov[3];
1589
1590 if (family == PF_UNSPEC)
1591 return tcpdiag_send(fd, protocol, f);
1592
1593 memset(&nladdr, 0, sizeof(nladdr));
1594 nladdr.nl_family = AF_NETLINK;
1595
1596 req.nlh.nlmsg_len = sizeof(req);
1597 req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
1598 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1599 req.nlh.nlmsg_pid = 0;
1600 req.nlh.nlmsg_seq = 123456;
1601 memset(&req.r, 0, sizeof(req.r));
1602 req.r.sdiag_family = family;
1603 req.r.sdiag_protocol = protocol;
1604 req.r.idiag_states = f->states;
1605 if (show_mem) {
1606 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
1607 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
1608 }
1609
1610 if (show_tcpinfo) {
1611 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
1612 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
1613 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
1614 }
1615
1616 iov[0] = (struct iovec){
1617 .iov_base = &req,
1618 .iov_len = sizeof(req)
1619 };
1620 if (f->f) {
1621 bclen = ssfilter_bytecompile(f->f, &bc);
1622 rta.rta_type = INET_DIAG_REQ_BYTECODE;
1623 rta.rta_len = RTA_LENGTH(bclen);
1624 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1625 iov[2] = (struct iovec){ bc, bclen };
1626 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1627 }
1628
1629 msg = (struct msghdr) {
1630 .msg_name = (void*)&nladdr,
1631 .msg_namelen = sizeof(nladdr),
1632 .msg_iov = iov,
1633 .msg_iovlen = f->f ? 3 : 1,
1634 };
1635
1636 if (sendmsg(fd, &msg, 0) < 0) {
1637 close(fd);
1638 return -1;
1639 }
1640
1641 return 0;
1642}
1643
746a695f
PE
1644static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
1645{
886d19d6 1646 int fd, family;
746a695f
PE
1647 struct sockaddr_nl nladdr;
1648 struct msghdr msg;
1649 char buf[8192];
1650 struct iovec iov[3];
1651
1652 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
1653 return -1;
1654
886d19d6
PE
1655 family = PF_INET;
1656again:
1657 if (sockdiag_send(family, fd, protocol, f))
746a695f
PE
1658 return -1;
1659
1660 memset(&nladdr, 0, sizeof(nladdr));
1661 nladdr.nl_family = AF_NETLINK;
1662
ae665a52
SH
1663 iov[0] = (struct iovec){
1664 .iov_base = buf,
1665 .iov_len = sizeof(buf)
ea8fc104 1666 };
aba5acdf
SH
1667
1668 while (1) {
1669 int status;
1670 struct nlmsghdr *h;
1671
1672 msg = (struct msghdr) {
1673 (void*)&nladdr, sizeof(nladdr),
1674 iov, 1,
1675 NULL, 0,
1676 0
1677 };
1678
1679 status = recvmsg(fd, &msg, 0);
1680
1681 if (status < 0) {
1682 if (errno == EINTR)
1683 continue;
1684 perror("OVERRUN");
1685 continue;
1686 }
1687 if (status == 0) {
1688 fprintf(stderr, "EOF on netlink\n");
c51577cd 1689 close(fd);
aba5acdf
SH
1690 return 0;
1691 }
1692
1693 if (dump_fp)
1694 fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
1695
1696 h = (struct nlmsghdr*)buf;
1697 while (NLMSG_OK(h, status)) {
1698 int err;
a37b01c1 1699 struct inet_diag_msg *r = NLMSG_DATA(h);
aba5acdf
SH
1700
1701 if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
1702 h->nlmsg_seq != 123456)
1703 goto skip_it;
1704
886d19d6
PE
1705 if (h->nlmsg_type == NLMSG_DONE)
1706 goto done;
1707
aba5acdf
SH
1708 if (h->nlmsg_type == NLMSG_ERROR) {
1709 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1710 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1711 fprintf(stderr, "ERROR truncated\n");
1712 } else {
886d19d6
PE
1713 if (family != PF_UNSPEC) {
1714 family = PF_UNSPEC;
1715 goto again;
1716 }
1717
aba5acdf 1718 errno = -err->error;
930a75f9
ED
1719 if (errno == EOPNOTSUPP) {
1720 close(fd);
1721 return -1;
1722 }
aba5acdf
SH
1723 perror("TCPDIAG answers");
1724 }
886d19d6
PE
1725
1726 goto done;
aba5acdf
SH
1727 }
1728 if (!dump_fp) {
a37b01c1
LY
1729 if (!(f->families & (1<<r->idiag_family))) {
1730 h = NLMSG_NEXT(h, status);
1731 continue;
1732 }
3fe5b534 1733 err = inet_show_sock(h, NULL);
c51577cd
MT
1734 if (err < 0) {
1735 close(fd);
aba5acdf 1736 return err;
c51577cd 1737 }
aba5acdf
SH
1738 }
1739
1740skip_it:
1741 h = NLMSG_NEXT(h, status);
1742 }
1743 if (msg.msg_flags & MSG_TRUNC) {
1744 fprintf(stderr, "Message truncated\n");
1745 continue;
1746 }
1747 if (status) {
1748 fprintf(stderr, "!!!Remnant of size %d\n", status);
1749 exit(1);
1750 }
1751 }
886d19d6
PE
1752done:
1753 if (family == PF_INET) {
1754 family = PF_INET6;
1755 goto again;
1756 }
1757
c51577cd 1758 close(fd);
aba5acdf
SH
1759 return 0;
1760}
1761
ab01dbbb 1762static int tcp_show_netlink_file(struct filter *f)
aba5acdf
SH
1763{
1764 FILE *fp;
1765 char buf[8192];
1766
1767 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
1768 perror("fopen($TCPDIAG_FILE)");
1769 return -1;
1770 }
1771
1772 while (1) {
1773 int status, err;
1774 struct nlmsghdr *h = (struct nlmsghdr*)buf;
1775
1776 status = fread(buf, 1, sizeof(*h), fp);
1777 if (status < 0) {
1778 perror("Reading header from $TCPDIAG_FILE");
1779 return -1;
1780 }
1781 if (status != sizeof(*h)) {
1782 perror("Unexpected EOF reading $TCPDIAG_FILE");
1783 return -1;
1784 }
1785
1786 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
1787
1788 if (status < 0) {
1789 perror("Reading $TCPDIAG_FILE");
1790 return -1;
1791 }
1792 if (status + sizeof(*h) < h->nlmsg_len) {
1793 perror("Unexpected EOF reading $TCPDIAG_FILE");
1794 return -1;
1795 }
1796
1797 /* The only legal exit point */
1798 if (h->nlmsg_type == NLMSG_DONE)
1799 return 0;
1800
1801 if (h->nlmsg_type == NLMSG_ERROR) {
1802 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1803 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1804 fprintf(stderr, "ERROR truncated\n");
1805 } else {
1806 errno = -err->error;
1807 perror("TCPDIAG answered");
1808 }
1809 return -1;
1810 }
1811
3fe5b534 1812 err = inet_show_sock(h, f);
aba5acdf
SH
1813 if (err < 0)
1814 return err;
1815 }
1816}
1817
ab01dbbb 1818static int tcp_show(struct filter *f, int socktype)
aba5acdf 1819{
ab01dbbb 1820 FILE *fp = NULL;
aba5acdf
SH
1821 char *buf = NULL;
1822 int bufsize = 64*1024;
1823
1824 dg_proto = TCP_PROTO;
1825
1826 if (getenv("TCPDIAG_FILE"))
1827 return tcp_show_netlink_file(f);
1828
1829 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
3fe5b534 1830 && inet_show_netlink(f, NULL, socktype) == 0)
aba5acdf
SH
1831 return 0;
1832
1833 /* Sigh... We have to parse /proc/net/tcp... */
1834
ab01dbbb 1835
aba5acdf
SH
1836 /* Estimate amount of sockets and try to allocate
1837 * huge buffer to read all the table at one read.
1838 * Limit it by 16MB though. The assumption is: as soon as
1839 * kernel was able to hold information about N connections,
1840 * it is able to give us some memory for snapshot.
1841 */
1842 if (1) {
1843 int guess = slabstat.socks+slabstat.tcp_syns;
1844 if (f->states&(1<<SS_TIME_WAIT))
1845 guess += slabstat.tcp_tws;
1846 if (guess > (16*1024*1024)/128)
1847 guess = (16*1024*1024)/128;
1848 guess *= 128;
1849 if (guess > bufsize)
1850 bufsize = guess;
1851 }
1852 while (bufsize >= 64*1024) {
1853 if ((buf = malloc(bufsize)) != NULL)
1854 break;
1855 bufsize /= 2;
1856 }
1857 if (buf == NULL) {
1858 errno = ENOMEM;
1859 return -1;
1860 }
1861
1862 if (f->families & (1<<AF_INET)) {
69cae645 1863 if ((fp = net_tcp_open()) == NULL)
aba5acdf 1864 goto outerr;
ab01dbbb
SH
1865
1866 setbuffer(fp, buf, bufsize);
1867 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
aba5acdf 1868 goto outerr;
ab01dbbb 1869 fclose(fp);
aba5acdf
SH
1870 }
1871
1872 if ((f->families & (1<<AF_INET6)) &&
69cae645 1873 (fp = net_tcp6_open()) != NULL) {
ab01dbbb
SH
1874 setbuffer(fp, buf, bufsize);
1875 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
aba5acdf 1876 goto outerr;
ab01dbbb 1877 fclose(fp);
aba5acdf
SH
1878 }
1879
1880 free(buf);
1881 return 0;
1882
1883outerr:
1884 do {
1885 int saved_errno = errno;
1886 if (buf)
1887 free(buf);
ab01dbbb
SH
1888 if (fp)
1889 fclose(fp);
aba5acdf
SH
1890 errno = saved_errno;
1891 return -1;
1892 } while (0);
1893}
1894
1895
d1f28cf1 1896static int dgram_show_line(char *line, const struct filter *f, int family)
aba5acdf
SH
1897{
1898 struct tcpstat s;
1899 char *loc, *rem, *data;
1900 char opt[256];
1901 int n;
1902 char *p;
1903
1904 if ((p = strchr(line, ':')) == NULL)
1905 return -1;
1906 loc = p+2;
1907
1908 if ((p = strchr(loc, ':')) == NULL)
1909 return -1;
1910 p[5] = 0;
1911 rem = p+6;
1912
1913 if ((p = strchr(rem, ':')) == NULL)
1914 return -1;
1915 p[5] = 0;
1916 data = p+6;
1917
1918 do {
1919 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1920
1921 if (!(f->states & (1<<state)))
1922 return 0;
1923 } while (0);
1924
1925 s.local.family = s.remote.family = family;
1926 if (family == AF_INET) {
1927 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1928 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1929 s.local.bytelen = s.remote.bytelen = 4;
1930 } else {
1931 sscanf(loc, "%08x%08x%08x%08x:%x",
1932 s.local.data,
1933 s.local.data+1,
1934 s.local.data+2,
1935 s.local.data+3,
1936 &s.lport);
1937 sscanf(rem, "%08x%08x%08x%08x:%x",
1938 s.remote.data,
1939 s.remote.data+1,
1940 s.remote.data+2,
1941 s.remote.data+3,
1942 &s.rport);
1943 s.local.bytelen = s.remote.bytelen = 16;
1944 }
1945
1946 if (f->f && run_ssfilter(f->f, &s) == 0)
1947 return 0;
1948
1949 opt[0] = 0;
e7113c61 1950 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
aba5acdf
SH
1951 &s.state, &s.wq, &s.rq,
1952 &s.uid, &s.ino,
1953 &s.refcnt, &s.sk, opt);
1954
1955 if (n < 9)
1956 opt[0] = 0;
1957
1958 if (netid_width)
1959 printf("%-*s ", netid_width, dg_proto);
1960 if (state_width)
1961 printf("%-*s ", state_width, sstate_name[s.state]);
1962
1963 printf("%-6d %-6d ", s.rq, s.wq);
1964
1965 formatted_print(&s.local, s.lport);
1966 formatted_print(&s.remote, s.rport);
1967
1968 if (show_users) {
1969 char ubuf[4096];
1970 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1971 printf(" users:(%s)", ubuf);
1972 }
1973
1974 if (show_details) {
1975 if (s.uid)
1976 printf(" uid=%u", (unsigned)s.uid);
e7113c61 1977 printf(" ino=%u", s.ino);
aba5acdf
SH
1978 printf(" sk=%llx", s.sk);
1979 if (opt[0])
1980 printf(" opt:\"%s\"", opt);
1981 }
1982 printf("\n");
1983
1984 return 0;
1985}
1986
1987
d1f28cf1 1988static int udp_show(struct filter *f)
aba5acdf 1989{
ab01dbbb 1990 FILE *fp = NULL;
aba5acdf 1991
346f8ca8
PE
1992 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
1993 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
1994 return 0;
1995
aba5acdf
SH
1996 dg_proto = UDP_PROTO;
1997
1998 if (f->families&(1<<AF_INET)) {
69cae645 1999 if ((fp = net_udp_open()) == NULL)
aba5acdf 2000 goto outerr;
ab01dbbb 2001 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
aba5acdf 2002 goto outerr;
ab01dbbb 2003 fclose(fp);
aba5acdf
SH
2004 }
2005
2006 if ((f->families&(1<<AF_INET6)) &&
69cae645 2007 (fp = net_udp6_open()) != NULL) {
ab01dbbb 2008 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
aba5acdf 2009 goto outerr;
ab01dbbb 2010 fclose(fp);
aba5acdf
SH
2011 }
2012 return 0;
2013
2014outerr:
2015 do {
2016 int saved_errno = errno;
ab01dbbb
SH
2017 if (fp)
2018 fclose(fp);
aba5acdf
SH
2019 errno = saved_errno;
2020 return -1;
2021 } while (0);
2022}
2023
d1f28cf1 2024static int raw_show(struct filter *f)
aba5acdf 2025{
ab01dbbb 2026 FILE *fp = NULL;
aba5acdf
SH
2027
2028 dg_proto = RAW_PROTO;
2029
2030 if (f->families&(1<<AF_INET)) {
69cae645 2031 if ((fp = net_raw_open()) == NULL)
aba5acdf 2032 goto outerr;
ab01dbbb 2033 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
aba5acdf 2034 goto outerr;
ab01dbbb 2035 fclose(fp);
aba5acdf
SH
2036 }
2037
2038 if ((f->families&(1<<AF_INET6)) &&
69cae645 2039 (fp = net_raw6_open()) != NULL) {
ab01dbbb 2040 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
aba5acdf 2041 goto outerr;
ab01dbbb 2042 fclose(fp);
aba5acdf
SH
2043 }
2044 return 0;
2045
2046outerr:
2047 do {
2048 int saved_errno = errno;
ab01dbbb
SH
2049 if (fp)
2050 fclose(fp);
aba5acdf
SH
2051 errno = saved_errno;
2052 return -1;
2053 } while (0);
2054}
2055
2056
2057struct unixstat
2058{
2059 struct unixstat *next;
2060 int ino;
2061 int peer;
2062 int rq;
2063 int wq;
2064 int state;
2065 int type;
2066 char *name;
2067};
2068
2069
2070
2071int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2072 SS_ESTABLISHED, SS_CLOSING };
2073
2074
2075#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat))
2076
d1f28cf1 2077static void unix_list_free(struct unixstat *list)
aba5acdf
SH
2078{
2079 while (list) {
2080 struct unixstat *s = list;
2081 list = list->next;
2082 if (s->name)
2083 free(s->name);
2084 free(s);
2085 }
2086}
2087
d1f28cf1 2088static void unix_list_print(struct unixstat *list, struct filter *f)
aba5acdf
SH
2089{
2090 struct unixstat *s;
2091 char *peer;
2092
2093 for (s = list; s; s = s->next) {
2094 if (!(f->states & (1<<s->state)))
2095 continue;
2096 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2097 continue;
2098 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2099 continue;
2100
2101 peer = "*";
2102 if (s->peer) {
2103 struct unixstat *p;
2104 for (p = list; p; p = p->next) {
2105 if (s->peer == p->ino)
2106 break;
2107 }
2108 if (!p) {
2109 peer = "?";
2110 } else {
2111 peer = p->name ? : "*";
2112 }
2113 }
2114
2115 if (f->f) {
2116 struct tcpstat tst;
2117 tst.local.family = AF_UNIX;
2118 tst.remote.family = AF_UNIX;
2119 memcpy(tst.local.data, &s->name, sizeof(s->name));
2120 if (strcmp(peer, "*") == 0)
2121 memset(tst.remote.data, 0, sizeof(peer));
2122 else
ae665a52 2123 memcpy(tst.remote.data, &peer, sizeof(peer));
aba5acdf
SH
2124 if (run_ssfilter(f->f, &tst) == 0)
2125 continue;
2126 }
2127
2128 if (netid_width)
ae665a52 2129 printf("%-*s ", netid_width,
aba5acdf
SH
2130 s->type == SOCK_STREAM ? "u_str" : "u_dgr");
2131 if (state_width)
2132 printf("%-*s ", state_width, sstate_name[s->state]);
2133 printf("%-6d %-6d ", s->rq, s->wq);
2134 printf("%*s %-*d %*s %-*d",
2135 addr_width, s->name ? : "*", serv_width, s->ino,
2136 addr_width, peer, serv_width, s->peer);
2137 if (show_users) {
2138 char ubuf[4096];
2139 if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0)
2140 printf(" users:(%s)", ubuf);
2141 }
2142 printf("\n");
2143 }
2144}
2145
dfbaa90d
PE
2146static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
2147{
2148 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2149 struct rtattr *tb[UNIX_DIAG_MAX+1];
2150 char name[128];
2151 int peer_ino;
defd61ca 2152 __u32 rqlen, wqlen;
dfbaa90d
PE
2153
2154 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
2155 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2156
2157 if (netid_width)
2158 printf("%-*s ", netid_width,
2159 r->udiag_type == SOCK_STREAM ? "u_str" : "u_dgr");
2160 if (state_width)
2161 printf("%-*s ", state_width, sstate_name[r->udiag_state]);
2162
defd61ca
HFS
2163 if (tb[UNIX_DIAG_RQLEN]) {
2164 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
2165 rqlen = rql->udiag_rqueue;
2166 wqlen = rql->udiag_wqueue;
2167 } else {
dfbaa90d 2168 rqlen = 0;
defd61ca
HFS
2169 wqlen = 0;
2170 }
dfbaa90d 2171
defd61ca 2172 printf("%-6u %-6u ", rqlen, wqlen);
dfbaa90d
PE
2173
2174 if (tb[UNIX_DIAG_NAME]) {
2175 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2176
2177 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2178 name[len] = '\0';
2179 if (name[0] == '\0')
2180 name[0] = '@';
2181 } else
2182 sprintf(name, "*");
2183
2184 if (tb[UNIX_DIAG_PEER])
5048f9a0 2185 peer_ino = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
dfbaa90d
PE
2186 else
2187 peer_ino = 0;
2188
2189 printf("%*s %-*d %*s %-*d",
2190 addr_width, name,
2191 serv_width, r->udiag_ino,
2192 addr_width, "*", /* FIXME */
2193 serv_width, peer_ino);
2194
2195 if (show_users) {
2196 char ubuf[4096];
2197 if (find_users(r->udiag_ino, ubuf, sizeof(ubuf)) > 0)
2198 printf(" users:(%s)", ubuf);
2199 }
2200
51ff9f24
HFS
2201 if (show_mem) {
2202 printf("\n\t");
2203 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2204 }
2205
dfbaa90d
PE
2206 printf("\n");
2207
2208 return 0;
2209}
2210
2211static int unix_show_netlink(struct filter *f, FILE *dump_fp)
2212{
2213 int fd;
dfbaa90d
PE
2214 struct {
2215 struct nlmsghdr nlh;
2216 struct unix_diag_req r;
2217 } req;
dfbaa90d 2218 char buf[8192];
dfbaa90d
PE
2219
2220 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
2221 return -1;
2222
2728f598 2223 memset(&req, 0, sizeof(req));
dfbaa90d
PE
2224 req.nlh.nlmsg_len = sizeof(req);
2225 req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
2226 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
dfbaa90d 2227 req.nlh.nlmsg_seq = 123456;
2728f598 2228
dfbaa90d 2229 req.r.sdiag_family = AF_UNIX;
dfbaa90d
PE
2230 req.r.udiag_states = f->states;
2231 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
51ff9f24
HFS
2232 if (show_mem)
2233 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
dfbaa90d 2234
2728f598 2235 if (send(fd, &req, sizeof(req), 0) < 0) {
a3fd8e58 2236 close(fd);
dfbaa90d 2237 return -1;
a3fd8e58 2238 }
dfbaa90d 2239
dfbaa90d 2240 while (1) {
2728f598 2241 ssize_t status;
dfbaa90d 2242 struct nlmsghdr *h;
2728f598
SH
2243 struct sockaddr_nl nladdr;
2244 socklen_t slen = sizeof(nladdr);
dfbaa90d 2245
2728f598
SH
2246 status = recvfrom(fd, buf, sizeof(buf), 0,
2247 (struct sockaddr *) &nladdr, &slen);
dfbaa90d
PE
2248 if (status < 0) {
2249 if (errno == EINTR)
2250 continue;
2251 perror("OVERRUN");
2252 continue;
2253 }
2254 if (status == 0) {
2255 fprintf(stderr, "EOF on netlink\n");
2728f598 2256 goto close_it;
dfbaa90d
PE
2257 }
2258
2259 if (dump_fp)
2260 fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
2261
2262 h = (struct nlmsghdr*)buf;
2263 while (NLMSG_OK(h, status)) {
2264 int err;
2265
2266 if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
2267 h->nlmsg_seq != 123456)
2268 goto skip_it;
2269
2728f598
SH
2270 if (h->nlmsg_type == NLMSG_DONE)
2271 goto close_it;
2272
dfbaa90d
PE
2273 if (h->nlmsg_type == NLMSG_ERROR) {
2274 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
2275 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
2276 fprintf(stderr, "ERROR truncated\n");
2277 } else {
2278 errno = -err->error;
a3fd8e58
ED
2279 if (errno != ENOENT)
2280 fprintf(stderr, "UDIAG answers %d\n", errno);
dfbaa90d 2281 }
a3fd8e58
ED
2282 close(fd);
2283 return -1;
dfbaa90d
PE
2284 }
2285 if (!dump_fp) {
2286 err = unix_show_sock(h, f);
a3fd8e58
ED
2287 if (err < 0) {
2288 close(fd);
dfbaa90d 2289 return err;
a3fd8e58 2290 }
dfbaa90d
PE
2291 }
2292
2293skip_it:
2294 h = NLMSG_NEXT(h, status);
2295 }
2728f598 2296
dfbaa90d 2297 if (status) {
2728f598 2298 fprintf(stderr, "!!!Remnant of size %zd\n", status);
dfbaa90d
PE
2299 exit(1);
2300 }
2301 }
2728f598
SH
2302
2303close_it:
a3fd8e58 2304 close(fd);
dfbaa90d
PE
2305 return 0;
2306}
2307
d1f28cf1 2308static int unix_show(struct filter *f)
aba5acdf
SH
2309{
2310 FILE *fp;
2311 char buf[256];
2312 char name[128];
2313 int newformat = 0;
2314 int cnt;
2315 struct unixstat *list = NULL;
2316
dfbaa90d
PE
2317 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
2318 && unix_show_netlink(f, NULL) == 0)
2319 return 0;
2320
ab01dbbb 2321 if ((fp = net_unix_open()) == NULL)
aba5acdf
SH
2322 return -1;
2323 fgets(buf, sizeof(buf)-1, fp);
2324
ae665a52 2325 if (memcmp(buf, "Peer", 4) == 0)
aba5acdf
SH
2326 newformat = 1;
2327 cnt = 0;
2328
2329 while (fgets(buf, sizeof(buf)-1, fp)) {
2330 struct unixstat *u, **insp;
2331 int flags;
2332
2333 if (!(u = malloc(sizeof(*u))))
2334 break;
2335 u->name = NULL;
2336
2337 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
2338 &u->peer, &u->rq, &u->wq, &flags, &u->type,
2339 &u->state, &u->ino, name) < 8)
2340 name[0] = 0;
2341
2342 if (flags&(1<<16)) {
2343 u->state = SS_LISTEN;
2344 } else {
2345 u->state = unix_state_map[u->state-1];
2346 if (u->type == SOCK_DGRAM &&
2347 u->state == SS_CLOSE &&
2348 u->peer)
2349 u->state = SS_ESTABLISHED;
2350 }
2351
2352 if (!newformat) {
2353 u->peer = 0;
2354 u->rq = 0;
2355 u->wq = 0;
2356 }
2357
2358 insp = &list;
2359 while (*insp) {
2360 if (u->type < (*insp)->type ||
2361 (u->type == (*insp)->type &&
2362 u->ino < (*insp)->ino))
2363 break;
2364 insp = &(*insp)->next;
2365 }
2366 u->next = *insp;
2367 *insp = u;
2368
2369 if (name[0]) {
2370 if ((u->name = malloc(strlen(name)+1)) == NULL)
2371 break;
2372 strcpy(u->name, name);
2373 }
2374 if (++cnt > MAX_UNIX_REMEMBER) {
2375 unix_list_print(list, f);
2376 unix_list_free(list);
2377 list = NULL;
2378 cnt = 0;
2379 }
2380 }
a3fd8e58 2381 fclose(fp);
aba5acdf
SH
2382 if (list) {
2383 unix_list_print(list, f);
2384 unix_list_free(list);
2385 list = NULL;
2386 cnt = 0;
2387 }
2388
2389 return 0;
2390}
2391
2392
d1f28cf1 2393static int packet_show(struct filter *f)
aba5acdf
SH
2394{
2395 FILE *fp;
2396 char buf[256];
2397 int type;
2398 int prot;
2399 int iface;
2400 int state;
2401 int rq;
2402 int uid;
2403 int ino;
2404 unsigned long long sk;
2405
2406 if (!(f->states & (1<<SS_CLOSE)))
2407 return 0;
2408
ab01dbbb 2409 if ((fp = net_packet_open()) == NULL)
aba5acdf
SH
2410 return -1;
2411 fgets(buf, sizeof(buf)-1, fp);
2412
2413 while (fgets(buf, sizeof(buf)-1, fp)) {
2414 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
2415 &sk,
2416 &type, &prot, &iface, &state,
2417 &rq, &uid, &ino);
2418
2419 if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
2420 continue;
2421 if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
2422 continue;
2423 if (f->f) {
2424 struct tcpstat tst;
2425 tst.local.family = AF_PACKET;
2426 tst.remote.family = AF_PACKET;
2427 tst.rport = 0;
2428 tst.lport = iface;
2429 tst.local.data[0] = prot;
2430 tst.remote.data[0] = 0;
2431 if (run_ssfilter(f->f, &tst) == 0)
2432 continue;
2433 }
2434
2435 if (netid_width)
ae665a52 2436 printf("%-*s ", netid_width,
aba5acdf
SH
2437 type == SOCK_RAW ? "p_raw" : "p_dgr");
2438 if (state_width)
2439 printf("%-*s ", state_width, "UNCONN");
2440 printf("%-6d %-6d ", rq, 0);
2441 if (prot == 3) {
2442 printf("%*s:", addr_width, "*");
2443 } else {
2444 char tb[16];
ae665a52 2445 printf("%*s:", addr_width,
aba5acdf
SH
2446 ll_proto_n2a(htons(prot), tb, sizeof(tb)));
2447 }
2448 if (iface == 0) {
2449 printf("%-*s ", serv_width, "*");
2450 } else {
2451 printf("%-*s ", serv_width, xll_index_to_name(iface));
2452 }
2453 printf("%*s*%-*s",
2454 addr_width, "", serv_width, "");
2455
2456 if (show_users) {
2457 char ubuf[4096];
2458 if (find_users(ino, ubuf, sizeof(ubuf)) > 0)
2459 printf(" users:(%s)", ubuf);
2460 }
2461 if (show_details) {
2462 printf(" ino=%u uid=%u sk=%llx", ino, uid, sk);
2463 }
2464 printf("\n");
2465 }
2466
2467 return 0;
2468}
2469
d1f28cf1 2470static int netlink_show(struct filter *f)
aba5acdf
SH
2471{
2472 FILE *fp;
2473 char buf[256];
2474 int prot, pid;
2475 unsigned groups;
2476 int rq, wq, rc;
2477 unsigned long long sk, cb;
2478
2479 if (!(f->states & (1<<SS_CLOSE)))
2480 return 0;
2481
ab01dbbb 2482 if ((fp = net_netlink_open()) == NULL)
aba5acdf
SH
2483 return -1;
2484 fgets(buf, sizeof(buf)-1, fp);
2485
2486 while (fgets(buf, sizeof(buf)-1, fp)) {
2487 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
2488 &sk,
2489 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
2490
2491 if (f->f) {
2492 struct tcpstat tst;
2493 tst.local.family = AF_NETLINK;
2494 tst.remote.family = AF_NETLINK;
2495 tst.rport = -1;
2496 tst.lport = pid;
2497 tst.local.data[0] = prot;
2498 tst.remote.data[0] = 0;
2499 if (run_ssfilter(f->f, &tst) == 0)
2500 continue;
2501 }
2502
2503 if (netid_width)
ae665a52 2504 printf("%-*s ", netid_width, "nl");
aba5acdf
SH
2505 if (state_width)
2506 printf("%-*s ", state_width, "UNCONN");
2507 printf("%-6d %-6d ", rq, wq);
2508 if (resolve_services && prot == 0)
2509 printf("%*s:", addr_width, "rtnl");
2510 else if (resolve_services && prot == 3)
2511 printf("%*s:", addr_width, "fw");
2512 else if (resolve_services && prot == 4)
2513 printf("%*s:", addr_width, "tcpdiag");
2514 else
2515 printf("%*d:", addr_width, prot);
2516 if (pid == -1) {
2517 printf("%-*s ", serv_width, "*");
2518 } else if (resolve_services) {
2519 int done = 0;
2520 if (!pid) {
2521 done = 1;
2522 printf("%-*s ", serv_width, "kernel");
2523 } else if (pid > 0) {
2524 char procname[64];
2525 FILE *fp;
2526 sprintf(procname, "%s/%d/stat",
2527 getenv("PROC_ROOT") ? : "/proc", pid);
2528 if ((fp = fopen(procname, "r")) != NULL) {
2529 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
ae665a52 2530 sprintf(procname+strlen(procname), "/%d", pid);
aba5acdf
SH
2531 printf("%-*s ", serv_width, procname);
2532 done = 1;
2533 }
2534 fclose(fp);
2535 }
2536 }
2537 if (!done)
2538 printf("%-*d ", serv_width, pid);
2539 } else {
2540 printf("%-*d ", serv_width, pid);
2541 }
2542 printf("%*s*%-*s",
2543 addr_width, "", serv_width, "");
2544
2545 if (show_details) {
2546 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
2547 }
2548 printf("\n");
2549 }
2550
2551 return 0;
2552}
2553
2554struct snmpstat
2555{
2556 int tcp_estab;
2557};
2558
d1f28cf1 2559static int get_snmp_int(char *proto, char *key, int *result)
aba5acdf
SH
2560{
2561 char buf[1024];
2562 FILE *fp;
2563 int protolen = strlen(proto);
2564 int keylen = strlen(key);
2565
2566 *result = 0;
2567
ab01dbbb 2568 if ((fp = net_snmp_open()) == NULL)
aba5acdf
SH
2569 return -1;
2570
2571 while (fgets(buf, sizeof(buf), fp) != NULL) {
2572 char *p = buf;
2573 int pos = 0;
2574 if (memcmp(buf, proto, protolen))
2575 continue;
2576 while ((p = strchr(p, ' ')) != NULL) {
2577 pos++;
2578 p++;
2579 if (memcmp(p, key, keylen) == 0 &&
2580 (p[keylen] == ' ' || p[keylen] == '\n'))
2581 break;
2582 }
2583 if (fgets(buf, sizeof(buf), fp) == NULL)
2584 break;
2585 if (memcmp(buf, proto, protolen))
2586 break;
2587 p = buf;
2588 while ((p = strchr(p, ' ')) != NULL) {
2589 p++;
2590 if (--pos == 0) {
2591 sscanf(p, "%d", result);
2592 fclose(fp);
2593 return 0;
2594 }
2595 }
2596 }
2597
2598 fclose(fp);
2599 errno = ESRCH;
2600 return -1;
2601}
2602
2603
2604/* Get stats from sockstat */
2605
2606struct sockstat
2607{
2608 int socks;
2609 int tcp_mem;
2610 int tcp_total;
2611 int tcp_orphans;
2612 int tcp_tws;
2613 int tcp4_hashed;
2614 int udp4;
2615 int raw4;
2616 int frag4;
2617 int frag4_mem;
2618 int tcp6_hashed;
2619 int udp6;
2620 int raw6;
2621 int frag6;
2622 int frag6_mem;
2623};
2624
2625static void get_sockstat_line(char *line, struct sockstat *s)
2626{
2627 char id[256], rem[256];
2628
2629 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
2630 return;
2631
2632 if (strcmp(id, "sockets:") == 0)
2633 sscanf(rem, "%*s%d", &s->socks);
2634 else if (strcmp(id, "UDP:") == 0)
2635 sscanf(rem, "%*s%d", &s->udp4);
2636 else if (strcmp(id, "UDP6:") == 0)
2637 sscanf(rem, "%*s%d", &s->udp6);
2638 else if (strcmp(id, "RAW:") == 0)
2639 sscanf(rem, "%*s%d", &s->raw4);
2640 else if (strcmp(id, "RAW6:") == 0)
2641 sscanf(rem, "%*s%d", &s->raw6);
2642 else if (strcmp(id, "TCP6:") == 0)
2643 sscanf(rem, "%*s%d", &s->tcp6_hashed);
2644 else if (strcmp(id, "FRAG:") == 0)
2645 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
2646 else if (strcmp(id, "FRAG6:") == 0)
2647 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
2648 else if (strcmp(id, "TCP:") == 0)
2649 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
2650 &s->tcp4_hashed,
2651 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
2652}
2653
d1f28cf1 2654static int get_sockstat(struct sockstat *s)
aba5acdf
SH
2655{
2656 char buf[256];
2657 FILE *fp;
2658
2659 memset(s, 0, sizeof(*s));
2660
ab01dbbb 2661 if ((fp = net_sockstat_open()) == NULL)
aba5acdf
SH
2662 return -1;
2663 while(fgets(buf, sizeof(buf), fp) != NULL)
2664 get_sockstat_line(buf, s);
2665 fclose(fp);
2666
ab01dbbb 2667 if ((fp = net_sockstat6_open()) == NULL)
aba5acdf
SH
2668 return 0;
2669 while(fgets(buf, sizeof(buf), fp) != NULL)
2670 get_sockstat_line(buf, s);
2671 fclose(fp);
2672
2673 return 0;
2674}
2675
d1f28cf1 2676static int print_summary(void)
aba5acdf
SH
2677{
2678 struct sockstat s;
2679 struct snmpstat sn;
2680
2681 if (get_sockstat(&s) < 0)
2682 perror("ss: get_sockstat");
2683 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
2684 perror("ss: get_snmpstat");
2685
2686 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
2687
2688 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
2689 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
2690 sn.tcp_estab,
2691 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
2692 s.tcp_orphans,
2693 slabstat.tcp_syns,
2694 s.tcp_tws, slabstat.tcp_tws,
2695 slabstat.tcp_ports
2696 );
2697
2698 printf("\n");
2699 printf("Transport Total IP IPv6\n");
2700 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
2701 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
2702 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
2703 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
ae665a52 2704 printf("INET %-9d %-9d %-9d\n",
aba5acdf
SH
2705 s.raw4+s.udp4+s.tcp4_hashed+
2706 s.raw6+s.udp6+s.tcp6_hashed,
2707 s.raw4+s.udp4+s.tcp4_hashed,
2708 s.raw6+s.udp6+s.tcp6_hashed);
2709 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
2710
2711 printf("\n");
2712
2713 return 0;
2714}
2715
7a96e199 2716static void _usage(FILE *dest)
aba5acdf 2717{
7a96e199 2718 fprintf(dest,
aba5acdf
SH
2719"Usage: ss [ OPTIONS ]\n"
2720" ss [ OPTIONS ] [ FILTER ]\n"
ab61159a
SH
2721" -h, --help this message\n"
2722" -V, --version output version information\n"
2723" -n, --numeric don't resolve service names\n"
2724" -r, --resolve resolve host names\n"
2725" -a, --all display all sockets\n"
2726" -l, --listening display listening sockets\n"
2727" -o, --options show timer information\n"
2728" -e, --extended show detailed socket information\n"
2729" -m, --memory show socket memory usage\n"
2730" -p, --processes show process using socket\n"
2731" -i, --info show internal TCP information\n"
2732" -s, --summary show socket usage summary\n"
2733"\n"
2734" -4, --ipv4 display only IP version 4 sockets\n"
2735" -6, --ipv6 display only IP version 6 sockets\n"
2736" -0, --packet display PACKET sockets\n"
2737" -t, --tcp display only TCP sockets\n"
2738" -u, --udp display only UDP sockets\n"
351efcde 2739" -d, --dccp display only DCCP sockets\n"
ab61159a
SH
2740" -w, --raw display only RAW sockets\n"
2741" -x, --unix display only Unix domain sockets\n"
2742" -f, --family=FAMILY display sockets of type FAMILY\n"
2743"\n"
583de149 2744" -A, --query=QUERY, --socket=QUERY\n"
aba5acdf 2745" QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
ab61159a 2746"\n"
583de149 2747" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
ab61159a 2748" -F, --filter=FILE read filter information from FILE\n"
aba5acdf 2749" FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
ab61159a 2750 );
7a96e199
AH
2751}
2752
2753static void help(void) __attribute__((noreturn));
2754static void help(void)
2755{
2756 _usage(stdout);
2757 exit(0);
2758}
2759
2760static void usage(void) __attribute__((noreturn));
2761static void usage(void)
2762{
2763 _usage(stderr);
aba5acdf
SH
2764 exit(-1);
2765}
2766
2767
d1f28cf1 2768static int scan_state(const char *state)
aba5acdf
SH
2769{
2770 int i;
2771 if (strcasecmp(state, "close") == 0 ||
2772 strcasecmp(state, "closed") == 0)
2773 return (1<<SS_CLOSE);
2774 if (strcasecmp(state, "syn-rcv") == 0)
2775 return (1<<SS_SYN_RECV);
1a5bad5a 2776 if (strcasecmp(state, "established") == 0)
aba5acdf
SH
2777 return (1<<SS_ESTABLISHED);
2778 if (strcasecmp(state, "all") == 0)
2779 return SS_ALL;
2780 if (strcasecmp(state, "connected") == 0)
2781 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
1a5bad5a 2782 if (strcasecmp(state, "synchronized") == 0)
aba5acdf
SH
2783 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
2784 if (strcasecmp(state, "bucket") == 0)
2785 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
2786 if (strcasecmp(state, "big") == 0)
2787 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
2788 for (i=0; i<SS_MAX; i++) {
1a5bad5a 2789 if (strcasecmp(state, sstate_namel[i]) == 0)
aba5acdf
SH
2790 return (1<<i);
2791 }
2792 return 0;
2793}
2794
ab61159a
SH
2795static const struct option long_opts[] = {
2796 { "numeric", 0, 0, 'n' },
2797 { "resolve", 0, 0, 'r' },
2798 { "options", 0, 0, 'o' },
2799 { "extended", 0, 0, 'e' },
2800 { "memory", 0, 0, 'm' },
2801 { "info", 0, 0, 'i' },
2802 { "processes", 0, 0, 'p' },
351efcde 2803 { "dccp", 0, 0, 'd' },
ab61159a
SH
2804 { "tcp", 0, 0, 't' },
2805 { "udp", 0, 0, 'u' },
2806 { "raw", 0, 0, 'w' },
2807 { "unix", 0, 0, 'x' },
2808 { "all", 0, 0, 'a' },
2809 { "listening", 0, 0, 'l' },
2810 { "ipv4", 0, 0, '4' },
2811 { "ipv6", 0, 0, '6' },
2812 { "packet", 0, 0, '0' },
2813 { "family", 1, 0, 'f' },
2814 { "socket", 1, 0, 'A' },
583de149 2815 { "query", 1, 0, 'A' },
c3f346b0 2816 { "summary", 0, 0, 's' },
583de149 2817 { "diag", 1, 0, 'D' },
ab61159a
SH
2818 { "filter", 1, 0, 'F' },
2819 { "version", 0, 0, 'V' },
2820 { "help", 0, 0, 'h' },
2821 { 0 }
ae665a52 2822
ab61159a
SH
2823};
2824
aba5acdf
SH
2825int main(int argc, char *argv[])
2826{
2827 int do_default = 1;
2828 int saw_states = 0;
2829 int saw_query = 0;
2830 int do_summary = 0;
7d105b56 2831 const char *dump_tcpdiag = NULL;
aba5acdf
SH
2832 FILE *filter_fp = NULL;
2833 int ch;
2834
2835 memset(&current_filter, 0, sizeof(current_filter));
2836
2837 current_filter.states = default_filter.states;
2838
351efcde 2839 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spf:miA:D:F:vV",
ab61159a 2840 long_opts, NULL)) != EOF) {
aba5acdf
SH
2841 switch(ch) {
2842 case 'n':
2843 resolve_services = 0;
2844 break;
2845 case 'r':
2846 resolve_hosts = 1;
2847 break;
2848 case 'o':
2849 show_options = 1;
2850 break;
2851 case 'e':
2852 show_options = 1;
2853 show_details++;
2854 break;
2855 case 'm':
2856 show_mem = 1;
2857 break;
2858 case 'i':
2859 show_tcpinfo = 1;
2860 break;
2861 case 'p':
2862 show_users++;
fbc0f876 2863 user_ent_hash_build();
aba5acdf 2864 break;
351efcde
SH
2865 case 'd':
2866 current_filter.dbs |= (1<<DCCP_DB);
2867 do_default = 0;
2868 break;
aba5acdf
SH
2869 case 't':
2870 current_filter.dbs |= (1<<TCP_DB);
2871 do_default = 0;
2872 break;
2873 case 'u':
2874 current_filter.dbs |= (1<<UDP_DB);
2875 do_default = 0;
2876 break;
2877 case 'w':
2878 current_filter.dbs |= (1<<RAW_DB);
2879 do_default = 0;
2880 break;
2881 case 'x':
2882 current_filter.dbs |= UNIX_DBM;
2883 do_default = 0;
2884 break;
2885 case 'a':
2886 current_filter.states = SS_ALL;
2887 break;
2888 case 'l':
16963ce6 2889 current_filter.states = (1<<SS_LISTEN) | (1<<SS_CLOSE);
aba5acdf
SH
2890 break;
2891 case '4':
2892 preferred_family = AF_INET;
2893 break;
2894 case '6':
2895 preferred_family = AF_INET6;
2896 break;
2897 case '0':
2898 preferred_family = AF_PACKET;
2899 break;
2900 case 'f':
2901 if (strcmp(optarg, "inet") == 0)
2902 preferred_family = AF_INET;
2903 else if (strcmp(optarg, "inet6") == 0)
2904 preferred_family = AF_INET6;
2905 else if (strcmp(optarg, "link") == 0)
2906 preferred_family = AF_PACKET;
2907 else if (strcmp(optarg, "unix") == 0)
2908 preferred_family = AF_UNIX;
2909 else if (strcmp(optarg, "netlink") == 0)
2910 preferred_family = AF_NETLINK;
2911 else if (strcmp(optarg, "help") == 0)
7a96e199 2912 help();
aba5acdf
SH
2913 else {
2914 fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
2915 usage();
2916 }
2917 break;
2918 case 'A':
2919 {
2920 char *p, *p1;
2921 if (!saw_query) {
2922 current_filter.dbs = 0;
2923 saw_query = 1;
2924 do_default = 0;
2925 }
2926 p = p1 = optarg;
2927 do {
2928 if ((p1 = strchr(p, ',')) != NULL)
ae665a52 2929 *p1 = 0;
aba5acdf
SH
2930 if (strcmp(p, "all") == 0) {
2931 current_filter.dbs = ALL_DB;
2932 } else if (strcmp(p, "inet") == 0) {
351efcde 2933 current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB);
aba5acdf
SH
2934 } else if (strcmp(p, "udp") == 0) {
2935 current_filter.dbs |= (1<<UDP_DB);
351efcde
SH
2936 } else if (strcmp(p, "dccp") == 0) {
2937 current_filter.dbs |= (1<<DCCP_DB);
aba5acdf
SH
2938 } else if (strcmp(p, "tcp") == 0) {
2939 current_filter.dbs |= (1<<TCP_DB);
2940 } else if (strcmp(p, "raw") == 0) {
2941 current_filter.dbs |= (1<<RAW_DB);
2942 } else if (strcmp(p, "unix") == 0) {
2943 current_filter.dbs |= UNIX_DBM;
1a5bad5a 2944 } else if (strcasecmp(p, "unix_stream") == 0 ||
aba5acdf
SH
2945 strcmp(p, "u_str") == 0) {
2946 current_filter.dbs |= (1<<UNIX_ST_DB);
1a5bad5a 2947 } else if (strcasecmp(p, "unix_dgram") == 0 ||
aba5acdf
SH
2948 strcmp(p, "u_dgr") == 0) {
2949 current_filter.dbs |= (1<<UNIX_DG_DB);
2950 } else if (strcmp(p, "packet") == 0) {
2951 current_filter.dbs |= PACKET_DBM;
2952 } else if (strcmp(p, "packet_raw") == 0 ||
2953 strcmp(p, "p_raw") == 0) {
2954 current_filter.dbs |= (1<<PACKET_R_DB);
2955 } else if (strcmp(p, "packet_dgram") == 0 ||
2956 strcmp(p, "p_dgr") == 0) {
2957 current_filter.dbs |= (1<<PACKET_DG_DB);
2958 } else if (strcmp(p, "netlink") == 0) {
2959 current_filter.dbs |= (1<<NETLINK_DB);
2960 } else {
2961 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
2962 usage();
2963 }
2964 p = p1 + 1;
2965 } while (p1);
2966 break;
2967 }
2968 case 's':
2969 do_summary = 1;
2970 break;
2971 case 'D':
2972 dump_tcpdiag = optarg;
2973 break;
2974 case 'F':
2975 if (filter_fp) {
2976 fprintf(stderr, "More than one filter file\n");
2977 exit(-1);
2978 }
2979 if (optarg[0] == '-')
2980 filter_fp = stdin;
2981 else
2982 filter_fp = fopen(optarg, "r");
2983 if (!filter_fp) {
2984 perror("fopen filter file");
2985 exit(-1);
2986 }
2987 break;
2988 case 'v':
2989 case 'V':
2990 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
2991 exit(0);
2992 case 'h':
2993 case '?':
7a96e199 2994 help();
aba5acdf
SH
2995 default:
2996 usage();
2997 }
2998 }
2999
3000 argc -= optind;
3001 argv += optind;
3002
3003 get_slabstat(&slabstat);
3004
3005 if (do_summary) {
3006 print_summary();
3007 if (do_default && argc == 0)
3008 exit(0);
3009 }
3010
3011 if (do_default)
3012 current_filter.dbs = default_filter.dbs;
3013
3014 if (preferred_family == AF_UNSPEC) {
3015 if (!(current_filter.dbs&~UNIX_DBM))
3016 preferred_family = AF_UNIX;
3017 else if (!(current_filter.dbs&~PACKET_DBM))
3018 preferred_family = AF_PACKET;
3019 else if (!(current_filter.dbs&~(1<<NETLINK_DB)))
3020 preferred_family = AF_NETLINK;
3021 }
3022
3023 if (preferred_family != AF_UNSPEC) {
3024 int mask2;
3025 if (preferred_family == AF_INET ||
3026 preferred_family == AF_INET6) {
f70d96a4 3027 mask2= current_filter.dbs;
aba5acdf
SH
3028 } else if (preferred_family == AF_PACKET) {
3029 mask2 = PACKET_DBM;
3030 } else if (preferred_family == AF_UNIX) {
3031 mask2 = UNIX_DBM;
3032 } else if (preferred_family == AF_NETLINK) {
3033 mask2 = (1<<NETLINK_DB);
3034 } else {
3035 mask2 = 0;
3036 }
3037
3038 if (do_default)
3039 current_filter.dbs = mask2;
3040 else
3041 current_filter.dbs &= mask2;
3042 current_filter.families = (1<<preferred_family);
3043 } else {
3044 if (!do_default)
3045 current_filter.families = ~0;
3046 else
3047 current_filter.families = default_filter.families;
3048 }
3049 if (current_filter.dbs == 0) {
3050 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
3051 exit(0);
3052 }
3053 if (current_filter.families == 0) {
3054 fprintf(stderr, "ss: no families to show with such filter.\n");
3055 exit(0);
3056 }
3057
3058 if (resolve_services && resolve_hosts &&
351efcde 3059 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
aba5acdf
SH
3060 init_service_resolver();
3061
3062 /* Now parse filter... */
3063 if (argc == 0 && filter_fp) {
3064 if (ssfilter_parse(&current_filter.f, 0, NULL, filter_fp))
3065 usage();
3066 }
3067
3068 while (argc > 0) {
3069 if (strcmp(*argv, "state") == 0) {
3070 NEXT_ARG();
3071 if (!saw_states)
3072 current_filter.states = 0;
3073 current_filter.states |= scan_state(*argv);
3074 saw_states = 1;
3075 } else if (strcmp(*argv, "exclude") == 0 ||
3076 strcmp(*argv, "excl") == 0) {
3077 NEXT_ARG();
3078 if (!saw_states)
3079 current_filter.states = SS_ALL;
3080 current_filter.states &= ~scan_state(*argv);
3081 saw_states = 1;
3082 } else {
3083 if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
3084 usage();
3085 break;
3086 }
3087 argc--; argv++;
3088 }
3089
3090 if (current_filter.states == 0) {
3091 fprintf(stderr, "ss: no socket states to show with such filter.\n");
3092 exit(0);
3093 }
3094
3095 if (dump_tcpdiag) {
3096 FILE *dump_fp = stdout;
3097 if (!(current_filter.dbs & (1<<TCP_DB))) {
3098 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
3099 exit(0);
3100 }
3101 if (dump_tcpdiag[0] != '-') {
3102 dump_fp = fopen(dump_tcpdiag, "w");
3103 if (!dump_tcpdiag) {
3104 perror("fopen dump file");
3105 exit(-1);
3106 }
3107 }
3fe5b534 3108 inet_show_netlink(&current_filter, dump_fp, IPPROTO_TCP);
aba5acdf
SH
3109 fflush(dump_fp);
3110 exit(0);
3111 }
3112
3113 netid_width = 0;
3114 if (current_filter.dbs&(current_filter.dbs-1))
3115 netid_width = 5;
3116
3117 state_width = 0;
3118 if (current_filter.states&(current_filter.states-1))
3119 state_width = 10;
3120
3121 screen_width = 80;
3122 if (isatty(STDOUT_FILENO)) {
3123 struct winsize w;
3124
3125 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
3126 if (w.ws_col > 0)
3127 screen_width = w.ws_col;
3128 }
3129 }
3130
3131 addrp_width = screen_width;
3132 addrp_width -= netid_width+1;
3133 addrp_width -= state_width+1;
3134 addrp_width -= 14;
3135
3136 if (addrp_width&1) {
3137 if (netid_width)
3138 netid_width++;
3139 else if (state_width)
3140 state_width++;
3141 }
3142
3143 addrp_width /= 2;
3144 addrp_width--;
3145
3146 serv_width = resolve_services ? 7 : 5;
3147
3148 if (addrp_width < 15+serv_width+1)
3149 addrp_width = 15+serv_width+1;
3150
ae665a52 3151 addr_width = addrp_width - serv_width - 1;
aba5acdf
SH
3152
3153 if (netid_width)
3154 printf("%-*s ", netid_width, "Netid");
3155 if (state_width)
3156 printf("%-*s ", state_width, "State");
3157 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
3158
3159 printf("%*s:%-*s %*s:%-*s\n",
3160 addr_width, "Local Address", serv_width, "Port",
3161 addr_width, "Peer Address", serv_width, "Port");
3162
aba5acdf
SH
3163 fflush(stdout);
3164
3165 if (current_filter.dbs & (1<<NETLINK_DB))
3166 netlink_show(&current_filter);
3167 if (current_filter.dbs & PACKET_DBM)
3168 packet_show(&current_filter);
3169 if (current_filter.dbs & UNIX_DBM)
3170 unix_show(&current_filter);
3171 if (current_filter.dbs & (1<<RAW_DB))
3172 raw_show(&current_filter);
3173 if (current_filter.dbs & (1<<UDP_DB))
3174 udp_show(&current_filter);
3175 if (current_filter.dbs & (1<<TCP_DB))
3fe5b534 3176 tcp_show(&current_filter, IPPROTO_TCP);
351efcde 3177 if (current_filter.dbs & (1<<DCCP_DB))
3fe5b534 3178 tcp_show(&current_filter, IPPROTO_DCCP);
aba5acdf
SH
3179 return 0;
3180}