]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/ss.c
ss: Support displaying and filtering on socket marks.
[mirror_iproute2.git] / misc / ss.c
CommitLineData
b9de3ecf 1/*
aba5acdf
SH
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 <syslog.h>
16#include <fcntl.h>
17#include <sys/ioctl.h>
18#include <sys/socket.h>
19#include <sys/uio.h>
20#include <netinet/in.h>
21#include <string.h>
22#include <errno.h>
23#include <netdb.h>
24#include <arpa/inet.h>
aba5acdf
SH
25#include <dirent.h>
26#include <fnmatch.h>
ab61159a 27#include <getopt.h>
bf4ceee6 28#include <stdbool.h>
6b376ebd 29#include <limits.h>
aba5acdf
SH
30
31#include "utils.h"
32#include "rt_names.h"
33#include "ll_map.h"
34#include "libnetlink.h"
95ce04bc 35#include "namespace.h"
aba5acdf
SH
36#include "SNAPSHOT.h"
37
9cb1eccf 38#include <linux/tcp.h>
f6062360 39#include <linux/sock_diag.h>
351efcde 40#include <linux/inet_diag.h>
dfbaa90d 41#include <linux/unix_diag.h>
372c30d2
ND
42#include <linux/netdevice.h> /* for MAX_ADDR_LEN */
43#include <linux/filter.h>
44#include <linux/packet_diag.h>
ecb928c8 45#include <linux/netlink_diag.h>
aba5acdf 46
8a4025f6 47#define MAGIC_SEQ 123456
48
5fb421d4 49#define DIAG_REQUEST(_req, _r) \
50 struct { \
51 struct nlmsghdr nlh; \
52 _r; \
53 } _req = { \
54 .nlh = { \
55 .nlmsg_type = SOCK_DIAG_BY_FAMILY, \
56 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\
8a4025f6 57 .nlmsg_seq = MAGIC_SEQ, \
5fb421d4 58 .nlmsg_len = sizeof(_req), \
59 }, \
60 }
61
116ac927
RH
62#if HAVE_SELINUX
63#include <selinux/selinux.h>
64#else
65/* Stubs for SELinux functions */
66static int is_selinux_enabled(void)
67{
68 return -1;
69}
70
71static int getpidcon(pid_t pid, char **context)
72{
73 *context = NULL;
74 return -1;
75}
76
77static int getfilecon(char *path, char **context)
78{
79 *context = NULL;
80 return -1;
81}
82
83static int security_get_initial_context(char *name, char **context)
84{
85 *context = NULL;
86 return -1;
87}
88#endif
89
acd1e437 90int resolve_hosts;
aba5acdf
SH
91int resolve_services = 1;
92int preferred_family = AF_UNSPEC;
acd1e437
SH
93int show_options;
94int show_details;
95int show_users;
96int show_mem;
97int show_tcpinfo;
98int show_bpf;
99int show_proc_ctx;
100int show_sock_ctx;
7a4559f6 101int show_header = 1;
116ac927 102/* If show_users & show_proc_ctx only do user_ent_hash_build() once */
acd1e437
SH
103int user_ent_hash_build_init;
104int follow_events;
aba5acdf
SH
105
106int netid_width;
107int state_width;
108int addrp_width;
109int addr_width;
110int serv_width;
111int screen_width;
112
113static const char *TCP_PROTO = "tcp";
114static const char *UDP_PROTO = "udp";
115static const char *RAW_PROTO = "raw";
acd1e437 116static const char *dg_proto;
aba5acdf 117
acd1e437 118enum {
aba5acdf 119 TCP_DB,
351efcde 120 DCCP_DB,
aba5acdf
SH
121 UDP_DB,
122 RAW_DB,
123 UNIX_DG_DB,
124 UNIX_ST_DB,
30b669d7 125 UNIX_SQ_DB,
aba5acdf
SH
126 PACKET_DG_DB,
127 PACKET_R_DB,
128 NETLINK_DB,
129 MAX_DB
130};
131
132#define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
30b669d7 133#define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
aba5acdf 134#define ALL_DB ((1<<MAX_DB)-1)
9db7bf15 135#define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB))
aba5acdf
SH
136
137enum {
7d105b56
SH
138 SS_UNKNOWN,
139 SS_ESTABLISHED,
140 SS_SYN_SENT,
141 SS_SYN_RECV,
142 SS_FIN_WAIT1,
143 SS_FIN_WAIT2,
144 SS_TIME_WAIT,
145 SS_CLOSE,
146 SS_CLOSE_WAIT,
147 SS_LAST_ACK,
148 SS_LISTEN,
149 SS_CLOSING,
150 SS_MAX
aba5acdf
SH
151};
152
9db7bf15
VK
153#define SS_ALL ((1 << SS_MAX) - 1)
154#define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)))
aba5acdf
SH
155
156#include "ssfilter.h"
157
acd1e437 158struct filter {
aba5acdf
SH
159 int dbs;
160 int states;
161 int families;
162 struct ssfilter *f;
fb2594c1 163 bool kill;
aba5acdf
SH
164};
165
9db7bf15
VK
166static const struct filter default_dbs[MAX_DB] = {
167 [TCP_DB] = {
168 .states = SS_CONN,
169 .families = (1 << AF_INET) | (1 << AF_INET6),
170 },
171 [DCCP_DB] = {
172 .states = SS_CONN,
173 .families = (1 << AF_INET) | (1 << AF_INET6),
174 },
175 [UDP_DB] = {
f42a4574 176 .states = (1 << SS_ESTABLISHED),
9db7bf15
VK
177 .families = (1 << AF_INET) | (1 << AF_INET6),
178 },
179 [RAW_DB] = {
f42a4574 180 .states = (1 << SS_ESTABLISHED),
9db7bf15
VK
181 .families = (1 << AF_INET) | (1 << AF_INET6),
182 },
183 [UNIX_DG_DB] = {
184 .states = (1 << SS_CLOSE),
185 .families = (1 << AF_UNIX),
186 },
187 [UNIX_ST_DB] = {
188 .states = SS_CONN,
189 .families = (1 << AF_UNIX),
190 },
191 [UNIX_SQ_DB] = {
192 .states = SS_CONN,
193 .families = (1 << AF_UNIX),
194 },
195 [PACKET_DG_DB] = {
196 .states = (1 << SS_CLOSE),
197 .families = (1 << AF_PACKET),
198 },
199 [PACKET_R_DB] = {
200 .states = (1 << SS_CLOSE),
201 .families = (1 << AF_PACKET),
202 },
203 [NETLINK_DB] = {
204 .states = (1 << SS_CLOSE),
205 .families = (1 << AF_NETLINK),
206 },
aba5acdf
SH
207};
208
9db7bf15
VK
209static const struct filter default_afs[AF_MAX] = {
210 [AF_INET] = {
211 .dbs = INET_DBM,
212 .states = SS_CONN,
213 },
214 [AF_INET6] = {
215 .dbs = INET_DBM,
216 .states = SS_CONN,
217 },
218 [AF_UNIX] = {
219 .dbs = UNIX_DBM,
220 .states = SS_CONN,
221 },
222 [AF_PACKET] = {
223 .dbs = PACKET_DBM,
224 .states = (1 << SS_CLOSE),
225 },
226 [AF_NETLINK] = {
227 .dbs = (1 << NETLINK_DB),
228 .states = (1 << SS_CLOSE),
229 },
230};
231
232static int do_default = 1;
233static struct filter current_filter;
234
235static void filter_db_set(struct filter *f, int db)
236{
237 f->states |= default_dbs[db].states;
9db7bf15
VK
238 f->dbs |= 1 << db;
239 do_default = 0;
240}
241
242static void filter_af_set(struct filter *f, int af)
243{
1527a17e
VK
244 f->states |= default_afs[af].states;
245 f->families |= 1 << af;
246 do_default = 0;
247 preferred_family = af;
9db7bf15
VK
248}
249
250static int filter_af_get(struct filter *f, int af)
251{
252 return f->families & (1 << af);
253}
254
255static void filter_default_dbs(struct filter *f)
256{
257 filter_db_set(f, UDP_DB);
258 filter_db_set(f, DCCP_DB);
259 filter_db_set(f, TCP_DB);
260 filter_db_set(f, RAW_DB);
261 filter_db_set(f, UNIX_ST_DB);
262 filter_db_set(f, UNIX_DG_DB);
263 filter_db_set(f, UNIX_SQ_DB);
264 filter_db_set(f, PACKET_R_DB);
265 filter_db_set(f, PACKET_DG_DB);
266 filter_db_set(f, NETLINK_DB);
267}
268
57ff5a10 269static void filter_states_set(struct filter *f, int states)
9db7bf15 270{
57ff5a10 271 if (states)
9d320e1e 272 f->states = states;
57ff5a10 273}
9db7bf15 274
57ff5a10
VK
275static void filter_merge_defaults(struct filter *f)
276{
277 int db;
278 int af;
9db7bf15 279
57ff5a10
VK
280 for (db = 0; db < MAX_DB; db++) {
281 if (!(f->dbs & (1 << db)))
282 continue;
9db7bf15 283
57ff5a10
VK
284 if (!(default_dbs[db].families & f->families))
285 f->families |= default_dbs[db].families;
286 }
287 for (af = 0; af < AF_MAX; af++) {
288 if (!(f->families & (1 << af)))
289 continue;
290
291 if (!(default_afs[af].dbs & f->dbs))
292 f->dbs |= default_afs[af].dbs;
293 }
9db7bf15 294}
aba5acdf 295
ab01dbbb 296static FILE *generic_proc_open(const char *env, const char *name)
aba5acdf 297{
ab01dbbb 298 const char *p = getenv(env);
aba5acdf 299 char store[128];
ab01dbbb 300
aba5acdf
SH
301 if (!p) {
302 p = getenv("PROC_ROOT") ? : "/proc";
303 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
304 p = store;
305 }
ab01dbbb
SH
306
307 return fopen(p, "r");
aba5acdf
SH
308}
309
ab01dbbb 310static FILE *net_tcp_open(void)
aba5acdf
SH
311{
312 return generic_proc_open("PROC_NET_TCP", "net/tcp");
313}
314
ab01dbbb 315static FILE *net_tcp6_open(void)
aba5acdf
SH
316{
317 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
318}
319
ab01dbbb 320static FILE *net_udp_open(void)
aba5acdf
SH
321{
322 return generic_proc_open("PROC_NET_UDP", "net/udp");
323}
324
ab01dbbb 325static FILE *net_udp6_open(void)
aba5acdf
SH
326{
327 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
328}
329
ab01dbbb 330static FILE *net_raw_open(void)
aba5acdf
SH
331{
332 return generic_proc_open("PROC_NET_RAW", "net/raw");
333}
334
ab01dbbb 335static FILE *net_raw6_open(void)
aba5acdf
SH
336{
337 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
338}
339
ab01dbbb 340static FILE *net_unix_open(void)
aba5acdf
SH
341{
342 return generic_proc_open("PROC_NET_UNIX", "net/unix");
343}
344
ab01dbbb 345static FILE *net_packet_open(void)
aba5acdf
SH
346{
347 return generic_proc_open("PROC_NET_PACKET", "net/packet");
348}
349
ab01dbbb 350static FILE *net_netlink_open(void)
aba5acdf
SH
351{
352 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
353}
354
ab01dbbb 355static FILE *slabinfo_open(void)
aba5acdf
SH
356{
357 return generic_proc_open("PROC_SLABINFO", "slabinfo");
358}
359
ab01dbbb 360static FILE *net_sockstat_open(void)
aba5acdf
SH
361{
362 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
363}
364
ab01dbbb 365static FILE *net_sockstat6_open(void)
aba5acdf
SH
366{
367 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
368}
369
ab01dbbb 370static FILE *net_snmp_open(void)
aba5acdf
SH
371{
372 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
373}
374
ab01dbbb 375static FILE *ephemeral_ports_open(void)
aba5acdf
SH
376{
377 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
378}
379
fbc0f876
SF
380struct user_ent {
381 struct user_ent *next;
382 unsigned int ino;
383 int pid;
384 int fd;
116ac927
RH
385 char *process;
386 char *process_ctx;
387 char *socket_ctx;
fbc0f876
SF
388};
389
390#define USER_ENT_HASH_SIZE 256
391struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
392
393static int user_ent_hashfn(unsigned int ino)
aba5acdf 394{
fbc0f876 395 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
aba5acdf 396
fbc0f876
SF
397 return val & (USER_ENT_HASH_SIZE - 1);
398}
399
116ac927
RH
400static void user_ent_add(unsigned int ino, char *process,
401 int pid, int fd,
402 char *proc_ctx,
403 char *sock_ctx)
fbc0f876
SF
404{
405 struct user_ent *p, **pp;
aba5acdf 406
116ac927
RH
407 p = malloc(sizeof(struct user_ent));
408 if (!p) {
409 fprintf(stderr, "ss: failed to malloc buffer\n");
fbc0f876 410 abort();
116ac927 411 }
fbc0f876
SF
412 p->next = NULL;
413 p->ino = ino;
414 p->pid = pid;
415 p->fd = fd;
116ac927
RH
416 p->process = strdup(process);
417 p->process_ctx = strdup(proc_ctx);
418 p->socket_ctx = strdup(sock_ctx);
fbc0f876
SF
419
420 pp = &user_ent_hash[user_ent_hashfn(ino)];
421 p->next = *pp;
422 *pp = p;
423}
aba5acdf 424
116ac927
RH
425static void user_ent_destroy(void)
426{
427 struct user_ent *p, *p_next;
428 int cnt = 0;
429
430 while (cnt != USER_ENT_HASH_SIZE) {
431 p = user_ent_hash[cnt];
432 while (p) {
433 free(p->process);
434 free(p->process_ctx);
435 free(p->socket_ctx);
436 p_next = p->next;
437 free(p);
438 p = p_next;
439 }
440 cnt++;
441 }
442}
443
fbc0f876
SF
444static void user_ent_hash_build(void)
445{
446 const char *root = getenv("PROC_ROOT") ? : "/proc/";
447 struct dirent *d;
448 char name[1024];
449 int nameoff;
450 DIR *dir;
116ac927
RH
451 char *pid_context;
452 char *sock_context;
453 const char *no_ctx = "unavailable";
454
455 /* If show_users & show_proc_ctx set only do this once */
456 if (user_ent_hash_build_init != 0)
457 return;
458
459 user_ent_hash_build_init = 1;
fbc0f876 460
0ee9052f 461 strncpy(name, root, sizeof(name)-1);
462 name[sizeof(name)-1] = 0;
463
fbc0f876 464 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
aba5acdf 465 strcat(name, "/");
fbc0f876 466
aba5acdf 467 nameoff = strlen(name);
fbc0f876
SF
468
469 dir = opendir(name);
470 if (!dir)
471 return;
aba5acdf
SH
472
473 while ((d = readdir(dir)) != NULL) {
aba5acdf 474 struct dirent *d1;
aba5acdf 475 char process[16];
116ac927 476 char *p;
fbc0f876
SF
477 int pid, pos;
478 DIR *dir1;
479 char crap;
aba5acdf
SH
480
481 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
482 continue;
483
116ac927
RH
484 if (getpidcon(pid, &pid_context) != 0)
485 pid_context = strdup(no_ctx);
486
0ee9052f 487 snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
aba5acdf 488 pos = strlen(name);
a02371fb
PS
489 if ((dir1 = opendir(name)) == NULL) {
490 free(pid_context);
aba5acdf 491 continue;
a02371fb 492 }
aba5acdf 493
fbc0f876 494 process[0] = '\0';
116ac927 495 p = process;
aba5acdf
SH
496
497 while ((d1 = readdir(dir1)) != NULL) {
fbc0f876
SF
498 const char *pattern = "socket:[";
499 unsigned int ino;
aba5acdf 500 char lnk[64];
18445b3e 501 int fd;
788731b3 502 ssize_t link_len;
116ac927 503 char tmp[1024];
aba5acdf
SH
504
505 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
506 continue;
507
0ee9052f 508 snprintf(name+pos, sizeof(name) - pos, "%d", fd);
788731b3
TJ
509
510 link_len = readlink(name, lnk, sizeof(lnk)-1);
511 if (link_len == -1)
512 continue;
513 lnk[link_len] = '\0';
514
515 if (strncmp(lnk, pattern, strlen(pattern)))
aba5acdf
SH
516 continue;
517
fbc0f876 518 sscanf(lnk, "socket:[%u]", &ino);
aba5acdf 519
116ac927
RH
520 snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
521 root, pid, d1->d_name);
522
523 if (getfilecon(tmp, &sock_context) <= 0)
524 sock_context = strdup(no_ctx);
525
526 if (*p == '\0') {
aba5acdf 527 FILE *fp;
fbc0f876 528
116ac927
RH
529 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
530 root, pid);
aba5acdf 531 if ((fp = fopen(tmp, "r")) != NULL) {
d572ed4d
PS
532 if (fscanf(fp, "%*d (%[^)])", p) < 1)
533 ; /* ignore */
aba5acdf
SH
534 fclose(fp);
535 }
536 }
116ac927
RH
537 user_ent_add(ino, p, pid, fd,
538 pid_context, sock_context);
539 free(sock_context);
aba5acdf 540 }
116ac927 541 free(pid_context);
aba5acdf
SH
542 closedir(dir1);
543 }
544 closedir(dir);
fbc0f876
SF
545}
546
116ac927
RH
547enum entry_types {
548 USERS,
549 PROC_CTX,
550 PROC_SOCK_CTX
551};
552
553#define ENTRY_BUF_SIZE 512
acd1e437 554static int find_entry(unsigned int ino, char **buf, int type)
fbc0f876
SF
555{
556 struct user_ent *p;
557 int cnt = 0;
558 char *ptr;
532ca40a 559 char *new_buf;
116ac927
RH
560 int len, new_buf_len;
561 int buf_used = 0;
562 int buf_len = 0;
fbc0f876
SF
563
564 if (!ino)
565 return 0;
566
567 p = user_ent_hash[user_ent_hashfn(ino)];
116ac927 568 ptr = *buf = NULL;
fbc0f876
SF
569 while (p) {
570 if (p->ino != ino)
571 goto next;
572
116ac927
RH
573 while (1) {
574 ptr = *buf + buf_used;
575 switch (type) {
576 case USERS:
577 len = snprintf(ptr, buf_len - buf_used,
578 "(\"%s\",pid=%d,fd=%d),",
579 p->process, p->pid, p->fd);
580 break;
581 case PROC_CTX:
582 len = snprintf(ptr, buf_len - buf_used,
583 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
584 p->process, p->pid,
585 p->process_ctx, p->fd);
586 break;
587 case PROC_SOCK_CTX:
588 len = snprintf(ptr, buf_len - buf_used,
589 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
590 p->process, p->pid,
591 p->process_ctx, p->fd,
592 p->socket_ctx);
593 break;
594 default:
595 fprintf(stderr, "ss: invalid type: %d\n", type);
596 abort();
597 }
fbc0f876 598
116ac927
RH
599 if (len < 0 || len >= buf_len - buf_used) {
600 new_buf_len = buf_len + ENTRY_BUF_SIZE;
532ca40a 601 new_buf = realloc(*buf, new_buf_len);
116ac927
RH
602 if (!new_buf) {
603 fprintf(stderr, "ss: failed to malloc buffer\n");
604 abort();
605 }
532ca40a 606 *buf = new_buf;
116ac927
RH
607 buf_len = new_buf_len;
608 continue;
609 } else {
610 buf_used += len;
611 break;
612 }
613 }
fbc0f876 614 cnt++;
116ac927 615next:
fbc0f876
SF
616 p = p->next;
617 }
116ac927
RH
618 if (buf_used) {
619 ptr = *buf + buf_used;
fbc0f876 620 ptr[-1] = '\0';
116ac927 621 }
aba5acdf
SH
622 return cnt;
623}
624
aba5acdf
SH
625/* Get stats from slab */
626
acd1e437 627struct slabstat {
aba5acdf
SH
628 int socks;
629 int tcp_ports;
630 int tcp_tws;
631 int tcp_syns;
632 int skbs;
633};
634
a221d621 635static struct slabstat slabstat;
aba5acdf 636
acd1e437
SH
637static const char *slabstat_ids[] = {
638
aba5acdf
SH
639 "sock",
640 "tcp_bind_bucket",
641 "tcp_tw_bucket",
642 "tcp_open_request",
643 "skbuff_head_cache",
644};
645
d1f28cf1 646static int get_slabstat(struct slabstat *s)
aba5acdf
SH
647{
648 char buf[256];
649 FILE *fp;
650 int cnt;
a221d621
BL
651 static int slabstat_valid;
652
653 if (slabstat_valid)
654 return 0;
aba5acdf
SH
655
656 memset(s, 0, sizeof(*s));
657
ab01dbbb
SH
658 fp = slabinfo_open();
659 if (!fp)
aba5acdf
SH
660 return -1;
661
662 cnt = sizeof(*s)/sizeof(int);
663
d572ed4d
PS
664 if (!fgets(buf, sizeof(buf), fp)) {
665 fclose(fp);
666 return -1;
667 }
acd1e437 668 while (fgets(buf, sizeof(buf), fp) != NULL) {
aba5acdf 669 int i;
acd1e437 670
62000e51 671 for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) {
aba5acdf
SH
672 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
673 sscanf(buf, "%*s%d", ((int *)s) + i);
674 cnt--;
675 break;
676 }
677 }
678 if (cnt <= 0)
679 break;
680 }
681
a221d621
BL
682 slabstat_valid = 1;
683
aba5acdf
SH
684 fclose(fp);
685 return 0;
686}
687
2e7e805d 688static unsigned long long cookie_sk_get(const uint32_t *cookie)
f1b39e1b 689{
2e7e805d 690 return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
f1b39e1b
VK
691}
692
7d105b56
SH
693static const char *sstate_name[] = {
694 "UNKNOWN",
9cb1eccf
ED
695 [SS_ESTABLISHED] = "ESTAB",
696 [SS_SYN_SENT] = "SYN-SENT",
697 [SS_SYN_RECV] = "SYN-RECV",
698 [SS_FIN_WAIT1] = "FIN-WAIT-1",
699 [SS_FIN_WAIT2] = "FIN-WAIT-2",
700 [SS_TIME_WAIT] = "TIME-WAIT",
701 [SS_CLOSE] = "UNCONN",
702 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
703 [SS_LAST_ACK] = "LAST-ACK",
acd1e437 704 [SS_LISTEN] = "LISTEN",
9cb1eccf 705 [SS_CLOSING] = "CLOSING",
aba5acdf
SH
706};
707
7d105b56
SH
708static const char *sstate_namel[] = {
709 "UNKNOWN",
9cb1eccf
ED
710 [SS_ESTABLISHED] = "established",
711 [SS_SYN_SENT] = "syn-sent",
712 [SS_SYN_RECV] = "syn-recv",
713 [SS_FIN_WAIT1] = "fin-wait-1",
714 [SS_FIN_WAIT2] = "fin-wait-2",
715 [SS_TIME_WAIT] = "time-wait",
716 [SS_CLOSE] = "unconnected",
717 [SS_CLOSE_WAIT] = "close-wait",
718 [SS_LAST_ACK] = "last-ack",
acd1e437 719 [SS_LISTEN] = "listening",
9cb1eccf 720 [SS_CLOSING] = "closing",
aba5acdf
SH
721};
722
acd1e437 723struct sockstat {
ec4d0d8a
VK
724 struct sockstat *next;
725 unsigned int type;
89f634f9 726 uint16_t prot;
8250bc9f
VK
727 inet_prefix local;
728 inet_prefix remote;
729 int lport;
730 int rport;
731 int state;
732 int rq, wq;
acd1e437
SH
733 unsigned int ino;
734 unsigned int uid;
8250bc9f
VK
735 int refcnt;
736 unsigned int iface;
737 unsigned long long sk;
99bb68ff
VK
738 char *name;
739 char *peer_name;
ec75249b 740 __u32 mark;
055840f2
VK
741};
742
acd1e437 743struct dctcpstat {
055840f2
VK
744 unsigned int ce_state;
745 unsigned int alpha;
746 unsigned int ab_ecn;
747 unsigned int ab_tot;
748 bool enabled;
749};
750
acd1e437 751struct tcpstat {
055840f2 752 struct sockstat ss;
8250bc9f
VK
753 int timer;
754 int timeout;
755 int probes;
d2055ea5 756 char cong_alg[16];
8250bc9f
VK
757 double rto, ato, rtt, rttvar;
758 int qack, cwnd, ssthresh, backoff;
759 double send_bps;
760 int snd_wscale;
761 int rcv_wscale;
762 int mss;
763 unsigned int lastsnd;
764 unsigned int lastrcv;
765 unsigned int lastack;
766 double pacing_rate;
767 double pacing_rate_max;
1a4dda71
ED
768 unsigned long long bytes_acked;
769 unsigned long long bytes_received;
ecb435ea
CG
770 unsigned int segs_out;
771 unsigned int segs_in;
414aeec9
MKL
772 unsigned int data_segs_out;
773 unsigned int data_segs_in;
8250bc9f
VK
774 unsigned int unacked;
775 unsigned int retrans;
776 unsigned int retrans_total;
777 unsigned int lost;
778 unsigned int sacked;
779 unsigned int fackets;
780 unsigned int reordering;
9e99e495 781 unsigned int not_sent;
8250bc9f 782 double rcv_rtt;
9e99e495 783 double min_rtt;
8250bc9f
VK
784 int rcv_space;
785 bool has_ts_opt;
786 bool has_sack_opt;
787 bool has_ecn_opt;
788 bool has_ecnseen_opt;
789 bool has_fastopen_opt;
790 bool has_wscale_opt;
791 struct dctcpstat *dctcp;
2f0f9aef 792 struct tcp_bbr_info *bbr_info;
aba5acdf
SH
793};
794
2d791bc8
VK
795static void sock_state_print(struct sockstat *s, const char *sock_name)
796{
797 if (netid_width)
798 printf("%-*s ", netid_width, sock_name);
799 if (state_width)
800 printf("%-*s ", state_width, sstate_name[s->state]);
801
802 printf("%-6d %-6d ", s->rq, s->wq);
803}
804
f1b39e1b
VK
805static void sock_details_print(struct sockstat *s)
806{
807 if (s->uid)
808 printf(" uid:%u", s->uid);
809
810 printf(" ino:%u", s->ino);
811 printf(" sk:%llx", s->sk);
ec75249b
LC
812
813 if (s->mark)
814 printf(" fwmark:0x%x", s->mark);
f1b39e1b
VK
815}
816
b217df10
VK
817static void sock_addr_print_width(int addr_len, const char *addr, char *delim,
818 int port_len, const char *port, const char *ifname)
819{
820 if (ifname) {
821 printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim,
822 port_len, port);
acd1e437 823 } else {
b217df10
VK
824 printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port);
825 }
826}
827
828static void sock_addr_print(const char *addr, char *delim, const char *port,
829 const char *ifname)
830{
831 sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname);
832}
833
7d105b56 834static const char *tmr_name[] = {
aba5acdf
SH
835 "off",
836 "on",
837 "keepalive",
838 "timewait",
839 "persist",
840 "unknown"
841};
842
d1f28cf1 843static const char *print_ms_timer(int timeout)
aba5acdf
SH
844{
845 static char buf[64];
846 int secs, msecs, minutes;
acd1e437 847
aba5acdf
SH
848 if (timeout < 0)
849 timeout = 0;
850 secs = timeout/1000;
851 minutes = secs/60;
852 secs = secs%60;
853 msecs = timeout%1000;
854 buf[0] = 0;
855 if (minutes) {
856 msecs = 0;
857 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
858 if (minutes > 9)
859 secs = 0;
860 }
861 if (secs) {
862 if (secs > 9)
863 msecs = 0;
864 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
865 }
866 if (msecs)
867 sprintf(buf+strlen(buf), "%03dms", msecs);
868 return buf;
e7113c61 869}
aba5acdf 870
22588a0e 871struct scache {
aba5acdf
SH
872 struct scache *next;
873 int port;
874 char *name;
875 const char *proto;
876};
877
878struct scache *rlist;
879
d1f28cf1 880static void init_service_resolver(void)
aba5acdf
SH
881{
882 char buf[128];
883 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
596307ea
PS
884
885 if (!fp)
886 return;
887
888 if (!fgets(buf, sizeof(buf), fp)) {
2bcc3c16 889 pclose(fp);
596307ea
PS
890 return;
891 }
892 while (fgets(buf, sizeof(buf), fp) != NULL) {
893 unsigned int progn, port;
894 char proto[128], prog[128] = "rpc.";
895 struct scache *c;
896
897 if (sscanf(buf, "%u %*d %s %u %s",
acd1e437 898 &progn, proto, &port, prog+4) != 4)
596307ea
PS
899 continue;
900
901 if (!(c = malloc(sizeof(*c))))
902 continue;
903
904 c->port = port;
905 c->name = strdup(prog);
906 if (strcmp(proto, TCP_PROTO) == 0)
907 c->proto = TCP_PROTO;
908 else if (strcmp(proto, UDP_PROTO) == 0)
909 c->proto = UDP_PROTO;
910 else
911 c->proto = NULL;
912 c->next = rlist;
913 rlist = c;
aba5acdf 914 }
596307ea 915 pclose(fp);
aba5acdf
SH
916}
917
ab61159a
SH
918/* Even do not try default linux ephemeral port ranges:
919 * default /etc/services contains so much of useless crap
920 * wouldbe "allocated" to this area that resolution
921 * is really harmful. I shrug each time when seeing
922 * "socks" or "cfinger" in dumps.
923 */
924static int is_ephemeral(int port)
925{
acd1e437 926 static int min = 0, max;
c29d3792
PS
927
928 if (!min) {
ab01dbbb 929 FILE *f = ephemeral_ports_open();
acd1e437 930
c29d3792
PS
931 if (!f || fscanf(f, "%d %d", &min, &max) < 2) {
932 min = 1024;
933 max = 4999;
ab61159a 934 }
c29d3792
PS
935 if (f)
936 fclose(f);
ab61159a 937 }
c29d3792 938 return port >= min && port <= max;
ab61159a
SH
939}
940
941
d1f28cf1 942static const char *__resolve_service(int port)
aba5acdf
SH
943{
944 struct scache *c;
945
946 for (c = rlist; c; c = c->next) {
947 if (c->port == port && c->proto == dg_proto)
948 return c->name;
949 }
950
ab61159a 951 if (!is_ephemeral(port)) {
aba5acdf
SH
952 static int notfirst;
953 struct servent *se;
acd1e437 954
aba5acdf
SH
955 if (!notfirst) {
956 setservent(1);
957 notfirst = 1;
ae665a52 958 }
aba5acdf
SH
959 se = getservbyport(htons(port), dg_proto);
960 if (se)
961 return se->s_name;
962 }
963
964 return NULL;
965}
966
22588a0e
ED
967#define SCACHE_BUCKETS 1024
968static struct scache *cache_htab[SCACHE_BUCKETS];
aba5acdf 969
d1f28cf1 970static const char *resolve_service(int port)
aba5acdf
SH
971{
972 static char buf[128];
22588a0e
ED
973 struct scache *c;
974 const char *res;
975 int hash;
aba5acdf
SH
976
977 if (port == 0) {
978 buf[0] = '*';
979 buf[1] = 0;
980 return buf;
981 }
982
22588a0e
ED
983 if (!resolve_services)
984 goto do_numeric;
aba5acdf 985
22588a0e
ED
986 if (dg_proto == RAW_PROTO)
987 return inet_proto_n2a(port, buf, sizeof(buf));
988
989
990 hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS;
991
992 for (c = cache_htab[hash]; c; c = c->next) {
993 if (c->port == port && c->proto == dg_proto)
994 goto do_cache;
aba5acdf
SH
995 }
996
22588a0e
ED
997 c = malloc(sizeof(*c));
998 if (!c)
999 goto do_numeric;
1000 res = __resolve_service(port);
1001 c->port = port;
1002 c->name = res ? strdup(res) : NULL;
1003 c->proto = dg_proto;
1004 c->next = cache_htab[hash];
1005 cache_htab[hash] = c;
1006
1007do_cache:
1008 if (c->name)
1009 return c->name;
1010
1011do_numeric:
aba5acdf
SH
1012 sprintf(buf, "%u", port);
1013 return buf;
1014}
1015
b217df10 1016static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex)
aba5acdf
SH
1017{
1018 char buf[1024];
1019 const char *ap = buf;
b217df10
VK
1020 int est_len = addr_width;
1021 const char *ifname = NULL;
aba5acdf
SH
1022
1023 if (a->family == AF_INET) {
1024 if (a->data[0] == 0) {
1025 buf[0] = '*';
1026 buf[1] = 0;
1027 } else {
a418e451 1028 ap = format_host(AF_INET, 4, a->data);
aba5acdf
SH
1029 }
1030 } else {
a418e451 1031 ap = format_host(a->family, 16, a->data);
aba5acdf
SH
1032 est_len = strlen(ap);
1033 if (est_len <= addr_width)
1034 est_len = addr_width;
1035 else
1036 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
1037 }
b217df10 1038
7c8a3cfb 1039 if (ifindex) {
b217df10
VK
1040 ifname = ll_index_to_name(ifindex);
1041 est_len -= strlen(ifname) + 1; /* +1 for percent char */
4fcfb6bc
MS
1042 if (est_len < 0)
1043 est_len = 0;
b217df10 1044 }
7c8a3cfb 1045
b217df10
VK
1046 sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port),
1047 ifname);
aba5acdf
SH
1048}
1049
acd1e437 1050struct aafilter {
aba5acdf
SH
1051 inet_prefix addr;
1052 int port;
2d293212 1053 unsigned int iface;
ec75249b
LC
1054 __u32 mark;
1055 __u32 mask;
aba5acdf
SH
1056 struct aafilter *next;
1057};
1058
d1f28cf1
SH
1059static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
1060 int plen)
aba5acdf
SH
1061{
1062 if (!inet_addr_match(a, p, plen))
1063 return 0;
7d105b56 1064
aba5acdf
SH
1065 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1066 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1067 * sockets. Fair? */
1068 if (p->family == AF_INET && a->family == AF_INET6) {
1069 if (a->data[0] == 0 && a->data[1] == 0 &&
1070 a->data[2] == htonl(0xffff)) {
1071 inet_prefix tmp = *a;
acd1e437 1072
aba5acdf
SH
1073 tmp.data[0] = a->data[3];
1074 return inet_addr_match(&tmp, p, plen);
1075 }
1076 }
1077 return 1;
1078}
1079
d1f28cf1 1080static int unix_match(const inet_prefix *a, const inet_prefix *p)
aba5acdf 1081{
99bb68ff 1082 char *addr, *pattern;
acd1e437 1083
99bb68ff
VK
1084 memcpy(&addr, a->data, sizeof(addr));
1085 memcpy(&pattern, p->data, sizeof(pattern));
aba5acdf
SH
1086 if (pattern == NULL)
1087 return 1;
1088 if (addr == NULL)
1089 addr = "";
1090 return !fnmatch(pattern, addr, 0);
1091}
1092
055840f2 1093static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
aba5acdf
SH
1094{
1095 switch (f->type) {
1096 case SSF_S_AUTO:
1097 {
aba5acdf 1098 if (s->local.family == AF_UNIX) {
99bb68ff 1099 char *p;
acd1e437 1100
99bb68ff 1101 memcpy(&p, s->local.data, sizeof(p));
aba5acdf 1102 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
ae665a52 1103 strspn(p+1, "0123456789abcdef") == 5);
aba5acdf
SH
1104 }
1105 if (s->local.family == AF_PACKET)
bbd303d1 1106 return s->lport == 0 && s->local.data[0] == 0;
aba5acdf
SH
1107 if (s->local.family == AF_NETLINK)
1108 return s->lport < 0;
1109
c29d3792 1110 return is_ephemeral(s->lport);
aba5acdf
SH
1111 }
1112 case SSF_DCOND:
1113 {
acd1e437
SH
1114 struct aafilter *a = (void *)f->pred;
1115
aba5acdf
SH
1116 if (a->addr.family == AF_UNIX)
1117 return unix_match(&s->remote, &a->addr);
1118 if (a->port != -1 && a->port != s->rport)
1119 return 0;
1120 if (a->addr.bitlen) {
1121 do {
1122 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
1123 return 1;
1124 } while ((a = a->next) != NULL);
1125 return 0;
1126 }
1127 return 1;
1128 }
1129 case SSF_SCOND:
1130 {
acd1e437
SH
1131 struct aafilter *a = (void *)f->pred;
1132
aba5acdf
SH
1133 if (a->addr.family == AF_UNIX)
1134 return unix_match(&s->local, &a->addr);
1135 if (a->port != -1 && a->port != s->lport)
1136 return 0;
1137 if (a->addr.bitlen) {
1138 do {
1139 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
1140 return 1;
ae665a52 1141 } while ((a = a->next) != NULL);
aba5acdf
SH
1142 return 0;
1143 }
1144 return 1;
1145 }
1146 case SSF_D_GE:
1147 {
acd1e437
SH
1148 struct aafilter *a = (void *)f->pred;
1149
aba5acdf
SH
1150 return s->rport >= a->port;
1151 }
1152 case SSF_D_LE:
1153 {
acd1e437
SH
1154 struct aafilter *a = (void *)f->pred;
1155
aba5acdf
SH
1156 return s->rport <= a->port;
1157 }
1158 case SSF_S_GE:
1159 {
acd1e437
SH
1160 struct aafilter *a = (void *)f->pred;
1161
aba5acdf
SH
1162 return s->lport >= a->port;
1163 }
1164 case SSF_S_LE:
1165 {
acd1e437
SH
1166 struct aafilter *a = (void *)f->pred;
1167
aba5acdf
SH
1168 return s->lport <= a->port;
1169 }
2d293212
DA
1170 case SSF_DEVCOND:
1171 {
1172 struct aafilter *a = (void *)f->pred;
aba5acdf 1173
2d293212 1174 return s->iface == a->iface;
ec75249b
LC
1175 }
1176 case SSF_MARKMASK:
1177 {
1178 struct aafilter *a = (void *)f->pred;
1179
1180 return (s->mark & a->mask) == a->mark;
2d293212 1181 }
aba5acdf
SH
1182 /* Yup. It is recursion. Sorry. */
1183 case SSF_AND:
1184 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
1185 case SSF_OR:
1186 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
1187 case SSF_NOT:
1188 return !run_ssfilter(f->pred, s);
1189 default:
1190 abort();
1191 }
1192}
1193
ae665a52 1194/* Relocate external jumps by reloc. */
b4b0b7d5 1195static void ssfilter_patch(char *a, int len, int reloc)
aba5acdf
SH
1196{
1197 while (len > 0) {
acd1e437
SH
1198 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a;
1199
aba5acdf
SH
1200 if (op->no == len+4)
1201 op->no += reloc;
1202 len -= op->yes;
1203 a += op->yes;
1204 }
1205 if (len < 0)
1206 abort();
1207}
1208
b4b0b7d5 1209static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
aba5acdf
SH
1210{
1211 switch (f->type) {
1212 case SSF_S_AUTO:
1213 {
acd1e437
SH
1214 if (!(*bytecode = malloc(4))) abort();
1215 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
df39de8d 1216 return 4;
aba5acdf
SH
1217 }
1218 case SSF_DCOND:
1219 case SSF_SCOND:
1220 {
acd1e437 1221 struct aafilter *a = (void *)f->pred;
aba5acdf
SH
1222 struct aafilter *b;
1223 char *ptr;
351efcde 1224 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
aba5acdf
SH
1225 int len = 0;
1226
acd1e437 1227 for (b = a; b; b = b->next) {
351efcde 1228 len += 4 + sizeof(struct inet_diag_hostcond);
aba5acdf
SH
1229 if (a->addr.family == AF_INET6)
1230 len += 16;
1231 else
1232 len += 4;
1233 if (b->next)
1234 len += 4;
1235 }
1236 if (!(ptr = malloc(len))) abort();
1237 *bytecode = ptr;
acd1e437 1238 for (b = a; b; b = b->next) {
351efcde 1239 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
aba5acdf 1240 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
351efcde 1241 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
acd1e437 1242 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4);
aba5acdf 1243
351efcde 1244 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
aba5acdf
SH
1245 cond->family = a->addr.family;
1246 cond->port = a->port;
1247 cond->prefix_len = a->addr.bitlen;
1248 memcpy(cond->addr, a->addr.data, alen);
1249 ptr += oplen;
1250 if (b->next) {
351efcde
SH
1251 op = (struct inet_diag_bc_op *)ptr;
1252 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
aba5acdf
SH
1253 ptr += 4;
1254 }
1255 }
1256 return ptr - *bytecode;
1257 }
1258 case SSF_D_GE:
1259 {
acd1e437
SH
1260 struct aafilter *x = (void *)f->pred;
1261
1262 if (!(*bytecode = malloc(8))) abort();
1263 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
1264 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
aba5acdf
SH
1265 return 8;
1266 }
1267 case SSF_D_LE:
1268 {
acd1e437
SH
1269 struct aafilter *x = (void *)f->pred;
1270
1271 if (!(*bytecode = malloc(8))) abort();
1272 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
1273 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
aba5acdf
SH
1274 return 8;
1275 }
1276 case SSF_S_GE:
1277 {
acd1e437
SH
1278 struct aafilter *x = (void *)f->pred;
1279
1280 if (!(*bytecode = malloc(8))) abort();
1281 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
1282 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
aba5acdf
SH
1283 return 8;
1284 }
1285 case SSF_S_LE:
1286 {
acd1e437
SH
1287 struct aafilter *x = (void *)f->pred;
1288
1289 if (!(*bytecode = malloc(8))) abort();
1290 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
1291 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
aba5acdf
SH
1292 return 8;
1293 }
1294
1295 case SSF_AND:
1296 {
376fb868 1297 char *a1 = NULL, *a2 = NULL, *a;
2a4fa1c3 1298 int l1, l2;
acd1e437 1299
aba5acdf
SH
1300 l1 = ssfilter_bytecompile(f->pred, &a1);
1301 l2 = ssfilter_bytecompile(f->post, &a2);
376fb868
DA
1302 if (!l1 || !l2) {
1303 free(a1);
1304 free(a2);
1305 return 0;
1306 }
aba5acdf
SH
1307 if (!(a = malloc(l1+l2))) abort();
1308 memcpy(a, a1, l1);
1309 memcpy(a+l1, a2, l2);
1310 free(a1); free(a2);
1311 ssfilter_patch(a, l1, l2);
1312 *bytecode = a;
1313 return l1+l2;
1314 }
1315 case SSF_OR:
1316 {
376fb868 1317 char *a1 = NULL, *a2 = NULL, *a;
2a4fa1c3 1318 int l1, l2;
acd1e437 1319
aba5acdf
SH
1320 l1 = ssfilter_bytecompile(f->pred, &a1);
1321 l2 = ssfilter_bytecompile(f->post, &a2);
376fb868
DA
1322 if (!l1 || !l2) {
1323 free(a1);
1324 free(a2);
1325 return 0;
1326 }
aba5acdf
SH
1327 if (!(a = malloc(l1+l2+4))) abort();
1328 memcpy(a, a1, l1);
1329 memcpy(a+l1+4, a2, l2);
1330 free(a1); free(a2);
acd1e437 1331 *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
aba5acdf
SH
1332 *bytecode = a;
1333 return l1+l2+4;
1334 }
1335 case SSF_NOT:
1336 {
376fb868 1337 char *a1 = NULL, *a;
2a4fa1c3 1338 int l1;
acd1e437 1339
aba5acdf 1340 l1 = ssfilter_bytecompile(f->pred, &a1);
376fb868
DA
1341 if (!l1) {
1342 free(a1);
1343 return 0;
1344 }
aba5acdf
SH
1345 if (!(a = malloc(l1+4))) abort();
1346 memcpy(a, a1, l1);
1347 free(a1);
acd1e437 1348 *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
aba5acdf
SH
1349 *bytecode = a;
1350 return l1+4;
2d293212
DA
1351 }
1352 case SSF_DEVCOND:
1353 {
1354 /* bytecompile for SSF_DEVCOND not supported yet */
1355 return 0;
ec75249b
LC
1356 }
1357 case SSF_MARKMASK:
1358 {
1359 struct aafilter *a = (void *)f->pred;
1360 struct instr {
1361 struct inet_diag_bc_op op;
1362 struct inet_diag_markcond cond;
1363 };
1364 int inslen = sizeof(struct instr);
1365
1366 if (!(*bytecode = malloc(inslen))) abort();
1367 ((struct instr *)*bytecode)[0] = (struct instr) {
1368 { INET_DIAG_BC_MARK_COND, inslen, inslen + 4 },
1369 { a->mark, a->mask},
1370 };
1371
1372 return inslen;
aba5acdf
SH
1373 }
1374 default:
1375 abort();
1376 }
1377}
1378
b4b0b7d5 1379static int remember_he(struct aafilter *a, struct hostent *he)
aba5acdf 1380{
ae665a52 1381 char **ptr = he->h_addr_list;
aba5acdf
SH
1382 int cnt = 0;
1383 int len;
1384
1385 if (he->h_addrtype == AF_INET)
1386 len = 4;
1387 else if (he->h_addrtype == AF_INET6)
1388 len = 16;
1389 else
1390 return 0;
1391
1392 while (*ptr) {
1393 struct aafilter *b = a;
acd1e437 1394
aba5acdf
SH
1395 if (a->addr.bitlen) {
1396 if ((b = malloc(sizeof(*b))) == NULL)
1397 return cnt;
1398 *b = *a;
1399 b->next = a->next;
1400 a->next = b;
1401 }
1402 memcpy(b->addr.data, *ptr, len);
1403 b->addr.bytelen = len;
1404 b->addr.bitlen = len*8;
1405 b->addr.family = he->h_addrtype;
1406 ptr++;
1407 cnt++;
1408 }
1409 return cnt;
1410}
1411
b4b0b7d5 1412static int get_dns_host(struct aafilter *a, const char *addr, int fam)
aba5acdf
SH
1413{
1414 static int notfirst;
1415 int cnt = 0;
1416 struct hostent *he;
1417
1418 a->addr.bitlen = 0;
1419 if (!notfirst) {
1420 sethostent(1);
1421 notfirst = 1;
1422 }
1423 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
1424 if (he)
1425 cnt = remember_he(a, he);
1426 if (fam == AF_UNSPEC) {
1427 he = gethostbyname2(addr, AF_INET6);
1428 if (he)
1429 cnt += remember_he(a, he);
1430 }
1431 return !cnt;
1432}
1433
acd1e437 1434static int xll_initted;
aba5acdf 1435
b4b0b7d5 1436static void xll_init(void)
aba5acdf
SH
1437{
1438 struct rtnl_handle rth;
acd1e437 1439
d2468da0
SH
1440 if (rtnl_open(&rth, 0) < 0)
1441 exit(1);
1442
aba5acdf
SH
1443 ll_init_map(&rth);
1444 rtnl_close(&rth);
1445 xll_initted = 1;
1446}
1447
b4b0b7d5 1448static const char *xll_index_to_name(int index)
aba5acdf
SH
1449{
1450 if (!xll_initted)
1451 xll_init();
1452 return ll_index_to_name(index);
1453}
1454
b4b0b7d5 1455static int xll_name_to_index(const char *dev)
aba5acdf
SH
1456{
1457 if (!xll_initted)
1458 xll_init();
1459 return ll_name_to_index(dev);
1460}
1461
2d293212
DA
1462void *parse_devcond(char *name)
1463{
1464 struct aafilter a = { .iface = 0 };
1465 struct aafilter *res;
1466
1467 a.iface = xll_name_to_index(name);
1468 if (a.iface == 0) {
1469 char *end;
930d3f28 1470 unsigned long n;
2d293212 1471
930d3f28
DA
1472 n = strtoul(name, &end, 0);
1473 if (!end || end == name || *end || n > UINT_MAX)
2d293212 1474 return NULL;
930d3f28
DA
1475
1476 a.iface = n;
2d293212
DA
1477 }
1478
1479 res = malloc(sizeof(*res));
1480 *res = a;
1481
1482 return res;
1483}
1484
7871f7db 1485void *parse_hostcond(char *addr, bool is_port)
aba5acdf
SH
1486{
1487 char *port = NULL;
1527a17e 1488 struct aafilter a = { .port = -1 };
aba5acdf 1489 struct aafilter *res;
1527a17e 1490 int fam = preferred_family;
9db7bf15 1491 struct filter *f = &current_filter;
aba5acdf 1492
1527a17e 1493 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
aba5acdf 1494 char *p;
acd1e437 1495
aba5acdf
SH
1496 a.addr.family = AF_UNIX;
1497 if (strncmp(addr, "unix:", 5) == 0)
acd1e437 1498 addr += 5;
aba5acdf
SH
1499 p = strdup(addr);
1500 a.addr.bitlen = 8*strlen(p);
99bb68ff 1501 memcpy(a.addr.data, &p, sizeof(p));
9db7bf15 1502 fam = AF_UNIX;
aba5acdf
SH
1503 goto out;
1504 }
1505
1527a17e 1506 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
aba5acdf
SH
1507 a.addr.family = AF_PACKET;
1508 a.addr.bitlen = 0;
1509 if (strncmp(addr, "link:", 5) == 0)
acd1e437 1510 addr += 5;
aba5acdf
SH
1511 port = strchr(addr, ':');
1512 if (port) {
1513 *port = 0;
1514 if (port[1] && strcmp(port+1, "*")) {
1515 if (get_integer(&a.port, port+1, 0)) {
1516 if ((a.port = xll_name_to_index(port+1)) <= 0)
1517 return NULL;
1518 }
1519 }
1520 }
1521 if (addr[0] && strcmp(addr, "*")) {
1522 unsigned short tmp;
acd1e437 1523
aba5acdf
SH
1524 a.addr.bitlen = 32;
1525 if (ll_proto_a2n(&tmp, addr))
1526 return NULL;
1527 a.addr.data[0] = ntohs(tmp);
1528 }
9db7bf15 1529 fam = AF_PACKET;
aba5acdf
SH
1530 goto out;
1531 }
1532
1527a17e 1533 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
aba5acdf
SH
1534 a.addr.family = AF_NETLINK;
1535 a.addr.bitlen = 0;
1536 if (strncmp(addr, "netlink:", 8) == 0)
acd1e437 1537 addr += 8;
aba5acdf
SH
1538 port = strchr(addr, ':');
1539 if (port) {
1540 *port = 0;
1541 if (port[1] && strcmp(port+1, "*")) {
1542 if (get_integer(&a.port, port+1, 0)) {
1543 if (strcmp(port+1, "kernel") == 0)
1544 a.port = 0;
1545 else
1546 return NULL;
1547 }
1548 }
1549 }
1550 if (addr[0] && strcmp(addr, "*")) {
1551 a.addr.bitlen = 32;
b00daf6a 1552 if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
1553 return NULL;
aba5acdf 1554 }
9db7bf15 1555 fam = AF_NETLINK;
aba5acdf
SH
1556 goto out;
1557 }
1558
1527a17e 1559 if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
aba5acdf 1560 fam = AF_INET;
1527a17e
VK
1561 if (!strncmp(addr, "inet:", 5))
1562 addr += 5;
1563 } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
aba5acdf 1564 fam = AF_INET6;
1527a17e
VK
1565 if (!strncmp(addr, "inet6:", 6))
1566 addr += 6;
aba5acdf
SH
1567 }
1568
1569 /* URL-like literal [] */
1570 if (addr[0] == '[') {
1571 addr++;
1572 if ((port = strchr(addr, ']')) == NULL)
1573 return NULL;
1574 *port++ = 0;
1575 } else if (addr[0] == '*') {
1576 port = addr+1;
1577 } else {
1578 port = strrchr(strchr(addr, '/') ? : addr, ':');
1579 }
7871f7db
VK
1580
1581 if (is_port)
1582 port = addr;
1583
aba5acdf 1584 if (port && *port) {
7871f7db
VK
1585 if (*port == ':')
1586 *port++ = 0;
1587
aba5acdf
SH
1588 if (*port && *port != '*') {
1589 if (get_integer(&a.port, port, 0)) {
1590 struct servent *se1 = NULL;
1591 struct servent *se2 = NULL;
acd1e437 1592
aba5acdf
SH
1593 if (current_filter.dbs&(1<<UDP_DB))
1594 se1 = getservbyname(port, UDP_PROTO);
1595 if (current_filter.dbs&(1<<TCP_DB))
1596 se2 = getservbyname(port, TCP_PROTO);
1597 if (se1 && se2 && se1->s_port != se2->s_port) {
1598 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1599 return NULL;
1600 }
1601 if (!se1)
1602 se1 = se2;
1603 if (se1) {
1604 a.port = ntohs(se1->s_port);
1605 } else {
1606 struct scache *s;
acd1e437 1607
aba5acdf
SH
1608 for (s = rlist; s; s = s->next) {
1609 if ((s->proto == UDP_PROTO &&
1610 (current_filter.dbs&(1<<UDP_DB))) ||
1611 (s->proto == TCP_PROTO &&
1612 (current_filter.dbs&(1<<TCP_DB)))) {
1613 if (s->name && strcmp(s->name, port) == 0) {
1614 if (a.port > 0 && a.port != s->port) {
1615 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1616 return NULL;
1617 }
1618 a.port = s->port;
1619 }
1620 }
1621 }
1622 if (a.port <= 0) {
1623 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1624 return NULL;
1625 }
1626 }
1627 }
1628 }
1629 }
7871f7db 1630 if (!is_port && addr && *addr && *addr != '*') {
aba5acdf
SH
1631 if (get_prefix_1(&a.addr, addr, fam)) {
1632 if (get_dns_host(&a, addr, fam)) {
1633 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1634 return NULL;
1635 }
1636 }
1637 }
1638
9db7bf15 1639out:
1527a17e 1640 if (fam != AF_UNSPEC) {
e56a959e 1641 int states = f->states;
1527a17e 1642 f->families = 0;
9db7bf15 1643 filter_af_set(f, fam);
e56a959e 1644 filter_states_set(f, states);
1527a17e 1645 }
9db7bf15 1646
aba5acdf
SH
1647 res = malloc(sizeof(*res));
1648 if (res)
1649 memcpy(res, &a, sizeof(a));
1650 return res;
1651}
1652
ec75249b
LC
1653void *parse_markmask(const char *markmask)
1654{
1655 struct aafilter a, *res;
1656
1657 if (strchr(markmask, '/')) {
1658 if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2)
1659 return NULL;
1660 } else {
1661 a.mask = 0xffffffff;
1662 if (sscanf(markmask, "%i", &a.mark) != 1)
1663 return NULL;
1664 }
1665
1666 res = malloc(sizeof(*res));
1667 if (res)
1668 memcpy(res, &a, sizeof(a));
1669 return res;
1670}
1671
8250bc9f
VK
1672static char *proto_name(int protocol)
1673{
1674 switch (protocol) {
235c4453
NA
1675 case 0:
1676 return "raw";
8250bc9f
VK
1677 case IPPROTO_UDP:
1678 return "udp";
1679 case IPPROTO_TCP:
1680 return "tcp";
1681 case IPPROTO_DCCP:
1682 return "dccp";
1683 }
1684
1685 return "???";
1686}
1687
055840f2 1688static void inet_stats_print(struct sockstat *s, int protocol)
8250bc9f
VK
1689{
1690 char *buf = NULL;
1691
2d791bc8 1692 sock_state_print(s, proto_name(protocol));
8250bc9f 1693
b217df10
VK
1694 inet_addr_print(&s->local, s->lport, s->iface);
1695 inet_addr_print(&s->remote, s->rport, 0);
8250bc9f 1696
8250bc9f
VK
1697 if (show_proc_ctx || show_sock_ctx) {
1698 if (find_entry(s->ino, &buf,
1699 (show_proc_ctx & show_sock_ctx) ?
1700 PROC_SOCK_CTX : PROC_CTX) > 0) {
1701 printf(" users:(%s)", buf);
1702 free(buf);
1703 }
1704 } else if (show_users) {
1705 if (find_entry(s->ino, &buf, USERS) > 0) {
1706 printf(" users:(%s)", buf);
1707 free(buf);
1708 }
1709 }
1710}
1711
055840f2 1712static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
acd1e437 1713 sockstat * s)
8250bc9f
VK
1714{
1715 s->local.family = s->remote.family = family;
1716 if (family == AF_INET) {
acd1e437
SH
1717 sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
1718 sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
8250bc9f
VK
1719 s->local.bytelen = s->remote.bytelen = 4;
1720 return 0;
1721 } else {
1722 sscanf(loc, "%08x%08x%08x%08x:%x",
1723 s->local.data,
1724 s->local.data + 1,
1725 s->local.data + 2,
1726 s->local.data + 3,
1727 &s->lport);
1728 sscanf(rem, "%08x%08x%08x%08x:%x",
1729 s->remote.data,
1730 s->remote.data + 1,
1731 s->remote.data + 2,
1732 s->remote.data + 3,
1733 &s->rport);
1734 s->local.bytelen = s->remote.bytelen = 16;
1735 return 0;
1736 }
1737 return -1;
1738}
1739
1740static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
aba5acdf 1741{
aba5acdf 1742 char *p;
ae665a52 1743
aba5acdf
SH
1744 if ((p = strchr(line, ':')) == NULL)
1745 return -1;
ae665a52 1746
8250bc9f
VK
1747 *loc = p+2;
1748 if ((p = strchr(*loc, ':')) == NULL)
aba5acdf 1749 return -1;
ae665a52 1750
8250bc9f
VK
1751 p[5] = 0;
1752 *rem = p+6;
1753 if ((p = strchr(*rem, ':')) == NULL)
aba5acdf 1754 return -1;
8250bc9f 1755
aba5acdf 1756 p[5] = 0;
8250bc9f
VK
1757 *data = p+6;
1758 return 0;
1759}
ae665a52 1760
8250bc9f
VK
1761static char *sprint_bw(char *buf, double bw)
1762{
1763 if (bw > 1000000.)
acd1e437 1764 sprintf(buf, "%.1fM", bw / 1000000.);
8250bc9f 1765 else if (bw > 1000.)
acd1e437 1766 sprintf(buf, "%.1fK", bw / 1000.);
8250bc9f
VK
1767 else
1768 sprintf(buf, "%g", bw);
aba5acdf 1769
8250bc9f
VK
1770 return buf;
1771}
ae665a52 1772
8250bc9f
VK
1773static void tcp_stats_print(struct tcpstat *s)
1774{
1775 char b1[64];
1776
1777 if (s->has_ts_opt)
1778 printf(" ts");
1779 if (s->has_sack_opt)
1780 printf(" sack");
1781 if (s->has_ecn_opt)
1782 printf(" ecn");
1783 if (s->has_ecnseen_opt)
1784 printf(" ecnseen");
1785 if (s->has_fastopen_opt)
1786 printf(" fastopen");
d2055ea5 1787 if (s->cong_alg[0])
8250bc9f
VK
1788 printf(" %s", s->cong_alg);
1789 if (s->has_wscale_opt)
1790 printf(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale);
1791 if (s->rto)
1792 printf(" rto:%g", s->rto);
1793 if (s->backoff)
1794 printf(" backoff:%u", s->backoff);
1795 if (s->rtt)
1796 printf(" rtt:%g/%g", s->rtt, s->rttvar);
1797 if (s->ato)
1798 printf(" ato:%g", s->ato);
1799
1800 if (s->qack)
1801 printf(" qack:%d", s->qack);
1802 if (s->qack & 1)
1803 printf(" bidir");
1804
1805 if (s->mss)
1806 printf(" mss:%d", s->mss);
3bf5445c 1807 if (s->cwnd)
8250bc9f
VK
1808 printf(" cwnd:%d", s->cwnd);
1809 if (s->ssthresh)
1810 printf(" ssthresh:%d", s->ssthresh);
1811
1a4dda71
ED
1812 if (s->bytes_acked)
1813 printf(" bytes_acked:%llu", s->bytes_acked);
1814 if (s->bytes_received)
1815 printf(" bytes_received:%llu", s->bytes_received);
ecb435ea
CG
1816 if (s->segs_out)
1817 printf(" segs_out:%u", s->segs_out);
1818 if (s->segs_in)
1819 printf(" segs_in:%u", s->segs_in);
414aeec9
MKL
1820 if (s->data_segs_out)
1821 printf(" data_segs_out:%u", s->data_segs_out);
1822 if (s->data_segs_in)
1823 printf(" data_segs_in:%u", s->data_segs_in);
1a4dda71 1824
8250bc9f
VK
1825 if (s->dctcp && s->dctcp->enabled) {
1826 struct dctcpstat *dctcp = s->dctcp;
1827
3bf5445c 1828 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
8250bc9f
VK
1829 dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
1830 dctcp->ab_tot);
1831 } else if (s->dctcp) {
3bf5445c 1832 printf(" dctcp:fallback_mode");
8250bc9f
VK
1833 }
1834
2f0f9aef
NC
1835 if (s->bbr_info) {
1836 __u64 bw;
1837
1838 bw = s->bbr_info->bbr_bw_hi;
1839 bw <<= 32;
1840 bw |= s->bbr_info->bbr_bw_lo;
1841
1842 printf(" bbr:(bw:%sbps,mrtt:%g",
1843 sprint_bw(b1, bw * 8.0),
1844 (double)s->bbr_info->bbr_min_rtt / 1000.0);
1845 if (s->bbr_info->bbr_pacing_gain)
1846 printf(",pacing_gain:%g",
1847 (double)s->bbr_info->bbr_pacing_gain / 256.0);
1848 if (s->bbr_info->bbr_cwnd_gain)
1849 printf(",cwnd_gain:%g",
1850 (double)s->bbr_info->bbr_cwnd_gain / 256.0);
1851 printf(")");
1852 }
1853
8250bc9f
VK
1854 if (s->send_bps)
1855 printf(" send %sbps", sprint_bw(b1, s->send_bps));
1856 if (s->lastsnd)
1857 printf(" lastsnd:%u", s->lastsnd);
1858 if (s->lastrcv)
1859 printf(" lastrcv:%u", s->lastrcv);
1860 if (s->lastack)
1861 printf(" lastack:%u", s->lastack);
1862
1863 if (s->pacing_rate) {
1864 printf(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate));
1865 if (s->pacing_rate_max)
1866 printf("/%sbps", sprint_bw(b1,
1867 s->pacing_rate_max));
1868 }
1869
1870 if (s->unacked)
1871 printf(" unacked:%u", s->unacked);
1872 if (s->retrans || s->retrans_total)
1873 printf(" retrans:%u/%u", s->retrans, s->retrans_total);
1874 if (s->lost)
1875 printf(" lost:%u", s->lost);
055840f2 1876 if (s->sacked && s->ss.state != SS_LISTEN)
8250bc9f
VK
1877 printf(" sacked:%u", s->sacked);
1878 if (s->fackets)
1879 printf(" fackets:%u", s->fackets);
1880 if (s->reordering != 3)
1881 printf(" reordering:%d", s->reordering);
1882 if (s->rcv_rtt)
1883 printf(" rcv_rtt:%g", s->rcv_rtt);
1884 if (s->rcv_space)
1885 printf(" rcv_space:%d", s->rcv_space);
9e99e495
SH
1886 if (s->not_sent)
1887 printf(" notsent:%u", s->not_sent);
1888 if (s->min_rtt)
1889 printf(" minrtt:%g", s->min_rtt);
8250bc9f
VK
1890}
1891
055840f2
VK
1892static void tcp_timer_print(struct tcpstat *s)
1893{
1894 if (s->timer) {
1895 if (s->timer > 4)
1896 s->timer = 5;
1897 printf(" timer:(%s,%s,%d)",
1898 tmr_name[s->timer],
1899 print_ms_timer(s->timeout),
1900 s->retrans);
1901 }
1902}
1903
8250bc9f
VK
1904static int tcp_show_line(char *line, const struct filter *f, int family)
1905{
1906 int rto = 0, ato = 0;
1907 struct tcpstat s = {};
1908 char *loc, *rem, *data;
1909 char opt[256];
1910 int n;
1911 int hz = get_user_hz();
1912
1913 if (proc_inet_split_line(line, &loc, &rem, &data))
1914 return -1;
1915
1916 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
acd1e437 1917
8250bc9f
VK
1918 if (!(f->states & (1 << state)))
1919 return 0;
1920
055840f2 1921 proc_parse_inet_addr(loc, rem, family, &s.ss);
ae665a52 1922
055840f2 1923 if (f->f && run_ssfilter(f->f, &s.ss) == 0)
aba5acdf 1924 return 0;
ae665a52 1925
aba5acdf 1926 opt[0] = 0;
e7113c61 1927 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
055840f2
VK
1928 &s.ss.state, &s.ss.wq, &s.ss.rq,
1929 &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes,
1930 &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd,
1931 &s.ssthresh, opt);
ae665a52 1932
aba5acdf
SH
1933 if (n < 17)
1934 opt[0] = 0;
ae665a52 1935
aba5acdf 1936 if (n < 12) {
8250bc9f 1937 rto = 0;
aba5acdf
SH
1938 s.cwnd = 2;
1939 s.ssthresh = -1;
8250bc9f 1940 ato = s.qack = 0;
aba5acdf 1941 }
ae665a52 1942
8250bc9f
VK
1943 s.retrans = s.timer != 1 ? s.probes : s.retrans;
1944 s.timeout = (s.timeout * 1000 + hz - 1) / hz;
1945 s.ato = (double)ato / hz;
1946 s.qack /= 2;
1947 s.rto = (double)rto;
1948 s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh;
1949 s.rto = s.rto != 3 * hz ? s.rto / hz : 0;
ae665a52 1950
055840f2
VK
1951 inet_stats_print(&s.ss, IPPROTO_TCP);
1952
1953 if (show_options)
1954 tcp_timer_print(&s);
116ac927 1955
aba5acdf 1956 if (show_details) {
f1b39e1b 1957 sock_details_print(&s.ss);
aba5acdf
SH
1958 if (opt[0])
1959 printf(" opt:\"%s\"", opt);
1960 }
aba5acdf 1961
8250bc9f
VK
1962 if (show_tcpinfo)
1963 tcp_stats_print(&s);
1964
1965 printf("\n");
aba5acdf
SH
1966 return 0;
1967}
1968
ab01dbbb
SH
1969static int generic_record_read(FILE *fp,
1970 int (*worker)(char*, const struct filter *, int),
1971 const struct filter *f, int fam)
aba5acdf 1972{
ab01dbbb 1973 char line[256];
aba5acdf 1974
ab01dbbb
SH
1975 /* skip header */
1976 if (fgets(line, sizeof(line), fp) == NULL)
aba5acdf 1977 goto outerr;
ab01dbbb
SH
1978
1979 while (fgets(line, sizeof(line), fp) != NULL) {
1980 int n = strlen(line);
acd1e437 1981
ab01dbbb
SH
1982 if (n == 0 || line[n-1] != '\n') {
1983 errno = -EINVAL;
1984 return -1;
aba5acdf 1985 }
ab01dbbb 1986 line[n-1] = 0;
aba5acdf 1987
ab01dbbb
SH
1988 if (worker(line, f, fam) < 0)
1989 return 0;
1990 }
aba5acdf 1991outerr:
ab01dbbb
SH
1992
1993 return ferror(fp) ? -1 : 0;
aba5acdf 1994}
ae665a52 1995
51ff9f24
HFS
1996static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1997{
1998 const __u32 *skmeminfo;
db08bdb8
VK
1999
2000 if (!tb[attrtype]) {
2001 if (attrtype == INET_DIAG_SKMEMINFO) {
2002 if (!tb[INET_DIAG_MEMINFO])
2003 return;
2004
2005 const struct inet_diag_meminfo *minfo =
2006 RTA_DATA(tb[INET_DIAG_MEMINFO]);
2007
2008 printf(" mem:(r%u,w%u,f%u,t%u)",
2009 minfo->idiag_rmem,
2010 minfo->idiag_wmem,
2011 minfo->idiag_fmem,
2012 minfo->idiag_tmem);
2013 }
51ff9f24 2014 return;
db08bdb8
VK
2015 }
2016
51ff9f24
HFS
2017 skmeminfo = RTA_DATA(tb[attrtype]);
2018
2019 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
2020 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
2021 skmeminfo[SK_MEMINFO_RCVBUF],
2022 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
2023 skmeminfo[SK_MEMINFO_SNDBUF],
2024 skmeminfo[SK_MEMINFO_FWD_ALLOC],
2025 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
2026 skmeminfo[SK_MEMINFO_OPTMEM]);
2027
2028 if (RTA_PAYLOAD(tb[attrtype]) >=
2029 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
2030 printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
2031
6df9c7a0
ED
2032 if (RTA_PAYLOAD(tb[attrtype]) >=
2033 (SK_MEMINFO_DROPS + 1) * sizeof(__u32))
2034 printf(",d%u", skmeminfo[SK_MEMINFO_DROPS]);
2035
51ff9f24
HFS
2036 printf(")");
2037}
2038
8250bc9f
VK
2039#define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
2040
5b816047
PE
2041static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
2042 struct rtattr *tb[])
7d105b56 2043{
b4b0b7d5 2044 double rtt = 0;
8250bc9f 2045 struct tcpstat s = {};
7d105b56 2046
055840f2
VK
2047 s.ss.state = r->idiag_state;
2048
db08bdb8 2049 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
7d105b56 2050
351efcde 2051 if (tb[INET_DIAG_INFO]) {
05e18118 2052 struct tcp_info *info;
351efcde 2053 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
05e18118
SH
2054
2055 /* workaround for older kernels with less fields */
2056 if (len < sizeof(*info)) {
2057 info = alloca(sizeof(*info));
351efcde 2058 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
656e8fdd 2059 memset((char *)info + len, 0, sizeof(*info) - len);
05e18118 2060 } else
351efcde 2061 info = RTA_DATA(tb[INET_DIAG_INFO]);
05e18118 2062
b4b0b7d5 2063 if (show_options) {
8250bc9f
VK
2064 s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS);
2065 s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK);
2066 s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN);
2067 s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN);
2068 s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA);
b4b0b7d5 2069 }
52d5ac3f 2070
d2055ea5
ED
2071 if (tb[INET_DIAG_CONG])
2072 strncpy(s.cong_alg,
2073 rta_getattr_str(tb[INET_DIAG_CONG]),
2074 sizeof(s.cong_alg) - 1);
8250bc9f
VK
2075
2076 if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) {
2077 s.has_wscale_opt = true;
2078 s.snd_wscale = info->tcpi_snd_wscale;
2079 s.rcv_wscale = info->tcpi_rcv_wscale;
2080 }
ea8fc104 2081
7d105b56 2082 if (info->tcpi_rto && info->tcpi_rto != 3000000)
8250bc9f
VK
2083 s.rto = (double)info->tcpi_rto / 1000;
2084
2085 s.backoff = info->tcpi_backoff;
2086 s.rtt = (double)info->tcpi_rtt / 1000;
2087 s.rttvar = (double)info->tcpi_rttvar / 1000;
11ba90fc 2088 s.ato = (double)info->tcpi_ato / 1000;
8250bc9f
VK
2089 s.mss = info->tcpi_snd_mss;
2090 s.rcv_space = info->tcpi_rcv_space;
2091 s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000;
2092 s.lastsnd = info->tcpi_last_data_sent;
2093 s.lastrcv = info->tcpi_last_data_recv;
2094 s.lastack = info->tcpi_last_ack_recv;
2095 s.unacked = info->tcpi_unacked;
2096 s.retrans = info->tcpi_retrans;
2097 s.retrans_total = info->tcpi_total_retrans;
2098 s.lost = info->tcpi_lost;
2099 s.sacked = info->tcpi_sacked;
2100 s.reordering = info->tcpi_reordering;
2101 s.rcv_space = info->tcpi_rcv_space;
2102 s.cwnd = info->tcpi_snd_cwnd;
2103
7d105b56 2104 if (info->tcpi_snd_ssthresh < 0xFFFF)
8250bc9f 2105 s.ssthresh = info->tcpi_snd_ssthresh;
52d5ac3f 2106
b4b0b7d5 2107 rtt = (double) info->tcpi_rtt;
351efcde 2108 if (tb[INET_DIAG_VEGASINFO]) {
05e18118 2109 const struct tcpvegas_info *vinfo
351efcde 2110 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
7d105b56 2111
ae665a52 2112 if (vinfo->tcpv_enabled &&
8250bc9f 2113 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
ea8fc104 2114 rtt = vinfo->tcpv_rtt;
b4b0b7d5
SH
2115 }
2116
907e1aca 2117 if (tb[INET_DIAG_DCTCPINFO]) {
8250bc9f
VK
2118 struct dctcpstat *dctcp = malloc(sizeof(struct
2119 dctcpstat));
2120
907e1aca
DB
2121 const struct tcp_dctcp_info *dinfo
2122 = RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
2123
8250bc9f
VK
2124 dctcp->enabled = !!dinfo->dctcp_enabled;
2125 dctcp->ce_state = dinfo->dctcp_ce_state;
2126 dctcp->alpha = dinfo->dctcp_alpha;
2127 dctcp->ab_ecn = dinfo->dctcp_ab_ecn;
2128 dctcp->ab_tot = dinfo->dctcp_ab_tot;
2129 s.dctcp = dctcp;
907e1aca
DB
2130 }
2131
2f0f9aef
NC
2132 if (tb[INET_DIAG_BBRINFO]) {
2133 const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]);
2134 int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]),
2135 sizeof(*s.bbr_info));
2136
2137 s.bbr_info = calloc(1, sizeof(*s.bbr_info));
2138 if (s.bbr_info && bbr_info)
2139 memcpy(s.bbr_info, bbr_info, len);
2140 }
2141
b4b0b7d5 2142 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
8250bc9f
VK
2143 s.send_bps = (double) info->tcpi_snd_cwnd *
2144 (double)info->tcpi_snd_mss * 8000000. / rtt;
7d105b56 2145 }
b4b0b7d5 2146
eb6028b2 2147 if (info->tcpi_pacing_rate &&
8250bc9f
VK
2148 info->tcpi_pacing_rate != ~0ULL) {
2149 s.pacing_rate = info->tcpi_pacing_rate * 8.;
eb6028b2
ED
2150
2151 if (info->tcpi_max_pacing_rate &&
8250bc9f
VK
2152 info->tcpi_max_pacing_rate != ~0ULL)
2153 s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
eb6028b2 2154 }
1a4dda71
ED
2155 s.bytes_acked = info->tcpi_bytes_acked;
2156 s.bytes_received = info->tcpi_bytes_received;
ecb435ea
CG
2157 s.segs_out = info->tcpi_segs_out;
2158 s.segs_in = info->tcpi_segs_in;
414aeec9
MKL
2159 s.data_segs_out = info->tcpi_data_segs_out;
2160 s.data_segs_in = info->tcpi_data_segs_in;
9e99e495 2161 s.not_sent = info->tcpi_notsent_bytes;
d9ba887e
ED
2162 if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U)
2163 s.min_rtt = (double) info->tcpi_min_rtt / 1000;
8250bc9f 2164 tcp_stats_print(&s);
92de1c2c 2165 free(s.dctcp);
2f0f9aef 2166 free(s.bbr_info);
77a8ca81 2167 }
77a8ca81
PE
2168}
2169
82d73ea0 2170static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
aba5acdf 2171{
acd1e437 2172 struct rtattr *tb[INET_DIAG_MAX+1];
351efcde 2173 struct inet_diag_msg *r = NLMSG_DATA(nlh);
aba5acdf 2174
acd1e437 2175 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
5b816047
PE
2176 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2177
82d73ea0
DA
2178 s->state = r->idiag_state;
2179 s->local.family = s->remote.family = r->idiag_family;
2180 s->lport = ntohs(r->id.idiag_sport);
2181 s->rport = ntohs(r->id.idiag_dport);
2182 s->wq = r->idiag_wqueue;
2183 s->rq = r->idiag_rqueue;
2184 s->ino = r->idiag_inode;
2185 s->uid = r->idiag_uid;
2186 s->iface = r->id.idiag_if;
2187 s->sk = cookie_sk_get(&r->id.idiag_cookie[0]);
2188
ec75249b
LC
2189 s->mark = 0;
2190 if (tb[INET_DIAG_MARK])
2191 s->mark = *(__u32 *) RTA_DATA(tb[INET_DIAG_MARK]);
2192
82d73ea0
DA
2193 if (s->local.family == AF_INET)
2194 s->local.bytelen = s->remote.bytelen = 4;
2195 else
2196 s->local.bytelen = s->remote.bytelen = 16;
8250bc9f 2197
82d73ea0
DA
2198 memcpy(s->local.data, r->id.idiag_src, s->local.bytelen);
2199 memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen);
2200}
aba5acdf 2201
82d73ea0
DA
2202static int inet_show_sock(struct nlmsghdr *nlh,
2203 struct sockstat *s,
2204 int protocol)
2205{
2206 struct rtattr *tb[INET_DIAG_MAX+1];
2207 struct inet_diag_msg *r = NLMSG_DATA(nlh);
2208
2209 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
2210 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
aba5acdf 2211
6885e3bf
CG
2212 if (tb[INET_DIAG_PROTOCOL])
2213 protocol = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]);
2214
82d73ea0 2215 inet_stats_print(s, protocol);
116ac927 2216
055840f2
VK
2217 if (show_options) {
2218 struct tcpstat t = {};
2219
2220 t.timer = r->idiag_timer;
2221 t.timeout = r->idiag_expires;
2222 t.retrans = r->idiag_retrans;
2223 tcp_timer_print(&t);
2224 }
2225
aba5acdf 2226 if (show_details) {
82d73ea0
DA
2227 sock_details_print(s);
2228 if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) {
f32dc746 2229 unsigned char v6only;
acd1e437 2230
f32dc746
PS
2231 v6only = *(__u8 *)RTA_DATA(tb[INET_DIAG_SKV6ONLY]);
2232 printf(" v6only:%u", v6only);
2233 }
5b816047
PE
2234 if (tb[INET_DIAG_SHUTDOWN]) {
2235 unsigned char mask;
acd1e437 2236
5b816047
PE
2237 mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]);
2238 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2239 }
aba5acdf 2240 }
8250bc9f 2241
aba5acdf 2242 if (show_mem || show_tcpinfo) {
7d105b56 2243 printf("\n\t");
5b816047 2244 tcp_show_info(nlh, r, tb);
aba5acdf 2245 }
7d105b56 2246
aba5acdf 2247 printf("\n");
aba5acdf 2248 return 0;
aba5acdf
SH
2249}
2250
746a695f 2251static int tcpdiag_send(int fd, int protocol, struct filter *f)
aba5acdf 2252{
d17b136f 2253 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
aba5acdf
SH
2254 struct {
2255 struct nlmsghdr nlh;
351efcde 2256 struct inet_diag_req r;
d17b136f
PS
2257 } req = {
2258 .nlh.nlmsg_len = sizeof(req),
2259 .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
2260 .nlh.nlmsg_seq = MAGIC_SEQ,
2261 .r.idiag_family = AF_INET,
2262 .r.idiag_states = f->states,
2263 };
aba5acdf
SH
2264 char *bc = NULL;
2265 int bclen;
2266 struct msghdr msg;
2267 struct rtattr rta;
aba5acdf 2268 struct iovec iov[3];
376fb868 2269 int iovlen = 1;
aba5acdf 2270
346f8ca8
PE
2271 if (protocol == IPPROTO_UDP)
2272 return -1;
2273
3fe5b534
PE
2274 if (protocol == IPPROTO_TCP)
2275 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
2276 else
2277 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
910b0397 2278 if (show_mem) {
ae665a52 2279 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
910b0397
SW
2280 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2281 }
b4b0b7d5 2282
7d105b56 2283 if (show_tcpinfo) {
351efcde
SH
2284 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2285 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2286 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
7d105b56 2287 }
aba5acdf 2288
ae665a52
SH
2289 iov[0] = (struct iovec){
2290 .iov_base = &req,
2291 .iov_len = sizeof(req)
ea8fc104 2292 };
aba5acdf
SH
2293 if (f->f) {
2294 bclen = ssfilter_bytecompile(f->f, &bc);
376fb868
DA
2295 if (bclen) {
2296 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2297 rta.rta_len = RTA_LENGTH(bclen);
2298 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2299 iov[2] = (struct iovec){ bc, bclen };
2300 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2301 iovlen = 3;
2302 }
aba5acdf
SH
2303 }
2304
2305 msg = (struct msghdr) {
acd1e437 2306 .msg_name = (void *)&nladdr,
ea8fc104 2307 .msg_namelen = sizeof(nladdr),
ae665a52 2308 .msg_iov = iov,
376fb868 2309 .msg_iovlen = iovlen,
aba5acdf
SH
2310 };
2311
930a75f9
ED
2312 if (sendmsg(fd, &msg, 0) < 0) {
2313 close(fd);
aba5acdf 2314 return -1;
930a75f9 2315 }
aba5acdf 2316
746a695f
PE
2317 return 0;
2318}
2319
886d19d6
PE
2320static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
2321{
d17b136f 2322 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
5fb421d4 2323 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
886d19d6
PE
2324 char *bc = NULL;
2325 int bclen;
2326 struct msghdr msg;
2327 struct rtattr rta;
2328 struct iovec iov[3];
376fb868 2329 int iovlen = 1;
886d19d6
PE
2330
2331 if (family == PF_UNSPEC)
2332 return tcpdiag_send(fd, protocol, f);
2333
886d19d6
PE
2334 memset(&req.r, 0, sizeof(req.r));
2335 req.r.sdiag_family = family;
2336 req.r.sdiag_protocol = protocol;
2337 req.r.idiag_states = f->states;
2338 if (show_mem) {
2339 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
2340 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2341 }
2342
2343 if (show_tcpinfo) {
2344 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2345 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2346 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
2347 }
2348
2349 iov[0] = (struct iovec){
2350 .iov_base = &req,
2351 .iov_len = sizeof(req)
2352 };
2353 if (f->f) {
2354 bclen = ssfilter_bytecompile(f->f, &bc);
376fb868
DA
2355 if (bclen) {
2356 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2357 rta.rta_len = RTA_LENGTH(bclen);
2358 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2359 iov[2] = (struct iovec){ bc, bclen };
2360 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2361 iovlen = 3;
2362 }
886d19d6
PE
2363 }
2364
2365 msg = (struct msghdr) {
acd1e437 2366 .msg_name = (void *)&nladdr,
886d19d6
PE
2367 .msg_namelen = sizeof(nladdr),
2368 .msg_iov = iov,
376fb868 2369 .msg_iovlen = iovlen,
886d19d6
PE
2370 };
2371
2372 if (sendmsg(fd, &msg, 0) < 0) {
2373 close(fd);
2374 return -1;
2375 }
2376
2377 return 0;
2378}
2379
486ccd99
VK
2380struct inet_diag_arg {
2381 struct filter *f;
2382 int protocol;
fb2594c1 2383 struct rtnl_handle *rth;
486ccd99 2384};
aba5acdf 2385
b38e7409 2386static int kill_inet_sock(struct nlmsghdr *h, void *arg)
fb2594c1
LC
2387{
2388 struct inet_diag_msg *d = NLMSG_DATA(h);
2389 struct inet_diag_arg *diag_arg = arg;
2390 struct rtnl_handle *rth = diag_arg->rth;
acd1e437 2391
fb2594c1
LC
2392 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
2393
2394 req.nlh.nlmsg_type = SOCK_DESTROY;
2395 req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2396 req.nlh.nlmsg_seq = ++rth->seq;
2397 req.r.sdiag_family = d->idiag_family;
2398 req.r.sdiag_protocol = diag_arg->protocol;
2399 req.r.id = d->id;
2400
2401 return rtnl_talk(rth, &req.nlh, NULL, 0);
2402}
2403
486ccd99
VK
2404static int show_one_inet_sock(const struct sockaddr_nl *addr,
2405 struct nlmsghdr *h, void *arg)
2406{
2407 int err;
2408 struct inet_diag_arg *diag_arg = arg;
2409 struct inet_diag_msg *r = NLMSG_DATA(h);
82d73ea0 2410 struct sockstat s = {};
aba5acdf 2411
486ccd99
VK
2412 if (!(diag_arg->f->families & (1 << r->idiag_family)))
2413 return 0;
82d73ea0
DA
2414
2415 parse_diag_msg(h, &s);
2416
2417 if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0)
2418 return 0;
2419
b38e7409 2420 if (diag_arg->f->kill && kill_inet_sock(h, arg) != 0) {
fb2594c1
LC
2421 if (errno == EOPNOTSUPP || errno == ENOENT) {
2422 /* Socket can't be closed, or is already closed. */
2423 return 0;
2424 } else {
2425 perror("SOCK_DESTROY answers");
2426 return -1;
2427 }
2428 }
82d73ea0
DA
2429
2430 err = inet_show_sock(h, &s, diag_arg->protocol);
2431 if (err < 0)
486ccd99 2432 return err;
aba5acdf 2433
486ccd99
VK
2434 return 0;
2435}
886d19d6 2436
486ccd99
VK
2437static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
2438{
2439 int err = 0;
fb2594c1 2440 struct rtnl_handle rth, rth2;
486ccd99
VK
2441 int family = PF_INET;
2442 struct inet_diag_arg arg = { .f = f, .protocol = protocol };
886d19d6 2443
486ccd99
VK
2444 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
2445 return -1;
fb2594c1
LC
2446
2447 if (f->kill) {
2448 if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) {
2449 rtnl_close(&rth);
2450 return -1;
2451 }
2452 arg.rth = &rth2;
2453 }
2454
486ccd99
VK
2455 rth.dump = MAGIC_SEQ;
2456 rth.dump_fp = dump_fp;
518af1e0
ED
2457 if (preferred_family == PF_INET6)
2458 family = PF_INET6;
886d19d6 2459
486ccd99
VK
2460again:
2461 if ((err = sockdiag_send(family, rth.fd, protocol, f)))
2462 goto Exit;
aba5acdf 2463
486ccd99
VK
2464 if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) {
2465 if (family != PF_UNSPEC) {
2466 family = PF_UNSPEC;
2467 goto again;
aba5acdf 2468 }
486ccd99 2469 goto Exit;
aba5acdf 2470 }
518af1e0 2471 if (family == PF_INET && preferred_family != PF_INET) {
886d19d6
PE
2472 family = PF_INET6;
2473 goto again;
2474 }
2475
486ccd99
VK
2476Exit:
2477 rtnl_close(&rth);
fb2594c1
LC
2478 if (arg.rth)
2479 rtnl_close(arg.rth);
486ccd99 2480 return err;
aba5acdf
SH
2481}
2482
ab01dbbb 2483static int tcp_show_netlink_file(struct filter *f)
aba5acdf
SH
2484{
2485 FILE *fp;
e557212e 2486 char buf[16384];
aba5acdf
SH
2487
2488 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
2489 perror("fopen($TCPDIAG_FILE)");
2490 return -1;
2491 }
2492
2493 while (1) {
2494 int status, err;
acd1e437 2495 struct nlmsghdr *h = (struct nlmsghdr *)buf;
82d73ea0 2496 struct sockstat s = {};
aba5acdf
SH
2497
2498 status = fread(buf, 1, sizeof(*h), fp);
2499 if (status < 0) {
2500 perror("Reading header from $TCPDIAG_FILE");
2501 return -1;
2502 }
2503 if (status != sizeof(*h)) {
2504 perror("Unexpected EOF reading $TCPDIAG_FILE");
2505 return -1;
2506 }
2507
2508 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
2509
2510 if (status < 0) {
2511 perror("Reading $TCPDIAG_FILE");
2512 return -1;
2513 }
2514 if (status + sizeof(*h) < h->nlmsg_len) {
2515 perror("Unexpected EOF reading $TCPDIAG_FILE");
2516 return -1;
2517 }
2518
2519 /* The only legal exit point */
2520 if (h->nlmsg_type == NLMSG_DONE)
2521 return 0;
2522
2523 if (h->nlmsg_type == NLMSG_ERROR) {
acd1e437
SH
2524 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
2525
aba5acdf
SH
2526 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
2527 fprintf(stderr, "ERROR truncated\n");
2528 } else {
2529 errno = -err->error;
2530 perror("TCPDIAG answered");
2531 }
2532 return -1;
2533 }
2534
82d73ea0
DA
2535 parse_diag_msg(h, &s);
2536
2537 if (f && f->f && run_ssfilter(f->f, &s) == 0)
2538 continue;
2539
2540 err = inet_show_sock(h, &s, IPPROTO_TCP);
aba5acdf
SH
2541 if (err < 0)
2542 return err;
2543 }
2544}
2545
ab01dbbb 2546static int tcp_show(struct filter *f, int socktype)
aba5acdf 2547{
ab01dbbb 2548 FILE *fp = NULL;
aba5acdf
SH
2549 char *buf = NULL;
2550 int bufsize = 64*1024;
2551
1527a17e
VK
2552 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2553 return 0;
2554
aba5acdf
SH
2555 dg_proto = TCP_PROTO;
2556
2557 if (getenv("TCPDIAG_FILE"))
2558 return tcp_show_netlink_file(f);
2559
2560 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
3fe5b534 2561 && inet_show_netlink(f, NULL, socktype) == 0)
aba5acdf
SH
2562 return 0;
2563
2564 /* Sigh... We have to parse /proc/net/tcp... */
2565
ab01dbbb 2566
aba5acdf
SH
2567 /* Estimate amount of sockets and try to allocate
2568 * huge buffer to read all the table at one read.
2569 * Limit it by 16MB though. The assumption is: as soon as
2570 * kernel was able to hold information about N connections,
2571 * it is able to give us some memory for snapshot.
2572 */
2573 if (1) {
a221d621
BL
2574 get_slabstat(&slabstat);
2575
aba5acdf 2576 int guess = slabstat.socks+slabstat.tcp_syns;
acd1e437 2577
aba5acdf
SH
2578 if (f->states&(1<<SS_TIME_WAIT))
2579 guess += slabstat.tcp_tws;
2580 if (guess > (16*1024*1024)/128)
2581 guess = (16*1024*1024)/128;
2582 guess *= 128;
2583 if (guess > bufsize)
2584 bufsize = guess;
2585 }
2586 while (bufsize >= 64*1024) {
2587 if ((buf = malloc(bufsize)) != NULL)
2588 break;
2589 bufsize /= 2;
2590 }
2591 if (buf == NULL) {
2592 errno = ENOMEM;
2593 return -1;
2594 }
2595
2596 if (f->families & (1<<AF_INET)) {
69cae645 2597 if ((fp = net_tcp_open()) == NULL)
aba5acdf 2598 goto outerr;
ab01dbbb
SH
2599
2600 setbuffer(fp, buf, bufsize);
2601 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
aba5acdf 2602 goto outerr;
ab01dbbb 2603 fclose(fp);
aba5acdf
SH
2604 }
2605
2606 if ((f->families & (1<<AF_INET6)) &&
69cae645 2607 (fp = net_tcp6_open()) != NULL) {
ab01dbbb
SH
2608 setbuffer(fp, buf, bufsize);
2609 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
aba5acdf 2610 goto outerr;
ab01dbbb 2611 fclose(fp);
aba5acdf
SH
2612 }
2613
2614 free(buf);
2615 return 0;
2616
2617outerr:
2618 do {
2619 int saved_errno = errno;
acd1e437 2620
92de1c2c 2621 free(buf);
ab01dbbb
SH
2622 if (fp)
2623 fclose(fp);
aba5acdf
SH
2624 errno = saved_errno;
2625 return -1;
2626 } while (0);
2627}
2628
2629
d1f28cf1 2630static int dgram_show_line(char *line, const struct filter *f, int family)
aba5acdf 2631{
055840f2 2632 struct sockstat s = {};
aba5acdf
SH
2633 char *loc, *rem, *data;
2634 char opt[256];
2635 int n;
aba5acdf 2636
8250bc9f 2637 if (proc_inet_split_line(line, &loc, &rem, &data))
aba5acdf 2638 return -1;
aba5acdf 2639
8250bc9f 2640 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
acd1e437 2641
8250bc9f
VK
2642 if (!(f->states & (1 << state)))
2643 return 0;
aba5acdf 2644
8250bc9f 2645 proc_parse_inet_addr(loc, rem, family, &s);
aba5acdf
SH
2646
2647 if (f->f && run_ssfilter(f->f, &s) == 0)
2648 return 0;
2649
2650 opt[0] = 0;
e7113c61 2651 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
aba5acdf
SH
2652 &s.state, &s.wq, &s.rq,
2653 &s.uid, &s.ino,
2654 &s.refcnt, &s.sk, opt);
2655
2656 if (n < 9)
2657 opt[0] = 0;
2658
235c4453 2659 inet_stats_print(&s, dg_proto == UDP_PROTO ? IPPROTO_UDP : 0);
aba5acdf 2660
f1b39e1b
VK
2661 if (show_details && opt[0])
2662 printf(" opt:\"%s\"", opt);
aba5acdf 2663
8250bc9f 2664 printf("\n");
aba5acdf
SH
2665 return 0;
2666}
2667
d1f28cf1 2668static int udp_show(struct filter *f)
aba5acdf 2669{
ab01dbbb 2670 FILE *fp = NULL;
aba5acdf 2671
1527a17e
VK
2672 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2673 return 0;
2674
ace5cb31
VK
2675 dg_proto = UDP_PROTO;
2676
346f8ca8
PE
2677 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2678 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
2679 return 0;
2680
aba5acdf 2681 if (f->families&(1<<AF_INET)) {
69cae645 2682 if ((fp = net_udp_open()) == NULL)
aba5acdf 2683 goto outerr;
ab01dbbb 2684 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
aba5acdf 2685 goto outerr;
ab01dbbb 2686 fclose(fp);
aba5acdf
SH
2687 }
2688
2689 if ((f->families&(1<<AF_INET6)) &&
69cae645 2690 (fp = net_udp6_open()) != NULL) {
ab01dbbb 2691 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
aba5acdf 2692 goto outerr;
ab01dbbb 2693 fclose(fp);
aba5acdf
SH
2694 }
2695 return 0;
2696
2697outerr:
2698 do {
2699 int saved_errno = errno;
acd1e437 2700
ab01dbbb
SH
2701 if (fp)
2702 fclose(fp);
aba5acdf
SH
2703 errno = saved_errno;
2704 return -1;
2705 } while (0);
2706}
2707
d1f28cf1 2708static int raw_show(struct filter *f)
aba5acdf 2709{
ab01dbbb 2710 FILE *fp = NULL;
aba5acdf 2711
1527a17e
VK
2712 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2713 return 0;
2714
aba5acdf
SH
2715 dg_proto = RAW_PROTO;
2716
2717 if (f->families&(1<<AF_INET)) {
69cae645 2718 if ((fp = net_raw_open()) == NULL)
aba5acdf 2719 goto outerr;
ab01dbbb 2720 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
aba5acdf 2721 goto outerr;
ab01dbbb 2722 fclose(fp);
aba5acdf
SH
2723 }
2724
2725 if ((f->families&(1<<AF_INET6)) &&
69cae645 2726 (fp = net_raw6_open()) != NULL) {
ab01dbbb 2727 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
aba5acdf 2728 goto outerr;
ab01dbbb 2729 fclose(fp);
aba5acdf
SH
2730 }
2731 return 0;
2732
2733outerr:
2734 do {
2735 int saved_errno = errno;
acd1e437 2736
ab01dbbb
SH
2737 if (fp)
2738 fclose(fp);
aba5acdf
SH
2739 errno = saved_errno;
2740 return -1;
2741 } while (0);
2742}
2743
aba5acdf
SH
2744int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2745 SS_ESTABLISHED, SS_CLOSING };
2746
ec4d0d8a 2747#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
aba5acdf 2748
ec4d0d8a 2749static void unix_list_free(struct sockstat *list)
aba5acdf
SH
2750{
2751 while (list) {
ec4d0d8a 2752 struct sockstat *s = list;
ec4d0d8a 2753
aba5acdf 2754 list = list->next;
99bb68ff 2755 free(s->name);
aba5acdf
SH
2756 free(s);
2757 }
2758}
2759
30b669d7
MY
2760static const char *unix_netid_name(int type)
2761{
2762 const char *netid;
2763
2764 switch (type) {
2765 case SOCK_STREAM:
2766 netid = "u_str";
2767 break;
2768 case SOCK_SEQPACKET:
2769 netid = "u_seq";
2770 break;
2771 case SOCK_DGRAM:
2772 default:
2773 netid = "u_dgr";
2774 break;
2775 }
2776 return netid;
2777}
2778
ec4d0d8a 2779static bool unix_type_skip(struct sockstat *s, struct filter *f)
bf4ceee6
VK
2780{
2781 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2782 return true;
2783 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2784 return true;
2785 if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB)))
2786 return true;
2787 return false;
2788}
2789
ec4d0d8a
VK
2790static bool unix_use_proc(void)
2791{
2792 return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT");
2793}
2794
2795static void unix_stats_print(struct sockstat *list, struct filter *f)
aba5acdf 2796{
ec4d0d8a 2797 struct sockstat *s;
99bb68ff 2798 char *peer;
b217df10 2799 char *ctx_buf = NULL;
ec4d0d8a 2800 bool use_proc = unix_use_proc();
b217df10 2801 char port_name[30] = {};
aba5acdf
SH
2802
2803 for (s = list; s; s = s->next) {
ec4d0d8a 2804 if (!(f->states & (1 << s->state)))
aba5acdf 2805 continue;
bf4ceee6 2806 if (unix_type_skip(s, f))
30b669d7 2807 continue;
aba5acdf 2808
99bb68ff
VK
2809 peer = "*";
2810 if (s->peer_name)
2811 peer = s->peer_name;
ec4d0d8a
VK
2812
2813 if (s->rport && use_proc) {
2814 struct sockstat *p;
bf4ceee6 2815
aba5acdf 2816 for (p = list; p; p = p->next) {
ec4d0d8a 2817 if (s->rport == p->lport)
aba5acdf
SH
2818 break;
2819 }
ec4d0d8a 2820
aba5acdf
SH
2821 if (!p) {
2822 peer = "?";
2823 } else {
99bb68ff 2824 peer = p->name ? : "*";
aba5acdf
SH
2825 }
2826 }
2827
29999b0f 2828 if (use_proc && f->f) {
d17b136f
PS
2829 struct sockstat st = {
2830 .local.family = AF_UNIX,
2831 .remote.family = AF_UNIX,
2832 };
acd1e437 2833
99bb68ff 2834 memcpy(st.local.data, &s->name, sizeof(s->name));
d17b136f 2835 if (strcmp(peer, "*"))
99bb68ff
VK
2836 memcpy(st.remote.data, &peer, sizeof(peer));
2837 if (run_ssfilter(f->f, &st) == 0)
aba5acdf
SH
2838 continue;
2839 }
2840
2d791bc8
VK
2841 sock_state_print(s, unix_netid_name(s->type));
2842
99bb68ff 2843 sock_addr_print(s->name ?: "*", " ",
b217df10
VK
2844 int_to_str(s->lport, port_name), NULL);
2845 sock_addr_print(peer, " ", int_to_str(s->rport, port_name),
2846 NULL);
116ac927
RH
2847
2848 if (show_proc_ctx || show_sock_ctx) {
b217df10 2849 if (find_entry(s->ino, &ctx_buf,
116ac927
RH
2850 (show_proc_ctx & show_sock_ctx) ?
2851 PROC_SOCK_CTX : PROC_CTX) > 0) {
b217df10
VK
2852 printf(" users:(%s)", ctx_buf);
2853 free(ctx_buf);
116ac927
RH
2854 }
2855 } else if (show_users) {
b217df10
VK
2856 if (find_entry(s->ino, &ctx_buf, USERS) > 0) {
2857 printf(" users:(%s)", ctx_buf);
2858 free(ctx_buf);
116ac927 2859 }
aba5acdf
SH
2860 }
2861 printf("\n");
2862 }
2863}
2864
8a4025f6 2865static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
2866 void *arg)
dfbaa90d 2867{
8a4025f6 2868 struct filter *f = (struct filter *)arg;
dfbaa90d
PE
2869 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2870 struct rtattr *tb[UNIX_DIAG_MAX+1];
99bb68ff
VK
2871 char name[128];
2872 struct sockstat stat = { .name = "*", .peer_name = "*" };
dfbaa90d 2873
acd1e437 2874 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1),
dfbaa90d
PE
2875 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2876
bf4ceee6
VK
2877 stat.type = r->udiag_type;
2878 stat.state = r->udiag_state;
ec4d0d8a
VK
2879 stat.ino = stat.lport = r->udiag_ino;
2880 stat.local.family = stat.remote.family = AF_UNIX;
0d2e01c5 2881
bf4ceee6
VK
2882 if (unix_type_skip(&stat, f))
2883 return 0;
dfbaa90d 2884
defd61ca
HFS
2885 if (tb[UNIX_DIAG_RQLEN]) {
2886 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
acd1e437 2887
bf4ceee6
VK
2888 stat.rq = rql->udiag_rqueue;
2889 stat.wq = rql->udiag_wqueue;
defd61ca 2890 }
dfbaa90d
PE
2891 if (tb[UNIX_DIAG_NAME]) {
2892 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2893
2894 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2895 name[len] = '\0';
2896 if (name[0] == '\0')
2897 name[0] = '@';
99bb68ff
VK
2898 stat.name = &name[0];
2899 memcpy(stat.local.data, &stat.name, sizeof(stat.name));
bf4ceee6 2900 }
dfbaa90d 2901 if (tb[UNIX_DIAG_PEER])
ec4d0d8a 2902 stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
dfbaa90d 2903
29999b0f
VK
2904 if (f->f && run_ssfilter(f->f, &stat) == 0)
2905 return 0;
2906
bf4ceee6 2907 unix_stats_print(&stat, f);
dfbaa90d 2908
51ff9f24 2909 if (show_mem) {
bf4ceee6 2910 printf("\t");
51ff9f24
HFS
2911 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2912 }
5b816047
PE
2913 if (show_details) {
2914 if (tb[UNIX_DIAG_SHUTDOWN]) {
2915 unsigned char mask;
acd1e437 2916
5b816047
PE
2917 mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]);
2918 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2919 }
2920 }
bf4ceee6
VK
2921 if (show_mem || show_details)
2922 printf("\n");
ec4d0d8a 2923
dfbaa90d
PE
2924 return 0;
2925}
2926
8a4025f6 2927static int handle_netlink_request(struct filter *f, struct nlmsghdr *req,
2928 size_t size, rtnl_filter_t show_one_sock)
dfbaa90d 2929{
8a4025f6 2930 int ret = -1;
2931 struct rtnl_handle rth;
dfbaa90d 2932
8a4025f6 2933 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
dfbaa90d
PE
2934 return -1;
2935
8a4025f6 2936 rth.dump = MAGIC_SEQ;
dfbaa90d 2937
8a4025f6 2938 if (rtnl_send(&rth, req, size) < 0)
2939 goto Exit;
dfbaa90d 2940
8a4025f6 2941 if (rtnl_dump_filter(&rth, show_one_sock, f))
2942 goto Exit;
dfbaa90d 2943
8a4025f6 2944 ret = 0;
2945Exit:
2946 rtnl_close(&rth);
2947 return ret;
dfbaa90d
PE
2948}
2949
8a4025f6 2950static int unix_show_netlink(struct filter *f)
d8402b96 2951{
5fb421d4 2952 DIAG_REQUEST(req, struct unix_diag_req r);
d8402b96
AV
2953
2954 req.r.sdiag_family = AF_UNIX;
2955 req.r.udiag_states = f->states;
2956 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
2957 if (show_mem)
2958 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
2959
8a4025f6 2960 return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
d8402b96
AV
2961}
2962
d1f28cf1 2963static int unix_show(struct filter *f)
aba5acdf
SH
2964{
2965 FILE *fp;
2966 char buf[256];
2967 char name[128];
2968 int newformat = 0;
2969 int cnt;
ec4d0d8a 2970 struct sockstat *list = NULL;
aba5acdf 2971
9db7bf15
VK
2972 if (!filter_af_get(f, AF_UNIX))
2973 return 0;
2974
ec4d0d8a 2975 if (!unix_use_proc() && unix_show_netlink(f) == 0)
dfbaa90d
PE
2976 return 0;
2977
ab01dbbb 2978 if ((fp = net_unix_open()) == NULL)
aba5acdf 2979 return -1;
61170fd8 2980 if (!fgets(buf, sizeof(buf), fp)) {
d572ed4d
PS
2981 fclose(fp);
2982 return -1;
2983 }
aba5acdf 2984
ae665a52 2985 if (memcmp(buf, "Peer", 4) == 0)
aba5acdf
SH
2986 newformat = 1;
2987 cnt = 0;
2988
61170fd8 2989 while (fgets(buf, sizeof(buf), fp)) {
ec4d0d8a 2990 struct sockstat *u, **insp;
aba5acdf
SH
2991 int flags;
2992
0ee9052f 2993 if (!(u = calloc(1, sizeof(*u))))
aba5acdf 2994 break;
99bb68ff
VK
2995 u->name = NULL;
2996 u->peer_name = NULL;
aba5acdf
SH
2997
2998 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
ec4d0d8a 2999 &u->rport, &u->rq, &u->wq, &flags, &u->type,
aba5acdf
SH
3000 &u->state, &u->ino, name) < 8)
3001 name[0] = 0;
3002
ec4d0d8a
VK
3003 u->lport = u->ino;
3004 u->local.family = u->remote.family = AF_UNIX;
3005
3006 if (flags & (1 << 16)) {
aba5acdf
SH
3007 u->state = SS_LISTEN;
3008 } else {
3009 u->state = unix_state_map[u->state-1];
ec4d0d8a 3010 if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport)
aba5acdf
SH
3011 u->state = SS_ESTABLISHED;
3012 }
3013
3014 if (!newformat) {
ec4d0d8a 3015 u->rport = 0;
aba5acdf
SH
3016 u->rq = 0;
3017 u->wq = 0;
3018 }
3019
3020 insp = &list;
3021 while (*insp) {
3022 if (u->type < (*insp)->type ||
3023 (u->type == (*insp)->type &&
3024 u->ino < (*insp)->ino))
3025 break;
3026 insp = &(*insp)->next;
3027 }
3028 u->next = *insp;
3029 *insp = u;
3030
3031 if (name[0]) {
99bb68ff
VK
3032 if ((u->name = malloc(strlen(name)+1)) == NULL)
3033 break;
3034 strcpy(u->name, name);
aba5acdf
SH
3035 }
3036 if (++cnt > MAX_UNIX_REMEMBER) {
bf4ceee6 3037 unix_stats_print(list, f);
aba5acdf
SH
3038 unix_list_free(list);
3039 list = NULL;
3040 cnt = 0;
3041 }
3042 }
a3fd8e58 3043 fclose(fp);
aba5acdf 3044 if (list) {
bf4ceee6 3045 unix_stats_print(list, f);
aba5acdf
SH
3046 unix_list_free(list);
3047 list = NULL;
3048 cnt = 0;
3049 }
3050
3051 return 0;
3052}
3053
89f634f9 3054static int packet_stats_print(struct sockstat *s, const struct filter *f)
4a0053b6
VK
3055{
3056 char *buf = NULL;
b217df10
VK
3057 const char *addr, *port;
3058 char ll_name[16];
372c30d2 3059
4a0053b6 3060 if (f->f) {
89f634f9
VK
3061 s->local.family = AF_PACKET;
3062 s->remote.family = AF_PACKET;
89f634f9 3063 s->local.data[0] = s->prot;
89f634f9 3064 if (run_ssfilter(f->f, s) == 0)
4a0053b6
VK
3065 return 1;
3066 }
372c30d2 3067
2d791bc8 3068 sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr");
372c30d2 3069
b217df10
VK
3070 if (s->prot == 3)
3071 addr = "*";
3072 else
3073 addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name));
3074
3075 if (s->iface == 0)
3076 port = "*";
3077 else
3078 port = xll_index_to_name(s->iface);
372c30d2 3079
b217df10
VK
3080 sock_addr_print(addr, ":", port, NULL);
3081 sock_addr_print("", "*", "", NULL);
116ac927
RH
3082
3083 if (show_proc_ctx || show_sock_ctx) {
4a0053b6
VK
3084 if (find_entry(s->ino, &buf,
3085 (show_proc_ctx & show_sock_ctx) ?
3086 PROC_SOCK_CTX : PROC_CTX) > 0) {
116ac927
RH
3087 printf(" users:(%s)", buf);
3088 free(buf);
3089 }
3090 } else if (show_users) {
4a0053b6 3091 if (find_entry(s->ino, &buf, USERS) > 0) {
116ac927
RH
3092 printf(" users:(%s)", buf);
3093 free(buf);
3094 }
372c30d2 3095 }
116ac927 3096
f1b39e1b
VK
3097 if (show_details)
3098 sock_details_print(s);
3099
4a0053b6
VK
3100 return 0;
3101}
3102
2631b856
VK
3103static void packet_show_ring(struct packet_diag_ring *ring)
3104{
3105 printf("blk_size:%d", ring->pdr_block_size);
3106 printf(",blk_nr:%d", ring->pdr_block_nr);
3107 printf(",frm_size:%d", ring->pdr_frame_size);
3108 printf(",frm_nr:%d", ring->pdr_frame_nr);
3109 printf(",tmo:%d", ring->pdr_retire_tmo);
3110 printf(",features:0x%x", ring->pdr_features);
3111}
3112
4a0053b6
VK
3113static int packet_show_sock(const struct sockaddr_nl *addr,
3114 struct nlmsghdr *nlh, void *arg)
3115{
3116 const struct filter *f = arg;
3117 struct packet_diag_msg *r = NLMSG_DATA(nlh);
2631b856
VK
3118 struct packet_diag_info *pinfo = NULL;
3119 struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
4a0053b6 3120 struct rtattr *tb[PACKET_DIAG_MAX+1];
89f634f9 3121 struct sockstat stat = {};
2631b856
VK
3122 uint32_t fanout = 0;
3123 bool has_fanout = false;
4a0053b6 3124
acd1e437 3125 parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1),
4a0053b6
VK
3126 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3127
3128 /* use /proc/net/packet if all info are not available */
3129 if (!tb[PACKET_DIAG_MEMINFO])
3130 return -1;
3131
2d791bc8
VK
3132 stat.type = r->pdiag_type;
3133 stat.prot = r->pdiag_num;
3134 stat.ino = r->pdiag_ino;
3135 stat.state = SS_CLOSE;
f1b39e1b 3136 stat.sk = cookie_sk_get(&r->pdiag_cookie[0]);
4a0053b6
VK
3137
3138 if (tb[PACKET_DIAG_MEMINFO]) {
3139 __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
acd1e437 3140
4a0053b6
VK
3141 stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3142 }
3143
3144 if (tb[PACKET_DIAG_INFO]) {
2631b856 3145 pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
2d791bc8 3146 stat.lport = stat.iface = pinfo->pdi_index;
4a0053b6
VK
3147 }
3148
f1b39e1b
VK
3149 if (tb[PACKET_DIAG_UID])
3150 stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]);
3151
2631b856
VK
3152 if (tb[PACKET_DIAG_RX_RING])
3153 ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
3154
3155 if (tb[PACKET_DIAG_TX_RING])
3156 ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
3157
3158 if (tb[PACKET_DIAG_FANOUT]) {
3159 has_fanout = true;
3160 fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]);
3161 }
3162
4a0053b6
VK
3163 if (packet_stats_print(&stat, f))
3164 return 0;
3165
2631b856
VK
3166 if (show_details) {
3167 if (pinfo) {
3168 printf("\n\tver:%d", pinfo->pdi_version);
3169 printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
3170 printf(" flags( ");
3171 if (pinfo->pdi_flags & PDI_RUNNING)
3172 printf("running");
3173 if (pinfo->pdi_flags & PDI_AUXDATA)
3174 printf(" auxdata");
3175 if (pinfo->pdi_flags & PDI_ORIGDEV)
3176 printf(" origdev");
3177 if (pinfo->pdi_flags & PDI_VNETHDR)
3178 printf(" vnethdr");
3179 if (pinfo->pdi_flags & PDI_LOSS)
3180 printf(" loss");
3181 if (!pinfo->pdi_flags)
3182 printf("0");
3183 printf(" )");
3184 }
3185 if (ring_rx) {
3186 printf("\n\tring_rx(");
3187 packet_show_ring(ring_rx);
3188 printf(")");
3189 }
3190 if (ring_tx) {
3191 printf("\n\tring_tx(");
3192 packet_show_ring(ring_tx);
3193 printf(")");
3194 }
3195 if (has_fanout) {
3196 uint16_t type = (fanout >> 16) & 0xffff;
3197
3198 printf("\n\tfanout(");
3199 printf("id:%d,", fanout & 0xffff);
3200 printf("type:");
3201
3202 if (type == 0)
3203 printf("hash");
3204 else if (type == 1)
3205 printf("lb");
3206 else if (type == 2)
3207 printf("cpu");
3208 else if (type == 3)
3209 printf("roll");
3210 else if (type == 4)
3211 printf("random");
3212 else if (type == 5)
3213 printf("qm");
3214 else
3215 printf("0x%x", type);
3216
3217 printf(")");
3218 }
3219 }
3220
372c30d2
ND
3221 if (show_bpf && tb[PACKET_DIAG_FILTER]) {
3222 struct sock_filter *fil =
3223 RTA_DATA(tb[PACKET_DIAG_FILTER]);
3224 int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) /
3225 sizeof(struct sock_filter);
3226
3227 printf("\n\tbpf filter (%d): ", num);
3228 while (num) {
3229 printf(" 0x%02x %u %u %u,",
3230 fil->code, fil->jt, fil->jf, fil->k);
3231 num--;
3232 fil++;
3233 }
3234 }
3235 printf("\n");
3236 return 0;
3237}
3238
8a4025f6 3239static int packet_show_netlink(struct filter *f)
372c30d2 3240{
5fb421d4 3241 DIAG_REQUEST(req, struct packet_diag_req r);
372c30d2 3242
372c30d2 3243 req.r.sdiag_family = AF_PACKET;
2631b856
VK
3244 req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
3245 PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
372c30d2 3246
8a4025f6 3247 return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
372c30d2
ND
3248}
3249
4a0053b6
VK
3250static int packet_show_line(char *buf, const struct filter *f, int fam)
3251{
3252 unsigned long long sk;
89f634f9 3253 struct sockstat stat = {};
4a0053b6
VK
3254 int type, prot, iface, state, rq, uid, ino;
3255
3256 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
3257 &sk,
3258 &type, &prot, &iface, &state,
3259 &rq, &uid, &ino);
3260
3261 if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
3262 return 0;
3263 if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
3264 return 0;
3265
3266 stat.type = type;
3267 stat.prot = prot;
2d791bc8 3268 stat.lport = stat.iface = iface;
4a0053b6
VK
3269 stat.state = state;
3270 stat.rq = rq;
3271 stat.uid = uid;
3272 stat.ino = ino;
2d791bc8
VK
3273 stat.state = SS_CLOSE;
3274
4a0053b6
VK
3275 if (packet_stats_print(&stat, f))
3276 return 0;
3277
4a0053b6
VK
3278 printf("\n");
3279 return 0;
3280}
aba5acdf 3281
d1f28cf1 3282static int packet_show(struct filter *f)
aba5acdf
SH
3283{
3284 FILE *fp;
b95d28c3 3285 int rc = 0;
3d0b7439 3286
9db7bf15 3287 if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
b9ea445d 3288 return 0;
aba5acdf 3289
4a0053b6
VK
3290 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3291 packet_show_netlink(f) == 0)
372c30d2
ND
3292 return 0;
3293
ab01dbbb 3294 if ((fp = net_packet_open()) == NULL)
aba5acdf 3295 return -1;
4a0053b6 3296 if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
b95d28c3 3297 rc = -1;
aba5acdf 3298
b95d28c3
PS
3299 fclose(fp);
3300 return rc;
aba5acdf
SH
3301}
3302
5f24ec0e 3303static int netlink_show_one(struct filter *f,
acd1e437
SH
3304 int prot, int pid, unsigned int groups,
3305 int state, int dst_pid, unsigned int dst_group,
129709ae
AV
3306 int rq, int wq,
3307 unsigned long long sk, unsigned long long cb)
3308{
2d791bc8 3309 struct sockstat st;
acd1e437 3310
b217df10
VK
3311 SPRINT_BUF(prot_buf) = {};
3312 const char *prot_name;
3313 char procname[64] = {};
eef43b50 3314
2d791bc8
VK
3315 st.state = SS_CLOSE;
3316 st.rq = rq;
3317 st.wq = wq;
3318
129709ae 3319 if (f->f) {
055840f2
VK
3320 st.local.family = AF_NETLINK;
3321 st.remote.family = AF_NETLINK;
3322 st.rport = -1;
3323 st.lport = pid;
3324 st.local.data[0] = prot;
055840f2 3325 if (run_ssfilter(f->f, &st) == 0)
5f24ec0e 3326 return 1;
129709ae
AV
3327 }
3328
2d791bc8 3329 sock_state_print(&st, "nl");
eef43b50 3330
b217df10
VK
3331 if (resolve_services)
3332 prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf));
3333 else
3334 prot_name = int_to_str(prot, prot_buf);
eef43b50 3335
129709ae 3336 if (pid == -1) {
b217df10 3337 procname[0] = '*';
129709ae
AV
3338 } else if (resolve_services) {
3339 int done = 0;
acd1e437 3340
129709ae
AV
3341 if (!pid) {
3342 done = 1;
b217df10 3343 strncpy(procname, "kernel", 6);
129709ae 3344 } else if (pid > 0) {
129709ae 3345 FILE *fp;
acd1e437 3346
0ee9052f 3347 snprintf(procname, sizeof(procname), "%s/%d/stat",
129709ae
AV
3348 getenv("PROC_ROOT") ? : "/proc", pid);
3349 if ((fp = fopen(procname, "r")) != NULL) {
3350 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
0ee9052f 3351 snprintf(procname+strlen(procname),
3352 sizeof(procname)-strlen(procname),
3353 "/%d", pid);
129709ae
AV
3354 done = 1;
3355 }
3356 fclose(fp);
3357 }
3358 }
3359 if (!done)
b217df10 3360 int_to_str(pid, procname);
129709ae 3361 } else {
b217df10 3362 int_to_str(pid, procname);
129709ae 3363 }
f271fe01 3364
b217df10
VK
3365 sock_addr_print(prot_name, ":", procname, NULL);
3366
f271fe01 3367 if (state == NETLINK_CONNECTED) {
b217df10
VK
3368 char dst_group_buf[30];
3369 char dst_pid_buf[30];
acd1e437 3370
b217df10
VK
3371 sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
3372 int_to_str(dst_pid, dst_pid_buf), NULL);
f271fe01 3373 } else {
b217df10 3374 sock_addr_print("", "*", "", NULL);
f271fe01 3375 }
129709ae 3376
116ac927 3377 char *pid_context = NULL;
acd1e437 3378
116ac927
RH
3379 if (show_proc_ctx) {
3380 /* The pid value will either be:
3381 * 0 if destination kernel - show kernel initial context.
3382 * A valid process pid - use getpidcon.
3383 * A unique value allocated by the kernel or netlink user
3384 * to the process - show context as "not available".
3385 */
3386 if (!pid)
3387 security_get_initial_context("kernel", &pid_context);
3388 else if (pid > 0)
3389 getpidcon(pid, &pid_context);
3390
3391 if (pid_context != NULL) {
3392 printf("proc_ctx=%-*s ", serv_width, pid_context);
3393 free(pid_context);
3394 } else {
3395 printf("proc_ctx=%-*s ", serv_width, "unavailable");
3396 }
3397 }
3398
129709ae
AV
3399 if (show_details) {
3400 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
3401 }
3402 printf("\n");
3403
5f24ec0e 3404 return 0;
129709ae
AV
3405}
3406
8a4025f6 3407static int netlink_show_sock(const struct sockaddr_nl *addr,
3408 struct nlmsghdr *nlh, void *arg)
ecb928c8 3409{
8a4025f6 3410 struct filter *f = (struct filter *)arg;
ecb928c8
AV
3411 struct netlink_diag_msg *r = NLMSG_DATA(nlh);
3412 struct rtattr *tb[NETLINK_DIAG_MAX+1];
3413 int rq = 0, wq = 0;
3414 unsigned long groups = 0;
3415
acd1e437 3416 parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1),
ecb928c8
AV
3417 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3418
3419 if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
3420 groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]);
3421
3422 if (tb[NETLINK_DIAG_MEMINFO]) {
3423 const __u32 *skmeminfo;
acd1e437 3424
ecb928c8
AV
3425 skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
3426
3427 rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3428 wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC];
3429 }
3430
5f24ec0e 3431 if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups,
ecb928c8 3432 r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group,
5f24ec0e
VK
3433 rq, wq, 0, 0)) {
3434 return 0;
3435 }
ecb928c8
AV
3436
3437 if (show_mem) {
3438 printf("\t");
3439 print_skmeminfo(tb, NETLINK_DIAG_MEMINFO);
3440 printf("\n");
3441 }
3442
3443 return 0;
3444}
3445
8a4025f6 3446static int netlink_show_netlink(struct filter *f)
ecb928c8 3447{
5fb421d4 3448 DIAG_REQUEST(req, struct netlink_diag_req r);
ecb928c8
AV
3449
3450 req.r.sdiag_family = AF_NETLINK;
3451 req.r.sdiag_protocol = NDIAG_PROTO_ALL;
3452 req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO;
3453
8a4025f6 3454 return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock);
ecb928c8
AV
3455}
3456
d1f28cf1 3457static int netlink_show(struct filter *f)
aba5acdf
SH
3458{
3459 FILE *fp;
3460 char buf[256];
3461 int prot, pid;
acd1e437 3462 unsigned int groups;
aba5acdf
SH
3463 int rq, wq, rc;
3464 unsigned long long sk, cb;
3465
9db7bf15 3466 if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
b9ea445d
VK
3467 return 0;
3468
129709ae 3469 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
8a4025f6 3470 netlink_show_netlink(f) == 0)
129709ae
AV
3471 return 0;
3472
ab01dbbb 3473 if ((fp = net_netlink_open()) == NULL)
aba5acdf 3474 return -1;
61170fd8 3475 if (!fgets(buf, sizeof(buf), fp)) {
d572ed4d
PS
3476 fclose(fp);
3477 return -1;
3478 }
aba5acdf 3479
61170fd8 3480 while (fgets(buf, sizeof(buf), fp)) {
aba5acdf
SH
3481 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
3482 &sk,
3483 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
3484
f271fe01 3485 netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
aba5acdf
SH
3486 }
3487
b95d28c3 3488 fclose(fp);
aba5acdf
SH
3489 return 0;
3490}
3491
6885e3bf
CG
3492struct sock_diag_msg {
3493 __u8 sdiag_family;
3494};
3495
3496static int generic_show_sock(const struct sockaddr_nl *addr,
3497 struct nlmsghdr *nlh, void *arg)
3498{
3499 struct sock_diag_msg *r = NLMSG_DATA(nlh);
3500 struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX };
3501
3502 switch (r->sdiag_family) {
3503 case AF_INET:
3504 case AF_INET6:
3505 return show_one_inet_sock(addr, nlh, &inet_arg);
3506 case AF_UNIX:
3507 return unix_show_sock(addr, nlh, arg);
3508 case AF_PACKET:
3509 return packet_show_sock(addr, nlh, arg);
3510 case AF_NETLINK:
3511 return netlink_show_sock(addr, nlh, arg);
3512 default:
3513 return -1;
3514 }
3515}
3516
3517static int handle_follow_request(struct filter *f)
3518{
3519 int ret = -1;
3520 int groups = 0;
3521 struct rtnl_handle rth;
3522
3523 if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB))
3524 groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1);
3525 if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB))
3526 groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1);
3527 if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB))
3528 groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1);
3529 if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB))
3530 groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1);
3531
3532 if (groups == 0)
3533 return -1;
3534
3535 if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG))
3536 return -1;
3537
3538 rth.dump = 0;
3539 rth.local.nl_pid = 0;
3540
3541 if (rtnl_dump_filter(&rth, generic_show_sock, f))
3542 goto Exit;
3543
3544 ret = 0;
3545Exit:
3546 rtnl_close(&rth);
3547 return ret;
3548}
3549
acd1e437 3550struct snmpstat {
aba5acdf
SH
3551 int tcp_estab;
3552};
3553
d1f28cf1 3554static int get_snmp_int(char *proto, char *key, int *result)
aba5acdf
SH
3555{
3556 char buf[1024];
3557 FILE *fp;
3558 int protolen = strlen(proto);
3559 int keylen = strlen(key);
3560
3561 *result = 0;
3562
ab01dbbb 3563 if ((fp = net_snmp_open()) == NULL)
aba5acdf
SH
3564 return -1;
3565
3566 while (fgets(buf, sizeof(buf), fp) != NULL) {
3567 char *p = buf;
3568 int pos = 0;
acd1e437 3569
aba5acdf
SH
3570 if (memcmp(buf, proto, protolen))
3571 continue;
3572 while ((p = strchr(p, ' ')) != NULL) {
3573 pos++;
3574 p++;
3575 if (memcmp(p, key, keylen) == 0 &&
3576 (p[keylen] == ' ' || p[keylen] == '\n'))
3577 break;
3578 }
3579 if (fgets(buf, sizeof(buf), fp) == NULL)
3580 break;
3581 if (memcmp(buf, proto, protolen))
3582 break;
3583 p = buf;
3584 while ((p = strchr(p, ' ')) != NULL) {
3585 p++;
3586 if (--pos == 0) {
3587 sscanf(p, "%d", result);
3588 fclose(fp);
3589 return 0;
3590 }
3591 }
3592 }
3593
3594 fclose(fp);
3595 errno = ESRCH;
3596 return -1;
3597}
3598
3599
3600/* Get stats from sockstat */
3601
acd1e437 3602struct ssummary {
aba5acdf
SH
3603 int socks;
3604 int tcp_mem;
3605 int tcp_total;
3606 int tcp_orphans;
3607 int tcp_tws;
3608 int tcp4_hashed;
3609 int udp4;
3610 int raw4;
3611 int frag4;
3612 int frag4_mem;
3613 int tcp6_hashed;
3614 int udp6;
3615 int raw6;
3616 int frag6;
3617 int frag6_mem;
3618};
3619
055840f2 3620static void get_sockstat_line(char *line, struct ssummary *s)
aba5acdf
SH
3621{
3622 char id[256], rem[256];
3623
3624 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
3625 return;
3626
3627 if (strcmp(id, "sockets:") == 0)
3628 sscanf(rem, "%*s%d", &s->socks);
3629 else if (strcmp(id, "UDP:") == 0)
3630 sscanf(rem, "%*s%d", &s->udp4);
3631 else if (strcmp(id, "UDP6:") == 0)
3632 sscanf(rem, "%*s%d", &s->udp6);
3633 else if (strcmp(id, "RAW:") == 0)
3634 sscanf(rem, "%*s%d", &s->raw4);
3635 else if (strcmp(id, "RAW6:") == 0)
3636 sscanf(rem, "%*s%d", &s->raw6);
3637 else if (strcmp(id, "TCP6:") == 0)
3638 sscanf(rem, "%*s%d", &s->tcp6_hashed);
3639 else if (strcmp(id, "FRAG:") == 0)
3640 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
3641 else if (strcmp(id, "FRAG6:") == 0)
3642 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
3643 else if (strcmp(id, "TCP:") == 0)
3644 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
3645 &s->tcp4_hashed,
3646 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
3647}
3648
055840f2 3649static int get_sockstat(struct ssummary *s)
aba5acdf
SH
3650{
3651 char buf[256];
3652 FILE *fp;
3653
3654 memset(s, 0, sizeof(*s));
3655
ab01dbbb 3656 if ((fp = net_sockstat_open()) == NULL)
aba5acdf 3657 return -1;
acd1e437 3658 while (fgets(buf, sizeof(buf), fp) != NULL)
aba5acdf
SH
3659 get_sockstat_line(buf, s);
3660 fclose(fp);
3661
ab01dbbb 3662 if ((fp = net_sockstat6_open()) == NULL)
aba5acdf 3663 return 0;
acd1e437 3664 while (fgets(buf, sizeof(buf), fp) != NULL)
aba5acdf
SH
3665 get_sockstat_line(buf, s);
3666 fclose(fp);
3667
3668 return 0;
3669}
3670
d1f28cf1 3671static int print_summary(void)
aba5acdf 3672{
055840f2 3673 struct ssummary s;
aba5acdf
SH
3674 struct snmpstat sn;
3675
3676 if (get_sockstat(&s) < 0)
3677 perror("ss: get_sockstat");
3678 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
3679 perror("ss: get_snmpstat");
3680
a221d621
BL
3681 get_slabstat(&slabstat);
3682
aba5acdf
SH
3683 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
3684
3685 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
3686 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
3687 sn.tcp_estab,
3688 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
3689 s.tcp_orphans,
3690 slabstat.tcp_syns,
3691 s.tcp_tws, slabstat.tcp_tws,
3692 slabstat.tcp_ports
3693 );
3694
3695 printf("\n");
3696 printf("Transport Total IP IPv6\n");
3697 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
3698 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
3699 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
3700 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
ae665a52 3701 printf("INET %-9d %-9d %-9d\n",
aba5acdf
SH
3702 s.raw4+s.udp4+s.tcp4_hashed+
3703 s.raw6+s.udp6+s.tcp6_hashed,
3704 s.raw4+s.udp4+s.tcp4_hashed,
3705 s.raw6+s.udp6+s.tcp6_hashed);
3706 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
3707
3708 printf("\n");
3709
3710 return 0;
3711}
3712
7a96e199 3713static void _usage(FILE *dest)
aba5acdf 3714{
7a96e199 3715 fprintf(dest,
aba5acdf
SH
3716"Usage: ss [ OPTIONS ]\n"
3717" ss [ OPTIONS ] [ FILTER ]\n"
ff041f16
VK
3718" -h, --help this message\n"
3719" -V, --version output version information\n"
3720" -n, --numeric don't resolve service names\n"
ab61159a 3721" -r, --resolve resolve host names\n"
ff041f16
VK
3722" -a, --all display all sockets\n"
3723" -l, --listening display listening sockets\n"
ab61159a
SH
3724" -o, --options show timer information\n"
3725" -e, --extended show detailed socket information\n"
3726" -m, --memory show socket memory usage\n"
ff041f16
VK
3727" -p, --processes show process using socket\n"
3728" -i, --info show internal TCP information\n"
3729" -s, --summary show socket usage summary\n"
b0f01cf6 3730" -b, --bpf show bpf filter socket information\n"
6885e3bf 3731" -E, --events continually display sockets as they are destroyed\n"
ff041f16
VK
3732" -Z, --context display process SELinux security contexts\n"
3733" -z, --contexts display process and socket SELinux security contexts\n"
95ce04bc 3734" -N, --net switch to the specified network namespace name\n"
ab61159a
SH
3735"\n"
3736" -4, --ipv4 display only IP version 4 sockets\n"
3737" -6, --ipv6 display only IP version 6 sockets\n"
ff041f16
VK
3738" -0, --packet display PACKET sockets\n"
3739" -t, --tcp display only TCP sockets\n"
3740" -u, --udp display only UDP sockets\n"
3741" -d, --dccp display only DCCP sockets\n"
3742" -w, --raw display only RAW sockets\n"
3743" -x, --unix display only Unix domain sockets\n"
ab61159a 3744" -f, --family=FAMILY display sockets of type FAMILY\n"
ebef3174 3745" FAMILY := {inet|inet6|link|unix|netlink|help}\n"
ab61159a 3746"\n"
fb2594c1 3747" -K, --kill forcibly close sockets, display what was closed\n"
7a4559f6 3748" -H, --no-header Suppress header line\n"
fb2594c1 3749"\n"
583de149 3750" -A, --query=QUERY, --socket=QUERY\n"
56dee73e 3751" QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
ab61159a 3752"\n"
583de149 3753" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
ab61159a 3754" -F, --filter=FILE read filter information from FILE\n"
ff041f16
VK
3755" FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
3756" STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
3757" TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n"
3758" connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3759" synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3760" bucket := {syn-recv|time-wait}\n"
3761" big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n"
ab61159a 3762 );
7a96e199
AH
3763}
3764
3765static void help(void) __attribute__((noreturn));
3766static void help(void)
3767{
3768 _usage(stdout);
3769 exit(0);
3770}
3771
3772static void usage(void) __attribute__((noreturn));
3773static void usage(void)
3774{
3775 _usage(stderr);
aba5acdf
SH
3776 exit(-1);
3777}
3778
3779
d1f28cf1 3780static int scan_state(const char *state)
aba5acdf
SH
3781{
3782 int i;
acd1e437 3783
aba5acdf
SH
3784 if (strcasecmp(state, "close") == 0 ||
3785 strcasecmp(state, "closed") == 0)
3786 return (1<<SS_CLOSE);
3787 if (strcasecmp(state, "syn-rcv") == 0)
3788 return (1<<SS_SYN_RECV);
1a5bad5a 3789 if (strcasecmp(state, "established") == 0)
aba5acdf
SH
3790 return (1<<SS_ESTABLISHED);
3791 if (strcasecmp(state, "all") == 0)
3792 return SS_ALL;
3793 if (strcasecmp(state, "connected") == 0)
3794 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
1a5bad5a 3795 if (strcasecmp(state, "synchronized") == 0)
aba5acdf
SH
3796 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
3797 if (strcasecmp(state, "bucket") == 0)
3798 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
3799 if (strcasecmp(state, "big") == 0)
3800 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
acd1e437 3801 for (i = 0; i < SS_MAX; i++) {
1a5bad5a 3802 if (strcasecmp(state, sstate_namel[i]) == 0)
aba5acdf
SH
3803 return (1<<i);
3804 }
9db7bf15
VK
3805
3806 fprintf(stderr, "ss: wrong state name: %s\n", state);
3807 exit(-1);
aba5acdf
SH
3808}
3809
ab61159a
SH
3810static const struct option long_opts[] = {
3811 { "numeric", 0, 0, 'n' },
3812 { "resolve", 0, 0, 'r' },
3813 { "options", 0, 0, 'o' },
3814 { "extended", 0, 0, 'e' },
3815 { "memory", 0, 0, 'm' },
3816 { "info", 0, 0, 'i' },
3817 { "processes", 0, 0, 'p' },
372c30d2 3818 { "bpf", 0, 0, 'b' },
6885e3bf 3819 { "events", 0, 0, 'E' },
351efcde 3820 { "dccp", 0, 0, 'd' },
ab61159a
SH
3821 { "tcp", 0, 0, 't' },
3822 { "udp", 0, 0, 'u' },
3823 { "raw", 0, 0, 'w' },
3824 { "unix", 0, 0, 'x' },
3825 { "all", 0, 0, 'a' },
3826 { "listening", 0, 0, 'l' },
3827 { "ipv4", 0, 0, '4' },
3828 { "ipv6", 0, 0, '6' },
3829 { "packet", 0, 0, '0' },
3830 { "family", 1, 0, 'f' },
3831 { "socket", 1, 0, 'A' },
583de149 3832 { "query", 1, 0, 'A' },
c3f346b0 3833 { "summary", 0, 0, 's' },
583de149 3834 { "diag", 1, 0, 'D' },
ab61159a
SH
3835 { "filter", 1, 0, 'F' },
3836 { "version", 0, 0, 'V' },
3837 { "help", 0, 0, 'h' },
116ac927
RH
3838 { "context", 0, 0, 'Z' },
3839 { "contexts", 0, 0, 'z' },
95ce04bc 3840 { "net", 1, 0, 'N' },
fb2594c1 3841 { "kill", 0, 0, 'K' },
7a4559f6 3842 { "no-header", 0, 0, 'H' },
ab61159a 3843 { 0 }
ae665a52 3844
ab61159a
SH
3845};
3846
aba5acdf
SH
3847int main(int argc, char *argv[])
3848{
aba5acdf
SH
3849 int saw_states = 0;
3850 int saw_query = 0;
3851 int do_summary = 0;
7d105b56 3852 const char *dump_tcpdiag = NULL;
aba5acdf
SH
3853 FILE *filter_fp = NULL;
3854 int ch;
9db7bf15 3855 int state_filter = 0;
aba5acdf 3856
7a4559f6 3857 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KH",
ab61159a 3858 long_opts, NULL)) != EOF) {
acd1e437 3859 switch (ch) {
aba5acdf
SH
3860 case 'n':
3861 resolve_services = 0;
3862 break;
3863 case 'r':
3864 resolve_hosts = 1;
3865 break;
3866 case 'o':
3867 show_options = 1;
3868 break;
3869 case 'e':
3870 show_options = 1;
3871 show_details++;
3872 break;
3873 case 'm':
3874 show_mem = 1;
3875 break;
3876 case 'i':
3877 show_tcpinfo = 1;
3878 break;
3879 case 'p':
3880 show_users++;
fbc0f876 3881 user_ent_hash_build();
aba5acdf 3882 break;
372c30d2
ND
3883 case 'b':
3884 show_options = 1;
3885 show_bpf++;
3886 break;
6885e3bf
CG
3887 case 'E':
3888 follow_events = 1;
3889 break;
351efcde 3890 case 'd':
57ff5a10 3891 filter_db_set(&current_filter, DCCP_DB);
351efcde 3892 break;
aba5acdf 3893 case 't':
57ff5a10 3894 filter_db_set(&current_filter, TCP_DB);
aba5acdf
SH
3895 break;
3896 case 'u':
57ff5a10 3897 filter_db_set(&current_filter, UDP_DB);
aba5acdf
SH
3898 break;
3899 case 'w':
57ff5a10 3900 filter_db_set(&current_filter, RAW_DB);
aba5acdf
SH
3901 break;
3902 case 'x':
9db7bf15 3903 filter_af_set(&current_filter, AF_UNIX);
aba5acdf
SH
3904 break;
3905 case 'a':
9db7bf15 3906 state_filter = SS_ALL;
aba5acdf
SH
3907 break;
3908 case 'l':
9db7bf15 3909 state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE);
aba5acdf
SH
3910 break;
3911 case '4':
9db7bf15 3912 filter_af_set(&current_filter, AF_INET);
aba5acdf
SH
3913 break;
3914 case '6':
9db7bf15 3915 filter_af_set(&current_filter, AF_INET6);
aba5acdf
SH
3916 break;
3917 case '0':
9db7bf15 3918 filter_af_set(&current_filter, AF_PACKET);
aba5acdf
SH
3919 break;
3920 case 'f':
3921 if (strcmp(optarg, "inet") == 0)
9db7bf15 3922 filter_af_set(&current_filter, AF_INET);
aba5acdf 3923 else if (strcmp(optarg, "inet6") == 0)
9db7bf15 3924 filter_af_set(&current_filter, AF_INET6);
aba5acdf 3925 else if (strcmp(optarg, "link") == 0)
9db7bf15 3926 filter_af_set(&current_filter, AF_PACKET);
aba5acdf 3927 else if (strcmp(optarg, "unix") == 0)
9db7bf15 3928 filter_af_set(&current_filter, AF_UNIX);
aba5acdf 3929 else if (strcmp(optarg, "netlink") == 0)
9db7bf15 3930 filter_af_set(&current_filter, AF_NETLINK);
aba5acdf 3931 else if (strcmp(optarg, "help") == 0)
7a96e199 3932 help();
aba5acdf 3933 else {
9db7bf15
VK
3934 fprintf(stderr, "ss: \"%s\" is invalid family\n",
3935 optarg);
aba5acdf
SH
3936 usage();
3937 }
3938 break;
3939 case 'A':
3940 {
3941 char *p, *p1;
acd1e437 3942
aba5acdf
SH
3943 if (!saw_query) {
3944 current_filter.dbs = 0;
7f9dddbe 3945 state_filter = state_filter ?
acd1e437 3946 state_filter : SS_CONN;
aba5acdf
SH
3947 saw_query = 1;
3948 do_default = 0;
3949 }
3950 p = p1 = optarg;
3951 do {
3952 if ((p1 = strchr(p, ',')) != NULL)
ae665a52 3953 *p1 = 0;
aba5acdf 3954 if (strcmp(p, "all") == 0) {
57ff5a10 3955 filter_default_dbs(&current_filter);
aba5acdf 3956 } else if (strcmp(p, "inet") == 0) {
57ff5a10
VK
3957 filter_db_set(&current_filter, UDP_DB);
3958 filter_db_set(&current_filter, DCCP_DB);
3959 filter_db_set(&current_filter, TCP_DB);
3960 filter_db_set(&current_filter, RAW_DB);
aba5acdf 3961 } else if (strcmp(p, "udp") == 0) {
57ff5a10 3962 filter_db_set(&current_filter, UDP_DB);
351efcde 3963 } else if (strcmp(p, "dccp") == 0) {
57ff5a10 3964 filter_db_set(&current_filter, DCCP_DB);
aba5acdf 3965 } else if (strcmp(p, "tcp") == 0) {
57ff5a10 3966 filter_db_set(&current_filter, TCP_DB);
aba5acdf 3967 } else if (strcmp(p, "raw") == 0) {
57ff5a10 3968 filter_db_set(&current_filter, RAW_DB);
aba5acdf 3969 } else if (strcmp(p, "unix") == 0) {
57ff5a10
VK
3970 filter_db_set(&current_filter, UNIX_ST_DB);
3971 filter_db_set(&current_filter, UNIX_DG_DB);
3972 filter_db_set(&current_filter, UNIX_SQ_DB);
1a5bad5a 3973 } else if (strcasecmp(p, "unix_stream") == 0 ||
aba5acdf 3974 strcmp(p, "u_str") == 0) {
57ff5a10 3975 filter_db_set(&current_filter, UNIX_ST_DB);
1a5bad5a 3976 } else if (strcasecmp(p, "unix_dgram") == 0 ||
aba5acdf 3977 strcmp(p, "u_dgr") == 0) {
57ff5a10 3978 filter_db_set(&current_filter, UNIX_DG_DB);
30b669d7
MY
3979 } else if (strcasecmp(p, "unix_seqpacket") == 0 ||
3980 strcmp(p, "u_seq") == 0) {
57ff5a10 3981 filter_db_set(&current_filter, UNIX_SQ_DB);
aba5acdf 3982 } else if (strcmp(p, "packet") == 0) {
57ff5a10
VK
3983 filter_db_set(&current_filter, PACKET_R_DB);
3984 filter_db_set(&current_filter, PACKET_DG_DB);
aba5acdf
SH
3985 } else if (strcmp(p, "packet_raw") == 0 ||
3986 strcmp(p, "p_raw") == 0) {
57ff5a10 3987 filter_db_set(&current_filter, PACKET_R_DB);
aba5acdf
SH
3988 } else if (strcmp(p, "packet_dgram") == 0 ||
3989 strcmp(p, "p_dgr") == 0) {
57ff5a10 3990 filter_db_set(&current_filter, PACKET_DG_DB);
aba5acdf 3991 } else if (strcmp(p, "netlink") == 0) {
57ff5a10 3992 filter_db_set(&current_filter, NETLINK_DB);
aba5acdf
SH
3993 } else {
3994 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
3995 usage();
3996 }
3997 p = p1 + 1;
3998 } while (p1);
3999 break;
4000 }
4001 case 's':
4002 do_summary = 1;
4003 break;
4004 case 'D':
4005 dump_tcpdiag = optarg;
4006 break;
4007 case 'F':
4008 if (filter_fp) {
4009 fprintf(stderr, "More than one filter file\n");
4010 exit(-1);
4011 }
4012 if (optarg[0] == '-')
4013 filter_fp = stdin;
4014 else
4015 filter_fp = fopen(optarg, "r");
4016 if (!filter_fp) {
4017 perror("fopen filter file");
4018 exit(-1);
4019 }
4020 break;
4021 case 'v':
4022 case 'V':
4023 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
4024 exit(0);
116ac927
RH
4025 case 'z':
4026 show_sock_ctx++;
4027 case 'Z':
4028 if (is_selinux_enabled() <= 0) {
4029 fprintf(stderr, "ss: SELinux is not enabled.\n");
4030 exit(1);
4031 }
4032 show_proc_ctx++;
4033 user_ent_hash_build();
4034 break;
95ce04bc
VK
4035 case 'N':
4036 if (netns_switch(optarg))
4037 exit(1);
4038 break;
fb2594c1
LC
4039 case 'K':
4040 current_filter.kill = 1;
4041 break;
7a4559f6
DA
4042 case 'H':
4043 show_header = 0;
4044 break;
aba5acdf 4045 case 'h':
7a96e199 4046 help();
f73105ab 4047 case '?':
aba5acdf
SH
4048 default:
4049 usage();
4050 }
4051 }
4052
4053 argc -= optind;
4054 argv += optind;
4055
aba5acdf
SH
4056 if (do_summary) {
4057 print_summary();
4058 if (do_default && argc == 0)
4059 exit(0);
4060 }
4061
aba5acdf
SH
4062 while (argc > 0) {
4063 if (strcmp(*argv, "state") == 0) {
4064 NEXT_ARG();
4065 if (!saw_states)
9db7bf15
VK
4066 state_filter = 0;
4067 state_filter |= scan_state(*argv);
aba5acdf
SH
4068 saw_states = 1;
4069 } else if (strcmp(*argv, "exclude") == 0 ||
4070 strcmp(*argv, "excl") == 0) {
4071 NEXT_ARG();
4072 if (!saw_states)
9db7bf15
VK
4073 state_filter = SS_ALL;
4074 state_filter &= ~scan_state(*argv);
aba5acdf
SH
4075 saw_states = 1;
4076 } else {
aba5acdf
SH
4077 break;
4078 }
4079 argc--; argv++;
4080 }
4081
9db7bf15
VK
4082 if (do_default) {
4083 state_filter = state_filter ? state_filter : SS_CONN;
4084 filter_default_dbs(&current_filter);
9db7bf15
VK
4085 }
4086
57ff5a10
VK
4087 filter_states_set(&current_filter, state_filter);
4088 filter_merge_defaults(&current_filter);
4089
9db7bf15
VK
4090 if (resolve_services && resolve_hosts &&
4091 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
4092 init_service_resolver();
4093
4094
4095 if (current_filter.dbs == 0) {
4096 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
4097 exit(0);
4098 }
4099 if (current_filter.families == 0) {
4100 fprintf(stderr, "ss: no families to show with such filter.\n");
4101 exit(0);
4102 }
aba5acdf
SH
4103 if (current_filter.states == 0) {
4104 fprintf(stderr, "ss: no socket states to show with such filter.\n");
4105 exit(0);
4106 }
4107
4108 if (dump_tcpdiag) {
4109 FILE *dump_fp = stdout;
acd1e437 4110
aba5acdf
SH
4111 if (!(current_filter.dbs & (1<<TCP_DB))) {
4112 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
4113 exit(0);
4114 }
4115 if (dump_tcpdiag[0] != '-') {
4116 dump_fp = fopen(dump_tcpdiag, "w");
4117 if (!dump_tcpdiag) {
4118 perror("fopen dump file");
4119 exit(-1);
4120 }
4121 }
3fe5b534 4122 inet_show_netlink(&current_filter, dump_fp, IPPROTO_TCP);
aba5acdf
SH
4123 fflush(dump_fp);
4124 exit(0);
4125 }
4126
1527a17e
VK
4127 if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
4128 usage();
4129
aba5acdf
SH
4130 netid_width = 0;
4131 if (current_filter.dbs&(current_filter.dbs-1))
4132 netid_width = 5;
4133
4134 state_width = 0;
4135 if (current_filter.states&(current_filter.states-1))
4136 state_width = 10;
4137
4138 screen_width = 80;
4139 if (isatty(STDOUT_FILENO)) {
4140 struct winsize w;
4141
4142 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
4143 if (w.ws_col > 0)
4144 screen_width = w.ws_col;
4145 }
4146 }
4147
4148 addrp_width = screen_width;
4149 addrp_width -= netid_width+1;
4150 addrp_width -= state_width+1;
4151 addrp_width -= 14;
4152
4153 if (addrp_width&1) {
4154 if (netid_width)
4155 netid_width++;
4156 else if (state_width)
4157 state_width++;
4158 }
4159
4160 addrp_width /= 2;
4161 addrp_width--;
4162
4163 serv_width = resolve_services ? 7 : 5;
4164
4165 if (addrp_width < 15+serv_width+1)
4166 addrp_width = 15+serv_width+1;
4167
ae665a52 4168 addr_width = addrp_width - serv_width - 1;
aba5acdf 4169
7a4559f6
DA
4170 if (show_header) {
4171 if (netid_width)
4172 printf("%-*s ", netid_width, "Netid");
4173 if (state_width)
4174 printf("%-*s ", state_width, "State");
4175 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
4176 }
aba5acdf 4177
d68e00f7 4178 /* Make enough space for the local/remote port field */
4179 addr_width -= 13;
4180 serv_width += 13;
2dc85485 4181
7a4559f6
DA
4182 if (show_header) {
4183 printf("%*s:%-*s %*s:%-*s\n",
4184 addr_width, "Local Address", serv_width, "Port",
4185 addr_width, "Peer Address", serv_width, "Port");
4186 }
aba5acdf 4187
aba5acdf
SH
4188 fflush(stdout);
4189
6885e3bf
CG
4190 if (follow_events)
4191 exit(handle_follow_request(&current_filter));
4192
aba5acdf
SH
4193 if (current_filter.dbs & (1<<NETLINK_DB))
4194 netlink_show(&current_filter);
4195 if (current_filter.dbs & PACKET_DBM)
4196 packet_show(&current_filter);
4197 if (current_filter.dbs & UNIX_DBM)
4198 unix_show(&current_filter);
4199 if (current_filter.dbs & (1<<RAW_DB))
4200 raw_show(&current_filter);
4201 if (current_filter.dbs & (1<<UDP_DB))
4202 udp_show(&current_filter);
4203 if (current_filter.dbs & (1<<TCP_DB))
3fe5b534 4204 tcp_show(&current_filter, IPPROTO_TCP);
351efcde 4205 if (current_filter.dbs & (1<<DCCP_DB))
3fe5b534 4206 tcp_show(&current_filter, IPPROTO_DCCP);
116ac927
RH
4207
4208 if (show_users || show_proc_ctx || show_sock_ctx)
4209 user_ent_destroy();
4210
aba5acdf
SH
4211 return 0;
4212}