Blame misc/ss.c

Packit d3f73b
/*
Packit d3f73b
 * ss.c		"sockstat", socket statistics
Packit d3f73b
 *
Packit d3f73b
 *		This program is free software; you can redistribute it and/or
Packit d3f73b
 *		modify it under the terms of the GNU General Public License
Packit d3f73b
 *		as published by the Free Software Foundation; either version
Packit d3f73b
 *		2 of the License, or (at your option) any later version.
Packit d3f73b
 *
Packit d3f73b
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Packit d3f73b
 */
Packit d3f73b
Packit d3f73b
#include <stdio.h>
Packit d3f73b
#include <stdlib.h>
Packit d3f73b
#include <unistd.h>
Packit d3f73b
#include <fcntl.h>
Packit d3f73b
#include <sys/ioctl.h>
Packit d3f73b
#include <sys/socket.h>
Packit d3f73b
#include <sys/uio.h>
Packit d3f73b
#include <sys/sysmacros.h>
Packit d3f73b
#include <netinet/in.h>
Packit d3f73b
#include <string.h>
Packit d3f73b
#include <errno.h>
Packit d3f73b
#include <netdb.h>
Packit d3f73b
#include <arpa/inet.h>
Packit d3f73b
#include <dirent.h>
Packit d3f73b
#include <fnmatch.h>
Packit d3f73b
#include <getopt.h>
Packit d3f73b
#include <stdbool.h>
Packit d3f73b
#include <limits.h>
Packit d3f73b
#include <stdarg.h>
Packit d3f73b
Packit d3f73b
#include "utils.h"
Packit d3f73b
#include "rt_names.h"
Packit d3f73b
#include "ll_map.h"
Packit d3f73b
#include "libnetlink.h"
Packit d3f73b
#include "namespace.h"
Packit d3f73b
#include "SNAPSHOT.h"
Packit d3f73b
Packit d3f73b
#include <linux/tcp.h>
Packit d3f73b
#include <linux/sock_diag.h>
Packit d3f73b
#include <linux/inet_diag.h>
Packit d3f73b
#include <linux/unix_diag.h>
Packit d3f73b
#include <linux/netdevice.h>	/* for MAX_ADDR_LEN */
Packit d3f73b
#include <linux/filter.h>
Packit d3f73b
#include <linux/xdp_diag.h>
Packit d3f73b
#include <linux/packet_diag.h>
Packit d3f73b
#include <linux/netlink_diag.h>
Packit d3f73b
#include <linux/sctp.h>
Packit d3f73b
#include <linux/vm_sockets_diag.h>
Packit d3f73b
#include <linux/net.h>
Packit d3f73b
#include <linux/tipc.h>
Packit d3f73b
#include <linux/tipc_netlink.h>
Packit d3f73b
#include <linux/tipc_sockets_diag.h>
Packit Service 728d12
#include <linux/tls.h>
Packit Service 8e2ce8
#include <linux/mptcp.h>
Packit d3f73b
Packit d3f73b
/* AF_VSOCK/PF_VSOCK is only provided since glibc 2.18 */
Packit d3f73b
#ifndef PF_VSOCK
Packit d3f73b
#define PF_VSOCK 40
Packit d3f73b
#endif
Packit d3f73b
#ifndef AF_VSOCK
Packit d3f73b
#define AF_VSOCK PF_VSOCK
Packit d3f73b
#endif
Packit d3f73b
Packit d3f73b
#define MAGIC_SEQ 123456
Packit d3f73b
#define BUF_CHUNK (1024 * 1024)	/* Buffer chunk allocation size */
Packit d3f73b
#define BUF_CHUNKS_MAX 5	/* Maximum number of allocated buffer chunks */
Packit d3f73b
#define LEN_ALIGN(x) (((x) + 1) & ~1)
Packit d3f73b
Packit d3f73b
#define DIAG_REQUEST(_req, _r)						    \
Packit d3f73b
	struct {							    \
Packit d3f73b
		struct nlmsghdr nlh;					    \
Packit d3f73b
		_r;							    \
Packit d3f73b
	} _req = {							    \
Packit d3f73b
		.nlh = {						    \
Packit d3f73b
			.nlmsg_type = SOCK_DIAG_BY_FAMILY,		    \
Packit d3f73b
			.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
Packit d3f73b
			.nlmsg_seq = MAGIC_SEQ,				    \
Packit d3f73b
			.nlmsg_len = sizeof(_req),			    \
Packit d3f73b
		},							    \
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
#if HAVE_SELINUX
Packit d3f73b
#include <selinux/selinux.h>
Packit d3f73b
#else
Packit d3f73b
/* Stubs for SELinux functions */
Packit d3f73b
static int is_selinux_enabled(void)
Packit d3f73b
{
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int getpidcon(pid_t pid, char **context)
Packit d3f73b
{
Packit d3f73b
	*context = NULL;
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int getfilecon(char *path, char **context)
Packit d3f73b
{
Packit d3f73b
	*context = NULL;
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int security_get_initial_context(char *name,  char **context)
Packit d3f73b
{
Packit d3f73b
	*context = NULL;
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
#endif
Packit d3f73b
Packit d3f73b
int preferred_family = AF_UNSPEC;
Packit d3f73b
static int show_options;
Packit d3f73b
int show_details;
Packit d3f73b
static int show_users;
Packit d3f73b
static int show_mem;
Packit d3f73b
static int show_tcpinfo;
Packit d3f73b
static int show_bpf;
Packit d3f73b
static int show_proc_ctx;
Packit d3f73b
static int show_sock_ctx;
Packit d3f73b
static int show_header = 1;
Packit d3f73b
static int follow_events;
Packit d3f73b
static int sctp_ino;
Packit d3f73b
static int show_tipcinfo;
Packit d3f73b
static int show_tos;
Packit d3f73b
int numeric;
Packit d3f73b
int oneline;
Packit d3f73b
Packit d3f73b
enum col_id {
Packit d3f73b
	COL_NETID,
Packit d3f73b
	COL_STATE,
Packit d3f73b
	COL_RECVQ,
Packit d3f73b
	COL_SENDQ,
Packit d3f73b
	COL_ADDR,
Packit d3f73b
	COL_SERV,
Packit d3f73b
	COL_RADDR,
Packit d3f73b
	COL_RSERV,
Packit d3f73b
	COL_EXT,
Packit d3f73b
	COL_MAX
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
enum col_align {
Packit d3f73b
	ALIGN_LEFT,
Packit d3f73b
	ALIGN_CENTER,
Packit d3f73b
	ALIGN_RIGHT
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
struct column {
Packit d3f73b
	const enum col_align align;
Packit d3f73b
	const char *header;
Packit d3f73b
	const char *ldelim;
Packit d3f73b
	int disabled;
Packit d3f73b
	int width;	/* Calculated, including additional layout spacing */
Packit d3f73b
	int max_len;	/* Measured maximum field length in this column */
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static struct column columns[] = {
Packit d3f73b
	{ ALIGN_LEFT,	"Netid",		"",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"State",		" ",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"Recv-Q",		" ",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"Send-Q",		" ",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_RIGHT,	"Local Address:",	" ",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"Port",			"",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_RIGHT,	"Peer Address:",	" ",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"Port",			"",	0, 0, 0 },
Packit d3f73b
	{ ALIGN_LEFT,	"",			"",	0, 0, 0 },
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static struct column *current_field = columns;
Packit d3f73b
Packit d3f73b
/* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to
Packit d3f73b
 * the buffer as a variable size token. A token consists of a 16 bits length
Packit d3f73b
 * field, followed by a string which is not NULL-terminated.
Packit d3f73b
 *
Packit d3f73b
 * A new chunk is allocated and linked when the current chunk doesn't have
Packit d3f73b
 * enough room to store the current token as a whole.
Packit d3f73b
 */
Packit d3f73b
struct buf_chunk {
Packit d3f73b
	struct buf_chunk *next;	/* Next chained chunk */
Packit d3f73b
	char *end;		/* Current end of content */
Packit d3f73b
	char data[0];
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
struct buf_token {
Packit d3f73b
	uint16_t len;		/* Data length, excluding length descriptor */
Packit d3f73b
	char data[0];
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static struct {
Packit d3f73b
	struct buf_token *cur;	/* Position of current token in chunk */
Packit d3f73b
	struct buf_chunk *head;	/* First chunk */
Packit d3f73b
	struct buf_chunk *tail;	/* Current chunk */
Packit d3f73b
	int chunks;		/* Number of allocated chunks */
Packit d3f73b
} buffer;
Packit d3f73b
Packit d3f73b
static const char *TCP_PROTO = "tcp";
Packit d3f73b
static const char *SCTP_PROTO = "sctp";
Packit d3f73b
static const char *UDP_PROTO = "udp";
Packit d3f73b
static const char *RAW_PROTO = "raw";
Packit d3f73b
static const char *dg_proto;
Packit d3f73b
Packit d3f73b
enum {
Packit d3f73b
	TCP_DB,
Packit d3f73b
	DCCP_DB,
Packit d3f73b
	UDP_DB,
Packit d3f73b
	RAW_DB,
Packit d3f73b
	UNIX_DG_DB,
Packit d3f73b
	UNIX_ST_DB,
Packit d3f73b
	UNIX_SQ_DB,
Packit d3f73b
	PACKET_DG_DB,
Packit d3f73b
	PACKET_R_DB,
Packit d3f73b
	NETLINK_DB,
Packit d3f73b
	SCTP_DB,
Packit d3f73b
	VSOCK_ST_DB,
Packit d3f73b
	VSOCK_DG_DB,
Packit d3f73b
	TIPC_DB,
Packit d3f73b
	XDP_DB,
Packit d3f73b
	MAX_DB
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
#define PACKET_DBM ((1<
Packit d3f73b
#define UNIX_DBM ((1<
Packit d3f73b
#define ALL_DB ((1<
Packit d3f73b
#define INET_L4_DBM ((1<
Packit d3f73b
#define INET_DBM (INET_L4_DBM | (1<
Packit d3f73b
#define VSOCK_DBM ((1<
Packit d3f73b
Packit d3f73b
enum {
Packit d3f73b
	SS_UNKNOWN,
Packit d3f73b
	SS_ESTABLISHED,
Packit d3f73b
	SS_SYN_SENT,
Packit d3f73b
	SS_SYN_RECV,
Packit d3f73b
	SS_FIN_WAIT1,
Packit d3f73b
	SS_FIN_WAIT2,
Packit d3f73b
	SS_TIME_WAIT,
Packit d3f73b
	SS_CLOSE,
Packit d3f73b
	SS_CLOSE_WAIT,
Packit d3f73b
	SS_LAST_ACK,
Packit d3f73b
	SS_LISTEN,
Packit d3f73b
	SS_CLOSING,
Packit d3f73b
	SS_MAX
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
enum {
Packit d3f73b
	SCTP_STATE_CLOSED		= 0,
Packit d3f73b
	SCTP_STATE_COOKIE_WAIT		= 1,
Packit d3f73b
	SCTP_STATE_COOKIE_ECHOED	= 2,
Packit d3f73b
	SCTP_STATE_ESTABLISHED		= 3,
Packit d3f73b
	SCTP_STATE_SHUTDOWN_PENDING	= 4,
Packit d3f73b
	SCTP_STATE_SHUTDOWN_SENT	= 5,
Packit d3f73b
	SCTP_STATE_SHUTDOWN_RECEIVED	= 6,
Packit d3f73b
	SCTP_STATE_SHUTDOWN_ACK_SENT	= 7,
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
#define SS_ALL ((1 << SS_MAX) - 1)
Packit d3f73b
#define SS_CONN (SS_ALL & ~((1<
Packit d3f73b
#define TIPC_SS_CONN ((1<
Packit d3f73b
Packit d3f73b
#include "ssfilter.h"
Packit d3f73b
Packit d3f73b
struct filter {
Packit d3f73b
	int dbs;
Packit d3f73b
	int states;
Packit d3f73b
	uint64_t families;
Packit d3f73b
	struct ssfilter *f;
Packit d3f73b
	bool kill;
Packit d3f73b
	struct rtnl_handle *rth_for_killing;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
#define FAMILY_MASK(family) ((uint64_t)1 << (family))
Packit d3f73b
Packit d3f73b
static const struct filter default_dbs[MAX_DB] = {
Packit d3f73b
	[TCP_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
Packit d3f73b
	},
Packit d3f73b
	[DCCP_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
Packit d3f73b
	},
Packit d3f73b
	[UDP_DB] = {
Packit d3f73b
		.states   = (1 << SS_ESTABLISHED),
Packit d3f73b
		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
Packit d3f73b
	},
Packit d3f73b
	[RAW_DB] = {
Packit d3f73b
		.states   = (1 << SS_ESTABLISHED),
Packit d3f73b
		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
Packit d3f73b
	},
Packit d3f73b
	[UNIX_DG_DB] = {
Packit d3f73b
		.states   = (1 << SS_CLOSE),
Packit d3f73b
		.families = FAMILY_MASK(AF_UNIX),
Packit d3f73b
	},
Packit d3f73b
	[UNIX_ST_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_UNIX),
Packit d3f73b
	},
Packit d3f73b
	[UNIX_SQ_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_UNIX),
Packit d3f73b
	},
Packit d3f73b
	[PACKET_DG_DB] = {
Packit d3f73b
		.states   = (1 << SS_CLOSE),
Packit d3f73b
		.families = FAMILY_MASK(AF_PACKET),
Packit d3f73b
	},
Packit d3f73b
	[PACKET_R_DB] = {
Packit d3f73b
		.states   = (1 << SS_CLOSE),
Packit d3f73b
		.families = FAMILY_MASK(AF_PACKET),
Packit d3f73b
	},
Packit d3f73b
	[NETLINK_DB] = {
Packit d3f73b
		.states   = (1 << SS_CLOSE),
Packit d3f73b
		.families = FAMILY_MASK(AF_NETLINK),
Packit d3f73b
	},
Packit d3f73b
	[SCTP_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
Packit d3f73b
	},
Packit d3f73b
	[VSOCK_ST_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_VSOCK),
Packit d3f73b
	},
Packit d3f73b
	[VSOCK_DG_DB] = {
Packit d3f73b
		.states   = SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_VSOCK),
Packit d3f73b
	},
Packit d3f73b
	[TIPC_DB] = {
Packit d3f73b
		.states   = TIPC_SS_CONN,
Packit d3f73b
		.families = FAMILY_MASK(AF_TIPC),
Packit d3f73b
	},
Packit d3f73b
	[XDP_DB] = {
Packit d3f73b
		.states   = (1 << SS_CLOSE),
Packit d3f73b
		.families = FAMILY_MASK(AF_XDP),
Packit d3f73b
	},
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static const struct filter default_afs[AF_MAX] = {
Packit d3f73b
	[AF_INET] = {
Packit d3f73b
		.dbs    = INET_DBM,
Packit d3f73b
		.states = SS_CONN,
Packit d3f73b
	},
Packit d3f73b
	[AF_INET6] = {
Packit d3f73b
		.dbs    = INET_DBM,
Packit d3f73b
		.states = SS_CONN,
Packit d3f73b
	},
Packit d3f73b
	[AF_UNIX] = {
Packit d3f73b
		.dbs    = UNIX_DBM,
Packit d3f73b
		.states = SS_CONN,
Packit d3f73b
	},
Packit d3f73b
	[AF_PACKET] = {
Packit d3f73b
		.dbs    = PACKET_DBM,
Packit d3f73b
		.states = (1 << SS_CLOSE),
Packit d3f73b
	},
Packit d3f73b
	[AF_NETLINK] = {
Packit d3f73b
		.dbs    = (1 << NETLINK_DB),
Packit d3f73b
		.states = (1 << SS_CLOSE),
Packit d3f73b
	},
Packit d3f73b
	[AF_VSOCK] = {
Packit d3f73b
		.dbs    = VSOCK_DBM,
Packit d3f73b
		.states = SS_CONN,
Packit d3f73b
	},
Packit d3f73b
	[AF_TIPC] = {
Packit d3f73b
		.dbs    = (1 << TIPC_DB),
Packit d3f73b
		.states = TIPC_SS_CONN,
Packit d3f73b
	},
Packit d3f73b
	[AF_XDP] = {
Packit d3f73b
		.dbs    = (1 << XDP_DB),
Packit d3f73b
		.states = (1 << SS_CLOSE),
Packit d3f73b
	},
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static int do_default = 1;
Packit d3f73b
static struct filter current_filter;
Packit d3f73b
Packit d3f73b
static void filter_db_set(struct filter *f, int db, bool enable)
Packit d3f73b
{
Packit d3f73b
	if (enable) {
Packit d3f73b
		f->states   |= default_dbs[db].states;
Packit d3f73b
		f->dbs	    |= 1 << db;
Packit d3f73b
	} else {
Packit d3f73b
		f->dbs &= ~(1 << db);
Packit d3f73b
	}
Packit d3f73b
	do_default   = 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int filter_db_parse(struct filter *f, const char *s)
Packit d3f73b
{
Packit d3f73b
	const struct {
Packit d3f73b
		const char *name;
Packit d3f73b
		int dbs[MAX_DB + 1];
Packit d3f73b
	} db_name_tbl[] = {
Packit d3f73b
#define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } }
Packit d3f73b
		ENTRY(all, UDP_DB, DCCP_DB, TCP_DB, RAW_DB,
Packit d3f73b
			   UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB,
Packit d3f73b
			   PACKET_R_DB, PACKET_DG_DB, NETLINK_DB,
Packit d3f73b
			   SCTP_DB, VSOCK_ST_DB, VSOCK_DG_DB, XDP_DB),
Packit d3f73b
		ENTRY(inet, UDP_DB, DCCP_DB, TCP_DB, SCTP_DB, RAW_DB),
Packit d3f73b
		ENTRY(udp, UDP_DB),
Packit d3f73b
		ENTRY(dccp, DCCP_DB),
Packit d3f73b
		ENTRY(tcp, TCP_DB),
Packit d3f73b
		ENTRY(sctp, SCTP_DB),
Packit d3f73b
		ENTRY(raw, RAW_DB),
Packit d3f73b
		ENTRY(unix, UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB),
Packit d3f73b
		ENTRY(unix_stream, UNIX_ST_DB),
Packit d3f73b
		ENTRY(u_str, UNIX_ST_DB),	/* alias for unix_stream */
Packit d3f73b
		ENTRY(unix_dgram, UNIX_DG_DB),
Packit d3f73b
		ENTRY(u_dgr, UNIX_DG_DB),	/* alias for unix_dgram */
Packit d3f73b
		ENTRY(unix_seqpacket, UNIX_SQ_DB),
Packit d3f73b
		ENTRY(u_seq, UNIX_SQ_DB),	/* alias for unix_seqpacket */
Packit d3f73b
		ENTRY(packet, PACKET_R_DB, PACKET_DG_DB),
Packit d3f73b
		ENTRY(packet_raw, PACKET_R_DB),
Packit d3f73b
		ENTRY(p_raw, PACKET_R_DB),	/* alias for packet_raw */
Packit d3f73b
		ENTRY(packet_dgram, PACKET_DG_DB),
Packit d3f73b
		ENTRY(p_dgr, PACKET_DG_DB),	/* alias for packet_dgram */
Packit d3f73b
		ENTRY(netlink, NETLINK_DB),
Packit d3f73b
		ENTRY(vsock, VSOCK_ST_DB, VSOCK_DG_DB),
Packit d3f73b
		ENTRY(vsock_stream, VSOCK_ST_DB),
Packit d3f73b
		ENTRY(v_str, VSOCK_ST_DB),	/* alias for vsock_stream */
Packit d3f73b
		ENTRY(vsock_dgram, VSOCK_DG_DB),
Packit d3f73b
		ENTRY(v_dgr, VSOCK_DG_DB),	/* alias for vsock_dgram */
Packit d3f73b
		ENTRY(xdp, XDP_DB),
Packit d3f73b
#undef ENTRY
Packit d3f73b
	};
Packit d3f73b
	bool enable = true;
Packit d3f73b
	unsigned int i;
Packit d3f73b
	const int *dbp;
Packit d3f73b
Packit d3f73b
	if (s[0] == '!') {
Packit d3f73b
		enable = false;
Packit d3f73b
		s++;
Packit d3f73b
	}
Packit d3f73b
	for (i = 0; i < ARRAY_SIZE(db_name_tbl); i++) {
Packit d3f73b
		if (strcmp(s, db_name_tbl[i].name))
Packit d3f73b
			continue;
Packit d3f73b
		for (dbp = db_name_tbl[i].dbs; *dbp != MAX_DB; dbp++)
Packit d3f73b
			filter_db_set(f, *dbp, enable);
Packit d3f73b
		return 0;
Packit d3f73b
	}
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void filter_af_set(struct filter *f, int af)
Packit d3f73b
{
Packit d3f73b
	f->states	   |= default_afs[af].states;
Packit d3f73b
	f->families	   |= FAMILY_MASK(af);
Packit d3f73b
	do_default	    = 0;
Packit d3f73b
	preferred_family    = af;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int filter_af_get(struct filter *f, int af)
Packit d3f73b
{
Packit d3f73b
	return !!(f->families & FAMILY_MASK(af));
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void filter_states_set(struct filter *f, int states)
Packit d3f73b
{
Packit d3f73b
	if (states)
Packit d3f73b
		f->states = states;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void filter_merge_defaults(struct filter *f)
Packit d3f73b
{
Packit d3f73b
	int db;
Packit d3f73b
	int af;
Packit d3f73b
Packit d3f73b
	for (db = 0; db < MAX_DB; db++) {
Packit d3f73b
		if (!(f->dbs & (1 << db)))
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		if (!(default_dbs[db].families & f->families))
Packit d3f73b
			f->families |= default_dbs[db].families;
Packit d3f73b
	}
Packit d3f73b
	for (af = 0; af < AF_MAX; af++) {
Packit d3f73b
		if (!(f->families & FAMILY_MASK(af)))
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		if (!(default_afs[af].dbs & f->dbs))
Packit d3f73b
			f->dbs |= default_afs[af].dbs;
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static FILE *generic_proc_open(const char *env, const char *name)
Packit d3f73b
{
Packit d3f73b
	const char *p = getenv(env);
Packit d3f73b
	char store[128];
Packit d3f73b
Packit d3f73b
	if (!p) {
Packit d3f73b
		p = getenv("PROC_ROOT") ? : "/proc";
Packit d3f73b
		snprintf(store, sizeof(store)-1, "%s/%s", p, name);
Packit d3f73b
		p = store;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return fopen(p, "r");
Packit d3f73b
}
Packit d3f73b
#define net_tcp_open()		generic_proc_open("PROC_NET_TCP", "net/tcp")
Packit d3f73b
#define net_tcp6_open()		generic_proc_open("PROC_NET_TCP6", "net/tcp6")
Packit d3f73b
#define net_udp_open()		generic_proc_open("PROC_NET_UDP", "net/udp")
Packit d3f73b
#define net_udp6_open()		generic_proc_open("PROC_NET_UDP6", "net/udp6")
Packit d3f73b
#define net_raw_open()		generic_proc_open("PROC_NET_RAW", "net/raw")
Packit d3f73b
#define net_raw6_open()		generic_proc_open("PROC_NET_RAW6", "net/raw6")
Packit d3f73b
#define net_unix_open()		generic_proc_open("PROC_NET_UNIX", "net/unix")
Packit d3f73b
#define net_packet_open()	generic_proc_open("PROC_NET_PACKET", \
Packit d3f73b
							"net/packet")
Packit d3f73b
#define net_netlink_open()	generic_proc_open("PROC_NET_NETLINK", \
Packit d3f73b
							"net/netlink")
Packit d3f73b
#define net_sockstat_open()	generic_proc_open("PROC_NET_SOCKSTAT", \
Packit d3f73b
							"net/sockstat")
Packit d3f73b
#define net_sockstat6_open()	generic_proc_open("PROC_NET_SOCKSTAT6", \
Packit d3f73b
							"net/sockstat6")
Packit d3f73b
#define net_snmp_open()		generic_proc_open("PROC_NET_SNMP", "net/snmp")
Packit d3f73b
#define ephemeral_ports_open()	generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \
Packit d3f73b
					"sys/net/ipv4/ip_local_port_range")
Packit d3f73b
Packit d3f73b
struct user_ent {
Packit d3f73b
	struct user_ent	*next;
Packit d3f73b
	unsigned int	ino;
Packit d3f73b
	int		pid;
Packit d3f73b
	int		fd;
Packit d3f73b
	char		*process;
Packit d3f73b
	char		*process_ctx;
Packit d3f73b
	char		*socket_ctx;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
#define USER_ENT_HASH_SIZE	256
Packit d3f73b
static struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
Packit d3f73b
Packit d3f73b
static int user_ent_hashfn(unsigned int ino)
Packit d3f73b
{
Packit d3f73b
	int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
Packit d3f73b
Packit d3f73b
	return val & (USER_ENT_HASH_SIZE - 1);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void user_ent_add(unsigned int ino, char *process,
Packit d3f73b
					int pid, int fd,
Packit d3f73b
					char *proc_ctx,
Packit d3f73b
					char *sock_ctx)
Packit d3f73b
{
Packit d3f73b
	struct user_ent *p, **pp;
Packit d3f73b
Packit d3f73b
	p = malloc(sizeof(struct user_ent));
Packit d3f73b
	if (!p) {
Packit d3f73b
		fprintf(stderr, "ss: failed to malloc buffer\n");
Packit d3f73b
		abort();
Packit d3f73b
	}
Packit d3f73b
	p->next = NULL;
Packit d3f73b
	p->ino = ino;
Packit d3f73b
	p->pid = pid;
Packit d3f73b
	p->fd = fd;
Packit d3f73b
	p->process = strdup(process);
Packit d3f73b
	p->process_ctx = strdup(proc_ctx);
Packit d3f73b
	p->socket_ctx = strdup(sock_ctx);
Packit d3f73b
Packit d3f73b
	pp = &user_ent_hash[user_ent_hashfn(ino)];
Packit d3f73b
	p->next = *pp;
Packit d3f73b
	*pp = p;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void user_ent_destroy(void)
Packit d3f73b
{
Packit d3f73b
	struct user_ent *p, *p_next;
Packit d3f73b
	int cnt = 0;
Packit d3f73b
Packit d3f73b
	while (cnt != USER_ENT_HASH_SIZE) {
Packit d3f73b
		p = user_ent_hash[cnt];
Packit d3f73b
		while (p) {
Packit d3f73b
			free(p->process);
Packit d3f73b
			free(p->process_ctx);
Packit d3f73b
			free(p->socket_ctx);
Packit d3f73b
			p_next = p->next;
Packit d3f73b
			free(p);
Packit d3f73b
			p = p_next;
Packit d3f73b
		}
Packit d3f73b
		cnt++;
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void user_ent_hash_build(void)
Packit d3f73b
{
Packit d3f73b
	const char *root = getenv("PROC_ROOT") ? : "/proc/";
Packit d3f73b
	struct dirent *d;
Packit d3f73b
	char name[1024];
Packit d3f73b
	int nameoff;
Packit d3f73b
	DIR *dir;
Packit d3f73b
	char *pid_context;
Packit d3f73b
	char *sock_context;
Packit d3f73b
	const char *no_ctx = "unavailable";
Packit d3f73b
	static int user_ent_hash_build_init;
Packit d3f73b
Packit d3f73b
	/* If show_users & show_proc_ctx set only do this once */
Packit d3f73b
	if (user_ent_hash_build_init != 0)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	user_ent_hash_build_init = 1;
Packit d3f73b
Packit d3f73b
	strlcpy(name, root, sizeof(name));
Packit d3f73b
Packit d3f73b
	if (strlen(name) == 0 || name[strlen(name)-1] != '/')
Packit d3f73b
		strcat(name, "/");
Packit d3f73b
Packit d3f73b
	nameoff = strlen(name);
Packit d3f73b
Packit d3f73b
	dir = opendir(name);
Packit d3f73b
	if (!dir)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	while ((d = readdir(dir)) != NULL) {
Packit d3f73b
		struct dirent *d1;
Packit d3f73b
		char process[16];
Packit d3f73b
		char *p;
Packit d3f73b
		int pid, pos;
Packit d3f73b
		DIR *dir1;
Packit d3f73b
		char crap;
Packit d3f73b
Packit d3f73b
		if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		if (getpidcon(pid, &pid_context) != 0)
Packit d3f73b
			pid_context = strdup(no_ctx);
Packit d3f73b
Packit d3f73b
		snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
Packit d3f73b
		pos = strlen(name);
Packit d3f73b
		if ((dir1 = opendir(name)) == NULL) {
Packit d3f73b
			free(pid_context);
Packit d3f73b
			continue;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		process[0] = '\0';
Packit d3f73b
		p = process;
Packit d3f73b
Packit d3f73b
		while ((d1 = readdir(dir1)) != NULL) {
Packit d3f73b
			const char *pattern = "socket:[";
Packit d3f73b
			unsigned int ino;
Packit d3f73b
			char lnk[64];
Packit d3f73b
			int fd;
Packit d3f73b
			ssize_t link_len;
Packit d3f73b
			char tmp[1024];
Packit d3f73b
Packit d3f73b
			if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
Packit d3f73b
				continue;
Packit d3f73b
Packit d3f73b
			snprintf(name+pos, sizeof(name) - pos, "%d", fd);
Packit d3f73b
Packit d3f73b
			link_len = readlink(name, lnk, sizeof(lnk)-1);
Packit d3f73b
			if (link_len == -1)
Packit d3f73b
				continue;
Packit d3f73b
			lnk[link_len] = '\0';
Packit d3f73b
Packit d3f73b
			if (strncmp(lnk, pattern, strlen(pattern)))
Packit d3f73b
				continue;
Packit d3f73b
Packit d3f73b
			sscanf(lnk, "socket:[%u]", &ino);
Packit d3f73b
Packit d3f73b
			snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
Packit d3f73b
					root, pid, d1->d_name);
Packit d3f73b
Packit d3f73b
			if (getfilecon(tmp, &sock_context) <= 0)
Packit d3f73b
				sock_context = strdup(no_ctx);
Packit d3f73b
Packit d3f73b
			if (*p == '\0') {
Packit d3f73b
				FILE *fp;
Packit d3f73b
Packit d3f73b
				snprintf(tmp, sizeof(tmp), "%s/%d/stat",
Packit d3f73b
					root, pid);
Packit d3f73b
				if ((fp = fopen(tmp, "r")) != NULL) {
Packit d3f73b
					if (fscanf(fp, "%*d (%[^)])", p) < 1)
Packit d3f73b
						; /* ignore */
Packit d3f73b
					fclose(fp);
Packit d3f73b
				}
Packit d3f73b
			}
Packit d3f73b
			user_ent_add(ino, p, pid, fd,
Packit d3f73b
					pid_context, sock_context);
Packit d3f73b
			free(sock_context);
Packit d3f73b
		}
Packit d3f73b
		free(pid_context);
Packit d3f73b
		closedir(dir1);
Packit d3f73b
	}
Packit d3f73b
	closedir(dir);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
enum entry_types {
Packit d3f73b
	USERS,
Packit d3f73b
	PROC_CTX,
Packit d3f73b
	PROC_SOCK_CTX
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
#define ENTRY_BUF_SIZE 512
Packit d3f73b
static int find_entry(unsigned int ino, char **buf, int type)
Packit d3f73b
{
Packit d3f73b
	struct user_ent *p;
Packit d3f73b
	int cnt = 0;
Packit d3f73b
	char *ptr;
Packit d3f73b
	char *new_buf;
Packit d3f73b
	int len, new_buf_len;
Packit d3f73b
	int buf_used = 0;
Packit d3f73b
	int buf_len = 0;
Packit d3f73b
Packit d3f73b
	if (!ino)
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	p = user_ent_hash[user_ent_hashfn(ino)];
Packit d3f73b
	ptr = *buf = NULL;
Packit d3f73b
	while (p) {
Packit d3f73b
		if (p->ino != ino)
Packit d3f73b
			goto next;
Packit d3f73b
Packit d3f73b
		while (1) {
Packit d3f73b
			ptr = *buf + buf_used;
Packit d3f73b
			switch (type) {
Packit d3f73b
			case USERS:
Packit d3f73b
				len = snprintf(ptr, buf_len - buf_used,
Packit d3f73b
					"(\"%s\",pid=%d,fd=%d),",
Packit d3f73b
					p->process, p->pid, p->fd);
Packit d3f73b
				break;
Packit d3f73b
			case PROC_CTX:
Packit d3f73b
				len = snprintf(ptr, buf_len - buf_used,
Packit d3f73b
					"(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
Packit d3f73b
					p->process, p->pid,
Packit d3f73b
					p->process_ctx, p->fd);
Packit d3f73b
				break;
Packit d3f73b
			case PROC_SOCK_CTX:
Packit d3f73b
				len = snprintf(ptr, buf_len - buf_used,
Packit d3f73b
					"(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
Packit d3f73b
					p->process, p->pid,
Packit d3f73b
					p->process_ctx, p->fd,
Packit d3f73b
					p->socket_ctx);
Packit d3f73b
				break;
Packit d3f73b
			default:
Packit d3f73b
				fprintf(stderr, "ss: invalid type: %d\n", type);
Packit d3f73b
				abort();
Packit d3f73b
			}
Packit d3f73b
Packit d3f73b
			if (len < 0 || len >= buf_len - buf_used) {
Packit d3f73b
				new_buf_len = buf_len + ENTRY_BUF_SIZE;
Packit d3f73b
				new_buf = realloc(*buf, new_buf_len);
Packit d3f73b
				if (!new_buf) {
Packit d3f73b
					fprintf(stderr, "ss: failed to malloc buffer\n");
Packit d3f73b
					abort();
Packit d3f73b
				}
Packit d3f73b
				*buf = new_buf;
Packit d3f73b
				buf_len = new_buf_len;
Packit d3f73b
				continue;
Packit d3f73b
			} else {
Packit d3f73b
				buf_used += len;
Packit d3f73b
				break;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
		cnt++;
Packit d3f73b
next:
Packit d3f73b
		p = p->next;
Packit d3f73b
	}
Packit d3f73b
	if (buf_used) {
Packit d3f73b
		ptr = *buf + buf_used;
Packit d3f73b
		ptr[-1] = '\0';
Packit d3f73b
	}
Packit d3f73b
	return cnt;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static unsigned long long cookie_sk_get(const uint32_t *cookie)
Packit d3f73b
{
Packit d3f73b
	return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *sctp_sstate_name[] = {
Packit d3f73b
	[SCTP_STATE_CLOSED] = "CLOSED",
Packit d3f73b
	[SCTP_STATE_COOKIE_WAIT] = "COOKIE_WAIT",
Packit d3f73b
	[SCTP_STATE_COOKIE_ECHOED] = "COOKIE_ECHOED",
Packit d3f73b
	[SCTP_STATE_ESTABLISHED] = "ESTAB",
Packit d3f73b
	[SCTP_STATE_SHUTDOWN_PENDING] = "SHUTDOWN_PENDING",
Packit d3f73b
	[SCTP_STATE_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
Packit d3f73b
	[SCTP_STATE_SHUTDOWN_RECEIVED] = "SHUTDOWN_RECEIVED",
Packit d3f73b
	[SCTP_STATE_SHUTDOWN_ACK_SENT] = "ACK_SENT",
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static const char * const stype_nameg[] = {
Packit d3f73b
	"UNKNOWN",
Packit d3f73b
	[SOCK_STREAM] = "STREAM",
Packit d3f73b
	[SOCK_DGRAM] = "DGRAM",
Packit d3f73b
	[SOCK_RDM] = "RDM",
Packit d3f73b
	[SOCK_SEQPACKET] = "SEQPACKET",
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
struct sockstat {
Packit d3f73b
	struct sockstat	   *next;
Packit d3f73b
	unsigned int	    type;
Packit d3f73b
	uint16_t	    prot;
Packit d3f73b
	uint16_t	    raw_prot;
Packit d3f73b
	inet_prefix	    local;
Packit d3f73b
	inet_prefix	    remote;
Packit d3f73b
	int		    lport;
Packit d3f73b
	int		    rport;
Packit d3f73b
	int		    state;
Packit d3f73b
	int		    rq, wq;
Packit d3f73b
	unsigned int ino;
Packit d3f73b
	unsigned int uid;
Packit d3f73b
	int		    refcnt;
Packit d3f73b
	unsigned int	    iface;
Packit d3f73b
	unsigned long long  sk;
Packit d3f73b
	char *name;
Packit d3f73b
	char *peer_name;
Packit d3f73b
	__u32		    mark;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
struct dctcpstat {
Packit d3f73b
	unsigned int	ce_state;
Packit d3f73b
	unsigned int	alpha;
Packit d3f73b
	unsigned int	ab_ecn;
Packit d3f73b
	unsigned int	ab_tot;
Packit d3f73b
	bool		enabled;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
struct tcpstat {
Packit d3f73b
	struct sockstat	    ss;
Packit d3f73b
	unsigned int	    timer;
Packit d3f73b
	unsigned int	    timeout;
Packit d3f73b
	int		    probes;
Packit d3f73b
	char		    cong_alg[16];
Packit d3f73b
	double		    rto, ato, rtt, rttvar;
Packit d3f73b
	int		    qack, ssthresh, backoff;
Packit d3f73b
	double		    send_bps;
Packit d3f73b
	int		    snd_wscale;
Packit d3f73b
	int		    rcv_wscale;
Packit d3f73b
	int		    mss;
Packit d3f73b
	int		    rcv_mss;
Packit d3f73b
	int		    advmss;
Packit d3f73b
	unsigned int	    pmtu;
Packit d3f73b
	unsigned int	    cwnd;
Packit d3f73b
	unsigned int	    lastsnd;
Packit d3f73b
	unsigned int	    lastrcv;
Packit d3f73b
	unsigned int	    lastack;
Packit d3f73b
	double		    pacing_rate;
Packit d3f73b
	double		    pacing_rate_max;
Packit d3f73b
	double		    delivery_rate;
Packit d3f73b
	unsigned long long  bytes_acked;
Packit d3f73b
	unsigned long long  bytes_received;
Packit d3f73b
	unsigned int	    segs_out;
Packit d3f73b
	unsigned int	    segs_in;
Packit d3f73b
	unsigned int	    data_segs_out;
Packit d3f73b
	unsigned int	    data_segs_in;
Packit d3f73b
	unsigned int	    unacked;
Packit d3f73b
	unsigned int	    retrans;
Packit d3f73b
	unsigned int	    retrans_total;
Packit d3f73b
	unsigned int	    lost;
Packit d3f73b
	unsigned int	    sacked;
Packit d3f73b
	unsigned int	    fackets;
Packit d3f73b
	unsigned int	    reordering;
Packit d3f73b
	unsigned int	    not_sent;
Packit d3f73b
	unsigned int	    delivered;
Packit d3f73b
	unsigned int	    delivered_ce;
Packit d3f73b
	unsigned int	    dsack_dups;
Packit d3f73b
	unsigned int	    reord_seen;
Packit d3f73b
	double		    rcv_rtt;
Packit d3f73b
	double		    min_rtt;
Packit d3f73b
	int		    rcv_space;
Packit d3f73b
	unsigned int        rcv_ssthresh;
Packit d3f73b
	unsigned long long  busy_time;
Packit d3f73b
	unsigned long long  rwnd_limited;
Packit d3f73b
	unsigned long long  sndbuf_limited;
Packit d3f73b
	unsigned long long  bytes_sent;
Packit d3f73b
	unsigned long long  bytes_retrans;
Packit d3f73b
	bool		    has_ts_opt;
Packit d3f73b
	bool		    has_sack_opt;
Packit d3f73b
	bool		    has_ecn_opt;
Packit d3f73b
	bool		    has_ecnseen_opt;
Packit d3f73b
	bool		    has_fastopen_opt;
Packit d3f73b
	bool		    has_wscale_opt;
Packit d3f73b
	bool		    app_limited;
Packit d3f73b
	struct dctcpstat    *dctcp;
Packit d3f73b
	struct tcp_bbr_info *bbr_info;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
/* SCTP assocs share the same inode number with their parent endpoint. So if we
Packit d3f73b
 * have seen the inode number before, it must be an assoc instead of the next
Packit d3f73b
 * endpoint. */
Packit d3f73b
static bool is_sctp_assoc(struct sockstat *s, const char *sock_name)
Packit d3f73b
{
Packit d3f73b
	if (strcmp(sock_name, "sctp"))
Packit d3f73b
		return false;
Packit d3f73b
	if (!sctp_ino || sctp_ino != s->ino)
Packit d3f73b
		return false;
Packit d3f73b
	return true;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *unix_netid_name(int type)
Packit d3f73b
{
Packit d3f73b
	switch (type) {
Packit d3f73b
	case SOCK_STREAM:
Packit d3f73b
		return "u_str";
Packit d3f73b
	case SOCK_SEQPACKET:
Packit d3f73b
		return "u_seq";
Packit d3f73b
	case SOCK_DGRAM:
Packit d3f73b
	default:
Packit d3f73b
		return "u_dgr";
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *proto_name(int protocol)
Packit d3f73b
{
Packit d3f73b
	switch (protocol) {
Packit d3f73b
	case 0:
Packit d3f73b
		return "raw";
Packit d3f73b
	case IPPROTO_UDP:
Packit d3f73b
		return "udp";
Packit d3f73b
	case IPPROTO_TCP:
Packit d3f73b
		return "tcp";
Packit d3f73b
	case IPPROTO_SCTP:
Packit d3f73b
		return "sctp";
Packit d3f73b
	case IPPROTO_DCCP:
Packit d3f73b
		return "dccp";
Packit d3f73b
	case IPPROTO_ICMPV6:
Packit d3f73b
		return "icmp6";
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return "???";
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *vsock_netid_name(int type)
Packit d3f73b
{
Packit d3f73b
	switch (type) {
Packit d3f73b
	case SOCK_STREAM:
Packit d3f73b
		return "v_str";
Packit d3f73b
	case SOCK_DGRAM:
Packit d3f73b
		return "v_dgr";
Packit d3f73b
	default:
Packit d3f73b
		return "???";
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *tipc_netid_name(int type)
Packit d3f73b
{
Packit d3f73b
	switch (type) {
Packit d3f73b
	case SOCK_STREAM:
Packit d3f73b
		return "ti_st";
Packit d3f73b
	case SOCK_DGRAM:
Packit d3f73b
		return "ti_dg";
Packit d3f73b
	case SOCK_RDM:
Packit d3f73b
		return "ti_rd";
Packit d3f73b
	case SOCK_SEQPACKET:
Packit d3f73b
		return "ti_sq";
Packit d3f73b
	default:
Packit d3f73b
		return "???";
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Allocate and initialize a new buffer chunk */
Packit d3f73b
static struct buf_chunk *buf_chunk_new(void)
Packit d3f73b
{
Packit d3f73b
	struct buf_chunk *new = malloc(BUF_CHUNK);
Packit d3f73b
Packit d3f73b
	if (!new)
Packit d3f73b
		abort();
Packit d3f73b
Packit d3f73b
	new->next = NULL;
Packit d3f73b
Packit d3f73b
	/* This is also the last block */
Packit d3f73b
	buffer.tail = new;
Packit d3f73b
Packit d3f73b
	/* Next token will be stored at the beginning of chunk data area, and
Packit d3f73b
	 * its initial length is zero.
Packit d3f73b
	 */
Packit d3f73b
	buffer.cur = (struct buf_token *)new->data;
Packit d3f73b
	buffer.cur->len = 0;
Packit d3f73b
Packit d3f73b
	new->end = buffer.cur->data;
Packit d3f73b
Packit d3f73b
	buffer.chunks++;
Packit d3f73b
Packit d3f73b
	return new;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Return available tail room in given chunk */
Packit d3f73b
static int buf_chunk_avail(struct buf_chunk *chunk)
Packit d3f73b
{
Packit d3f73b
	return BUF_CHUNK - offsetof(struct buf_chunk, data) -
Packit d3f73b
	       (chunk->end - chunk->data);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Update end pointer and token length, link new chunk if we hit the end of the
Packit d3f73b
 * current one. Return -EAGAIN if we got a new chunk, caller has to print again.
Packit d3f73b
 */
Packit d3f73b
static int buf_update(int len)
Packit d3f73b
{
Packit d3f73b
	struct buf_chunk *chunk = buffer.tail;
Packit d3f73b
	struct buf_token *t = buffer.cur;
Packit d3f73b
Packit d3f73b
	/* Claim success if new content fits in the current chunk, and anyway
Packit d3f73b
	 * if this is the first token in the chunk: in the latter case,
Packit d3f73b
	 * allocating a new chunk won't help, so we'll just cut the output.
Packit d3f73b
	 */
Packit d3f73b
	if ((len < buf_chunk_avail(chunk) && len != -1 /* glibc < 2.0.6 */) ||
Packit d3f73b
	    t == (struct buf_token *)chunk->data) {
Packit d3f73b
		len = min(len, buf_chunk_avail(chunk));
Packit d3f73b
Packit d3f73b
		/* Total field length can't exceed 2^16 bytes, cut as needed */
Packit d3f73b
		len = min(len, USHRT_MAX - t->len);
Packit d3f73b
Packit d3f73b
		chunk->end += len;
Packit d3f73b
		t->len += len;
Packit d3f73b
		return 0;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	/* Content truncated, time to allocate more */
Packit d3f73b
	chunk->next = buf_chunk_new();
Packit d3f73b
Packit d3f73b
	/* Copy current token over to new chunk, including length descriptor */
Packit d3f73b
	memcpy(chunk->next->data, t, sizeof(t->len) + t->len);
Packit d3f73b
	chunk->next->end += t->len;
Packit d3f73b
Packit d3f73b
	/* Discard partially written field in old chunk */
Packit d3f73b
	chunk->end -= t->len + sizeof(t->len);
Packit d3f73b
Packit d3f73b
	return -EAGAIN;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Append content to buffer as part of the current field */
Packit d3f73b
__attribute__((format(printf, 1, 2)))
Packit d3f73b
static void out(const char *fmt, ...)
Packit d3f73b
{
Packit d3f73b
	struct column *f = current_field;
Packit d3f73b
	va_list args;
Packit d3f73b
	char *pos;
Packit d3f73b
	int len;
Packit d3f73b
Packit d3f73b
	if (f->disabled)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	if (!buffer.head)
Packit d3f73b
		buffer.head = buf_chunk_new();
Packit d3f73b
Packit d3f73b
again:	/* Append to buffer: if we have a new chunk, print again */
Packit d3f73b
Packit d3f73b
	pos = buffer.cur->data + buffer.cur->len;
Packit d3f73b
	va_start(args, fmt);
Packit d3f73b
Packit d3f73b
	/* Limit to tail room. If we hit the limit, buf_update() will tell us */
Packit d3f73b
	len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, args);
Packit d3f73b
	va_end(args);
Packit d3f73b
Packit d3f73b
	if (buf_update(len))
Packit d3f73b
		goto again;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int print_left_spacing(struct column *f, int stored, int printed)
Packit d3f73b
{
Packit d3f73b
	int s;
Packit d3f73b
Packit d3f73b
	if (!f->width || f->align == ALIGN_LEFT)
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	s = f->width - stored - printed;
Packit d3f73b
	if (f->align == ALIGN_CENTER)
Packit d3f73b
		/* If count of total spacing is odd, shift right by one */
Packit d3f73b
		s = (s + 1) / 2;
Packit d3f73b
Packit d3f73b
	if (s > 0)
Packit d3f73b
		return printf("%*c", s, ' ');
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void print_right_spacing(struct column *f, int printed)
Packit d3f73b
{
Packit d3f73b
	int s;
Packit d3f73b
Packit d3f73b
	if (!f->width || f->align == ALIGN_RIGHT)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	s = f->width - printed;
Packit d3f73b
	if (f->align == ALIGN_CENTER)
Packit d3f73b
		s /= 2;
Packit d3f73b
Packit d3f73b
	if (s > 0)
Packit d3f73b
		printf("%*c", s, ' ');
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Done with field: update buffer pointer, start new token after current one */
Packit d3f73b
static void field_flush(struct column *f)
Packit d3f73b
{
Packit d3f73b
	struct buf_chunk *chunk;
Packit d3f73b
	unsigned int pad;
Packit d3f73b
Packit d3f73b
	if (f->disabled)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	chunk = buffer.tail;
Packit d3f73b
	pad = buffer.cur->len % 2;
Packit d3f73b
Packit d3f73b
	if (buffer.cur->len > f->max_len)
Packit d3f73b
		f->max_len = buffer.cur->len;
Packit d3f73b
Packit d3f73b
	/* We need a new chunk if we can't store the next length descriptor.
Packit d3f73b
	 * Mind the gap between end of previous token and next aligned position
Packit d3f73b
	 * for length descriptor.
Packit d3f73b
	 */
Packit d3f73b
	if (buf_chunk_avail(chunk) - pad < sizeof(buffer.cur->len)) {
Packit d3f73b
		chunk->end += pad;
Packit d3f73b
		chunk->next = buf_chunk_new();
Packit d3f73b
		return;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	buffer.cur = (struct buf_token *)(buffer.cur->data +
Packit d3f73b
					  LEN_ALIGN(buffer.cur->len));
Packit d3f73b
	buffer.cur->len = 0;
Packit d3f73b
	buffer.tail->end = buffer.cur->data;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int field_is_last(struct column *f)
Packit d3f73b
{
Packit d3f73b
	return f - columns == COL_MAX - 1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Get the next available token in the buffer starting from the current token */
Packit d3f73b
static struct buf_token *buf_token_next(struct buf_token *cur)
Packit d3f73b
{
Packit d3f73b
	struct buf_chunk *chunk = buffer.tail;
Packit d3f73b
Packit d3f73b
	/* If we reached the end of chunk contents, get token from next chunk */
Packit d3f73b
	if (cur->data + LEN_ALIGN(cur->len) == chunk->end) {
Packit d3f73b
		buffer.tail = chunk = chunk->next;
Packit d3f73b
		return chunk ? (struct buf_token *)chunk->data : NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return (struct buf_token *)(cur->data + LEN_ALIGN(cur->len));
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Free up all allocated buffer chunks */
Packit d3f73b
static void buf_free_all(void)
Packit d3f73b
{
Packit d3f73b
	struct buf_chunk *tmp;
Packit d3f73b
Packit d3f73b
	for (buffer.tail = buffer.head; buffer.tail; ) {
Packit d3f73b
		tmp = buffer.tail;
Packit d3f73b
		buffer.tail = buffer.tail->next;
Packit d3f73b
		free(tmp);
Packit d3f73b
	}
Packit d3f73b
	buffer.head = NULL;
Packit d3f73b
	buffer.chunks = 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Get current screen width, default to 80 columns if TIOCGWINSZ fails */
Packit d3f73b
static int render_screen_width(void)
Packit d3f73b
{
Packit d3f73b
	int width = 80;
Packit d3f73b
Packit d3f73b
	if (isatty(STDOUT_FILENO)) {
Packit d3f73b
		struct winsize w;
Packit d3f73b
Packit d3f73b
		if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
Packit d3f73b
			if (w.ws_col > 0)
Packit d3f73b
				width = w.ws_col;
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return width;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Calculate column width from contents length. If columns don't fit on one
Packit d3f73b
 * line, break them into the least possible amount of lines and keep them
Packit d3f73b
 * aligned across lines. Available screen space is equally spread between fields
Packit d3f73b
 * as additional spacing.
Packit d3f73b
 */
Packit d3f73b
static void render_calc_width(void)
Packit d3f73b
{
Packit d3f73b
	int screen_width = render_screen_width();
Packit d3f73b
	struct column *c, *eol = columns - 1;
Packit d3f73b
	int first, len = 0, linecols = 0;
Packit d3f73b
Packit d3f73b
	/* First pass: set width for each column to measured content length */
Packit d3f73b
	for (first = 1, c = columns; c - columns < COL_MAX; c++) {
Packit d3f73b
		if (c->disabled)
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		if (!first && c->max_len)
Packit d3f73b
			c->width = c->max_len + strlen(c->ldelim);
Packit d3f73b
		else
Packit d3f73b
			c->width = c->max_len;
Packit d3f73b
Packit d3f73b
		/* But don't exceed screen size. If we exceed the screen size
Packit d3f73b
		 * for even a single field, it will just start on a line of its
Packit d3f73b
		 * own and then naturally wrap.
Packit d3f73b
		 */
Packit d3f73b
		c->width = min(c->width, screen_width);
Packit d3f73b
Packit d3f73b
		if (c->width)
Packit d3f73b
			first = 0;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	/* Second pass: find out newlines and distribute available spacing */
Packit d3f73b
	for (c = columns; c - columns < COL_MAX; c++) {
Packit d3f73b
		int pad, spacing, rem, last;
Packit d3f73b
		struct column *tmp;
Packit d3f73b
Packit d3f73b
		if (!c->width)
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		linecols++;
Packit d3f73b
		len += c->width;
Packit d3f73b
Packit d3f73b
		for (last = 1, tmp = c + 1; tmp - columns < COL_MAX; tmp++) {
Packit d3f73b
			if (tmp->width) {
Packit d3f73b
				last = 0;
Packit d3f73b
				break;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (!last && len < screen_width) {
Packit d3f73b
			/* Columns fit on screen so far, nothing to do yet */
Packit d3f73b
			continue;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (len == screen_width) {
Packit d3f73b
			/* Exact fit, just start with new line */
Packit d3f73b
			goto newline;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (len > screen_width) {
Packit d3f73b
			/* Screen width exceeded: go back one column */
Packit d3f73b
			len -= c->width;
Packit d3f73b
			c--;
Packit d3f73b
			linecols--;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		/* Distribute remaining space to columns on this line */
Packit d3f73b
		pad = screen_width - len;
Packit d3f73b
		spacing = pad / linecols;
Packit d3f73b
		rem = pad % linecols;
Packit d3f73b
		for (tmp = c; tmp > eol; tmp--) {
Packit d3f73b
			if (!tmp->width)
Packit d3f73b
				continue;
Packit d3f73b
Packit d3f73b
			tmp->width += spacing;
Packit d3f73b
			if (rem) {
Packit d3f73b
				tmp->width++;
Packit d3f73b
				rem--;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
newline:
Packit d3f73b
		/* Line break: reset line counters, mark end-of-line */
Packit d3f73b
		eol = c;
Packit d3f73b
		len = 0;
Packit d3f73b
		linecols = 0;
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Render buffered output with spacing and delimiters, then free up buffers */
Packit d3f73b
static void render(void)
Packit d3f73b
{
Packit d3f73b
	struct buf_token *token;
Packit d3f73b
	int printed, line_started = 0;
Packit d3f73b
	struct column *f;
Packit d3f73b
Packit d3f73b
	if (!buffer.head)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	token = (struct buf_token *)buffer.head->data;
Packit d3f73b
Packit d3f73b
	/* Ensure end alignment of last token, it wasn't necessarily flushed */
Packit d3f73b
	buffer.tail->end += buffer.cur->len % 2;
Packit d3f73b
Packit d3f73b
	render_calc_width();
Packit d3f73b
Packit d3f73b
	/* Rewind and replay */
Packit d3f73b
	buffer.tail = buffer.head;
Packit d3f73b
Packit d3f73b
	f = columns;
Packit d3f73b
	while (!f->width)
Packit d3f73b
		f++;
Packit d3f73b
Packit d3f73b
	while (token) {
Packit d3f73b
		/* Print left delimiter only if we already started a line */
Packit d3f73b
		if (line_started++)
Packit d3f73b
			printed = printf("%s", f->ldelim);
Packit d3f73b
		else
Packit d3f73b
			printed = 0;
Packit d3f73b
Packit d3f73b
		/* Print field content from token data with spacing */
Packit d3f73b
		printed += print_left_spacing(f, token->len, printed);
Packit d3f73b
		printed += fwrite(token->data, 1, token->len, stdout);
Packit d3f73b
		print_right_spacing(f, printed);
Packit d3f73b
Packit d3f73b
		/* Go to next non-empty field, deal with end-of-line */
Packit d3f73b
		do {
Packit d3f73b
			if (field_is_last(f)) {
Packit d3f73b
				printf("\n");
Packit d3f73b
				f = columns;
Packit d3f73b
				line_started = 0;
Packit d3f73b
			} else {
Packit d3f73b
				f++;
Packit d3f73b
			}
Packit d3f73b
		} while (f->disabled);
Packit d3f73b
Packit d3f73b
		token = buf_token_next(token);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	buf_free_all();
Packit d3f73b
	current_field = columns;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Move to next field, and render buffer if we reached the maximum number of
Packit d3f73b
 * chunks, at the last field in a line.
Packit d3f73b
 */
Packit d3f73b
static void field_next(void)
Packit d3f73b
{
Packit d3f73b
	if (field_is_last(current_field) && buffer.chunks >= BUF_CHUNKS_MAX) {
Packit d3f73b
		render();
Packit d3f73b
		return;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	field_flush(current_field);
Packit d3f73b
	if (field_is_last(current_field))
Packit d3f73b
		current_field = columns;
Packit d3f73b
	else
Packit d3f73b
		current_field++;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Walk through fields and flush them until we reach the desired one */
Packit d3f73b
static void field_set(enum col_id id)
Packit d3f73b
{
Packit d3f73b
	while (id != current_field - columns)
Packit d3f73b
		field_next();
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Print header for all non-empty columns */
Packit d3f73b
static void print_header(void)
Packit d3f73b
{
Packit d3f73b
	while (!field_is_last(current_field)) {
Packit d3f73b
		if (!current_field->disabled)
Packit d3f73b
			out("%s", current_field->header);
Packit d3f73b
		field_next();
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sock_state_print(struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	const char *sock_name;
Packit d3f73b
	static const char * const sstate_name[] = {
Packit d3f73b
		"UNKNOWN",
Packit d3f73b
		[SS_ESTABLISHED] = "ESTAB",
Packit d3f73b
		[SS_SYN_SENT] = "SYN-SENT",
Packit d3f73b
		[SS_SYN_RECV] = "SYN-RECV",
Packit d3f73b
		[SS_FIN_WAIT1] = "FIN-WAIT-1",
Packit d3f73b
		[SS_FIN_WAIT2] = "FIN-WAIT-2",
Packit d3f73b
		[SS_TIME_WAIT] = "TIME-WAIT",
Packit d3f73b
		[SS_CLOSE] = "UNCONN",
Packit d3f73b
		[SS_CLOSE_WAIT] = "CLOSE-WAIT",
Packit d3f73b
		[SS_LAST_ACK] = "LAST-ACK",
Packit d3f73b
		[SS_LISTEN] =	"LISTEN",
Packit d3f73b
		[SS_CLOSING] = "CLOSING",
Packit d3f73b
	};
Packit d3f73b
Packit d3f73b
	switch (s->local.family) {
Packit d3f73b
	case AF_UNIX:
Packit d3f73b
		sock_name = unix_netid_name(s->type);
Packit d3f73b
		break;
Packit d3f73b
	case AF_INET:
Packit d3f73b
	case AF_INET6:
Packit d3f73b
		sock_name = proto_name(s->type);
Packit d3f73b
		break;
Packit d3f73b
	case AF_PACKET:
Packit d3f73b
		sock_name = s->type == SOCK_RAW ? "p_raw" : "p_dgr";
Packit d3f73b
		break;
Packit d3f73b
	case AF_NETLINK:
Packit d3f73b
		sock_name = "nl";
Packit d3f73b
		break;
Packit d3f73b
	case AF_TIPC:
Packit d3f73b
		sock_name = tipc_netid_name(s->type);
Packit d3f73b
		break;
Packit d3f73b
	case AF_VSOCK:
Packit d3f73b
		sock_name = vsock_netid_name(s->type);
Packit d3f73b
		break;
Packit d3f73b
	case AF_XDP:
Packit d3f73b
		sock_name = "xdp";
Packit d3f73b
		break;
Packit d3f73b
	default:
Packit d3f73b
		sock_name = "unknown";
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (is_sctp_assoc(s, sock_name)) {
Packit d3f73b
		field_set(COL_STATE);		/* Empty Netid field */
Packit d3f73b
		out("`- %s", sctp_sstate_name[s->state]);
Packit d3f73b
	} else {
Packit d3f73b
		field_set(COL_NETID);
Packit d3f73b
		out("%s", sock_name);
Packit d3f73b
		field_set(COL_STATE);
Packit d3f73b
		out("%s", sstate_name[s->state]);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	field_set(COL_RECVQ);
Packit d3f73b
	out("%-6d", s->rq);
Packit d3f73b
	field_set(COL_SENDQ);
Packit d3f73b
	out("%-6d", s->wq);
Packit d3f73b
	field_set(COL_ADDR);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sock_details_print(struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	if (s->uid)
Packit d3f73b
		out(" uid:%u", s->uid);
Packit d3f73b
Packit d3f73b
	out(" ino:%u", s->ino);
Packit d3f73b
	out(" sk:%llx", s->sk);
Packit d3f73b
Packit d3f73b
	if (s->mark)
Packit d3f73b
		out(" fwmark:0x%x", s->mark);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sock_addr_print(const char *addr, char *delim, const char *port,
Packit d3f73b
		const char *ifname)
Packit d3f73b
{
Packit d3f73b
	if (ifname)
Packit d3f73b
		out("%s" "%%" "%s%s", addr, ifname, delim);
Packit d3f73b
	else
Packit d3f73b
		out("%s%s", addr, delim);
Packit d3f73b
Packit d3f73b
	field_next();
Packit d3f73b
	out("%s", port);
Packit d3f73b
	field_next();
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *print_ms_timer(unsigned int timeout)
Packit d3f73b
{
Packit d3f73b
	static char buf[64];
Packit d3f73b
	int secs, msecs, minutes;
Packit d3f73b
Packit d3f73b
	secs = timeout/1000;
Packit d3f73b
	minutes = secs/60;
Packit d3f73b
	secs = secs%60;
Packit d3f73b
	msecs = timeout%1000;
Packit d3f73b
	buf[0] = 0;
Packit d3f73b
	if (minutes) {
Packit d3f73b
		msecs = 0;
Packit d3f73b
		snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
Packit d3f73b
		if (minutes > 9)
Packit d3f73b
			secs = 0;
Packit d3f73b
	}
Packit d3f73b
	if (secs) {
Packit d3f73b
		if (secs > 9)
Packit d3f73b
			msecs = 0;
Packit d3f73b
		sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
Packit d3f73b
	}
Packit d3f73b
	if (msecs)
Packit d3f73b
		sprintf(buf+strlen(buf), "%03dms", msecs);
Packit d3f73b
	return buf;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
struct scache {
Packit d3f73b
	struct scache *next;
Packit d3f73b
	int port;
Packit d3f73b
	char *name;
Packit d3f73b
	const char *proto;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static struct scache *rlist;
Packit d3f73b
Packit d3f73b
static void init_service_resolver(void)
Packit d3f73b
{
Packit d3f73b
	char buf[128];
Packit d3f73b
	FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
Packit d3f73b
Packit d3f73b
	if (!fp)
Packit d3f73b
		return;
Packit d3f73b
Packit d3f73b
	if (!fgets(buf, sizeof(buf), fp)) {
Packit d3f73b
		pclose(fp);
Packit d3f73b
		return;
Packit d3f73b
	}
Packit d3f73b
	while (fgets(buf, sizeof(buf), fp) != NULL) {
Packit d3f73b
		unsigned int progn, port;
Packit d3f73b
		char proto[128], prog[128] = "rpc.";
Packit d3f73b
		struct scache *c;
Packit d3f73b
Packit d3f73b
		if (sscanf(buf, "%u %*d %s %u %s",
Packit d3f73b
			   &progn, proto, &port, prog+4) != 4)
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		if (!(c = malloc(sizeof(*c))))
Packit d3f73b
			continue;
Packit d3f73b
Packit d3f73b
		c->port = port;
Packit d3f73b
		c->name = strdup(prog);
Packit d3f73b
		if (strcmp(proto, TCP_PROTO) == 0)
Packit d3f73b
			c->proto = TCP_PROTO;
Packit d3f73b
		else if (strcmp(proto, UDP_PROTO) == 0)
Packit d3f73b
			c->proto = UDP_PROTO;
Packit d3f73b
		else if (strcmp(proto, SCTP_PROTO) == 0)
Packit d3f73b
			c->proto = SCTP_PROTO;
Packit d3f73b
		else
Packit d3f73b
			c->proto = NULL;
Packit d3f73b
		c->next = rlist;
Packit d3f73b
		rlist = c;
Packit d3f73b
	}
Packit d3f73b
	pclose(fp);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Even do not try default linux ephemeral port ranges:
Packit d3f73b
 * default /etc/services contains so much of useless crap
Packit d3f73b
 * wouldbe "allocated" to this area that resolution
Packit d3f73b
 * is really harmful. I shrug each time when seeing
Packit d3f73b
 * "socks" or "cfinger" in dumps.
Packit d3f73b
 */
Packit d3f73b
static int is_ephemeral(int port)
Packit d3f73b
{
Packit d3f73b
	static int min = 0, max;
Packit d3f73b
Packit d3f73b
	if (!min) {
Packit d3f73b
		FILE *f = ephemeral_ports_open();
Packit d3f73b
Packit d3f73b
		if (!f || fscanf(f, "%d %d", &min, &max) < 2) {
Packit d3f73b
			min = 1024;
Packit d3f73b
			max = 4999;
Packit d3f73b
		}
Packit d3f73b
		if (f)
Packit d3f73b
			fclose(f);
Packit d3f73b
	}
Packit d3f73b
	return port >= min && port <= max;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
Packit d3f73b
static const char *__resolve_service(int port)
Packit d3f73b
{
Packit d3f73b
	struct scache *c;
Packit d3f73b
Packit d3f73b
	for (c = rlist; c; c = c->next) {
Packit d3f73b
		if (c->port == port && c->proto == dg_proto)
Packit d3f73b
			return c->name;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (!is_ephemeral(port)) {
Packit d3f73b
		static int notfirst;
Packit d3f73b
		struct servent *se;
Packit d3f73b
Packit d3f73b
		if (!notfirst) {
Packit d3f73b
			setservent(1);
Packit d3f73b
			notfirst = 1;
Packit d3f73b
		}
Packit d3f73b
		se = getservbyport(htons(port), dg_proto);
Packit d3f73b
		if (se)
Packit d3f73b
			return se->s_name;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return NULL;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
#define SCACHE_BUCKETS 1024
Packit d3f73b
static struct scache *cache_htab[SCACHE_BUCKETS];
Packit d3f73b
Packit d3f73b
static const char *resolve_service(int port)
Packit d3f73b
{
Packit d3f73b
	static char buf[128];
Packit d3f73b
	struct scache *c;
Packit d3f73b
	const char *res;
Packit d3f73b
	int hash;
Packit d3f73b
Packit d3f73b
	if (port == 0) {
Packit d3f73b
		buf[0] = '*';
Packit d3f73b
		buf[1] = 0;
Packit d3f73b
		return buf;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (numeric)
Packit d3f73b
		goto do_numeric;
Packit d3f73b
Packit d3f73b
	if (dg_proto == RAW_PROTO)
Packit d3f73b
		return inet_proto_n2a(port, buf, sizeof(buf));
Packit d3f73b
Packit d3f73b
Packit d3f73b
	hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS;
Packit d3f73b
Packit d3f73b
	for (c = cache_htab[hash]; c; c = c->next) {
Packit d3f73b
		if (c->port == port && c->proto == dg_proto)
Packit d3f73b
			goto do_cache;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	c = malloc(sizeof(*c));
Packit d3f73b
	if (!c)
Packit d3f73b
		goto do_numeric;
Packit d3f73b
	res = __resolve_service(port);
Packit d3f73b
	c->port = port;
Packit d3f73b
	c->name = res ? strdup(res) : NULL;
Packit d3f73b
	c->proto = dg_proto;
Packit d3f73b
	c->next = cache_htab[hash];
Packit d3f73b
	cache_htab[hash] = c;
Packit d3f73b
Packit d3f73b
do_cache:
Packit d3f73b
	if (c->name)
Packit d3f73b
		return c->name;
Packit d3f73b
Packit d3f73b
do_numeric:
Packit d3f73b
	sprintf(buf, "%u", port);
Packit d3f73b
	return buf;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void inet_addr_print(const inet_prefix *a, int port,
Packit d3f73b
			    unsigned int ifindex, bool v6only)
Packit d3f73b
{
Packit d3f73b
	char buf[1024];
Packit d3f73b
	const char *ap = buf;
Packit d3f73b
	const char *ifname = NULL;
Packit d3f73b
Packit d3f73b
	if (a->family == AF_INET) {
Packit d3f73b
		ap = format_host(AF_INET, 4, a->data);
Packit d3f73b
	} else {
Packit d3f73b
		if (!v6only &&
Packit d3f73b
		    !memcmp(a->data, &in6addr_any, sizeof(in6addr_any))) {
Packit d3f73b
			buf[0] = '*';
Packit d3f73b
			buf[1] = 0;
Packit d3f73b
		} else {
Packit d3f73b
			ap = format_host(a->family, 16, a->data);
Packit d3f73b
Packit d3f73b
			/* Numeric IPv6 addresses should be bracketed */
Packit d3f73b
			if (strchr(ap, ':')) {
Packit d3f73b
				snprintf(buf, sizeof(buf),
Packit d3f73b
					 "[%s]", ap);
Packit d3f73b
				ap = buf;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (ifindex)
Packit d3f73b
		ifname = ll_index_to_name(ifindex);
Packit d3f73b
Packit d3f73b
	sock_addr_print(ap, ":", resolve_service(port), ifname);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
struct aafilter {
Packit d3f73b
	inet_prefix	addr;
Packit d3f73b
	int		port;
Packit d3f73b
	unsigned int	iface;
Packit d3f73b
	__u32		mark;
Packit d3f73b
	__u32		mask;
Packit d3f73b
	struct aafilter *next;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
Packit d3f73b
			    int plen)
Packit d3f73b
{
Packit d3f73b
	if (!inet_addr_match(a, p, plen))
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	/* Cursed "v4 mapped" addresses: v4 mapped socket matches
Packit d3f73b
	 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
Packit d3f73b
	 * sockets. Fair? */
Packit d3f73b
	if (p->family == AF_INET && a->family == AF_INET6) {
Packit d3f73b
		if (a->data[0] == 0 && a->data[1] == 0 &&
Packit d3f73b
		    a->data[2] == htonl(0xffff)) {
Packit d3f73b
			inet_prefix tmp = *a;
Packit d3f73b
Packit d3f73b
			tmp.data[0] = a->data[3];
Packit d3f73b
			return inet_addr_match(&tmp, p, plen);
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
	return 1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int unix_match(const inet_prefix *a, const inet_prefix *p)
Packit d3f73b
{
Packit d3f73b
	char *addr, *pattern;
Packit d3f73b
Packit d3f73b
	memcpy(&addr, a->data, sizeof(addr));
Packit d3f73b
	memcpy(&pattern, p->data, sizeof(pattern));
Packit d3f73b
	if (pattern == NULL)
Packit d3f73b
		return 1;
Packit d3f73b
	if (addr == NULL)
Packit d3f73b
		addr = "";
Packit d3f73b
	return !fnmatch(pattern, addr, 0);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	switch (f->type) {
Packit d3f73b
		case SSF_S_AUTO:
Packit d3f73b
	{
Packit d3f73b
		if (s->local.family == AF_UNIX) {
Packit d3f73b
			char *p;
Packit d3f73b
Packit d3f73b
			memcpy(&p, s->local.data, sizeof(p));
Packit d3f73b
			return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
Packit d3f73b
					     strspn(p+1, "0123456789abcdef") == 5);
Packit d3f73b
		}
Packit d3f73b
		if (s->local.family == AF_PACKET)
Packit d3f73b
			return s->lport == 0 && s->local.data[0] == 0;
Packit d3f73b
		if (s->local.family == AF_NETLINK)
Packit d3f73b
			return s->lport < 0;
Packit d3f73b
		if (s->local.family == AF_VSOCK)
Packit d3f73b
			return s->lport > 1023;
Packit d3f73b
Packit d3f73b
		return is_ephemeral(s->lport);
Packit d3f73b
	}
Packit d3f73b
		case SSF_DCOND:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (a->addr.family == AF_UNIX)
Packit d3f73b
			return unix_match(&s->remote, &a->addr);
Packit d3f73b
		if (a->port != -1 && a->port != s->rport)
Packit d3f73b
			return 0;
Packit d3f73b
		if (a->addr.bitlen) {
Packit d3f73b
			do {
Packit d3f73b
				if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
Packit d3f73b
					return 1;
Packit d3f73b
			} while ((a = a->next) != NULL);
Packit d3f73b
			return 0;
Packit d3f73b
		}
Packit d3f73b
		return 1;
Packit d3f73b
	}
Packit d3f73b
		case SSF_SCOND:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (a->addr.family == AF_UNIX)
Packit d3f73b
			return unix_match(&s->local, &a->addr);
Packit d3f73b
		if (a->port != -1 && a->port != s->lport)
Packit d3f73b
			return 0;
Packit d3f73b
		if (a->addr.bitlen) {
Packit d3f73b
			do {
Packit d3f73b
				if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
Packit d3f73b
					return 1;
Packit d3f73b
			} while ((a = a->next) != NULL);
Packit d3f73b
			return 0;
Packit d3f73b
		}
Packit d3f73b
		return 1;
Packit d3f73b
	}
Packit d3f73b
		case SSF_D_GE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return s->rport >= a->port;
Packit d3f73b
	}
Packit d3f73b
		case SSF_D_LE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return s->rport <= a->port;
Packit d3f73b
	}
Packit d3f73b
		case SSF_S_GE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return s->lport >= a->port;
Packit d3f73b
	}
Packit d3f73b
		case SSF_S_LE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return s->lport <= a->port;
Packit d3f73b
	}
Packit d3f73b
		case SSF_DEVCOND:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return s->iface == a->iface;
Packit d3f73b
	}
Packit d3f73b
		case SSF_MARKMASK:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		return (s->mark & a->mask) == a->mark;
Packit d3f73b
	}
Packit d3f73b
		/* Yup. It is recursion. Sorry. */
Packit d3f73b
		case SSF_AND:
Packit d3f73b
		return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
Packit d3f73b
		case SSF_OR:
Packit d3f73b
		return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
Packit d3f73b
		case SSF_NOT:
Packit d3f73b
		return !run_ssfilter(f->pred, s);
Packit d3f73b
		default:
Packit d3f73b
		abort();
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
/* Relocate external jumps by reloc. */
Packit d3f73b
static void ssfilter_patch(char *a, int len, int reloc)
Packit d3f73b
{
Packit d3f73b
	while (len > 0) {
Packit d3f73b
		struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a;
Packit d3f73b
Packit d3f73b
		if (op->no == len+4)
Packit d3f73b
			op->no += reloc;
Packit d3f73b
		len -= op->yes;
Packit d3f73b
		a += op->yes;
Packit d3f73b
	}
Packit d3f73b
	if (len < 0)
Packit d3f73b
		abort();
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
Packit d3f73b
{
Packit d3f73b
	switch (f->type) {
Packit d3f73b
		case SSF_S_AUTO:
Packit d3f73b
	{
Packit d3f73b
		if (!(*bytecode = malloc(4))) abort();
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
Packit d3f73b
		return 4;
Packit d3f73b
	}
Packit d3f73b
		case SSF_DCOND:
Packit d3f73b
		case SSF_SCOND:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
		struct aafilter *b;
Packit d3f73b
		char *ptr;
Packit d3f73b
		int  code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
Packit d3f73b
		int len = 0;
Packit d3f73b
Packit d3f73b
		for (b = a; b; b = b->next) {
Packit d3f73b
			len += 4 + sizeof(struct inet_diag_hostcond);
Packit d3f73b
			if (a->addr.family == AF_INET6)
Packit d3f73b
				len += 16;
Packit d3f73b
			else
Packit d3f73b
				len += 4;
Packit d3f73b
			if (b->next)
Packit d3f73b
				len += 4;
Packit d3f73b
		}
Packit d3f73b
		if (!(ptr = malloc(len))) abort();
Packit d3f73b
		*bytecode = ptr;
Packit d3f73b
		for (b = a; b; b = b->next) {
Packit d3f73b
			struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
Packit d3f73b
			int alen = (a->addr.family == AF_INET6 ? 16 : 4);
Packit d3f73b
			int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
Packit d3f73b
			struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4);
Packit d3f73b
Packit d3f73b
			*op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
Packit d3f73b
			cond->family = a->addr.family;
Packit d3f73b
			cond->port = a->port;
Packit d3f73b
			cond->prefix_len = a->addr.bitlen;
Packit d3f73b
			memcpy(cond->addr, a->addr.data, alen);
Packit d3f73b
			ptr += oplen;
Packit d3f73b
			if (b->next) {
Packit d3f73b
				op = (struct inet_diag_bc_op *)ptr;
Packit d3f73b
				*op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
Packit d3f73b
				ptr += 4;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
		return ptr - *bytecode;
Packit d3f73b
	}
Packit d3f73b
		case SSF_D_GE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *x = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (!(*bytecode = malloc(8))) abort();
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
Packit d3f73b
		return 8;
Packit d3f73b
	}
Packit d3f73b
		case SSF_D_LE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *x = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (!(*bytecode = malloc(8))) abort();
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
Packit d3f73b
		return 8;
Packit d3f73b
	}
Packit d3f73b
		case SSF_S_GE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *x = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (!(*bytecode = malloc(8))) abort();
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
Packit d3f73b
		return 8;
Packit d3f73b
	}
Packit d3f73b
		case SSF_S_LE:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *x = (void *)f->pred;
Packit d3f73b
Packit d3f73b
		if (!(*bytecode = malloc(8))) abort();
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
Packit d3f73b
		((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
Packit d3f73b
		return 8;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
		case SSF_AND:
Packit d3f73b
	{
Packit d3f73b
		char *a1 = NULL, *a2 = NULL, *a;
Packit d3f73b
		int l1, l2;
Packit d3f73b
Packit d3f73b
		l1 = ssfilter_bytecompile(f->pred, &a1;;
Packit d3f73b
		l2 = ssfilter_bytecompile(f->post, &a2;;
Packit d3f73b
		if (!l1 || !l2) {
Packit d3f73b
			free(a1);
Packit d3f73b
			free(a2);
Packit d3f73b
			return 0;
Packit d3f73b
		}
Packit d3f73b
		if (!(a = malloc(l1+l2))) abort();
Packit d3f73b
		memcpy(a, a1, l1);
Packit d3f73b
		memcpy(a+l1, a2, l2);
Packit d3f73b
		free(a1); free(a2);
Packit d3f73b
		ssfilter_patch(a, l1, l2);
Packit d3f73b
		*bytecode = a;
Packit d3f73b
		return l1+l2;
Packit d3f73b
	}
Packit d3f73b
		case SSF_OR:
Packit d3f73b
	{
Packit d3f73b
		char *a1 = NULL, *a2 = NULL, *a;
Packit d3f73b
		int l1, l2;
Packit d3f73b
Packit d3f73b
		l1 = ssfilter_bytecompile(f->pred, &a1;;
Packit d3f73b
		l2 = ssfilter_bytecompile(f->post, &a2;;
Packit d3f73b
		if (!l1 || !l2) {
Packit d3f73b
			free(a1);
Packit d3f73b
			free(a2);
Packit d3f73b
			return 0;
Packit d3f73b
		}
Packit d3f73b
		if (!(a = malloc(l1+l2+4))) abort();
Packit d3f73b
		memcpy(a, a1, l1);
Packit d3f73b
		memcpy(a+l1+4, a2, l2);
Packit d3f73b
		free(a1); free(a2);
Packit d3f73b
		*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
Packit d3f73b
		*bytecode = a;
Packit d3f73b
		return l1+l2+4;
Packit d3f73b
	}
Packit d3f73b
		case SSF_NOT:
Packit d3f73b
	{
Packit d3f73b
		char *a1 = NULL, *a;
Packit d3f73b
		int l1;
Packit d3f73b
Packit d3f73b
		l1 = ssfilter_bytecompile(f->pred, &a1;;
Packit d3f73b
		if (!l1) {
Packit d3f73b
			free(a1);
Packit d3f73b
			return 0;
Packit d3f73b
		}
Packit d3f73b
		if (!(a = malloc(l1+4))) abort();
Packit d3f73b
		memcpy(a, a1, l1);
Packit d3f73b
		free(a1);
Packit d3f73b
		*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
Packit d3f73b
		*bytecode = a;
Packit d3f73b
		return l1+4;
Packit d3f73b
	}
Packit d3f73b
		case SSF_DEVCOND:
Packit d3f73b
	{
Packit d3f73b
		/* bytecompile for SSF_DEVCOND not supported yet */
Packit d3f73b
		return 0;
Packit d3f73b
	}
Packit d3f73b
		case SSF_MARKMASK:
Packit d3f73b
	{
Packit d3f73b
		struct aafilter *a = (void *)f->pred;
Packit d3f73b
		struct instr {
Packit d3f73b
			struct inet_diag_bc_op op;
Packit d3f73b
			struct inet_diag_markcond cond;
Packit d3f73b
		};
Packit d3f73b
		int inslen = sizeof(struct instr);
Packit d3f73b
Packit d3f73b
		if (!(*bytecode = malloc(inslen))) abort();
Packit d3f73b
		((struct instr *)*bytecode)[0] = (struct instr) {
Packit d3f73b
			{ INET_DIAG_BC_MARK_COND, inslen, inslen + 4 },
Packit d3f73b
			{ a->mark, a->mask},
Packit d3f73b
		};
Packit d3f73b
Packit d3f73b
		return inslen;
Packit d3f73b
	}
Packit d3f73b
		default:
Packit d3f73b
		abort();
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int remember_he(struct aafilter *a, struct hostent *he)
Packit d3f73b
{
Packit d3f73b
	char **ptr = he->h_addr_list;
Packit d3f73b
	int cnt = 0;
Packit d3f73b
	int len;
Packit d3f73b
Packit d3f73b
	if (he->h_addrtype == AF_INET)
Packit d3f73b
		len = 4;
Packit d3f73b
	else if (he->h_addrtype == AF_INET6)
Packit d3f73b
		len = 16;
Packit d3f73b
	else
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	while (*ptr) {
Packit d3f73b
		struct aafilter *b = a;
Packit d3f73b
Packit d3f73b
		if (a->addr.bitlen) {
Packit d3f73b
			if ((b = malloc(sizeof(*b))) == NULL)
Packit d3f73b
				return cnt;
Packit d3f73b
			*b = *a;
Packit d3f73b
			a->next = b;
Packit d3f73b
		}
Packit d3f73b
		memcpy(b->addr.data, *ptr, len);
Packit d3f73b
		b->addr.bytelen = len;
Packit d3f73b
		b->addr.bitlen = len*8;
Packit d3f73b
		b->addr.family = he->h_addrtype;
Packit d3f73b
		ptr++;
Packit d3f73b
		cnt++;
Packit d3f73b
	}
Packit d3f73b
	return cnt;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int get_dns_host(struct aafilter *a, const char *addr, int fam)
Packit d3f73b
{
Packit d3f73b
	static int notfirst;
Packit d3f73b
	int cnt = 0;
Packit d3f73b
	struct hostent *he;
Packit d3f73b
Packit d3f73b
	a->addr.bitlen = 0;
Packit d3f73b
	if (!notfirst) {
Packit d3f73b
		sethostent(1);
Packit d3f73b
		notfirst = 1;
Packit d3f73b
	}
Packit d3f73b
	he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
Packit d3f73b
	if (he)
Packit d3f73b
		cnt = remember_he(a, he);
Packit d3f73b
	if (fam == AF_UNSPEC) {
Packit d3f73b
		he = gethostbyname2(addr, AF_INET6);
Packit d3f73b
		if (he)
Packit d3f73b
			cnt += remember_he(a, he);
Packit d3f73b
	}
Packit d3f73b
	return !cnt;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int xll_initted;
Packit d3f73b
Packit d3f73b
static void xll_init(void)
Packit d3f73b
{
Packit d3f73b
	struct rtnl_handle rth;
Packit d3f73b
Packit d3f73b
	if (rtnl_open(&rth, 0) < 0)
Packit d3f73b
		exit(1);
Packit d3f73b
Packit d3f73b
	ll_init_map(&rth);
Packit d3f73b
	rtnl_close(&rth);
Packit d3f73b
	xll_initted = 1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *xll_index_to_name(int index)
Packit d3f73b
{
Packit d3f73b
	if (!xll_initted)
Packit d3f73b
		xll_init();
Packit d3f73b
	return ll_index_to_name(index);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int xll_name_to_index(const char *dev)
Packit d3f73b
{
Packit d3f73b
	if (!xll_initted)
Packit d3f73b
		xll_init();
Packit d3f73b
	return ll_name_to_index(dev);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
void *parse_devcond(char *name)
Packit d3f73b
{
Packit d3f73b
	struct aafilter a = { .iface = 0 };
Packit d3f73b
	struct aafilter *res;
Packit d3f73b
Packit d3f73b
	a.iface = xll_name_to_index(name);
Packit d3f73b
	if (a.iface == 0) {
Packit d3f73b
		char *end;
Packit d3f73b
		unsigned long n;
Packit d3f73b
Packit d3f73b
		n = strtoul(name, &end, 0);
Packit d3f73b
		if (!end || end == name || *end || n > UINT_MAX)
Packit d3f73b
			return NULL;
Packit d3f73b
Packit d3f73b
		a.iface = n;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	res = malloc(sizeof(*res));
Packit d3f73b
	*res = a;
Packit d3f73b
Packit d3f73b
	return res;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid)
Packit d3f73b
{
Packit d3f73b
	*a = (inet_prefix){
Packit d3f73b
		.bytelen = sizeof(cid),
Packit d3f73b
		.family = AF_VSOCK,
Packit d3f73b
	};
Packit d3f73b
	memcpy(a->data, &cid, sizeof(cid));
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
void *parse_hostcond(char *addr, bool is_port)
Packit d3f73b
{
Packit d3f73b
	char *port = NULL;
Packit d3f73b
	struct aafilter a = { .port = -1 };
Packit d3f73b
	struct aafilter *res;
Packit d3f73b
	int fam = preferred_family;
Packit d3f73b
	struct filter *f = &current_filter;
Packit d3f73b
Packit d3f73b
	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
Packit d3f73b
		char *p;
Packit d3f73b
Packit d3f73b
		a.addr.family = AF_UNIX;
Packit d3f73b
		if (strncmp(addr, "unix:", 5) == 0)
Packit d3f73b
			addr += 5;
Packit d3f73b
		p = strdup(addr);
Packit d3f73b
		a.addr.bitlen = 8*strlen(p);
Packit d3f73b
		memcpy(a.addr.data, &p, sizeof(p));
Packit d3f73b
		fam = AF_UNIX;
Packit d3f73b
		goto out;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
Packit d3f73b
		a.addr.family = AF_PACKET;
Packit d3f73b
		a.addr.bitlen = 0;
Packit d3f73b
		if (strncmp(addr, "link:", 5) == 0)
Packit d3f73b
			addr += 5;
Packit d3f73b
		port = strchr(addr, ':');
Packit d3f73b
		if (port) {
Packit d3f73b
			*port = 0;
Packit d3f73b
			if (port[1] && strcmp(port+1, "*")) {
Packit d3f73b
				if (get_integer(&a.port, port+1, 0)) {
Packit d3f73b
					if ((a.port = xll_name_to_index(port+1)) <= 0)
Packit d3f73b
						return NULL;
Packit d3f73b
				}
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
		if (addr[0] && strcmp(addr, "*")) {
Packit d3f73b
			unsigned short tmp;
Packit d3f73b
Packit d3f73b
			a.addr.bitlen = 32;
Packit d3f73b
			if (ll_proto_a2n(&tmp, addr))
Packit d3f73b
				return NULL;
Packit d3f73b
			a.addr.data[0] = ntohs(tmp);
Packit d3f73b
		}
Packit d3f73b
		fam = AF_PACKET;
Packit d3f73b
		goto out;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
Packit d3f73b
		a.addr.family = AF_NETLINK;
Packit d3f73b
		a.addr.bitlen = 0;
Packit d3f73b
		if (strncmp(addr, "netlink:", 8) == 0)
Packit d3f73b
			addr += 8;
Packit d3f73b
		port = strchr(addr, ':');
Packit d3f73b
		if (port) {
Packit d3f73b
			*port = 0;
Packit d3f73b
			if (port[1] && strcmp(port+1, "*")) {
Packit d3f73b
				if (get_integer(&a.port, port+1, 0)) {
Packit d3f73b
					if (strcmp(port+1, "kernel") == 0)
Packit d3f73b
						a.port = 0;
Packit d3f73b
					else
Packit d3f73b
						return NULL;
Packit d3f73b
				}
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
		if (addr[0] && strcmp(addr, "*")) {
Packit d3f73b
			a.addr.bitlen = 32;
Packit d3f73b
			if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
Packit d3f73b
				return NULL;
Packit d3f73b
		}
Packit d3f73b
		fam = AF_NETLINK;
Packit d3f73b
		goto out;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
Packit d3f73b
		__u32 cid = ~(__u32)0;
Packit d3f73b
Packit d3f73b
		a.addr.family = AF_VSOCK;
Packit d3f73b
		if (strncmp(addr, "vsock:", 6) == 0)
Packit d3f73b
			addr += 6;
Packit d3f73b
Packit d3f73b
		if (is_port)
Packit d3f73b
			port = addr;
Packit d3f73b
		else {
Packit d3f73b
			port = strchr(addr, ':');
Packit d3f73b
			if (port) {
Packit d3f73b
				*port = '\0';
Packit d3f73b
				port++;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (port && strcmp(port, "*") &&
Packit d3f73b
		    get_u32((__u32 *)&a.port, port, 0))
Packit d3f73b
			return NULL;
Packit d3f73b
Packit d3f73b
		if (addr[0] && strcmp(addr, "*")) {
Packit d3f73b
			a.addr.bitlen = 32;
Packit d3f73b
			if (get_u32(&cid, addr, 0))
Packit d3f73b
				return NULL;
Packit d3f73b
		}
Packit d3f73b
		vsock_set_inet_prefix(&a.addr, cid);
Packit d3f73b
		fam = AF_VSOCK;
Packit d3f73b
		goto out;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
Packit d3f73b
		fam = AF_INET;
Packit d3f73b
		if (!strncmp(addr, "inet:", 5))
Packit d3f73b
			addr += 5;
Packit d3f73b
	} else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
Packit d3f73b
		fam = AF_INET6;
Packit d3f73b
		if (!strncmp(addr, "inet6:", 6))
Packit d3f73b
			addr += 6;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	/* URL-like literal [] */
Packit d3f73b
	if (addr[0] == '[') {
Packit d3f73b
		addr++;
Packit d3f73b
		if ((port = strchr(addr, ']')) == NULL)
Packit d3f73b
			return NULL;
Packit d3f73b
		*port++ = 0;
Packit d3f73b
	} else if (addr[0] == '*') {
Packit d3f73b
		port = addr+1;
Packit d3f73b
	} else {
Packit d3f73b
		port = strrchr(strchr(addr, '/') ? : addr, ':');
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (is_port)
Packit d3f73b
		port = addr;
Packit d3f73b
Packit d3f73b
	if (port && *port) {
Packit d3f73b
		if (*port == ':')
Packit d3f73b
			*port++ = 0;
Packit d3f73b
Packit d3f73b
		if (*port && *port != '*') {
Packit d3f73b
			if (get_integer(&a.port, port, 0)) {
Packit d3f73b
				struct servent *se1 = NULL;
Packit d3f73b
				struct servent *se2 = NULL;
Packit d3f73b
Packit d3f73b
				if (current_filter.dbs&(1<
Packit d3f73b
					se1 = getservbyname(port, UDP_PROTO);
Packit d3f73b
				if (current_filter.dbs&(1<
Packit d3f73b
					se2 = getservbyname(port, TCP_PROTO);
Packit d3f73b
				if (se1 && se2 && se1->s_port != se2->s_port) {
Packit d3f73b
					fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
Packit d3f73b
					return NULL;
Packit d3f73b
				}
Packit d3f73b
				if (!se1)
Packit d3f73b
					se1 = se2;
Packit d3f73b
				if (se1) {
Packit d3f73b
					a.port = ntohs(se1->s_port);
Packit d3f73b
				} else {
Packit d3f73b
					struct scache *s;
Packit d3f73b
Packit d3f73b
					for (s = rlist; s; s = s->next) {
Packit d3f73b
						if ((s->proto == UDP_PROTO &&
Packit d3f73b
						     (current_filter.dbs&(1<
Packit d3f73b
						    (s->proto == TCP_PROTO &&
Packit d3f73b
						     (current_filter.dbs&(1<
Packit d3f73b
							if (s->name && strcmp(s->name, port) == 0) {
Packit d3f73b
								if (a.port > 0 && a.port != s->port) {
Packit d3f73b
									fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
Packit d3f73b
									return NULL;
Packit d3f73b
								}
Packit d3f73b
								a.port = s->port;
Packit d3f73b
							}
Packit d3f73b
						}
Packit d3f73b
					}
Packit d3f73b
					if (a.port <= 0) {
Packit d3f73b
						fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
Packit d3f73b
						return NULL;
Packit d3f73b
					}
Packit d3f73b
				}
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
	if (!is_port && *addr && *addr != '*') {
Packit d3f73b
		if (get_prefix_1(&a.addr, addr, fam)) {
Packit d3f73b
			if (get_dns_host(&a, addr, fam)) {
Packit d3f73b
				fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
Packit d3f73b
				return NULL;
Packit d3f73b
			}
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
out:
Packit d3f73b
	if (fam != AF_UNSPEC) {
Packit d3f73b
		int states = f->states;
Packit d3f73b
		f->families = 0;
Packit d3f73b
		filter_af_set(f, fam);
Packit d3f73b
		filter_states_set(f, states);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	res = malloc(sizeof(*res));
Packit d3f73b
	if (res)
Packit d3f73b
		memcpy(res, &a, sizeof(a));
Packit d3f73b
	return res;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
void *parse_markmask(const char *markmask)
Packit d3f73b
{
Packit d3f73b
	struct aafilter a, *res;
Packit d3f73b
Packit d3f73b
	if (strchr(markmask, '/')) {
Packit d3f73b
		if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2)
Packit d3f73b
			return NULL;
Packit d3f73b
	} else {
Packit d3f73b
		a.mask = 0xffffffff;
Packit d3f73b
		if (sscanf(markmask, "%i", &a.mark) != 1)
Packit d3f73b
			return NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	res = malloc(sizeof(*res));
Packit d3f73b
	if (res)
Packit d3f73b
		memcpy(res, &a, sizeof(a));
Packit d3f73b
	return res;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void proc_ctx_print(struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	char *buf;
Packit d3f73b
Packit d3f73b
	if (show_proc_ctx || show_sock_ctx) {
Packit d3f73b
		if (find_entry(s->ino, &buf,
Packit d3f73b
				(show_proc_ctx & show_sock_ctx) ?
Packit d3f73b
				PROC_SOCK_CTX : PROC_CTX) > 0) {
Packit d3f73b
			out(" users:(%s)", buf);
Packit d3f73b
			free(buf);
Packit d3f73b
		}
Packit d3f73b
	} else if (show_users) {
Packit d3f73b
		if (find_entry(s->ino, &buf, USERS) > 0) {
Packit d3f73b
			out(" users:(%s)", buf);
Packit d3f73b
			free(buf);
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void inet_stats_print(struct sockstat *s, bool v6only)
Packit d3f73b
{
Packit d3f73b
	sock_state_print(s);
Packit d3f73b
Packit d3f73b
	inet_addr_print(&s->local, s->lport, s->iface, v6only);
Packit d3f73b
	inet_addr_print(&s->remote, s->rport, 0, v6only);
Packit d3f73b
Packit d3f73b
	proc_ctx_print(s);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
Packit d3f73b
		sockstat * s)
Packit d3f73b
{
Packit d3f73b
	s->local.family = s->remote.family = family;
Packit d3f73b
	if (family == AF_INET) {
Packit d3f73b
		sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
Packit d3f73b
		sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
Packit d3f73b
		s->local.bytelen = s->remote.bytelen = 4;
Packit d3f73b
		return 0;
Packit d3f73b
	} else {
Packit d3f73b
		sscanf(loc, "%08x%08x%08x%08x:%x",
Packit d3f73b
		       s->local.data,
Packit d3f73b
		       s->local.data + 1,
Packit d3f73b
		       s->local.data + 2,
Packit d3f73b
		       s->local.data + 3,
Packit d3f73b
		       &s->lport);
Packit d3f73b
		sscanf(rem, "%08x%08x%08x%08x:%x",
Packit d3f73b
		       s->remote.data,
Packit d3f73b
		       s->remote.data + 1,
Packit d3f73b
		       s->remote.data + 2,
Packit d3f73b
		       s->remote.data + 3,
Packit d3f73b
		       &s->rport);
Packit d3f73b
		s->local.bytelen = s->remote.bytelen = 16;
Packit d3f73b
		return 0;
Packit d3f73b
	}
Packit d3f73b
	return -1;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
Packit d3f73b
{
Packit d3f73b
	char *p;
Packit d3f73b
Packit d3f73b
	if ((p = strchr(line, ':')) == NULL)
Packit d3f73b
		return -1;
Packit d3f73b
Packit d3f73b
	*loc = p+2;
Packit d3f73b
	if ((p = strchr(*loc, ':')) == NULL)
Packit d3f73b
		return -1;
Packit d3f73b
Packit d3f73b
	p[5] = 0;
Packit d3f73b
	*rem = p+6;
Packit d3f73b
	if ((p = strchr(*rem, ':')) == NULL)
Packit d3f73b
		return -1;
Packit d3f73b
Packit d3f73b
	p[5] = 0;
Packit d3f73b
	*data = p+6;
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static char *sprint_bw(char *buf, double bw)
Packit d3f73b
{
Packit d3f73b
	if (numeric)
Packit d3f73b
		sprintf(buf, "%.0f", bw);
Packit d3f73b
	else if (bw > 1000000.)
Packit d3f73b
		sprintf(buf, "%.1fM", bw / 1000000.);
Packit d3f73b
	else if (bw > 1000.)
Packit d3f73b
		sprintf(buf, "%.1fK", bw / 1000.);
Packit d3f73b
	else
Packit d3f73b
		sprintf(buf, "%g", bw);
Packit d3f73b
Packit d3f73b
	return buf;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sctp_stats_print(struct sctp_info *s)
Packit d3f73b
{
Packit d3f73b
	if (s->sctpi_tag)
Packit d3f73b
		out(" tag:%x", s->sctpi_tag);
Packit d3f73b
	if (s->sctpi_state)
Packit d3f73b
		out(" state:%s", sctp_sstate_name[s->sctpi_state]);
Packit d3f73b
	if (s->sctpi_rwnd)
Packit d3f73b
		out(" rwnd:%d", s->sctpi_rwnd);
Packit d3f73b
	if (s->sctpi_unackdata)
Packit d3f73b
		out(" unackdata:%d", s->sctpi_unackdata);
Packit d3f73b
	if (s->sctpi_penddata)
Packit d3f73b
		out(" penddata:%d", s->sctpi_penddata);
Packit d3f73b
	if (s->sctpi_instrms)
Packit d3f73b
		out(" instrms:%d", s->sctpi_instrms);
Packit d3f73b
	if (s->sctpi_outstrms)
Packit d3f73b
		out(" outstrms:%d", s->sctpi_outstrms);
Packit d3f73b
	if (s->sctpi_inqueue)
Packit d3f73b
		out(" inqueue:%d", s->sctpi_inqueue);
Packit d3f73b
	if (s->sctpi_outqueue)
Packit d3f73b
		out(" outqueue:%d", s->sctpi_outqueue);
Packit d3f73b
	if (s->sctpi_overall_error)
Packit d3f73b
		out(" overerr:%d", s->sctpi_overall_error);
Packit d3f73b
	if (s->sctpi_max_burst)
Packit d3f73b
		out(" maxburst:%d", s->sctpi_max_burst);
Packit d3f73b
	if (s->sctpi_maxseg)
Packit d3f73b
		out(" maxseg:%d", s->sctpi_maxseg);
Packit d3f73b
	if (s->sctpi_peer_rwnd)
Packit d3f73b
		out(" prwnd:%d", s->sctpi_peer_rwnd);
Packit d3f73b
	if (s->sctpi_peer_tag)
Packit d3f73b
		out(" ptag:%x", s->sctpi_peer_tag);
Packit d3f73b
	if (s->sctpi_peer_capable)
Packit d3f73b
		out(" pcapable:%d", s->sctpi_peer_capable);
Packit d3f73b
	if (s->sctpi_peer_sack)
Packit d3f73b
		out(" psack:%d", s->sctpi_peer_sack);
Packit d3f73b
	if (s->sctpi_s_autoclose)
Packit d3f73b
		out(" autoclose:%d", s->sctpi_s_autoclose);
Packit d3f73b
	if (s->sctpi_s_adaptation_ind)
Packit d3f73b
		out(" adapind:%d", s->sctpi_s_adaptation_ind);
Packit d3f73b
	if (s->sctpi_s_pd_point)
Packit d3f73b
		out(" pdpoint:%d", s->sctpi_s_pd_point);
Packit d3f73b
	if (s->sctpi_s_nodelay)
Packit d3f73b
		out(" nodelay:%d", s->sctpi_s_nodelay);
Packit d3f73b
	if (s->sctpi_s_disable_fragments)
Packit d3f73b
		out(" nofrag:%d", s->sctpi_s_disable_fragments);
Packit d3f73b
	if (s->sctpi_s_v4mapped)
Packit d3f73b
		out(" v4mapped:%d", s->sctpi_s_v4mapped);
Packit d3f73b
	if (s->sctpi_s_frag_interleave)
Packit d3f73b
		out(" fraginl:%d", s->sctpi_s_frag_interleave);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void tcp_stats_print(struct tcpstat *s)
Packit d3f73b
{
Packit d3f73b
	char b1[64];
Packit d3f73b
Packit d3f73b
	if (s->has_ts_opt)
Packit d3f73b
		out(" ts");
Packit d3f73b
	if (s->has_sack_opt)
Packit d3f73b
		out(" sack");
Packit d3f73b
	if (s->has_ecn_opt)
Packit d3f73b
		out(" ecn");
Packit d3f73b
	if (s->has_ecnseen_opt)
Packit d3f73b
		out(" ecnseen");
Packit d3f73b
	if (s->has_fastopen_opt)
Packit d3f73b
		out(" fastopen");
Packit d3f73b
	if (s->cong_alg[0])
Packit d3f73b
		out(" %s", s->cong_alg);
Packit d3f73b
	if (s->has_wscale_opt)
Packit d3f73b
		out(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale);
Packit d3f73b
	if (s->rto)
Packit d3f73b
		out(" rto:%g", s->rto);
Packit d3f73b
	if (s->backoff)
Packit d3f73b
		out(" backoff:%u", s->backoff);
Packit d3f73b
	if (s->rtt)
Packit d3f73b
		out(" rtt:%g/%g", s->rtt, s->rttvar);
Packit d3f73b
	if (s->ato)
Packit d3f73b
		out(" ato:%g", s->ato);
Packit d3f73b
Packit d3f73b
	if (s->qack)
Packit d3f73b
		out(" qack:%d", s->qack);
Packit d3f73b
	if (s->qack & 1)
Packit d3f73b
		out(" bidir");
Packit d3f73b
Packit d3f73b
	if (s->mss)
Packit d3f73b
		out(" mss:%d", s->mss);
Packit d3f73b
	if (s->pmtu)
Packit d3f73b
		out(" pmtu:%u", s->pmtu);
Packit d3f73b
	if (s->rcv_mss)
Packit d3f73b
		out(" rcvmss:%d", s->rcv_mss);
Packit d3f73b
	if (s->advmss)
Packit d3f73b
		out(" advmss:%d", s->advmss);
Packit d3f73b
	if (s->cwnd)
Packit d3f73b
		out(" cwnd:%u", s->cwnd);
Packit d3f73b
	if (s->ssthresh)
Packit d3f73b
		out(" ssthresh:%d", s->ssthresh);
Packit d3f73b
Packit d3f73b
	if (s->bytes_sent)
Packit d3f73b
		out(" bytes_sent:%llu", s->bytes_sent);
Packit d3f73b
	if (s->bytes_retrans)
Packit d3f73b
		out(" bytes_retrans:%llu", s->bytes_retrans);
Packit d3f73b
	if (s->bytes_acked)
Packit d3f73b
		out(" bytes_acked:%llu", s->bytes_acked);
Packit d3f73b
	if (s->bytes_received)
Packit d3f73b
		out(" bytes_received:%llu", s->bytes_received);
Packit d3f73b
	if (s->segs_out)
Packit d3f73b
		out(" segs_out:%u", s->segs_out);
Packit d3f73b
	if (s->segs_in)
Packit d3f73b
		out(" segs_in:%u", s->segs_in);
Packit d3f73b
	if (s->data_segs_out)
Packit d3f73b
		out(" data_segs_out:%u", s->data_segs_out);
Packit d3f73b
	if (s->data_segs_in)
Packit d3f73b
		out(" data_segs_in:%u", s->data_segs_in);
Packit d3f73b
Packit d3f73b
	if (s->dctcp && s->dctcp->enabled) {
Packit d3f73b
		struct dctcpstat *dctcp = s->dctcp;
Packit d3f73b
Packit d3f73b
		out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
Packit d3f73b
			     dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
Packit d3f73b
			     dctcp->ab_tot);
Packit d3f73b
	} else if (s->dctcp) {
Packit d3f73b
		out(" dctcp:fallback_mode");
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (s->bbr_info) {
Packit d3f73b
		__u64 bw;
Packit d3f73b
Packit d3f73b
		bw = s->bbr_info->bbr_bw_hi;
Packit d3f73b
		bw <<= 32;
Packit d3f73b
		bw |= s->bbr_info->bbr_bw_lo;
Packit d3f73b
Packit d3f73b
		out(" bbr:(bw:%sbps,mrtt:%g",
Packit d3f73b
		    sprint_bw(b1, bw * 8.0),
Packit d3f73b
		    (double)s->bbr_info->bbr_min_rtt / 1000.0);
Packit d3f73b
		if (s->bbr_info->bbr_pacing_gain)
Packit d3f73b
			out(",pacing_gain:%g",
Packit d3f73b
			    (double)s->bbr_info->bbr_pacing_gain / 256.0);
Packit d3f73b
		if (s->bbr_info->bbr_cwnd_gain)
Packit d3f73b
			out(",cwnd_gain:%g",
Packit d3f73b
			    (double)s->bbr_info->bbr_cwnd_gain / 256.0);
Packit d3f73b
		out(")");
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (s->send_bps)
Packit d3f73b
		out(" send %sbps", sprint_bw(b1, s->send_bps));
Packit d3f73b
	if (s->lastsnd)
Packit d3f73b
		out(" lastsnd:%u", s->lastsnd);
Packit d3f73b
	if (s->lastrcv)
Packit d3f73b
		out(" lastrcv:%u", s->lastrcv);
Packit d3f73b
	if (s->lastack)
Packit d3f73b
		out(" lastack:%u", s->lastack);
Packit d3f73b
Packit d3f73b
	if (s->pacing_rate) {
Packit d3f73b
		out(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate));
Packit d3f73b
		if (s->pacing_rate_max)
Packit d3f73b
			out("/%sbps", sprint_bw(b1, s->pacing_rate_max));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (s->delivery_rate)
Packit d3f73b
		out(" delivery_rate %sbps", sprint_bw(b1, s->delivery_rate));
Packit d3f73b
	if (s->delivered)
Packit d3f73b
		out(" delivered:%u", s->delivered);
Packit d3f73b
	if (s->delivered_ce)
Packit d3f73b
		out(" delivered_ce:%u", s->delivered_ce);
Packit d3f73b
	if (s->app_limited)
Packit d3f73b
		out(" app_limited");
Packit d3f73b
Packit d3f73b
	if (s->busy_time) {
Packit d3f73b
		out(" busy:%llums", s->busy_time / 1000);
Packit d3f73b
		if (s->rwnd_limited)
Packit d3f73b
			out(" rwnd_limited:%llums(%.1f%%)",
Packit d3f73b
			    s->rwnd_limited / 1000,
Packit d3f73b
			    100.0 * s->rwnd_limited / s->busy_time);
Packit d3f73b
		if (s->sndbuf_limited)
Packit d3f73b
			out(" sndbuf_limited:%llums(%.1f%%)",
Packit d3f73b
			    s->sndbuf_limited / 1000,
Packit d3f73b
			    100.0 * s->sndbuf_limited / s->busy_time);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (s->unacked)
Packit d3f73b
		out(" unacked:%u", s->unacked);
Packit d3f73b
	if (s->retrans || s->retrans_total)
Packit d3f73b
		out(" retrans:%u/%u", s->retrans, s->retrans_total);
Packit d3f73b
	if (s->lost)
Packit d3f73b
		out(" lost:%u", s->lost);
Packit d3f73b
	if (s->sacked && s->ss.state != SS_LISTEN)
Packit d3f73b
		out(" sacked:%u", s->sacked);
Packit d3f73b
	if (s->dsack_dups)
Packit d3f73b
		out(" dsack_dups:%u", s->dsack_dups);
Packit d3f73b
	if (s->fackets)
Packit d3f73b
		out(" fackets:%u", s->fackets);
Packit d3f73b
	if (s->reordering != 3)
Packit d3f73b
		out(" reordering:%d", s->reordering);
Packit d3f73b
	if (s->reord_seen)
Packit d3f73b
		out(" reord_seen:%d", s->reord_seen);
Packit d3f73b
	if (s->rcv_rtt)
Packit d3f73b
		out(" rcv_rtt:%g", s->rcv_rtt);
Packit d3f73b
	if (s->rcv_space)
Packit d3f73b
		out(" rcv_space:%d", s->rcv_space);
Packit d3f73b
	if (s->rcv_ssthresh)
Packit d3f73b
		out(" rcv_ssthresh:%u", s->rcv_ssthresh);
Packit d3f73b
	if (s->not_sent)
Packit d3f73b
		out(" notsent:%u", s->not_sent);
Packit d3f73b
	if (s->min_rtt)
Packit d3f73b
		out(" minrtt:%g", s->min_rtt);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void tcp_timer_print(struct tcpstat *s)
Packit d3f73b
{
Packit d3f73b
	static const char * const tmr_name[] = {
Packit d3f73b
		"off",
Packit d3f73b
		"on",
Packit d3f73b
		"keepalive",
Packit d3f73b
		"timewait",
Packit d3f73b
		"persist",
Packit d3f73b
		"unknown"
Packit d3f73b
	};
Packit d3f73b
Packit d3f73b
	if (s->timer) {
Packit d3f73b
		if (s->timer > 4)
Packit d3f73b
			s->timer = 5;
Packit d3f73b
		out(" timer:(%s,%s,%d)",
Packit d3f73b
			     tmr_name[s->timer],
Packit d3f73b
			     print_ms_timer(s->timeout),
Packit d3f73b
			     s->retrans);
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sctp_timer_print(struct tcpstat *s)
Packit d3f73b
{
Packit d3f73b
	if (s->timer)
Packit d3f73b
		out(" timer:(T3_RTX,%s,%d)",
Packit d3f73b
		    print_ms_timer(s->timeout), s->retrans);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int tcp_show_line(char *line, const struct filter *f, int family)
Packit d3f73b
{
Packit d3f73b
	int rto = 0, ato = 0;
Packit d3f73b
	struct tcpstat s = {};
Packit d3f73b
	char *loc, *rem, *data;
Packit d3f73b
	char opt[256];
Packit d3f73b
	int n;
Packit d3f73b
	int hz = get_user_hz();
Packit d3f73b
Packit d3f73b
	if (proc_inet_split_line(line, &loc, &rem, &data))
Packit d3f73b
		return -1;
Packit d3f73b
Packit d3f73b
	int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
Packit d3f73b
Packit d3f73b
	if (!(f->states & (1 << state)))
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	proc_parse_inet_addr(loc, rem, family, &s.ss);
Packit d3f73b
Packit d3f73b
	if (f->f && run_ssfilter(f->f, &s.ss) == 0)
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	opt[0] = 0;
Packit d3f73b
	n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n",
Packit d3f73b
		   &s.ss.state, &s.ss.wq, &s.ss.rq,
Packit d3f73b
		   &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes,
Packit d3f73b
		   &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd,
Packit d3f73b
		   &s.ssthresh, opt);
Packit d3f73b
Packit d3f73b
	if (n < 17)
Packit d3f73b
		opt[0] = 0;
Packit d3f73b
Packit d3f73b
	if (n < 12) {
Packit d3f73b
		rto = 0;
Packit d3f73b
		s.cwnd = 2;
Packit d3f73b
		s.ssthresh = -1;
Packit d3f73b
		ato = s.qack = 0;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	s.retrans   = s.timer != 1 ? s.probes : s.retrans;
Packit d3f73b
	s.timeout   = (s.timeout * 1000 + hz - 1) / hz;
Packit d3f73b
	s.ato	    = (double)ato / hz;
Packit d3f73b
	s.qack	   /= 2;
Packit d3f73b
	s.rto	    = (double)rto;
Packit d3f73b
	s.ssthresh  = s.ssthresh == -1 ? 0 : s.ssthresh;
Packit d3f73b
	s.rto	    = s.rto != 3 * hz  ? s.rto / hz : 0;
Packit d3f73b
	s.ss.type   = IPPROTO_TCP;
Packit d3f73b
Packit d3f73b
	inet_stats_print(&s.ss, false);
Packit d3f73b
Packit d3f73b
	if (show_options)
Packit d3f73b
		tcp_timer_print(&s);
Packit d3f73b
Packit d3f73b
	if (show_details) {
Packit d3f73b
		sock_details_print(&s.ss);
Packit d3f73b
		if (opt[0])
Packit d3f73b
			out(" opt:\"%s\"", opt);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tcpinfo)
Packit d3f73b
		tcp_stats_print(&s);
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int generic_record_read(FILE *fp,
Packit d3f73b
			       int (*worker)(char*, const struct filter *, int),
Packit d3f73b
			       const struct filter *f, int fam)
Packit d3f73b
{
Packit d3f73b
	char line[256];
Packit d3f73b
Packit d3f73b
	/* skip header */
Packit d3f73b
	if (fgets(line, sizeof(line), fp) == NULL)
Packit d3f73b
		goto outerr;
Packit d3f73b
Packit d3f73b
	while (fgets(line, sizeof(line), fp) != NULL) {
Packit d3f73b
		int n = strlen(line);
Packit d3f73b
Packit d3f73b
		if (n == 0 || line[n-1] != '\n') {
Packit d3f73b
			errno = -EINVAL;
Packit d3f73b
			return -1;
Packit d3f73b
		}
Packit d3f73b
		line[n-1] = 0;
Packit d3f73b
Packit d3f73b
		if (worker(line, f, fam) < 0)
Packit d3f73b
			return 0;
Packit d3f73b
	}
Packit d3f73b
outerr:
Packit d3f73b
Packit d3f73b
	return ferror(fp) ? -1 : 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void print_skmeminfo(struct rtattr *tb[], int attrtype)
Packit d3f73b
{
Packit d3f73b
	const __u32 *skmeminfo;
Packit d3f73b
Packit d3f73b
	if (!tb[attrtype]) {
Packit d3f73b
		if (attrtype == INET_DIAG_SKMEMINFO) {
Packit d3f73b
			if (!tb[INET_DIAG_MEMINFO])
Packit d3f73b
				return;
Packit d3f73b
Packit d3f73b
			const struct inet_diag_meminfo *minfo =
Packit d3f73b
				RTA_DATA(tb[INET_DIAG_MEMINFO]);
Packit d3f73b
Packit d3f73b
			out(" mem:(r%u,w%u,f%u,t%u)",
Packit d3f73b
				   minfo->idiag_rmem,
Packit d3f73b
				   minfo->idiag_wmem,
Packit d3f73b
				   minfo->idiag_fmem,
Packit d3f73b
				   minfo->idiag_tmem);
Packit d3f73b
		}
Packit d3f73b
		return;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	skmeminfo = RTA_DATA(tb[attrtype]);
Packit d3f73b
Packit d3f73b
	out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
Packit d3f73b
		     skmeminfo[SK_MEMINFO_RMEM_ALLOC],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_RCVBUF],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_WMEM_ALLOC],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_SNDBUF],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_FWD_ALLOC],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_WMEM_QUEUED],
Packit d3f73b
		     skmeminfo[SK_MEMINFO_OPTMEM]);
Packit d3f73b
Packit d3f73b
	if (RTA_PAYLOAD(tb[attrtype]) >=
Packit d3f73b
		(SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
Packit d3f73b
		out(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
Packit d3f73b
Packit d3f73b
	if (RTA_PAYLOAD(tb[attrtype]) >=
Packit d3f73b
		(SK_MEMINFO_DROPS + 1) * sizeof(__u32))
Packit d3f73b
		out(",d%u", skmeminfo[SK_MEMINFO_DROPS]);
Packit d3f73b
Packit d3f73b
	out(")");
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void print_md5sig(struct tcp_diag_md5sig *sig)
Packit d3f73b
{
Packit d3f73b
	out("%s/%d=",
Packit d3f73b
	    format_host(sig->tcpm_family,
Packit d3f73b
			sig->tcpm_family == AF_INET6 ? 16 : 4,
Packit d3f73b
			&sig->tcpm_addr),
Packit d3f73b
	    sig->tcpm_prefixlen);
Packit d3f73b
	print_escape_buf(sig->tcpm_key, sig->tcpm_keylen, " ,");
Packit d3f73b
}
Packit d3f73b
Packit Service 728d12
static void tcp_tls_version(struct rtattr *attr)
Packit Service 728d12
{
Packit Service 728d12
	u_int16_t val;
Packit Service 728d12
Packit Service 728d12
	if (!attr)
Packit Service 728d12
		return;
Packit Service 728d12
	val = rta_getattr_u16(attr);
Packit Service 728d12
Packit Service 728d12
	switch (val) {
Packit Service 728d12
	case TLS_1_2_VERSION:
Packit Service 728d12
		out(" version: 1.2");
Packit Service 728d12
		break;
Packit Service 728d12
	case TLS_1_3_VERSION:
Packit Service 728d12
		out(" version: 1.3");
Packit Service 728d12
		break;
Packit Service 728d12
	default:
Packit Service 728d12
		out(" version: unknown(%hu)", val);
Packit Service 728d12
		break;
Packit Service 728d12
	}
Packit Service 728d12
}
Packit Service 728d12
Packit Service 728d12
static void tcp_tls_cipher(struct rtattr *attr)
Packit Service 728d12
{
Packit Service 728d12
	u_int16_t val;
Packit Service 728d12
Packit Service 728d12
	if (!attr)
Packit Service 728d12
		return;
Packit Service 728d12
	val = rta_getattr_u16(attr);
Packit Service 728d12
Packit Service 728d12
	switch (val) {
Packit Service 728d12
	case TLS_CIPHER_AES_GCM_128:
Packit Service 728d12
		out(" cipher: aes-gcm-128");
Packit Service 728d12
		break;
Packit Service 728d12
	case TLS_CIPHER_AES_GCM_256:
Packit Service 728d12
		out(" cipher: aes-gcm-256");
Packit Service 728d12
		break;
Packit Service 728d12
	}
Packit Service 728d12
}
Packit Service 728d12
Packit Service 728d12
static void tcp_tls_conf(const char *name, struct rtattr *attr)
Packit Service 728d12
{
Packit Service 728d12
	u_int16_t val;
Packit Service 728d12
Packit Service 728d12
	if (!attr)
Packit Service 728d12
		return;
Packit Service 728d12
	val = rta_getattr_u16(attr);
Packit Service 728d12
Packit Service 728d12
	switch (val) {
Packit Service 728d12
	case TLS_CONF_BASE:
Packit Service 728d12
		out(" %s: none", name);
Packit Service 728d12
		break;
Packit Service 728d12
	case TLS_CONF_SW:
Packit Service 728d12
		out(" %s: sw", name);
Packit Service 728d12
		break;
Packit Service 728d12
	case TLS_CONF_HW:
Packit Service 728d12
		out(" %s: hw", name);
Packit Service 728d12
		break;
Packit Service 728d12
	case TLS_CONF_HW_RECORD:
Packit Service 728d12
		out(" %s: hw-record", name);
Packit Service 728d12
		break;
Packit Service 728d12
	default:
Packit Service 728d12
		out(" %s: unknown(%hu)", name, val);
Packit Service 728d12
		break;
Packit Service 728d12
	}
Packit Service 728d12
}
Packit Service 728d12
Packit Service 8e2ce8
static void mptcp_subflow_info(struct rtattr *tb[])
Packit Service 8e2ce8
{
Packit Service 8e2ce8
	u_int32_t flags = 0;
Packit Service 8e2ce8
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_FLAGS]) {
Packit Service 8e2ce8
		char caps[32 + 1] = { 0 }, *cap = &caps[0];
Packit Service 8e2ce8
Packit Service 8e2ce8
		flags = rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_FLAGS]);
Packit Service 8e2ce8
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_MCAP_REM)
Packit Service 8e2ce8
			*cap++ = 'M';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_MCAP_LOC)
Packit Service 8e2ce8
			*cap++ = 'm';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_JOIN_REM)
Packit Service 8e2ce8
			*cap++ = 'J';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_JOIN_LOC)
Packit Service 8e2ce8
			*cap++ = 'j';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_BKUP_REM)
Packit Service 8e2ce8
			*cap++ = 'B';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_BKUP_LOC)
Packit Service 8e2ce8
			*cap++ = 'b';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED)
Packit Service 8e2ce8
			*cap++ = 'e';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_CONNECTED)
Packit Service 8e2ce8
			*cap++ = 'c';
Packit Service 8e2ce8
		if (flags & MPTCP_SUBFLOW_FLAG_MAPVALID)
Packit Service 8e2ce8
			*cap++ = 'v';
Packit Service 8e2ce8
		if (flags)
Packit Service 8e2ce8
			out(" flags:%s", caps);
Packit Service 8e2ce8
	}
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM] &&
Packit Service 8e2ce8
	    tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC] &&
Packit Service 8e2ce8
	    tb[MPTCP_SUBFLOW_ATTR_ID_REM] &&
Packit Service 8e2ce8
	    tb[MPTCP_SUBFLOW_ATTR_ID_LOC])
Packit Service 8e2ce8
		out(" token:%04x(id:%hhu)/%04x(id:%hhu)",
Packit Service 8e2ce8
		    rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM]),
Packit Service 8e2ce8
		    rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_REM]),
Packit Service 8e2ce8
		    rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC]),
Packit Service 8e2ce8
		    rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_LOC]));
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ])
Packit Service 8e2ce8
		out(" seq:%llx",
Packit Service 8e2ce8
		    rta_getattr_u64(tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ]));
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ])
Packit Service 8e2ce8
		out(" sfseq:%x",
Packit Service 8e2ce8
		    rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ]));
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET])
Packit Service 8e2ce8
		out(" ssnoff:%x",
Packit Service 8e2ce8
		    rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET]));
Packit Service 8e2ce8
	if (tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN])
Packit Service 8e2ce8
		out(" maplen:%x",
Packit Service 8e2ce8
		    rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN]));
Packit Service 8e2ce8
}
Packit Service 8e2ce8
Packit d3f73b
#define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
Packit d3f73b
Packit d3f73b
static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
Packit d3f73b
		struct rtattr *tb[])
Packit d3f73b
{
Packit d3f73b
	double rtt = 0;
Packit d3f73b
	struct tcpstat s = {};
Packit d3f73b
Packit d3f73b
	s.ss.state = r->idiag_state;
Packit d3f73b
Packit d3f73b
	print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
Packit d3f73b
Packit d3f73b
	if (tb[INET_DIAG_INFO]) {
Packit d3f73b
		struct tcp_info *info;
Packit d3f73b
		int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
Packit d3f73b
Packit d3f73b
		/* workaround for older kernels with less fields */
Packit d3f73b
		if (len < sizeof(*info)) {
Packit d3f73b
			info = alloca(sizeof(*info));
Packit d3f73b
			memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
Packit d3f73b
			memset((char *)info + len, 0, sizeof(*info) - len);
Packit d3f73b
		} else
Packit d3f73b
			info = RTA_DATA(tb[INET_DIAG_INFO]);
Packit d3f73b
Packit d3f73b
		if (show_options) {
Packit d3f73b
			s.has_ts_opt	   = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS);
Packit d3f73b
			s.has_sack_opt	   = TCPI_HAS_OPT(info, TCPI_OPT_SACK);
Packit d3f73b
			s.has_ecn_opt	   = TCPI_HAS_OPT(info, TCPI_OPT_ECN);
Packit d3f73b
			s.has_ecnseen_opt  = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN);
Packit d3f73b
			s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA);
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (tb[INET_DIAG_CONG])
Packit d3f73b
			strncpy(s.cong_alg,
Packit d3f73b
				rta_getattr_str(tb[INET_DIAG_CONG]),
Packit d3f73b
				sizeof(s.cong_alg) - 1);
Packit d3f73b
Packit d3f73b
		if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) {
Packit d3f73b
			s.has_wscale_opt  = true;
Packit d3f73b
			s.snd_wscale	  = info->tcpi_snd_wscale;
Packit d3f73b
			s.rcv_wscale	  = info->tcpi_rcv_wscale;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (info->tcpi_rto && info->tcpi_rto != 3000000)
Packit d3f73b
			s.rto = (double)info->tcpi_rto / 1000;
Packit d3f73b
Packit d3f73b
		s.backoff	 = info->tcpi_backoff;
Packit d3f73b
		s.rtt		 = (double)info->tcpi_rtt / 1000;
Packit d3f73b
		s.rttvar	 = (double)info->tcpi_rttvar / 1000;
Packit d3f73b
		s.ato		 = (double)info->tcpi_ato / 1000;
Packit d3f73b
		s.mss		 = info->tcpi_snd_mss;
Packit d3f73b
		s.rcv_mss	 = info->tcpi_rcv_mss;
Packit d3f73b
		s.advmss	 = info->tcpi_advmss;
Packit d3f73b
		s.rcv_space	 = info->tcpi_rcv_space;
Packit d3f73b
		s.rcv_rtt	 = (double)info->tcpi_rcv_rtt / 1000;
Packit d3f73b
		s.lastsnd	 = info->tcpi_last_data_sent;
Packit d3f73b
		s.lastrcv	 = info->tcpi_last_data_recv;
Packit d3f73b
		s.lastack	 = info->tcpi_last_ack_recv;
Packit d3f73b
		s.unacked	 = info->tcpi_unacked;
Packit d3f73b
		s.retrans	 = info->tcpi_retrans;
Packit d3f73b
		s.retrans_total  = info->tcpi_total_retrans;
Packit d3f73b
		s.lost		 = info->tcpi_lost;
Packit d3f73b
		s.sacked	 = info->tcpi_sacked;
Packit d3f73b
		s.fackets	 = info->tcpi_fackets;
Packit d3f73b
		s.reordering	 = info->tcpi_reordering;
Packit d3f73b
		s.rcv_ssthresh   = info->tcpi_rcv_ssthresh;
Packit d3f73b
		s.cwnd		 = info->tcpi_snd_cwnd;
Packit d3f73b
		s.pmtu		 = info->tcpi_pmtu;
Packit d3f73b
Packit d3f73b
		if (info->tcpi_snd_ssthresh < 0xFFFF)
Packit d3f73b
			s.ssthresh = info->tcpi_snd_ssthresh;
Packit d3f73b
Packit d3f73b
		rtt = (double) info->tcpi_rtt;
Packit d3f73b
		if (tb[INET_DIAG_VEGASINFO]) {
Packit d3f73b
			const struct tcpvegas_info *vinfo
Packit d3f73b
				= RTA_DATA(tb[INET_DIAG_VEGASINFO]);
Packit d3f73b
Packit d3f73b
			if (vinfo->tcpv_enabled &&
Packit d3f73b
					vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
Packit d3f73b
				rtt =  vinfo->tcpv_rtt;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (tb[INET_DIAG_DCTCPINFO]) {
Packit d3f73b
			struct dctcpstat *dctcp = malloc(sizeof(struct
Packit d3f73b
						dctcpstat));
Packit d3f73b
Packit d3f73b
			const struct tcp_dctcp_info *dinfo
Packit d3f73b
				= RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
Packit d3f73b
Packit d3f73b
			dctcp->enabled	= !!dinfo->dctcp_enabled;
Packit d3f73b
			dctcp->ce_state = dinfo->dctcp_ce_state;
Packit d3f73b
			dctcp->alpha	= dinfo->dctcp_alpha;
Packit d3f73b
			dctcp->ab_ecn	= dinfo->dctcp_ab_ecn;
Packit d3f73b
			dctcp->ab_tot	= dinfo->dctcp_ab_tot;
Packit d3f73b
			s.dctcp		= dctcp;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (tb[INET_DIAG_BBRINFO]) {
Packit d3f73b
			const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]);
Packit d3f73b
			int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]),
Packit d3f73b
				      sizeof(*s.bbr_info));
Packit d3f73b
Packit d3f73b
			s.bbr_info = calloc(1, sizeof(*s.bbr_info));
Packit d3f73b
			if (s.bbr_info && bbr_info)
Packit d3f73b
				memcpy(s.bbr_info, bbr_info, len);
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
Packit d3f73b
			s.send_bps = (double) info->tcpi_snd_cwnd *
Packit d3f73b
				(double)info->tcpi_snd_mss * 8000000. / rtt;
Packit d3f73b
		}
Packit d3f73b
Packit d3f73b
		if (info->tcpi_pacing_rate &&
Packit d3f73b
				info->tcpi_pacing_rate != ~0ULL) {
Packit d3f73b
			s.pacing_rate = info->tcpi_pacing_rate * 8.;
Packit d3f73b
Packit d3f73b
			if (info->tcpi_max_pacing_rate &&
Packit d3f73b
					info->tcpi_max_pacing_rate != ~0ULL)
Packit d3f73b
				s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
Packit d3f73b
		}
Packit d3f73b
		s.bytes_acked = info->tcpi_bytes_acked;
Packit d3f73b
		s.bytes_received = info->tcpi_bytes_received;
Packit d3f73b
		s.segs_out = info->tcpi_segs_out;
Packit d3f73b
		s.segs_in = info->tcpi_segs_in;
Packit d3f73b
		s.data_segs_out = info->tcpi_data_segs_out;
Packit d3f73b
		s.data_segs_in = info->tcpi_data_segs_in;
Packit d3f73b
		s.not_sent = info->tcpi_notsent_bytes;
Packit d3f73b
		if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U)
Packit d3f73b
			s.min_rtt = (double) info->tcpi_min_rtt / 1000;
Packit d3f73b
		s.delivery_rate = info->tcpi_delivery_rate * 8.;
Packit d3f73b
		s.app_limited = info->tcpi_delivery_rate_app_limited;
Packit d3f73b
		s.busy_time = info->tcpi_busy_time;
Packit d3f73b
		s.rwnd_limited = info->tcpi_rwnd_limited;
Packit d3f73b
		s.sndbuf_limited = info->tcpi_sndbuf_limited;
Packit d3f73b
		s.delivered = info->tcpi_delivered;
Packit d3f73b
		s.delivered_ce = info->tcpi_delivered_ce;
Packit d3f73b
		s.dsack_dups = info->tcpi_dsack_dups;
Packit d3f73b
		s.reord_seen = info->tcpi_reord_seen;
Packit d3f73b
		s.bytes_sent = info->tcpi_bytes_sent;
Packit d3f73b
		s.bytes_retrans = info->tcpi_bytes_retrans;
Packit d3f73b
		tcp_stats_print(&s);
Packit d3f73b
		free(s.dctcp);
Packit d3f73b
		free(s.bbr_info);
Packit d3f73b
	}
Packit d3f73b
	if (tb[INET_DIAG_MD5SIG]) {
Packit d3f73b
		struct tcp_diag_md5sig *sig = RTA_DATA(tb[INET_DIAG_MD5SIG]);
Packit d3f73b
		int len = RTA_PAYLOAD(tb[INET_DIAG_MD5SIG]);
Packit d3f73b
Packit d3f73b
		out(" md5keys:");
Packit d3f73b
		print_md5sig(sig++);
Packit d3f73b
		for (len -= sizeof(*sig); len > 0; len -= sizeof(*sig)) {
Packit d3f73b
			out(",");
Packit d3f73b
			print_md5sig(sig++);
Packit d3f73b
		}
Packit d3f73b
	}
Packit Service 8e2ce8
	if (tb[INET_DIAG_ULP_INFO]) {
Packit Service 8e2ce8
		struct rtattr *ulpinfo[INET_ULP_INFO_MAX + 1] = { 0 };
Packit Service 8e2ce8
Packit Service 8e2ce8
		parse_rtattr_nested(ulpinfo, INET_ULP_INFO_MAX,
Packit Service 8e2ce8
				    tb[INET_DIAG_ULP_INFO]);
Packit Service 8e2ce8
Packit Service 728d12
		if (ulpinfo[INET_ULP_INFO_NAME])
Packit Service 728d12
			out(" tcp-ulp-%s",
Packit Service 728d12
			    rta_getattr_str(ulpinfo[INET_ULP_INFO_NAME]));
Packit Service 728d12
Packit Service 728d12
		if (ulpinfo[INET_ULP_INFO_TLS]) {
Packit Service 728d12
			struct rtattr *tlsinfo[TLS_INFO_MAX + 1] = { 0 };
Packit Service 728d12
Packit Service 728d12
			parse_rtattr_nested(tlsinfo, TLS_INFO_MAX,
Packit Service 728d12
					    ulpinfo[INET_ULP_INFO_TLS]);
Packit Service 728d12
Packit Service 728d12
			tcp_tls_version(tlsinfo[TLS_INFO_VERSION]);
Packit Service 728d12
			tcp_tls_cipher(tlsinfo[TLS_INFO_CIPHER]);
Packit Service 728d12
			tcp_tls_conf("rxconf", tlsinfo[TLS_INFO_RXCONF]);
Packit Service 728d12
			tcp_tls_conf("txconf", tlsinfo[TLS_INFO_TXCONF]);
Packit Service 728d12
		}
Packit Service 8e2ce8
		if (ulpinfo[INET_ULP_INFO_MPTCP]) {
Packit Service 8e2ce8
			struct rtattr *sfinfo[MPTCP_SUBFLOW_ATTR_MAX + 1] =
Packit Service 8e2ce8
				{ 0 };
Packit Service 8e2ce8
Packit Service 8e2ce8
			parse_rtattr_nested(sfinfo, MPTCP_SUBFLOW_ATTR_MAX,
Packit Service 8e2ce8
					    ulpinfo[INET_ULP_INFO_MPTCP]);
Packit Service 8e2ce8
			mptcp_subflow_info(sfinfo);
Packit Service 8e2ce8
		}
Packit Service 8e2ce8
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static const char *format_host_sa(struct sockaddr_storage *sa)
Packit d3f73b
{
Packit d3f73b
	union {
Packit d3f73b
		struct sockaddr_in sin;
Packit d3f73b
		struct sockaddr_in6 sin6;
Packit d3f73b
	} *saddr = (void *)sa;
Packit d3f73b
Packit d3f73b
	switch (sa->ss_family) {
Packit d3f73b
	case AF_INET:
Packit d3f73b
		return format_host(AF_INET, 4, &saddr->sin.sin_addr);
Packit d3f73b
	case AF_INET6:
Packit d3f73b
		return format_host(AF_INET6, 16, &saddr->sin6.sin6_addr);
Packit d3f73b
	default:
Packit d3f73b
		return "";
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void sctp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
Packit d3f73b
		struct rtattr *tb[])
Packit d3f73b
{
Packit d3f73b
	struct sockaddr_storage *sa;
Packit d3f73b
	int len;
Packit d3f73b
Packit d3f73b
	print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
Packit d3f73b
Packit d3f73b
	if (tb[INET_DIAG_LOCALS]) {
Packit d3f73b
		len = RTA_PAYLOAD(tb[INET_DIAG_LOCALS]);
Packit d3f73b
		sa = RTA_DATA(tb[INET_DIAG_LOCALS]);
Packit d3f73b
Packit d3f73b
		out(" locals:%s", format_host_sa(sa));
Packit d3f73b
		for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa))
Packit d3f73b
			out(",%s", format_host_sa(sa));
Packit d3f73b
Packit d3f73b
	}
Packit d3f73b
	if (tb[INET_DIAG_PEERS]) {
Packit d3f73b
		len = RTA_PAYLOAD(tb[INET_DIAG_PEERS]);
Packit d3f73b
		sa = RTA_DATA(tb[INET_DIAG_PEERS]);
Packit d3f73b
Packit d3f73b
		out(" peers:%s", format_host_sa(sa));
Packit d3f73b
		for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa))
Packit d3f73b
			out(",%s", format_host_sa(sa));
Packit d3f73b
	}
Packit d3f73b
	if (tb[INET_DIAG_INFO]) {
Packit d3f73b
		struct sctp_info *info;
Packit d3f73b
		len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
Packit d3f73b
Packit d3f73b
		/* workaround for older kernels with less fields */
Packit d3f73b
		if (len < sizeof(*info)) {
Packit d3f73b
			info = alloca(sizeof(*info));
Packit d3f73b
			memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
Packit d3f73b
			memset((char *)info + len, 0, sizeof(*info) - len);
Packit d3f73b
		} else
Packit d3f73b
			info = RTA_DATA(tb[INET_DIAG_INFO]);
Packit d3f73b
Packit d3f73b
		sctp_stats_print(info);
Packit d3f73b
	}
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	struct rtattr *tb[INET_DIAG_MAX+1];
Packit d3f73b
	struct inet_diag_msg *r = NLMSG_DATA(nlh);
Packit d3f73b
Packit d3f73b
	parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
Packit d3f73b
		     nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
Packit d3f73b
Packit d3f73b
	s->state	= r->idiag_state;
Packit d3f73b
	s->local.family	= s->remote.family = r->idiag_family;
Packit d3f73b
	s->lport	= ntohs(r->id.idiag_sport);
Packit d3f73b
	s->rport	= ntohs(r->id.idiag_dport);
Packit d3f73b
	s->wq		= r->idiag_wqueue;
Packit d3f73b
	s->rq		= r->idiag_rqueue;
Packit d3f73b
	s->ino		= r->idiag_inode;
Packit d3f73b
	s->uid		= r->idiag_uid;
Packit d3f73b
	s->iface	= r->id.idiag_if;
Packit d3f73b
	s->sk		= cookie_sk_get(&r->id.idiag_cookie[0]);
Packit d3f73b
Packit d3f73b
	s->mark = 0;
Packit d3f73b
	if (tb[INET_DIAG_MARK])
Packit d3f73b
		s->mark = rta_getattr_u32(tb[INET_DIAG_MARK]);
Packit d3f73b
	if (tb[INET_DIAG_PROTOCOL])
Packit d3f73b
		s->raw_prot = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]);
Packit d3f73b
	else
Packit d3f73b
		s->raw_prot = 0;
Packit d3f73b
Packit d3f73b
	if (s->local.family == AF_INET)
Packit d3f73b
		s->local.bytelen = s->remote.bytelen = 4;
Packit d3f73b
	else
Packit d3f73b
		s->local.bytelen = s->remote.bytelen = 16;
Packit d3f73b
Packit d3f73b
	memcpy(s->local.data, r->id.idiag_src, s->local.bytelen);
Packit d3f73b
	memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int inet_show_sock(struct nlmsghdr *nlh,
Packit d3f73b
			  struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	struct rtattr *tb[INET_DIAG_MAX+1];
Packit d3f73b
	struct inet_diag_msg *r = NLMSG_DATA(nlh);
Packit d3f73b
	unsigned char v6only = 0;
Packit d3f73b
Packit d3f73b
	parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
Packit d3f73b
		     nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
Packit d3f73b
Packit d3f73b
	if (tb[INET_DIAG_PROTOCOL])
Packit d3f73b
		s->type = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]);
Packit d3f73b
Packit d3f73b
	if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY])
Packit d3f73b
		v6only = rta_getattr_u8(tb[INET_DIAG_SKV6ONLY]);
Packit d3f73b
Packit d3f73b
	inet_stats_print(s, v6only);
Packit d3f73b
Packit d3f73b
	if (show_options) {
Packit d3f73b
		struct tcpstat t = {};
Packit d3f73b
Packit d3f73b
		t.timer = r->idiag_timer;
Packit d3f73b
		t.timeout = r->idiag_expires;
Packit d3f73b
		t.retrans = r->idiag_retrans;
Packit d3f73b
		if (s->type == IPPROTO_SCTP)
Packit d3f73b
			sctp_timer_print(&t);
Packit d3f73b
		else
Packit d3f73b
			tcp_timer_print(&t);
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_details) {
Packit d3f73b
		sock_details_print(s);
Packit d3f73b
		if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY])
Packit d3f73b
			out(" v6only:%u", v6only);
Packit d3f73b
Packit d3f73b
		if (tb[INET_DIAG_SHUTDOWN]) {
Packit d3f73b
			unsigned char mask;
Packit d3f73b
Packit d3f73b
			mask = rta_getattr_u8(tb[INET_DIAG_SHUTDOWN]);
Packit d3f73b
			out(" %c-%c",
Packit d3f73b
			    mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tos) {
Packit d3f73b
		if (tb[INET_DIAG_TOS])
Packit d3f73b
			out(" tos:%#x", rta_getattr_u8(tb[INET_DIAG_TOS]));
Packit d3f73b
		if (tb[INET_DIAG_TCLASS])
Packit d3f73b
			out(" tclass:%#x", rta_getattr_u8(tb[INET_DIAG_TCLASS]));
Packit d3f73b
		if (tb[INET_DIAG_CLASS_ID])
Packit d3f73b
			out(" class_id:%#x", rta_getattr_u32(tb[INET_DIAG_CLASS_ID]));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) {
Packit d3f73b
		if (!oneline)
Packit d3f73b
			out("\n\t");
Packit d3f73b
		if (s->type == IPPROTO_SCTP)
Packit d3f73b
			sctp_show_info(nlh, r, tb);
Packit d3f73b
		else
Packit d3f73b
			tcp_show_info(nlh, r, tb);
Packit d3f73b
	}
Packit d3f73b
	sctp_ino = s->ino;
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int tcpdiag_send(int fd, int protocol, struct filter *f)
Packit d3f73b
{
Packit d3f73b
	struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
Packit d3f73b
	struct {
Packit d3f73b
		struct nlmsghdr nlh;
Packit d3f73b
		struct inet_diag_req r;
Packit d3f73b
	} req = {
Packit d3f73b
		.nlh.nlmsg_len = sizeof(req),
Packit d3f73b
		.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
Packit d3f73b
		.nlh.nlmsg_seq = MAGIC_SEQ,
Packit d3f73b
		.r.idiag_family = AF_INET,
Packit d3f73b
		.r.idiag_states = f->states,
Packit d3f73b
	};
Packit d3f73b
	char    *bc = NULL;
Packit d3f73b
	int	bclen;
Packit d3f73b
	struct msghdr msg;
Packit d3f73b
	struct rtattr rta;
Packit d3f73b
	struct iovec iov[3];
Packit d3f73b
	int iovlen = 1;
Packit d3f73b
Packit d3f73b
	if (protocol == IPPROTO_UDP)
Packit d3f73b
		return -1;
Packit d3f73b
Packit d3f73b
	if (protocol == IPPROTO_TCP)
Packit d3f73b
		req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
Packit d3f73b
	else
Packit d3f73b
		req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
Packit d3f73b
	if (show_mem) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tcpinfo) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tos) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	iov[0] = (struct iovec){
Packit d3f73b
		.iov_base = &req,
Packit d3f73b
		.iov_len = sizeof(req)
Packit d3f73b
	};
Packit d3f73b
	if (f->f) {
Packit d3f73b
		bclen = ssfilter_bytecompile(f->f, &bc);
Packit d3f73b
		if (bclen) {
Packit d3f73b
			rta.rta_type = INET_DIAG_REQ_BYTECODE;
Packit d3f73b
			rta.rta_len = RTA_LENGTH(bclen);
Packit d3f73b
			iov[1] = (struct iovec){ &rta, sizeof(rta) };
Packit d3f73b
			iov[2] = (struct iovec){ bc, bclen };
Packit d3f73b
			req.nlh.nlmsg_len += RTA_LENGTH(bclen);
Packit d3f73b
			iovlen = 3;
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	msg = (struct msghdr) {
Packit d3f73b
		.msg_name = (void *)&nladdr,
Packit d3f73b
		.msg_namelen = sizeof(nladdr),
Packit d3f73b
		.msg_iov = iov,
Packit d3f73b
		.msg_iovlen = iovlen,
Packit d3f73b
	};
Packit d3f73b
Packit d3f73b
	if (sendmsg(fd, &msg, 0) < 0) {
Packit d3f73b
		close(fd);
Packit d3f73b
		return -1;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
Packit d3f73b
{
Packit d3f73b
	struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
Packit d3f73b
	DIAG_REQUEST(req, struct inet_diag_req_v2 r);
Packit d3f73b
	char    *bc = NULL;
Packit d3f73b
	int	bclen;
Packit d3f73b
	struct msghdr msg;
Packit d3f73b
	struct rtattr rta;
Packit d3f73b
	struct iovec iov[3];
Packit d3f73b
	int iovlen = 1;
Packit d3f73b
Packit d3f73b
	if (family == PF_UNSPEC)
Packit d3f73b
		return tcpdiag_send(fd, protocol, f);
Packit d3f73b
Packit d3f73b
	memset(&req.r, 0, sizeof(req.r));
Packit d3f73b
	req.r.sdiag_family = family;
Packit d3f73b
	req.r.sdiag_protocol = protocol;
Packit d3f73b
	req.r.idiag_states = f->states;
Packit d3f73b
	if (show_mem) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tcpinfo) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (show_tos) {
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1));
Packit d3f73b
		req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1));
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	iov[0] = (struct iovec){
Packit d3f73b
		.iov_base = &req,
Packit d3f73b
		.iov_len = sizeof(req)
Packit d3f73b
	};
Packit d3f73b
	if (f->f) {
Packit d3f73b
		bclen = ssfilter_bytecompile(f->f, &bc);
Packit d3f73b
		if (bclen) {
Packit d3f73b
			rta.rta_type = INET_DIAG_REQ_BYTECODE;
Packit d3f73b
			rta.rta_len = RTA_LENGTH(bclen);
Packit d3f73b
			iov[1] = (struct iovec){ &rta, sizeof(rta) };
Packit d3f73b
			iov[2] = (struct iovec){ bc, bclen };
Packit d3f73b
			req.nlh.nlmsg_len += RTA_LENGTH(bclen);
Packit d3f73b
			iovlen = 3;
Packit d3f73b
		}
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	msg = (struct msghdr) {
Packit d3f73b
		.msg_name = (void *)&nladdr,
Packit d3f73b
		.msg_namelen = sizeof(nladdr),
Packit d3f73b
		.msg_iov = iov,
Packit d3f73b
		.msg_iovlen = iovlen,
Packit d3f73b
	};
Packit d3f73b
Packit d3f73b
	if (sendmsg(fd, &msg, 0) < 0) {
Packit d3f73b
		close(fd);
Packit d3f73b
		return -1;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
struct inet_diag_arg {
Packit d3f73b
	struct filter *f;
Packit d3f73b
	int protocol;
Packit d3f73b
	struct rtnl_handle *rth;
Packit d3f73b
};
Packit d3f73b
Packit d3f73b
static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s)
Packit d3f73b
{
Packit d3f73b
	struct inet_diag_msg *d = NLMSG_DATA(h);
Packit d3f73b
	struct inet_diag_arg *diag_arg = arg;
Packit d3f73b
	struct rtnl_handle *rth = diag_arg->rth;
Packit d3f73b
Packit d3f73b
	DIAG_REQUEST(req, struct inet_diag_req_v2 r);
Packit d3f73b
Packit d3f73b
	req.nlh.nlmsg_type = SOCK_DESTROY;
Packit d3f73b
	req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
Packit d3f73b
	req.nlh.nlmsg_seq = ++rth->seq;
Packit d3f73b
	req.r.sdiag_family = d->idiag_family;
Packit d3f73b
	req.r.sdiag_protocol = diag_arg->protocol;
Packit d3f73b
	req.r.id = d->id;
Packit d3f73b
Packit d3f73b
	if (diag_arg->protocol == IPPROTO_RAW) {
Packit d3f73b
		struct inet_diag_req_raw *raw = (void *)&req.r;
Packit d3f73b
Packit d3f73b
		BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw));
Packit d3f73b
		raw->sdiag_raw_protocol = s->raw_prot;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return rtnl_talk(rth, &req.nlh, NULL);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int show_one_inet_sock(struct nlmsghdr *h, void *arg)
Packit d3f73b
{
Packit d3f73b
	int err;
Packit d3f73b
	struct inet_diag_arg *diag_arg = arg;
Packit d3f73b
	struct inet_diag_msg *r = NLMSG_DATA(h);
Packit d3f73b
	struct sockstat s = {};
Packit d3f73b
Packit d3f73b
	if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family)))
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	parse_diag_msg(h, &s);
Packit d3f73b
	s.type = diag_arg->protocol;
Packit d3f73b
Packit d3f73b
	if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0)
Packit d3f73b
		return 0;
Packit d3f73b
Packit d3f73b
	if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) {
Packit d3f73b
		if (errno == EOPNOTSUPP || errno == ENOENT) {
Packit d3f73b
			/* Socket can't be closed, or is already closed. */
Packit d3f73b
			return 0;
Packit d3f73b
		} else {
Packit d3f73b
			perror("SOCK_DESTROY answers");