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>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
19 #include <sys/sysmacros.h>
20 #include <netinet/in.h>
24 #include <arpa/inet.h>
35 #include "libnetlink.h"
36 #include "namespace.h"
40 #include <linux/tcp.h>
41 #include <linux/sock_diag.h>
42 #include <linux/inet_diag.h>
43 #include <linux/unix_diag.h>
44 #include <linux/netdevice.h> /* for MAX_ADDR_LEN */
45 #include <linux/filter.h>
46 #include <linux/xdp_diag.h>
47 #include <linux/packet_diag.h>
48 #include <linux/netlink_diag.h>
49 #include <linux/sctp.h>
50 #include <linux/vm_sockets_diag.h>
51 #include <linux/net.h>
52 #include <linux/tipc.h>
53 #include <linux/tipc_netlink.h>
54 #include <linux/tipc_sockets_diag.h>
55 #include <linux/tls.h>
57 /* AF_VSOCK/PF_VSOCK is only provided since glibc 2.18 */
62 #define AF_VSOCK PF_VSOCK
65 #define MAGIC_SEQ 123456
66 #define BUF_CHUNK (1024 * 1024) /* Buffer chunk allocation size */
67 #define BUF_CHUNKS_MAX 5 /* Maximum number of allocated buffer chunks */
68 #define LEN_ALIGN(x) (((x) + 1) & ~1)
70 #define DIAG_REQUEST(_req, _r) \
72 struct nlmsghdr nlh; \
76 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
77 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
78 .nlmsg_seq = MAGIC_SEQ, \
79 .nlmsg_len = sizeof(_req), \
84 #include <selinux/selinux.h>
86 /* Stubs for SELinux functions */
87 static int is_selinux_enabled(void)
92 static int getpidcon(pid_t pid
, char **context
)
98 static int getfilecon(char *path
, char **context
)
104 static int security_get_initial_context(char *name
, char **context
)
111 int preferred_family
= AF_UNSPEC
;
112 static int show_options
;
114 static int show_users
;
116 static int show_tcpinfo
;
118 static int show_proc_ctx
;
119 static int show_sock_ctx
;
120 static int show_header
= 1;
121 static int follow_events
;
123 static int show_tipcinfo
;
148 const enum col_align align
;
152 int width
; /* Calculated, including additional layout spacing */
153 int max_len
; /* Measured maximum field length in this column */
156 static struct column columns
[] = {
157 { ALIGN_LEFT
, "Netid", "", 0, 0, 0 },
158 { ALIGN_LEFT
, "State", " ", 0, 0, 0 },
159 { ALIGN_LEFT
, "Recv-Q", " ", 0, 0, 0 },
160 { ALIGN_LEFT
, "Send-Q", " ", 0, 0, 0 },
161 { ALIGN_RIGHT
, "Local Address:", " ", 0, 0, 0 },
162 { ALIGN_LEFT
, "Port", "", 0, 0, 0 },
163 { ALIGN_RIGHT
, "Peer Address:", " ", 0, 0, 0 },
164 { ALIGN_LEFT
, "Port", "", 0, 0, 0 },
165 { ALIGN_LEFT
, "Process", "", 0, 0, 0 },
166 { ALIGN_LEFT
, "", "", 0, 0, 0 },
169 static struct column
*current_field
= columns
;
171 /* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to
172 * the buffer as a variable size token. A token consists of a 16 bits length
173 * field, followed by a string which is not NULL-terminated.
175 * A new chunk is allocated and linked when the current chunk doesn't have
176 * enough room to store the current token as a whole.
179 struct buf_chunk
*next
; /* Next chained chunk */
180 char *end
; /* Current end of content */
185 uint16_t len
; /* Data length, excluding length descriptor */
190 struct buf_token
*cur
; /* Position of current token in chunk */
191 struct buf_chunk
*head
; /* First chunk */
192 struct buf_chunk
*tail
; /* Current chunk */
193 int chunks
; /* Number of allocated chunks */
196 static const char *TCP_PROTO
= "tcp";
197 static const char *SCTP_PROTO
= "sctp";
198 static const char *UDP_PROTO
= "udp";
199 static const char *RAW_PROTO
= "raw";
200 static const char *dg_proto
;
221 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
222 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
223 #define ALL_DB ((1<<MAX_DB)-1)
224 #define INET_L4_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB))
225 #define INET_DBM (INET_L4_DBM | (1<<RAW_DB))
226 #define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB))
245 SCTP_STATE_CLOSED
= 0,
246 SCTP_STATE_COOKIE_WAIT
= 1,
247 SCTP_STATE_COOKIE_ECHOED
= 2,
248 SCTP_STATE_ESTABLISHED
= 3,
249 SCTP_STATE_SHUTDOWN_PENDING
= 4,
250 SCTP_STATE_SHUTDOWN_SENT
= 5,
251 SCTP_STATE_SHUTDOWN_RECEIVED
= 6,
252 SCTP_STATE_SHUTDOWN_ACK_SENT
= 7,
255 #define SS_ALL ((1 << SS_MAX) - 1)
256 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
257 #define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE))
259 #include "ssfilter.h"
267 struct rtnl_handle
*rth_for_killing
;
270 #define FAMILY_MASK(family) ((uint64_t)1 << (family))
272 static const struct filter default_dbs
[MAX_DB
] = {
275 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
279 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
282 .states
= (1 << SS_ESTABLISHED
),
283 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
286 .states
= (1 << SS_ESTABLISHED
),
287 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
290 .states
= (1 << SS_CLOSE
),
291 .families
= FAMILY_MASK(AF_UNIX
),
295 .families
= FAMILY_MASK(AF_UNIX
),
299 .families
= FAMILY_MASK(AF_UNIX
),
302 .states
= (1 << SS_CLOSE
),
303 .families
= FAMILY_MASK(AF_PACKET
),
306 .states
= (1 << SS_CLOSE
),
307 .families
= FAMILY_MASK(AF_PACKET
),
310 .states
= (1 << SS_CLOSE
),
311 .families
= FAMILY_MASK(AF_NETLINK
),
315 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
319 .families
= FAMILY_MASK(AF_VSOCK
),
323 .families
= FAMILY_MASK(AF_VSOCK
),
326 .states
= TIPC_SS_CONN
,
327 .families
= FAMILY_MASK(AF_TIPC
),
330 .states
= (1 << SS_CLOSE
),
331 .families
= FAMILY_MASK(AF_XDP
),
335 static const struct filter default_afs
[AF_MAX
] = {
350 .states
= (1 << SS_CLOSE
),
353 .dbs
= (1 << NETLINK_DB
),
354 .states
= (1 << SS_CLOSE
),
361 .dbs
= (1 << TIPC_DB
),
362 .states
= TIPC_SS_CONN
,
365 .dbs
= (1 << XDP_DB
),
366 .states
= (1 << SS_CLOSE
),
370 static int do_default
= 1;
371 static struct filter current_filter
;
373 static void filter_db_set(struct filter
*f
, int db
, bool enable
)
376 f
->states
|= default_dbs
[db
].states
;
379 f
->dbs
&= ~(1 << db
);
384 static int filter_db_parse(struct filter
*f
, const char *s
)
390 #define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } }
391 ENTRY(all
, UDP_DB
, DCCP_DB
, TCP_DB
, RAW_DB
,
392 UNIX_ST_DB
, UNIX_DG_DB
, UNIX_SQ_DB
,
393 PACKET_R_DB
, PACKET_DG_DB
, NETLINK_DB
,
394 SCTP_DB
, VSOCK_ST_DB
, VSOCK_DG_DB
, XDP_DB
),
395 ENTRY(inet
, UDP_DB
, DCCP_DB
, TCP_DB
, SCTP_DB
, RAW_DB
),
397 ENTRY(dccp
, DCCP_DB
),
399 ENTRY(sctp
, SCTP_DB
),
401 ENTRY(unix
, UNIX_ST_DB
, UNIX_DG_DB
, UNIX_SQ_DB
),
402 ENTRY(unix_stream
, UNIX_ST_DB
),
403 ENTRY(u_str
, UNIX_ST_DB
), /* alias for unix_stream */
404 ENTRY(unix_dgram
, UNIX_DG_DB
),
405 ENTRY(u_dgr
, UNIX_DG_DB
), /* alias for unix_dgram */
406 ENTRY(unix_seqpacket
, UNIX_SQ_DB
),
407 ENTRY(u_seq
, UNIX_SQ_DB
), /* alias for unix_seqpacket */
408 ENTRY(packet
, PACKET_R_DB
, PACKET_DG_DB
),
409 ENTRY(packet_raw
, PACKET_R_DB
),
410 ENTRY(p_raw
, PACKET_R_DB
), /* alias for packet_raw */
411 ENTRY(packet_dgram
, PACKET_DG_DB
),
412 ENTRY(p_dgr
, PACKET_DG_DB
), /* alias for packet_dgram */
413 ENTRY(netlink
, NETLINK_DB
),
414 ENTRY(vsock
, VSOCK_ST_DB
, VSOCK_DG_DB
),
415 ENTRY(vsock_stream
, VSOCK_ST_DB
),
416 ENTRY(v_str
, VSOCK_ST_DB
), /* alias for vsock_stream */
417 ENTRY(vsock_dgram
, VSOCK_DG_DB
),
418 ENTRY(v_dgr
, VSOCK_DG_DB
), /* alias for vsock_dgram */
430 for (i
= 0; i
< ARRAY_SIZE(db_name_tbl
); i
++) {
431 if (strcmp(s
, db_name_tbl
[i
].name
))
433 for (dbp
= db_name_tbl
[i
].dbs
; *dbp
!= MAX_DB
; dbp
++)
434 filter_db_set(f
, *dbp
, enable
);
440 static void filter_af_set(struct filter
*f
, int af
)
442 f
->states
|= default_afs
[af
].states
;
443 f
->families
|= FAMILY_MASK(af
);
445 preferred_family
= af
;
448 static int filter_af_get(struct filter
*f
, int af
)
450 return !!(f
->families
& FAMILY_MASK(af
));
453 static void filter_states_set(struct filter
*f
, int states
)
459 static void filter_merge_defaults(struct filter
*f
)
464 for (db
= 0; db
< MAX_DB
; db
++) {
465 if (!(f
->dbs
& (1 << db
)))
468 if (!(default_dbs
[db
].families
& f
->families
))
469 f
->families
|= default_dbs
[db
].families
;
471 for (af
= 0; af
< AF_MAX
; af
++) {
472 if (!(f
->families
& FAMILY_MASK(af
)))
475 if (!(default_afs
[af
].dbs
& f
->dbs
))
476 f
->dbs
|= default_afs
[af
].dbs
;
480 static FILE *generic_proc_open(const char *env
, const char *name
)
482 const char *p
= getenv(env
);
486 p
= getenv("PROC_ROOT") ? : "/proc";
487 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
491 return fopen(p
, "r");
493 #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp")
494 #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6")
495 #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp")
496 #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6")
497 #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw")
498 #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6")
499 #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix")
500 #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \
502 #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \
504 #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \
506 #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \
508 #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp")
509 #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \
510 "sys/net/ipv4/ip_local_port_range")
513 struct user_ent
*next
;
522 #define USER_ENT_HASH_SIZE 256
523 static struct user_ent
*user_ent_hash
[USER_ENT_HASH_SIZE
];
525 static int user_ent_hashfn(unsigned int ino
)
527 int val
= (ino
>> 24) ^ (ino
>> 16) ^ (ino
>> 8) ^ ino
;
529 return val
& (USER_ENT_HASH_SIZE
- 1);
532 static void user_ent_add(unsigned int ino
, char *process
,
537 struct user_ent
*p
, **pp
;
539 p
= malloc(sizeof(struct user_ent
));
541 fprintf(stderr
, "ss: failed to malloc buffer\n");
548 p
->process
= strdup(process
);
549 p
->process_ctx
= strdup(proc_ctx
);
550 p
->socket_ctx
= strdup(sock_ctx
);
552 pp
= &user_ent_hash
[user_ent_hashfn(ino
)];
557 static void user_ent_destroy(void)
559 struct user_ent
*p
, *p_next
;
562 while (cnt
!= USER_ENT_HASH_SIZE
) {
563 p
= user_ent_hash
[cnt
];
566 free(p
->process_ctx
);
576 static void user_ent_hash_build(void)
578 const char *root
= getenv("PROC_ROOT") ? : "/proc/";
585 const char *no_ctx
= "unavailable";
586 static int user_ent_hash_build_init
;
588 /* If show_users & show_proc_ctx set only do this once */
589 if (user_ent_hash_build_init
!= 0)
592 user_ent_hash_build_init
= 1;
594 strlcpy(name
, root
, sizeof(name
));
596 if (strlen(name
) == 0 || name
[strlen(name
)-1] != '/')
599 nameoff
= strlen(name
);
605 while ((d
= readdir(dir
)) != NULL
) {
613 if (sscanf(d
->d_name
, "%d%c", &pid
, &crap
) != 1)
616 if (getpidcon(pid
, &pid_context
) != 0)
617 pid_context
= strdup(no_ctx
);
619 snprintf(name
+ nameoff
, sizeof(name
) - nameoff
, "%d/fd/", pid
);
621 if ((dir1
= opendir(name
)) == NULL
) {
629 while ((d1
= readdir(dir1
)) != NULL
) {
630 const char *pattern
= "socket:[";
637 if (sscanf(d1
->d_name
, "%d%c", &fd
, &crap
) != 1)
640 snprintf(name
+pos
, sizeof(name
) - pos
, "%d", fd
);
642 link_len
= readlink(name
, lnk
, sizeof(lnk
)-1);
645 lnk
[link_len
] = '\0';
647 if (strncmp(lnk
, pattern
, strlen(pattern
)))
650 sscanf(lnk
, "socket:[%u]", &ino
);
652 snprintf(tmp
, sizeof(tmp
), "%s/%d/fd/%s",
653 root
, pid
, d1
->d_name
);
655 if (getfilecon(tmp
, &sock_context
) <= 0)
656 sock_context
= strdup(no_ctx
);
661 snprintf(tmp
, sizeof(tmp
), "%s/%d/stat",
663 if ((fp
= fopen(tmp
, "r")) != NULL
) {
664 if (fscanf(fp
, "%*d (%[^)])", p
) < 1)
669 user_ent_add(ino
, p
, pid
, fd
,
670 pid_context
, sock_context
);
685 #define ENTRY_BUF_SIZE 512
686 static int find_entry(unsigned int ino
, char **buf
, int type
)
692 int len
, new_buf_len
;
699 p
= user_ent_hash
[user_ent_hashfn(ino
)];
706 ptr
= *buf
+ buf_used
;
709 len
= snprintf(ptr
, buf_len
- buf_used
,
710 "(\"%s\",pid=%d,fd=%d),",
711 p
->process
, p
->pid
, p
->fd
);
714 len
= snprintf(ptr
, buf_len
- buf_used
,
715 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
717 p
->process_ctx
, p
->fd
);
720 len
= snprintf(ptr
, buf_len
- buf_used
,
721 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
723 p
->process_ctx
, p
->fd
,
727 fprintf(stderr
, "ss: invalid type: %d\n", type
);
731 if (len
< 0 || len
>= buf_len
- buf_used
) {
732 new_buf_len
= buf_len
+ ENTRY_BUF_SIZE
;
733 new_buf
= realloc(*buf
, new_buf_len
);
735 fprintf(stderr
, "ss: failed to malloc buffer\n");
739 buf_len
= new_buf_len
;
751 ptr
= *buf
+ buf_used
;
757 static unsigned long long cookie_sk_get(const uint32_t *cookie
)
759 return (((unsigned long long)cookie
[1] << 31) << 1) | cookie
[0];
762 static const char *sctp_sstate_name
[] = {
763 [SCTP_STATE_CLOSED
] = "CLOSED",
764 [SCTP_STATE_COOKIE_WAIT
] = "COOKIE_WAIT",
765 [SCTP_STATE_COOKIE_ECHOED
] = "COOKIE_ECHOED",
766 [SCTP_STATE_ESTABLISHED
] = "ESTAB",
767 [SCTP_STATE_SHUTDOWN_PENDING
] = "SHUTDOWN_PENDING",
768 [SCTP_STATE_SHUTDOWN_SENT
] = "SHUTDOWN_SENT",
769 [SCTP_STATE_SHUTDOWN_RECEIVED
] = "SHUTDOWN_RECEIVED",
770 [SCTP_STATE_SHUTDOWN_ACK_SENT
] = "ACK_SENT",
773 static const char * const stype_nameg
[] = {
775 [SOCK_STREAM
] = "STREAM",
776 [SOCK_DGRAM
] = "DGRAM",
778 [SOCK_SEQPACKET
] = "SEQPACKET",
782 struct sockstat
*next
;
796 unsigned long long sk
;
803 unsigned int ce_state
;
813 unsigned int timeout
;
816 double rto
, ato
, rtt
, rttvar
;
817 int qack
, ssthresh
, backoff
;
826 unsigned int lastsnd
;
827 unsigned int lastrcv
;
828 unsigned int lastack
;
830 double pacing_rate_max
;
831 double delivery_rate
;
832 unsigned long long bytes_acked
;
833 unsigned long long bytes_received
;
834 unsigned int segs_out
;
835 unsigned int segs_in
;
836 unsigned int data_segs_out
;
837 unsigned int data_segs_in
;
838 unsigned int unacked
;
839 unsigned int retrans
;
840 unsigned int retrans_total
;
843 unsigned int fackets
;
844 unsigned int reordering
;
845 unsigned int not_sent
;
846 unsigned int delivered
;
847 unsigned int delivered_ce
;
848 unsigned int dsack_dups
;
849 unsigned int reord_seen
;
853 unsigned int rcv_ssthresh
;
854 unsigned long long busy_time
;
855 unsigned long long rwnd_limited
;
856 unsigned long long sndbuf_limited
;
857 unsigned long long bytes_sent
;
858 unsigned long long bytes_retrans
;
862 bool has_ecnseen_opt
;
863 bool has_fastopen_opt
;
866 struct dctcpstat
*dctcp
;
867 struct tcp_bbr_info
*bbr_info
;
870 /* SCTP assocs share the same inode number with their parent endpoint. So if we
871 * have seen the inode number before, it must be an assoc instead of the next
873 static bool is_sctp_assoc(struct sockstat
*s
, const char *sock_name
)
875 if (strcmp(sock_name
, "sctp"))
877 if (!sctp_ino
|| sctp_ino
!= s
->ino
)
882 static const char *unix_netid_name(int type
)
895 static const char *proto_name(int protocol
)
915 static const char *vsock_netid_name(int type
)
927 static const char *tipc_netid_name(int type
)
943 /* Allocate and initialize a new buffer chunk */
944 static struct buf_chunk
*buf_chunk_new(void)
946 struct buf_chunk
*new = malloc(BUF_CHUNK
);
953 /* This is also the last block */
956 /* Next token will be stored at the beginning of chunk data area, and
957 * its initial length is zero.
959 buffer
.cur
= (struct buf_token
*)new->data
;
962 new->end
= buffer
.cur
->data
;
969 /* Return available tail room in given chunk */
970 static int buf_chunk_avail(struct buf_chunk
*chunk
)
972 return BUF_CHUNK
- offsetof(struct buf_chunk
, data
) -
973 (chunk
->end
- chunk
->data
);
976 /* Update end pointer and token length, link new chunk if we hit the end of the
977 * current one. Return -EAGAIN if we got a new chunk, caller has to print again.
979 static int buf_update(int len
)
981 struct buf_chunk
*chunk
= buffer
.tail
;
982 struct buf_token
*t
= buffer
.cur
;
984 /* Claim success if new content fits in the current chunk, and anyway
985 * if this is the first token in the chunk: in the latter case,
986 * allocating a new chunk won't help, so we'll just cut the output.
988 if ((len
< buf_chunk_avail(chunk
) && len
!= -1 /* glibc < 2.0.6 */) ||
989 t
== (struct buf_token
*)chunk
->data
) {
990 len
= min(len
, buf_chunk_avail(chunk
));
992 /* Total field length can't exceed 2^16 bytes, cut as needed */
993 len
= min(len
, USHRT_MAX
- t
->len
);
1000 /* Content truncated, time to allocate more */
1001 chunk
->next
= buf_chunk_new();
1003 /* Copy current token over to new chunk, including length descriptor */
1004 memcpy(chunk
->next
->data
, t
, sizeof(t
->len
) + t
->len
);
1005 chunk
->next
->end
+= t
->len
;
1007 /* Discard partially written field in old chunk */
1008 chunk
->end
-= t
->len
+ sizeof(t
->len
);
1013 /* Append content to buffer as part of the current field */
1014 __attribute__((format(printf
, 1, 2)))
1015 static void out(const char *fmt
, ...)
1017 struct column
*f
= current_field
;
1026 buffer
.head
= buf_chunk_new();
1028 again
: /* Append to buffer: if we have a new chunk, print again */
1030 pos
= buffer
.cur
->data
+ buffer
.cur
->len
;
1031 va_start(args
, fmt
);
1033 /* Limit to tail room. If we hit the limit, buf_update() will tell us */
1034 len
= vsnprintf(pos
, buf_chunk_avail(buffer
.tail
), fmt
, args
);
1037 if (buf_update(len
))
1041 static int print_left_spacing(struct column
*f
, int stored
, int printed
)
1045 if (!f
->width
|| f
->align
== ALIGN_LEFT
)
1048 s
= f
->width
- stored
- printed
;
1049 if (f
->align
== ALIGN_CENTER
)
1050 /* If count of total spacing is odd, shift right by one */
1054 return printf("%*c", s
, ' ');
1059 static void print_right_spacing(struct column
*f
, int printed
)
1063 if (!f
->width
|| f
->align
== ALIGN_RIGHT
)
1066 s
= f
->width
- printed
;
1067 if (f
->align
== ALIGN_CENTER
)
1071 printf("%*c", s
, ' ');
1074 /* Done with field: update buffer pointer, start new token after current one */
1075 static void field_flush(struct column
*f
)
1077 struct buf_chunk
*chunk
;
1083 chunk
= buffer
.tail
;
1084 pad
= buffer
.cur
->len
% 2;
1086 if (buffer
.cur
->len
> f
->max_len
)
1087 f
->max_len
= buffer
.cur
->len
;
1089 /* We need a new chunk if we can't store the next length descriptor.
1090 * Mind the gap between end of previous token and next aligned position
1091 * for length descriptor.
1093 if (buf_chunk_avail(chunk
) - pad
< sizeof(buffer
.cur
->len
)) {
1095 chunk
->next
= buf_chunk_new();
1099 buffer
.cur
= (struct buf_token
*)(buffer
.cur
->data
+
1100 LEN_ALIGN(buffer
.cur
->len
));
1101 buffer
.cur
->len
= 0;
1102 buffer
.tail
->end
= buffer
.cur
->data
;
1105 static int field_is_last(struct column
*f
)
1107 return f
- columns
== COL_MAX
- 1;
1110 /* Get the next available token in the buffer starting from the current token */
1111 static struct buf_token
*buf_token_next(struct buf_token
*cur
)
1113 struct buf_chunk
*chunk
= buffer
.tail
;
1115 /* If we reached the end of chunk contents, get token from next chunk */
1116 if (cur
->data
+ LEN_ALIGN(cur
->len
) == chunk
->end
) {
1117 buffer
.tail
= chunk
= chunk
->next
;
1118 return chunk
? (struct buf_token
*)chunk
->data
: NULL
;
1121 return (struct buf_token
*)(cur
->data
+ LEN_ALIGN(cur
->len
));
1124 /* Free up all allocated buffer chunks */
1125 static void buf_free_all(void)
1127 struct buf_chunk
*tmp
;
1129 for (buffer
.tail
= buffer
.head
; buffer
.tail
; ) {
1131 buffer
.tail
= buffer
.tail
->next
;
1138 /* Get current screen width, returns -1 if TIOCGWINSZ fails */
1139 static int render_screen_width(void)
1143 if (isatty(STDOUT_FILENO
)) {
1146 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
) != -1) {
1155 /* Calculate column width from contents length. If columns don't fit on one
1156 * line, break them into the least possible amount of lines and keep them
1157 * aligned across lines. Available screen space is equally spread between fields
1158 * as additional spacing.
1160 static void render_calc_width(void)
1162 int screen_width
, first
, len
= 0, linecols
= 0;
1163 struct column
*c
, *eol
= columns
- 1;
1164 bool compact_output
= false;
1166 screen_width
= render_screen_width();
1167 if (screen_width
== -1) {
1168 screen_width
= INT_MAX
;
1169 compact_output
= true;
1172 /* First pass: set width for each column to measured content length */
1173 for (first
= 1, c
= columns
; c
- columns
< COL_MAX
; c
++) {
1177 if (!first
&& c
->max_len
)
1178 c
->width
= c
->max_len
+ strlen(c
->ldelim
);
1180 c
->width
= c
->max_len
;
1182 /* But don't exceed screen size. If we exceed the screen size
1183 * for even a single field, it will just start on a line of its
1184 * own and then naturally wrap.
1186 c
->width
= min(c
->width
, screen_width
);
1192 if (compact_output
) {
1193 /* Compact output, skip extending columns. */
1197 /* Second pass: find out newlines and distribute available spacing */
1198 for (c
= columns
; c
- columns
< COL_MAX
; c
++) {
1199 int pad
, spacing
, rem
, last
;
1208 for (last
= 1, tmp
= c
+ 1; tmp
- columns
< COL_MAX
; tmp
++) {
1215 if (!last
&& len
< screen_width
) {
1216 /* Columns fit on screen so far, nothing to do yet */
1220 if (len
== screen_width
) {
1221 /* Exact fit, just start with new line */
1225 if (len
> screen_width
) {
1226 /* Screen width exceeded: go back one column */
1232 /* Distribute remaining space to columns on this line */
1233 pad
= screen_width
- len
;
1234 spacing
= pad
/ linecols
;
1235 rem
= pad
% linecols
;
1236 for (tmp
= c
; tmp
> eol
; tmp
--) {
1240 tmp
->width
+= spacing
;
1248 /* Line break: reset line counters, mark end-of-line */
1255 /* Render buffered output with spacing and delimiters, then free up buffers */
1256 static void render(void)
1258 struct buf_token
*token
;
1259 int printed
, line_started
= 0;
1265 token
= (struct buf_token
*)buffer
.head
->data
;
1267 /* Ensure end alignment of last token, it wasn't necessarily flushed */
1268 buffer
.tail
->end
+= buffer
.cur
->len
% 2;
1270 render_calc_width();
1272 /* Rewind and replay */
1273 buffer
.tail
= buffer
.head
;
1280 /* Print left delimiter only if we already started a line */
1282 printed
= printf("%s", f
->ldelim
);
1286 /* Print field content from token data with spacing */
1287 printed
+= print_left_spacing(f
, token
->len
, printed
);
1288 printed
+= fwrite(token
->data
, 1, token
->len
, stdout
);
1289 print_right_spacing(f
, printed
);
1291 /* Go to next non-empty field, deal with end-of-line */
1293 if (field_is_last(f
)) {
1300 } while (f
->disabled
);
1302 token
= buf_token_next(token
);
1304 /* Deal with final end-of-line when the last non-empty field printed
1305 * is not the last field.
1311 current_field
= columns
;
1314 /* Move to next field, and render buffer if we reached the maximum number of
1315 * chunks, at the last field in a line.
1317 static void field_next(void)
1319 if (field_is_last(current_field
) && buffer
.chunks
>= BUF_CHUNKS_MAX
) {
1324 field_flush(current_field
);
1325 if (field_is_last(current_field
))
1326 current_field
= columns
;
1331 /* Walk through fields and flush them until we reach the desired one */
1332 static void field_set(enum col_id id
)
1334 while (id
!= current_field
- columns
)
1338 /* Print header for all non-empty columns */
1339 static void print_header(void)
1341 while (!field_is_last(current_field
)) {
1342 if (!current_field
->disabled
)
1343 out("%s", current_field
->header
);
1348 static void sock_state_print(struct sockstat
*s
)
1350 const char *sock_name
;
1351 static const char * const sstate_name
[] = {
1353 [SS_ESTABLISHED
] = "ESTAB",
1354 [SS_SYN_SENT
] = "SYN-SENT",
1355 [SS_SYN_RECV
] = "SYN-RECV",
1356 [SS_FIN_WAIT1
] = "FIN-WAIT-1",
1357 [SS_FIN_WAIT2
] = "FIN-WAIT-2",
1358 [SS_TIME_WAIT
] = "TIME-WAIT",
1359 [SS_CLOSE
] = "UNCONN",
1360 [SS_CLOSE_WAIT
] = "CLOSE-WAIT",
1361 [SS_LAST_ACK
] = "LAST-ACK",
1362 [SS_LISTEN
] = "LISTEN",
1363 [SS_CLOSING
] = "CLOSING",
1366 switch (s
->local
.family
) {
1368 sock_name
= unix_netid_name(s
->type
);
1372 sock_name
= proto_name(s
->type
);
1375 sock_name
= s
->type
== SOCK_RAW
? "p_raw" : "p_dgr";
1381 sock_name
= tipc_netid_name(s
->type
);
1384 sock_name
= vsock_netid_name(s
->type
);
1390 sock_name
= "unknown";
1393 if (is_sctp_assoc(s
, sock_name
)) {
1394 field_set(COL_STATE
); /* Empty Netid field */
1395 out("`- %s", sctp_sstate_name
[s
->state
]);
1397 field_set(COL_NETID
);
1398 out("%s", sock_name
);
1399 field_set(COL_STATE
);
1400 out("%s", sstate_name
[s
->state
]);
1403 field_set(COL_RECVQ
);
1405 field_set(COL_SENDQ
);
1407 field_set(COL_ADDR
);
1410 static void sock_details_print(struct sockstat
*s
)
1413 out(" uid:%u", s
->uid
);
1415 out(" ino:%u", s
->ino
);
1416 out(" sk:%llx", s
->sk
);
1419 out(" fwmark:0x%x", s
->mark
);
1422 static void sock_addr_print(const char *addr
, char *delim
, const char *port
,
1426 out("%s" "%%" "%s%s", addr
, ifname
, delim
);
1428 out("%s%s", addr
, delim
);
1435 static const char *print_ms_timer(unsigned int timeout
)
1437 static char buf
[64];
1438 int secs
, msecs
, minutes
;
1440 secs
= timeout
/1000;
1443 msecs
= timeout
%1000;
1447 snprintf(buf
, sizeof(buf
)-16, "%dmin", minutes
);
1454 sprintf(buf
+strlen(buf
), "%d%s", secs
, msecs
? "." : "sec");
1457 sprintf(buf
+strlen(buf
), "%03dms", msecs
);
1462 struct scache
*next
;
1468 static struct scache
*rlist
;
1470 static void init_service_resolver(void)
1473 FILE *fp
= popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
1478 if (!fgets(buf
, sizeof(buf
), fp
)) {
1482 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1483 unsigned int progn
, port
;
1484 char proto
[128], prog
[128] = "rpc.";
1487 if (sscanf(buf
, "%u %*d %s %u %s",
1488 &progn
, proto
, &port
, prog
+4) != 4)
1491 if (!(c
= malloc(sizeof(*c
))))
1495 c
->name
= strdup(prog
);
1496 if (strcmp(proto
, TCP_PROTO
) == 0)
1497 c
->proto
= TCP_PROTO
;
1498 else if (strcmp(proto
, UDP_PROTO
) == 0)
1499 c
->proto
= UDP_PROTO
;
1500 else if (strcmp(proto
, SCTP_PROTO
) == 0)
1501 c
->proto
= SCTP_PROTO
;
1510 /* Even do not try default linux ephemeral port ranges:
1511 * default /etc/services contains so much of useless crap
1512 * wouldbe "allocated" to this area that resolution
1513 * is really harmful. I shrug each time when seeing
1514 * "socks" or "cfinger" in dumps.
1516 static int is_ephemeral(int port
)
1518 static int min
= 0, max
;
1521 FILE *f
= ephemeral_ports_open();
1523 if (!f
|| fscanf(f
, "%d %d", &min
, &max
) < 2) {
1530 return port
>= min
&& port
<= max
;
1534 static const char *__resolve_service(int port
)
1538 for (c
= rlist
; c
; c
= c
->next
) {
1539 if (c
->port
== port
&& c
->proto
== dg_proto
)
1543 if (!is_ephemeral(port
)) {
1544 static int notfirst
;
1551 se
= getservbyport(htons(port
), dg_proto
);
1559 #define SCACHE_BUCKETS 1024
1560 static struct scache
*cache_htab
[SCACHE_BUCKETS
];
1562 static const char *resolve_service(int port
)
1564 static char buf
[128];
1578 if (dg_proto
== RAW_PROTO
)
1579 return inet_proto_n2a(port
, buf
, sizeof(buf
));
1582 hash
= (port
^(((unsigned long)dg_proto
)>>2)) % SCACHE_BUCKETS
;
1584 for (c
= cache_htab
[hash
]; c
; c
= c
->next
) {
1585 if (c
->port
== port
&& c
->proto
== dg_proto
)
1589 c
= malloc(sizeof(*c
));
1592 res
= __resolve_service(port
);
1594 c
->name
= res
? strdup(res
) : NULL
;
1595 c
->proto
= dg_proto
;
1596 c
->next
= cache_htab
[hash
];
1597 cache_htab
[hash
] = c
;
1604 sprintf(buf
, "%u", port
);
1608 static void inet_addr_print(const inet_prefix
*a
, int port
,
1609 unsigned int ifindex
, bool v6only
)
1612 const char *ap
= buf
;
1613 const char *ifname
= NULL
;
1615 if (a
->family
== AF_INET
) {
1616 ap
= format_host(AF_INET
, 4, a
->data
);
1619 !memcmp(a
->data
, &in6addr_any
, sizeof(in6addr_any
))) {
1623 ap
= format_host(a
->family
, 16, a
->data
);
1625 /* Numeric IPv6 addresses should be bracketed */
1626 if (strchr(ap
, ':')) {
1627 snprintf(buf
, sizeof(buf
),
1635 ifname
= ll_index_to_name(ifindex
);
1637 sock_addr_print(ap
, ":", resolve_service(port
), ifname
);
1646 struct aafilter
*next
;
1649 static int inet2_addr_match(const inet_prefix
*a
, const inet_prefix
*p
,
1652 if (!inet_addr_match(a
, p
, plen
))
1655 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1656 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1658 if (p
->family
== AF_INET
&& a
->family
== AF_INET6
) {
1659 if (a
->data
[0] == 0 && a
->data
[1] == 0 &&
1660 a
->data
[2] == htonl(0xffff)) {
1661 inet_prefix tmp
= *a
;
1663 tmp
.data
[0] = a
->data
[3];
1664 return inet_addr_match(&tmp
, p
, plen
);
1670 static int unix_match(const inet_prefix
*a
, const inet_prefix
*p
)
1672 char *addr
, *pattern
;
1674 memcpy(&addr
, a
->data
, sizeof(addr
));
1675 memcpy(&pattern
, p
->data
, sizeof(pattern
));
1676 if (pattern
== NULL
)
1680 return !fnmatch(pattern
, addr
, 0);
1683 static int run_ssfilter(struct ssfilter
*f
, struct sockstat
*s
)
1688 if (s
->local
.family
== AF_UNIX
) {
1691 memcpy(&p
, s
->local
.data
, sizeof(p
));
1692 return p
== NULL
|| (p
[0] == '@' && strlen(p
) == 6 &&
1693 strspn(p
+1, "0123456789abcdef") == 5);
1695 if (s
->local
.family
== AF_PACKET
)
1696 return s
->lport
== 0 && s
->local
.data
[0] == 0;
1697 if (s
->local
.family
== AF_NETLINK
)
1698 return s
->lport
< 0;
1699 if (s
->local
.family
== AF_VSOCK
)
1700 return s
->lport
> 1023;
1702 return is_ephemeral(s
->lport
);
1706 struct aafilter
*a
= (void *)f
->pred
;
1708 if (a
->addr
.family
== AF_UNIX
)
1709 return unix_match(&s
->remote
, &a
->addr
);
1710 if (a
->port
!= -1 && a
->port
!= s
->rport
)
1712 if (a
->addr
.bitlen
) {
1714 if (!inet2_addr_match(&s
->remote
, &a
->addr
, a
->addr
.bitlen
))
1716 } while ((a
= a
->next
) != NULL
);
1723 struct aafilter
*a
= (void *)f
->pred
;
1725 if (a
->addr
.family
== AF_UNIX
)
1726 return unix_match(&s
->local
, &a
->addr
);
1727 if (a
->port
!= -1 && a
->port
!= s
->lport
)
1729 if (a
->addr
.bitlen
) {
1731 if (!inet2_addr_match(&s
->local
, &a
->addr
, a
->addr
.bitlen
))
1733 } while ((a
= a
->next
) != NULL
);
1740 struct aafilter
*a
= (void *)f
->pred
;
1742 return s
->rport
>= a
->port
;
1746 struct aafilter
*a
= (void *)f
->pred
;
1748 return s
->rport
<= a
->port
;
1752 struct aafilter
*a
= (void *)f
->pred
;
1754 return s
->lport
>= a
->port
;
1758 struct aafilter
*a
= (void *)f
->pred
;
1760 return s
->lport
<= a
->port
;
1764 struct aafilter
*a
= (void *)f
->pred
;
1766 return s
->iface
== a
->iface
;
1770 struct aafilter
*a
= (void *)f
->pred
;
1772 return (s
->mark
& a
->mask
) == a
->mark
;
1774 /* Yup. It is recursion. Sorry. */
1776 return run_ssfilter(f
->pred
, s
) && run_ssfilter(f
->post
, s
);
1778 return run_ssfilter(f
->pred
, s
) || run_ssfilter(f
->post
, s
);
1780 return !run_ssfilter(f
->pred
, s
);
1786 /* Relocate external jumps by reloc. */
1787 static void ssfilter_patch(char *a
, int len
, int reloc
)
1790 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)a
;
1792 if (op
->no
== len
+4)
1801 static int ssfilter_bytecompile(struct ssfilter
*f
, char **bytecode
)
1806 if (!(*bytecode
= malloc(4))) abort();
1807 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_AUTO
, 4, 8 };
1813 struct aafilter
*a
= (void *)f
->pred
;
1816 int code
= (f
->type
== SSF_DCOND
? INET_DIAG_BC_D_COND
: INET_DIAG_BC_S_COND
);
1819 for (b
= a
; b
; b
= b
->next
) {
1820 len
+= 4 + sizeof(struct inet_diag_hostcond
);
1821 if (a
->addr
.family
== AF_INET6
)
1828 if (!(ptr
= malloc(len
))) abort();
1830 for (b
= a
; b
; b
= b
->next
) {
1831 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)ptr
;
1832 int alen
= (a
->addr
.family
== AF_INET6
? 16 : 4);
1833 int oplen
= alen
+ 4 + sizeof(struct inet_diag_hostcond
);
1834 struct inet_diag_hostcond
*cond
= (struct inet_diag_hostcond
*)(ptr
+4);
1836 *op
= (struct inet_diag_bc_op
){ code
, oplen
, oplen
+4 };
1837 cond
->family
= a
->addr
.family
;
1838 cond
->port
= a
->port
;
1839 cond
->prefix_len
= a
->addr
.bitlen
;
1840 memcpy(cond
->addr
, a
->addr
.data
, alen
);
1843 op
= (struct inet_diag_bc_op
*)ptr
;
1844 *op
= (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, len
- (ptr
-*bytecode
)};
1848 return ptr
- *bytecode
;
1852 struct aafilter
*x
= (void *)f
->pred
;
1854 if (!(*bytecode
= malloc(8))) abort();
1855 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_GE
, 8, 12 };
1856 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1861 struct aafilter
*x
= (void *)f
->pred
;
1863 if (!(*bytecode
= malloc(8))) abort();
1864 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_LE
, 8, 12 };
1865 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1870 struct aafilter
*x
= (void *)f
->pred
;
1872 if (!(*bytecode
= malloc(8))) abort();
1873 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_GE
, 8, 12 };
1874 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1879 struct aafilter
*x
= (void *)f
->pred
;
1881 if (!(*bytecode
= malloc(8))) abort();
1882 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_LE
, 8, 12 };
1883 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1889 char *a1
= NULL
, *a2
= NULL
, *a
;
1892 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1893 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1899 if (!(a
= malloc(l1
+l2
))) abort();
1901 memcpy(a
+l1
, a2
, l2
);
1903 ssfilter_patch(a
, l1
, l2
);
1909 char *a1
= NULL
, *a2
= NULL
, *a
;
1912 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1913 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1919 if (!(a
= malloc(l1
+l2
+4))) abort();
1921 memcpy(a
+l1
+4, a2
, l2
);
1923 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, l2
+4 };
1929 char *a1
= NULL
, *a
;
1932 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1937 if (!(a
= malloc(l1
+4))) abort();
1940 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, 8 };
1946 /* bytecompile for SSF_DEVCOND not supported yet */
1951 struct aafilter
*a
= (void *)f
->pred
;
1953 struct inet_diag_bc_op op
;
1954 struct inet_diag_markcond cond
;
1956 int inslen
= sizeof(struct instr
);
1958 if (!(*bytecode
= malloc(inslen
))) abort();
1959 ((struct instr
*)*bytecode
)[0] = (struct instr
) {
1960 { INET_DIAG_BC_MARK_COND
, inslen
, inslen
+ 4 },
1961 { a
->mark
, a
->mask
},
1971 static int remember_he(struct aafilter
*a
, struct hostent
*he
)
1973 char **ptr
= he
->h_addr_list
;
1977 if (he
->h_addrtype
== AF_INET
)
1979 else if (he
->h_addrtype
== AF_INET6
)
1985 struct aafilter
*b
= a
;
1987 if (a
->addr
.bitlen
) {
1988 if ((b
= malloc(sizeof(*b
))) == NULL
)
1993 memcpy(b
->addr
.data
, *ptr
, len
);
1994 b
->addr
.bytelen
= len
;
1995 b
->addr
.bitlen
= len
*8;
1996 b
->addr
.family
= he
->h_addrtype
;
2003 static int get_dns_host(struct aafilter
*a
, const char *addr
, int fam
)
2005 static int notfirst
;
2014 he
= gethostbyname2(addr
, fam
== AF_UNSPEC
? AF_INET
: fam
);
2016 cnt
= remember_he(a
, he
);
2017 if (fam
== AF_UNSPEC
) {
2018 he
= gethostbyname2(addr
, AF_INET6
);
2020 cnt
+= remember_he(a
, he
);
2025 static int xll_initted
;
2027 static void xll_init(void)
2029 struct rtnl_handle rth
;
2031 if (rtnl_open(&rth
, 0) < 0)
2039 static const char *xll_index_to_name(int index
)
2043 return ll_index_to_name(index
);
2046 static int xll_name_to_index(const char *dev
)
2050 return ll_name_to_index(dev
);
2053 void *parse_devcond(char *name
)
2055 struct aafilter a
= { .iface
= 0 };
2056 struct aafilter
*res
;
2058 a
.iface
= xll_name_to_index(name
);
2063 n
= strtoul(name
, &end
, 0);
2064 if (!end
|| end
== name
|| *end
|| n
> UINT_MAX
)
2070 res
= malloc(sizeof(*res
));
2076 static void vsock_set_inet_prefix(inet_prefix
*a
, __u32 cid
)
2079 .bytelen
= sizeof(cid
),
2082 memcpy(a
->data
, &cid
, sizeof(cid
));
2085 void *parse_hostcond(char *addr
, bool is_port
)
2088 struct aafilter a
= { .port
= -1 };
2089 struct aafilter
*res
;
2090 int fam
= preferred_family
;
2091 struct filter
*f
= ¤t_filter
;
2093 if (fam
== AF_UNIX
|| strncmp(addr
, "unix:", 5) == 0) {
2096 a
.addr
.family
= AF_UNIX
;
2097 if (strncmp(addr
, "unix:", 5) == 0)
2100 a
.addr
.bitlen
= 8*strlen(p
);
2101 memcpy(a
.addr
.data
, &p
, sizeof(p
));
2106 if (fam
== AF_PACKET
|| strncmp(addr
, "link:", 5) == 0) {
2107 a
.addr
.family
= AF_PACKET
;
2109 if (strncmp(addr
, "link:", 5) == 0)
2111 port
= strchr(addr
, ':');
2114 if (port
[1] && strcmp(port
+1, "*")) {
2115 if (get_integer(&a
.port
, port
+1, 0)) {
2116 if ((a
.port
= xll_name_to_index(port
+1)) <= 0)
2121 if (addr
[0] && strcmp(addr
, "*")) {
2125 if (ll_proto_a2n(&tmp
, addr
))
2127 a
.addr
.data
[0] = ntohs(tmp
);
2133 if (fam
== AF_NETLINK
|| strncmp(addr
, "netlink:", 8) == 0) {
2134 a
.addr
.family
= AF_NETLINK
;
2136 if (strncmp(addr
, "netlink:", 8) == 0)
2138 port
= strchr(addr
, ':');
2141 if (port
[1] && strcmp(port
+1, "*")) {
2142 if (get_integer(&a
.port
, port
+1, 0)) {
2143 if (strcmp(port
+1, "kernel") == 0)
2150 if (addr
[0] && strcmp(addr
, "*")) {
2152 if (nl_proto_a2n(&a
.addr
.data
[0], addr
) == -1)
2159 if (fam
== AF_VSOCK
|| strncmp(addr
, "vsock:", 6) == 0) {
2160 __u32 cid
= ~(__u32
)0;
2162 a
.addr
.family
= AF_VSOCK
;
2163 if (strncmp(addr
, "vsock:", 6) == 0)
2169 port
= strchr(addr
, ':');
2176 if (port
&& strcmp(port
, "*") &&
2177 get_u32((__u32
*)&a
.port
, port
, 0))
2180 if (addr
[0] && strcmp(addr
, "*")) {
2182 if (get_u32(&cid
, addr
, 0))
2185 vsock_set_inet_prefix(&a
.addr
, cid
);
2190 if (fam
== AF_INET
|| !strncmp(addr
, "inet:", 5)) {
2192 if (!strncmp(addr
, "inet:", 5))
2194 } else if (fam
== AF_INET6
|| !strncmp(addr
, "inet6:", 6)) {
2196 if (!strncmp(addr
, "inet6:", 6))
2200 /* URL-like literal [] */
2201 if (addr
[0] == '[') {
2203 if ((port
= strchr(addr
, ']')) == NULL
)
2206 } else if (addr
[0] == '*') {
2209 port
= strrchr(strchr(addr
, '/') ? : addr
, ':');
2215 if (port
&& *port
) {
2219 if (*port
&& *port
!= '*') {
2220 if (get_integer(&a
.port
, port
, 0)) {
2221 struct servent
*se1
= NULL
;
2222 struct servent
*se2
= NULL
;
2224 if (current_filter
.dbs
&(1<<UDP_DB
))
2225 se1
= getservbyname(port
, UDP_PROTO
);
2226 if (current_filter
.dbs
&(1<<TCP_DB
))
2227 se2
= getservbyname(port
, TCP_PROTO
);
2228 if (se1
&& se2
&& se1
->s_port
!= se2
->s_port
) {
2229 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
2235 a
.port
= ntohs(se1
->s_port
);
2239 for (s
= rlist
; s
; s
= s
->next
) {
2240 if ((s
->proto
== UDP_PROTO
&&
2241 (current_filter
.dbs
&(1<<UDP_DB
))) ||
2242 (s
->proto
== TCP_PROTO
&&
2243 (current_filter
.dbs
&(1<<TCP_DB
)))) {
2244 if (s
->name
&& strcmp(s
->name
, port
) == 0) {
2245 if (a
.port
> 0 && a
.port
!= s
->port
) {
2246 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
2254 fprintf(stderr
, "Error: \"%s\" does not look like a port.\n", port
);
2261 if (!is_port
&& *addr
&& *addr
!= '*') {
2262 if (get_prefix_1(&a
.addr
, addr
, fam
)) {
2263 if (get_dns_host(&a
, addr
, fam
)) {
2264 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", addr
);
2271 if (fam
!= AF_UNSPEC
) {
2272 int states
= f
->states
;
2274 filter_af_set(f
, fam
);
2275 filter_states_set(f
, states
);
2278 res
= malloc(sizeof(*res
));
2280 memcpy(res
, &a
, sizeof(a
));
2284 void *parse_markmask(const char *markmask
)
2286 struct aafilter a
, *res
;
2288 if (strchr(markmask
, '/')) {
2289 if (sscanf(markmask
, "%i/%i", &a
.mark
, &a
.mask
) != 2)
2292 a
.mask
= 0xffffffff;
2293 if (sscanf(markmask
, "%i", &a
.mark
) != 1)
2297 res
= malloc(sizeof(*res
));
2299 memcpy(res
, &a
, sizeof(a
));
2303 static void proc_ctx_print(struct sockstat
*s
)
2307 if (show_proc_ctx
|| show_sock_ctx
) {
2308 if (find_entry(s
->ino
, &buf
,
2309 (show_proc_ctx
& show_sock_ctx
) ?
2310 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
2311 out(" users:(%s)", buf
);
2314 } else if (show_users
) {
2315 if (find_entry(s
->ino
, &buf
, USERS
) > 0) {
2316 out(" users:(%s)", buf
);
2322 static void inet_stats_print(struct sockstat
*s
, bool v6only
)
2324 sock_state_print(s
);
2326 inet_addr_print(&s
->local
, s
->lport
, s
->iface
, v6only
);
2327 inet_addr_print(&s
->remote
, s
->rport
, 0, v6only
);
2332 static int proc_parse_inet_addr(char *loc
, char *rem
, int family
, struct
2335 s
->local
.family
= s
->remote
.family
= family
;
2336 if (family
== AF_INET
) {
2337 sscanf(loc
, "%x:%x", s
->local
.data
, (unsigned *)&s
->lport
);
2338 sscanf(rem
, "%x:%x", s
->remote
.data
, (unsigned *)&s
->rport
);
2339 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
2342 sscanf(loc
, "%08x%08x%08x%08x:%x",
2348 sscanf(rem
, "%08x%08x%08x%08x:%x",
2354 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
2360 static int proc_inet_split_line(char *line
, char **loc
, char **rem
, char **data
)
2364 if ((p
= strchr(line
, ':')) == NULL
)
2368 if ((p
= strchr(*loc
, ':')) == NULL
)
2373 if ((p
= strchr(*rem
, ':')) == NULL
)
2381 static char *sprint_bw(char *buf
, double bw
)
2384 sprintf(buf
, "%.0f", bw
);
2386 sprintf(buf
, "%.3gG", bw
/ 1e9
);
2388 sprintf(buf
, "%.3gM", bw
/ 1e6
);
2390 sprintf(buf
, "%.3gK", bw
/ 1e3
);
2392 sprintf(buf
, "%g", bw
);
2397 static void sctp_stats_print(struct sctp_info
*s
)
2400 out(" tag:%x", s
->sctpi_tag
);
2402 out(" state:%s", sctp_sstate_name
[s
->sctpi_state
]);
2404 out(" rwnd:%d", s
->sctpi_rwnd
);
2405 if (s
->sctpi_unackdata
)
2406 out(" unackdata:%d", s
->sctpi_unackdata
);
2407 if (s
->sctpi_penddata
)
2408 out(" penddata:%d", s
->sctpi_penddata
);
2409 if (s
->sctpi_instrms
)
2410 out(" instrms:%d", s
->sctpi_instrms
);
2411 if (s
->sctpi_outstrms
)
2412 out(" outstrms:%d", s
->sctpi_outstrms
);
2413 if (s
->sctpi_inqueue
)
2414 out(" inqueue:%d", s
->sctpi_inqueue
);
2415 if (s
->sctpi_outqueue
)
2416 out(" outqueue:%d", s
->sctpi_outqueue
);
2417 if (s
->sctpi_overall_error
)
2418 out(" overerr:%d", s
->sctpi_overall_error
);
2419 if (s
->sctpi_max_burst
)
2420 out(" maxburst:%d", s
->sctpi_max_burst
);
2421 if (s
->sctpi_maxseg
)
2422 out(" maxseg:%d", s
->sctpi_maxseg
);
2423 if (s
->sctpi_peer_rwnd
)
2424 out(" prwnd:%d", s
->sctpi_peer_rwnd
);
2425 if (s
->sctpi_peer_tag
)
2426 out(" ptag:%x", s
->sctpi_peer_tag
);
2427 if (s
->sctpi_peer_capable
)
2428 out(" pcapable:%d", s
->sctpi_peer_capable
);
2429 if (s
->sctpi_peer_sack
)
2430 out(" psack:%d", s
->sctpi_peer_sack
);
2431 if (s
->sctpi_s_autoclose
)
2432 out(" autoclose:%d", s
->sctpi_s_autoclose
);
2433 if (s
->sctpi_s_adaptation_ind
)
2434 out(" adapind:%d", s
->sctpi_s_adaptation_ind
);
2435 if (s
->sctpi_s_pd_point
)
2436 out(" pdpoint:%d", s
->sctpi_s_pd_point
);
2437 if (s
->sctpi_s_nodelay
)
2438 out(" nodelay:%d", s
->sctpi_s_nodelay
);
2439 if (s
->sctpi_s_disable_fragments
)
2440 out(" nofrag:%d", s
->sctpi_s_disable_fragments
);
2441 if (s
->sctpi_s_v4mapped
)
2442 out(" v4mapped:%d", s
->sctpi_s_v4mapped
);
2443 if (s
->sctpi_s_frag_interleave
)
2444 out(" fraginl:%d", s
->sctpi_s_frag_interleave
);
2447 static void tcp_stats_print(struct tcpstat
*s
)
2453 if (s
->has_sack_opt
)
2457 if (s
->has_ecnseen_opt
)
2459 if (s
->has_fastopen_opt
)
2462 out(" %s", s
->cong_alg
);
2463 if (s
->has_wscale_opt
)
2464 out(" wscale:%d,%d", s
->snd_wscale
, s
->rcv_wscale
);
2466 out(" rto:%g", s
->rto
);
2468 out(" backoff:%u", s
->backoff
);
2470 out(" rtt:%g/%g", s
->rtt
, s
->rttvar
);
2472 out(" ato:%g", s
->ato
);
2475 out(" qack:%d", s
->qack
);
2480 out(" mss:%d", s
->mss
);
2482 out(" pmtu:%u", s
->pmtu
);
2484 out(" rcvmss:%d", s
->rcv_mss
);
2486 out(" advmss:%d", s
->advmss
);
2488 out(" cwnd:%u", s
->cwnd
);
2490 out(" ssthresh:%d", s
->ssthresh
);
2493 out(" bytes_sent:%llu", s
->bytes_sent
);
2494 if (s
->bytes_retrans
)
2495 out(" bytes_retrans:%llu", s
->bytes_retrans
);
2497 out(" bytes_acked:%llu", s
->bytes_acked
);
2498 if (s
->bytes_received
)
2499 out(" bytes_received:%llu", s
->bytes_received
);
2501 out(" segs_out:%u", s
->segs_out
);
2503 out(" segs_in:%u", s
->segs_in
);
2504 if (s
->data_segs_out
)
2505 out(" data_segs_out:%u", s
->data_segs_out
);
2506 if (s
->data_segs_in
)
2507 out(" data_segs_in:%u", s
->data_segs_in
);
2509 if (s
->dctcp
&& s
->dctcp
->enabled
) {
2510 struct dctcpstat
*dctcp
= s
->dctcp
;
2512 out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
2513 dctcp
->ce_state
, dctcp
->alpha
, dctcp
->ab_ecn
,
2515 } else if (s
->dctcp
) {
2516 out(" dctcp:fallback_mode");
2522 bw
= s
->bbr_info
->bbr_bw_hi
;
2524 bw
|= s
->bbr_info
->bbr_bw_lo
;
2526 out(" bbr:(bw:%sbps,mrtt:%g",
2527 sprint_bw(b1
, bw
* 8.0),
2528 (double)s
->bbr_info
->bbr_min_rtt
/ 1000.0);
2529 if (s
->bbr_info
->bbr_pacing_gain
)
2530 out(",pacing_gain:%g",
2531 (double)s
->bbr_info
->bbr_pacing_gain
/ 256.0);
2532 if (s
->bbr_info
->bbr_cwnd_gain
)
2533 out(",cwnd_gain:%g",
2534 (double)s
->bbr_info
->bbr_cwnd_gain
/ 256.0);
2539 out(" send %sbps", sprint_bw(b1
, s
->send_bps
));
2541 out(" lastsnd:%u", s
->lastsnd
);
2543 out(" lastrcv:%u", s
->lastrcv
);
2545 out(" lastack:%u", s
->lastack
);
2547 if (s
->pacing_rate
) {
2548 out(" pacing_rate %sbps", sprint_bw(b1
, s
->pacing_rate
));
2549 if (s
->pacing_rate_max
)
2550 out("/%sbps", sprint_bw(b1
, s
->pacing_rate_max
));
2553 if (s
->delivery_rate
)
2554 out(" delivery_rate %sbps", sprint_bw(b1
, s
->delivery_rate
));
2556 out(" delivered:%u", s
->delivered
);
2557 if (s
->delivered_ce
)
2558 out(" delivered_ce:%u", s
->delivered_ce
);
2560 out(" app_limited");
2563 out(" busy:%llums", s
->busy_time
/ 1000);
2564 if (s
->rwnd_limited
)
2565 out(" rwnd_limited:%llums(%.1f%%)",
2566 s
->rwnd_limited
/ 1000,
2567 100.0 * s
->rwnd_limited
/ s
->busy_time
);
2568 if (s
->sndbuf_limited
)
2569 out(" sndbuf_limited:%llums(%.1f%%)",
2570 s
->sndbuf_limited
/ 1000,
2571 100.0 * s
->sndbuf_limited
/ s
->busy_time
);
2575 out(" unacked:%u", s
->unacked
);
2576 if (s
->retrans
|| s
->retrans_total
)
2577 out(" retrans:%u/%u", s
->retrans
, s
->retrans_total
);
2579 out(" lost:%u", s
->lost
);
2580 if (s
->sacked
&& s
->ss
.state
!= SS_LISTEN
)
2581 out(" sacked:%u", s
->sacked
);
2583 out(" dsack_dups:%u", s
->dsack_dups
);
2585 out(" fackets:%u", s
->fackets
);
2586 if (s
->reordering
!= 3)
2587 out(" reordering:%d", s
->reordering
);
2589 out(" reord_seen:%d", s
->reord_seen
);
2591 out(" rcv_rtt:%g", s
->rcv_rtt
);
2593 out(" rcv_space:%d", s
->rcv_space
);
2594 if (s
->rcv_ssthresh
)
2595 out(" rcv_ssthresh:%u", s
->rcv_ssthresh
);
2597 out(" notsent:%u", s
->not_sent
);
2599 out(" minrtt:%g", s
->min_rtt
);
2602 static void tcp_timer_print(struct tcpstat
*s
)
2604 static const char * const tmr_name
[] = {
2616 out(" timer:(%s,%s,%d)",
2618 print_ms_timer(s
->timeout
),
2623 static void sctp_timer_print(struct tcpstat
*s
)
2626 out(" timer:(T3_RTX,%s,%d)",
2627 print_ms_timer(s
->timeout
), s
->retrans
);
2630 static int tcp_show_line(char *line
, const struct filter
*f
, int family
)
2632 int rto
= 0, ato
= 0;
2633 struct tcpstat s
= {};
2634 char *loc
, *rem
, *data
;
2637 int hz
= get_user_hz();
2639 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
2642 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
2644 if (!(f
->states
& (1 << state
)))
2647 proc_parse_inet_addr(loc
, rem
, family
, &s
.ss
);
2649 if (f
->f
&& run_ssfilter(f
->f
, &s
.ss
) == 0)
2653 n
= sscanf(data
, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n",
2654 &s
.ss
.state
, &s
.ss
.wq
, &s
.ss
.rq
,
2655 &s
.timer
, &s
.timeout
, &s
.retrans
, &s
.ss
.uid
, &s
.probes
,
2656 &s
.ss
.ino
, &s
.ss
.refcnt
, &s
.ss
.sk
, &rto
, &ato
, &s
.qack
, &s
.cwnd
,
2669 s
.retrans
= s
.timer
!= 1 ? s
.probes
: s
.retrans
;
2670 s
.timeout
= (s
.timeout
* 1000 + hz
- 1) / hz
;
2671 s
.ato
= (double)ato
/ hz
;
2673 s
.rto
= (double)rto
;
2674 s
.ssthresh
= s
.ssthresh
== -1 ? 0 : s
.ssthresh
;
2675 s
.rto
= s
.rto
!= 3 * hz
? s
.rto
/ hz
: 0;
2676 s
.ss
.type
= IPPROTO_TCP
;
2678 inet_stats_print(&s
.ss
, false);
2681 tcp_timer_print(&s
);
2684 sock_details_print(&s
.ss
);
2686 out(" opt:\"%s\"", opt
);
2690 tcp_stats_print(&s
);
2695 static int generic_record_read(FILE *fp
,
2696 int (*worker
)(char*, const struct filter
*, int),
2697 const struct filter
*f
, int fam
)
2702 if (fgets(line
, sizeof(line
), fp
) == NULL
)
2705 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2706 int n
= strlen(line
);
2708 if (n
== 0 || line
[n
-1] != '\n') {
2714 if (worker(line
, f
, fam
) < 0)
2719 return ferror(fp
) ? -1 : 0;
2722 static void print_skmeminfo(struct rtattr
*tb
[], int attrtype
)
2724 const __u32
*skmeminfo
;
2726 if (!tb
[attrtype
]) {
2727 if (attrtype
== INET_DIAG_SKMEMINFO
) {
2728 if (!tb
[INET_DIAG_MEMINFO
])
2731 const struct inet_diag_meminfo
*minfo
=
2732 RTA_DATA(tb
[INET_DIAG_MEMINFO
]);
2734 out(" mem:(r%u,w%u,f%u,t%u)",
2743 skmeminfo
= RTA_DATA(tb
[attrtype
]);
2745 out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
2746 skmeminfo
[SK_MEMINFO_RMEM_ALLOC
],
2747 skmeminfo
[SK_MEMINFO_RCVBUF
],
2748 skmeminfo
[SK_MEMINFO_WMEM_ALLOC
],
2749 skmeminfo
[SK_MEMINFO_SNDBUF
],
2750 skmeminfo
[SK_MEMINFO_FWD_ALLOC
],
2751 skmeminfo
[SK_MEMINFO_WMEM_QUEUED
],
2752 skmeminfo
[SK_MEMINFO_OPTMEM
]);
2754 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2755 (SK_MEMINFO_BACKLOG
+ 1) * sizeof(__u32
))
2756 out(",bl%u", skmeminfo
[SK_MEMINFO_BACKLOG
]);
2758 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2759 (SK_MEMINFO_DROPS
+ 1) * sizeof(__u32
))
2760 out(",d%u", skmeminfo
[SK_MEMINFO_DROPS
]);
2765 static void print_md5sig(struct tcp_diag_md5sig
*sig
)
2768 format_host(sig
->tcpm_family
,
2769 sig
->tcpm_family
== AF_INET6
? 16 : 4,
2771 sig
->tcpm_prefixlen
);
2772 print_escape_buf(sig
->tcpm_key
, sig
->tcpm_keylen
, " ,");
2775 static void tcp_tls_version(struct rtattr
*attr
)
2781 val
= rta_getattr_u16(attr
);
2784 case TLS_1_2_VERSION
:
2785 out(" version: 1.2");
2787 case TLS_1_3_VERSION
:
2788 out(" version: 1.3");
2791 out(" version: unknown(%hu)", val
);
2796 static void tcp_tls_cipher(struct rtattr
*attr
)
2802 val
= rta_getattr_u16(attr
);
2805 case TLS_CIPHER_AES_GCM_128
:
2806 out(" cipher: aes-gcm-128");
2808 case TLS_CIPHER_AES_GCM_256
:
2809 out(" cipher: aes-gcm-256");
2814 static void tcp_tls_conf(const char *name
, struct rtattr
*attr
)
2820 val
= rta_getattr_u16(attr
);
2824 out(" %s: none", name
);
2827 out(" %s: sw", name
);
2830 out(" %s: hw", name
);
2832 case TLS_CONF_HW_RECORD
:
2833 out(" %s: hw-record", name
);
2836 out(" %s: unknown(%hu)", name
, val
);
2841 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
2843 static void tcp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
2844 struct rtattr
*tb
[])
2847 struct tcpstat s
= {};
2849 s
.ss
.state
= r
->idiag_state
;
2851 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
2853 if (tb
[INET_DIAG_INFO
]) {
2854 struct tcp_info
*info
;
2855 int len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
2857 /* workaround for older kernels with less fields */
2858 if (len
< sizeof(*info
)) {
2859 info
= alloca(sizeof(*info
));
2860 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
2861 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
2863 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
2866 s
.has_ts_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_TIMESTAMPS
);
2867 s
.has_sack_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SACK
);
2868 s
.has_ecn_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN
);
2869 s
.has_ecnseen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN_SEEN
);
2870 s
.has_fastopen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SYN_DATA
);
2873 if (tb
[INET_DIAG_CONG
])
2875 rta_getattr_str(tb
[INET_DIAG_CONG
]),
2876 sizeof(s
.cong_alg
) - 1);
2878 if (TCPI_HAS_OPT(info
, TCPI_OPT_WSCALE
)) {
2879 s
.has_wscale_opt
= true;
2880 s
.snd_wscale
= info
->tcpi_snd_wscale
;
2881 s
.rcv_wscale
= info
->tcpi_rcv_wscale
;
2884 if (info
->tcpi_rto
&& info
->tcpi_rto
!= 3000000)
2885 s
.rto
= (double)info
->tcpi_rto
/ 1000;
2887 s
.backoff
= info
->tcpi_backoff
;
2888 s
.rtt
= (double)info
->tcpi_rtt
/ 1000;
2889 s
.rttvar
= (double)info
->tcpi_rttvar
/ 1000;
2890 s
.ato
= (double)info
->tcpi_ato
/ 1000;
2891 s
.mss
= info
->tcpi_snd_mss
;
2892 s
.rcv_mss
= info
->tcpi_rcv_mss
;
2893 s
.advmss
= info
->tcpi_advmss
;
2894 s
.rcv_space
= info
->tcpi_rcv_space
;
2895 s
.rcv_rtt
= (double)info
->tcpi_rcv_rtt
/ 1000;
2896 s
.lastsnd
= info
->tcpi_last_data_sent
;
2897 s
.lastrcv
= info
->tcpi_last_data_recv
;
2898 s
.lastack
= info
->tcpi_last_ack_recv
;
2899 s
.unacked
= info
->tcpi_unacked
;
2900 s
.retrans
= info
->tcpi_retrans
;
2901 s
.retrans_total
= info
->tcpi_total_retrans
;
2902 s
.lost
= info
->tcpi_lost
;
2903 s
.sacked
= info
->tcpi_sacked
;
2904 s
.fackets
= info
->tcpi_fackets
;
2905 s
.reordering
= info
->tcpi_reordering
;
2906 s
.rcv_ssthresh
= info
->tcpi_rcv_ssthresh
;
2907 s
.cwnd
= info
->tcpi_snd_cwnd
;
2908 s
.pmtu
= info
->tcpi_pmtu
;
2910 if (info
->tcpi_snd_ssthresh
< 0xFFFF)
2911 s
.ssthresh
= info
->tcpi_snd_ssthresh
;
2913 rtt
= (double) info
->tcpi_rtt
;
2914 if (tb
[INET_DIAG_VEGASINFO
]) {
2915 const struct tcpvegas_info
*vinfo
2916 = RTA_DATA(tb
[INET_DIAG_VEGASINFO
]);
2918 if (vinfo
->tcpv_enabled
&&
2919 vinfo
->tcpv_rtt
&& vinfo
->tcpv_rtt
!= 0x7fffffff)
2920 rtt
= vinfo
->tcpv_rtt
;
2923 if (tb
[INET_DIAG_DCTCPINFO
]) {
2924 struct dctcpstat
*dctcp
= malloc(sizeof(struct
2927 const struct tcp_dctcp_info
*dinfo
2928 = RTA_DATA(tb
[INET_DIAG_DCTCPINFO
]);
2930 dctcp
->enabled
= !!dinfo
->dctcp_enabled
;
2931 dctcp
->ce_state
= dinfo
->dctcp_ce_state
;
2932 dctcp
->alpha
= dinfo
->dctcp_alpha
;
2933 dctcp
->ab_ecn
= dinfo
->dctcp_ab_ecn
;
2934 dctcp
->ab_tot
= dinfo
->dctcp_ab_tot
;
2938 if (tb
[INET_DIAG_BBRINFO
]) {
2939 const void *bbr_info
= RTA_DATA(tb
[INET_DIAG_BBRINFO
]);
2940 int len
= min(RTA_PAYLOAD(tb
[INET_DIAG_BBRINFO
]),
2941 sizeof(*s
.bbr_info
));
2943 s
.bbr_info
= calloc(1, sizeof(*s
.bbr_info
));
2944 if (s
.bbr_info
&& bbr_info
)
2945 memcpy(s
.bbr_info
, bbr_info
, len
);
2948 if (rtt
> 0 && info
->tcpi_snd_mss
&& info
->tcpi_snd_cwnd
) {
2949 s
.send_bps
= (double) info
->tcpi_snd_cwnd
*
2950 (double)info
->tcpi_snd_mss
* 8000000. / rtt
;
2953 if (info
->tcpi_pacing_rate
&&
2954 info
->tcpi_pacing_rate
!= ~0ULL) {
2955 s
.pacing_rate
= info
->tcpi_pacing_rate
* 8.;
2957 if (info
->tcpi_max_pacing_rate
&&
2958 info
->tcpi_max_pacing_rate
!= ~0ULL)
2959 s
.pacing_rate_max
= info
->tcpi_max_pacing_rate
* 8.;
2961 s
.bytes_acked
= info
->tcpi_bytes_acked
;
2962 s
.bytes_received
= info
->tcpi_bytes_received
;
2963 s
.segs_out
= info
->tcpi_segs_out
;
2964 s
.segs_in
= info
->tcpi_segs_in
;
2965 s
.data_segs_out
= info
->tcpi_data_segs_out
;
2966 s
.data_segs_in
= info
->tcpi_data_segs_in
;
2967 s
.not_sent
= info
->tcpi_notsent_bytes
;
2968 if (info
->tcpi_min_rtt
&& info
->tcpi_min_rtt
!= ~0U)
2969 s
.min_rtt
= (double) info
->tcpi_min_rtt
/ 1000;
2970 s
.delivery_rate
= info
->tcpi_delivery_rate
* 8.;
2971 s
.app_limited
= info
->tcpi_delivery_rate_app_limited
;
2972 s
.busy_time
= info
->tcpi_busy_time
;
2973 s
.rwnd_limited
= info
->tcpi_rwnd_limited
;
2974 s
.sndbuf_limited
= info
->tcpi_sndbuf_limited
;
2975 s
.delivered
= info
->tcpi_delivered
;
2976 s
.delivered_ce
= info
->tcpi_delivered_ce
;
2977 s
.dsack_dups
= info
->tcpi_dsack_dups
;
2978 s
.reord_seen
= info
->tcpi_reord_seen
;
2979 s
.bytes_sent
= info
->tcpi_bytes_sent
;
2980 s
.bytes_retrans
= info
->tcpi_bytes_retrans
;
2981 tcp_stats_print(&s
);
2985 if (tb
[INET_DIAG_MD5SIG
]) {
2986 struct tcp_diag_md5sig
*sig
= RTA_DATA(tb
[INET_DIAG_MD5SIG
]);
2987 int len
= RTA_PAYLOAD(tb
[INET_DIAG_MD5SIG
]);
2990 print_md5sig(sig
++);
2991 for (len
-= sizeof(*sig
); len
> 0; len
-= sizeof(*sig
)) {
2993 print_md5sig(sig
++);
2996 if (tb
[INET_DIAG_ULP_INFO
]) {
2997 struct rtattr
*ulpinfo
[INET_ULP_INFO_MAX
+ 1] = { 0 };
2999 parse_rtattr_nested(ulpinfo
, INET_ULP_INFO_MAX
,
3000 tb
[INET_DIAG_ULP_INFO
]);
3002 if (ulpinfo
[INET_ULP_INFO_NAME
])
3004 rta_getattr_str(ulpinfo
[INET_ULP_INFO_NAME
]));
3006 if (ulpinfo
[INET_ULP_INFO_TLS
]) {
3007 struct rtattr
*tlsinfo
[TLS_INFO_MAX
+ 1] = { 0 };
3009 parse_rtattr_nested(tlsinfo
, TLS_INFO_MAX
,
3010 ulpinfo
[INET_ULP_INFO_TLS
]);
3012 tcp_tls_version(tlsinfo
[TLS_INFO_VERSION
]);
3013 tcp_tls_cipher(tlsinfo
[TLS_INFO_CIPHER
]);
3014 tcp_tls_conf("rxconf", tlsinfo
[TLS_INFO_RXCONF
]);
3015 tcp_tls_conf("txconf", tlsinfo
[TLS_INFO_TXCONF
]);
3020 static const char *format_host_sa(struct sockaddr_storage
*sa
)
3023 struct sockaddr_in sin
;
3024 struct sockaddr_in6 sin6
;
3025 } *saddr
= (void *)sa
;
3027 switch (sa
->ss_family
) {
3029 return format_host(AF_INET
, 4, &saddr
->sin
.sin_addr
);
3031 return format_host(AF_INET6
, 16, &saddr
->sin6
.sin6_addr
);
3037 static void sctp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
3038 struct rtattr
*tb
[])
3040 struct sockaddr_storage
*sa
;
3043 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
3045 if (tb
[INET_DIAG_LOCALS
]) {
3046 len
= RTA_PAYLOAD(tb
[INET_DIAG_LOCALS
]);
3047 sa
= RTA_DATA(tb
[INET_DIAG_LOCALS
]);
3049 out(" locals:%s", format_host_sa(sa
));
3050 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
3051 out(",%s", format_host_sa(sa
));
3054 if (tb
[INET_DIAG_PEERS
]) {
3055 len
= RTA_PAYLOAD(tb
[INET_DIAG_PEERS
]);
3056 sa
= RTA_DATA(tb
[INET_DIAG_PEERS
]);
3058 out(" peers:%s", format_host_sa(sa
));
3059 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
3060 out(",%s", format_host_sa(sa
));
3062 if (tb
[INET_DIAG_INFO
]) {
3063 struct sctp_info
*info
;
3064 len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
3066 /* workaround for older kernels with less fields */
3067 if (len
< sizeof(*info
)) {
3068 info
= alloca(sizeof(*info
));
3069 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
3070 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
3072 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
3074 sctp_stats_print(info
);
3078 static void parse_diag_msg(struct nlmsghdr
*nlh
, struct sockstat
*s
)
3080 struct rtattr
*tb
[INET_DIAG_MAX
+1];
3081 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
3083 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
3084 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3086 s
->state
= r
->idiag_state
;
3087 s
->local
.family
= s
->remote
.family
= r
->idiag_family
;
3088 s
->lport
= ntohs(r
->id
.idiag_sport
);
3089 s
->rport
= ntohs(r
->id
.idiag_dport
);
3090 s
->wq
= r
->idiag_wqueue
;
3091 s
->rq
= r
->idiag_rqueue
;
3092 s
->ino
= r
->idiag_inode
;
3093 s
->uid
= r
->idiag_uid
;
3094 s
->iface
= r
->id
.idiag_if
;
3095 s
->sk
= cookie_sk_get(&r
->id
.idiag_cookie
[0]);
3098 if (tb
[INET_DIAG_MARK
])
3099 s
->mark
= rta_getattr_u32(tb
[INET_DIAG_MARK
]);
3100 if (tb
[INET_DIAG_PROTOCOL
])
3101 s
->raw_prot
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
3105 if (s
->local
.family
== AF_INET
)
3106 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
3108 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
3110 memcpy(s
->local
.data
, r
->id
.idiag_src
, s
->local
.bytelen
);
3111 memcpy(s
->remote
.data
, r
->id
.idiag_dst
, s
->local
.bytelen
);
3114 static int inet_show_sock(struct nlmsghdr
*nlh
,
3117 struct rtattr
*tb
[INET_DIAG_MAX
+1];
3118 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
3119 unsigned char v6only
= 0;
3121 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
3122 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3124 if (tb
[INET_DIAG_PROTOCOL
])
3125 s
->type
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
3127 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
])
3128 v6only
= rta_getattr_u8(tb
[INET_DIAG_SKV6ONLY
]);
3130 inet_stats_print(s
, v6only
);
3133 struct tcpstat t
= {};
3135 t
.timer
= r
->idiag_timer
;
3136 t
.timeout
= r
->idiag_expires
;
3137 t
.retrans
= r
->idiag_retrans
;
3138 if (s
->type
== IPPROTO_SCTP
)
3139 sctp_timer_print(&t
);
3141 tcp_timer_print(&t
);
3145 sock_details_print(s
);
3146 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
])
3147 out(" v6only:%u", v6only
);
3149 if (tb
[INET_DIAG_SHUTDOWN
]) {
3152 mask
= rta_getattr_u8(tb
[INET_DIAG_SHUTDOWN
]);
3154 mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
3159 if (tb
[INET_DIAG_TOS
])
3160 out(" tos:%#x", rta_getattr_u8(tb
[INET_DIAG_TOS
]));
3161 if (tb
[INET_DIAG_TCLASS
])
3162 out(" tclass:%#x", rta_getattr_u8(tb
[INET_DIAG_TCLASS
]));
3163 if (tb
[INET_DIAG_CLASS_ID
])
3164 out(" class_id:%#x", rta_getattr_u32(tb
[INET_DIAG_CLASS_ID
]));
3167 if (show_mem
|| (show_tcpinfo
&& s
->type
!= IPPROTO_UDP
)) {
3170 if (s
->type
== IPPROTO_SCTP
)
3171 sctp_show_info(nlh
, r
, tb
);
3173 tcp_show_info(nlh
, r
, tb
);
3180 static int tcpdiag_send(int fd
, int protocol
, struct filter
*f
)
3182 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
3184 struct nlmsghdr nlh
;
3185 struct inet_diag_req r
;
3187 .nlh
.nlmsg_len
= sizeof(req
),
3188 .nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
,
3189 .nlh
.nlmsg_seq
= MAGIC_SEQ
,
3190 .r
.idiag_family
= AF_INET
,
3191 .r
.idiag_states
= f
->states
,
3197 struct iovec iov
[3];
3200 if (protocol
== IPPROTO_UDP
)
3203 if (protocol
== IPPROTO_TCP
)
3204 req
.nlh
.nlmsg_type
= TCPDIAG_GETSOCK
;
3206 req
.nlh
.nlmsg_type
= DCCPDIAG_GETSOCK
;
3208 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
3209 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
3213 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
3214 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
3215 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
3219 req
.r
.idiag_ext
|= (1<<(INET_DIAG_TOS
-1));
3220 req
.r
.idiag_ext
|= (1<<(INET_DIAG_TCLASS
-1));
3223 iov
[0] = (struct iovec
){
3225 .iov_len
= sizeof(req
)
3228 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
3230 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
3231 rta
.rta_len
= RTA_LENGTH(bclen
);
3232 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
3233 iov
[2] = (struct iovec
){ bc
, bclen
};
3234 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
3239 msg
= (struct msghdr
) {
3240 .msg_name
= (void *)&nladdr
,
3241 .msg_namelen
= sizeof(nladdr
),
3243 .msg_iovlen
= iovlen
,
3246 if (sendmsg(fd
, &msg
, 0) < 0) {
3254 static int sockdiag_send(int family
, int fd
, int protocol
, struct filter
*f
)
3256 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
3257 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
3262 struct iovec iov
[3];
3265 if (family
== PF_UNSPEC
)
3266 return tcpdiag_send(fd
, protocol
, f
);
3268 memset(&req
.r
, 0, sizeof(req
.r
));
3269 req
.r
.sdiag_family
= family
;
3270 req
.r
.sdiag_protocol
= protocol
;
3271 req
.r
.idiag_states
= f
->states
;
3273 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
3274 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
3278 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
3279 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
3280 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
3284 req
.r
.idiag_ext
|= (1<<(INET_DIAG_TOS
-1));
3285 req
.r
.idiag_ext
|= (1<<(INET_DIAG_TCLASS
-1));
3288 iov
[0] = (struct iovec
){
3290 .iov_len
= sizeof(req
)
3293 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
3295 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
3296 rta
.rta_len
= RTA_LENGTH(bclen
);
3297 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
3298 iov
[2] = (struct iovec
){ bc
, bclen
};
3299 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
3304 msg
= (struct msghdr
) {
3305 .msg_name
= (void *)&nladdr
,
3306 .msg_namelen
= sizeof(nladdr
),
3308 .msg_iovlen
= iovlen
,
3311 if (sendmsg(fd
, &msg
, 0) < 0) {
3319 struct inet_diag_arg
{
3322 struct rtnl_handle
*rth
;
3325 static int kill_inet_sock(struct nlmsghdr
*h
, void *arg
, struct sockstat
*s
)
3327 struct inet_diag_msg
*d
= NLMSG_DATA(h
);
3328 struct inet_diag_arg
*diag_arg
= arg
;
3329 struct rtnl_handle
*rth
= diag_arg
->rth
;
3331 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
3333 req
.nlh
.nlmsg_type
= SOCK_DESTROY
;
3334 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3335 req
.nlh
.nlmsg_seq
= ++rth
->seq
;
3336 req
.r
.sdiag_family
= d
->idiag_family
;
3337 req
.r
.sdiag_protocol
= diag_arg
->protocol
;
3340 if (diag_arg
->protocol
== IPPROTO_RAW
) {
3341 struct inet_diag_req_raw
*raw
= (void *)&req
.r
;
3343 BUILD_BUG_ON(sizeof(req
.r
) != sizeof(*raw
));
3344 raw
->sdiag_raw_protocol
= s
->raw_prot
;
3347 return rtnl_talk(rth
, &req
.nlh
, NULL
);
3350 static int show_one_inet_sock(struct nlmsghdr
*h
, void *arg
)
3353 struct inet_diag_arg
*diag_arg
= arg
;
3354 struct inet_diag_msg
*r
= NLMSG_DATA(h
);
3355 struct sockstat s
= {};
3357 if (!(diag_arg
->f
->families
& FAMILY_MASK(r
->idiag_family
)))
3360 parse_diag_msg(h
, &s
);
3361 s
.type
= diag_arg
->protocol
;
3363 if (diag_arg
->f
->f
&& run_ssfilter(diag_arg
->f
->f
, &s
) == 0)
3366 if (diag_arg
->f
->kill
&& kill_inet_sock(h
, arg
, &s
) != 0) {
3367 if (errno
== EOPNOTSUPP
|| errno
== ENOENT
) {
3368 /* Socket can't be closed, or is already closed. */
3371 perror("SOCK_DESTROY answers");
3376 err
= inet_show_sock(h
, &s
);
3383 static int inet_show_netlink(struct filter
*f
, FILE *dump_fp
, int protocol
)
3386 struct rtnl_handle rth
, rth2
;
3387 int family
= PF_INET
;
3388 struct inet_diag_arg arg
= { .f
= f
, .protocol
= protocol
};
3390 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
3394 if (rtnl_open_byproto(&rth2
, 0, NETLINK_SOCK_DIAG
)) {
3401 rth
.dump
= MAGIC_SEQ
;
3402 rth
.dump_fp
= dump_fp
;
3403 if (preferred_family
== PF_INET6
)
3407 if ((err
= sockdiag_send(family
, rth
.fd
, protocol
, f
)))
3410 if ((err
= rtnl_dump_filter(&rth
, show_one_inet_sock
, &arg
))) {
3411 if (family
!= PF_UNSPEC
) {
3417 if (family
== PF_INET
&& preferred_family
!= PF_INET
) {
3425 rtnl_close(arg
.rth
);
3429 static int tcp_show_netlink_file(struct filter
*f
)
3435 if ((fp
= fopen(getenv("TCPDIAG_FILE"), "r")) == NULL
) {
3436 perror("fopen($TCPDIAG_FILE)");
3442 size_t status
, nitems
;
3443 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
3444 struct sockstat s
= {};
3446 status
= fread(buf
, 1, sizeof(*h
), fp
);
3447 if (status
!= sizeof(*h
)) {
3449 perror("Reading header from $TCPDIAG_FILE");
3451 fprintf(stderr
, "Unexpected EOF reading $TCPDIAG_FILE");
3455 nitems
= NLMSG_ALIGN(h
->nlmsg_len
- sizeof(*h
));
3456 status
= fread(h
+1, 1, nitems
, fp
);
3458 if (status
!= nitems
) {
3460 perror("Reading $TCPDIAG_FILE");
3462 fprintf(stderr
, "Unexpected EOF reading $TCPDIAG_FILE");
3466 /* The only legal exit point */
3467 if (h
->nlmsg_type
== NLMSG_DONE
) {
3472 if (h
->nlmsg_type
== NLMSG_ERROR
) {
3473 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
3475 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
3476 fprintf(stderr
, "ERROR truncated\n");
3478 errno
= -err
->error
;
3479 perror("TCPDIAG answered");
3484 parse_diag_msg(h
, &s
);
3485 s
.type
= IPPROTO_TCP
;
3487 if (f
&& f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
3490 err2
= inet_show_sock(h
, &s
);
3501 static int tcp_show(struct filter
*f
)
3505 int bufsize
= 1024*1024;
3507 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3510 dg_proto
= TCP_PROTO
;
3512 if (getenv("TCPDIAG_FILE"))
3513 return tcp_show_netlink_file(f
);
3515 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
3516 && inet_show_netlink(f
, NULL
, IPPROTO_TCP
) == 0)
3519 /* Sigh... We have to parse /proc/net/tcp... */
3520 while (bufsize
>= 64*1024) {
3521 if ((buf
= malloc(bufsize
)) != NULL
)
3530 if (f
->families
& FAMILY_MASK(AF_INET
)) {
3531 if ((fp
= net_tcp_open()) == NULL
)
3534 setbuffer(fp
, buf
, bufsize
);
3535 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET
))
3540 if ((f
->families
& FAMILY_MASK(AF_INET6
)) &&
3541 (fp
= net_tcp6_open()) != NULL
) {
3542 setbuffer(fp
, buf
, bufsize
);
3543 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET6
))
3553 int saved_errno
= errno
;
3558 errno
= saved_errno
;
3563 static int dccp_show(struct filter
*f
)
3565 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3568 if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT")
3569 && inet_show_netlink(f
, NULL
, IPPROTO_DCCP
) == 0)
3575 static int sctp_show(struct filter
*f
)
3577 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3580 if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT")
3581 && inet_show_netlink(f
, NULL
, IPPROTO_SCTP
) == 0)
3587 static int dgram_show_line(char *line
, const struct filter
*f
, int family
)
3589 struct sockstat s
= {};
3590 char *loc
, *rem
, *data
;
3594 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
3597 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
3599 if (!(f
->states
& (1 << state
)))
3602 proc_parse_inet_addr(loc
, rem
, family
, &s
);
3604 if (f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
3608 n
= sscanf(data
, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
3609 &s
.state
, &s
.wq
, &s
.rq
,
3611 &s
.refcnt
, &s
.sk
, opt
);
3616 s
.type
= dg_proto
== UDP_PROTO
? IPPROTO_UDP
: 0;
3617 inet_stats_print(&s
, false);
3619 if (show_details
&& opt
[0])
3620 out(" opt:\"%s\"", opt
);
3625 static int udp_show(struct filter
*f
)
3629 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3632 dg_proto
= UDP_PROTO
;
3634 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
3635 && inet_show_netlink(f
, NULL
, IPPROTO_UDP
) == 0)
3638 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3639 if ((fp
= net_udp_open()) == NULL
)
3641 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3646 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3647 (fp
= net_udp6_open()) != NULL
) {
3648 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3656 int saved_errno
= errno
;
3660 errno
= saved_errno
;
3665 static int raw_show(struct filter
*f
)
3669 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3672 dg_proto
= RAW_PROTO
;
3674 if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") &&
3675 inet_show_netlink(f
, NULL
, IPPROTO_RAW
) == 0)
3678 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3679 if ((fp
= net_raw_open()) == NULL
)
3681 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3686 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3687 (fp
= net_raw6_open()) != NULL
) {
3688 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3696 int saved_errno
= errno
;
3700 errno
= saved_errno
;
3705 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
3707 static void unix_list_drop_first(struct sockstat
**list
)
3709 struct sockstat
*s
= *list
;
3711 (*list
) = (*list
)->next
;
3716 static bool unix_type_skip(struct sockstat
*s
, struct filter
*f
)
3718 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
&(1<<UNIX_ST_DB
)))
3720 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<UNIX_DG_DB
)))
3722 if (s
->type
== SOCK_SEQPACKET
&& !(f
->dbs
&(1<<UNIX_SQ_DB
)))
3727 static void unix_stats_print(struct sockstat
*s
, struct filter
*f
)
3729 char port_name
[30] = {};
3731 sock_state_print(s
);
3733 sock_addr_print(s
->name
?: "*", " ",
3734 int_to_str(s
->lport
, port_name
), NULL
);
3735 sock_addr_print(s
->peer_name
?: "*", " ",
3736 int_to_str(s
->rport
, port_name
), NULL
);
3741 static int unix_show_sock(struct nlmsghdr
*nlh
, void *arg
)
3743 struct filter
*f
= (struct filter
*)arg
;
3744 struct unix_diag_msg
*r
= NLMSG_DATA(nlh
);
3745 struct rtattr
*tb
[UNIX_DIAG_MAX
+1];
3747 struct sockstat stat
= { .name
= "*", .peer_name
= "*" };
3749 parse_rtattr(tb
, UNIX_DIAG_MAX
, (struct rtattr
*)(r
+1),
3750 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3752 stat
.type
= r
->udiag_type
;
3753 stat
.state
= r
->udiag_state
;
3754 stat
.ino
= stat
.lport
= r
->udiag_ino
;
3755 stat
.local
.family
= stat
.remote
.family
= AF_UNIX
;
3757 if (unix_type_skip(&stat
, f
))
3760 if (tb
[UNIX_DIAG_RQLEN
]) {
3761 struct unix_diag_rqlen
*rql
= RTA_DATA(tb
[UNIX_DIAG_RQLEN
]);
3763 stat
.rq
= rql
->udiag_rqueue
;
3764 stat
.wq
= rql
->udiag_wqueue
;
3766 if (tb
[UNIX_DIAG_NAME
]) {
3767 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_NAME
]);
3769 memcpy(name
, RTA_DATA(tb
[UNIX_DIAG_NAME
]), len
);
3771 if (name
[0] == '\0') {
3773 for (i
= 0; i
< len
; i
++)
3774 if (name
[i
] == '\0')
3777 stat
.name
= &name
[0];
3778 memcpy(stat
.local
.data
, &stat
.name
, sizeof(stat
.name
));
3780 if (tb
[UNIX_DIAG_PEER
])
3781 stat
.rport
= rta_getattr_u32(tb
[UNIX_DIAG_PEER
]);
3783 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
3786 unix_stats_print(&stat
, f
);
3789 print_skmeminfo(tb
, UNIX_DIAG_MEMINFO
);
3791 if (tb
[UNIX_DIAG_SHUTDOWN
]) {
3794 mask
= rta_getattr_u8(tb
[UNIX_DIAG_SHUTDOWN
]);
3796 mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
3798 if (tb
[UNIX_DIAG_VFS
]) {
3799 struct unix_diag_vfs
*uv
= RTA_DATA(tb
[UNIX_DIAG_VFS
]);
3801 out(" ino:%u dev:%u/%u", uv
->udiag_vfs_ino
, major(uv
->udiag_vfs_dev
),
3802 minor(uv
->udiag_vfs_dev
));
3804 if (tb
[UNIX_DIAG_ICONS
]) {
3805 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_ICONS
]);
3806 __u32
*peers
= RTA_DATA(tb
[UNIX_DIAG_ICONS
]);
3810 for (i
= 0; i
< len
/ sizeof(__u32
); i
++)
3811 out(" %u", peers
[i
]);
3818 static int handle_netlink_request(struct filter
*f
, struct nlmsghdr
*req
,
3819 size_t size
, rtnl_filter_t show_one_sock
)
3822 struct rtnl_handle rth
;
3824 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
3827 rth
.dump
= MAGIC_SEQ
;
3829 if (rtnl_send(&rth
, req
, size
) < 0)
3832 if (rtnl_dump_filter(&rth
, show_one_sock
, f
))
3841 static int unix_show_netlink(struct filter
*f
)
3843 DIAG_REQUEST(req
, struct unix_diag_req r
);
3845 req
.r
.sdiag_family
= AF_UNIX
;
3846 req
.r
.udiag_states
= f
->states
;
3847 req
.r
.udiag_show
= UDIAG_SHOW_NAME
| UDIAG_SHOW_PEER
| UDIAG_SHOW_RQLEN
;
3849 req
.r
.udiag_show
|= UDIAG_SHOW_MEMINFO
;
3851 req
.r
.udiag_show
|= UDIAG_SHOW_VFS
| UDIAG_SHOW_ICONS
;
3853 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), unix_show_sock
);
3856 static int unix_show(struct filter
*f
)
3863 struct sockstat
*list
= NULL
;
3864 const int unix_state_map
[] = { SS_CLOSE
, SS_SYN_SENT
,
3865 SS_ESTABLISHED
, SS_CLOSING
};
3867 if (!filter_af_get(f
, AF_UNIX
))
3870 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
3871 && unix_show_netlink(f
) == 0)
3874 if ((fp
= net_unix_open()) == NULL
)
3876 if (!fgets(buf
, sizeof(buf
), fp
)) {
3881 if (memcmp(buf
, "Peer", 4) == 0)
3885 while (fgets(buf
, sizeof(buf
), fp
)) {
3886 struct sockstat
*u
, **insp
;
3889 if (!(u
= calloc(1, sizeof(*u
))))
3892 if (sscanf(buf
, "%x: %x %x %x %x %x %d %s",
3893 &u
->rport
, &u
->rq
, &u
->wq
, &flags
, &u
->type
,
3894 &u
->state
, &u
->ino
, name
) < 8)
3898 u
->local
.family
= u
->remote
.family
= AF_UNIX
;
3900 if (flags
& (1 << 16)) {
3901 u
->state
= SS_LISTEN
;
3902 } else if (u
->state
> 0 &&
3903 u
->state
<= ARRAY_SIZE(unix_state_map
)) {
3904 u
->state
= unix_state_map
[u
->state
-1];
3905 if (u
->type
== SOCK_DGRAM
&& u
->state
== SS_CLOSE
&& u
->rport
)
3906 u
->state
= SS_ESTABLISHED
;
3908 if (unix_type_skip(u
, f
) ||
3909 !(f
->states
& (1 << u
->state
))) {
3921 u
->name
= strdup(name
);
3931 for (p
= list
; p
; p
= p
->next
) {
3932 if (u
->rport
== p
->lport
)
3938 u
->peer_name
= p
->name
? : "*";
3942 struct sockstat st
= {
3943 .local
.family
= AF_UNIX
,
3944 .remote
.family
= AF_UNIX
,
3947 memcpy(st
.local
.data
, &u
->name
, sizeof(u
->name
));
3948 /* when parsing the old format rport is set to 0 and
3949 * therefore peer_name remains NULL
3951 if (u
->peer_name
&& strcmp(u
->peer_name
, "*"))
3952 memcpy(st
.remote
.data
, &u
->peer_name
,
3953 sizeof(u
->peer_name
));
3954 if (run_ssfilter(f
->f
, &st
) == 0) {
3963 if (u
->type
< (*insp
)->type
||
3964 (u
->type
== (*insp
)->type
&&
3965 u
->ino
< (*insp
)->ino
))
3967 insp
= &(*insp
)->next
;
3972 if (++cnt
> MAX_UNIX_REMEMBER
) {
3974 unix_stats_print(list
, f
);
3975 unix_list_drop_first(&list
);
3982 unix_stats_print(list
, f
);
3983 unix_list_drop_first(&list
);
3989 static int packet_stats_print(struct sockstat
*s
, const struct filter
*f
)
3991 const char *addr
, *port
;
3994 s
->local
.family
= s
->remote
.family
= AF_PACKET
;
3997 s
->local
.data
[0] = s
->prot
;
3998 if (run_ssfilter(f
->f
, s
) == 0)
4002 sock_state_print(s
);
4007 addr
= ll_proto_n2a(htons(s
->prot
), ll_name
, sizeof(ll_name
));
4012 port
= xll_index_to_name(s
->iface
);
4014 sock_addr_print(addr
, ":", port
, NULL
);
4015 sock_addr_print("", "*", "", NULL
);
4020 sock_details_print(s
);
4025 static void packet_show_ring(struct packet_diag_ring
*ring
)
4027 out("blk_size:%d", ring
->pdr_block_size
);
4028 out(",blk_nr:%d", ring
->pdr_block_nr
);
4029 out(",frm_size:%d", ring
->pdr_frame_size
);
4030 out(",frm_nr:%d", ring
->pdr_frame_nr
);
4031 out(",tmo:%d", ring
->pdr_retire_tmo
);
4032 out(",features:0x%x", ring
->pdr_features
);
4035 static int packet_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4037 const struct filter
*f
= arg
;
4038 struct packet_diag_msg
*r
= NLMSG_DATA(nlh
);
4039 struct packet_diag_info
*pinfo
= NULL
;
4040 struct packet_diag_ring
*ring_rx
= NULL
, *ring_tx
= NULL
;
4041 struct rtattr
*tb
[PACKET_DIAG_MAX
+1];
4042 struct sockstat stat
= {};
4043 uint32_t fanout
= 0;
4044 bool has_fanout
= false;
4046 parse_rtattr(tb
, PACKET_DIAG_MAX
, (struct rtattr
*)(r
+1),
4047 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
4049 /* use /proc/net/packet if all info are not available */
4050 if (!tb
[PACKET_DIAG_MEMINFO
])
4053 stat
.type
= r
->pdiag_type
;
4054 stat
.prot
= r
->pdiag_num
;
4055 stat
.ino
= r
->pdiag_ino
;
4056 stat
.state
= SS_CLOSE
;
4057 stat
.sk
= cookie_sk_get(&r
->pdiag_cookie
[0]);
4059 if (tb
[PACKET_DIAG_MEMINFO
]) {
4060 __u32
*skmeminfo
= RTA_DATA(tb
[PACKET_DIAG_MEMINFO
]);
4062 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
4065 if (tb
[PACKET_DIAG_INFO
]) {
4066 pinfo
= RTA_DATA(tb
[PACKET_DIAG_INFO
]);
4067 stat
.lport
= stat
.iface
= pinfo
->pdi_index
;
4070 if (tb
[PACKET_DIAG_UID
])
4071 stat
.uid
= rta_getattr_u32(tb
[PACKET_DIAG_UID
]);
4073 if (tb
[PACKET_DIAG_RX_RING
])
4074 ring_rx
= RTA_DATA(tb
[PACKET_DIAG_RX_RING
]);
4076 if (tb
[PACKET_DIAG_TX_RING
])
4077 ring_tx
= RTA_DATA(tb
[PACKET_DIAG_TX_RING
]);
4079 if (tb
[PACKET_DIAG_FANOUT
]) {
4081 fanout
= rta_getattr_u32(tb
[PACKET_DIAG_FANOUT
]);
4084 if (packet_stats_print(&stat
, f
))
4090 out(" ver:%d", pinfo
->pdi_version
);
4092 out("\n\tver:%d", pinfo
->pdi_version
);
4093 out(" cpy_thresh:%d", pinfo
->pdi_copy_thresh
);
4095 if (pinfo
->pdi_flags
& PDI_RUNNING
)
4097 if (pinfo
->pdi_flags
& PDI_AUXDATA
)
4099 if (pinfo
->pdi_flags
& PDI_ORIGDEV
)
4101 if (pinfo
->pdi_flags
& PDI_VNETHDR
)
4103 if (pinfo
->pdi_flags
& PDI_LOSS
)
4105 if (!pinfo
->pdi_flags
)
4113 out("\n\tring_rx(");
4114 packet_show_ring(ring_rx
);
4121 out("\n\tring_tx(");
4122 packet_show_ring(ring_tx
);
4126 uint16_t type
= (fanout
>> 16) & 0xffff;
4132 out("id:%d,", fanout
& 0xffff);
4154 if (show_bpf
&& tb
[PACKET_DIAG_FILTER
]) {
4155 struct sock_filter
*fil
=
4156 RTA_DATA(tb
[PACKET_DIAG_FILTER
]);
4157 int num
= RTA_PAYLOAD(tb
[PACKET_DIAG_FILTER
]) /
4158 sizeof(struct sock_filter
);
4161 out(" bpf filter (%d): ", num
);
4163 out("\n\tbpf filter (%d): ", num
);
4165 out(" 0x%02x %u %u %u,",
4166 fil
->code
, fil
->jt
, fil
->jf
, fil
->k
);
4173 print_skmeminfo(tb
, PACKET_DIAG_MEMINFO
);
4177 static int packet_show_netlink(struct filter
*f
)
4179 DIAG_REQUEST(req
, struct packet_diag_req r
);
4181 req
.r
.sdiag_family
= AF_PACKET
;
4182 req
.r
.pdiag_show
= PACKET_SHOW_INFO
| PACKET_SHOW_MEMINFO
|
4183 PACKET_SHOW_FILTER
| PACKET_SHOW_RING_CFG
| PACKET_SHOW_FANOUT
;
4185 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), packet_show_sock
);
4188 static int packet_show_line(char *buf
, const struct filter
*f
, int fam
)
4190 unsigned long long sk
;
4191 struct sockstat stat
= {};
4192 int type
, prot
, iface
, state
, rq
, uid
, ino
;
4194 sscanf(buf
, "%llx %*d %d %x %d %d %u %u %u",
4196 &type
, &prot
, &iface
, &state
,
4199 if (stat
.type
== SOCK_RAW
&& !(f
->dbs
&(1<<PACKET_R_DB
)))
4201 if (stat
.type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<PACKET_DG_DB
)))
4206 stat
.lport
= stat
.iface
= iface
;
4211 stat
.state
= SS_CLOSE
;
4213 if (packet_stats_print(&stat
, f
))
4219 static int packet_show(struct filter
*f
)
4224 if (!filter_af_get(f
, AF_PACKET
) || !(f
->states
& (1 << SS_CLOSE
)))
4227 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
4228 packet_show_netlink(f
) == 0)
4231 if ((fp
= net_packet_open()) == NULL
)
4233 if (generic_record_read(fp
, packet_show_line
, f
, AF_PACKET
))
4240 static int xdp_stats_print(struct sockstat
*s
, const struct filter
*f
)
4242 const char *addr
, *port
;
4245 s
->local
.family
= s
->remote
.family
= AF_XDP
;
4248 if (run_ssfilter(f
->f
, s
) == 0)
4252 sock_state_print(s
);
4255 addr
= xll_index_to_name(s
->iface
);
4256 snprintf(q_str
, sizeof(q_str
), "q%d", s
->lport
);
4258 sock_addr_print(addr
, ":", port
, NULL
);
4260 sock_addr_print("", "*", "", NULL
);
4263 sock_addr_print("", "*", "", NULL
);
4268 sock_details_print(s
);
4273 static void xdp_show_ring(const char *name
, struct xdp_diag_ring
*ring
)
4278 out("\n\t%s(", name
);
4279 out("entries:%u", ring
->entries
);
4283 static void xdp_show_umem(struct xdp_diag_umem
*umem
, struct xdp_diag_ring
*fr
,
4284 struct xdp_diag_ring
*cr
)
4290 out("id:%u", umem
->id
);
4291 out(",size:%llu", umem
->size
);
4292 out(",num_pages:%u", umem
->num_pages
);
4293 out(",chunk_size:%u", umem
->chunk_size
);
4294 out(",headroom:%u", umem
->headroom
);
4295 out(",ifindex:%u", umem
->ifindex
);
4296 out(",qid:%u", umem
->queue_id
);
4297 out(",zc:%u", umem
->flags
& XDP_DU_F_ZEROCOPY
);
4298 out(",refs:%u", umem
->refs
);
4302 xdp_show_ring("fr", fr
);
4304 xdp_show_ring("cr", cr
);
4307 static int xdp_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4309 struct xdp_diag_ring
*rx
= NULL
, *tx
= NULL
, *fr
= NULL
, *cr
= NULL
;
4310 struct xdp_diag_msg
*msg
= NLMSG_DATA(nlh
);
4311 struct rtattr
*tb
[XDP_DIAG_MAX
+ 1];
4312 struct xdp_diag_info
*info
= NULL
;
4313 struct xdp_diag_umem
*umem
= NULL
;
4314 const struct filter
*f
= arg
;
4315 struct sockstat stat
= {};
4317 parse_rtattr(tb
, XDP_DIAG_MAX
, (struct rtattr
*)(msg
+ 1),
4318 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*msg
)));
4320 stat
.type
= msg
->xdiag_type
;
4321 stat
.ino
= msg
->xdiag_ino
;
4322 stat
.state
= SS_CLOSE
;
4323 stat
.sk
= cookie_sk_get(&msg
->xdiag_cookie
[0]);
4325 if (tb
[XDP_DIAG_INFO
]) {
4326 info
= RTA_DATA(tb
[XDP_DIAG_INFO
]);
4327 stat
.iface
= info
->ifindex
;
4328 stat
.lport
= info
->queue_id
;
4331 if (tb
[XDP_DIAG_UID
])
4332 stat
.uid
= rta_getattr_u32(tb
[XDP_DIAG_UID
]);
4333 if (tb
[XDP_DIAG_RX_RING
])
4334 rx
= RTA_DATA(tb
[XDP_DIAG_RX_RING
]);
4335 if (tb
[XDP_DIAG_TX_RING
])
4336 tx
= RTA_DATA(tb
[XDP_DIAG_TX_RING
]);
4337 if (tb
[XDP_DIAG_UMEM
])
4338 umem
= RTA_DATA(tb
[XDP_DIAG_UMEM
]);
4339 if (tb
[XDP_DIAG_UMEM_FILL_RING
])
4340 fr
= RTA_DATA(tb
[XDP_DIAG_UMEM_FILL_RING
]);
4341 if (tb
[XDP_DIAG_UMEM_COMPLETION_RING
])
4342 cr
= RTA_DATA(tb
[XDP_DIAG_UMEM_COMPLETION_RING
]);
4343 if (tb
[XDP_DIAG_MEMINFO
]) {
4344 __u32
*skmeminfo
= RTA_DATA(tb
[XDP_DIAG_MEMINFO
]);
4346 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
4349 if (xdp_stats_print(&stat
, f
))
4354 xdp_show_ring("rx", rx
);
4356 xdp_show_ring("tx", tx
);
4358 xdp_show_umem(umem
, fr
, cr
);
4362 print_skmeminfo(tb
, XDP_DIAG_MEMINFO
); // really?
4368 static int xdp_show(struct filter
*f
)
4370 DIAG_REQUEST(req
, struct xdp_diag_req r
);
4372 if (!filter_af_get(f
, AF_XDP
) || !(f
->states
& (1 << SS_CLOSE
)))
4375 req
.r
.sdiag_family
= AF_XDP
;
4376 req
.r
.xdiag_show
= XDP_SHOW_INFO
| XDP_SHOW_RING_CFG
| XDP_SHOW_UMEM
|
4379 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), xdp_show_sock
);
4382 static int netlink_show_one(struct filter
*f
,
4383 int prot
, int pid
, unsigned int groups
,
4384 int state
, int dst_pid
, unsigned int dst_group
,
4386 unsigned long long sk
, unsigned long long cb
)
4388 struct sockstat st
= {
4392 .local
.family
= AF_NETLINK
,
4393 .remote
.family
= AF_NETLINK
,
4396 SPRINT_BUF(prot_buf
) = {};
4397 const char *prot_name
;
4398 char procname
[64] = {};
4403 st
.local
.data
[0] = prot
;
4404 if (run_ssfilter(f
->f
, &st
) == 0)
4408 sock_state_print(&st
);
4410 prot_name
= nl_proto_n2a(prot
, prot_buf
, sizeof(prot_buf
));
4414 } else if (!numeric
) {
4419 strncpy(procname
, "kernel", 7);
4420 } else if (pid
> 0) {
4423 snprintf(procname
, sizeof(procname
), "%s/%d/stat",
4424 getenv("PROC_ROOT") ? : "/proc", pid
);
4425 if ((fp
= fopen(procname
, "r")) != NULL
) {
4426 if (fscanf(fp
, "%*d (%[^)])", procname
) == 1) {
4427 snprintf(procname
+strlen(procname
),
4428 sizeof(procname
)-strlen(procname
),
4436 int_to_str(pid
, procname
);
4438 int_to_str(pid
, procname
);
4441 sock_addr_print(prot_name
, ":", procname
, NULL
);
4443 if (state
== NETLINK_CONNECTED
) {
4444 char dst_group_buf
[30];
4445 char dst_pid_buf
[30];
4447 sock_addr_print(int_to_str(dst_group
, dst_group_buf
), ":",
4448 int_to_str(dst_pid
, dst_pid_buf
), NULL
);
4450 sock_addr_print("", "*", "", NULL
);
4453 char *pid_context
= NULL
;
4455 if (show_proc_ctx
) {
4456 /* The pid value will either be:
4457 * 0 if destination kernel - show kernel initial context.
4458 * A valid process pid - use getpidcon.
4459 * A unique value allocated by the kernel or netlink user
4460 * to the process - show context as "not available".
4463 security_get_initial_context("kernel", &pid_context
);
4465 getpidcon(pid
, &pid_context
);
4467 out(" proc_ctx=%s", pid_context
? : "unavailable");
4472 out(" sk=%llx cb=%llx groups=0x%08x", sk
, cb
, groups
);
4478 static int netlink_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4480 struct filter
*f
= (struct filter
*)arg
;
4481 struct netlink_diag_msg
*r
= NLMSG_DATA(nlh
);
4482 struct rtattr
*tb
[NETLINK_DIAG_MAX
+1];
4484 unsigned long groups
= 0;
4486 parse_rtattr(tb
, NETLINK_DIAG_MAX
, (struct rtattr
*)(r
+1),
4487 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
4489 if (tb
[NETLINK_DIAG_GROUPS
] && RTA_PAYLOAD(tb
[NETLINK_DIAG_GROUPS
]))
4490 groups
= *(unsigned long *) RTA_DATA(tb
[NETLINK_DIAG_GROUPS
]);
4492 if (tb
[NETLINK_DIAG_MEMINFO
]) {
4493 const __u32
*skmeminfo
;
4495 skmeminfo
= RTA_DATA(tb
[NETLINK_DIAG_MEMINFO
]);
4497 rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
4498 wq
= skmeminfo
[SK_MEMINFO_WMEM_ALLOC
];
4501 if (netlink_show_one(f
, r
->ndiag_protocol
, r
->ndiag_portid
, groups
,
4502 r
->ndiag_state
, r
->ndiag_dst_portid
, r
->ndiag_dst_group
,
4509 print_skmeminfo(tb
, NETLINK_DIAG_MEMINFO
);
4515 static int netlink_show_netlink(struct filter
*f
)
4517 DIAG_REQUEST(req
, struct netlink_diag_req r
);
4519 req
.r
.sdiag_family
= AF_NETLINK
;
4520 req
.r
.sdiag_protocol
= NDIAG_PROTO_ALL
;
4521 req
.r
.ndiag_show
= NDIAG_SHOW_GROUPS
| NDIAG_SHOW_MEMINFO
;
4523 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), netlink_show_sock
);
4526 static int netlink_show(struct filter
*f
)
4531 unsigned int groups
;
4533 unsigned long long sk
, cb
;
4535 if (!filter_af_get(f
, AF_NETLINK
) || !(f
->states
& (1 << SS_CLOSE
)))
4538 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
4539 netlink_show_netlink(f
) == 0)
4542 if ((fp
= net_netlink_open()) == NULL
)
4544 if (!fgets(buf
, sizeof(buf
), fp
)) {
4549 while (fgets(buf
, sizeof(buf
), fp
)) {
4550 sscanf(buf
, "%llx %d %d %x %d %d %llx %d",
4552 &prot
, &pid
, &groups
, &rq
, &wq
, &cb
, &rc
);
4554 netlink_show_one(f
, prot
, pid
, groups
, 0, 0, 0, rq
, wq
, sk
, cb
);
4561 static bool vsock_type_skip(struct sockstat
*s
, struct filter
*f
)
4563 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
& (1 << VSOCK_ST_DB
)))
4565 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
& (1 << VSOCK_DG_DB
)))
4570 static void vsock_addr_print(inet_prefix
*a
, __u32 port
)
4572 char cid_str
[sizeof("4294967295")];
4573 char port_str
[sizeof("4294967295")];
4576 memcpy(&cid
, a
->data
, sizeof(cid
));
4578 if (cid
== ~(__u32
)0)
4579 snprintf(cid_str
, sizeof(cid_str
), "*");
4581 snprintf(cid_str
, sizeof(cid_str
), "%u", cid
);
4583 if (port
== ~(__u32
)0)
4584 snprintf(port_str
, sizeof(port_str
), "*");
4586 snprintf(port_str
, sizeof(port_str
), "%u", port
);
4588 sock_addr_print(cid_str
, ":", port_str
, NULL
);
4591 static void vsock_stats_print(struct sockstat
*s
, struct filter
*f
)
4593 sock_state_print(s
);
4595 vsock_addr_print(&s
->local
, s
->lport
);
4596 vsock_addr_print(&s
->remote
, s
->rport
);
4601 static int vsock_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4603 struct filter
*f
= (struct filter
*)arg
;
4604 struct vsock_diag_msg
*r
= NLMSG_DATA(nlh
);
4605 struct sockstat stat
= {
4606 .type
= r
->vdiag_type
,
4607 .lport
= r
->vdiag_src_port
,
4608 .rport
= r
->vdiag_dst_port
,
4609 .state
= r
->vdiag_state
,
4610 .ino
= r
->vdiag_ino
,
4613 vsock_set_inet_prefix(&stat
.local
, r
->vdiag_src_cid
);
4614 vsock_set_inet_prefix(&stat
.remote
, r
->vdiag_dst_cid
);
4616 if (vsock_type_skip(&stat
, f
))
4619 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
4622 vsock_stats_print(&stat
, f
);
4627 static int vsock_show(struct filter
*f
)
4629 DIAG_REQUEST(req
, struct vsock_diag_req r
);
4631 if (!filter_af_get(f
, AF_VSOCK
))
4634 req
.r
.sdiag_family
= AF_VSOCK
;
4635 req
.r
.vdiag_states
= f
->states
;
4637 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), vsock_show_sock
);
4640 static void tipc_sock_addr_print(struct rtattr
*net_addr
, struct rtattr
*id
)
4642 uint32_t node
= rta_getattr_u32(net_addr
);
4643 uint32_t identity
= rta_getattr_u32(id
);
4645 SPRINT_BUF(addr
) = {};
4646 SPRINT_BUF(port
) = {};
4648 sprintf(addr
, "%u", node
);
4649 sprintf(port
, "%u", identity
);
4650 sock_addr_print(addr
, ":", port
, NULL
);
4654 static int tipc_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4656 struct rtattr
*stat
[TIPC_NLA_SOCK_STAT_MAX
+ 1] = {};
4657 struct rtattr
*attrs
[TIPC_NLA_SOCK_MAX
+ 1] = {};
4658 struct rtattr
*con
[TIPC_NLA_CON_MAX
+ 1] = {};
4659 struct rtattr
*info
[TIPC_NLA_MAX
+ 1] = {};
4660 struct rtattr
*msg_ref
;
4661 struct sockstat ss
= {};
4663 parse_rtattr(info
, TIPC_NLA_MAX
, NLMSG_DATA(nlh
),
4664 NLMSG_PAYLOAD(nlh
, 0));
4666 if (!info
[TIPC_NLA_SOCK
])
4669 msg_ref
= info
[TIPC_NLA_SOCK
];
4670 parse_rtattr(attrs
, TIPC_NLA_SOCK_MAX
, RTA_DATA(msg_ref
),
4671 RTA_PAYLOAD(msg_ref
));
4673 msg_ref
= attrs
[TIPC_NLA_SOCK_STAT
];
4674 parse_rtattr(stat
, TIPC_NLA_SOCK_STAT_MAX
,
4675 RTA_DATA(msg_ref
), RTA_PAYLOAD(msg_ref
));
4678 ss
.local
.family
= AF_TIPC
;
4679 ss
.type
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_TYPE
]);
4680 ss
.state
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_TIPC_STATE
]);
4681 ss
.uid
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_UID
]);
4682 ss
.ino
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_INO
]);
4683 ss
.rq
= rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_RCVQ
]);
4684 ss
.wq
= rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_SENDQ
]);
4685 ss
.sk
= rta_getattr_u64(attrs
[TIPC_NLA_SOCK_COOKIE
]);
4687 sock_state_print (&ss
);
4689 tipc_sock_addr_print(attrs
[TIPC_NLA_SOCK_ADDR
],
4690 attrs
[TIPC_NLA_SOCK_REF
]);
4692 msg_ref
= attrs
[TIPC_NLA_SOCK_CON
];
4694 parse_rtattr(con
, TIPC_NLA_CON_MAX
,
4695 RTA_DATA(msg_ref
), RTA_PAYLOAD(msg_ref
));
4697 tipc_sock_addr_print(con
[TIPC_NLA_CON_NODE
],
4698 con
[TIPC_NLA_CON_SOCK
]);
4700 sock_addr_print("", "-", "", NULL
);
4703 sock_details_print(&ss
);
4705 proc_ctx_print(&ss
);
4707 if (show_tipcinfo
) {
4709 out(" type:%s", stype_nameg
[ss
.type
]);
4711 out("\n type:%s", stype_nameg
[ss
.type
]);
4713 stat
[TIPC_NLA_SOCK_STAT_LINK_CONG
] ? "link" :
4714 stat
[TIPC_NLA_SOCK_STAT_CONN_CONG
] ? "conn" : "none");
4716 rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_DROP
]));
4718 if (attrs
[TIPC_NLA_SOCK_HAS_PUBL
])
4721 if (con
[TIPC_NLA_CON_FLAG
])
4722 out(" via {%u,%u} ",
4723 rta_getattr_u32(con
[TIPC_NLA_CON_TYPE
]),
4724 rta_getattr_u32(con
[TIPC_NLA_CON_INST
]));
4730 static int tipc_show(struct filter
*f
)
4732 DIAG_REQUEST(req
, struct tipc_sock_diag_req r
);
4734 memset(&req
.r
, 0, sizeof(req
.r
));
4735 req
.r
.sdiag_family
= AF_TIPC
;
4736 req
.r
.tidiag_states
= f
->states
;
4738 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), tipc_show_sock
);
4741 struct sock_diag_msg
{
4745 static int generic_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4747 struct sock_diag_msg
*r
= NLMSG_DATA(nlh
);
4748 struct inet_diag_arg inet_arg
= { .f
= arg
, .protocol
= IPPROTO_MAX
};
4751 switch (r
->sdiag_family
) {
4754 inet_arg
.rth
= inet_arg
.f
->rth_for_killing
;
4755 ret
= show_one_inet_sock(nlh
, &inet_arg
);
4758 ret
= unix_show_sock(nlh
, arg
);
4761 ret
= packet_show_sock(nlh
, arg
);
4764 ret
= netlink_show_sock(nlh
, arg
);
4767 ret
= vsock_show_sock(nlh
, arg
);
4770 ret
= xdp_show_sock(nlh
, arg
);
4781 static int handle_follow_request(struct filter
*f
)
4785 struct rtnl_handle rth
, rth2
;
4787 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << TCP_DB
))
4788 groups
|= 1 << (SKNLGRP_INET_TCP_DESTROY
- 1);
4789 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << UDP_DB
))
4790 groups
|= 1 << (SKNLGRP_INET_UDP_DESTROY
- 1);
4791 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << TCP_DB
))
4792 groups
|= 1 << (SKNLGRP_INET6_TCP_DESTROY
- 1);
4793 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << UDP_DB
))
4794 groups
|= 1 << (SKNLGRP_INET6_UDP_DESTROY
- 1);
4799 if (rtnl_open_byproto(&rth
, groups
, NETLINK_SOCK_DIAG
))
4803 rth
.local
.nl_pid
= 0;
4806 if (rtnl_open_byproto(&rth2
, groups
, NETLINK_SOCK_DIAG
)) {
4810 f
->rth_for_killing
= &rth2
;
4813 if (rtnl_dump_filter(&rth
, generic_show_sock
, f
))
4817 if (f
->rth_for_killing
)
4818 rtnl_close(f
->rth_for_killing
);
4822 static int get_snmp_int(char *proto
, char *key
, int *result
)
4826 int protolen
= strlen(proto
);
4827 int keylen
= strlen(key
);
4831 if ((fp
= net_snmp_open()) == NULL
)
4834 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
4838 if (memcmp(buf
, proto
, protolen
))
4840 while ((p
= strchr(p
, ' ')) != NULL
) {
4843 if (memcmp(p
, key
, keylen
) == 0 &&
4844 (p
[keylen
] == ' ' || p
[keylen
] == '\n'))
4847 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
4849 if (memcmp(buf
, proto
, protolen
))
4852 while ((p
= strchr(p
, ' ')) != NULL
) {
4855 sscanf(p
, "%d", result
);
4868 /* Get stats from sockstat */
4888 static void get_sockstat_line(char *line
, struct ssummary
*s
)
4890 char id
[256], rem
[256];
4892 if (sscanf(line
, "%[^ ] %[^\n]\n", id
, rem
) != 2)
4895 if (strcmp(id
, "sockets:") == 0)
4896 sscanf(rem
, "%*s%d", &s
->socks
);
4897 else if (strcmp(id
, "UDP:") == 0)
4898 sscanf(rem
, "%*s%d", &s
->udp4
);
4899 else if (strcmp(id
, "UDP6:") == 0)
4900 sscanf(rem
, "%*s%d", &s
->udp6
);
4901 else if (strcmp(id
, "RAW:") == 0)
4902 sscanf(rem
, "%*s%d", &s
->raw4
);
4903 else if (strcmp(id
, "RAW6:") == 0)
4904 sscanf(rem
, "%*s%d", &s
->raw6
);
4905 else if (strcmp(id
, "TCP6:") == 0)
4906 sscanf(rem
, "%*s%d", &s
->tcp6_hashed
);
4907 else if (strcmp(id
, "FRAG:") == 0)
4908 sscanf(rem
, "%*s%d%*s%d", &s
->frag4
, &s
->frag4_mem
);
4909 else if (strcmp(id
, "FRAG6:") == 0)
4910 sscanf(rem
, "%*s%d%*s%d", &s
->frag6
, &s
->frag6_mem
);
4911 else if (strcmp(id
, "TCP:") == 0)
4912 sscanf(rem
, "%*s%d%*s%d%*s%d%*s%d%*s%d",
4914 &s
->tcp_orphans
, &s
->tcp_tws
, &s
->tcp_total
, &s
->tcp_mem
);
4917 static int get_sockstat(struct ssummary
*s
)
4922 memset(s
, 0, sizeof(*s
));
4924 if ((fp
= net_sockstat_open()) == NULL
)
4926 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4927 get_sockstat_line(buf
, s
);
4930 if ((fp
= net_sockstat6_open()) == NULL
)
4932 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4933 get_sockstat_line(buf
, s
);
4939 static int print_summary(void)
4944 if (get_sockstat(&s
) < 0)
4945 perror("ss: get_sockstat");
4946 if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab
) < 0)
4947 perror("ss: get_snmpstat");
4949 printf("Total: %d\n", s
.socks
);
4951 printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n",
4952 s
.tcp_total
+ s
.tcp_tws
, tcp_estab
,
4953 s
.tcp_total
- (s
.tcp4_hashed
+ s
.tcp6_hashed
- s
.tcp_tws
),
4954 s
.tcp_orphans
, s
.tcp_tws
);
4957 printf("Transport Total IP IPv6\n");
4958 printf("RAW %-9d %-9d %-9d\n", s
.raw4
+s
.raw6
, s
.raw4
, s
.raw6
);
4959 printf("UDP %-9d %-9d %-9d\n", s
.udp4
+s
.udp6
, s
.udp4
, s
.udp6
);
4960 printf("TCP %-9d %-9d %-9d\n", s
.tcp4_hashed
+s
.tcp6_hashed
, s
.tcp4_hashed
, s
.tcp6_hashed
);
4961 printf("INET %-9d %-9d %-9d\n",
4962 s
.raw4
+s
.udp4
+s
.tcp4_hashed
+
4963 s
.raw6
+s
.udp6
+s
.tcp6_hashed
,
4964 s
.raw4
+s
.udp4
+s
.tcp4_hashed
,
4965 s
.raw6
+s
.udp6
+s
.tcp6_hashed
);
4966 printf("FRAG %-9d %-9d %-9d\n", s
.frag4
+s
.frag6
, s
.frag4
, s
.frag6
);
4973 static void _usage(FILE *dest
)
4976 "Usage: ss [ OPTIONS ]\n"
4977 " ss [ OPTIONS ] [ FILTER ]\n"
4978 " -h, --help this message\n"
4979 " -V, --version output version information\n"
4980 " -n, --numeric don't resolve service names\n"
4981 " -r, --resolve resolve host names\n"
4982 " -a, --all display all sockets\n"
4983 " -l, --listening display listening sockets\n"
4984 " -o, --options show timer information\n"
4985 " -e, --extended show detailed socket information\n"
4986 " -m, --memory show socket memory usage\n"
4987 " -p, --processes show process using socket\n"
4988 " -i, --info show internal TCP information\n"
4989 " --tipcinfo show internal tipc socket information\n"
4990 " -s, --summary show socket usage summary\n"
4991 " --tos show tos and priority information\n"
4992 " -b, --bpf show bpf filter socket information\n"
4993 " -E, --events continually display sockets as they are destroyed\n"
4994 " -Z, --context display process SELinux security contexts\n"
4995 " -z, --contexts display process and socket SELinux security contexts\n"
4996 " -N, --net switch to the specified network namespace name\n"
4998 " -4, --ipv4 display only IP version 4 sockets\n"
4999 " -6, --ipv6 display only IP version 6 sockets\n"
5000 " -0, --packet display PACKET sockets\n"
5001 " -t, --tcp display only TCP sockets\n"
5002 " -S, --sctp display only SCTP sockets\n"
5003 " -u, --udp display only UDP sockets\n"
5004 " -d, --dccp display only DCCP sockets\n"
5005 " -w, --raw display only RAW sockets\n"
5006 " -x, --unix display only Unix domain sockets\n"
5007 " --tipc display only TIPC sockets\n"
5008 " --vsock display only vsock sockets\n"
5009 " -f, --family=FAMILY display sockets of type FAMILY\n"
5010 " FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help}\n"
5012 " -K, --kill forcibly close sockets, display what was closed\n"
5013 " -H, --no-header Suppress header line\n"
5014 " -O, --oneline socket's data printed on a single line\n"
5016 " -A, --query=QUERY, --socket=QUERY\n"
5017 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY]\n"
5019 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
5020 " -F, --filter=FILE read filter information from FILE\n"
5021 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
5022 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
5023 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n"
5024 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
5025 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
5026 " bucket := {syn-recv|time-wait}\n"
5027 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n"
5031 static void help(void) __attribute__((noreturn
));
5032 static void help(void)
5038 static void usage(void) __attribute__((noreturn
));
5039 static void usage(void)
5046 static int scan_state(const char *state
)
5048 static const char * const sstate_namel
[] = {
5050 [SS_ESTABLISHED
] = "established",
5051 [SS_SYN_SENT
] = "syn-sent",
5052 [SS_SYN_RECV
] = "syn-recv",
5053 [SS_FIN_WAIT1
] = "fin-wait-1",
5054 [SS_FIN_WAIT2
] = "fin-wait-2",
5055 [SS_TIME_WAIT
] = "time-wait",
5056 [SS_CLOSE
] = "unconnected",
5057 [SS_CLOSE_WAIT
] = "close-wait",
5058 [SS_LAST_ACK
] = "last-ack",
5059 [SS_LISTEN
] = "listening",
5060 [SS_CLOSING
] = "closing",
5064 if (strcasecmp(state
, "close") == 0 ||
5065 strcasecmp(state
, "closed") == 0)
5066 return (1<<SS_CLOSE
);
5067 if (strcasecmp(state
, "syn-rcv") == 0)
5068 return (1<<SS_SYN_RECV
);
5069 if (strcasecmp(state
, "established") == 0)
5070 return (1<<SS_ESTABLISHED
);
5071 if (strcasecmp(state
, "all") == 0)
5073 if (strcasecmp(state
, "connected") == 0)
5074 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
));
5075 if (strcasecmp(state
, "synchronized") == 0)
5076 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
)|(1<<SS_SYN_SENT
));
5077 if (strcasecmp(state
, "bucket") == 0)
5078 return (1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
);
5079 if (strcasecmp(state
, "big") == 0)
5080 return SS_ALL
& ~((1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
));
5081 for (i
= 0; i
< SS_MAX
; i
++) {
5082 if (strcasecmp(state
, sstate_namel
[i
]) == 0)
5086 fprintf(stderr
, "ss: wrong state name: %s\n", state
);
5090 /* Values 'v' and 'V' are already used so a non-character is used */
5091 #define OPT_VSOCK 256
5093 /* Values of 't' are already used so a non-character is used */
5094 #define OPT_TIPCSOCK 257
5095 #define OPT_TIPCINFO 258
5099 /* Values of 'x' are already used so a non-character is used */
5100 #define OPT_XDPSOCK 260
5102 static const struct option long_opts
[] = {
5103 { "numeric", 0, 0, 'n' },
5104 { "resolve", 0, 0, 'r' },
5105 { "options", 0, 0, 'o' },
5106 { "extended", 0, 0, 'e' },
5107 { "memory", 0, 0, 'm' },
5108 { "info", 0, 0, 'i' },
5109 { "processes", 0, 0, 'p' },
5110 { "bpf", 0, 0, 'b' },
5111 { "events", 0, 0, 'E' },
5112 { "dccp", 0, 0, 'd' },
5113 { "tcp", 0, 0, 't' },
5114 { "sctp", 0, 0, 'S' },
5115 { "udp", 0, 0, 'u' },
5116 { "raw", 0, 0, 'w' },
5117 { "unix", 0, 0, 'x' },
5118 { "tipc", 0, 0, OPT_TIPCSOCK
},
5119 { "vsock", 0, 0, OPT_VSOCK
},
5120 { "all", 0, 0, 'a' },
5121 { "listening", 0, 0, 'l' },
5122 { "ipv4", 0, 0, '4' },
5123 { "ipv6", 0, 0, '6' },
5124 { "packet", 0, 0, '0' },
5125 { "family", 1, 0, 'f' },
5126 { "socket", 1, 0, 'A' },
5127 { "query", 1, 0, 'A' },
5128 { "summary", 0, 0, 's' },
5129 { "diag", 1, 0, 'D' },
5130 { "filter", 1, 0, 'F' },
5131 { "version", 0, 0, 'V' },
5132 { "help", 0, 0, 'h' },
5133 { "context", 0, 0, 'Z' },
5134 { "contexts", 0, 0, 'z' },
5135 { "net", 1, 0, 'N' },
5136 { "tipcinfo", 0, 0, OPT_TIPCINFO
},
5137 { "tos", 0, 0, OPT_TOS
},
5138 { "kill", 0, 0, 'K' },
5139 { "no-header", 0, 0, 'H' },
5140 { "xdp", 0, 0, OPT_XDPSOCK
},
5141 { "oneline", 0, 0, 'O' },
5146 int main(int argc
, char *argv
[])
5151 const char *dump_tcpdiag
= NULL
;
5152 FILE *filter_fp
= NULL
;
5154 int state_filter
= 0;
5156 while ((ch
= getopt_long(argc
, argv
,
5157 "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KHSO",
5158 long_opts
, NULL
)) != EOF
) {
5181 user_ent_hash_build();
5191 filter_db_set(¤t_filter
, DCCP_DB
, true);
5194 filter_db_set(¤t_filter
, TCP_DB
, true);
5197 filter_db_set(¤t_filter
, SCTP_DB
, true);
5200 filter_db_set(¤t_filter
, UDP_DB
, true);
5203 filter_db_set(¤t_filter
, RAW_DB
, true);
5206 filter_af_set(¤t_filter
, AF_UNIX
);
5209 filter_af_set(¤t_filter
, AF_VSOCK
);
5212 filter_af_set(¤t_filter
, AF_TIPC
);
5215 state_filter
= SS_ALL
;
5218 state_filter
= (1 << SS_LISTEN
) | (1 << SS_CLOSE
);
5221 filter_af_set(¤t_filter
, AF_INET
);
5224 filter_af_set(¤t_filter
, AF_INET6
);
5227 filter_af_set(¤t_filter
, AF_PACKET
);
5230 filter_af_set(¤t_filter
, AF_XDP
);
5233 if (strcmp(optarg
, "inet") == 0)
5234 filter_af_set(¤t_filter
, AF_INET
);
5235 else if (strcmp(optarg
, "inet6") == 0)
5236 filter_af_set(¤t_filter
, AF_INET6
);
5237 else if (strcmp(optarg
, "link") == 0)
5238 filter_af_set(¤t_filter
, AF_PACKET
);
5239 else if (strcmp(optarg
, "unix") == 0)
5240 filter_af_set(¤t_filter
, AF_UNIX
);
5241 else if (strcmp(optarg
, "netlink") == 0)
5242 filter_af_set(¤t_filter
, AF_NETLINK
);
5243 else if (strcmp(optarg
, "tipc") == 0)
5244 filter_af_set(¤t_filter
, AF_TIPC
);
5245 else if (strcmp(optarg
, "vsock") == 0)
5246 filter_af_set(¤t_filter
, AF_VSOCK
);
5247 else if (strcmp(optarg
, "xdp") == 0)
5248 filter_af_set(¤t_filter
, AF_XDP
);
5249 else if (strcmp(optarg
, "help") == 0)
5252 fprintf(stderr
, "ss: \"%s\" is invalid family\n",
5262 current_filter
.dbs
= 0;
5263 state_filter
= state_filter
?
5264 state_filter
: SS_CONN
;
5270 if ((p1
= strchr(p
, ',')) != NULL
)
5272 if (filter_db_parse(¤t_filter
, p
)) {
5273 fprintf(stderr
, "ss: \"%s\" is illegal socket table id\n", p
);
5284 dump_tcpdiag
= optarg
;
5288 fprintf(stderr
, "More than one filter file\n");
5291 if (optarg
[0] == '-')
5294 filter_fp
= fopen(optarg
, "r");
5296 perror("fopen filter file");
5302 printf("ss utility, iproute2-ss%s\n", SNAPSHOT
);
5308 if (is_selinux_enabled() <= 0) {
5309 fprintf(stderr
, "ss: SELinux is not enabled.\n");
5313 user_ent_hash_build();
5316 if (netns_switch(optarg
))
5326 current_filter
.kill
= 1;
5347 if (do_default
&& argc
== 0)
5352 if (strcmp(*argv
, "state") == 0) {
5356 state_filter
|= scan_state(*argv
);
5358 } else if (strcmp(*argv
, "exclude") == 0 ||
5359 strcmp(*argv
, "excl") == 0) {
5362 state_filter
= SS_ALL
;
5363 state_filter
&= ~scan_state(*argv
);
5372 state_filter
= state_filter
? state_filter
: SS_CONN
;
5373 filter_db_parse(¤t_filter
, "all");
5376 filter_states_set(¤t_filter
, state_filter
);
5377 filter_merge_defaults(¤t_filter
);
5379 if (!numeric
&& resolve_hosts
&&
5380 (current_filter
.dbs
& (UNIX_DBM
|INET_L4_DBM
)))
5381 init_service_resolver();
5383 if (current_filter
.dbs
== 0) {
5384 fprintf(stderr
, "ss: no socket tables to show with such filter.\n");
5387 if (current_filter
.families
== 0) {
5388 fprintf(stderr
, "ss: no families to show with such filter.\n");
5391 if (current_filter
.states
== 0) {
5392 fprintf(stderr
, "ss: no socket states to show with such filter.\n");
5397 FILE *dump_fp
= stdout
;
5399 if (!(current_filter
.dbs
& (1<<TCP_DB
))) {
5400 fprintf(stderr
, "ss: tcpdiag dump requested and no tcp in filter.\n");
5403 if (dump_tcpdiag
[0] != '-') {
5404 dump_fp
= fopen(dump_tcpdiag
, "w");
5405 if (!dump_tcpdiag
) {
5406 perror("fopen dump file");
5410 inet_show_netlink(¤t_filter
, dump_fp
, IPPROTO_TCP
);
5415 if (ssfilter_parse(¤t_filter
.f
, argc
, argv
, filter_fp
))
5418 if (!(current_filter
.dbs
& (current_filter
.dbs
- 1)))
5419 columns
[COL_NETID
].disabled
= 1;
5421 if (!(current_filter
.states
& (current_filter
.states
- 1)))
5422 columns
[COL_STATE
].disabled
= 1;
5430 exit(handle_follow_request(¤t_filter
));
5432 if (current_filter
.dbs
& (1<<NETLINK_DB
))
5433 netlink_show(¤t_filter
);
5434 if (current_filter
.dbs
& PACKET_DBM
)
5435 packet_show(¤t_filter
);
5436 if (current_filter
.dbs
& UNIX_DBM
)
5437 unix_show(¤t_filter
);
5438 if (current_filter
.dbs
& (1<<RAW_DB
))
5439 raw_show(¤t_filter
);
5440 if (current_filter
.dbs
& (1<<UDP_DB
))
5441 udp_show(¤t_filter
);
5442 if (current_filter
.dbs
& (1<<TCP_DB
))
5443 tcp_show(¤t_filter
);
5444 if (current_filter
.dbs
& (1<<DCCP_DB
))
5445 dccp_show(¤t_filter
);
5446 if (current_filter
.dbs
& (1<<SCTP_DB
))
5447 sctp_show(¤t_filter
);
5448 if (current_filter
.dbs
& VSOCK_DBM
)
5449 vsock_show(¤t_filter
);
5450 if (current_filter
.dbs
& (1<<TIPC_DB
))
5451 tipc_show(¤t_filter
);
5452 if (current_filter
.dbs
& (1<<XDP_DB
))
5453 xdp_show(¤t_filter
);
5455 if (show_users
|| show_proc_ctx
|| show_sock_ctx
)