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