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
)
90 int resolve_services
= 1;
91 int preferred_family
= AF_UNSPEC
;
101 /* If show_users & show_proc_ctx only do user_ent_hash_build() once */
102 int user_ent_hash_build_init
;
112 static const char *TCP_PROTO
= "tcp";
113 static const char *UDP_PROTO
= "udp";
114 static const char *RAW_PROTO
= "raw";
115 static const char *dg_proto
;
131 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
132 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
133 #define ALL_DB ((1<<MAX_DB)-1)
134 #define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB))
152 #define SS_ALL ((1 << SS_MAX) - 1)
153 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
155 #include "ssfilter.h"
165 static const struct filter default_dbs
[MAX_DB
] = {
168 .families
= (1 << AF_INET
) | (1 << AF_INET6
),
172 .families
= (1 << AF_INET
) | (1 << AF_INET6
),
175 .states
= (1 << SS_ESTABLISHED
),
176 .families
= (1 << AF_INET
) | (1 << AF_INET6
),
179 .states
= (1 << SS_ESTABLISHED
),
180 .families
= (1 << AF_INET
) | (1 << AF_INET6
),
183 .states
= (1 << SS_CLOSE
),
184 .families
= (1 << AF_UNIX
),
188 .families
= (1 << AF_UNIX
),
192 .families
= (1 << AF_UNIX
),
195 .states
= (1 << SS_CLOSE
),
196 .families
= (1 << AF_PACKET
),
199 .states
= (1 << SS_CLOSE
),
200 .families
= (1 << AF_PACKET
),
203 .states
= (1 << SS_CLOSE
),
204 .families
= (1 << AF_NETLINK
),
208 static const struct filter default_afs
[AF_MAX
] = {
223 .states
= (1 << SS_CLOSE
),
226 .dbs
= (1 << NETLINK_DB
),
227 .states
= (1 << SS_CLOSE
),
231 static int do_default
= 1;
232 static struct filter current_filter
;
234 static void filter_db_set(struct filter
*f
, int db
)
236 f
->states
|= default_dbs
[db
].states
;
241 static void filter_af_set(struct filter
*f
, int af
)
243 f
->states
|= default_afs
[af
].states
;
244 f
->families
|= 1 << af
;
246 preferred_family
= af
;
249 static int filter_af_get(struct filter
*f
, int af
)
251 return f
->families
& (1 << af
);
254 static void filter_default_dbs(struct filter
*f
)
256 filter_db_set(f
, UDP_DB
);
257 filter_db_set(f
, DCCP_DB
);
258 filter_db_set(f
, TCP_DB
);
259 filter_db_set(f
, RAW_DB
);
260 filter_db_set(f
, UNIX_ST_DB
);
261 filter_db_set(f
, UNIX_DG_DB
);
262 filter_db_set(f
, UNIX_SQ_DB
);
263 filter_db_set(f
, PACKET_R_DB
);
264 filter_db_set(f
, PACKET_DG_DB
);
265 filter_db_set(f
, NETLINK_DB
);
268 static void filter_states_set(struct filter
*f
, int states
)
274 static void filter_merge_defaults(struct filter
*f
)
279 for (db
= 0; db
< MAX_DB
; db
++) {
280 if (!(f
->dbs
& (1 << db
)))
283 if (!(default_dbs
[db
].families
& f
->families
))
284 f
->families
|= default_dbs
[db
].families
;
286 for (af
= 0; af
< AF_MAX
; af
++) {
287 if (!(f
->families
& (1 << af
)))
290 if (!(default_afs
[af
].dbs
& f
->dbs
))
291 f
->dbs
|= default_afs
[af
].dbs
;
295 static FILE *generic_proc_open(const char *env
, const char *name
)
297 const char *p
= getenv(env
);
301 p
= getenv("PROC_ROOT") ? : "/proc";
302 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
306 return fopen(p
, "r");
309 static FILE *net_tcp_open(void)
311 return generic_proc_open("PROC_NET_TCP", "net/tcp");
314 static FILE *net_tcp6_open(void)
316 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
319 static FILE *net_udp_open(void)
321 return generic_proc_open("PROC_NET_UDP", "net/udp");
324 static FILE *net_udp6_open(void)
326 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
329 static FILE *net_raw_open(void)
331 return generic_proc_open("PROC_NET_RAW", "net/raw");
334 static FILE *net_raw6_open(void)
336 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
339 static FILE *net_unix_open(void)
341 return generic_proc_open("PROC_NET_UNIX", "net/unix");
344 static FILE *net_packet_open(void)
346 return generic_proc_open("PROC_NET_PACKET", "net/packet");
349 static FILE *net_netlink_open(void)
351 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
354 static FILE *slabinfo_open(void)
356 return generic_proc_open("PROC_SLABINFO", "slabinfo");
359 static FILE *net_sockstat_open(void)
361 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
364 static FILE *net_sockstat6_open(void)
366 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
369 static FILE *net_snmp_open(void)
371 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
374 static FILE *ephemeral_ports_open(void)
376 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
380 struct user_ent
*next
;
389 #define USER_ENT_HASH_SIZE 256
390 struct user_ent
*user_ent_hash
[USER_ENT_HASH_SIZE
];
392 static int user_ent_hashfn(unsigned int ino
)
394 int val
= (ino
>> 24) ^ (ino
>> 16) ^ (ino
>> 8) ^ ino
;
396 return val
& (USER_ENT_HASH_SIZE
- 1);
399 static void user_ent_add(unsigned int ino
, char *process
,
404 struct user_ent
*p
, **pp
;
406 p
= malloc(sizeof(struct user_ent
));
408 fprintf(stderr
, "ss: failed to malloc buffer\n");
415 p
->process
= strdup(process
);
416 p
->process_ctx
= strdup(proc_ctx
);
417 p
->socket_ctx
= strdup(sock_ctx
);
419 pp
= &user_ent_hash
[user_ent_hashfn(ino
)];
424 static void user_ent_destroy(void)
426 struct user_ent
*p
, *p_next
;
429 while (cnt
!= USER_ENT_HASH_SIZE
) {
430 p
= user_ent_hash
[cnt
];
433 free(p
->process_ctx
);
443 static void user_ent_hash_build(void)
445 const char *root
= getenv("PROC_ROOT") ? : "/proc/";
452 const char *no_ctx
= "unavailable";
454 /* If show_users & show_proc_ctx set only do this once */
455 if (user_ent_hash_build_init
!= 0)
458 user_ent_hash_build_init
= 1;
460 strncpy(name
, root
, sizeof(name
)-1);
461 name
[sizeof(name
)-1] = 0;
463 if (strlen(name
) == 0 || name
[strlen(name
)-1] != '/')
466 nameoff
= strlen(name
);
472 while ((d
= readdir(dir
)) != NULL
) {
480 if (sscanf(d
->d_name
, "%d%c", &pid
, &crap
) != 1)
483 if (getpidcon(pid
, &pid_context
) != 0)
484 pid_context
= strdup(no_ctx
);
486 snprintf(name
+ nameoff
, sizeof(name
) - nameoff
, "%d/fd/", pid
);
488 if ((dir1
= opendir(name
)) == NULL
) {
496 while ((d1
= readdir(dir1
)) != NULL
) {
497 const char *pattern
= "socket:[";
504 if (sscanf(d1
->d_name
, "%d%c", &fd
, &crap
) != 1)
507 snprintf(name
+pos
, sizeof(name
) - pos
, "%d", fd
);
509 link_len
= readlink(name
, lnk
, sizeof(lnk
)-1);
512 lnk
[link_len
] = '\0';
514 if (strncmp(lnk
, pattern
, strlen(pattern
)))
517 sscanf(lnk
, "socket:[%u]", &ino
);
519 snprintf(tmp
, sizeof(tmp
), "%s/%d/fd/%s",
520 root
, pid
, d1
->d_name
);
522 if (getfilecon(tmp
, &sock_context
) <= 0)
523 sock_context
= strdup(no_ctx
);
528 snprintf(tmp
, sizeof(tmp
), "%s/%d/stat",
530 if ((fp
= fopen(tmp
, "r")) != NULL
) {
531 if (fscanf(fp
, "%*d (%[^)])", p
) < 1)
536 user_ent_add(ino
, p
, pid
, fd
,
537 pid_context
, sock_context
);
552 #define ENTRY_BUF_SIZE 512
553 static int find_entry(unsigned int ino
, char **buf
, int type
)
559 int len
, new_buf_len
;
566 p
= user_ent_hash
[user_ent_hashfn(ino
)];
573 ptr
= *buf
+ buf_used
;
576 len
= snprintf(ptr
, buf_len
- buf_used
,
577 "(\"%s\",pid=%d,fd=%d),",
578 p
->process
, p
->pid
, p
->fd
);
581 len
= snprintf(ptr
, buf_len
- buf_used
,
582 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
584 p
->process_ctx
, p
->fd
);
587 len
= snprintf(ptr
, buf_len
- buf_used
,
588 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
590 p
->process_ctx
, p
->fd
,
594 fprintf(stderr
, "ss: invalid type: %d\n", type
);
598 if (len
< 0 || len
>= buf_len
- buf_used
) {
599 new_buf_len
= buf_len
+ ENTRY_BUF_SIZE
;
600 new_buf
= realloc(*buf
, new_buf_len
);
602 fprintf(stderr
, "ss: failed to malloc buffer\n");
606 buf_len
= new_buf_len
;
618 ptr
= *buf
+ buf_used
;
624 /* Get stats from slab */
634 static struct slabstat slabstat
;
636 static const char *slabstat_ids
[] = {
645 static int get_slabstat(struct slabstat
*s
)
650 static int slabstat_valid
;
655 memset(s
, 0, sizeof(*s
));
657 fp
= slabinfo_open();
661 cnt
= sizeof(*s
)/sizeof(int);
663 if (!fgets(buf
, sizeof(buf
), fp
)) {
667 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
670 for (i
= 0; i
< ARRAY_SIZE(slabstat_ids
); i
++) {
671 if (memcmp(buf
, slabstat_ids
[i
], strlen(slabstat_ids
[i
])) == 0) {
672 sscanf(buf
, "%*s%d", ((int *)s
) + i
);
687 static unsigned long long cookie_sk_get(const uint32_t *cookie
)
689 return (((unsigned long long)cookie
[1] << 31) << 1) | cookie
[0];
692 static const char *sstate_name
[] = {
694 [SS_ESTABLISHED
] = "ESTAB",
695 [SS_SYN_SENT
] = "SYN-SENT",
696 [SS_SYN_RECV
] = "SYN-RECV",
697 [SS_FIN_WAIT1
] = "FIN-WAIT-1",
698 [SS_FIN_WAIT2
] = "FIN-WAIT-2",
699 [SS_TIME_WAIT
] = "TIME-WAIT",
700 [SS_CLOSE
] = "UNCONN",
701 [SS_CLOSE_WAIT
] = "CLOSE-WAIT",
702 [SS_LAST_ACK
] = "LAST-ACK",
703 [SS_LISTEN
] = "LISTEN",
704 [SS_CLOSING
] = "CLOSING",
707 static const char *sstate_namel
[] = {
709 [SS_ESTABLISHED
] = "established",
710 [SS_SYN_SENT
] = "syn-sent",
711 [SS_SYN_RECV
] = "syn-recv",
712 [SS_FIN_WAIT1
] = "fin-wait-1",
713 [SS_FIN_WAIT2
] = "fin-wait-2",
714 [SS_TIME_WAIT
] = "time-wait",
715 [SS_CLOSE
] = "unconnected",
716 [SS_CLOSE_WAIT
] = "close-wait",
717 [SS_LAST_ACK
] = "last-ack",
718 [SS_LISTEN
] = "listening",
719 [SS_CLOSING
] = "closing",
723 struct sockstat
*next
;
736 unsigned long long sk
;
742 unsigned int ce_state
;
755 double rto
, ato
, rtt
, rttvar
;
756 int qack
, cwnd
, ssthresh
, backoff
;
761 unsigned int lastsnd
;
762 unsigned int lastrcv
;
763 unsigned int lastack
;
765 double pacing_rate_max
;
766 unsigned long long bytes_acked
;
767 unsigned long long bytes_received
;
768 unsigned int segs_out
;
769 unsigned int segs_in
;
770 unsigned int data_segs_out
;
771 unsigned int data_segs_in
;
772 unsigned int unacked
;
773 unsigned int retrans
;
774 unsigned int retrans_total
;
777 unsigned int fackets
;
778 unsigned int reordering
;
779 unsigned int not_sent
;
786 bool has_ecnseen_opt
;
787 bool has_fastopen_opt
;
789 struct dctcpstat
*dctcp
;
792 static void sock_state_print(struct sockstat
*s
, const char *sock_name
)
795 printf("%-*s ", netid_width
, sock_name
);
797 printf("%-*s ", state_width
, sstate_name
[s
->state
]);
799 printf("%-6d %-6d ", s
->rq
, s
->wq
);
802 static void sock_details_print(struct sockstat
*s
)
805 printf(" uid:%u", s
->uid
);
807 printf(" ino:%u", s
->ino
);
808 printf(" sk:%llx", s
->sk
);
811 static void sock_addr_print_width(int addr_len
, const char *addr
, char *delim
,
812 int port_len
, const char *port
, const char *ifname
)
815 printf("%*s%%%s%s%-*s ", addr_len
, addr
, ifname
, delim
,
818 printf("%*s%s%-*s ", addr_len
, addr
, delim
, port_len
, port
);
822 static void sock_addr_print(const char *addr
, char *delim
, const char *port
,
825 sock_addr_print_width(addr_width
, addr
, delim
, serv_width
, port
, ifname
);
828 static const char *tmr_name
[] = {
837 static const char *print_ms_timer(int timeout
)
840 int secs
, msecs
, minutes
;
847 msecs
= timeout
%1000;
851 snprintf(buf
, sizeof(buf
)-16, "%dmin", minutes
);
858 sprintf(buf
+strlen(buf
), "%d%s", secs
, msecs
? "." : "sec");
861 sprintf(buf
+strlen(buf
), "%03dms", msecs
);
872 struct scache
*rlist
;
874 static void init_service_resolver(void)
877 FILE *fp
= popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
882 if (!fgets(buf
, sizeof(buf
), fp
)) {
886 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
887 unsigned int progn
, port
;
888 char proto
[128], prog
[128] = "rpc.";
891 if (sscanf(buf
, "%u %*d %s %u %s",
892 &progn
, proto
, &port
, prog
+4) != 4)
895 if (!(c
= malloc(sizeof(*c
))))
899 c
->name
= strdup(prog
);
900 if (strcmp(proto
, TCP_PROTO
) == 0)
901 c
->proto
= TCP_PROTO
;
902 else if (strcmp(proto
, UDP_PROTO
) == 0)
903 c
->proto
= UDP_PROTO
;
912 /* Even do not try default linux ephemeral port ranges:
913 * default /etc/services contains so much of useless crap
914 * wouldbe "allocated" to this area that resolution
915 * is really harmful. I shrug each time when seeing
916 * "socks" or "cfinger" in dumps.
918 static int is_ephemeral(int port
)
920 static int min
= 0, max
;
923 FILE *f
= ephemeral_ports_open();
925 if (!f
|| fscanf(f
, "%d %d", &min
, &max
) < 2) {
932 return port
>= min
&& port
<= max
;
936 static const char *__resolve_service(int port
)
940 for (c
= rlist
; c
; c
= c
->next
) {
941 if (c
->port
== port
&& c
->proto
== dg_proto
)
945 if (!is_ephemeral(port
)) {
953 se
= getservbyport(htons(port
), dg_proto
);
961 #define SCACHE_BUCKETS 1024
962 static struct scache
*cache_htab
[SCACHE_BUCKETS
];
964 static const char *resolve_service(int port
)
966 static char buf
[128];
977 if (!resolve_services
)
980 if (dg_proto
== RAW_PROTO
)
981 return inet_proto_n2a(port
, buf
, sizeof(buf
));
984 hash
= (port
^(((unsigned long)dg_proto
)>>2)) % SCACHE_BUCKETS
;
986 for (c
= cache_htab
[hash
]; c
; c
= c
->next
) {
987 if (c
->port
== port
&& c
->proto
== dg_proto
)
991 c
= malloc(sizeof(*c
));
994 res
= __resolve_service(port
);
996 c
->name
= res
? strdup(res
) : NULL
;
998 c
->next
= cache_htab
[hash
];
999 cache_htab
[hash
] = c
;
1006 sprintf(buf
, "%u", port
);
1010 static void inet_addr_print(const inet_prefix
*a
, int port
, unsigned int ifindex
)
1013 const char *ap
= buf
;
1014 int est_len
= addr_width
;
1015 const char *ifname
= NULL
;
1017 if (a
->family
== AF_INET
) {
1018 if (a
->data
[0] == 0) {
1022 ap
= format_host(AF_INET
, 4, a
->data
);
1025 ap
= format_host(a
->family
, 16, a
->data
);
1026 est_len
= strlen(ap
);
1027 if (est_len
<= addr_width
)
1028 est_len
= addr_width
;
1030 est_len
= addr_width
+ ((est_len
-addr_width
+3)/4)*4;
1034 ifname
= ll_index_to_name(ifindex
);
1035 est_len
-= strlen(ifname
) + 1; /* +1 for percent char */
1040 sock_addr_print_width(est_len
, ap
, ":", serv_width
, resolve_service(port
),
1048 struct aafilter
*next
;
1051 static int inet2_addr_match(const inet_prefix
*a
, const inet_prefix
*p
,
1054 if (!inet_addr_match(a
, p
, plen
))
1057 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1058 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1060 if (p
->family
== AF_INET
&& a
->family
== AF_INET6
) {
1061 if (a
->data
[0] == 0 && a
->data
[1] == 0 &&
1062 a
->data
[2] == htonl(0xffff)) {
1063 inet_prefix tmp
= *a
;
1065 tmp
.data
[0] = a
->data
[3];
1066 return inet_addr_match(&tmp
, p
, plen
);
1072 static int unix_match(const inet_prefix
*a
, const inet_prefix
*p
)
1074 char *addr
, *pattern
;
1076 memcpy(&addr
, a
->data
, sizeof(addr
));
1077 memcpy(&pattern
, p
->data
, sizeof(pattern
));
1078 if (pattern
== NULL
)
1082 return !fnmatch(pattern
, addr
, 0);
1085 static int run_ssfilter(struct ssfilter
*f
, struct sockstat
*s
)
1090 if (s
->local
.family
== AF_UNIX
) {
1093 memcpy(&p
, s
->local
.data
, sizeof(p
));
1094 return p
== NULL
|| (p
[0] == '@' && strlen(p
) == 6 &&
1095 strspn(p
+1, "0123456789abcdef") == 5);
1097 if (s
->local
.family
== AF_PACKET
)
1098 return s
->lport
== 0 && s
->local
.data
[0] == 0;
1099 if (s
->local
.family
== AF_NETLINK
)
1100 return s
->lport
< 0;
1102 return is_ephemeral(s
->lport
);
1106 struct aafilter
*a
= (void *)f
->pred
;
1108 if (a
->addr
.family
== AF_UNIX
)
1109 return unix_match(&s
->remote
, &a
->addr
);
1110 if (a
->port
!= -1 && a
->port
!= s
->rport
)
1112 if (a
->addr
.bitlen
) {
1114 if (!inet2_addr_match(&s
->remote
, &a
->addr
, a
->addr
.bitlen
))
1116 } while ((a
= a
->next
) != NULL
);
1123 struct aafilter
*a
= (void *)f
->pred
;
1125 if (a
->addr
.family
== AF_UNIX
)
1126 return unix_match(&s
->local
, &a
->addr
);
1127 if (a
->port
!= -1 && a
->port
!= s
->lport
)
1129 if (a
->addr
.bitlen
) {
1131 if (!inet2_addr_match(&s
->local
, &a
->addr
, a
->addr
.bitlen
))
1133 } while ((a
= a
->next
) != NULL
);
1140 struct aafilter
*a
= (void *)f
->pred
;
1142 return s
->rport
>= a
->port
;
1146 struct aafilter
*a
= (void *)f
->pred
;
1148 return s
->rport
<= a
->port
;
1152 struct aafilter
*a
= (void *)f
->pred
;
1154 return s
->lport
>= a
->port
;
1158 struct aafilter
*a
= (void *)f
->pred
;
1160 return s
->lport
<= a
->port
;
1164 struct aafilter
*a
= (void *)f
->pred
;
1166 return s
->iface
== a
->iface
;
1168 /* Yup. It is recursion. Sorry. */
1170 return run_ssfilter(f
->pred
, s
) && run_ssfilter(f
->post
, s
);
1172 return run_ssfilter(f
->pred
, s
) || run_ssfilter(f
->post
, s
);
1174 return !run_ssfilter(f
->pred
, s
);
1180 /* Relocate external jumps by reloc. */
1181 static void ssfilter_patch(char *a
, int len
, int reloc
)
1184 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)a
;
1186 if (op
->no
== len
+4)
1195 static int ssfilter_bytecompile(struct ssfilter
*f
, char **bytecode
)
1200 if (!(*bytecode
= malloc(4))) abort();
1201 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_AUTO
, 4, 8 };
1207 struct aafilter
*a
= (void *)f
->pred
;
1210 int code
= (f
->type
== SSF_DCOND
? INET_DIAG_BC_D_COND
: INET_DIAG_BC_S_COND
);
1213 for (b
= a
; b
; b
= b
->next
) {
1214 len
+= 4 + sizeof(struct inet_diag_hostcond
);
1215 if (a
->addr
.family
== AF_INET6
)
1222 if (!(ptr
= malloc(len
))) abort();
1224 for (b
= a
; b
; b
= b
->next
) {
1225 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)ptr
;
1226 int alen
= (a
->addr
.family
== AF_INET6
? 16 : 4);
1227 int oplen
= alen
+ 4 + sizeof(struct inet_diag_hostcond
);
1228 struct inet_diag_hostcond
*cond
= (struct inet_diag_hostcond
*)(ptr
+4);
1230 *op
= (struct inet_diag_bc_op
){ code
, oplen
, oplen
+4 };
1231 cond
->family
= a
->addr
.family
;
1232 cond
->port
= a
->port
;
1233 cond
->prefix_len
= a
->addr
.bitlen
;
1234 memcpy(cond
->addr
, a
->addr
.data
, alen
);
1237 op
= (struct inet_diag_bc_op
*)ptr
;
1238 *op
= (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, len
- (ptr
-*bytecode
)};
1242 return ptr
- *bytecode
;
1246 struct aafilter
*x
= (void *)f
->pred
;
1248 if (!(*bytecode
= malloc(8))) abort();
1249 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_GE
, 8, 12 };
1250 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1255 struct aafilter
*x
= (void *)f
->pred
;
1257 if (!(*bytecode
= malloc(8))) abort();
1258 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_LE
, 8, 12 };
1259 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1264 struct aafilter
*x
= (void *)f
->pred
;
1266 if (!(*bytecode
= malloc(8))) abort();
1267 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_GE
, 8, 12 };
1268 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1273 struct aafilter
*x
= (void *)f
->pred
;
1275 if (!(*bytecode
= malloc(8))) abort();
1276 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_LE
, 8, 12 };
1277 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1283 char *a1
= NULL
, *a2
= NULL
, *a
;
1286 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1287 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1293 if (!(a
= malloc(l1
+l2
))) abort();
1295 memcpy(a
+l1
, a2
, l2
);
1297 ssfilter_patch(a
, l1
, l2
);
1303 char *a1
= NULL
, *a2
= NULL
, *a
;
1306 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1307 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1313 if (!(a
= malloc(l1
+l2
+4))) abort();
1315 memcpy(a
+l1
+4, a2
, l2
);
1317 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, l2
+4 };
1323 char *a1
= NULL
, *a
;
1326 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1331 if (!(a
= malloc(l1
+4))) abort();
1334 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, 8 };
1340 /* bytecompile for SSF_DEVCOND not supported yet */
1348 static int remember_he(struct aafilter
*a
, struct hostent
*he
)
1350 char **ptr
= he
->h_addr_list
;
1354 if (he
->h_addrtype
== AF_INET
)
1356 else if (he
->h_addrtype
== AF_INET6
)
1362 struct aafilter
*b
= a
;
1364 if (a
->addr
.bitlen
) {
1365 if ((b
= malloc(sizeof(*b
))) == NULL
)
1371 memcpy(b
->addr
.data
, *ptr
, len
);
1372 b
->addr
.bytelen
= len
;
1373 b
->addr
.bitlen
= len
*8;
1374 b
->addr
.family
= he
->h_addrtype
;
1381 static int get_dns_host(struct aafilter
*a
, const char *addr
, int fam
)
1383 static int notfirst
;
1392 he
= gethostbyname2(addr
, fam
== AF_UNSPEC
? AF_INET
: fam
);
1394 cnt
= remember_he(a
, he
);
1395 if (fam
== AF_UNSPEC
) {
1396 he
= gethostbyname2(addr
, AF_INET6
);
1398 cnt
+= remember_he(a
, he
);
1403 static int xll_initted
;
1405 static void xll_init(void)
1407 struct rtnl_handle rth
;
1409 if (rtnl_open(&rth
, 0) < 0)
1417 static const char *xll_index_to_name(int index
)
1421 return ll_index_to_name(index
);
1424 static int xll_name_to_index(const char *dev
)
1428 return ll_name_to_index(dev
);
1431 void *parse_devcond(char *name
)
1433 struct aafilter a
= { .iface
= 0 };
1434 struct aafilter
*res
;
1436 a
.iface
= xll_name_to_index(name
);
1441 n
= strtoul(name
, &end
, 0);
1442 if (!end
|| end
== name
|| *end
|| n
> UINT_MAX
)
1448 res
= malloc(sizeof(*res
));
1454 void *parse_hostcond(char *addr
, bool is_port
)
1457 struct aafilter a
= { .port
= -1 };
1458 struct aafilter
*res
;
1459 int fam
= preferred_family
;
1460 struct filter
*f
= ¤t_filter
;
1462 if (fam
== AF_UNIX
|| strncmp(addr
, "unix:", 5) == 0) {
1465 a
.addr
.family
= AF_UNIX
;
1466 if (strncmp(addr
, "unix:", 5) == 0)
1469 a
.addr
.bitlen
= 8*strlen(p
);
1470 memcpy(a
.addr
.data
, &p
, sizeof(p
));
1475 if (fam
== AF_PACKET
|| strncmp(addr
, "link:", 5) == 0) {
1476 a
.addr
.family
= AF_PACKET
;
1478 if (strncmp(addr
, "link:", 5) == 0)
1480 port
= strchr(addr
, ':');
1483 if (port
[1] && strcmp(port
+1, "*")) {
1484 if (get_integer(&a
.port
, port
+1, 0)) {
1485 if ((a
.port
= xll_name_to_index(port
+1)) <= 0)
1490 if (addr
[0] && strcmp(addr
, "*")) {
1494 if (ll_proto_a2n(&tmp
, addr
))
1496 a
.addr
.data
[0] = ntohs(tmp
);
1502 if (fam
== AF_NETLINK
|| strncmp(addr
, "netlink:", 8) == 0) {
1503 a
.addr
.family
= AF_NETLINK
;
1505 if (strncmp(addr
, "netlink:", 8) == 0)
1507 port
= strchr(addr
, ':');
1510 if (port
[1] && strcmp(port
+1, "*")) {
1511 if (get_integer(&a
.port
, port
+1, 0)) {
1512 if (strcmp(port
+1, "kernel") == 0)
1519 if (addr
[0] && strcmp(addr
, "*")) {
1521 if (nl_proto_a2n(&a
.addr
.data
[0], addr
) == -1)
1528 if (fam
== AF_INET
|| !strncmp(addr
, "inet:", 5)) {
1530 if (!strncmp(addr
, "inet:", 5))
1532 } else if (fam
== AF_INET6
|| !strncmp(addr
, "inet6:", 6)) {
1534 if (!strncmp(addr
, "inet6:", 6))
1538 /* URL-like literal [] */
1539 if (addr
[0] == '[') {
1541 if ((port
= strchr(addr
, ']')) == NULL
)
1544 } else if (addr
[0] == '*') {
1547 port
= strrchr(strchr(addr
, '/') ? : addr
, ':');
1553 if (port
&& *port
) {
1557 if (*port
&& *port
!= '*') {
1558 if (get_integer(&a
.port
, port
, 0)) {
1559 struct servent
*se1
= NULL
;
1560 struct servent
*se2
= NULL
;
1562 if (current_filter
.dbs
&(1<<UDP_DB
))
1563 se1
= getservbyname(port
, UDP_PROTO
);
1564 if (current_filter
.dbs
&(1<<TCP_DB
))
1565 se2
= getservbyname(port
, TCP_PROTO
);
1566 if (se1
&& se2
&& se1
->s_port
!= se2
->s_port
) {
1567 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
1573 a
.port
= ntohs(se1
->s_port
);
1577 for (s
= rlist
; s
; s
= s
->next
) {
1578 if ((s
->proto
== UDP_PROTO
&&
1579 (current_filter
.dbs
&(1<<UDP_DB
))) ||
1580 (s
->proto
== TCP_PROTO
&&
1581 (current_filter
.dbs
&(1<<TCP_DB
)))) {
1582 if (s
->name
&& strcmp(s
->name
, port
) == 0) {
1583 if (a
.port
> 0 && a
.port
!= s
->port
) {
1584 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
1592 fprintf(stderr
, "Error: \"%s\" does not look like a port.\n", port
);
1599 if (!is_port
&& addr
&& *addr
&& *addr
!= '*') {
1600 if (get_prefix_1(&a
.addr
, addr
, fam
)) {
1601 if (get_dns_host(&a
, addr
, fam
)) {
1602 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", addr
);
1609 if (fam
!= AF_UNSPEC
) {
1610 int states
= f
->states
;
1612 filter_af_set(f
, fam
);
1613 filter_states_set(f
, states
);
1616 res
= malloc(sizeof(*res
));
1618 memcpy(res
, &a
, sizeof(a
));
1622 static char *proto_name(int protocol
)
1638 static void inet_stats_print(struct sockstat
*s
, int protocol
)
1642 sock_state_print(s
, proto_name(protocol
));
1644 inet_addr_print(&s
->local
, s
->lport
, s
->iface
);
1645 inet_addr_print(&s
->remote
, s
->rport
, 0);
1647 if (show_proc_ctx
|| show_sock_ctx
) {
1648 if (find_entry(s
->ino
, &buf
,
1649 (show_proc_ctx
& show_sock_ctx
) ?
1650 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
1651 printf(" users:(%s)", buf
);
1654 } else if (show_users
) {
1655 if (find_entry(s
->ino
, &buf
, USERS
) > 0) {
1656 printf(" users:(%s)", buf
);
1662 static int proc_parse_inet_addr(char *loc
, char *rem
, int family
, struct
1665 s
->local
.family
= s
->remote
.family
= family
;
1666 if (family
== AF_INET
) {
1667 sscanf(loc
, "%x:%x", s
->local
.data
, (unsigned *)&s
->lport
);
1668 sscanf(rem
, "%x:%x", s
->remote
.data
, (unsigned *)&s
->rport
);
1669 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
1672 sscanf(loc
, "%08x%08x%08x%08x:%x",
1678 sscanf(rem
, "%08x%08x%08x%08x:%x",
1684 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
1690 static int proc_inet_split_line(char *line
, char **loc
, char **rem
, char **data
)
1694 if ((p
= strchr(line
, ':')) == NULL
)
1698 if ((p
= strchr(*loc
, ':')) == NULL
)
1703 if ((p
= strchr(*rem
, ':')) == NULL
)
1711 static char *sprint_bw(char *buf
, double bw
)
1714 sprintf(buf
, "%.1fM", bw
/ 1000000.);
1715 else if (bw
> 1000.)
1716 sprintf(buf
, "%.1fK", bw
/ 1000.);
1718 sprintf(buf
, "%g", bw
);
1723 static void tcp_stats_print(struct tcpstat
*s
)
1729 if (s
->has_sack_opt
)
1733 if (s
->has_ecnseen_opt
)
1735 if (s
->has_fastopen_opt
)
1736 printf(" fastopen");
1738 printf(" %s", s
->cong_alg
);
1739 if (s
->has_wscale_opt
)
1740 printf(" wscale:%d,%d", s
->snd_wscale
, s
->rcv_wscale
);
1742 printf(" rto:%g", s
->rto
);
1744 printf(" backoff:%u", s
->backoff
);
1746 printf(" rtt:%g/%g", s
->rtt
, s
->rttvar
);
1748 printf(" ato:%g", s
->ato
);
1751 printf(" qack:%d", s
->qack
);
1756 printf(" mss:%d", s
->mss
);
1758 printf(" cwnd:%d", s
->cwnd
);
1760 printf(" ssthresh:%d", s
->ssthresh
);
1763 printf(" bytes_acked:%llu", s
->bytes_acked
);
1764 if (s
->bytes_received
)
1765 printf(" bytes_received:%llu", s
->bytes_received
);
1767 printf(" segs_out:%u", s
->segs_out
);
1769 printf(" segs_in:%u", s
->segs_in
);
1770 if (s
->data_segs_out
)
1771 printf(" data_segs_out:%u", s
->data_segs_out
);
1772 if (s
->data_segs_in
)
1773 printf(" data_segs_in:%u", s
->data_segs_in
);
1775 if (s
->dctcp
&& s
->dctcp
->enabled
) {
1776 struct dctcpstat
*dctcp
= s
->dctcp
;
1778 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
1779 dctcp
->ce_state
, dctcp
->alpha
, dctcp
->ab_ecn
,
1781 } else if (s
->dctcp
) {
1782 printf(" dctcp:fallback_mode");
1786 printf(" send %sbps", sprint_bw(b1
, s
->send_bps
));
1788 printf(" lastsnd:%u", s
->lastsnd
);
1790 printf(" lastrcv:%u", s
->lastrcv
);
1792 printf(" lastack:%u", s
->lastack
);
1794 if (s
->pacing_rate
) {
1795 printf(" pacing_rate %sbps", sprint_bw(b1
, s
->pacing_rate
));
1796 if (s
->pacing_rate_max
)
1797 printf("/%sbps", sprint_bw(b1
,
1798 s
->pacing_rate_max
));
1802 printf(" unacked:%u", s
->unacked
);
1803 if (s
->retrans
|| s
->retrans_total
)
1804 printf(" retrans:%u/%u", s
->retrans
, s
->retrans_total
);
1806 printf(" lost:%u", s
->lost
);
1807 if (s
->sacked
&& s
->ss
.state
!= SS_LISTEN
)
1808 printf(" sacked:%u", s
->sacked
);
1810 printf(" fackets:%u", s
->fackets
);
1811 if (s
->reordering
!= 3)
1812 printf(" reordering:%d", s
->reordering
);
1814 printf(" rcv_rtt:%g", s
->rcv_rtt
);
1816 printf(" rcv_space:%d", s
->rcv_space
);
1818 printf(" notsent:%u", s
->not_sent
);
1820 printf(" minrtt:%g", s
->min_rtt
);
1823 static void tcp_timer_print(struct tcpstat
*s
)
1828 printf(" timer:(%s,%s,%d)",
1830 print_ms_timer(s
->timeout
),
1835 static int tcp_show_line(char *line
, const struct filter
*f
, int family
)
1837 int rto
= 0, ato
= 0;
1838 struct tcpstat s
= {};
1839 char *loc
, *rem
, *data
;
1842 int hz
= get_user_hz();
1844 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
1847 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
1849 if (!(f
->states
& (1 << state
)))
1852 proc_parse_inet_addr(loc
, rem
, family
, &s
.ss
);
1854 if (f
->f
&& run_ssfilter(f
->f
, &s
.ss
) == 0)
1858 n
= sscanf(data
, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
1859 &s
.ss
.state
, &s
.ss
.wq
, &s
.ss
.rq
,
1860 &s
.timer
, &s
.timeout
, &s
.retrans
, &s
.ss
.uid
, &s
.probes
,
1861 &s
.ss
.ino
, &s
.ss
.refcnt
, &s
.ss
.sk
, &rto
, &ato
, &s
.qack
, &s
.cwnd
,
1874 s
.retrans
= s
.timer
!= 1 ? s
.probes
: s
.retrans
;
1875 s
.timeout
= (s
.timeout
* 1000 + hz
- 1) / hz
;
1876 s
.ato
= (double)ato
/ hz
;
1878 s
.rto
= (double)rto
;
1879 s
.ssthresh
= s
.ssthresh
== -1 ? 0 : s
.ssthresh
;
1880 s
.rto
= s
.rto
!= 3 * hz
? s
.rto
/ hz
: 0;
1882 inet_stats_print(&s
.ss
, IPPROTO_TCP
);
1885 tcp_timer_print(&s
);
1888 sock_details_print(&s
.ss
);
1890 printf(" opt:\"%s\"", opt
);
1894 tcp_stats_print(&s
);
1900 static int generic_record_read(FILE *fp
,
1901 int (*worker
)(char*, const struct filter
*, int),
1902 const struct filter
*f
, int fam
)
1907 if (fgets(line
, sizeof(line
), fp
) == NULL
)
1910 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1911 int n
= strlen(line
);
1913 if (n
== 0 || line
[n
-1] != '\n') {
1919 if (worker(line
, f
, fam
) < 0)
1924 return ferror(fp
) ? -1 : 0;
1927 static void print_skmeminfo(struct rtattr
*tb
[], int attrtype
)
1929 const __u32
*skmeminfo
;
1931 if (!tb
[attrtype
]) {
1932 if (attrtype
== INET_DIAG_SKMEMINFO
) {
1933 if (!tb
[INET_DIAG_MEMINFO
])
1936 const struct inet_diag_meminfo
*minfo
=
1937 RTA_DATA(tb
[INET_DIAG_MEMINFO
]);
1939 printf(" mem:(r%u,w%u,f%u,t%u)",
1948 skmeminfo
= RTA_DATA(tb
[attrtype
]);
1950 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
1951 skmeminfo
[SK_MEMINFO_RMEM_ALLOC
],
1952 skmeminfo
[SK_MEMINFO_RCVBUF
],
1953 skmeminfo
[SK_MEMINFO_WMEM_ALLOC
],
1954 skmeminfo
[SK_MEMINFO_SNDBUF
],
1955 skmeminfo
[SK_MEMINFO_FWD_ALLOC
],
1956 skmeminfo
[SK_MEMINFO_WMEM_QUEUED
],
1957 skmeminfo
[SK_MEMINFO_OPTMEM
]);
1959 if (RTA_PAYLOAD(tb
[attrtype
]) >=
1960 (SK_MEMINFO_BACKLOG
+ 1) * sizeof(__u32
))
1961 printf(",bl%u", skmeminfo
[SK_MEMINFO_BACKLOG
]);
1963 if (RTA_PAYLOAD(tb
[attrtype
]) >=
1964 (SK_MEMINFO_DROPS
+ 1) * sizeof(__u32
))
1965 printf(",d%u", skmeminfo
[SK_MEMINFO_DROPS
]);
1970 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
1972 static void tcp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
1973 struct rtattr
*tb
[])
1976 struct tcpstat s
= {};
1978 s
.ss
.state
= r
->idiag_state
;
1980 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
1982 if (tb
[INET_DIAG_INFO
]) {
1983 struct tcp_info
*info
;
1984 int len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
1986 /* workaround for older kernels with less fields */
1987 if (len
< sizeof(*info
)) {
1988 info
= alloca(sizeof(*info
));
1989 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
1990 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
1992 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
1995 s
.has_ts_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_TIMESTAMPS
);
1996 s
.has_sack_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SACK
);
1997 s
.has_ecn_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN
);
1998 s
.has_ecnseen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN_SEEN
);
1999 s
.has_fastopen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SYN_DATA
);
2002 if (tb
[INET_DIAG_CONG
])
2004 rta_getattr_str(tb
[INET_DIAG_CONG
]),
2005 sizeof(s
.cong_alg
) - 1);
2007 if (TCPI_HAS_OPT(info
, TCPI_OPT_WSCALE
)) {
2008 s
.has_wscale_opt
= true;
2009 s
.snd_wscale
= info
->tcpi_snd_wscale
;
2010 s
.rcv_wscale
= info
->tcpi_rcv_wscale
;
2013 if (info
->tcpi_rto
&& info
->tcpi_rto
!= 3000000)
2014 s
.rto
= (double)info
->tcpi_rto
/ 1000;
2016 s
.backoff
= info
->tcpi_backoff
;
2017 s
.rtt
= (double)info
->tcpi_rtt
/ 1000;
2018 s
.rttvar
= (double)info
->tcpi_rttvar
/ 1000;
2019 s
.ato
= (double)info
->tcpi_ato
/ 1000;
2020 s
.mss
= info
->tcpi_snd_mss
;
2021 s
.rcv_space
= info
->tcpi_rcv_space
;
2022 s
.rcv_rtt
= (double)info
->tcpi_rcv_rtt
/ 1000;
2023 s
.lastsnd
= info
->tcpi_last_data_sent
;
2024 s
.lastrcv
= info
->tcpi_last_data_recv
;
2025 s
.lastack
= info
->tcpi_last_ack_recv
;
2026 s
.unacked
= info
->tcpi_unacked
;
2027 s
.retrans
= info
->tcpi_retrans
;
2028 s
.retrans_total
= info
->tcpi_total_retrans
;
2029 s
.lost
= info
->tcpi_lost
;
2030 s
.sacked
= info
->tcpi_sacked
;
2031 s
.reordering
= info
->tcpi_reordering
;
2032 s
.rcv_space
= info
->tcpi_rcv_space
;
2033 s
.cwnd
= info
->tcpi_snd_cwnd
;
2035 if (info
->tcpi_snd_ssthresh
< 0xFFFF)
2036 s
.ssthresh
= info
->tcpi_snd_ssthresh
;
2038 rtt
= (double) info
->tcpi_rtt
;
2039 if (tb
[INET_DIAG_VEGASINFO
]) {
2040 const struct tcpvegas_info
*vinfo
2041 = RTA_DATA(tb
[INET_DIAG_VEGASINFO
]);
2043 if (vinfo
->tcpv_enabled
&&
2044 vinfo
->tcpv_rtt
&& vinfo
->tcpv_rtt
!= 0x7fffffff)
2045 rtt
= vinfo
->tcpv_rtt
;
2048 if (tb
[INET_DIAG_DCTCPINFO
]) {
2049 struct dctcpstat
*dctcp
= malloc(sizeof(struct
2052 const struct tcp_dctcp_info
*dinfo
2053 = RTA_DATA(tb
[INET_DIAG_DCTCPINFO
]);
2055 dctcp
->enabled
= !!dinfo
->dctcp_enabled
;
2056 dctcp
->ce_state
= dinfo
->dctcp_ce_state
;
2057 dctcp
->alpha
= dinfo
->dctcp_alpha
;
2058 dctcp
->ab_ecn
= dinfo
->dctcp_ab_ecn
;
2059 dctcp
->ab_tot
= dinfo
->dctcp_ab_tot
;
2063 if (rtt
> 0 && info
->tcpi_snd_mss
&& info
->tcpi_snd_cwnd
) {
2064 s
.send_bps
= (double) info
->tcpi_snd_cwnd
*
2065 (double)info
->tcpi_snd_mss
* 8000000. / rtt
;
2068 if (info
->tcpi_pacing_rate
&&
2069 info
->tcpi_pacing_rate
!= ~0ULL) {
2070 s
.pacing_rate
= info
->tcpi_pacing_rate
* 8.;
2072 if (info
->tcpi_max_pacing_rate
&&
2073 info
->tcpi_max_pacing_rate
!= ~0ULL)
2074 s
.pacing_rate_max
= info
->tcpi_max_pacing_rate
* 8.;
2076 s
.bytes_acked
= info
->tcpi_bytes_acked
;
2077 s
.bytes_received
= info
->tcpi_bytes_received
;
2078 s
.segs_out
= info
->tcpi_segs_out
;
2079 s
.segs_in
= info
->tcpi_segs_in
;
2080 s
.data_segs_out
= info
->tcpi_data_segs_out
;
2081 s
.data_segs_in
= info
->tcpi_data_segs_in
;
2082 s
.not_sent
= info
->tcpi_notsent_bytes
;
2083 if (info
->tcpi_min_rtt
&& info
->tcpi_min_rtt
!= ~0U)
2084 s
.min_rtt
= (double) info
->tcpi_min_rtt
/ 1000;
2085 tcp_stats_print(&s
);
2090 static void parse_diag_msg(struct nlmsghdr
*nlh
, struct sockstat
*s
)
2092 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2093 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2095 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2096 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2098 s
->state
= r
->idiag_state
;
2099 s
->local
.family
= s
->remote
.family
= r
->idiag_family
;
2100 s
->lport
= ntohs(r
->id
.idiag_sport
);
2101 s
->rport
= ntohs(r
->id
.idiag_dport
);
2102 s
->wq
= r
->idiag_wqueue
;
2103 s
->rq
= r
->idiag_rqueue
;
2104 s
->ino
= r
->idiag_inode
;
2105 s
->uid
= r
->idiag_uid
;
2106 s
->iface
= r
->id
.idiag_if
;
2107 s
->sk
= cookie_sk_get(&r
->id
.idiag_cookie
[0]);
2109 if (s
->local
.family
== AF_INET
)
2110 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
2112 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
2114 memcpy(s
->local
.data
, r
->id
.idiag_src
, s
->local
.bytelen
);
2115 memcpy(s
->remote
.data
, r
->id
.idiag_dst
, s
->local
.bytelen
);
2118 static int inet_show_sock(struct nlmsghdr
*nlh
,
2122 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2123 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2125 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2126 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2128 if (tb
[INET_DIAG_PROTOCOL
])
2129 protocol
= *(__u8
*)RTA_DATA(tb
[INET_DIAG_PROTOCOL
]);
2131 inet_stats_print(s
, protocol
);
2134 struct tcpstat t
= {};
2136 t
.timer
= r
->idiag_timer
;
2137 t
.timeout
= r
->idiag_expires
;
2138 t
.retrans
= r
->idiag_retrans
;
2139 tcp_timer_print(&t
);
2143 sock_details_print(s
);
2144 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
]) {
2145 unsigned char v6only
;
2147 v6only
= *(__u8
*)RTA_DATA(tb
[INET_DIAG_SKV6ONLY
]);
2148 printf(" v6only:%u", v6only
);
2150 if (tb
[INET_DIAG_SHUTDOWN
]) {
2153 mask
= *(__u8
*)RTA_DATA(tb
[INET_DIAG_SHUTDOWN
]);
2154 printf(" %c-%c", mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
2158 if (show_mem
|| show_tcpinfo
) {
2160 tcp_show_info(nlh
, r
, tb
);
2167 static int tcpdiag_send(int fd
, int protocol
, struct filter
*f
)
2169 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
2171 struct nlmsghdr nlh
;
2172 struct inet_diag_req r
;
2174 .nlh
.nlmsg_len
= sizeof(req
),
2175 .nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
,
2176 .nlh
.nlmsg_seq
= MAGIC_SEQ
,
2177 .r
.idiag_family
= AF_INET
,
2178 .r
.idiag_states
= f
->states
,
2184 struct iovec iov
[3];
2187 if (protocol
== IPPROTO_UDP
)
2190 if (protocol
== IPPROTO_TCP
)
2191 req
.nlh
.nlmsg_type
= TCPDIAG_GETSOCK
;
2193 req
.nlh
.nlmsg_type
= DCCPDIAG_GETSOCK
;
2195 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
2196 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
2200 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
2201 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
2202 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
2205 iov
[0] = (struct iovec
){
2207 .iov_len
= sizeof(req
)
2210 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
2212 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
2213 rta
.rta_len
= RTA_LENGTH(bclen
);
2214 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
2215 iov
[2] = (struct iovec
){ bc
, bclen
};
2216 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
2221 msg
= (struct msghdr
) {
2222 .msg_name
= (void *)&nladdr
,
2223 .msg_namelen
= sizeof(nladdr
),
2225 .msg_iovlen
= iovlen
,
2228 if (sendmsg(fd
, &msg
, 0) < 0) {
2236 static int sockdiag_send(int family
, int fd
, int protocol
, struct filter
*f
)
2238 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
2239 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
2244 struct iovec iov
[3];
2247 if (family
== PF_UNSPEC
)
2248 return tcpdiag_send(fd
, protocol
, f
);
2250 memset(&req
.r
, 0, sizeof(req
.r
));
2251 req
.r
.sdiag_family
= family
;
2252 req
.r
.sdiag_protocol
= protocol
;
2253 req
.r
.idiag_states
= f
->states
;
2255 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
2256 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
2260 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
2261 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
2262 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
2265 iov
[0] = (struct iovec
){
2267 .iov_len
= sizeof(req
)
2270 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
2272 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
2273 rta
.rta_len
= RTA_LENGTH(bclen
);
2274 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
2275 iov
[2] = (struct iovec
){ bc
, bclen
};
2276 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
2281 msg
= (struct msghdr
) {
2282 .msg_name
= (void *)&nladdr
,
2283 .msg_namelen
= sizeof(nladdr
),
2285 .msg_iovlen
= iovlen
,
2288 if (sendmsg(fd
, &msg
, 0) < 0) {
2296 struct inet_diag_arg
{
2299 struct rtnl_handle
*rth
;
2302 static int kill_inet_sock(struct nlmsghdr
*h
, void *arg
)
2304 struct inet_diag_msg
*d
= NLMSG_DATA(h
);
2305 struct inet_diag_arg
*diag_arg
= arg
;
2306 struct rtnl_handle
*rth
= diag_arg
->rth
;
2308 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
2310 req
.nlh
.nlmsg_type
= SOCK_DESTROY
;
2311 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2312 req
.nlh
.nlmsg_seq
= ++rth
->seq
;
2313 req
.r
.sdiag_family
= d
->idiag_family
;
2314 req
.r
.sdiag_protocol
= diag_arg
->protocol
;
2317 return rtnl_talk(rth
, &req
.nlh
, NULL
, 0);
2320 static int show_one_inet_sock(const struct sockaddr_nl
*addr
,
2321 struct nlmsghdr
*h
, void *arg
)
2324 struct inet_diag_arg
*diag_arg
= arg
;
2325 struct inet_diag_msg
*r
= NLMSG_DATA(h
);
2326 struct sockstat s
= {};
2328 if (!(diag_arg
->f
->families
& (1 << r
->idiag_family
)))
2331 parse_diag_msg(h
, &s
);
2333 if (diag_arg
->f
->f
&& run_ssfilter(diag_arg
->f
->f
, &s
) == 0)
2336 if (diag_arg
->f
->kill
&& kill_inet_sock(h
, arg
) != 0) {
2337 if (errno
== EOPNOTSUPP
|| errno
== ENOENT
) {
2338 /* Socket can't be closed, or is already closed. */
2341 perror("SOCK_DESTROY answers");
2346 err
= inet_show_sock(h
, &s
, diag_arg
->protocol
);
2353 static int inet_show_netlink(struct filter
*f
, FILE *dump_fp
, int protocol
)
2356 struct rtnl_handle rth
, rth2
;
2357 int family
= PF_INET
;
2358 struct inet_diag_arg arg
= { .f
= f
, .protocol
= protocol
};
2360 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
2364 if (rtnl_open_byproto(&rth2
, 0, NETLINK_SOCK_DIAG
)) {
2371 rth
.dump
= MAGIC_SEQ
;
2372 rth
.dump_fp
= dump_fp
;
2373 if (preferred_family
== PF_INET6
)
2377 if ((err
= sockdiag_send(family
, rth
.fd
, protocol
, f
)))
2380 if ((err
= rtnl_dump_filter(&rth
, show_one_inet_sock
, &arg
))) {
2381 if (family
!= PF_UNSPEC
) {
2387 if (family
== PF_INET
&& preferred_family
!= PF_INET
) {
2395 rtnl_close(arg
.rth
);
2399 static int tcp_show_netlink_file(struct filter
*f
)
2404 if ((fp
= fopen(getenv("TCPDIAG_FILE"), "r")) == NULL
) {
2405 perror("fopen($TCPDIAG_FILE)");
2411 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
2412 struct sockstat s
= {};
2414 status
= fread(buf
, 1, sizeof(*h
), fp
);
2416 perror("Reading header from $TCPDIAG_FILE");
2419 if (status
!= sizeof(*h
)) {
2420 perror("Unexpected EOF reading $TCPDIAG_FILE");
2424 status
= fread(h
+1, 1, NLMSG_ALIGN(h
->nlmsg_len
-sizeof(*h
)), fp
);
2427 perror("Reading $TCPDIAG_FILE");
2430 if (status
+ sizeof(*h
) < h
->nlmsg_len
) {
2431 perror("Unexpected EOF reading $TCPDIAG_FILE");
2435 /* The only legal exit point */
2436 if (h
->nlmsg_type
== NLMSG_DONE
)
2439 if (h
->nlmsg_type
== NLMSG_ERROR
) {
2440 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
2442 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
2443 fprintf(stderr
, "ERROR truncated\n");
2445 errno
= -err
->error
;
2446 perror("TCPDIAG answered");
2451 parse_diag_msg(h
, &s
);
2453 if (f
&& f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
2456 err
= inet_show_sock(h
, &s
, IPPROTO_TCP
);
2462 static int tcp_show(struct filter
*f
, int socktype
)
2466 int bufsize
= 64*1024;
2468 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2471 dg_proto
= TCP_PROTO
;
2473 if (getenv("TCPDIAG_FILE"))
2474 return tcp_show_netlink_file(f
);
2476 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
2477 && inet_show_netlink(f
, NULL
, socktype
) == 0)
2480 /* Sigh... We have to parse /proc/net/tcp... */
2483 /* Estimate amount of sockets and try to allocate
2484 * huge buffer to read all the table at one read.
2485 * Limit it by 16MB though. The assumption is: as soon as
2486 * kernel was able to hold information about N connections,
2487 * it is able to give us some memory for snapshot.
2490 get_slabstat(&slabstat
);
2492 int guess
= slabstat
.socks
+slabstat
.tcp_syns
;
2494 if (f
->states
&(1<<SS_TIME_WAIT
))
2495 guess
+= slabstat
.tcp_tws
;
2496 if (guess
> (16*1024*1024)/128)
2497 guess
= (16*1024*1024)/128;
2499 if (guess
> bufsize
)
2502 while (bufsize
>= 64*1024) {
2503 if ((buf
= malloc(bufsize
)) != NULL
)
2512 if (f
->families
& (1<<AF_INET
)) {
2513 if ((fp
= net_tcp_open()) == NULL
)
2516 setbuffer(fp
, buf
, bufsize
);
2517 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET
))
2522 if ((f
->families
& (1<<AF_INET6
)) &&
2523 (fp
= net_tcp6_open()) != NULL
) {
2524 setbuffer(fp
, buf
, bufsize
);
2525 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET6
))
2535 int saved_errno
= errno
;
2540 errno
= saved_errno
;
2546 static int dgram_show_line(char *line
, const struct filter
*f
, int family
)
2548 struct sockstat s
= {};
2549 char *loc
, *rem
, *data
;
2553 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
2556 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
2558 if (!(f
->states
& (1 << state
)))
2561 proc_parse_inet_addr(loc
, rem
, family
, &s
);
2563 if (f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
2567 n
= sscanf(data
, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
2568 &s
.state
, &s
.wq
, &s
.rq
,
2570 &s
.refcnt
, &s
.sk
, opt
);
2575 inet_stats_print(&s
, dg_proto
== UDP_PROTO
? IPPROTO_UDP
: 0);
2577 if (show_details
&& opt
[0])
2578 printf(" opt:\"%s\"", opt
);
2584 static int udp_show(struct filter
*f
)
2588 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2591 dg_proto
= UDP_PROTO
;
2593 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2594 && inet_show_netlink(f
, NULL
, IPPROTO_UDP
) == 0)
2597 if (f
->families
&(1<<AF_INET
)) {
2598 if ((fp
= net_udp_open()) == NULL
)
2600 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
2605 if ((f
->families
&(1<<AF_INET6
)) &&
2606 (fp
= net_udp6_open()) != NULL
) {
2607 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
2615 int saved_errno
= errno
;
2619 errno
= saved_errno
;
2624 static int raw_show(struct filter
*f
)
2628 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2631 dg_proto
= RAW_PROTO
;
2633 if (f
->families
&(1<<AF_INET
)) {
2634 if ((fp
= net_raw_open()) == NULL
)
2636 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
2641 if ((f
->families
&(1<<AF_INET6
)) &&
2642 (fp
= net_raw6_open()) != NULL
) {
2643 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
2651 int saved_errno
= errno
;
2655 errno
= saved_errno
;
2660 int unix_state_map
[] = { SS_CLOSE
, SS_SYN_SENT
,
2661 SS_ESTABLISHED
, SS_CLOSING
};
2663 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
2665 static void unix_list_free(struct sockstat
*list
)
2668 struct sockstat
*s
= list
;
2676 static const char *unix_netid_name(int type
)
2684 case SOCK_SEQPACKET
:
2695 static bool unix_type_skip(struct sockstat
*s
, struct filter
*f
)
2697 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
&(1<<UNIX_ST_DB
)))
2699 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<UNIX_DG_DB
)))
2701 if (s
->type
== SOCK_SEQPACKET
&& !(f
->dbs
&(1<<UNIX_SQ_DB
)))
2706 static bool unix_use_proc(void)
2708 return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT");
2711 static void unix_stats_print(struct sockstat
*list
, struct filter
*f
)
2715 char *ctx_buf
= NULL
;
2716 bool use_proc
= unix_use_proc();
2717 char port_name
[30] = {};
2719 for (s
= list
; s
; s
= s
->next
) {
2720 if (!(f
->states
& (1 << s
->state
)))
2722 if (unix_type_skip(s
, f
))
2727 peer
= s
->peer_name
;
2729 if (s
->rport
&& use_proc
) {
2732 for (p
= list
; p
; p
= p
->next
) {
2733 if (s
->rport
== p
->lport
)
2740 peer
= p
->name
? : "*";
2744 if (use_proc
&& f
->f
) {
2745 struct sockstat st
= {
2746 .local
.family
= AF_UNIX
,
2747 .remote
.family
= AF_UNIX
,
2750 memcpy(st
.local
.data
, &s
->name
, sizeof(s
->name
));
2751 if (strcmp(peer
, "*"))
2752 memcpy(st
.remote
.data
, &peer
, sizeof(peer
));
2753 if (run_ssfilter(f
->f
, &st
) == 0)
2757 sock_state_print(s
, unix_netid_name(s
->type
));
2759 sock_addr_print(s
->name
?: "*", " ",
2760 int_to_str(s
->lport
, port_name
), NULL
);
2761 sock_addr_print(peer
, " ", int_to_str(s
->rport
, port_name
),
2764 if (show_proc_ctx
|| show_sock_ctx
) {
2765 if (find_entry(s
->ino
, &ctx_buf
,
2766 (show_proc_ctx
& show_sock_ctx
) ?
2767 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
2768 printf(" users:(%s)", ctx_buf
);
2771 } else if (show_users
) {
2772 if (find_entry(s
->ino
, &ctx_buf
, USERS
) > 0) {
2773 printf(" users:(%s)", ctx_buf
);
2781 static int unix_show_sock(const struct sockaddr_nl
*addr
, struct nlmsghdr
*nlh
,
2784 struct filter
*f
= (struct filter
*)arg
;
2785 struct unix_diag_msg
*r
= NLMSG_DATA(nlh
);
2786 struct rtattr
*tb
[UNIX_DIAG_MAX
+1];
2788 struct sockstat stat
= { .name
= "*", .peer_name
= "*" };
2790 parse_rtattr(tb
, UNIX_DIAG_MAX
, (struct rtattr
*)(r
+1),
2791 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2793 stat
.type
= r
->udiag_type
;
2794 stat
.state
= r
->udiag_state
;
2795 stat
.ino
= stat
.lport
= r
->udiag_ino
;
2796 stat
.local
.family
= stat
.remote
.family
= AF_UNIX
;
2798 if (unix_type_skip(&stat
, f
))
2801 if (tb
[UNIX_DIAG_RQLEN
]) {
2802 struct unix_diag_rqlen
*rql
= RTA_DATA(tb
[UNIX_DIAG_RQLEN
]);
2804 stat
.rq
= rql
->udiag_rqueue
;
2805 stat
.wq
= rql
->udiag_wqueue
;
2807 if (tb
[UNIX_DIAG_NAME
]) {
2808 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_NAME
]);
2810 memcpy(name
, RTA_DATA(tb
[UNIX_DIAG_NAME
]), len
);
2812 if (name
[0] == '\0')
2814 stat
.name
= &name
[0];
2815 memcpy(stat
.local
.data
, &stat
.name
, sizeof(stat
.name
));
2817 if (tb
[UNIX_DIAG_PEER
])
2818 stat
.rport
= rta_getattr_u32(tb
[UNIX_DIAG_PEER
]);
2820 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
2823 unix_stats_print(&stat
, f
);
2827 print_skmeminfo(tb
, UNIX_DIAG_MEMINFO
);
2830 if (tb
[UNIX_DIAG_SHUTDOWN
]) {
2833 mask
= *(__u8
*)RTA_DATA(tb
[UNIX_DIAG_SHUTDOWN
]);
2834 printf(" %c-%c", mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
2837 if (show_mem
|| show_details
)
2843 static int handle_netlink_request(struct filter
*f
, struct nlmsghdr
*req
,
2844 size_t size
, rtnl_filter_t show_one_sock
)
2847 struct rtnl_handle rth
;
2849 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
2852 rth
.dump
= MAGIC_SEQ
;
2854 if (rtnl_send(&rth
, req
, size
) < 0)
2857 if (rtnl_dump_filter(&rth
, show_one_sock
, f
))
2866 static int unix_show_netlink(struct filter
*f
)
2868 DIAG_REQUEST(req
, struct unix_diag_req r
);
2870 req
.r
.sdiag_family
= AF_UNIX
;
2871 req
.r
.udiag_states
= f
->states
;
2872 req
.r
.udiag_show
= UDIAG_SHOW_NAME
| UDIAG_SHOW_PEER
| UDIAG_SHOW_RQLEN
;
2874 req
.r
.udiag_show
|= UDIAG_SHOW_MEMINFO
;
2876 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), unix_show_sock
);
2879 static int unix_show(struct filter
*f
)
2886 struct sockstat
*list
= NULL
;
2888 if (!filter_af_get(f
, AF_UNIX
))
2891 if (!unix_use_proc() && unix_show_netlink(f
) == 0)
2894 if ((fp
= net_unix_open()) == NULL
)
2896 if (!fgets(buf
, sizeof(buf
), fp
)) {
2901 if (memcmp(buf
, "Peer", 4) == 0)
2905 while (fgets(buf
, sizeof(buf
), fp
)) {
2906 struct sockstat
*u
, **insp
;
2909 if (!(u
= calloc(1, sizeof(*u
))))
2912 u
->peer_name
= NULL
;
2914 if (sscanf(buf
, "%x: %x %x %x %x %x %d %s",
2915 &u
->rport
, &u
->rq
, &u
->wq
, &flags
, &u
->type
,
2916 &u
->state
, &u
->ino
, name
) < 8)
2920 u
->local
.family
= u
->remote
.family
= AF_UNIX
;
2922 if (flags
& (1 << 16)) {
2923 u
->state
= SS_LISTEN
;
2925 u
->state
= unix_state_map
[u
->state
-1];
2926 if (u
->type
== SOCK_DGRAM
&& u
->state
== SS_CLOSE
&& u
->rport
)
2927 u
->state
= SS_ESTABLISHED
;
2938 if (u
->type
< (*insp
)->type
||
2939 (u
->type
== (*insp
)->type
&&
2940 u
->ino
< (*insp
)->ino
))
2942 insp
= &(*insp
)->next
;
2948 if ((u
->name
= malloc(strlen(name
)+1)) == NULL
)
2950 strcpy(u
->name
, name
);
2952 if (++cnt
> MAX_UNIX_REMEMBER
) {
2953 unix_stats_print(list
, f
);
2954 unix_list_free(list
);
2961 unix_stats_print(list
, f
);
2962 unix_list_free(list
);
2970 static int packet_stats_print(struct sockstat
*s
, const struct filter
*f
)
2973 const char *addr
, *port
;
2977 s
->local
.family
= AF_PACKET
;
2978 s
->remote
.family
= AF_PACKET
;
2979 s
->local
.data
[0] = s
->prot
;
2980 if (run_ssfilter(f
->f
, s
) == 0)
2984 sock_state_print(s
, s
->type
== SOCK_RAW
? "p_raw" : "p_dgr");
2989 addr
= ll_proto_n2a(htons(s
->prot
), ll_name
, sizeof(ll_name
));
2994 port
= xll_index_to_name(s
->iface
);
2996 sock_addr_print(addr
, ":", port
, NULL
);
2997 sock_addr_print("", "*", "", NULL
);
2999 if (show_proc_ctx
|| show_sock_ctx
) {
3000 if (find_entry(s
->ino
, &buf
,
3001 (show_proc_ctx
& show_sock_ctx
) ?
3002 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
3003 printf(" users:(%s)", buf
);
3006 } else if (show_users
) {
3007 if (find_entry(s
->ino
, &buf
, USERS
) > 0) {
3008 printf(" users:(%s)", buf
);
3014 sock_details_print(s
);
3019 static void packet_show_ring(struct packet_diag_ring
*ring
)
3021 printf("blk_size:%d", ring
->pdr_block_size
);
3022 printf(",blk_nr:%d", ring
->pdr_block_nr
);
3023 printf(",frm_size:%d", ring
->pdr_frame_size
);
3024 printf(",frm_nr:%d", ring
->pdr_frame_nr
);
3025 printf(",tmo:%d", ring
->pdr_retire_tmo
);
3026 printf(",features:0x%x", ring
->pdr_features
);
3029 static int packet_show_sock(const struct sockaddr_nl
*addr
,
3030 struct nlmsghdr
*nlh
, void *arg
)
3032 const struct filter
*f
= arg
;
3033 struct packet_diag_msg
*r
= NLMSG_DATA(nlh
);
3034 struct packet_diag_info
*pinfo
= NULL
;
3035 struct packet_diag_ring
*ring_rx
= NULL
, *ring_tx
= NULL
;
3036 struct rtattr
*tb
[PACKET_DIAG_MAX
+1];
3037 struct sockstat stat
= {};
3038 uint32_t fanout
= 0;
3039 bool has_fanout
= false;
3041 parse_rtattr(tb
, PACKET_DIAG_MAX
, (struct rtattr
*)(r
+1),
3042 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3044 /* use /proc/net/packet if all info are not available */
3045 if (!tb
[PACKET_DIAG_MEMINFO
])
3048 stat
.type
= r
->pdiag_type
;
3049 stat
.prot
= r
->pdiag_num
;
3050 stat
.ino
= r
->pdiag_ino
;
3051 stat
.state
= SS_CLOSE
;
3052 stat
.sk
= cookie_sk_get(&r
->pdiag_cookie
[0]);
3054 if (tb
[PACKET_DIAG_MEMINFO
]) {
3055 __u32
*skmeminfo
= RTA_DATA(tb
[PACKET_DIAG_MEMINFO
]);
3057 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
3060 if (tb
[PACKET_DIAG_INFO
]) {
3061 pinfo
= RTA_DATA(tb
[PACKET_DIAG_INFO
]);
3062 stat
.lport
= stat
.iface
= pinfo
->pdi_index
;
3065 if (tb
[PACKET_DIAG_UID
])
3066 stat
.uid
= *(__u32
*)RTA_DATA(tb
[PACKET_DIAG_UID
]);
3068 if (tb
[PACKET_DIAG_RX_RING
])
3069 ring_rx
= RTA_DATA(tb
[PACKET_DIAG_RX_RING
]);
3071 if (tb
[PACKET_DIAG_TX_RING
])
3072 ring_tx
= RTA_DATA(tb
[PACKET_DIAG_TX_RING
]);
3074 if (tb
[PACKET_DIAG_FANOUT
]) {
3076 fanout
= *(uint32_t *)RTA_DATA(tb
[PACKET_DIAG_FANOUT
]);
3079 if (packet_stats_print(&stat
, f
))
3084 printf("\n\tver:%d", pinfo
->pdi_version
);
3085 printf(" cpy_thresh:%d", pinfo
->pdi_copy_thresh
);
3087 if (pinfo
->pdi_flags
& PDI_RUNNING
)
3089 if (pinfo
->pdi_flags
& PDI_AUXDATA
)
3091 if (pinfo
->pdi_flags
& PDI_ORIGDEV
)
3093 if (pinfo
->pdi_flags
& PDI_VNETHDR
)
3095 if (pinfo
->pdi_flags
& PDI_LOSS
)
3097 if (!pinfo
->pdi_flags
)
3102 printf("\n\tring_rx(");
3103 packet_show_ring(ring_rx
);
3107 printf("\n\tring_tx(");
3108 packet_show_ring(ring_tx
);
3112 uint16_t type
= (fanout
>> 16) & 0xffff;
3114 printf("\n\tfanout(");
3115 printf("id:%d,", fanout
& 0xffff);
3131 printf("0x%x", type
);
3137 if (show_bpf
&& tb
[PACKET_DIAG_FILTER
]) {
3138 struct sock_filter
*fil
=
3139 RTA_DATA(tb
[PACKET_DIAG_FILTER
]);
3140 int num
= RTA_PAYLOAD(tb
[PACKET_DIAG_FILTER
]) /
3141 sizeof(struct sock_filter
);
3143 printf("\n\tbpf filter (%d): ", num
);
3145 printf(" 0x%02x %u %u %u,",
3146 fil
->code
, fil
->jt
, fil
->jf
, fil
->k
);
3155 static int packet_show_netlink(struct filter
*f
)
3157 DIAG_REQUEST(req
, struct packet_diag_req r
);
3159 req
.r
.sdiag_family
= AF_PACKET
;
3160 req
.r
.pdiag_show
= PACKET_SHOW_INFO
| PACKET_SHOW_MEMINFO
|
3161 PACKET_SHOW_FILTER
| PACKET_SHOW_RING_CFG
| PACKET_SHOW_FANOUT
;
3163 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), packet_show_sock
);
3166 static int packet_show_line(char *buf
, const struct filter
*f
, int fam
)
3168 unsigned long long sk
;
3169 struct sockstat stat
= {};
3170 int type
, prot
, iface
, state
, rq
, uid
, ino
;
3172 sscanf(buf
, "%llx %*d %d %x %d %d %u %u %u",
3174 &type
, &prot
, &iface
, &state
,
3177 if (stat
.type
== SOCK_RAW
&& !(f
->dbs
&(1<<PACKET_R_DB
)))
3179 if (stat
.type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<PACKET_DG_DB
)))
3184 stat
.lport
= stat
.iface
= iface
;
3189 stat
.state
= SS_CLOSE
;
3191 if (packet_stats_print(&stat
, f
))
3198 static int packet_show(struct filter
*f
)
3203 if (!filter_af_get(f
, AF_PACKET
) || !(f
->states
& (1 << SS_CLOSE
)))
3206 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3207 packet_show_netlink(f
) == 0)
3210 if ((fp
= net_packet_open()) == NULL
)
3212 if (generic_record_read(fp
, packet_show_line
, f
, AF_PACKET
))
3219 static int netlink_show_one(struct filter
*f
,
3220 int prot
, int pid
, unsigned int groups
,
3221 int state
, int dst_pid
, unsigned int dst_group
,
3223 unsigned long long sk
, unsigned long long cb
)
3227 SPRINT_BUF(prot_buf
) = {};
3228 const char *prot_name
;
3229 char procname
[64] = {};
3231 st
.state
= SS_CLOSE
;
3236 st
.local
.family
= AF_NETLINK
;
3237 st
.remote
.family
= AF_NETLINK
;
3240 st
.local
.data
[0] = prot
;
3241 if (run_ssfilter(f
->f
, &st
) == 0)
3245 sock_state_print(&st
, "nl");
3247 if (resolve_services
)
3248 prot_name
= nl_proto_n2a(prot
, prot_buf
, sizeof(prot_buf
));
3250 prot_name
= int_to_str(prot
, prot_buf
);
3254 } else if (resolve_services
) {
3259 strncpy(procname
, "kernel", 6);
3260 } else if (pid
> 0) {
3263 snprintf(procname
, sizeof(procname
), "%s/%d/stat",
3264 getenv("PROC_ROOT") ? : "/proc", pid
);
3265 if ((fp
= fopen(procname
, "r")) != NULL
) {
3266 if (fscanf(fp
, "%*d (%[^)])", procname
) == 1) {
3267 snprintf(procname
+strlen(procname
),
3268 sizeof(procname
)-strlen(procname
),
3276 int_to_str(pid
, procname
);
3278 int_to_str(pid
, procname
);
3281 sock_addr_print(prot_name
, ":", procname
, NULL
);
3283 if (state
== NETLINK_CONNECTED
) {
3284 char dst_group_buf
[30];
3285 char dst_pid_buf
[30];
3287 sock_addr_print(int_to_str(dst_group
, dst_group_buf
), ":",
3288 int_to_str(dst_pid
, dst_pid_buf
), NULL
);
3290 sock_addr_print("", "*", "", NULL
);
3293 char *pid_context
= NULL
;
3295 if (show_proc_ctx
) {
3296 /* The pid value will either be:
3297 * 0 if destination kernel - show kernel initial context.
3298 * A valid process pid - use getpidcon.
3299 * A unique value allocated by the kernel or netlink user
3300 * to the process - show context as "not available".
3303 security_get_initial_context("kernel", &pid_context
);
3305 getpidcon(pid
, &pid_context
);
3307 if (pid_context
!= NULL
) {
3308 printf("proc_ctx=%-*s ", serv_width
, pid_context
);
3311 printf("proc_ctx=%-*s ", serv_width
, "unavailable");
3316 printf(" sk=%llx cb=%llx groups=0x%08x", sk
, cb
, groups
);
3323 static int netlink_show_sock(const struct sockaddr_nl
*addr
,
3324 struct nlmsghdr
*nlh
, void *arg
)
3326 struct filter
*f
= (struct filter
*)arg
;
3327 struct netlink_diag_msg
*r
= NLMSG_DATA(nlh
);
3328 struct rtattr
*tb
[NETLINK_DIAG_MAX
+1];
3330 unsigned long groups
= 0;
3332 parse_rtattr(tb
, NETLINK_DIAG_MAX
, (struct rtattr
*)(r
+1),
3333 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3335 if (tb
[NETLINK_DIAG_GROUPS
] && RTA_PAYLOAD(tb
[NETLINK_DIAG_GROUPS
]))
3336 groups
= *(unsigned long *) RTA_DATA(tb
[NETLINK_DIAG_GROUPS
]);
3338 if (tb
[NETLINK_DIAG_MEMINFO
]) {
3339 const __u32
*skmeminfo
;
3341 skmeminfo
= RTA_DATA(tb
[NETLINK_DIAG_MEMINFO
]);
3343 rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
3344 wq
= skmeminfo
[SK_MEMINFO_WMEM_ALLOC
];
3347 if (netlink_show_one(f
, r
->ndiag_protocol
, r
->ndiag_portid
, groups
,
3348 r
->ndiag_state
, r
->ndiag_dst_portid
, r
->ndiag_dst_group
,
3355 print_skmeminfo(tb
, NETLINK_DIAG_MEMINFO
);
3362 static int netlink_show_netlink(struct filter
*f
)
3364 DIAG_REQUEST(req
, struct netlink_diag_req r
);
3366 req
.r
.sdiag_family
= AF_NETLINK
;
3367 req
.r
.sdiag_protocol
= NDIAG_PROTO_ALL
;
3368 req
.r
.ndiag_show
= NDIAG_SHOW_GROUPS
| NDIAG_SHOW_MEMINFO
;
3370 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), netlink_show_sock
);
3373 static int netlink_show(struct filter
*f
)
3378 unsigned int groups
;
3380 unsigned long long sk
, cb
;
3382 if (!filter_af_get(f
, AF_NETLINK
) || !(f
->states
& (1 << SS_CLOSE
)))
3385 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
3386 netlink_show_netlink(f
) == 0)
3389 if ((fp
= net_netlink_open()) == NULL
)
3391 if (!fgets(buf
, sizeof(buf
), fp
)) {
3396 while (fgets(buf
, sizeof(buf
), fp
)) {
3397 sscanf(buf
, "%llx %d %d %x %d %d %llx %d",
3399 &prot
, &pid
, &groups
, &rq
, &wq
, &cb
, &rc
);
3401 netlink_show_one(f
, prot
, pid
, groups
, 0, 0, 0, rq
, wq
, sk
, cb
);
3408 struct sock_diag_msg
{
3412 static int generic_show_sock(const struct sockaddr_nl
*addr
,
3413 struct nlmsghdr
*nlh
, void *arg
)
3415 struct sock_diag_msg
*r
= NLMSG_DATA(nlh
);
3416 struct inet_diag_arg inet_arg
= { .f
= arg
, .protocol
= IPPROTO_MAX
};
3418 switch (r
->sdiag_family
) {
3421 return show_one_inet_sock(addr
, nlh
, &inet_arg
);
3423 return unix_show_sock(addr
, nlh
, arg
);
3425 return packet_show_sock(addr
, nlh
, arg
);
3427 return netlink_show_sock(addr
, nlh
, arg
);
3433 static int handle_follow_request(struct filter
*f
)
3437 struct rtnl_handle rth
;
3439 if (f
->families
& (1 << AF_INET
) && f
->dbs
& (1 << TCP_DB
))
3440 groups
|= 1 << (SKNLGRP_INET_TCP_DESTROY
- 1);
3441 if (f
->families
& (1 << AF_INET
) && f
->dbs
& (1 << UDP_DB
))
3442 groups
|= 1 << (SKNLGRP_INET_UDP_DESTROY
- 1);
3443 if (f
->families
& (1 << AF_INET6
) && f
->dbs
& (1 << TCP_DB
))
3444 groups
|= 1 << (SKNLGRP_INET6_TCP_DESTROY
- 1);
3445 if (f
->families
& (1 << AF_INET6
) && f
->dbs
& (1 << UDP_DB
))
3446 groups
|= 1 << (SKNLGRP_INET6_UDP_DESTROY
- 1);
3451 if (rtnl_open_byproto(&rth
, groups
, NETLINK_SOCK_DIAG
))
3455 rth
.local
.nl_pid
= 0;
3457 if (rtnl_dump_filter(&rth
, generic_show_sock
, f
))
3470 static int get_snmp_int(char *proto
, char *key
, int *result
)
3474 int protolen
= strlen(proto
);
3475 int keylen
= strlen(key
);
3479 if ((fp
= net_snmp_open()) == NULL
)
3482 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
3486 if (memcmp(buf
, proto
, protolen
))
3488 while ((p
= strchr(p
, ' ')) != NULL
) {
3491 if (memcmp(p
, key
, keylen
) == 0 &&
3492 (p
[keylen
] == ' ' || p
[keylen
] == '\n'))
3495 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
3497 if (memcmp(buf
, proto
, protolen
))
3500 while ((p
= strchr(p
, ' ')) != NULL
) {
3503 sscanf(p
, "%d", result
);
3516 /* Get stats from sockstat */
3536 static void get_sockstat_line(char *line
, struct ssummary
*s
)
3538 char id
[256], rem
[256];
3540 if (sscanf(line
, "%[^ ] %[^\n]\n", id
, rem
) != 2)
3543 if (strcmp(id
, "sockets:") == 0)
3544 sscanf(rem
, "%*s%d", &s
->socks
);
3545 else if (strcmp(id
, "UDP:") == 0)
3546 sscanf(rem
, "%*s%d", &s
->udp4
);
3547 else if (strcmp(id
, "UDP6:") == 0)
3548 sscanf(rem
, "%*s%d", &s
->udp6
);
3549 else if (strcmp(id
, "RAW:") == 0)
3550 sscanf(rem
, "%*s%d", &s
->raw4
);
3551 else if (strcmp(id
, "RAW6:") == 0)
3552 sscanf(rem
, "%*s%d", &s
->raw6
);
3553 else if (strcmp(id
, "TCP6:") == 0)
3554 sscanf(rem
, "%*s%d", &s
->tcp6_hashed
);
3555 else if (strcmp(id
, "FRAG:") == 0)
3556 sscanf(rem
, "%*s%d%*s%d", &s
->frag4
, &s
->frag4_mem
);
3557 else if (strcmp(id
, "FRAG6:") == 0)
3558 sscanf(rem
, "%*s%d%*s%d", &s
->frag6
, &s
->frag6_mem
);
3559 else if (strcmp(id
, "TCP:") == 0)
3560 sscanf(rem
, "%*s%d%*s%d%*s%d%*s%d%*s%d",
3562 &s
->tcp_orphans
, &s
->tcp_tws
, &s
->tcp_total
, &s
->tcp_mem
);
3565 static int get_sockstat(struct ssummary
*s
)
3570 memset(s
, 0, sizeof(*s
));
3572 if ((fp
= net_sockstat_open()) == NULL
)
3574 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
3575 get_sockstat_line(buf
, s
);
3578 if ((fp
= net_sockstat6_open()) == NULL
)
3580 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
3581 get_sockstat_line(buf
, s
);
3587 static int print_summary(void)
3592 if (get_sockstat(&s
) < 0)
3593 perror("ss: get_sockstat");
3594 if (get_snmp_int("Tcp:", "CurrEstab", &sn
.tcp_estab
) < 0)
3595 perror("ss: get_snmpstat");
3597 get_slabstat(&slabstat
);
3599 printf("Total: %d (kernel %d)\n", s
.socks
, slabstat
.socks
);
3601 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
3602 s
.tcp_total
+ slabstat
.tcp_syns
+ s
.tcp_tws
,
3604 s
.tcp_total
- (s
.tcp4_hashed
+s
.tcp6_hashed
-s
.tcp_tws
),
3607 s
.tcp_tws
, slabstat
.tcp_tws
,
3612 printf("Transport Total IP IPv6\n");
3613 printf("* %-9d %-9s %-9s\n", slabstat
.socks
, "-", "-");
3614 printf("RAW %-9d %-9d %-9d\n", s
.raw4
+s
.raw6
, s
.raw4
, s
.raw6
);
3615 printf("UDP %-9d %-9d %-9d\n", s
.udp4
+s
.udp6
, s
.udp4
, s
.udp6
);
3616 printf("TCP %-9d %-9d %-9d\n", s
.tcp4_hashed
+s
.tcp6_hashed
, s
.tcp4_hashed
, s
.tcp6_hashed
);
3617 printf("INET %-9d %-9d %-9d\n",
3618 s
.raw4
+s
.udp4
+s
.tcp4_hashed
+
3619 s
.raw6
+s
.udp6
+s
.tcp6_hashed
,
3620 s
.raw4
+s
.udp4
+s
.tcp4_hashed
,
3621 s
.raw6
+s
.udp6
+s
.tcp6_hashed
);
3622 printf("FRAG %-9d %-9d %-9d\n", s
.frag4
+s
.frag6
, s
.frag4
, s
.frag6
);
3629 static void _usage(FILE *dest
)
3632 "Usage: ss [ OPTIONS ]\n"
3633 " ss [ OPTIONS ] [ FILTER ]\n"
3634 " -h, --help this message\n"
3635 " -V, --version output version information\n"
3636 " -n, --numeric don't resolve service names\n"
3637 " -r, --resolve resolve host names\n"
3638 " -a, --all display all sockets\n"
3639 " -l, --listening display listening sockets\n"
3640 " -o, --options show timer information\n"
3641 " -e, --extended show detailed socket information\n"
3642 " -m, --memory show socket memory usage\n"
3643 " -p, --processes show process using socket\n"
3644 " -i, --info show internal TCP information\n"
3645 " -s, --summary show socket usage summary\n"
3646 " -b, --bpf show bpf filter socket information\n"
3647 " -E, --events continually display sockets as they are destroyed\n"
3648 " -Z, --context display process SELinux security contexts\n"
3649 " -z, --contexts display process and socket SELinux security contexts\n"
3650 " -N, --net switch to the specified network namespace name\n"
3652 " -4, --ipv4 display only IP version 4 sockets\n"
3653 " -6, --ipv6 display only IP version 6 sockets\n"
3654 " -0, --packet display PACKET sockets\n"
3655 " -t, --tcp display only TCP sockets\n"
3656 " -u, --udp display only UDP sockets\n"
3657 " -d, --dccp display only DCCP sockets\n"
3658 " -w, --raw display only RAW sockets\n"
3659 " -x, --unix display only Unix domain sockets\n"
3660 " -f, --family=FAMILY display sockets of type FAMILY\n"
3661 " FAMILY := {inet|inet6|link|unix|netlink|help}\n"
3663 " -K, --kill forcibly close sockets, display what was closed\n"
3664 " -H, --no-header Suppress header line\n"
3666 " -A, --query=QUERY, --socket=QUERY\n"
3667 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
3669 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
3670 " -F, --filter=FILE read filter information from FILE\n"
3671 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
3672 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
3673 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n"
3674 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3675 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3676 " bucket := {syn-recv|time-wait}\n"
3677 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n"
3681 static void help(void) __attribute__((noreturn
));
3682 static void help(void)
3688 static void usage(void) __attribute__((noreturn
));
3689 static void usage(void)
3696 static int scan_state(const char *state
)
3700 if (strcasecmp(state
, "close") == 0 ||
3701 strcasecmp(state
, "closed") == 0)
3702 return (1<<SS_CLOSE
);
3703 if (strcasecmp(state
, "syn-rcv") == 0)
3704 return (1<<SS_SYN_RECV
);
3705 if (strcasecmp(state
, "established") == 0)
3706 return (1<<SS_ESTABLISHED
);
3707 if (strcasecmp(state
, "all") == 0)
3709 if (strcasecmp(state
, "connected") == 0)
3710 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
));
3711 if (strcasecmp(state
, "synchronized") == 0)
3712 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
)|(1<<SS_SYN_SENT
));
3713 if (strcasecmp(state
, "bucket") == 0)
3714 return (1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
);
3715 if (strcasecmp(state
, "big") == 0)
3716 return SS_ALL
& ~((1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
));
3717 for (i
= 0; i
< SS_MAX
; i
++) {
3718 if (strcasecmp(state
, sstate_namel
[i
]) == 0)
3722 fprintf(stderr
, "ss: wrong state name: %s\n", state
);
3726 static const struct option long_opts
[] = {
3727 { "numeric", 0, 0, 'n' },
3728 { "resolve", 0, 0, 'r' },
3729 { "options", 0, 0, 'o' },
3730 { "extended", 0, 0, 'e' },
3731 { "memory", 0, 0, 'm' },
3732 { "info", 0, 0, 'i' },
3733 { "processes", 0, 0, 'p' },
3734 { "bpf", 0, 0, 'b' },
3735 { "events", 0, 0, 'E' },
3736 { "dccp", 0, 0, 'd' },
3737 { "tcp", 0, 0, 't' },
3738 { "udp", 0, 0, 'u' },
3739 { "raw", 0, 0, 'w' },
3740 { "unix", 0, 0, 'x' },
3741 { "all", 0, 0, 'a' },
3742 { "listening", 0, 0, 'l' },
3743 { "ipv4", 0, 0, '4' },
3744 { "ipv6", 0, 0, '6' },
3745 { "packet", 0, 0, '0' },
3746 { "family", 1, 0, 'f' },
3747 { "socket", 1, 0, 'A' },
3748 { "query", 1, 0, 'A' },
3749 { "summary", 0, 0, 's' },
3750 { "diag", 1, 0, 'D' },
3751 { "filter", 1, 0, 'F' },
3752 { "version", 0, 0, 'V' },
3753 { "help", 0, 0, 'h' },
3754 { "context", 0, 0, 'Z' },
3755 { "contexts", 0, 0, 'z' },
3756 { "net", 1, 0, 'N' },
3757 { "kill", 0, 0, 'K' },
3758 { "no-header", 0, 0, 'H' },
3763 int main(int argc
, char *argv
[])
3768 const char *dump_tcpdiag
= NULL
;
3769 FILE *filter_fp
= NULL
;
3771 int state_filter
= 0;
3773 while ((ch
= getopt_long(argc
, argv
, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KH",
3774 long_opts
, NULL
)) != EOF
) {
3777 resolve_services
= 0;
3797 user_ent_hash_build();
3807 filter_db_set(¤t_filter
, DCCP_DB
);
3810 filter_db_set(¤t_filter
, TCP_DB
);
3813 filter_db_set(¤t_filter
, UDP_DB
);
3816 filter_db_set(¤t_filter
, RAW_DB
);
3819 filter_af_set(¤t_filter
, AF_UNIX
);
3822 state_filter
= SS_ALL
;
3825 state_filter
= (1 << SS_LISTEN
) | (1 << SS_CLOSE
);
3828 filter_af_set(¤t_filter
, AF_INET
);
3831 filter_af_set(¤t_filter
, AF_INET6
);
3834 filter_af_set(¤t_filter
, AF_PACKET
);
3837 if (strcmp(optarg
, "inet") == 0)
3838 filter_af_set(¤t_filter
, AF_INET
);
3839 else if (strcmp(optarg
, "inet6") == 0)
3840 filter_af_set(¤t_filter
, AF_INET6
);
3841 else if (strcmp(optarg
, "link") == 0)
3842 filter_af_set(¤t_filter
, AF_PACKET
);
3843 else if (strcmp(optarg
, "unix") == 0)
3844 filter_af_set(¤t_filter
, AF_UNIX
);
3845 else if (strcmp(optarg
, "netlink") == 0)
3846 filter_af_set(¤t_filter
, AF_NETLINK
);
3847 else if (strcmp(optarg
, "help") == 0)
3850 fprintf(stderr
, "ss: \"%s\" is invalid family\n",
3860 current_filter
.dbs
= 0;
3861 state_filter
= state_filter
?
3862 state_filter
: SS_CONN
;
3868 if ((p1
= strchr(p
, ',')) != NULL
)
3870 if (strcmp(p
, "all") == 0) {
3871 filter_default_dbs(¤t_filter
);
3872 } else if (strcmp(p
, "inet") == 0) {
3873 filter_db_set(¤t_filter
, UDP_DB
);
3874 filter_db_set(¤t_filter
, DCCP_DB
);
3875 filter_db_set(¤t_filter
, TCP_DB
);
3876 filter_db_set(¤t_filter
, RAW_DB
);
3877 } else if (strcmp(p
, "udp") == 0) {
3878 filter_db_set(¤t_filter
, UDP_DB
);
3879 } else if (strcmp(p
, "dccp") == 0) {
3880 filter_db_set(¤t_filter
, DCCP_DB
);
3881 } else if (strcmp(p
, "tcp") == 0) {
3882 filter_db_set(¤t_filter
, TCP_DB
);
3883 } else if (strcmp(p
, "raw") == 0) {
3884 filter_db_set(¤t_filter
, RAW_DB
);
3885 } else if (strcmp(p
, "unix") == 0) {
3886 filter_db_set(¤t_filter
, UNIX_ST_DB
);
3887 filter_db_set(¤t_filter
, UNIX_DG_DB
);
3888 filter_db_set(¤t_filter
, UNIX_SQ_DB
);
3889 } else if (strcasecmp(p
, "unix_stream") == 0 ||
3890 strcmp(p
, "u_str") == 0) {
3891 filter_db_set(¤t_filter
, UNIX_ST_DB
);
3892 } else if (strcasecmp(p
, "unix_dgram") == 0 ||
3893 strcmp(p
, "u_dgr") == 0) {
3894 filter_db_set(¤t_filter
, UNIX_DG_DB
);
3895 } else if (strcasecmp(p
, "unix_seqpacket") == 0 ||
3896 strcmp(p
, "u_seq") == 0) {
3897 filter_db_set(¤t_filter
, UNIX_SQ_DB
);
3898 } else if (strcmp(p
, "packet") == 0) {
3899 filter_db_set(¤t_filter
, PACKET_R_DB
);
3900 filter_db_set(¤t_filter
, PACKET_DG_DB
);
3901 } else if (strcmp(p
, "packet_raw") == 0 ||
3902 strcmp(p
, "p_raw") == 0) {
3903 filter_db_set(¤t_filter
, PACKET_R_DB
);
3904 } else if (strcmp(p
, "packet_dgram") == 0 ||
3905 strcmp(p
, "p_dgr") == 0) {
3906 filter_db_set(¤t_filter
, PACKET_DG_DB
);
3907 } else if (strcmp(p
, "netlink") == 0) {
3908 filter_db_set(¤t_filter
, NETLINK_DB
);
3910 fprintf(stderr
, "ss: \"%s\" is illegal socket table id\n", p
);
3921 dump_tcpdiag
= optarg
;
3925 fprintf(stderr
, "More than one filter file\n");
3928 if (optarg
[0] == '-')
3931 filter_fp
= fopen(optarg
, "r");
3933 perror("fopen filter file");
3939 printf("ss utility, iproute2-ss%s\n", SNAPSHOT
);
3944 if (is_selinux_enabled() <= 0) {
3945 fprintf(stderr
, "ss: SELinux is not enabled.\n");
3949 user_ent_hash_build();
3952 if (netns_switch(optarg
))
3956 current_filter
.kill
= 1;
3974 if (do_default
&& argc
== 0)
3979 if (strcmp(*argv
, "state") == 0) {
3983 state_filter
|= scan_state(*argv
);
3985 } else if (strcmp(*argv
, "exclude") == 0 ||
3986 strcmp(*argv
, "excl") == 0) {
3989 state_filter
= SS_ALL
;
3990 state_filter
&= ~scan_state(*argv
);
3999 state_filter
= state_filter
? state_filter
: SS_CONN
;
4000 filter_default_dbs(¤t_filter
);
4003 filter_states_set(¤t_filter
, state_filter
);
4004 filter_merge_defaults(¤t_filter
);
4006 if (resolve_services
&& resolve_hosts
&&
4007 (current_filter
.dbs
&(UNIX_DBM
|(1<<TCP_DB
)|(1<<UDP_DB
)|(1<<DCCP_DB
))))
4008 init_service_resolver();
4011 if (current_filter
.dbs
== 0) {
4012 fprintf(stderr
, "ss: no socket tables to show with such filter.\n");
4015 if (current_filter
.families
== 0) {
4016 fprintf(stderr
, "ss: no families to show with such filter.\n");
4019 if (current_filter
.states
== 0) {
4020 fprintf(stderr
, "ss: no socket states to show with such filter.\n");
4025 FILE *dump_fp
= stdout
;
4027 if (!(current_filter
.dbs
& (1<<TCP_DB
))) {
4028 fprintf(stderr
, "ss: tcpdiag dump requested and no tcp in filter.\n");
4031 if (dump_tcpdiag
[0] != '-') {
4032 dump_fp
= fopen(dump_tcpdiag
, "w");
4033 if (!dump_tcpdiag
) {
4034 perror("fopen dump file");
4038 inet_show_netlink(¤t_filter
, dump_fp
, IPPROTO_TCP
);
4043 if (ssfilter_parse(¤t_filter
.f
, argc
, argv
, filter_fp
))
4047 if (current_filter
.dbs
&(current_filter
.dbs
-1))
4051 if (current_filter
.states
&(current_filter
.states
-1))
4055 if (isatty(STDOUT_FILENO
)) {
4058 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
) != -1) {
4060 screen_width
= w
.ws_col
;
4064 addrp_width
= screen_width
;
4065 addrp_width
-= netid_width
+1;
4066 addrp_width
-= state_width
+1;
4069 if (addrp_width
&1) {
4072 else if (state_width
)
4079 serv_width
= resolve_services
? 7 : 5;
4081 if (addrp_width
< 15+serv_width
+1)
4082 addrp_width
= 15+serv_width
+1;
4084 addr_width
= addrp_width
- serv_width
- 1;
4088 printf("%-*s ", netid_width
, "Netid");
4090 printf("%-*s ", state_width
, "State");
4091 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
4094 /* Make enough space for the local/remote port field */
4099 printf("%*s:%-*s %*s:%-*s\n",
4100 addr_width
, "Local Address", serv_width
, "Port",
4101 addr_width
, "Peer Address", serv_width
, "Port");
4107 exit(handle_follow_request(¤t_filter
));
4109 if (current_filter
.dbs
& (1<<NETLINK_DB
))
4110 netlink_show(¤t_filter
);
4111 if (current_filter
.dbs
& PACKET_DBM
)
4112 packet_show(¤t_filter
);
4113 if (current_filter
.dbs
& UNIX_DBM
)
4114 unix_show(¤t_filter
);
4115 if (current_filter
.dbs
& (1<<RAW_DB
))
4116 raw_show(¤t_filter
);
4117 if (current_filter
.dbs
& (1<<UDP_DB
))
4118 udp_show(¤t_filter
);
4119 if (current_filter
.dbs
& (1<<TCP_DB
))
4120 tcp_show(¤t_filter
, IPPROTO_TCP
);
4121 if (current_filter
.dbs
& (1<<DCCP_DB
))
4122 tcp_show(¤t_filter
, IPPROTO_DCCP
);
4124 if (show_users
|| show_proc_ctx
|| show_sock_ctx
)