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>
34 #include "libnetlink.h"
35 #include "namespace.h"
38 #include <linux/tcp.h>
39 #include <linux/sock_diag.h>
40 #include <linux/inet_diag.h>
41 #include <linux/unix_diag.h>
42 #include <linux/netdevice.h> /* for MAX_ADDR_LEN */
43 #include <linux/filter.h>
44 #include <linux/packet_diag.h>
45 #include <linux/netlink_diag.h>
46 #include <linux/sctp.h>
47 #include <linux/vm_sockets_diag.h>
49 #define MAGIC_SEQ 123456
51 #define DIAG_REQUEST(_req, _r) \
53 struct nlmsghdr nlh; \
57 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
58 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
59 .nlmsg_seq = MAGIC_SEQ, \
60 .nlmsg_len = sizeof(_req), \
65 #include <selinux/selinux.h>
67 /* Stubs for SELinux functions */
68 static int is_selinux_enabled(void)
73 static int getpidcon(pid_t pid
, char **context
)
79 static int getfilecon(char *path
, char **context
)
85 static int security_get_initial_context(char *name
, char **context
)
93 int resolve_services
= 1;
94 int preferred_family
= AF_UNSPEC
;
112 static const char *TCP_PROTO
= "tcp";
113 static const char *SCTP_PROTO
= "sctp";
114 static const char *UDP_PROTO
= "udp";
115 static const char *RAW_PROTO
= "raw";
116 static const char *dg_proto
;
135 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
136 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
137 #define ALL_DB ((1<<MAX_DB)-1)
138 #define INET_L4_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB))
139 #define INET_DBM (INET_L4_DBM | (1<<RAW_DB))
140 #define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB))
159 SCTP_STATE_CLOSED
= 0,
160 SCTP_STATE_COOKIE_WAIT
= 1,
161 SCTP_STATE_COOKIE_ECHOED
= 2,
162 SCTP_STATE_ESTABLISHED
= 3,
163 SCTP_STATE_SHUTDOWN_PENDING
= 4,
164 SCTP_STATE_SHUTDOWN_SENT
= 5,
165 SCTP_STATE_SHUTDOWN_RECEIVED
= 6,
166 SCTP_STATE_SHUTDOWN_ACK_SENT
= 7,
169 #define SS_ALL ((1 << SS_MAX) - 1)
170 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
172 #include "ssfilter.h"
182 #define FAMILY_MASK(family) ((uint64_t)1 << (family))
184 static const struct filter default_dbs
[MAX_DB
] = {
187 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
191 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
194 .states
= (1 << SS_ESTABLISHED
),
195 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
198 .states
= (1 << SS_ESTABLISHED
),
199 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
202 .states
= (1 << SS_CLOSE
),
203 .families
= FAMILY_MASK(AF_UNIX
),
207 .families
= FAMILY_MASK(AF_UNIX
),
211 .families
= FAMILY_MASK(AF_UNIX
),
214 .states
= (1 << SS_CLOSE
),
215 .families
= FAMILY_MASK(AF_PACKET
),
218 .states
= (1 << SS_CLOSE
),
219 .families
= FAMILY_MASK(AF_PACKET
),
222 .states
= (1 << SS_CLOSE
),
223 .families
= FAMILY_MASK(AF_NETLINK
),
227 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
231 .families
= FAMILY_MASK(AF_VSOCK
),
235 .families
= FAMILY_MASK(AF_VSOCK
),
239 static const struct filter default_afs
[AF_MAX
] = {
254 .states
= (1 << SS_CLOSE
),
257 .dbs
= (1 << NETLINK_DB
),
258 .states
= (1 << SS_CLOSE
),
266 static int do_default
= 1;
267 static struct filter current_filter
;
269 static void filter_db_set(struct filter
*f
, int db
)
271 f
->states
|= default_dbs
[db
].states
;
276 static void filter_af_set(struct filter
*f
, int af
)
278 f
->states
|= default_afs
[af
].states
;
279 f
->families
|= FAMILY_MASK(af
);
281 preferred_family
= af
;
284 static int filter_af_get(struct filter
*f
, int af
)
286 return !!(f
->families
& FAMILY_MASK(af
));
289 static void filter_default_dbs(struct filter
*f
)
291 filter_db_set(f
, UDP_DB
);
292 filter_db_set(f
, DCCP_DB
);
293 filter_db_set(f
, TCP_DB
);
294 filter_db_set(f
, RAW_DB
);
295 filter_db_set(f
, UNIX_ST_DB
);
296 filter_db_set(f
, UNIX_DG_DB
);
297 filter_db_set(f
, UNIX_SQ_DB
);
298 filter_db_set(f
, PACKET_R_DB
);
299 filter_db_set(f
, PACKET_DG_DB
);
300 filter_db_set(f
, NETLINK_DB
);
301 filter_db_set(f
, SCTP_DB
);
302 filter_db_set(f
, VSOCK_ST_DB
);
303 filter_db_set(f
, VSOCK_DG_DB
);
306 static void filter_states_set(struct filter
*f
, int states
)
312 static void filter_merge_defaults(struct filter
*f
)
317 for (db
= 0; db
< MAX_DB
; db
++) {
318 if (!(f
->dbs
& (1 << db
)))
321 if (!(default_dbs
[db
].families
& f
->families
))
322 f
->families
|= default_dbs
[db
].families
;
324 for (af
= 0; af
< AF_MAX
; af
++) {
325 if (!(f
->families
& FAMILY_MASK(af
)))
328 if (!(default_afs
[af
].dbs
& f
->dbs
))
329 f
->dbs
|= default_afs
[af
].dbs
;
333 static FILE *generic_proc_open(const char *env
, const char *name
)
335 const char *p
= getenv(env
);
339 p
= getenv("PROC_ROOT") ? : "/proc";
340 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
344 return fopen(p
, "r");
346 #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp")
347 #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6")
348 #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp")
349 #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6")
350 #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw")
351 #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6")
352 #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix")
353 #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \
355 #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \
357 #define slabinfo_open() generic_proc_open("PROC_SLABINFO", "slabinfo")
358 #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \
360 #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \
362 #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp")
363 #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \
364 "sys/net/ipv4/ip_local_port_range")
367 struct user_ent
*next
;
376 #define USER_ENT_HASH_SIZE 256
377 struct user_ent
*user_ent_hash
[USER_ENT_HASH_SIZE
];
379 static int user_ent_hashfn(unsigned int ino
)
381 int val
= (ino
>> 24) ^ (ino
>> 16) ^ (ino
>> 8) ^ ino
;
383 return val
& (USER_ENT_HASH_SIZE
- 1);
386 static void user_ent_add(unsigned int ino
, char *process
,
391 struct user_ent
*p
, **pp
;
393 p
= malloc(sizeof(struct user_ent
));
395 fprintf(stderr
, "ss: failed to malloc buffer\n");
402 p
->process
= strdup(process
);
403 p
->process_ctx
= strdup(proc_ctx
);
404 p
->socket_ctx
= strdup(sock_ctx
);
406 pp
= &user_ent_hash
[user_ent_hashfn(ino
)];
411 static void user_ent_destroy(void)
413 struct user_ent
*p
, *p_next
;
416 while (cnt
!= USER_ENT_HASH_SIZE
) {
417 p
= user_ent_hash
[cnt
];
420 free(p
->process_ctx
);
430 static void user_ent_hash_build(void)
432 const char *root
= getenv("PROC_ROOT") ? : "/proc/";
439 const char *no_ctx
= "unavailable";
440 static int user_ent_hash_build_init
;
442 /* If show_users & show_proc_ctx set only do this once */
443 if (user_ent_hash_build_init
!= 0)
446 user_ent_hash_build_init
= 1;
448 strlcpy(name
, root
, sizeof(name
));
450 if (strlen(name
) == 0 || name
[strlen(name
)-1] != '/')
453 nameoff
= strlen(name
);
459 while ((d
= readdir(dir
)) != NULL
) {
467 if (sscanf(d
->d_name
, "%d%c", &pid
, &crap
) != 1)
470 if (getpidcon(pid
, &pid_context
) != 0)
471 pid_context
= strdup(no_ctx
);
473 snprintf(name
+ nameoff
, sizeof(name
) - nameoff
, "%d/fd/", pid
);
475 if ((dir1
= opendir(name
)) == NULL
) {
483 while ((d1
= readdir(dir1
)) != NULL
) {
484 const char *pattern
= "socket:[";
491 if (sscanf(d1
->d_name
, "%d%c", &fd
, &crap
) != 1)
494 snprintf(name
+pos
, sizeof(name
) - pos
, "%d", fd
);
496 link_len
= readlink(name
, lnk
, sizeof(lnk
)-1);
499 lnk
[link_len
] = '\0';
501 if (strncmp(lnk
, pattern
, strlen(pattern
)))
504 sscanf(lnk
, "socket:[%u]", &ino
);
506 snprintf(tmp
, sizeof(tmp
), "%s/%d/fd/%s",
507 root
, pid
, d1
->d_name
);
509 if (getfilecon(tmp
, &sock_context
) <= 0)
510 sock_context
= strdup(no_ctx
);
515 snprintf(tmp
, sizeof(tmp
), "%s/%d/stat",
517 if ((fp
= fopen(tmp
, "r")) != NULL
) {
518 if (fscanf(fp
, "%*d (%[^)])", p
) < 1)
523 user_ent_add(ino
, p
, pid
, fd
,
524 pid_context
, sock_context
);
539 #define ENTRY_BUF_SIZE 512
540 static int find_entry(unsigned int ino
, char **buf
, int type
)
546 int len
, new_buf_len
;
553 p
= user_ent_hash
[user_ent_hashfn(ino
)];
560 ptr
= *buf
+ buf_used
;
563 len
= snprintf(ptr
, buf_len
- buf_used
,
564 "(\"%s\",pid=%d,fd=%d),",
565 p
->process
, p
->pid
, p
->fd
);
568 len
= snprintf(ptr
, buf_len
- buf_used
,
569 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
571 p
->process_ctx
, p
->fd
);
574 len
= snprintf(ptr
, buf_len
- buf_used
,
575 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
577 p
->process_ctx
, p
->fd
,
581 fprintf(stderr
, "ss: invalid type: %d\n", type
);
585 if (len
< 0 || len
>= buf_len
- buf_used
) {
586 new_buf_len
= buf_len
+ ENTRY_BUF_SIZE
;
587 new_buf
= realloc(*buf
, new_buf_len
);
589 fprintf(stderr
, "ss: failed to malloc buffer\n");
593 buf_len
= new_buf_len
;
605 ptr
= *buf
+ buf_used
;
611 /* Get stats from slab */
621 static struct slabstat slabstat
;
623 static int get_slabstat(struct slabstat
*s
)
628 static int slabstat_valid
;
629 static const char * const slabstat_ids
[] = {
640 memset(s
, 0, sizeof(*s
));
642 fp
= slabinfo_open();
646 cnt
= sizeof(*s
)/sizeof(int);
648 if (!fgets(buf
, sizeof(buf
), fp
)) {
652 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
655 for (i
= 0; i
< ARRAY_SIZE(slabstat_ids
); i
++) {
656 if (memcmp(buf
, slabstat_ids
[i
], strlen(slabstat_ids
[i
])) == 0) {
657 sscanf(buf
, "%*s%d", ((int *)s
) + i
);
672 static unsigned long long cookie_sk_get(const uint32_t *cookie
)
674 return (((unsigned long long)cookie
[1] << 31) << 1) | cookie
[0];
677 static const char *sctp_sstate_name
[] = {
678 [SCTP_STATE_CLOSED
] = "CLOSED",
679 [SCTP_STATE_COOKIE_WAIT
] = "COOKIE_WAIT",
680 [SCTP_STATE_COOKIE_ECHOED
] = "COOKIE_ECHOED",
681 [SCTP_STATE_ESTABLISHED
] = "ESTAB",
682 [SCTP_STATE_SHUTDOWN_PENDING
] = "SHUTDOWN_PENDING",
683 [SCTP_STATE_SHUTDOWN_SENT
] = "SHUTDOWN_SENT",
684 [SCTP_STATE_SHUTDOWN_RECEIVED
] = "SHUTDOWN_RECEIVED",
685 [SCTP_STATE_SHUTDOWN_ACK_SENT
] = "ACK_SENT",
689 struct sockstat
*next
;
703 unsigned long long sk
;
710 unsigned int ce_state
;
720 unsigned int timeout
;
723 double rto
, ato
, rtt
, rttvar
;
724 int qack
, ssthresh
, backoff
;
732 unsigned int lastsnd
;
733 unsigned int lastrcv
;
734 unsigned int lastack
;
736 double pacing_rate_max
;
737 double delivery_rate
;
738 unsigned long long bytes_acked
;
739 unsigned long long bytes_received
;
740 unsigned int segs_out
;
741 unsigned int segs_in
;
742 unsigned int data_segs_out
;
743 unsigned int data_segs_in
;
744 unsigned int unacked
;
745 unsigned int retrans
;
746 unsigned int retrans_total
;
749 unsigned int fackets
;
750 unsigned int reordering
;
751 unsigned int not_sent
;
755 unsigned long long busy_time
;
756 unsigned long long rwnd_limited
;
757 unsigned long long sndbuf_limited
;
761 bool has_ecnseen_opt
;
762 bool has_fastopen_opt
;
765 struct dctcpstat
*dctcp
;
766 struct tcp_bbr_info
*bbr_info
;
769 /* SCTP assocs share the same inode number with their parent endpoint. So if we
770 * have seen the inode number before, it must be an assoc instead of the next
772 static bool is_sctp_assoc(struct sockstat
*s
, const char *sock_name
)
774 if (strcmp(sock_name
, "sctp"))
776 if (!sctp_ino
|| sctp_ino
!= s
->ino
)
781 static const char *unix_netid_name(int type
)
794 static const char *proto_name(int protocol
)
812 static const char *vsock_netid_name(int type
)
824 static void sock_state_print(struct sockstat
*s
)
826 const char *sock_name
;
827 static const char * const sstate_name
[] = {
829 [SS_ESTABLISHED
] = "ESTAB",
830 [SS_SYN_SENT
] = "SYN-SENT",
831 [SS_SYN_RECV
] = "SYN-RECV",
832 [SS_FIN_WAIT1
] = "FIN-WAIT-1",
833 [SS_FIN_WAIT2
] = "FIN-WAIT-2",
834 [SS_TIME_WAIT
] = "TIME-WAIT",
835 [SS_CLOSE
] = "UNCONN",
836 [SS_CLOSE_WAIT
] = "CLOSE-WAIT",
837 [SS_LAST_ACK
] = "LAST-ACK",
838 [SS_LISTEN
] = "LISTEN",
839 [SS_CLOSING
] = "CLOSING",
842 switch (s
->local
.family
) {
844 sock_name
= unix_netid_name(s
->type
);
848 sock_name
= proto_name(s
->type
);
851 sock_name
= s
->type
== SOCK_RAW
? "p_raw" : "p_dgr";
857 sock_name
= vsock_netid_name(s
->type
);
860 sock_name
= "unknown";
864 printf("%-*s ", netid_width
,
865 is_sctp_assoc(s
, sock_name
) ? "" : sock_name
);
867 if (is_sctp_assoc(s
, sock_name
))
868 printf("`- %-*s ", state_width
- 3,
869 sctp_sstate_name
[s
->state
]);
871 printf("%-*s ", state_width
, sstate_name
[s
->state
]);
874 printf("%-6d %-6d ", s
->rq
, s
->wq
);
877 static void sock_details_print(struct sockstat
*s
)
880 printf(" uid:%u", s
->uid
);
882 printf(" ino:%u", s
->ino
);
883 printf(" sk:%llx", s
->sk
);
886 printf(" fwmark:0x%x", s
->mark
);
889 static void sock_addr_print_width(int addr_len
, const char *addr
, char *delim
,
890 int port_len
, const char *port
, const char *ifname
)
893 printf("%*s%%%s%s%-*s ", addr_len
, addr
, ifname
, delim
,
896 printf("%*s%s%-*s ", addr_len
, addr
, delim
, port_len
, port
);
900 static void sock_addr_print(const char *addr
, char *delim
, const char *port
,
903 sock_addr_print_width(addr_width
, addr
, delim
, serv_width
, port
, ifname
);
906 static const char *print_ms_timer(unsigned int timeout
)
909 int secs
, msecs
, minutes
;
914 msecs
= timeout
%1000;
918 snprintf(buf
, sizeof(buf
)-16, "%dmin", minutes
);
925 sprintf(buf
+strlen(buf
), "%d%s", secs
, msecs
? "." : "sec");
928 sprintf(buf
+strlen(buf
), "%03dms", msecs
);
939 struct scache
*rlist
;
941 static void init_service_resolver(void)
944 FILE *fp
= popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
949 if (!fgets(buf
, sizeof(buf
), fp
)) {
953 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
954 unsigned int progn
, port
;
955 char proto
[128], prog
[128] = "rpc.";
958 if (sscanf(buf
, "%u %*d %s %u %s",
959 &progn
, proto
, &port
, prog
+4) != 4)
962 if (!(c
= malloc(sizeof(*c
))))
966 c
->name
= strdup(prog
);
967 if (strcmp(proto
, TCP_PROTO
) == 0)
968 c
->proto
= TCP_PROTO
;
969 else if (strcmp(proto
, UDP_PROTO
) == 0)
970 c
->proto
= UDP_PROTO
;
971 else if (strcmp(proto
, SCTP_PROTO
) == 0)
972 c
->proto
= SCTP_PROTO
;
981 /* Even do not try default linux ephemeral port ranges:
982 * default /etc/services contains so much of useless crap
983 * wouldbe "allocated" to this area that resolution
984 * is really harmful. I shrug each time when seeing
985 * "socks" or "cfinger" in dumps.
987 static int is_ephemeral(int port
)
989 static int min
= 0, max
;
992 FILE *f
= ephemeral_ports_open();
994 if (!f
|| fscanf(f
, "%d %d", &min
, &max
) < 2) {
1001 return port
>= min
&& port
<= max
;
1005 static const char *__resolve_service(int port
)
1009 for (c
= rlist
; c
; c
= c
->next
) {
1010 if (c
->port
== port
&& c
->proto
== dg_proto
)
1014 if (!is_ephemeral(port
)) {
1015 static int notfirst
;
1022 se
= getservbyport(htons(port
), dg_proto
);
1030 #define SCACHE_BUCKETS 1024
1031 static struct scache
*cache_htab
[SCACHE_BUCKETS
];
1033 static const char *resolve_service(int port
)
1035 static char buf
[128];
1046 if (!resolve_services
)
1049 if (dg_proto
== RAW_PROTO
)
1050 return inet_proto_n2a(port
, buf
, sizeof(buf
));
1053 hash
= (port
^(((unsigned long)dg_proto
)>>2)) % SCACHE_BUCKETS
;
1055 for (c
= cache_htab
[hash
]; c
; c
= c
->next
) {
1056 if (c
->port
== port
&& c
->proto
== dg_proto
)
1060 c
= malloc(sizeof(*c
));
1063 res
= __resolve_service(port
);
1065 c
->name
= res
? strdup(res
) : NULL
;
1066 c
->proto
= dg_proto
;
1067 c
->next
= cache_htab
[hash
];
1068 cache_htab
[hash
] = c
;
1075 sprintf(buf
, "%u", port
);
1079 static void inet_addr_print(const inet_prefix
*a
, int port
, unsigned int ifindex
)
1082 const char *ap
= buf
;
1083 int est_len
= addr_width
;
1084 const char *ifname
= NULL
;
1086 if (a
->family
== AF_INET
) {
1087 if (a
->data
[0] == 0) {
1091 ap
= format_host(AF_INET
, 4, a
->data
);
1094 if (!memcmp(a
->data
, &in6addr_any
, sizeof(in6addr_any
))) {
1098 ap
= format_host(a
->family
, 16, a
->data
);
1100 /* Numeric IPv6 addresses should be bracketed */
1101 if (strchr(ap
, ':')) {
1102 snprintf(buf
, sizeof(buf
),
1107 est_len
= strlen(ap
);
1108 if (est_len
<= addr_width
)
1109 est_len
= addr_width
;
1111 est_len
= addr_width
+ ((est_len
-addr_width
+3)/4)*4;
1116 ifname
= ll_index_to_name(ifindex
);
1117 est_len
-= strlen(ifname
) + 1; /* +1 for percent char */
1122 sock_addr_print_width(est_len
, ap
, ":", serv_width
, resolve_service(port
),
1132 struct aafilter
*next
;
1135 static int inet2_addr_match(const inet_prefix
*a
, const inet_prefix
*p
,
1138 if (!inet_addr_match(a
, p
, plen
))
1141 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1142 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1144 if (p
->family
== AF_INET
&& a
->family
== AF_INET6
) {
1145 if (a
->data
[0] == 0 && a
->data
[1] == 0 &&
1146 a
->data
[2] == htonl(0xffff)) {
1147 inet_prefix tmp
= *a
;
1149 tmp
.data
[0] = a
->data
[3];
1150 return inet_addr_match(&tmp
, p
, plen
);
1156 static int unix_match(const inet_prefix
*a
, const inet_prefix
*p
)
1158 char *addr
, *pattern
;
1160 memcpy(&addr
, a
->data
, sizeof(addr
));
1161 memcpy(&pattern
, p
->data
, sizeof(pattern
));
1162 if (pattern
== NULL
)
1166 return !fnmatch(pattern
, addr
, 0);
1169 static int run_ssfilter(struct ssfilter
*f
, struct sockstat
*s
)
1174 if (s
->local
.family
== AF_UNIX
) {
1177 memcpy(&p
, s
->local
.data
, sizeof(p
));
1178 return p
== NULL
|| (p
[0] == '@' && strlen(p
) == 6 &&
1179 strspn(p
+1, "0123456789abcdef") == 5);
1181 if (s
->local
.family
== AF_PACKET
)
1182 return s
->lport
== 0 && s
->local
.data
[0] == 0;
1183 if (s
->local
.family
== AF_NETLINK
)
1184 return s
->lport
< 0;
1185 if (s
->local
.family
== AF_VSOCK
)
1186 return s
->lport
> 1023;
1188 return is_ephemeral(s
->lport
);
1192 struct aafilter
*a
= (void *)f
->pred
;
1194 if (a
->addr
.family
== AF_UNIX
)
1195 return unix_match(&s
->remote
, &a
->addr
);
1196 if (a
->port
!= -1 && a
->port
!= s
->rport
)
1198 if (a
->addr
.bitlen
) {
1200 if (!inet2_addr_match(&s
->remote
, &a
->addr
, a
->addr
.bitlen
))
1202 } while ((a
= a
->next
) != NULL
);
1209 struct aafilter
*a
= (void *)f
->pred
;
1211 if (a
->addr
.family
== AF_UNIX
)
1212 return unix_match(&s
->local
, &a
->addr
);
1213 if (a
->port
!= -1 && a
->port
!= s
->lport
)
1215 if (a
->addr
.bitlen
) {
1217 if (!inet2_addr_match(&s
->local
, &a
->addr
, a
->addr
.bitlen
))
1219 } while ((a
= a
->next
) != NULL
);
1226 struct aafilter
*a
= (void *)f
->pred
;
1228 return s
->rport
>= a
->port
;
1232 struct aafilter
*a
= (void *)f
->pred
;
1234 return s
->rport
<= a
->port
;
1238 struct aafilter
*a
= (void *)f
->pred
;
1240 return s
->lport
>= a
->port
;
1244 struct aafilter
*a
= (void *)f
->pred
;
1246 return s
->lport
<= a
->port
;
1250 struct aafilter
*a
= (void *)f
->pred
;
1252 return s
->iface
== a
->iface
;
1256 struct aafilter
*a
= (void *)f
->pred
;
1258 return (s
->mark
& a
->mask
) == a
->mark
;
1260 /* Yup. It is recursion. Sorry. */
1262 return run_ssfilter(f
->pred
, s
) && run_ssfilter(f
->post
, s
);
1264 return run_ssfilter(f
->pred
, s
) || run_ssfilter(f
->post
, s
);
1266 return !run_ssfilter(f
->pred
, s
);
1272 /* Relocate external jumps by reloc. */
1273 static void ssfilter_patch(char *a
, int len
, int reloc
)
1276 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)a
;
1278 if (op
->no
== len
+4)
1287 static int ssfilter_bytecompile(struct ssfilter
*f
, char **bytecode
)
1292 if (!(*bytecode
= malloc(4))) abort();
1293 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_AUTO
, 4, 8 };
1299 struct aafilter
*a
= (void *)f
->pred
;
1302 int code
= (f
->type
== SSF_DCOND
? INET_DIAG_BC_D_COND
: INET_DIAG_BC_S_COND
);
1305 for (b
= a
; b
; b
= b
->next
) {
1306 len
+= 4 + sizeof(struct inet_diag_hostcond
);
1307 if (a
->addr
.family
== AF_INET6
)
1314 if (!(ptr
= malloc(len
))) abort();
1316 for (b
= a
; b
; b
= b
->next
) {
1317 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)ptr
;
1318 int alen
= (a
->addr
.family
== AF_INET6
? 16 : 4);
1319 int oplen
= alen
+ 4 + sizeof(struct inet_diag_hostcond
);
1320 struct inet_diag_hostcond
*cond
= (struct inet_diag_hostcond
*)(ptr
+4);
1322 *op
= (struct inet_diag_bc_op
){ code
, oplen
, oplen
+4 };
1323 cond
->family
= a
->addr
.family
;
1324 cond
->port
= a
->port
;
1325 cond
->prefix_len
= a
->addr
.bitlen
;
1326 memcpy(cond
->addr
, a
->addr
.data
, alen
);
1329 op
= (struct inet_diag_bc_op
*)ptr
;
1330 *op
= (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, len
- (ptr
-*bytecode
)};
1334 return ptr
- *bytecode
;
1338 struct aafilter
*x
= (void *)f
->pred
;
1340 if (!(*bytecode
= malloc(8))) abort();
1341 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_GE
, 8, 12 };
1342 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1347 struct aafilter
*x
= (void *)f
->pred
;
1349 if (!(*bytecode
= malloc(8))) abort();
1350 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_LE
, 8, 12 };
1351 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1356 struct aafilter
*x
= (void *)f
->pred
;
1358 if (!(*bytecode
= malloc(8))) abort();
1359 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_GE
, 8, 12 };
1360 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1365 struct aafilter
*x
= (void *)f
->pred
;
1367 if (!(*bytecode
= malloc(8))) abort();
1368 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_LE
, 8, 12 };
1369 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1375 char *a1
= NULL
, *a2
= NULL
, *a
;
1378 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1379 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1385 if (!(a
= malloc(l1
+l2
))) abort();
1387 memcpy(a
+l1
, a2
, l2
);
1389 ssfilter_patch(a
, l1
, l2
);
1395 char *a1
= NULL
, *a2
= NULL
, *a
;
1398 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1399 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1405 if (!(a
= malloc(l1
+l2
+4))) abort();
1407 memcpy(a
+l1
+4, a2
, l2
);
1409 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, l2
+4 };
1415 char *a1
= NULL
, *a
;
1418 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1423 if (!(a
= malloc(l1
+4))) abort();
1426 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, 8 };
1432 /* bytecompile for SSF_DEVCOND not supported yet */
1437 struct aafilter
*a
= (void *)f
->pred
;
1439 struct inet_diag_bc_op op
;
1440 struct inet_diag_markcond cond
;
1442 int inslen
= sizeof(struct instr
);
1444 if (!(*bytecode
= malloc(inslen
))) abort();
1445 ((struct instr
*)*bytecode
)[0] = (struct instr
) {
1446 { INET_DIAG_BC_MARK_COND
, inslen
, inslen
+ 4 },
1447 { a
->mark
, a
->mask
},
1457 static int remember_he(struct aafilter
*a
, struct hostent
*he
)
1459 char **ptr
= he
->h_addr_list
;
1463 if (he
->h_addrtype
== AF_INET
)
1465 else if (he
->h_addrtype
== AF_INET6
)
1471 struct aafilter
*b
= a
;
1473 if (a
->addr
.bitlen
) {
1474 if ((b
= malloc(sizeof(*b
))) == NULL
)
1479 memcpy(b
->addr
.data
, *ptr
, len
);
1480 b
->addr
.bytelen
= len
;
1481 b
->addr
.bitlen
= len
*8;
1482 b
->addr
.family
= he
->h_addrtype
;
1489 static int get_dns_host(struct aafilter
*a
, const char *addr
, int fam
)
1491 static int notfirst
;
1500 he
= gethostbyname2(addr
, fam
== AF_UNSPEC
? AF_INET
: fam
);
1502 cnt
= remember_he(a
, he
);
1503 if (fam
== AF_UNSPEC
) {
1504 he
= gethostbyname2(addr
, AF_INET6
);
1506 cnt
+= remember_he(a
, he
);
1511 static int xll_initted
;
1513 static void xll_init(void)
1515 struct rtnl_handle rth
;
1517 if (rtnl_open(&rth
, 0) < 0)
1525 static const char *xll_index_to_name(int index
)
1529 return ll_index_to_name(index
);
1532 static int xll_name_to_index(const char *dev
)
1536 return ll_name_to_index(dev
);
1539 void *parse_devcond(char *name
)
1541 struct aafilter a
= { .iface
= 0 };
1542 struct aafilter
*res
;
1544 a
.iface
= xll_name_to_index(name
);
1549 n
= strtoul(name
, &end
, 0);
1550 if (!end
|| end
== name
|| *end
|| n
> UINT_MAX
)
1556 res
= malloc(sizeof(*res
));
1562 static void vsock_set_inet_prefix(inet_prefix
*a
, __u32 cid
)
1565 .bytelen
= sizeof(cid
),
1568 memcpy(a
->data
, &cid
, sizeof(cid
));
1571 void *parse_hostcond(char *addr
, bool is_port
)
1574 struct aafilter a
= { .port
= -1 };
1575 struct aafilter
*res
;
1576 int fam
= preferred_family
;
1577 struct filter
*f
= ¤t_filter
;
1579 if (fam
== AF_UNIX
|| strncmp(addr
, "unix:", 5) == 0) {
1582 a
.addr
.family
= AF_UNIX
;
1583 if (strncmp(addr
, "unix:", 5) == 0)
1586 a
.addr
.bitlen
= 8*strlen(p
);
1587 memcpy(a
.addr
.data
, &p
, sizeof(p
));
1592 if (fam
== AF_PACKET
|| strncmp(addr
, "link:", 5) == 0) {
1593 a
.addr
.family
= AF_PACKET
;
1595 if (strncmp(addr
, "link:", 5) == 0)
1597 port
= strchr(addr
, ':');
1600 if (port
[1] && strcmp(port
+1, "*")) {
1601 if (get_integer(&a
.port
, port
+1, 0)) {
1602 if ((a
.port
= xll_name_to_index(port
+1)) <= 0)
1607 if (addr
[0] && strcmp(addr
, "*")) {
1611 if (ll_proto_a2n(&tmp
, addr
))
1613 a
.addr
.data
[0] = ntohs(tmp
);
1619 if (fam
== AF_NETLINK
|| strncmp(addr
, "netlink:", 8) == 0) {
1620 a
.addr
.family
= AF_NETLINK
;
1622 if (strncmp(addr
, "netlink:", 8) == 0)
1624 port
= strchr(addr
, ':');
1627 if (port
[1] && strcmp(port
+1, "*")) {
1628 if (get_integer(&a
.port
, port
+1, 0)) {
1629 if (strcmp(port
+1, "kernel") == 0)
1636 if (addr
[0] && strcmp(addr
, "*")) {
1638 if (nl_proto_a2n(&a
.addr
.data
[0], addr
) == -1)
1645 if (fam
== AF_VSOCK
|| strncmp(addr
, "vsock:", 6) == 0) {
1646 __u32 cid
= ~(__u32
)0;
1648 a
.addr
.family
= AF_VSOCK
;
1649 if (strncmp(addr
, "vsock:", 6) == 0)
1655 port
= strchr(addr
, ':');
1662 if (port
&& strcmp(port
, "*") &&
1663 get_u32((__u32
*)&a
.port
, port
, 0))
1666 if (addr
[0] && strcmp(addr
, "*")) {
1668 if (get_u32(&cid
, addr
, 0))
1671 vsock_set_inet_prefix(&a
.addr
, cid
);
1676 if (fam
== AF_INET
|| !strncmp(addr
, "inet:", 5)) {
1678 if (!strncmp(addr
, "inet:", 5))
1680 } else if (fam
== AF_INET6
|| !strncmp(addr
, "inet6:", 6)) {
1682 if (!strncmp(addr
, "inet6:", 6))
1686 /* URL-like literal [] */
1687 if (addr
[0] == '[') {
1689 if ((port
= strchr(addr
, ']')) == NULL
)
1692 } else if (addr
[0] == '*') {
1695 port
= strrchr(strchr(addr
, '/') ? : addr
, ':');
1701 if (port
&& *port
) {
1705 if (*port
&& *port
!= '*') {
1706 if (get_integer(&a
.port
, port
, 0)) {
1707 struct servent
*se1
= NULL
;
1708 struct servent
*se2
= NULL
;
1710 if (current_filter
.dbs
&(1<<UDP_DB
))
1711 se1
= getservbyname(port
, UDP_PROTO
);
1712 if (current_filter
.dbs
&(1<<TCP_DB
))
1713 se2
= getservbyname(port
, TCP_PROTO
);
1714 if (se1
&& se2
&& se1
->s_port
!= se2
->s_port
) {
1715 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
1721 a
.port
= ntohs(se1
->s_port
);
1725 for (s
= rlist
; s
; s
= s
->next
) {
1726 if ((s
->proto
== UDP_PROTO
&&
1727 (current_filter
.dbs
&(1<<UDP_DB
))) ||
1728 (s
->proto
== TCP_PROTO
&&
1729 (current_filter
.dbs
&(1<<TCP_DB
)))) {
1730 if (s
->name
&& strcmp(s
->name
, port
) == 0) {
1731 if (a
.port
> 0 && a
.port
!= s
->port
) {
1732 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
1740 fprintf(stderr
, "Error: \"%s\" does not look like a port.\n", port
);
1747 if (!is_port
&& *addr
&& *addr
!= '*') {
1748 if (get_prefix_1(&a
.addr
, addr
, fam
)) {
1749 if (get_dns_host(&a
, addr
, fam
)) {
1750 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", addr
);
1757 if (fam
!= AF_UNSPEC
) {
1758 int states
= f
->states
;
1760 filter_af_set(f
, fam
);
1761 filter_states_set(f
, states
);
1764 res
= malloc(sizeof(*res
));
1766 memcpy(res
, &a
, sizeof(a
));
1770 void *parse_markmask(const char *markmask
)
1772 struct aafilter a
, *res
;
1774 if (strchr(markmask
, '/')) {
1775 if (sscanf(markmask
, "%i/%i", &a
.mark
, &a
.mask
) != 2)
1778 a
.mask
= 0xffffffff;
1779 if (sscanf(markmask
, "%i", &a
.mark
) != 1)
1783 res
= malloc(sizeof(*res
));
1785 memcpy(res
, &a
, sizeof(a
));
1789 static void proc_ctx_print(struct sockstat
*s
)
1793 if (show_proc_ctx
|| show_sock_ctx
) {
1794 if (find_entry(s
->ino
, &buf
,
1795 (show_proc_ctx
& show_sock_ctx
) ?
1796 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
1797 printf(" users:(%s)", buf
);
1800 } else if (show_users
) {
1801 if (find_entry(s
->ino
, &buf
, USERS
) > 0) {
1802 printf(" users:(%s)", buf
);
1808 static void inet_stats_print(struct sockstat
*s
)
1810 sock_state_print(s
);
1812 inet_addr_print(&s
->local
, s
->lport
, s
->iface
);
1813 inet_addr_print(&s
->remote
, s
->rport
, 0);
1818 static int proc_parse_inet_addr(char *loc
, char *rem
, int family
, struct
1821 s
->local
.family
= s
->remote
.family
= family
;
1822 if (family
== AF_INET
) {
1823 sscanf(loc
, "%x:%x", s
->local
.data
, (unsigned *)&s
->lport
);
1824 sscanf(rem
, "%x:%x", s
->remote
.data
, (unsigned *)&s
->rport
);
1825 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
1828 sscanf(loc
, "%08x%08x%08x%08x:%x",
1834 sscanf(rem
, "%08x%08x%08x%08x:%x",
1840 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
1846 static int proc_inet_split_line(char *line
, char **loc
, char **rem
, char **data
)
1850 if ((p
= strchr(line
, ':')) == NULL
)
1854 if ((p
= strchr(*loc
, ':')) == NULL
)
1859 if ((p
= strchr(*rem
, ':')) == NULL
)
1867 static char *sprint_bw(char *buf
, double bw
)
1870 sprintf(buf
, "%.1fM", bw
/ 1000000.);
1871 else if (bw
> 1000.)
1872 sprintf(buf
, "%.1fK", bw
/ 1000.);
1874 sprintf(buf
, "%g", bw
);
1879 static void sctp_stats_print(struct sctp_info
*s
)
1882 printf(" tag:%x", s
->sctpi_tag
);
1884 printf(" state:%s", sctp_sstate_name
[s
->sctpi_state
]);
1886 printf(" rwnd:%d", s
->sctpi_rwnd
);
1887 if (s
->sctpi_unackdata
)
1888 printf(" unackdata:%d", s
->sctpi_unackdata
);
1889 if (s
->sctpi_penddata
)
1890 printf(" penddata:%d", s
->sctpi_penddata
);
1891 if (s
->sctpi_instrms
)
1892 printf(" instrms:%d", s
->sctpi_instrms
);
1893 if (s
->sctpi_outstrms
)
1894 printf(" outstrms:%d", s
->sctpi_outstrms
);
1895 if (s
->sctpi_inqueue
)
1896 printf(" inqueue:%d", s
->sctpi_inqueue
);
1897 if (s
->sctpi_outqueue
)
1898 printf(" outqueue:%d", s
->sctpi_outqueue
);
1899 if (s
->sctpi_overall_error
)
1900 printf(" overerr:%d", s
->sctpi_overall_error
);
1901 if (s
->sctpi_max_burst
)
1902 printf(" maxburst:%d", s
->sctpi_max_burst
);
1903 if (s
->sctpi_maxseg
)
1904 printf(" maxseg:%d", s
->sctpi_maxseg
);
1905 if (s
->sctpi_peer_rwnd
)
1906 printf(" prwnd:%d", s
->sctpi_peer_rwnd
);
1907 if (s
->sctpi_peer_tag
)
1908 printf(" ptag:%x", s
->sctpi_peer_tag
);
1909 if (s
->sctpi_peer_capable
)
1910 printf(" pcapable:%d", s
->sctpi_peer_capable
);
1911 if (s
->sctpi_peer_sack
)
1912 printf(" psack:%d", s
->sctpi_peer_sack
);
1913 if (s
->sctpi_s_autoclose
)
1914 printf(" autoclose:%d", s
->sctpi_s_autoclose
);
1915 if (s
->sctpi_s_adaptation_ind
)
1916 printf(" adapind:%d", s
->sctpi_s_adaptation_ind
);
1917 if (s
->sctpi_s_pd_point
)
1918 printf(" pdpoint:%d", s
->sctpi_s_pd_point
);
1919 if (s
->sctpi_s_nodelay
)
1920 printf(" nodealy:%d", s
->sctpi_s_nodelay
);
1921 if (s
->sctpi_s_disable_fragments
)
1922 printf(" nofrag:%d", s
->sctpi_s_disable_fragments
);
1923 if (s
->sctpi_s_v4mapped
)
1924 printf(" v4mapped:%d", s
->sctpi_s_v4mapped
);
1925 if (s
->sctpi_s_frag_interleave
)
1926 printf(" fraginl:%d", s
->sctpi_s_frag_interleave
);
1929 static void tcp_stats_print(struct tcpstat
*s
)
1935 if (s
->has_sack_opt
)
1939 if (s
->has_ecnseen_opt
)
1941 if (s
->has_fastopen_opt
)
1942 printf(" fastopen");
1944 printf(" %s", s
->cong_alg
);
1945 if (s
->has_wscale_opt
)
1946 printf(" wscale:%d,%d", s
->snd_wscale
, s
->rcv_wscale
);
1948 printf(" rto:%g", s
->rto
);
1950 printf(" backoff:%u", s
->backoff
);
1952 printf(" rtt:%g/%g", s
->rtt
, s
->rttvar
);
1954 printf(" ato:%g", s
->ato
);
1957 printf(" qack:%d", s
->qack
);
1962 printf(" mss:%d", s
->mss
);
1964 printf(" rcvmss:%d", s
->rcv_mss
);
1966 printf(" advmss:%d", s
->advmss
);
1968 printf(" cwnd:%u", s
->cwnd
);
1970 printf(" ssthresh:%d", s
->ssthresh
);
1973 printf(" bytes_acked:%llu", s
->bytes_acked
);
1974 if (s
->bytes_received
)
1975 printf(" bytes_received:%llu", s
->bytes_received
);
1977 printf(" segs_out:%u", s
->segs_out
);
1979 printf(" segs_in:%u", s
->segs_in
);
1980 if (s
->data_segs_out
)
1981 printf(" data_segs_out:%u", s
->data_segs_out
);
1982 if (s
->data_segs_in
)
1983 printf(" data_segs_in:%u", s
->data_segs_in
);
1985 if (s
->dctcp
&& s
->dctcp
->enabled
) {
1986 struct dctcpstat
*dctcp
= s
->dctcp
;
1988 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
1989 dctcp
->ce_state
, dctcp
->alpha
, dctcp
->ab_ecn
,
1991 } else if (s
->dctcp
) {
1992 printf(" dctcp:fallback_mode");
1998 bw
= s
->bbr_info
->bbr_bw_hi
;
2000 bw
|= s
->bbr_info
->bbr_bw_lo
;
2002 printf(" bbr:(bw:%sbps,mrtt:%g",
2003 sprint_bw(b1
, bw
* 8.0),
2004 (double)s
->bbr_info
->bbr_min_rtt
/ 1000.0);
2005 if (s
->bbr_info
->bbr_pacing_gain
)
2006 printf(",pacing_gain:%g",
2007 (double)s
->bbr_info
->bbr_pacing_gain
/ 256.0);
2008 if (s
->bbr_info
->bbr_cwnd_gain
)
2009 printf(",cwnd_gain:%g",
2010 (double)s
->bbr_info
->bbr_cwnd_gain
/ 256.0);
2015 printf(" send %sbps", sprint_bw(b1
, s
->send_bps
));
2017 printf(" lastsnd:%u", s
->lastsnd
);
2019 printf(" lastrcv:%u", s
->lastrcv
);
2021 printf(" lastack:%u", s
->lastack
);
2023 if (s
->pacing_rate
) {
2024 printf(" pacing_rate %sbps", sprint_bw(b1
, s
->pacing_rate
));
2025 if (s
->pacing_rate_max
)
2026 printf("/%sbps", sprint_bw(b1
,
2027 s
->pacing_rate_max
));
2030 if (s
->delivery_rate
)
2031 printf(" delivery_rate %sbps", sprint_bw(b1
, s
->delivery_rate
));
2033 printf(" app_limited");
2036 printf(" busy:%llums", s
->busy_time
/ 1000);
2037 if (s
->rwnd_limited
)
2038 printf(" rwnd_limited:%llums(%.1f%%)",
2039 s
->rwnd_limited
/ 1000,
2040 100.0 * s
->rwnd_limited
/ s
->busy_time
);
2041 if (s
->sndbuf_limited
)
2042 printf(" sndbuf_limited:%llums(%.1f%%)",
2043 s
->sndbuf_limited
/ 1000,
2044 100.0 * s
->sndbuf_limited
/ s
->busy_time
);
2048 printf(" unacked:%u", s
->unacked
);
2049 if (s
->retrans
|| s
->retrans_total
)
2050 printf(" retrans:%u/%u", s
->retrans
, s
->retrans_total
);
2052 printf(" lost:%u", s
->lost
);
2053 if (s
->sacked
&& s
->ss
.state
!= SS_LISTEN
)
2054 printf(" sacked:%u", s
->sacked
);
2056 printf(" fackets:%u", s
->fackets
);
2057 if (s
->reordering
!= 3)
2058 printf(" reordering:%d", s
->reordering
);
2060 printf(" rcv_rtt:%g", s
->rcv_rtt
);
2062 printf(" rcv_space:%d", s
->rcv_space
);
2064 printf(" notsent:%u", s
->not_sent
);
2066 printf(" minrtt:%g", s
->min_rtt
);
2069 static void tcp_timer_print(struct tcpstat
*s
)
2071 static const char * const tmr_name
[] = {
2083 printf(" timer:(%s,%s,%d)",
2085 print_ms_timer(s
->timeout
),
2090 static void sctp_timer_print(struct tcpstat
*s
)
2093 printf(" timer:(T3_RTX,%s,%d)",
2094 print_ms_timer(s
->timeout
), s
->retrans
);
2097 static int tcp_show_line(char *line
, const struct filter
*f
, int family
)
2099 int rto
= 0, ato
= 0;
2100 struct tcpstat s
= {};
2101 char *loc
, *rem
, *data
;
2104 int hz
= get_user_hz();
2106 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
2109 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
2111 if (!(f
->states
& (1 << state
)))
2114 proc_parse_inet_addr(loc
, rem
, family
, &s
.ss
);
2116 if (f
->f
&& run_ssfilter(f
->f
, &s
.ss
) == 0)
2120 n
= sscanf(data
, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n",
2121 &s
.ss
.state
, &s
.ss
.wq
, &s
.ss
.rq
,
2122 &s
.timer
, &s
.timeout
, &s
.retrans
, &s
.ss
.uid
, &s
.probes
,
2123 &s
.ss
.ino
, &s
.ss
.refcnt
, &s
.ss
.sk
, &rto
, &ato
, &s
.qack
, &s
.cwnd
,
2136 s
.retrans
= s
.timer
!= 1 ? s
.probes
: s
.retrans
;
2137 s
.timeout
= (s
.timeout
* 1000 + hz
- 1) / hz
;
2138 s
.ato
= (double)ato
/ hz
;
2140 s
.rto
= (double)rto
;
2141 s
.ssthresh
= s
.ssthresh
== -1 ? 0 : s
.ssthresh
;
2142 s
.rto
= s
.rto
!= 3 * hz
? s
.rto
/ hz
: 0;
2143 s
.ss
.type
= IPPROTO_TCP
;
2145 inet_stats_print(&s
.ss
);
2148 tcp_timer_print(&s
);
2151 sock_details_print(&s
.ss
);
2153 printf(" opt:\"%s\"", opt
);
2157 tcp_stats_print(&s
);
2163 static int generic_record_read(FILE *fp
,
2164 int (*worker
)(char*, const struct filter
*, int),
2165 const struct filter
*f
, int fam
)
2170 if (fgets(line
, sizeof(line
), fp
) == NULL
)
2173 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2174 int n
= strlen(line
);
2176 if (n
== 0 || line
[n
-1] != '\n') {
2182 if (worker(line
, f
, fam
) < 0)
2187 return ferror(fp
) ? -1 : 0;
2190 static void print_skmeminfo(struct rtattr
*tb
[], int attrtype
)
2192 const __u32
*skmeminfo
;
2194 if (!tb
[attrtype
]) {
2195 if (attrtype
== INET_DIAG_SKMEMINFO
) {
2196 if (!tb
[INET_DIAG_MEMINFO
])
2199 const struct inet_diag_meminfo
*minfo
=
2200 RTA_DATA(tb
[INET_DIAG_MEMINFO
]);
2202 printf(" mem:(r%u,w%u,f%u,t%u)",
2211 skmeminfo
= RTA_DATA(tb
[attrtype
]);
2213 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
2214 skmeminfo
[SK_MEMINFO_RMEM_ALLOC
],
2215 skmeminfo
[SK_MEMINFO_RCVBUF
],
2216 skmeminfo
[SK_MEMINFO_WMEM_ALLOC
],
2217 skmeminfo
[SK_MEMINFO_SNDBUF
],
2218 skmeminfo
[SK_MEMINFO_FWD_ALLOC
],
2219 skmeminfo
[SK_MEMINFO_WMEM_QUEUED
],
2220 skmeminfo
[SK_MEMINFO_OPTMEM
]);
2222 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2223 (SK_MEMINFO_BACKLOG
+ 1) * sizeof(__u32
))
2224 printf(",bl%u", skmeminfo
[SK_MEMINFO_BACKLOG
]);
2226 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2227 (SK_MEMINFO_DROPS
+ 1) * sizeof(__u32
))
2228 printf(",d%u", skmeminfo
[SK_MEMINFO_DROPS
]);
2233 static void print_md5sig(struct tcp_diag_md5sig
*sig
)
2236 format_host(sig
->tcpm_family
,
2237 sig
->tcpm_family
== AF_INET6
? 16 : 4,
2239 sig
->tcpm_prefixlen
);
2240 print_escape_buf(sig
->tcpm_key
, sig
->tcpm_keylen
, " ,");
2243 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
2245 static void tcp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
2246 struct rtattr
*tb
[])
2249 struct tcpstat s
= {};
2251 s
.ss
.state
= r
->idiag_state
;
2253 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
2255 if (tb
[INET_DIAG_INFO
]) {
2256 struct tcp_info
*info
;
2257 int len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
2259 /* workaround for older kernels with less fields */
2260 if (len
< sizeof(*info
)) {
2261 info
= alloca(sizeof(*info
));
2262 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
2263 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
2265 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
2268 s
.has_ts_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_TIMESTAMPS
);
2269 s
.has_sack_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SACK
);
2270 s
.has_ecn_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN
);
2271 s
.has_ecnseen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN_SEEN
);
2272 s
.has_fastopen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SYN_DATA
);
2275 if (tb
[INET_DIAG_CONG
])
2277 rta_getattr_str(tb
[INET_DIAG_CONG
]),
2278 sizeof(s
.cong_alg
) - 1);
2280 if (TCPI_HAS_OPT(info
, TCPI_OPT_WSCALE
)) {
2281 s
.has_wscale_opt
= true;
2282 s
.snd_wscale
= info
->tcpi_snd_wscale
;
2283 s
.rcv_wscale
= info
->tcpi_rcv_wscale
;
2286 if (info
->tcpi_rto
&& info
->tcpi_rto
!= 3000000)
2287 s
.rto
= (double)info
->tcpi_rto
/ 1000;
2289 s
.backoff
= info
->tcpi_backoff
;
2290 s
.rtt
= (double)info
->tcpi_rtt
/ 1000;
2291 s
.rttvar
= (double)info
->tcpi_rttvar
/ 1000;
2292 s
.ato
= (double)info
->tcpi_ato
/ 1000;
2293 s
.mss
= info
->tcpi_snd_mss
;
2294 s
.rcv_mss
= info
->tcpi_rcv_mss
;
2295 s
.advmss
= info
->tcpi_advmss
;
2296 s
.rcv_space
= info
->tcpi_rcv_space
;
2297 s
.rcv_rtt
= (double)info
->tcpi_rcv_rtt
/ 1000;
2298 s
.lastsnd
= info
->tcpi_last_data_sent
;
2299 s
.lastrcv
= info
->tcpi_last_data_recv
;
2300 s
.lastack
= info
->tcpi_last_ack_recv
;
2301 s
.unacked
= info
->tcpi_unacked
;
2302 s
.retrans
= info
->tcpi_retrans
;
2303 s
.retrans_total
= info
->tcpi_total_retrans
;
2304 s
.lost
= info
->tcpi_lost
;
2305 s
.sacked
= info
->tcpi_sacked
;
2306 s
.reordering
= info
->tcpi_reordering
;
2307 s
.rcv_space
= info
->tcpi_rcv_space
;
2308 s
.cwnd
= info
->tcpi_snd_cwnd
;
2310 if (info
->tcpi_snd_ssthresh
< 0xFFFF)
2311 s
.ssthresh
= info
->tcpi_snd_ssthresh
;
2313 rtt
= (double) info
->tcpi_rtt
;
2314 if (tb
[INET_DIAG_VEGASINFO
]) {
2315 const struct tcpvegas_info
*vinfo
2316 = RTA_DATA(tb
[INET_DIAG_VEGASINFO
]);
2318 if (vinfo
->tcpv_enabled
&&
2319 vinfo
->tcpv_rtt
&& vinfo
->tcpv_rtt
!= 0x7fffffff)
2320 rtt
= vinfo
->tcpv_rtt
;
2323 if (tb
[INET_DIAG_DCTCPINFO
]) {
2324 struct dctcpstat
*dctcp
= malloc(sizeof(struct
2327 const struct tcp_dctcp_info
*dinfo
2328 = RTA_DATA(tb
[INET_DIAG_DCTCPINFO
]);
2330 dctcp
->enabled
= !!dinfo
->dctcp_enabled
;
2331 dctcp
->ce_state
= dinfo
->dctcp_ce_state
;
2332 dctcp
->alpha
= dinfo
->dctcp_alpha
;
2333 dctcp
->ab_ecn
= dinfo
->dctcp_ab_ecn
;
2334 dctcp
->ab_tot
= dinfo
->dctcp_ab_tot
;
2338 if (tb
[INET_DIAG_BBRINFO
]) {
2339 const void *bbr_info
= RTA_DATA(tb
[INET_DIAG_BBRINFO
]);
2340 int len
= min(RTA_PAYLOAD(tb
[INET_DIAG_BBRINFO
]),
2341 sizeof(*s
.bbr_info
));
2343 s
.bbr_info
= calloc(1, sizeof(*s
.bbr_info
));
2344 if (s
.bbr_info
&& bbr_info
)
2345 memcpy(s
.bbr_info
, bbr_info
, len
);
2348 if (rtt
> 0 && info
->tcpi_snd_mss
&& info
->tcpi_snd_cwnd
) {
2349 s
.send_bps
= (double) info
->tcpi_snd_cwnd
*
2350 (double)info
->tcpi_snd_mss
* 8000000. / rtt
;
2353 if (info
->tcpi_pacing_rate
&&
2354 info
->tcpi_pacing_rate
!= ~0ULL) {
2355 s
.pacing_rate
= info
->tcpi_pacing_rate
* 8.;
2357 if (info
->tcpi_max_pacing_rate
&&
2358 info
->tcpi_max_pacing_rate
!= ~0ULL)
2359 s
.pacing_rate_max
= info
->tcpi_max_pacing_rate
* 8.;
2361 s
.bytes_acked
= info
->tcpi_bytes_acked
;
2362 s
.bytes_received
= info
->tcpi_bytes_received
;
2363 s
.segs_out
= info
->tcpi_segs_out
;
2364 s
.segs_in
= info
->tcpi_segs_in
;
2365 s
.data_segs_out
= info
->tcpi_data_segs_out
;
2366 s
.data_segs_in
= info
->tcpi_data_segs_in
;
2367 s
.not_sent
= info
->tcpi_notsent_bytes
;
2368 if (info
->tcpi_min_rtt
&& info
->tcpi_min_rtt
!= ~0U)
2369 s
.min_rtt
= (double) info
->tcpi_min_rtt
/ 1000;
2370 s
.delivery_rate
= info
->tcpi_delivery_rate
* 8.;
2371 s
.app_limited
= info
->tcpi_delivery_rate_app_limited
;
2372 s
.busy_time
= info
->tcpi_busy_time
;
2373 s
.rwnd_limited
= info
->tcpi_rwnd_limited
;
2374 s
.sndbuf_limited
= info
->tcpi_sndbuf_limited
;
2375 tcp_stats_print(&s
);
2379 if (tb
[INET_DIAG_MD5SIG
]) {
2380 struct tcp_diag_md5sig
*sig
= RTA_DATA(tb
[INET_DIAG_MD5SIG
]);
2381 int len
= RTA_PAYLOAD(tb
[INET_DIAG_MD5SIG
]);
2383 printf(" md5keys:");
2384 print_md5sig(sig
++);
2385 for (len
-= sizeof(*sig
); len
> 0; len
-= sizeof(*sig
)) {
2387 print_md5sig(sig
++);
2392 static const char *format_host_sa(struct sockaddr_storage
*sa
)
2395 struct sockaddr_in sin
;
2396 struct sockaddr_in6 sin6
;
2397 } *saddr
= (void *)sa
;
2399 switch (sa
->ss_family
) {
2401 return format_host(AF_INET
, 4, &saddr
->sin
.sin_addr
);
2403 return format_host(AF_INET6
, 16, &saddr
->sin6
.sin6_addr
);
2409 static void sctp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
2410 struct rtattr
*tb
[])
2412 struct sockaddr_storage
*sa
;
2415 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
2417 if (tb
[INET_DIAG_LOCALS
]) {
2418 len
= RTA_PAYLOAD(tb
[INET_DIAG_LOCALS
]);
2419 sa
= RTA_DATA(tb
[INET_DIAG_LOCALS
]);
2421 printf("locals:%s", format_host_sa(sa
));
2422 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
2423 printf(",%s", format_host_sa(sa
));
2426 if (tb
[INET_DIAG_PEERS
]) {
2427 len
= RTA_PAYLOAD(tb
[INET_DIAG_PEERS
]);
2428 sa
= RTA_DATA(tb
[INET_DIAG_PEERS
]);
2430 printf(" peers:%s", format_host_sa(sa
));
2431 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
2432 printf(",%s", format_host_sa(sa
));
2434 if (tb
[INET_DIAG_INFO
]) {
2435 struct sctp_info
*info
;
2436 len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
2438 /* workaround for older kernels with less fields */
2439 if (len
< sizeof(*info
)) {
2440 info
= alloca(sizeof(*info
));
2441 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
2442 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
2444 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
2446 sctp_stats_print(info
);
2450 static void parse_diag_msg(struct nlmsghdr
*nlh
, struct sockstat
*s
)
2452 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2453 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2455 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2456 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2458 s
->state
= r
->idiag_state
;
2459 s
->local
.family
= s
->remote
.family
= r
->idiag_family
;
2460 s
->lport
= ntohs(r
->id
.idiag_sport
);
2461 s
->rport
= ntohs(r
->id
.idiag_dport
);
2462 s
->wq
= r
->idiag_wqueue
;
2463 s
->rq
= r
->idiag_rqueue
;
2464 s
->ino
= r
->idiag_inode
;
2465 s
->uid
= r
->idiag_uid
;
2466 s
->iface
= r
->id
.idiag_if
;
2467 s
->sk
= cookie_sk_get(&r
->id
.idiag_cookie
[0]);
2470 if (tb
[INET_DIAG_MARK
])
2471 s
->mark
= rta_getattr_u32(tb
[INET_DIAG_MARK
]);
2472 if (tb
[INET_DIAG_PROTOCOL
])
2473 s
->raw_prot
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
2477 if (s
->local
.family
== AF_INET
)
2478 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
2480 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
2482 memcpy(s
->local
.data
, r
->id
.idiag_src
, s
->local
.bytelen
);
2483 memcpy(s
->remote
.data
, r
->id
.idiag_dst
, s
->local
.bytelen
);
2486 static int inet_show_sock(struct nlmsghdr
*nlh
,
2489 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2490 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2492 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2493 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2495 if (tb
[INET_DIAG_PROTOCOL
])
2496 s
->type
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
2498 inet_stats_print(s
);
2501 struct tcpstat t
= {};
2503 t
.timer
= r
->idiag_timer
;
2504 t
.timeout
= r
->idiag_expires
;
2505 t
.retrans
= r
->idiag_retrans
;
2506 if (s
->type
== IPPROTO_SCTP
)
2507 sctp_timer_print(&t
);
2509 tcp_timer_print(&t
);
2513 sock_details_print(s
);
2514 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
]) {
2515 unsigned char v6only
;
2517 v6only
= rta_getattr_u8(tb
[INET_DIAG_SKV6ONLY
]);
2518 printf(" v6only:%u", v6only
);
2520 if (tb
[INET_DIAG_SHUTDOWN
]) {
2523 mask
= rta_getattr_u8(tb
[INET_DIAG_SHUTDOWN
]);
2524 printf(" %c-%c", mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
2528 if (show_mem
|| (show_tcpinfo
&& s
->type
!= IPPROTO_UDP
)) {
2530 if (s
->type
== IPPROTO_SCTP
)
2531 sctp_show_info(nlh
, r
, tb
);
2533 tcp_show_info(nlh
, r
, tb
);
2541 static int tcpdiag_send(int fd
, int protocol
, struct filter
*f
)
2543 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
2545 struct nlmsghdr nlh
;
2546 struct inet_diag_req r
;
2548 .nlh
.nlmsg_len
= sizeof(req
),
2549 .nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
,
2550 .nlh
.nlmsg_seq
= MAGIC_SEQ
,
2551 .r
.idiag_family
= AF_INET
,
2552 .r
.idiag_states
= f
->states
,
2558 struct iovec iov
[3];
2561 if (protocol
== IPPROTO_UDP
)
2564 if (protocol
== IPPROTO_TCP
)
2565 req
.nlh
.nlmsg_type
= TCPDIAG_GETSOCK
;
2567 req
.nlh
.nlmsg_type
= DCCPDIAG_GETSOCK
;
2569 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
2570 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
2574 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
2575 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
2576 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
2579 iov
[0] = (struct iovec
){
2581 .iov_len
= sizeof(req
)
2584 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
2586 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
2587 rta
.rta_len
= RTA_LENGTH(bclen
);
2588 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
2589 iov
[2] = (struct iovec
){ bc
, bclen
};
2590 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
2595 msg
= (struct msghdr
) {
2596 .msg_name
= (void *)&nladdr
,
2597 .msg_namelen
= sizeof(nladdr
),
2599 .msg_iovlen
= iovlen
,
2602 if (sendmsg(fd
, &msg
, 0) < 0) {
2610 static int sockdiag_send(int family
, int fd
, int protocol
, struct filter
*f
)
2612 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
2613 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
2618 struct iovec iov
[3];
2621 if (family
== PF_UNSPEC
)
2622 return tcpdiag_send(fd
, protocol
, f
);
2624 memset(&req
.r
, 0, sizeof(req
.r
));
2625 req
.r
.sdiag_family
= family
;
2626 req
.r
.sdiag_protocol
= protocol
;
2627 req
.r
.idiag_states
= f
->states
;
2629 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
2630 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
2634 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
2635 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
2636 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
2639 iov
[0] = (struct iovec
){
2641 .iov_len
= sizeof(req
)
2644 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
2646 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
2647 rta
.rta_len
= RTA_LENGTH(bclen
);
2648 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
2649 iov
[2] = (struct iovec
){ bc
, bclen
};
2650 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
2655 msg
= (struct msghdr
) {
2656 .msg_name
= (void *)&nladdr
,
2657 .msg_namelen
= sizeof(nladdr
),
2659 .msg_iovlen
= iovlen
,
2662 if (sendmsg(fd
, &msg
, 0) < 0) {
2670 struct inet_diag_arg
{
2673 struct rtnl_handle
*rth
;
2676 static int kill_inet_sock(struct nlmsghdr
*h
, void *arg
, struct sockstat
*s
)
2678 struct inet_diag_msg
*d
= NLMSG_DATA(h
);
2679 struct inet_diag_arg
*diag_arg
= arg
;
2680 struct rtnl_handle
*rth
= diag_arg
->rth
;
2682 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
2684 req
.nlh
.nlmsg_type
= SOCK_DESTROY
;
2685 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2686 req
.nlh
.nlmsg_seq
= ++rth
->seq
;
2687 req
.r
.sdiag_family
= d
->idiag_family
;
2688 req
.r
.sdiag_protocol
= diag_arg
->protocol
;
2691 if (diag_arg
->protocol
== IPPROTO_RAW
) {
2692 struct inet_diag_req_raw
*raw
= (void *)&req
.r
;
2694 BUILD_BUG_ON(sizeof(req
.r
) != sizeof(*raw
));
2695 raw
->sdiag_raw_protocol
= s
->raw_prot
;
2698 return rtnl_talk(rth
, &req
.nlh
, NULL
, 0);
2701 static int show_one_inet_sock(const struct sockaddr_nl
*addr
,
2702 struct nlmsghdr
*h
, void *arg
)
2705 struct inet_diag_arg
*diag_arg
= arg
;
2706 struct inet_diag_msg
*r
= NLMSG_DATA(h
);
2707 struct sockstat s
= {};
2709 if (!(diag_arg
->f
->families
& FAMILY_MASK(r
->idiag_family
)))
2712 parse_diag_msg(h
, &s
);
2713 s
.type
= diag_arg
->protocol
;
2715 if (diag_arg
->f
->f
&& run_ssfilter(diag_arg
->f
->f
, &s
) == 0)
2718 if (diag_arg
->f
->kill
&& kill_inet_sock(h
, arg
, &s
) != 0) {
2719 if (errno
== EOPNOTSUPP
|| errno
== ENOENT
) {
2720 /* Socket can't be closed, or is already closed. */
2723 perror("SOCK_DESTROY answers");
2728 err
= inet_show_sock(h
, &s
);
2735 static int inet_show_netlink(struct filter
*f
, FILE *dump_fp
, int protocol
)
2738 struct rtnl_handle rth
, rth2
;
2739 int family
= PF_INET
;
2740 struct inet_diag_arg arg
= { .f
= f
, .protocol
= protocol
};
2742 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
2746 if (rtnl_open_byproto(&rth2
, 0, NETLINK_SOCK_DIAG
)) {
2753 rth
.dump
= MAGIC_SEQ
;
2754 rth
.dump_fp
= dump_fp
;
2755 if (preferred_family
== PF_INET6
)
2759 if ((err
= sockdiag_send(family
, rth
.fd
, protocol
, f
)))
2762 if ((err
= rtnl_dump_filter(&rth
, show_one_inet_sock
, &arg
))) {
2763 if (family
!= PF_UNSPEC
) {
2769 if (family
== PF_INET
&& preferred_family
!= PF_INET
) {
2777 rtnl_close(arg
.rth
);
2781 static int tcp_show_netlink_file(struct filter
*f
)
2787 if ((fp
= fopen(getenv("TCPDIAG_FILE"), "r")) == NULL
) {
2788 perror("fopen($TCPDIAG_FILE)");
2794 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
2795 struct sockstat s
= {};
2797 status
= fread(buf
, 1, sizeof(*h
), fp
);
2799 perror("Reading header from $TCPDIAG_FILE");
2802 if (status
!= sizeof(*h
)) {
2803 perror("Unexpected EOF reading $TCPDIAG_FILE");
2807 status
= fread(h
+1, 1, NLMSG_ALIGN(h
->nlmsg_len
-sizeof(*h
)), fp
);
2810 perror("Reading $TCPDIAG_FILE");
2813 if (status
+ sizeof(*h
) < h
->nlmsg_len
) {
2814 perror("Unexpected EOF reading $TCPDIAG_FILE");
2818 /* The only legal exit point */
2819 if (h
->nlmsg_type
== NLMSG_DONE
) {
2824 if (h
->nlmsg_type
== NLMSG_ERROR
) {
2825 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
2827 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
2828 fprintf(stderr
, "ERROR truncated\n");
2830 errno
= -err
->error
;
2831 perror("TCPDIAG answered");
2836 parse_diag_msg(h
, &s
);
2837 s
.type
= IPPROTO_TCP
;
2839 if (f
&& f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
2842 err2
= inet_show_sock(h
, &s
);
2853 static int tcp_show(struct filter
*f
)
2857 int bufsize
= 64*1024;
2859 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2862 dg_proto
= TCP_PROTO
;
2864 if (getenv("TCPDIAG_FILE"))
2865 return tcp_show_netlink_file(f
);
2867 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
2868 && inet_show_netlink(f
, NULL
, IPPROTO_TCP
) == 0)
2871 /* Sigh... We have to parse /proc/net/tcp... */
2874 /* Estimate amount of sockets and try to allocate
2875 * huge buffer to read all the table at one read.
2876 * Limit it by 16MB though. The assumption is: as soon as
2877 * kernel was able to hold information about N connections,
2878 * it is able to give us some memory for snapshot.
2881 get_slabstat(&slabstat
);
2883 int guess
= slabstat
.socks
+slabstat
.tcp_syns
;
2885 if (f
->states
&(1<<SS_TIME_WAIT
))
2886 guess
+= slabstat
.tcp_tws
;
2887 if (guess
> (16*1024*1024)/128)
2888 guess
= (16*1024*1024)/128;
2890 if (guess
> bufsize
)
2893 while (bufsize
>= 64*1024) {
2894 if ((buf
= malloc(bufsize
)) != NULL
)
2903 if (f
->families
& FAMILY_MASK(AF_INET
)) {
2904 if ((fp
= net_tcp_open()) == NULL
)
2907 setbuffer(fp
, buf
, bufsize
);
2908 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET
))
2913 if ((f
->families
& FAMILY_MASK(AF_INET6
)) &&
2914 (fp
= net_tcp6_open()) != NULL
) {
2915 setbuffer(fp
, buf
, bufsize
);
2916 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET6
))
2926 int saved_errno
= errno
;
2931 errno
= saved_errno
;
2936 static int dccp_show(struct filter
*f
)
2938 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2941 if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT")
2942 && inet_show_netlink(f
, NULL
, IPPROTO_DCCP
) == 0)
2948 static int sctp_show(struct filter
*f
)
2950 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
2953 if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT")
2954 && inet_show_netlink(f
, NULL
, IPPROTO_SCTP
) == 0)
2960 static int dgram_show_line(char *line
, const struct filter
*f
, int family
)
2962 struct sockstat s
= {};
2963 char *loc
, *rem
, *data
;
2967 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
2970 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
2972 if (!(f
->states
& (1 << state
)))
2975 proc_parse_inet_addr(loc
, rem
, family
, &s
);
2977 if (f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
2981 n
= sscanf(data
, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
2982 &s
.state
, &s
.wq
, &s
.rq
,
2984 &s
.refcnt
, &s
.sk
, opt
);
2989 s
.type
= dg_proto
== UDP_PROTO
? IPPROTO_UDP
: 0;
2990 inet_stats_print(&s
);
2992 if (show_details
&& opt
[0])
2993 printf(" opt:\"%s\"", opt
);
2999 static int udp_show(struct filter
*f
)
3003 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3006 dg_proto
= UDP_PROTO
;
3008 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
3009 && inet_show_netlink(f
, NULL
, IPPROTO_UDP
) == 0)
3012 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3013 if ((fp
= net_udp_open()) == NULL
)
3015 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3020 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3021 (fp
= net_udp6_open()) != NULL
) {
3022 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3030 int saved_errno
= errno
;
3034 errno
= saved_errno
;
3039 static int raw_show(struct filter
*f
)
3043 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3046 dg_proto
= RAW_PROTO
;
3048 if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") &&
3049 inet_show_netlink(f
, NULL
, IPPROTO_RAW
) == 0)
3052 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3053 if ((fp
= net_raw_open()) == NULL
)
3055 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3060 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3061 (fp
= net_raw6_open()) != NULL
) {
3062 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3070 int saved_errno
= errno
;
3074 errno
= saved_errno
;
3079 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
3081 static void unix_list_drop_first(struct sockstat
**list
)
3083 struct sockstat
*s
= *list
;
3085 (*list
) = (*list
)->next
;
3090 static bool unix_type_skip(struct sockstat
*s
, struct filter
*f
)
3092 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
&(1<<UNIX_ST_DB
)))
3094 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<UNIX_DG_DB
)))
3096 if (s
->type
== SOCK_SEQPACKET
&& !(f
->dbs
&(1<<UNIX_SQ_DB
)))
3101 static void unix_stats_print(struct sockstat
*s
, struct filter
*f
)
3103 char port_name
[30] = {};
3105 sock_state_print(s
);
3107 sock_addr_print(s
->name
?: "*", " ",
3108 int_to_str(s
->lport
, port_name
), NULL
);
3109 sock_addr_print(s
->peer_name
?: "*", " ",
3110 int_to_str(s
->rport
, port_name
), NULL
);
3115 static int unix_show_sock(const struct sockaddr_nl
*addr
, struct nlmsghdr
*nlh
,
3118 struct filter
*f
= (struct filter
*)arg
;
3119 struct unix_diag_msg
*r
= NLMSG_DATA(nlh
);
3120 struct rtattr
*tb
[UNIX_DIAG_MAX
+1];
3122 struct sockstat stat
= { .name
= "*", .peer_name
= "*" };
3124 parse_rtattr(tb
, UNIX_DIAG_MAX
, (struct rtattr
*)(r
+1),
3125 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3127 stat
.type
= r
->udiag_type
;
3128 stat
.state
= r
->udiag_state
;
3129 stat
.ino
= stat
.lport
= r
->udiag_ino
;
3130 stat
.local
.family
= stat
.remote
.family
= AF_UNIX
;
3132 if (unix_type_skip(&stat
, f
))
3135 if (tb
[UNIX_DIAG_RQLEN
]) {
3136 struct unix_diag_rqlen
*rql
= RTA_DATA(tb
[UNIX_DIAG_RQLEN
]);
3138 stat
.rq
= rql
->udiag_rqueue
;
3139 stat
.wq
= rql
->udiag_wqueue
;
3141 if (tb
[UNIX_DIAG_NAME
]) {
3142 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_NAME
]);
3144 memcpy(name
, RTA_DATA(tb
[UNIX_DIAG_NAME
]), len
);
3146 if (name
[0] == '\0') {
3148 for (i
= 0; i
< len
; i
++)
3149 if (name
[i
] == '\0')
3152 stat
.name
= &name
[0];
3153 memcpy(stat
.local
.data
, &stat
.name
, sizeof(stat
.name
));
3155 if (tb
[UNIX_DIAG_PEER
])
3156 stat
.rport
= rta_getattr_u32(tb
[UNIX_DIAG_PEER
]);
3158 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
3161 unix_stats_print(&stat
, f
);
3164 print_skmeminfo(tb
, UNIX_DIAG_MEMINFO
);
3166 if (tb
[UNIX_DIAG_SHUTDOWN
]) {
3169 mask
= rta_getattr_u8(tb
[UNIX_DIAG_SHUTDOWN
]);
3170 printf(" %c-%c", mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
3178 static int handle_netlink_request(struct filter
*f
, struct nlmsghdr
*req
,
3179 size_t size
, rtnl_filter_t show_one_sock
)
3182 struct rtnl_handle rth
;
3184 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
3187 rth
.dump
= MAGIC_SEQ
;
3189 if (rtnl_send(&rth
, req
, size
) < 0)
3192 if (rtnl_dump_filter(&rth
, show_one_sock
, f
))
3201 static int unix_show_netlink(struct filter
*f
)
3203 DIAG_REQUEST(req
, struct unix_diag_req r
);
3205 req
.r
.sdiag_family
= AF_UNIX
;
3206 req
.r
.udiag_states
= f
->states
;
3207 req
.r
.udiag_show
= UDIAG_SHOW_NAME
| UDIAG_SHOW_PEER
| UDIAG_SHOW_RQLEN
;
3209 req
.r
.udiag_show
|= UDIAG_SHOW_MEMINFO
;
3211 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), unix_show_sock
);
3214 static int unix_show(struct filter
*f
)
3221 struct sockstat
*list
= NULL
;
3222 const int unix_state_map
[] = { SS_CLOSE
, SS_SYN_SENT
,
3223 SS_ESTABLISHED
, SS_CLOSING
};
3225 if (!filter_af_get(f
, AF_UNIX
))
3228 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
3229 && unix_show_netlink(f
) == 0)
3232 if ((fp
= net_unix_open()) == NULL
)
3234 if (!fgets(buf
, sizeof(buf
), fp
)) {
3239 if (memcmp(buf
, "Peer", 4) == 0)
3243 while (fgets(buf
, sizeof(buf
), fp
)) {
3244 struct sockstat
*u
, **insp
;
3247 if (!(u
= calloc(1, sizeof(*u
))))
3250 if (sscanf(buf
, "%x: %x %x %x %x %x %d %s",
3251 &u
->rport
, &u
->rq
, &u
->wq
, &flags
, &u
->type
,
3252 &u
->state
, &u
->ino
, name
) < 8)
3256 u
->local
.family
= u
->remote
.family
= AF_UNIX
;
3258 if (flags
& (1 << 16)) {
3259 u
->state
= SS_LISTEN
;
3260 } else if (u
->state
> 0 &&
3261 u
->state
<= ARRAY_SIZE(unix_state_map
)) {
3262 u
->state
= unix_state_map
[u
->state
-1];
3263 if (u
->type
== SOCK_DGRAM
&& u
->state
== SS_CLOSE
&& u
->rport
)
3264 u
->state
= SS_ESTABLISHED
;
3266 if (unix_type_skip(u
, f
) ||
3267 !(f
->states
& (1 << u
->state
))) {
3279 u
->name
= strdup(name
);
3289 for (p
= list
; p
; p
= p
->next
) {
3290 if (u
->rport
== p
->lport
)
3296 u
->peer_name
= p
->name
? : "*";
3300 struct sockstat st
= {
3301 .local
.family
= AF_UNIX
,
3302 .remote
.family
= AF_UNIX
,
3305 memcpy(st
.local
.data
, &u
->name
, sizeof(u
->name
));
3306 if (strcmp(u
->peer_name
, "*"))
3307 memcpy(st
.remote
.data
, &u
->peer_name
,
3308 sizeof(u
->peer_name
));
3309 if (run_ssfilter(f
->f
, &st
) == 0) {
3318 if (u
->type
< (*insp
)->type
||
3319 (u
->type
== (*insp
)->type
&&
3320 u
->ino
< (*insp
)->ino
))
3322 insp
= &(*insp
)->next
;
3327 if (++cnt
> MAX_UNIX_REMEMBER
) {
3329 unix_stats_print(list
, f
);
3332 unix_list_drop_first(&list
);
3339 unix_stats_print(list
, f
);
3342 unix_list_drop_first(&list
);
3348 static int packet_stats_print(struct sockstat
*s
, const struct filter
*f
)
3350 const char *addr
, *port
;
3353 s
->local
.family
= s
->remote
.family
= AF_PACKET
;
3356 s
->local
.data
[0] = s
->prot
;
3357 if (run_ssfilter(f
->f
, s
) == 0)
3361 sock_state_print(s
);
3366 addr
= ll_proto_n2a(htons(s
->prot
), ll_name
, sizeof(ll_name
));
3371 port
= xll_index_to_name(s
->iface
);
3373 sock_addr_print(addr
, ":", port
, NULL
);
3374 sock_addr_print("", "*", "", NULL
);
3379 sock_details_print(s
);
3384 static void packet_show_ring(struct packet_diag_ring
*ring
)
3386 printf("blk_size:%d", ring
->pdr_block_size
);
3387 printf(",blk_nr:%d", ring
->pdr_block_nr
);
3388 printf(",frm_size:%d", ring
->pdr_frame_size
);
3389 printf(",frm_nr:%d", ring
->pdr_frame_nr
);
3390 printf(",tmo:%d", ring
->pdr_retire_tmo
);
3391 printf(",features:0x%x", ring
->pdr_features
);
3394 static int packet_show_sock(const struct sockaddr_nl
*addr
,
3395 struct nlmsghdr
*nlh
, void *arg
)
3397 const struct filter
*f
= arg
;
3398 struct packet_diag_msg
*r
= NLMSG_DATA(nlh
);
3399 struct packet_diag_info
*pinfo
= NULL
;
3400 struct packet_diag_ring
*ring_rx
= NULL
, *ring_tx
= NULL
;
3401 struct rtattr
*tb
[PACKET_DIAG_MAX
+1];
3402 struct sockstat stat
= {};
3403 uint32_t fanout
= 0;
3404 bool has_fanout
= false;
3406 parse_rtattr(tb
, PACKET_DIAG_MAX
, (struct rtattr
*)(r
+1),
3407 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3409 /* use /proc/net/packet if all info are not available */
3410 if (!tb
[PACKET_DIAG_MEMINFO
])
3413 stat
.type
= r
->pdiag_type
;
3414 stat
.prot
= r
->pdiag_num
;
3415 stat
.ino
= r
->pdiag_ino
;
3416 stat
.state
= SS_CLOSE
;
3417 stat
.sk
= cookie_sk_get(&r
->pdiag_cookie
[0]);
3419 if (tb
[PACKET_DIAG_MEMINFO
]) {
3420 __u32
*skmeminfo
= RTA_DATA(tb
[PACKET_DIAG_MEMINFO
]);
3422 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
3425 if (tb
[PACKET_DIAG_INFO
]) {
3426 pinfo
= RTA_DATA(tb
[PACKET_DIAG_INFO
]);
3427 stat
.lport
= stat
.iface
= pinfo
->pdi_index
;
3430 if (tb
[PACKET_DIAG_UID
])
3431 stat
.uid
= rta_getattr_u32(tb
[PACKET_DIAG_UID
]);
3433 if (tb
[PACKET_DIAG_RX_RING
])
3434 ring_rx
= RTA_DATA(tb
[PACKET_DIAG_RX_RING
]);
3436 if (tb
[PACKET_DIAG_TX_RING
])
3437 ring_tx
= RTA_DATA(tb
[PACKET_DIAG_TX_RING
]);
3439 if (tb
[PACKET_DIAG_FANOUT
]) {
3441 fanout
= rta_getattr_u32(tb
[PACKET_DIAG_FANOUT
]);
3444 if (packet_stats_print(&stat
, f
))
3449 printf("\n\tver:%d", pinfo
->pdi_version
);
3450 printf(" cpy_thresh:%d", pinfo
->pdi_copy_thresh
);
3452 if (pinfo
->pdi_flags
& PDI_RUNNING
)
3454 if (pinfo
->pdi_flags
& PDI_AUXDATA
)
3456 if (pinfo
->pdi_flags
& PDI_ORIGDEV
)
3458 if (pinfo
->pdi_flags
& PDI_VNETHDR
)
3460 if (pinfo
->pdi_flags
& PDI_LOSS
)
3462 if (!pinfo
->pdi_flags
)
3467 printf("\n\tring_rx(");
3468 packet_show_ring(ring_rx
);
3472 printf("\n\tring_tx(");
3473 packet_show_ring(ring_tx
);
3477 uint16_t type
= (fanout
>> 16) & 0xffff;
3479 printf("\n\tfanout(");
3480 printf("id:%d,", fanout
& 0xffff);
3496 printf("0x%x", type
);
3502 if (show_bpf
&& tb
[PACKET_DIAG_FILTER
]) {
3503 struct sock_filter
*fil
=
3504 RTA_DATA(tb
[PACKET_DIAG_FILTER
]);
3505 int num
= RTA_PAYLOAD(tb
[PACKET_DIAG_FILTER
]) /
3506 sizeof(struct sock_filter
);
3508 printf("\n\tbpf filter (%d): ", num
);
3510 printf(" 0x%02x %u %u %u,",
3511 fil
->code
, fil
->jt
, fil
->jf
, fil
->k
);
3520 static int packet_show_netlink(struct filter
*f
)
3522 DIAG_REQUEST(req
, struct packet_diag_req r
);
3524 req
.r
.sdiag_family
= AF_PACKET
;
3525 req
.r
.pdiag_show
= PACKET_SHOW_INFO
| PACKET_SHOW_MEMINFO
|
3526 PACKET_SHOW_FILTER
| PACKET_SHOW_RING_CFG
| PACKET_SHOW_FANOUT
;
3528 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), packet_show_sock
);
3531 static int packet_show_line(char *buf
, const struct filter
*f
, int fam
)
3533 unsigned long long sk
;
3534 struct sockstat stat
= {};
3535 int type
, prot
, iface
, state
, rq
, uid
, ino
;
3537 sscanf(buf
, "%llx %*d %d %x %d %d %u %u %u",
3539 &type
, &prot
, &iface
, &state
,
3542 if (stat
.type
== SOCK_RAW
&& !(f
->dbs
&(1<<PACKET_R_DB
)))
3544 if (stat
.type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<PACKET_DG_DB
)))
3549 stat
.lport
= stat
.iface
= iface
;
3554 stat
.state
= SS_CLOSE
;
3556 if (packet_stats_print(&stat
, f
))
3563 static int packet_show(struct filter
*f
)
3568 if (!filter_af_get(f
, AF_PACKET
) || !(f
->states
& (1 << SS_CLOSE
)))
3571 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3572 packet_show_netlink(f
) == 0)
3575 if ((fp
= net_packet_open()) == NULL
)
3577 if (generic_record_read(fp
, packet_show_line
, f
, AF_PACKET
))
3584 static int netlink_show_one(struct filter
*f
,
3585 int prot
, int pid
, unsigned int groups
,
3586 int state
, int dst_pid
, unsigned int dst_group
,
3588 unsigned long long sk
, unsigned long long cb
)
3590 struct sockstat st
= {
3594 .local
.family
= AF_NETLINK
,
3595 .remote
.family
= AF_NETLINK
,
3598 SPRINT_BUF(prot_buf
) = {};
3599 const char *prot_name
;
3600 char procname
[64] = {};
3605 st
.local
.data
[0] = prot
;
3606 if (run_ssfilter(f
->f
, &st
) == 0)
3610 sock_state_print(&st
);
3612 if (resolve_services
)
3613 prot_name
= nl_proto_n2a(prot
, prot_buf
, sizeof(prot_buf
));
3615 prot_name
= int_to_str(prot
, prot_buf
);
3619 } else if (resolve_services
) {
3624 strncpy(procname
, "kernel", 6);
3625 } else if (pid
> 0) {
3628 snprintf(procname
, sizeof(procname
), "%s/%d/stat",
3629 getenv("PROC_ROOT") ? : "/proc", pid
);
3630 if ((fp
= fopen(procname
, "r")) != NULL
) {
3631 if (fscanf(fp
, "%*d (%[^)])", procname
) == 1) {
3632 snprintf(procname
+strlen(procname
),
3633 sizeof(procname
)-strlen(procname
),
3641 int_to_str(pid
, procname
);
3643 int_to_str(pid
, procname
);
3646 sock_addr_print(prot_name
, ":", procname
, NULL
);
3648 if (state
== NETLINK_CONNECTED
) {
3649 char dst_group_buf
[30];
3650 char dst_pid_buf
[30];
3652 sock_addr_print(int_to_str(dst_group
, dst_group_buf
), ":",
3653 int_to_str(dst_pid
, dst_pid_buf
), NULL
);
3655 sock_addr_print("", "*", "", NULL
);
3658 char *pid_context
= NULL
;
3660 if (show_proc_ctx
) {
3661 /* The pid value will either be:
3662 * 0 if destination kernel - show kernel initial context.
3663 * A valid process pid - use getpidcon.
3664 * A unique value allocated by the kernel or netlink user
3665 * to the process - show context as "not available".
3668 security_get_initial_context("kernel", &pid_context
);
3670 getpidcon(pid
, &pid_context
);
3672 if (pid_context
!= NULL
) {
3673 printf("proc_ctx=%-*s ", serv_width
, pid_context
);
3676 printf("proc_ctx=%-*s ", serv_width
, "unavailable");
3681 printf(" sk=%llx cb=%llx groups=0x%08x", sk
, cb
, groups
);
3688 static int netlink_show_sock(const struct sockaddr_nl
*addr
,
3689 struct nlmsghdr
*nlh
, void *arg
)
3691 struct filter
*f
= (struct filter
*)arg
;
3692 struct netlink_diag_msg
*r
= NLMSG_DATA(nlh
);
3693 struct rtattr
*tb
[NETLINK_DIAG_MAX
+1];
3695 unsigned long groups
= 0;
3697 parse_rtattr(tb
, NETLINK_DIAG_MAX
, (struct rtattr
*)(r
+1),
3698 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3700 if (tb
[NETLINK_DIAG_GROUPS
] && RTA_PAYLOAD(tb
[NETLINK_DIAG_GROUPS
]))
3701 groups
= *(unsigned long *) RTA_DATA(tb
[NETLINK_DIAG_GROUPS
]);
3703 if (tb
[NETLINK_DIAG_MEMINFO
]) {
3704 const __u32
*skmeminfo
;
3706 skmeminfo
= RTA_DATA(tb
[NETLINK_DIAG_MEMINFO
]);
3708 rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
3709 wq
= skmeminfo
[SK_MEMINFO_WMEM_ALLOC
];
3712 if (netlink_show_one(f
, r
->ndiag_protocol
, r
->ndiag_portid
, groups
,
3713 r
->ndiag_state
, r
->ndiag_dst_portid
, r
->ndiag_dst_group
,
3720 print_skmeminfo(tb
, NETLINK_DIAG_MEMINFO
);
3727 static int netlink_show_netlink(struct filter
*f
)
3729 DIAG_REQUEST(req
, struct netlink_diag_req r
);
3731 req
.r
.sdiag_family
= AF_NETLINK
;
3732 req
.r
.sdiag_protocol
= NDIAG_PROTO_ALL
;
3733 req
.r
.ndiag_show
= NDIAG_SHOW_GROUPS
| NDIAG_SHOW_MEMINFO
;
3735 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), netlink_show_sock
);
3738 static int netlink_show(struct filter
*f
)
3743 unsigned int groups
;
3745 unsigned long long sk
, cb
;
3747 if (!filter_af_get(f
, AF_NETLINK
) || !(f
->states
& (1 << SS_CLOSE
)))
3750 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
3751 netlink_show_netlink(f
) == 0)
3754 if ((fp
= net_netlink_open()) == NULL
)
3756 if (!fgets(buf
, sizeof(buf
), fp
)) {
3761 while (fgets(buf
, sizeof(buf
), fp
)) {
3762 sscanf(buf
, "%llx %d %d %x %d %d %llx %d",
3764 &prot
, &pid
, &groups
, &rq
, &wq
, &cb
, &rc
);
3766 netlink_show_one(f
, prot
, pid
, groups
, 0, 0, 0, rq
, wq
, sk
, cb
);
3773 static bool vsock_type_skip(struct sockstat
*s
, struct filter
*f
)
3775 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
& (1 << VSOCK_ST_DB
)))
3777 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
& (1 << VSOCK_DG_DB
)))
3782 static void vsock_addr_print(inet_prefix
*a
, __u32 port
)
3784 char cid_str
[sizeof("4294967295")];
3785 char port_str
[sizeof("4294967295")];
3788 memcpy(&cid
, a
->data
, sizeof(cid
));
3790 if (cid
== ~(__u32
)0)
3791 snprintf(cid_str
, sizeof(cid_str
), "*");
3793 snprintf(cid_str
, sizeof(cid_str
), "%u", cid
);
3795 if (port
== ~(__u32
)0)
3796 snprintf(port_str
, sizeof(port_str
), "*");
3798 snprintf(port_str
, sizeof(port_str
), "%u", port
);
3800 sock_addr_print(cid_str
, ":", port_str
, NULL
);
3803 static void vsock_stats_print(struct sockstat
*s
, struct filter
*f
)
3805 sock_state_print(s
);
3807 vsock_addr_print(&s
->local
, s
->lport
);
3808 vsock_addr_print(&s
->remote
, s
->rport
);
3815 static int vsock_show_sock(const struct sockaddr_nl
*addr
,
3816 struct nlmsghdr
*nlh
, void *arg
)
3818 struct filter
*f
= (struct filter
*)arg
;
3819 struct vsock_diag_msg
*r
= NLMSG_DATA(nlh
);
3820 struct sockstat stat
= {
3821 .type
= r
->vdiag_type
,
3822 .lport
= r
->vdiag_src_port
,
3823 .rport
= r
->vdiag_dst_port
,
3824 .state
= r
->vdiag_state
,
3825 .ino
= r
->vdiag_ino
,
3828 vsock_set_inet_prefix(&stat
.local
, r
->vdiag_src_cid
);
3829 vsock_set_inet_prefix(&stat
.remote
, r
->vdiag_dst_cid
);
3831 if (vsock_type_skip(&stat
, f
))
3834 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
3837 vsock_stats_print(&stat
, f
);
3842 static int vsock_show(struct filter
*f
)
3844 DIAG_REQUEST(req
, struct vsock_diag_req r
);
3846 if (!filter_af_get(f
, AF_VSOCK
))
3849 req
.r
.sdiag_family
= AF_VSOCK
;
3850 req
.r
.vdiag_states
= f
->states
;
3852 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), vsock_show_sock
);
3855 struct sock_diag_msg
{
3859 static int generic_show_sock(const struct sockaddr_nl
*addr
,
3860 struct nlmsghdr
*nlh
, void *arg
)
3862 struct sock_diag_msg
*r
= NLMSG_DATA(nlh
);
3863 struct inet_diag_arg inet_arg
= { .f
= arg
, .protocol
= IPPROTO_MAX
};
3865 switch (r
->sdiag_family
) {
3868 return show_one_inet_sock(addr
, nlh
, &inet_arg
);
3870 return unix_show_sock(addr
, nlh
, arg
);
3872 return packet_show_sock(addr
, nlh
, arg
);
3874 return netlink_show_sock(addr
, nlh
, arg
);
3876 return vsock_show_sock(addr
, nlh
, arg
);
3882 static int handle_follow_request(struct filter
*f
)
3886 struct rtnl_handle rth
;
3888 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << TCP_DB
))
3889 groups
|= 1 << (SKNLGRP_INET_TCP_DESTROY
- 1);
3890 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << UDP_DB
))
3891 groups
|= 1 << (SKNLGRP_INET_UDP_DESTROY
- 1);
3892 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << TCP_DB
))
3893 groups
|= 1 << (SKNLGRP_INET6_TCP_DESTROY
- 1);
3894 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << UDP_DB
))
3895 groups
|= 1 << (SKNLGRP_INET6_UDP_DESTROY
- 1);
3900 if (rtnl_open_byproto(&rth
, groups
, NETLINK_SOCK_DIAG
))
3904 rth
.local
.nl_pid
= 0;
3906 if (rtnl_dump_filter(&rth
, generic_show_sock
, f
))
3913 static int get_snmp_int(char *proto
, char *key
, int *result
)
3917 int protolen
= strlen(proto
);
3918 int keylen
= strlen(key
);
3922 if ((fp
= net_snmp_open()) == NULL
)
3925 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
3929 if (memcmp(buf
, proto
, protolen
))
3931 while ((p
= strchr(p
, ' ')) != NULL
) {
3934 if (memcmp(p
, key
, keylen
) == 0 &&
3935 (p
[keylen
] == ' ' || p
[keylen
] == '\n'))
3938 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
3940 if (memcmp(buf
, proto
, protolen
))
3943 while ((p
= strchr(p
, ' ')) != NULL
) {
3946 sscanf(p
, "%d", result
);
3959 /* Get stats from sockstat */
3979 static void get_sockstat_line(char *line
, struct ssummary
*s
)
3981 char id
[256], rem
[256];
3983 if (sscanf(line
, "%[^ ] %[^\n]\n", id
, rem
) != 2)
3986 if (strcmp(id
, "sockets:") == 0)
3987 sscanf(rem
, "%*s%d", &s
->socks
);
3988 else if (strcmp(id
, "UDP:") == 0)
3989 sscanf(rem
, "%*s%d", &s
->udp4
);
3990 else if (strcmp(id
, "UDP6:") == 0)
3991 sscanf(rem
, "%*s%d", &s
->udp6
);
3992 else if (strcmp(id
, "RAW:") == 0)
3993 sscanf(rem
, "%*s%d", &s
->raw4
);
3994 else if (strcmp(id
, "RAW6:") == 0)
3995 sscanf(rem
, "%*s%d", &s
->raw6
);
3996 else if (strcmp(id
, "TCP6:") == 0)
3997 sscanf(rem
, "%*s%d", &s
->tcp6_hashed
);
3998 else if (strcmp(id
, "FRAG:") == 0)
3999 sscanf(rem
, "%*s%d%*s%d", &s
->frag4
, &s
->frag4_mem
);
4000 else if (strcmp(id
, "FRAG6:") == 0)
4001 sscanf(rem
, "%*s%d%*s%d", &s
->frag6
, &s
->frag6_mem
);
4002 else if (strcmp(id
, "TCP:") == 0)
4003 sscanf(rem
, "%*s%d%*s%d%*s%d%*s%d%*s%d",
4005 &s
->tcp_orphans
, &s
->tcp_tws
, &s
->tcp_total
, &s
->tcp_mem
);
4008 static int get_sockstat(struct ssummary
*s
)
4013 memset(s
, 0, sizeof(*s
));
4015 if ((fp
= net_sockstat_open()) == NULL
)
4017 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4018 get_sockstat_line(buf
, s
);
4021 if ((fp
= net_sockstat6_open()) == NULL
)
4023 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4024 get_sockstat_line(buf
, s
);
4030 static int print_summary(void)
4035 if (get_sockstat(&s
) < 0)
4036 perror("ss: get_sockstat");
4037 if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab
) < 0)
4038 perror("ss: get_snmpstat");
4040 get_slabstat(&slabstat
);
4042 printf("Total: %d (kernel %d)\n", s
.socks
, slabstat
.socks
);
4044 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
4045 s
.tcp_total
+ slabstat
.tcp_syns
+ s
.tcp_tws
,
4047 s
.tcp_total
- (s
.tcp4_hashed
+s
.tcp6_hashed
-s
.tcp_tws
),
4050 s
.tcp_tws
, slabstat
.tcp_tws
,
4055 printf("Transport Total IP IPv6\n");
4056 printf("* %-9d %-9s %-9s\n", slabstat
.socks
, "-", "-");
4057 printf("RAW %-9d %-9d %-9d\n", s
.raw4
+s
.raw6
, s
.raw4
, s
.raw6
);
4058 printf("UDP %-9d %-9d %-9d\n", s
.udp4
+s
.udp6
, s
.udp4
, s
.udp6
);
4059 printf("TCP %-9d %-9d %-9d\n", s
.tcp4_hashed
+s
.tcp6_hashed
, s
.tcp4_hashed
, s
.tcp6_hashed
);
4060 printf("INET %-9d %-9d %-9d\n",
4061 s
.raw4
+s
.udp4
+s
.tcp4_hashed
+
4062 s
.raw6
+s
.udp6
+s
.tcp6_hashed
,
4063 s
.raw4
+s
.udp4
+s
.tcp4_hashed
,
4064 s
.raw6
+s
.udp6
+s
.tcp6_hashed
);
4065 printf("FRAG %-9d %-9d %-9d\n", s
.frag4
+s
.frag6
, s
.frag4
, s
.frag6
);
4072 static void _usage(FILE *dest
)
4075 "Usage: ss [ OPTIONS ]\n"
4076 " ss [ OPTIONS ] [ FILTER ]\n"
4077 " -h, --help this message\n"
4078 " -V, --version output version information\n"
4079 " -n, --numeric don't resolve service names\n"
4080 " -r, --resolve resolve host names\n"
4081 " -a, --all display all sockets\n"
4082 " -l, --listening display listening sockets\n"
4083 " -o, --options show timer information\n"
4084 " -e, --extended show detailed socket information\n"
4085 " -m, --memory show socket memory usage\n"
4086 " -p, --processes show process using socket\n"
4087 " -i, --info show internal TCP information\n"
4088 " -s, --summary show socket usage summary\n"
4089 " -b, --bpf show bpf filter socket information\n"
4090 " -E, --events continually display sockets as they are destroyed\n"
4091 " -Z, --context display process SELinux security contexts\n"
4092 " -z, --contexts display process and socket SELinux security contexts\n"
4093 " -N, --net switch to the specified network namespace name\n"
4095 " -4, --ipv4 display only IP version 4 sockets\n"
4096 " -6, --ipv6 display only IP version 6 sockets\n"
4097 " -0, --packet display PACKET sockets\n"
4098 " -t, --tcp display only TCP sockets\n"
4099 " -S, --sctp display only SCTP sockets\n"
4100 " -u, --udp display only UDP sockets\n"
4101 " -d, --dccp display only DCCP sockets\n"
4102 " -w, --raw display only RAW sockets\n"
4103 " -x, --unix display only Unix domain sockets\n"
4104 " --vsock display only vsock sockets\n"
4105 " -f, --family=FAMILY display sockets of type FAMILY\n"
4106 " FAMILY := {inet|inet6|link|unix|netlink|vsock|help}\n"
4108 " -K, --kill forcibly close sockets, display what was closed\n"
4109 " -H, --no-header Suppress header line\n"
4111 " -A, --query=QUERY, --socket=QUERY\n"
4112 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram}[,QUERY]\n"
4114 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
4115 " -F, --filter=FILE read filter information from FILE\n"
4116 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
4117 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
4118 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n"
4119 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4120 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4121 " bucket := {syn-recv|time-wait}\n"
4122 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n"
4126 static void help(void) __attribute__((noreturn
));
4127 static void help(void)
4133 static void usage(void) __attribute__((noreturn
));
4134 static void usage(void)
4141 static int scan_state(const char *state
)
4143 static const char * const sstate_namel
[] = {
4145 [SS_ESTABLISHED
] = "established",
4146 [SS_SYN_SENT
] = "syn-sent",
4147 [SS_SYN_RECV
] = "syn-recv",
4148 [SS_FIN_WAIT1
] = "fin-wait-1",
4149 [SS_FIN_WAIT2
] = "fin-wait-2",
4150 [SS_TIME_WAIT
] = "time-wait",
4151 [SS_CLOSE
] = "unconnected",
4152 [SS_CLOSE_WAIT
] = "close-wait",
4153 [SS_LAST_ACK
] = "last-ack",
4154 [SS_LISTEN
] = "listening",
4155 [SS_CLOSING
] = "closing",
4159 if (strcasecmp(state
, "close") == 0 ||
4160 strcasecmp(state
, "closed") == 0)
4161 return (1<<SS_CLOSE
);
4162 if (strcasecmp(state
, "syn-rcv") == 0)
4163 return (1<<SS_SYN_RECV
);
4164 if (strcasecmp(state
, "established") == 0)
4165 return (1<<SS_ESTABLISHED
);
4166 if (strcasecmp(state
, "all") == 0)
4168 if (strcasecmp(state
, "connected") == 0)
4169 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
));
4170 if (strcasecmp(state
, "synchronized") == 0)
4171 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
)|(1<<SS_SYN_SENT
));
4172 if (strcasecmp(state
, "bucket") == 0)
4173 return (1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
);
4174 if (strcasecmp(state
, "big") == 0)
4175 return SS_ALL
& ~((1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
));
4176 for (i
= 0; i
< SS_MAX
; i
++) {
4177 if (strcasecmp(state
, sstate_namel
[i
]) == 0)
4181 fprintf(stderr
, "ss: wrong state name: %s\n", state
);
4185 /* Values 'v' and 'V' are already used so a non-character is used */
4186 #define OPT_VSOCK 256
4188 static const struct option long_opts
[] = {
4189 { "numeric", 0, 0, 'n' },
4190 { "resolve", 0, 0, 'r' },
4191 { "options", 0, 0, 'o' },
4192 { "extended", 0, 0, 'e' },
4193 { "memory", 0, 0, 'm' },
4194 { "info", 0, 0, 'i' },
4195 { "processes", 0, 0, 'p' },
4196 { "bpf", 0, 0, 'b' },
4197 { "events", 0, 0, 'E' },
4198 { "dccp", 0, 0, 'd' },
4199 { "tcp", 0, 0, 't' },
4200 { "sctp", 0, 0, 'S' },
4201 { "udp", 0, 0, 'u' },
4202 { "raw", 0, 0, 'w' },
4203 { "unix", 0, 0, 'x' },
4204 { "vsock", 0, 0, OPT_VSOCK
},
4205 { "all", 0, 0, 'a' },
4206 { "listening", 0, 0, 'l' },
4207 { "ipv4", 0, 0, '4' },
4208 { "ipv6", 0, 0, '6' },
4209 { "packet", 0, 0, '0' },
4210 { "family", 1, 0, 'f' },
4211 { "socket", 1, 0, 'A' },
4212 { "query", 1, 0, 'A' },
4213 { "summary", 0, 0, 's' },
4214 { "diag", 1, 0, 'D' },
4215 { "filter", 1, 0, 'F' },
4216 { "version", 0, 0, 'V' },
4217 { "help", 0, 0, 'h' },
4218 { "context", 0, 0, 'Z' },
4219 { "contexts", 0, 0, 'z' },
4220 { "net", 1, 0, 'N' },
4221 { "kill", 0, 0, 'K' },
4222 { "no-header", 0, 0, 'H' },
4227 int main(int argc
, char *argv
[])
4232 const char *dump_tcpdiag
= NULL
;
4233 FILE *filter_fp
= NULL
;
4235 int state_filter
= 0;
4236 int addrp_width
, screen_width
= 80;
4238 while ((ch
= getopt_long(argc
, argv
,
4239 "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KHS",
4240 long_opts
, NULL
)) != EOF
) {
4243 resolve_services
= 0;
4263 user_ent_hash_build();
4273 filter_db_set(¤t_filter
, DCCP_DB
);
4276 filter_db_set(¤t_filter
, TCP_DB
);
4279 filter_db_set(¤t_filter
, SCTP_DB
);
4282 filter_db_set(¤t_filter
, UDP_DB
);
4285 filter_db_set(¤t_filter
, RAW_DB
);
4288 filter_af_set(¤t_filter
, AF_UNIX
);
4291 filter_af_set(¤t_filter
, AF_VSOCK
);
4294 state_filter
= SS_ALL
;
4297 state_filter
= (1 << SS_LISTEN
) | (1 << SS_CLOSE
);
4300 filter_af_set(¤t_filter
, AF_INET
);
4303 filter_af_set(¤t_filter
, AF_INET6
);
4306 filter_af_set(¤t_filter
, AF_PACKET
);
4309 if (strcmp(optarg
, "inet") == 0)
4310 filter_af_set(¤t_filter
, AF_INET
);
4311 else if (strcmp(optarg
, "inet6") == 0)
4312 filter_af_set(¤t_filter
, AF_INET6
);
4313 else if (strcmp(optarg
, "link") == 0)
4314 filter_af_set(¤t_filter
, AF_PACKET
);
4315 else if (strcmp(optarg
, "unix") == 0)
4316 filter_af_set(¤t_filter
, AF_UNIX
);
4317 else if (strcmp(optarg
, "netlink") == 0)
4318 filter_af_set(¤t_filter
, AF_NETLINK
);
4319 else if (strcmp(optarg
, "vsock") == 0)
4320 filter_af_set(¤t_filter
, AF_VSOCK
);
4321 else if (strcmp(optarg
, "help") == 0)
4324 fprintf(stderr
, "ss: \"%s\" is invalid family\n",
4334 current_filter
.dbs
= 0;
4335 state_filter
= state_filter
?
4336 state_filter
: SS_CONN
;
4342 if ((p1
= strchr(p
, ',')) != NULL
)
4344 if (strcmp(p
, "all") == 0) {
4345 filter_default_dbs(¤t_filter
);
4346 } else if (strcmp(p
, "inet") == 0) {
4347 filter_db_set(¤t_filter
, UDP_DB
);
4348 filter_db_set(¤t_filter
, DCCP_DB
);
4349 filter_db_set(¤t_filter
, TCP_DB
);
4350 filter_db_set(¤t_filter
, SCTP_DB
);
4351 filter_db_set(¤t_filter
, RAW_DB
);
4352 } else if (strcmp(p
, "udp") == 0) {
4353 filter_db_set(¤t_filter
, UDP_DB
);
4354 } else if (strcmp(p
, "dccp") == 0) {
4355 filter_db_set(¤t_filter
, DCCP_DB
);
4356 } else if (strcmp(p
, "tcp") == 0) {
4357 filter_db_set(¤t_filter
, TCP_DB
);
4358 } else if (strcmp(p
, "sctp") == 0) {
4359 filter_db_set(¤t_filter
, SCTP_DB
);
4360 } else if (strcmp(p
, "raw") == 0) {
4361 filter_db_set(¤t_filter
, RAW_DB
);
4362 } else if (strcmp(p
, "unix") == 0) {
4363 filter_db_set(¤t_filter
, UNIX_ST_DB
);
4364 filter_db_set(¤t_filter
, UNIX_DG_DB
);
4365 filter_db_set(¤t_filter
, UNIX_SQ_DB
);
4366 } else if (strcasecmp(p
, "unix_stream") == 0 ||
4367 strcmp(p
, "u_str") == 0) {
4368 filter_db_set(¤t_filter
, UNIX_ST_DB
);
4369 } else if (strcasecmp(p
, "unix_dgram") == 0 ||
4370 strcmp(p
, "u_dgr") == 0) {
4371 filter_db_set(¤t_filter
, UNIX_DG_DB
);
4372 } else if (strcasecmp(p
, "unix_seqpacket") == 0 ||
4373 strcmp(p
, "u_seq") == 0) {
4374 filter_db_set(¤t_filter
, UNIX_SQ_DB
);
4375 } else if (strcmp(p
, "packet") == 0) {
4376 filter_db_set(¤t_filter
, PACKET_R_DB
);
4377 filter_db_set(¤t_filter
, PACKET_DG_DB
);
4378 } else if (strcmp(p
, "packet_raw") == 0 ||
4379 strcmp(p
, "p_raw") == 0) {
4380 filter_db_set(¤t_filter
, PACKET_R_DB
);
4381 } else if (strcmp(p
, "packet_dgram") == 0 ||
4382 strcmp(p
, "p_dgr") == 0) {
4383 filter_db_set(¤t_filter
, PACKET_DG_DB
);
4384 } else if (strcmp(p
, "netlink") == 0) {
4385 filter_db_set(¤t_filter
, NETLINK_DB
);
4386 } else if (strcmp(p
, "vsock") == 0) {
4387 filter_db_set(¤t_filter
, VSOCK_ST_DB
);
4388 filter_db_set(¤t_filter
, VSOCK_DG_DB
);
4389 } else if (strcmp(p
, "vsock_stream") == 0 ||
4390 strcmp(p
, "v_str") == 0) {
4391 filter_db_set(¤t_filter
, VSOCK_ST_DB
);
4392 } else if (strcmp(p
, "vsock_dgram") == 0 ||
4393 strcmp(p
, "v_dgr") == 0) {
4394 filter_db_set(¤t_filter
, VSOCK_DG_DB
);
4396 fprintf(stderr
, "ss: \"%s\" is illegal socket table id\n", p
);
4407 dump_tcpdiag
= optarg
;
4411 fprintf(stderr
, "More than one filter file\n");
4414 if (optarg
[0] == '-')
4417 filter_fp
= fopen(optarg
, "r");
4419 perror("fopen filter file");
4425 printf("ss utility, iproute2-ss%s\n", SNAPSHOT
);
4431 if (is_selinux_enabled() <= 0) {
4432 fprintf(stderr
, "ss: SELinux is not enabled.\n");
4436 user_ent_hash_build();
4439 if (netns_switch(optarg
))
4443 current_filter
.kill
= 1;
4461 if (do_default
&& argc
== 0)
4466 if (strcmp(*argv
, "state") == 0) {
4470 state_filter
|= scan_state(*argv
);
4472 } else if (strcmp(*argv
, "exclude") == 0 ||
4473 strcmp(*argv
, "excl") == 0) {
4476 state_filter
= SS_ALL
;
4477 state_filter
&= ~scan_state(*argv
);
4486 state_filter
= state_filter
? state_filter
: SS_CONN
;
4487 filter_default_dbs(¤t_filter
);
4490 filter_states_set(¤t_filter
, state_filter
);
4491 filter_merge_defaults(¤t_filter
);
4493 if (resolve_services
&& resolve_hosts
&&
4494 (current_filter
.dbs
& (UNIX_DBM
|INET_L4_DBM
)))
4495 init_service_resolver();
4497 if (current_filter
.dbs
== 0) {
4498 fprintf(stderr
, "ss: no socket tables to show with such filter.\n");
4501 if (current_filter
.families
== 0) {
4502 fprintf(stderr
, "ss: no families to show with such filter.\n");
4505 if (current_filter
.states
== 0) {
4506 fprintf(stderr
, "ss: no socket states to show with such filter.\n");
4511 FILE *dump_fp
= stdout
;
4513 if (!(current_filter
.dbs
& (1<<TCP_DB
))) {
4514 fprintf(stderr
, "ss: tcpdiag dump requested and no tcp in filter.\n");
4517 if (dump_tcpdiag
[0] != '-') {
4518 dump_fp
= fopen(dump_tcpdiag
, "w");
4519 if (!dump_tcpdiag
) {
4520 perror("fopen dump file");
4524 inet_show_netlink(¤t_filter
, dump_fp
, IPPROTO_TCP
);
4529 if (ssfilter_parse(¤t_filter
.f
, argc
, argv
, filter_fp
))
4533 if (current_filter
.dbs
&(current_filter
.dbs
-1))
4537 if (current_filter
.states
&(current_filter
.states
-1))
4540 if (isatty(STDOUT_FILENO
)) {
4543 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
) != -1) {
4545 screen_width
= w
.ws_col
;
4549 addrp_width
= screen_width
;
4550 addrp_width
-= netid_width
+1;
4551 addrp_width
-= state_width
+1;
4554 if (addrp_width
&1) {
4557 else if (state_width
)
4564 serv_width
= resolve_services
? 7 : 5;
4566 if (addrp_width
< 15+serv_width
+1)
4567 addrp_width
= 15+serv_width
+1;
4569 addr_width
= addrp_width
- serv_width
- 1;
4573 printf("%-*s ", netid_width
, "Netid");
4575 printf("%-*s ", state_width
, "State");
4576 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
4579 /* Make enough space for the local/remote port field */
4584 printf("%*s:%-*s %*s:%-*s\n",
4585 addr_width
, "Local Address", serv_width
, "Port",
4586 addr_width
, "Peer Address", serv_width
, "Port");
4592 exit(handle_follow_request(¤t_filter
));
4594 if (current_filter
.dbs
& (1<<NETLINK_DB
))
4595 netlink_show(¤t_filter
);
4596 if (current_filter
.dbs
& PACKET_DBM
)
4597 packet_show(¤t_filter
);
4598 if (current_filter
.dbs
& UNIX_DBM
)
4599 unix_show(¤t_filter
);
4600 if (current_filter
.dbs
& (1<<RAW_DB
))
4601 raw_show(¤t_filter
);
4602 if (current_filter
.dbs
& (1<<UDP_DB
))
4603 udp_show(¤t_filter
);
4604 if (current_filter
.dbs
& (1<<TCP_DB
))
4605 tcp_show(¤t_filter
);
4606 if (current_filter
.dbs
& (1<<DCCP_DB
))
4607 dccp_show(¤t_filter
);
4608 if (current_filter
.dbs
& (1<<SCTP_DB
))
4609 sctp_show(¤t_filter
);
4610 if (current_filter
.dbs
& VSOCK_DBM
)
4611 vsock_show(¤t_filter
);
4613 if (show_users
|| show_proc_ctx
|| show_sock_ctx
)