]> git.proxmox.com Git - mirror_iproute2.git/blob - misc/ss.c
ss: Render buffer to output every time a number of chunks are allocated
[mirror_iproute2.git] / misc / ss.c
1 /*
2 * ss.c "sockstat", socket statistics
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18 #include <sys/uio.h>
19 #include <sys/sysmacros.h>
20 #include <netinet/in.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <arpa/inet.h>
25 #include <dirent.h>
26 #include <fnmatch.h>
27 #include <getopt.h>
28 #include <stdbool.h>
29 #include <limits.h>
30 #include <stdarg.h>
31
32 #include "utils.h"
33 #include "rt_names.h"
34 #include "ll_map.h"
35 #include "libnetlink.h"
36 #include "namespace.h"
37 #include "SNAPSHOT.h"
38
39 #include <linux/tcp.h>
40 #include <linux/sock_diag.h>
41 #include <linux/inet_diag.h>
42 #include <linux/unix_diag.h>
43 #include <linux/netdevice.h> /* for MAX_ADDR_LEN */
44 #include <linux/filter.h>
45 #include <linux/packet_diag.h>
46 #include <linux/netlink_diag.h>
47 #include <linux/sctp.h>
48 #include <linux/vm_sockets_diag.h>
49 #include <linux/net.h>
50 #include <linux/tipc.h>
51 #include <linux/tipc_netlink.h>
52 #include <linux/tipc_sockets_diag.h>
53
54 /* AF_VSOCK/PF_VSOCK is only provided since glibc 2.18 */
55 #ifndef PF_VSOCK
56 #define PF_VSOCK 40
57 #endif
58 #ifndef AF_VSOCK
59 #define AF_VSOCK PF_VSOCK
60 #endif
61
62 #define MAGIC_SEQ 123456
63 #define BUF_CHUNK (1024 * 1024) /* Buffer chunk allocation size */
64 #define BUF_CHUNKS_MAX 5 /* Maximum number of allocated buffer chunks */
65 #define LEN_ALIGN(x) (((x) + 1) & ~1)
66
67 #define DIAG_REQUEST(_req, _r) \
68 struct { \
69 struct nlmsghdr nlh; \
70 _r; \
71 } _req = { \
72 .nlh = { \
73 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
74 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
75 .nlmsg_seq = MAGIC_SEQ, \
76 .nlmsg_len = sizeof(_req), \
77 }, \
78 }
79
80 #if HAVE_SELINUX
81 #include <selinux/selinux.h>
82 #else
83 /* Stubs for SELinux functions */
84 static int is_selinux_enabled(void)
85 {
86 return -1;
87 }
88
89 static int getpidcon(pid_t pid, char **context)
90 {
91 *context = NULL;
92 return -1;
93 }
94
95 static int getfilecon(char *path, char **context)
96 {
97 *context = NULL;
98 return -1;
99 }
100
101 static int security_get_initial_context(char *name, char **context)
102 {
103 *context = NULL;
104 return -1;
105 }
106 #endif
107
108 static int resolve_services = 1;
109 int preferred_family = AF_UNSPEC;
110 static int show_options;
111 int show_details;
112 static int show_users;
113 static int show_mem;
114 static int show_tcpinfo;
115 static int show_bpf;
116 static int show_proc_ctx;
117 static int show_sock_ctx;
118 static int show_header = 1;
119 static int follow_events;
120 static int sctp_ino;
121 static int show_tipcinfo;
122 static int show_tos;
123
124 enum col_id {
125 COL_NETID,
126 COL_STATE,
127 COL_RECVQ,
128 COL_SENDQ,
129 COL_ADDR,
130 COL_SERV,
131 COL_RADDR,
132 COL_RSERV,
133 COL_EXT,
134 COL_MAX
135 };
136
137 enum col_align {
138 ALIGN_LEFT,
139 ALIGN_CENTER,
140 ALIGN_RIGHT
141 };
142
143 struct column {
144 const enum col_align align;
145 const char *header;
146 const char *ldelim;
147 int disabled;
148 int width; /* Calculated, including additional layout spacing */
149 int max_len; /* Measured maximum field length in this column */
150 };
151
152 static struct column columns[] = {
153 { ALIGN_LEFT, "Netid", "", 0, 0, 0 },
154 { ALIGN_LEFT, "State", " ", 0, 0, 0 },
155 { ALIGN_LEFT, "Recv-Q", " ", 0, 0, 0 },
156 { ALIGN_LEFT, "Send-Q", " ", 0, 0, 0 },
157 { ALIGN_RIGHT, "Local Address:", " ", 0, 0, 0 },
158 { ALIGN_LEFT, "Port", "", 0, 0, 0 },
159 { ALIGN_RIGHT, "Peer Address:", " ", 0, 0, 0 },
160 { ALIGN_LEFT, "Port", "", 0, 0, 0 },
161 { ALIGN_LEFT, "", "", 0, 0, 0 },
162 };
163
164 static struct column *current_field = columns;
165
166 /* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to
167 * the buffer as a variable size token. A token consists of a 16 bits length
168 * field, followed by a string which is not NULL-terminated.
169 *
170 * A new chunk is allocated and linked when the current chunk doesn't have
171 * enough room to store the current token as a whole.
172 */
173 struct buf_chunk {
174 struct buf_chunk *next; /* Next chained chunk */
175 char *end; /* Current end of content */
176 char data[0];
177 };
178
179 struct buf_token {
180 uint16_t len; /* Data length, excluding length descriptor */
181 char data[0];
182 };
183
184 static struct {
185 struct buf_token *cur; /* Position of current token in chunk */
186 struct buf_chunk *head; /* First chunk */
187 struct buf_chunk *tail; /* Current chunk */
188 int chunks; /* Number of allocated chunks */
189 } buffer;
190
191 static const char *TCP_PROTO = "tcp";
192 static const char *SCTP_PROTO = "sctp";
193 static const char *UDP_PROTO = "udp";
194 static const char *RAW_PROTO = "raw";
195 static const char *dg_proto;
196
197 enum {
198 TCP_DB,
199 DCCP_DB,
200 UDP_DB,
201 RAW_DB,
202 UNIX_DG_DB,
203 UNIX_ST_DB,
204 UNIX_SQ_DB,
205 PACKET_DG_DB,
206 PACKET_R_DB,
207 NETLINK_DB,
208 SCTP_DB,
209 VSOCK_ST_DB,
210 VSOCK_DG_DB,
211 TIPC_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<<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_MAX
236 };
237
238 enum {
239 SCTP_STATE_CLOSED = 0,
240 SCTP_STATE_COOKIE_WAIT = 1,
241 SCTP_STATE_COOKIE_ECHOED = 2,
242 SCTP_STATE_ESTABLISHED = 3,
243 SCTP_STATE_SHUTDOWN_PENDING = 4,
244 SCTP_STATE_SHUTDOWN_SENT = 5,
245 SCTP_STATE_SHUTDOWN_RECEIVED = 6,
246 SCTP_STATE_SHUTDOWN_ACK_SENT = 7,
247 };
248
249 #define SS_ALL ((1 << SS_MAX) - 1)
250 #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
251 #define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE))
252
253 #include "ssfilter.h"
254
255 struct filter {
256 int dbs;
257 int states;
258 uint64_t families;
259 struct ssfilter *f;
260 bool kill;
261 struct rtnl_handle *rth_for_killing;
262 };
263
264 #define FAMILY_MASK(family) ((uint64_t)1 << (family))
265
266 static const struct filter default_dbs[MAX_DB] = {
267 [TCP_DB] = {
268 .states = SS_CONN,
269 .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
270 },
271 [DCCP_DB] = {
272 .states = SS_CONN,
273 .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
274 },
275 [UDP_DB] = {
276 .states = (1 << SS_ESTABLISHED),
277 .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
278 },
279 [RAW_DB] = {
280 .states = (1 << SS_ESTABLISHED),
281 .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
282 },
283 [UNIX_DG_DB] = {
284 .states = (1 << SS_CLOSE),
285 .families = FAMILY_MASK(AF_UNIX),
286 },
287 [UNIX_ST_DB] = {
288 .states = SS_CONN,
289 .families = FAMILY_MASK(AF_UNIX),
290 },
291 [UNIX_SQ_DB] = {
292 .states = SS_CONN,
293 .families = FAMILY_MASK(AF_UNIX),
294 },
295 [PACKET_DG_DB] = {
296 .states = (1 << SS_CLOSE),
297 .families = FAMILY_MASK(AF_PACKET),
298 },
299 [PACKET_R_DB] = {
300 .states = (1 << SS_CLOSE),
301 .families = FAMILY_MASK(AF_PACKET),
302 },
303 [NETLINK_DB] = {
304 .states = (1 << SS_CLOSE),
305 .families = FAMILY_MASK(AF_NETLINK),
306 },
307 [SCTP_DB] = {
308 .states = SS_CONN,
309 .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
310 },
311 [VSOCK_ST_DB] = {
312 .states = SS_CONN,
313 .families = FAMILY_MASK(AF_VSOCK),
314 },
315 [VSOCK_DG_DB] = {
316 .states = SS_CONN,
317 .families = FAMILY_MASK(AF_VSOCK),
318 },
319 [TIPC_DB] = {
320 .states = TIPC_SS_CONN,
321 .families = FAMILY_MASK(AF_TIPC),
322 },
323 };
324
325 static const struct filter default_afs[AF_MAX] = {
326 [AF_INET] = {
327 .dbs = INET_DBM,
328 .states = SS_CONN,
329 },
330 [AF_INET6] = {
331 .dbs = INET_DBM,
332 .states = SS_CONN,
333 },
334 [AF_UNIX] = {
335 .dbs = UNIX_DBM,
336 .states = SS_CONN,
337 },
338 [AF_PACKET] = {
339 .dbs = PACKET_DBM,
340 .states = (1 << SS_CLOSE),
341 },
342 [AF_NETLINK] = {
343 .dbs = (1 << NETLINK_DB),
344 .states = (1 << SS_CLOSE),
345 },
346 [AF_VSOCK] = {
347 .dbs = VSOCK_DBM,
348 .states = SS_CONN,
349 },
350 [AF_TIPC] = {
351 .dbs = (1 << TIPC_DB),
352 .states = TIPC_SS_CONN,
353 },
354 };
355
356 static int do_default = 1;
357 static struct filter current_filter;
358
359 static void filter_db_set(struct filter *f, int db, bool enable)
360 {
361 if (enable) {
362 f->states |= default_dbs[db].states;
363 f->dbs |= 1 << db;
364 } else {
365 f->dbs &= ~(1 << db);
366 }
367 do_default = 0;
368 }
369
370 static int filter_db_parse(struct filter *f, const char *s)
371 {
372 const struct {
373 const char *name;
374 int dbs[MAX_DB + 1];
375 } db_name_tbl[] = {
376 #define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } }
377 ENTRY(all, UDP_DB, DCCP_DB, TCP_DB, RAW_DB,
378 UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB,
379 PACKET_R_DB, PACKET_DG_DB, NETLINK_DB,
380 SCTP_DB, VSOCK_ST_DB, VSOCK_DG_DB),
381 ENTRY(inet, UDP_DB, DCCP_DB, TCP_DB, SCTP_DB, RAW_DB),
382 ENTRY(udp, UDP_DB),
383 ENTRY(dccp, DCCP_DB),
384 ENTRY(tcp, TCP_DB),
385 ENTRY(sctp, SCTP_DB),
386 ENTRY(raw, RAW_DB),
387 ENTRY(unix, UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB),
388 ENTRY(unix_stream, UNIX_ST_DB),
389 ENTRY(u_str, UNIX_ST_DB), /* alias for unix_stream */
390 ENTRY(unix_dgram, UNIX_DG_DB),
391 ENTRY(u_dgr, UNIX_DG_DB), /* alias for unix_dgram */
392 ENTRY(unix_seqpacket, UNIX_SQ_DB),
393 ENTRY(u_seq, UNIX_SQ_DB), /* alias for unix_seqpacket */
394 ENTRY(packet, PACKET_R_DB, PACKET_DG_DB),
395 ENTRY(packet_raw, PACKET_R_DB),
396 ENTRY(p_raw, PACKET_R_DB), /* alias for packet_raw */
397 ENTRY(packet_dgram, PACKET_DG_DB),
398 ENTRY(p_dgr, PACKET_DG_DB), /* alias for packet_dgram */
399 ENTRY(netlink, NETLINK_DB),
400 ENTRY(vsock, VSOCK_ST_DB, VSOCK_DG_DB),
401 ENTRY(vsock_stream, VSOCK_ST_DB),
402 ENTRY(v_str, VSOCK_ST_DB), /* alias for vsock_stream */
403 ENTRY(vsock_dgram, VSOCK_DG_DB),
404 ENTRY(v_dgr, VSOCK_DG_DB), /* alias for vsock_dgram */
405 #undef ENTRY
406 };
407 bool enable = true;
408 unsigned int i;
409 const int *dbp;
410
411 if (s[0] == '!') {
412 enable = false;
413 s++;
414 }
415 for (i = 0; i < ARRAY_SIZE(db_name_tbl); i++) {
416 if (strcmp(s, db_name_tbl[i].name))
417 continue;
418 for (dbp = db_name_tbl[i].dbs; *dbp != MAX_DB; dbp++)
419 filter_db_set(f, *dbp, enable);
420 return 0;
421 }
422 return -1;
423 }
424
425 static void filter_af_set(struct filter *f, int af)
426 {
427 f->states |= default_afs[af].states;
428 f->families |= FAMILY_MASK(af);
429 do_default = 0;
430 preferred_family = af;
431 }
432
433 static int filter_af_get(struct filter *f, int af)
434 {
435 return !!(f->families & FAMILY_MASK(af));
436 }
437
438 static void filter_states_set(struct filter *f, int states)
439 {
440 if (states)
441 f->states = states;
442 }
443
444 static void filter_merge_defaults(struct filter *f)
445 {
446 int db;
447 int af;
448
449 for (db = 0; db < MAX_DB; db++) {
450 if (!(f->dbs & (1 << db)))
451 continue;
452
453 if (!(default_dbs[db].families & f->families))
454 f->families |= default_dbs[db].families;
455 }
456 for (af = 0; af < AF_MAX; af++) {
457 if (!(f->families & FAMILY_MASK(af)))
458 continue;
459
460 if (!(default_afs[af].dbs & f->dbs))
461 f->dbs |= default_afs[af].dbs;
462 }
463 }
464
465 static FILE *generic_proc_open(const char *env, const char *name)
466 {
467 const char *p = getenv(env);
468 char store[128];
469
470 if (!p) {
471 p = getenv("PROC_ROOT") ? : "/proc";
472 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
473 p = store;
474 }
475
476 return fopen(p, "r");
477 }
478 #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp")
479 #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6")
480 #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp")
481 #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6")
482 #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw")
483 #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6")
484 #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix")
485 #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \
486 "net/packet")
487 #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \
488 "net/netlink")
489 #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \
490 "net/sockstat")
491 #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \
492 "net/sockstat6")
493 #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp")
494 #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \
495 "sys/net/ipv4/ip_local_port_range")
496
497 struct user_ent {
498 struct user_ent *next;
499 unsigned int ino;
500 int pid;
501 int fd;
502 char *process;
503 char *process_ctx;
504 char *socket_ctx;
505 };
506
507 #define USER_ENT_HASH_SIZE 256
508 static struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
509
510 static int user_ent_hashfn(unsigned int ino)
511 {
512 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
513
514 return val & (USER_ENT_HASH_SIZE - 1);
515 }
516
517 static void user_ent_add(unsigned int ino, char *process,
518 int pid, int fd,
519 char *proc_ctx,
520 char *sock_ctx)
521 {
522 struct user_ent *p, **pp;
523
524 p = malloc(sizeof(struct user_ent));
525 if (!p) {
526 fprintf(stderr, "ss: failed to malloc buffer\n");
527 abort();
528 }
529 p->next = NULL;
530 p->ino = ino;
531 p->pid = pid;
532 p->fd = fd;
533 p->process = strdup(process);
534 p->process_ctx = strdup(proc_ctx);
535 p->socket_ctx = strdup(sock_ctx);
536
537 pp = &user_ent_hash[user_ent_hashfn(ino)];
538 p->next = *pp;
539 *pp = p;
540 }
541
542 static void user_ent_destroy(void)
543 {
544 struct user_ent *p, *p_next;
545 int cnt = 0;
546
547 while (cnt != USER_ENT_HASH_SIZE) {
548 p = user_ent_hash[cnt];
549 while (p) {
550 free(p->process);
551 free(p->process_ctx);
552 free(p->socket_ctx);
553 p_next = p->next;
554 free(p);
555 p = p_next;
556 }
557 cnt++;
558 }
559 }
560
561 static void user_ent_hash_build(void)
562 {
563 const char *root = getenv("PROC_ROOT") ? : "/proc/";
564 struct dirent *d;
565 char name[1024];
566 int nameoff;
567 DIR *dir;
568 char *pid_context;
569 char *sock_context;
570 const char *no_ctx = "unavailable";
571 static int user_ent_hash_build_init;
572
573 /* If show_users & show_proc_ctx set only do this once */
574 if (user_ent_hash_build_init != 0)
575 return;
576
577 user_ent_hash_build_init = 1;
578
579 strlcpy(name, root, sizeof(name));
580
581 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
582 strcat(name, "/");
583
584 nameoff = strlen(name);
585
586 dir = opendir(name);
587 if (!dir)
588 return;
589
590 while ((d = readdir(dir)) != NULL) {
591 struct dirent *d1;
592 char process[16];
593 char *p;
594 int pid, pos;
595 DIR *dir1;
596 char crap;
597
598 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
599 continue;
600
601 if (getpidcon(pid, &pid_context) != 0)
602 pid_context = strdup(no_ctx);
603
604 snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
605 pos = strlen(name);
606 if ((dir1 = opendir(name)) == NULL) {
607 free(pid_context);
608 continue;
609 }
610
611 process[0] = '\0';
612 p = process;
613
614 while ((d1 = readdir(dir1)) != NULL) {
615 const char *pattern = "socket:[";
616 unsigned int ino;
617 char lnk[64];
618 int fd;
619 ssize_t link_len;
620 char tmp[1024];
621
622 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
623 continue;
624
625 snprintf(name+pos, sizeof(name) - pos, "%d", fd);
626
627 link_len = readlink(name, lnk, sizeof(lnk)-1);
628 if (link_len == -1)
629 continue;
630 lnk[link_len] = '\0';
631
632 if (strncmp(lnk, pattern, strlen(pattern)))
633 continue;
634
635 sscanf(lnk, "socket:[%u]", &ino);
636
637 snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
638 root, pid, d1->d_name);
639
640 if (getfilecon(tmp, &sock_context) <= 0)
641 sock_context = strdup(no_ctx);
642
643 if (*p == '\0') {
644 FILE *fp;
645
646 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
647 root, pid);
648 if ((fp = fopen(tmp, "r")) != NULL) {
649 if (fscanf(fp, "%*d (%[^)])", p) < 1)
650 ; /* ignore */
651 fclose(fp);
652 }
653 }
654 user_ent_add(ino, p, pid, fd,
655 pid_context, sock_context);
656 free(sock_context);
657 }
658 free(pid_context);
659 closedir(dir1);
660 }
661 closedir(dir);
662 }
663
664 enum entry_types {
665 USERS,
666 PROC_CTX,
667 PROC_SOCK_CTX
668 };
669
670 #define ENTRY_BUF_SIZE 512
671 static int find_entry(unsigned int ino, char **buf, int type)
672 {
673 struct user_ent *p;
674 int cnt = 0;
675 char *ptr;
676 char *new_buf;
677 int len, new_buf_len;
678 int buf_used = 0;
679 int buf_len = 0;
680
681 if (!ino)
682 return 0;
683
684 p = user_ent_hash[user_ent_hashfn(ino)];
685 ptr = *buf = NULL;
686 while (p) {
687 if (p->ino != ino)
688 goto next;
689
690 while (1) {
691 ptr = *buf + buf_used;
692 switch (type) {
693 case USERS:
694 len = snprintf(ptr, buf_len - buf_used,
695 "(\"%s\",pid=%d,fd=%d),",
696 p->process, p->pid, p->fd);
697 break;
698 case PROC_CTX:
699 len = snprintf(ptr, buf_len - buf_used,
700 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
701 p->process, p->pid,
702 p->process_ctx, p->fd);
703 break;
704 case PROC_SOCK_CTX:
705 len = snprintf(ptr, buf_len - buf_used,
706 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
707 p->process, p->pid,
708 p->process_ctx, p->fd,
709 p->socket_ctx);
710 break;
711 default:
712 fprintf(stderr, "ss: invalid type: %d\n", type);
713 abort();
714 }
715
716 if (len < 0 || len >= buf_len - buf_used) {
717 new_buf_len = buf_len + ENTRY_BUF_SIZE;
718 new_buf = realloc(*buf, new_buf_len);
719 if (!new_buf) {
720 fprintf(stderr, "ss: failed to malloc buffer\n");
721 abort();
722 }
723 *buf = new_buf;
724 buf_len = new_buf_len;
725 continue;
726 } else {
727 buf_used += len;
728 break;
729 }
730 }
731 cnt++;
732 next:
733 p = p->next;
734 }
735 if (buf_used) {
736 ptr = *buf + buf_used;
737 ptr[-1] = '\0';
738 }
739 return cnt;
740 }
741
742 static unsigned long long cookie_sk_get(const uint32_t *cookie)
743 {
744 return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
745 }
746
747 static const char *sctp_sstate_name[] = {
748 [SCTP_STATE_CLOSED] = "CLOSED",
749 [SCTP_STATE_COOKIE_WAIT] = "COOKIE_WAIT",
750 [SCTP_STATE_COOKIE_ECHOED] = "COOKIE_ECHOED",
751 [SCTP_STATE_ESTABLISHED] = "ESTAB",
752 [SCTP_STATE_SHUTDOWN_PENDING] = "SHUTDOWN_PENDING",
753 [SCTP_STATE_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
754 [SCTP_STATE_SHUTDOWN_RECEIVED] = "SHUTDOWN_RECEIVED",
755 [SCTP_STATE_SHUTDOWN_ACK_SENT] = "ACK_SENT",
756 };
757
758 static const char * const stype_nameg[] = {
759 "UNKNOWN",
760 [SOCK_STREAM] = "STREAM",
761 [SOCK_DGRAM] = "DGRAM",
762 [SOCK_RDM] = "RDM",
763 [SOCK_SEQPACKET] = "SEQPACKET",
764 };
765
766 struct sockstat {
767 struct sockstat *next;
768 unsigned int type;
769 uint16_t prot;
770 uint16_t raw_prot;
771 inet_prefix local;
772 inet_prefix remote;
773 int lport;
774 int rport;
775 int state;
776 int rq, wq;
777 unsigned int ino;
778 unsigned int uid;
779 int refcnt;
780 unsigned int iface;
781 unsigned long long sk;
782 char *name;
783 char *peer_name;
784 __u32 mark;
785 };
786
787 struct dctcpstat {
788 unsigned int ce_state;
789 unsigned int alpha;
790 unsigned int ab_ecn;
791 unsigned int ab_tot;
792 bool enabled;
793 };
794
795 struct tcpstat {
796 struct sockstat ss;
797 unsigned int timer;
798 unsigned int timeout;
799 int probes;
800 char cong_alg[16];
801 double rto, ato, rtt, rttvar;
802 int qack, ssthresh, backoff;
803 double send_bps;
804 int snd_wscale;
805 int rcv_wscale;
806 int mss;
807 int rcv_mss;
808 int advmss;
809 unsigned int pmtu;
810 unsigned int cwnd;
811 unsigned int lastsnd;
812 unsigned int lastrcv;
813 unsigned int lastack;
814 double pacing_rate;
815 double pacing_rate_max;
816 double delivery_rate;
817 unsigned long long bytes_acked;
818 unsigned long long bytes_received;
819 unsigned int segs_out;
820 unsigned int segs_in;
821 unsigned int data_segs_out;
822 unsigned int data_segs_in;
823 unsigned int unacked;
824 unsigned int retrans;
825 unsigned int retrans_total;
826 unsigned int lost;
827 unsigned int sacked;
828 unsigned int fackets;
829 unsigned int reordering;
830 unsigned int not_sent;
831 unsigned int delivered;
832 unsigned int delivered_ce;
833 unsigned int dsack_dups;
834 unsigned int reord_seen;
835 double rcv_rtt;
836 double min_rtt;
837 int rcv_space;
838 unsigned int rcv_ssthresh;
839 unsigned long long busy_time;
840 unsigned long long rwnd_limited;
841 unsigned long long sndbuf_limited;
842 unsigned long long bytes_sent;
843 unsigned long long bytes_retrans;
844 bool has_ts_opt;
845 bool has_sack_opt;
846 bool has_ecn_opt;
847 bool has_ecnseen_opt;
848 bool has_fastopen_opt;
849 bool has_wscale_opt;
850 bool app_limited;
851 struct dctcpstat *dctcp;
852 struct tcp_bbr_info *bbr_info;
853 };
854
855 /* SCTP assocs share the same inode number with their parent endpoint. So if we
856 * have seen the inode number before, it must be an assoc instead of the next
857 * endpoint. */
858 static bool is_sctp_assoc(struct sockstat *s, const char *sock_name)
859 {
860 if (strcmp(sock_name, "sctp"))
861 return false;
862 if (!sctp_ino || sctp_ino != s->ino)
863 return false;
864 return true;
865 }
866
867 static const char *unix_netid_name(int type)
868 {
869 switch (type) {
870 case SOCK_STREAM:
871 return "u_str";
872 case SOCK_SEQPACKET:
873 return "u_seq";
874 case SOCK_DGRAM:
875 default:
876 return "u_dgr";
877 }
878 }
879
880 static const char *proto_name(int protocol)
881 {
882 switch (protocol) {
883 case 0:
884 return "raw";
885 case IPPROTO_UDP:
886 return "udp";
887 case IPPROTO_TCP:
888 return "tcp";
889 case IPPROTO_SCTP:
890 return "sctp";
891 case IPPROTO_DCCP:
892 return "dccp";
893 case IPPROTO_ICMPV6:
894 return "icmp6";
895 }
896
897 return "???";
898 }
899
900 static const char *vsock_netid_name(int type)
901 {
902 switch (type) {
903 case SOCK_STREAM:
904 return "v_str";
905 case SOCK_DGRAM:
906 return "v_dgr";
907 default:
908 return "???";
909 }
910 }
911
912 static const char *tipc_netid_name(int type)
913 {
914 switch (type) {
915 case SOCK_STREAM:
916 return "ti_st";
917 case SOCK_DGRAM:
918 return "ti_dg";
919 case SOCK_RDM:
920 return "ti_rd";
921 case SOCK_SEQPACKET:
922 return "ti_sq";
923 default:
924 return "???";
925 }
926 }
927
928 /* Allocate and initialize a new buffer chunk */
929 static struct buf_chunk *buf_chunk_new(void)
930 {
931 struct buf_chunk *new = malloc(BUF_CHUNK);
932
933 if (!new)
934 abort();
935
936 new->next = NULL;
937
938 /* This is also the last block */
939 buffer.tail = new;
940
941 /* Next token will be stored at the beginning of chunk data area, and
942 * its initial length is zero.
943 */
944 buffer.cur = (struct buf_token *)new->data;
945 buffer.cur->len = 0;
946
947 new->end = buffer.cur->data;
948
949 buffer.chunks++;
950
951 return new;
952 }
953
954 /* Return available tail room in given chunk */
955 static int buf_chunk_avail(struct buf_chunk *chunk)
956 {
957 return BUF_CHUNK - offsetof(struct buf_chunk, data) -
958 (chunk->end - chunk->data);
959 }
960
961 /* Update end pointer and token length, link new chunk if we hit the end of the
962 * current one. Return -EAGAIN if we got a new chunk, caller has to print again.
963 */
964 static int buf_update(int len)
965 {
966 struct buf_chunk *chunk = buffer.tail;
967 struct buf_token *t = buffer.cur;
968
969 /* Claim success if new content fits in the current chunk, and anyway
970 * if this is the first token in the chunk: in the latter case,
971 * allocating a new chunk won't help, so we'll just cut the output.
972 */
973 if ((len < buf_chunk_avail(chunk) && len != -1 /* glibc < 2.0.6 */) ||
974 t == (struct buf_token *)chunk->data) {
975 len = min(len, buf_chunk_avail(chunk));
976
977 /* Total field length can't exceed 2^16 bytes, cut as needed */
978 len = min(len, USHRT_MAX - t->len);
979
980 chunk->end += len;
981 t->len += len;
982 return 0;
983 }
984
985 /* Content truncated, time to allocate more */
986 chunk->next = buf_chunk_new();
987
988 /* Copy current token over to new chunk, including length descriptor */
989 memcpy(chunk->next->data, t, sizeof(t->len) + t->len);
990 chunk->next->end += t->len;
991
992 /* Discard partially written field in old chunk */
993 chunk->end -= t->len + sizeof(t->len);
994
995 return -EAGAIN;
996 }
997
998 /* Append content to buffer as part of the current field */
999 __attribute__((format(printf, 1, 2)))
1000 static void out(const char *fmt, ...)
1001 {
1002 struct column *f = current_field;
1003 va_list args;
1004 char *pos;
1005 int len;
1006
1007 if (f->disabled)
1008 return;
1009
1010 if (!buffer.head)
1011 buffer.head = buf_chunk_new();
1012
1013 again: /* Append to buffer: if we have a new chunk, print again */
1014
1015 pos = buffer.cur->data + buffer.cur->len;
1016 va_start(args, fmt);
1017
1018 /* Limit to tail room. If we hit the limit, buf_update() will tell us */
1019 len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, args);
1020 va_end(args);
1021
1022 if (buf_update(len))
1023 goto again;
1024 }
1025
1026 static int print_left_spacing(struct column *f, int stored, int printed)
1027 {
1028 int s;
1029
1030 if (!f->width || f->align == ALIGN_LEFT)
1031 return 0;
1032
1033 s = f->width - stored - printed;
1034 if (f->align == ALIGN_CENTER)
1035 /* If count of total spacing is odd, shift right by one */
1036 s = (s + 1) / 2;
1037
1038 if (s > 0)
1039 return printf("%*c", s, ' ');
1040
1041 return 0;
1042 }
1043
1044 static void print_right_spacing(struct column *f, int printed)
1045 {
1046 int s;
1047
1048 if (!f->width || f->align == ALIGN_RIGHT)
1049 return;
1050
1051 s = f->width - printed;
1052 if (f->align == ALIGN_CENTER)
1053 s /= 2;
1054
1055 if (s > 0)
1056 printf("%*c", s, ' ');
1057 }
1058
1059 /* Done with field: update buffer pointer, start new token after current one */
1060 static void field_flush(struct column *f)
1061 {
1062 struct buf_chunk *chunk;
1063 unsigned int pad;
1064
1065 if (f->disabled)
1066 return;
1067
1068 chunk = buffer.tail;
1069 pad = buffer.cur->len % 2;
1070
1071 if (buffer.cur->len > f->max_len)
1072 f->max_len = buffer.cur->len;
1073
1074 /* We need a new chunk if we can't store the next length descriptor.
1075 * Mind the gap between end of previous token and next aligned position
1076 * for length descriptor.
1077 */
1078 if (buf_chunk_avail(chunk) - pad < sizeof(buffer.cur->len)) {
1079 chunk->end += pad;
1080 chunk->next = buf_chunk_new();
1081 return;
1082 }
1083
1084 buffer.cur = (struct buf_token *)(buffer.cur->data +
1085 LEN_ALIGN(buffer.cur->len));
1086 buffer.cur->len = 0;
1087 buffer.tail->end = buffer.cur->data;
1088 }
1089
1090 static int field_is_last(struct column *f)
1091 {
1092 return f - columns == COL_MAX - 1;
1093 }
1094
1095 /* Get the next available token in the buffer starting from the current token */
1096 static struct buf_token *buf_token_next(struct buf_token *cur)
1097 {
1098 struct buf_chunk *chunk = buffer.tail;
1099
1100 /* If we reached the end of chunk contents, get token from next chunk */
1101 if (cur->data + LEN_ALIGN(cur->len) == chunk->end) {
1102 buffer.tail = chunk = chunk->next;
1103 return chunk ? (struct buf_token *)chunk->data : NULL;
1104 }
1105
1106 return (struct buf_token *)(cur->data + LEN_ALIGN(cur->len));
1107 }
1108
1109 /* Free up all allocated buffer chunks */
1110 static void buf_free_all(void)
1111 {
1112 struct buf_chunk *tmp;
1113
1114 for (buffer.tail = buffer.head; buffer.tail; ) {
1115 tmp = buffer.tail;
1116 buffer.tail = buffer.tail->next;
1117 free(tmp);
1118 }
1119 buffer.head = NULL;
1120 buffer.chunks = 0;
1121 }
1122
1123 /* Get current screen width, default to 80 columns if TIOCGWINSZ fails */
1124 static int render_screen_width(void)
1125 {
1126 int width = 80;
1127
1128 if (isatty(STDOUT_FILENO)) {
1129 struct winsize w;
1130
1131 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
1132 if (w.ws_col > 0)
1133 width = w.ws_col;
1134 }
1135 }
1136
1137 return width;
1138 }
1139
1140 /* Calculate column width from contents length. If columns don't fit on one
1141 * line, break them into the least possible amount of lines and keep them
1142 * aligned across lines. Available screen space is equally spread between fields
1143 * as additional spacing.
1144 */
1145 static void render_calc_width(void)
1146 {
1147 int screen_width = render_screen_width();
1148 struct column *c, *eol = columns - 1;
1149 int first, len = 0, linecols = 0;
1150
1151 /* First pass: set width for each column to measured content length */
1152 for (first = 1, c = columns; c - columns < COL_MAX; c++) {
1153 if (c->disabled)
1154 continue;
1155
1156 if (!first && c->max_len)
1157 c->width = c->max_len + strlen(c->ldelim);
1158 else
1159 c->width = c->max_len;
1160
1161 /* But don't exceed screen size. If we exceed the screen size
1162 * for even a single field, it will just start on a line of its
1163 * own and then naturally wrap.
1164 */
1165 c->width = min(c->width, screen_width);
1166
1167 if (c->width)
1168 first = 0;
1169 }
1170
1171 /* Second pass: find out newlines and distribute available spacing */
1172 for (c = columns; c - columns < COL_MAX; c++) {
1173 int pad, spacing, rem, last;
1174 struct column *tmp;
1175
1176 if (!c->width)
1177 continue;
1178
1179 linecols++;
1180 len += c->width;
1181
1182 for (last = 1, tmp = c + 1; tmp - columns < COL_MAX; tmp++) {
1183 if (tmp->width) {
1184 last = 0;
1185 break;
1186 }
1187 }
1188
1189 if (!last && len < screen_width) {
1190 /* Columns fit on screen so far, nothing to do yet */
1191 continue;
1192 }
1193
1194 if (len == screen_width) {
1195 /* Exact fit, just start with new line */
1196 goto newline;
1197 }
1198
1199 if (len > screen_width) {
1200 /* Screen width exceeded: go back one column */
1201 len -= c->width;
1202 c--;
1203 linecols--;
1204 }
1205
1206 /* Distribute remaining space to columns on this line */
1207 pad = screen_width - len;
1208 spacing = pad / linecols;
1209 rem = pad % linecols;
1210 for (tmp = c; tmp > eol; tmp--) {
1211 if (!tmp->width)
1212 continue;
1213
1214 tmp->width += spacing;
1215 if (rem) {
1216 tmp->width++;
1217 rem--;
1218 }
1219 }
1220
1221 newline:
1222 /* Line break: reset line counters, mark end-of-line */
1223 eol = c;
1224 len = 0;
1225 linecols = 0;
1226 }
1227 }
1228
1229 /* Render buffered output with spacing and delimiters, then free up buffers */
1230 static void render(void)
1231 {
1232 struct buf_token *token;
1233 int printed, line_started = 0;
1234 struct column *f;
1235
1236 if (!buffer.head)
1237 return;
1238
1239 token = (struct buf_token *)buffer.head->data;
1240
1241 /* Ensure end alignment of last token, it wasn't necessarily flushed */
1242 buffer.tail->end += buffer.cur->len % 2;
1243
1244 render_calc_width();
1245
1246 /* Rewind and replay */
1247 buffer.tail = buffer.head;
1248
1249 f = columns;
1250 while (!f->width)
1251 f++;
1252
1253 while (token) {
1254 /* Print left delimiter only if we already started a line */
1255 if (line_started++)
1256 printed = printf("%s", f->ldelim);
1257 else
1258 printed = 0;
1259
1260 /* Print field content from token data with spacing */
1261 printed += print_left_spacing(f, token->len, printed);
1262 printed += fwrite(token->data, 1, token->len, stdout);
1263 print_right_spacing(f, printed);
1264
1265 /* Go to next non-empty field, deal with end-of-line */
1266 do {
1267 if (field_is_last(f)) {
1268 printf("\n");
1269 f = columns;
1270 line_started = 0;
1271 } else {
1272 f++;
1273 }
1274 } while (f->disabled);
1275
1276 token = buf_token_next(token);
1277 }
1278
1279 buf_free_all();
1280 current_field = columns;
1281 }
1282
1283 /* Move to next field, and render buffer if we reached the maximum number of
1284 * chunks, at the last field in a line.
1285 */
1286 static void field_next(void)
1287 {
1288 if (field_is_last(current_field) && buffer.chunks >= BUF_CHUNKS_MAX) {
1289 render();
1290 return;
1291 }
1292
1293 field_flush(current_field);
1294 if (field_is_last(current_field))
1295 current_field = columns;
1296 else
1297 current_field++;
1298 }
1299
1300 /* Walk through fields and flush them until we reach the desired one */
1301 static void field_set(enum col_id id)
1302 {
1303 while (id != current_field - columns)
1304 field_next();
1305 }
1306
1307 /* Print header for all non-empty columns */
1308 static void print_header(void)
1309 {
1310 while (!field_is_last(current_field)) {
1311 if (!current_field->disabled)
1312 out("%s", current_field->header);
1313 field_next();
1314 }
1315 }
1316
1317 static void sock_state_print(struct sockstat *s)
1318 {
1319 const char *sock_name;
1320 static const char * const sstate_name[] = {
1321 "UNKNOWN",
1322 [SS_ESTABLISHED] = "ESTAB",
1323 [SS_SYN_SENT] = "SYN-SENT",
1324 [SS_SYN_RECV] = "SYN-RECV",
1325 [SS_FIN_WAIT1] = "FIN-WAIT-1",
1326 [SS_FIN_WAIT2] = "FIN-WAIT-2",
1327 [SS_TIME_WAIT] = "TIME-WAIT",
1328 [SS_CLOSE] = "UNCONN",
1329 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
1330 [SS_LAST_ACK] = "LAST-ACK",
1331 [SS_LISTEN] = "LISTEN",
1332 [SS_CLOSING] = "CLOSING",
1333 };
1334
1335 switch (s->local.family) {
1336 case AF_UNIX:
1337 sock_name = unix_netid_name(s->type);
1338 break;
1339 case AF_INET:
1340 case AF_INET6:
1341 sock_name = proto_name(s->type);
1342 break;
1343 case AF_PACKET:
1344 sock_name = s->type == SOCK_RAW ? "p_raw" : "p_dgr";
1345 break;
1346 case AF_NETLINK:
1347 sock_name = "nl";
1348 break;
1349 case AF_TIPC:
1350 sock_name = tipc_netid_name(s->type);
1351 break;
1352 case AF_VSOCK:
1353 sock_name = vsock_netid_name(s->type);
1354 break;
1355 default:
1356 sock_name = "unknown";
1357 }
1358
1359 if (is_sctp_assoc(s, sock_name)) {
1360 field_set(COL_STATE); /* Empty Netid field */
1361 out("`- %s", sctp_sstate_name[s->state]);
1362 } else {
1363 field_set(COL_NETID);
1364 out("%s", sock_name);
1365 field_set(COL_STATE);
1366 out("%s", sstate_name[s->state]);
1367 }
1368
1369 field_set(COL_RECVQ);
1370 out("%-6d", s->rq);
1371 field_set(COL_SENDQ);
1372 out("%-6d", s->wq);
1373 field_set(COL_ADDR);
1374 }
1375
1376 static void sock_details_print(struct sockstat *s)
1377 {
1378 if (s->uid)
1379 out(" uid:%u", s->uid);
1380
1381 out(" ino:%u", s->ino);
1382 out(" sk:%llx", s->sk);
1383
1384 if (s->mark)
1385 out(" fwmark:0x%x", s->mark);
1386 }
1387
1388 static void sock_addr_print(const char *addr, char *delim, const char *port,
1389 const char *ifname)
1390 {
1391 if (ifname)
1392 out("%s" "%%" "%s%s", addr, ifname, delim);
1393 else
1394 out("%s%s", addr, delim);
1395
1396 field_next();
1397 out("%s", port);
1398 field_next();
1399 }
1400
1401 static const char *print_ms_timer(unsigned int timeout)
1402 {
1403 static char buf[64];
1404 int secs, msecs, minutes;
1405
1406 secs = timeout/1000;
1407 minutes = secs/60;
1408 secs = secs%60;
1409 msecs = timeout%1000;
1410 buf[0] = 0;
1411 if (minutes) {
1412 msecs = 0;
1413 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
1414 if (minutes > 9)
1415 secs = 0;
1416 }
1417 if (secs) {
1418 if (secs > 9)
1419 msecs = 0;
1420 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
1421 }
1422 if (msecs)
1423 sprintf(buf+strlen(buf), "%03dms", msecs);
1424 return buf;
1425 }
1426
1427 struct scache {
1428 struct scache *next;
1429 int port;
1430 char *name;
1431 const char *proto;
1432 };
1433
1434 static struct scache *rlist;
1435
1436 static void init_service_resolver(void)
1437 {
1438 char buf[128];
1439 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
1440
1441 if (!fp)
1442 return;
1443
1444 if (!fgets(buf, sizeof(buf), fp)) {
1445 pclose(fp);
1446 return;
1447 }
1448 while (fgets(buf, sizeof(buf), fp) != NULL) {
1449 unsigned int progn, port;
1450 char proto[128], prog[128] = "rpc.";
1451 struct scache *c;
1452
1453 if (sscanf(buf, "%u %*d %s %u %s",
1454 &progn, proto, &port, prog+4) != 4)
1455 continue;
1456
1457 if (!(c = malloc(sizeof(*c))))
1458 continue;
1459
1460 c->port = port;
1461 c->name = strdup(prog);
1462 if (strcmp(proto, TCP_PROTO) == 0)
1463 c->proto = TCP_PROTO;
1464 else if (strcmp(proto, UDP_PROTO) == 0)
1465 c->proto = UDP_PROTO;
1466 else if (strcmp(proto, SCTP_PROTO) == 0)
1467 c->proto = SCTP_PROTO;
1468 else
1469 c->proto = NULL;
1470 c->next = rlist;
1471 rlist = c;
1472 }
1473 pclose(fp);
1474 }
1475
1476 /* Even do not try default linux ephemeral port ranges:
1477 * default /etc/services contains so much of useless crap
1478 * wouldbe "allocated" to this area that resolution
1479 * is really harmful. I shrug each time when seeing
1480 * "socks" or "cfinger" in dumps.
1481 */
1482 static int is_ephemeral(int port)
1483 {
1484 static int min = 0, max;
1485
1486 if (!min) {
1487 FILE *f = ephemeral_ports_open();
1488
1489 if (!f || fscanf(f, "%d %d", &min, &max) < 2) {
1490 min = 1024;
1491 max = 4999;
1492 }
1493 if (f)
1494 fclose(f);
1495 }
1496 return port >= min && port <= max;
1497 }
1498
1499
1500 static const char *__resolve_service(int port)
1501 {
1502 struct scache *c;
1503
1504 for (c = rlist; c; c = c->next) {
1505 if (c->port == port && c->proto == dg_proto)
1506 return c->name;
1507 }
1508
1509 if (!is_ephemeral(port)) {
1510 static int notfirst;
1511 struct servent *se;
1512
1513 if (!notfirst) {
1514 setservent(1);
1515 notfirst = 1;
1516 }
1517 se = getservbyport(htons(port), dg_proto);
1518 if (se)
1519 return se->s_name;
1520 }
1521
1522 return NULL;
1523 }
1524
1525 #define SCACHE_BUCKETS 1024
1526 static struct scache *cache_htab[SCACHE_BUCKETS];
1527
1528 static const char *resolve_service(int port)
1529 {
1530 static char buf[128];
1531 struct scache *c;
1532 const char *res;
1533 int hash;
1534
1535 if (port == 0) {
1536 buf[0] = '*';
1537 buf[1] = 0;
1538 return buf;
1539 }
1540
1541 if (!resolve_services)
1542 goto do_numeric;
1543
1544 if (dg_proto == RAW_PROTO)
1545 return inet_proto_n2a(port, buf, sizeof(buf));
1546
1547
1548 hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS;
1549
1550 for (c = cache_htab[hash]; c; c = c->next) {
1551 if (c->port == port && c->proto == dg_proto)
1552 goto do_cache;
1553 }
1554
1555 c = malloc(sizeof(*c));
1556 if (!c)
1557 goto do_numeric;
1558 res = __resolve_service(port);
1559 c->port = port;
1560 c->name = res ? strdup(res) : NULL;
1561 c->proto = dg_proto;
1562 c->next = cache_htab[hash];
1563 cache_htab[hash] = c;
1564
1565 do_cache:
1566 if (c->name)
1567 return c->name;
1568
1569 do_numeric:
1570 sprintf(buf, "%u", port);
1571 return buf;
1572 }
1573
1574 static void inet_addr_print(const inet_prefix *a, int port,
1575 unsigned int ifindex, bool v6only)
1576 {
1577 char buf[1024];
1578 const char *ap = buf;
1579 const char *ifname = NULL;
1580
1581 if (a->family == AF_INET) {
1582 ap = format_host(AF_INET, 4, a->data);
1583 } else {
1584 if (!v6only &&
1585 !memcmp(a->data, &in6addr_any, sizeof(in6addr_any))) {
1586 buf[0] = '*';
1587 buf[1] = 0;
1588 } else {
1589 ap = format_host(a->family, 16, a->data);
1590
1591 /* Numeric IPv6 addresses should be bracketed */
1592 if (strchr(ap, ':')) {
1593 snprintf(buf, sizeof(buf),
1594 "[%s]", ap);
1595 ap = buf;
1596 }
1597 }
1598 }
1599
1600 if (ifindex)
1601 ifname = ll_index_to_name(ifindex);
1602
1603 sock_addr_print(ap, ":", resolve_service(port), ifname);
1604 }
1605
1606 struct aafilter {
1607 inet_prefix addr;
1608 int port;
1609 unsigned int iface;
1610 __u32 mark;
1611 __u32 mask;
1612 struct aafilter *next;
1613 };
1614
1615 static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
1616 int plen)
1617 {
1618 if (!inet_addr_match(a, p, plen))
1619 return 0;
1620
1621 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1622 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1623 * sockets. Fair? */
1624 if (p->family == AF_INET && a->family == AF_INET6) {
1625 if (a->data[0] == 0 && a->data[1] == 0 &&
1626 a->data[2] == htonl(0xffff)) {
1627 inet_prefix tmp = *a;
1628
1629 tmp.data[0] = a->data[3];
1630 return inet_addr_match(&tmp, p, plen);
1631 }
1632 }
1633 return 1;
1634 }
1635
1636 static int unix_match(const inet_prefix *a, const inet_prefix *p)
1637 {
1638 char *addr, *pattern;
1639
1640 memcpy(&addr, a->data, sizeof(addr));
1641 memcpy(&pattern, p->data, sizeof(pattern));
1642 if (pattern == NULL)
1643 return 1;
1644 if (addr == NULL)
1645 addr = "";
1646 return !fnmatch(pattern, addr, 0);
1647 }
1648
1649 static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
1650 {
1651 switch (f->type) {
1652 case SSF_S_AUTO:
1653 {
1654 if (s->local.family == AF_UNIX) {
1655 char *p;
1656
1657 memcpy(&p, s->local.data, sizeof(p));
1658 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
1659 strspn(p+1, "0123456789abcdef") == 5);
1660 }
1661 if (s->local.family == AF_PACKET)
1662 return s->lport == 0 && s->local.data[0] == 0;
1663 if (s->local.family == AF_NETLINK)
1664 return s->lport < 0;
1665 if (s->local.family == AF_VSOCK)
1666 return s->lport > 1023;
1667
1668 return is_ephemeral(s->lport);
1669 }
1670 case SSF_DCOND:
1671 {
1672 struct aafilter *a = (void *)f->pred;
1673
1674 if (a->addr.family == AF_UNIX)
1675 return unix_match(&s->remote, &a->addr);
1676 if (a->port != -1 && a->port != s->rport)
1677 return 0;
1678 if (a->addr.bitlen) {
1679 do {
1680 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
1681 return 1;
1682 } while ((a = a->next) != NULL);
1683 return 0;
1684 }
1685 return 1;
1686 }
1687 case SSF_SCOND:
1688 {
1689 struct aafilter *a = (void *)f->pred;
1690
1691 if (a->addr.family == AF_UNIX)
1692 return unix_match(&s->local, &a->addr);
1693 if (a->port != -1 && a->port != s->lport)
1694 return 0;
1695 if (a->addr.bitlen) {
1696 do {
1697 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
1698 return 1;
1699 } while ((a = a->next) != NULL);
1700 return 0;
1701 }
1702 return 1;
1703 }
1704 case SSF_D_GE:
1705 {
1706 struct aafilter *a = (void *)f->pred;
1707
1708 return s->rport >= a->port;
1709 }
1710 case SSF_D_LE:
1711 {
1712 struct aafilter *a = (void *)f->pred;
1713
1714 return s->rport <= a->port;
1715 }
1716 case SSF_S_GE:
1717 {
1718 struct aafilter *a = (void *)f->pred;
1719
1720 return s->lport >= a->port;
1721 }
1722 case SSF_S_LE:
1723 {
1724 struct aafilter *a = (void *)f->pred;
1725
1726 return s->lport <= a->port;
1727 }
1728 case SSF_DEVCOND:
1729 {
1730 struct aafilter *a = (void *)f->pred;
1731
1732 return s->iface == a->iface;
1733 }
1734 case SSF_MARKMASK:
1735 {
1736 struct aafilter *a = (void *)f->pred;
1737
1738 return (s->mark & a->mask) == a->mark;
1739 }
1740 /* Yup. It is recursion. Sorry. */
1741 case SSF_AND:
1742 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
1743 case SSF_OR:
1744 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
1745 case SSF_NOT:
1746 return !run_ssfilter(f->pred, s);
1747 default:
1748 abort();
1749 }
1750 }
1751
1752 /* Relocate external jumps by reloc. */
1753 static void ssfilter_patch(char *a, int len, int reloc)
1754 {
1755 while (len > 0) {
1756 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a;
1757
1758 if (op->no == len+4)
1759 op->no += reloc;
1760 len -= op->yes;
1761 a += op->yes;
1762 }
1763 if (len < 0)
1764 abort();
1765 }
1766
1767 static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
1768 {
1769 switch (f->type) {
1770 case SSF_S_AUTO:
1771 {
1772 if (!(*bytecode = malloc(4))) abort();
1773 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
1774 return 4;
1775 }
1776 case SSF_DCOND:
1777 case SSF_SCOND:
1778 {
1779 struct aafilter *a = (void *)f->pred;
1780 struct aafilter *b;
1781 char *ptr;
1782 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
1783 int len = 0;
1784
1785 for (b = a; b; b = b->next) {
1786 len += 4 + sizeof(struct inet_diag_hostcond);
1787 if (a->addr.family == AF_INET6)
1788 len += 16;
1789 else
1790 len += 4;
1791 if (b->next)
1792 len += 4;
1793 }
1794 if (!(ptr = malloc(len))) abort();
1795 *bytecode = ptr;
1796 for (b = a; b; b = b->next) {
1797 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
1798 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
1799 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
1800 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4);
1801
1802 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
1803 cond->family = a->addr.family;
1804 cond->port = a->port;
1805 cond->prefix_len = a->addr.bitlen;
1806 memcpy(cond->addr, a->addr.data, alen);
1807 ptr += oplen;
1808 if (b->next) {
1809 op = (struct inet_diag_bc_op *)ptr;
1810 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
1811 ptr += 4;
1812 }
1813 }
1814 return ptr - *bytecode;
1815 }
1816 case SSF_D_GE:
1817 {
1818 struct aafilter *x = (void *)f->pred;
1819
1820 if (!(*bytecode = malloc(8))) abort();
1821 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
1822 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1823 return 8;
1824 }
1825 case SSF_D_LE:
1826 {
1827 struct aafilter *x = (void *)f->pred;
1828
1829 if (!(*bytecode = malloc(8))) abort();
1830 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
1831 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1832 return 8;
1833 }
1834 case SSF_S_GE:
1835 {
1836 struct aafilter *x = (void *)f->pred;
1837
1838 if (!(*bytecode = malloc(8))) abort();
1839 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
1840 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1841 return 8;
1842 }
1843 case SSF_S_LE:
1844 {
1845 struct aafilter *x = (void *)f->pred;
1846
1847 if (!(*bytecode = malloc(8))) abort();
1848 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
1849 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
1850 return 8;
1851 }
1852
1853 case SSF_AND:
1854 {
1855 char *a1 = NULL, *a2 = NULL, *a;
1856 int l1, l2;
1857
1858 l1 = ssfilter_bytecompile(f->pred, &a1);
1859 l2 = ssfilter_bytecompile(f->post, &a2);
1860 if (!l1 || !l2) {
1861 free(a1);
1862 free(a2);
1863 return 0;
1864 }
1865 if (!(a = malloc(l1+l2))) abort();
1866 memcpy(a, a1, l1);
1867 memcpy(a+l1, a2, l2);
1868 free(a1); free(a2);
1869 ssfilter_patch(a, l1, l2);
1870 *bytecode = a;
1871 return l1+l2;
1872 }
1873 case SSF_OR:
1874 {
1875 char *a1 = NULL, *a2 = NULL, *a;
1876 int l1, l2;
1877
1878 l1 = ssfilter_bytecompile(f->pred, &a1);
1879 l2 = ssfilter_bytecompile(f->post, &a2);
1880 if (!l1 || !l2) {
1881 free(a1);
1882 free(a2);
1883 return 0;
1884 }
1885 if (!(a = malloc(l1+l2+4))) abort();
1886 memcpy(a, a1, l1);
1887 memcpy(a+l1+4, a2, l2);
1888 free(a1); free(a2);
1889 *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
1890 *bytecode = a;
1891 return l1+l2+4;
1892 }
1893 case SSF_NOT:
1894 {
1895 char *a1 = NULL, *a;
1896 int l1;
1897
1898 l1 = ssfilter_bytecompile(f->pred, &a1);
1899 if (!l1) {
1900 free(a1);
1901 return 0;
1902 }
1903 if (!(a = malloc(l1+4))) abort();
1904 memcpy(a, a1, l1);
1905 free(a1);
1906 *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
1907 *bytecode = a;
1908 return l1+4;
1909 }
1910 case SSF_DEVCOND:
1911 {
1912 /* bytecompile for SSF_DEVCOND not supported yet */
1913 return 0;
1914 }
1915 case SSF_MARKMASK:
1916 {
1917 struct aafilter *a = (void *)f->pred;
1918 struct instr {
1919 struct inet_diag_bc_op op;
1920 struct inet_diag_markcond cond;
1921 };
1922 int inslen = sizeof(struct instr);
1923
1924 if (!(*bytecode = malloc(inslen))) abort();
1925 ((struct instr *)*bytecode)[0] = (struct instr) {
1926 { INET_DIAG_BC_MARK_COND, inslen, inslen + 4 },
1927 { a->mark, a->mask},
1928 };
1929
1930 return inslen;
1931 }
1932 default:
1933 abort();
1934 }
1935 }
1936
1937 static int remember_he(struct aafilter *a, struct hostent *he)
1938 {
1939 char **ptr = he->h_addr_list;
1940 int cnt = 0;
1941 int len;
1942
1943 if (he->h_addrtype == AF_INET)
1944 len = 4;
1945 else if (he->h_addrtype == AF_INET6)
1946 len = 16;
1947 else
1948 return 0;
1949
1950 while (*ptr) {
1951 struct aafilter *b = a;
1952
1953 if (a->addr.bitlen) {
1954 if ((b = malloc(sizeof(*b))) == NULL)
1955 return cnt;
1956 *b = *a;
1957 a->next = b;
1958 }
1959 memcpy(b->addr.data, *ptr, len);
1960 b->addr.bytelen = len;
1961 b->addr.bitlen = len*8;
1962 b->addr.family = he->h_addrtype;
1963 ptr++;
1964 cnt++;
1965 }
1966 return cnt;
1967 }
1968
1969 static int get_dns_host(struct aafilter *a, const char *addr, int fam)
1970 {
1971 static int notfirst;
1972 int cnt = 0;
1973 struct hostent *he;
1974
1975 a->addr.bitlen = 0;
1976 if (!notfirst) {
1977 sethostent(1);
1978 notfirst = 1;
1979 }
1980 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
1981 if (he)
1982 cnt = remember_he(a, he);
1983 if (fam == AF_UNSPEC) {
1984 he = gethostbyname2(addr, AF_INET6);
1985 if (he)
1986 cnt += remember_he(a, he);
1987 }
1988 return !cnt;
1989 }
1990
1991 static int xll_initted;
1992
1993 static void xll_init(void)
1994 {
1995 struct rtnl_handle rth;
1996
1997 if (rtnl_open(&rth, 0) < 0)
1998 exit(1);
1999
2000 ll_init_map(&rth);
2001 rtnl_close(&rth);
2002 xll_initted = 1;
2003 }
2004
2005 static const char *xll_index_to_name(int index)
2006 {
2007 if (!xll_initted)
2008 xll_init();
2009 return ll_index_to_name(index);
2010 }
2011
2012 static int xll_name_to_index(const char *dev)
2013 {
2014 if (!xll_initted)
2015 xll_init();
2016 return ll_name_to_index(dev);
2017 }
2018
2019 void *parse_devcond(char *name)
2020 {
2021 struct aafilter a = { .iface = 0 };
2022 struct aafilter *res;
2023
2024 a.iface = xll_name_to_index(name);
2025 if (a.iface == 0) {
2026 char *end;
2027 unsigned long n;
2028
2029 n = strtoul(name, &end, 0);
2030 if (!end || end == name || *end || n > UINT_MAX)
2031 return NULL;
2032
2033 a.iface = n;
2034 }
2035
2036 res = malloc(sizeof(*res));
2037 *res = a;
2038
2039 return res;
2040 }
2041
2042 static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid)
2043 {
2044 *a = (inet_prefix){
2045 .bytelen = sizeof(cid),
2046 .family = AF_VSOCK,
2047 };
2048 memcpy(a->data, &cid, sizeof(cid));
2049 }
2050
2051 void *parse_hostcond(char *addr, bool is_port)
2052 {
2053 char *port = NULL;
2054 struct aafilter a = { .port = -1 };
2055 struct aafilter *res;
2056 int fam = preferred_family;
2057 struct filter *f = &current_filter;
2058
2059 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
2060 char *p;
2061
2062 a.addr.family = AF_UNIX;
2063 if (strncmp(addr, "unix:", 5) == 0)
2064 addr += 5;
2065 p = strdup(addr);
2066 a.addr.bitlen = 8*strlen(p);
2067 memcpy(a.addr.data, &p, sizeof(p));
2068 fam = AF_UNIX;
2069 goto out;
2070 }
2071
2072 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
2073 a.addr.family = AF_PACKET;
2074 a.addr.bitlen = 0;
2075 if (strncmp(addr, "link:", 5) == 0)
2076 addr += 5;
2077 port = strchr(addr, ':');
2078 if (port) {
2079 *port = 0;
2080 if (port[1] && strcmp(port+1, "*")) {
2081 if (get_integer(&a.port, port+1, 0)) {
2082 if ((a.port = xll_name_to_index(port+1)) <= 0)
2083 return NULL;
2084 }
2085 }
2086 }
2087 if (addr[0] && strcmp(addr, "*")) {
2088 unsigned short tmp;
2089
2090 a.addr.bitlen = 32;
2091 if (ll_proto_a2n(&tmp, addr))
2092 return NULL;
2093 a.addr.data[0] = ntohs(tmp);
2094 }
2095 fam = AF_PACKET;
2096 goto out;
2097 }
2098
2099 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
2100 a.addr.family = AF_NETLINK;
2101 a.addr.bitlen = 0;
2102 if (strncmp(addr, "netlink:", 8) == 0)
2103 addr += 8;
2104 port = strchr(addr, ':');
2105 if (port) {
2106 *port = 0;
2107 if (port[1] && strcmp(port+1, "*")) {
2108 if (get_integer(&a.port, port+1, 0)) {
2109 if (strcmp(port+1, "kernel") == 0)
2110 a.port = 0;
2111 else
2112 return NULL;
2113 }
2114 }
2115 }
2116 if (addr[0] && strcmp(addr, "*")) {
2117 a.addr.bitlen = 32;
2118 if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
2119 return NULL;
2120 }
2121 fam = AF_NETLINK;
2122 goto out;
2123 }
2124
2125 if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
2126 __u32 cid = ~(__u32)0;
2127
2128 a.addr.family = AF_VSOCK;
2129 if (strncmp(addr, "vsock:", 6) == 0)
2130 addr += 6;
2131
2132 if (is_port)
2133 port = addr;
2134 else {
2135 port = strchr(addr, ':');
2136 if (port) {
2137 *port = '\0';
2138 port++;
2139 }
2140 }
2141
2142 if (port && strcmp(port, "*") &&
2143 get_u32((__u32 *)&a.port, port, 0))
2144 return NULL;
2145
2146 if (addr[0] && strcmp(addr, "*")) {
2147 a.addr.bitlen = 32;
2148 if (get_u32(&cid, addr, 0))
2149 return NULL;
2150 }
2151 vsock_set_inet_prefix(&a.addr, cid);
2152 fam = AF_VSOCK;
2153 goto out;
2154 }
2155
2156 if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
2157 fam = AF_INET;
2158 if (!strncmp(addr, "inet:", 5))
2159 addr += 5;
2160 } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
2161 fam = AF_INET6;
2162 if (!strncmp(addr, "inet6:", 6))
2163 addr += 6;
2164 }
2165
2166 /* URL-like literal [] */
2167 if (addr[0] == '[') {
2168 addr++;
2169 if ((port = strchr(addr, ']')) == NULL)
2170 return NULL;
2171 *port++ = 0;
2172 } else if (addr[0] == '*') {
2173 port = addr+1;
2174 } else {
2175 port = strrchr(strchr(addr, '/') ? : addr, ':');
2176 }
2177
2178 if (is_port)
2179 port = addr;
2180
2181 if (port && *port) {
2182 if (*port == ':')
2183 *port++ = 0;
2184
2185 if (*port && *port != '*') {
2186 if (get_integer(&a.port, port, 0)) {
2187 struct servent *se1 = NULL;
2188 struct servent *se2 = NULL;
2189
2190 if (current_filter.dbs&(1<<UDP_DB))
2191 se1 = getservbyname(port, UDP_PROTO);
2192 if (current_filter.dbs&(1<<TCP_DB))
2193 se2 = getservbyname(port, TCP_PROTO);
2194 if (se1 && se2 && se1->s_port != se2->s_port) {
2195 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
2196 return NULL;
2197 }
2198 if (!se1)
2199 se1 = se2;
2200 if (se1) {
2201 a.port = ntohs(se1->s_port);
2202 } else {
2203 struct scache *s;
2204
2205 for (s = rlist; s; s = s->next) {
2206 if ((s->proto == UDP_PROTO &&
2207 (current_filter.dbs&(1<<UDP_DB))) ||
2208 (s->proto == TCP_PROTO &&
2209 (current_filter.dbs&(1<<TCP_DB)))) {
2210 if (s->name && strcmp(s->name, port) == 0) {
2211 if (a.port > 0 && a.port != s->port) {
2212 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
2213 return NULL;
2214 }
2215 a.port = s->port;
2216 }
2217 }
2218 }
2219 if (a.port <= 0) {
2220 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
2221 return NULL;
2222 }
2223 }
2224 }
2225 }
2226 }
2227 if (!is_port && *addr && *addr != '*') {
2228 if (get_prefix_1(&a.addr, addr, fam)) {
2229 if (get_dns_host(&a, addr, fam)) {
2230 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
2231 return NULL;
2232 }
2233 }
2234 }
2235
2236 out:
2237 if (fam != AF_UNSPEC) {
2238 int states = f->states;
2239 f->families = 0;
2240 filter_af_set(f, fam);
2241 filter_states_set(f, states);
2242 }
2243
2244 res = malloc(sizeof(*res));
2245 if (res)
2246 memcpy(res, &a, sizeof(a));
2247 return res;
2248 }
2249
2250 void *parse_markmask(const char *markmask)
2251 {
2252 struct aafilter a, *res;
2253
2254 if (strchr(markmask, '/')) {
2255 if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2)
2256 return NULL;
2257 } else {
2258 a.mask = 0xffffffff;
2259 if (sscanf(markmask, "%i", &a.mark) != 1)
2260 return NULL;
2261 }
2262
2263 res = malloc(sizeof(*res));
2264 if (res)
2265 memcpy(res, &a, sizeof(a));
2266 return res;
2267 }
2268
2269 static void proc_ctx_print(struct sockstat *s)
2270 {
2271 char *buf;
2272
2273 if (show_proc_ctx || show_sock_ctx) {
2274 if (find_entry(s->ino, &buf,
2275 (show_proc_ctx & show_sock_ctx) ?
2276 PROC_SOCK_CTX : PROC_CTX) > 0) {
2277 out(" users:(%s)", buf);
2278 free(buf);
2279 }
2280 } else if (show_users) {
2281 if (find_entry(s->ino, &buf, USERS) > 0) {
2282 out(" users:(%s)", buf);
2283 free(buf);
2284 }
2285 }
2286 }
2287
2288 static void inet_stats_print(struct sockstat *s, bool v6only)
2289 {
2290 sock_state_print(s);
2291
2292 inet_addr_print(&s->local, s->lport, s->iface, v6only);
2293 inet_addr_print(&s->remote, s->rport, 0, v6only);
2294
2295 proc_ctx_print(s);
2296 }
2297
2298 static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
2299 sockstat * s)
2300 {
2301 s->local.family = s->remote.family = family;
2302 if (family == AF_INET) {
2303 sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
2304 sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
2305 s->local.bytelen = s->remote.bytelen = 4;
2306 return 0;
2307 } else {
2308 sscanf(loc, "%08x%08x%08x%08x:%x",
2309 s->local.data,
2310 s->local.data + 1,
2311 s->local.data + 2,
2312 s->local.data + 3,
2313 &s->lport);
2314 sscanf(rem, "%08x%08x%08x%08x:%x",
2315 s->remote.data,
2316 s->remote.data + 1,
2317 s->remote.data + 2,
2318 s->remote.data + 3,
2319 &s->rport);
2320 s->local.bytelen = s->remote.bytelen = 16;
2321 return 0;
2322 }
2323 return -1;
2324 }
2325
2326 static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
2327 {
2328 char *p;
2329
2330 if ((p = strchr(line, ':')) == NULL)
2331 return -1;
2332
2333 *loc = p+2;
2334 if ((p = strchr(*loc, ':')) == NULL)
2335 return -1;
2336
2337 p[5] = 0;
2338 *rem = p+6;
2339 if ((p = strchr(*rem, ':')) == NULL)
2340 return -1;
2341
2342 p[5] = 0;
2343 *data = p+6;
2344 return 0;
2345 }
2346
2347 static char *sprint_bw(char *buf, double bw)
2348 {
2349 if (bw > 1000000.)
2350 sprintf(buf, "%.1fM", bw / 1000000.);
2351 else if (bw > 1000.)
2352 sprintf(buf, "%.1fK", bw / 1000.);
2353 else
2354 sprintf(buf, "%g", bw);
2355
2356 return buf;
2357 }
2358
2359 static void sctp_stats_print(struct sctp_info *s)
2360 {
2361 if (s->sctpi_tag)
2362 out(" tag:%x", s->sctpi_tag);
2363 if (s->sctpi_state)
2364 out(" state:%s", sctp_sstate_name[s->sctpi_state]);
2365 if (s->sctpi_rwnd)
2366 out(" rwnd:%d", s->sctpi_rwnd);
2367 if (s->sctpi_unackdata)
2368 out(" unackdata:%d", s->sctpi_unackdata);
2369 if (s->sctpi_penddata)
2370 out(" penddata:%d", s->sctpi_penddata);
2371 if (s->sctpi_instrms)
2372 out(" instrms:%d", s->sctpi_instrms);
2373 if (s->sctpi_outstrms)
2374 out(" outstrms:%d", s->sctpi_outstrms);
2375 if (s->sctpi_inqueue)
2376 out(" inqueue:%d", s->sctpi_inqueue);
2377 if (s->sctpi_outqueue)
2378 out(" outqueue:%d", s->sctpi_outqueue);
2379 if (s->sctpi_overall_error)
2380 out(" overerr:%d", s->sctpi_overall_error);
2381 if (s->sctpi_max_burst)
2382 out(" maxburst:%d", s->sctpi_max_burst);
2383 if (s->sctpi_maxseg)
2384 out(" maxseg:%d", s->sctpi_maxseg);
2385 if (s->sctpi_peer_rwnd)
2386 out(" prwnd:%d", s->sctpi_peer_rwnd);
2387 if (s->sctpi_peer_tag)
2388 out(" ptag:%x", s->sctpi_peer_tag);
2389 if (s->sctpi_peer_capable)
2390 out(" pcapable:%d", s->sctpi_peer_capable);
2391 if (s->sctpi_peer_sack)
2392 out(" psack:%d", s->sctpi_peer_sack);
2393 if (s->sctpi_s_autoclose)
2394 out(" autoclose:%d", s->sctpi_s_autoclose);
2395 if (s->sctpi_s_adaptation_ind)
2396 out(" adapind:%d", s->sctpi_s_adaptation_ind);
2397 if (s->sctpi_s_pd_point)
2398 out(" pdpoint:%d", s->sctpi_s_pd_point);
2399 if (s->sctpi_s_nodelay)
2400 out(" nodealy:%d", s->sctpi_s_nodelay);
2401 if (s->sctpi_s_disable_fragments)
2402 out(" nofrag:%d", s->sctpi_s_disable_fragments);
2403 if (s->sctpi_s_v4mapped)
2404 out(" v4mapped:%d", s->sctpi_s_v4mapped);
2405 if (s->sctpi_s_frag_interleave)
2406 out(" fraginl:%d", s->sctpi_s_frag_interleave);
2407 }
2408
2409 static void tcp_stats_print(struct tcpstat *s)
2410 {
2411 char b1[64];
2412
2413 if (s->has_ts_opt)
2414 out(" ts");
2415 if (s->has_sack_opt)
2416 out(" sack");
2417 if (s->has_ecn_opt)
2418 out(" ecn");
2419 if (s->has_ecnseen_opt)
2420 out(" ecnseen");
2421 if (s->has_fastopen_opt)
2422 out(" fastopen");
2423 if (s->cong_alg[0])
2424 out(" %s", s->cong_alg);
2425 if (s->has_wscale_opt)
2426 out(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale);
2427 if (s->rto)
2428 out(" rto:%g", s->rto);
2429 if (s->backoff)
2430 out(" backoff:%u", s->backoff);
2431 if (s->rtt)
2432 out(" rtt:%g/%g", s->rtt, s->rttvar);
2433 if (s->ato)
2434 out(" ato:%g", s->ato);
2435
2436 if (s->qack)
2437 out(" qack:%d", s->qack);
2438 if (s->qack & 1)
2439 out(" bidir");
2440
2441 if (s->mss)
2442 out(" mss:%d", s->mss);
2443 if (s->pmtu)
2444 out(" pmtu:%u", s->pmtu);
2445 if (s->rcv_mss)
2446 out(" rcvmss:%d", s->rcv_mss);
2447 if (s->advmss)
2448 out(" advmss:%d", s->advmss);
2449 if (s->cwnd)
2450 out(" cwnd:%u", s->cwnd);
2451 if (s->ssthresh)
2452 out(" ssthresh:%d", s->ssthresh);
2453
2454 if (s->bytes_sent)
2455 out(" bytes_sent:%llu", s->bytes_sent);
2456 if (s->bytes_retrans)
2457 out(" bytes_retrans:%llu", s->bytes_retrans);
2458 if (s->bytes_acked)
2459 out(" bytes_acked:%llu", s->bytes_acked);
2460 if (s->bytes_received)
2461 out(" bytes_received:%llu", s->bytes_received);
2462 if (s->segs_out)
2463 out(" segs_out:%u", s->segs_out);
2464 if (s->segs_in)
2465 out(" segs_in:%u", s->segs_in);
2466 if (s->data_segs_out)
2467 out(" data_segs_out:%u", s->data_segs_out);
2468 if (s->data_segs_in)
2469 out(" data_segs_in:%u", s->data_segs_in);
2470
2471 if (s->dctcp && s->dctcp->enabled) {
2472 struct dctcpstat *dctcp = s->dctcp;
2473
2474 out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
2475 dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
2476 dctcp->ab_tot);
2477 } else if (s->dctcp) {
2478 out(" dctcp:fallback_mode");
2479 }
2480
2481 if (s->bbr_info) {
2482 __u64 bw;
2483
2484 bw = s->bbr_info->bbr_bw_hi;
2485 bw <<= 32;
2486 bw |= s->bbr_info->bbr_bw_lo;
2487
2488 out(" bbr:(bw:%sbps,mrtt:%g",
2489 sprint_bw(b1, bw * 8.0),
2490 (double)s->bbr_info->bbr_min_rtt / 1000.0);
2491 if (s->bbr_info->bbr_pacing_gain)
2492 out(",pacing_gain:%g",
2493 (double)s->bbr_info->bbr_pacing_gain / 256.0);
2494 if (s->bbr_info->bbr_cwnd_gain)
2495 out(",cwnd_gain:%g",
2496 (double)s->bbr_info->bbr_cwnd_gain / 256.0);
2497 out(")");
2498 }
2499
2500 if (s->send_bps)
2501 out(" send %sbps", sprint_bw(b1, s->send_bps));
2502 if (s->lastsnd)
2503 out(" lastsnd:%u", s->lastsnd);
2504 if (s->lastrcv)
2505 out(" lastrcv:%u", s->lastrcv);
2506 if (s->lastack)
2507 out(" lastack:%u", s->lastack);
2508
2509 if (s->pacing_rate) {
2510 out(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate));
2511 if (s->pacing_rate_max)
2512 out("/%sbps", sprint_bw(b1, s->pacing_rate_max));
2513 }
2514
2515 if (s->delivery_rate)
2516 out(" delivery_rate %sbps", sprint_bw(b1, s->delivery_rate));
2517 if (s->delivered)
2518 out(" delivered:%u", s->delivered);
2519 if (s->delivered_ce)
2520 out(" delivered_ce:%u", s->delivered_ce);
2521 if (s->app_limited)
2522 out(" app_limited");
2523
2524 if (s->busy_time) {
2525 out(" busy:%llums", s->busy_time / 1000);
2526 if (s->rwnd_limited)
2527 out(" rwnd_limited:%llums(%.1f%%)",
2528 s->rwnd_limited / 1000,
2529 100.0 * s->rwnd_limited / s->busy_time);
2530 if (s->sndbuf_limited)
2531 out(" sndbuf_limited:%llums(%.1f%%)",
2532 s->sndbuf_limited / 1000,
2533 100.0 * s->sndbuf_limited / s->busy_time);
2534 }
2535
2536 if (s->unacked)
2537 out(" unacked:%u", s->unacked);
2538 if (s->retrans || s->retrans_total)
2539 out(" retrans:%u/%u", s->retrans, s->retrans_total);
2540 if (s->lost)
2541 out(" lost:%u", s->lost);
2542 if (s->sacked && s->ss.state != SS_LISTEN)
2543 out(" sacked:%u", s->sacked);
2544 if (s->dsack_dups)
2545 out(" dsack_dups:%u", s->dsack_dups);
2546 if (s->fackets)
2547 out(" fackets:%u", s->fackets);
2548 if (s->reordering != 3)
2549 out(" reordering:%d", s->reordering);
2550 if (s->reord_seen)
2551 out(" reord_seen:%d", s->reord_seen);
2552 if (s->rcv_rtt)
2553 out(" rcv_rtt:%g", s->rcv_rtt);
2554 if (s->rcv_space)
2555 out(" rcv_space:%d", s->rcv_space);
2556 if (s->rcv_ssthresh)
2557 out(" rcv_ssthresh:%u", s->rcv_ssthresh);
2558 if (s->not_sent)
2559 out(" notsent:%u", s->not_sent);
2560 if (s->min_rtt)
2561 out(" minrtt:%g", s->min_rtt);
2562 }
2563
2564 static void tcp_timer_print(struct tcpstat *s)
2565 {
2566 static const char * const tmr_name[] = {
2567 "off",
2568 "on",
2569 "keepalive",
2570 "timewait",
2571 "persist",
2572 "unknown"
2573 };
2574
2575 if (s->timer) {
2576 if (s->timer > 4)
2577 s->timer = 5;
2578 out(" timer:(%s,%s,%d)",
2579 tmr_name[s->timer],
2580 print_ms_timer(s->timeout),
2581 s->retrans);
2582 }
2583 }
2584
2585 static void sctp_timer_print(struct tcpstat *s)
2586 {
2587 if (s->timer)
2588 out(" timer:(T3_RTX,%s,%d)",
2589 print_ms_timer(s->timeout), s->retrans);
2590 }
2591
2592 static int tcp_show_line(char *line, const struct filter *f, int family)
2593 {
2594 int rto = 0, ato = 0;
2595 struct tcpstat s = {};
2596 char *loc, *rem, *data;
2597 char opt[256];
2598 int n;
2599 int hz = get_user_hz();
2600
2601 if (proc_inet_split_line(line, &loc, &rem, &data))
2602 return -1;
2603
2604 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
2605
2606 if (!(f->states & (1 << state)))
2607 return 0;
2608
2609 proc_parse_inet_addr(loc, rem, family, &s.ss);
2610
2611 if (f->f && run_ssfilter(f->f, &s.ss) == 0)
2612 return 0;
2613
2614 opt[0] = 0;
2615 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n",
2616 &s.ss.state, &s.ss.wq, &s.ss.rq,
2617 &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes,
2618 &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd,
2619 &s.ssthresh, opt);
2620
2621 if (n < 17)
2622 opt[0] = 0;
2623
2624 if (n < 12) {
2625 rto = 0;
2626 s.cwnd = 2;
2627 s.ssthresh = -1;
2628 ato = s.qack = 0;
2629 }
2630
2631 s.retrans = s.timer != 1 ? s.probes : s.retrans;
2632 s.timeout = (s.timeout * 1000 + hz - 1) / hz;
2633 s.ato = (double)ato / hz;
2634 s.qack /= 2;
2635 s.rto = (double)rto;
2636 s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh;
2637 s.rto = s.rto != 3 * hz ? s.rto / hz : 0;
2638 s.ss.type = IPPROTO_TCP;
2639
2640 inet_stats_print(&s.ss, false);
2641
2642 if (show_options)
2643 tcp_timer_print(&s);
2644
2645 if (show_details) {
2646 sock_details_print(&s.ss);
2647 if (opt[0])
2648 out(" opt:\"%s\"", opt);
2649 }
2650
2651 if (show_tcpinfo)
2652 tcp_stats_print(&s);
2653
2654 return 0;
2655 }
2656
2657 static int generic_record_read(FILE *fp,
2658 int (*worker)(char*, const struct filter *, int),
2659 const struct filter *f, int fam)
2660 {
2661 char line[256];
2662
2663 /* skip header */
2664 if (fgets(line, sizeof(line), fp) == NULL)
2665 goto outerr;
2666
2667 while (fgets(line, sizeof(line), fp) != NULL) {
2668 int n = strlen(line);
2669
2670 if (n == 0 || line[n-1] != '\n') {
2671 errno = -EINVAL;
2672 return -1;
2673 }
2674 line[n-1] = 0;
2675
2676 if (worker(line, f, fam) < 0)
2677 return 0;
2678 }
2679 outerr:
2680
2681 return ferror(fp) ? -1 : 0;
2682 }
2683
2684 static void print_skmeminfo(struct rtattr *tb[], int attrtype)
2685 {
2686 const __u32 *skmeminfo;
2687
2688 if (!tb[attrtype]) {
2689 if (attrtype == INET_DIAG_SKMEMINFO) {
2690 if (!tb[INET_DIAG_MEMINFO])
2691 return;
2692
2693 const struct inet_diag_meminfo *minfo =
2694 RTA_DATA(tb[INET_DIAG_MEMINFO]);
2695
2696 out(" mem:(r%u,w%u,f%u,t%u)",
2697 minfo->idiag_rmem,
2698 minfo->idiag_wmem,
2699 minfo->idiag_fmem,
2700 minfo->idiag_tmem);
2701 }
2702 return;
2703 }
2704
2705 skmeminfo = RTA_DATA(tb[attrtype]);
2706
2707 out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
2708 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
2709 skmeminfo[SK_MEMINFO_RCVBUF],
2710 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
2711 skmeminfo[SK_MEMINFO_SNDBUF],
2712 skmeminfo[SK_MEMINFO_FWD_ALLOC],
2713 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
2714 skmeminfo[SK_MEMINFO_OPTMEM]);
2715
2716 if (RTA_PAYLOAD(tb[attrtype]) >=
2717 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
2718 out(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
2719
2720 if (RTA_PAYLOAD(tb[attrtype]) >=
2721 (SK_MEMINFO_DROPS + 1) * sizeof(__u32))
2722 out(",d%u", skmeminfo[SK_MEMINFO_DROPS]);
2723
2724 out(")");
2725 }
2726
2727 static void print_md5sig(struct tcp_diag_md5sig *sig)
2728 {
2729 out("%s/%d=",
2730 format_host(sig->tcpm_family,
2731 sig->tcpm_family == AF_INET6 ? 16 : 4,
2732 &sig->tcpm_addr),
2733 sig->tcpm_prefixlen);
2734 print_escape_buf(sig->tcpm_key, sig->tcpm_keylen, " ,");
2735 }
2736
2737 #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
2738
2739 static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
2740 struct rtattr *tb[])
2741 {
2742 double rtt = 0;
2743 struct tcpstat s = {};
2744
2745 s.ss.state = r->idiag_state;
2746
2747 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
2748
2749 if (tb[INET_DIAG_INFO]) {
2750 struct tcp_info *info;
2751 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
2752
2753 /* workaround for older kernels with less fields */
2754 if (len < sizeof(*info)) {
2755 info = alloca(sizeof(*info));
2756 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
2757 memset((char *)info + len, 0, sizeof(*info) - len);
2758 } else
2759 info = RTA_DATA(tb[INET_DIAG_INFO]);
2760
2761 if (show_options) {
2762 s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS);
2763 s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK);
2764 s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN);
2765 s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN);
2766 s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA);
2767 }
2768
2769 if (tb[INET_DIAG_CONG])
2770 strncpy(s.cong_alg,
2771 rta_getattr_str(tb[INET_DIAG_CONG]),
2772 sizeof(s.cong_alg) - 1);
2773
2774 if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) {
2775 s.has_wscale_opt = true;
2776 s.snd_wscale = info->tcpi_snd_wscale;
2777 s.rcv_wscale = info->tcpi_rcv_wscale;
2778 }
2779
2780 if (info->tcpi_rto && info->tcpi_rto != 3000000)
2781 s.rto = (double)info->tcpi_rto / 1000;
2782
2783 s.backoff = info->tcpi_backoff;
2784 s.rtt = (double)info->tcpi_rtt / 1000;
2785 s.rttvar = (double)info->tcpi_rttvar / 1000;
2786 s.ato = (double)info->tcpi_ato / 1000;
2787 s.mss = info->tcpi_snd_mss;
2788 s.rcv_mss = info->tcpi_rcv_mss;
2789 s.advmss = info->tcpi_advmss;
2790 s.rcv_space = info->tcpi_rcv_space;
2791 s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000;
2792 s.lastsnd = info->tcpi_last_data_sent;
2793 s.lastrcv = info->tcpi_last_data_recv;
2794 s.lastack = info->tcpi_last_ack_recv;
2795 s.unacked = info->tcpi_unacked;
2796 s.retrans = info->tcpi_retrans;
2797 s.retrans_total = info->tcpi_total_retrans;
2798 s.lost = info->tcpi_lost;
2799 s.sacked = info->tcpi_sacked;
2800 s.fackets = info->tcpi_fackets;
2801 s.reordering = info->tcpi_reordering;
2802 s.rcv_ssthresh = info->tcpi_rcv_ssthresh;
2803 s.cwnd = info->tcpi_snd_cwnd;
2804 s.pmtu = info->tcpi_pmtu;
2805
2806 if (info->tcpi_snd_ssthresh < 0xFFFF)
2807 s.ssthresh = info->tcpi_snd_ssthresh;
2808
2809 rtt = (double) info->tcpi_rtt;
2810 if (tb[INET_DIAG_VEGASINFO]) {
2811 const struct tcpvegas_info *vinfo
2812 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
2813
2814 if (vinfo->tcpv_enabled &&
2815 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
2816 rtt = vinfo->tcpv_rtt;
2817 }
2818
2819 if (tb[INET_DIAG_DCTCPINFO]) {
2820 struct dctcpstat *dctcp = malloc(sizeof(struct
2821 dctcpstat));
2822
2823 const struct tcp_dctcp_info *dinfo
2824 = RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
2825
2826 dctcp->enabled = !!dinfo->dctcp_enabled;
2827 dctcp->ce_state = dinfo->dctcp_ce_state;
2828 dctcp->alpha = dinfo->dctcp_alpha;
2829 dctcp->ab_ecn = dinfo->dctcp_ab_ecn;
2830 dctcp->ab_tot = dinfo->dctcp_ab_tot;
2831 s.dctcp = dctcp;
2832 }
2833
2834 if (tb[INET_DIAG_BBRINFO]) {
2835 const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]);
2836 int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]),
2837 sizeof(*s.bbr_info));
2838
2839 s.bbr_info = calloc(1, sizeof(*s.bbr_info));
2840 if (s.bbr_info && bbr_info)
2841 memcpy(s.bbr_info, bbr_info, len);
2842 }
2843
2844 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
2845 s.send_bps = (double) info->tcpi_snd_cwnd *
2846 (double)info->tcpi_snd_mss * 8000000. / rtt;
2847 }
2848
2849 if (info->tcpi_pacing_rate &&
2850 info->tcpi_pacing_rate != ~0ULL) {
2851 s.pacing_rate = info->tcpi_pacing_rate * 8.;
2852
2853 if (info->tcpi_max_pacing_rate &&
2854 info->tcpi_max_pacing_rate != ~0ULL)
2855 s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
2856 }
2857 s.bytes_acked = info->tcpi_bytes_acked;
2858 s.bytes_received = info->tcpi_bytes_received;
2859 s.segs_out = info->tcpi_segs_out;
2860 s.segs_in = info->tcpi_segs_in;
2861 s.data_segs_out = info->tcpi_data_segs_out;
2862 s.data_segs_in = info->tcpi_data_segs_in;
2863 s.not_sent = info->tcpi_notsent_bytes;
2864 if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U)
2865 s.min_rtt = (double) info->tcpi_min_rtt / 1000;
2866 s.delivery_rate = info->tcpi_delivery_rate * 8.;
2867 s.app_limited = info->tcpi_delivery_rate_app_limited;
2868 s.busy_time = info->tcpi_busy_time;
2869 s.rwnd_limited = info->tcpi_rwnd_limited;
2870 s.sndbuf_limited = info->tcpi_sndbuf_limited;
2871 s.delivered = info->tcpi_delivered;
2872 s.delivered_ce = info->tcpi_delivered_ce;
2873 s.dsack_dups = info->tcpi_dsack_dups;
2874 s.reord_seen = info->tcpi_reord_seen;
2875 s.bytes_sent = info->tcpi_bytes_sent;
2876 s.bytes_retrans = info->tcpi_bytes_retrans;
2877 tcp_stats_print(&s);
2878 free(s.dctcp);
2879 free(s.bbr_info);
2880 }
2881 if (tb[INET_DIAG_MD5SIG]) {
2882 struct tcp_diag_md5sig *sig = RTA_DATA(tb[INET_DIAG_MD5SIG]);
2883 int len = RTA_PAYLOAD(tb[INET_DIAG_MD5SIG]);
2884
2885 out(" md5keys:");
2886 print_md5sig(sig++);
2887 for (len -= sizeof(*sig); len > 0; len -= sizeof(*sig)) {
2888 out(",");
2889 print_md5sig(sig++);
2890 }
2891 }
2892 }
2893
2894 static const char *format_host_sa(struct sockaddr_storage *sa)
2895 {
2896 union {
2897 struct sockaddr_in sin;
2898 struct sockaddr_in6 sin6;
2899 } *saddr = (void *)sa;
2900
2901 switch (sa->ss_family) {
2902 case AF_INET:
2903 return format_host(AF_INET, 4, &saddr->sin.sin_addr);
2904 case AF_INET6:
2905 return format_host(AF_INET6, 16, &saddr->sin6.sin6_addr);
2906 default:
2907 return "";
2908 }
2909 }
2910
2911 static void sctp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
2912 struct rtattr *tb[])
2913 {
2914 struct sockaddr_storage *sa;
2915 int len;
2916
2917 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
2918
2919 if (tb[INET_DIAG_LOCALS]) {
2920 len = RTA_PAYLOAD(tb[INET_DIAG_LOCALS]);
2921 sa = RTA_DATA(tb[INET_DIAG_LOCALS]);
2922
2923 out("locals:%s", format_host_sa(sa));
2924 for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa))
2925 out(",%s", format_host_sa(sa));
2926
2927 }
2928 if (tb[INET_DIAG_PEERS]) {
2929 len = RTA_PAYLOAD(tb[INET_DIAG_PEERS]);
2930 sa = RTA_DATA(tb[INET_DIAG_PEERS]);
2931
2932 out(" peers:%s", format_host_sa(sa));
2933 for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa))
2934 out(",%s", format_host_sa(sa));
2935 }
2936 if (tb[INET_DIAG_INFO]) {
2937 struct sctp_info *info;
2938 len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
2939
2940 /* workaround for older kernels with less fields */
2941 if (len < sizeof(*info)) {
2942 info = alloca(sizeof(*info));
2943 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
2944 memset((char *)info + len, 0, sizeof(*info) - len);
2945 } else
2946 info = RTA_DATA(tb[INET_DIAG_INFO]);
2947
2948 sctp_stats_print(info);
2949 }
2950 }
2951
2952 static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
2953 {
2954 struct rtattr *tb[INET_DIAG_MAX+1];
2955 struct inet_diag_msg *r = NLMSG_DATA(nlh);
2956
2957 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
2958 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2959
2960 s->state = r->idiag_state;
2961 s->local.family = s->remote.family = r->idiag_family;
2962 s->lport = ntohs(r->id.idiag_sport);
2963 s->rport = ntohs(r->id.idiag_dport);
2964 s->wq = r->idiag_wqueue;
2965 s->rq = r->idiag_rqueue;
2966 s->ino = r->idiag_inode;
2967 s->uid = r->idiag_uid;
2968 s->iface = r->id.idiag_if;
2969 s->sk = cookie_sk_get(&r->id.idiag_cookie[0]);
2970
2971 s->mark = 0;
2972 if (tb[INET_DIAG_MARK])
2973 s->mark = rta_getattr_u32(tb[INET_DIAG_MARK]);
2974 if (tb[INET_DIAG_PROTOCOL])
2975 s->raw_prot = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]);
2976 else
2977 s->raw_prot = 0;
2978
2979 if (s->local.family == AF_INET)
2980 s->local.bytelen = s->remote.bytelen = 4;
2981 else
2982 s->local.bytelen = s->remote.bytelen = 16;
2983
2984 memcpy(s->local.data, r->id.idiag_src, s->local.bytelen);
2985 memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen);
2986 }
2987
2988 static int inet_show_sock(struct nlmsghdr *nlh,
2989 struct sockstat *s)
2990 {
2991 struct rtattr *tb[INET_DIAG_MAX+1];
2992 struct inet_diag_msg *r = NLMSG_DATA(nlh);
2993 unsigned char v6only = 0;
2994
2995 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
2996 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2997
2998 if (tb[INET_DIAG_PROTOCOL])
2999 s->type = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]);
3000
3001 if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY])
3002 v6only = rta_getattr_u8(tb[INET_DIAG_SKV6ONLY]);
3003
3004 inet_stats_print(s, v6only);
3005
3006 if (show_options) {
3007 struct tcpstat t = {};
3008
3009 t.timer = r->idiag_timer;
3010 t.timeout = r->idiag_expires;
3011 t.retrans = r->idiag_retrans;
3012 if (s->type == IPPROTO_SCTP)
3013 sctp_timer_print(&t);
3014 else
3015 tcp_timer_print(&t);
3016 }
3017
3018 if (show_details) {
3019 sock_details_print(s);
3020 if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY])
3021 out(" v6only:%u", v6only);
3022
3023 if (tb[INET_DIAG_SHUTDOWN]) {
3024 unsigned char mask;
3025
3026 mask = rta_getattr_u8(tb[INET_DIAG_SHUTDOWN]);
3027 out(" %c-%c",
3028 mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
3029 }
3030 }
3031
3032 if (show_tos) {
3033 if (tb[INET_DIAG_TOS])
3034 out(" tos:%#x", rta_getattr_u8(tb[INET_DIAG_TOS]));
3035 if (tb[INET_DIAG_TCLASS])
3036 out(" tclass:%#x", rta_getattr_u8(tb[INET_DIAG_TCLASS]));
3037 if (tb[INET_DIAG_CLASS_ID])
3038 out(" class_id:%#x", rta_getattr_u32(tb[INET_DIAG_CLASS_ID]));
3039 }
3040
3041 if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) {
3042 out("\n\t");
3043 if (s->type == IPPROTO_SCTP)
3044 sctp_show_info(nlh, r, tb);
3045 else
3046 tcp_show_info(nlh, r, tb);
3047 }
3048 sctp_ino = s->ino;
3049
3050 return 0;
3051 }
3052
3053 static int tcpdiag_send(int fd, int protocol, struct filter *f)
3054 {
3055 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
3056 struct {
3057 struct nlmsghdr nlh;
3058 struct inet_diag_req r;
3059 } req = {
3060 .nlh.nlmsg_len = sizeof(req),
3061 .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
3062 .nlh.nlmsg_seq = MAGIC_SEQ,
3063 .r.idiag_family = AF_INET,
3064 .r.idiag_states = f->states,
3065 };
3066 char *bc = NULL;
3067 int bclen;
3068 struct msghdr msg;
3069 struct rtattr rta;
3070 struct iovec iov[3];
3071 int iovlen = 1;
3072
3073 if (protocol == IPPROTO_UDP)
3074 return -1;
3075
3076 if (protocol == IPPROTO_TCP)
3077 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
3078 else
3079 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
3080 if (show_mem) {
3081 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
3082 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
3083 }
3084
3085 if (show_tcpinfo) {
3086 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
3087 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
3088 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
3089 }
3090
3091 if (show_tos) {
3092 req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1));
3093 req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1));
3094 }
3095
3096 iov[0] = (struct iovec){
3097 .iov_base = &req,
3098 .iov_len = sizeof(req)
3099 };
3100 if (f->f) {
3101 bclen = ssfilter_bytecompile(f->f, &bc);
3102 if (bclen) {
3103 rta.rta_type = INET_DIAG_REQ_BYTECODE;
3104 rta.rta_len = RTA_LENGTH(bclen);
3105 iov[1] = (struct iovec){ &rta, sizeof(rta) };
3106 iov[2] = (struct iovec){ bc, bclen };
3107 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
3108 iovlen = 3;
3109 }
3110 }
3111
3112 msg = (struct msghdr) {
3113 .msg_name = (void *)&nladdr,
3114 .msg_namelen = sizeof(nladdr),
3115 .msg_iov = iov,
3116 .msg_iovlen = iovlen,
3117 };
3118
3119 if (sendmsg(fd, &msg, 0) < 0) {
3120 close(fd);
3121 return -1;
3122 }
3123
3124 return 0;
3125 }
3126
3127 static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
3128 {
3129 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
3130 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
3131 char *bc = NULL;
3132 int bclen;
3133 struct msghdr msg;
3134 struct rtattr rta;
3135 struct iovec iov[3];
3136 int iovlen = 1;
3137
3138 if (family == PF_UNSPEC)
3139 return tcpdiag_send(fd, protocol, f);
3140
3141 memset(&req.r, 0, sizeof(req.r));
3142 req.r.sdiag_family = family;
3143 req.r.sdiag_protocol = protocol;
3144 req.r.idiag_states = f->states;
3145 if (show_mem) {
3146 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
3147 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
3148 }
3149
3150 if (show_tcpinfo) {
3151 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
3152 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
3153 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
3154 }
3155
3156 if (show_tos) {
3157 req.r.idiag_ext |= (1<<(INET_DIAG_TOS-1));
3158 req.r.idiag_ext |= (1<<(INET_DIAG_TCLASS-1));
3159 }
3160
3161 iov[0] = (struct iovec){
3162 .iov_base = &req,
3163 .iov_len = sizeof(req)
3164 };
3165 if (f->f) {
3166 bclen = ssfilter_bytecompile(f->f, &bc);
3167 if (bclen) {
3168 rta.rta_type = INET_DIAG_REQ_BYTECODE;
3169 rta.rta_len = RTA_LENGTH(bclen);
3170 iov[1] = (struct iovec){ &rta, sizeof(rta) };
3171 iov[2] = (struct iovec){ bc, bclen };
3172 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
3173 iovlen = 3;
3174 }
3175 }
3176
3177 msg = (struct msghdr) {
3178 .msg_name = (void *)&nladdr,
3179 .msg_namelen = sizeof(nladdr),
3180 .msg_iov = iov,
3181 .msg_iovlen = iovlen,
3182 };
3183
3184 if (sendmsg(fd, &msg, 0) < 0) {
3185 close(fd);
3186 return -1;
3187 }
3188
3189 return 0;
3190 }
3191
3192 struct inet_diag_arg {
3193 struct filter *f;
3194 int protocol;
3195 struct rtnl_handle *rth;
3196 };
3197
3198 static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s)
3199 {
3200 struct inet_diag_msg *d = NLMSG_DATA(h);
3201 struct inet_diag_arg *diag_arg = arg;
3202 struct rtnl_handle *rth = diag_arg->rth;
3203
3204 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
3205
3206 req.nlh.nlmsg_type = SOCK_DESTROY;
3207 req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
3208 req.nlh.nlmsg_seq = ++rth->seq;
3209 req.r.sdiag_family = d->idiag_family;
3210 req.r.sdiag_protocol = diag_arg->protocol;
3211 req.r.id = d->id;
3212
3213 if (diag_arg->protocol == IPPROTO_RAW) {
3214 struct inet_diag_req_raw *raw = (void *)&req.r;
3215
3216 BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw));
3217 raw->sdiag_raw_protocol = s->raw_prot;
3218 }
3219
3220 return rtnl_talk(rth, &req.nlh, NULL);
3221 }
3222
3223 static int show_one_inet_sock(struct nlmsghdr *h, void *arg)
3224 {
3225 int err;
3226 struct inet_diag_arg *diag_arg = arg;
3227 struct inet_diag_msg *r = NLMSG_DATA(h);
3228 struct sockstat s = {};
3229
3230 if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family)))
3231 return 0;
3232
3233 parse_diag_msg(h, &s);
3234 s.type = diag_arg->protocol;
3235
3236 if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0)
3237 return 0;
3238
3239 if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) {
3240 if (errno == EOPNOTSUPP || errno == ENOENT) {
3241 /* Socket can't be closed, or is already closed. */
3242 return 0;
3243 } else {
3244 perror("SOCK_DESTROY answers");
3245 return -1;
3246 }
3247 }
3248
3249 err = inet_show_sock(h, &s);
3250 if (err < 0)
3251 return err;
3252
3253 return 0;
3254 }
3255
3256 static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
3257 {
3258 int err = 0;
3259 struct rtnl_handle rth, rth2;
3260 int family = PF_INET;
3261 struct inet_diag_arg arg = { .f = f, .protocol = protocol };
3262
3263 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
3264 return -1;
3265
3266 if (f->kill) {
3267 if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) {
3268 rtnl_close(&rth);
3269 return -1;
3270 }
3271 arg.rth = &rth2;
3272 }
3273
3274 rth.dump = MAGIC_SEQ;
3275 rth.dump_fp = dump_fp;
3276 if (preferred_family == PF_INET6)
3277 family = PF_INET6;
3278
3279 again:
3280 if ((err = sockdiag_send(family, rth.fd, protocol, f)))
3281 goto Exit;
3282
3283 if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) {
3284 if (family != PF_UNSPEC) {
3285 family = PF_UNSPEC;
3286 goto again;
3287 }
3288 goto Exit;
3289 }
3290 if (family == PF_INET && preferred_family != PF_INET) {
3291 family = PF_INET6;
3292 goto again;
3293 }
3294
3295 Exit:
3296 rtnl_close(&rth);
3297 if (arg.rth)
3298 rtnl_close(arg.rth);
3299 return err;
3300 }
3301
3302 static int tcp_show_netlink_file(struct filter *f)
3303 {
3304 FILE *fp;
3305 char buf[16384];
3306 int err = -1;
3307
3308 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
3309 perror("fopen($TCPDIAG_FILE)");
3310 return err;
3311 }
3312
3313 while (1) {
3314 int status, err2;
3315 struct nlmsghdr *h = (struct nlmsghdr *)buf;
3316 struct sockstat s = {};
3317
3318 status = fread(buf, 1, sizeof(*h), fp);
3319 if (status < 0) {
3320 perror("Reading header from $TCPDIAG_FILE");
3321 break;
3322 }
3323 if (status != sizeof(*h)) {
3324 perror("Unexpected EOF reading $TCPDIAG_FILE");
3325 break;
3326 }
3327
3328 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
3329
3330 if (status < 0) {
3331 perror("Reading $TCPDIAG_FILE");
3332 break;
3333 }
3334 if (status + sizeof(*h) < h->nlmsg_len) {
3335 perror("Unexpected EOF reading $TCPDIAG_FILE");
3336 break;
3337 }
3338
3339 /* The only legal exit point */
3340 if (h->nlmsg_type == NLMSG_DONE) {
3341 err = 0;
3342 break;
3343 }
3344
3345 if (h->nlmsg_type == NLMSG_ERROR) {
3346 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
3347
3348 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
3349 fprintf(stderr, "ERROR truncated\n");
3350 } else {
3351 errno = -err->error;
3352 perror("TCPDIAG answered");
3353 }
3354 break;
3355 }
3356
3357 parse_diag_msg(h, &s);
3358 s.type = IPPROTO_TCP;
3359
3360 if (f && f->f && run_ssfilter(f->f, &s) == 0)
3361 continue;
3362
3363 err2 = inet_show_sock(h, &s);
3364 if (err2 < 0) {
3365 err = err2;
3366 break;
3367 }
3368 }
3369
3370 fclose(fp);
3371 return err;
3372 }
3373
3374 static int tcp_show(struct filter *f)
3375 {
3376 FILE *fp = NULL;
3377 char *buf = NULL;
3378 int bufsize = 1024*1024;
3379
3380 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
3381 return 0;
3382
3383 dg_proto = TCP_PROTO;
3384
3385 if (getenv("TCPDIAG_FILE"))
3386 return tcp_show_netlink_file(f);
3387
3388 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
3389 && inet_show_netlink(f, NULL, IPPROTO_TCP) == 0)
3390 return 0;
3391
3392 /* Sigh... We have to parse /proc/net/tcp... */
3393 while (bufsize >= 64*1024) {
3394 if ((buf = malloc(bufsize)) != NULL)
3395 break;
3396 bufsize /= 2;
3397 }
3398 if (buf == NULL) {
3399 errno = ENOMEM;
3400 return -1;
3401 }
3402
3403 if (f->families & FAMILY_MASK(AF_INET)) {
3404 if ((fp = net_tcp_open()) == NULL)
3405 goto outerr;
3406
3407 setbuffer(fp, buf, bufsize);
3408 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
3409 goto outerr;
3410 fclose(fp);
3411 }
3412
3413 if ((f->families & FAMILY_MASK(AF_INET6)) &&
3414 (fp = net_tcp6_open()) != NULL) {
3415 setbuffer(fp, buf, bufsize);
3416 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
3417 goto outerr;
3418 fclose(fp);
3419 }
3420
3421 free(buf);
3422 return 0;
3423
3424 outerr:
3425 do {
3426 int saved_errno = errno;
3427
3428 free(buf);
3429 if (fp)
3430 fclose(fp);
3431 errno = saved_errno;
3432 return -1;
3433 } while (0);
3434 }
3435
3436 static int dccp_show(struct filter *f)
3437 {
3438 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
3439 return 0;
3440
3441 if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT")
3442 && inet_show_netlink(f, NULL, IPPROTO_DCCP) == 0)
3443 return 0;
3444
3445 return 0;
3446 }
3447
3448 static int sctp_show(struct filter *f)
3449 {
3450 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
3451 return 0;
3452
3453 if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT")
3454 && inet_show_netlink(f, NULL, IPPROTO_SCTP) == 0)
3455 return 0;
3456
3457 return 0;
3458 }
3459
3460 static int dgram_show_line(char *line, const struct filter *f, int family)
3461 {
3462 struct sockstat s = {};
3463 char *loc, *rem, *data;
3464 char opt[256];
3465 int n;
3466
3467 if (proc_inet_split_line(line, &loc, &rem, &data))
3468 return -1;
3469
3470 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
3471
3472 if (!(f->states & (1 << state)))
3473 return 0;
3474
3475 proc_parse_inet_addr(loc, rem, family, &s);
3476
3477 if (f->f && run_ssfilter(f->f, &s) == 0)
3478 return 0;
3479
3480 opt[0] = 0;
3481 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
3482 &s.state, &s.wq, &s.rq,
3483 &s.uid, &s.ino,
3484 &s.refcnt, &s.sk, opt);
3485
3486 if (n < 9)
3487 opt[0] = 0;
3488
3489 s.type = dg_proto == UDP_PROTO ? IPPROTO_UDP : 0;
3490 inet_stats_print(&s, false);
3491
3492 if (show_details && opt[0])
3493 out(" opt:\"%s\"", opt);
3494
3495 return 0;
3496 }
3497
3498 static int udp_show(struct filter *f)
3499 {
3500 FILE *fp = NULL;
3501
3502 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
3503 return 0;
3504
3505 dg_proto = UDP_PROTO;
3506
3507 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
3508 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
3509 return 0;
3510
3511 if (f->families&FAMILY_MASK(AF_INET)) {
3512 if ((fp = net_udp_open()) == NULL)
3513 goto outerr;
3514 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
3515 goto outerr;
3516 fclose(fp);
3517 }
3518
3519 if ((f->families&FAMILY_MASK(AF_INET6)) &&
3520 (fp = net_udp6_open()) != NULL) {
3521 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
3522 goto outerr;
3523 fclose(fp);
3524 }
3525 return 0;
3526
3527 outerr:
3528 do {
3529 int saved_errno = errno;
3530
3531 if (fp)
3532 fclose(fp);
3533 errno = saved_errno;
3534 return -1;
3535 } while (0);
3536 }
3537
3538 static int raw_show(struct filter *f)
3539 {
3540 FILE *fp = NULL;
3541
3542 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
3543 return 0;
3544
3545 dg_proto = RAW_PROTO;
3546
3547 if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") &&
3548 inet_show_netlink(f, NULL, IPPROTO_RAW) == 0)
3549 return 0;
3550
3551 if (f->families&FAMILY_MASK(AF_INET)) {
3552 if ((fp = net_raw_open()) == NULL)
3553 goto outerr;
3554 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
3555 goto outerr;
3556 fclose(fp);
3557 }
3558
3559 if ((f->families&FAMILY_MASK(AF_INET6)) &&
3560 (fp = net_raw6_open()) != NULL) {
3561 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
3562 goto outerr;
3563 fclose(fp);
3564 }
3565 return 0;
3566
3567 outerr:
3568 do {
3569 int saved_errno = errno;
3570
3571 if (fp)
3572 fclose(fp);
3573 errno = saved_errno;
3574 return -1;
3575 } while (0);
3576 }
3577
3578 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
3579
3580 static void unix_list_drop_first(struct sockstat **list)
3581 {
3582 struct sockstat *s = *list;
3583
3584 (*list) = (*list)->next;
3585 free(s->name);
3586 free(s);
3587 }
3588
3589 static bool unix_type_skip(struct sockstat *s, struct filter *f)
3590 {
3591 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
3592 return true;
3593 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
3594 return true;
3595 if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB)))
3596 return true;
3597 return false;
3598 }
3599
3600 static void unix_stats_print(struct sockstat *s, struct filter *f)
3601 {
3602 char port_name[30] = {};
3603
3604 sock_state_print(s);
3605
3606 sock_addr_print(s->name ?: "*", " ",
3607 int_to_str(s->lport, port_name), NULL);
3608 sock_addr_print(s->peer_name ?: "*", " ",
3609 int_to_str(s->rport, port_name), NULL);
3610
3611 proc_ctx_print(s);
3612 }
3613
3614 static int unix_show_sock(struct nlmsghdr *nlh, void *arg)
3615 {
3616 struct filter *f = (struct filter *)arg;
3617 struct unix_diag_msg *r = NLMSG_DATA(nlh);
3618 struct rtattr *tb[UNIX_DIAG_MAX+1];
3619 char name[128];
3620 struct sockstat stat = { .name = "*", .peer_name = "*" };
3621
3622 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1),
3623 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3624
3625 stat.type = r->udiag_type;
3626 stat.state = r->udiag_state;
3627 stat.ino = stat.lport = r->udiag_ino;
3628 stat.local.family = stat.remote.family = AF_UNIX;
3629
3630 if (unix_type_skip(&stat, f))
3631 return 0;
3632
3633 if (tb[UNIX_DIAG_RQLEN]) {
3634 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
3635
3636 stat.rq = rql->udiag_rqueue;
3637 stat.wq = rql->udiag_wqueue;
3638 }
3639 if (tb[UNIX_DIAG_NAME]) {
3640 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
3641
3642 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
3643 name[len] = '\0';
3644 if (name[0] == '\0') {
3645 int i;
3646 for (i = 0; i < len; i++)
3647 if (name[i] == '\0')
3648 name[i] = '@';
3649 }
3650 stat.name = &name[0];
3651 memcpy(stat.local.data, &stat.name, sizeof(stat.name));
3652 }
3653 if (tb[UNIX_DIAG_PEER])
3654 stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
3655
3656 if (f->f && run_ssfilter(f->f, &stat) == 0)
3657 return 0;
3658
3659 unix_stats_print(&stat, f);
3660
3661 if (show_mem)
3662 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
3663 if (show_details) {
3664 if (tb[UNIX_DIAG_SHUTDOWN]) {
3665 unsigned char mask;
3666
3667 mask = rta_getattr_u8(tb[UNIX_DIAG_SHUTDOWN]);
3668 out(" %c-%c",
3669 mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
3670 }
3671 if (tb[UNIX_DIAG_VFS]) {
3672 struct unix_diag_vfs *uv = RTA_DATA(tb[UNIX_DIAG_VFS]);
3673
3674 out(" ino:%u dev:%u/%u", uv->udiag_vfs_ino, major(uv->udiag_vfs_dev),
3675 minor(uv->udiag_vfs_dev));
3676 }
3677 if (tb[UNIX_DIAG_ICONS]) {
3678 int len = RTA_PAYLOAD(tb[UNIX_DIAG_ICONS]);
3679 __u32 *peers = RTA_DATA(tb[UNIX_DIAG_ICONS]);
3680 int i;
3681
3682 out(" peers:");
3683 for (i = 0; i < len / sizeof(__u32); i++)
3684 out(" %u", peers[i]);
3685 }
3686 }
3687
3688 return 0;
3689 }
3690
3691 static int handle_netlink_request(struct filter *f, struct nlmsghdr *req,
3692 size_t size, rtnl_filter_t show_one_sock)
3693 {
3694 int ret = -1;
3695 struct rtnl_handle rth;
3696
3697 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
3698 return -1;
3699
3700 rth.dump = MAGIC_SEQ;
3701
3702 if (rtnl_send(&rth, req, size) < 0)
3703 goto Exit;
3704
3705 if (rtnl_dump_filter(&rth, show_one_sock, f))
3706 goto Exit;
3707
3708 ret = 0;
3709 Exit:
3710 rtnl_close(&rth);
3711 return ret;
3712 }
3713
3714 static int unix_show_netlink(struct filter *f)
3715 {
3716 DIAG_REQUEST(req, struct unix_diag_req r);
3717
3718 req.r.sdiag_family = AF_UNIX;
3719 req.r.udiag_states = f->states;
3720 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
3721 if (show_mem)
3722 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
3723 if (show_details)
3724 req.r.udiag_show |= UDIAG_SHOW_VFS | UDIAG_SHOW_ICONS;
3725
3726 return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
3727 }
3728
3729 static int unix_show(struct filter *f)
3730 {
3731 FILE *fp;
3732 char buf[256];
3733 char name[128];
3734 int newformat = 0;
3735 int cnt;
3736 struct sockstat *list = NULL;
3737 const int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
3738 SS_ESTABLISHED, SS_CLOSING };
3739
3740 if (!filter_af_get(f, AF_UNIX))
3741 return 0;
3742
3743 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
3744 && unix_show_netlink(f) == 0)
3745 return 0;
3746
3747 if ((fp = net_unix_open()) == NULL)
3748 return -1;
3749 if (!fgets(buf, sizeof(buf), fp)) {
3750 fclose(fp);
3751 return -1;
3752 }
3753
3754 if (memcmp(buf, "Peer", 4) == 0)
3755 newformat = 1;
3756 cnt = 0;
3757
3758 while (fgets(buf, sizeof(buf), fp)) {
3759 struct sockstat *u, **insp;
3760 int flags;
3761
3762 if (!(u = calloc(1, sizeof(*u))))
3763 break;
3764
3765 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
3766 &u->rport, &u->rq, &u->wq, &flags, &u->type,
3767 &u->state, &u->ino, name) < 8)
3768 name[0] = 0;
3769
3770 u->lport = u->ino;
3771 u->local.family = u->remote.family = AF_UNIX;
3772
3773 if (flags & (1 << 16)) {
3774 u->state = SS_LISTEN;
3775 } else if (u->state > 0 &&
3776 u->state <= ARRAY_SIZE(unix_state_map)) {
3777 u->state = unix_state_map[u->state-1];
3778 if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport)
3779 u->state = SS_ESTABLISHED;
3780 }
3781 if (unix_type_skip(u, f) ||
3782 !(f->states & (1 << u->state))) {
3783 free(u);
3784 continue;
3785 }
3786
3787 if (!newformat) {
3788 u->rport = 0;
3789 u->rq = 0;
3790 u->wq = 0;
3791 }
3792
3793 if (name[0]) {
3794 u->name = strdup(name);
3795 if (!u->name) {
3796 free(u);
3797 break;
3798 }
3799 }
3800
3801 if (u->rport) {
3802 struct sockstat *p;
3803
3804 for (p = list; p; p = p->next) {
3805 if (u->rport == p->lport)
3806 break;
3807 }
3808 if (!p)
3809 u->peer_name = "?";
3810 else
3811 u->peer_name = p->name ? : "*";
3812 }
3813
3814 if (f->f) {
3815 struct sockstat st = {
3816 .local.family = AF_UNIX,
3817 .remote.family = AF_UNIX,
3818 };
3819
3820 memcpy(st.local.data, &u->name, sizeof(u->name));
3821 /* when parsing the old format rport is set to 0 and
3822 * therefore peer_name remains NULL
3823 */
3824 if (u->peer_name && strcmp(u->peer_name, "*"))
3825 memcpy(st.remote.data, &u->peer_name,
3826 sizeof(u->peer_name));
3827 if (run_ssfilter(f->f, &st) == 0) {
3828 free(u->name);
3829 free(u);
3830 continue;
3831 }
3832 }
3833
3834 insp = &list;
3835 while (*insp) {
3836 if (u->type < (*insp)->type ||
3837 (u->type == (*insp)->type &&
3838 u->ino < (*insp)->ino))
3839 break;
3840 insp = &(*insp)->next;
3841 }
3842 u->next = *insp;
3843 *insp = u;
3844
3845 if (++cnt > MAX_UNIX_REMEMBER) {
3846 while (list) {
3847 unix_stats_print(list, f);
3848 unix_list_drop_first(&list);
3849 }
3850 cnt = 0;
3851 }
3852 }
3853 fclose(fp);
3854 while (list) {
3855 unix_stats_print(list, f);
3856 unix_list_drop_first(&list);
3857 }
3858
3859 return 0;
3860 }
3861
3862 static int packet_stats_print(struct sockstat *s, const struct filter *f)
3863 {
3864 const char *addr, *port;
3865 char ll_name[16];
3866
3867 s->local.family = s->remote.family = AF_PACKET;
3868
3869 if (f->f) {
3870 s->local.data[0] = s->prot;
3871 if (run_ssfilter(f->f, s) == 0)
3872 return 1;
3873 }
3874
3875 sock_state_print(s);
3876
3877 if (s->prot == 3)
3878 addr = "*";
3879 else
3880 addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name));
3881
3882 if (s->iface == 0)
3883 port = "*";
3884 else
3885 port = xll_index_to_name(s->iface);
3886
3887 sock_addr_print(addr, ":", port, NULL);
3888 sock_addr_print("", "*", "", NULL);
3889
3890 proc_ctx_print(s);
3891
3892 if (show_details)
3893 sock_details_print(s);
3894
3895 return 0;
3896 }
3897
3898 static void packet_show_ring(struct packet_diag_ring *ring)
3899 {
3900 out("blk_size:%d", ring->pdr_block_size);
3901 out(",blk_nr:%d", ring->pdr_block_nr);
3902 out(",frm_size:%d", ring->pdr_frame_size);
3903 out(",frm_nr:%d", ring->pdr_frame_nr);
3904 out(",tmo:%d", ring->pdr_retire_tmo);
3905 out(",features:0x%x", ring->pdr_features);
3906 }
3907
3908 static int packet_show_sock(struct nlmsghdr *nlh, void *arg)
3909 {
3910 const struct filter *f = arg;
3911 struct packet_diag_msg *r = NLMSG_DATA(nlh);
3912 struct packet_diag_info *pinfo = NULL;
3913 struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
3914 struct rtattr *tb[PACKET_DIAG_MAX+1];
3915 struct sockstat stat = {};
3916 uint32_t fanout = 0;
3917 bool has_fanout = false;
3918
3919 parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1),
3920 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3921
3922 /* use /proc/net/packet if all info are not available */
3923 if (!tb[PACKET_DIAG_MEMINFO])
3924 return -1;
3925
3926 stat.type = r->pdiag_type;
3927 stat.prot = r->pdiag_num;
3928 stat.ino = r->pdiag_ino;
3929 stat.state = SS_CLOSE;
3930 stat.sk = cookie_sk_get(&r->pdiag_cookie[0]);
3931
3932 if (tb[PACKET_DIAG_MEMINFO]) {
3933 __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
3934
3935 stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3936 }
3937
3938 if (tb[PACKET_DIAG_INFO]) {
3939 pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
3940 stat.lport = stat.iface = pinfo->pdi_index;
3941 }
3942
3943 if (tb[PACKET_DIAG_UID])
3944 stat.uid = rta_getattr_u32(tb[PACKET_DIAG_UID]);
3945
3946 if (tb[PACKET_DIAG_RX_RING])
3947 ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
3948
3949 if (tb[PACKET_DIAG_TX_RING])
3950 ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
3951
3952 if (tb[PACKET_DIAG_FANOUT]) {
3953 has_fanout = true;
3954 fanout = rta_getattr_u32(tb[PACKET_DIAG_FANOUT]);
3955 }
3956
3957 if (packet_stats_print(&stat, f))
3958 return 0;
3959
3960 if (show_details) {
3961 if (pinfo) {
3962 out("\n\tver:%d", pinfo->pdi_version);
3963 out(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
3964 out(" flags( ");
3965 if (pinfo->pdi_flags & PDI_RUNNING)
3966 out("running");
3967 if (pinfo->pdi_flags & PDI_AUXDATA)
3968 out(" auxdata");
3969 if (pinfo->pdi_flags & PDI_ORIGDEV)
3970 out(" origdev");
3971 if (pinfo->pdi_flags & PDI_VNETHDR)
3972 out(" vnethdr");
3973 if (pinfo->pdi_flags & PDI_LOSS)
3974 out(" loss");
3975 if (!pinfo->pdi_flags)
3976 out("0");
3977 out(" )");
3978 }
3979 if (ring_rx) {
3980 out("\n\tring_rx(");
3981 packet_show_ring(ring_rx);
3982 out(")");
3983 }
3984 if (ring_tx) {
3985 out("\n\tring_tx(");
3986 packet_show_ring(ring_tx);
3987 out(")");
3988 }
3989 if (has_fanout) {
3990 uint16_t type = (fanout >> 16) & 0xffff;
3991
3992 out("\n\tfanout(");
3993 out("id:%d,", fanout & 0xffff);
3994 out("type:");
3995
3996 if (type == 0)
3997 out("hash");
3998 else if (type == 1)
3999 out("lb");
4000 else if (type == 2)
4001 out("cpu");
4002 else if (type == 3)
4003 out("roll");
4004 else if (type == 4)
4005 out("random");
4006 else if (type == 5)
4007 out("qm");
4008 else
4009 out("0x%x", type);
4010
4011 out(")");
4012 }
4013 }
4014
4015 if (show_bpf && tb[PACKET_DIAG_FILTER]) {
4016 struct sock_filter *fil =
4017 RTA_DATA(tb[PACKET_DIAG_FILTER]);
4018 int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) /
4019 sizeof(struct sock_filter);
4020
4021 out("\n\tbpf filter (%d): ", num);
4022 while (num) {
4023 out(" 0x%02x %u %u %u,",
4024 fil->code, fil->jt, fil->jf, fil->k);
4025 num--;
4026 fil++;
4027 }
4028 }
4029
4030 if (show_mem)
4031 print_skmeminfo(tb, PACKET_DIAG_MEMINFO);
4032 return 0;
4033 }
4034
4035 static int packet_show_netlink(struct filter *f)
4036 {
4037 DIAG_REQUEST(req, struct packet_diag_req r);
4038
4039 req.r.sdiag_family = AF_PACKET;
4040 req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
4041 PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
4042
4043 return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
4044 }
4045
4046 static int packet_show_line(char *buf, const struct filter *f, int fam)
4047 {
4048 unsigned long long sk;
4049 struct sockstat stat = {};
4050 int type, prot, iface, state, rq, uid, ino;
4051
4052 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
4053 &sk,
4054 &type, &prot, &iface, &state,
4055 &rq, &uid, &ino);
4056
4057 if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
4058 return 0;
4059 if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
4060 return 0;
4061
4062 stat.type = type;
4063 stat.prot = prot;
4064 stat.lport = stat.iface = iface;
4065 stat.state = state;
4066 stat.rq = rq;
4067 stat.uid = uid;
4068 stat.ino = ino;
4069 stat.state = SS_CLOSE;
4070
4071 if (packet_stats_print(&stat, f))
4072 return 0;
4073
4074 return 0;
4075 }
4076
4077 static int packet_show(struct filter *f)
4078 {
4079 FILE *fp;
4080 int rc = 0;
4081
4082 if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
4083 return 0;
4084
4085 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
4086 packet_show_netlink(f) == 0)
4087 return 0;
4088
4089 if ((fp = net_packet_open()) == NULL)
4090 return -1;
4091 if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
4092 rc = -1;
4093
4094 fclose(fp);
4095 return rc;
4096 }
4097
4098 static int netlink_show_one(struct filter *f,
4099 int prot, int pid, unsigned int groups,
4100 int state, int dst_pid, unsigned int dst_group,
4101 int rq, int wq,
4102 unsigned long long sk, unsigned long long cb)
4103 {
4104 struct sockstat st = {
4105 .state = SS_CLOSE,
4106 .rq = rq,
4107 .wq = wq,
4108 .local.family = AF_NETLINK,
4109 .remote.family = AF_NETLINK,
4110 };
4111
4112 SPRINT_BUF(prot_buf) = {};
4113 const char *prot_name;
4114 char procname[64] = {};
4115
4116 if (f->f) {
4117 st.rport = -1;
4118 st.lport = pid;
4119 st.local.data[0] = prot;
4120 if (run_ssfilter(f->f, &st) == 0)
4121 return 1;
4122 }
4123
4124 sock_state_print(&st);
4125
4126 if (resolve_services)
4127 prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf));
4128 else
4129 prot_name = int_to_str(prot, prot_buf);
4130
4131 if (pid == -1) {
4132 procname[0] = '*';
4133 } else if (resolve_services) {
4134 int done = 0;
4135
4136 if (!pid) {
4137 done = 1;
4138 strncpy(procname, "kernel", 7);
4139 } else if (pid > 0) {
4140 FILE *fp;
4141
4142 snprintf(procname, sizeof(procname), "%s/%d/stat",
4143 getenv("PROC_ROOT") ? : "/proc", pid);
4144 if ((fp = fopen(procname, "r")) != NULL) {
4145 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
4146 snprintf(procname+strlen(procname),
4147 sizeof(procname)-strlen(procname),
4148 "/%d", pid);
4149 done = 1;
4150 }
4151 fclose(fp);
4152 }
4153 }
4154 if (!done)
4155 int_to_str(pid, procname);
4156 } else {
4157 int_to_str(pid, procname);
4158 }
4159
4160 sock_addr_print(prot_name, ":", procname, NULL);
4161
4162 if (state == NETLINK_CONNECTED) {
4163 char dst_group_buf[30];
4164 char dst_pid_buf[30];
4165
4166 sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
4167 int_to_str(dst_pid, dst_pid_buf), NULL);
4168 } else {
4169 sock_addr_print("", "*", "", NULL);
4170 }
4171
4172 char *pid_context = NULL;
4173
4174 if (show_proc_ctx) {
4175 /* The pid value will either be:
4176 * 0 if destination kernel - show kernel initial context.
4177 * A valid process pid - use getpidcon.
4178 * A unique value allocated by the kernel or netlink user
4179 * to the process - show context as "not available".
4180 */
4181 if (!pid)
4182 security_get_initial_context("kernel", &pid_context);
4183 else if (pid > 0)
4184 getpidcon(pid, &pid_context);
4185
4186 out(" proc_ctx=%s", pid_context ? : "unavailable");
4187 free(pid_context);
4188 }
4189
4190 if (show_details) {
4191 out(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
4192 }
4193
4194 return 0;
4195 }
4196
4197 static int netlink_show_sock(struct nlmsghdr *nlh, void *arg)
4198 {
4199 struct filter *f = (struct filter *)arg;
4200 struct netlink_diag_msg *r = NLMSG_DATA(nlh);
4201 struct rtattr *tb[NETLINK_DIAG_MAX+1];
4202 int rq = 0, wq = 0;
4203 unsigned long groups = 0;
4204
4205 parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1),
4206 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
4207
4208 if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
4209 groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]);
4210
4211 if (tb[NETLINK_DIAG_MEMINFO]) {
4212 const __u32 *skmeminfo;
4213
4214 skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
4215
4216 rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
4217 wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC];
4218 }
4219
4220 if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups,
4221 r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group,
4222 rq, wq, 0, 0)) {
4223 return 0;
4224 }
4225
4226 if (show_mem) {
4227 out("\t");
4228 print_skmeminfo(tb, NETLINK_DIAG_MEMINFO);
4229 }
4230
4231 return 0;
4232 }
4233
4234 static int netlink_show_netlink(struct filter *f)
4235 {
4236 DIAG_REQUEST(req, struct netlink_diag_req r);
4237
4238 req.r.sdiag_family = AF_NETLINK;
4239 req.r.sdiag_protocol = NDIAG_PROTO_ALL;
4240 req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO;
4241
4242 return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock);
4243 }
4244
4245 static int netlink_show(struct filter *f)
4246 {
4247 FILE *fp;
4248 char buf[256];
4249 int prot, pid;
4250 unsigned int groups;
4251 int rq, wq, rc;
4252 unsigned long long sk, cb;
4253
4254 if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
4255 return 0;
4256
4257 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
4258 netlink_show_netlink(f) == 0)
4259 return 0;
4260
4261 if ((fp = net_netlink_open()) == NULL)
4262 return -1;
4263 if (!fgets(buf, sizeof(buf), fp)) {
4264 fclose(fp);
4265 return -1;
4266 }
4267
4268 while (fgets(buf, sizeof(buf), fp)) {
4269 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
4270 &sk,
4271 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
4272
4273 netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
4274 }
4275
4276 fclose(fp);
4277 return 0;
4278 }
4279
4280 static bool vsock_type_skip(struct sockstat *s, struct filter *f)
4281 {
4282 if (s->type == SOCK_STREAM && !(f->dbs & (1 << VSOCK_ST_DB)))
4283 return true;
4284 if (s->type == SOCK_DGRAM && !(f->dbs & (1 << VSOCK_DG_DB)))
4285 return true;
4286 return false;
4287 }
4288
4289 static void vsock_addr_print(inet_prefix *a, __u32 port)
4290 {
4291 char cid_str[sizeof("4294967295")];
4292 char port_str[sizeof("4294967295")];
4293 __u32 cid;
4294
4295 memcpy(&cid, a->data, sizeof(cid));
4296
4297 if (cid == ~(__u32)0)
4298 snprintf(cid_str, sizeof(cid_str), "*");
4299 else
4300 snprintf(cid_str, sizeof(cid_str), "%u", cid);
4301
4302 if (port == ~(__u32)0)
4303 snprintf(port_str, sizeof(port_str), "*");
4304 else
4305 snprintf(port_str, sizeof(port_str), "%u", port);
4306
4307 sock_addr_print(cid_str, ":", port_str, NULL);
4308 }
4309
4310 static void vsock_stats_print(struct sockstat *s, struct filter *f)
4311 {
4312 sock_state_print(s);
4313
4314 vsock_addr_print(&s->local, s->lport);
4315 vsock_addr_print(&s->remote, s->rport);
4316
4317 proc_ctx_print(s);
4318 }
4319
4320 static int vsock_show_sock(struct nlmsghdr *nlh, void *arg)
4321 {
4322 struct filter *f = (struct filter *)arg;
4323 struct vsock_diag_msg *r = NLMSG_DATA(nlh);
4324 struct sockstat stat = {
4325 .type = r->vdiag_type,
4326 .lport = r->vdiag_src_port,
4327 .rport = r->vdiag_dst_port,
4328 .state = r->vdiag_state,
4329 .ino = r->vdiag_ino,
4330 };
4331
4332 vsock_set_inet_prefix(&stat.local, r->vdiag_src_cid);
4333 vsock_set_inet_prefix(&stat.remote, r->vdiag_dst_cid);
4334
4335 if (vsock_type_skip(&stat, f))
4336 return 0;
4337
4338 if (f->f && run_ssfilter(f->f, &stat) == 0)
4339 return 0;
4340
4341 vsock_stats_print(&stat, f);
4342
4343 return 0;
4344 }
4345
4346 static int vsock_show(struct filter *f)
4347 {
4348 DIAG_REQUEST(req, struct vsock_diag_req r);
4349
4350 if (!filter_af_get(f, AF_VSOCK))
4351 return 0;
4352
4353 req.r.sdiag_family = AF_VSOCK;
4354 req.r.vdiag_states = f->states;
4355
4356 return handle_netlink_request(f, &req.nlh, sizeof(req), vsock_show_sock);
4357 }
4358
4359 static void tipc_sock_addr_print(struct rtattr *net_addr, struct rtattr *id)
4360 {
4361 uint32_t node = rta_getattr_u32(net_addr);
4362 uint32_t identity = rta_getattr_u32(id);
4363
4364 SPRINT_BUF(addr) = {};
4365 SPRINT_BUF(port) = {};
4366
4367 sprintf(addr, "%u", node);
4368 sprintf(port, "%u", identity);
4369 sock_addr_print(addr, ":", port, NULL);
4370
4371 }
4372
4373 static int tipc_show_sock(struct nlmsghdr *nlh, void *arg)
4374 {
4375 struct rtattr *stat[TIPC_NLA_SOCK_STAT_MAX + 1] = {};
4376 struct rtattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {};
4377 struct rtattr *con[TIPC_NLA_CON_MAX + 1] = {};
4378 struct rtattr *info[TIPC_NLA_MAX + 1] = {};
4379 struct rtattr *msg_ref;
4380 struct sockstat ss = {};
4381
4382 parse_rtattr(info, TIPC_NLA_MAX, NLMSG_DATA(nlh),
4383 NLMSG_PAYLOAD(nlh, 0));
4384
4385 if (!info[TIPC_NLA_SOCK])
4386 return 0;
4387
4388 msg_ref = info[TIPC_NLA_SOCK];
4389 parse_rtattr(attrs, TIPC_NLA_SOCK_MAX, RTA_DATA(msg_ref),
4390 RTA_PAYLOAD(msg_ref));
4391
4392 msg_ref = attrs[TIPC_NLA_SOCK_STAT];
4393 parse_rtattr(stat, TIPC_NLA_SOCK_STAT_MAX,
4394 RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref));
4395
4396
4397 ss.local.family = AF_TIPC;
4398 ss.type = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TYPE]);
4399 ss.state = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TIPC_STATE]);
4400 ss.uid = rta_getattr_u32(attrs[TIPC_NLA_SOCK_UID]);
4401 ss.ino = rta_getattr_u32(attrs[TIPC_NLA_SOCK_INO]);
4402 ss.rq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_RCVQ]);
4403 ss.wq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_SENDQ]);
4404 ss.sk = rta_getattr_u64(attrs[TIPC_NLA_SOCK_COOKIE]);
4405
4406 sock_state_print (&ss);
4407
4408 tipc_sock_addr_print(attrs[TIPC_NLA_SOCK_ADDR],
4409 attrs[TIPC_NLA_SOCK_REF]);
4410
4411 msg_ref = attrs[TIPC_NLA_SOCK_CON];
4412 if (msg_ref) {
4413 parse_rtattr(con, TIPC_NLA_CON_MAX,
4414 RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref));
4415
4416 tipc_sock_addr_print(con[TIPC_NLA_CON_NODE],
4417 con[TIPC_NLA_CON_SOCK]);
4418 } else
4419 sock_addr_print("", "-", "", NULL);
4420
4421 if (show_details)
4422 sock_details_print(&ss);
4423
4424 proc_ctx_print(&ss);
4425
4426 if (show_tipcinfo) {
4427 out("\n type:%s", stype_nameg[ss.type]);
4428 out(" cong:%s ",
4429 stat[TIPC_NLA_SOCK_STAT_LINK_CONG] ? "link" :
4430 stat[TIPC_NLA_SOCK_STAT_CONN_CONG] ? "conn" : "none");
4431 out(" drop:%d ",
4432 rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_DROP]));
4433
4434 if (attrs[TIPC_NLA_SOCK_HAS_PUBL])
4435 out(" publ");
4436
4437 if (con[TIPC_NLA_CON_FLAG])
4438 out(" via {%u,%u} ",
4439 rta_getattr_u32(con[TIPC_NLA_CON_TYPE]),
4440 rta_getattr_u32(con[TIPC_NLA_CON_INST]));
4441 }
4442
4443 return 0;
4444 }
4445
4446 static int tipc_show(struct filter *f)
4447 {
4448 DIAG_REQUEST(req, struct tipc_sock_diag_req r);
4449
4450 memset(&req.r, 0, sizeof(req.r));
4451 req.r.sdiag_family = AF_TIPC;
4452 req.r.tidiag_states = f->states;
4453
4454 return handle_netlink_request(f, &req.nlh, sizeof(req), tipc_show_sock);
4455 }
4456
4457 struct sock_diag_msg {
4458 __u8 sdiag_family;
4459 };
4460
4461 static int generic_show_sock(struct nlmsghdr *nlh, void *arg)
4462 {
4463 struct sock_diag_msg *r = NLMSG_DATA(nlh);
4464 struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX };
4465 int ret;
4466
4467 switch (r->sdiag_family) {
4468 case AF_INET:
4469 case AF_INET6:
4470 inet_arg.rth = inet_arg.f->rth_for_killing;
4471 ret = show_one_inet_sock(nlh, &inet_arg);
4472 break;
4473 case AF_UNIX:
4474 ret = unix_show_sock(nlh, arg);
4475 break;
4476 case AF_PACKET:
4477 ret = packet_show_sock(nlh, arg);
4478 break;
4479 case AF_NETLINK:
4480 ret = netlink_show_sock(nlh, arg);
4481 break;
4482 case AF_VSOCK:
4483 ret = vsock_show_sock(nlh, arg);
4484 break;
4485 default:
4486 ret = -1;
4487 }
4488
4489 render();
4490
4491 return ret;
4492 }
4493
4494 static int handle_follow_request(struct filter *f)
4495 {
4496 int ret = 0;
4497 int groups = 0;
4498 struct rtnl_handle rth, rth2;
4499
4500 if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << TCP_DB))
4501 groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1);
4502 if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << UDP_DB))
4503 groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1);
4504 if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << TCP_DB))
4505 groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1);
4506 if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << UDP_DB))
4507 groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1);
4508
4509 if (groups == 0)
4510 return -1;
4511
4512 if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG))
4513 return -1;
4514
4515 rth.dump = 0;
4516 rth.local.nl_pid = 0;
4517
4518 if (f->kill) {
4519 if (rtnl_open_byproto(&rth2, groups, NETLINK_SOCK_DIAG)) {
4520 rtnl_close(&rth);
4521 return -1;
4522 }
4523 f->rth_for_killing = &rth2;
4524 }
4525
4526 if (rtnl_dump_filter(&rth, generic_show_sock, f))
4527 ret = -1;
4528
4529 rtnl_close(&rth);
4530 if (f->rth_for_killing)
4531 rtnl_close(f->rth_for_killing);
4532 return ret;
4533 }
4534
4535 static int get_snmp_int(char *proto, char *key, int *result)
4536 {
4537 char buf[1024];
4538 FILE *fp;
4539 int protolen = strlen(proto);
4540 int keylen = strlen(key);
4541
4542 *result = 0;
4543
4544 if ((fp = net_snmp_open()) == NULL)
4545 return -1;
4546
4547 while (fgets(buf, sizeof(buf), fp) != NULL) {
4548 char *p = buf;
4549 int pos = 0;
4550
4551 if (memcmp(buf, proto, protolen))
4552 continue;
4553 while ((p = strchr(p, ' ')) != NULL) {
4554 pos++;
4555 p++;
4556 if (memcmp(p, key, keylen) == 0 &&
4557 (p[keylen] == ' ' || p[keylen] == '\n'))
4558 break;
4559 }
4560 if (fgets(buf, sizeof(buf), fp) == NULL)
4561 break;
4562 if (memcmp(buf, proto, protolen))
4563 break;
4564 p = buf;
4565 while ((p = strchr(p, ' ')) != NULL) {
4566 p++;
4567 if (--pos == 0) {
4568 sscanf(p, "%d", result);
4569 fclose(fp);
4570 return 0;
4571 }
4572 }
4573 }
4574
4575 fclose(fp);
4576 errno = ESRCH;
4577 return -1;
4578 }
4579
4580
4581 /* Get stats from sockstat */
4582
4583 struct ssummary {
4584 int socks;
4585 int tcp_mem;
4586 int tcp_total;
4587 int tcp_orphans;
4588 int tcp_tws;
4589 int tcp4_hashed;
4590 int udp4;
4591 int raw4;
4592 int frag4;
4593 int frag4_mem;
4594 int tcp6_hashed;
4595 int udp6;
4596 int raw6;
4597 int frag6;
4598 int frag6_mem;
4599 };
4600
4601 static void get_sockstat_line(char *line, struct ssummary *s)
4602 {
4603 char id[256], rem[256];
4604
4605 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
4606 return;
4607
4608 if (strcmp(id, "sockets:") == 0)
4609 sscanf(rem, "%*s%d", &s->socks);
4610 else if (strcmp(id, "UDP:") == 0)
4611 sscanf(rem, "%*s%d", &s->udp4);
4612 else if (strcmp(id, "UDP6:") == 0)
4613 sscanf(rem, "%*s%d", &s->udp6);
4614 else if (strcmp(id, "RAW:") == 0)
4615 sscanf(rem, "%*s%d", &s->raw4);
4616 else if (strcmp(id, "RAW6:") == 0)
4617 sscanf(rem, "%*s%d", &s->raw6);
4618 else if (strcmp(id, "TCP6:") == 0)
4619 sscanf(rem, "%*s%d", &s->tcp6_hashed);
4620 else if (strcmp(id, "FRAG:") == 0)
4621 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
4622 else if (strcmp(id, "FRAG6:") == 0)
4623 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
4624 else if (strcmp(id, "TCP:") == 0)
4625 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
4626 &s->tcp4_hashed,
4627 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
4628 }
4629
4630 static int get_sockstat(struct ssummary *s)
4631 {
4632 char buf[256];
4633 FILE *fp;
4634
4635 memset(s, 0, sizeof(*s));
4636
4637 if ((fp = net_sockstat_open()) == NULL)
4638 return -1;
4639 while (fgets(buf, sizeof(buf), fp) != NULL)
4640 get_sockstat_line(buf, s);
4641 fclose(fp);
4642
4643 if ((fp = net_sockstat6_open()) == NULL)
4644 return 0;
4645 while (fgets(buf, sizeof(buf), fp) != NULL)
4646 get_sockstat_line(buf, s);
4647 fclose(fp);
4648
4649 return 0;
4650 }
4651
4652 static int print_summary(void)
4653 {
4654 struct ssummary s;
4655 int tcp_estab;
4656
4657 if (get_sockstat(&s) < 0)
4658 perror("ss: get_sockstat");
4659 if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab) < 0)
4660 perror("ss: get_snmpstat");
4661
4662 printf("Total: %d\n", s.socks);
4663
4664 printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n",
4665 s.tcp_total + s.tcp_tws, tcp_estab,
4666 s.tcp_total - (s.tcp4_hashed + s.tcp6_hashed - s.tcp_tws),
4667 s.tcp_orphans, s.tcp_tws);
4668
4669 printf("\n");
4670 printf("Transport Total IP IPv6\n");
4671 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
4672 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
4673 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
4674 printf("INET %-9d %-9d %-9d\n",
4675 s.raw4+s.udp4+s.tcp4_hashed+
4676 s.raw6+s.udp6+s.tcp6_hashed,
4677 s.raw4+s.udp4+s.tcp4_hashed,
4678 s.raw6+s.udp6+s.tcp6_hashed);
4679 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
4680
4681 printf("\n");
4682
4683 return 0;
4684 }
4685
4686 static void _usage(FILE *dest)
4687 {
4688 fprintf(dest,
4689 "Usage: ss [ OPTIONS ]\n"
4690 " ss [ OPTIONS ] [ FILTER ]\n"
4691 " -h, --help this message\n"
4692 " -V, --version output version information\n"
4693 " -n, --numeric don't resolve service names\n"
4694 " -r, --resolve resolve host names\n"
4695 " -a, --all display all sockets\n"
4696 " -l, --listening display listening sockets\n"
4697 " -o, --options show timer information\n"
4698 " -e, --extended show detailed socket information\n"
4699 " -m, --memory show socket memory usage\n"
4700 " -p, --processes show process using socket\n"
4701 " -i, --info show internal TCP information\n"
4702 " --tipcinfo show internal tipc socket information\n"
4703 " -s, --summary show socket usage summary\n"
4704 " --tos show tos and priority information\n"
4705 " -b, --bpf show bpf filter socket information\n"
4706 " -E, --events continually display sockets as they are destroyed\n"
4707 " -Z, --context display process SELinux security contexts\n"
4708 " -z, --contexts display process and socket SELinux security contexts\n"
4709 " -N, --net switch to the specified network namespace name\n"
4710 "\n"
4711 " -4, --ipv4 display only IP version 4 sockets\n"
4712 " -6, --ipv6 display only IP version 6 sockets\n"
4713 " -0, --packet display PACKET sockets\n"
4714 " -t, --tcp display only TCP sockets\n"
4715 " -S, --sctp display only SCTP sockets\n"
4716 " -u, --udp display only UDP sockets\n"
4717 " -d, --dccp display only DCCP sockets\n"
4718 " -w, --raw display only RAW sockets\n"
4719 " -x, --unix display only Unix domain sockets\n"
4720 " --tipc display only TIPC sockets\n"
4721 " --vsock display only vsock sockets\n"
4722 " -f, --family=FAMILY display sockets of type FAMILY\n"
4723 " FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|help}\n"
4724 "\n"
4725 " -K, --kill forcibly close sockets, display what was closed\n"
4726 " -H, --no-header Suppress header line\n"
4727 "\n"
4728 " -A, --query=QUERY, --socket=QUERY\n"
4729 " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY]\n"
4730 "\n"
4731 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
4732 " -F, --filter=FILE read filter information from FILE\n"
4733 " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
4734 " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
4735 " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n"
4736 " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4737 " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
4738 " bucket := {syn-recv|time-wait}\n"
4739 " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n"
4740 );
4741 }
4742
4743 static void help(void) __attribute__((noreturn));
4744 static void help(void)
4745 {
4746 _usage(stdout);
4747 exit(0);
4748 }
4749
4750 static void usage(void) __attribute__((noreturn));
4751 static void usage(void)
4752 {
4753 _usage(stderr);
4754 exit(-1);
4755 }
4756
4757
4758 static int scan_state(const char *state)
4759 {
4760 static const char * const sstate_namel[] = {
4761 "UNKNOWN",
4762 [SS_ESTABLISHED] = "established",
4763 [SS_SYN_SENT] = "syn-sent",
4764 [SS_SYN_RECV] = "syn-recv",
4765 [SS_FIN_WAIT1] = "fin-wait-1",
4766 [SS_FIN_WAIT2] = "fin-wait-2",
4767 [SS_TIME_WAIT] = "time-wait",
4768 [SS_CLOSE] = "unconnected",
4769 [SS_CLOSE_WAIT] = "close-wait",
4770 [SS_LAST_ACK] = "last-ack",
4771 [SS_LISTEN] = "listening",
4772 [SS_CLOSING] = "closing",
4773 };
4774 int i;
4775
4776 if (strcasecmp(state, "close") == 0 ||
4777 strcasecmp(state, "closed") == 0)
4778 return (1<<SS_CLOSE);
4779 if (strcasecmp(state, "syn-rcv") == 0)
4780 return (1<<SS_SYN_RECV);
4781 if (strcasecmp(state, "established") == 0)
4782 return (1<<SS_ESTABLISHED);
4783 if (strcasecmp(state, "all") == 0)
4784 return SS_ALL;
4785 if (strcasecmp(state, "connected") == 0)
4786 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
4787 if (strcasecmp(state, "synchronized") == 0)
4788 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
4789 if (strcasecmp(state, "bucket") == 0)
4790 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
4791 if (strcasecmp(state, "big") == 0)
4792 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
4793 for (i = 0; i < SS_MAX; i++) {
4794 if (strcasecmp(state, sstate_namel[i]) == 0)
4795 return (1<<i);
4796 }
4797
4798 fprintf(stderr, "ss: wrong state name: %s\n", state);
4799 exit(-1);
4800 }
4801
4802 /* Values 'v' and 'V' are already used so a non-character is used */
4803 #define OPT_VSOCK 256
4804
4805 /* Values of 't' are already used so a non-character is used */
4806 #define OPT_TIPCSOCK 257
4807 #define OPT_TIPCINFO 258
4808
4809 #define OPT_TOS 259
4810
4811 static const struct option long_opts[] = {
4812 { "numeric", 0, 0, 'n' },
4813 { "resolve", 0, 0, 'r' },
4814 { "options", 0, 0, 'o' },
4815 { "extended", 0, 0, 'e' },
4816 { "memory", 0, 0, 'm' },
4817 { "info", 0, 0, 'i' },
4818 { "processes", 0, 0, 'p' },
4819 { "bpf", 0, 0, 'b' },
4820 { "events", 0, 0, 'E' },
4821 { "dccp", 0, 0, 'd' },
4822 { "tcp", 0, 0, 't' },
4823 { "sctp", 0, 0, 'S' },
4824 { "udp", 0, 0, 'u' },
4825 { "raw", 0, 0, 'w' },
4826 { "unix", 0, 0, 'x' },
4827 { "tipc", 0, 0, OPT_TIPCSOCK},
4828 { "vsock", 0, 0, OPT_VSOCK },
4829 { "all", 0, 0, 'a' },
4830 { "listening", 0, 0, 'l' },
4831 { "ipv4", 0, 0, '4' },
4832 { "ipv6", 0, 0, '6' },
4833 { "packet", 0, 0, '0' },
4834 { "family", 1, 0, 'f' },
4835 { "socket", 1, 0, 'A' },
4836 { "query", 1, 0, 'A' },
4837 { "summary", 0, 0, 's' },
4838 { "diag", 1, 0, 'D' },
4839 { "filter", 1, 0, 'F' },
4840 { "version", 0, 0, 'V' },
4841 { "help", 0, 0, 'h' },
4842 { "context", 0, 0, 'Z' },
4843 { "contexts", 0, 0, 'z' },
4844 { "net", 1, 0, 'N' },
4845 { "tipcinfo", 0, 0, OPT_TIPCINFO},
4846 { "tos", 0, 0, OPT_TOS },
4847 { "kill", 0, 0, 'K' },
4848 { "no-header", 0, 0, 'H' },
4849 { 0 }
4850
4851 };
4852
4853 int main(int argc, char *argv[])
4854 {
4855 int saw_states = 0;
4856 int saw_query = 0;
4857 int do_summary = 0;
4858 const char *dump_tcpdiag = NULL;
4859 FILE *filter_fp = NULL;
4860 int ch;
4861 int state_filter = 0;
4862
4863 while ((ch = getopt_long(argc, argv,
4864 "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KHS",
4865 long_opts, NULL)) != EOF) {
4866 switch (ch) {
4867 case 'n':
4868 resolve_services = 0;
4869 break;
4870 case 'r':
4871 resolve_hosts = 1;
4872 break;
4873 case 'o':
4874 show_options = 1;
4875 break;
4876 case 'e':
4877 show_options = 1;
4878 show_details++;
4879 break;
4880 case 'm':
4881 show_mem = 1;
4882 break;
4883 case 'i':
4884 show_tcpinfo = 1;
4885 break;
4886 case 'p':
4887 show_users++;
4888 user_ent_hash_build();
4889 break;
4890 case 'b':
4891 show_options = 1;
4892 show_bpf++;
4893 break;
4894 case 'E':
4895 follow_events = 1;
4896 break;
4897 case 'd':
4898 filter_db_set(&current_filter, DCCP_DB, true);
4899 break;
4900 case 't':
4901 filter_db_set(&current_filter, TCP_DB, true);
4902 break;
4903 case 'S':
4904 filter_db_set(&current_filter, SCTP_DB, true);
4905 break;
4906 case 'u':
4907 filter_db_set(&current_filter, UDP_DB, true);
4908 break;
4909 case 'w':
4910 filter_db_set(&current_filter, RAW_DB, true);
4911 break;
4912 case 'x':
4913 filter_af_set(&current_filter, AF_UNIX);
4914 break;
4915 case OPT_VSOCK:
4916 filter_af_set(&current_filter, AF_VSOCK);
4917 break;
4918 case OPT_TIPCSOCK:
4919 filter_af_set(&current_filter, AF_TIPC);
4920 break;
4921 case 'a':
4922 state_filter = SS_ALL;
4923 break;
4924 case 'l':
4925 state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE);
4926 break;
4927 case '4':
4928 filter_af_set(&current_filter, AF_INET);
4929 break;
4930 case '6':
4931 filter_af_set(&current_filter, AF_INET6);
4932 break;
4933 case '0':
4934 filter_af_set(&current_filter, AF_PACKET);
4935 break;
4936 case 'f':
4937 if (strcmp(optarg, "inet") == 0)
4938 filter_af_set(&current_filter, AF_INET);
4939 else if (strcmp(optarg, "inet6") == 0)
4940 filter_af_set(&current_filter, AF_INET6);
4941 else if (strcmp(optarg, "link") == 0)
4942 filter_af_set(&current_filter, AF_PACKET);
4943 else if (strcmp(optarg, "unix") == 0)
4944 filter_af_set(&current_filter, AF_UNIX);
4945 else if (strcmp(optarg, "netlink") == 0)
4946 filter_af_set(&current_filter, AF_NETLINK);
4947 else if (strcmp(optarg, "tipc") == 0)
4948 filter_af_set(&current_filter, AF_TIPC);
4949 else if (strcmp(optarg, "vsock") == 0)
4950 filter_af_set(&current_filter, AF_VSOCK);
4951 else if (strcmp(optarg, "help") == 0)
4952 help();
4953 else {
4954 fprintf(stderr, "ss: \"%s\" is invalid family\n",
4955 optarg);
4956 usage();
4957 }
4958 break;
4959 case 'A':
4960 {
4961 char *p, *p1;
4962
4963 if (!saw_query) {
4964 current_filter.dbs = 0;
4965 state_filter = state_filter ?
4966 state_filter : SS_CONN;
4967 saw_query = 1;
4968 do_default = 0;
4969 }
4970 p = p1 = optarg;
4971 do {
4972 if ((p1 = strchr(p, ',')) != NULL)
4973 *p1 = 0;
4974 if (filter_db_parse(&current_filter, p)) {
4975 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
4976 usage();
4977 }
4978 p = p1 + 1;
4979 } while (p1);
4980 break;
4981 }
4982 case 's':
4983 do_summary = 1;
4984 break;
4985 case 'D':
4986 dump_tcpdiag = optarg;
4987 break;
4988 case 'F':
4989 if (filter_fp) {
4990 fprintf(stderr, "More than one filter file\n");
4991 exit(-1);
4992 }
4993 if (optarg[0] == '-')
4994 filter_fp = stdin;
4995 else
4996 filter_fp = fopen(optarg, "r");
4997 if (!filter_fp) {
4998 perror("fopen filter file");
4999 exit(-1);
5000 }
5001 break;
5002 case 'v':
5003 case 'V':
5004 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
5005 exit(0);
5006 case 'z':
5007 show_sock_ctx++;
5008 /* fall through */
5009 case 'Z':
5010 if (is_selinux_enabled() <= 0) {
5011 fprintf(stderr, "ss: SELinux is not enabled.\n");
5012 exit(1);
5013 }
5014 show_proc_ctx++;
5015 user_ent_hash_build();
5016 break;
5017 case 'N':
5018 if (netns_switch(optarg))
5019 exit(1);
5020 break;
5021 case OPT_TIPCINFO:
5022 show_tipcinfo = 1;
5023 break;
5024 case OPT_TOS:
5025 show_tos = 1;
5026 break;
5027 case 'K':
5028 current_filter.kill = 1;
5029 break;
5030 case 'H':
5031 show_header = 0;
5032 break;
5033 case 'h':
5034 help();
5035 case '?':
5036 default:
5037 usage();
5038 }
5039 }
5040
5041 argc -= optind;
5042 argv += optind;
5043
5044 if (do_summary) {
5045 print_summary();
5046 if (do_default && argc == 0)
5047 exit(0);
5048 }
5049
5050 while (argc > 0) {
5051 if (strcmp(*argv, "state") == 0) {
5052 NEXT_ARG();
5053 if (!saw_states)
5054 state_filter = 0;
5055 state_filter |= scan_state(*argv);
5056 saw_states = 1;
5057 } else if (strcmp(*argv, "exclude") == 0 ||
5058 strcmp(*argv, "excl") == 0) {
5059 NEXT_ARG();
5060 if (!saw_states)
5061 state_filter = SS_ALL;
5062 state_filter &= ~scan_state(*argv);
5063 saw_states = 1;
5064 } else {
5065 break;
5066 }
5067 argc--; argv++;
5068 }
5069
5070 if (do_default) {
5071 state_filter = state_filter ? state_filter : SS_CONN;
5072 filter_db_parse(&current_filter, "all");
5073 }
5074
5075 filter_states_set(&current_filter, state_filter);
5076 filter_merge_defaults(&current_filter);
5077
5078 if (resolve_services && resolve_hosts &&
5079 (current_filter.dbs & (UNIX_DBM|INET_L4_DBM)))
5080 init_service_resolver();
5081
5082 if (current_filter.dbs == 0) {
5083 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
5084 exit(0);
5085 }
5086 if (current_filter.families == 0) {
5087 fprintf(stderr, "ss: no families to show with such filter.\n");
5088 exit(0);
5089 }
5090 if (current_filter.states == 0) {
5091 fprintf(stderr, "ss: no socket states to show with such filter.\n");
5092 exit(0);
5093 }
5094
5095 if (dump_tcpdiag) {
5096 FILE *dump_fp = stdout;
5097
5098 if (!(current_filter.dbs & (1<<TCP_DB))) {
5099 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
5100 exit(0);
5101 }
5102 if (dump_tcpdiag[0] != '-') {
5103 dump_fp = fopen(dump_tcpdiag, "w");
5104 if (!dump_tcpdiag) {
5105 perror("fopen dump file");
5106 exit(-1);
5107 }
5108 }
5109 inet_show_netlink(&current_filter, dump_fp, IPPROTO_TCP);
5110 fflush(dump_fp);
5111 exit(0);
5112 }
5113
5114 if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
5115 usage();
5116
5117 if (!(current_filter.dbs & (current_filter.dbs - 1)))
5118 columns[COL_NETID].disabled = 1;
5119
5120 if (!(current_filter.states & (current_filter.states - 1)))
5121 columns[COL_STATE].disabled = 1;
5122
5123 if (show_header)
5124 print_header();
5125
5126 fflush(stdout);
5127
5128 if (follow_events)
5129 exit(handle_follow_request(&current_filter));
5130
5131 if (current_filter.dbs & (1<<NETLINK_DB))
5132 netlink_show(&current_filter);
5133 if (current_filter.dbs & PACKET_DBM)
5134 packet_show(&current_filter);
5135 if (current_filter.dbs & UNIX_DBM)
5136 unix_show(&current_filter);
5137 if (current_filter.dbs & (1<<RAW_DB))
5138 raw_show(&current_filter);
5139 if (current_filter.dbs & (1<<UDP_DB))
5140 udp_show(&current_filter);
5141 if (current_filter.dbs & (1<<TCP_DB))
5142 tcp_show(&current_filter);
5143 if (current_filter.dbs & (1<<DCCP_DB))
5144 dccp_show(&current_filter);
5145 if (current_filter.dbs & (1<<SCTP_DB))
5146 sctp_show(&current_filter);
5147 if (current_filter.dbs & VSOCK_DBM)
5148 vsock_show(&current_filter);
5149 if (current_filter.dbs & (1<<TIPC_DB))
5150 tipc_show(&current_filter);
5151
5152 if (show_users || show_proc_ctx || show_sock_ctx)
5153 user_ent_destroy();
5154
5155 render();
5156
5157 return 0;
5158 }