| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* ss.c "sockstat", socket statistics |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/ioctl.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <sys/uio.h> |
| 15 |
#include <sys/sysmacros.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <string.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <netdb.h> |
| 20 |
#include <arpa/inet.h> |
| 21 |
#include <dirent.h> |
| 22 |
#include <fnmatch.h> |
| 23 |
#include <getopt.h> |
| 24 |
#include <stdbool.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <ctype.h> |
| 28 |
|
| 29 |
#include "ss_util.h" |
| 30 |
#include "utils.h" |
| 31 |
#include "ll_map.h" |
| 32 |
#include "libnetlink.h" |
| 33 |
#include "namespace.h" |
| 34 |
#include "version.h" |
| 35 |
#include "rt_names.h" |
| 36 |
#include "cg_map.h" |
| 37 |
#include "selinux.h" |
| 38 |
|
| 39 |
#include <linux/tcp.h> |
| 40 |
#include <linux/unix_diag.h> |
| 41 |
#include <linux/netdevice.h> /* for MAX_ADDR_LEN */ |
| 42 |
#include <linux/filter.h> |
| 43 |
#include <linux/xdp_diag.h> |
| 44 |
#include <linux/packet_diag.h> |
| 45 |
#include <linux/netlink_diag.h> |
| 46 |
#include <linux/sctp.h> |
| 47 |
#include <linux/vm_sockets_diag.h> |
| 48 |
#include <linux/net.h> |
| 49 |
#include <linux/tipc.h> |
| 50 |
#include <linux/tipc_netlink.h> |
| 51 |
#include <linux/tipc_sockets_diag.h> |
| 52 |
#include <linux/tls.h> |
| 53 |
#include <linux/mptcp.h> |
| 54 |
|
| 55 |
#ifdef HAVE_LIBBPF |
| 56 |
/* If libbpf is new enough (0.6+), support for pretty-printing BPF socket-local |
| 57 |
* storage is enabled, otherwise we emit a warning and disable it. |
| 58 |
* ENABLE_BPF_SKSTORAGE_SUPPORT is only used to gate the socket-local storage |
| 59 |
* feature, so this wouldn't prevent any feature relying on HAVE_LIBBPF to be |
| 60 |
* usable. |
| 61 |
*/ |
| 62 |
#define ENABLE_BPF_SKSTORAGE_SUPPORT |
| 63 |
|
| 64 |
#include <bpf/bpf.h> |
| 65 |
#include <bpf/btf.h> |
| 66 |
#include <bpf/libbpf.h> |
| 67 |
#include <linux/btf.h> |
| 68 |
|
| 69 |
#if ((LIBBPF_MAJOR_VERSION == 0) && (LIBBPF_MINOR_VERSION < 6)) |
| 70 |
#warning "libbpf version 0.6 or later is required, disabling BPF socket-local storage support" |
| 71 |
#undef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 72 |
#endif |
| 73 |
#endif |
| 74 |
|
| 75 |
#if HAVE_RPC |
| 76 |
#include <rpc/rpc.h> |
| 77 |
#include <rpc/xdr.h> |
| 78 |
#endif |
| 79 |
|
| 80 |
/* AF_VSOCK/PF_VSOCK is only provided since glibc 2.18 */ |
| 81 |
#ifndef PF_VSOCK |
| 82 |
#define PF_VSOCK 40 |
| 83 |
#endif |
| 84 |
#ifndef AF_VSOCK |
| 85 |
#define AF_VSOCK PF_VSOCK |
| 86 |
#endif |
| 87 |
|
| 88 |
#ifndef IPPROTO_MPTCP |
| 89 |
#define IPPROTO_MPTCP 262 |
| 90 |
#endif |
| 91 |
|
| 92 |
#define BUF_CHUNK (1024 * 1024) /* Buffer chunk allocation size */ |
| 93 |
#define BUF_CHUNKS_MAX 5 /* Maximum number of allocated buffer chunks */ |
| 94 |
#define LEN_ALIGN(x) (((x) + 1) & ~1) |
| 95 |
|
| 96 |
int preferred_family = AF_UNSPEC; |
| 97 |
static int show_options; |
| 98 |
int show_details; |
| 99 |
static int show_queues = 1; |
| 100 |
static int show_processes; |
| 101 |
static int show_threads; |
| 102 |
static int show_mem; |
| 103 |
static int show_tcpinfo; |
| 104 |
static int show_bpf; |
| 105 |
static int show_proc_ctx; |
| 106 |
static int show_sock_ctx; |
| 107 |
static int show_header = 1; |
| 108 |
static int follow_events; |
| 109 |
static int sctp_ino; |
| 110 |
static int show_tipcinfo; |
| 111 |
static int show_tos; |
| 112 |
static int show_cgroup; |
| 113 |
static int show_inet_sockopt; |
| 114 |
int oneline; |
| 115 |
|
| 116 |
enum col_id { |
| 117 |
COL_NETID, |
| 118 |
COL_STATE, |
| 119 |
COL_RECVQ, |
| 120 |
COL_SENDQ, |
| 121 |
COL_ADDR, |
| 122 |
COL_SERV, |
| 123 |
COL_RADDR, |
| 124 |
COL_RSERV, |
| 125 |
COL_PROC, |
| 126 |
COL_EXT, |
| 127 |
COL_MAX |
| 128 |
}; |
| 129 |
|
| 130 |
enum col_align { |
| 131 |
ALIGN_LEFT, |
| 132 |
ALIGN_CENTER, |
| 133 |
ALIGN_RIGHT |
| 134 |
}; |
| 135 |
|
| 136 |
struct column { |
| 137 |
const enum col_align align; |
| 138 |
const char *header; |
| 139 |
const char *ldelim; |
| 140 |
int disabled; |
| 141 |
int width; /* Calculated, including additional layout spacing */ |
| 142 |
int max_len; /* Measured maximum field length in this column */ |
| 143 |
}; |
| 144 |
|
| 145 |
static struct column columns[] = { |
| 146 |
{ ALIGN_LEFT, "Netid", "", 0, 0, 0 }, |
| 147 |
{ ALIGN_LEFT, "State", " ", 0, 0, 0 }, |
| 148 |
{ ALIGN_LEFT, "Recv-Q", " ", 0, 0, 0 }, |
| 149 |
{ ALIGN_LEFT, "Send-Q", " ", 0, 0, 0 }, |
| 150 |
{ ALIGN_RIGHT, "Local Address:", " ", 0, 0, 0 }, |
| 151 |
{ ALIGN_LEFT, "Port", "", 0, 0, 0 }, |
| 152 |
{ ALIGN_RIGHT, "Peer Address:", " ", 0, 0, 0 }, |
| 153 |
{ ALIGN_LEFT, "Port", "", 0, 0, 0 }, |
| 154 |
{ ALIGN_LEFT, "Process", "", 0, 0, 0 }, |
| 155 |
{ ALIGN_LEFT, "", "", 0, 0, 0 }, |
| 156 |
}; |
| 157 |
|
| 158 |
static struct column *current_field = columns; |
| 159 |
|
| 160 |
/* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to |
| 161 |
* the buffer as a variable size token. A token consists of a 16 bits length |
| 162 |
* field, followed by a string which is not NULL-terminated. |
| 163 |
* |
| 164 |
* A new chunk is allocated and linked when the current chunk doesn't have |
| 165 |
* enough room to store the current token as a whole. |
| 166 |
*/ |
| 167 |
struct buf_chunk { |
| 168 |
struct buf_chunk *next; /* Next chained chunk */ |
| 169 |
char *end; /* Current end of content */ |
| 170 |
char data[0]; |
| 171 |
}; |
| 172 |
|
| 173 |
struct buf_token { |
| 174 |
uint16_t len; /* Data length, excluding length descriptor */ |
| 175 |
char data[0]; |
| 176 |
}; |
| 177 |
|
| 178 |
static struct { |
| 179 |
struct buf_token *cur; /* Position of current token in chunk */ |
| 180 |
struct buf_chunk *head; /* First chunk */ |
| 181 |
struct buf_chunk *tail; /* Current chunk */ |
| 182 |
int chunks; /* Number of allocated chunks */ |
| 183 |
} buffer; |
| 184 |
|
| 185 |
static const char *TCP_PROTO = "tcp"; |
| 186 |
static const char *UDP_PROTO = "udp"; |
| 187 |
#ifdef HAVE_RPC |
| 188 |
static const char *TCP6_PROTO = "tcp6"; |
| 189 |
static const char *UDP6_PROTO = "udp6"; |
| 190 |
static const char *SCTP_PROTO = "sctp"; |
| 191 |
#endif |
| 192 |
static const char *RAW_PROTO = "raw"; |
| 193 |
static const char *dg_proto; |
| 194 |
|
| 195 |
enum { |
| 196 |
TCP_DB, |
| 197 |
MPTCP_DB, |
| 198 |
DCCP_DB, |
| 199 |
UDP_DB, |
| 200 |
RAW_DB, |
| 201 |
UNIX_DG_DB, |
| 202 |
UNIX_ST_DB, |
| 203 |
UNIX_SQ_DB, |
| 204 |
PACKET_DG_DB, |
| 205 |
PACKET_R_DB, |
| 206 |
NETLINK_DB, |
| 207 |
SCTP_DB, |
| 208 |
VSOCK_ST_DB, |
| 209 |
VSOCK_DG_DB, |
| 210 |
TIPC_DB, |
| 211 |
XDP_DB, |
| 212 |
MAX_DB |
| 213 |
}; |
| 214 |
|
| 215 |
#define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) |
| 216 |
#define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB)) |
| 217 |
#define ALL_DB ((1<<MAX_DB)-1) |
| 218 |
#define INET_L4_DBM ((1<<TCP_DB)|(1<<MPTCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB)) |
| 219 |
#define INET_DBM (INET_L4_DBM | (1<<RAW_DB)) |
| 220 |
#define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB)) |
| 221 |
|
| 222 |
enum { |
| 223 |
SS_UNKNOWN, |
| 224 |
SS_ESTABLISHED, |
| 225 |
SS_SYN_SENT, |
| 226 |
SS_SYN_RECV, |
| 227 |
SS_FIN_WAIT1, |
| 228 |
SS_FIN_WAIT2, |
| 229 |
SS_TIME_WAIT, |
| 230 |
SS_CLOSE, |
| 231 |
SS_CLOSE_WAIT, |
| 232 |
SS_LAST_ACK, |
| 233 |
SS_LISTEN, |
| 234 |
SS_CLOSING, |
| 235 |
SS_NEW_SYN_RECV, /* Kernel only value, not for use in user space */ |
| 236 |
SS_BOUND_INACTIVE, |
| 237 |
SS_MAX |
| 238 |
}; |
| 239 |
|
| 240 |
enum { |
| 241 |
SCTP_STATE_CLOSED = 0, |
| 242 |
SCTP_STATE_COOKIE_WAIT = 1, |
| 243 |
SCTP_STATE_COOKIE_ECHOED = 2, |
| 244 |
SCTP_STATE_ESTABLISHED = 3, |
| 245 |
SCTP_STATE_SHUTDOWN_PENDING = 4, |
| 246 |
SCTP_STATE_SHUTDOWN_SENT = 5, |
| 247 |
SCTP_STATE_SHUTDOWN_RECEIVED = 6, |
| 248 |
SCTP_STATE_SHUTDOWN_ACK_SENT = 7, |
| 249 |
}; |
| 250 |
|
| 251 |
#define SS_ALL ((1 << SS_MAX) - 1) |
| 252 |
#define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV))) |
| 253 |
#define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE)) |
| 254 |
|
| 255 |
#include "ssfilter.h" |
| 256 |
|
| 257 |
struct filter { |
| 258 |
int dbs; |
| 259 |
int states; |
| 260 |
uint64_t families; |
| 261 |
struct ssfilter *f; |
| 262 |
bool kill; |
| 263 |
struct rtnl_handle *rth_for_killing; |
| 264 |
}; |
| 265 |
|
| 266 |
#define FAMILY_MASK(family) ((uint64_t)1 << (family)) |
| 267 |
|
| 268 |
static const struct filter default_dbs[MAX_DB] = { |
| 269 |
[TCP_DB] = { |
| 270 |
.states = SS_CONN, |
| 271 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 272 |
}, |
| 273 |
[MPTCP_DB] = { |
| 274 |
.states = SS_CONN, |
| 275 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 276 |
}, |
| 277 |
[DCCP_DB] = { |
| 278 |
.states = SS_CONN, |
| 279 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 280 |
}, |
| 281 |
[UDP_DB] = { |
| 282 |
.states = (1 << SS_ESTABLISHED), |
| 283 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 284 |
}, |
| 285 |
[RAW_DB] = { |
| 286 |
.states = (1 << SS_ESTABLISHED), |
| 287 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 288 |
}, |
| 289 |
[UNIX_DG_DB] = { |
| 290 |
.states = (1 << SS_CLOSE), |
| 291 |
.families = FAMILY_MASK(AF_UNIX), |
| 292 |
}, |
| 293 |
[UNIX_ST_DB] = { |
| 294 |
.states = SS_CONN, |
| 295 |
.families = FAMILY_MASK(AF_UNIX), |
| 296 |
}, |
| 297 |
[UNIX_SQ_DB] = { |
| 298 |
.states = SS_CONN, |
| 299 |
.families = FAMILY_MASK(AF_UNIX), |
| 300 |
}, |
| 301 |
[PACKET_DG_DB] = { |
| 302 |
.states = (1 << SS_CLOSE), |
| 303 |
.families = FAMILY_MASK(AF_PACKET), |
| 304 |
}, |
| 305 |
[PACKET_R_DB] = { |
| 306 |
.states = (1 << SS_CLOSE), |
| 307 |
.families = FAMILY_MASK(AF_PACKET), |
| 308 |
}, |
| 309 |
[NETLINK_DB] = { |
| 310 |
.states = (1 << SS_CLOSE), |
| 311 |
.families = FAMILY_MASK(AF_NETLINK), |
| 312 |
}, |
| 313 |
[SCTP_DB] = { |
| 314 |
.states = SS_CONN, |
| 315 |
.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), |
| 316 |
}, |
| 317 |
[VSOCK_ST_DB] = { |
| 318 |
.states = SS_CONN, |
| 319 |
.families = FAMILY_MASK(AF_VSOCK), |
| 320 |
}, |
| 321 |
[VSOCK_DG_DB] = { |
| 322 |
.states = SS_CONN, |
| 323 |
.families = FAMILY_MASK(AF_VSOCK), |
| 324 |
}, |
| 325 |
[TIPC_DB] = { |
| 326 |
.states = TIPC_SS_CONN, |
| 327 |
.families = FAMILY_MASK(AF_TIPC), |
| 328 |
}, |
| 329 |
[XDP_DB] = { |
| 330 |
.states = (1 << SS_CLOSE), |
| 331 |
.families = FAMILY_MASK(AF_XDP), |
| 332 |
}, |
| 333 |
}; |
| 334 |
|
| 335 |
static const struct filter default_afs[AF_MAX] = { |
| 336 |
[AF_INET] = { |
| 337 |
.dbs = INET_DBM, |
| 338 |
.states = SS_CONN, |
| 339 |
}, |
| 340 |
[AF_INET6] = { |
| 341 |
.dbs = INET_DBM, |
| 342 |
.states = SS_CONN, |
| 343 |
}, |
| 344 |
[AF_UNIX] = { |
| 345 |
.dbs = UNIX_DBM, |
| 346 |
.states = SS_CONN, |
| 347 |
}, |
| 348 |
[AF_PACKET] = { |
| 349 |
.dbs = PACKET_DBM, |
| 350 |
.states = (1 << SS_CLOSE), |
| 351 |
}, |
| 352 |
[AF_NETLINK] = { |
| 353 |
.dbs = (1 << NETLINK_DB), |
| 354 |
.states = (1 << SS_CLOSE), |
| 355 |
}, |
| 356 |
[AF_VSOCK] = { |
| 357 |
.dbs = VSOCK_DBM, |
| 358 |
.states = SS_CONN, |
| 359 |
}, |
| 360 |
[AF_TIPC] = { |
| 361 |
.dbs = (1 << TIPC_DB), |
| 362 |
.states = TIPC_SS_CONN, |
| 363 |
}, |
| 364 |
[AF_XDP] = { |
| 365 |
.dbs = (1 << XDP_DB), |
| 366 |
.states = (1 << SS_CLOSE), |
| 367 |
}, |
| 368 |
}; |
| 369 |
|
| 370 |
static int do_default = 1; |
| 371 |
static struct filter current_filter; |
| 372 |
|
| 373 |
static void filter_db_set(struct filter *f, int db, bool enable) |
| 374 |
{ |
| 375 |
if (enable) { |
| 376 |
f->states |= default_dbs[db].states; |
| 377 |
f->dbs |= 1 << db; |
| 378 |
} else { |
| 379 |
f->dbs &= ~(1 << db); |
| 380 |
} |
| 381 |
do_default = 0; |
| 382 |
} |
| 383 |
|
| 384 |
static int filter_db_parse(struct filter *f, const char *s) |
| 385 |
{ |
| 386 |
const struct { |
| 387 |
const char *name; |
| 388 |
int dbs[MAX_DB + 1]; |
| 389 |
} db_name_tbl[] = { |
| 390 |
#define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } } |
| 391 |
ENTRY(all, UDP_DB, DCCP_DB, TCP_DB, MPTCP_DB, RAW_DB, |
| 392 |
UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB, |
| 393 |
PACKET_R_DB, PACKET_DG_DB, NETLINK_DB, |
| 394 |
SCTP_DB, VSOCK_ST_DB, VSOCK_DG_DB, XDP_DB), |
| 395 |
ENTRY(inet, UDP_DB, DCCP_DB, TCP_DB, MPTCP_DB, SCTP_DB, RAW_DB), |
| 396 |
ENTRY(udp, UDP_DB), |
| 397 |
ENTRY(dccp, DCCP_DB), |
| 398 |
ENTRY(tcp, TCP_DB), |
| 399 |
ENTRY(mptcp, MPTCP_DB), |
| 400 |
ENTRY(sctp, SCTP_DB), |
| 401 |
ENTRY(raw, RAW_DB), |
| 402 |
ENTRY(unix, UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB), |
| 403 |
ENTRY(unix_stream, UNIX_ST_DB), |
| 404 |
ENTRY(u_str, UNIX_ST_DB), /* alias for unix_stream */ |
| 405 |
ENTRY(unix_dgram, UNIX_DG_DB), |
| 406 |
ENTRY(u_dgr, UNIX_DG_DB), /* alias for unix_dgram */ |
| 407 |
ENTRY(unix_seqpacket, UNIX_SQ_DB), |
| 408 |
ENTRY(u_seq, UNIX_SQ_DB), /* alias for unix_seqpacket */ |
| 409 |
ENTRY(packet, PACKET_R_DB, PACKET_DG_DB), |
| 410 |
ENTRY(packet_raw, PACKET_R_DB), |
| 411 |
ENTRY(p_raw, PACKET_R_DB), /* alias for packet_raw */ |
| 412 |
ENTRY(packet_dgram, PACKET_DG_DB), |
| 413 |
ENTRY(p_dgr, PACKET_DG_DB), /* alias for packet_dgram */ |
| 414 |
ENTRY(netlink, NETLINK_DB), |
| 415 |
ENTRY(tipc, TIPC_DB), |
| 416 |
ENTRY(vsock, VSOCK_ST_DB, VSOCK_DG_DB), |
| 417 |
ENTRY(vsock_stream, VSOCK_ST_DB), |
| 418 |
ENTRY(v_str, VSOCK_ST_DB), /* alias for vsock_stream */ |
| 419 |
ENTRY(vsock_dgram, VSOCK_DG_DB), |
| 420 |
ENTRY(v_dgr, VSOCK_DG_DB), /* alias for vsock_dgram */ |
| 421 |
ENTRY(xdp, XDP_DB), |
| 422 |
#undef ENTRY |
| 423 |
}; |
| 424 |
bool enable = true; |
| 425 |
unsigned int i; |
| 426 |
const int *dbp; |
| 427 |
|
| 428 |
if (s[0] == '!') { |
| 429 |
enable = false; |
| 430 |
s++; |
| 431 |
} |
| 432 |
for (i = 0; i < ARRAY_SIZE(db_name_tbl); i++) { |
| 433 |
if (strcmp(s, db_name_tbl[i].name)) |
| 434 |
continue; |
| 435 |
for (dbp = db_name_tbl[i].dbs; *dbp != MAX_DB; dbp++) |
| 436 |
filter_db_set(f, *dbp, enable); |
| 437 |
return 0; |
| 438 |
} |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
static void filter_af_set(struct filter *f, int af) |
| 443 |
{ |
| 444 |
f->states |= default_afs[af].states; |
| 445 |
f->families |= FAMILY_MASK(af); |
| 446 |
do_default = 0; |
| 447 |
preferred_family = af; |
| 448 |
} |
| 449 |
|
| 450 |
static int filter_af_get(struct filter *f, int af) |
| 451 |
{ |
| 452 |
return !!(f->families & FAMILY_MASK(af)); |
| 453 |
} |
| 454 |
|
| 455 |
static void filter_states_set(struct filter *f, int states) |
| 456 |
{ |
| 457 |
if (states) |
| 458 |
f->states = states; |
| 459 |
} |
| 460 |
|
| 461 |
static void filter_merge_defaults(struct filter *f) |
| 462 |
{ |
| 463 |
int db; |
| 464 |
int af; |
| 465 |
|
| 466 |
for (db = 0; db < MAX_DB; db++) { |
| 467 |
if (!(f->dbs & (1 << db))) |
| 468 |
continue; |
| 469 |
|
| 470 |
if (!(default_dbs[db].families & f->families)) |
| 471 |
f->families |= default_dbs[db].families; |
| 472 |
} |
| 473 |
for (af = 0; af < AF_MAX; af++) { |
| 474 |
if (!(f->families & FAMILY_MASK(af))) |
| 475 |
continue; |
| 476 |
|
| 477 |
if (!(default_afs[af].dbs & f->dbs)) |
| 478 |
f->dbs |= default_afs[af].dbs; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
#define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp") |
| 483 |
#define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6") |
| 484 |
#define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp") |
| 485 |
#define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6") |
| 486 |
#define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw") |
| 487 |
#define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6") |
| 488 |
#define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix") |
| 489 |
#define net_packet_open() generic_proc_open("PROC_NET_PACKET", \ |
| 490 |
"net/packet") |
| 491 |
#define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \ |
| 492 |
"net/netlink") |
| 493 |
#define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \ |
| 494 |
"net/sockstat") |
| 495 |
#define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \ |
| 496 |
"net/sockstat6") |
| 497 |
#define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp") |
| 498 |
#define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \ |
| 499 |
"sys/net/ipv4/ip_local_port_range") |
| 500 |
|
| 501 |
struct user_ent { |
| 502 |
struct user_ent *next; |
| 503 |
unsigned int ino; |
| 504 |
int pid; |
| 505 |
int tid; |
| 506 |
int fd; |
| 507 |
char *task; |
| 508 |
char *task_ctx; |
| 509 |
char *socket_ctx; |
| 510 |
}; |
| 511 |
|
| 512 |
#define USER_ENT_HASH_SIZE 256 |
| 513 |
static struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; |
| 514 |
|
| 515 |
static int user_ent_hashfn(unsigned int ino) |
| 516 |
{ |
| 517 |
int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino; |
| 518 |
|
| 519 |
return val & (USER_ENT_HASH_SIZE - 1); |
| 520 |
} |
| 521 |
|
| 522 |
static void user_ent_add(unsigned int ino, char *task, |
| 523 |
int pid, int tid, int fd, |
| 524 |
char *task_ctx, |
| 525 |
char *sock_ctx) |
| 526 |
{ |
| 527 |
struct user_ent *p, **pp; |
| 528 |
|
| 529 |
p = malloc(sizeof(struct user_ent)); |
| 530 |
if (!p) { |
| 531 |
fprintf(stderr, "ss: failed to malloc buffer\n"); |
| 532 |
abort(); |
| 533 |
} |
| 534 |
p->next = NULL; |
| 535 |
p->ino = ino; |
| 536 |
p->pid = pid; |
| 537 |
p->tid = tid; |
| 538 |
p->fd = fd; |
| 539 |
p->task = strdup(task); |
| 540 |
p->task_ctx = strdup(task_ctx); |
| 541 |
p->socket_ctx = strdup(sock_ctx); |
| 542 |
|
| 543 |
pp = &user_ent_hash[user_ent_hashfn(ino)]; |
| 544 |
p->next = *pp; |
| 545 |
*pp = p; |
| 546 |
} |
| 547 |
|
| 548 |
#define MAX_PATH_LEN 1024 |
| 549 |
|
| 550 |
static void user_ent_hash_build_task(char *path, int pid, int tid) |
| 551 |
{ |
| 552 |
const char *no_ctx = "unavailable"; |
| 553 |
char task[20] = { }; |
| 554 |
int pos_id, pos_fd; |
| 555 |
char *task_context; |
| 556 |
struct dirent *d; |
| 557 |
DIR *dir; |
| 558 |
|
| 559 |
if (getpidcon(tid, &task_context) != 0) |
| 560 |
task_context = strdup(no_ctx); |
| 561 |
|
| 562 |
pos_id = strlen(path); /* $PROC_ROOT/$ID/ */ |
| 563 |
|
| 564 |
snprintf(path + pos_id, MAX_PATH_LEN - pos_id, "fd/"); |
| 565 |
dir = opendir(path); |
| 566 |
if (!dir) { |
| 567 |
freecon(task_context); |
| 568 |
return; |
| 569 |
} |
| 570 |
|
| 571 |
pos_fd = strlen(path); /* $PROC_ROOT/$ID/fd/ */ |
| 572 |
|
| 573 |
while ((d = readdir(dir)) != NULL) { |
| 574 |
const char *pattern = "socket:["; |
| 575 |
char *sock_context; |
| 576 |
unsigned int ino; |
| 577 |
ssize_t link_len; |
| 578 |
char lnk[64]; |
| 579 |
int fd; |
| 580 |
|
| 581 |
if (sscanf(d->d_name, "%d%*c", &fd) != 1) |
| 582 |
continue; |
| 583 |
|
| 584 |
snprintf(path + pos_fd, MAX_PATH_LEN - pos_fd, "%d", fd); |
| 585 |
|
| 586 |
link_len = readlink(path, lnk, sizeof(lnk) - 1); |
| 587 |
if (link_len == -1) |
| 588 |
continue; |
| 589 |
lnk[link_len] = '\0'; |
| 590 |
|
| 591 |
if (strncmp(lnk, pattern, strlen(pattern))) |
| 592 |
continue; |
| 593 |
|
| 594 |
if (sscanf(lnk, "socket:[%u]", &ino) != 1) |
| 595 |
continue; |
| 596 |
|
| 597 |
if (getfilecon(path, &sock_context) <= 0) |
| 598 |
sock_context = strdup(no_ctx); |
| 599 |
|
| 600 |
if (task[0] == '\0') { |
| 601 |
char stat[MAX_PATH_LEN]; |
| 602 |
char name[16]; |
| 603 |
FILE *fp; |
| 604 |
|
| 605 |
strlcpy(stat, path, pos_id + 1); |
| 606 |
snprintf(stat + pos_id, sizeof(stat) - pos_id, "stat"); |
| 607 |
|
| 608 |
fp = fopen(stat, "r"); |
| 609 |
if (fp) { |
| 610 |
if (fscanf(fp, "%*d (%[^)])", name) == 1) |
| 611 |
escape_str(task, name, sizeof(task)); |
| 612 |
fclose(fp); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
user_ent_add(ino, task, pid, tid, fd, task_context, sock_context); |
| 617 |
freecon(sock_context); |
| 618 |
} |
| 619 |
|
| 620 |
freecon(task_context); |
| 621 |
closedir(dir); |
| 622 |
} |
| 623 |
|
| 624 |
static void user_ent_destroy(void) |
| 625 |
{ |
| 626 |
struct user_ent *p, *p_next; |
| 627 |
int cnt = 0; |
| 628 |
|
| 629 |
while (cnt != USER_ENT_HASH_SIZE) { |
| 630 |
p = user_ent_hash[cnt]; |
| 631 |
while (p) { |
| 632 |
free(p->task); |
| 633 |
free(p->task_ctx); |
| 634 |
free(p->socket_ctx); |
| 635 |
p_next = p->next; |
| 636 |
free(p); |
| 637 |
p = p_next; |
| 638 |
} |
| 639 |
cnt++; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
static void user_ent_hash_build(void) |
| 644 |
{ |
| 645 |
const char *root = getenv("PROC_ROOT") ? : "/proc/"; |
| 646 |
char name[MAX_PATH_LEN]; |
| 647 |
struct dirent *d; |
| 648 |
int nameoff; |
| 649 |
DIR *dir; |
| 650 |
|
| 651 |
strlcpy(name, root, sizeof(name)); |
| 652 |
|
| 653 |
if (strlen(name) == 0 || name[strlen(name) - 1] != '/') |
| 654 |
strcat(name, "/"); |
| 655 |
|
| 656 |
nameoff = strlen(name); |
| 657 |
|
| 658 |
dir = opendir(name); |
| 659 |
if (!dir) |
| 660 |
return; |
| 661 |
|
| 662 |
while ((d = readdir(dir)) != NULL) { |
| 663 |
int pid; |
| 664 |
|
| 665 |
if (sscanf(d->d_name, "%d%*c", &pid) != 1) |
| 666 |
continue; |
| 667 |
|
| 668 |
snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", pid); |
| 669 |
user_ent_hash_build_task(name, pid, pid); |
| 670 |
|
| 671 |
if (show_threads) { |
| 672 |
struct dirent *task_d; |
| 673 |
DIR *task_dir; |
| 674 |
|
| 675 |
snprintf(name + nameoff, sizeof(name) - nameoff, "%d/task/", pid); |
| 676 |
|
| 677 |
task_dir = opendir(name); |
| 678 |
if (!task_dir) |
| 679 |
continue; |
| 680 |
|
| 681 |
while ((task_d = readdir(task_dir)) != NULL) { |
| 682 |
int tid; |
| 683 |
|
| 684 |
if (sscanf(task_d->d_name, "%d%*c", &tid) != 1) |
| 685 |
continue; |
| 686 |
if (tid == pid) |
| 687 |
continue; |
| 688 |
|
| 689 |
snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", tid); |
| 690 |
user_ent_hash_build_task(name, pid, tid); |
| 691 |
} |
| 692 |
closedir(task_dir); |
| 693 |
} |
| 694 |
} |
| 695 |
closedir(dir); |
| 696 |
} |
| 697 |
|
| 698 |
enum entry_types { |
| 699 |
USERS, |
| 700 |
PROC_CTX, |
| 701 |
PROC_SOCK_CTX |
| 702 |
}; |
| 703 |
|
| 704 |
#define ENTRY_BUF_SIZE 512 |
| 705 |
static int find_entry(unsigned int ino, char **buf, int type) |
| 706 |
{ |
| 707 |
struct user_ent *p; |
| 708 |
int cnt = 0; |
| 709 |
char *ptr; |
| 710 |
char thread_info[16] = {'\0', }; |
| 711 |
char *new_buf; |
| 712 |
int len, new_buf_len; |
| 713 |
int buf_used = 0; |
| 714 |
int buf_len = 0; |
| 715 |
|
| 716 |
if (!ino) |
| 717 |
return 0; |
| 718 |
|
| 719 |
p = user_ent_hash[user_ent_hashfn(ino)]; |
| 720 |
ptr = *buf = NULL; |
| 721 |
while (p) { |
| 722 |
if (p->ino != ino) |
| 723 |
goto next; |
| 724 |
|
| 725 |
while (1) { |
| 726 |
ptr = *buf + buf_used; |
| 727 |
|
| 728 |
if (show_threads) |
| 729 |
snprintf(thread_info, sizeof(thread_info), "tid=%d,", p->tid); |
| 730 |
|
| 731 |
switch (type) { |
| 732 |
case USERS: |
| 733 |
len = snprintf(ptr, buf_len - buf_used, |
| 734 |
"(\"%s\",pid=%d,%sfd=%d),", |
| 735 |
p->task, p->pid, thread_info, p->fd); |
| 736 |
break; |
| 737 |
case PROC_CTX: |
| 738 |
len = snprintf(ptr, buf_len - buf_used, |
| 739 |
"(\"%s\",pid=%d,%sproc_ctx=%s,fd=%d),", |
| 740 |
p->task, p->pid, thread_info, |
| 741 |
p->task_ctx, p->fd); |
| 742 |
break; |
| 743 |
case PROC_SOCK_CTX: |
| 744 |
len = snprintf(ptr, buf_len - buf_used, |
| 745 |
"(\"%s\",pid=%d,%sproc_ctx=%s,fd=%d,sock_ctx=%s),", |
| 746 |
p->task, p->pid, thread_info, |
| 747 |
p->task_ctx, p->fd, |
| 748 |
p->socket_ctx); |
| 749 |
break; |
| 750 |
default: |
| 751 |
fprintf(stderr, "ss: invalid type: %d\n", type); |
| 752 |
abort(); |
| 753 |
} |
| 754 |
|
| 755 |
if (len < 0 || len >= buf_len - buf_used) { |
| 756 |
new_buf_len = buf_len + ENTRY_BUF_SIZE; |
| 757 |
new_buf = realloc(*buf, new_buf_len); |
| 758 |
if (!new_buf) { |
| 759 |
fprintf(stderr, "ss: failed to malloc buffer\n"); |
| 760 |
abort(); |
| 761 |
} |
| 762 |
*buf = new_buf; |
| 763 |
buf_len = new_buf_len; |
| 764 |
continue; |
| 765 |
} else { |
| 766 |
buf_used += len; |
| 767 |
break; |
| 768 |
} |
| 769 |
} |
| 770 |
cnt++; |
| 771 |
next: |
| 772 |
p = p->next; |
| 773 |
} |
| 774 |
if (buf_used) { |
| 775 |
ptr = *buf + buf_used; |
| 776 |
ptr[-1] = '\0'; |
| 777 |
} |
| 778 |
return cnt; |
| 779 |
} |
| 780 |
|
| 781 |
static unsigned long long cookie_sk_get(const uint32_t *cookie) |
| 782 |
{ |
| 783 |
return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0]; |
| 784 |
} |
| 785 |
|
| 786 |
static const char *sctp_sstate_name[] = { |
| 787 |
[SCTP_STATE_CLOSED] = "CLOSED", |
| 788 |
[SCTP_STATE_COOKIE_WAIT] = "COOKIE_WAIT", |
| 789 |
[SCTP_STATE_COOKIE_ECHOED] = "COOKIE_ECHOED", |
| 790 |
[SCTP_STATE_ESTABLISHED] = "ESTAB", |
| 791 |
[SCTP_STATE_SHUTDOWN_PENDING] = "SHUTDOWN_PENDING", |
| 792 |
[SCTP_STATE_SHUTDOWN_SENT] = "SHUTDOWN_SENT", |
| 793 |
[SCTP_STATE_SHUTDOWN_RECEIVED] = "SHUTDOWN_RECEIVED", |
| 794 |
[SCTP_STATE_SHUTDOWN_ACK_SENT] = "ACK_SENT", |
| 795 |
}; |
| 796 |
|
| 797 |
static const char * const stype_nameg[] = { |
| 798 |
"UNKNOWN", |
| 799 |
[SOCK_STREAM] = "STREAM", |
| 800 |
[SOCK_DGRAM] = "DGRAM", |
| 801 |
[SOCK_RDM] = "RDM", |
| 802 |
[SOCK_SEQPACKET] = "SEQPACKET", |
| 803 |
}; |
| 804 |
|
| 805 |
struct sockstat { |
| 806 |
struct sockstat *next; |
| 807 |
unsigned int type; |
| 808 |
uint16_t prot; |
| 809 |
uint16_t raw_prot; |
| 810 |
inet_prefix local; |
| 811 |
inet_prefix remote; |
| 812 |
int lport; |
| 813 |
int rport; |
| 814 |
int state; |
| 815 |
int rq, wq; |
| 816 |
unsigned int ino; |
| 817 |
unsigned int uid; |
| 818 |
int refcnt; |
| 819 |
unsigned int iface; |
| 820 |
unsigned long long sk; |
| 821 |
char *name; |
| 822 |
char *peer_name; |
| 823 |
__u32 mark; |
| 824 |
__u64 cgroup_id; |
| 825 |
}; |
| 826 |
|
| 827 |
struct dctcpstat { |
| 828 |
unsigned int ce_state; |
| 829 |
unsigned int alpha; |
| 830 |
unsigned int ab_ecn; |
| 831 |
unsigned int ab_tot; |
| 832 |
bool enabled; |
| 833 |
}; |
| 834 |
|
| 835 |
struct tcpstat { |
| 836 |
struct sockstat ss; |
| 837 |
unsigned int timer; |
| 838 |
unsigned int timeout; |
| 839 |
int probes; |
| 840 |
char cong_alg[16]; |
| 841 |
double rto, ato, rtt, rttvar; |
| 842 |
int qack, ssthresh, backoff; |
| 843 |
double send_bps; |
| 844 |
int snd_wscale; |
| 845 |
int rcv_wscale; |
| 846 |
int mss; |
| 847 |
int rcv_mss; |
| 848 |
int advmss; |
| 849 |
unsigned int pmtu; |
| 850 |
unsigned int cwnd; |
| 851 |
unsigned int lastsnd; |
| 852 |
unsigned int lastrcv; |
| 853 |
unsigned int lastack; |
| 854 |
double pacing_rate; |
| 855 |
double pacing_rate_max; |
| 856 |
double delivery_rate; |
| 857 |
unsigned long long bytes_acked; |
| 858 |
unsigned long long bytes_received; |
| 859 |
unsigned int segs_out; |
| 860 |
unsigned int segs_in; |
| 861 |
unsigned int data_segs_out; |
| 862 |
unsigned int data_segs_in; |
| 863 |
unsigned int unacked; |
| 864 |
unsigned int retrans; |
| 865 |
unsigned int retrans_total; |
| 866 |
unsigned int lost; |
| 867 |
unsigned int sacked; |
| 868 |
unsigned int fackets; |
| 869 |
unsigned int reordering; |
| 870 |
unsigned int not_sent; |
| 871 |
unsigned int delivered; |
| 872 |
unsigned int delivered_ce; |
| 873 |
unsigned int dsack_dups; |
| 874 |
unsigned int reord_seen; |
| 875 |
double rcv_rtt; |
| 876 |
double min_rtt; |
| 877 |
unsigned int rcv_ooopack; |
| 878 |
unsigned int snd_wnd; |
| 879 |
unsigned int rcv_wnd; |
| 880 |
unsigned int rehash; |
| 881 |
int rcv_space; |
| 882 |
unsigned int rcv_ssthresh; |
| 883 |
unsigned long long busy_time; |
| 884 |
unsigned long long rwnd_limited; |
| 885 |
unsigned long long sndbuf_limited; |
| 886 |
unsigned long long bytes_sent; |
| 887 |
unsigned long long bytes_retrans; |
| 888 |
bool has_ts_opt; |
| 889 |
bool has_usec_ts_opt; |
| 890 |
bool has_sack_opt; |
| 891 |
bool has_ecn_opt; |
| 892 |
bool has_ecnseen_opt; |
| 893 |
bool has_fastopen_opt; |
| 894 |
bool has_wscale_opt; |
| 895 |
bool app_limited; |
| 896 |
struct dctcpstat *dctcp; |
| 897 |
struct tcp_bbr_info *bbr_info; |
| 898 |
}; |
| 899 |
|
| 900 |
/* SCTP assocs share the same inode number with their parent endpoint. So if we |
| 901 |
* have seen the inode number before, it must be an assoc instead of the next |
| 902 |
* endpoint. */ |
| 903 |
static bool is_sctp_assoc(struct sockstat *s, const char *sock_name) |
| 904 |
{ |
| 905 |
if (strcmp(sock_name, "sctp")) |
| 906 |
return false; |
| 907 |
if (!sctp_ino || sctp_ino != s->ino) |
| 908 |
return false; |
| 909 |
return true; |
| 910 |
} |
| 911 |
|
| 912 |
static const char *unix_netid_name(int type) |
| 913 |
{ |
| 914 |
switch (type) { |
| 915 |
case SOCK_STREAM: |
| 916 |
return "u_str"; |
| 917 |
case SOCK_SEQPACKET: |
| 918 |
return "u_seq"; |
| 919 |
case SOCK_DGRAM: |
| 920 |
default: |
| 921 |
return "u_dgr"; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
static const char *proto_name(int protocol) |
| 926 |
{ |
| 927 |
switch (protocol) { |
| 928 |
case 0: |
| 929 |
return "raw"; |
| 930 |
case IPPROTO_UDP: |
| 931 |
return "udp"; |
| 932 |
case IPPROTO_TCP: |
| 933 |
return "tcp"; |
| 934 |
case IPPROTO_MPTCP: |
| 935 |
return "mptcp"; |
| 936 |
case IPPROTO_SCTP: |
| 937 |
return "sctp"; |
| 938 |
case IPPROTO_DCCP: |
| 939 |
return "dccp"; |
| 940 |
case IPPROTO_ICMPV6: |
| 941 |
return "icmp6"; |
| 942 |
} |
| 943 |
|
| 944 |
return "???"; |
| 945 |
} |
| 946 |
|
| 947 |
static const char *vsock_netid_name(int type) |
| 948 |
{ |
| 949 |
switch (type) { |
| 950 |
case SOCK_STREAM: |
| 951 |
return "v_str"; |
| 952 |
case SOCK_DGRAM: |
| 953 |
return "v_dgr"; |
| 954 |
default: |
| 955 |
return "???"; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
static const char *tipc_netid_name(int type) |
| 960 |
{ |
| 961 |
switch (type) { |
| 962 |
case SOCK_STREAM: |
| 963 |
return "ti_st"; |
| 964 |
case SOCK_DGRAM: |
| 965 |
return "ti_dg"; |
| 966 |
case SOCK_RDM: |
| 967 |
return "ti_rd"; |
| 968 |
case SOCK_SEQPACKET: |
| 969 |
return "ti_sq"; |
| 970 |
default: |
| 971 |
return "???"; |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
/* Allocate and initialize a new buffer chunk */ |
| 976 |
static struct buf_chunk *buf_chunk_new(void) |
| 977 |
{ |
| 978 |
struct buf_chunk *new = malloc(BUF_CHUNK); |
| 979 |
|
| 980 |
if (!new) |
| 981 |
abort(); |
| 982 |
|
| 983 |
new->next = NULL; |
| 984 |
|
| 985 |
/* This is also the last block */ |
| 986 |
buffer.tail = new; |
| 987 |
|
| 988 |
/* Next token will be stored at the beginning of chunk data area, and |
| 989 |
* its initial length is zero. |
| 990 |
*/ |
| 991 |
buffer.cur = (struct buf_token *)new->data; |
| 992 |
buffer.cur->len = 0; |
| 993 |
|
| 994 |
new->end = buffer.cur->data; |
| 995 |
|
| 996 |
buffer.chunks++; |
| 997 |
|
| 998 |
return new; |
| 999 |
} |
| 1000 |
|
| 1001 |
/* Return available tail room in given chunk */ |
| 1002 |
static int buf_chunk_avail(struct buf_chunk *chunk) |
| 1003 |
{ |
| 1004 |
return BUF_CHUNK - offsetof(struct buf_chunk, data) - |
| 1005 |
(chunk->end - chunk->data); |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* Update end pointer and token length, link new chunk if we hit the end of the |
| 1009 |
* current one. Return -EAGAIN if we got a new chunk, caller has to print again. |
| 1010 |
*/ |
| 1011 |
static int buf_update(int len) |
| 1012 |
{ |
| 1013 |
struct buf_chunk *chunk = buffer.tail; |
| 1014 |
struct buf_token *t = buffer.cur; |
| 1015 |
|
| 1016 |
/* Claim success if new content fits in the current chunk, and anyway |
| 1017 |
* if this is the first token in the chunk: in the latter case, |
| 1018 |
* allocating a new chunk won't help, so we'll just cut the output. |
| 1019 |
*/ |
| 1020 |
if ((len < buf_chunk_avail(chunk) && len != -1 /* glibc < 2.0.6 */) || |
| 1021 |
t == (struct buf_token *)chunk->data) { |
| 1022 |
len = min(len, buf_chunk_avail(chunk)); |
| 1023 |
|
| 1024 |
/* Total field length can't exceed 2^16 bytes, cut as needed */ |
| 1025 |
len = min(len, USHRT_MAX - t->len); |
| 1026 |
|
| 1027 |
chunk->end += len; |
| 1028 |
t->len += len; |
| 1029 |
return 0; |
| 1030 |
} |
| 1031 |
|
| 1032 |
/* Content truncated, time to allocate more */ |
| 1033 |
chunk->next = buf_chunk_new(); |
| 1034 |
|
| 1035 |
/* Copy current token over to new chunk, including length descriptor */ |
| 1036 |
memcpy(chunk->next->data, t, sizeof(t->len) + t->len); |
| 1037 |
chunk->next->end += t->len; |
| 1038 |
|
| 1039 |
/* Discard partially written field in old chunk */ |
| 1040 |
chunk->end -= t->len + sizeof(t->len); |
| 1041 |
|
| 1042 |
return -EAGAIN; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* Append content to buffer as part of the current field */ |
| 1046 |
__attribute__((format(printf, 1, 0))) |
| 1047 |
static void vout(const char *fmt, va_list args) |
| 1048 |
{ |
| 1049 |
struct column *f = current_field; |
| 1050 |
va_list _args; |
| 1051 |
char *pos; |
| 1052 |
int len; |
| 1053 |
|
| 1054 |
if (f->disabled) |
| 1055 |
return; |
| 1056 |
|
| 1057 |
if (!buffer.head) |
| 1058 |
buffer.head = buf_chunk_new(); |
| 1059 |
|
| 1060 |
again: /* Append to buffer: if we have a new chunk, print again */ |
| 1061 |
va_copy(_args, args); |
| 1062 |
|
| 1063 |
pos = buffer.cur->data + buffer.cur->len; |
| 1064 |
|
| 1065 |
/* Limit to tail room. If we hit the limit, buf_update() will tell us */ |
| 1066 |
len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, _args); |
| 1067 |
|
| 1068 |
if (buf_update(len)) |
| 1069 |
goto again; |
| 1070 |
} |
| 1071 |
|
| 1072 |
__attribute__((format(printf, 1, 2))) |
| 1073 |
static void out(const char *fmt, ...) |
| 1074 |
{ |
| 1075 |
va_list args; |
| 1076 |
|
| 1077 |
va_start(args, fmt); |
| 1078 |
vout(fmt, args); |
| 1079 |
va_end(args); |
| 1080 |
} |
| 1081 |
|
| 1082 |
static int print_left_spacing(struct column *f, int stored, int printed) |
| 1083 |
{ |
| 1084 |
int s; |
| 1085 |
|
| 1086 |
if (!f->width || f->align == ALIGN_LEFT) |
| 1087 |
return 0; |
| 1088 |
|
| 1089 |
s = f->width - stored - printed; |
| 1090 |
if (f->align == ALIGN_CENTER) |
| 1091 |
/* If count of total spacing is odd, shift right by one */ |
| 1092 |
s = (s + 1) / 2; |
| 1093 |
|
| 1094 |
if (s > 0) |
| 1095 |
return printf("%*c", s, ' '); |
| 1096 |
|
| 1097 |
return 0; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static void print_right_spacing(struct column *f, int printed) |
| 1101 |
{ |
| 1102 |
int s; |
| 1103 |
|
| 1104 |
if (!f->width || f->align == ALIGN_RIGHT) |
| 1105 |
return; |
| 1106 |
|
| 1107 |
s = f->width - printed; |
| 1108 |
if (f->align == ALIGN_CENTER) |
| 1109 |
s /= 2; |
| 1110 |
|
| 1111 |
if (s > 0) |
| 1112 |
printf("%*c", s, ' '); |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* Done with field: update buffer pointer, start new token after current one */ |
| 1116 |
static void field_flush(struct column *f) |
| 1117 |
{ |
| 1118 |
struct buf_chunk *chunk; |
| 1119 |
unsigned int pad; |
| 1120 |
|
| 1121 |
if (f->disabled) |
| 1122 |
return; |
| 1123 |
|
| 1124 |
chunk = buffer.tail; |
| 1125 |
pad = buffer.cur->len % 2; |
| 1126 |
|
| 1127 |
if (buffer.cur->len > f->max_len) |
| 1128 |
f->max_len = buffer.cur->len; |
| 1129 |
|
| 1130 |
/* We need a new chunk if we can't store the next length descriptor. |
| 1131 |
* Mind the gap between end of previous token and next aligned position |
| 1132 |
* for length descriptor. |
| 1133 |
*/ |
| 1134 |
if (buf_chunk_avail(chunk) - pad < sizeof(buffer.cur->len)) { |
| 1135 |
chunk->end += pad; |
| 1136 |
chunk->next = buf_chunk_new(); |
| 1137 |
return; |
| 1138 |
} |
| 1139 |
|
| 1140 |
buffer.cur = (struct buf_token *)(buffer.cur->data + |
| 1141 |
LEN_ALIGN(buffer.cur->len)); |
| 1142 |
buffer.cur->len = 0; |
| 1143 |
buffer.tail->end = buffer.cur->data; |
| 1144 |
} |
| 1145 |
|
| 1146 |
static int field_is_last(struct column *f) |
| 1147 |
{ |
| 1148 |
return f - columns == COL_MAX - 1; |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* Get the next available token in the buffer starting from the current token */ |
| 1152 |
static struct buf_token *buf_token_next(struct buf_token *cur) |
| 1153 |
{ |
| 1154 |
struct buf_chunk *chunk = buffer.tail; |
| 1155 |
|
| 1156 |
/* If we reached the end of chunk contents, get token from next chunk */ |
| 1157 |
if (cur->data + LEN_ALIGN(cur->len) == chunk->end) { |
| 1158 |
buffer.tail = chunk = chunk->next; |
| 1159 |
return chunk ? (struct buf_token *)chunk->data : NULL; |
| 1160 |
} |
| 1161 |
|
| 1162 |
return (struct buf_token *)(cur->data + LEN_ALIGN(cur->len)); |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* Free up all allocated buffer chunks */ |
| 1166 |
static void buf_free_all(void) |
| 1167 |
{ |
| 1168 |
struct buf_chunk *tmp; |
| 1169 |
|
| 1170 |
for (buffer.tail = buffer.head; buffer.tail; ) { |
| 1171 |
tmp = buffer.tail; |
| 1172 |
buffer.tail = buffer.tail->next; |
| 1173 |
free(tmp); |
| 1174 |
} |
| 1175 |
buffer.head = NULL; |
| 1176 |
buffer.chunks = 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
/* Get current screen width. Returns -1 if TIOCGWINSZ fails and there's |
| 1180 |
* no COLUMNS variable in the environment. |
| 1181 |
*/ |
| 1182 |
static int render_screen_width(void) |
| 1183 |
{ |
| 1184 |
int width = -1; |
| 1185 |
|
| 1186 |
if (isatty(STDOUT_FILENO)) { |
| 1187 |
struct winsize w; |
| 1188 |
|
| 1189 |
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { |
| 1190 |
if (w.ws_col > 0) |
| 1191 |
width = w.ws_col; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (width == -1) { |
| 1196 |
const char *p = getenv("COLUMNS"); |
| 1197 |
int c; |
| 1198 |
|
| 1199 |
if (p) { |
| 1200 |
c = atoi(p); |
| 1201 |
if (c > 0) |
| 1202 |
width = c; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
return width; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* Calculate column width from contents length. If columns don't fit on one |
| 1210 |
* line, break them into the least possible amount of lines and keep them |
| 1211 |
* aligned across lines. Available screen space is equally spread between fields |
| 1212 |
* as additional spacing. |
| 1213 |
*/ |
| 1214 |
static bool render_calc_width(void) |
| 1215 |
{ |
| 1216 |
int screen_width, first, len = 0, linecols = 0; |
| 1217 |
struct column *c, *eol = columns - 1; |
| 1218 |
bool compact_output = false; |
| 1219 |
|
| 1220 |
screen_width = render_screen_width(); |
| 1221 |
if (screen_width == -1) { |
| 1222 |
screen_width = INT_MAX; |
| 1223 |
compact_output = true; |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* First pass: set width for each column to measured content length */ |
| 1227 |
for (first = 1, c = columns; c - columns < COL_MAX; c++) { |
| 1228 |
if (c->disabled) |
| 1229 |
continue; |
| 1230 |
|
| 1231 |
if (!first && c->max_len) |
| 1232 |
c->width = c->max_len + strlen(c->ldelim); |
| 1233 |
else |
| 1234 |
c->width = c->max_len; |
| 1235 |
|
| 1236 |
/* But don't exceed screen size. If we exceed the screen size |
| 1237 |
* for even a single field, it will just start on a line of its |
| 1238 |
* own and then naturally wrap. |
| 1239 |
*/ |
| 1240 |
c->width = min(c->width, screen_width); |
| 1241 |
|
| 1242 |
if (c->width) |
| 1243 |
first = 0; |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (compact_output) { |
| 1247 |
/* Compact output, skip extending columns. */ |
| 1248 |
return compact_output; |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* Second pass: find out newlines and distribute available spacing */ |
| 1252 |
for (c = columns; c - columns < COL_MAX; c++) { |
| 1253 |
int pad, spacing, rem, last; |
| 1254 |
struct column *tmp; |
| 1255 |
|
| 1256 |
if (!c->width) |
| 1257 |
continue; |
| 1258 |
|
| 1259 |
linecols++; |
| 1260 |
len += c->width; |
| 1261 |
|
| 1262 |
for (last = 1, tmp = c + 1; tmp - columns < COL_MAX; tmp++) { |
| 1263 |
if (tmp->width) { |
| 1264 |
last = 0; |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
if (!last && len < screen_width) { |
| 1270 |
/* Columns fit on screen so far, nothing to do yet */ |
| 1271 |
continue; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (len == screen_width) { |
| 1275 |
/* Exact fit, just start with new line */ |
| 1276 |
goto newline; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (len > screen_width) { |
| 1280 |
/* Screen width exceeded: go back one column */ |
| 1281 |
len -= c->width; |
| 1282 |
c--; |
| 1283 |
linecols--; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* Distribute remaining space to columns on this line */ |
| 1287 |
pad = screen_width - len; |
| 1288 |
spacing = pad / linecols; |
| 1289 |
rem = pad % linecols; |
| 1290 |
for (tmp = c; tmp > eol; tmp--) { |
| 1291 |
if (!tmp->width) |
| 1292 |
continue; |
| 1293 |
|
| 1294 |
tmp->width += spacing; |
| 1295 |
if (rem) { |
| 1296 |
tmp->width++; |
| 1297 |
rem--; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
newline: |
| 1302 |
/* Line break: reset line counters, mark end-of-line */ |
| 1303 |
eol = c; |
| 1304 |
len = 0; |
| 1305 |
linecols = 0; |
| 1306 |
} |
| 1307 |
return compact_output; |
| 1308 |
} |
| 1309 |
|
| 1310 |
/* Render buffered output with spacing and delimiters, then free up buffers */ |
| 1311 |
static void render(void) |
| 1312 |
{ |
| 1313 |
struct buf_token *token; |
| 1314 |
int printed, line_started = 0; |
| 1315 |
struct column *f, *last_visible_column = 0; |
| 1316 |
bool compact_output = false; |
| 1317 |
|
| 1318 |
if (!buffer.head) |
| 1319 |
return; |
| 1320 |
|
| 1321 |
token = (struct buf_token *)buffer.head->data; |
| 1322 |
|
| 1323 |
/* Ensure end alignment of last token, it wasn't necessarily flushed */ |
| 1324 |
buffer.tail->end += buffer.cur->len % 2; |
| 1325 |
|
| 1326 |
compact_output = render_calc_width(); |
| 1327 |
|
| 1328 |
/* Rewind and replay */ |
| 1329 |
buffer.tail = buffer.head; |
| 1330 |
|
| 1331 |
f = columns; |
| 1332 |
while (!field_is_last(f)) { |
| 1333 |
if (!f->disabled) |
| 1334 |
last_visible_column = f; |
| 1335 |
f++; |
| 1336 |
} |
| 1337 |
|
| 1338 |
f = columns; |
| 1339 |
while (!f->width) |
| 1340 |
f++; |
| 1341 |
|
| 1342 |
while (token) { |
| 1343 |
/* Print left delimiter only if we already started a line */ |
| 1344 |
if (line_started++) |
| 1345 |
printed = printf("%s", f->ldelim); |
| 1346 |
else |
| 1347 |
printed = 0; |
| 1348 |
|
| 1349 |
/* Print field content from token data with spacing */ |
| 1350 |
printed += print_left_spacing(f, token->len, printed); |
| 1351 |
printed += fwrite(token->data, 1, token->len, stdout); |
| 1352 |
if (!compact_output || f != last_visible_column) |
| 1353 |
print_right_spacing(f, printed); |
| 1354 |
|
| 1355 |
/* Go to next non-empty field, deal with end-of-line */ |
| 1356 |
do { |
| 1357 |
if (field_is_last(f)) { |
| 1358 |
printf("\n"); |
| 1359 |
f = columns; |
| 1360 |
line_started = 0; |
| 1361 |
} else { |
| 1362 |
f++; |
| 1363 |
} |
| 1364 |
} while (f->disabled); |
| 1365 |
|
| 1366 |
token = buf_token_next(token); |
| 1367 |
} |
| 1368 |
/* Deal with final end-of-line when the last non-empty field printed |
| 1369 |
* is not the last field. |
| 1370 |
*/ |
| 1371 |
if (line_started) |
| 1372 |
printf("\n"); |
| 1373 |
|
| 1374 |
buf_free_all(); |
| 1375 |
current_field = columns; |
| 1376 |
} |
| 1377 |
|
| 1378 |
/* Move to next field, and render buffer if we reached the maximum number of |
| 1379 |
* chunks, at the last field in a line. |
| 1380 |
*/ |
| 1381 |
static void field_next(void) |
| 1382 |
{ |
| 1383 |
if (field_is_last(current_field) && buffer.chunks >= BUF_CHUNKS_MAX) { |
| 1384 |
render(); |
| 1385 |
return; |
| 1386 |
} |
| 1387 |
|
| 1388 |
field_flush(current_field); |
| 1389 |
if (field_is_last(current_field)) |
| 1390 |
current_field = columns; |
| 1391 |
else |
| 1392 |
current_field++; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/* Walk through fields and flush them until we reach the desired one */ |
| 1396 |
static void field_set(enum col_id id) |
| 1397 |
{ |
| 1398 |
while (id != current_field - columns) |
| 1399 |
field_next(); |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* Print header for all non-empty columns */ |
| 1403 |
static void print_header(void) |
| 1404 |
{ |
| 1405 |
while (!field_is_last(current_field)) { |
| 1406 |
if (!current_field->disabled) |
| 1407 |
out("%s", current_field->header); |
| 1408 |
field_next(); |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
static void sock_state_print(struct sockstat *s) |
| 1413 |
{ |
| 1414 |
const char *sock_name; |
| 1415 |
static const char * const sstate_name[] = { |
| 1416 |
"UNKNOWN", |
| 1417 |
[SS_ESTABLISHED] = "ESTAB", |
| 1418 |
[SS_SYN_SENT] = "SYN-SENT", |
| 1419 |
[SS_SYN_RECV] = "SYN-RECV", |
| 1420 |
[SS_FIN_WAIT1] = "FIN-WAIT-1", |
| 1421 |
[SS_FIN_WAIT2] = "FIN-WAIT-2", |
| 1422 |
[SS_TIME_WAIT] = "TIME-WAIT", |
| 1423 |
[SS_CLOSE] = "UNCONN", |
| 1424 |
[SS_CLOSE_WAIT] = "CLOSE-WAIT", |
| 1425 |
[SS_LAST_ACK] = "LAST-ACK", |
| 1426 |
[SS_LISTEN] = "LISTEN", |
| 1427 |
[SS_CLOSING] = "CLOSING", |
| 1428 |
[SS_NEW_SYN_RECV] = "UNDEF", /* Never returned by kernel */ |
| 1429 |
[SS_BOUND_INACTIVE] = "UNDEF", /* Never returned by kernel */ |
| 1430 |
}; |
| 1431 |
|
| 1432 |
switch (s->local.family) { |
| 1433 |
case AF_UNIX: |
| 1434 |
sock_name = unix_netid_name(s->type); |
| 1435 |
break; |
| 1436 |
case AF_INET: |
| 1437 |
case AF_INET6: |
| 1438 |
sock_name = proto_name(s->type); |
| 1439 |
break; |
| 1440 |
case AF_PACKET: |
| 1441 |
sock_name = s->type == SOCK_RAW ? "p_raw" : "p_dgr"; |
| 1442 |
break; |
| 1443 |
case AF_NETLINK: |
| 1444 |
sock_name = "nl"; |
| 1445 |
break; |
| 1446 |
case AF_TIPC: |
| 1447 |
sock_name = tipc_netid_name(s->type); |
| 1448 |
break; |
| 1449 |
case AF_VSOCK: |
| 1450 |
sock_name = vsock_netid_name(s->type); |
| 1451 |
break; |
| 1452 |
case AF_XDP: |
| 1453 |
sock_name = "xdp"; |
| 1454 |
break; |
| 1455 |
default: |
| 1456 |
sock_name = "unknown"; |
| 1457 |
} |
| 1458 |
|
| 1459 |
if (is_sctp_assoc(s, sock_name)) { |
| 1460 |
field_set(COL_STATE); /* Empty Netid field */ |
| 1461 |
out("`- %s", sctp_sstate_name[s->state]); |
| 1462 |
} else { |
| 1463 |
field_set(COL_NETID); |
| 1464 |
out("%s", sock_name); |
| 1465 |
field_set(COL_STATE); |
| 1466 |
out("%s", sstate_name[s->state]); |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (show_queues) { |
| 1470 |
field_set(COL_RECVQ); |
| 1471 |
out("%-6d", s->rq); |
| 1472 |
field_set(COL_SENDQ); |
| 1473 |
out("%-6d", s->wq); |
| 1474 |
} |
| 1475 |
|
| 1476 |
field_set(COL_ADDR); |
| 1477 |
} |
| 1478 |
|
| 1479 |
static void sock_details_print(struct sockstat *s) |
| 1480 |
{ |
| 1481 |
if (s->uid) |
| 1482 |
out(" uid:%u", s->uid); |
| 1483 |
|
| 1484 |
out(" ino:%u", s->ino); |
| 1485 |
out(" sk:%llx", s->sk); |
| 1486 |
|
| 1487 |
if (s->mark) |
| 1488 |
out(" fwmark:0x%x", s->mark); |
| 1489 |
|
| 1490 |
if (s->cgroup_id) |
| 1491 |
out(" cgroup:%s", cg_id_to_path(s->cgroup_id)); |
| 1492 |
} |
| 1493 |
|
| 1494 |
static void sock_addr_print(const char *addr, char *delim, const char *port, |
| 1495 |
const char *ifname) |
| 1496 |
{ |
| 1497 |
if (ifname) |
| 1498 |
out("%s" "%%" "%s%s", addr, ifname, delim); |
| 1499 |
else |
| 1500 |
out("%s%s", addr, delim); |
| 1501 |
|
| 1502 |
field_next(); |
| 1503 |
out("%s", port); |
| 1504 |
field_next(); |
| 1505 |
} |
| 1506 |
|
| 1507 |
static const char *print_ms_timer(unsigned int timeout) |
| 1508 |
{ |
| 1509 |
static char buf[64]; |
| 1510 |
int secs, msecs, minutes; |
| 1511 |
|
| 1512 |
secs = timeout/1000; |
| 1513 |
minutes = secs/60; |
| 1514 |
secs = secs%60; |
| 1515 |
msecs = timeout%1000; |
| 1516 |
buf[0] = 0; |
| 1517 |
if (minutes) { |
| 1518 |
msecs = 0; |
| 1519 |
snprintf(buf, sizeof(buf)-16, "%dmin", minutes); |
| 1520 |
if (minutes > 9) |
| 1521 |
secs = 0; |
| 1522 |
} |
| 1523 |
if (secs) { |
| 1524 |
if (secs > 9) |
| 1525 |
msecs = 0; |
| 1526 |
sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); |
| 1527 |
} |
| 1528 |
if (msecs) |
| 1529 |
sprintf(buf+strlen(buf), "%03d%s", msecs, secs ? "sec" : "ms"); |
| 1530 |
return buf; |
| 1531 |
} |
| 1532 |
|
| 1533 |
struct scache { |
| 1534 |
struct scache *next; |
| 1535 |
int port; |
| 1536 |
char *name; |
| 1537 |
const char *proto; |
| 1538 |
}; |
| 1539 |
|
| 1540 |
static struct scache *rlist; |
| 1541 |
|
| 1542 |
#ifdef HAVE_RPC |
| 1543 |
static CLIENT *rpc_client_create(rpcprog_t prog, rpcvers_t vers) |
| 1544 |
{ |
| 1545 |
struct netbuf nbuf; |
| 1546 |
struct sockaddr_un saddr; |
| 1547 |
int sock; |
| 1548 |
|
| 1549 |
memset(&saddr, 0, sizeof(saddr)); |
| 1550 |
sock = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 1551 |
if (sock < 0) |
| 1552 |
return NULL; |
| 1553 |
|
| 1554 |
saddr.sun_family = AF_LOCAL; |
| 1555 |
strcpy(saddr.sun_path, _PATH_RPCBINDSOCK); |
| 1556 |
nbuf.len = SUN_LEN(&saddr); |
| 1557 |
nbuf.maxlen = sizeof(struct sockaddr_un); |
| 1558 |
nbuf.buf = &saddr; |
| 1559 |
|
| 1560 |
return clnt_vc_create(sock, &nbuf, prog, vers, 0, 0); |
| 1561 |
} |
| 1562 |
|
| 1563 |
static void init_service_resolver(void) |
| 1564 |
{ |
| 1565 |
struct rpcblist *rhead = NULL; |
| 1566 |
struct timeval timeout; |
| 1567 |
struct rpcent *rpc; |
| 1568 |
enum clnt_stat res; |
| 1569 |
CLIENT *client; |
| 1570 |
|
| 1571 |
timeout.tv_sec = 5; |
| 1572 |
timeout.tv_usec = 0; |
| 1573 |
|
| 1574 |
client = rpc_client_create(PMAPPROG, RPCBVERS4); |
| 1575 |
if (!client) |
| 1576 |
return; |
| 1577 |
|
| 1578 |
res = clnt_call(client, RPCBPROC_DUMP, (xdrproc_t)xdr_void, NULL, |
| 1579 |
(xdrproc_t)xdr_rpcblist_ptr, (char *)&rhead, |
| 1580 |
timeout); |
| 1581 |
if (res != RPC_SUCCESS) |
| 1582 |
return; |
| 1583 |
|
| 1584 |
for (; rhead; rhead = rhead->rpcb_next) { |
| 1585 |
char prog[128] = "rpc."; |
| 1586 |
struct scache *c; |
| 1587 |
int hport, lport, ok; |
| 1588 |
|
| 1589 |
c = malloc(sizeof(*c)); |
| 1590 |
if (!c) |
| 1591 |
continue; |
| 1592 |
|
| 1593 |
ok = sscanf(rhead->rpcb_map.r_addr, "::.%d.%d", &hport, &lport); |
| 1594 |
if (!ok) |
| 1595 |
ok = sscanf(rhead->rpcb_map.r_addr, "0.0.0.0.%d.%d", |
| 1596 |
&hport, &lport); |
| 1597 |
if (!ok) |
| 1598 |
continue; |
| 1599 |
c->port = hport << 8 | lport; |
| 1600 |
|
| 1601 |
if (strcmp(rhead->rpcb_map.r_netid, TCP_PROTO) == 0 || |
| 1602 |
strcmp(rhead->rpcb_map.r_netid, TCP6_PROTO) == 0) |
| 1603 |
c->proto = TCP_PROTO; |
| 1604 |
else if (strcmp(rhead->rpcb_map.r_netid, UDP_PROTO) == 0 || |
| 1605 |
strcmp(rhead->rpcb_map.r_netid, UDP6_PROTO) == 0) |
| 1606 |
c->proto = UDP_PROTO; |
| 1607 |
else if (strcmp(rhead->rpcb_map.r_netid, SCTP_PROTO) == 0) |
| 1608 |
c->proto = SCTP_PROTO; |
| 1609 |
else |
| 1610 |
continue; |
| 1611 |
|
| 1612 |
rpc = getrpcbynumber(rhead->rpcb_map.r_prog); |
| 1613 |
if (rpc) { |
| 1614 |
strncat(prog, rpc->r_name, 128 - strlen(prog)); |
| 1615 |
c->name = strdup(prog); |
| 1616 |
} |
| 1617 |
|
| 1618 |
c->next = rlist; |
| 1619 |
rlist = c; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
#endif |
| 1623 |
|
| 1624 |
/* Even do not try default linux ephemeral port ranges: |
| 1625 |
* default /etc/services contains so much of useless crap |
| 1626 |
* wouldbe "allocated" to this area that resolution |
| 1627 |
* is really harmful. I shrug each time when seeing |
| 1628 |
* "socks" or "cfinger" in dumps. |
| 1629 |
*/ |
| 1630 |
static int is_ephemeral(int port) |
| 1631 |
{ |
| 1632 |
static int min = 0, max; |
| 1633 |
|
| 1634 |
if (!min) { |
| 1635 |
FILE *f = ephemeral_ports_open(); |
| 1636 |
|
| 1637 |
if (!f || fscanf(f, "%d %d", &min, &max) < 2) { |
| 1638 |
min = 1024; |
| 1639 |
max = 4999; |
| 1640 |
} |
| 1641 |
if (f) |
| 1642 |
fclose(f); |
| 1643 |
} |
| 1644 |
return port >= min && port <= max; |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
static const char *__resolve_service(int port) |
| 1649 |
{ |
| 1650 |
struct scache *c; |
| 1651 |
|
| 1652 |
for (c = rlist; c; c = c->next) { |
| 1653 |
if (c->port == port && c->proto == dg_proto) |
| 1654 |
return c->name; |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (!is_ephemeral(port)) { |
| 1658 |
static int notfirst; |
| 1659 |
struct servent *se; |
| 1660 |
|
| 1661 |
if (!notfirst) { |
| 1662 |
setservent(1); |
| 1663 |
notfirst = 1; |
| 1664 |
} |
| 1665 |
se = getservbyport(htons(port), dg_proto); |
| 1666 |
if (se) |
| 1667 |
return se->s_name; |
| 1668 |
} |
| 1669 |
|
| 1670 |
return NULL; |
| 1671 |
} |
| 1672 |
|
| 1673 |
#define SCACHE_BUCKETS 1024 |
| 1674 |
static struct scache *cache_htab[SCACHE_BUCKETS]; |
| 1675 |
|
| 1676 |
static const char *resolve_service(int port) |
| 1677 |
{ |
| 1678 |
static char buf[128]; |
| 1679 |
struct scache *c; |
| 1680 |
const char *res; |
| 1681 |
int hash; |
| 1682 |
|
| 1683 |
if (port == 0) { |
| 1684 |
buf[0] = '*'; |
| 1685 |
buf[1] = 0; |
| 1686 |
return buf; |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (numeric) |
| 1690 |
goto do_numeric; |
| 1691 |
|
| 1692 |
if (dg_proto == RAW_PROTO) |
| 1693 |
return inet_proto_n2a(port, buf, sizeof(buf)); |
| 1694 |
|
| 1695 |
|
| 1696 |
hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS; |
| 1697 |
|
| 1698 |
for (c = cache_htab[hash]; c; c = c->next) { |
| 1699 |
if (c->port == port && c->proto == dg_proto) |
| 1700 |
goto do_cache; |
| 1701 |
} |
| 1702 |
|
| 1703 |
c = malloc(sizeof(*c)); |
| 1704 |
if (!c) |
| 1705 |
goto do_numeric; |
| 1706 |
res = __resolve_service(port); |
| 1707 |
c->port = port; |
| 1708 |
c->name = res ? strdup(res) : NULL; |
| 1709 |
c->proto = dg_proto; |
| 1710 |
c->next = cache_htab[hash]; |
| 1711 |
cache_htab[hash] = c; |
| 1712 |
|
| 1713 |
do_cache: |
| 1714 |
if (c->name) |
| 1715 |
return c->name; |
| 1716 |
|
| 1717 |
do_numeric: |
| 1718 |
sprintf(buf, "%u", port); |
| 1719 |
return buf; |
| 1720 |
} |
| 1721 |
|
| 1722 |
static void inet_addr_print(const inet_prefix *a, int port, |
| 1723 |
unsigned int ifindex, bool v6only) |
| 1724 |
{ |
| 1725 |
char buf[1024]; |
| 1726 |
const char *ap = buf; |
| 1727 |
const char *ifname = NULL; |
| 1728 |
|
| 1729 |
if (a->family == AF_INET) { |
| 1730 |
ap = format_host(AF_INET, 4, a->data); |
| 1731 |
} else { |
| 1732 |
if (!v6only && |
| 1733 |
!memcmp(a->data, &in6addr_any, sizeof(in6addr_any))) { |
| 1734 |
buf[0] = '*'; |
| 1735 |
buf[1] = 0; |
| 1736 |
} else { |
| 1737 |
ap = format_host(a->family, 16, a->data); |
| 1738 |
|
| 1739 |
/* Numeric IPv6 addresses should be bracketed */ |
| 1740 |
if (strchr(ap, ':')) { |
| 1741 |
snprintf(buf, sizeof(buf), |
| 1742 |
"[%s]", ap); |
| 1743 |
ap = buf; |
| 1744 |
} |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
if (ifindex) |
| 1749 |
ifname = ll_index_to_name(ifindex); |
| 1750 |
|
| 1751 |
sock_addr_print(ap, ":", resolve_service(port), ifname); |
| 1752 |
} |
| 1753 |
|
| 1754 |
struct aafilter { |
| 1755 |
inet_prefix addr; |
| 1756 |
long port; |
| 1757 |
unsigned int iface; |
| 1758 |
__u32 mark; |
| 1759 |
__u32 mask; |
| 1760 |
__u64 cgroup_id; |
| 1761 |
struct aafilter *next; |
| 1762 |
}; |
| 1763 |
|
| 1764 |
static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, |
| 1765 |
int plen) |
| 1766 |
{ |
| 1767 |
if (!inet_addr_match(a, p, plen)) |
| 1768 |
return 0; |
| 1769 |
|
| 1770 |
/* Cursed "v4 mapped" addresses: v4 mapped socket matches |
| 1771 |
* pure IPv4 rule, but v4-mapped rule selects only v4-mapped |
| 1772 |
* sockets. Fair? */ |
| 1773 |
if (p->family == AF_INET && a->family == AF_INET6) { |
| 1774 |
if (a->data[0] == 0 && a->data[1] == 0 && |
| 1775 |
a->data[2] == htonl(0xffff)) { |
| 1776 |
inet_prefix tmp = *a; |
| 1777 |
|
| 1778 |
tmp.data[0] = a->data[3]; |
| 1779 |
return inet_addr_match(&tmp, p, plen); |
| 1780 |
} |
| 1781 |
} |
| 1782 |
return 1; |
| 1783 |
} |
| 1784 |
|
| 1785 |
static int unix_match(const inet_prefix *a, const inet_prefix *p) |
| 1786 |
{ |
| 1787 |
char *addr, *pattern; |
| 1788 |
|
| 1789 |
memcpy(&addr, a->data, sizeof(addr)); |
| 1790 |
memcpy(&pattern, p->data, sizeof(pattern)); |
| 1791 |
if (pattern == NULL) |
| 1792 |
return 1; |
| 1793 |
if (addr == NULL) |
| 1794 |
addr = ""; |
| 1795 |
return !fnmatch(pattern, addr, FNM_CASEFOLD); |
| 1796 |
} |
| 1797 |
|
| 1798 |
static int run_ssfilter(struct ssfilter *f, struct sockstat *s) |
| 1799 |
{ |
| 1800 |
switch (f->type) { |
| 1801 |
case SSF_S_AUTO: |
| 1802 |
{ |
| 1803 |
if (s->local.family == AF_UNIX) { |
| 1804 |
char *p; |
| 1805 |
|
| 1806 |
memcpy(&p, s->local.data, sizeof(p)); |
| 1807 |
return p == NULL || (p[0] == '@' && strlen(p) == 6 && |
| 1808 |
strspn(p+1, "0123456789abcdef") == 5); |
| 1809 |
} |
| 1810 |
if (s->local.family == AF_PACKET) |
| 1811 |
return s->lport == 0 && s->local.data[0] == 0; |
| 1812 |
if (s->local.family == AF_NETLINK) |
| 1813 |
return s->lport < 0; |
| 1814 |
if (s->local.family == AF_VSOCK) |
| 1815 |
return s->lport > 1023; |
| 1816 |
|
| 1817 |
return is_ephemeral(s->lport); |
| 1818 |
} |
| 1819 |
case SSF_DCOND: |
| 1820 |
{ |
| 1821 |
struct aafilter *a = (void *)f->pred; |
| 1822 |
|
| 1823 |
if (a->addr.family == AF_UNIX) |
| 1824 |
return unix_match(&s->remote, &a->addr); |
| 1825 |
if (a->port != -1 && a->port != s->rport) |
| 1826 |
return 0; |
| 1827 |
if (a->addr.bitlen) { |
| 1828 |
do { |
| 1829 |
if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) |
| 1830 |
return 1; |
| 1831 |
} while ((a = a->next) != NULL); |
| 1832 |
return 0; |
| 1833 |
} |
| 1834 |
return 1; |
| 1835 |
} |
| 1836 |
case SSF_SCOND: |
| 1837 |
{ |
| 1838 |
struct aafilter *a = (void *)f->pred; |
| 1839 |
|
| 1840 |
if (a->addr.family == AF_UNIX) |
| 1841 |
return unix_match(&s->local, &a->addr); |
| 1842 |
if (a->port != -1 && a->port != s->lport) |
| 1843 |
return 0; |
| 1844 |
if (a->addr.bitlen) { |
| 1845 |
do { |
| 1846 |
if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) |
| 1847 |
return 1; |
| 1848 |
} while ((a = a->next) != NULL); |
| 1849 |
return 0; |
| 1850 |
} |
| 1851 |
return 1; |
| 1852 |
} |
| 1853 |
case SSF_D_GE: |
| 1854 |
{ |
| 1855 |
struct aafilter *a = (void *)f->pred; |
| 1856 |
|
| 1857 |
return s->rport >= a->port; |
| 1858 |
} |
| 1859 |
case SSF_D_LE: |
| 1860 |
{ |
| 1861 |
struct aafilter *a = (void *)f->pred; |
| 1862 |
|
| 1863 |
return s->rport <= a->port; |
| 1864 |
} |
| 1865 |
case SSF_S_GE: |
| 1866 |
{ |
| 1867 |
struct aafilter *a = (void *)f->pred; |
| 1868 |
|
| 1869 |
return s->lport >= a->port; |
| 1870 |
} |
| 1871 |
case SSF_S_LE: |
| 1872 |
{ |
| 1873 |
struct aafilter *a = (void *)f->pred; |
| 1874 |
|
| 1875 |
return s->lport <= a->port; |
| 1876 |
} |
| 1877 |
case SSF_DEVCOND: |
| 1878 |
{ |
| 1879 |
struct aafilter *a = (void *)f->pred; |
| 1880 |
|
| 1881 |
return s->iface == a->iface; |
| 1882 |
} |
| 1883 |
case SSF_MARKMASK: |
| 1884 |
{ |
| 1885 |
struct aafilter *a = (void *)f->pred; |
| 1886 |
|
| 1887 |
return (s->mark & a->mask) == a->mark; |
| 1888 |
} |
| 1889 |
case SSF_CGROUPCOND: |
| 1890 |
{ |
| 1891 |
struct aafilter *a = (void *)f->pred; |
| 1892 |
|
| 1893 |
return s->cgroup_id == a->cgroup_id; |
| 1894 |
} |
| 1895 |
/* Yup. It is recursion. Sorry. */ |
| 1896 |
case SSF_AND: |
| 1897 |
return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); |
| 1898 |
case SSF_OR: |
| 1899 |
return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); |
| 1900 |
case SSF_NOT: |
| 1901 |
return !run_ssfilter(f->pred, s); |
| 1902 |
default: |
| 1903 |
abort(); |
| 1904 |
} |
| 1905 |
} |
| 1906 |
|
| 1907 |
/* Relocate external jumps by reloc. */ |
| 1908 |
static void ssfilter_patch(char *a, int len, int reloc) |
| 1909 |
{ |
| 1910 |
while (len > 0) { |
| 1911 |
struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a; |
| 1912 |
|
| 1913 |
if (op->no == len+4) |
| 1914 |
op->no += reloc; |
| 1915 |
len -= op->yes; |
| 1916 |
a += op->yes; |
| 1917 |
} |
| 1918 |
if (len < 0) |
| 1919 |
abort(); |
| 1920 |
} |
| 1921 |
|
| 1922 |
static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
| 1923 |
{ |
| 1924 |
switch (f->type) { |
| 1925 |
case SSF_S_AUTO: |
| 1926 |
{ |
| 1927 |
if (!(*bytecode = malloc(4))) abort(); |
| 1928 |
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; |
| 1929 |
return 4; |
| 1930 |
} |
| 1931 |
case SSF_DCOND: |
| 1932 |
case SSF_SCOND: |
| 1933 |
{ |
| 1934 |
struct aafilter *a = (void *)f->pred; |
| 1935 |
struct aafilter *b; |
| 1936 |
char *ptr; |
| 1937 |
int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); |
| 1938 |
int len = 0; |
| 1939 |
|
| 1940 |
for (b = a; b; b = b->next) { |
| 1941 |
len += 4 + sizeof(struct inet_diag_hostcond); |
| 1942 |
if (a->addr.family == AF_INET6) |
| 1943 |
len += 16; |
| 1944 |
else |
| 1945 |
len += 4; |
| 1946 |
if (b->next) |
| 1947 |
len += 4; |
| 1948 |
} |
| 1949 |
if (!(ptr = malloc(len))) abort(); |
| 1950 |
*bytecode = ptr; |
| 1951 |
for (b = a; b; b = b->next) { |
| 1952 |
struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; |
| 1953 |
int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
| 1954 |
int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
| 1955 |
struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4); |
| 1956 |
|
| 1957 |
*op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
| 1958 |
cond->family = a->addr.family; |
| 1959 |
cond->port = a->port; |
| 1960 |
cond->prefix_len = a->addr.bitlen; |
| 1961 |
memcpy(cond->addr, a->addr.data, alen); |
| 1962 |
ptr += oplen; |
| 1963 |
if (b->next) { |
| 1964 |
op = (struct inet_diag_bc_op *)ptr; |
| 1965 |
*op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; |
| 1966 |
ptr += 4; |
| 1967 |
} |
| 1968 |
} |
| 1969 |
return ptr - *bytecode; |
| 1970 |
} |
| 1971 |
case SSF_D_GE: |
| 1972 |
{ |
| 1973 |
struct aafilter *x = (void *)f->pred; |
| 1974 |
|
| 1975 |
if (!(*bytecode = malloc(8))) abort(); |
| 1976 |
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; |
| 1977 |
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
| 1978 |
return 8; |
| 1979 |
} |
| 1980 |
case SSF_D_LE: |
| 1981 |
{ |
| 1982 |
struct aafilter *x = (void *)f->pred; |
| 1983 |
|
| 1984 |
if (!(*bytecode = malloc(8))) abort(); |
| 1985 |
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; |
| 1986 |
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
| 1987 |
return 8; |
| 1988 |
} |
| 1989 |
case SSF_S_GE: |
| 1990 |
{ |
| 1991 |
struct aafilter *x = (void *)f->pred; |
| 1992 |
|
| 1993 |
if (!(*bytecode = malloc(8))) abort(); |
| 1994 |
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; |
| 1995 |
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
| 1996 |
return 8; |
| 1997 |
} |
| 1998 |
case SSF_S_LE: |
| 1999 |
{ |
| 2000 |
struct aafilter *x = (void *)f->pred; |
| 2001 |
|
| 2002 |
if (!(*bytecode = malloc(8))) abort(); |
| 2003 |
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; |
| 2004 |
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
| 2005 |
return 8; |
| 2006 |
} |
| 2007 |
|
| 2008 |
case SSF_AND: |
| 2009 |
{ |
| 2010 |
char *a1 = NULL, *a2 = NULL, *a; |
| 2011 |
int l1, l2; |
| 2012 |
|
| 2013 |
l1 = ssfilter_bytecompile(f->pred, &a1); |
| 2014 |
l2 = ssfilter_bytecompile(f->post, &a2); |
| 2015 |
if (!l1 || !l2) { |
| 2016 |
free(a1); |
| 2017 |
free(a2); |
| 2018 |
return 0; |
| 2019 |
} |
| 2020 |
if (!(a = malloc(l1+l2))) abort(); |
| 2021 |
memcpy(a, a1, l1); |
| 2022 |
memcpy(a+l1, a2, l2); |
| 2023 |
free(a1); free(a2); |
| 2024 |
ssfilter_patch(a, l1, l2); |
| 2025 |
*bytecode = a; |
| 2026 |
return l1+l2; |
| 2027 |
} |
| 2028 |
case SSF_OR: |
| 2029 |
{ |
| 2030 |
char *a1 = NULL, *a2 = NULL, *a; |
| 2031 |
int l1, l2; |
| 2032 |
|
| 2033 |
l1 = ssfilter_bytecompile(f->pred, &a1); |
| 2034 |
l2 = ssfilter_bytecompile(f->post, &a2); |
| 2035 |
if (!l1 || !l2) { |
| 2036 |
free(a1); |
| 2037 |
free(a2); |
| 2038 |
return 0; |
| 2039 |
} |
| 2040 |
if (!(a = malloc(l1+l2+4))) abort(); |
| 2041 |
memcpy(a, a1, l1); |
| 2042 |
memcpy(a+l1+4, a2, l2); |
| 2043 |
free(a1); free(a2); |
| 2044 |
*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; |
| 2045 |
*bytecode = a; |
| 2046 |
return l1+l2+4; |
| 2047 |
} |
| 2048 |
case SSF_NOT: |
| 2049 |
{ |
| 2050 |
char *a1 = NULL, *a; |
| 2051 |
int l1; |
| 2052 |
|
| 2053 |
l1 = ssfilter_bytecompile(f->pred, &a1); |
| 2054 |
if (!l1) { |
| 2055 |
free(a1); |
| 2056 |
return 0; |
| 2057 |
} |
| 2058 |
if (!(a = malloc(l1+4))) abort(); |
| 2059 |
memcpy(a, a1, l1); |
| 2060 |
free(a1); |
| 2061 |
*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; |
| 2062 |
*bytecode = a; |
| 2063 |
return l1+4; |
| 2064 |
} |
| 2065 |
case SSF_DEVCOND: |
| 2066 |
{ |
| 2067 |
/* bytecompile for SSF_DEVCOND not supported yet */ |
| 2068 |
return 0; |
| 2069 |
} |
| 2070 |
case SSF_MARKMASK: |
| 2071 |
{ |
| 2072 |
struct aafilter *a = (void *)f->pred; |
| 2073 |
struct instr { |
| 2074 |
struct inet_diag_bc_op op; |
| 2075 |
struct inet_diag_markcond cond; |
| 2076 |
}; |
| 2077 |
int inslen = sizeof(struct instr); |
| 2078 |
|
| 2079 |
if (!(*bytecode = malloc(inslen))) abort(); |
| 2080 |
((struct instr *)*bytecode)[0] = (struct instr) { |
| 2081 |
{ INET_DIAG_BC_MARK_COND, inslen, inslen + 4 }, |
| 2082 |
{ a->mark, a->mask}, |
| 2083 |
}; |
| 2084 |
|
| 2085 |
return inslen; |
| 2086 |
} |
| 2087 |
case SSF_CGROUPCOND: |
| 2088 |
{ |
| 2089 |
struct aafilter *a = (void *)f->pred; |
| 2090 |
struct instr { |
| 2091 |
struct inet_diag_bc_op op; |
| 2092 |
__u64 cgroup_id; |
| 2093 |
} __attribute__((packed)); |
| 2094 |
int inslen = sizeof(struct instr); |
| 2095 |
|
| 2096 |
if (!(*bytecode = malloc(inslen))) abort(); |
| 2097 |
((struct instr *)*bytecode)[0] = (struct instr) { |
| 2098 |
{ INET_DIAG_BC_CGROUP_COND, inslen, inslen + 4 }, |
| 2099 |
a->cgroup_id, |
| 2100 |
}; |
| 2101 |
|
| 2102 |
return inslen; |
| 2103 |
} |
| 2104 |
default: |
| 2105 |
abort(); |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
static int remember_he(struct aafilter *a, struct hostent *he) |
| 2110 |
{ |
| 2111 |
char **ptr = he->h_addr_list; |
| 2112 |
int cnt = 0; |
| 2113 |
int len; |
| 2114 |
|
| 2115 |
if (he->h_addrtype == AF_INET) |
| 2116 |
len = 4; |
| 2117 |
else if (he->h_addrtype == AF_INET6) |
| 2118 |
len = 16; |
| 2119 |
else |
| 2120 |
return 0; |
| 2121 |
|
| 2122 |
while (*ptr) { |
| 2123 |
struct aafilter *b = a; |
| 2124 |
|
| 2125 |
if (a->addr.bitlen) { |
| 2126 |
if ((b = malloc(sizeof(*b))) == NULL) |
| 2127 |
return cnt; |
| 2128 |
*b = *a; |
| 2129 |
a->next = b; |
| 2130 |
} |
| 2131 |
memcpy(b->addr.data, *ptr, len); |
| 2132 |
b->addr.bytelen = len; |
| 2133 |
b->addr.bitlen = len*8; |
| 2134 |
b->addr.family = he->h_addrtype; |
| 2135 |
ptr++; |
| 2136 |
cnt++; |
| 2137 |
} |
| 2138 |
return cnt; |
| 2139 |
} |
| 2140 |
|
| 2141 |
static int get_dns_host(struct aafilter *a, const char *addr, int fam) |
| 2142 |
{ |
| 2143 |
static int notfirst; |
| 2144 |
int cnt = 0; |
| 2145 |
struct hostent *he; |
| 2146 |
|
| 2147 |
a->addr.bitlen = 0; |
| 2148 |
if (!notfirst) { |
| 2149 |
sethostent(1); |
| 2150 |
notfirst = 1; |
| 2151 |
} |
| 2152 |
he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); |
| 2153 |
if (he) |
| 2154 |
cnt = remember_he(a, he); |
| 2155 |
if (fam == AF_UNSPEC) { |
| 2156 |
he = gethostbyname2(addr, AF_INET6); |
| 2157 |
if (he) |
| 2158 |
cnt += remember_he(a, he); |
| 2159 |
} |
| 2160 |
return !cnt; |
| 2161 |
} |
| 2162 |
|
| 2163 |
static int xll_initted; |
| 2164 |
|
| 2165 |
static void xll_init(void) |
| 2166 |
{ |
| 2167 |
struct rtnl_handle rth; |
| 2168 |
|
| 2169 |
if (rtnl_open(&rth, 0) < 0) |
| 2170 |
exit(1); |
| 2171 |
|
| 2172 |
ll_init_map(&rth); |
| 2173 |
rtnl_close(&rth); |
| 2174 |
xll_initted = 1; |
| 2175 |
} |
| 2176 |
|
| 2177 |
static const char *xll_index_to_name(int index) |
| 2178 |
{ |
| 2179 |
if (!xll_initted) |
| 2180 |
xll_init(); |
| 2181 |
return ll_index_to_name(index); |
| 2182 |
} |
| 2183 |
|
| 2184 |
static int xll_name_to_index(const char *dev) |
| 2185 |
{ |
| 2186 |
if (!xll_initted) |
| 2187 |
xll_init(); |
| 2188 |
return ll_name_to_index(dev); |
| 2189 |
} |
| 2190 |
|
| 2191 |
void *parse_devcond(char *name) |
| 2192 |
{ |
| 2193 |
struct aafilter a = { .iface = 0 }; |
| 2194 |
struct aafilter *res; |
| 2195 |
|
| 2196 |
a.iface = xll_name_to_index(name); |
| 2197 |
if (a.iface == 0) { |
| 2198 |
char *end; |
| 2199 |
unsigned long n; |
| 2200 |
|
| 2201 |
n = strtoul(name, &end, 0); |
| 2202 |
if (!end || end == name || *end || n > UINT_MAX) |
| 2203 |
return NULL; |
| 2204 |
|
| 2205 |
a.iface = n; |
| 2206 |
} |
| 2207 |
|
| 2208 |
res = malloc(sizeof(*res)); |
| 2209 |
*res = a; |
| 2210 |
|
| 2211 |
return res; |
| 2212 |
} |
| 2213 |
|
| 2214 |
static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid) |
| 2215 |
{ |
| 2216 |
*a = (inet_prefix){ |
| 2217 |
.bytelen = sizeof(cid), |
| 2218 |
.family = AF_VSOCK, |
| 2219 |
}; |
| 2220 |
memcpy(a->data, &cid, sizeof(cid)); |
| 2221 |
} |
| 2222 |
|
| 2223 |
static char* find_port(char *addr, bool is_port) |
| 2224 |
{ |
| 2225 |
char *port = NULL; |
| 2226 |
if (is_port) |
| 2227 |
port = addr; |
| 2228 |
else |
| 2229 |
port = strchr(addr, ':'); |
| 2230 |
if (port && *port == ':') |
| 2231 |
*port++ = '\0'; |
| 2232 |
return port; |
| 2233 |
} |
| 2234 |
|
| 2235 |
void *parse_hostcond(char *addr, bool is_port) |
| 2236 |
{ |
| 2237 |
char *port = NULL; |
| 2238 |
struct aafilter a = { .port = -1 }; |
| 2239 |
struct aafilter *res; |
| 2240 |
int fam = preferred_family; |
| 2241 |
struct filter *f = ¤t_filter; |
| 2242 |
|
| 2243 |
if (strncmp(addr, "unix:", 5) == 0) { |
| 2244 |
fam = AF_UNIX; |
| 2245 |
addr += 5; |
| 2246 |
} else if (strncmp(addr, "link:", 5) == 0) { |
| 2247 |
fam = AF_PACKET; |
| 2248 |
addr += 5; |
| 2249 |
} else if (strncmp(addr, "netlink:", 8) == 0) { |
| 2250 |
fam = AF_NETLINK; |
| 2251 |
addr += 8; |
| 2252 |
} else if (strncmp(addr, "vsock:", 6) == 0) { |
| 2253 |
fam = AF_VSOCK; |
| 2254 |
addr += 6; |
| 2255 |
} else if (strncmp(addr, "inet:", 5) == 0) { |
| 2256 |
fam = AF_INET; |
| 2257 |
addr += 5; |
| 2258 |
} else if (strncmp(addr, "inet6:", 6) == 0) { |
| 2259 |
fam = AF_INET6; |
| 2260 |
addr += 6; |
| 2261 |
} |
| 2262 |
|
| 2263 |
if (fam == AF_UNIX) { |
| 2264 |
char *p; |
| 2265 |
|
| 2266 |
a.addr.family = AF_UNIX; |
| 2267 |
p = strdup(addr); |
| 2268 |
a.addr.bitlen = 8*strlen(p); |
| 2269 |
memcpy(a.addr.data, &p, sizeof(p)); |
| 2270 |
goto out; |
| 2271 |
} |
| 2272 |
|
| 2273 |
if (fam == AF_PACKET) { |
| 2274 |
a.addr.family = AF_PACKET; |
| 2275 |
a.addr.bitlen = 0; |
| 2276 |
port = find_port(addr, is_port); |
| 2277 |
if (port) { |
| 2278 |
if (*port && strcmp(port, "*")) { |
| 2279 |
if (get_long(&a.port, port, 0)) { |
| 2280 |
if ((a.port = xll_name_to_index(port)) <= 0) |
| 2281 |
return NULL; |
| 2282 |
} |
| 2283 |
} |
| 2284 |
} |
| 2285 |
if (!is_port && addr[0] && strcmp(addr, "*")) { |
| 2286 |
unsigned short tmp; |
| 2287 |
|
| 2288 |
a.addr.bitlen = 32; |
| 2289 |
if (ll_proto_a2n(&tmp, addr)) |
| 2290 |
return NULL; |
| 2291 |
a.addr.data[0] = ntohs(tmp); |
| 2292 |
} |
| 2293 |
goto out; |
| 2294 |
} |
| 2295 |
|
| 2296 |
if (fam == AF_NETLINK) { |
| 2297 |
a.addr.family = AF_NETLINK; |
| 2298 |
a.addr.bitlen = 0; |
| 2299 |
port = find_port(addr, is_port); |
| 2300 |
if (port) { |
| 2301 |
if (*port && strcmp(port, "*")) { |
| 2302 |
if (get_long(&a.port, port, 0)) { |
| 2303 |
if (strcmp(port, "kernel") == 0) |
| 2304 |
a.port = 0; |
| 2305 |
else |
| 2306 |
return NULL; |
| 2307 |
} |
| 2308 |
} |
| 2309 |
} |
| 2310 |
if (!is_port && addr[0] && strcmp(addr, "*")) { |
| 2311 |
a.addr.bitlen = 32; |
| 2312 |
if (nl_proto_a2n(&a.addr.data[0], addr) == -1) |
| 2313 |
return NULL; |
| 2314 |
} |
| 2315 |
goto out; |
| 2316 |
} |
| 2317 |
|
| 2318 |
if (fam == AF_VSOCK) { |
| 2319 |
__u32 cid = ~(__u32)0; |
| 2320 |
|
| 2321 |
a.addr.family = AF_VSOCK; |
| 2322 |
|
| 2323 |
port = find_port(addr, is_port); |
| 2324 |
|
| 2325 |
if (port && strcmp(port, "*") && |
| 2326 |
get_u32((__u32 *)&a.port, port, 0)) |
| 2327 |
return NULL; |
| 2328 |
|
| 2329 |
if (!is_port && addr[0] && strcmp(addr, "*")) { |
| 2330 |
a.addr.bitlen = 32; |
| 2331 |
if (get_u32(&cid, addr, 0)) |
| 2332 |
return NULL; |
| 2333 |
} |
| 2334 |
vsock_set_inet_prefix(&a.addr, cid); |
| 2335 |
goto out; |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* URL-like literal [] */ |
| 2339 |
if (addr[0] == '[') { |
| 2340 |
addr++; |
| 2341 |
if ((port = strchr(addr, ']')) == NULL) |
| 2342 |
return NULL; |
| 2343 |
*port++ = 0; |
| 2344 |
} else if (addr[0] == '*') { |
| 2345 |
port = addr+1; |
| 2346 |
} else { |
| 2347 |
port = strrchr(strchr(addr, '/') ? : addr, ':'); |
| 2348 |
} |
| 2349 |
|
| 2350 |
if (is_port) |
| 2351 |
port = addr; |
| 2352 |
|
| 2353 |
if (port && *port) { |
| 2354 |
if (*port == ':') |
| 2355 |
*port++ = 0; |
| 2356 |
|
| 2357 |
if (*port && *port != '*') { |
| 2358 |
if (get_long(&a.port, port, 0)) { |
| 2359 |
struct servent *se1 = NULL; |
| 2360 |
struct servent *se2 = NULL; |
| 2361 |
|
| 2362 |
if (current_filter.dbs&(1<<UDP_DB)) |
| 2363 |
se1 = getservbyname(port, UDP_PROTO); |
| 2364 |
if (current_filter.dbs&(1<<TCP_DB)) |
| 2365 |
se2 = getservbyname(port, TCP_PROTO); |
| 2366 |
if (se1 && se2 && se1->s_port != se2->s_port) { |
| 2367 |
fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 2368 |
return NULL; |
| 2369 |
} |
| 2370 |
if (!se1) |
| 2371 |
se1 = se2; |
| 2372 |
if (se1) { |
| 2373 |
a.port = ntohs(se1->s_port); |
| 2374 |
} else { |
| 2375 |
struct scache *s; |
| 2376 |
|
| 2377 |
for (s = rlist; s; s = s->next) { |
| 2378 |
if ((s->proto == UDP_PROTO && |
| 2379 |
(current_filter.dbs&(1<<UDP_DB))) || |
| 2380 |
(s->proto == TCP_PROTO && |
| 2381 |
(current_filter.dbs&(1<<TCP_DB)))) { |
| 2382 |
if (s->name && strcmp(s->name, port) == 0) { |
| 2383 |
if (a.port > 0 && a.port != s->port) { |
| 2384 |
fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 2385 |
return NULL; |
| 2386 |
} |
| 2387 |
a.port = s->port; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
} |
| 2391 |
if (a.port <= 0) { |
| 2392 |
fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); |
| 2393 |
return NULL; |
| 2394 |
} |
| 2395 |
} |
| 2396 |
} |
| 2397 |
} |
| 2398 |
} |
| 2399 |
if (!is_port && *addr && *addr != '*') { |
| 2400 |
if (get_prefix_1(&a.addr, addr, fam)) { |
| 2401 |
if (get_dns_host(&a, addr, fam)) { |
| 2402 |
fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); |
| 2403 |
return NULL; |
| 2404 |
} |
| 2405 |
} |
| 2406 |
} |
| 2407 |
|
| 2408 |
out: |
| 2409 |
if (fam != AF_UNSPEC) { |
| 2410 |
int states = f->states; |
| 2411 |
f->families = 0; |
| 2412 |
filter_af_set(f, fam); |
| 2413 |
filter_states_set(f, states); |
| 2414 |
} |
| 2415 |
|
| 2416 |
res = malloc(sizeof(*res)); |
| 2417 |
if (res) |
| 2418 |
memcpy(res, &a, sizeof(a)); |
| 2419 |
return res; |
| 2420 |
} |
| 2421 |
|
| 2422 |
void *parse_markmask(const char *markmask) |
| 2423 |
{ |
| 2424 |
struct aafilter a, *res; |
| 2425 |
|
| 2426 |
if (strchr(markmask, '/')) { |
| 2427 |
if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2) |
| 2428 |
return NULL; |
| 2429 |
} else { |
| 2430 |
a.mask = 0xffffffff; |
| 2431 |
if (sscanf(markmask, "%i", &a.mark) != 1) |
| 2432 |
return NULL; |
| 2433 |
} |
| 2434 |
|
| 2435 |
res = malloc(sizeof(*res)); |
| 2436 |
if (res) |
| 2437 |
memcpy(res, &a, sizeof(a)); |
| 2438 |
return res; |
| 2439 |
} |
| 2440 |
|
| 2441 |
void *parse_cgroupcond(const char *path) |
| 2442 |
{ |
| 2443 |
struct aafilter *res; |
| 2444 |
__u64 id; |
| 2445 |
|
| 2446 |
id = get_cgroup2_id(path); |
| 2447 |
if (!id) |
| 2448 |
return NULL; |
| 2449 |
|
| 2450 |
res = malloc(sizeof(*res)); |
| 2451 |
if (res) |
| 2452 |
res->cgroup_id = id; |
| 2453 |
|
| 2454 |
return res; |
| 2455 |
} |
| 2456 |
|
| 2457 |
static void proc_ctx_print(struct sockstat *s) |
| 2458 |
{ |
| 2459 |
char *buf; |
| 2460 |
|
| 2461 |
if (show_proc_ctx || show_sock_ctx) { |
| 2462 |
if (find_entry(s->ino, &buf, |
| 2463 |
(show_proc_ctx & show_sock_ctx) ? |
| 2464 |
PROC_SOCK_CTX : PROC_CTX) > 0) { |
| 2465 |
out(" users:(%s)", buf); |
| 2466 |
free(buf); |
| 2467 |
} |
| 2468 |
} else if (show_processes || show_threads) { |
| 2469 |
if (find_entry(s->ino, &buf, USERS) > 0) { |
| 2470 |
out(" users:(%s)", buf); |
| 2471 |
free(buf); |
| 2472 |
} |
| 2473 |
} |
| 2474 |
|
| 2475 |
field_next(); |
| 2476 |
} |
| 2477 |
|
| 2478 |
static void inet_stats_print(struct sockstat *s, bool v6only) |
| 2479 |
{ |
| 2480 |
sock_state_print(s); |
| 2481 |
|
| 2482 |
inet_addr_print(&s->local, s->lport, s->iface, v6only); |
| 2483 |
inet_addr_print(&s->remote, s->rport, 0, v6only); |
| 2484 |
|
| 2485 |
proc_ctx_print(s); |
| 2486 |
} |
| 2487 |
|
| 2488 |
static int proc_parse_inet_addr(char *loc, char *rem, int family, struct |
| 2489 |
sockstat * s) |
| 2490 |
{ |
| 2491 |
s->local.family = s->remote.family = family; |
| 2492 |
if (family == AF_INET) { |
| 2493 |
sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport); |
| 2494 |
sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport); |
| 2495 |
s->local.bytelen = s->remote.bytelen = 4; |
| 2496 |
return 0; |
| 2497 |
} else { |
| 2498 |
sscanf(loc, "%08x%08x%08x%08x:%x", |
| 2499 |
s->local.data, |
| 2500 |
s->local.data + 1, |
| 2501 |
s->local.data + 2, |
| 2502 |
s->local.data + 3, |
| 2503 |
&s->lport); |
| 2504 |
sscanf(rem, "%08x%08x%08x%08x:%x", |
| 2505 |
s->remote.data, |
| 2506 |
s->remote.data + 1, |
| 2507 |
s->remote.data + 2, |
| 2508 |
s->remote.data + 3, |
| 2509 |
&s->rport); |
| 2510 |
s->local.bytelen = s->remote.bytelen = 16; |
| 2511 |
return 0; |
| 2512 |
} |
| 2513 |
return -1; |
| 2514 |
} |
| 2515 |
|
| 2516 |
static int proc_inet_split_line(char *line, char **loc, char **rem, char **data) |
| 2517 |
{ |
| 2518 |
char *p; |
| 2519 |
|
| 2520 |
if ((p = strchr(line, ':')) == NULL) |
| 2521 |
return -1; |
| 2522 |
|
| 2523 |
*loc = p+2; |
| 2524 |
if ((p = strchr(*loc, ':')) == NULL) |
| 2525 |
return -1; |
| 2526 |
|
| 2527 |
p[5] = 0; |
| 2528 |
*rem = p+6; |
| 2529 |
if ((p = strchr(*rem, ':')) == NULL) |
| 2530 |
return -1; |
| 2531 |
|
| 2532 |
p[5] = 0; |
| 2533 |
*data = p+6; |
| 2534 |
return 0; |
| 2535 |
} |
| 2536 |
|
| 2537 |
/* |
| 2538 |
* Display bandwidth in standard units |
| 2539 |
* See: https://en.wikipedia.org/wiki/Data-rate_units |
| 2540 |
* bw is in bits per second |
| 2541 |
*/ |
| 2542 |
static char *sprint_bw(char *buf, double bw) |
| 2543 |
{ |
| 2544 |
if (numeric) |
| 2545 |
sprintf(buf, "%.0f", bw); |
| 2546 |
else if (bw >= 1e12) |
| 2547 |
sprintf(buf, "%.3gT", bw / 1e12); |
| 2548 |
else if (bw >= 1e9) |
| 2549 |
sprintf(buf, "%.3gG", bw / 1e9); |
| 2550 |
else if (bw >= 1e6) |
| 2551 |
sprintf(buf, "%.3gM", bw / 1e6); |
| 2552 |
else if (bw >= 1e3) |
| 2553 |
sprintf(buf, "%.3gk", bw / 1e3); |
| 2554 |
else |
| 2555 |
sprintf(buf, "%g", bw); |
| 2556 |
|
| 2557 |
return buf; |
| 2558 |
} |
| 2559 |
|
| 2560 |
static void sctp_stats_print(struct sctp_info *s) |
| 2561 |
{ |
| 2562 |
if (s->sctpi_tag) |
| 2563 |
out(" tag:%x", s->sctpi_tag); |
| 2564 |
if (s->sctpi_state) |
| 2565 |
out(" state:%s", sctp_sstate_name[s->sctpi_state]); |
| 2566 |
if (s->sctpi_rwnd) |
| 2567 |
out(" rwnd:%d", s->sctpi_rwnd); |
| 2568 |
if (s->sctpi_unackdata) |
| 2569 |
out(" unackdata:%d", s->sctpi_unackdata); |
| 2570 |
if (s->sctpi_penddata) |
| 2571 |
out(" penddata:%d", s->sctpi_penddata); |
| 2572 |
if (s->sctpi_instrms) |
| 2573 |
out(" instrms:%d", s->sctpi_instrms); |
| 2574 |
if (s->sctpi_outstrms) |
| 2575 |
out(" outstrms:%d", s->sctpi_outstrms); |
| 2576 |
if (s->sctpi_inqueue) |
| 2577 |
out(" inqueue:%d", s->sctpi_inqueue); |
| 2578 |
if (s->sctpi_outqueue) |
| 2579 |
out(" outqueue:%d", s->sctpi_outqueue); |
| 2580 |
if (s->sctpi_overall_error) |
| 2581 |
out(" overerr:%d", s->sctpi_overall_error); |
| 2582 |
if (s->sctpi_max_burst) |
| 2583 |
out(" maxburst:%d", s->sctpi_max_burst); |
| 2584 |
if (s->sctpi_maxseg) |
| 2585 |
out(" maxseg:%d", s->sctpi_maxseg); |
| 2586 |
if (s->sctpi_peer_rwnd) |
| 2587 |
out(" prwnd:%d", s->sctpi_peer_rwnd); |
| 2588 |
if (s->sctpi_peer_tag) |
| 2589 |
out(" ptag:%x", s->sctpi_peer_tag); |
| 2590 |
if (s->sctpi_peer_capable) |
| 2591 |
out(" pcapable:%d", s->sctpi_peer_capable); |
| 2592 |
if (s->sctpi_peer_sack) |
| 2593 |
out(" psack:%d", s->sctpi_peer_sack); |
| 2594 |
if (s->sctpi_s_autoclose) |
| 2595 |
out(" autoclose:%d", s->sctpi_s_autoclose); |
| 2596 |
if (s->sctpi_s_adaptation_ind) |
| 2597 |
out(" adapind:%d", s->sctpi_s_adaptation_ind); |
| 2598 |
if (s->sctpi_s_pd_point) |
| 2599 |
out(" pdpoint:%d", s->sctpi_s_pd_point); |
| 2600 |
if (s->sctpi_s_nodelay) |
| 2601 |
out(" nodelay:%d", s->sctpi_s_nodelay); |
| 2602 |
if (s->sctpi_s_disable_fragments) |
| 2603 |
out(" nofrag:%d", s->sctpi_s_disable_fragments); |
| 2604 |
if (s->sctpi_s_v4mapped) |
| 2605 |
out(" v4mapped:%d", s->sctpi_s_v4mapped); |
| 2606 |
if (s->sctpi_s_frag_interleave) |
| 2607 |
out(" fraginl:%d", s->sctpi_s_frag_interleave); |
| 2608 |
} |
| 2609 |
|
| 2610 |
static void tcp_stats_print(struct tcpstat *s) |
| 2611 |
{ |
| 2612 |
char b1[64]; |
| 2613 |
|
| 2614 |
if (s->has_ts_opt) |
| 2615 |
out(" ts"); |
| 2616 |
if (s->has_usec_ts_opt) |
| 2617 |
out(" usec_ts"); |
| 2618 |
if (s->has_sack_opt) |
| 2619 |
out(" sack"); |
| 2620 |
if (s->has_ecn_opt) |
| 2621 |
out(" ecn"); |
| 2622 |
if (s->has_ecnseen_opt) |
| 2623 |
out(" ecnseen"); |
| 2624 |
if (s->has_fastopen_opt) |
| 2625 |
out(" fastopen"); |
| 2626 |
if (s->cong_alg[0]) |
| 2627 |
out(" %s", s->cong_alg); |
| 2628 |
if (s->has_wscale_opt) |
| 2629 |
out(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale); |
| 2630 |
if (s->rto) |
| 2631 |
out(" rto:%g", s->rto); |
| 2632 |
if (s->backoff) |
| 2633 |
out(" backoff:%u", s->backoff); |
| 2634 |
if (s->rtt) |
| 2635 |
out(" rtt:%g/%g", s->rtt, s->rttvar); |
| 2636 |
if (s->ato) |
| 2637 |
out(" ato:%g", s->ato); |
| 2638 |
|
| 2639 |
if (s->qack) |
| 2640 |
out(" qack:%d", s->qack); |
| 2641 |
if (s->qack & 1) |
| 2642 |
out(" bidir"); |
| 2643 |
|
| 2644 |
if (s->mss) |
| 2645 |
out(" mss:%d", s->mss); |
| 2646 |
if (s->pmtu) |
| 2647 |
out(" pmtu:%u", s->pmtu); |
| 2648 |
if (s->rcv_mss) |
| 2649 |
out(" rcvmss:%d", s->rcv_mss); |
| 2650 |
if (s->advmss) |
| 2651 |
out(" advmss:%d", s->advmss); |
| 2652 |
if (s->cwnd) |
| 2653 |
out(" cwnd:%u", s->cwnd); |
| 2654 |
if (s->ssthresh) |
| 2655 |
out(" ssthresh:%d", s->ssthresh); |
| 2656 |
|
| 2657 |
if (s->bytes_sent) |
| 2658 |
out(" bytes_sent:%llu", s->bytes_sent); |
| 2659 |
if (s->bytes_retrans) |
| 2660 |
out(" bytes_retrans:%llu", s->bytes_retrans); |
| 2661 |
if (s->bytes_acked) |
| 2662 |
out(" bytes_acked:%llu", s->bytes_acked); |
| 2663 |
if (s->bytes_received) |
| 2664 |
out(" bytes_received:%llu", s->bytes_received); |
| 2665 |
if (s->segs_out) |
| 2666 |
out(" segs_out:%u", s->segs_out); |
| 2667 |
if (s->segs_in) |
| 2668 |
out(" segs_in:%u", s->segs_in); |
| 2669 |
if (s->data_segs_out) |
| 2670 |
out(" data_segs_out:%u", s->data_segs_out); |
| 2671 |
if (s->data_segs_in) |
| 2672 |
out(" data_segs_in:%u", s->data_segs_in); |
| 2673 |
|
| 2674 |
if (s->dctcp && s->dctcp->enabled) { |
| 2675 |
struct dctcpstat *dctcp = s->dctcp; |
| 2676 |
|
| 2677 |
out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)", |
| 2678 |
dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn, |
| 2679 |
dctcp->ab_tot); |
| 2680 |
} else if (s->dctcp) { |
| 2681 |
out(" dctcp:fallback_mode"); |
| 2682 |
} |
| 2683 |
|
| 2684 |
if (s->bbr_info) { |
| 2685 |
__u64 bw; |
| 2686 |
|
| 2687 |
bw = s->bbr_info->bbr_bw_hi; |
| 2688 |
bw <<= 32; |
| 2689 |
bw |= s->bbr_info->bbr_bw_lo; |
| 2690 |
|
| 2691 |
out(" bbr:(bw:%sbps,mrtt:%g", |
| 2692 |
sprint_bw(b1, bw * 8.0), |
| 2693 |
(double)s->bbr_info->bbr_min_rtt / 1000.0); |
| 2694 |
if (s->bbr_info->bbr_pacing_gain) |
| 2695 |
out(",pacing_gain:%g", |
| 2696 |
(double)s->bbr_info->bbr_pacing_gain / 256.0); |
| 2697 |
if (s->bbr_info->bbr_cwnd_gain) |
| 2698 |
out(",cwnd_gain:%g", |
| 2699 |
(double)s->bbr_info->bbr_cwnd_gain / 256.0); |
| 2700 |
out(")"); |
| 2701 |
} |
| 2702 |
|
| 2703 |
if (s->send_bps) |
| 2704 |
out(" send %sbps", sprint_bw(b1, s->send_bps)); |
| 2705 |
if (s->lastsnd) |
| 2706 |
out(" lastsnd:%u", s->lastsnd); |
| 2707 |
if (s->lastrcv) |
| 2708 |
out(" lastrcv:%u", s->lastrcv); |
| 2709 |
if (s->lastack) |
| 2710 |
out(" lastack:%u", s->lastack); |
| 2711 |
|
| 2712 |
if (s->pacing_rate) { |
| 2713 |
out(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate)); |
| 2714 |
if (s->pacing_rate_max) |
| 2715 |
out("/%sbps", sprint_bw(b1, s->pacing_rate_max)); |
| 2716 |
} |
| 2717 |
|
| 2718 |
if (s->delivery_rate) |
| 2719 |
out(" delivery_rate %sbps", sprint_bw(b1, s->delivery_rate)); |
| 2720 |
if (s->delivered) |
| 2721 |
out(" delivered:%u", s->delivered); |
| 2722 |
if (s->delivered_ce) |
| 2723 |
out(" delivered_ce:%u", s->delivered_ce); |
| 2724 |
if (s->app_limited) |
| 2725 |
out(" app_limited"); |
| 2726 |
|
| 2727 |
if (s->busy_time) { |
| 2728 |
out(" busy:%llums", s->busy_time / 1000); |
| 2729 |
if (s->rwnd_limited) |
| 2730 |
out(" rwnd_limited:%llums(%.1f%%)", |
| 2731 |
s->rwnd_limited / 1000, |
| 2732 |
100.0 * s->rwnd_limited / s->busy_time); |
| 2733 |
if (s->sndbuf_limited) |
| 2734 |
out(" sndbuf_limited:%llums(%.1f%%)", |
| 2735 |
s->sndbuf_limited / 1000, |
| 2736 |
100.0 * s->sndbuf_limited / s->busy_time); |
| 2737 |
} |
| 2738 |
|
| 2739 |
if (s->unacked) |
| 2740 |
out(" unacked:%u", s->unacked); |
| 2741 |
if (s->retrans || s->retrans_total) |
| 2742 |
out(" retrans:%u/%u", s->retrans, s->retrans_total); |
| 2743 |
if (s->lost) |
| 2744 |
out(" lost:%u", s->lost); |
| 2745 |
if (s->sacked && s->ss.state != SS_LISTEN) |
| 2746 |
out(" sacked:%u", s->sacked); |
| 2747 |
if (s->dsack_dups) |
| 2748 |
out(" dsack_dups:%u", s->dsack_dups); |
| 2749 |
if (s->fackets) |
| 2750 |
out(" fackets:%u", s->fackets); |
| 2751 |
if (s->reordering != 3) |
| 2752 |
out(" reordering:%d", s->reordering); |
| 2753 |
if (s->reord_seen) |
| 2754 |
out(" reord_seen:%d", s->reord_seen); |
| 2755 |
if (s->rcv_rtt) |
| 2756 |
out(" rcv_rtt:%g", s->rcv_rtt); |
| 2757 |
if (s->rcv_space) |
| 2758 |
out(" rcv_space:%d", s->rcv_space); |
| 2759 |
if (s->rcv_ssthresh) |
| 2760 |
out(" rcv_ssthresh:%u", s->rcv_ssthresh); |
| 2761 |
if (s->not_sent) |
| 2762 |
out(" notsent:%u", s->not_sent); |
| 2763 |
if (s->min_rtt) |
| 2764 |
out(" minrtt:%g", s->min_rtt); |
| 2765 |
if (s->rcv_ooopack) |
| 2766 |
out(" rcv_ooopack:%u", s->rcv_ooopack); |
| 2767 |
if (s->snd_wnd) |
| 2768 |
out(" snd_wnd:%u", s->snd_wnd); |
| 2769 |
if (s->rcv_wnd) |
| 2770 |
out(" rcv_wnd:%u", s->rcv_wnd); |
| 2771 |
if (s->rehash) |
| 2772 |
out(" rehash:%u", s->rehash); |
| 2773 |
} |
| 2774 |
|
| 2775 |
static void tcp_timer_print(struct tcpstat *s) |
| 2776 |
{ |
| 2777 |
static const char * const tmr_name[] = { |
| 2778 |
"off", |
| 2779 |
"on", |
| 2780 |
"keepalive", |
| 2781 |
"timewait", |
| 2782 |
"persist", |
| 2783 |
"unknown" |
| 2784 |
}; |
| 2785 |
|
| 2786 |
if (s->timer) { |
| 2787 |
if (s->timer > 4) |
| 2788 |
s->timer = 5; |
| 2789 |
out(" timer:(%s,%s,%d)", |
| 2790 |
tmr_name[s->timer], |
| 2791 |
print_ms_timer(s->timeout), |
| 2792 |
s->retrans); |
| 2793 |
} |
| 2794 |
} |
| 2795 |
|
| 2796 |
static void sctp_timer_print(struct tcpstat *s) |
| 2797 |
{ |
| 2798 |
if (s->timer) |
| 2799 |
out(" timer:(T3_RTX,%s,%d)", |
| 2800 |
print_ms_timer(s->timeout), s->retrans); |
| 2801 |
} |
| 2802 |
|
| 2803 |
static int tcp_show_line(char *line, const struct filter *f, int family) |
| 2804 |
{ |
| 2805 |
int rto = 0, ato = 0; |
| 2806 |
struct tcpstat s = {}; |
| 2807 |
char *loc, *rem, *data; |
| 2808 |
char opt[256]; |
| 2809 |
int n; |
| 2810 |
int hz = get_user_hz(); |
| 2811 |
|
| 2812 |
if (proc_inet_split_line(line, &loc, &rem, &data)) |
| 2813 |
return -1; |
| 2814 |
|
| 2815 |
int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 2816 |
|
| 2817 |
if (!(f->states & (1 << state))) |
| 2818 |
return 0; |
| 2819 |
|
| 2820 |
proc_parse_inet_addr(loc, rem, family, &s.ss); |
| 2821 |
|
| 2822 |
if (f->f && run_ssfilter(f->f, &s.ss) == 0) |
| 2823 |
return 0; |
| 2824 |
|
| 2825 |
opt[0] = 0; |
| 2826 |
n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n", |
| 2827 |
&s.ss.state, &s.ss.wq, &s.ss.rq, |
| 2828 |
&s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, |
| 2829 |
&s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, |
| 2830 |
&s.ssthresh, opt); |
| 2831 |
|
| 2832 |
if (n < 17) |
| 2833 |
opt[0] = 0; |
| 2834 |
|
| 2835 |
if (n < 12) { |
| 2836 |
rto = 0; |
| 2837 |
s.cwnd = 2; |
| 2838 |
s.ssthresh = -1; |
| 2839 |
ato = s.qack = 0; |
| 2840 |
} |
| 2841 |
|
| 2842 |
s.retrans = s.timer != 1 ? s.probes : s.retrans; |
| 2843 |
s.timeout = (s.timeout * 1000 + hz - 1) / hz; |
| 2844 |
s.ato = (double)ato / hz; |
| 2845 |
s.qack /= 2; |
| 2846 |
s.rto = (double)rto; |
| 2847 |
s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; |
| 2848 |
s.rto = s.rto != 3 * hz ? s.rto / hz : 0; |
| 2849 |
s.ss.type = IPPROTO_TCP; |
| 2850 |
|
| 2851 |
inet_stats_print(&s.ss, false); |
| 2852 |
|
| 2853 |
if (show_options) |
| 2854 |
tcp_timer_print(&s); |
| 2855 |
|
| 2856 |
if (show_details) { |
| 2857 |
sock_details_print(&s.ss); |
| 2858 |
if (opt[0]) |
| 2859 |
out(" opt:\"%s\"", opt); |
| 2860 |
} |
| 2861 |
|
| 2862 |
if (show_tcpinfo) |
| 2863 |
tcp_stats_print(&s); |
| 2864 |
|
| 2865 |
return 0; |
| 2866 |
} |
| 2867 |
|
| 2868 |
static int generic_record_read(FILE *fp, |
| 2869 |
int (*worker)(char*, const struct filter *, int), |
| 2870 |
const struct filter *f, int fam) |
| 2871 |
{ |
| 2872 |
char line[256]; |
| 2873 |
|
| 2874 |
/* skip header */ |
| 2875 |
if (fgets(line, sizeof(line), fp) == NULL) |
| 2876 |
goto outerr; |
| 2877 |
|
| 2878 |
while (fgets(line, sizeof(line), fp) != NULL) { |
| 2879 |
int n = strlen(line); |
| 2880 |
|
| 2881 |
if (n == 0 || line[n-1] != '\n') { |
| 2882 |
errno = -EINVAL; |
| 2883 |
return -1; |
| 2884 |
} |
| 2885 |
line[n-1] = 0; |
| 2886 |
|
| 2887 |
if (worker(line, f, fam) < 0) |
| 2888 |
return 0; |
| 2889 |
} |
| 2890 |
outerr: |
| 2891 |
|
| 2892 |
return ferror(fp) ? -1 : 0; |
| 2893 |
} |
| 2894 |
|
| 2895 |
static void print_skmeminfo(struct rtattr *tb[], int attrtype) |
| 2896 |
{ |
| 2897 |
const __u32 *skmeminfo; |
| 2898 |
|
| 2899 |
if (!tb[attrtype]) { |
| 2900 |
if (attrtype == INET_DIAG_SKMEMINFO) { |
| 2901 |
if (!tb[INET_DIAG_MEMINFO]) |
| 2902 |
return; |
| 2903 |
|
| 2904 |
const struct inet_diag_meminfo *minfo = |
| 2905 |
RTA_DATA(tb[INET_DIAG_MEMINFO]); |
| 2906 |
|
| 2907 |
out(" mem:(r%u,w%u,f%u,t%u)", |
| 2908 |
minfo->idiag_rmem, |
| 2909 |
minfo->idiag_wmem, |
| 2910 |
minfo->idiag_fmem, |
| 2911 |
minfo->idiag_tmem); |
| 2912 |
} |
| 2913 |
return; |
| 2914 |
} |
| 2915 |
|
| 2916 |
skmeminfo = RTA_DATA(tb[attrtype]); |
| 2917 |
|
| 2918 |
out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u", |
| 2919 |
skmeminfo[SK_MEMINFO_RMEM_ALLOC], |
| 2920 |
skmeminfo[SK_MEMINFO_RCVBUF], |
| 2921 |
skmeminfo[SK_MEMINFO_WMEM_ALLOC], |
| 2922 |
skmeminfo[SK_MEMINFO_SNDBUF], |
| 2923 |
skmeminfo[SK_MEMINFO_FWD_ALLOC], |
| 2924 |
skmeminfo[SK_MEMINFO_WMEM_QUEUED], |
| 2925 |
skmeminfo[SK_MEMINFO_OPTMEM]); |
| 2926 |
|
| 2927 |
if (RTA_PAYLOAD(tb[attrtype]) >= |
| 2928 |
(SK_MEMINFO_BACKLOG + 1) * sizeof(__u32)) |
| 2929 |
out(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]); |
| 2930 |
|
| 2931 |
if (RTA_PAYLOAD(tb[attrtype]) >= |
| 2932 |
(SK_MEMINFO_DROPS + 1) * sizeof(__u32)) |
| 2933 |
out(",d%u", skmeminfo[SK_MEMINFO_DROPS]); |
| 2934 |
|
| 2935 |
out(")"); |
| 2936 |
} |
| 2937 |
|
| 2938 |
/* like lib/utils.c print_escape_buf(), but use out(), not printf()! */ |
| 2939 |
static void out_escape_buf(const __u8 *buf, size_t len, const char *escape) |
| 2940 |
{ |
| 2941 |
size_t i; |
| 2942 |
|
| 2943 |
for (i = 0; i < len; ++i) { |
| 2944 |
if (isprint(buf[i]) && buf[i] != '\\' && |
| 2945 |
!strchr(escape, buf[i])) |
| 2946 |
out("%c", buf[i]); |
| 2947 |
else |
| 2948 |
out("\\%03o", buf[i]); |
| 2949 |
} |
| 2950 |
} |
| 2951 |
|
| 2952 |
static void print_md5sig(struct tcp_diag_md5sig *sig) |
| 2953 |
{ |
| 2954 |
out("%s/%d=", |
| 2955 |
format_host(sig->tcpm_family, |
| 2956 |
sig->tcpm_family == AF_INET6 ? 16 : 4, |
| 2957 |
&sig->tcpm_addr), |
| 2958 |
sig->tcpm_prefixlen); |
| 2959 |
out_escape_buf(sig->tcpm_key, sig->tcpm_keylen, " ,"); |
| 2960 |
} |
| 2961 |
|
| 2962 |
static void tcp_tls_version(struct rtattr *attr) |
| 2963 |
{ |
| 2964 |
u_int16_t val; |
| 2965 |
|
| 2966 |
if (!attr) |
| 2967 |
return; |
| 2968 |
val = rta_getattr_u16(attr); |
| 2969 |
|
| 2970 |
switch (val) { |
| 2971 |
case TLS_1_2_VERSION: |
| 2972 |
out(" version: 1.2"); |
| 2973 |
break; |
| 2974 |
case TLS_1_3_VERSION: |
| 2975 |
out(" version: 1.3"); |
| 2976 |
break; |
| 2977 |
default: |
| 2978 |
out(" version: unknown(%hu)", val); |
| 2979 |
break; |
| 2980 |
} |
| 2981 |
} |
| 2982 |
|
| 2983 |
static void tcp_tls_cipher(struct rtattr *attr) |
| 2984 |
{ |
| 2985 |
u_int16_t val; |
| 2986 |
|
| 2987 |
if (!attr) |
| 2988 |
return; |
| 2989 |
val = rta_getattr_u16(attr); |
| 2990 |
|
| 2991 |
switch (val) { |
| 2992 |
case TLS_CIPHER_AES_GCM_128: |
| 2993 |
out(" cipher: aes-gcm-128"); |
| 2994 |
break; |
| 2995 |
case TLS_CIPHER_AES_GCM_256: |
| 2996 |
out(" cipher: aes-gcm-256"); |
| 2997 |
break; |
| 2998 |
} |
| 2999 |
} |
| 3000 |
|
| 3001 |
static void tcp_tls_conf(const char *name, struct rtattr *attr) |
| 3002 |
{ |
| 3003 |
u_int16_t val; |
| 3004 |
|
| 3005 |
if (!attr) |
| 3006 |
return; |
| 3007 |
val = rta_getattr_u16(attr); |
| 3008 |
|
| 3009 |
switch (val) { |
| 3010 |
case TLS_CONF_BASE: |
| 3011 |
out(" %s: none", name); |
| 3012 |
break; |
| 3013 |
case TLS_CONF_SW: |
| 3014 |
out(" %s: sw", name); |
| 3015 |
break; |
| 3016 |
case TLS_CONF_HW: |
| 3017 |
out(" %s: hw", name); |
| 3018 |
break; |
| 3019 |
case TLS_CONF_HW_RECORD: |
| 3020 |
out(" %s: hw-record", name); |
| 3021 |
break; |
| 3022 |
default: |
| 3023 |
out(" %s: unknown(%hu)", name, val); |
| 3024 |
break; |
| 3025 |
} |
| 3026 |
} |
| 3027 |
|
| 3028 |
static void mptcp_subflow_info(struct rtattr *tb[]) |
| 3029 |
{ |
| 3030 |
u_int32_t flags = 0; |
| 3031 |
|
| 3032 |
if (tb[MPTCP_SUBFLOW_ATTR_FLAGS]) { |
| 3033 |
char caps[32 + 1] = { 0 }, *cap = &caps[0]; |
| 3034 |
|
| 3035 |
flags = rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_FLAGS]); |
| 3036 |
|
| 3037 |
if (flags & MPTCP_SUBFLOW_FLAG_MCAP_REM) |
| 3038 |
*cap++ = 'M'; |
| 3039 |
if (flags & MPTCP_SUBFLOW_FLAG_MCAP_LOC) |
| 3040 |
*cap++ = 'm'; |
| 3041 |
if (flags & MPTCP_SUBFLOW_FLAG_JOIN_REM) |
| 3042 |
*cap++ = 'J'; |
| 3043 |
if (flags & MPTCP_SUBFLOW_FLAG_JOIN_LOC) |
| 3044 |
*cap++ = 'j'; |
| 3045 |
if (flags & MPTCP_SUBFLOW_FLAG_BKUP_REM) |
| 3046 |
*cap++ = 'B'; |
| 3047 |
if (flags & MPTCP_SUBFLOW_FLAG_BKUP_LOC) |
| 3048 |
*cap++ = 'b'; |
| 3049 |
if (flags & MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED) |
| 3050 |
*cap++ = 'e'; |
| 3051 |
if (flags & MPTCP_SUBFLOW_FLAG_CONNECTED) |
| 3052 |
*cap++ = 'c'; |
| 3053 |
if (flags & MPTCP_SUBFLOW_FLAG_MAPVALID) |
| 3054 |
*cap++ = 'v'; |
| 3055 |
if (flags) |
| 3056 |
out(" flags:%s", caps); |
| 3057 |
} |
| 3058 |
if (tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM] && |
| 3059 |
tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC] && |
| 3060 |
tb[MPTCP_SUBFLOW_ATTR_ID_REM] && |
| 3061 |
tb[MPTCP_SUBFLOW_ATTR_ID_LOC]) |
| 3062 |
out(" token:%04x(id:%hhu)/%04x(id:%hhu)", |
| 3063 |
rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM]), |
| 3064 |
rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_REM]), |
| 3065 |
rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC]), |
| 3066 |
rta_getattr_u8(tb[MPTCP_SUBFLOW_ATTR_ID_LOC])); |
| 3067 |
if (tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ]) |
| 3068 |
out(" seq:%llu", |
| 3069 |
rta_getattr_u64(tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ])); |
| 3070 |
if (tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ]) |
| 3071 |
out(" sfseq:%u", |
| 3072 |
rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ])); |
| 3073 |
if (tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET]) |
| 3074 |
out(" ssnoff:%u", |
| 3075 |
rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET])); |
| 3076 |
if (tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN]) |
| 3077 |
out(" maplen:%u", |
| 3078 |
rta_getattr_u32(tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN])); |
| 3079 |
} |
| 3080 |
|
| 3081 |
#define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt)) |
| 3082 |
|
| 3083 |
static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
| 3084 |
struct rtattr *tb[]) |
| 3085 |
{ |
| 3086 |
double rtt = 0; |
| 3087 |
struct tcpstat s = {}; |
| 3088 |
|
| 3089 |
s.ss.state = r->idiag_state; |
| 3090 |
|
| 3091 |
print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
| 3092 |
|
| 3093 |
if (tb[INET_DIAG_INFO]) { |
| 3094 |
struct tcp_info *info; |
| 3095 |
int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
| 3096 |
|
| 3097 |
/* workaround for older kernels with less fields */ |
| 3098 |
if (len < sizeof(*info)) { |
| 3099 |
info = alloca(sizeof(*info)); |
| 3100 |
memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
| 3101 |
memset((char *)info + len, 0, sizeof(*info) - len); |
| 3102 |
} else |
| 3103 |
info = RTA_DATA(tb[INET_DIAG_INFO]); |
| 3104 |
|
| 3105 |
if (show_options) { |
| 3106 |
s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS); |
| 3107 |
s.has_usec_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_USEC_TS); |
| 3108 |
s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK); |
| 3109 |
s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN); |
| 3110 |
s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN); |
| 3111 |
s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA); |
| 3112 |
} |
| 3113 |
|
| 3114 |
if (tb[INET_DIAG_CONG]) |
| 3115 |
strncpy(s.cong_alg, |
| 3116 |
rta_getattr_str(tb[INET_DIAG_CONG]), |
| 3117 |
sizeof(s.cong_alg) - 1); |
| 3118 |
|
| 3119 |
if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) { |
| 3120 |
s.has_wscale_opt = true; |
| 3121 |
s.snd_wscale = info->tcpi_snd_wscale; |
| 3122 |
s.rcv_wscale = info->tcpi_rcv_wscale; |
| 3123 |
} |
| 3124 |
|
| 3125 |
if (info->tcpi_rto && info->tcpi_rto != 3000000) |
| 3126 |
s.rto = (double)info->tcpi_rto / 1000; |
| 3127 |
|
| 3128 |
s.backoff = info->tcpi_backoff; |
| 3129 |
s.rtt = (double)info->tcpi_rtt / 1000; |
| 3130 |
s.rttvar = (double)info->tcpi_rttvar / 1000; |
| 3131 |
s.ato = (double)info->tcpi_ato / 1000; |
| 3132 |
s.mss = info->tcpi_snd_mss; |
| 3133 |
s.rcv_mss = info->tcpi_rcv_mss; |
| 3134 |
s.advmss = info->tcpi_advmss; |
| 3135 |
s.rcv_space = info->tcpi_rcv_space; |
| 3136 |
s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000; |
| 3137 |
s.lastsnd = info->tcpi_last_data_sent; |
| 3138 |
s.lastrcv = info->tcpi_last_data_recv; |
| 3139 |
s.lastack = info->tcpi_last_ack_recv; |
| 3140 |
s.unacked = info->tcpi_unacked; |
| 3141 |
s.retrans = info->tcpi_retrans; |
| 3142 |
s.retrans_total = info->tcpi_total_retrans; |
| 3143 |
s.lost = info->tcpi_lost; |
| 3144 |
s.sacked = info->tcpi_sacked; |
| 3145 |
s.fackets = info->tcpi_fackets; |
| 3146 |
s.reordering = info->tcpi_reordering; |
| 3147 |
s.rcv_ssthresh = info->tcpi_rcv_ssthresh; |
| 3148 |
s.cwnd = info->tcpi_snd_cwnd; |
| 3149 |
s.pmtu = info->tcpi_pmtu; |
| 3150 |
|
| 3151 |
if (info->tcpi_snd_ssthresh < 0xFFFF) |
| 3152 |
s.ssthresh = info->tcpi_snd_ssthresh; |
| 3153 |
|
| 3154 |
rtt = (double) info->tcpi_rtt; |
| 3155 |
if (tb[INET_DIAG_VEGASINFO]) { |
| 3156 |
const struct tcpvegas_info *vinfo |
| 3157 |
= RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
| 3158 |
|
| 3159 |
if (vinfo->tcpv_enabled && |
| 3160 |
vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
| 3161 |
rtt = vinfo->tcpv_rtt; |
| 3162 |
} |
| 3163 |
|
| 3164 |
if (tb[INET_DIAG_DCTCPINFO]) { |
| 3165 |
struct dctcpstat *dctcp = malloc(sizeof(struct |
| 3166 |
dctcpstat)); |
| 3167 |
|
| 3168 |
const struct tcp_dctcp_info *dinfo |
| 3169 |
= RTA_DATA(tb[INET_DIAG_DCTCPINFO]); |
| 3170 |
|
| 3171 |
dctcp->enabled = !!dinfo->dctcp_enabled; |
| 3172 |
dctcp->ce_state = dinfo->dctcp_ce_state; |
| 3173 |
dctcp->alpha = dinfo->dctcp_alpha; |
| 3174 |
dctcp->ab_ecn = dinfo->dctcp_ab_ecn; |
| 3175 |
dctcp->ab_tot = dinfo->dctcp_ab_tot; |
| 3176 |
s.dctcp = dctcp; |
| 3177 |
} |
| 3178 |
|
| 3179 |
if (tb[INET_DIAG_BBRINFO]) { |
| 3180 |
const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]); |
| 3181 |
int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]), |
| 3182 |
sizeof(*s.bbr_info)); |
| 3183 |
|
| 3184 |
s.bbr_info = calloc(1, sizeof(*s.bbr_info)); |
| 3185 |
if (s.bbr_info && bbr_info) |
| 3186 |
memcpy(s.bbr_info, bbr_info, len); |
| 3187 |
} |
| 3188 |
|
| 3189 |
if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { |
| 3190 |
s.send_bps = (double) info->tcpi_snd_cwnd * |
| 3191 |
(double)info->tcpi_snd_mss * 8000000. / rtt; |
| 3192 |
} |
| 3193 |
|
| 3194 |
if (info->tcpi_pacing_rate && |
| 3195 |
info->tcpi_pacing_rate != ~0ULL) { |
| 3196 |
s.pacing_rate = info->tcpi_pacing_rate * 8.; |
| 3197 |
|
| 3198 |
if (info->tcpi_max_pacing_rate && |
| 3199 |
info->tcpi_max_pacing_rate != ~0ULL) |
| 3200 |
s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.; |
| 3201 |
} |
| 3202 |
s.bytes_acked = info->tcpi_bytes_acked; |
| 3203 |
s.bytes_received = info->tcpi_bytes_received; |
| 3204 |
s.segs_out = info->tcpi_segs_out; |
| 3205 |
s.segs_in = info->tcpi_segs_in; |
| 3206 |
s.data_segs_out = info->tcpi_data_segs_out; |
| 3207 |
s.data_segs_in = info->tcpi_data_segs_in; |
| 3208 |
s.not_sent = info->tcpi_notsent_bytes; |
| 3209 |
if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U) |
| 3210 |
s.min_rtt = (double) info->tcpi_min_rtt / 1000; |
| 3211 |
s.delivery_rate = info->tcpi_delivery_rate * 8.; |
| 3212 |
s.app_limited = info->tcpi_delivery_rate_app_limited; |
| 3213 |
s.busy_time = info->tcpi_busy_time; |
| 3214 |
s.rwnd_limited = info->tcpi_rwnd_limited; |
| 3215 |
s.sndbuf_limited = info->tcpi_sndbuf_limited; |
| 3216 |
s.delivered = info->tcpi_delivered; |
| 3217 |
s.delivered_ce = info->tcpi_delivered_ce; |
| 3218 |
s.dsack_dups = info->tcpi_dsack_dups; |
| 3219 |
s.reord_seen = info->tcpi_reord_seen; |
| 3220 |
s.bytes_sent = info->tcpi_bytes_sent; |
| 3221 |
s.bytes_retrans = info->tcpi_bytes_retrans; |
| 3222 |
s.rcv_ooopack = info->tcpi_rcv_ooopack; |
| 3223 |
s.snd_wnd = info->tcpi_snd_wnd; |
| 3224 |
s.rcv_wnd = info->tcpi_rcv_wnd; |
| 3225 |
s.rehash = info->tcpi_rehash; |
| 3226 |
tcp_stats_print(&s); |
| 3227 |
free(s.dctcp); |
| 3228 |
free(s.bbr_info); |
| 3229 |
} |
| 3230 |
if (tb[INET_DIAG_MD5SIG]) { |
| 3231 |
struct tcp_diag_md5sig *sig = RTA_DATA(tb[INET_DIAG_MD5SIG]); |
| 3232 |
int len = RTA_PAYLOAD(tb[INET_DIAG_MD5SIG]); |
| 3233 |
|
| 3234 |
out(" md5keys:"); |
| 3235 |
print_md5sig(sig++); |
| 3236 |
for (len -= sizeof(*sig); len > 0; len -= sizeof(*sig)) { |
| 3237 |
out(","); |
| 3238 |
print_md5sig(sig++); |
| 3239 |
} |
| 3240 |
} |
| 3241 |
if (tb[INET_DIAG_ULP_INFO]) { |
| 3242 |
struct rtattr *ulpinfo[INET_ULP_INFO_MAX + 1] = { 0 }; |
| 3243 |
|
| 3244 |
parse_rtattr_nested(ulpinfo, INET_ULP_INFO_MAX, |
| 3245 |
tb[INET_DIAG_ULP_INFO]); |
| 3246 |
|
| 3247 |
if (ulpinfo[INET_ULP_INFO_NAME]) |
| 3248 |
out(" tcp-ulp-%s", |
| 3249 |
rta_getattr_str(ulpinfo[INET_ULP_INFO_NAME])); |
| 3250 |
|
| 3251 |
if (ulpinfo[INET_ULP_INFO_TLS]) { |
| 3252 |
struct rtattr *tlsinfo[TLS_INFO_MAX + 1] = { 0 }; |
| 3253 |
|
| 3254 |
parse_rtattr_nested(tlsinfo, TLS_INFO_MAX, |
| 3255 |
ulpinfo[INET_ULP_INFO_TLS]); |
| 3256 |
|
| 3257 |
tcp_tls_version(tlsinfo[TLS_INFO_VERSION]); |
| 3258 |
tcp_tls_cipher(tlsinfo[TLS_INFO_CIPHER]); |
| 3259 |
tcp_tls_conf("rxconf", tlsinfo[TLS_INFO_RXCONF]); |
| 3260 |
tcp_tls_conf("txconf", tlsinfo[TLS_INFO_TXCONF]); |
| 3261 |
if (!!tlsinfo[TLS_INFO_ZC_RO_TX]) |
| 3262 |
out(" zc_ro_tx"); |
| 3263 |
if (!!tlsinfo[TLS_INFO_RX_NO_PAD]) |
| 3264 |
out(" no_pad_rx"); |
| 3265 |
} |
| 3266 |
if (ulpinfo[INET_ULP_INFO_MPTCP]) { |
| 3267 |
struct rtattr *sfinfo[MPTCP_SUBFLOW_ATTR_MAX + 1] = |
| 3268 |
{ 0 }; |
| 3269 |
|
| 3270 |
parse_rtattr_nested(sfinfo, MPTCP_SUBFLOW_ATTR_MAX, |
| 3271 |
ulpinfo[INET_ULP_INFO_MPTCP]); |
| 3272 |
mptcp_subflow_info(sfinfo); |
| 3273 |
} |
| 3274 |
} |
| 3275 |
} |
| 3276 |
|
| 3277 |
static void mptcp_stats_print(struct mptcp_info *s) |
| 3278 |
{ |
| 3279 |
if (s->mptcpi_subflows) |
| 3280 |
out(" subflows:%u", s->mptcpi_subflows); |
| 3281 |
if (s->mptcpi_add_addr_signal) |
| 3282 |
out(" add_addr_signal:%u", s->mptcpi_add_addr_signal); |
| 3283 |
if (s->mptcpi_add_addr_accepted) |
| 3284 |
out(" add_addr_accepted:%u", s->mptcpi_add_addr_accepted); |
| 3285 |
if (s->mptcpi_subflows_max) |
| 3286 |
out(" subflows_max:%u", s->mptcpi_subflows_max); |
| 3287 |
if (s->mptcpi_add_addr_signal_max) |
| 3288 |
out(" add_addr_signal_max:%u", s->mptcpi_add_addr_signal_max); |
| 3289 |
if (s->mptcpi_add_addr_accepted_max) |
| 3290 |
out(" add_addr_accepted_max:%u", s->mptcpi_add_addr_accepted_max); |
| 3291 |
if (s->mptcpi_flags & MPTCP_INFO_FLAG_FALLBACK) |
| 3292 |
out(" fallback"); |
| 3293 |
if (s->mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED) |
| 3294 |
out(" remote_key"); |
| 3295 |
if (s->mptcpi_token) |
| 3296 |
out(" token:%x", s->mptcpi_token); |
| 3297 |
if (s->mptcpi_write_seq) |
| 3298 |
out(" write_seq:%llu", s->mptcpi_write_seq); |
| 3299 |
if (s->mptcpi_snd_una) |
| 3300 |
out(" snd_una:%llu", s->mptcpi_snd_una); |
| 3301 |
if (s->mptcpi_rcv_nxt) |
| 3302 |
out(" rcv_nxt:%llu", s->mptcpi_rcv_nxt); |
| 3303 |
if (s->mptcpi_local_addr_used) |
| 3304 |
out(" local_addr_used:%u", s->mptcpi_local_addr_used); |
| 3305 |
if (s->mptcpi_local_addr_max) |
| 3306 |
out(" local_addr_max:%u", s->mptcpi_local_addr_max); |
| 3307 |
if (s->mptcpi_csum_enabled) |
| 3308 |
out(" csum_enabled:%u", s->mptcpi_csum_enabled); |
| 3309 |
if (s->mptcpi_retransmits) |
| 3310 |
out(" retransmits:%u", s->mptcpi_retransmits); |
| 3311 |
if (s->mptcpi_bytes_retrans) |
| 3312 |
out(" bytes_retrans:%llu", s->mptcpi_bytes_retrans); |
| 3313 |
if (s->mptcpi_bytes_sent) |
| 3314 |
out(" bytes_sent:%llu", s->mptcpi_bytes_sent); |
| 3315 |
if (s->mptcpi_bytes_received) |
| 3316 |
out(" bytes_received:%llu", s->mptcpi_bytes_received); |
| 3317 |
if (s->mptcpi_bytes_acked) |
| 3318 |
out(" bytes_acked:%llu", s->mptcpi_bytes_acked); |
| 3319 |
if (s->mptcpi_subflows_total) |
| 3320 |
out(" subflows_total:%u", s->mptcpi_subflows_total); |
| 3321 |
if (s->mptcpi_endp_laminar_max) |
| 3322 |
out(" endp_laminar_max:%u", s->mptcpi_endp_laminar_max); |
| 3323 |
if (s->mptcpi_last_data_sent) |
| 3324 |
out(" last_data_sent:%u", s->mptcpi_last_data_sent); |
| 3325 |
if (s->mptcpi_last_data_recv) |
| 3326 |
out(" last_data_recv:%u", s->mptcpi_last_data_recv); |
| 3327 |
if (s->mptcpi_last_ack_recv) |
| 3328 |
out(" last_ack_recv:%u", s->mptcpi_last_ack_recv); |
| 3329 |
} |
| 3330 |
|
| 3331 |
static void mptcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
| 3332 |
struct rtattr *tb[]) |
| 3333 |
{ |
| 3334 |
print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
| 3335 |
|
| 3336 |
if (tb[INET_DIAG_INFO]) { |
| 3337 |
struct mptcp_info *info; |
| 3338 |
int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
| 3339 |
|
| 3340 |
/* workaround for older kernels with less fields */ |
| 3341 |
if (len < sizeof(*info)) { |
| 3342 |
info = alloca(sizeof(*info)); |
| 3343 |
memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
| 3344 |
memset((char *)info + len, 0, sizeof(*info) - len); |
| 3345 |
} else |
| 3346 |
info = RTA_DATA(tb[INET_DIAG_INFO]); |
| 3347 |
|
| 3348 |
mptcp_stats_print(info); |
| 3349 |
} |
| 3350 |
} |
| 3351 |
|
| 3352 |
static const char *format_host_sa(struct sockaddr_storage *sa) |
| 3353 |
{ |
| 3354 |
union { |
| 3355 |
struct sockaddr_in sin; |
| 3356 |
struct sockaddr_in6 sin6; |
| 3357 |
} *saddr = (void *)sa; |
| 3358 |
|
| 3359 |
switch (sa->ss_family) { |
| 3360 |
case AF_INET: |
| 3361 |
return format_host(AF_INET, 4, &saddr->sin.sin_addr); |
| 3362 |
case AF_INET6: |
| 3363 |
return format_host(AF_INET6, 16, &saddr->sin6.sin6_addr); |
| 3364 |
default: |
| 3365 |
return ""; |
| 3366 |
} |
| 3367 |
} |
| 3368 |
|
| 3369 |
static void sctp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
| 3370 |
struct rtattr *tb[]) |
| 3371 |
{ |
| 3372 |
struct sockaddr_storage *sa; |
| 3373 |
int len; |
| 3374 |
|
| 3375 |
print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
| 3376 |
|
| 3377 |
if (tb[INET_DIAG_LOCALS]) { |
| 3378 |
len = RTA_PAYLOAD(tb[INET_DIAG_LOCALS]); |
| 3379 |
sa = RTA_DATA(tb[INET_DIAG_LOCALS]); |
| 3380 |
|
| 3381 |
out(" locals:%s", format_host_sa(sa)); |
| 3382 |
for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) |
| 3383 |
out(",%s", format_host_sa(sa)); |
| 3384 |
|
| 3385 |
} |
| 3386 |
if (tb[INET_DIAG_PEERS]) { |
| 3387 |
len = RTA_PAYLOAD(tb[INET_DIAG_PEERS]); |
| 3388 |
sa = RTA_DATA(tb[INET_DIAG_PEERS]); |
| 3389 |
|
| 3390 |
out(" peers:%s", format_host_sa(sa)); |
| 3391 |
for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) |
| 3392 |
out(",%s", format_host_sa(sa)); |
| 3393 |
} |
| 3394 |
if (tb[INET_DIAG_INFO]) { |
| 3395 |
struct sctp_info *info; |
| 3396 |
len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
| 3397 |
|
| 3398 |
/* workaround for older kernels with less fields */ |
| 3399 |
if (len < sizeof(*info)) { |
| 3400 |
info = alloca(sizeof(*info)); |
| 3401 |
memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
| 3402 |
memset((char *)info + len, 0, sizeof(*info) - len); |
| 3403 |
} else |
| 3404 |
info = RTA_DATA(tb[INET_DIAG_INFO]); |
| 3405 |
|
| 3406 |
sctp_stats_print(info); |
| 3407 |
} |
| 3408 |
} |
| 3409 |
|
| 3410 |
static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s) |
| 3411 |
{ |
| 3412 |
struct rtattr *tb[INET_DIAG_MAX+1]; |
| 3413 |
struct inet_diag_msg *r = NLMSG_DATA(nlh); |
| 3414 |
|
| 3415 |
parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), |
| 3416 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 3417 |
|
| 3418 |
s->state = r->idiag_state; |
| 3419 |
s->local.family = s->remote.family = r->idiag_family; |
| 3420 |
s->lport = ntohs(r->id.idiag_sport); |
| 3421 |
s->rport = ntohs(r->id.idiag_dport); |
| 3422 |
s->wq = r->idiag_wqueue; |
| 3423 |
s->rq = r->idiag_rqueue; |
| 3424 |
s->ino = r->idiag_inode; |
| 3425 |
s->uid = r->idiag_uid; |
| 3426 |
s->iface = r->id.idiag_if; |
| 3427 |
s->sk = cookie_sk_get(&r->id.idiag_cookie[0]); |
| 3428 |
|
| 3429 |
s->mark = 0; |
| 3430 |
if (tb[INET_DIAG_MARK]) |
| 3431 |
s->mark = rta_getattr_u32(tb[INET_DIAG_MARK]); |
| 3432 |
s->cgroup_id = 0; |
| 3433 |
if (tb[INET_DIAG_CGROUP_ID]) |
| 3434 |
s->cgroup_id = rta_getattr_u64(tb[INET_DIAG_CGROUP_ID]); |
| 3435 |
if (tb[INET_DIAG_PROTOCOL]) |
| 3436 |
s->raw_prot = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); |
| 3437 |
else |
| 3438 |
s->raw_prot = 0; |
| 3439 |
|
| 3440 |
if (s->local.family == AF_INET) |
| 3441 |
s->local.bytelen = s->remote.bytelen = 4; |
| 3442 |
else |
| 3443 |
s->local.bytelen = s->remote.bytelen = 16; |
| 3444 |
|
| 3445 |
memcpy(s->local.data, r->id.idiag_src, s->local.bytelen); |
| 3446 |
memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen); |
| 3447 |
} |
| 3448 |
|
| 3449 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 3450 |
|
| 3451 |
#define MAX_NR_BPF_MAP_ID_OPTS 32 |
| 3452 |
|
| 3453 |
struct btf; |
| 3454 |
|
| 3455 |
static struct bpf_map_opts { |
| 3456 |
unsigned int nr_maps; |
| 3457 |
struct bpf_sk_storage_map_info { |
| 3458 |
unsigned int id; |
| 3459 |
int fd; |
| 3460 |
struct bpf_map_info info; |
| 3461 |
struct btf *btf; |
| 3462 |
struct btf_dump *dump; |
| 3463 |
} maps[MAX_NR_BPF_MAP_ID_OPTS]; |
| 3464 |
bool show_all; |
| 3465 |
} bpf_map_opts; |
| 3466 |
|
| 3467 |
static void bpf_map_opts_mixed_error(void) |
| 3468 |
{ |
| 3469 |
fprintf(stderr, |
| 3470 |
"ss: --bpf-maps and --bpf-map-id cannot be used together\n"); |
| 3471 |
} |
| 3472 |
|
| 3473 |
static int bpf_maps_opts_load_btf(struct bpf_map_info *info, struct btf **btf) |
| 3474 |
{ |
| 3475 |
if (info->btf_value_type_id) { |
| 3476 |
*btf = btf__load_from_kernel_by_id(info->btf_id); |
| 3477 |
if (!*btf) { |
| 3478 |
fprintf(stderr, "ss: failed to load BTF for map ID %u\n", |
| 3479 |
info->id); |
| 3480 |
return -1; |
| 3481 |
} |
| 3482 |
} else { |
| 3483 |
*btf = NULL; |
| 3484 |
} |
| 3485 |
|
| 3486 |
return 0; |
| 3487 |
} |
| 3488 |
|
| 3489 |
__attribute__((format(printf, 2, 0))) |
| 3490 |
static void out_bpf_sk_storage_print_fn(void *ctx, const char *fmt, va_list args) |
| 3491 |
{ |
| 3492 |
vout(fmt, args); |
| 3493 |
} |
| 3494 |
|
| 3495 |
static int bpf_map_opts_load_info(unsigned int map_id) |
| 3496 |
{ |
| 3497 |
struct btf_dump_opts dopts = { |
| 3498 |
.sz = sizeof(struct btf_dump_opts) |
| 3499 |
}; |
| 3500 |
struct bpf_map_info info = {}; |
| 3501 |
uint32_t len = sizeof(info); |
| 3502 |
struct btf_dump *dump; |
| 3503 |
struct btf *btf; |
| 3504 |
int fd; |
| 3505 |
int r; |
| 3506 |
|
| 3507 |
if (bpf_map_opts.nr_maps == MAX_NR_BPF_MAP_ID_OPTS) { |
| 3508 |
fprintf(stderr, |
| 3509 |
"ss: too many (> %u) BPF socket-local storage maps found, skipping map ID %u\n", |
| 3510 |
MAX_NR_BPF_MAP_ID_OPTS, map_id); |
| 3511 |
return 0; |
| 3512 |
} |
| 3513 |
|
| 3514 |
fd = bpf_map_get_fd_by_id(map_id); |
| 3515 |
if (fd < 0) { |
| 3516 |
if (errno == -ENOENT) |
| 3517 |
return 0; |
| 3518 |
|
| 3519 |
fprintf(stderr, "ss: cannot get fd for BPF map ID %u%s\n", |
| 3520 |
map_id, errno == EPERM ? |
| 3521 |
": missing root permissions, CAP_BPF, or CAP_SYS_ADMIN" : ""); |
| 3522 |
return -1; |
| 3523 |
} |
| 3524 |
|
| 3525 |
r = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 3526 |
if (r) { |
| 3527 |
fprintf(stderr, "ss: failed to get info for BPF map ID %u\n", |
| 3528 |
map_id); |
| 3529 |
close(fd); |
| 3530 |
return -1; |
| 3531 |
} |
| 3532 |
|
| 3533 |
if (info.type != BPF_MAP_TYPE_SK_STORAGE) { |
| 3534 |
fprintf(stderr, |
| 3535 |
"ss: BPF map with ID %s has type ID %d, expecting %d ('sk_storage')\n", |
| 3536 |
optarg, info.type, BPF_MAP_TYPE_SK_STORAGE); |
| 3537 |
close(fd); |
| 3538 |
return -1; |
| 3539 |
} |
| 3540 |
|
| 3541 |
r = bpf_maps_opts_load_btf(&info, &btf); |
| 3542 |
if (r) { |
| 3543 |
close(fd); |
| 3544 |
return -1; |
| 3545 |
} |
| 3546 |
|
| 3547 |
dump = btf_dump__new(btf, out_bpf_sk_storage_print_fn, NULL, &dopts); |
| 3548 |
if (!dump) { |
| 3549 |
btf__free(btf); |
| 3550 |
close(fd); |
| 3551 |
fprintf(stderr, "Failed to create btf_dump object\n"); |
| 3552 |
return -1; |
| 3553 |
} |
| 3554 |
|
| 3555 |
bpf_map_opts.maps[bpf_map_opts.nr_maps].id = map_id; |
| 3556 |
bpf_map_opts.maps[bpf_map_opts.nr_maps].fd = fd; |
| 3557 |
bpf_map_opts.maps[bpf_map_opts.nr_maps].info = info; |
| 3558 |
bpf_map_opts.maps[bpf_map_opts.nr_maps].btf = btf; |
| 3559 |
bpf_map_opts.maps[bpf_map_opts.nr_maps++].dump = dump; |
| 3560 |
|
| 3561 |
return 0; |
| 3562 |
} |
| 3563 |
|
| 3564 |
static struct bpf_sk_storage_map_info *bpf_map_opts_get_info( |
| 3565 |
unsigned int map_id) |
| 3566 |
{ |
| 3567 |
unsigned int i; |
| 3568 |
int r; |
| 3569 |
|
| 3570 |
for (i = 0; i < bpf_map_opts.nr_maps; ++i) { |
| 3571 |
if (bpf_map_opts.maps[i].id == map_id) |
| 3572 |
return &bpf_map_opts.maps[i]; |
| 3573 |
} |
| 3574 |
|
| 3575 |
r = bpf_map_opts_load_info(map_id); |
| 3576 |
if (r) |
| 3577 |
return NULL; |
| 3578 |
|
| 3579 |
return &bpf_map_opts.maps[bpf_map_opts.nr_maps - 1]; |
| 3580 |
} |
| 3581 |
|
| 3582 |
static int bpf_map_opts_add_id(const char *optarg) |
| 3583 |
{ |
| 3584 |
size_t optarg_len; |
| 3585 |
unsigned long id; |
| 3586 |
char *end; |
| 3587 |
|
| 3588 |
if (bpf_map_opts.show_all) { |
| 3589 |
bpf_map_opts_mixed_error(); |
| 3590 |
return -1; |
| 3591 |
} |
| 3592 |
|
| 3593 |
optarg_len = strlen(optarg); |
| 3594 |
id = strtoul(optarg, &end, 0); |
| 3595 |
if (end != optarg + optarg_len || id == 0 || id >= UINT32_MAX) { |
| 3596 |
fprintf(stderr, "ss: invalid BPF map ID %s\n", optarg); |
| 3597 |
return -1; |
| 3598 |
} |
| 3599 |
|
| 3600 |
/* Force lazy loading of the map's data. */ |
| 3601 |
if (!bpf_map_opts_get_info(id)) |
| 3602 |
return -1; |
| 3603 |
|
| 3604 |
return 0; |
| 3605 |
} |
| 3606 |
|
| 3607 |
static void bpf_map_opts_destroy(void) |
| 3608 |
{ |
| 3609 |
int i; |
| 3610 |
|
| 3611 |
for (i = 0; i < bpf_map_opts.nr_maps; ++i) { |
| 3612 |
btf_dump__free(bpf_map_opts.maps[i].dump); |
| 3613 |
btf__free(bpf_map_opts.maps[i].btf); |
| 3614 |
close(bpf_map_opts.maps[i].fd); |
| 3615 |
} |
| 3616 |
} |
| 3617 |
|
| 3618 |
static struct rtattr *bpf_map_opts_alloc_rta(void) |
| 3619 |
{ |
| 3620 |
struct rtattr *stgs_rta, *fd_rta; |
| 3621 |
size_t total_size; |
| 3622 |
unsigned int i; |
| 3623 |
void *buf; |
| 3624 |
|
| 3625 |
/* If bpf_map_opts.show_all == true, we will send an empty message to |
| 3626 |
* the kernel, which will return all the socket-local data attached to |
| 3627 |
* a socket, no matter their map ID |
| 3628 |
*/ |
| 3629 |
if (bpf_map_opts.show_all) { |
| 3630 |
total_size = RTA_LENGTH(0); |
| 3631 |
} else { |
| 3632 |
total_size = RTA_LENGTH(RTA_LENGTH(sizeof(int)) * |
| 3633 |
bpf_map_opts.nr_maps); |
| 3634 |
} |
| 3635 |
|
| 3636 |
buf = malloc(total_size); |
| 3637 |
if (!buf) |
| 3638 |
return NULL; |
| 3639 |
|
| 3640 |
stgs_rta = buf; |
| 3641 |
stgs_rta->rta_type = INET_DIAG_REQ_SK_BPF_STORAGES | NLA_F_NESTED; |
| 3642 |
stgs_rta->rta_len = total_size; |
| 3643 |
|
| 3644 |
/* If inet_show_netlink() retries fetching socket data, nr_maps might |
| 3645 |
* be different from 0, even with show_all == true, so we return early |
| 3646 |
* to avoid inserting specific map IDs into the request. |
| 3647 |
*/ |
| 3648 |
if (bpf_map_opts.show_all) |
| 3649 |
return stgs_rta; |
| 3650 |
|
| 3651 |
buf = RTA_DATA(stgs_rta); |
| 3652 |
for (i = 0; i < bpf_map_opts.nr_maps; i++) { |
| 3653 |
int *fd; |
| 3654 |
|
| 3655 |
fd_rta = buf; |
| 3656 |
fd_rta->rta_type = SK_DIAG_BPF_STORAGE_REQ_MAP_FD; |
| 3657 |
fd_rta->rta_len = RTA_LENGTH(sizeof(int)); |
| 3658 |
|
| 3659 |
fd = RTA_DATA(fd_rta); |
| 3660 |
*fd = bpf_map_opts.maps[i].fd; |
| 3661 |
|
| 3662 |
buf += fd_rta->rta_len; |
| 3663 |
} |
| 3664 |
|
| 3665 |
return stgs_rta; |
| 3666 |
} |
| 3667 |
|
| 3668 |
static void out_bpf_sk_storage_oneline(struct bpf_sk_storage_map_info *info, |
| 3669 |
const void *data, size_t len) |
| 3670 |
{ |
| 3671 |
struct btf_dump_type_data_opts opts = { |
| 3672 |
.sz = sizeof(struct btf_dump_type_data_opts), |
| 3673 |
.emit_zeroes = 1, |
| 3674 |
.compact = 1 |
| 3675 |
}; |
| 3676 |
int r; |
| 3677 |
|
| 3678 |
out(" map_id:%d", info->id); |
| 3679 |
r = btf_dump__dump_type_data(info->dump, info->info.btf_value_type_id, |
| 3680 |
data, len, &opts); |
| 3681 |
if (r < 0) |
| 3682 |
out("failed to dump data: %d", r); |
| 3683 |
} |
| 3684 |
|
| 3685 |
static void out_bpf_sk_storage_multiline(struct bpf_sk_storage_map_info *info, |
| 3686 |
const void *data, size_t len) |
| 3687 |
{ |
| 3688 |
struct btf_dump_type_data_opts opts = { |
| 3689 |
.sz = sizeof(struct btf_dump_type_data_opts), |
| 3690 |
.indent_level = 2, |
| 3691 |
.emit_zeroes = 1 |
| 3692 |
}; |
| 3693 |
int r; |
| 3694 |
|
| 3695 |
out("\n\tmap_id:%d [\n", info->id); |
| 3696 |
|
| 3697 |
r = btf_dump__dump_type_data(info->dump, info->info.btf_value_type_id, |
| 3698 |
data, len, &opts); |
| 3699 |
if (r < 0) |
| 3700 |
out("\t\tfailed to dump data: %d", r); |
| 3701 |
|
| 3702 |
out("\n\t]"); |
| 3703 |
} |
| 3704 |
|
| 3705 |
static void out_bpf_sk_storage(int map_id, const void *data, size_t len) |
| 3706 |
{ |
| 3707 |
struct bpf_sk_storage_map_info *map_info; |
| 3708 |
|
| 3709 |
map_info = bpf_map_opts_get_info(map_id); |
| 3710 |
if (!map_info) { |
| 3711 |
/* The kernel might return a map we can't get info for, skip |
| 3712 |
* it but print the other ones. |
| 3713 |
*/ |
| 3714 |
out("\n\tmap_id: %d failed to fetch info, skipping\n", map_id); |
| 3715 |
return; |
| 3716 |
} |
| 3717 |
|
| 3718 |
if (map_info->info.value_size != len) { |
| 3719 |
fprintf(stderr, |
| 3720 |
"map_id: %d: invalid value size, expecting %u, got %lu\n", |
| 3721 |
map_id, map_info->info.value_size, len); |
| 3722 |
return; |
| 3723 |
} |
| 3724 |
|
| 3725 |
if (oneline) |
| 3726 |
out_bpf_sk_storage_oneline(map_info, data, len); |
| 3727 |
else |
| 3728 |
out_bpf_sk_storage_multiline(map_info, data, len); |
| 3729 |
} |
| 3730 |
|
| 3731 |
static void show_sk_bpf_storages(struct rtattr *bpf_stgs) |
| 3732 |
{ |
| 3733 |
struct rtattr *tb[SK_DIAG_BPF_STORAGE_MAX + 1], *bpf_stg; |
| 3734 |
unsigned int rem, map_id; |
| 3735 |
struct rtattr *value; |
| 3736 |
|
| 3737 |
for (bpf_stg = RTA_DATA(bpf_stgs), rem = RTA_PAYLOAD(bpf_stgs); |
| 3738 |
RTA_OK(bpf_stg, rem); bpf_stg = RTA_NEXT(bpf_stg, rem)) { |
| 3739 |
|
| 3740 |
if ((bpf_stg->rta_type & NLA_TYPE_MASK) != SK_DIAG_BPF_STORAGE) |
| 3741 |
continue; |
| 3742 |
|
| 3743 |
parse_rtattr_nested(tb, SK_DIAG_BPF_STORAGE_MAX, |
| 3744 |
(struct rtattr *)bpf_stg); |
| 3745 |
|
| 3746 |
if (tb[SK_DIAG_BPF_STORAGE_MAP_ID]) { |
| 3747 |
map_id = rta_getattr_u32(tb[SK_DIAG_BPF_STORAGE_MAP_ID]); |
| 3748 |
value = tb[SK_DIAG_BPF_STORAGE_MAP_VALUE]; |
| 3749 |
|
| 3750 |
out_bpf_sk_storage(map_id, RTA_DATA(value), |
| 3751 |
RTA_PAYLOAD(value)); |
| 3752 |
} |
| 3753 |
} |
| 3754 |
} |
| 3755 |
|
| 3756 |
static bool bpf_map_opts_is_enabled(void) |
| 3757 |
{ |
| 3758 |
return bpf_map_opts.nr_maps || bpf_map_opts.show_all; |
| 3759 |
} |
| 3760 |
#endif |
| 3761 |
|
| 3762 |
static int inet_show_sock(struct nlmsghdr *nlh, |
| 3763 |
struct sockstat *s) |
| 3764 |
{ |
| 3765 |
struct rtattr *tb[INET_DIAG_MAX+1]; |
| 3766 |
struct inet_diag_msg *r = NLMSG_DATA(nlh); |
| 3767 |
unsigned char v6only = 0; |
| 3768 |
|
| 3769 |
parse_rtattr_flags(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), |
| 3770 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)), |
| 3771 |
NLA_F_NESTED); |
| 3772 |
|
| 3773 |
if (tb[INET_DIAG_PROTOCOL]) |
| 3774 |
s->type = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); |
| 3775 |
|
| 3776 |
if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) |
| 3777 |
v6only = rta_getattr_u8(tb[INET_DIAG_SKV6ONLY]); |
| 3778 |
|
| 3779 |
inet_stats_print(s, v6only); |
| 3780 |
|
| 3781 |
if (show_options) { |
| 3782 |
struct tcpstat t = {}; |
| 3783 |
|
| 3784 |
t.timer = r->idiag_timer; |
| 3785 |
t.timeout = r->idiag_expires; |
| 3786 |
t.retrans = r->idiag_retrans; |
| 3787 |
if (s->type == IPPROTO_SCTP) |
| 3788 |
sctp_timer_print(&t); |
| 3789 |
else |
| 3790 |
tcp_timer_print(&t); |
| 3791 |
} |
| 3792 |
|
| 3793 |
if (show_details) { |
| 3794 |
sock_details_print(s); |
| 3795 |
if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) |
| 3796 |
out(" v6only:%u", v6only); |
| 3797 |
|
| 3798 |
if (tb[INET_DIAG_SHUTDOWN]) { |
| 3799 |
unsigned char mask; |
| 3800 |
|
| 3801 |
mask = rta_getattr_u8(tb[INET_DIAG_SHUTDOWN]); |
| 3802 |
out(" %c-%c", |
| 3803 |
mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); |
| 3804 |
} |
| 3805 |
} |
| 3806 |
|
| 3807 |
if (show_tos) { |
| 3808 |
if (tb[INET_DIAG_TOS]) |
| 3809 |
out(" tos:%#x", rta_getattr_u8(tb[INET_DIAG_TOS])); |
| 3810 |
if (tb[INET_DIAG_TCLASS]) |
| 3811 |
out(" tclass:%#x", rta_getattr_u8(tb[INET_DIAG_TCLASS])); |
| 3812 |
if (tb[INET_DIAG_CLASS_ID]) |
| 3813 |
out(" class_id:%#x", rta_getattr_u32(tb[INET_DIAG_CLASS_ID])); |
| 3814 |
} |
| 3815 |
|
| 3816 |
if (show_cgroup) { |
| 3817 |
if (tb[INET_DIAG_CGROUP_ID]) |
| 3818 |
out(" cgroup:%s", cg_id_to_path(rta_getattr_u64(tb[INET_DIAG_CGROUP_ID]))); |
| 3819 |
} |
| 3820 |
|
| 3821 |
if (show_inet_sockopt) { |
| 3822 |
if (tb[INET_DIAG_SOCKOPT] && RTA_PAYLOAD(tb[INET_DIAG_SOCKOPT]) >= |
| 3823 |
sizeof(struct inet_diag_sockopt)) { |
| 3824 |
const struct inet_diag_sockopt *sockopt = |
| 3825 |
RTA_DATA(tb[INET_DIAG_SOCKOPT]); |
| 3826 |
if (!oneline) |
| 3827 |
out("\n\tinet-sockopt: ("); |
| 3828 |
else |
| 3829 |
out(" inet-sockopt: ("); |
| 3830 |
if (sockopt->recverr) |
| 3831 |
out(" recverr"); |
| 3832 |
if (sockopt->is_icsk) |
| 3833 |
out(" is_icsk"); |
| 3834 |
if (sockopt->freebind) |
| 3835 |
out(" freebind"); |
| 3836 |
if (sockopt->hdrincl) |
| 3837 |
out(" hdrincl"); |
| 3838 |
if (sockopt->mc_loop) |
| 3839 |
out(" mc_loop"); |
| 3840 |
if (sockopt->transparent) |
| 3841 |
out(" transparent"); |
| 3842 |
if (sockopt->mc_all) |
| 3843 |
out(" mc_all"); |
| 3844 |
if (sockopt->nodefrag) |
| 3845 |
out(" nodefrag"); |
| 3846 |
if (sockopt->bind_address_no_port) |
| 3847 |
out(" bind_addr_no_port"); |
| 3848 |
if (sockopt->recverr_rfc4884) |
| 3849 |
out(" recverr_rfc4884"); |
| 3850 |
if (sockopt->defer_connect) |
| 3851 |
out(" defer_connect"); |
| 3852 |
out(")"); |
| 3853 |
} |
| 3854 |
} |
| 3855 |
|
| 3856 |
if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) { |
| 3857 |
if (!oneline) |
| 3858 |
out("\n\t"); |
| 3859 |
if (s->type == IPPROTO_SCTP) |
| 3860 |
sctp_show_info(nlh, r, tb); |
| 3861 |
else if (s->type == IPPROTO_MPTCP) |
| 3862 |
mptcp_show_info(nlh, r, tb); |
| 3863 |
else |
| 3864 |
tcp_show_info(nlh, r, tb); |
| 3865 |
} |
| 3866 |
sctp_ino = s->ino; |
| 3867 |
|
| 3868 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 3869 |
if (tb[INET_DIAG_SK_BPF_STORAGES]) |
| 3870 |
show_sk_bpf_storages(tb[INET_DIAG_SK_BPF_STORAGES]); |
| 3871 |
#endif |
| 3872 |
|
| 3873 |
return 0; |
| 3874 |
} |
| 3875 |
|
| 3876 |
static int tcpdiag_send(int fd, int protocol, struct filter *f) |
| 3877 |
{ |
| 3878 |
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
| 3879 |
struct { |
| 3880 |
struct nlmsghdr nlh; |
| 3881 |
struct inet_diag_req r; |
| 3882 |
} req = { |
| 3883 |
.nlh.nlmsg_len = sizeof(req), |
| 3884 |
.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST, |
| 3885 |
.nlh.nlmsg_seq = MAGIC_SEQ, |
| 3886 |
.r.idiag_family = AF_INET, |
| 3887 |
.r.idiag_states = f->states, |
| 3888 |
}; |
| 3889 |
char *bc = NULL; |
| 3890 |
int bclen; |
| 3891 |
struct msghdr msg; |
| 3892 |
struct rtattr rta; |
| 3893 |
struct iovec iov[3]; |
| 3894 |
int iovlen = 1; |
| 3895 |
|
| 3896 |
if (protocol == IPPROTO_TCP) |
| 3897 |
req.nlh.nlmsg_type = TCPDIAG_GETSOCK; |
| 3898 |
else if (protocol == IPPROTO_DCCP) |
| 3899 |
req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; |
| 3900 |
else |
| 3901 |
return -1; |
| 3902 |
|
| 3903 |
if (show_mem) { |
| 3904 |
req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
| 3905 |
req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
| 3906 |
} |
| 3907 |
|
| 3908 |
if (show_tcpinfo) { |
| 3909 |
req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
| 3910 |
req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); |
| 3911 |
req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); |
| 3912 |
} |
| 3913 |
|
| 3914 |
if (show_tos) { |
| 3915 |
req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1)); |
| 3916 |
req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1)); |
| 3917 |
} |
| 3918 |
|
| 3919 |
iov[0] = (struct iovec){ |
| 3920 |
.iov_base = &req, |
| 3921 |
.iov_len = sizeof(req) |
| 3922 |
}; |
| 3923 |
if (f->f) { |
| 3924 |
bclen = ssfilter_bytecompile(f->f, &bc); |
| 3925 |
if (bclen) { |
| 3926 |
rta.rta_type = INET_DIAG_REQ_BYTECODE; |
| 3927 |
rta.rta_len = RTA_LENGTH(bclen); |
| 3928 |
iov[1] = (struct iovec){ &rta, sizeof(rta) }; |
| 3929 |
iov[2] = (struct iovec){ bc, bclen }; |
| 3930 |
req.nlh.nlmsg_len += RTA_LENGTH(bclen); |
| 3931 |
iovlen = 3; |
| 3932 |
} |
| 3933 |
} |
| 3934 |
|
| 3935 |
msg = (struct msghdr) { |
| 3936 |
.msg_name = (void *)&nladdr, |
| 3937 |
.msg_namelen = sizeof(nladdr), |
| 3938 |
.msg_iov = iov, |
| 3939 |
.msg_iovlen = iovlen, |
| 3940 |
}; |
| 3941 |
|
| 3942 |
if (sendmsg(fd, &msg, 0) < 0) { |
| 3943 |
close(fd); |
| 3944 |
return -1; |
| 3945 |
} |
| 3946 |
|
| 3947 |
return 0; |
| 3948 |
} |
| 3949 |
|
| 3950 |
static int sockdiag_send(int family, int fd, int protocol, struct filter *f) |
| 3951 |
{ |
| 3952 |
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
| 3953 |
DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
| 3954 |
struct rtattr *bpf_rta = NULL; |
| 3955 |
char *bc = NULL; |
| 3956 |
int bclen; |
| 3957 |
__u32 proto; |
| 3958 |
struct msghdr msg; |
| 3959 |
struct rtattr rta_bc; |
| 3960 |
struct rtattr rta_proto; |
| 3961 |
struct iovec iov[6]; |
| 3962 |
int iovlen = 1; |
| 3963 |
|
| 3964 |
if (family == PF_UNSPEC) |
| 3965 |
return tcpdiag_send(fd, protocol, f); |
| 3966 |
|
| 3967 |
memset(&req.r, 0, sizeof(req.r)); |
| 3968 |
req.r.sdiag_family = family; |
| 3969 |
req.r.sdiag_protocol = protocol; |
| 3970 |
req.r.idiag_states = f->states; |
| 3971 |
if (show_mem) { |
| 3972 |
req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
| 3973 |
req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
| 3974 |
} |
| 3975 |
|
| 3976 |
if (show_tcpinfo) { |
| 3977 |
req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
| 3978 |
req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); |
| 3979 |
req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); |
| 3980 |
} |
| 3981 |
|
| 3982 |
if (show_tos) { |
| 3983 |
req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1)); |
| 3984 |
req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1)); |
| 3985 |
} |
| 3986 |
|
| 3987 |
iov[0] = (struct iovec){ |
| 3988 |
.iov_base = &req, |
| 3989 |
.iov_len = sizeof(req) |
| 3990 |
}; |
| 3991 |
if (f->f) { |
| 3992 |
bclen = ssfilter_bytecompile(f->f, &bc); |
| 3993 |
if (bclen) { |
| 3994 |
rta_bc.rta_type = INET_DIAG_REQ_BYTECODE; |
| 3995 |
rta_bc.rta_len = RTA_LENGTH(bclen); |
| 3996 |
iov[1] = (struct iovec){ &rta_bc, sizeof(rta_bc) }; |
| 3997 |
iov[2] = (struct iovec){ bc, bclen }; |
| 3998 |
req.nlh.nlmsg_len += RTA_LENGTH(bclen); |
| 3999 |
iovlen = 3; |
| 4000 |
} |
| 4001 |
} |
| 4002 |
|
| 4003 |
/* put extended protocol attribute, if required */ |
| 4004 |
if (protocol > 255) { |
| 4005 |
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL; |
| 4006 |
rta_proto.rta_len = RTA_LENGTH(sizeof(proto)); |
| 4007 |
proto = protocol; |
| 4008 |
iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto) }; |
| 4009 |
iov[iovlen + 1] = (struct iovec){ &proto, sizeof(proto) }; |
| 4010 |
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto)); |
| 4011 |
iovlen += 2; |
| 4012 |
} |
| 4013 |
|
| 4014 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 4015 |
if (bpf_map_opts_is_enabled()) { |
| 4016 |
bpf_rta = bpf_map_opts_alloc_rta(); |
| 4017 |
if (!bpf_rta) { |
| 4018 |
fprintf(stderr, |
| 4019 |
"ss: cannot alloc request for --bpf-map\n"); |
| 4020 |
return -1; |
| 4021 |
} |
| 4022 |
|
| 4023 |
iov[iovlen++] = (struct iovec){ bpf_rta, bpf_rta->rta_len }; |
| 4024 |
req.nlh.nlmsg_len += bpf_rta->rta_len; |
| 4025 |
} |
| 4026 |
#endif |
| 4027 |
|
| 4028 |
msg = (struct msghdr) { |
| 4029 |
.msg_name = (void *)&nladdr, |
| 4030 |
.msg_namelen = sizeof(nladdr), |
| 4031 |
.msg_iov = iov, |
| 4032 |
.msg_iovlen = iovlen, |
| 4033 |
}; |
| 4034 |
|
| 4035 |
if (sendmsg(fd, &msg, 0) < 0) { |
| 4036 |
free(bpf_rta); |
| 4037 |
close(fd); |
| 4038 |
return -1; |
| 4039 |
} |
| 4040 |
|
| 4041 |
free(bpf_rta); |
| 4042 |
|
| 4043 |
return 0; |
| 4044 |
} |
| 4045 |
|
| 4046 |
struct inet_diag_arg { |
| 4047 |
struct filter *f; |
| 4048 |
int protocol; |
| 4049 |
struct rtnl_handle *rth; |
| 4050 |
}; |
| 4051 |
|
| 4052 |
static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s) |
| 4053 |
{ |
| 4054 |
struct inet_diag_msg *d = NLMSG_DATA(h); |
| 4055 |
struct inet_diag_arg *diag_arg = arg; |
| 4056 |
struct rtnl_handle *rth = diag_arg->rth; |
| 4057 |
|
| 4058 |
DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
| 4059 |
|
| 4060 |
req.nlh.nlmsg_type = SOCK_DESTROY; |
| 4061 |
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| 4062 |
req.nlh.nlmsg_seq = ++rth->seq; |
| 4063 |
req.r.sdiag_family = d->idiag_family; |
| 4064 |
req.r.sdiag_protocol = diag_arg->protocol; |
| 4065 |
req.r.id = d->id; |
| 4066 |
|
| 4067 |
if (diag_arg->protocol == IPPROTO_RAW) { |
| 4068 |
struct inet_diag_req_raw *raw = (void *)&req.r; |
| 4069 |
|
| 4070 |
BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw)); |
| 4071 |
raw->sdiag_raw_protocol = s->raw_prot; |
| 4072 |
} |
| 4073 |
|
| 4074 |
return rtnl_talk(rth, &req.nlh, NULL); |
| 4075 |
} |
| 4076 |
|
| 4077 |
static int show_one_inet_sock(struct nlmsghdr *h, void *arg) |
| 4078 |
{ |
| 4079 |
int err; |
| 4080 |
struct inet_diag_arg *diag_arg = arg; |
| 4081 |
struct inet_diag_msg *r = NLMSG_DATA(h); |
| 4082 |
struct sockstat s = {}; |
| 4083 |
|
| 4084 |
if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family))) |
| 4085 |
return 0; |
| 4086 |
|
| 4087 |
parse_diag_msg(h, &s); |
| 4088 |
s.type = diag_arg->protocol; |
| 4089 |
|
| 4090 |
if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0) |
| 4091 |
return 0; |
| 4092 |
|
| 4093 |
if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) { |
| 4094 |
if (errno == EOPNOTSUPP || errno == ENOENT) { |
| 4095 |
/* Socket can't be closed, or is already closed. */ |
| 4096 |
return 0; |
| 4097 |
} else { |
| 4098 |
perror("SOCK_DESTROY answers"); |
| 4099 |
return -1; |
| 4100 |
} |
| 4101 |
} |
| 4102 |
|
| 4103 |
err = inet_show_sock(h, &s); |
| 4104 |
if (err < 0) |
| 4105 |
return err; |
| 4106 |
|
| 4107 |
return 0; |
| 4108 |
} |
| 4109 |
|
| 4110 |
static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) |
| 4111 |
{ |
| 4112 |
int err = 0; |
| 4113 |
struct rtnl_handle rth, rth2; |
| 4114 |
int family = PF_INET; |
| 4115 |
struct inet_diag_arg arg = { .f = f, .protocol = protocol }; |
| 4116 |
|
| 4117 |
if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
| 4118 |
return -1; |
| 4119 |
|
| 4120 |
if (f->kill) { |
| 4121 |
if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) { |
| 4122 |
rtnl_close(&rth); |
| 4123 |
return -1; |
| 4124 |
} |
| 4125 |
arg.rth = &rth2; |
| 4126 |
} |
| 4127 |
|
| 4128 |
rth.dump = MAGIC_SEQ; |
| 4129 |
rth.dump_fp = dump_fp; |
| 4130 |
if (preferred_family == PF_INET6) |
| 4131 |
family = PF_INET6; |
| 4132 |
|
| 4133 |
/* Suppress netlink errors. Older kernels do not support extended |
| 4134 |
* protocol requests using INET_DIAG_REQ_PROTOCOL, and some protocols |
| 4135 |
* may not be available in the running kernel (e.g. SCTP, DCCP). |
| 4136 |
* In both cases the kernel returns EINVAL which would cause |
| 4137 |
* rtnl_dump_error() to print a confusing "RTNETLINK answers" error. |
| 4138 |
*/ |
| 4139 |
rth.flags |= RTNL_HANDLE_F_SUPPRESS_NLERR; |
| 4140 |
|
| 4141 |
again: |
| 4142 |
if ((err = sockdiag_send(family, rth.fd, protocol, f))) |
| 4143 |
goto Exit; |
| 4144 |
|
| 4145 |
if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) { |
| 4146 |
if (family != PF_UNSPEC) { |
| 4147 |
family = PF_UNSPEC; |
| 4148 |
goto again; |
| 4149 |
} |
| 4150 |
goto Exit; |
| 4151 |
} |
| 4152 |
if (family == PF_INET && preferred_family != PF_INET) { |
| 4153 |
family = PF_INET6; |
| 4154 |
goto again; |
| 4155 |
} |
| 4156 |
|
| 4157 |
Exit: |
| 4158 |
rtnl_close(&rth); |
| 4159 |
if (arg.rth) |
| 4160 |
rtnl_close(arg.rth); |
| 4161 |
return err; |
| 4162 |
} |
| 4163 |
|
| 4164 |
static int tcp_show_netlink_file(struct filter *f) |
| 4165 |
{ |
| 4166 |
FILE *fp; |
| 4167 |
char buf[16384]; |
| 4168 |
int err = -1; |
| 4169 |
|
| 4170 |
if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { |
| 4171 |
perror("fopen($TCPDIAG_FILE)"); |
| 4172 |
return err; |
| 4173 |
} |
| 4174 |
|
| 4175 |
while (1) { |
| 4176 |
int err2; |
| 4177 |
size_t status, nitems; |
| 4178 |
struct nlmsghdr *h = (struct nlmsghdr *)buf; |
| 4179 |
struct sockstat s = {}; |
| 4180 |
|
| 4181 |
status = fread(buf, 1, sizeof(*h), fp); |
| 4182 |
if (status != sizeof(*h)) { |
| 4183 |
if (ferror(fp)) |
| 4184 |
perror("Reading header from $TCPDIAG_FILE"); |
| 4185 |
if (feof(fp)) |
| 4186 |
fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE"); |
| 4187 |
break; |
| 4188 |
} |
| 4189 |
|
| 4190 |
nitems = NLMSG_ALIGN(h->nlmsg_len - sizeof(*h)); |
| 4191 |
status = fread(h+1, 1, nitems, fp); |
| 4192 |
|
| 4193 |
if (status != nitems) { |
| 4194 |
if (ferror(fp)) |
| 4195 |
perror("Reading $TCPDIAG_FILE"); |
| 4196 |
if (feof(fp)) |
| 4197 |
fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE"); |
| 4198 |
break; |
| 4199 |
} |
| 4200 |
|
| 4201 |
/* The only legal exit point */ |
| 4202 |
if (h->nlmsg_type == NLMSG_DONE) { |
| 4203 |
err = 0; |
| 4204 |
break; |
| 4205 |
} |
| 4206 |
|
| 4207 |
if (h->nlmsg_type == NLMSG_ERROR) { |
| 4208 |
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
| 4209 |
|
| 4210 |
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 4211 |
fprintf(stderr, "ERROR truncated\n"); |
| 4212 |
} else { |
| 4213 |
errno = -err->error; |
| 4214 |
perror("TCPDIAG answered"); |
| 4215 |
} |
| 4216 |
break; |
| 4217 |
} |
| 4218 |
|
| 4219 |
parse_diag_msg(h, &s); |
| 4220 |
s.type = IPPROTO_TCP; |
| 4221 |
|
| 4222 |
if (f && f->f && run_ssfilter(f->f, &s) == 0) |
| 4223 |
continue; |
| 4224 |
|
| 4225 |
err2 = inet_show_sock(h, &s); |
| 4226 |
if (err2 < 0) { |
| 4227 |
err = err2; |
| 4228 |
break; |
| 4229 |
} |
| 4230 |
} |
| 4231 |
|
| 4232 |
fclose(fp); |
| 4233 |
return err; |
| 4234 |
} |
| 4235 |
|
| 4236 |
static int tcp_show(struct filter *f) |
| 4237 |
{ |
| 4238 |
FILE *fp = NULL; |
| 4239 |
char *buf = NULL; |
| 4240 |
int bufsize = 1024*1024; |
| 4241 |
|
| 4242 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4243 |
return 0; |
| 4244 |
|
| 4245 |
dg_proto = TCP_PROTO; |
| 4246 |
|
| 4247 |
if (getenv("TCPDIAG_FILE")) |
| 4248 |
return tcp_show_netlink_file(f); |
| 4249 |
|
| 4250 |
if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") |
| 4251 |
&& inet_show_netlink(f, NULL, IPPROTO_TCP) == 0) |
| 4252 |
return 0; |
| 4253 |
|
| 4254 |
/* Sigh... We have to parse /proc/net/tcp... */ |
| 4255 |
while (bufsize >= 64*1024) { |
| 4256 |
if ((buf = malloc(bufsize)) != NULL) |
| 4257 |
break; |
| 4258 |
bufsize /= 2; |
| 4259 |
} |
| 4260 |
if (buf == NULL) { |
| 4261 |
errno = ENOMEM; |
| 4262 |
return -1; |
| 4263 |
} |
| 4264 |
|
| 4265 |
if (f->families & FAMILY_MASK(AF_INET)) { |
| 4266 |
if ((fp = net_tcp_open()) == NULL) |
| 4267 |
goto outerr; |
| 4268 |
|
| 4269 |
setbuffer(fp, buf, bufsize); |
| 4270 |
if (generic_record_read(fp, tcp_show_line, f, AF_INET)) |
| 4271 |
goto outerr; |
| 4272 |
fclose(fp); |
| 4273 |
} |
| 4274 |
|
| 4275 |
if ((f->families & FAMILY_MASK(AF_INET6)) && |
| 4276 |
(fp = net_tcp6_open()) != NULL) { |
| 4277 |
setbuffer(fp, buf, bufsize); |
| 4278 |
if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) |
| 4279 |
goto outerr; |
| 4280 |
fclose(fp); |
| 4281 |
} |
| 4282 |
|
| 4283 |
free(buf); |
| 4284 |
return 0; |
| 4285 |
|
| 4286 |
outerr: |
| 4287 |
do { |
| 4288 |
int saved_errno = errno; |
| 4289 |
|
| 4290 |
free(buf); |
| 4291 |
if (fp) |
| 4292 |
fclose(fp); |
| 4293 |
errno = saved_errno; |
| 4294 |
return -1; |
| 4295 |
} while (0); |
| 4296 |
} |
| 4297 |
|
| 4298 |
static int mptcp_show(struct filter *f) |
| 4299 |
{ |
| 4300 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4301 |
return 0; |
| 4302 |
|
| 4303 |
if (!getenv("PROC_NET_MPTCP") && !getenv("PROC_ROOT") |
| 4304 |
&& inet_show_netlink(f, NULL, IPPROTO_MPTCP) == 0) |
| 4305 |
return 0; |
| 4306 |
|
| 4307 |
return 0; |
| 4308 |
} |
| 4309 |
|
| 4310 |
static int dccp_show(struct filter *f) |
| 4311 |
{ |
| 4312 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4313 |
return 0; |
| 4314 |
|
| 4315 |
if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT") |
| 4316 |
&& inet_show_netlink(f, NULL, IPPROTO_DCCP) == 0) |
| 4317 |
return 0; |
| 4318 |
|
| 4319 |
return 0; |
| 4320 |
} |
| 4321 |
|
| 4322 |
static int sctp_show(struct filter *f) |
| 4323 |
{ |
| 4324 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4325 |
return 0; |
| 4326 |
|
| 4327 |
if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT") |
| 4328 |
&& inet_show_netlink(f, NULL, IPPROTO_SCTP) == 0) |
| 4329 |
return 0; |
| 4330 |
|
| 4331 |
return 0; |
| 4332 |
} |
| 4333 |
|
| 4334 |
static int dgram_show_line(char *line, const struct filter *f, int family) |
| 4335 |
{ |
| 4336 |
struct sockstat s = {}; |
| 4337 |
char *loc, *rem, *data; |
| 4338 |
char opt[256]; |
| 4339 |
int n; |
| 4340 |
|
| 4341 |
if (proc_inet_split_line(line, &loc, &rem, &data)) |
| 4342 |
return -1; |
| 4343 |
|
| 4344 |
int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 4345 |
|
| 4346 |
if (!(f->states & (1 << state))) |
| 4347 |
return 0; |
| 4348 |
|
| 4349 |
proc_parse_inet_addr(loc, rem, family, &s); |
| 4350 |
|
| 4351 |
if (f->f && run_ssfilter(f->f, &s) == 0) |
| 4352 |
return 0; |
| 4353 |
|
| 4354 |
opt[0] = 0; |
| 4355 |
n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
| 4356 |
&s.state, &s.wq, &s.rq, |
| 4357 |
&s.uid, &s.ino, |
| 4358 |
&s.refcnt, &s.sk, opt); |
| 4359 |
|
| 4360 |
if (n < 9) |
| 4361 |
opt[0] = 0; |
| 4362 |
|
| 4363 |
s.type = dg_proto == UDP_PROTO ? IPPROTO_UDP : 0; |
| 4364 |
inet_stats_print(&s, false); |
| 4365 |
|
| 4366 |
if (show_details && opt[0]) |
| 4367 |
out(" opt:\"%s\"", opt); |
| 4368 |
|
| 4369 |
return 0; |
| 4370 |
} |
| 4371 |
|
| 4372 |
static int udp_show(struct filter *f) |
| 4373 |
{ |
| 4374 |
FILE *fp = NULL; |
| 4375 |
|
| 4376 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4377 |
return 0; |
| 4378 |
|
| 4379 |
dg_proto = UDP_PROTO; |
| 4380 |
|
| 4381 |
if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") |
| 4382 |
&& inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) |
| 4383 |
return 0; |
| 4384 |
|
| 4385 |
if (f->families&FAMILY_MASK(AF_INET)) { |
| 4386 |
if ((fp = net_udp_open()) == NULL) |
| 4387 |
goto outerr; |
| 4388 |
if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
| 4389 |
goto outerr; |
| 4390 |
fclose(fp); |
| 4391 |
} |
| 4392 |
|
| 4393 |
if ((f->families&FAMILY_MASK(AF_INET6)) && |
| 4394 |
(fp = net_udp6_open()) != NULL) { |
| 4395 |
if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
| 4396 |
goto outerr; |
| 4397 |
fclose(fp); |
| 4398 |
} |
| 4399 |
return 0; |
| 4400 |
|
| 4401 |
outerr: |
| 4402 |
do { |
| 4403 |
int saved_errno = errno; |
| 4404 |
|
| 4405 |
if (fp) |
| 4406 |
fclose(fp); |
| 4407 |
errno = saved_errno; |
| 4408 |
return -1; |
| 4409 |
} while (0); |
| 4410 |
} |
| 4411 |
|
| 4412 |
static int raw_show(struct filter *f) |
| 4413 |
{ |
| 4414 |
FILE *fp = NULL; |
| 4415 |
|
| 4416 |
if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 4417 |
return 0; |
| 4418 |
|
| 4419 |
dg_proto = RAW_PROTO; |
| 4420 |
|
| 4421 |
if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") && |
| 4422 |
inet_show_netlink(f, NULL, IPPROTO_RAW) == 0) |
| 4423 |
return 0; |
| 4424 |
|
| 4425 |
if (f->families&FAMILY_MASK(AF_INET)) { |
| 4426 |
if ((fp = net_raw_open()) == NULL) |
| 4427 |
goto outerr; |
| 4428 |
if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
| 4429 |
goto outerr; |
| 4430 |
fclose(fp); |
| 4431 |
} |
| 4432 |
|
| 4433 |
if ((f->families&FAMILY_MASK(AF_INET6)) && |
| 4434 |
(fp = net_raw6_open()) != NULL) { |
| 4435 |
if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
| 4436 |
goto outerr; |
| 4437 |
fclose(fp); |
| 4438 |
} |
| 4439 |
return 0; |
| 4440 |
|
| 4441 |
outerr: |
| 4442 |
do { |
| 4443 |
int saved_errno = errno; |
| 4444 |
|
| 4445 |
if (fp) |
| 4446 |
fclose(fp); |
| 4447 |
errno = saved_errno; |
| 4448 |
return -1; |
| 4449 |
} while (0); |
| 4450 |
} |
| 4451 |
|
| 4452 |
#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) |
| 4453 |
|
| 4454 |
static void unix_list_drop_first(struct sockstat **list) |
| 4455 |
{ |
| 4456 |
struct sockstat *s = *list; |
| 4457 |
|
| 4458 |
(*list) = (*list)->next; |
| 4459 |
free(s->name); |
| 4460 |
free(s); |
| 4461 |
} |
| 4462 |
|
| 4463 |
static bool unix_type_skip(struct sockstat *s, struct filter *f) |
| 4464 |
{ |
| 4465 |
if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) |
| 4466 |
return true; |
| 4467 |
if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) |
| 4468 |
return true; |
| 4469 |
if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB))) |
| 4470 |
return true; |
| 4471 |
return false; |
| 4472 |
} |
| 4473 |
|
| 4474 |
static void unix_stats_print(struct sockstat *s, struct filter *f) |
| 4475 |
{ |
| 4476 |
char port_name[30] = {}; |
| 4477 |
|
| 4478 |
sock_state_print(s); |
| 4479 |
|
| 4480 |
sock_addr_print(s->name ?: "*", " ", |
| 4481 |
uint_to_str(s->lport, port_name), NULL); |
| 4482 |
sock_addr_print(s->peer_name ?: "*", " ", |
| 4483 |
uint_to_str(s->rport, port_name), NULL); |
| 4484 |
|
| 4485 |
proc_ctx_print(s); |
| 4486 |
} |
| 4487 |
|
| 4488 |
static int unix_show_sock(struct nlmsghdr *nlh, void *arg) |
| 4489 |
{ |
| 4490 |
struct filter *f = (struct filter *)arg; |
| 4491 |
struct unix_diag_msg *r = NLMSG_DATA(nlh); |
| 4492 |
struct rtattr *tb[UNIX_DIAG_MAX+1]; |
| 4493 |
char name[128]; |
| 4494 |
struct sockstat stat = { .name = "*", .peer_name = "*" }; |
| 4495 |
|
| 4496 |
parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1), |
| 4497 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 4498 |
|
| 4499 |
stat.type = r->udiag_type; |
| 4500 |
stat.state = r->udiag_state; |
| 4501 |
stat.ino = stat.lport = r->udiag_ino; |
| 4502 |
stat.local.family = stat.remote.family = AF_UNIX; |
| 4503 |
|
| 4504 |
if (unix_type_skip(&stat, f)) |
| 4505 |
return 0; |
| 4506 |
|
| 4507 |
if (tb[UNIX_DIAG_RQLEN]) { |
| 4508 |
struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); |
| 4509 |
|
| 4510 |
stat.rq = rql->udiag_rqueue; |
| 4511 |
stat.wq = rql->udiag_wqueue; |
| 4512 |
} |
| 4513 |
if (tb[UNIX_DIAG_NAME]) { |
| 4514 |
int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); |
| 4515 |
|
| 4516 |
memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); |
| 4517 |
name[len] = '\0'; |
| 4518 |
if (name[0] == '\0') { |
| 4519 |
int i; |
| 4520 |
for (i = 0; i < len; i++) |
| 4521 |
if (name[i] == '\0') |
| 4522 |
name[i] = '@'; |
| 4523 |
} |
| 4524 |
stat.name = &name[0]; |
| 4525 |
memcpy(stat.local.data, &stat.name, sizeof(stat.name)); |
| 4526 |
} |
| 4527 |
if (tb[UNIX_DIAG_PEER]) |
| 4528 |
stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); |
| 4529 |
|
| 4530 |
if (f->f && run_ssfilter(f->f, &stat) == 0) |
| 4531 |
return 0; |
| 4532 |
|
| 4533 |
unix_stats_print(&stat, f); |
| 4534 |
|
| 4535 |
if (show_mem) |
| 4536 |
print_skmeminfo(tb, UNIX_DIAG_MEMINFO); |
| 4537 |
if (show_details) { |
| 4538 |
if (tb[UNIX_DIAG_SHUTDOWN]) { |
| 4539 |
unsigned char mask; |
| 4540 |
|
| 4541 |
mask = rta_getattr_u8(tb[UNIX_DIAG_SHUTDOWN]); |
| 4542 |
out(" %c-%c", |
| 4543 |
mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); |
| 4544 |
} |
| 4545 |
if (tb[UNIX_DIAG_VFS]) { |
| 4546 |
struct unix_diag_vfs *uv = RTA_DATA(tb[UNIX_DIAG_VFS]); |
| 4547 |
|
| 4548 |
out(" ino:%u dev:%u/%u", uv->udiag_vfs_ino, major(uv->udiag_vfs_dev), |
| 4549 |
minor(uv->udiag_vfs_dev)); |
| 4550 |
} |
| 4551 |
if (tb[UNIX_DIAG_ICONS]) { |
| 4552 |
int len = RTA_PAYLOAD(tb[UNIX_DIAG_ICONS]); |
| 4553 |
__u32 *peers = RTA_DATA(tb[UNIX_DIAG_ICONS]); |
| 4554 |
int i; |
| 4555 |
|
| 4556 |
out(" peers:"); |
| 4557 |
for (i = 0; i < len / sizeof(__u32); i++) |
| 4558 |
out(" %u", peers[i]); |
| 4559 |
} |
| 4560 |
} |
| 4561 |
|
| 4562 |
return 0; |
| 4563 |
} |
| 4564 |
|
| 4565 |
static int handle_netlink_request(struct filter *f, struct nlmsghdr *req, |
| 4566 |
size_t size, rtnl_filter_t show_one_sock) |
| 4567 |
{ |
| 4568 |
int ret = -1; |
| 4569 |
struct rtnl_handle rth; |
| 4570 |
|
| 4571 |
if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
| 4572 |
return -1; |
| 4573 |
|
| 4574 |
rth.dump = MAGIC_SEQ; |
| 4575 |
|
| 4576 |
if (rtnl_send(&rth, req, size) < 0) |
| 4577 |
goto Exit; |
| 4578 |
|
| 4579 |
if (rtnl_dump_filter(&rth, show_one_sock, f)) |
| 4580 |
goto Exit; |
| 4581 |
|
| 4582 |
ret = 0; |
| 4583 |
Exit: |
| 4584 |
rtnl_close(&rth); |
| 4585 |
return ret; |
| 4586 |
} |
| 4587 |
|
| 4588 |
static int unix_show_netlink(struct filter *f) |
| 4589 |
{ |
| 4590 |
DIAG_REQUEST(req, struct unix_diag_req r); |
| 4591 |
|
| 4592 |
req.r.sdiag_family = AF_UNIX; |
| 4593 |
req.r.udiag_states = f->states; |
| 4594 |
req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; |
| 4595 |
if (show_mem) |
| 4596 |
req.r.udiag_show |= UDIAG_SHOW_MEMINFO; |
| 4597 |
if (show_details) |
| 4598 |
req.r.udiag_show |= UDIAG_SHOW_VFS | UDIAG_SHOW_ICONS; |
| 4599 |
|
| 4600 |
return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock); |
| 4601 |
} |
| 4602 |
|
| 4603 |
static int unix_show(struct filter *f) |
| 4604 |
{ |
| 4605 |
FILE *fp; |
| 4606 |
char buf[256]; |
| 4607 |
char name[128]; |
| 4608 |
int newformat = 0; |
| 4609 |
int cnt; |
| 4610 |
struct sockstat *list = NULL; |
| 4611 |
const int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, |
| 4612 |
SS_ESTABLISHED, SS_CLOSING }; |
| 4613 |
|
| 4614 |
if (!filter_af_get(f, AF_UNIX)) |
| 4615 |
return 0; |
| 4616 |
|
| 4617 |
if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT") |
| 4618 |
&& unix_show_netlink(f) == 0) |
| 4619 |
return 0; |
| 4620 |
|
| 4621 |
if ((fp = net_unix_open()) == NULL) |
| 4622 |
return -1; |
| 4623 |
if (!fgets(buf, sizeof(buf), fp)) { |
| 4624 |
fclose(fp); |
| 4625 |
return -1; |
| 4626 |
} |
| 4627 |
|
| 4628 |
if (memcmp(buf, "Peer", 4) == 0) |
| 4629 |
newformat = 1; |
| 4630 |
cnt = 0; |
| 4631 |
|
| 4632 |
while (fgets(buf, sizeof(buf), fp)) { |
| 4633 |
struct sockstat *u, **insp; |
| 4634 |
int flags; |
| 4635 |
|
| 4636 |
if (!(u = calloc(1, sizeof(*u)))) |
| 4637 |
break; |
| 4638 |
|
| 4639 |
if (sscanf(buf, "%x: %x %x %x %x %x %d %s", |
| 4640 |
&u->rport, &u->rq, &u->wq, &flags, &u->type, |
| 4641 |
&u->state, &u->ino, name) < 8) |
| 4642 |
name[0] = 0; |
| 4643 |
|
| 4644 |
u->lport = u->ino; |
| 4645 |
u->local.family = u->remote.family = AF_UNIX; |
| 4646 |
|
| 4647 |
if (flags & (1 << 16)) { |
| 4648 |
u->state = SS_LISTEN; |
| 4649 |
} else if (u->state > 0 && |
| 4650 |
u->state <= ARRAY_SIZE(unix_state_map)) { |
| 4651 |
u->state = unix_state_map[u->state-1]; |
| 4652 |
if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) |
| 4653 |
u->state = SS_ESTABLISHED; |
| 4654 |
} |
| 4655 |
if (unix_type_skip(u, f) || |
| 4656 |
!(f->states & (1 << u->state))) { |
| 4657 |
free(u); |
| 4658 |
continue; |
| 4659 |
} |
| 4660 |
|
| 4661 |
if (!newformat) { |
| 4662 |
u->rport = 0; |
| 4663 |
u->rq = 0; |
| 4664 |
u->wq = 0; |
| 4665 |
} |
| 4666 |
|
| 4667 |
if (name[0]) { |
| 4668 |
u->name = strdup(name); |
| 4669 |
if (!u->name) { |
| 4670 |
free(u); |
| 4671 |
break; |
| 4672 |
} |
| 4673 |
} |
| 4674 |
|
| 4675 |
if (u->rport) { |
| 4676 |
struct sockstat *p; |
| 4677 |
|
| 4678 |
for (p = list; p; p = p->next) { |
| 4679 |
if (u->rport == p->lport) |
| 4680 |
break; |
| 4681 |
} |
| 4682 |
if (!p) |
| 4683 |
u->peer_name = "?"; |
| 4684 |
else |
| 4685 |
u->peer_name = p->name ? : "*"; |
| 4686 |
} |
| 4687 |
|
| 4688 |
if (f->f) { |
| 4689 |
struct sockstat st = { |
| 4690 |
.local.family = AF_UNIX, |
| 4691 |
.remote.family = AF_UNIX, |
| 4692 |
}; |
| 4693 |
|
| 4694 |
memcpy(st.local.data, &u->name, sizeof(u->name)); |
| 4695 |
/* when parsing the old format rport is set to 0 and |
| 4696 |
* therefore peer_name remains NULL |
| 4697 |
*/ |
| 4698 |
if (u->peer_name && strcmp(u->peer_name, "*")) |
| 4699 |
memcpy(st.remote.data, &u->peer_name, |
| 4700 |
sizeof(u->peer_name)); |
| 4701 |
if (run_ssfilter(f->f, &st) == 0) { |
| 4702 |
free(u->name); |
| 4703 |
free(u); |
| 4704 |
continue; |
| 4705 |
} |
| 4706 |
} |
| 4707 |
|
| 4708 |
insp = &list; |
| 4709 |
while (*insp) { |
| 4710 |
if (u->type < (*insp)->type || |
| 4711 |
(u->type == (*insp)->type && |
| 4712 |
u->ino < (*insp)->ino)) |
| 4713 |
break; |
| 4714 |
insp = &(*insp)->next; |
| 4715 |
} |
| 4716 |
u->next = *insp; |
| 4717 |
*insp = u; |
| 4718 |
|
| 4719 |
if (++cnt > MAX_UNIX_REMEMBER) { |
| 4720 |
while (list) { |
| 4721 |
unix_stats_print(list, f); |
| 4722 |
unix_list_drop_first(&list); |
| 4723 |
} |
| 4724 |
cnt = 0; |
| 4725 |
} |
| 4726 |
} |
| 4727 |
fclose(fp); |
| 4728 |
while (list) { |
| 4729 |
unix_stats_print(list, f); |
| 4730 |
unix_list_drop_first(&list); |
| 4731 |
} |
| 4732 |
|
| 4733 |
return 0; |
| 4734 |
} |
| 4735 |
|
| 4736 |
static int packet_stats_print(struct sockstat *s, const struct filter *f) |
| 4737 |
{ |
| 4738 |
const char *addr, *port; |
| 4739 |
char ll_name[16]; |
| 4740 |
|
| 4741 |
s->local.family = s->remote.family = AF_PACKET; |
| 4742 |
|
| 4743 |
if (f->f) { |
| 4744 |
s->local.data[0] = s->prot; |
| 4745 |
if (run_ssfilter(f->f, s) == 0) |
| 4746 |
return 1; |
| 4747 |
} |
| 4748 |
|
| 4749 |
sock_state_print(s); |
| 4750 |
|
| 4751 |
if (s->prot == 3) |
| 4752 |
addr = "*"; |
| 4753 |
else |
| 4754 |
addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); |
| 4755 |
|
| 4756 |
if (s->iface == 0) |
| 4757 |
port = "*"; |
| 4758 |
else |
| 4759 |
port = xll_index_to_name(s->iface); |
| 4760 |
|
| 4761 |
sock_addr_print(addr, ":", port, NULL); |
| 4762 |
sock_addr_print("", "*", "", NULL); |
| 4763 |
|
| 4764 |
proc_ctx_print(s); |
| 4765 |
|
| 4766 |
if (show_details) |
| 4767 |
sock_details_print(s); |
| 4768 |
|
| 4769 |
return 0; |
| 4770 |
} |
| 4771 |
|
| 4772 |
static void packet_show_ring(struct packet_diag_ring *ring) |
| 4773 |
{ |
| 4774 |
out("blk_size:%d", ring->pdr_block_size); |
| 4775 |
out(",blk_nr:%d", ring->pdr_block_nr); |
| 4776 |
out(",frm_size:%d", ring->pdr_frame_size); |
| 4777 |
out(",frm_nr:%d", ring->pdr_frame_nr); |
| 4778 |
out(",tmo:%d", ring->pdr_retire_tmo); |
| 4779 |
out(",features:0x%x", ring->pdr_features); |
| 4780 |
} |
| 4781 |
|
| 4782 |
static int packet_show_sock(struct nlmsghdr *nlh, void *arg) |
| 4783 |
{ |
| 4784 |
const struct filter *f = arg; |
| 4785 |
struct packet_diag_msg *r = NLMSG_DATA(nlh); |
| 4786 |
struct packet_diag_info *pinfo = NULL; |
| 4787 |
struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL; |
| 4788 |
struct rtattr *tb[PACKET_DIAG_MAX+1]; |
| 4789 |
struct sockstat stat = {}; |
| 4790 |
uint32_t fanout = 0; |
| 4791 |
bool has_fanout = false; |
| 4792 |
|
| 4793 |
parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1), |
| 4794 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 4795 |
|
| 4796 |
/* use /proc/net/packet if all info are not available */ |
| 4797 |
if (!tb[PACKET_DIAG_MEMINFO]) |
| 4798 |
return -1; |
| 4799 |
|
| 4800 |
stat.type = r->pdiag_type; |
| 4801 |
stat.prot = r->pdiag_num; |
| 4802 |
stat.ino = r->pdiag_ino; |
| 4803 |
stat.state = SS_CLOSE; |
| 4804 |
stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); |
| 4805 |
|
| 4806 |
if (tb[PACKET_DIAG_MEMINFO]) { |
| 4807 |
__u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); |
| 4808 |
|
| 4809 |
stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
| 4810 |
} |
| 4811 |
|
| 4812 |
if (tb[PACKET_DIAG_INFO]) { |
| 4813 |
pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); |
| 4814 |
stat.lport = stat.iface = pinfo->pdi_index; |
| 4815 |
} |
| 4816 |
|
| 4817 |
if (tb[PACKET_DIAG_UID]) |
| 4818 |
stat.uid = rta_getattr_u32(tb[PACKET_DIAG_UID]); |
| 4819 |
|
| 4820 |
if (tb[PACKET_DIAG_RX_RING]) |
| 4821 |
ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]); |
| 4822 |
|
| 4823 |
if (tb[PACKET_DIAG_TX_RING]) |
| 4824 |
ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]); |
| 4825 |
|
| 4826 |
if (tb[PACKET_DIAG_FANOUT]) { |
| 4827 |
has_fanout = true; |
| 4828 |
fanout = rta_getattr_u32(tb[PACKET_DIAG_FANOUT]); |
| 4829 |
} |
| 4830 |
|
| 4831 |
if (packet_stats_print(&stat, f)) |
| 4832 |
return 0; |
| 4833 |
|
| 4834 |
if (show_details) { |
| 4835 |
if (pinfo) { |
| 4836 |
if (oneline) |
| 4837 |
out(" ver:%d", pinfo->pdi_version); |
| 4838 |
else |
| 4839 |
out("\n\tver:%d", pinfo->pdi_version); |
| 4840 |
out(" cpy_thresh:%d", pinfo->pdi_copy_thresh); |
| 4841 |
out(" flags( "); |
| 4842 |
if (pinfo->pdi_flags & PDI_RUNNING) |
| 4843 |
out("running"); |
| 4844 |
if (pinfo->pdi_flags & PDI_AUXDATA) |
| 4845 |
out(" auxdata"); |
| 4846 |
if (pinfo->pdi_flags & PDI_ORIGDEV) |
| 4847 |
out(" origdev"); |
| 4848 |
if (pinfo->pdi_flags & PDI_VNETHDR) |
| 4849 |
out(" vnethdr"); |
| 4850 |
if (pinfo->pdi_flags & PDI_LOSS) |
| 4851 |
out(" loss"); |
| 4852 |
if (!pinfo->pdi_flags) |
| 4853 |
out("0"); |
| 4854 |
out(" )"); |
| 4855 |
} |
| 4856 |
if (ring_rx) { |
| 4857 |
if (oneline) |
| 4858 |
out(" ring_rx("); |
| 4859 |
else |
| 4860 |
out("\n\tring_rx("); |
| 4861 |
packet_show_ring(ring_rx); |
| 4862 |
out(")"); |
| 4863 |
} |
| 4864 |
if (ring_tx) { |
| 4865 |
if (oneline) |
| 4866 |
out(" ring_tx("); |
| 4867 |
else |
| 4868 |
out("\n\tring_tx("); |
| 4869 |
packet_show_ring(ring_tx); |
| 4870 |
out(")"); |
| 4871 |
} |
| 4872 |
if (has_fanout) { |
| 4873 |
uint16_t type = (fanout >> 16) & 0xffff; |
| 4874 |
|
| 4875 |
if (oneline) |
| 4876 |
out(" fanout("); |
| 4877 |
else |
| 4878 |
out("\n\tfanout("); |
| 4879 |
out("id:%d,", fanout & 0xffff); |
| 4880 |
out("type:"); |
| 4881 |
|
| 4882 |
if (type == 0) |
| 4883 |
out("hash"); |
| 4884 |
else if (type == 1) |
| 4885 |
out("lb"); |
| 4886 |
else if (type == 2) |
| 4887 |
out("cpu"); |
| 4888 |
else if (type == 3) |
| 4889 |
out("roll"); |
| 4890 |
else if (type == 4) |
| 4891 |
out("random"); |
| 4892 |
else if (type == 5) |
| 4893 |
out("qm"); |
| 4894 |
else |
| 4895 |
out("0x%x", type); |
| 4896 |
|
| 4897 |
out(")"); |
| 4898 |
} |
| 4899 |
} |
| 4900 |
|
| 4901 |
if (show_bpf && tb[PACKET_DIAG_FILTER]) { |
| 4902 |
struct sock_filter *fil = |
| 4903 |
RTA_DATA(tb[PACKET_DIAG_FILTER]); |
| 4904 |
int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / |
| 4905 |
sizeof(struct sock_filter); |
| 4906 |
|
| 4907 |
if (oneline) |
| 4908 |
out(" bpf filter (%d): ", num); |
| 4909 |
else |
| 4910 |
out("\n\tbpf filter (%d): ", num); |
| 4911 |
while (num) { |
| 4912 |
out(" 0x%02x %u %u %u,", |
| 4913 |
fil->code, fil->jt, fil->jf, fil->k); |
| 4914 |
num--; |
| 4915 |
fil++; |
| 4916 |
} |
| 4917 |
} |
| 4918 |
|
| 4919 |
if (show_mem) |
| 4920 |
print_skmeminfo(tb, PACKET_DIAG_MEMINFO); |
| 4921 |
return 0; |
| 4922 |
} |
| 4923 |
|
| 4924 |
static int packet_show_netlink(struct filter *f) |
| 4925 |
{ |
| 4926 |
DIAG_REQUEST(req, struct packet_diag_req r); |
| 4927 |
|
| 4928 |
req.r.sdiag_family = AF_PACKET; |
| 4929 |
req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | |
| 4930 |
PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT; |
| 4931 |
|
| 4932 |
return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock); |
| 4933 |
} |
| 4934 |
|
| 4935 |
static int packet_show_line(char *buf, const struct filter *f, int fam) |
| 4936 |
{ |
| 4937 |
unsigned long long sk; |
| 4938 |
struct sockstat stat = {}; |
| 4939 |
int type, prot, iface, state, rq, uid, ino; |
| 4940 |
|
| 4941 |
sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", |
| 4942 |
&sk, |
| 4943 |
&type, &prot, &iface, &state, |
| 4944 |
&rq, &uid, &ino); |
| 4945 |
|
| 4946 |
if (type == SOCK_RAW && !(f->dbs & (1<<PACKET_R_DB))) |
| 4947 |
return 0; |
| 4948 |
if (type == SOCK_DGRAM && !(f->dbs & (1<<PACKET_DG_DB))) |
| 4949 |
return 0; |
| 4950 |
|
| 4951 |
stat.type = type; |
| 4952 |
stat.prot = prot; |
| 4953 |
stat.lport = stat.iface = iface; |
| 4954 |
stat.state = state; |
| 4955 |
stat.rq = rq; |
| 4956 |
stat.uid = uid; |
| 4957 |
stat.ino = ino; |
| 4958 |
stat.state = SS_CLOSE; |
| 4959 |
|
| 4960 |
if (packet_stats_print(&stat, f)) |
| 4961 |
return 0; |
| 4962 |
|
| 4963 |
return 0; |
| 4964 |
} |
| 4965 |
|
| 4966 |
static int packet_show(struct filter *f) |
| 4967 |
{ |
| 4968 |
FILE *fp; |
| 4969 |
int rc = 0; |
| 4970 |
|
| 4971 |
if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE))) |
| 4972 |
return 0; |
| 4973 |
|
| 4974 |
if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") && |
| 4975 |
packet_show_netlink(f) == 0) |
| 4976 |
return 0; |
| 4977 |
|
| 4978 |
if ((fp = net_packet_open()) == NULL) |
| 4979 |
return -1; |
| 4980 |
if (generic_record_read(fp, packet_show_line, f, AF_PACKET)) |
| 4981 |
rc = -1; |
| 4982 |
|
| 4983 |
fclose(fp); |
| 4984 |
return rc; |
| 4985 |
} |
| 4986 |
|
| 4987 |
static int xdp_stats_print(struct sockstat *s, const struct filter *f) |
| 4988 |
{ |
| 4989 |
const char *addr, *port; |
| 4990 |
char q_str[16]; |
| 4991 |
|
| 4992 |
s->local.family = s->remote.family = AF_XDP; |
| 4993 |
|
| 4994 |
if (f->f) { |
| 4995 |
if (run_ssfilter(f->f, s) == 0) |
| 4996 |
return 1; |
| 4997 |
} |
| 4998 |
|
| 4999 |
sock_state_print(s); |
| 5000 |
|
| 5001 |
if (s->iface) { |
| 5002 |
addr = xll_index_to_name(s->iface); |
| 5003 |
snprintf(q_str, sizeof(q_str), "q%d", s->lport); |
| 5004 |
port = q_str; |
| 5005 |
sock_addr_print(addr, ":", port, NULL); |
| 5006 |
} else { |
| 5007 |
sock_addr_print("", "*", "", NULL); |
| 5008 |
} |
| 5009 |
|
| 5010 |
sock_addr_print("", "*", "", NULL); |
| 5011 |
|
| 5012 |
proc_ctx_print(s); |
| 5013 |
|
| 5014 |
if (show_details) |
| 5015 |
sock_details_print(s); |
| 5016 |
|
| 5017 |
return 0; |
| 5018 |
} |
| 5019 |
|
| 5020 |
static void xdp_show_ring(const char *name, struct xdp_diag_ring *ring) |
| 5021 |
{ |
| 5022 |
if (oneline) |
| 5023 |
out(" %s(", name); |
| 5024 |
else |
| 5025 |
out("\n\t%s(", name); |
| 5026 |
out("entries:%u", ring->entries); |
| 5027 |
out(")"); |
| 5028 |
} |
| 5029 |
|
| 5030 |
static void xdp_show_umem(struct xdp_diag_umem *umem, struct xdp_diag_ring *fr, |
| 5031 |
struct xdp_diag_ring *cr) |
| 5032 |
{ |
| 5033 |
if (oneline) |
| 5034 |
out(" tumem("); |
| 5035 |
else |
| 5036 |
out("\n\tumem("); |
| 5037 |
out("id:%u", umem->id); |
| 5038 |
out(",size:%llu", umem->size); |
| 5039 |
out(",num_pages:%u", umem->num_pages); |
| 5040 |
out(",chunk_size:%u", umem->chunk_size); |
| 5041 |
out(",headroom:%u", umem->headroom); |
| 5042 |
out(",ifindex:%u", umem->ifindex); |
| 5043 |
out(",qid:%u", umem->queue_id); |
| 5044 |
out(",zc:%u", umem->flags & XDP_DU_F_ZEROCOPY); |
| 5045 |
out(",refs:%u", umem->refs); |
| 5046 |
out(")"); |
| 5047 |
|
| 5048 |
if (fr) |
| 5049 |
xdp_show_ring("fr", fr); |
| 5050 |
if (cr) |
| 5051 |
xdp_show_ring("cr", cr); |
| 5052 |
} |
| 5053 |
|
| 5054 |
static void xdp_show_stats(struct xdp_diag_stats *stats) |
| 5055 |
{ |
| 5056 |
if (oneline) |
| 5057 |
out(" stats("); |
| 5058 |
else |
| 5059 |
out("\n\tstats("); |
| 5060 |
out("rx dropped:%llu", stats->n_rx_dropped); |
| 5061 |
out(",rx invalid:%llu", stats->n_rx_invalid); |
| 5062 |
out(",rx queue full:%llu", stats->n_rx_full); |
| 5063 |
out(",rx fill ring empty:%llu", stats->n_fill_ring_empty); |
| 5064 |
out(",tx invalid:%llu", stats->n_tx_invalid); |
| 5065 |
out(",tx ring empty:%llu", stats->n_tx_ring_empty); |
| 5066 |
out(")"); |
| 5067 |
} |
| 5068 |
|
| 5069 |
static int xdp_show_sock(struct nlmsghdr *nlh, void *arg) |
| 5070 |
{ |
| 5071 |
struct xdp_diag_ring *rx = NULL, *tx = NULL, *fr = NULL, *cr = NULL; |
| 5072 |
struct xdp_diag_msg *msg = NLMSG_DATA(nlh); |
| 5073 |
struct rtattr *tb[XDP_DIAG_MAX + 1]; |
| 5074 |
struct xdp_diag_info *info = NULL; |
| 5075 |
struct xdp_diag_umem *umem = NULL; |
| 5076 |
struct xdp_diag_stats *stats = NULL; |
| 5077 |
const struct filter *f = arg; |
| 5078 |
struct sockstat stat = {}; |
| 5079 |
|
| 5080 |
parse_rtattr(tb, XDP_DIAG_MAX, (struct rtattr *)(msg + 1), |
| 5081 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*msg))); |
| 5082 |
|
| 5083 |
stat.type = msg->xdiag_type; |
| 5084 |
stat.ino = msg->xdiag_ino; |
| 5085 |
stat.state = SS_CLOSE; |
| 5086 |
stat.sk = cookie_sk_get(&msg->xdiag_cookie[0]); |
| 5087 |
|
| 5088 |
if (tb[XDP_DIAG_INFO]) { |
| 5089 |
info = RTA_DATA(tb[XDP_DIAG_INFO]); |
| 5090 |
stat.iface = info->ifindex; |
| 5091 |
stat.lport = info->queue_id; |
| 5092 |
} |
| 5093 |
|
| 5094 |
if (tb[XDP_DIAG_UID]) |
| 5095 |
stat.uid = rta_getattr_u32(tb[XDP_DIAG_UID]); |
| 5096 |
if (tb[XDP_DIAG_RX_RING]) |
| 5097 |
rx = RTA_DATA(tb[XDP_DIAG_RX_RING]); |
| 5098 |
if (tb[XDP_DIAG_TX_RING]) |
| 5099 |
tx = RTA_DATA(tb[XDP_DIAG_TX_RING]); |
| 5100 |
if (tb[XDP_DIAG_UMEM]) |
| 5101 |
umem = RTA_DATA(tb[XDP_DIAG_UMEM]); |
| 5102 |
if (tb[XDP_DIAG_UMEM_FILL_RING]) |
| 5103 |
fr = RTA_DATA(tb[XDP_DIAG_UMEM_FILL_RING]); |
| 5104 |
if (tb[XDP_DIAG_UMEM_COMPLETION_RING]) |
| 5105 |
cr = RTA_DATA(tb[XDP_DIAG_UMEM_COMPLETION_RING]); |
| 5106 |
if (tb[XDP_DIAG_MEMINFO]) { |
| 5107 |
__u32 *skmeminfo = RTA_DATA(tb[XDP_DIAG_MEMINFO]); |
| 5108 |
|
| 5109 |
stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
| 5110 |
} |
| 5111 |
if (tb[XDP_DIAG_STATS]) |
| 5112 |
stats = RTA_DATA(tb[XDP_DIAG_STATS]); |
| 5113 |
|
| 5114 |
if (xdp_stats_print(&stat, f)) |
| 5115 |
return 0; |
| 5116 |
|
| 5117 |
if (show_details) { |
| 5118 |
if (rx) |
| 5119 |
xdp_show_ring("rx", rx); |
| 5120 |
if (tx) |
| 5121 |
xdp_show_ring("tx", tx); |
| 5122 |
if (umem) |
| 5123 |
xdp_show_umem(umem, fr, cr); |
| 5124 |
if (stats) |
| 5125 |
xdp_show_stats(stats); |
| 5126 |
} |
| 5127 |
|
| 5128 |
if (show_mem) |
| 5129 |
print_skmeminfo(tb, XDP_DIAG_MEMINFO); // really? |
| 5130 |
|
| 5131 |
|
| 5132 |
return 0; |
| 5133 |
} |
| 5134 |
|
| 5135 |
static int xdp_show(struct filter *f) |
| 5136 |
{ |
| 5137 |
DIAG_REQUEST(req, struct xdp_diag_req r); |
| 5138 |
|
| 5139 |
if (!filter_af_get(f, AF_XDP) || !(f->states & (1 << SS_CLOSE))) |
| 5140 |
return 0; |
| 5141 |
|
| 5142 |
req.r.sdiag_family = AF_XDP; |
| 5143 |
req.r.xdiag_show = XDP_SHOW_INFO | XDP_SHOW_RING_CFG | XDP_SHOW_UMEM | |
| 5144 |
XDP_SHOW_MEMINFO | XDP_SHOW_STATS; |
| 5145 |
|
| 5146 |
return handle_netlink_request(f, &req.nlh, sizeof(req), xdp_show_sock); |
| 5147 |
} |
| 5148 |
|
| 5149 |
static int netlink_show_one(struct filter *f, |
| 5150 |
int prot, int pid, unsigned int groups, |
| 5151 |
int state, int dst_pid, unsigned int dst_group, |
| 5152 |
int rq, int wq, |
| 5153 |
unsigned long long sk, unsigned long long cb) |
| 5154 |
{ |
| 5155 |
struct sockstat st = { |
| 5156 |
.state = SS_CLOSE, |
| 5157 |
.rq = rq, |
| 5158 |
.wq = wq, |
| 5159 |
.local.family = AF_NETLINK, |
| 5160 |
.remote.family = AF_NETLINK, |
| 5161 |
}; |
| 5162 |
|
| 5163 |
SPRINT_BUF(prot_buf) = {}; |
| 5164 |
const char *prot_name; |
| 5165 |
char procname[64] = {}; |
| 5166 |
|
| 5167 |
if (f->f) { |
| 5168 |
st.rport = -1; |
| 5169 |
st.lport = pid; |
| 5170 |
st.local.data[0] = prot; |
| 5171 |
if (run_ssfilter(f->f, &st) == 0) |
| 5172 |
return 1; |
| 5173 |
} |
| 5174 |
|
| 5175 |
sock_state_print(&st); |
| 5176 |
|
| 5177 |
prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); |
| 5178 |
|
| 5179 |
if (pid == -1) { |
| 5180 |
procname[0] = '*'; |
| 5181 |
} else if (!numeric) { |
| 5182 |
int done = 0; |
| 5183 |
|
| 5184 |
if (!pid) { |
| 5185 |
done = 1; |
| 5186 |
strncpy(procname, "kernel", 7); |
| 5187 |
} else if (pid > 0) { |
| 5188 |
FILE *fp; |
| 5189 |
|
| 5190 |
snprintf(procname, sizeof(procname), "%s/%d/stat", |
| 5191 |
getenv("PROC_ROOT") ? : "/proc", pid); |
| 5192 |
if ((fp = fopen(procname, "r")) != NULL) { |
| 5193 |
if (fscanf(fp, "%*d (%[^)])", procname) == 1) { |
| 5194 |
snprintf(procname+strlen(procname), |
| 5195 |
sizeof(procname)-strlen(procname), |
| 5196 |
"/%d", pid); |
| 5197 |
done = 1; |
| 5198 |
} |
| 5199 |
fclose(fp); |
| 5200 |
} |
| 5201 |
} |
| 5202 |
if (!done) |
| 5203 |
int_to_str(pid, procname); |
| 5204 |
} else { |
| 5205 |
int_to_str(pid, procname); |
| 5206 |
} |
| 5207 |
|
| 5208 |
sock_addr_print(prot_name, ":", procname, NULL); |
| 5209 |
|
| 5210 |
if (state == NETLINK_CONNECTED) { |
| 5211 |
char dst_group_buf[30]; |
| 5212 |
char dst_pid_buf[30]; |
| 5213 |
|
| 5214 |
sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", |
| 5215 |
int_to_str(dst_pid, dst_pid_buf), NULL); |
| 5216 |
} else { |
| 5217 |
sock_addr_print("", "*", "", NULL); |
| 5218 |
} |
| 5219 |
|
| 5220 |
char *pid_context = NULL; |
| 5221 |
|
| 5222 |
if (show_proc_ctx) { |
| 5223 |
/* The pid value will either be: |
| 5224 |
* 0 if destination kernel - show kernel initial context. |
| 5225 |
* A valid process pid - use getpidcon. |
| 5226 |
* A unique value allocated by the kernel or netlink user |
| 5227 |
* to the process - show context as "not available". |
| 5228 |
*/ |
| 5229 |
if (!pid) |
| 5230 |
security_get_initial_context("kernel", &pid_context); |
| 5231 |
else if (pid > 0) |
| 5232 |
getpidcon(pid, &pid_context); |
| 5233 |
|
| 5234 |
out(" proc_ctx=%s", pid_context ? : "unavailable"); |
| 5235 |
freecon(pid_context); |
| 5236 |
} |
| 5237 |
|
| 5238 |
if (show_details) { |
| 5239 |
out(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); |
| 5240 |
} |
| 5241 |
|
| 5242 |
return 0; |
| 5243 |
} |
| 5244 |
|
| 5245 |
static int netlink_show_sock(struct nlmsghdr *nlh, void *arg) |
| 5246 |
{ |
| 5247 |
struct filter *f = (struct filter *)arg; |
| 5248 |
struct netlink_diag_msg *r = NLMSG_DATA(nlh); |
| 5249 |
struct rtattr *tb[NETLINK_DIAG_MAX+1]; |
| 5250 |
int rq = 0, wq = 0; |
| 5251 |
unsigned long groups = 0; |
| 5252 |
|
| 5253 |
parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1), |
| 5254 |
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 5255 |
|
| 5256 |
if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS])) |
| 5257 |
groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]); |
| 5258 |
|
| 5259 |
if (tb[NETLINK_DIAG_MEMINFO]) { |
| 5260 |
const __u32 *skmeminfo; |
| 5261 |
|
| 5262 |
skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]); |
| 5263 |
|
| 5264 |
rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
| 5265 |
wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC]; |
| 5266 |
} |
| 5267 |
|
| 5268 |
if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups, |
| 5269 |
r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group, |
| 5270 |
rq, wq, 0, 0)) { |
| 5271 |
return 0; |
| 5272 |
} |
| 5273 |
|
| 5274 |
if (show_mem) { |
| 5275 |
out("\t"); |
| 5276 |
print_skmeminfo(tb, NETLINK_DIAG_MEMINFO); |
| 5277 |
} |
| 5278 |
|
| 5279 |
return 0; |
| 5280 |
} |
| 5281 |
|
| 5282 |
static int netlink_show_netlink(struct filter *f) |
| 5283 |
{ |
| 5284 |
DIAG_REQUEST(req, struct netlink_diag_req r); |
| 5285 |
|
| 5286 |
req.r.sdiag_family = AF_NETLINK; |
| 5287 |
req.r.sdiag_protocol = NDIAG_PROTO_ALL; |
| 5288 |
req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO; |
| 5289 |
|
| 5290 |
return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock); |
| 5291 |
} |
| 5292 |
|
| 5293 |
static int netlink_show(struct filter *f) |
| 5294 |
{ |
| 5295 |
FILE *fp; |
| 5296 |
char buf[256]; |
| 5297 |
int prot, pid; |
| 5298 |
unsigned int groups; |
| 5299 |
int rq, wq, rc; |
| 5300 |
unsigned long long sk, cb; |
| 5301 |
|
| 5302 |
if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE))) |
| 5303 |
return 0; |
| 5304 |
|
| 5305 |
if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") && |
| 5306 |
netlink_show_netlink(f) == 0) |
| 5307 |
return 0; |
| 5308 |
|
| 5309 |
if ((fp = net_netlink_open()) == NULL) |
| 5310 |
return -1; |
| 5311 |
if (!fgets(buf, sizeof(buf), fp)) { |
| 5312 |
fclose(fp); |
| 5313 |
return -1; |
| 5314 |
} |
| 5315 |
|
| 5316 |
while (fgets(buf, sizeof(buf), fp)) { |
| 5317 |
sscanf(buf, "%llx %d %d %x %d %d %llx %d", |
| 5318 |
&sk, |
| 5319 |
&prot, &pid, &groups, &rq, &wq, &cb, &rc); |
| 5320 |
|
| 5321 |
netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb); |
| 5322 |
} |
| 5323 |
|
| 5324 |
fclose(fp); |
| 5325 |
return 0; |
| 5326 |
} |
| 5327 |
|
| 5328 |
static bool vsock_type_skip(struct sockstat *s, struct filter *f) |
| 5329 |
{ |
| 5330 |
if (s->type == SOCK_STREAM && !(f->dbs & (1 << VSOCK_ST_DB))) |
| 5331 |
return true; |
| 5332 |
if (s->type == SOCK_DGRAM && !(f->dbs & (1 << VSOCK_DG_DB))) |
| 5333 |
return true; |
| 5334 |
return false; |
| 5335 |
} |
| 5336 |
|
| 5337 |
static void vsock_addr_print(inet_prefix *a, __u32 port) |
| 5338 |
{ |
| 5339 |
char cid_str[sizeof("4294967295")]; |
| 5340 |
char port_str[sizeof("4294967295")]; |
| 5341 |
__u32 cid; |
| 5342 |
|
| 5343 |
memcpy(&cid, a->data, sizeof(cid)); |
| 5344 |
|
| 5345 |
if (cid == ~(__u32)0) |
| 5346 |
snprintf(cid_str, sizeof(cid_str), "*"); |
| 5347 |
else |
| 5348 |
snprintf(cid_str, sizeof(cid_str), "%u", cid); |
| 5349 |
|
| 5350 |
if (port == ~(__u32)0) |
| 5351 |
snprintf(port_str, sizeof(port_str), "*"); |
| 5352 |
else |
| 5353 |
snprintf(port_str, sizeof(port_str), "%u", port); |
| 5354 |
|
| 5355 |
sock_addr_print(cid_str, ":", port_str, NULL); |
| 5356 |
} |
| 5357 |
|
| 5358 |
static void vsock_stats_print(struct sockstat *s, struct filter *f) |
| 5359 |
{ |
| 5360 |
sock_state_print(s); |
| 5361 |
|
| 5362 |
vsock_addr_print(&s->local, s->lport); |
| 5363 |
vsock_addr_print(&s->remote, s->rport); |
| 5364 |
|
| 5365 |
proc_ctx_print(s); |
| 5366 |
} |
| 5367 |
|
| 5368 |
static int vsock_show_sock(struct nlmsghdr *nlh, void *arg) |
| 5369 |
{ |
| 5370 |
struct filter *f = (struct filter *)arg; |
| 5371 |
struct vsock_diag_msg *r = NLMSG_DATA(nlh); |
| 5372 |
struct sockstat stat = { |
| 5373 |
.type = r->vdiag_type, |
| 5374 |
.lport = r->vdiag_src_port, |
| 5375 |
.rport = r->vdiag_dst_port, |
| 5376 |
.state = r->vdiag_state, |
| 5377 |
.ino = r->vdiag_ino, |
| 5378 |
}; |
| 5379 |
|
| 5380 |
vsock_set_inet_prefix(&stat.local, r->vdiag_src_cid); |
| 5381 |
vsock_set_inet_prefix(&stat.remote, r->vdiag_dst_cid); |
| 5382 |
|
| 5383 |
if (vsock_type_skip(&stat, f)) |
| 5384 |
return 0; |
| 5385 |
|
| 5386 |
if (f->f && run_ssfilter(f->f, &stat) == 0) |
| 5387 |
return 0; |
| 5388 |
|
| 5389 |
vsock_stats_print(&stat, f); |
| 5390 |
|
| 5391 |
return 0; |
| 5392 |
} |
| 5393 |
|
| 5394 |
static int vsock_show(struct filter *f) |
| 5395 |
{ |
| 5396 |
DIAG_REQUEST(req, struct vsock_diag_req r); |
| 5397 |
|
| 5398 |
if (!filter_af_get(f, AF_VSOCK)) |
| 5399 |
return 0; |
| 5400 |
|
| 5401 |
req.r.sdiag_family = AF_VSOCK; |
| 5402 |
req.r.vdiag_states = f->states; |
| 5403 |
|
| 5404 |
return handle_netlink_request(f, &req.nlh, sizeof(req), vsock_show_sock); |
| 5405 |
} |
| 5406 |
|
| 5407 |
static void tipc_sock_addr_print(struct rtattr *net_addr, struct rtattr *id) |
| 5408 |
{ |
| 5409 |
uint32_t node = rta_getattr_u32(net_addr); |
| 5410 |
uint32_t identity = rta_getattr_u32(id); |
| 5411 |
|
| 5412 |
SPRINT_BUF(addr) = {}; |
| 5413 |
SPRINT_BUF(port) = {}; |
| 5414 |
|
| 5415 |
sprintf(addr, "%u", node); |
| 5416 |
sprintf(port, "%u", identity); |
| 5417 |
sock_addr_print(addr, ":", port, NULL); |
| 5418 |
|
| 5419 |
} |
| 5420 |
|
| 5421 |
static int tipc_show_sock(struct nlmsghdr *nlh, void *arg) |
| 5422 |
{ |
| 5423 |
struct rtattr *stat[TIPC_NLA_SOCK_STAT_MAX + 1] = {}; |
| 5424 |
struct rtattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {}; |
| 5425 |
struct rtattr *con[TIPC_NLA_CON_MAX + 1] = {}; |
| 5426 |
struct rtattr *info[TIPC_NLA_MAX + 1] = {}; |
| 5427 |
struct rtattr *msg_ref; |
| 5428 |
struct sockstat ss = {}; |
| 5429 |
|
| 5430 |
parse_rtattr(info, TIPC_NLA_MAX, NLMSG_DATA(nlh), |
| 5431 |
NLMSG_PAYLOAD(nlh, 0)); |
| 5432 |
|
| 5433 |
if (!info[TIPC_NLA_SOCK]) |
| 5434 |
return 0; |
| 5435 |
|
| 5436 |
msg_ref = info[TIPC_NLA_SOCK]; |
| 5437 |
parse_rtattr(attrs, TIPC_NLA_SOCK_MAX, RTA_DATA(msg_ref), |
| 5438 |
RTA_PAYLOAD(msg_ref)); |
| 5439 |
|
| 5440 |
msg_ref = attrs[TIPC_NLA_SOCK_STAT]; |
| 5441 |
parse_rtattr(stat, TIPC_NLA_SOCK_STAT_MAX, |
| 5442 |
RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); |
| 5443 |
|
| 5444 |
|
| 5445 |
ss.local.family = AF_TIPC; |
| 5446 |
ss.type = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TYPE]); |
| 5447 |
ss.state = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TIPC_STATE]); |
| 5448 |
ss.uid = rta_getattr_u32(attrs[TIPC_NLA_SOCK_UID]); |
| 5449 |
ss.ino = rta_getattr_u32(attrs[TIPC_NLA_SOCK_INO]); |
| 5450 |
ss.rq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_RCVQ]); |
| 5451 |
ss.wq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_SENDQ]); |
| 5452 |
ss.sk = rta_getattr_u64(attrs[TIPC_NLA_SOCK_COOKIE]); |
| 5453 |
|
| 5454 |
sock_state_print (&ss); |
| 5455 |
|
| 5456 |
tipc_sock_addr_print(attrs[TIPC_NLA_SOCK_ADDR], |
| 5457 |
attrs[TIPC_NLA_SOCK_REF]); |
| 5458 |
|
| 5459 |
msg_ref = attrs[TIPC_NLA_SOCK_CON]; |
| 5460 |
if (msg_ref) { |
| 5461 |
parse_rtattr(con, TIPC_NLA_CON_MAX, |
| 5462 |
RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); |
| 5463 |
|
| 5464 |
tipc_sock_addr_print(con[TIPC_NLA_CON_NODE], |
| 5465 |
con[TIPC_NLA_CON_SOCK]); |
| 5466 |
} else |
| 5467 |
sock_addr_print("", "-", "", NULL); |
| 5468 |
|
| 5469 |
if (show_details) |
| 5470 |
sock_details_print(&ss); |
| 5471 |
|
| 5472 |
proc_ctx_print(&ss); |
| 5473 |
|
| 5474 |
if (show_tipcinfo) { |
| 5475 |
if (oneline) |
| 5476 |
out(" type:%s", stype_nameg[ss.type]); |
| 5477 |
else |
| 5478 |
out("\n type:%s", stype_nameg[ss.type]); |
| 5479 |
out(" cong:%s ", |
| 5480 |
stat[TIPC_NLA_SOCK_STAT_LINK_CONG] ? "link" : |
| 5481 |
stat[TIPC_NLA_SOCK_STAT_CONN_CONG] ? "conn" : "none"); |
| 5482 |
out(" drop:%d ", |
| 5483 |
rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_DROP])); |
| 5484 |
|
| 5485 |
if (attrs[TIPC_NLA_SOCK_HAS_PUBL]) |
| 5486 |
out(" publ"); |
| 5487 |
|
| 5488 |
if (con[TIPC_NLA_CON_FLAG]) |
| 5489 |
out(" via {%u,%u} ", |
| 5490 |
rta_getattr_u32(con[TIPC_NLA_CON_TYPE]), |
| 5491 |
rta_getattr_u32(con[TIPC_NLA_CON_INST])); |
| 5492 |
} |
| 5493 |
|
| 5494 |
return 0; |
| 5495 |
} |
| 5496 |
|
| 5497 |
static int tipc_show(struct filter *f) |
| 5498 |
{ |
| 5499 |
DIAG_REQUEST(req, struct tipc_sock_diag_req r); |
| 5500 |
|
| 5501 |
memset(&req.r, 0, sizeof(req.r)); |
| 5502 |
req.r.sdiag_family = AF_TIPC; |
| 5503 |
req.r.tidiag_states = f->states; |
| 5504 |
|
| 5505 |
return handle_netlink_request(f, &req.nlh, sizeof(req), tipc_show_sock); |
| 5506 |
} |
| 5507 |
|
| 5508 |
struct sock_diag_msg { |
| 5509 |
__u8 sdiag_family; |
| 5510 |
}; |
| 5511 |
|
| 5512 |
static int generic_show_sock(struct nlmsghdr *nlh, void *arg) |
| 5513 |
{ |
| 5514 |
struct sock_diag_msg *r = NLMSG_DATA(nlh); |
| 5515 |
struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX }; |
| 5516 |
int ret; |
| 5517 |
|
| 5518 |
switch (r->sdiag_family) { |
| 5519 |
case AF_INET: |
| 5520 |
case AF_INET6: |
| 5521 |
inet_arg.rth = inet_arg.f->rth_for_killing; |
| 5522 |
ret = show_one_inet_sock(nlh, &inet_arg); |
| 5523 |
break; |
| 5524 |
case AF_UNIX: |
| 5525 |
ret = unix_show_sock(nlh, arg); |
| 5526 |
break; |
| 5527 |
case AF_PACKET: |
| 5528 |
ret = packet_show_sock(nlh, arg); |
| 5529 |
break; |
| 5530 |
case AF_NETLINK: |
| 5531 |
ret = netlink_show_sock(nlh, arg); |
| 5532 |
break; |
| 5533 |
case AF_VSOCK: |
| 5534 |
ret = vsock_show_sock(nlh, arg); |
| 5535 |
break; |
| 5536 |
case AF_XDP: |
| 5537 |
ret = xdp_show_sock(nlh, arg); |
| 5538 |
break; |
| 5539 |
default: |
| 5540 |
ret = -1; |
| 5541 |
} |
| 5542 |
|
| 5543 |
render(); |
| 5544 |
|
| 5545 |
return ret; |
| 5546 |
} |
| 5547 |
|
| 5548 |
static int handle_follow_request(struct filter *f) |
| 5549 |
{ |
| 5550 |
int ret = 0; |
| 5551 |
int groups = 0; |
| 5552 |
struct rtnl_handle rth, rth2; |
| 5553 |
|
| 5554 |
if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << TCP_DB)) |
| 5555 |
groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1); |
| 5556 |
if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << UDP_DB)) |
| 5557 |
groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1); |
| 5558 |
if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << TCP_DB)) |
| 5559 |
groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1); |
| 5560 |
if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << UDP_DB)) |
| 5561 |
groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1); |
| 5562 |
|
| 5563 |
if (groups == 0) |
| 5564 |
return -1; |
| 5565 |
|
| 5566 |
if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG)) |
| 5567 |
return -1; |
| 5568 |
|
| 5569 |
rth.dump = 0; |
| 5570 |
rth.local.nl_pid = 0; |
| 5571 |
|
| 5572 |
if (f->kill) { |
| 5573 |
if (rtnl_open_byproto(&rth2, groups, NETLINK_SOCK_DIAG)) { |
| 5574 |
rtnl_close(&rth); |
| 5575 |
return -1; |
| 5576 |
} |
| 5577 |
f->rth_for_killing = &rth2; |
| 5578 |
} |
| 5579 |
|
| 5580 |
if (rtnl_dump_filter(&rth, generic_show_sock, f)) |
| 5581 |
ret = -1; |
| 5582 |
|
| 5583 |
rtnl_close(&rth); |
| 5584 |
if (f->rth_for_killing) |
| 5585 |
rtnl_close(f->rth_for_killing); |
| 5586 |
return ret; |
| 5587 |
} |
| 5588 |
|
| 5589 |
static int get_snmp_int(char *proto, char *key, int *result) |
| 5590 |
{ |
| 5591 |
char buf[1024]; |
| 5592 |
FILE *fp; |
| 5593 |
int protolen = strlen(proto); |
| 5594 |
int keylen = strlen(key); |
| 5595 |
|
| 5596 |
*result = 0; |
| 5597 |
|
| 5598 |
if ((fp = net_snmp_open()) == NULL) |
| 5599 |
return -1; |
| 5600 |
|
| 5601 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 5602 |
char *p = buf; |
| 5603 |
int pos = 0; |
| 5604 |
|
| 5605 |
if (memcmp(buf, proto, protolen)) |
| 5606 |
continue; |
| 5607 |
while ((p = strchr(p, ' ')) != NULL) { |
| 5608 |
pos++; |
| 5609 |
p++; |
| 5610 |
if (memcmp(p, key, keylen) == 0 && |
| 5611 |
(p[keylen] == ' ' || p[keylen] == '\n')) |
| 5612 |
break; |
| 5613 |
} |
| 5614 |
if (fgets(buf, sizeof(buf), fp) == NULL) |
| 5615 |
break; |
| 5616 |
if (memcmp(buf, proto, protolen)) |
| 5617 |
break; |
| 5618 |
p = buf; |
| 5619 |
while ((p = strchr(p, ' ')) != NULL) { |
| 5620 |
p++; |
| 5621 |
if (--pos == 0) { |
| 5622 |
sscanf(p, "%d", result); |
| 5623 |
fclose(fp); |
| 5624 |
return 0; |
| 5625 |
} |
| 5626 |
} |
| 5627 |
} |
| 5628 |
|
| 5629 |
fclose(fp); |
| 5630 |
errno = ESRCH; |
| 5631 |
return -1; |
| 5632 |
} |
| 5633 |
|
| 5634 |
|
| 5635 |
/* Get stats from sockstat */ |
| 5636 |
|
| 5637 |
struct ssummary { |
| 5638 |
int socks; |
| 5639 |
int tcp_mem; |
| 5640 |
int tcp_total; |
| 5641 |
int tcp_orphans; |
| 5642 |
int tcp_tws; |
| 5643 |
int tcp4_hashed; |
| 5644 |
int udp4; |
| 5645 |
int raw4; |
| 5646 |
int frag4; |
| 5647 |
int frag4_mem; |
| 5648 |
int tcp6_hashed; |
| 5649 |
int udp6; |
| 5650 |
int raw6; |
| 5651 |
int frag6; |
| 5652 |
int frag6_mem; |
| 5653 |
}; |
| 5654 |
|
| 5655 |
static void get_sockstat_line(char *line, struct ssummary *s) |
| 5656 |
{ |
| 5657 |
char id[256], rem[256]; |
| 5658 |
|
| 5659 |
if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) |
| 5660 |
return; |
| 5661 |
|
| 5662 |
if (strcmp(id, "sockets:") == 0) |
| 5663 |
sscanf(rem, "%*s%d", &s->socks); |
| 5664 |
else if (strcmp(id, "UDP:") == 0) |
| 5665 |
sscanf(rem, "%*s%d", &s->udp4); |
| 5666 |
else if (strcmp(id, "UDP6:") == 0) |
| 5667 |
sscanf(rem, "%*s%d", &s->udp6); |
| 5668 |
else if (strcmp(id, "RAW:") == 0) |
| 5669 |
sscanf(rem, "%*s%d", &s->raw4); |
| 5670 |
else if (strcmp(id, "RAW6:") == 0) |
| 5671 |
sscanf(rem, "%*s%d", &s->raw6); |
| 5672 |
else if (strcmp(id, "TCP6:") == 0) |
| 5673 |
sscanf(rem, "%*s%d", &s->tcp6_hashed); |
| 5674 |
else if (strcmp(id, "FRAG:") == 0) |
| 5675 |
sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); |
| 5676 |
else if (strcmp(id, "FRAG6:") == 0) |
| 5677 |
sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); |
| 5678 |
else if (strcmp(id, "TCP:") == 0) |
| 5679 |
sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", |
| 5680 |
&s->tcp4_hashed, |
| 5681 |
&s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); |
| 5682 |
} |
| 5683 |
|
| 5684 |
static int get_sockstat(struct ssummary *s) |
| 5685 |
{ |
| 5686 |
char buf[256]; |
| 5687 |
FILE *fp; |
| 5688 |
|
| 5689 |
memset(s, 0, sizeof(*s)); |
| 5690 |
|
| 5691 |
if ((fp = net_sockstat_open()) == NULL) |
| 5692 |
return -1; |
| 5693 |
while (fgets(buf, sizeof(buf), fp) != NULL) |
| 5694 |
get_sockstat_line(buf, s); |
| 5695 |
fclose(fp); |
| 5696 |
|
| 5697 |
if ((fp = net_sockstat6_open()) == NULL) |
| 5698 |
return 0; |
| 5699 |
while (fgets(buf, sizeof(buf), fp) != NULL) |
| 5700 |
get_sockstat_line(buf, s); |
| 5701 |
fclose(fp); |
| 5702 |
|
| 5703 |
return 0; |
| 5704 |
} |
| 5705 |
|
| 5706 |
static int print_summary(void) |
| 5707 |
{ |
| 5708 |
struct ssummary s; |
| 5709 |
int tcp_estab; |
| 5710 |
|
| 5711 |
if (get_sockstat(&s) < 0) |
| 5712 |
perror("ss: get_sockstat"); |
| 5713 |
if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab) < 0) |
| 5714 |
perror("ss: get_snmpstat"); |
| 5715 |
|
| 5716 |
printf("Total: %d\n", s.socks); |
| 5717 |
|
| 5718 |
printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n", |
| 5719 |
s.tcp_total + s.tcp_tws, tcp_estab, |
| 5720 |
s.tcp_total - (s.tcp4_hashed + s.tcp6_hashed - s.tcp_tws), |
| 5721 |
s.tcp_orphans, s.tcp_tws); |
| 5722 |
|
| 5723 |
printf("\n"); |
| 5724 |
printf("Transport Total IP IPv6\n"); |
| 5725 |
printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); |
| 5726 |
printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); |
| 5727 |
printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); |
| 5728 |
printf("INET %-9d %-9d %-9d\n", |
| 5729 |
s.raw4+s.udp4+s.tcp4_hashed+ |
| 5730 |
s.raw6+s.udp6+s.tcp6_hashed, |
| 5731 |
s.raw4+s.udp4+s.tcp4_hashed, |
| 5732 |
s.raw6+s.udp6+s.tcp6_hashed); |
| 5733 |
printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); |
| 5734 |
|
| 5735 |
printf("\n"); |
| 5736 |
|
| 5737 |
return 0; |
| 5738 |
} |
| 5739 |
|
| 5740 |
static void _usage(FILE *dest) |
| 5741 |
{ |
| 5742 |
fprintf(dest, |
| 5743 |
"Usage: ss [ OPTIONS ]\n" |
| 5744 |
" ss [ OPTIONS ] [ FILTER ]\n" |
| 5745 |
" -h, --help this message\n" |
| 5746 |
" -V, --version output version information\n" |
| 5747 |
" -n, --numeric don't resolve service names\n" |
| 5748 |
" -r, --resolve resolve host names\n" |
| 5749 |
" -a, --all display all sockets\n" |
| 5750 |
" -l, --listening display listening sockets\n" |
| 5751 |
" -B, --bound-inactive display TCP bound but inactive sockets\n" |
| 5752 |
" -o, --options show timer information\n" |
| 5753 |
" -e, --extended show detailed socket information\n" |
| 5754 |
" -m, --memory show socket memory usage\n" |
| 5755 |
" -p, --processes show process using socket\n" |
| 5756 |
" -T, --threads show thread using socket\n" |
| 5757 |
" -i, --info show internal TCP information\n" |
| 5758 |
" --tipcinfo show internal tipc socket information\n" |
| 5759 |
" -s, --summary show socket usage summary\n" |
| 5760 |
" --tos show tos and priority information\n" |
| 5761 |
" --cgroup show cgroup information\n" |
| 5762 |
" -b, --bpf show bpf filter socket information\n" |
| 5763 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 5764 |
" --bpf-maps show all BPF socket-local storage maps\n" |
| 5765 |
" --bpf-map-id=MAP-ID show a BPF socket-local storage map\n" |
| 5766 |
#endif |
| 5767 |
" -E, --events continually display sockets as they are destroyed\n" |
| 5768 |
" -Z, --context display task SELinux security contexts\n" |
| 5769 |
" -z, --contexts display task and socket SELinux security contexts\n" |
| 5770 |
" -N, --net switch to the specified network namespace name\n" |
| 5771 |
"\n" |
| 5772 |
" -4, --ipv4 display only IP version 4 sockets\n" |
| 5773 |
" -6, --ipv6 display only IP version 6 sockets\n" |
| 5774 |
" -0, --packet display PACKET sockets\n" |
| 5775 |
" -t, --tcp display only TCP sockets\n" |
| 5776 |
" -M, --mptcp display only MPTCP sockets\n" |
| 5777 |
" -S, --sctp display only SCTP sockets\n" |
| 5778 |
" -u, --udp display only UDP sockets\n" |
| 5779 |
" -d, --dccp display only DCCP sockets\n" |
| 5780 |
" -w, --raw display only RAW sockets\n" |
| 5781 |
" -x, --unix display only Unix domain sockets\n" |
| 5782 |
" --tipc display only TIPC sockets\n" |
| 5783 |
" --vsock display only vsock sockets\n" |
| 5784 |
" --xdp display only XDP sockets\n" |
| 5785 |
" -f, --family=FAMILY display sockets of type FAMILY\n" |
| 5786 |
" FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help}\n" |
| 5787 |
"\n" |
| 5788 |
" -K, --kill forcibly close sockets, display what was closed\n" |
| 5789 |
" -H, --no-header Suppress header line\n" |
| 5790 |
" -Q, --no-queues Suppress sending and receiving queue columns\n" |
| 5791 |
" -O, --oneline socket's data printed on a single line\n" |
| 5792 |
" --inet-sockopt show various inet socket options\n" |
| 5793 |
"\n" |
| 5794 |
" -A, --query=QUERY, --socket=QUERY\n" |
| 5795 |
" QUERY := {all|inet|tcp|mptcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|packet_raw|packet_dgram|netlink|dccp|sctp|vsock_stream|vsock_dgram|tipc|xdp}[,QUERY]\n" |
| 5796 |
"\n" |
| 5797 |
" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" |
| 5798 |
" -F, --filter=FILE read filter information from FILE\n" |
| 5799 |
" FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n" |
| 5800 |
" STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n" |
| 5801 |
" TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n" |
| 5802 |
" connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" |
| 5803 |
" synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" |
| 5804 |
" bucket := {syn-recv|time-wait}\n" |
| 5805 |
" big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n" |
| 5806 |
); |
| 5807 |
} |
| 5808 |
|
| 5809 |
static void help(void) __attribute__((noreturn)); |
| 5810 |
static void help(void) |
| 5811 |
{ |
| 5812 |
_usage(stdout); |
| 5813 |
exit(0); |
| 5814 |
} |
| 5815 |
|
| 5816 |
static void usage(void) __attribute__((noreturn)); |
| 5817 |
static void usage(void) |
| 5818 |
{ |
| 5819 |
_usage(stderr); |
| 5820 |
exit(-1); |
| 5821 |
} |
| 5822 |
|
| 5823 |
|
| 5824 |
static int scan_state(const char *state) |
| 5825 |
{ |
| 5826 |
static const char * const sstate_namel[] = { |
| 5827 |
"UNKNOWN", |
| 5828 |
[SS_ESTABLISHED] = "established", |
| 5829 |
[SS_SYN_SENT] = "syn-sent", |
| 5830 |
[SS_SYN_RECV] = "syn-recv", |
| 5831 |
[SS_FIN_WAIT1] = "fin-wait-1", |
| 5832 |
[SS_FIN_WAIT2] = "fin-wait-2", |
| 5833 |
[SS_TIME_WAIT] = "time-wait", |
| 5834 |
[SS_CLOSE] = "unconnected", |
| 5835 |
[SS_CLOSE_WAIT] = "close-wait", |
| 5836 |
[SS_LAST_ACK] = "last-ack", |
| 5837 |
[SS_LISTEN] = "listening", |
| 5838 |
[SS_CLOSING] = "closing", |
| 5839 |
[SS_NEW_SYN_RECV] = "new-syn-recv", |
| 5840 |
[SS_BOUND_INACTIVE] = "bound-inactive", |
| 5841 |
}; |
| 5842 |
int i; |
| 5843 |
|
| 5844 |
/* NEW_SYN_RECV is a kernel implementation detail. It shouldn't be used |
| 5845 |
* or even be visible by users. |
| 5846 |
*/ |
| 5847 |
if (strcasecmp(state, "new-syn-recv") == 0) |
| 5848 |
goto wrong_state; |
| 5849 |
|
| 5850 |
if (strcasecmp(state, "close") == 0 || |
| 5851 |
strcasecmp(state, "closed") == 0) |
| 5852 |
return (1<<SS_CLOSE); |
| 5853 |
if (strcasecmp(state, "syn-rcv") == 0) |
| 5854 |
return (1<<SS_SYN_RECV); |
| 5855 |
if (strcasecmp(state, "established") == 0) |
| 5856 |
return (1<<SS_ESTABLISHED); |
| 5857 |
if (strcasecmp(state, "all") == 0) |
| 5858 |
return SS_ALL; |
| 5859 |
if (strcasecmp(state, "connected") == 0) |
| 5860 |
return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); |
| 5861 |
if (strcasecmp(state, "synchronized") == 0) |
| 5862 |
return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
| 5863 |
if (strcasecmp(state, "bucket") == 0) |
| 5864 |
return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); |
| 5865 |
if (strcasecmp(state, "big") == 0) |
| 5866 |
return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); |
| 5867 |
for (i = 0; i < SS_MAX; i++) { |
| 5868 |
if (strcasecmp(state, sstate_namel[i]) == 0) |
| 5869 |
return (1<<i); |
| 5870 |
} |
| 5871 |
|
| 5872 |
wrong_state: |
| 5873 |
fprintf(stderr, "ss: wrong state name: %s\n", state); |
| 5874 |
exit(-1); |
| 5875 |
} |
| 5876 |
|
| 5877 |
/* Values 'v' and 'V' are already used so a non-character is used */ |
| 5878 |
#define OPT_VSOCK 256 |
| 5879 |
|
| 5880 |
/* Values of 't' are already used so a non-character is used */ |
| 5881 |
#define OPT_TIPCSOCK 257 |
| 5882 |
#define OPT_TIPCINFO 258 |
| 5883 |
|
| 5884 |
#define OPT_TOS 259 |
| 5885 |
|
| 5886 |
/* Values of 'x' are already used so a non-character is used */ |
| 5887 |
#define OPT_XDPSOCK 260 |
| 5888 |
|
| 5889 |
#define OPT_CGROUP 261 |
| 5890 |
|
| 5891 |
#define OPT_INET_SOCKOPT 262 |
| 5892 |
|
| 5893 |
#define OPT_BPF_MAPS 263 |
| 5894 |
#define OPT_BPF_MAP_ID 264 |
| 5895 |
|
| 5896 |
static const struct option long_opts[] = { |
| 5897 |
{ "numeric", 0, 0, 'n' }, |
| 5898 |
{ "resolve", 0, 0, 'r' }, |
| 5899 |
{ "options", 0, 0, 'o' }, |
| 5900 |
{ "extended", 0, 0, 'e' }, |
| 5901 |
{ "memory", 0, 0, 'm' }, |
| 5902 |
{ "info", 0, 0, 'i' }, |
| 5903 |
{ "processes", 0, 0, 'p' }, |
| 5904 |
{ "threads", 0, 0, 'T' }, |
| 5905 |
{ "bpf", 0, 0, 'b' }, |
| 5906 |
{ "events", 0, 0, 'E' }, |
| 5907 |
{ "dccp", 0, 0, 'd' }, |
| 5908 |
{ "tcp", 0, 0, 't' }, |
| 5909 |
{ "sctp", 0, 0, 'S' }, |
| 5910 |
{ "udp", 0, 0, 'u' }, |
| 5911 |
{ "raw", 0, 0, 'w' }, |
| 5912 |
{ "unix", 0, 0, 'x' }, |
| 5913 |
{ "tipc", 0, 0, OPT_TIPCSOCK}, |
| 5914 |
{ "vsock", 0, 0, OPT_VSOCK }, |
| 5915 |
{ "all", 0, 0, 'a' }, |
| 5916 |
{ "listening", 0, 0, 'l' }, |
| 5917 |
{ "bound-inactive", 0, 0, 'B' }, |
| 5918 |
{ "ipv4", 0, 0, '4' }, |
| 5919 |
{ "ipv6", 0, 0, '6' }, |
| 5920 |
{ "packet", 0, 0, '0' }, |
| 5921 |
{ "family", 1, 0, 'f' }, |
| 5922 |
{ "socket", 1, 0, 'A' }, |
| 5923 |
{ "query", 1, 0, 'A' }, |
| 5924 |
{ "summary", 0, 0, 's' }, |
| 5925 |
{ "diag", 1, 0, 'D' }, |
| 5926 |
{ "filter", 1, 0, 'F' }, |
| 5927 |
{ "version", 0, 0, 'V' }, |
| 5928 |
{ "help", 0, 0, 'h' }, |
| 5929 |
{ "context", 0, 0, 'Z' }, |
| 5930 |
{ "contexts", 0, 0, 'z' }, |
| 5931 |
{ "net", 1, 0, 'N' }, |
| 5932 |
{ "tipcinfo", 0, 0, OPT_TIPCINFO}, |
| 5933 |
{ "tos", 0, 0, OPT_TOS }, |
| 5934 |
{ "cgroup", 0, 0, OPT_CGROUP }, |
| 5935 |
{ "kill", 0, 0, 'K' }, |
| 5936 |
{ "no-header", 0, 0, 'H' }, |
| 5937 |
{ "no-queues", 0, 0, 'Q' }, |
| 5938 |
{ "xdp", 0, 0, OPT_XDPSOCK}, |
| 5939 |
{ "mptcp", 0, 0, 'M' }, |
| 5940 |
{ "oneline", 0, 0, 'O' }, |
| 5941 |
{ "inet-sockopt", 0, 0, OPT_INET_SOCKOPT }, |
| 5942 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 5943 |
{ "bpf-maps", 0, 0, OPT_BPF_MAPS}, |
| 5944 |
{ "bpf-map-id", 1, 0, OPT_BPF_MAP_ID}, |
| 5945 |
#endif |
| 5946 |
{ 0 } |
| 5947 |
|
| 5948 |
}; |
| 5949 |
|
| 5950 |
int main(int argc, char *argv[]) |
| 5951 |
{ |
| 5952 |
int saw_states = 0; |
| 5953 |
int saw_query = 0; |
| 5954 |
int do_summary = 0; |
| 5955 |
const char *dump_tcpdiag = NULL; |
| 5956 |
FILE *filter_fp = NULL; |
| 5957 |
int ch; |
| 5958 |
int state_filter = 0; |
| 5959 |
|
| 5960 |
while ((ch = getopt_long(argc, argv, |
| 5961 |
"dhalBetuwxnro460spTbEf:mMiA:D:F:vVzZN:KHQSO", |
| 5962 |
long_opts, NULL)) != EOF) { |
| 5963 |
switch (ch) { |
| 5964 |
case 'n': |
| 5965 |
numeric = 1; |
| 5966 |
break; |
| 5967 |
case 'r': |
| 5968 |
resolve_hosts = 1; |
| 5969 |
break; |
| 5970 |
case 'o': |
| 5971 |
show_options = 1; |
| 5972 |
break; |
| 5973 |
case 'e': |
| 5974 |
show_options = 1; |
| 5975 |
show_details++; |
| 5976 |
break; |
| 5977 |
case 'm': |
| 5978 |
show_mem = 1; |
| 5979 |
break; |
| 5980 |
case 'i': |
| 5981 |
show_tcpinfo = 1; |
| 5982 |
break; |
| 5983 |
case 'p': |
| 5984 |
show_processes++; |
| 5985 |
break; |
| 5986 |
case 'T': |
| 5987 |
show_threads++; |
| 5988 |
break; |
| 5989 |
case 'b': |
| 5990 |
show_options = 1; |
| 5991 |
show_bpf++; |
| 5992 |
break; |
| 5993 |
case 'E': |
| 5994 |
follow_events = 1; |
| 5995 |
break; |
| 5996 |
case 'd': |
| 5997 |
filter_db_set(¤t_filter, DCCP_DB, true); |
| 5998 |
break; |
| 5999 |
case 't': |
| 6000 |
filter_db_set(¤t_filter, TCP_DB, true); |
| 6001 |
break; |
| 6002 |
case 'S': |
| 6003 |
filter_db_set(¤t_filter, SCTP_DB, true); |
| 6004 |
break; |
| 6005 |
case 'u': |
| 6006 |
filter_db_set(¤t_filter, UDP_DB, true); |
| 6007 |
break; |
| 6008 |
case 'w': |
| 6009 |
filter_db_set(¤t_filter, RAW_DB, true); |
| 6010 |
break; |
| 6011 |
case 'x': |
| 6012 |
filter_af_set(¤t_filter, AF_UNIX); |
| 6013 |
break; |
| 6014 |
case OPT_VSOCK: |
| 6015 |
filter_af_set(¤t_filter, AF_VSOCK); |
| 6016 |
break; |
| 6017 |
case OPT_TIPCSOCK: |
| 6018 |
filter_af_set(¤t_filter, AF_TIPC); |
| 6019 |
break; |
| 6020 |
case 'a': |
| 6021 |
state_filter = SS_ALL; |
| 6022 |
break; |
| 6023 |
case 'l': |
| 6024 |
state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE); |
| 6025 |
break; |
| 6026 |
case 'B': |
| 6027 |
state_filter = 1 << SS_BOUND_INACTIVE; |
| 6028 |
break; |
| 6029 |
case '4': |
| 6030 |
filter_af_set(¤t_filter, AF_INET); |
| 6031 |
break; |
| 6032 |
case '6': |
| 6033 |
filter_af_set(¤t_filter, AF_INET6); |
| 6034 |
break; |
| 6035 |
case '0': |
| 6036 |
filter_af_set(¤t_filter, AF_PACKET); |
| 6037 |
break; |
| 6038 |
case OPT_XDPSOCK: |
| 6039 |
filter_af_set(¤t_filter, AF_XDP); |
| 6040 |
break; |
| 6041 |
case 'M': |
| 6042 |
filter_db_set(¤t_filter, MPTCP_DB, true); |
| 6043 |
break; |
| 6044 |
case 'f': |
| 6045 |
if (strcmp(optarg, "inet") == 0) |
| 6046 |
filter_af_set(¤t_filter, AF_INET); |
| 6047 |
else if (strcmp(optarg, "inet6") == 0) |
| 6048 |
filter_af_set(¤t_filter, AF_INET6); |
| 6049 |
else if (strcmp(optarg, "link") == 0) |
| 6050 |
filter_af_set(¤t_filter, AF_PACKET); |
| 6051 |
else if (strcmp(optarg, "unix") == 0) |
| 6052 |
filter_af_set(¤t_filter, AF_UNIX); |
| 6053 |
else if (strcmp(optarg, "netlink") == 0) |
| 6054 |
filter_af_set(¤t_filter, AF_NETLINK); |
| 6055 |
else if (strcmp(optarg, "tipc") == 0) |
| 6056 |
filter_af_set(¤t_filter, AF_TIPC); |
| 6057 |
else if (strcmp(optarg, "vsock") == 0) |
| 6058 |
filter_af_set(¤t_filter, AF_VSOCK); |
| 6059 |
else if (strcmp(optarg, "xdp") == 0) |
| 6060 |
filter_af_set(¤t_filter, AF_XDP); |
| 6061 |
else if (strcmp(optarg, "help") == 0) |
| 6062 |
help(); |
| 6063 |
else { |
| 6064 |
fprintf(stderr, "ss: \"%s\" is invalid family\n", |
| 6065 |
optarg); |
| 6066 |
usage(); |
| 6067 |
} |
| 6068 |
break; |
| 6069 |
case 'A': |
| 6070 |
{ |
| 6071 |
char *p, *p1; |
| 6072 |
|
| 6073 |
if (!saw_query) { |
| 6074 |
current_filter.dbs = 0; |
| 6075 |
state_filter = state_filter ? |
| 6076 |
state_filter : SS_CONN; |
| 6077 |
saw_query = 1; |
| 6078 |
do_default = 0; |
| 6079 |
} |
| 6080 |
p = p1 = optarg; |
| 6081 |
do { |
| 6082 |
if ((p1 = strchr(p, ',')) != NULL) |
| 6083 |
*p1 = 0; |
| 6084 |
if (filter_db_parse(¤t_filter, p)) { |
| 6085 |
fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); |
| 6086 |
usage(); |
| 6087 |
} |
| 6088 |
p = p1 + 1; |
| 6089 |
} while (p1); |
| 6090 |
break; |
| 6091 |
} |
| 6092 |
case 's': |
| 6093 |
do_summary = 1; |
| 6094 |
break; |
| 6095 |
case 'D': |
| 6096 |
dump_tcpdiag = optarg; |
| 6097 |
break; |
| 6098 |
case 'F': |
| 6099 |
if (filter_fp) { |
| 6100 |
fprintf(stderr, "More than one filter file\n"); |
| 6101 |
exit(-1); |
| 6102 |
} |
| 6103 |
if (optarg[0] == '-') |
| 6104 |
filter_fp = stdin; |
| 6105 |
else |
| 6106 |
filter_fp = fopen(optarg, "r"); |
| 6107 |
if (!filter_fp) { |
| 6108 |
perror("fopen filter file"); |
| 6109 |
exit(-1); |
| 6110 |
} |
| 6111 |
break; |
| 6112 |
case 'v': |
| 6113 |
case 'V': |
| 6114 |
printf("ss utility, iproute2-%s\n", version); |
| 6115 |
exit(0); |
| 6116 |
case 'z': |
| 6117 |
show_sock_ctx++; |
| 6118 |
/* fall through */ |
| 6119 |
case 'Z': |
| 6120 |
if (!is_selinux_enabled()) { |
| 6121 |
fprintf(stderr, "ss: SELinux is not enabled.\n"); |
| 6122 |
exit(1); |
| 6123 |
} |
| 6124 |
show_proc_ctx++; |
| 6125 |
break; |
| 6126 |
case 'N': |
| 6127 |
if (netns_switch(optarg)) |
| 6128 |
exit(1); |
| 6129 |
break; |
| 6130 |
case OPT_TIPCINFO: |
| 6131 |
show_tipcinfo = 1; |
| 6132 |
break; |
| 6133 |
case OPT_TOS: |
| 6134 |
show_tos = 1; |
| 6135 |
break; |
| 6136 |
case OPT_CGROUP: |
| 6137 |
show_cgroup = 1; |
| 6138 |
break; |
| 6139 |
case 'K': |
| 6140 |
current_filter.kill = 1; |
| 6141 |
break; |
| 6142 |
case 'H': |
| 6143 |
show_header = 0; |
| 6144 |
break; |
| 6145 |
case 'Q': |
| 6146 |
show_queues = 0; |
| 6147 |
break; |
| 6148 |
case 'O': |
| 6149 |
oneline = 1; |
| 6150 |
break; |
| 6151 |
case OPT_INET_SOCKOPT: |
| 6152 |
show_inet_sockopt = 1; |
| 6153 |
break; |
| 6154 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 6155 |
case OPT_BPF_MAPS: |
| 6156 |
if (bpf_map_opts.nr_maps) { |
| 6157 |
bpf_map_opts_mixed_error(); |
| 6158 |
return -1; |
| 6159 |
} |
| 6160 |
bpf_map_opts.show_all = true; |
| 6161 |
break; |
| 6162 |
case OPT_BPF_MAP_ID: |
| 6163 |
if (bpf_map_opts_add_id(optarg)) |
| 6164 |
exit(1); |
| 6165 |
break; |
| 6166 |
#endif |
| 6167 |
case 'h': |
| 6168 |
help(); |
| 6169 |
case '?': |
| 6170 |
default: |
| 6171 |
usage(); |
| 6172 |
} |
| 6173 |
} |
| 6174 |
|
| 6175 |
if (show_processes || show_threads || show_proc_ctx || show_sock_ctx) |
| 6176 |
user_ent_hash_build(); |
| 6177 |
|
| 6178 |
argc -= optind; |
| 6179 |
argv += optind; |
| 6180 |
|
| 6181 |
if (do_summary) { |
| 6182 |
print_summary(); |
| 6183 |
if (do_default && argc == 0) |
| 6184 |
exit(0); |
| 6185 |
} |
| 6186 |
|
| 6187 |
while (argc > 0) { |
| 6188 |
if (strcmp(*argv, "state") == 0) { |
| 6189 |
NEXT_ARG(); |
| 6190 |
if (!saw_states) |
| 6191 |
state_filter = 0; |
| 6192 |
state_filter |= scan_state(*argv); |
| 6193 |
saw_states = 1; |
| 6194 |
} else if (strcmp(*argv, "exclude") == 0 || |
| 6195 |
strcmp(*argv, "excl") == 0) { |
| 6196 |
NEXT_ARG(); |
| 6197 |
if (!saw_states) |
| 6198 |
state_filter = SS_ALL; |
| 6199 |
state_filter &= ~scan_state(*argv); |
| 6200 |
saw_states = 1; |
| 6201 |
} else { |
| 6202 |
break; |
| 6203 |
} |
| 6204 |
argc--; argv++; |
| 6205 |
} |
| 6206 |
|
| 6207 |
if (do_default) { |
| 6208 |
state_filter = state_filter ? state_filter : SS_CONN; |
| 6209 |
filter_db_parse(¤t_filter, "all"); |
| 6210 |
} |
| 6211 |
|
| 6212 |
filter_states_set(¤t_filter, state_filter); |
| 6213 |
filter_merge_defaults(¤t_filter); |
| 6214 |
|
| 6215 |
#ifdef HAVE_RPC |
| 6216 |
if (!numeric && resolve_hosts && |
| 6217 |
(current_filter.dbs & (UNIX_DBM|INET_L4_DBM))) |
| 6218 |
init_service_resolver(); |
| 6219 |
#endif |
| 6220 |
|
| 6221 |
if (current_filter.dbs == 0) { |
| 6222 |
fprintf(stderr, "ss: no socket tables to show with such filter.\n"); |
| 6223 |
exit(0); |
| 6224 |
} |
| 6225 |
if (current_filter.families == 0) { |
| 6226 |
fprintf(stderr, "ss: no families to show with such filter.\n"); |
| 6227 |
exit(0); |
| 6228 |
} |
| 6229 |
if (current_filter.states == 0) { |
| 6230 |
fprintf(stderr, "ss: no socket states to show with such filter.\n"); |
| 6231 |
exit(0); |
| 6232 |
} |
| 6233 |
|
| 6234 |
if (dump_tcpdiag) { |
| 6235 |
FILE *dump_fp = stdout; |
| 6236 |
|
| 6237 |
if (!(current_filter.dbs & (1<<TCP_DB))) { |
| 6238 |
fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); |
| 6239 |
exit(0); |
| 6240 |
} |
| 6241 |
if (dump_tcpdiag[0] != '-') { |
| 6242 |
dump_fp = fopen(dump_tcpdiag, "w"); |
| 6243 |
if (!dump_fp) { |
| 6244 |
perror("fopen dump file"); |
| 6245 |
exit(-1); |
| 6246 |
} |
| 6247 |
} |
| 6248 |
inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); |
| 6249 |
fflush(dump_fp); |
| 6250 |
exit(0); |
| 6251 |
} |
| 6252 |
|
| 6253 |
if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) |
| 6254 |
usage(); |
| 6255 |
|
| 6256 |
if (!show_processes) |
| 6257 |
columns[COL_PROC].disabled = 1; |
| 6258 |
|
| 6259 |
if (!show_queues) { |
| 6260 |
columns[COL_SENDQ].disabled = 1; |
| 6261 |
columns[COL_RECVQ].disabled = 1; |
| 6262 |
} |
| 6263 |
|
| 6264 |
if (!(current_filter.dbs & (current_filter.dbs - 1))) |
| 6265 |
columns[COL_NETID].disabled = 1; |
| 6266 |
|
| 6267 |
if (!(current_filter.states & (current_filter.states - 1))) |
| 6268 |
columns[COL_STATE].disabled = 1; |
| 6269 |
|
| 6270 |
if (show_header) |
| 6271 |
print_header(); |
| 6272 |
|
| 6273 |
fflush(stdout); |
| 6274 |
|
| 6275 |
if (follow_events) |
| 6276 |
exit(handle_follow_request(¤t_filter)); |
| 6277 |
|
| 6278 |
if (current_filter.dbs & (1<<NETLINK_DB)) |
| 6279 |
netlink_show(¤t_filter); |
| 6280 |
if (current_filter.dbs & PACKET_DBM) |
| 6281 |
packet_show(¤t_filter); |
| 6282 |
if (current_filter.dbs & UNIX_DBM) |
| 6283 |
unix_show(¤t_filter); |
| 6284 |
if (current_filter.dbs & (1<<RAW_DB)) |
| 6285 |
raw_show(¤t_filter); |
| 6286 |
if (current_filter.dbs & (1<<UDP_DB)) |
| 6287 |
udp_show(¤t_filter); |
| 6288 |
if (current_filter.dbs & (1<<TCP_DB)) |
| 6289 |
tcp_show(¤t_filter); |
| 6290 |
if (current_filter.dbs & (1<<DCCP_DB)) |
| 6291 |
dccp_show(¤t_filter); |
| 6292 |
if (current_filter.dbs & (1<<SCTP_DB)) |
| 6293 |
sctp_show(¤t_filter); |
| 6294 |
if (current_filter.dbs & VSOCK_DBM) |
| 6295 |
vsock_show(¤t_filter); |
| 6296 |
if (current_filter.dbs & (1<<TIPC_DB)) |
| 6297 |
tipc_show(¤t_filter); |
| 6298 |
if (current_filter.dbs & (1<<XDP_DB)) |
| 6299 |
xdp_show(¤t_filter); |
| 6300 |
if (current_filter.dbs & (1<<MPTCP_DB)) |
| 6301 |
mptcp_show(¤t_filter); |
| 6302 |
|
| 6303 |
if (show_processes || show_threads || show_proc_ctx || show_sock_ctx) |
| 6304 |
user_ent_destroy(); |
| 6305 |
|
| 6306 |
#ifdef ENABLE_BPF_SKSTORAGE_SUPPORT |
| 6307 |
bpf_map_opts_destroy(); |
| 6308 |
#endif |
| 6309 |
|
| 6310 |
render(); |
| 6311 |
|
| 6312 |
return 0; |
| 6313 |
} |