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"
39 #include <linux/tcp.h>
40 #include <linux/sock_diag.h>
41 #include <linux/inet_diag.h>
42 #include <linux/unix_diag.h>
43 #include <linux/netdevice.h> /* for MAX_ADDR_LEN */
44 #include <linux/filter.h>
45 #include <linux/xdp_diag.h>
46 #include <linux/packet_diag.h>
47 #include <linux/netlink_diag.h>
48 #include <linux/sctp.h>
49 #include <linux/vm_sockets_diag.h>
50 #include <linux/net.h>
51 #include <linux/tipc.h>
52 #include <linux/tipc_netlink.h>
53 #include <linux/tipc_sockets_diag.h>
55 #define MAGIC_SEQ 123456
56 #define BUF_CHUNK (1024 * 1024)
57 #define LEN_ALIGN(x) (((x) + 1) & ~1)
59 #define DIAG_REQUEST(_req, _r) \
61 struct nlmsghdr nlh; \
65 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
66 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
67 .nlmsg_seq = MAGIC_SEQ, \
68 .nlmsg_len = sizeof(_req), \
73 #include <selinux/selinux.h>
75 /* Stubs for SELinux functions */
76 static int is_selinux_enabled(void)
81 static int getpidcon(pid_t pid
, char **context
)
87 static int getfilecon(char *path
, char **context
)
93 static int security_get_initial_context(char *name
, char **context
)
100 static int resolve_services
= 1;
101 int preferred_family
= AF_UNSPEC
;
102 static int show_options
;
104 static int show_users
;
106 static int show_tcpinfo
;
108 static int show_proc_ctx
;
109 static int show_sock_ctx
;
110 static int show_header
= 1;
111 static int follow_events
;
113 static int show_tipcinfo
;
135 const enum col_align align
;
139 int width
; /* Calculated, including additional layout spacing */
140 int max_len
; /* Measured maximum field length in this column */
143 static struct column columns
[] = {
144 { ALIGN_LEFT
, "Netid", "", 0, 0, 0 },
145 { ALIGN_LEFT
, "State", " ", 0, 0, 0 },
146 { ALIGN_LEFT
, "Recv-Q", " ", 0, 0, 0 },
147 { ALIGN_LEFT
, "Send-Q", " ", 0, 0, 0 },
148 { ALIGN_RIGHT
, "Local Address:", " ", 0, 0, 0 },
149 { ALIGN_LEFT
, "Port", "", 0, 0, 0 },
150 { ALIGN_RIGHT
, "Peer Address:", " ", 0, 0, 0 },
151 { ALIGN_LEFT
, "Port", "", 0, 0, 0 },
152 { ALIGN_LEFT
, "", "", 0, 0, 0 },
155 static struct column
*current_field
= columns
;
157 /* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to
158 * the buffer as a variable size token. A token consists of a 16 bits length
159 * field, followed by a string which is not NULL-terminated.
161 * A new chunk is allocated and linked when the current chunk doesn't have
162 * enough room to store the current token as a whole.
165 struct buf_chunk
*next
; /* Next chained chunk */
166 char *end
; /* Current end of content */
171 uint16_t len
; /* Data length, excluding length descriptor */
176 struct buf_token
*cur
; /* Position of current token in chunk */
177 struct buf_chunk
*head
; /* First chunk */
178 struct buf_chunk
*tail
; /* Current chunk */
181 static const char *TCP_PROTO
= "tcp";
182 static const char *SCTP_PROTO
= "sctp";
183 static const char *UDP_PROTO
= "udp";
184 static const char *RAW_PROTO
= "raw";
185 static const char *dg_proto
;
206 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
207 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
208 #define ALL_DB ((1<<MAX_DB)-1)
209 #define INET_L4_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB))
210 #define INET_DBM (INET_L4_DBM | (1<<RAW_DB))
211 #define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB))
230 SCTP_STATE_CLOSED
= 0,
231 SCTP_STATE_COOKIE_WAIT
= 1,
232 SCTP_STATE_COOKIE_ECHOED
= 2,
233 SCTP_STATE_ESTABLISHED
= 3,
234 SCTP_STATE_SHUTDOWN_PENDING
= 4,
235 SCTP_STATE_SHUTDOWN_SENT
= 5,
236 SCTP_STATE_SHUTDOWN_RECEIVED
= 6,
237 SCTP_STATE_SHUTDOWN_ACK_SENT
= 7,
240 #define SS_ALL ((1 << SS_MAX) - 1)
241 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
242 #define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE))
244 #include "ssfilter.h"
252 struct rtnl_handle
*rth_for_killing
;
255 #define FAMILY_MASK(family) ((uint64_t)1 << (family))
257 static const struct filter default_dbs
[MAX_DB
] = {
260 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
264 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
267 .states
= (1 << SS_ESTABLISHED
),
268 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
271 .states
= (1 << SS_ESTABLISHED
),
272 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
275 .states
= (1 << SS_CLOSE
),
276 .families
= FAMILY_MASK(AF_UNIX
),
280 .families
= FAMILY_MASK(AF_UNIX
),
284 .families
= FAMILY_MASK(AF_UNIX
),
287 .states
= (1 << SS_CLOSE
),
288 .families
= FAMILY_MASK(AF_PACKET
),
291 .states
= (1 << SS_CLOSE
),
292 .families
= FAMILY_MASK(AF_PACKET
),
295 .states
= (1 << SS_CLOSE
),
296 .families
= FAMILY_MASK(AF_NETLINK
),
300 .families
= FAMILY_MASK(AF_INET
) | FAMILY_MASK(AF_INET6
),
304 .families
= FAMILY_MASK(AF_VSOCK
),
308 .families
= FAMILY_MASK(AF_VSOCK
),
311 .states
= TIPC_SS_CONN
,
312 .families
= FAMILY_MASK(AF_TIPC
),
315 .states
= (1 << SS_CLOSE
),
316 .families
= FAMILY_MASK(AF_XDP
),
320 static const struct filter default_afs
[AF_MAX
] = {
335 .states
= (1 << SS_CLOSE
),
338 .dbs
= (1 << NETLINK_DB
),
339 .states
= (1 << SS_CLOSE
),
346 .dbs
= (1 << TIPC_DB
),
347 .states
= TIPC_SS_CONN
,
350 .dbs
= (1 << XDP_DB
),
351 .states
= (1 << SS_CLOSE
),
355 static int do_default
= 1;
356 static struct filter current_filter
;
358 static void filter_db_set(struct filter
*f
, int db
, bool enable
)
361 f
->states
|= default_dbs
[db
].states
;
364 f
->dbs
&= ~(1 << db
);
369 static int filter_db_parse(struct filter
*f
, const char *s
)
375 #define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } }
376 ENTRY(all
, UDP_DB
, DCCP_DB
, TCP_DB
, RAW_DB
,
377 UNIX_ST_DB
, UNIX_DG_DB
, UNIX_SQ_DB
,
378 PACKET_R_DB
, PACKET_DG_DB
, NETLINK_DB
,
379 SCTP_DB
, VSOCK_ST_DB
, VSOCK_DG_DB
, XDP_DB
),
380 ENTRY(inet
, UDP_DB
, DCCP_DB
, TCP_DB
, SCTP_DB
, RAW_DB
),
382 ENTRY(dccp
, DCCP_DB
),
384 ENTRY(sctp
, SCTP_DB
),
386 ENTRY(unix
, UNIX_ST_DB
, UNIX_DG_DB
, UNIX_SQ_DB
),
387 ENTRY(unix_stream
, UNIX_ST_DB
),
388 ENTRY(u_str
, UNIX_ST_DB
), /* alias for unix_stream */
389 ENTRY(unix_dgram
, UNIX_DG_DB
),
390 ENTRY(u_dgr
, UNIX_DG_DB
), /* alias for unix_dgram */
391 ENTRY(unix_seqpacket
, UNIX_SQ_DB
),
392 ENTRY(u_seq
, UNIX_SQ_DB
), /* alias for unix_seqpacket */
393 ENTRY(packet
, PACKET_R_DB
, PACKET_DG_DB
),
394 ENTRY(packet_raw
, PACKET_R_DB
),
395 ENTRY(p_raw
, PACKET_R_DB
), /* alias for packet_raw */
396 ENTRY(packet_dgram
, PACKET_DG_DB
),
397 ENTRY(p_dgr
, PACKET_DG_DB
), /* alias for packet_dgram */
398 ENTRY(netlink
, NETLINK_DB
),
399 ENTRY(vsock
, VSOCK_ST_DB
, VSOCK_DG_DB
),
400 ENTRY(vsock_stream
, VSOCK_ST_DB
),
401 ENTRY(v_str
, VSOCK_ST_DB
), /* alias for vsock_stream */
402 ENTRY(vsock_dgram
, VSOCK_DG_DB
),
403 ENTRY(v_dgr
, VSOCK_DG_DB
), /* alias for vsock_dgram */
415 for (i
= 0; i
< ARRAY_SIZE(db_name_tbl
); i
++) {
416 if (strcmp(s
, db_name_tbl
[i
].name
))
418 for (dbp
= db_name_tbl
[i
].dbs
; *dbp
!= MAX_DB
; dbp
++)
419 filter_db_set(f
, *dbp
, enable
);
425 static void filter_af_set(struct filter
*f
, int af
)
427 f
->states
|= default_afs
[af
].states
;
428 f
->families
|= FAMILY_MASK(af
);
430 preferred_family
= af
;
433 static int filter_af_get(struct filter
*f
, int af
)
435 return !!(f
->families
& FAMILY_MASK(af
));
438 static void filter_states_set(struct filter
*f
, int states
)
444 static void filter_merge_defaults(struct filter
*f
)
449 for (db
= 0; db
< MAX_DB
; db
++) {
450 if (!(f
->dbs
& (1 << db
)))
453 if (!(default_dbs
[db
].families
& f
->families
))
454 f
->families
|= default_dbs
[db
].families
;
456 for (af
= 0; af
< AF_MAX
; af
++) {
457 if (!(f
->families
& FAMILY_MASK(af
)))
460 if (!(default_afs
[af
].dbs
& f
->dbs
))
461 f
->dbs
|= default_afs
[af
].dbs
;
465 static FILE *generic_proc_open(const char *env
, const char *name
)
467 const char *p
= getenv(env
);
471 p
= getenv("PROC_ROOT") ? : "/proc";
472 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
476 return fopen(p
, "r");
478 #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp")
479 #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6")
480 #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp")
481 #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6")
482 #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw")
483 #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6")
484 #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix")
485 #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \
487 #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \
489 #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \
491 #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \
493 #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp")
494 #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \
495 "sys/net/ipv4/ip_local_port_range")
498 struct user_ent
*next
;
507 #define USER_ENT_HASH_SIZE 256
508 static struct user_ent
*user_ent_hash
[USER_ENT_HASH_SIZE
];
510 static int user_ent_hashfn(unsigned int ino
)
512 int val
= (ino
>> 24) ^ (ino
>> 16) ^ (ino
>> 8) ^ ino
;
514 return val
& (USER_ENT_HASH_SIZE
- 1);
517 static void user_ent_add(unsigned int ino
, char *process
,
522 struct user_ent
*p
, **pp
;
524 p
= malloc(sizeof(struct user_ent
));
526 fprintf(stderr
, "ss: failed to malloc buffer\n");
533 p
->process
= strdup(process
);
534 p
->process_ctx
= strdup(proc_ctx
);
535 p
->socket_ctx
= strdup(sock_ctx
);
537 pp
= &user_ent_hash
[user_ent_hashfn(ino
)];
542 static void user_ent_destroy(void)
544 struct user_ent
*p
, *p_next
;
547 while (cnt
!= USER_ENT_HASH_SIZE
) {
548 p
= user_ent_hash
[cnt
];
551 free(p
->process_ctx
);
561 static void user_ent_hash_build(void)
563 const char *root
= getenv("PROC_ROOT") ? : "/proc/";
570 const char *no_ctx
= "unavailable";
571 static int user_ent_hash_build_init
;
573 /* If show_users & show_proc_ctx set only do this once */
574 if (user_ent_hash_build_init
!= 0)
577 user_ent_hash_build_init
= 1;
579 strlcpy(name
, root
, sizeof(name
));
581 if (strlen(name
) == 0 || name
[strlen(name
)-1] != '/')
584 nameoff
= strlen(name
);
590 while ((d
= readdir(dir
)) != NULL
) {
598 if (sscanf(d
->d_name
, "%d%c", &pid
, &crap
) != 1)
601 if (getpidcon(pid
, &pid_context
) != 0)
602 pid_context
= strdup(no_ctx
);
604 snprintf(name
+ nameoff
, sizeof(name
) - nameoff
, "%d/fd/", pid
);
606 if ((dir1
= opendir(name
)) == NULL
) {
614 while ((d1
= readdir(dir1
)) != NULL
) {
615 const char *pattern
= "socket:[";
622 if (sscanf(d1
->d_name
, "%d%c", &fd
, &crap
) != 1)
625 snprintf(name
+pos
, sizeof(name
) - pos
, "%d", fd
);
627 link_len
= readlink(name
, lnk
, sizeof(lnk
)-1);
630 lnk
[link_len
] = '\0';
632 if (strncmp(lnk
, pattern
, strlen(pattern
)))
635 sscanf(lnk
, "socket:[%u]", &ino
);
637 snprintf(tmp
, sizeof(tmp
), "%s/%d/fd/%s",
638 root
, pid
, d1
->d_name
);
640 if (getfilecon(tmp
, &sock_context
) <= 0)
641 sock_context
= strdup(no_ctx
);
646 snprintf(tmp
, sizeof(tmp
), "%s/%d/stat",
648 if ((fp
= fopen(tmp
, "r")) != NULL
) {
649 if (fscanf(fp
, "%*d (%[^)])", p
) < 1)
654 user_ent_add(ino
, p
, pid
, fd
,
655 pid_context
, sock_context
);
670 #define ENTRY_BUF_SIZE 512
671 static int find_entry(unsigned int ino
, char **buf
, int type
)
677 int len
, new_buf_len
;
684 p
= user_ent_hash
[user_ent_hashfn(ino
)];
691 ptr
= *buf
+ buf_used
;
694 len
= snprintf(ptr
, buf_len
- buf_used
,
695 "(\"%s\",pid=%d,fd=%d),",
696 p
->process
, p
->pid
, p
->fd
);
699 len
= snprintf(ptr
, buf_len
- buf_used
,
700 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
702 p
->process_ctx
, p
->fd
);
705 len
= snprintf(ptr
, buf_len
- buf_used
,
706 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
708 p
->process_ctx
, p
->fd
,
712 fprintf(stderr
, "ss: invalid type: %d\n", type
);
716 if (len
< 0 || len
>= buf_len
- buf_used
) {
717 new_buf_len
= buf_len
+ ENTRY_BUF_SIZE
;
718 new_buf
= realloc(*buf
, new_buf_len
);
720 fprintf(stderr
, "ss: failed to malloc buffer\n");
724 buf_len
= new_buf_len
;
736 ptr
= *buf
+ buf_used
;
742 static unsigned long long cookie_sk_get(const uint32_t *cookie
)
744 return (((unsigned long long)cookie
[1] << 31) << 1) | cookie
[0];
747 static const char *sctp_sstate_name
[] = {
748 [SCTP_STATE_CLOSED
] = "CLOSED",
749 [SCTP_STATE_COOKIE_WAIT
] = "COOKIE_WAIT",
750 [SCTP_STATE_COOKIE_ECHOED
] = "COOKIE_ECHOED",
751 [SCTP_STATE_ESTABLISHED
] = "ESTAB",
752 [SCTP_STATE_SHUTDOWN_PENDING
] = "SHUTDOWN_PENDING",
753 [SCTP_STATE_SHUTDOWN_SENT
] = "SHUTDOWN_SENT",
754 [SCTP_STATE_SHUTDOWN_RECEIVED
] = "SHUTDOWN_RECEIVED",
755 [SCTP_STATE_SHUTDOWN_ACK_SENT
] = "ACK_SENT",
758 static const char * const stype_nameg
[] = {
760 [SOCK_STREAM
] = "STREAM",
761 [SOCK_DGRAM
] = "DGRAM",
763 [SOCK_SEQPACKET
] = "SEQPACKET",
767 struct sockstat
*next
;
781 unsigned long long sk
;
788 unsigned int ce_state
;
798 unsigned int timeout
;
801 double rto
, ato
, rtt
, rttvar
;
802 int qack
, ssthresh
, backoff
;
811 unsigned int lastsnd
;
812 unsigned int lastrcv
;
813 unsigned int lastack
;
815 double pacing_rate_max
;
816 double delivery_rate
;
817 unsigned long long bytes_acked
;
818 unsigned long long bytes_received
;
819 unsigned int segs_out
;
820 unsigned int segs_in
;
821 unsigned int data_segs_out
;
822 unsigned int data_segs_in
;
823 unsigned int unacked
;
824 unsigned int retrans
;
825 unsigned int retrans_total
;
828 unsigned int fackets
;
829 unsigned int reordering
;
830 unsigned int not_sent
;
831 unsigned int delivered
;
832 unsigned int delivered_ce
;
833 unsigned int dsack_dups
;
834 unsigned int reord_seen
;
838 unsigned int rcv_ssthresh
;
839 unsigned long long busy_time
;
840 unsigned long long rwnd_limited
;
841 unsigned long long sndbuf_limited
;
842 unsigned long long bytes_sent
;
843 unsigned long long bytes_retrans
;
847 bool has_ecnseen_opt
;
848 bool has_fastopen_opt
;
851 struct dctcpstat
*dctcp
;
852 struct tcp_bbr_info
*bbr_info
;
855 /* SCTP assocs share the same inode number with their parent endpoint. So if we
856 * have seen the inode number before, it must be an assoc instead of the next
858 static bool is_sctp_assoc(struct sockstat
*s
, const char *sock_name
)
860 if (strcmp(sock_name
, "sctp"))
862 if (!sctp_ino
|| sctp_ino
!= s
->ino
)
867 static const char *unix_netid_name(int type
)
880 static const char *proto_name(int protocol
)
900 static const char *vsock_netid_name(int type
)
912 static const char *tipc_netid_name(int type
)
928 /* Allocate and initialize a new buffer chunk */
929 static struct buf_chunk
*buf_chunk_new(void)
931 struct buf_chunk
*new = malloc(BUF_CHUNK
);
938 /* This is also the last block */
941 /* Next token will be stored at the beginning of chunk data area, and
942 * its initial length is zero.
944 buffer
.cur
= (struct buf_token
*)new->data
;
947 new->end
= buffer
.cur
->data
;
952 /* Return available tail room in given chunk */
953 static int buf_chunk_avail(struct buf_chunk
*chunk
)
955 return BUF_CHUNK
- offsetof(struct buf_chunk
, data
) -
956 (chunk
->end
- chunk
->data
);
959 /* Update end pointer and token length, link new chunk if we hit the end of the
960 * current one. Return -EAGAIN if we got a new chunk, caller has to print again.
962 static int buf_update(int len
)
964 struct buf_chunk
*chunk
= buffer
.tail
;
965 struct buf_token
*t
= buffer
.cur
;
967 /* Claim success if new content fits in the current chunk, and anyway
968 * if this is the first token in the chunk: in the latter case,
969 * allocating a new chunk won't help, so we'll just cut the output.
971 if ((len
< buf_chunk_avail(chunk
) && len
!= -1 /* glibc < 2.0.6 */) ||
972 t
== (struct buf_token
*)chunk
->data
) {
973 len
= min(len
, buf_chunk_avail(chunk
));
975 /* Total field length can't exceed 2^16 bytes, cut as needed */
976 len
= min(len
, USHRT_MAX
- t
->len
);
983 /* Content truncated, time to allocate more */
984 chunk
->next
= buf_chunk_new();
986 /* Copy current token over to new chunk, including length descriptor */
987 memcpy(chunk
->next
->data
, t
, sizeof(t
->len
) + t
->len
);
988 chunk
->next
->end
+= t
->len
;
990 /* Discard partially written field in old chunk */
991 chunk
->end
-= t
->len
+ sizeof(t
->len
);
996 /* Append content to buffer as part of the current field */
997 __attribute__((format(printf
, 1, 2)))
998 static void out(const char *fmt
, ...)
1000 struct column
*f
= current_field
;
1009 buffer
.head
= buf_chunk_new();
1011 again
: /* Append to buffer: if we have a new chunk, print again */
1013 pos
= buffer
.cur
->data
+ buffer
.cur
->len
;
1014 va_start(args
, fmt
);
1016 /* Limit to tail room. If we hit the limit, buf_update() will tell us */
1017 len
= vsnprintf(pos
, buf_chunk_avail(buffer
.tail
), fmt
, args
);
1020 if (buf_update(len
))
1024 static int print_left_spacing(struct column
*f
, int stored
, int printed
)
1028 if (!f
->width
|| f
->align
== ALIGN_LEFT
)
1031 s
= f
->width
- stored
- printed
;
1032 if (f
->align
== ALIGN_CENTER
)
1033 /* If count of total spacing is odd, shift right by one */
1037 return printf("%*c", s
, ' ');
1042 static void print_right_spacing(struct column
*f
, int printed
)
1046 if (!f
->width
|| f
->align
== ALIGN_RIGHT
)
1049 s
= f
->width
- printed
;
1050 if (f
->align
== ALIGN_CENTER
)
1054 printf("%*c", s
, ' ');
1057 /* Done with field: update buffer pointer, start new token after current one */
1058 static void field_flush(struct column
*f
)
1060 struct buf_chunk
*chunk
;
1066 chunk
= buffer
.tail
;
1067 pad
= buffer
.cur
->len
% 2;
1069 if (buffer
.cur
->len
> f
->max_len
)
1070 f
->max_len
= buffer
.cur
->len
;
1072 /* We need a new chunk if we can't store the next length descriptor.
1073 * Mind the gap between end of previous token and next aligned position
1074 * for length descriptor.
1076 if (buf_chunk_avail(chunk
) - pad
< sizeof(buffer
.cur
->len
)) {
1078 chunk
->next
= buf_chunk_new();
1082 buffer
.cur
= (struct buf_token
*)(buffer
.cur
->data
+
1083 LEN_ALIGN(buffer
.cur
->len
));
1084 buffer
.cur
->len
= 0;
1085 buffer
.tail
->end
= buffer
.cur
->data
;
1088 static int field_is_last(struct column
*f
)
1090 return f
- columns
== COL_MAX
- 1;
1093 static void field_next(void)
1095 field_flush(current_field
);
1097 if (field_is_last(current_field
))
1098 current_field
= columns
;
1103 /* Walk through fields and flush them until we reach the desired one */
1104 static void field_set(enum col_id id
)
1106 while (id
!= current_field
- columns
)
1110 /* Print header for all non-empty columns */
1111 static void print_header(void)
1113 while (!field_is_last(current_field
)) {
1114 if (!current_field
->disabled
)
1115 out("%s", current_field
->header
);
1120 /* Get the next available token in the buffer starting from the current token */
1121 static struct buf_token
*buf_token_next(struct buf_token
*cur
)
1123 struct buf_chunk
*chunk
= buffer
.tail
;
1125 /* If we reached the end of chunk contents, get token from next chunk */
1126 if (cur
->data
+ LEN_ALIGN(cur
->len
) == chunk
->end
) {
1127 buffer
.tail
= chunk
= chunk
->next
;
1128 return chunk
? (struct buf_token
*)chunk
->data
: NULL
;
1131 return (struct buf_token
*)(cur
->data
+ LEN_ALIGN(cur
->len
));
1134 /* Free up all allocated buffer chunks */
1135 static void buf_free_all(void)
1137 struct buf_chunk
*tmp
;
1139 for (buffer
.tail
= buffer
.head
; buffer
.tail
; ) {
1141 buffer
.tail
= buffer
.tail
->next
;
1147 /* Get current screen width, default to 80 columns if TIOCGWINSZ fails */
1148 static int render_screen_width(void)
1152 if (isatty(STDOUT_FILENO
)) {
1155 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
) != -1) {
1164 /* Calculate column width from contents length. If columns don't fit on one
1165 * line, break them into the least possible amount of lines and keep them
1166 * aligned across lines. Available screen space is equally spread between fields
1167 * as additional spacing.
1169 static void render_calc_width(void)
1171 int screen_width
= render_screen_width();
1172 struct column
*c
, *eol
= columns
- 1;
1173 int first
, len
= 0, linecols
= 0;
1175 /* First pass: set width for each column to measured content length */
1176 for (first
= 1, c
= columns
; c
- columns
< COL_MAX
; c
++) {
1180 if (!first
&& c
->max_len
)
1181 c
->width
= c
->max_len
+ strlen(c
->ldelim
);
1183 c
->width
= c
->max_len
;
1185 /* But don't exceed screen size. If we exceed the screen size
1186 * for even a single field, it will just start on a line of its
1187 * own and then naturally wrap.
1189 c
->width
= min(c
->width
, screen_width
);
1195 /* Second pass: find out newlines and distribute available spacing */
1196 for (c
= columns
; c
- columns
< COL_MAX
; c
++) {
1197 int pad
, spacing
, rem
, last
;
1206 for (last
= 1, tmp
= c
+ 1; tmp
- columns
< COL_MAX
; tmp
++) {
1213 if (!last
&& len
< screen_width
) {
1214 /* Columns fit on screen so far, nothing to do yet */
1218 if (len
== screen_width
) {
1219 /* Exact fit, just start with new line */
1223 if (len
> screen_width
) {
1224 /* Screen width exceeded: go back one column */
1230 /* Distribute remaining space to columns on this line */
1231 pad
= screen_width
- len
;
1232 spacing
= pad
/ linecols
;
1233 rem
= pad
% linecols
;
1234 for (tmp
= c
; tmp
> eol
; tmp
--) {
1238 tmp
->width
+= spacing
;
1246 /* Line break: reset line counters, mark end-of-line */
1253 /* Render buffered output with spacing and delimiters, then free up buffers */
1254 static void render(void)
1256 struct buf_token
*token
;
1257 int printed
, line_started
= 0;
1263 token
= (struct buf_token
*)buffer
.head
->data
;
1265 /* Ensure end alignment of last token, it wasn't necessarily flushed */
1266 buffer
.tail
->end
+= buffer
.cur
->len
% 2;
1268 render_calc_width();
1270 /* Rewind and replay */
1271 buffer
.tail
= buffer
.head
;
1278 /* Print left delimiter only if we already started a line */
1280 printed
= printf("%s", f
->ldelim
);
1284 /* Print field content from token data with spacing */
1285 printed
+= print_left_spacing(f
, token
->len
, printed
);
1286 printed
+= fwrite(token
->data
, 1, token
->len
, stdout
);
1287 print_right_spacing(f
, printed
);
1289 /* Go to next non-empty field, deal with end-of-line */
1291 if (field_is_last(f
)) {
1298 } while (f
->disabled
);
1300 token
= buf_token_next(token
);
1304 current_field
= columns
;
1307 static void sock_state_print(struct sockstat
*s
)
1309 const char *sock_name
;
1310 static const char * const sstate_name
[] = {
1312 [SS_ESTABLISHED
] = "ESTAB",
1313 [SS_SYN_SENT
] = "SYN-SENT",
1314 [SS_SYN_RECV
] = "SYN-RECV",
1315 [SS_FIN_WAIT1
] = "FIN-WAIT-1",
1316 [SS_FIN_WAIT2
] = "FIN-WAIT-2",
1317 [SS_TIME_WAIT
] = "TIME-WAIT",
1318 [SS_CLOSE
] = "UNCONN",
1319 [SS_CLOSE_WAIT
] = "CLOSE-WAIT",
1320 [SS_LAST_ACK
] = "LAST-ACK",
1321 [SS_LISTEN
] = "LISTEN",
1322 [SS_CLOSING
] = "CLOSING",
1325 switch (s
->local
.family
) {
1327 sock_name
= unix_netid_name(s
->type
);
1331 sock_name
= proto_name(s
->type
);
1334 sock_name
= s
->type
== SOCK_RAW
? "p_raw" : "p_dgr";
1340 sock_name
= tipc_netid_name(s
->type
);
1343 sock_name
= vsock_netid_name(s
->type
);
1349 sock_name
= "unknown";
1352 if (is_sctp_assoc(s
, sock_name
)) {
1353 field_set(COL_STATE
); /* Empty Netid field */
1354 out("`- %s", sctp_sstate_name
[s
->state
]);
1356 field_set(COL_NETID
);
1357 out("%s", sock_name
);
1358 field_set(COL_STATE
);
1359 out("%s", sstate_name
[s
->state
]);
1362 field_set(COL_RECVQ
);
1364 field_set(COL_SENDQ
);
1366 field_set(COL_ADDR
);
1369 static void sock_details_print(struct sockstat
*s
)
1372 out(" uid:%u", s
->uid
);
1374 out(" ino:%u", s
->ino
);
1375 out(" sk:%llx", s
->sk
);
1378 out(" fwmark:0x%x", s
->mark
);
1381 static void sock_addr_print(const char *addr
, char *delim
, const char *port
,
1385 out("%s" "%%" "%s%s", addr
, ifname
, delim
);
1387 out("%s%s", addr
, delim
);
1394 static const char *print_ms_timer(unsigned int timeout
)
1396 static char buf
[64];
1397 int secs
, msecs
, minutes
;
1399 secs
= timeout
/1000;
1402 msecs
= timeout
%1000;
1406 snprintf(buf
, sizeof(buf
)-16, "%dmin", minutes
);
1413 sprintf(buf
+strlen(buf
), "%d%s", secs
, msecs
? "." : "sec");
1416 sprintf(buf
+strlen(buf
), "%03dms", msecs
);
1421 struct scache
*next
;
1427 static struct scache
*rlist
;
1429 static void init_service_resolver(void)
1432 FILE *fp
= popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
1437 if (!fgets(buf
, sizeof(buf
), fp
)) {
1441 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1442 unsigned int progn
, port
;
1443 char proto
[128], prog
[128] = "rpc.";
1446 if (sscanf(buf
, "%u %*d %s %u %s",
1447 &progn
, proto
, &port
, prog
+4) != 4)
1450 if (!(c
= malloc(sizeof(*c
))))
1454 c
->name
= strdup(prog
);
1455 if (strcmp(proto
, TCP_PROTO
) == 0)
1456 c
->proto
= TCP_PROTO
;
1457 else if (strcmp(proto
, UDP_PROTO
) == 0)
1458 c
->proto
= UDP_PROTO
;
1459 else if (strcmp(proto
, SCTP_PROTO
) == 0)
1460 c
->proto
= SCTP_PROTO
;
1469 /* Even do not try default linux ephemeral port ranges:
1470 * default /etc/services contains so much of useless crap
1471 * wouldbe "allocated" to this area that resolution
1472 * is really harmful. I shrug each time when seeing
1473 * "socks" or "cfinger" in dumps.
1475 static int is_ephemeral(int port
)
1477 static int min
= 0, max
;
1480 FILE *f
= ephemeral_ports_open();
1482 if (!f
|| fscanf(f
, "%d %d", &min
, &max
) < 2) {
1489 return port
>= min
&& port
<= max
;
1493 static const char *__resolve_service(int port
)
1497 for (c
= rlist
; c
; c
= c
->next
) {
1498 if (c
->port
== port
&& c
->proto
== dg_proto
)
1502 if (!is_ephemeral(port
)) {
1503 static int notfirst
;
1510 se
= getservbyport(htons(port
), dg_proto
);
1518 #define SCACHE_BUCKETS 1024
1519 static struct scache
*cache_htab
[SCACHE_BUCKETS
];
1521 static const char *resolve_service(int port
)
1523 static char buf
[128];
1534 if (!resolve_services
)
1537 if (dg_proto
== RAW_PROTO
)
1538 return inet_proto_n2a(port
, buf
, sizeof(buf
));
1541 hash
= (port
^(((unsigned long)dg_proto
)>>2)) % SCACHE_BUCKETS
;
1543 for (c
= cache_htab
[hash
]; c
; c
= c
->next
) {
1544 if (c
->port
== port
&& c
->proto
== dg_proto
)
1548 c
= malloc(sizeof(*c
));
1551 res
= __resolve_service(port
);
1553 c
->name
= res
? strdup(res
) : NULL
;
1554 c
->proto
= dg_proto
;
1555 c
->next
= cache_htab
[hash
];
1556 cache_htab
[hash
] = c
;
1563 sprintf(buf
, "%u", port
);
1567 static void inet_addr_print(const inet_prefix
*a
, int port
,
1568 unsigned int ifindex
, bool v6only
)
1571 const char *ap
= buf
;
1572 const char *ifname
= NULL
;
1574 if (a
->family
== AF_INET
) {
1575 ap
= format_host(AF_INET
, 4, a
->data
);
1578 !memcmp(a
->data
, &in6addr_any
, sizeof(in6addr_any
))) {
1582 ap
= format_host(a
->family
, 16, a
->data
);
1584 /* Numeric IPv6 addresses should be bracketed */
1585 if (strchr(ap
, ':')) {
1586 snprintf(buf
, sizeof(buf
),
1594 ifname
= ll_index_to_name(ifindex
);
1596 sock_addr_print(ap
, ":", resolve_service(port
), ifname
);
1605 struct aafilter
*next
;
1608 static int inet2_addr_match(const inet_prefix
*a
, const inet_prefix
*p
,
1611 if (!inet_addr_match(a
, p
, plen
))
1614 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1615 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1617 if (p
->family
== AF_INET
&& a
->family
== AF_INET6
) {
1618 if (a
->data
[0] == 0 && a
->data
[1] == 0 &&
1619 a
->data
[2] == htonl(0xffff)) {
1620 inet_prefix tmp
= *a
;
1622 tmp
.data
[0] = a
->data
[3];
1623 return inet_addr_match(&tmp
, p
, plen
);
1629 static int unix_match(const inet_prefix
*a
, const inet_prefix
*p
)
1631 char *addr
, *pattern
;
1633 memcpy(&addr
, a
->data
, sizeof(addr
));
1634 memcpy(&pattern
, p
->data
, sizeof(pattern
));
1635 if (pattern
== NULL
)
1639 return !fnmatch(pattern
, addr
, 0);
1642 static int run_ssfilter(struct ssfilter
*f
, struct sockstat
*s
)
1647 if (s
->local
.family
== AF_UNIX
) {
1650 memcpy(&p
, s
->local
.data
, sizeof(p
));
1651 return p
== NULL
|| (p
[0] == '@' && strlen(p
) == 6 &&
1652 strspn(p
+1, "0123456789abcdef") == 5);
1654 if (s
->local
.family
== AF_PACKET
)
1655 return s
->lport
== 0 && s
->local
.data
[0] == 0;
1656 if (s
->local
.family
== AF_NETLINK
)
1657 return s
->lport
< 0;
1658 if (s
->local
.family
== AF_VSOCK
)
1659 return s
->lport
> 1023;
1661 return is_ephemeral(s
->lport
);
1665 struct aafilter
*a
= (void *)f
->pred
;
1667 if (a
->addr
.family
== AF_UNIX
)
1668 return unix_match(&s
->remote
, &a
->addr
);
1669 if (a
->port
!= -1 && a
->port
!= s
->rport
)
1671 if (a
->addr
.bitlen
) {
1673 if (!inet2_addr_match(&s
->remote
, &a
->addr
, a
->addr
.bitlen
))
1675 } while ((a
= a
->next
) != NULL
);
1682 struct aafilter
*a
= (void *)f
->pred
;
1684 if (a
->addr
.family
== AF_UNIX
)
1685 return unix_match(&s
->local
, &a
->addr
);
1686 if (a
->port
!= -1 && a
->port
!= s
->lport
)
1688 if (a
->addr
.bitlen
) {
1690 if (!inet2_addr_match(&s
->local
, &a
->addr
, a
->addr
.bitlen
))
1692 } while ((a
= a
->next
) != NULL
);
1699 struct aafilter
*a
= (void *)f
->pred
;
1701 return s
->rport
>= a
->port
;
1705 struct aafilter
*a
= (void *)f
->pred
;
1707 return s
->rport
<= a
->port
;
1711 struct aafilter
*a
= (void *)f
->pred
;
1713 return s
->lport
>= a
->port
;
1717 struct aafilter
*a
= (void *)f
->pred
;
1719 return s
->lport
<= a
->port
;
1723 struct aafilter
*a
= (void *)f
->pred
;
1725 return s
->iface
== a
->iface
;
1729 struct aafilter
*a
= (void *)f
->pred
;
1731 return (s
->mark
& a
->mask
) == a
->mark
;
1733 /* Yup. It is recursion. Sorry. */
1735 return run_ssfilter(f
->pred
, s
) && run_ssfilter(f
->post
, s
);
1737 return run_ssfilter(f
->pred
, s
) || run_ssfilter(f
->post
, s
);
1739 return !run_ssfilter(f
->pred
, s
);
1745 /* Relocate external jumps by reloc. */
1746 static void ssfilter_patch(char *a
, int len
, int reloc
)
1749 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)a
;
1751 if (op
->no
== len
+4)
1760 static int ssfilter_bytecompile(struct ssfilter
*f
, char **bytecode
)
1765 if (!(*bytecode
= malloc(4))) abort();
1766 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_AUTO
, 4, 8 };
1772 struct aafilter
*a
= (void *)f
->pred
;
1775 int code
= (f
->type
== SSF_DCOND
? INET_DIAG_BC_D_COND
: INET_DIAG_BC_S_COND
);
1778 for (b
= a
; b
; b
= b
->next
) {
1779 len
+= 4 + sizeof(struct inet_diag_hostcond
);
1780 if (a
->addr
.family
== AF_INET6
)
1787 if (!(ptr
= malloc(len
))) abort();
1789 for (b
= a
; b
; b
= b
->next
) {
1790 struct inet_diag_bc_op
*op
= (struct inet_diag_bc_op
*)ptr
;
1791 int alen
= (a
->addr
.family
== AF_INET6
? 16 : 4);
1792 int oplen
= alen
+ 4 + sizeof(struct inet_diag_hostcond
);
1793 struct inet_diag_hostcond
*cond
= (struct inet_diag_hostcond
*)(ptr
+4);
1795 *op
= (struct inet_diag_bc_op
){ code
, oplen
, oplen
+4 };
1796 cond
->family
= a
->addr
.family
;
1797 cond
->port
= a
->port
;
1798 cond
->prefix_len
= a
->addr
.bitlen
;
1799 memcpy(cond
->addr
, a
->addr
.data
, alen
);
1802 op
= (struct inet_diag_bc_op
*)ptr
;
1803 *op
= (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, len
- (ptr
-*bytecode
)};
1807 return ptr
- *bytecode
;
1811 struct aafilter
*x
= (void *)f
->pred
;
1813 if (!(*bytecode
= malloc(8))) abort();
1814 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_GE
, 8, 12 };
1815 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1820 struct aafilter
*x
= (void *)f
->pred
;
1822 if (!(*bytecode
= malloc(8))) abort();
1823 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_D_LE
, 8, 12 };
1824 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1829 struct aafilter
*x
= (void *)f
->pred
;
1831 if (!(*bytecode
= malloc(8))) abort();
1832 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_GE
, 8, 12 };
1833 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1838 struct aafilter
*x
= (void *)f
->pred
;
1840 if (!(*bytecode
= malloc(8))) abort();
1841 ((struct inet_diag_bc_op
*)*bytecode
)[0] = (struct inet_diag_bc_op
){ INET_DIAG_BC_S_LE
, 8, 12 };
1842 ((struct inet_diag_bc_op
*)*bytecode
)[1] = (struct inet_diag_bc_op
){ 0, 0, x
->port
};
1848 char *a1
= NULL
, *a2
= NULL
, *a
;
1851 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1852 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1858 if (!(a
= malloc(l1
+l2
))) abort();
1860 memcpy(a
+l1
, a2
, l2
);
1862 ssfilter_patch(a
, l1
, l2
);
1868 char *a1
= NULL
, *a2
= NULL
, *a
;
1871 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1872 l2
= ssfilter_bytecompile(f
->post
, &a2
);
1878 if (!(a
= malloc(l1
+l2
+4))) abort();
1880 memcpy(a
+l1
+4, a2
, l2
);
1882 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, l2
+4 };
1888 char *a1
= NULL
, *a
;
1891 l1
= ssfilter_bytecompile(f
->pred
, &a1
);
1896 if (!(a
= malloc(l1
+4))) abort();
1899 *(struct inet_diag_bc_op
*)(a
+l1
) = (struct inet_diag_bc_op
){ INET_DIAG_BC_JMP
, 4, 8 };
1905 /* bytecompile for SSF_DEVCOND not supported yet */
1910 struct aafilter
*a
= (void *)f
->pred
;
1912 struct inet_diag_bc_op op
;
1913 struct inet_diag_markcond cond
;
1915 int inslen
= sizeof(struct instr
);
1917 if (!(*bytecode
= malloc(inslen
))) abort();
1918 ((struct instr
*)*bytecode
)[0] = (struct instr
) {
1919 { INET_DIAG_BC_MARK_COND
, inslen
, inslen
+ 4 },
1920 { a
->mark
, a
->mask
},
1930 static int remember_he(struct aafilter
*a
, struct hostent
*he
)
1932 char **ptr
= he
->h_addr_list
;
1936 if (he
->h_addrtype
== AF_INET
)
1938 else if (he
->h_addrtype
== AF_INET6
)
1944 struct aafilter
*b
= a
;
1946 if (a
->addr
.bitlen
) {
1947 if ((b
= malloc(sizeof(*b
))) == NULL
)
1952 memcpy(b
->addr
.data
, *ptr
, len
);
1953 b
->addr
.bytelen
= len
;
1954 b
->addr
.bitlen
= len
*8;
1955 b
->addr
.family
= he
->h_addrtype
;
1962 static int get_dns_host(struct aafilter
*a
, const char *addr
, int fam
)
1964 static int notfirst
;
1973 he
= gethostbyname2(addr
, fam
== AF_UNSPEC
? AF_INET
: fam
);
1975 cnt
= remember_he(a
, he
);
1976 if (fam
== AF_UNSPEC
) {
1977 he
= gethostbyname2(addr
, AF_INET6
);
1979 cnt
+= remember_he(a
, he
);
1984 static int xll_initted
;
1986 static void xll_init(void)
1988 struct rtnl_handle rth
;
1990 if (rtnl_open(&rth
, 0) < 0)
1998 static const char *xll_index_to_name(int index
)
2002 return ll_index_to_name(index
);
2005 static int xll_name_to_index(const char *dev
)
2009 return ll_name_to_index(dev
);
2012 void *parse_devcond(char *name
)
2014 struct aafilter a
= { .iface
= 0 };
2015 struct aafilter
*res
;
2017 a
.iface
= xll_name_to_index(name
);
2022 n
= strtoul(name
, &end
, 0);
2023 if (!end
|| end
== name
|| *end
|| n
> UINT_MAX
)
2029 res
= malloc(sizeof(*res
));
2035 static void vsock_set_inet_prefix(inet_prefix
*a
, __u32 cid
)
2038 .bytelen
= sizeof(cid
),
2041 memcpy(a
->data
, &cid
, sizeof(cid
));
2044 void *parse_hostcond(char *addr
, bool is_port
)
2047 struct aafilter a
= { .port
= -1 };
2048 struct aafilter
*res
;
2049 int fam
= preferred_family
;
2050 struct filter
*f
= ¤t_filter
;
2052 if (fam
== AF_UNIX
|| strncmp(addr
, "unix:", 5) == 0) {
2055 a
.addr
.family
= AF_UNIX
;
2056 if (strncmp(addr
, "unix:", 5) == 0)
2059 a
.addr
.bitlen
= 8*strlen(p
);
2060 memcpy(a
.addr
.data
, &p
, sizeof(p
));
2065 if (fam
== AF_PACKET
|| strncmp(addr
, "link:", 5) == 0) {
2066 a
.addr
.family
= AF_PACKET
;
2068 if (strncmp(addr
, "link:", 5) == 0)
2070 port
= strchr(addr
, ':');
2073 if (port
[1] && strcmp(port
+1, "*")) {
2074 if (get_integer(&a
.port
, port
+1, 0)) {
2075 if ((a
.port
= xll_name_to_index(port
+1)) <= 0)
2080 if (addr
[0] && strcmp(addr
, "*")) {
2084 if (ll_proto_a2n(&tmp
, addr
))
2086 a
.addr
.data
[0] = ntohs(tmp
);
2092 if (fam
== AF_NETLINK
|| strncmp(addr
, "netlink:", 8) == 0) {
2093 a
.addr
.family
= AF_NETLINK
;
2095 if (strncmp(addr
, "netlink:", 8) == 0)
2097 port
= strchr(addr
, ':');
2100 if (port
[1] && strcmp(port
+1, "*")) {
2101 if (get_integer(&a
.port
, port
+1, 0)) {
2102 if (strcmp(port
+1, "kernel") == 0)
2109 if (addr
[0] && strcmp(addr
, "*")) {
2111 if (nl_proto_a2n(&a
.addr
.data
[0], addr
) == -1)
2118 if (fam
== AF_VSOCK
|| strncmp(addr
, "vsock:", 6) == 0) {
2119 __u32 cid
= ~(__u32
)0;
2121 a
.addr
.family
= AF_VSOCK
;
2122 if (strncmp(addr
, "vsock:", 6) == 0)
2128 port
= strchr(addr
, ':');
2135 if (port
&& strcmp(port
, "*") &&
2136 get_u32((__u32
*)&a
.port
, port
, 0))
2139 if (addr
[0] && strcmp(addr
, "*")) {
2141 if (get_u32(&cid
, addr
, 0))
2144 vsock_set_inet_prefix(&a
.addr
, cid
);
2149 if (fam
== AF_INET
|| !strncmp(addr
, "inet:", 5)) {
2151 if (!strncmp(addr
, "inet:", 5))
2153 } else if (fam
== AF_INET6
|| !strncmp(addr
, "inet6:", 6)) {
2155 if (!strncmp(addr
, "inet6:", 6))
2159 /* URL-like literal [] */
2160 if (addr
[0] == '[') {
2162 if ((port
= strchr(addr
, ']')) == NULL
)
2165 } else if (addr
[0] == '*') {
2168 port
= strrchr(strchr(addr
, '/') ? : addr
, ':');
2174 if (port
&& *port
) {
2178 if (*port
&& *port
!= '*') {
2179 if (get_integer(&a
.port
, port
, 0)) {
2180 struct servent
*se1
= NULL
;
2181 struct servent
*se2
= NULL
;
2183 if (current_filter
.dbs
&(1<<UDP_DB
))
2184 se1
= getservbyname(port
, UDP_PROTO
);
2185 if (current_filter
.dbs
&(1<<TCP_DB
))
2186 se2
= getservbyname(port
, TCP_PROTO
);
2187 if (se1
&& se2
&& se1
->s_port
!= se2
->s_port
) {
2188 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
2194 a
.port
= ntohs(se1
->s_port
);
2198 for (s
= rlist
; s
; s
= s
->next
) {
2199 if ((s
->proto
== UDP_PROTO
&&
2200 (current_filter
.dbs
&(1<<UDP_DB
))) ||
2201 (s
->proto
== TCP_PROTO
&&
2202 (current_filter
.dbs
&(1<<TCP_DB
)))) {
2203 if (s
->name
&& strcmp(s
->name
, port
) == 0) {
2204 if (a
.port
> 0 && a
.port
!= s
->port
) {
2205 fprintf(stderr
, "Error: ambiguous port \"%s\".\n", port
);
2213 fprintf(stderr
, "Error: \"%s\" does not look like a port.\n", port
);
2220 if (!is_port
&& *addr
&& *addr
!= '*') {
2221 if (get_prefix_1(&a
.addr
, addr
, fam
)) {
2222 if (get_dns_host(&a
, addr
, fam
)) {
2223 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", addr
);
2230 if (fam
!= AF_UNSPEC
) {
2231 int states
= f
->states
;
2233 filter_af_set(f
, fam
);
2234 filter_states_set(f
, states
);
2237 res
= malloc(sizeof(*res
));
2239 memcpy(res
, &a
, sizeof(a
));
2243 void *parse_markmask(const char *markmask
)
2245 struct aafilter a
, *res
;
2247 if (strchr(markmask
, '/')) {
2248 if (sscanf(markmask
, "%i/%i", &a
.mark
, &a
.mask
) != 2)
2251 a
.mask
= 0xffffffff;
2252 if (sscanf(markmask
, "%i", &a
.mark
) != 1)
2256 res
= malloc(sizeof(*res
));
2258 memcpy(res
, &a
, sizeof(a
));
2262 static void proc_ctx_print(struct sockstat
*s
)
2266 if (show_proc_ctx
|| show_sock_ctx
) {
2267 if (find_entry(s
->ino
, &buf
,
2268 (show_proc_ctx
& show_sock_ctx
) ?
2269 PROC_SOCK_CTX
: PROC_CTX
) > 0) {
2270 out(" users:(%s)", buf
);
2273 } else if (show_users
) {
2274 if (find_entry(s
->ino
, &buf
, USERS
) > 0) {
2275 out(" users:(%s)", buf
);
2281 static void inet_stats_print(struct sockstat
*s
, bool v6only
)
2283 sock_state_print(s
);
2285 inet_addr_print(&s
->local
, s
->lport
, s
->iface
, v6only
);
2286 inet_addr_print(&s
->remote
, s
->rport
, 0, v6only
);
2291 static int proc_parse_inet_addr(char *loc
, char *rem
, int family
, struct
2294 s
->local
.family
= s
->remote
.family
= family
;
2295 if (family
== AF_INET
) {
2296 sscanf(loc
, "%x:%x", s
->local
.data
, (unsigned *)&s
->lport
);
2297 sscanf(rem
, "%x:%x", s
->remote
.data
, (unsigned *)&s
->rport
);
2298 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
2301 sscanf(loc
, "%08x%08x%08x%08x:%x",
2307 sscanf(rem
, "%08x%08x%08x%08x:%x",
2313 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
2319 static int proc_inet_split_line(char *line
, char **loc
, char **rem
, char **data
)
2323 if ((p
= strchr(line
, ':')) == NULL
)
2327 if ((p
= strchr(*loc
, ':')) == NULL
)
2332 if ((p
= strchr(*rem
, ':')) == NULL
)
2340 static char *sprint_bw(char *buf
, double bw
)
2343 sprintf(buf
, "%.1fM", bw
/ 1000000.);
2344 else if (bw
> 1000.)
2345 sprintf(buf
, "%.1fK", bw
/ 1000.);
2347 sprintf(buf
, "%g", bw
);
2352 static void sctp_stats_print(struct sctp_info
*s
)
2355 out(" tag:%x", s
->sctpi_tag
);
2357 out(" state:%s", sctp_sstate_name
[s
->sctpi_state
]);
2359 out(" rwnd:%d", s
->sctpi_rwnd
);
2360 if (s
->sctpi_unackdata
)
2361 out(" unackdata:%d", s
->sctpi_unackdata
);
2362 if (s
->sctpi_penddata
)
2363 out(" penddata:%d", s
->sctpi_penddata
);
2364 if (s
->sctpi_instrms
)
2365 out(" instrms:%d", s
->sctpi_instrms
);
2366 if (s
->sctpi_outstrms
)
2367 out(" outstrms:%d", s
->sctpi_outstrms
);
2368 if (s
->sctpi_inqueue
)
2369 out(" inqueue:%d", s
->sctpi_inqueue
);
2370 if (s
->sctpi_outqueue
)
2371 out(" outqueue:%d", s
->sctpi_outqueue
);
2372 if (s
->sctpi_overall_error
)
2373 out(" overerr:%d", s
->sctpi_overall_error
);
2374 if (s
->sctpi_max_burst
)
2375 out(" maxburst:%d", s
->sctpi_max_burst
);
2376 if (s
->sctpi_maxseg
)
2377 out(" maxseg:%d", s
->sctpi_maxseg
);
2378 if (s
->sctpi_peer_rwnd
)
2379 out(" prwnd:%d", s
->sctpi_peer_rwnd
);
2380 if (s
->sctpi_peer_tag
)
2381 out(" ptag:%x", s
->sctpi_peer_tag
);
2382 if (s
->sctpi_peer_capable
)
2383 out(" pcapable:%d", s
->sctpi_peer_capable
);
2384 if (s
->sctpi_peer_sack
)
2385 out(" psack:%d", s
->sctpi_peer_sack
);
2386 if (s
->sctpi_s_autoclose
)
2387 out(" autoclose:%d", s
->sctpi_s_autoclose
);
2388 if (s
->sctpi_s_adaptation_ind
)
2389 out(" adapind:%d", s
->sctpi_s_adaptation_ind
);
2390 if (s
->sctpi_s_pd_point
)
2391 out(" pdpoint:%d", s
->sctpi_s_pd_point
);
2392 if (s
->sctpi_s_nodelay
)
2393 out(" nodealy:%d", s
->sctpi_s_nodelay
);
2394 if (s
->sctpi_s_disable_fragments
)
2395 out(" nofrag:%d", s
->sctpi_s_disable_fragments
);
2396 if (s
->sctpi_s_v4mapped
)
2397 out(" v4mapped:%d", s
->sctpi_s_v4mapped
);
2398 if (s
->sctpi_s_frag_interleave
)
2399 out(" fraginl:%d", s
->sctpi_s_frag_interleave
);
2402 static void tcp_stats_print(struct tcpstat
*s
)
2408 if (s
->has_sack_opt
)
2412 if (s
->has_ecnseen_opt
)
2414 if (s
->has_fastopen_opt
)
2417 out(" %s", s
->cong_alg
);
2418 if (s
->has_wscale_opt
)
2419 out(" wscale:%d,%d", s
->snd_wscale
, s
->rcv_wscale
);
2421 out(" rto:%g", s
->rto
);
2423 out(" backoff:%u", s
->backoff
);
2425 out(" rtt:%g/%g", s
->rtt
, s
->rttvar
);
2427 out(" ato:%g", s
->ato
);
2430 out(" qack:%d", s
->qack
);
2435 out(" mss:%d", s
->mss
);
2437 out(" pmtu:%u", s
->pmtu
);
2439 out(" rcvmss:%d", s
->rcv_mss
);
2441 out(" advmss:%d", s
->advmss
);
2443 out(" cwnd:%u", s
->cwnd
);
2445 out(" ssthresh:%d", s
->ssthresh
);
2448 out(" bytes_sent:%llu", s
->bytes_sent
);
2449 if (s
->bytes_retrans
)
2450 out(" bytes_retrans:%llu", s
->bytes_retrans
);
2452 out(" bytes_acked:%llu", s
->bytes_acked
);
2453 if (s
->bytes_received
)
2454 out(" bytes_received:%llu", s
->bytes_received
);
2456 out(" segs_out:%u", s
->segs_out
);
2458 out(" segs_in:%u", s
->segs_in
);
2459 if (s
->data_segs_out
)
2460 out(" data_segs_out:%u", s
->data_segs_out
);
2461 if (s
->data_segs_in
)
2462 out(" data_segs_in:%u", s
->data_segs_in
);
2464 if (s
->dctcp
&& s
->dctcp
->enabled
) {
2465 struct dctcpstat
*dctcp
= s
->dctcp
;
2467 out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
2468 dctcp
->ce_state
, dctcp
->alpha
, dctcp
->ab_ecn
,
2470 } else if (s
->dctcp
) {
2471 out(" dctcp:fallback_mode");
2477 bw
= s
->bbr_info
->bbr_bw_hi
;
2479 bw
|= s
->bbr_info
->bbr_bw_lo
;
2481 out(" bbr:(bw:%sbps,mrtt:%g",
2482 sprint_bw(b1
, bw
* 8.0),
2483 (double)s
->bbr_info
->bbr_min_rtt
/ 1000.0);
2484 if (s
->bbr_info
->bbr_pacing_gain
)
2485 out(",pacing_gain:%g",
2486 (double)s
->bbr_info
->bbr_pacing_gain
/ 256.0);
2487 if (s
->bbr_info
->bbr_cwnd_gain
)
2488 out(",cwnd_gain:%g",
2489 (double)s
->bbr_info
->bbr_cwnd_gain
/ 256.0);
2494 out(" send %sbps", sprint_bw(b1
, s
->send_bps
));
2496 out(" lastsnd:%u", s
->lastsnd
);
2498 out(" lastrcv:%u", s
->lastrcv
);
2500 out(" lastack:%u", s
->lastack
);
2502 if (s
->pacing_rate
) {
2503 out(" pacing_rate %sbps", sprint_bw(b1
, s
->pacing_rate
));
2504 if (s
->pacing_rate_max
)
2505 out("/%sbps", sprint_bw(b1
, s
->pacing_rate_max
));
2508 if (s
->delivery_rate
)
2509 out(" delivery_rate %sbps", sprint_bw(b1
, s
->delivery_rate
));
2511 out(" delivered:%u", s
->delivered
);
2512 if (s
->delivered_ce
)
2513 out(" delivered_ce:%u", s
->delivered_ce
);
2515 out(" app_limited");
2518 out(" busy:%llums", s
->busy_time
/ 1000);
2519 if (s
->rwnd_limited
)
2520 out(" rwnd_limited:%llums(%.1f%%)",
2521 s
->rwnd_limited
/ 1000,
2522 100.0 * s
->rwnd_limited
/ s
->busy_time
);
2523 if (s
->sndbuf_limited
)
2524 out(" sndbuf_limited:%llums(%.1f%%)",
2525 s
->sndbuf_limited
/ 1000,
2526 100.0 * s
->sndbuf_limited
/ s
->busy_time
);
2530 out(" unacked:%u", s
->unacked
);
2531 if (s
->retrans
|| s
->retrans_total
)
2532 out(" retrans:%u/%u", s
->retrans
, s
->retrans_total
);
2534 out(" lost:%u", s
->lost
);
2535 if (s
->sacked
&& s
->ss
.state
!= SS_LISTEN
)
2536 out(" sacked:%u", s
->sacked
);
2538 out(" dsack_dups:%u", s
->dsack_dups
);
2540 out(" fackets:%u", s
->fackets
);
2541 if (s
->reordering
!= 3)
2542 out(" reordering:%d", s
->reordering
);
2544 out(" reord_seen:%d", s
->reord_seen
);
2546 out(" rcv_rtt:%g", s
->rcv_rtt
);
2548 out(" rcv_space:%d", s
->rcv_space
);
2549 if (s
->rcv_ssthresh
)
2550 out(" rcv_ssthresh:%u", s
->rcv_ssthresh
);
2552 out(" notsent:%u", s
->not_sent
);
2554 out(" minrtt:%g", s
->min_rtt
);
2557 static void tcp_timer_print(struct tcpstat
*s
)
2559 static const char * const tmr_name
[] = {
2571 out(" timer:(%s,%s,%d)",
2573 print_ms_timer(s
->timeout
),
2578 static void sctp_timer_print(struct tcpstat
*s
)
2581 out(" timer:(T3_RTX,%s,%d)",
2582 print_ms_timer(s
->timeout
), s
->retrans
);
2585 static int tcp_show_line(char *line
, const struct filter
*f
, int family
)
2587 int rto
= 0, ato
= 0;
2588 struct tcpstat s
= {};
2589 char *loc
, *rem
, *data
;
2592 int hz
= get_user_hz();
2594 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
2597 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
2599 if (!(f
->states
& (1 << state
)))
2602 proc_parse_inet_addr(loc
, rem
, family
, &s
.ss
);
2604 if (f
->f
&& run_ssfilter(f
->f
, &s
.ss
) == 0)
2608 n
= sscanf(data
, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n",
2609 &s
.ss
.state
, &s
.ss
.wq
, &s
.ss
.rq
,
2610 &s
.timer
, &s
.timeout
, &s
.retrans
, &s
.ss
.uid
, &s
.probes
,
2611 &s
.ss
.ino
, &s
.ss
.refcnt
, &s
.ss
.sk
, &rto
, &ato
, &s
.qack
, &s
.cwnd
,
2624 s
.retrans
= s
.timer
!= 1 ? s
.probes
: s
.retrans
;
2625 s
.timeout
= (s
.timeout
* 1000 + hz
- 1) / hz
;
2626 s
.ato
= (double)ato
/ hz
;
2628 s
.rto
= (double)rto
;
2629 s
.ssthresh
= s
.ssthresh
== -1 ? 0 : s
.ssthresh
;
2630 s
.rto
= s
.rto
!= 3 * hz
? s
.rto
/ hz
: 0;
2631 s
.ss
.type
= IPPROTO_TCP
;
2633 inet_stats_print(&s
.ss
, false);
2636 tcp_timer_print(&s
);
2639 sock_details_print(&s
.ss
);
2641 out(" opt:\"%s\"", opt
);
2645 tcp_stats_print(&s
);
2650 static int generic_record_read(FILE *fp
,
2651 int (*worker
)(char*, const struct filter
*, int),
2652 const struct filter
*f
, int fam
)
2657 if (fgets(line
, sizeof(line
), fp
) == NULL
)
2660 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2661 int n
= strlen(line
);
2663 if (n
== 0 || line
[n
-1] != '\n') {
2669 if (worker(line
, f
, fam
) < 0)
2674 return ferror(fp
) ? -1 : 0;
2677 static void print_skmeminfo(struct rtattr
*tb
[], int attrtype
)
2679 const __u32
*skmeminfo
;
2681 if (!tb
[attrtype
]) {
2682 if (attrtype
== INET_DIAG_SKMEMINFO
) {
2683 if (!tb
[INET_DIAG_MEMINFO
])
2686 const struct inet_diag_meminfo
*minfo
=
2687 RTA_DATA(tb
[INET_DIAG_MEMINFO
]);
2689 out(" mem:(r%u,w%u,f%u,t%u)",
2698 skmeminfo
= RTA_DATA(tb
[attrtype
]);
2700 out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
2701 skmeminfo
[SK_MEMINFO_RMEM_ALLOC
],
2702 skmeminfo
[SK_MEMINFO_RCVBUF
],
2703 skmeminfo
[SK_MEMINFO_WMEM_ALLOC
],
2704 skmeminfo
[SK_MEMINFO_SNDBUF
],
2705 skmeminfo
[SK_MEMINFO_FWD_ALLOC
],
2706 skmeminfo
[SK_MEMINFO_WMEM_QUEUED
],
2707 skmeminfo
[SK_MEMINFO_OPTMEM
]);
2709 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2710 (SK_MEMINFO_BACKLOG
+ 1) * sizeof(__u32
))
2711 out(",bl%u", skmeminfo
[SK_MEMINFO_BACKLOG
]);
2713 if (RTA_PAYLOAD(tb
[attrtype
]) >=
2714 (SK_MEMINFO_DROPS
+ 1) * sizeof(__u32
))
2715 out(",d%u", skmeminfo
[SK_MEMINFO_DROPS
]);
2720 static void print_md5sig(struct tcp_diag_md5sig
*sig
)
2723 format_host(sig
->tcpm_family
,
2724 sig
->tcpm_family
== AF_INET6
? 16 : 4,
2726 sig
->tcpm_prefixlen
);
2727 print_escape_buf(sig
->tcpm_key
, sig
->tcpm_keylen
, " ,");
2730 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
2732 static void tcp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
2733 struct rtattr
*tb
[])
2736 struct tcpstat s
= {};
2738 s
.ss
.state
= r
->idiag_state
;
2740 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
2742 if (tb
[INET_DIAG_INFO
]) {
2743 struct tcp_info
*info
;
2744 int len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
2746 /* workaround for older kernels with less fields */
2747 if (len
< sizeof(*info
)) {
2748 info
= alloca(sizeof(*info
));
2749 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
2750 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
2752 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
2755 s
.has_ts_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_TIMESTAMPS
);
2756 s
.has_sack_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SACK
);
2757 s
.has_ecn_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN
);
2758 s
.has_ecnseen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_ECN_SEEN
);
2759 s
.has_fastopen_opt
= TCPI_HAS_OPT(info
, TCPI_OPT_SYN_DATA
);
2762 if (tb
[INET_DIAG_CONG
])
2764 rta_getattr_str(tb
[INET_DIAG_CONG
]),
2765 sizeof(s
.cong_alg
) - 1);
2767 if (TCPI_HAS_OPT(info
, TCPI_OPT_WSCALE
)) {
2768 s
.has_wscale_opt
= true;
2769 s
.snd_wscale
= info
->tcpi_snd_wscale
;
2770 s
.rcv_wscale
= info
->tcpi_rcv_wscale
;
2773 if (info
->tcpi_rto
&& info
->tcpi_rto
!= 3000000)
2774 s
.rto
= (double)info
->tcpi_rto
/ 1000;
2776 s
.backoff
= info
->tcpi_backoff
;
2777 s
.rtt
= (double)info
->tcpi_rtt
/ 1000;
2778 s
.rttvar
= (double)info
->tcpi_rttvar
/ 1000;
2779 s
.ato
= (double)info
->tcpi_ato
/ 1000;
2780 s
.mss
= info
->tcpi_snd_mss
;
2781 s
.rcv_mss
= info
->tcpi_rcv_mss
;
2782 s
.advmss
= info
->tcpi_advmss
;
2783 s
.rcv_space
= info
->tcpi_rcv_space
;
2784 s
.rcv_rtt
= (double)info
->tcpi_rcv_rtt
/ 1000;
2785 s
.lastsnd
= info
->tcpi_last_data_sent
;
2786 s
.lastrcv
= info
->tcpi_last_data_recv
;
2787 s
.lastack
= info
->tcpi_last_ack_recv
;
2788 s
.unacked
= info
->tcpi_unacked
;
2789 s
.retrans
= info
->tcpi_retrans
;
2790 s
.retrans_total
= info
->tcpi_total_retrans
;
2791 s
.lost
= info
->tcpi_lost
;
2792 s
.sacked
= info
->tcpi_sacked
;
2793 s
.fackets
= info
->tcpi_fackets
;
2794 s
.reordering
= info
->tcpi_reordering
;
2795 s
.rcv_ssthresh
= info
->tcpi_rcv_ssthresh
;
2796 s
.cwnd
= info
->tcpi_snd_cwnd
;
2797 s
.pmtu
= info
->tcpi_pmtu
;
2799 if (info
->tcpi_snd_ssthresh
< 0xFFFF)
2800 s
.ssthresh
= info
->tcpi_snd_ssthresh
;
2802 rtt
= (double) info
->tcpi_rtt
;
2803 if (tb
[INET_DIAG_VEGASINFO
]) {
2804 const struct tcpvegas_info
*vinfo
2805 = RTA_DATA(tb
[INET_DIAG_VEGASINFO
]);
2807 if (vinfo
->tcpv_enabled
&&
2808 vinfo
->tcpv_rtt
&& vinfo
->tcpv_rtt
!= 0x7fffffff)
2809 rtt
= vinfo
->tcpv_rtt
;
2812 if (tb
[INET_DIAG_DCTCPINFO
]) {
2813 struct dctcpstat
*dctcp
= malloc(sizeof(struct
2816 const struct tcp_dctcp_info
*dinfo
2817 = RTA_DATA(tb
[INET_DIAG_DCTCPINFO
]);
2819 dctcp
->enabled
= !!dinfo
->dctcp_enabled
;
2820 dctcp
->ce_state
= dinfo
->dctcp_ce_state
;
2821 dctcp
->alpha
= dinfo
->dctcp_alpha
;
2822 dctcp
->ab_ecn
= dinfo
->dctcp_ab_ecn
;
2823 dctcp
->ab_tot
= dinfo
->dctcp_ab_tot
;
2827 if (tb
[INET_DIAG_BBRINFO
]) {
2828 const void *bbr_info
= RTA_DATA(tb
[INET_DIAG_BBRINFO
]);
2829 int len
= min(RTA_PAYLOAD(tb
[INET_DIAG_BBRINFO
]),
2830 sizeof(*s
.bbr_info
));
2832 s
.bbr_info
= calloc(1, sizeof(*s
.bbr_info
));
2833 if (s
.bbr_info
&& bbr_info
)
2834 memcpy(s
.bbr_info
, bbr_info
, len
);
2837 if (rtt
> 0 && info
->tcpi_snd_mss
&& info
->tcpi_snd_cwnd
) {
2838 s
.send_bps
= (double) info
->tcpi_snd_cwnd
*
2839 (double)info
->tcpi_snd_mss
* 8000000. / rtt
;
2842 if (info
->tcpi_pacing_rate
&&
2843 info
->tcpi_pacing_rate
!= ~0ULL) {
2844 s
.pacing_rate
= info
->tcpi_pacing_rate
* 8.;
2846 if (info
->tcpi_max_pacing_rate
&&
2847 info
->tcpi_max_pacing_rate
!= ~0ULL)
2848 s
.pacing_rate_max
= info
->tcpi_max_pacing_rate
* 8.;
2850 s
.bytes_acked
= info
->tcpi_bytes_acked
;
2851 s
.bytes_received
= info
->tcpi_bytes_received
;
2852 s
.segs_out
= info
->tcpi_segs_out
;
2853 s
.segs_in
= info
->tcpi_segs_in
;
2854 s
.data_segs_out
= info
->tcpi_data_segs_out
;
2855 s
.data_segs_in
= info
->tcpi_data_segs_in
;
2856 s
.not_sent
= info
->tcpi_notsent_bytes
;
2857 if (info
->tcpi_min_rtt
&& info
->tcpi_min_rtt
!= ~0U)
2858 s
.min_rtt
= (double) info
->tcpi_min_rtt
/ 1000;
2859 s
.delivery_rate
= info
->tcpi_delivery_rate
* 8.;
2860 s
.app_limited
= info
->tcpi_delivery_rate_app_limited
;
2861 s
.busy_time
= info
->tcpi_busy_time
;
2862 s
.rwnd_limited
= info
->tcpi_rwnd_limited
;
2863 s
.sndbuf_limited
= info
->tcpi_sndbuf_limited
;
2864 s
.delivered
= info
->tcpi_delivered
;
2865 s
.delivered_ce
= info
->tcpi_delivered_ce
;
2866 s
.dsack_dups
= info
->tcpi_dsack_dups
;
2867 s
.reord_seen
= info
->tcpi_reord_seen
;
2868 s
.bytes_sent
= info
->tcpi_bytes_sent
;
2869 s
.bytes_retrans
= info
->tcpi_bytes_retrans
;
2870 tcp_stats_print(&s
);
2874 if (tb
[INET_DIAG_MD5SIG
]) {
2875 struct tcp_diag_md5sig
*sig
= RTA_DATA(tb
[INET_DIAG_MD5SIG
]);
2876 int len
= RTA_PAYLOAD(tb
[INET_DIAG_MD5SIG
]);
2879 print_md5sig(sig
++);
2880 for (len
-= sizeof(*sig
); len
> 0; len
-= sizeof(*sig
)) {
2882 print_md5sig(sig
++);
2887 static const char *format_host_sa(struct sockaddr_storage
*sa
)
2890 struct sockaddr_in sin
;
2891 struct sockaddr_in6 sin6
;
2892 } *saddr
= (void *)sa
;
2894 switch (sa
->ss_family
) {
2896 return format_host(AF_INET
, 4, &saddr
->sin
.sin_addr
);
2898 return format_host(AF_INET6
, 16, &saddr
->sin6
.sin6_addr
);
2904 static void sctp_show_info(const struct nlmsghdr
*nlh
, struct inet_diag_msg
*r
,
2905 struct rtattr
*tb
[])
2907 struct sockaddr_storage
*sa
;
2910 print_skmeminfo(tb
, INET_DIAG_SKMEMINFO
);
2912 if (tb
[INET_DIAG_LOCALS
]) {
2913 len
= RTA_PAYLOAD(tb
[INET_DIAG_LOCALS
]);
2914 sa
= RTA_DATA(tb
[INET_DIAG_LOCALS
]);
2916 out("locals:%s", format_host_sa(sa
));
2917 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
2918 out(",%s", format_host_sa(sa
));
2921 if (tb
[INET_DIAG_PEERS
]) {
2922 len
= RTA_PAYLOAD(tb
[INET_DIAG_PEERS
]);
2923 sa
= RTA_DATA(tb
[INET_DIAG_PEERS
]);
2925 out(" peers:%s", format_host_sa(sa
));
2926 for (sa
++, len
-= sizeof(*sa
); len
> 0; sa
++, len
-= sizeof(*sa
))
2927 out(",%s", format_host_sa(sa
));
2929 if (tb
[INET_DIAG_INFO
]) {
2930 struct sctp_info
*info
;
2931 len
= RTA_PAYLOAD(tb
[INET_DIAG_INFO
]);
2933 /* workaround for older kernels with less fields */
2934 if (len
< sizeof(*info
)) {
2935 info
= alloca(sizeof(*info
));
2936 memcpy(info
, RTA_DATA(tb
[INET_DIAG_INFO
]), len
);
2937 memset((char *)info
+ len
, 0, sizeof(*info
) - len
);
2939 info
= RTA_DATA(tb
[INET_DIAG_INFO
]);
2941 sctp_stats_print(info
);
2945 static void parse_diag_msg(struct nlmsghdr
*nlh
, struct sockstat
*s
)
2947 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2948 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2950 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2951 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2953 s
->state
= r
->idiag_state
;
2954 s
->local
.family
= s
->remote
.family
= r
->idiag_family
;
2955 s
->lport
= ntohs(r
->id
.idiag_sport
);
2956 s
->rport
= ntohs(r
->id
.idiag_dport
);
2957 s
->wq
= r
->idiag_wqueue
;
2958 s
->rq
= r
->idiag_rqueue
;
2959 s
->ino
= r
->idiag_inode
;
2960 s
->uid
= r
->idiag_uid
;
2961 s
->iface
= r
->id
.idiag_if
;
2962 s
->sk
= cookie_sk_get(&r
->id
.idiag_cookie
[0]);
2965 if (tb
[INET_DIAG_MARK
])
2966 s
->mark
= rta_getattr_u32(tb
[INET_DIAG_MARK
]);
2967 if (tb
[INET_DIAG_PROTOCOL
])
2968 s
->raw_prot
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
2972 if (s
->local
.family
== AF_INET
)
2973 s
->local
.bytelen
= s
->remote
.bytelen
= 4;
2975 s
->local
.bytelen
= s
->remote
.bytelen
= 16;
2977 memcpy(s
->local
.data
, r
->id
.idiag_src
, s
->local
.bytelen
);
2978 memcpy(s
->remote
.data
, r
->id
.idiag_dst
, s
->local
.bytelen
);
2981 static int inet_show_sock(struct nlmsghdr
*nlh
,
2984 struct rtattr
*tb
[INET_DIAG_MAX
+1];
2985 struct inet_diag_msg
*r
= NLMSG_DATA(nlh
);
2986 unsigned char v6only
= 0;
2988 parse_rtattr(tb
, INET_DIAG_MAX
, (struct rtattr
*)(r
+1),
2989 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
2991 if (tb
[INET_DIAG_PROTOCOL
])
2992 s
->type
= rta_getattr_u8(tb
[INET_DIAG_PROTOCOL
]);
2994 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
])
2995 v6only
= rta_getattr_u8(tb
[INET_DIAG_SKV6ONLY
]);
2997 inet_stats_print(s
, v6only
);
3000 struct tcpstat t
= {};
3002 t
.timer
= r
->idiag_timer
;
3003 t
.timeout
= r
->idiag_expires
;
3004 t
.retrans
= r
->idiag_retrans
;
3005 if (s
->type
== IPPROTO_SCTP
)
3006 sctp_timer_print(&t
);
3008 tcp_timer_print(&t
);
3012 sock_details_print(s
);
3013 if (s
->local
.family
== AF_INET6
&& tb
[INET_DIAG_SKV6ONLY
])
3014 out(" v6only:%u", v6only
);
3016 if (tb
[INET_DIAG_SHUTDOWN
]) {
3019 mask
= rta_getattr_u8(tb
[INET_DIAG_SHUTDOWN
]);
3021 mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
3025 if (show_mem
|| (show_tcpinfo
&& s
->type
!= IPPROTO_UDP
)) {
3027 if (s
->type
== IPPROTO_SCTP
)
3028 sctp_show_info(nlh
, r
, tb
);
3030 tcp_show_info(nlh
, r
, tb
);
3037 static int tcpdiag_send(int fd
, int protocol
, struct filter
*f
)
3039 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
3041 struct nlmsghdr nlh
;
3042 struct inet_diag_req r
;
3044 .nlh
.nlmsg_len
= sizeof(req
),
3045 .nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
,
3046 .nlh
.nlmsg_seq
= MAGIC_SEQ
,
3047 .r
.idiag_family
= AF_INET
,
3048 .r
.idiag_states
= f
->states
,
3054 struct iovec iov
[3];
3057 if (protocol
== IPPROTO_UDP
)
3060 if (protocol
== IPPROTO_TCP
)
3061 req
.nlh
.nlmsg_type
= TCPDIAG_GETSOCK
;
3063 req
.nlh
.nlmsg_type
= DCCPDIAG_GETSOCK
;
3065 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
3066 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
3070 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
3071 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
3072 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
3075 iov
[0] = (struct iovec
){
3077 .iov_len
= sizeof(req
)
3080 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
3082 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
3083 rta
.rta_len
= RTA_LENGTH(bclen
);
3084 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
3085 iov
[2] = (struct iovec
){ bc
, bclen
};
3086 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
3091 msg
= (struct msghdr
) {
3092 .msg_name
= (void *)&nladdr
,
3093 .msg_namelen
= sizeof(nladdr
),
3095 .msg_iovlen
= iovlen
,
3098 if (sendmsg(fd
, &msg
, 0) < 0) {
3106 static int sockdiag_send(int family
, int fd
, int protocol
, struct filter
*f
)
3108 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
3109 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
3114 struct iovec iov
[3];
3117 if (family
== PF_UNSPEC
)
3118 return tcpdiag_send(fd
, protocol
, f
);
3120 memset(&req
.r
, 0, sizeof(req
.r
));
3121 req
.r
.sdiag_family
= family
;
3122 req
.r
.sdiag_protocol
= protocol
;
3123 req
.r
.idiag_states
= f
->states
;
3125 req
.r
.idiag_ext
|= (1<<(INET_DIAG_MEMINFO
-1));
3126 req
.r
.idiag_ext
|= (1<<(INET_DIAG_SKMEMINFO
-1));
3130 req
.r
.idiag_ext
|= (1<<(INET_DIAG_INFO
-1));
3131 req
.r
.idiag_ext
|= (1<<(INET_DIAG_VEGASINFO
-1));
3132 req
.r
.idiag_ext
|= (1<<(INET_DIAG_CONG
-1));
3135 iov
[0] = (struct iovec
){
3137 .iov_len
= sizeof(req
)
3140 bclen
= ssfilter_bytecompile(f
->f
, &bc
);
3142 rta
.rta_type
= INET_DIAG_REQ_BYTECODE
;
3143 rta
.rta_len
= RTA_LENGTH(bclen
);
3144 iov
[1] = (struct iovec
){ &rta
, sizeof(rta
) };
3145 iov
[2] = (struct iovec
){ bc
, bclen
};
3146 req
.nlh
.nlmsg_len
+= RTA_LENGTH(bclen
);
3151 msg
= (struct msghdr
) {
3152 .msg_name
= (void *)&nladdr
,
3153 .msg_namelen
= sizeof(nladdr
),
3155 .msg_iovlen
= iovlen
,
3158 if (sendmsg(fd
, &msg
, 0) < 0) {
3166 struct inet_diag_arg
{
3169 struct rtnl_handle
*rth
;
3172 static int kill_inet_sock(struct nlmsghdr
*h
, void *arg
, struct sockstat
*s
)
3174 struct inet_diag_msg
*d
= NLMSG_DATA(h
);
3175 struct inet_diag_arg
*diag_arg
= arg
;
3176 struct rtnl_handle
*rth
= diag_arg
->rth
;
3178 DIAG_REQUEST(req
, struct inet_diag_req_v2 r
);
3180 req
.nlh
.nlmsg_type
= SOCK_DESTROY
;
3181 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3182 req
.nlh
.nlmsg_seq
= ++rth
->seq
;
3183 req
.r
.sdiag_family
= d
->idiag_family
;
3184 req
.r
.sdiag_protocol
= diag_arg
->protocol
;
3187 if (diag_arg
->protocol
== IPPROTO_RAW
) {
3188 struct inet_diag_req_raw
*raw
= (void *)&req
.r
;
3190 BUILD_BUG_ON(sizeof(req
.r
) != sizeof(*raw
));
3191 raw
->sdiag_raw_protocol
= s
->raw_prot
;
3194 return rtnl_talk(rth
, &req
.nlh
, NULL
);
3197 static int show_one_inet_sock(struct nlmsghdr
*h
, void *arg
)
3200 struct inet_diag_arg
*diag_arg
= arg
;
3201 struct inet_diag_msg
*r
= NLMSG_DATA(h
);
3202 struct sockstat s
= {};
3204 if (!(diag_arg
->f
->families
& FAMILY_MASK(r
->idiag_family
)))
3207 parse_diag_msg(h
, &s
);
3208 s
.type
= diag_arg
->protocol
;
3210 if (diag_arg
->f
->f
&& run_ssfilter(diag_arg
->f
->f
, &s
) == 0)
3213 if (diag_arg
->f
->kill
&& kill_inet_sock(h
, arg
, &s
) != 0) {
3214 if (errno
== EOPNOTSUPP
|| errno
== ENOENT
) {
3215 /* Socket can't be closed, or is already closed. */
3218 perror("SOCK_DESTROY answers");
3223 err
= inet_show_sock(h
, &s
);
3230 static int inet_show_netlink(struct filter
*f
, FILE *dump_fp
, int protocol
)
3233 struct rtnl_handle rth
, rth2
;
3234 int family
= PF_INET
;
3235 struct inet_diag_arg arg
= { .f
= f
, .protocol
= protocol
};
3237 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
3241 if (rtnl_open_byproto(&rth2
, 0, NETLINK_SOCK_DIAG
)) {
3248 rth
.dump
= MAGIC_SEQ
;
3249 rth
.dump_fp
= dump_fp
;
3250 if (preferred_family
== PF_INET6
)
3254 if ((err
= sockdiag_send(family
, rth
.fd
, protocol
, f
)))
3257 if ((err
= rtnl_dump_filter(&rth
, show_one_inet_sock
, &arg
))) {
3258 if (family
!= PF_UNSPEC
) {
3264 if (family
== PF_INET
&& preferred_family
!= PF_INET
) {
3272 rtnl_close(arg
.rth
);
3276 static int tcp_show_netlink_file(struct filter
*f
)
3282 if ((fp
= fopen(getenv("TCPDIAG_FILE"), "r")) == NULL
) {
3283 perror("fopen($TCPDIAG_FILE)");
3289 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
3290 struct sockstat s
= {};
3292 status
= fread(buf
, 1, sizeof(*h
), fp
);
3294 perror("Reading header from $TCPDIAG_FILE");
3297 if (status
!= sizeof(*h
)) {
3298 perror("Unexpected EOF reading $TCPDIAG_FILE");
3302 status
= fread(h
+1, 1, NLMSG_ALIGN(h
->nlmsg_len
-sizeof(*h
)), fp
);
3305 perror("Reading $TCPDIAG_FILE");
3308 if (status
+ sizeof(*h
) < h
->nlmsg_len
) {
3309 perror("Unexpected EOF reading $TCPDIAG_FILE");
3313 /* The only legal exit point */
3314 if (h
->nlmsg_type
== NLMSG_DONE
) {
3319 if (h
->nlmsg_type
== NLMSG_ERROR
) {
3320 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
3322 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
3323 fprintf(stderr
, "ERROR truncated\n");
3325 errno
= -err
->error
;
3326 perror("TCPDIAG answered");
3331 parse_diag_msg(h
, &s
);
3332 s
.type
= IPPROTO_TCP
;
3334 if (f
&& f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
3337 err2
= inet_show_sock(h
, &s
);
3348 static int tcp_show(struct filter
*f
)
3352 int bufsize
= 1024*1024;
3354 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3357 dg_proto
= TCP_PROTO
;
3359 if (getenv("TCPDIAG_FILE"))
3360 return tcp_show_netlink_file(f
);
3362 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
3363 && inet_show_netlink(f
, NULL
, IPPROTO_TCP
) == 0)
3366 /* Sigh... We have to parse /proc/net/tcp... */
3367 while (bufsize
>= 64*1024) {
3368 if ((buf
= malloc(bufsize
)) != NULL
)
3377 if (f
->families
& FAMILY_MASK(AF_INET
)) {
3378 if ((fp
= net_tcp_open()) == NULL
)
3381 setbuffer(fp
, buf
, bufsize
);
3382 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET
))
3387 if ((f
->families
& FAMILY_MASK(AF_INET6
)) &&
3388 (fp
= net_tcp6_open()) != NULL
) {
3389 setbuffer(fp
, buf
, bufsize
);
3390 if (generic_record_read(fp
, tcp_show_line
, f
, AF_INET6
))
3400 int saved_errno
= errno
;
3405 errno
= saved_errno
;
3410 static int dccp_show(struct filter
*f
)
3412 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3415 if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT")
3416 && inet_show_netlink(f
, NULL
, IPPROTO_DCCP
) == 0)
3422 static int sctp_show(struct filter
*f
)
3424 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3427 if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT")
3428 && inet_show_netlink(f
, NULL
, IPPROTO_SCTP
) == 0)
3434 static int dgram_show_line(char *line
, const struct filter
*f
, int family
)
3436 struct sockstat s
= {};
3437 char *loc
, *rem
, *data
;
3441 if (proc_inet_split_line(line
, &loc
, &rem
, &data
))
3444 int state
= (data
[1] >= 'A') ? (data
[1] - 'A' + 10) : (data
[1] - '0');
3446 if (!(f
->states
& (1 << state
)))
3449 proc_parse_inet_addr(loc
, rem
, family
, &s
);
3451 if (f
->f
&& run_ssfilter(f
->f
, &s
) == 0)
3455 n
= sscanf(data
, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
3456 &s
.state
, &s
.wq
, &s
.rq
,
3458 &s
.refcnt
, &s
.sk
, opt
);
3463 s
.type
= dg_proto
== UDP_PROTO
? IPPROTO_UDP
: 0;
3464 inet_stats_print(&s
, false);
3466 if (show_details
&& opt
[0])
3467 out(" opt:\"%s\"", opt
);
3472 static int udp_show(struct filter
*f
)
3476 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3479 dg_proto
= UDP_PROTO
;
3481 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
3482 && inet_show_netlink(f
, NULL
, IPPROTO_UDP
) == 0)
3485 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3486 if ((fp
= net_udp_open()) == NULL
)
3488 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3493 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3494 (fp
= net_udp6_open()) != NULL
) {
3495 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3503 int saved_errno
= errno
;
3507 errno
= saved_errno
;
3512 static int raw_show(struct filter
*f
)
3516 if (!filter_af_get(f
, AF_INET
) && !filter_af_get(f
, AF_INET6
))
3519 dg_proto
= RAW_PROTO
;
3521 if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") &&
3522 inet_show_netlink(f
, NULL
, IPPROTO_RAW
) == 0)
3525 if (f
->families
&FAMILY_MASK(AF_INET
)) {
3526 if ((fp
= net_raw_open()) == NULL
)
3528 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET
))
3533 if ((f
->families
&FAMILY_MASK(AF_INET6
)) &&
3534 (fp
= net_raw6_open()) != NULL
) {
3535 if (generic_record_read(fp
, dgram_show_line
, f
, AF_INET6
))
3543 int saved_errno
= errno
;
3547 errno
= saved_errno
;
3552 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
3554 static void unix_list_drop_first(struct sockstat
**list
)
3556 struct sockstat
*s
= *list
;
3558 (*list
) = (*list
)->next
;
3563 static bool unix_type_skip(struct sockstat
*s
, struct filter
*f
)
3565 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
&(1<<UNIX_ST_DB
)))
3567 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<UNIX_DG_DB
)))
3569 if (s
->type
== SOCK_SEQPACKET
&& !(f
->dbs
&(1<<UNIX_SQ_DB
)))
3574 static void unix_stats_print(struct sockstat
*s
, struct filter
*f
)
3576 char port_name
[30] = {};
3578 sock_state_print(s
);
3580 sock_addr_print(s
->name
?: "*", " ",
3581 int_to_str(s
->lport
, port_name
), NULL
);
3582 sock_addr_print(s
->peer_name
?: "*", " ",
3583 int_to_str(s
->rport
, port_name
), NULL
);
3588 static int unix_show_sock(struct nlmsghdr
*nlh
, void *arg
)
3590 struct filter
*f
= (struct filter
*)arg
;
3591 struct unix_diag_msg
*r
= NLMSG_DATA(nlh
);
3592 struct rtattr
*tb
[UNIX_DIAG_MAX
+1];
3594 struct sockstat stat
= { .name
= "*", .peer_name
= "*" };
3596 parse_rtattr(tb
, UNIX_DIAG_MAX
, (struct rtattr
*)(r
+1),
3597 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3599 stat
.type
= r
->udiag_type
;
3600 stat
.state
= r
->udiag_state
;
3601 stat
.ino
= stat
.lport
= r
->udiag_ino
;
3602 stat
.local
.family
= stat
.remote
.family
= AF_UNIX
;
3604 if (unix_type_skip(&stat
, f
))
3607 if (tb
[UNIX_DIAG_RQLEN
]) {
3608 struct unix_diag_rqlen
*rql
= RTA_DATA(tb
[UNIX_DIAG_RQLEN
]);
3610 stat
.rq
= rql
->udiag_rqueue
;
3611 stat
.wq
= rql
->udiag_wqueue
;
3613 if (tb
[UNIX_DIAG_NAME
]) {
3614 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_NAME
]);
3616 memcpy(name
, RTA_DATA(tb
[UNIX_DIAG_NAME
]), len
);
3618 if (name
[0] == '\0') {
3620 for (i
= 0; i
< len
; i
++)
3621 if (name
[i
] == '\0')
3624 stat
.name
= &name
[0];
3625 memcpy(stat
.local
.data
, &stat
.name
, sizeof(stat
.name
));
3627 if (tb
[UNIX_DIAG_PEER
])
3628 stat
.rport
= rta_getattr_u32(tb
[UNIX_DIAG_PEER
]);
3630 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
3633 unix_stats_print(&stat
, f
);
3636 print_skmeminfo(tb
, UNIX_DIAG_MEMINFO
);
3638 if (tb
[UNIX_DIAG_SHUTDOWN
]) {
3641 mask
= rta_getattr_u8(tb
[UNIX_DIAG_SHUTDOWN
]);
3643 mask
& 1 ? '-' : '<', mask
& 2 ? '-' : '>');
3645 if (tb
[UNIX_DIAG_VFS
]) {
3646 struct unix_diag_vfs
*uv
= RTA_DATA(tb
[UNIX_DIAG_VFS
]);
3648 out(" ino:%u dev:%u/%u", uv
->udiag_vfs_ino
, major(uv
->udiag_vfs_dev
),
3649 minor(uv
->udiag_vfs_dev
));
3651 if (tb
[UNIX_DIAG_ICONS
]) {
3652 int len
= RTA_PAYLOAD(tb
[UNIX_DIAG_ICONS
]);
3653 __u32
*peers
= RTA_DATA(tb
[UNIX_DIAG_ICONS
]);
3657 for (i
= 0; i
< len
/ sizeof(__u32
); i
++)
3658 out(" %u", peers
[i
]);
3665 static int handle_netlink_request(struct filter
*f
, struct nlmsghdr
*req
,
3666 size_t size
, rtnl_filter_t show_one_sock
)
3669 struct rtnl_handle rth
;
3671 if (rtnl_open_byproto(&rth
, 0, NETLINK_SOCK_DIAG
))
3674 rth
.dump
= MAGIC_SEQ
;
3676 if (rtnl_send(&rth
, req
, size
) < 0)
3679 if (rtnl_dump_filter(&rth
, show_one_sock
, f
))
3688 static int unix_show_netlink(struct filter
*f
)
3690 DIAG_REQUEST(req
, struct unix_diag_req r
);
3692 req
.r
.sdiag_family
= AF_UNIX
;
3693 req
.r
.udiag_states
= f
->states
;
3694 req
.r
.udiag_show
= UDIAG_SHOW_NAME
| UDIAG_SHOW_PEER
| UDIAG_SHOW_RQLEN
;
3696 req
.r
.udiag_show
|= UDIAG_SHOW_MEMINFO
;
3698 req
.r
.udiag_show
|= UDIAG_SHOW_VFS
| UDIAG_SHOW_ICONS
;
3700 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), unix_show_sock
);
3703 static int unix_show(struct filter
*f
)
3710 struct sockstat
*list
= NULL
;
3711 const int unix_state_map
[] = { SS_CLOSE
, SS_SYN_SENT
,
3712 SS_ESTABLISHED
, SS_CLOSING
};
3714 if (!filter_af_get(f
, AF_UNIX
))
3717 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
3718 && unix_show_netlink(f
) == 0)
3721 if ((fp
= net_unix_open()) == NULL
)
3723 if (!fgets(buf
, sizeof(buf
), fp
)) {
3728 if (memcmp(buf
, "Peer", 4) == 0)
3732 while (fgets(buf
, sizeof(buf
), fp
)) {
3733 struct sockstat
*u
, **insp
;
3736 if (!(u
= calloc(1, sizeof(*u
))))
3739 if (sscanf(buf
, "%x: %x %x %x %x %x %d %s",
3740 &u
->rport
, &u
->rq
, &u
->wq
, &flags
, &u
->type
,
3741 &u
->state
, &u
->ino
, name
) < 8)
3745 u
->local
.family
= u
->remote
.family
= AF_UNIX
;
3747 if (flags
& (1 << 16)) {
3748 u
->state
= SS_LISTEN
;
3749 } else if (u
->state
> 0 &&
3750 u
->state
<= ARRAY_SIZE(unix_state_map
)) {
3751 u
->state
= unix_state_map
[u
->state
-1];
3752 if (u
->type
== SOCK_DGRAM
&& u
->state
== SS_CLOSE
&& u
->rport
)
3753 u
->state
= SS_ESTABLISHED
;
3755 if (unix_type_skip(u
, f
) ||
3756 !(f
->states
& (1 << u
->state
))) {
3768 u
->name
= strdup(name
);
3778 for (p
= list
; p
; p
= p
->next
) {
3779 if (u
->rport
== p
->lport
)
3785 u
->peer_name
= p
->name
? : "*";
3789 struct sockstat st
= {
3790 .local
.family
= AF_UNIX
,
3791 .remote
.family
= AF_UNIX
,
3794 memcpy(st
.local
.data
, &u
->name
, sizeof(u
->name
));
3795 /* when parsing the old format rport is set to 0 and
3796 * therefore peer_name remains NULL
3798 if (u
->peer_name
&& strcmp(u
->peer_name
, "*"))
3799 memcpy(st
.remote
.data
, &u
->peer_name
,
3800 sizeof(u
->peer_name
));
3801 if (run_ssfilter(f
->f
, &st
) == 0) {
3810 if (u
->type
< (*insp
)->type
||
3811 (u
->type
== (*insp
)->type
&&
3812 u
->ino
< (*insp
)->ino
))
3814 insp
= &(*insp
)->next
;
3819 if (++cnt
> MAX_UNIX_REMEMBER
) {
3821 unix_stats_print(list
, f
);
3822 unix_list_drop_first(&list
);
3829 unix_stats_print(list
, f
);
3830 unix_list_drop_first(&list
);
3836 static int packet_stats_print(struct sockstat
*s
, const struct filter
*f
)
3838 const char *addr
, *port
;
3841 s
->local
.family
= s
->remote
.family
= AF_PACKET
;
3844 s
->local
.data
[0] = s
->prot
;
3845 if (run_ssfilter(f
->f
, s
) == 0)
3849 sock_state_print(s
);
3854 addr
= ll_proto_n2a(htons(s
->prot
), ll_name
, sizeof(ll_name
));
3859 port
= xll_index_to_name(s
->iface
);
3861 sock_addr_print(addr
, ":", port
, NULL
);
3862 sock_addr_print("", "*", "", NULL
);
3867 sock_details_print(s
);
3872 static void packet_show_ring(struct packet_diag_ring
*ring
)
3874 out("blk_size:%d", ring
->pdr_block_size
);
3875 out(",blk_nr:%d", ring
->pdr_block_nr
);
3876 out(",frm_size:%d", ring
->pdr_frame_size
);
3877 out(",frm_nr:%d", ring
->pdr_frame_nr
);
3878 out(",tmo:%d", ring
->pdr_retire_tmo
);
3879 out(",features:0x%x", ring
->pdr_features
);
3882 static int packet_show_sock(struct nlmsghdr
*nlh
, void *arg
)
3884 const struct filter
*f
= arg
;
3885 struct packet_diag_msg
*r
= NLMSG_DATA(nlh
);
3886 struct packet_diag_info
*pinfo
= NULL
;
3887 struct packet_diag_ring
*ring_rx
= NULL
, *ring_tx
= NULL
;
3888 struct rtattr
*tb
[PACKET_DIAG_MAX
+1];
3889 struct sockstat stat
= {};
3890 uint32_t fanout
= 0;
3891 bool has_fanout
= false;
3893 parse_rtattr(tb
, PACKET_DIAG_MAX
, (struct rtattr
*)(r
+1),
3894 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
3896 /* use /proc/net/packet if all info are not available */
3897 if (!tb
[PACKET_DIAG_MEMINFO
])
3900 stat
.type
= r
->pdiag_type
;
3901 stat
.prot
= r
->pdiag_num
;
3902 stat
.ino
= r
->pdiag_ino
;
3903 stat
.state
= SS_CLOSE
;
3904 stat
.sk
= cookie_sk_get(&r
->pdiag_cookie
[0]);
3906 if (tb
[PACKET_DIAG_MEMINFO
]) {
3907 __u32
*skmeminfo
= RTA_DATA(tb
[PACKET_DIAG_MEMINFO
]);
3909 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
3912 if (tb
[PACKET_DIAG_INFO
]) {
3913 pinfo
= RTA_DATA(tb
[PACKET_DIAG_INFO
]);
3914 stat
.lport
= stat
.iface
= pinfo
->pdi_index
;
3917 if (tb
[PACKET_DIAG_UID
])
3918 stat
.uid
= rta_getattr_u32(tb
[PACKET_DIAG_UID
]);
3920 if (tb
[PACKET_DIAG_RX_RING
])
3921 ring_rx
= RTA_DATA(tb
[PACKET_DIAG_RX_RING
]);
3923 if (tb
[PACKET_DIAG_TX_RING
])
3924 ring_tx
= RTA_DATA(tb
[PACKET_DIAG_TX_RING
]);
3926 if (tb
[PACKET_DIAG_FANOUT
]) {
3928 fanout
= rta_getattr_u32(tb
[PACKET_DIAG_FANOUT
]);
3931 if (packet_stats_print(&stat
, f
))
3936 out("\n\tver:%d", pinfo
->pdi_version
);
3937 out(" cpy_thresh:%d", pinfo
->pdi_copy_thresh
);
3939 if (pinfo
->pdi_flags
& PDI_RUNNING
)
3941 if (pinfo
->pdi_flags
& PDI_AUXDATA
)
3943 if (pinfo
->pdi_flags
& PDI_ORIGDEV
)
3945 if (pinfo
->pdi_flags
& PDI_VNETHDR
)
3947 if (pinfo
->pdi_flags
& PDI_LOSS
)
3949 if (!pinfo
->pdi_flags
)
3954 out("\n\tring_rx(");
3955 packet_show_ring(ring_rx
);
3959 out("\n\tring_tx(");
3960 packet_show_ring(ring_tx
);
3964 uint16_t type
= (fanout
>> 16) & 0xffff;
3967 out("id:%d,", fanout
& 0xffff);
3989 if (show_bpf
&& tb
[PACKET_DIAG_FILTER
]) {
3990 struct sock_filter
*fil
=
3991 RTA_DATA(tb
[PACKET_DIAG_FILTER
]);
3992 int num
= RTA_PAYLOAD(tb
[PACKET_DIAG_FILTER
]) /
3993 sizeof(struct sock_filter
);
3995 out("\n\tbpf filter (%d): ", num
);
3997 out(" 0x%02x %u %u %u,",
3998 fil
->code
, fil
->jt
, fil
->jf
, fil
->k
);
4005 print_skmeminfo(tb
, PACKET_DIAG_MEMINFO
);
4009 static int packet_show_netlink(struct filter
*f
)
4011 DIAG_REQUEST(req
, struct packet_diag_req r
);
4013 req
.r
.sdiag_family
= AF_PACKET
;
4014 req
.r
.pdiag_show
= PACKET_SHOW_INFO
| PACKET_SHOW_MEMINFO
|
4015 PACKET_SHOW_FILTER
| PACKET_SHOW_RING_CFG
| PACKET_SHOW_FANOUT
;
4017 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), packet_show_sock
);
4020 static int packet_show_line(char *buf
, const struct filter
*f
, int fam
)
4022 unsigned long long sk
;
4023 struct sockstat stat
= {};
4024 int type
, prot
, iface
, state
, rq
, uid
, ino
;
4026 sscanf(buf
, "%llx %*d %d %x %d %d %u %u %u",
4028 &type
, &prot
, &iface
, &state
,
4031 if (stat
.type
== SOCK_RAW
&& !(f
->dbs
&(1<<PACKET_R_DB
)))
4033 if (stat
.type
== SOCK_DGRAM
&& !(f
->dbs
&(1<<PACKET_DG_DB
)))
4038 stat
.lport
= stat
.iface
= iface
;
4043 stat
.state
= SS_CLOSE
;
4045 if (packet_stats_print(&stat
, f
))
4051 static int packet_show(struct filter
*f
)
4056 if (!filter_af_get(f
, AF_PACKET
) || !(f
->states
& (1 << SS_CLOSE
)))
4059 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
4060 packet_show_netlink(f
) == 0)
4063 if ((fp
= net_packet_open()) == NULL
)
4065 if (generic_record_read(fp
, packet_show_line
, f
, AF_PACKET
))
4072 static int xdp_stats_print(struct sockstat
*s
, const struct filter
*f
)
4074 const char *addr
, *port
;
4077 s
->local
.family
= s
->remote
.family
= AF_XDP
;
4080 if (run_ssfilter(f
->f
, s
) == 0)
4084 sock_state_print(s
);
4087 addr
= xll_index_to_name(s
->iface
);
4088 snprintf(q_str
, sizeof(q_str
), "q%d", s
->lport
);
4090 sock_addr_print(addr
, ":", port
, NULL
);
4092 sock_addr_print("", "*", "", NULL
);
4095 sock_addr_print("", "*", "", NULL
);
4100 sock_details_print(s
);
4105 static void xdp_show_ring(const char *name
, struct xdp_diag_ring
*ring
)
4107 out("\n\t%s(", name
);
4108 out("entries:%u", ring
->entries
);
4112 static void xdp_show_umem(struct xdp_diag_umem
*umem
, struct xdp_diag_ring
*fr
,
4113 struct xdp_diag_ring
*cr
)
4116 out("id:%u", umem
->id
);
4117 out(",size:%llu", umem
->size
);
4118 out(",num_pages:%u", umem
->num_pages
);
4119 out(",chunk_size:%u", umem
->chunk_size
);
4120 out(",headroom:%u", umem
->headroom
);
4121 out(",ifindex:%u", umem
->ifindex
);
4122 out(",qid:%u", umem
->queue_id
);
4123 out(",zc:%u", umem
->flags
& XDP_DU_F_ZEROCOPY
);
4124 out(",refs:%u", umem
->refs
);
4128 xdp_show_ring("fr", fr
);
4130 xdp_show_ring("cr", cr
);
4133 static int xdp_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4135 struct xdp_diag_ring
*rx
= NULL
, *tx
= NULL
, *fr
= NULL
, *cr
= NULL
;
4136 struct xdp_diag_msg
*msg
= NLMSG_DATA(nlh
);
4137 struct rtattr
*tb
[XDP_DIAG_MAX
+ 1];
4138 struct xdp_diag_info
*info
= NULL
;
4139 struct xdp_diag_umem
*umem
= NULL
;
4140 const struct filter
*f
= arg
;
4141 struct sockstat stat
= {};
4143 parse_rtattr(tb
, XDP_DIAG_MAX
, (struct rtattr
*)(msg
+ 1),
4144 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*msg
)));
4146 stat
.type
= msg
->xdiag_type
;
4147 stat
.ino
= msg
->xdiag_ino
;
4148 stat
.state
= SS_CLOSE
;
4149 stat
.sk
= cookie_sk_get(&msg
->xdiag_cookie
[0]);
4151 if (tb
[XDP_DIAG_INFO
]) {
4152 info
= RTA_DATA(tb
[XDP_DIAG_INFO
]);
4153 stat
.iface
= info
->ifindex
;
4154 stat
.lport
= info
->queue_id
;
4157 if (tb
[XDP_DIAG_UID
])
4158 stat
.uid
= rta_getattr_u32(tb
[XDP_DIAG_UID
]);
4159 if (tb
[XDP_DIAG_RX_RING
])
4160 rx
= RTA_DATA(tb
[XDP_DIAG_RX_RING
]);
4161 if (tb
[XDP_DIAG_TX_RING
])
4162 tx
= RTA_DATA(tb
[XDP_DIAG_TX_RING
]);
4163 if (tb
[XDP_DIAG_UMEM
])
4164 umem
= RTA_DATA(tb
[XDP_DIAG_UMEM
]);
4165 if (tb
[XDP_DIAG_UMEM_FILL_RING
])
4166 fr
= RTA_DATA(tb
[XDP_DIAG_UMEM_FILL_RING
]);
4167 if (tb
[XDP_DIAG_UMEM_COMPLETION_RING
])
4168 cr
= RTA_DATA(tb
[XDP_DIAG_UMEM_COMPLETION_RING
]);
4169 if (tb
[XDP_DIAG_MEMINFO
]) {
4170 __u32
*skmeminfo
= RTA_DATA(tb
[XDP_DIAG_MEMINFO
]);
4172 stat
.rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
4175 if (xdp_stats_print(&stat
, f
))
4180 xdp_show_ring("rx", rx
);
4182 xdp_show_ring("tx", tx
);
4184 xdp_show_umem(umem
, fr
, cr
);
4188 print_skmeminfo(tb
, XDP_DIAG_MEMINFO
); // really?
4194 static int xdp_show(struct filter
*f
)
4196 DIAG_REQUEST(req
, struct xdp_diag_req r
);
4198 if (!filter_af_get(f
, AF_XDP
) || !(f
->states
& (1 << SS_CLOSE
)))
4201 req
.r
.sdiag_family
= AF_XDP
;
4202 req
.r
.xdiag_show
= XDP_SHOW_INFO
| XDP_SHOW_RING_CFG
| XDP_SHOW_UMEM
|
4205 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), xdp_show_sock
);
4208 static int netlink_show_one(struct filter
*f
,
4209 int prot
, int pid
, unsigned int groups
,
4210 int state
, int dst_pid
, unsigned int dst_group
,
4212 unsigned long long sk
, unsigned long long cb
)
4214 struct sockstat st
= {
4218 .local
.family
= AF_NETLINK
,
4219 .remote
.family
= AF_NETLINK
,
4222 SPRINT_BUF(prot_buf
) = {};
4223 const char *prot_name
;
4224 char procname
[64] = {};
4229 st
.local
.data
[0] = prot
;
4230 if (run_ssfilter(f
->f
, &st
) == 0)
4234 sock_state_print(&st
);
4236 if (resolve_services
)
4237 prot_name
= nl_proto_n2a(prot
, prot_buf
, sizeof(prot_buf
));
4239 prot_name
= int_to_str(prot
, prot_buf
);
4243 } else if (resolve_services
) {
4248 strncpy(procname
, "kernel", 7);
4249 } else if (pid
> 0) {
4252 snprintf(procname
, sizeof(procname
), "%s/%d/stat",
4253 getenv("PROC_ROOT") ? : "/proc", pid
);
4254 if ((fp
= fopen(procname
, "r")) != NULL
) {
4255 if (fscanf(fp
, "%*d (%[^)])", procname
) == 1) {
4256 snprintf(procname
+strlen(procname
),
4257 sizeof(procname
)-strlen(procname
),
4265 int_to_str(pid
, procname
);
4267 int_to_str(pid
, procname
);
4270 sock_addr_print(prot_name
, ":", procname
, NULL
);
4272 if (state
== NETLINK_CONNECTED
) {
4273 char dst_group_buf
[30];
4274 char dst_pid_buf
[30];
4276 sock_addr_print(int_to_str(dst_group
, dst_group_buf
), ":",
4277 int_to_str(dst_pid
, dst_pid_buf
), NULL
);
4279 sock_addr_print("", "*", "", NULL
);
4282 char *pid_context
= NULL
;
4284 if (show_proc_ctx
) {
4285 /* The pid value will either be:
4286 * 0 if destination kernel - show kernel initial context.
4287 * A valid process pid - use getpidcon.
4288 * A unique value allocated by the kernel or netlink user
4289 * to the process - show context as "not available".
4292 security_get_initial_context("kernel", &pid_context
);
4294 getpidcon(pid
, &pid_context
);
4296 out(" proc_ctx=%s", pid_context
? : "unavailable");
4301 out(" sk=%llx cb=%llx groups=0x%08x", sk
, cb
, groups
);
4307 static int netlink_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4309 struct filter
*f
= (struct filter
*)arg
;
4310 struct netlink_diag_msg
*r
= NLMSG_DATA(nlh
);
4311 struct rtattr
*tb
[NETLINK_DIAG_MAX
+1];
4313 unsigned long groups
= 0;
4315 parse_rtattr(tb
, NETLINK_DIAG_MAX
, (struct rtattr
*)(r
+1),
4316 nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
4318 if (tb
[NETLINK_DIAG_GROUPS
] && RTA_PAYLOAD(tb
[NETLINK_DIAG_GROUPS
]))
4319 groups
= *(unsigned long *) RTA_DATA(tb
[NETLINK_DIAG_GROUPS
]);
4321 if (tb
[NETLINK_DIAG_MEMINFO
]) {
4322 const __u32
*skmeminfo
;
4324 skmeminfo
= RTA_DATA(tb
[NETLINK_DIAG_MEMINFO
]);
4326 rq
= skmeminfo
[SK_MEMINFO_RMEM_ALLOC
];
4327 wq
= skmeminfo
[SK_MEMINFO_WMEM_ALLOC
];
4330 if (netlink_show_one(f
, r
->ndiag_protocol
, r
->ndiag_portid
, groups
,
4331 r
->ndiag_state
, r
->ndiag_dst_portid
, r
->ndiag_dst_group
,
4338 print_skmeminfo(tb
, NETLINK_DIAG_MEMINFO
);
4344 static int netlink_show_netlink(struct filter
*f
)
4346 DIAG_REQUEST(req
, struct netlink_diag_req r
);
4348 req
.r
.sdiag_family
= AF_NETLINK
;
4349 req
.r
.sdiag_protocol
= NDIAG_PROTO_ALL
;
4350 req
.r
.ndiag_show
= NDIAG_SHOW_GROUPS
| NDIAG_SHOW_MEMINFO
;
4352 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), netlink_show_sock
);
4355 static int netlink_show(struct filter
*f
)
4360 unsigned int groups
;
4362 unsigned long long sk
, cb
;
4364 if (!filter_af_get(f
, AF_NETLINK
) || !(f
->states
& (1 << SS_CLOSE
)))
4367 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
4368 netlink_show_netlink(f
) == 0)
4371 if ((fp
= net_netlink_open()) == NULL
)
4373 if (!fgets(buf
, sizeof(buf
), fp
)) {
4378 while (fgets(buf
, sizeof(buf
), fp
)) {
4379 sscanf(buf
, "%llx %d %d %x %d %d %llx %d",
4381 &prot
, &pid
, &groups
, &rq
, &wq
, &cb
, &rc
);
4383 netlink_show_one(f
, prot
, pid
, groups
, 0, 0, 0, rq
, wq
, sk
, cb
);
4390 static bool vsock_type_skip(struct sockstat
*s
, struct filter
*f
)
4392 if (s
->type
== SOCK_STREAM
&& !(f
->dbs
& (1 << VSOCK_ST_DB
)))
4394 if (s
->type
== SOCK_DGRAM
&& !(f
->dbs
& (1 << VSOCK_DG_DB
)))
4399 static void vsock_addr_print(inet_prefix
*a
, __u32 port
)
4401 char cid_str
[sizeof("4294967295")];
4402 char port_str
[sizeof("4294967295")];
4405 memcpy(&cid
, a
->data
, sizeof(cid
));
4407 if (cid
== ~(__u32
)0)
4408 snprintf(cid_str
, sizeof(cid_str
), "*");
4410 snprintf(cid_str
, sizeof(cid_str
), "%u", cid
);
4412 if (port
== ~(__u32
)0)
4413 snprintf(port_str
, sizeof(port_str
), "*");
4415 snprintf(port_str
, sizeof(port_str
), "%u", port
);
4417 sock_addr_print(cid_str
, ":", port_str
, NULL
);
4420 static void vsock_stats_print(struct sockstat
*s
, struct filter
*f
)
4422 sock_state_print(s
);
4424 vsock_addr_print(&s
->local
, s
->lport
);
4425 vsock_addr_print(&s
->remote
, s
->rport
);
4430 static int vsock_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4432 struct filter
*f
= (struct filter
*)arg
;
4433 struct vsock_diag_msg
*r
= NLMSG_DATA(nlh
);
4434 struct sockstat stat
= {
4435 .type
= r
->vdiag_type
,
4436 .lport
= r
->vdiag_src_port
,
4437 .rport
= r
->vdiag_dst_port
,
4438 .state
= r
->vdiag_state
,
4439 .ino
= r
->vdiag_ino
,
4442 vsock_set_inet_prefix(&stat
.local
, r
->vdiag_src_cid
);
4443 vsock_set_inet_prefix(&stat
.remote
, r
->vdiag_dst_cid
);
4445 if (vsock_type_skip(&stat
, f
))
4448 if (f
->f
&& run_ssfilter(f
->f
, &stat
) == 0)
4451 vsock_stats_print(&stat
, f
);
4456 static int vsock_show(struct filter
*f
)
4458 DIAG_REQUEST(req
, struct vsock_diag_req r
);
4460 if (!filter_af_get(f
, AF_VSOCK
))
4463 req
.r
.sdiag_family
= AF_VSOCK
;
4464 req
.r
.vdiag_states
= f
->states
;
4466 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), vsock_show_sock
);
4469 static void tipc_sock_addr_print(struct rtattr
*net_addr
, struct rtattr
*id
)
4471 uint32_t node
= rta_getattr_u32(net_addr
);
4472 uint32_t identity
= rta_getattr_u32(id
);
4474 SPRINT_BUF(addr
) = {};
4475 SPRINT_BUF(port
) = {};
4477 sprintf(addr
, "%u", node
);
4478 sprintf(port
, "%u", identity
);
4479 sock_addr_print(addr
, ":", port
, NULL
);
4483 static int tipc_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4485 struct rtattr
*stat
[TIPC_NLA_SOCK_STAT_MAX
+ 1] = {};
4486 struct rtattr
*attrs
[TIPC_NLA_SOCK_MAX
+ 1] = {};
4487 struct rtattr
*con
[TIPC_NLA_CON_MAX
+ 1] = {};
4488 struct rtattr
*info
[TIPC_NLA_MAX
+ 1] = {};
4489 struct rtattr
*msg_ref
;
4490 struct sockstat ss
= {};
4492 parse_rtattr(info
, TIPC_NLA_MAX
, NLMSG_DATA(nlh
),
4493 NLMSG_PAYLOAD(nlh
, 0));
4495 if (!info
[TIPC_NLA_SOCK
])
4498 msg_ref
= info
[TIPC_NLA_SOCK
];
4499 parse_rtattr(attrs
, TIPC_NLA_SOCK_MAX
, RTA_DATA(msg_ref
),
4500 RTA_PAYLOAD(msg_ref
));
4502 msg_ref
= attrs
[TIPC_NLA_SOCK_STAT
];
4503 parse_rtattr(stat
, TIPC_NLA_SOCK_STAT_MAX
,
4504 RTA_DATA(msg_ref
), RTA_PAYLOAD(msg_ref
));
4507 ss
.local
.family
= AF_TIPC
;
4508 ss
.type
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_TYPE
]);
4509 ss
.state
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_TIPC_STATE
]);
4510 ss
.uid
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_UID
]);
4511 ss
.ino
= rta_getattr_u32(attrs
[TIPC_NLA_SOCK_INO
]);
4512 ss
.rq
= rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_RCVQ
]);
4513 ss
.wq
= rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_SENDQ
]);
4514 ss
.sk
= rta_getattr_u64(attrs
[TIPC_NLA_SOCK_COOKIE
]);
4516 sock_state_print (&ss
);
4518 tipc_sock_addr_print(attrs
[TIPC_NLA_SOCK_ADDR
],
4519 attrs
[TIPC_NLA_SOCK_REF
]);
4521 msg_ref
= attrs
[TIPC_NLA_SOCK_CON
];
4523 parse_rtattr(con
, TIPC_NLA_CON_MAX
,
4524 RTA_DATA(msg_ref
), RTA_PAYLOAD(msg_ref
));
4526 tipc_sock_addr_print(con
[TIPC_NLA_CON_NODE
],
4527 con
[TIPC_NLA_CON_SOCK
]);
4529 sock_addr_print("", "-", "", NULL
);
4532 sock_details_print(&ss
);
4534 proc_ctx_print(&ss
);
4536 if (show_tipcinfo
) {
4537 out("\n type:%s", stype_nameg
[ss
.type
]);
4539 stat
[TIPC_NLA_SOCK_STAT_LINK_CONG
] ? "link" :
4540 stat
[TIPC_NLA_SOCK_STAT_CONN_CONG
] ? "conn" : "none");
4542 rta_getattr_u32(stat
[TIPC_NLA_SOCK_STAT_DROP
]));
4544 if (attrs
[TIPC_NLA_SOCK_HAS_PUBL
])
4547 if (con
[TIPC_NLA_CON_FLAG
])
4548 out(" via {%u,%u} ",
4549 rta_getattr_u32(con
[TIPC_NLA_CON_TYPE
]),
4550 rta_getattr_u32(con
[TIPC_NLA_CON_INST
]));
4556 static int tipc_show(struct filter
*f
)
4558 DIAG_REQUEST(req
, struct tipc_sock_diag_req r
);
4560 memset(&req
.r
, 0, sizeof(req
.r
));
4561 req
.r
.sdiag_family
= AF_TIPC
;
4562 req
.r
.tidiag_states
= f
->states
;
4564 return handle_netlink_request(f
, &req
.nlh
, sizeof(req
), tipc_show_sock
);
4567 struct sock_diag_msg
{
4571 static int generic_show_sock(struct nlmsghdr
*nlh
, void *arg
)
4573 struct sock_diag_msg
*r
= NLMSG_DATA(nlh
);
4574 struct inet_diag_arg inet_arg
= { .f
= arg
, .protocol
= IPPROTO_MAX
};
4577 switch (r
->sdiag_family
) {
4580 inet_arg
.rth
= inet_arg
.f
->rth_for_killing
;
4581 ret
= show_one_inet_sock(nlh
, &inet_arg
);
4584 ret
= unix_show_sock(nlh
, arg
);
4587 ret
= packet_show_sock(nlh
, arg
);
4590 ret
= netlink_show_sock(nlh
, arg
);
4593 ret
= vsock_show_sock(nlh
, arg
);
4596 ret
= xdp_show_sock(nlh
, arg
);
4607 static int handle_follow_request(struct filter
*f
)
4611 struct rtnl_handle rth
, rth2
;
4613 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << TCP_DB
))
4614 groups
|= 1 << (SKNLGRP_INET_TCP_DESTROY
- 1);
4615 if (f
->families
& FAMILY_MASK(AF_INET
) && f
->dbs
& (1 << UDP_DB
))
4616 groups
|= 1 << (SKNLGRP_INET_UDP_DESTROY
- 1);
4617 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << TCP_DB
))
4618 groups
|= 1 << (SKNLGRP_INET6_TCP_DESTROY
- 1);
4619 if (f
->families
& FAMILY_MASK(AF_INET6
) && f
->dbs
& (1 << UDP_DB
))
4620 groups
|= 1 << (SKNLGRP_INET6_UDP_DESTROY
- 1);
4625 if (rtnl_open_byproto(&rth
, groups
, NETLINK_SOCK_DIAG
))
4629 rth
.local
.nl_pid
= 0;
4632 if (rtnl_open_byproto(&rth2
, groups
, NETLINK_SOCK_DIAG
)) {
4636 f
->rth_for_killing
= &rth2
;
4639 if (rtnl_dump_filter(&rth
, generic_show_sock
, f
))
4643 if (f
->rth_for_killing
)
4644 rtnl_close(f
->rth_for_killing
);
4648 static int get_snmp_int(char *proto
, char *key
, int *result
)
4652 int protolen
= strlen(proto
);
4653 int keylen
= strlen(key
);
4657 if ((fp
= net_snmp_open()) == NULL
)
4660 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
4664 if (memcmp(buf
, proto
, protolen
))
4666 while ((p
= strchr(p
, ' ')) != NULL
) {
4669 if (memcmp(p
, key
, keylen
) == 0 &&
4670 (p
[keylen
] == ' ' || p
[keylen
] == '\n'))
4673 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
4675 if (memcmp(buf
, proto
, protolen
))
4678 while ((p
= strchr(p
, ' ')) != NULL
) {
4681 sscanf(p
, "%d", result
);
4694 /* Get stats from sockstat */
4714 static void get_sockstat_line(char *line
, struct ssummary
*s
)
4716 char id
[256], rem
[256];
4718 if (sscanf(line
, "%[^ ] %[^\n]\n", id
, rem
) != 2)
4721 if (strcmp(id
, "sockets:") == 0)
4722 sscanf(rem
, "%*s%d", &s
->socks
);
4723 else if (strcmp(id
, "UDP:") == 0)
4724 sscanf(rem
, "%*s%d", &s
->udp4
);
4725 else if (strcmp(id
, "UDP6:") == 0)
4726 sscanf(rem
, "%*s%d", &s
->udp6
);
4727 else if (strcmp(id
, "RAW:") == 0)
4728 sscanf(rem
, "%*s%d", &s
->raw4
);
4729 else if (strcmp(id
, "RAW6:") == 0)
4730 sscanf(rem
, "%*s%d", &s
->raw6
);
4731 else if (strcmp(id
, "TCP6:") == 0)
4732 sscanf(rem
, "%*s%d", &s
->tcp6_hashed
);
4733 else if (strcmp(id
, "FRAG:") == 0)
4734 sscanf(rem
, "%*s%d%*s%d", &s
->frag4
, &s
->frag4_mem
);
4735 else if (strcmp(id
, "FRAG6:") == 0)
4736 sscanf(rem
, "%*s%d%*s%d", &s
->frag6
, &s
->frag6_mem
);
4737 else if (strcmp(id
, "TCP:") == 0)
4738 sscanf(rem
, "%*s%d%*s%d%*s%d%*s%d%*s%d",
4740 &s
->tcp_orphans
, &s
->tcp_tws
, &s
->tcp_total
, &s
->tcp_mem
);
4743 static int get_sockstat(struct ssummary
*s
)
4748 memset(s
, 0, sizeof(*s
));
4750 if ((fp
= net_sockstat_open()) == NULL
)
4752 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4753 get_sockstat_line(buf
, s
);
4756 if ((fp
= net_sockstat6_open()) == NULL
)
4758 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
4759 get_sockstat_line(buf
, s
);
4765 static int print_summary(void)
4770 if (get_sockstat(&s
) < 0)
4771 perror("ss: get_sockstat");
4772 if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab
) < 0)
4773 perror("ss: get_snmpstat");
4775 printf("Total: %d\n", s
.socks
);
4777 printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n",
4778 s
.tcp_total
+ s
.tcp_tws
, tcp_estab
,
4779 s
.tcp_total
- (s
.tcp4_hashed
+ s
.tcp6_hashed
- s
.tcp_tws
),
4780 s
.tcp_orphans
, s
.tcp_tws
);
4783 printf("Transport Total IP IPv6\n");
4784 printf("RAW %-9d %-9d %-9d\n", s
.raw4
+s
.raw6
, s
.raw4
, s
.raw6
);
4785 printf("UDP %-9d %-9d %-9d\n", s
.udp4
+s
.udp6
, s
.udp4
, s
.udp6
);
4786 printf("TCP %-9d %-9d %-9d\n", s
.tcp4_hashed
+s
.tcp6_hashed
, s
.tcp4_hashed
, s
.tcp6_hashed
);
4787 printf("INET %-9d %-9d %-9d\n",
4788 s
.raw4
+s
.udp4
+s
.tcp4_hashed
+
4789 s
.raw6
+s
.udp6
+s
.tcp6_hashed
,
4790 s
.raw4
+s
.udp4
+s
.tcp4_hashed
,
4791 s
.raw6
+s
.udp6
+s
.tcp6_hashed
);
4792 printf("FRAG %-9d %-9d %-9d\n", s
.frag4
+s
.frag6
, s
.frag4
, s
.frag6
);
4799 static void _usage(FILE *dest
)
4802 "Usage: ss [ OPTIONS ]\n"
4803 " ss [ OPTIONS ] [ FILTER ]\n"
4804 " -h, --help this message\n"
4805 " -V, --version output version information\n"
4806 " -n, --numeric don't resolve service names\n"
4807 " -r, --resolve resolve host names\n"
4808 " -a, --all display all sockets\n"
4809 " -l, --listening display listening sockets\n"
4810 " -o, --options show timer information\n"
4811 " -e, --extended show detailed socket information\n"
4812 " -m, --memory show socket memory usage\n"
4813 " -p, --processes show process using socket\n"
4814 " -i, --info show internal TCP information\n"
4815 " --tipcinfo show internal tipc socket information\n"
4816 " -s, --summary show socket usage summary\n"
4817 " -b, --bpf show bpf filter socket information\n"
4818 " -E, --events continually display sockets as they are destroyed\n"
4819 " -Z, --context display process SELinux security contexts\n"
4820 " -z, --contexts display process and socket SELinux security contexts\n"
4821 " -N, --net switch to the specified network namespace name\n"
4823 " -4, --ipv4 display only IP version 4 sockets\n"
4824 " -6, --ipv6 display only IP version 6 sockets\n"
4825 " -0, --packet display PACKET sockets\n"
4826 " -t, --tcp display only TCP sockets\n"
4827 " -S, --sctp display only SCTP sockets\n"
4828 " -u, --udp display only UDP sockets\n"
4829 " -d, --dccp display only DCCP sockets\n"
4830 " -w, --raw display only RAW sockets\n"
4831 " -x, --unix display only Unix domain sockets\n"
4832 " --tipc display only TIPC sockets\n"
4833 " --vsock display only vsock sockets\n"
4834 " -f, --family=FAMILY display sockets of type FAMILY\n"
4835 " FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help}\n"
4837 " -K, --kill forcibly close sockets, display what was closed\n"
4838 " -H, --no-header Suppress header line\n"
4840 " -A, --query=QUERY, --socket=QUERY\n"
4841 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY]\n"
4843 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
4844 " -F, --filter=FILE read filter information from FILE\n"
4845 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
4846 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
4847 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n"
4848 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4849 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4850 " bucket := {syn-recv|time-wait}\n"
4851 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n"
4855 static void help(void) __attribute__((noreturn
));
4856 static void help(void)
4862 static void usage(void) __attribute__((noreturn
));
4863 static void usage(void)
4870 static int scan_state(const char *state
)
4872 static const char * const sstate_namel
[] = {
4874 [SS_ESTABLISHED
] = "established",
4875 [SS_SYN_SENT
] = "syn-sent",
4876 [SS_SYN_RECV
] = "syn-recv",
4877 [SS_FIN_WAIT1
] = "fin-wait-1",
4878 [SS_FIN_WAIT2
] = "fin-wait-2",
4879 [SS_TIME_WAIT
] = "time-wait",
4880 [SS_CLOSE
] = "unconnected",
4881 [SS_CLOSE_WAIT
] = "close-wait",
4882 [SS_LAST_ACK
] = "last-ack",
4883 [SS_LISTEN
] = "listening",
4884 [SS_CLOSING
] = "closing",
4888 if (strcasecmp(state
, "close") == 0 ||
4889 strcasecmp(state
, "closed") == 0)
4890 return (1<<SS_CLOSE
);
4891 if (strcasecmp(state
, "syn-rcv") == 0)
4892 return (1<<SS_SYN_RECV
);
4893 if (strcasecmp(state
, "established") == 0)
4894 return (1<<SS_ESTABLISHED
);
4895 if (strcasecmp(state
, "all") == 0)
4897 if (strcasecmp(state
, "connected") == 0)
4898 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
));
4899 if (strcasecmp(state
, "synchronized") == 0)
4900 return SS_ALL
& ~((1<<SS_CLOSE
)|(1<<SS_LISTEN
)|(1<<SS_SYN_SENT
));
4901 if (strcasecmp(state
, "bucket") == 0)
4902 return (1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
);
4903 if (strcasecmp(state
, "big") == 0)
4904 return SS_ALL
& ~((1<<SS_SYN_RECV
)|(1<<SS_TIME_WAIT
));
4905 for (i
= 0; i
< SS_MAX
; i
++) {
4906 if (strcasecmp(state
, sstate_namel
[i
]) == 0)
4910 fprintf(stderr
, "ss: wrong state name: %s\n", state
);
4914 /* Values 'v' and 'V' are already used so a non-character is used */
4915 #define OPT_VSOCK 256
4917 /* Values of 't' are already used so a non-character is used */
4918 #define OPT_TIPCSOCK 257
4919 #define OPT_TIPCINFO 258
4921 /* Values of 'x' are already used so a non-character is used */
4922 #define OPT_XDPSOCK 259
4924 static const struct option long_opts
[] = {
4925 { "numeric", 0, 0, 'n' },
4926 { "resolve", 0, 0, 'r' },
4927 { "options", 0, 0, 'o' },
4928 { "extended", 0, 0, 'e' },
4929 { "memory", 0, 0, 'm' },
4930 { "info", 0, 0, 'i' },
4931 { "processes", 0, 0, 'p' },
4932 { "bpf", 0, 0, 'b' },
4933 { "events", 0, 0, 'E' },
4934 { "dccp", 0, 0, 'd' },
4935 { "tcp", 0, 0, 't' },
4936 { "sctp", 0, 0, 'S' },
4937 { "udp", 0, 0, 'u' },
4938 { "raw", 0, 0, 'w' },
4939 { "unix", 0, 0, 'x' },
4940 { "tipc", 0, 0, OPT_TIPCSOCK
},
4941 { "vsock", 0, 0, OPT_VSOCK
},
4942 { "all", 0, 0, 'a' },
4943 { "listening", 0, 0, 'l' },
4944 { "ipv4", 0, 0, '4' },
4945 { "ipv6", 0, 0, '6' },
4946 { "packet", 0, 0, '0' },
4947 { "family", 1, 0, 'f' },
4948 { "socket", 1, 0, 'A' },
4949 { "query", 1, 0, 'A' },
4950 { "summary", 0, 0, 's' },
4951 { "diag", 1, 0, 'D' },
4952 { "filter", 1, 0, 'F' },
4953 { "version", 0, 0, 'V' },
4954 { "help", 0, 0, 'h' },
4955 { "context", 0, 0, 'Z' },
4956 { "contexts", 0, 0, 'z' },
4957 { "net", 1, 0, 'N' },
4958 { "tipcinfo", 0, 0, OPT_TIPCINFO
},
4959 { "kill", 0, 0, 'K' },
4960 { "no-header", 0, 0, 'H' },
4961 { "xdp", 0, 0, OPT_XDPSOCK
},
4966 int main(int argc
, char *argv
[])
4971 const char *dump_tcpdiag
= NULL
;
4972 FILE *filter_fp
= NULL
;
4974 int state_filter
= 0;
4976 while ((ch
= getopt_long(argc
, argv
,
4977 "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KHS",
4978 long_opts
, NULL
)) != EOF
) {
4981 resolve_services
= 0;
5001 user_ent_hash_build();
5011 filter_db_set(¤t_filter
, DCCP_DB
, true);
5014 filter_db_set(¤t_filter
, TCP_DB
, true);
5017 filter_db_set(¤t_filter
, SCTP_DB
, true);
5020 filter_db_set(¤t_filter
, UDP_DB
, true);
5023 filter_db_set(¤t_filter
, RAW_DB
, true);
5026 filter_af_set(¤t_filter
, AF_UNIX
);
5029 filter_af_set(¤t_filter
, AF_VSOCK
);
5032 filter_af_set(¤t_filter
, AF_TIPC
);
5035 state_filter
= SS_ALL
;
5038 state_filter
= (1 << SS_LISTEN
) | (1 << SS_CLOSE
);
5041 filter_af_set(¤t_filter
, AF_INET
);
5044 filter_af_set(¤t_filter
, AF_INET6
);
5047 filter_af_set(¤t_filter
, AF_PACKET
);
5050 filter_af_set(¤t_filter
, AF_XDP
);
5053 if (strcmp(optarg
, "inet") == 0)
5054 filter_af_set(¤t_filter
, AF_INET
);
5055 else if (strcmp(optarg
, "inet6") == 0)
5056 filter_af_set(¤t_filter
, AF_INET6
);
5057 else if (strcmp(optarg
, "link") == 0)
5058 filter_af_set(¤t_filter
, AF_PACKET
);
5059 else if (strcmp(optarg
, "unix") == 0)
5060 filter_af_set(¤t_filter
, AF_UNIX
);
5061 else if (strcmp(optarg
, "netlink") == 0)
5062 filter_af_set(¤t_filter
, AF_NETLINK
);
5063 else if (strcmp(optarg
, "tipc") == 0)
5064 filter_af_set(¤t_filter
, AF_TIPC
);
5065 else if (strcmp(optarg
, "vsock") == 0)
5066 filter_af_set(¤t_filter
, AF_VSOCK
);
5067 else if (strcmp(optarg
, "xdp") == 0)
5068 filter_af_set(¤t_filter
, AF_XDP
);
5069 else if (strcmp(optarg
, "help") == 0)
5072 fprintf(stderr
, "ss: \"%s\" is invalid family\n",
5082 current_filter
.dbs
= 0;
5083 state_filter
= state_filter
?
5084 state_filter
: SS_CONN
;
5090 if ((p1
= strchr(p
, ',')) != NULL
)
5092 if (filter_db_parse(¤t_filter
, p
)) {
5093 fprintf(stderr
, "ss: \"%s\" is illegal socket table id\n", p
);
5104 dump_tcpdiag
= optarg
;
5108 fprintf(stderr
, "More than one filter file\n");
5111 if (optarg
[0] == '-')
5114 filter_fp
= fopen(optarg
, "r");
5116 perror("fopen filter file");
5122 printf("ss utility, iproute2-ss%s\n", SNAPSHOT
);
5128 if (is_selinux_enabled() <= 0) {
5129 fprintf(stderr
, "ss: SELinux is not enabled.\n");
5133 user_ent_hash_build();
5136 if (netns_switch(optarg
))
5143 current_filter
.kill
= 1;
5161 if (do_default
&& argc
== 0)
5166 if (strcmp(*argv
, "state") == 0) {
5170 state_filter
|= scan_state(*argv
);
5172 } else if (strcmp(*argv
, "exclude") == 0 ||
5173 strcmp(*argv
, "excl") == 0) {
5176 state_filter
= SS_ALL
;
5177 state_filter
&= ~scan_state(*argv
);
5186 state_filter
= state_filter
? state_filter
: SS_CONN
;
5187 filter_db_parse(¤t_filter
, "all");
5190 filter_states_set(¤t_filter
, state_filter
);
5191 filter_merge_defaults(¤t_filter
);
5193 if (resolve_services
&& resolve_hosts
&&
5194 (current_filter
.dbs
& (UNIX_DBM
|INET_L4_DBM
)))
5195 init_service_resolver();
5197 if (current_filter
.dbs
== 0) {
5198 fprintf(stderr
, "ss: no socket tables to show with such filter.\n");
5201 if (current_filter
.families
== 0) {
5202 fprintf(stderr
, "ss: no families to show with such filter.\n");
5205 if (current_filter
.states
== 0) {
5206 fprintf(stderr
, "ss: no socket states to show with such filter.\n");
5211 FILE *dump_fp
= stdout
;
5213 if (!(current_filter
.dbs
& (1<<TCP_DB
))) {
5214 fprintf(stderr
, "ss: tcpdiag dump requested and no tcp in filter.\n");
5217 if (dump_tcpdiag
[0] != '-') {
5218 dump_fp
= fopen(dump_tcpdiag
, "w");
5219 if (!dump_tcpdiag
) {
5220 perror("fopen dump file");
5224 inet_show_netlink(¤t_filter
, dump_fp
, IPPROTO_TCP
);
5229 if (ssfilter_parse(¤t_filter
.f
, argc
, argv
, filter_fp
))
5232 if (!(current_filter
.dbs
& (current_filter
.dbs
- 1)))
5233 columns
[COL_NETID
].disabled
= 1;
5235 if (!(current_filter
.states
& (current_filter
.states
- 1)))
5236 columns
[COL_STATE
].disabled
= 1;
5244 exit(handle_follow_request(¤t_filter
));
5246 if (current_filter
.dbs
& (1<<NETLINK_DB
))
5247 netlink_show(¤t_filter
);
5248 if (current_filter
.dbs
& PACKET_DBM
)
5249 packet_show(¤t_filter
);
5250 if (current_filter
.dbs
& UNIX_DBM
)
5251 unix_show(¤t_filter
);
5252 if (current_filter
.dbs
& (1<<RAW_DB
))
5253 raw_show(¤t_filter
);
5254 if (current_filter
.dbs
& (1<<UDP_DB
))
5255 udp_show(¤t_filter
);
5256 if (current_filter
.dbs
& (1<<TCP_DB
))
5257 tcp_show(¤t_filter
);
5258 if (current_filter
.dbs
& (1<<DCCP_DB
))
5259 dccp_show(¤t_filter
);
5260 if (current_filter
.dbs
& (1<<SCTP_DB
))
5261 sctp_show(¤t_filter
);
5262 if (current_filter
.dbs
& VSOCK_DBM
)
5263 vsock_show(¤t_filter
);
5264 if (current_filter
.dbs
& (1<<TIPC_DB
))
5265 tipc_show(¤t_filter
);
5266 if (current_filter
.dbs
& (1<<XDP_DB
))
5267 xdp_show(¤t_filter
);
5269 if (show_users
|| show_proc_ctx
|| show_sock_ctx
)