2 * ss.c "sockstat", socket statistics
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
24 #include <arpa/inet.h>
33 #include "libnetlink.h"
34 #include "namespace.h"
37 #include <linux/tcp.h>
38 #include <linux/sock_diag.h>
39 #include <linux/inet_diag.h>
40 #include <linux/unix_diag.h>
41 #include <linux/netdevice.h> /* for MAX_ADDR_LEN */
42 #include <linux/filter.h>
43 #include <linux/packet_diag.h>
44 #include <linux/netlink_diag.h>
46 #define MAGIC_SEQ 123456
48 #define DIAG_REQUEST(_req, _r) \
50 struct nlmsghdr nlh; \
54 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
55 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
56 .nlmsg_seq = MAGIC_SEQ, \
57 .nlmsg_len = sizeof(_req), \
62 #include <selinux/selinux.h>
64 /* Stubs for SELinux functions */
65 static int is_selinux_enabled(void)
70 static int getpidcon(pid_t pid, char **context)
76 static int getfilecon(char *path, char **context)
82 static int security_get_initial_context(char *name, char **context)
89 int resolve_hosts = 0;
90 int resolve_services = 1;
91 int preferred_family = AF_UNSPEC;
98 int show_proc_ctx = 0;
99 int show_sock_ctx = 0;
100 /* If show_users & show_proc_ctx only do user_ent_hash_build() once */
101 int user_ent_hash_build_init = 0;
110 static const char *TCP_PROTO = "tcp";
111 static const char *UDP_PROTO = "udp";
112 static const char *RAW_PROTO = "raw";
113 static const char *dg_proto = NULL;
130 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
131 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
132 #define ALL_DB ((1<<MAX_DB)-1)
133 #define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB))
151 #define SS_ALL ((1 << SS_MAX) - 1)
152 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
154 #include "ssfilter.h"
164 static const struct filter default_dbs[MAX_DB] = {
167 .families = (1 << AF_INET) | (1 << AF_INET6),
171 .families = (1 << AF_INET) | (1 << AF_INET6),
174 .states = (1 << SS_ESTABLISHED),
175 .families = (1 << AF_INET) | (1 << AF_INET6),
178 .states = (1 << SS_ESTABLISHED),
179 .families = (1 << AF_INET) | (1 << AF_INET6),
182 .states = (1 << SS_CLOSE),
183 .families = (1 << AF_UNIX),
187 .families = (1 << AF_UNIX),
191 .families = (1 << AF_UNIX),
194 .states = (1 << SS_CLOSE),
195 .families = (1 << AF_PACKET),
198 .states = (1 << SS_CLOSE),
199 .families = (1 << AF_PACKET),
202 .states = (1 << SS_CLOSE),
203 .families = (1 << AF_NETLINK),
207 static const struct filter default_afs[AF_MAX] = {
222 .states = (1 << SS_CLOSE),
225 .dbs = (1 << NETLINK_DB),
226 .states = (1 << SS_CLOSE),
230 static int do_default = 1;
231 static struct filter current_filter;
233 static void filter_db_set(struct filter *f, int db)
235 f->states |= default_dbs[db].states;
240 static void filter_af_set(struct filter *f, int af)
242 f->states |= default_afs[af].states;
243 f->families |= 1 << af;
245 preferred_family = af;
248 static int filter_af_get(struct filter *f, int af)
250 return f->families & (1 << af);
253 static void filter_default_dbs(struct filter *f)
255 filter_db_set(f, UDP_DB);
256 filter_db_set(f, DCCP_DB);
257 filter_db_set(f, TCP_DB);
258 filter_db_set(f, RAW_DB);
259 filter_db_set(f, UNIX_ST_DB);
260 filter_db_set(f, UNIX_DG_DB);
261 filter_db_set(f, UNIX_SQ_DB);
262 filter_db_set(f, PACKET_R_DB);
263 filter_db_set(f, PACKET_DG_DB);
264 filter_db_set(f, NETLINK_DB);
267 static void filter_states_set(struct filter *f, int states)
270 f->states = (f->states | states) & states;
273 static void filter_merge_defaults(struct filter *f)
278 for (db = 0; db < MAX_DB; db++) {
279 if (!(f->dbs & (1 << db)))
282 if (!(default_dbs[db].families & f->families))
283 f->families |= default_dbs[db].families;
285 for (af = 0; af < AF_MAX; af++) {
286 if (!(f->families & (1 << af)))
289 if (!(default_afs[af].dbs & f->dbs))
290 f->dbs |= default_afs[af].dbs;
294 static FILE *generic_proc_open(const char *env, const char *name)
296 const char *p = getenv(env);
300 p = getenv("PROC_ROOT") ? : "/proc";
301 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
305 return fopen(p, "r");
308 static FILE *net_tcp_open(void)
310 return generic_proc_open("PROC_NET_TCP", "net/tcp");
313 static FILE *net_tcp6_open(void)
315 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
318 static FILE *net_udp_open(void)
320 return generic_proc_open("PROC_NET_UDP", "net/udp");
323 static FILE *net_udp6_open(void)
325 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
328 static FILE *net_raw_open(void)
330 return generic_proc_open("PROC_NET_RAW", "net/raw");
333 static FILE *net_raw6_open(void)
335 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
338 static FILE *net_unix_open(void)
340 return generic_proc_open("PROC_NET_UNIX", "net/unix");
343 static FILE *net_packet_open(void)
345 return generic_proc_open("PROC_NET_PACKET", "net/packet");
348 static FILE *net_netlink_open(void)
350 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
353 static FILE *slabinfo_open(void)
355 return generic_proc_open("PROC_SLABINFO", "slabinfo");
358 static FILE *net_sockstat_open(void)
360 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
363 static FILE *net_sockstat6_open(void)
365 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
368 static FILE *net_snmp_open(void)
370 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
373 static FILE *ephemeral_ports_open(void)
375 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
379 struct user_ent *next;
388 #define USER_ENT_HASH_SIZE 256
389 struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
391 static int user_ent_hashfn(unsigned int ino)
393 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
395 return val & (USER_ENT_HASH_SIZE - 1);
398 static void user_ent_add(unsigned int ino, char *process,
403 struct user_ent *p, **pp;
405 p = malloc(sizeof(struct user_ent));
407 fprintf(stderr, "ss: failed to malloc buffer\n");
414 p->process = strdup(process);
415 p->process_ctx = strdup(proc_ctx);
416 p->socket_ctx = strdup(sock_ctx);
418 pp = &user_ent_hash[user_ent_hashfn(ino)];
423 static void user_ent_destroy(void)
425 struct user_ent *p, *p_next;
428 while (cnt != USER_ENT_HASH_SIZE) {
429 p = user_ent_hash[cnt];
432 free(p->process_ctx);
442 static void user_ent_hash_build(void)
444 const char *root = getenv("PROC_ROOT") ? : "/proc/";
451 const char *no_ctx = "unavailable";
453 /* If show_users & show_proc_ctx set only do this once */
454 if (user_ent_hash_build_init != 0)
457 user_ent_hash_build_init = 1;
460 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
463 nameoff = strlen(name);
469 while ((d = readdir(dir)) != NULL) {
477 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
480 if (getpidcon(pid, &pid_context) != 0)
481 pid_context = strdup(no_ctx);
483 sprintf(name + nameoff, "%d/fd/", pid);
485 if ((dir1 = opendir(name)) == NULL)
491 while ((d1 = readdir(dir1)) != NULL) {
492 const char *pattern = "socket:[";
499 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
502 sprintf(name+pos, "%d", fd);
504 link_len = readlink(name, lnk, sizeof(lnk)-1);
507 lnk[link_len] = '\0';
509 if (strncmp(lnk, pattern, strlen(pattern)))
512 sscanf(lnk, "socket:[%u]", &ino);
514 snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
515 root, pid, d1->d_name);
517 if (getfilecon(tmp, &sock_context) <= 0)
518 sock_context = strdup(no_ctx);
523 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
525 if ((fp = fopen(tmp, "r")) != NULL) {
526 fscanf(fp, "%*d (%[^)])", p);
530 user_ent_add(ino, p, pid, fd,
531 pid_context, sock_context);
546 #define ENTRY_BUF_SIZE 512
547 static int find_entry(unsigned ino, char **buf, int type)
552 char **new_buf = buf;
553 int len, new_buf_len;
560 p = user_ent_hash[user_ent_hashfn(ino)];
567 ptr = *buf + buf_used;
570 len = snprintf(ptr, buf_len - buf_used,
571 "(\"%s\",pid=%d,fd=%d),",
572 p->process, p->pid, p->fd);
575 len = snprintf(ptr, buf_len - buf_used,
576 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
578 p->process_ctx, p->fd);
581 len = snprintf(ptr, buf_len - buf_used,
582 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
584 p->process_ctx, p->fd,
588 fprintf(stderr, "ss: invalid type: %d\n", type);
592 if (len < 0 || len >= buf_len - buf_used) {
593 new_buf_len = buf_len + ENTRY_BUF_SIZE;
594 *new_buf = realloc(*buf, new_buf_len);
596 fprintf(stderr, "ss: failed to malloc buffer\n");
600 buf_len = new_buf_len;
612 ptr = *buf + buf_used;
618 /* Get stats from slab */
629 static struct slabstat slabstat;
631 static const char *slabstat_ids[] =
640 static int get_slabstat(struct slabstat *s)
645 static int slabstat_valid;
650 memset(s, 0, sizeof(*s));
652 fp = slabinfo_open();
656 cnt = sizeof(*s)/sizeof(int);
658 fgets(buf, sizeof(buf), fp);
659 while(fgets(buf, sizeof(buf), fp) != NULL) {
661 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
662 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
663 sscanf(buf, "%*s%d", ((int *)s) + i);
678 static inline void sock_addr_set_str(inet_prefix *prefix, char **ptr)
680 memcpy(prefix->data, ptr, sizeof(char *));
683 static inline char *sock_addr_get_str(const inet_prefix *prefix)
686 memcpy(&tmp, prefix->data, sizeof(char *));
690 static unsigned long long cookie_sk_get(const uint32_t *cookie)
692 return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
695 static const char *sstate_name[] = {
697 [SS_ESTABLISHED] = "ESTAB",
698 [SS_SYN_SENT] = "SYN-SENT",
699 [SS_SYN_RECV] = "SYN-RECV",
700 [SS_FIN_WAIT1] = "FIN-WAIT-1",
701 [SS_FIN_WAIT2] = "FIN-WAIT-2",
702 [SS_TIME_WAIT] = "TIME-WAIT",
703 [SS_CLOSE] = "UNCONN",
704 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
705 [SS_LAST_ACK] = "LAST-ACK",
706 [SS_LISTEN] = "LISTEN",
707 [SS_CLOSING] = "CLOSING",
710 static const char *sstate_namel[] = {
712 [SS_ESTABLISHED] = "established",
713 [SS_SYN_SENT] = "syn-sent",
714 [SS_SYN_RECV] = "syn-recv",
715 [SS_FIN_WAIT1] = "fin-wait-1",
716 [SS_FIN_WAIT2] = "fin-wait-2",
717 [SS_TIME_WAIT] = "time-wait",
718 [SS_CLOSE] = "unconnected",
719 [SS_CLOSE_WAIT] = "close-wait",
720 [SS_LAST_ACK] = "last-ack",
721 [SS_LISTEN] = "listening",
722 [SS_CLOSING] = "closing",
727 struct sockstat *next;
740 unsigned long long sk;
745 unsigned int ce_state;
759 double rto, ato, rtt, rttvar;
760 int qack, cwnd, ssthresh, backoff;
765 unsigned int lastsnd;
766 unsigned int lastrcv;
767 unsigned int lastack;
769 double pacing_rate_max;
770 unsigned int unacked;
771 unsigned int retrans;
772 unsigned int retrans_total;
775 unsigned int fackets;
776 unsigned int reordering;
782 bool has_ecnseen_opt;
783 bool has_fastopen_opt;
785 struct dctcpstat *dctcp;
788 static void sock_state_print(struct sockstat *s, const char *sock_name)
791 printf("%-*s ", netid_width, sock_name);
793 printf("%-*s ", state_width, sstate_name[s->state]);
795 printf("%-6d %-6d ", s->rq, s->wq);
798 static void sock_details_print(struct sockstat *s)
801 printf(" uid:%u", s->uid);
803 printf(" ino:%u", s->ino);
804 printf(" sk:%llx", s->sk);
807 static void sock_addr_print_width(int addr_len, const char *addr, char *delim,
808 int port_len, const char *port, const char *ifname)
811 printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim,
815 printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port);
819 static void sock_addr_print(const char *addr, char *delim, const char *port,
822 sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname);
825 static const char *tmr_name[] = {
834 static const char *print_ms_timer(int timeout)
837 int secs, msecs, minutes;
843 msecs = timeout%1000;
847 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
854 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
857 sprintf(buf+strlen(buf), "%03dms", msecs);
869 struct scache *rlist;
871 static void init_service_resolver(void)
874 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
876 fgets(buf, sizeof(buf), fp);
877 while (fgets(buf, sizeof(buf), fp) != NULL) {
878 unsigned int progn, port;
879 char proto[128], prog[128];
880 if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
881 &port, prog+4) == 4) {
882 struct scache *c = malloc(sizeof(*c));
885 memcpy(prog, "rpc.", 4);
886 c->name = strdup(prog);
887 if (strcmp(proto, TCP_PROTO) == 0)
888 c->proto = TCP_PROTO;
889 else if (strcmp(proto, UDP_PROTO) == 0)
890 c->proto = UDP_PROTO;
902 static int ip_local_port_min, ip_local_port_max;
904 /* Even do not try default linux ephemeral port ranges:
905 * default /etc/services contains so much of useless crap
906 * wouldbe "allocated" to this area that resolution
907 * is really harmful. I shrug each time when seeing
908 * "socks" or "cfinger" in dumps.
910 static int is_ephemeral(int port)
912 if (!ip_local_port_min) {
913 FILE *f = ephemeral_ports_open();
916 &ip_local_port_min, &ip_local_port_max);
919 ip_local_port_min = 1024;
920 ip_local_port_max = 4999;
924 return (port >= ip_local_port_min && port<= ip_local_port_max);
928 static const char *__resolve_service(int port)
932 for (c = rlist; c; c = c->next) {
933 if (c->port == port && c->proto == dg_proto)
937 if (!is_ephemeral(port)) {
944 se = getservbyport(htons(port), dg_proto);
953 static const char *resolve_service(int port)
955 static char buf[128];
956 static struct scache cache[256];
964 if (resolve_services) {
965 if (dg_proto == RAW_PROTO) {
966 return inet_proto_n2a(port, buf, sizeof(buf));
970 int hash = (port^(((unsigned long)dg_proto)>>2))&255;
972 for (c = &cache[hash]; c; c = c->next) {
973 if (c->port == port &&
974 c->proto == dg_proto) {
981 if ((res = __resolve_service(port)) != NULL) {
982 if ((c = malloc(sizeof(*c))) == NULL)
993 c->name = strdup(res);
994 c->next = cache[hash].next;
995 cache[hash].next = c;
1003 sprintf(buf, "%u", port);
1007 static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex)
1010 const char *ap = buf;
1011 int est_len = addr_width;
1012 const char *ifname = NULL;
1014 if (a->family == AF_INET) {
1015 if (a->data[0] == 0) {
1019 ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
1022 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
1023 est_len = strlen(ap);
1024 if (est_len <= addr_width)
1025 est_len = addr_width;
1027 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
1031 ifname = ll_index_to_name(ifindex);
1032 est_len -= strlen(ifname) + 1; /* +1 for percent char */
1035 sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port),
1043 struct aafilter *next;
1046 static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
1049 if (!inet_addr_match(a, p, plen))
1052 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1053 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1055 if (p->family == AF_INET && a->family == AF_INET6) {
1056 if (a->data[0] == 0 && a->data[1] == 0 &&
1057 a->data[2] == htonl(0xffff)) {
1058 inet_prefix tmp = *a;
1059 tmp.data[0] = a->data[3];
1060 return inet_addr_match(&tmp, p, plen);
1066 static int unix_match(const inet_prefix *a, const inet_prefix *p)
1068 char *addr = sock_addr_get_str(a);
1069 char *pattern = sock_addr_get_str(p);
1071 if (pattern == NULL)
1075 return !fnmatch(pattern, addr, 0);
1078 static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
1083 static int low, high=65535;
1085 if (s->local.family == AF_UNIX) {
1086 char *p = sock_addr_get_str(&s->local);
1087 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
1088 strspn(p+1, "0123456789abcdef") == 5);
1090 if (s->local.family == AF_PACKET)
1091 return s->lport == 0 && s->local.data == 0;
1092 if (s->local.family == AF_NETLINK)
1093 return s->lport < 0;
1096 FILE *fp = ephemeral_ports_open();
1098 fscanf(fp, "%d%d", &low, &high);
1102 return s->lport >= low && s->lport <= high;
1106 struct aafilter *a = (void*)f->pred;
1107 if (a->addr.family == AF_UNIX)
1108 return unix_match(&s->remote, &a->addr);
1109 if (a->port != -1 && a->port != s->rport)
1111 if (a->addr.bitlen) {
1113 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
1115 } while ((a = a->next) != NULL);
1122 struct aafilter *a = (void*)f->pred;
1123 if (a->addr.family == AF_UNIX)
1124 return unix_match(&s->local, &a->addr);
1125 if (a->port != -1 && a->port != s->lport)
1127 if (a->addr.bitlen) {
1129 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
1131 } while ((a = a->next) != NULL);
1138 struct aafilter *a = (void*)f->pred;
1139 return s->rport >= a->port;
1143 struct aafilter *a = (void*)f->pred;
1144 return s->rport <= a->port;
1148 struct aafilter *a = (void*)f->pred;
1149 return s->lport >= a->port;
1153 struct aafilter *a = (void*)f->pred;
1154 return s->lport <= a->port;
1157 /* Yup. It is recursion. Sorry. */
1159 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
1161 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
1163 return !run_ssfilter(f->pred, s);
1169 /* Relocate external jumps by reloc. */
1170 static void ssfilter_patch(char *a, int len, int reloc)
1173 struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a;
1174 if (op->no == len+4)
1183 static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
1188 if (!(*bytecode=malloc(4))) abort();
1189 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
1195 struct aafilter *a = (void*)f->pred;
1198 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
1201 for (b=a; b; b=b->next) {
1202 len += 4 + sizeof(struct inet_diag_hostcond);
1203 if (a->addr.family == AF_INET6)
1210 if (!(ptr = malloc(len))) abort();
1212 for (b=a; b; b=b->next) {
1213 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
1214 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
1215 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
1216 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4);
1218 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
1219 cond->family = a->addr.family;
1220 cond->port = a->port;
1221 cond->prefix_len = a->addr.bitlen;
1222 memcpy(cond->addr, a->addr.data, alen);
1225 op = (struct inet_diag_bc_op *)ptr;
1226 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
1230 return ptr - *bytecode;
1234 struct aafilter *x = (void*)f->pred;
1235 if (!(*bytecode=malloc(8))) abort();
1236 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
1237 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1242 struct aafilter *x = (void*)f->pred;
1243 if (!(*bytecode=malloc(8))) abort();
1244 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
1245 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1250 struct aafilter *x = (void*)f->pred;
1251 if (!(*bytecode=malloc(8))) abort();
1252 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
1253 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1258 struct aafilter *x = (void*)f->pred;
1259 if (!(*bytecode=malloc(8))) abort();
1260 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
1261 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1269 l1 = ssfilter_bytecompile(f->pred, &a1);
1270 l2 = ssfilter_bytecompile(f->post, &a2);
1271 if (!(a = malloc(l1+l2))) abort();
1273 memcpy(a+l1, a2, l2);
1275 ssfilter_patch(a, l1, l2);
1283 l1 = ssfilter_bytecompile(f->pred, &a1);
1284 l2 = ssfilter_bytecompile(f->post, &a2);
1285 if (!(a = malloc(l1+l2+4))) abort();
1287 memcpy(a+l1+4, a2, l2);
1289 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
1297 l1 = ssfilter_bytecompile(f->pred, &a1);
1298 if (!(a = malloc(l1+4))) abort();
1301 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
1310 static int remember_he(struct aafilter *a, struct hostent *he)
1312 char **ptr = he->h_addr_list;
1316 if (he->h_addrtype == AF_INET)
1318 else if (he->h_addrtype == AF_INET6)
1324 struct aafilter *b = a;
1325 if (a->addr.bitlen) {
1326 if ((b = malloc(sizeof(*b))) == NULL)
1332 memcpy(b->addr.data, *ptr, len);
1333 b->addr.bytelen = len;
1334 b->addr.bitlen = len*8;
1335 b->addr.family = he->h_addrtype;
1342 static int get_dns_host(struct aafilter *a, const char *addr, int fam)
1344 static int notfirst;
1353 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
1355 cnt = remember_he(a, he);
1356 if (fam == AF_UNSPEC) {
1357 he = gethostbyname2(addr, AF_INET6);
1359 cnt += remember_he(a, he);
1364 static int xll_initted = 0;
1366 static void xll_init(void)
1368 struct rtnl_handle rth;
1369 if (rtnl_open(&rth, 0) < 0)
1377 static const char *xll_index_to_name(int index)
1381 return ll_index_to_name(index);
1384 static int xll_name_to_index(const char *dev)
1388 return ll_name_to_index(dev);
1391 void *parse_hostcond(char *addr, bool is_port)
1394 struct aafilter a = { .port = -1 };
1395 struct aafilter *res;
1396 int fam = preferred_family;
1397 struct filter *f = ¤t_filter;
1399 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
1401 a.addr.family = AF_UNIX;
1402 if (strncmp(addr, "unix:", 5) == 0)
1405 a.addr.bitlen = 8*strlen(p);
1406 sock_addr_set_str(&a.addr, &p);
1411 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
1412 a.addr.family = AF_PACKET;
1414 if (strncmp(addr, "link:", 5) == 0)
1416 port = strchr(addr, ':');
1419 if (port[1] && strcmp(port+1, "*")) {
1420 if (get_integer(&a.port, port+1, 0)) {
1421 if ((a.port = xll_name_to_index(port+1)) <= 0)
1426 if (addr[0] && strcmp(addr, "*")) {
1429 if (ll_proto_a2n(&tmp, addr))
1431 a.addr.data[0] = ntohs(tmp);
1437 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
1438 a.addr.family = AF_NETLINK;
1440 if (strncmp(addr, "netlink:", 8) == 0)
1442 port = strchr(addr, ':');
1445 if (port[1] && strcmp(port+1, "*")) {
1446 if (get_integer(&a.port, port+1, 0)) {
1447 if (strcmp(port+1, "kernel") == 0)
1454 if (addr[0] && strcmp(addr, "*")) {
1456 if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
1463 if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
1465 if (!strncmp(addr, "inet:", 5))
1467 } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
1469 if (!strncmp(addr, "inet6:", 6))
1473 /* URL-like literal [] */
1474 if (addr[0] == '[') {
1476 if ((port = strchr(addr, ']')) == NULL)
1479 } else if (addr[0] == '*') {
1482 port = strrchr(strchr(addr, '/') ? : addr, ':');
1488 if (port && *port) {
1492 if (*port && *port != '*') {
1493 if (get_integer(&a.port, port, 0)) {
1494 struct servent *se1 = NULL;
1495 struct servent *se2 = NULL;
1496 if (current_filter.dbs&(1<<UDP_DB))
1497 se1 = getservbyname(port, UDP_PROTO);
1498 if (current_filter.dbs&(1<<TCP_DB))
1499 se2 = getservbyname(port, TCP_PROTO);
1500 if (se1 && se2 && se1->s_port != se2->s_port) {
1501 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1507 a.port = ntohs(se1->s_port);
1510 for (s = rlist; s; s = s->next) {
1511 if ((s->proto == UDP_PROTO &&
1512 (current_filter.dbs&(1<<UDP_DB))) ||
1513 (s->proto == TCP_PROTO &&
1514 (current_filter.dbs&(1<<TCP_DB)))) {
1515 if (s->name && strcmp(s->name, port) == 0) {
1516 if (a.port > 0 && a.port != s->port) {
1517 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1525 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1532 if (!is_port && addr && *addr && *addr != '*') {
1533 if (get_prefix_1(&a.addr, addr, fam)) {
1534 if (get_dns_host(&a, addr, fam)) {
1535 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1542 if (fam != AF_UNSPEC) {
1544 filter_af_set(f, fam);
1545 filter_states_set(f, 0);
1548 res = malloc(sizeof(*res));
1550 memcpy(res, &a, sizeof(a));
1554 static char *proto_name(int protocol)
1568 static void inet_stats_print(struct sockstat *s, int protocol)
1572 sock_state_print(s, proto_name(protocol));
1574 inet_addr_print(&s->local, s->lport, s->iface);
1575 inet_addr_print(&s->remote, s->rport, 0);
1577 if (show_proc_ctx || show_sock_ctx) {
1578 if (find_entry(s->ino, &buf,
1579 (show_proc_ctx & show_sock_ctx) ?
1580 PROC_SOCK_CTX : PROC_CTX) > 0) {
1581 printf(" users:(%s)", buf);
1584 } else if (show_users) {
1585 if (find_entry(s->ino, &buf, USERS) > 0) {
1586 printf(" users:(%s)", buf);
1592 static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
1595 s->local.family = s->remote.family = family;
1596 if (family == AF_INET) {
1597 sscanf(loc, "%x:%x", s->local.data, (unsigned*)&s->lport);
1598 sscanf(rem, "%x:%x", s->remote.data, (unsigned*)&s->rport);
1599 s->local.bytelen = s->remote.bytelen = 4;
1602 sscanf(loc, "%08x%08x%08x%08x:%x",
1608 sscanf(rem, "%08x%08x%08x%08x:%x",
1614 s->local.bytelen = s->remote.bytelen = 16;
1620 static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
1624 if ((p = strchr(line, ':')) == NULL)
1628 if ((p = strchr(*loc, ':')) == NULL)
1633 if ((p = strchr(*rem, ':')) == NULL)
1641 static char *sprint_bw(char *buf, double bw)
1644 sprintf(buf,"%.1fM", bw / 1000000.);
1645 else if (bw > 1000.)
1646 sprintf(buf,"%.1fK", bw / 1000.);
1648 sprintf(buf, "%g", bw);
1653 static void tcp_stats_print(struct tcpstat *s)
1659 if (s->has_sack_opt)
1663 if (s->has_ecnseen_opt)
1665 if (s->has_fastopen_opt)
1666 printf(" fastopen");
1668 printf(" %s", s->cong_alg);
1669 if (s->has_wscale_opt)
1670 printf(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale);
1672 printf(" rto:%g", s->rto);
1674 printf(" backoff:%u", s->backoff);
1676 printf(" rtt:%g/%g", s->rtt, s->rttvar);
1678 printf(" ato:%g", s->ato);
1681 printf(" qack:%d", s->qack);
1686 printf(" mss:%d", s->mss);
1688 printf(" cwnd:%d", s->cwnd);
1690 printf(" ssthresh:%d", s->ssthresh);
1692 if (s->dctcp && s->dctcp->enabled) {
1693 struct dctcpstat *dctcp = s->dctcp;
1695 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
1696 dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
1698 } else if (s->dctcp) {
1699 printf(" dctcp:fallback_mode");
1703 printf(" send %sbps", sprint_bw(b1, s->send_bps));
1705 printf(" lastsnd:%u", s->lastsnd);
1707 printf(" lastrcv:%u", s->lastrcv);
1709 printf(" lastack:%u", s->lastack);
1711 if (s->pacing_rate) {
1712 printf(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate));
1713 if (s->pacing_rate_max)
1714 printf("/%sbps", sprint_bw(b1,
1715 s->pacing_rate_max));
1719 printf(" unacked:%u", s->unacked);
1720 if (s->retrans || s->retrans_total)
1721 printf(" retrans:%u/%u", s->retrans, s->retrans_total);
1723 printf(" lost:%u", s->lost);
1724 if (s->sacked && s->ss.state != SS_LISTEN)
1725 printf(" sacked:%u", s->sacked);
1727 printf(" fackets:%u", s->fackets);
1728 if (s->reordering != 3)
1729 printf(" reordering:%d", s->reordering);
1731 printf(" rcv_rtt:%g", s->rcv_rtt);
1733 printf(" rcv_space:%d", s->rcv_space);
1736 static void tcp_timer_print(struct tcpstat *s)
1741 printf(" timer:(%s,%s,%d)",
1743 print_ms_timer(s->timeout),
1748 static int tcp_show_line(char *line, const struct filter *f, int family)
1750 int rto = 0, ato = 0;
1751 struct tcpstat s = {};
1752 char *loc, *rem, *data;
1755 int hz = get_user_hz();
1757 if (proc_inet_split_line(line, &loc, &rem, &data))
1760 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1761 if (!(f->states & (1 << state)))
1764 proc_parse_inet_addr(loc, rem, family, &s.ss);
1766 if (f->f && run_ssfilter(f->f, &s.ss) == 0)
1770 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
1771 &s.ss.state, &s.ss.wq, &s.ss.rq,
1772 &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes,
1773 &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd,
1786 s.retrans = s.timer != 1 ? s.probes : s.retrans;
1787 s.timeout = (s.timeout * 1000 + hz - 1) / hz;
1788 s.ato = (double)ato / hz;
1790 s.rto = (double)rto;
1791 s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh;
1792 s.rto = s.rto != 3 * hz ? s.rto / hz : 0;
1794 inet_stats_print(&s.ss, IPPROTO_TCP);
1797 tcp_timer_print(&s);
1800 sock_details_print(&s.ss);
1802 printf(" opt:\"%s\"", opt);
1806 tcp_stats_print(&s);
1812 static int generic_record_read(FILE *fp,
1813 int (*worker)(char*, const struct filter *, int),
1814 const struct filter *f, int fam)
1819 if (fgets(line, sizeof(line), fp) == NULL)
1822 while (fgets(line, sizeof(line), fp) != NULL) {
1823 int n = strlen(line);
1824 if (n == 0 || line[n-1] != '\n') {
1830 if (worker(line, f, fam) < 0)
1835 return ferror(fp) ? -1 : 0;
1838 static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1840 const __u32 *skmeminfo;
1842 if (!tb[attrtype]) {
1843 if (attrtype == INET_DIAG_SKMEMINFO) {
1844 if (!tb[INET_DIAG_MEMINFO])
1847 const struct inet_diag_meminfo *minfo =
1848 RTA_DATA(tb[INET_DIAG_MEMINFO]);
1850 printf(" mem:(r%u,w%u,f%u,t%u)",
1859 skmeminfo = RTA_DATA(tb[attrtype]);
1861 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
1862 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
1863 skmeminfo[SK_MEMINFO_RCVBUF],
1864 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
1865 skmeminfo[SK_MEMINFO_SNDBUF],
1866 skmeminfo[SK_MEMINFO_FWD_ALLOC],
1867 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
1868 skmeminfo[SK_MEMINFO_OPTMEM]);
1870 if (RTA_PAYLOAD(tb[attrtype]) >=
1871 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
1872 printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
1877 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
1879 static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
1880 struct rtattr *tb[])
1883 struct tcpstat s = {};
1885 s.ss.state = r->idiag_state;
1887 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
1889 if (tb[INET_DIAG_INFO]) {
1890 struct tcp_info *info;
1891 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
1893 /* workaround for older kernels with less fields */
1894 if (len < sizeof(*info)) {
1895 info = alloca(sizeof(*info));
1896 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
1897 memset((char *)info + len, 0, sizeof(*info) - len);
1899 info = RTA_DATA(tb[INET_DIAG_INFO]);
1902 s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS);
1903 s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK);
1904 s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN);
1905 s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN);
1906 s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA);
1909 if (tb[INET_DIAG_CONG]) {
1910 const char *cong_attr = rta_getattr_str(tb[INET_DIAG_CONG]);
1911 s.cong_alg = malloc(strlen(cong_attr + 1));
1912 strcpy(s.cong_alg, cong_attr);
1915 if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) {
1916 s.has_wscale_opt = true;
1917 s.snd_wscale = info->tcpi_snd_wscale;
1918 s.rcv_wscale = info->tcpi_rcv_wscale;
1921 if (info->tcpi_rto && info->tcpi_rto != 3000000)
1922 s.rto = (double)info->tcpi_rto / 1000;
1924 s.backoff = info->tcpi_backoff;
1925 s.rtt = (double)info->tcpi_rtt / 1000;
1926 s.rttvar = (double)info->tcpi_rttvar / 1000;
1927 s.ato = (double)info->tcpi_ato / 1000;
1928 s.mss = info->tcpi_snd_mss;
1929 s.rcv_space = info->tcpi_rcv_space;
1930 s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000;
1931 s.lastsnd = info->tcpi_last_data_sent;
1932 s.lastrcv = info->tcpi_last_data_recv;
1933 s.lastack = info->tcpi_last_ack_recv;
1934 s.unacked = info->tcpi_unacked;
1935 s.retrans = info->tcpi_retrans;
1936 s.retrans_total = info->tcpi_total_retrans;
1937 s.lost = info->tcpi_lost;
1938 s.sacked = info->tcpi_sacked;
1939 s.reordering = info->tcpi_reordering;
1940 s.rcv_space = info->tcpi_rcv_space;
1941 s.cwnd = info->tcpi_snd_cwnd;
1943 if (info->tcpi_snd_ssthresh < 0xFFFF)
1944 s.ssthresh = info->tcpi_snd_ssthresh;
1946 rtt = (double) info->tcpi_rtt;
1947 if (tb[INET_DIAG_VEGASINFO]) {
1948 const struct tcpvegas_info *vinfo
1949 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
1951 if (vinfo->tcpv_enabled &&
1952 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
1953 rtt = vinfo->tcpv_rtt;
1956 if (tb[INET_DIAG_DCTCPINFO]) {
1957 struct dctcpstat *dctcp = malloc(sizeof(struct
1960 const struct tcp_dctcp_info *dinfo
1961 = RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
1963 dctcp->enabled = !!dinfo->dctcp_enabled;
1964 dctcp->ce_state = dinfo->dctcp_ce_state;
1965 dctcp->alpha = dinfo->dctcp_alpha;
1966 dctcp->ab_ecn = dinfo->dctcp_ab_ecn;
1967 dctcp->ab_tot = dinfo->dctcp_ab_tot;
1971 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
1972 s.send_bps = (double) info->tcpi_snd_cwnd *
1973 (double)info->tcpi_snd_mss * 8000000. / rtt;
1976 if (info->tcpi_pacing_rate &&
1977 info->tcpi_pacing_rate != ~0ULL) {
1978 s.pacing_rate = info->tcpi_pacing_rate * 8.;
1980 if (info->tcpi_max_pacing_rate &&
1981 info->tcpi_max_pacing_rate != ~0ULL)
1982 s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
1984 tcp_stats_print(&s);
1992 static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol)
1994 struct rtattr * tb[INET_DIAG_MAX+1];
1995 struct inet_diag_msg *r = NLMSG_DATA(nlh);
1996 struct sockstat s = {};
1998 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
1999 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2001 s.state = r->idiag_state;
2002 s.local.family = s.remote.family = r->idiag_family;
2003 s.lport = ntohs(r->id.idiag_sport);
2004 s.rport = ntohs(r->id.idiag_dport);
2005 s.wq = r->idiag_wqueue;
2006 s.rq = r->idiag_rqueue;
2007 s.ino = r->idiag_inode;
2008 s.uid = r->idiag_uid;
2009 s.iface = r->id.idiag_if;
2010 s.sk = cookie_sk_get(&r->id.idiag_cookie[0]);
2012 if (s.local.family == AF_INET) {
2013 s.local.bytelen = s.remote.bytelen = 4;
2015 s.local.bytelen = s.remote.bytelen = 16;
2018 memcpy(s.local.data, r->id.idiag_src, s.local.bytelen);
2019 memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen);
2021 if (f && f->f && run_ssfilter(f->f, &s) == 0)
2024 inet_stats_print(&s, protocol);
2027 struct tcpstat t = {};
2029 t.timer = r->idiag_timer;
2030 t.timeout = r->idiag_expires;
2031 t.retrans = r->idiag_retrans;
2032 tcp_timer_print(&t);
2036 sock_details_print(&s);
2037 if (tb[INET_DIAG_SHUTDOWN]) {
2039 mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]);
2040 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2044 if (show_mem || show_tcpinfo) {
2046 tcp_show_info(nlh, r, tb);
2053 static int tcpdiag_send(int fd, int protocol, struct filter *f)
2055 struct sockaddr_nl nladdr;
2057 struct nlmsghdr nlh;
2058 struct inet_diag_req r;
2064 struct iovec iov[3];
2066 if (protocol == IPPROTO_UDP)
2069 memset(&nladdr, 0, sizeof(nladdr));
2070 nladdr.nl_family = AF_NETLINK;
2072 req.nlh.nlmsg_len = sizeof(req);
2073 if (protocol == IPPROTO_TCP)
2074 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
2076 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
2077 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
2078 req.nlh.nlmsg_pid = 0;
2079 req.nlh.nlmsg_seq = MAGIC_SEQ;
2080 memset(&req.r, 0, sizeof(req.r));
2081 req.r.idiag_family = AF_INET;
2082 req.r.idiag_states = f->states;
2084 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
2085 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2089 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2090 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2091 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
2094 iov[0] = (struct iovec){
2096 .iov_len = sizeof(req)
2099 bclen = ssfilter_bytecompile(f->f, &bc);
2100 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2101 rta.rta_len = RTA_LENGTH(bclen);
2102 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2103 iov[2] = (struct iovec){ bc, bclen };
2104 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2107 msg = (struct msghdr) {
2108 .msg_name = (void*)&nladdr,
2109 .msg_namelen = sizeof(nladdr),
2111 .msg_iovlen = f->f ? 3 : 1,
2114 if (sendmsg(fd, &msg, 0) < 0) {
2122 static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
2124 struct sockaddr_nl nladdr;
2125 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
2130 struct iovec iov[3];
2132 if (family == PF_UNSPEC)
2133 return tcpdiag_send(fd, protocol, f);
2135 memset(&nladdr, 0, sizeof(nladdr));
2136 nladdr.nl_family = AF_NETLINK;
2138 memset(&req.r, 0, sizeof(req.r));
2139 req.r.sdiag_family = family;
2140 req.r.sdiag_protocol = protocol;
2141 req.r.idiag_states = f->states;
2143 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
2144 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2148 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2149 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2150 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
2153 iov[0] = (struct iovec){
2155 .iov_len = sizeof(req)
2158 bclen = ssfilter_bytecompile(f->f, &bc);
2159 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2160 rta.rta_len = RTA_LENGTH(bclen);
2161 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2162 iov[2] = (struct iovec){ bc, bclen };
2163 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2166 msg = (struct msghdr) {
2167 .msg_name = (void*)&nladdr,
2168 .msg_namelen = sizeof(nladdr),
2170 .msg_iovlen = f->f ? 3 : 1,
2173 if (sendmsg(fd, &msg, 0) < 0) {
2181 struct inet_diag_arg {
2186 static int show_one_inet_sock(const struct sockaddr_nl *addr,
2187 struct nlmsghdr *h, void *arg)
2190 struct inet_diag_arg *diag_arg = arg;
2191 struct inet_diag_msg *r = NLMSG_DATA(h);
2193 if (!(diag_arg->f->families & (1 << r->idiag_family)))
2195 if ((err = inet_show_sock(h, NULL, diag_arg->protocol)) < 0)
2201 static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
2204 struct rtnl_handle rth;
2205 int family = PF_INET;
2206 struct inet_diag_arg arg = { .f = f, .protocol = protocol };
2208 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
2210 rth.dump = MAGIC_SEQ;
2211 rth.dump_fp = dump_fp;
2214 if ((err = sockdiag_send(family, rth.fd, protocol, f)))
2217 if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) {
2218 if (family != PF_UNSPEC) {
2224 if (family == PF_INET) {
2234 static int tcp_show_netlink_file(struct filter *f)
2239 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
2240 perror("fopen($TCPDIAG_FILE)");
2246 struct nlmsghdr *h = (struct nlmsghdr*)buf;
2248 status = fread(buf, 1, sizeof(*h), fp);
2250 perror("Reading header from $TCPDIAG_FILE");
2253 if (status != sizeof(*h)) {
2254 perror("Unexpected EOF reading $TCPDIAG_FILE");
2258 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
2261 perror("Reading $TCPDIAG_FILE");
2264 if (status + sizeof(*h) < h->nlmsg_len) {
2265 perror("Unexpected EOF reading $TCPDIAG_FILE");
2269 /* The only legal exit point */
2270 if (h->nlmsg_type == NLMSG_DONE)
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");
2278 errno = -err->error;
2279 perror("TCPDIAG answered");
2284 err = inet_show_sock(h, f, IPPROTO_TCP);
2290 static int tcp_show(struct filter *f, int socktype)
2294 int bufsize = 64*1024;
2296 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2299 dg_proto = TCP_PROTO;
2301 if (getenv("TCPDIAG_FILE"))
2302 return tcp_show_netlink_file(f);
2304 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
2305 && inet_show_netlink(f, NULL, socktype) == 0)
2308 /* Sigh... We have to parse /proc/net/tcp... */
2311 /* Estimate amount of sockets and try to allocate
2312 * huge buffer to read all the table at one read.
2313 * Limit it by 16MB though. The assumption is: as soon as
2314 * kernel was able to hold information about N connections,
2315 * it is able to give us some memory for snapshot.
2318 get_slabstat(&slabstat);
2320 int guess = slabstat.socks+slabstat.tcp_syns;
2321 if (f->states&(1<<SS_TIME_WAIT))
2322 guess += slabstat.tcp_tws;
2323 if (guess > (16*1024*1024)/128)
2324 guess = (16*1024*1024)/128;
2326 if (guess > bufsize)
2329 while (bufsize >= 64*1024) {
2330 if ((buf = malloc(bufsize)) != NULL)
2339 if (f->families & (1<<AF_INET)) {
2340 if ((fp = net_tcp_open()) == NULL)
2343 setbuffer(fp, buf, bufsize);
2344 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
2349 if ((f->families & (1<<AF_INET6)) &&
2350 (fp = net_tcp6_open()) != NULL) {
2351 setbuffer(fp, buf, bufsize);
2352 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
2362 int saved_errno = errno;
2367 errno = saved_errno;
2373 static int dgram_show_line(char *line, const struct filter *f, int family)
2375 struct sockstat s = {};
2376 char *loc, *rem, *data;
2380 if (proc_inet_split_line(line, &loc, &rem, &data))
2383 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
2384 if (!(f->states & (1 << state)))
2387 proc_parse_inet_addr(loc, rem, family, &s);
2389 if (f->f && run_ssfilter(f->f, &s) == 0)
2393 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
2394 &s.state, &s.wq, &s.rq,
2396 &s.refcnt, &s.sk, opt);
2401 inet_stats_print(&s, IPPROTO_UDP);
2403 if (show_details && opt[0])
2404 printf(" opt:\"%s\"", opt);
2410 static int udp_show(struct filter *f)
2414 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2417 dg_proto = UDP_PROTO;
2419 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2420 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
2423 if (f->families&(1<<AF_INET)) {
2424 if ((fp = net_udp_open()) == NULL)
2426 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
2431 if ((f->families&(1<<AF_INET6)) &&
2432 (fp = net_udp6_open()) != NULL) {
2433 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
2441 int saved_errno = errno;
2444 errno = saved_errno;
2449 static int raw_show(struct filter *f)
2453 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2456 dg_proto = RAW_PROTO;
2458 if (f->families&(1<<AF_INET)) {
2459 if ((fp = net_raw_open()) == NULL)
2461 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
2466 if ((f->families&(1<<AF_INET6)) &&
2467 (fp = net_raw6_open()) != NULL) {
2468 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
2476 int saved_errno = errno;
2479 errno = saved_errno;
2484 int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2485 SS_ESTABLISHED, SS_CLOSING };
2487 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
2489 static void unix_list_free(struct sockstat *list)
2492 struct sockstat *s = list;
2493 char *name = sock_addr_get_str(&s->local);
2503 static const char *unix_netid_name(int type)
2511 case SOCK_SEQPACKET:
2522 static bool unix_type_skip(struct sockstat *s, struct filter *f)
2524 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2526 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2528 if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB)))
2533 static bool unix_use_proc(void)
2535 return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT");
2538 static void unix_stats_print(struct sockstat *list, struct filter *f)
2542 char *ctx_buf = NULL;
2543 bool use_proc = unix_use_proc();
2544 char port_name[30] = {};
2546 for (s = list; s; s = s->next) {
2547 if (!(f->states & (1 << s->state)))
2549 if (unix_type_skip(s, f))
2552 local = sock_addr_get_str(&s->local);
2555 if (s->rport && use_proc) {
2558 for (p = list; p; p = p->next) {
2559 if (s->rport == p->lport)
2566 peer = sock_addr_get_str(&p->local);
2567 peer = peer ? : "*";
2571 if (use_proc && f->f) {
2572 if (strcmp(peer, "*") == 0)
2573 memset(s->remote.data, 0, sizeof(char *));
2575 sock_addr_set_str(&s->remote, &peer);
2577 if (run_ssfilter(f->f, s) == 0)
2581 sock_state_print(s, unix_netid_name(s->type));
2583 sock_addr_print(local ?: "*", " ",
2584 int_to_str(s->lport, port_name), NULL);
2585 sock_addr_print(peer, " ", int_to_str(s->rport, port_name),
2588 if (show_proc_ctx || show_sock_ctx) {
2589 if (find_entry(s->ino, &ctx_buf,
2590 (show_proc_ctx & show_sock_ctx) ?
2591 PROC_SOCK_CTX : PROC_CTX) > 0) {
2592 printf(" users:(%s)", ctx_buf);
2595 } else if (show_users) {
2596 if (find_entry(s->ino, &ctx_buf, USERS) > 0) {
2597 printf(" users:(%s)", ctx_buf);
2605 static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
2608 struct filter *f = (struct filter *)arg;
2609 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2610 struct rtattr *tb[UNIX_DIAG_MAX+1];
2612 struct sockstat stat = {};
2614 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
2615 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2617 stat.type = r->udiag_type;
2618 stat.state = r->udiag_state;
2619 stat.ino = stat.lport = r->udiag_ino;
2620 stat.local.family = stat.remote.family = AF_UNIX;
2622 if (unix_type_skip(&stat, f))
2625 if (tb[UNIX_DIAG_RQLEN]) {
2626 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
2627 stat.rq = rql->udiag_rqueue;
2628 stat.wq = rql->udiag_wqueue;
2630 if (tb[UNIX_DIAG_NAME]) {
2631 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2633 name = malloc(len + 1);
2634 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2636 if (name[0] == '\0')
2638 sock_addr_set_str(&stat.local, &name);
2640 if (tb[UNIX_DIAG_PEER])
2641 stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
2643 if (f->f && run_ssfilter(f->f, &stat) == 0)
2646 unix_stats_print(&stat, f);
2650 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2653 if (tb[UNIX_DIAG_SHUTDOWN]) {
2655 mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]);
2656 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2659 if (show_mem || show_details)
2667 static int handle_netlink_request(struct filter *f, struct nlmsghdr *req,
2668 size_t size, rtnl_filter_t show_one_sock)
2671 struct rtnl_handle rth;
2673 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
2676 rth.dump = MAGIC_SEQ;
2678 if (rtnl_send(&rth, req, size) < 0)
2681 if (rtnl_dump_filter(&rth, show_one_sock, f))
2690 static int unix_show_netlink(struct filter *f)
2692 DIAG_REQUEST(req, struct unix_diag_req r);
2694 req.r.sdiag_family = AF_UNIX;
2695 req.r.udiag_states = f->states;
2696 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
2698 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
2700 return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
2703 static int unix_show(struct filter *f)
2710 struct sockstat *list = NULL;
2712 if (!filter_af_get(f, AF_UNIX))
2715 if (!unix_use_proc() && unix_show_netlink(f) == 0)
2718 if ((fp = net_unix_open()) == NULL)
2720 fgets(buf, sizeof(buf)-1, fp);
2722 if (memcmp(buf, "Peer", 4) == 0)
2726 while (fgets(buf, sizeof(buf)-1, fp)) {
2727 struct sockstat *u, **insp;
2730 if (!(u = malloc(sizeof(*u))))
2733 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
2734 &u->rport, &u->rq, &u->wq, &flags, &u->type,
2735 &u->state, &u->ino, name) < 8)
2739 u->local.family = u->remote.family = AF_UNIX;
2741 if (flags & (1 << 16)) {
2742 u->state = SS_LISTEN;
2744 u->state = unix_state_map[u->state-1];
2745 if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport)
2746 u->state = SS_ESTABLISHED;
2757 if (u->type < (*insp)->type ||
2758 (u->type == (*insp)->type &&
2759 u->ino < (*insp)->ino))
2761 insp = &(*insp)->next;
2767 char *tmp = strdup(name);
2768 sock_addr_set_str(&u->local, &tmp);
2770 if (++cnt > MAX_UNIX_REMEMBER) {
2771 unix_stats_print(list, f);
2772 unix_list_free(list);
2779 unix_stats_print(list, f);
2780 unix_list_free(list);
2788 static int packet_stats_print(struct sockstat *s, const struct filter *f)
2791 const char *addr, *port;
2795 s->local.family = AF_PACKET;
2796 s->remote.family = AF_PACKET;
2797 s->local.data[0] = s->prot;
2798 if (run_ssfilter(f->f, s) == 0)
2802 sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr");
2807 addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name));
2812 port = xll_index_to_name(s->iface);
2814 sock_addr_print(addr, ":", port, NULL);
2815 sock_addr_print("", "*", "", NULL);
2817 if (show_proc_ctx || show_sock_ctx) {
2818 if (find_entry(s->ino, &buf,
2819 (show_proc_ctx & show_sock_ctx) ?
2820 PROC_SOCK_CTX : PROC_CTX) > 0) {
2821 printf(" users:(%s)", buf);
2824 } else if (show_users) {
2825 if (find_entry(s->ino, &buf, USERS) > 0) {
2826 printf(" users:(%s)", buf);
2832 sock_details_print(s);
2837 static void packet_show_ring(struct packet_diag_ring *ring)
2839 printf("blk_size:%d", ring->pdr_block_size);
2840 printf(",blk_nr:%d", ring->pdr_block_nr);
2841 printf(",frm_size:%d", ring->pdr_frame_size);
2842 printf(",frm_nr:%d", ring->pdr_frame_nr);
2843 printf(",tmo:%d", ring->pdr_retire_tmo);
2844 printf(",features:0x%x", ring->pdr_features);
2847 static int packet_show_sock(const struct sockaddr_nl *addr,
2848 struct nlmsghdr *nlh, void *arg)
2850 const struct filter *f = arg;
2851 struct packet_diag_msg *r = NLMSG_DATA(nlh);
2852 struct packet_diag_info *pinfo = NULL;
2853 struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
2854 struct rtattr *tb[PACKET_DIAG_MAX+1];
2855 struct sockstat stat = {};
2856 uint32_t fanout = 0;
2857 bool has_fanout = false;
2859 parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1),
2860 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2862 /* use /proc/net/packet if all info are not available */
2863 if (!tb[PACKET_DIAG_MEMINFO])
2866 stat.type = r->pdiag_type;
2867 stat.prot = r->pdiag_num;
2868 stat.ino = r->pdiag_ino;
2869 stat.state = SS_CLOSE;
2870 stat.sk = cookie_sk_get(&r->pdiag_cookie[0]);
2872 if (tb[PACKET_DIAG_MEMINFO]) {
2873 __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
2874 stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
2877 if (tb[PACKET_DIAG_INFO]) {
2878 pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
2879 stat.lport = stat.iface = pinfo->pdi_index;
2882 if (tb[PACKET_DIAG_UID])
2883 stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]);
2885 if (tb[PACKET_DIAG_RX_RING])
2886 ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
2888 if (tb[PACKET_DIAG_TX_RING])
2889 ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
2891 if (tb[PACKET_DIAG_FANOUT]) {
2893 fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]);
2896 if (packet_stats_print(&stat, f))
2901 printf("\n\tver:%d", pinfo->pdi_version);
2902 printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
2904 if (pinfo->pdi_flags & PDI_RUNNING)
2906 if (pinfo->pdi_flags & PDI_AUXDATA)
2908 if (pinfo->pdi_flags & PDI_ORIGDEV)
2910 if (pinfo->pdi_flags & PDI_VNETHDR)
2912 if (pinfo->pdi_flags & PDI_LOSS)
2914 if (!pinfo->pdi_flags)
2919 printf("\n\tring_rx(");
2920 packet_show_ring(ring_rx);
2924 printf("\n\tring_tx(");
2925 packet_show_ring(ring_tx);
2929 uint16_t type = (fanout >> 16) & 0xffff;
2931 printf("\n\tfanout(");
2932 printf("id:%d,", fanout & 0xffff);
2948 printf("0x%x", type);
2954 if (show_bpf && tb[PACKET_DIAG_FILTER]) {
2955 struct sock_filter *fil =
2956 RTA_DATA(tb[PACKET_DIAG_FILTER]);
2957 int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) /
2958 sizeof(struct sock_filter);
2960 printf("\n\tbpf filter (%d): ", num);
2962 printf(" 0x%02x %u %u %u,",
2963 fil->code, fil->jt, fil->jf, fil->k);
2972 static int packet_show_netlink(struct filter *f)
2974 DIAG_REQUEST(req, struct packet_diag_req r);
2976 req.r.sdiag_family = AF_PACKET;
2977 req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
2978 PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
2980 return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
2983 static int packet_show_line(char *buf, const struct filter *f, int fam)
2985 unsigned long long sk;
2986 struct sockstat stat = {};
2987 int type, prot, iface, state, rq, uid, ino;
2989 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
2991 &type, &prot, &iface, &state,
2994 if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
2996 if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
3001 stat.lport = stat.iface = iface;
3006 stat.state = SS_CLOSE;
3008 if (packet_stats_print(&stat, f))
3015 static int packet_show(struct filter *f)
3019 if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
3022 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3023 packet_show_netlink(f) == 0)
3026 if ((fp = net_packet_open()) == NULL)
3028 if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
3034 static int netlink_show_one(struct filter *f,
3035 int prot, int pid, unsigned groups,
3036 int state, int dst_pid, unsigned dst_group,
3038 unsigned long long sk, unsigned long long cb)
3041 SPRINT_BUF(prot_buf) = {};
3042 const char *prot_name;
3043 char procname[64] = {};
3045 st.state = SS_CLOSE;
3050 st.local.family = AF_NETLINK;
3051 st.remote.family = AF_NETLINK;
3054 st.local.data[0] = prot;
3055 if (run_ssfilter(f->f, &st) == 0)
3059 sock_state_print(&st, "nl");
3061 if (resolve_services)
3062 prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf));
3064 prot_name = int_to_str(prot, prot_buf);
3068 } else if (resolve_services) {
3072 strncpy(procname, "kernel", 6);
3073 } else if (pid > 0) {
3075 sprintf(procname, "%s/%d/stat",
3076 getenv("PROC_ROOT") ? : "/proc", pid);
3077 if ((fp = fopen(procname, "r")) != NULL) {
3078 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
3079 sprintf(procname+strlen(procname), "/%d", pid);
3086 int_to_str(pid, procname);
3088 int_to_str(pid, procname);
3091 sock_addr_print(prot_name, ":", procname, NULL);
3093 if (state == NETLINK_CONNECTED) {
3094 char dst_group_buf[30];
3095 char dst_pid_buf[30];
3096 sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
3097 int_to_str(dst_pid, dst_pid_buf), NULL);
3099 sock_addr_print("", "*", "", NULL);
3102 char *pid_context = NULL;
3103 if (show_proc_ctx) {
3104 /* The pid value will either be:
3105 * 0 if destination kernel - show kernel initial context.
3106 * A valid process pid - use getpidcon.
3107 * A unique value allocated by the kernel or netlink user
3108 * to the process - show context as "not available".
3111 security_get_initial_context("kernel", &pid_context);
3113 getpidcon(pid, &pid_context);
3115 if (pid_context != NULL) {
3116 printf("proc_ctx=%-*s ", serv_width, pid_context);
3119 printf("proc_ctx=%-*s ", serv_width, "unavailable");
3124 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
3131 static int netlink_show_sock(const struct sockaddr_nl *addr,
3132 struct nlmsghdr *nlh, void *arg)
3134 struct filter *f = (struct filter *)arg;
3135 struct netlink_diag_msg *r = NLMSG_DATA(nlh);
3136 struct rtattr *tb[NETLINK_DIAG_MAX+1];
3138 unsigned long groups = 0;
3140 parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr*)(r+1),
3141 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3143 if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
3144 groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]);
3146 if (tb[NETLINK_DIAG_MEMINFO]) {
3147 const __u32 *skmeminfo;
3148 skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
3150 rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3151 wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC];
3154 if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups,
3155 r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group,
3162 print_skmeminfo(tb, NETLINK_DIAG_MEMINFO);
3169 static int netlink_show_netlink(struct filter *f)
3171 DIAG_REQUEST(req, struct netlink_diag_req r);
3173 req.r.sdiag_family = AF_NETLINK;
3174 req.r.sdiag_protocol = NDIAG_PROTO_ALL;
3175 req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO;
3177 return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock);
3180 static int netlink_show(struct filter *f)
3187 unsigned long long sk, cb;
3189 if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
3192 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
3193 netlink_show_netlink(f) == 0)
3196 if ((fp = net_netlink_open()) == NULL)
3198 fgets(buf, sizeof(buf)-1, fp);
3200 while (fgets(buf, sizeof(buf)-1, fp)) {
3201 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
3203 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
3205 netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
3216 static int get_snmp_int(char *proto, char *key, int *result)
3220 int protolen = strlen(proto);
3221 int keylen = strlen(key);
3225 if ((fp = net_snmp_open()) == NULL)
3228 while (fgets(buf, sizeof(buf), fp) != NULL) {
3231 if (memcmp(buf, proto, protolen))
3233 while ((p = strchr(p, ' ')) != NULL) {
3236 if (memcmp(p, key, keylen) == 0 &&
3237 (p[keylen] == ' ' || p[keylen] == '\n'))
3240 if (fgets(buf, sizeof(buf), fp) == NULL)
3242 if (memcmp(buf, proto, protolen))
3245 while ((p = strchr(p, ' ')) != NULL) {
3248 sscanf(p, "%d", result);
3261 /* Get stats from sockstat */
3282 static void get_sockstat_line(char *line, struct ssummary *s)
3284 char id[256], rem[256];
3286 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
3289 if (strcmp(id, "sockets:") == 0)
3290 sscanf(rem, "%*s%d", &s->socks);
3291 else if (strcmp(id, "UDP:") == 0)
3292 sscanf(rem, "%*s%d", &s->udp4);
3293 else if (strcmp(id, "UDP6:") == 0)
3294 sscanf(rem, "%*s%d", &s->udp6);
3295 else if (strcmp(id, "RAW:") == 0)
3296 sscanf(rem, "%*s%d", &s->raw4);
3297 else if (strcmp(id, "RAW6:") == 0)
3298 sscanf(rem, "%*s%d", &s->raw6);
3299 else if (strcmp(id, "TCP6:") == 0)
3300 sscanf(rem, "%*s%d", &s->tcp6_hashed);
3301 else if (strcmp(id, "FRAG:") == 0)
3302 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
3303 else if (strcmp(id, "FRAG6:") == 0)
3304 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
3305 else if (strcmp(id, "TCP:") == 0)
3306 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
3308 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
3311 static int get_sockstat(struct ssummary *s)
3316 memset(s, 0, sizeof(*s));
3318 if ((fp = net_sockstat_open()) == NULL)
3320 while(fgets(buf, sizeof(buf), fp) != NULL)
3321 get_sockstat_line(buf, s);
3324 if ((fp = net_sockstat6_open()) == NULL)
3326 while(fgets(buf, sizeof(buf), fp) != NULL)
3327 get_sockstat_line(buf, s);
3333 static int print_summary(void)
3338 if (get_sockstat(&s) < 0)
3339 perror("ss: get_sockstat");
3340 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
3341 perror("ss: get_snmpstat");
3343 get_slabstat(&slabstat);
3345 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
3347 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
3348 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
3350 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
3353 s.tcp_tws, slabstat.tcp_tws,
3358 printf("Transport Total IP IPv6\n");
3359 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
3360 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
3361 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
3362 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
3363 printf("INET %-9d %-9d %-9d\n",
3364 s.raw4+s.udp4+s.tcp4_hashed+
3365 s.raw6+s.udp6+s.tcp6_hashed,
3366 s.raw4+s.udp4+s.tcp4_hashed,
3367 s.raw6+s.udp6+s.tcp6_hashed);
3368 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
3375 static void _usage(FILE *dest)
3378 "Usage: ss [ OPTIONS ]\n"
3379 " ss [ OPTIONS ] [ FILTER ]\n"
3380 " -h, --help this message\n"
3381 " -V, --version output version information\n"
3382 " -n, --numeric don't resolve service names\n"
3383 " -r, --resolve resolve host names\n"
3384 " -a, --all display all sockets\n"
3385 " -l, --listening display listening sockets\n"
3386 " -o, --options show timer information\n"
3387 " -e, --extended show detailed socket information\n"
3388 " -m, --memory show socket memory usage\n"
3389 " -p, --processes show process using socket\n"
3390 " -i, --info show internal TCP information\n"
3391 " -s, --summary show socket usage summary\n"
3392 " -b, --bpf show bpf filter socket information\n"
3393 " -Z, --context display process SELinux security contexts\n"
3394 " -z, --contexts display process and socket SELinux security contexts\n"
3395 " -N, --net switch to the specified network namespace name\n"
3397 " -4, --ipv4 display only IP version 4 sockets\n"
3398 " -6, --ipv6 display only IP version 6 sockets\n"
3399 " -0, --packet display PACKET sockets\n"
3400 " -t, --tcp display only TCP sockets\n"
3401 " -u, --udp display only UDP sockets\n"
3402 " -d, --dccp display only DCCP sockets\n"
3403 " -w, --raw display only RAW sockets\n"
3404 " -x, --unix display only Unix domain sockets\n"
3405 " -f, --family=FAMILY display sockets of type FAMILY\n"
3407 " -A, --query=QUERY, --socket=QUERY\n"
3408 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
3410 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
3411 " -F, --filter=FILE read filter information from FILE\n"
3412 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
3413 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
3414 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n"
3415 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3416 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3417 " bucket := {syn-recv|time-wait}\n"
3418 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n"
3422 static void help(void) __attribute__((noreturn));
3423 static void help(void)
3429 static void usage(void) __attribute__((noreturn));
3430 static void usage(void)
3437 static int scan_state(const char *state)
3440 if (strcasecmp(state, "close") == 0 ||
3441 strcasecmp(state, "closed") == 0)
3442 return (1<<SS_CLOSE);
3443 if (strcasecmp(state, "syn-rcv") == 0)
3444 return (1<<SS_SYN_RECV);
3445 if (strcasecmp(state, "established") == 0)
3446 return (1<<SS_ESTABLISHED);
3447 if (strcasecmp(state, "all") == 0)
3449 if (strcasecmp(state, "connected") == 0)
3450 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
3451 if (strcasecmp(state, "synchronized") == 0)
3452 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
3453 if (strcasecmp(state, "bucket") == 0)
3454 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
3455 if (strcasecmp(state, "big") == 0)
3456 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
3457 for (i=0; i<SS_MAX; i++) {
3458 if (strcasecmp(state, sstate_namel[i]) == 0)
3462 fprintf(stderr, "ss: wrong state name: %s\n", state);
3466 static const struct option long_opts[] = {
3467 { "numeric", 0, 0, 'n' },
3468 { "resolve", 0, 0, 'r' },
3469 { "options", 0, 0, 'o' },
3470 { "extended", 0, 0, 'e' },
3471 { "memory", 0, 0, 'm' },
3472 { "info", 0, 0, 'i' },
3473 { "processes", 0, 0, 'p' },
3474 { "bpf", 0, 0, 'b' },
3475 { "dccp", 0, 0, 'd' },
3476 { "tcp", 0, 0, 't' },
3477 { "udp", 0, 0, 'u' },
3478 { "raw", 0, 0, 'w' },
3479 { "unix", 0, 0, 'x' },
3480 { "all", 0, 0, 'a' },
3481 { "listening", 0, 0, 'l' },
3482 { "ipv4", 0, 0, '4' },
3483 { "ipv6", 0, 0, '6' },
3484 { "packet", 0, 0, '0' },
3485 { "family", 1, 0, 'f' },
3486 { "socket", 1, 0, 'A' },
3487 { "query", 1, 0, 'A' },
3488 { "summary", 0, 0, 's' },
3489 { "diag", 1, 0, 'D' },
3490 { "filter", 1, 0, 'F' },
3491 { "version", 0, 0, 'V' },
3492 { "help", 0, 0, 'h' },
3493 { "context", 0, 0, 'Z' },
3494 { "contexts", 0, 0, 'z' },
3495 { "net", 1, 0, 'N' },
3500 int main(int argc, char *argv[])
3505 const char *dump_tcpdiag = NULL;
3506 FILE *filter_fp = NULL;
3508 int state_filter = 0;
3510 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbf:miA:D:F:vVzZN:",
3511 long_opts, NULL)) != EOF) {
3514 resolve_services = 0;
3534 user_ent_hash_build();
3541 filter_db_set(¤t_filter, DCCP_DB);
3544 filter_db_set(¤t_filter, TCP_DB);
3547 filter_db_set(¤t_filter, UDP_DB);
3550 filter_db_set(¤t_filter, RAW_DB);
3553 filter_af_set(¤t_filter, AF_UNIX);
3556 state_filter = SS_ALL;
3559 state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE);
3562 filter_af_set(¤t_filter, AF_INET);
3565 filter_af_set(¤t_filter, AF_INET6);
3568 filter_af_set(¤t_filter, AF_PACKET);
3571 if (strcmp(optarg, "inet") == 0)
3572 filter_af_set(¤t_filter, AF_INET);
3573 else if (strcmp(optarg, "inet6") == 0)
3574 filter_af_set(¤t_filter, AF_INET6);
3575 else if (strcmp(optarg, "link") == 0)
3576 filter_af_set(¤t_filter, AF_PACKET);
3577 else if (strcmp(optarg, "unix") == 0)
3578 filter_af_set(¤t_filter, AF_UNIX);
3579 else if (strcmp(optarg, "netlink") == 0)
3580 filter_af_set(¤t_filter, AF_NETLINK);
3581 else if (strcmp(optarg, "help") == 0)
3584 fprintf(stderr, "ss: \"%s\" is invalid family\n",
3593 current_filter.dbs = 0;
3599 if ((p1 = strchr(p, ',')) != NULL)
3601 if (strcmp(p, "all") == 0) {
3602 filter_default_dbs(¤t_filter);
3603 } else if (strcmp(p, "inet") == 0) {
3604 filter_db_set(¤t_filter, UDP_DB);
3605 filter_db_set(¤t_filter, DCCP_DB);
3606 filter_db_set(¤t_filter, TCP_DB);
3607 filter_db_set(¤t_filter, RAW_DB);
3608 } else if (strcmp(p, "udp") == 0) {
3609 filter_db_set(¤t_filter, UDP_DB);
3610 } else if (strcmp(p, "dccp") == 0) {
3611 filter_db_set(¤t_filter, DCCP_DB);
3612 } else if (strcmp(p, "tcp") == 0) {
3613 filter_db_set(¤t_filter, TCP_DB);
3614 } else if (strcmp(p, "raw") == 0) {
3615 filter_db_set(¤t_filter, RAW_DB);
3616 } else if (strcmp(p, "unix") == 0) {
3617 filter_db_set(¤t_filter, UNIX_ST_DB);
3618 filter_db_set(¤t_filter, UNIX_DG_DB);
3619 filter_db_set(¤t_filter, UNIX_SQ_DB);
3620 } else if (strcasecmp(p, "unix_stream") == 0 ||
3621 strcmp(p, "u_str") == 0) {
3622 filter_db_set(¤t_filter, UNIX_ST_DB);
3623 } else if (strcasecmp(p, "unix_dgram") == 0 ||
3624 strcmp(p, "u_dgr") == 0) {
3625 filter_db_set(¤t_filter, UNIX_DG_DB);
3626 } else if (strcasecmp(p, "unix_seqpacket") == 0 ||
3627 strcmp(p, "u_seq") == 0) {
3628 filter_db_set(¤t_filter, UNIX_SQ_DB);
3629 } else if (strcmp(p, "packet") == 0) {
3630 filter_db_set(¤t_filter, PACKET_R_DB);
3631 filter_db_set(¤t_filter, PACKET_DG_DB);
3632 } else if (strcmp(p, "packet_raw") == 0 ||
3633 strcmp(p, "p_raw") == 0) {
3634 filter_db_set(¤t_filter, PACKET_R_DB);
3635 } else if (strcmp(p, "packet_dgram") == 0 ||
3636 strcmp(p, "p_dgr") == 0) {
3637 filter_db_set(¤t_filter, PACKET_DG_DB);
3638 } else if (strcmp(p, "netlink") == 0) {
3639 filter_db_set(¤t_filter, NETLINK_DB);
3641 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
3652 dump_tcpdiag = optarg;
3656 fprintf(stderr, "More than one filter file\n");
3659 if (optarg[0] == '-')
3662 filter_fp = fopen(optarg, "r");
3664 perror("fopen filter file");
3670 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
3675 if (is_selinux_enabled() <= 0) {
3676 fprintf(stderr, "ss: SELinux is not enabled.\n");
3680 user_ent_hash_build();
3683 if (netns_switch(optarg))
3699 if (do_default && argc == 0)
3703 /* Now parse filter... */
3704 if (argc == 0 && filter_fp) {
3705 if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp))
3710 if (strcmp(*argv, "state") == 0) {
3714 state_filter |= scan_state(*argv);
3716 } else if (strcmp(*argv, "exclude") == 0 ||
3717 strcmp(*argv, "excl") == 0) {
3720 state_filter = SS_ALL;
3721 state_filter &= ~scan_state(*argv);
3730 state_filter = state_filter ? state_filter : SS_CONN;
3731 filter_default_dbs(¤t_filter);
3734 filter_states_set(¤t_filter, state_filter);
3735 filter_merge_defaults(¤t_filter);
3737 if (resolve_services && resolve_hosts &&
3738 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
3739 init_service_resolver();
3742 if (current_filter.dbs == 0) {
3743 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
3746 if (current_filter.families == 0) {
3747 fprintf(stderr, "ss: no families to show with such filter.\n");
3750 if (current_filter.states == 0) {
3751 fprintf(stderr, "ss: no socket states to show with such filter.\n");
3756 FILE *dump_fp = stdout;
3757 if (!(current_filter.dbs & (1<<TCP_DB))) {
3758 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
3761 if (dump_tcpdiag[0] != '-') {
3762 dump_fp = fopen(dump_tcpdiag, "w");
3763 if (!dump_tcpdiag) {
3764 perror("fopen dump file");
3768 inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP);
3773 if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp))
3777 if (current_filter.dbs&(current_filter.dbs-1))
3781 if (current_filter.states&(current_filter.states-1))
3785 if (isatty(STDOUT_FILENO)) {
3788 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
3790 screen_width = w.ws_col;
3794 addrp_width = screen_width;
3795 addrp_width -= netid_width+1;
3796 addrp_width -= state_width+1;
3799 if (addrp_width&1) {
3802 else if (state_width)
3809 serv_width = resolve_services ? 7 : 5;
3811 if (addrp_width < 15+serv_width+1)
3812 addrp_width = 15+serv_width+1;
3814 addr_width = addrp_width - serv_width - 1;
3817 printf("%-*s ", netid_width, "Netid");
3819 printf("%-*s ", state_width, "State");
3820 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
3822 /* Make enough space for the local/remote port field */
3826 printf("%*s:%-*s %*s:%-*s\n",
3827 addr_width, "Local Address", serv_width, "Port",
3828 addr_width, "Peer Address", serv_width, "Port");
3832 if (current_filter.dbs & (1<<NETLINK_DB))
3833 netlink_show(¤t_filter);
3834 if (current_filter.dbs & PACKET_DBM)
3835 packet_show(¤t_filter);
3836 if (current_filter.dbs & UNIX_DBM)
3837 unix_show(¤t_filter);
3838 if (current_filter.dbs & (1<<RAW_DB))
3839 raw_show(¤t_filter);
3840 if (current_filter.dbs & (1<<UDP_DB))
3841 udp_show(¤t_filter);
3842 if (current_filter.dbs & (1<<TCP_DB))
3843 tcp_show(¤t_filter, IPPROTO_TCP);
3844 if (current_filter.dbs & (1<<DCCP_DB))
3845 tcp_show(¤t_filter, IPPROTO_DCCP);
3847 if (show_users || show_proc_ctx || show_sock_ctx)