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