blob: a9ae85ec564e9b4a1371d650eda2cbdd7cb0f460 [file] [log] [blame]
osdl.net!shemmingerb9de3ec2005-01-19 00:23:53 +00001/*
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002 * 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>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025#include <dirent.h>
26#include <fnmatch.h>
osdl.org!shemmingerab611592004-06-09 21:30:13 +000027#include <getopt.h>
Vadim Kochanbf4ceee2015-01-04 22:18:38 +020028#include <stdbool.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
30#include "utils.h"
31#include "rt_names.h"
32#include "ll_map.h"
33#include "libnetlink.h"
Vadim Kochan95ce04b2015-02-08 08:58:43 +020034#include "namespace.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000035#include "SNAPSHOT.h"
36
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -070037#include <linux/tcp.h>
Stephen Hemmingerf6062362012-01-20 12:48:00 -080038#include <linux/sock_diag.h>
shemminger351efcd2005-09-01 19:21:50 +000039#include <linux/inet_diag.h>
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +000040#include <linux/unix_diag.h>
Nicolas Dichtel372c30d2013-05-17 08:42:34 -070041#include <linux/netdevice.h> /* for MAX_ADDR_LEN */
42#include <linux/filter.h>
43#include <linux/packet_diag.h>
Andrey Vaginecb928c2013-06-05 12:42:01 +040044#include <linux/netlink_diag.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045
vadimk8a4025f2014-12-04 12:32:58 +020046#define MAGIC_SEQ 123456
47
vadimk5fb421d2014-10-30 18:49:25 +020048#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,\
vadimk8a4025f2014-12-04 12:32:58 +020056 .nlmsg_seq = MAGIC_SEQ, \
vadimk5fb421d2014-10-30 18:49:25 +020057 .nlmsg_len = sizeof(_req), \
58 }, \
59 }
60
Richard Haines116ac922014-03-07 10:36:52 +000061#if HAVE_SELINUX
62#include <selinux/selinux.h>
63#else
64/* Stubs for SELinux functions */
65static int is_selinux_enabled(void)
66{
67 return -1;
68}
69
70static int getpidcon(pid_t pid, char **context)
71{
72 *context = NULL;
73 return -1;
74}
75
76static int getfilecon(char *path, char **context)
77{
78 *context = NULL;
79 return -1;
80}
81
82static int security_get_initial_context(char *name, char **context)
83{
84 *context = NULL;
85 return -1;
86}
87#endif
88
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000089int resolve_hosts = 0;
90int resolve_services = 1;
91int preferred_family = AF_UNSPEC;
92int show_options = 0;
93int show_details = 0;
94int show_users = 0;
95int show_mem = 0;
96int show_tcpinfo = 0;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -070097int show_bpf = 0;
Richard Haines116ac922014-03-07 10:36:52 +000098int show_proc_ctx = 0;
99int show_sock_ctx = 0;
100/* If show_users & show_proc_ctx only do user_ent_hash_build() once */
101int user_ent_hash_build_init = 0;
Craig Gallek6885e3b2015-06-17 11:14:48 -0400102int follow_events = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000103
104int netid_width;
105int state_width;
106int addrp_width;
107int addr_width;
108int serv_width;
109int screen_width;
110
111static const char *TCP_PROTO = "tcp";
112static const char *UDP_PROTO = "udp";
113static const char *RAW_PROTO = "raw";
114static const char *dg_proto = NULL;
115
116enum
117{
118 TCP_DB,
shemminger351efcd2005-09-01 19:21:50 +0000119 DCCP_DB,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000120 UDP_DB,
121 RAW_DB,
122 UNIX_DG_DB,
123 UNIX_ST_DB,
Masatake YAMATO30b669d2014-01-08 20:13:46 +0900124 UNIX_SQ_DB,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000125 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))
Masatake YAMATO30b669d2014-01-08 20:13:46 +0900132#define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000133#define ALL_DB ((1<<MAX_DB)-1)
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200134#define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000135
136enum {
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000137 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
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150};
151
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200152#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)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000154
155#include "ssfilter.h"
156
157struct filter
158{
159 int dbs;
160 int states;
161 int families;
162 struct ssfilter *f;
163};
164
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200165static const struct filter default_dbs[MAX_DB] = {
166 [TCP_DB] = {
167 .states = SS_CONN,
168 .families = (1 << AF_INET) | (1 << AF_INET6),
169 },
170 [DCCP_DB] = {
171 .states = SS_CONN,
172 .families = (1 << AF_INET) | (1 << AF_INET6),
173 },
174 [UDP_DB] = {
Vadim Kochanf42a4572015-01-08 19:32:22 +0200175 .states = (1 << SS_ESTABLISHED),
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200176 .families = (1 << AF_INET) | (1 << AF_INET6),
177 },
178 [RAW_DB] = {
Vadim Kochanf42a4572015-01-08 19:32:22 +0200179 .states = (1 << SS_ESTABLISHED),
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200180 .families = (1 << AF_INET) | (1 << AF_INET6),
181 },
182 [UNIX_DG_DB] = {
183 .states = (1 << SS_CLOSE),
184 .families = (1 << AF_UNIX),
185 },
186 [UNIX_ST_DB] = {
187 .states = SS_CONN,
188 .families = (1 << AF_UNIX),
189 },
190 [UNIX_SQ_DB] = {
191 .states = SS_CONN,
192 .families = (1 << AF_UNIX),
193 },
194 [PACKET_DG_DB] = {
195 .states = (1 << SS_CLOSE),
196 .families = (1 << AF_PACKET),
197 },
198 [PACKET_R_DB] = {
199 .states = (1 << SS_CLOSE),
200 .families = (1 << AF_PACKET),
201 },
202 [NETLINK_DB] = {
203 .states = (1 << SS_CLOSE),
204 .families = (1 << AF_NETLINK),
205 },
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000206};
207
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200208static const struct filter default_afs[AF_MAX] = {
209 [AF_INET] = {
210 .dbs = INET_DBM,
211 .states = SS_CONN,
212 },
213 [AF_INET6] = {
214 .dbs = INET_DBM,
215 .states = SS_CONN,
216 },
217 [AF_UNIX] = {
218 .dbs = UNIX_DBM,
219 .states = SS_CONN,
220 },
221 [AF_PACKET] = {
222 .dbs = PACKET_DBM,
223 .states = (1 << SS_CLOSE),
224 },
225 [AF_NETLINK] = {
226 .dbs = (1 << NETLINK_DB),
227 .states = (1 << SS_CLOSE),
228 },
229};
230
231static int do_default = 1;
232static struct filter current_filter;
233
234static void filter_db_set(struct filter *f, int db)
235{
236 f->states |= default_dbs[db].states;
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200237 f->dbs |= 1 << db;
238 do_default = 0;
239}
240
241static void filter_af_set(struct filter *f, int af)
242{
Vadim Kochan1527a172015-02-13 13:01:08 +0200243 f->states |= default_afs[af].states;
244 f->families |= 1 << af;
245 do_default = 0;
246 preferred_family = af;
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200247}
248
249static int filter_af_get(struct filter *f, int af)
250{
251 return f->families & (1 << af);
252}
253
254static void filter_default_dbs(struct filter *f)
255{
256 filter_db_set(f, UDP_DB);
257 filter_db_set(f, DCCP_DB);
258 filter_db_set(f, TCP_DB);
259 filter_db_set(f, RAW_DB);
260 filter_db_set(f, UNIX_ST_DB);
261 filter_db_set(f, UNIX_DG_DB);
262 filter_db_set(f, UNIX_SQ_DB);
263 filter_db_set(f, PACKET_R_DB);
264 filter_db_set(f, PACKET_DG_DB);
265 filter_db_set(f, NETLINK_DB);
266}
267
Vadim Kochan57ff5a12015-04-30 07:30:24 +0300268static void filter_states_set(struct filter *f, int states)
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200269{
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200270 if (states)
Vadim Kochan57ff5a12015-04-30 07:30:24 +0300271 f->states = (f->states | states) & states;
272}
273
274static void filter_merge_defaults(struct filter *f)
275{
276 int db;
277 int af;
278
279 for (db = 0; db < MAX_DB; db++) {
280 if (!(f->dbs & (1 << db)))
281 continue;
282
283 if (!(default_dbs[db].families & f->families))
284 f->families |= default_dbs[db].families;
285 }
286 for (af = 0; af < AF_MAX; af++) {
287 if (!(f->families & (1 << af)))
288 continue;
289
290 if (!(default_afs[af].dbs & f->dbs))
291 f->dbs |= default_afs[af].dbs;
292 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +0200293}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100295static FILE *generic_proc_open(const char *env, const char *name)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100297 const char *p = getenv(env);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 char store[128];
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100299
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 if (!p) {
301 p = getenv("PROC_ROOT") ? : "/proc";
302 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
303 p = store;
304 }
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100305
306 return fopen(p, "r");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000307}
308
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100309static FILE *net_tcp_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000310{
311 return generic_proc_open("PROC_NET_TCP", "net/tcp");
312}
313
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100314static FILE *net_tcp6_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315{
316 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
317}
318
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100319static FILE *net_udp_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000320{
321 return generic_proc_open("PROC_NET_UDP", "net/udp");
322}
323
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100324static FILE *net_udp6_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000325{
326 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
327}
328
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100329static FILE *net_raw_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000330{
331 return generic_proc_open("PROC_NET_RAW", "net/raw");
332}
333
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100334static FILE *net_raw6_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000335{
336 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
337}
338
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100339static FILE *net_unix_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000340{
341 return generic_proc_open("PROC_NET_UNIX", "net/unix");
342}
343
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100344static FILE *net_packet_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000345{
346 return generic_proc_open("PROC_NET_PACKET", "net/packet");
347}
348
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100349static FILE *net_netlink_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000350{
351 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
352}
353
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100354static FILE *slabinfo_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000355{
356 return generic_proc_open("PROC_SLABINFO", "slabinfo");
357}
358
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100359static FILE *net_sockstat_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000360{
361 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
362}
363
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100364static FILE *net_sockstat6_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000365{
366 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
367}
368
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100369static FILE *net_snmp_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000370{
371 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
372}
373
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100374static FILE *ephemeral_ports_open(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000375{
376 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
377}
378
Steve Finkfbc0f872010-06-09 11:42:38 -0700379struct user_ent {
380 struct user_ent *next;
381 unsigned int ino;
382 int pid;
383 int fd;
Richard Haines116ac922014-03-07 10:36:52 +0000384 char *process;
385 char *process_ctx;
386 char *socket_ctx;
Steve Finkfbc0f872010-06-09 11:42:38 -0700387};
388
389#define USER_ENT_HASH_SIZE 256
390struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
391
392static int user_ent_hashfn(unsigned int ino)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000393{
Steve Finkfbc0f872010-06-09 11:42:38 -0700394 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
395
396 return val & (USER_ENT_HASH_SIZE - 1);
397}
398
Richard Haines116ac922014-03-07 10:36:52 +0000399static void user_ent_add(unsigned int ino, char *process,
400 int pid, int fd,
401 char *proc_ctx,
402 char *sock_ctx)
Steve Finkfbc0f872010-06-09 11:42:38 -0700403{
404 struct user_ent *p, **pp;
Steve Finkfbc0f872010-06-09 11:42:38 -0700405
Richard Haines116ac922014-03-07 10:36:52 +0000406 p = malloc(sizeof(struct user_ent));
407 if (!p) {
408 fprintf(stderr, "ss: failed to malloc buffer\n");
Steve Finkfbc0f872010-06-09 11:42:38 -0700409 abort();
Richard Haines116ac922014-03-07 10:36:52 +0000410 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700411 p->next = NULL;
412 p->ino = ino;
413 p->pid = pid;
414 p->fd = fd;
Richard Haines116ac922014-03-07 10:36:52 +0000415 p->process = strdup(process);
416 p->process_ctx = strdup(proc_ctx);
417 p->socket_ctx = strdup(sock_ctx);
Steve Finkfbc0f872010-06-09 11:42:38 -0700418
419 pp = &user_ent_hash[user_ent_hashfn(ino)];
420 p->next = *pp;
421 *pp = p;
422}
423
Richard Haines116ac922014-03-07 10:36:52 +0000424static void user_ent_destroy(void)
425{
426 struct user_ent *p, *p_next;
427 int cnt = 0;
428
429 while (cnt != USER_ENT_HASH_SIZE) {
430 p = user_ent_hash[cnt];
431 while (p) {
432 free(p->process);
433 free(p->process_ctx);
434 free(p->socket_ctx);
435 p_next = p->next;
436 free(p);
437 p = p_next;
438 }
439 cnt++;
440 }
441}
442
Steve Finkfbc0f872010-06-09 11:42:38 -0700443static void user_ent_hash_build(void)
444{
445 const char *root = getenv("PROC_ROOT") ? : "/proc/";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000446 struct dirent *d;
Steve Finkfbc0f872010-06-09 11:42:38 -0700447 char name[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000448 int nameoff;
Steve Finkfbc0f872010-06-09 11:42:38 -0700449 DIR *dir;
Richard Haines116ac922014-03-07 10:36:52 +0000450 char *pid_context;
451 char *sock_context;
452 const char *no_ctx = "unavailable";
453
454 /* If show_users & show_proc_ctx set only do this once */
455 if (user_ent_hash_build_init != 0)
456 return;
457
458 user_ent_hash_build_init = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000459
willy tarreau0ee90522015-10-06 12:09:33 +0200460 strncpy(name, root, sizeof(name)-1);
461 name[sizeof(name)-1] = 0;
462
Steve Finkfbc0f872010-06-09 11:42:38 -0700463 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000464 strcat(name, "/");
Steve Finkfbc0f872010-06-09 11:42:38 -0700465
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000466 nameoff = strlen(name);
Steve Finkfbc0f872010-06-09 11:42:38 -0700467
468 dir = opendir(name);
469 if (!dir)
470 return;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000471
472 while ((d = readdir(dir)) != NULL) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000473 struct dirent *d1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000474 char process[16];
Richard Haines116ac922014-03-07 10:36:52 +0000475 char *p;
Steve Finkfbc0f872010-06-09 11:42:38 -0700476 int pid, pos;
477 DIR *dir1;
478 char crap;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000479
480 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
481 continue;
482
Richard Haines116ac922014-03-07 10:36:52 +0000483 if (getpidcon(pid, &pid_context) != 0)
484 pid_context = strdup(no_ctx);
485
willy tarreau0ee90522015-10-06 12:09:33 +0200486 snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000487 pos = strlen(name);
Phil Suttera02371f2015-08-06 14:24:36 +0200488 if ((dir1 = opendir(name)) == NULL) {
489 free(pid_context);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000490 continue;
Phil Suttera02371f2015-08-06 14:24:36 +0200491 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000492
Steve Finkfbc0f872010-06-09 11:42:38 -0700493 process[0] = '\0';
Richard Haines116ac922014-03-07 10:36:52 +0000494 p = process;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000495
496 while ((d1 = readdir(dir1)) != NULL) {
Steve Finkfbc0f872010-06-09 11:42:38 -0700497 const char *pattern = "socket:[";
498 unsigned int ino;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000499 char lnk[64];
Stephen Hemminger18445b32011-06-29 15:58:37 -0700500 int fd;
Thomas Jarosch788731b2011-10-13 10:30:21 +0200501 ssize_t link_len;
Richard Haines116ac922014-03-07 10:36:52 +0000502 char tmp[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000503
504 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
505 continue;
506
willy tarreau0ee90522015-10-06 12:09:33 +0200507 snprintf(name+pos, sizeof(name) - pos, "%d", fd);
Thomas Jarosch788731b2011-10-13 10:30:21 +0200508
509 link_len = readlink(name, lnk, sizeof(lnk)-1);
510 if (link_len == -1)
511 continue;
512 lnk[link_len] = '\0';
513
514 if (strncmp(lnk, pattern, strlen(pattern)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000515 continue;
516
Steve Finkfbc0f872010-06-09 11:42:38 -0700517 sscanf(lnk, "socket:[%u]", &ino);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000518
Richard Haines116ac922014-03-07 10:36:52 +0000519 snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
520 root, pid, d1->d_name);
521
522 if (getfilecon(tmp, &sock_context) <= 0)
523 sock_context = strdup(no_ctx);
524
525 if (*p == '\0') {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000526 FILE *fp;
Steve Finkfbc0f872010-06-09 11:42:38 -0700527
Richard Haines116ac922014-03-07 10:36:52 +0000528 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
529 root, pid);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000530 if ((fp = fopen(tmp, "r")) != NULL) {
Richard Haines116ac922014-03-07 10:36:52 +0000531 fscanf(fp, "%*d (%[^)])", p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000532 fclose(fp);
533 }
534 }
Richard Haines116ac922014-03-07 10:36:52 +0000535 user_ent_add(ino, p, pid, fd,
536 pid_context, sock_context);
537 free(sock_context);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000538 }
Richard Haines116ac922014-03-07 10:36:52 +0000539 free(pid_context);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000540 closedir(dir1);
541 }
542 closedir(dir);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000543}
544
Richard Haines116ac922014-03-07 10:36:52 +0000545enum entry_types {
546 USERS,
547 PROC_CTX,
548 PROC_SOCK_CTX
549};
550
551#define ENTRY_BUF_SIZE 512
552static int find_entry(unsigned ino, char **buf, int type)
Steve Finkfbc0f872010-06-09 11:42:38 -0700553{
554 struct user_ent *p;
555 int cnt = 0;
556 char *ptr;
Phil Sutter532ca402015-08-06 14:24:33 +0200557 char *new_buf;
Richard Haines116ac922014-03-07 10:36:52 +0000558 int len, new_buf_len;
559 int buf_used = 0;
560 int buf_len = 0;
Steve Finkfbc0f872010-06-09 11:42:38 -0700561
562 if (!ino)
563 return 0;
564
565 p = user_ent_hash[user_ent_hashfn(ino)];
Richard Haines116ac922014-03-07 10:36:52 +0000566 ptr = *buf = NULL;
Steve Finkfbc0f872010-06-09 11:42:38 -0700567 while (p) {
568 if (p->ino != ino)
569 goto next;
570
Richard Haines116ac922014-03-07 10:36:52 +0000571 while (1) {
572 ptr = *buf + buf_used;
573 switch (type) {
574 case USERS:
575 len = snprintf(ptr, buf_len - buf_used,
576 "(\"%s\",pid=%d,fd=%d),",
577 p->process, p->pid, p->fd);
578 break;
579 case PROC_CTX:
580 len = snprintf(ptr, buf_len - buf_used,
581 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
582 p->process, p->pid,
583 p->process_ctx, p->fd);
584 break;
585 case PROC_SOCK_CTX:
586 len = snprintf(ptr, buf_len - buf_used,
587 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
588 p->process, p->pid,
589 p->process_ctx, p->fd,
590 p->socket_ctx);
591 break;
592 default:
593 fprintf(stderr, "ss: invalid type: %d\n", type);
594 abort();
595 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700596
Richard Haines116ac922014-03-07 10:36:52 +0000597 if (len < 0 || len >= buf_len - buf_used) {
598 new_buf_len = buf_len + ENTRY_BUF_SIZE;
Phil Sutter532ca402015-08-06 14:24:33 +0200599 new_buf = realloc(*buf, new_buf_len);
Richard Haines116ac922014-03-07 10:36:52 +0000600 if (!new_buf) {
601 fprintf(stderr, "ss: failed to malloc buffer\n");
602 abort();
603 }
Phil Sutter532ca402015-08-06 14:24:33 +0200604 *buf = new_buf;
Richard Haines116ac922014-03-07 10:36:52 +0000605 buf_len = new_buf_len;
606 continue;
607 } else {
608 buf_used += len;
609 break;
610 }
611 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700612 cnt++;
Richard Haines116ac922014-03-07 10:36:52 +0000613next:
Steve Finkfbc0f872010-06-09 11:42:38 -0700614 p = p->next;
615 }
Richard Haines116ac922014-03-07 10:36:52 +0000616 if (buf_used) {
617 ptr = *buf + buf_used;
Steve Finkfbc0f872010-06-09 11:42:38 -0700618 ptr[-1] = '\0';
Richard Haines116ac922014-03-07 10:36:52 +0000619 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700620 return cnt;
621}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000622
623/* Get stats from slab */
624
625struct slabstat
626{
627 int socks;
628 int tcp_ports;
629 int tcp_tws;
630 int tcp_syns;
631 int skbs;
632};
633
Bryton Leea221d622015-02-12 14:16:04 +0800634static struct slabstat slabstat;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000635
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800636static const char *slabstat_ids[] =
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000637{
638 "sock",
639 "tcp_bind_bucket",
640 "tcp_tw_bucket",
641 "tcp_open_request",
642 "skbuff_head_cache",
643};
644
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800645static int get_slabstat(struct slabstat *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000646{
647 char buf[256];
648 FILE *fp;
649 int cnt;
Bryton Leea221d622015-02-12 14:16:04 +0800650 static int slabstat_valid;
651
652 if (slabstat_valid)
653 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000654
655 memset(s, 0, sizeof(*s));
656
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100657 fp = slabinfo_open();
658 if (!fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000659 return -1;
660
661 cnt = sizeof(*s)/sizeof(int);
662
663 fgets(buf, sizeof(buf), fp);
664 while(fgets(buf, sizeof(buf), fp) != NULL) {
665 int i;
666 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
667 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
668 sscanf(buf, "%*s%d", ((int *)s) + i);
669 cnt--;
670 break;
671 }
672 }
673 if (cnt <= 0)
674 break;
675 }
676
Bryton Leea221d622015-02-12 14:16:04 +0800677 slabstat_valid = 1;
678
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000679 fclose(fp);
680 return 0;
681}
682
Eric Dumazet2e7e8052015-03-10 09:56:47 -0700683static unsigned long long cookie_sk_get(const uint32_t *cookie)
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200684{
Eric Dumazet2e7e8052015-03-10 09:56:47 -0700685 return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200686}
687
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000688static const char *sstate_name[] = {
689 "UNKNOWN",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700690 [SS_ESTABLISHED] = "ESTAB",
691 [SS_SYN_SENT] = "SYN-SENT",
692 [SS_SYN_RECV] = "SYN-RECV",
693 [SS_FIN_WAIT1] = "FIN-WAIT-1",
694 [SS_FIN_WAIT2] = "FIN-WAIT-2",
695 [SS_TIME_WAIT] = "TIME-WAIT",
696 [SS_CLOSE] = "UNCONN",
697 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
698 [SS_LAST_ACK] = "LAST-ACK",
699 [SS_LISTEN] = "LISTEN",
700 [SS_CLOSING] = "CLOSING",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000701};
702
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000703static const char *sstate_namel[] = {
704 "UNKNOWN",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700705 [SS_ESTABLISHED] = "established",
706 [SS_SYN_SENT] = "syn-sent",
707 [SS_SYN_RECV] = "syn-recv",
708 [SS_FIN_WAIT1] = "fin-wait-1",
709 [SS_FIN_WAIT2] = "fin-wait-2",
710 [SS_TIME_WAIT] = "time-wait",
711 [SS_CLOSE] = "unconnected",
712 [SS_CLOSE_WAIT] = "close-wait",
713 [SS_LAST_ACK] = "last-ack",
714 [SS_LISTEN] = "listening",
715 [SS_CLOSING] = "closing",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000716};
717
Vadim Kochan055840f2015-02-13 22:13:58 +0200718struct sockstat
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000719{
Vadim Kochanec4d0d82015-02-13 22:14:00 +0200720 struct sockstat *next;
721 unsigned int type;
Vadim Kochan89f634f2015-02-13 22:13:59 +0200722 uint16_t prot;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200723 inet_prefix local;
724 inet_prefix remote;
725 int lport;
726 int rport;
727 int state;
728 int rq, wq;
729 unsigned ino;
730 unsigned uid;
731 int refcnt;
732 unsigned int iface;
733 unsigned long long sk;
Vadim Kochan99bb68f2015-07-21 10:53:19 -0700734 char *name;
735 char *peer_name;
Vadim Kochan055840f2015-02-13 22:13:58 +0200736};
737
738struct dctcpstat
739{
740 unsigned int ce_state;
741 unsigned int alpha;
742 unsigned int ab_ecn;
743 unsigned int ab_tot;
744 bool enabled;
745};
746
747struct tcpstat
748{
749 struct sockstat ss;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200750 int timer;
751 int timeout;
752 int probes;
Eric Dumazetd2055ea2015-05-29 04:04:05 -0700753 char cong_alg[16];
Vadim Kochan8250bc92015-01-20 16:14:24 +0200754 double rto, ato, rtt, rttvar;
755 int qack, cwnd, ssthresh, backoff;
756 double send_bps;
757 int snd_wscale;
758 int rcv_wscale;
759 int mss;
760 unsigned int lastsnd;
761 unsigned int lastrcv;
762 unsigned int lastack;
763 double pacing_rate;
764 double pacing_rate_max;
Eric Dumazet1a4dda72015-05-11 10:03:49 -0700765 unsigned long long bytes_acked;
766 unsigned long long bytes_received;
Craig Gallekecb435e2015-05-26 14:54:41 -0400767 unsigned int segs_out;
768 unsigned int segs_in;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200769 unsigned int unacked;
770 unsigned int retrans;
771 unsigned int retrans_total;
772 unsigned int lost;
773 unsigned int sacked;
774 unsigned int fackets;
775 unsigned int reordering;
776 double rcv_rtt;
777 int rcv_space;
778 bool has_ts_opt;
779 bool has_sack_opt;
780 bool has_ecn_opt;
781 bool has_ecnseen_opt;
782 bool has_fastopen_opt;
783 bool has_wscale_opt;
784 struct dctcpstat *dctcp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000785};
786
Vadim Kochan2d791bc2015-02-13 22:14:01 +0200787static void sock_state_print(struct sockstat *s, const char *sock_name)
788{
789 if (netid_width)
790 printf("%-*s ", netid_width, sock_name);
791 if (state_width)
792 printf("%-*s ", state_width, sstate_name[s->state]);
793
794 printf("%-6d %-6d ", s->rq, s->wq);
795}
796
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200797static void sock_details_print(struct sockstat *s)
798{
799 if (s->uid)
800 printf(" uid:%u", s->uid);
801
802 printf(" ino:%u", s->ino);
803 printf(" sk:%llx", s->sk);
804}
805
Vadim Kochanb217df12015-02-13 22:14:03 +0200806static void sock_addr_print_width(int addr_len, const char *addr, char *delim,
807 int port_len, const char *port, const char *ifname)
808{
809 if (ifname) {
810 printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim,
811 port_len, port);
812 }
813 else {
814 printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port);
815 }
816}
817
818static void sock_addr_print(const char *addr, char *delim, const char *port,
819 const char *ifname)
820{
821 sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname);
822}
823
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000824static const char *tmr_name[] = {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000825 "off",
826 "on",
827 "keepalive",
828 "timewait",
829 "persist",
830 "unknown"
831};
832
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800833static const char *print_ms_timer(int timeout)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000834{
835 static char buf[64];
836 int secs, msecs, minutes;
837 if (timeout < 0)
838 timeout = 0;
839 secs = timeout/1000;
840 minutes = secs/60;
841 secs = secs%60;
842 msecs = timeout%1000;
843 buf[0] = 0;
844 if (minutes) {
845 msecs = 0;
846 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
847 if (minutes > 9)
848 secs = 0;
849 }
850 if (secs) {
851 if (secs > 9)
852 msecs = 0;
853 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
854 }
855 if (msecs)
856 sprintf(buf+strlen(buf), "%03dms", msecs);
857 return buf;
Stephen Hemmingere7113c62007-07-10 18:26:54 -0700858}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000859
Eric Dumazet22588a02015-05-29 04:45:48 -0700860struct scache {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000861 struct scache *next;
862 int port;
863 char *name;
864 const char *proto;
865};
866
867struct scache *rlist;
868
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800869static void init_service_resolver(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000870{
871 char buf[128];
872 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
873 if (fp) {
874 fgets(buf, sizeof(buf), fp);
875 while (fgets(buf, sizeof(buf), fp) != NULL) {
876 unsigned int progn, port;
877 char proto[128], prog[128];
878 if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
879 &port, prog+4) == 4) {
880 struct scache *c = malloc(sizeof(*c));
881 if (c) {
882 c->port = port;
883 memcpy(prog, "rpc.", 4);
884 c->name = strdup(prog);
885 if (strcmp(proto, TCP_PROTO) == 0)
886 c->proto = TCP_PROTO;
887 else if (strcmp(proto, UDP_PROTO) == 0)
888 c->proto = UDP_PROTO;
889 else
890 c->proto = NULL;
891 c->next = rlist;
892 rlist = c;
893 }
894 }
895 }
Thomas Jarosch2bcc3c12011-10-03 05:22:27 +0000896 pclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000897 }
898}
899
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000900static int ip_local_port_min, ip_local_port_max;
901
902/* Even do not try default linux ephemeral port ranges:
903 * default /etc/services contains so much of useless crap
904 * wouldbe "allocated" to this area that resolution
905 * is really harmful. I shrug each time when seeing
906 * "socks" or "cfinger" in dumps.
907 */
908static int is_ephemeral(int port)
909{
910 if (!ip_local_port_min) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +0100911 FILE *f = ephemeral_ports_open();
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000912 if (f) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800913 fscanf(f, "%d %d",
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000914 &ip_local_port_min, &ip_local_port_max);
915 fclose(f);
916 } else {
917 ip_local_port_min = 1024;
918 ip_local_port_max = 4999;
919 }
920 }
921
922 return (port >= ip_local_port_min && port<= ip_local_port_max);
923}
924
925
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800926static const char *__resolve_service(int port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000927{
928 struct scache *c;
929
930 for (c = rlist; c; c = c->next) {
931 if (c->port == port && c->proto == dg_proto)
932 return c->name;
933 }
934
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000935 if (!is_ephemeral(port)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000936 static int notfirst;
937 struct servent *se;
938 if (!notfirst) {
939 setservent(1);
940 notfirst = 1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800941 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000942 se = getservbyport(htons(port), dg_proto);
943 if (se)
944 return se->s_name;
945 }
946
947 return NULL;
948}
949
Eric Dumazet22588a02015-05-29 04:45:48 -0700950#define SCACHE_BUCKETS 1024
951static struct scache *cache_htab[SCACHE_BUCKETS];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000952
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800953static const char *resolve_service(int port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000954{
955 static char buf[128];
Eric Dumazet22588a02015-05-29 04:45:48 -0700956 struct scache *c;
957 const char *res;
958 int hash;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000959
960 if (port == 0) {
961 buf[0] = '*';
962 buf[1] = 0;
963 return buf;
964 }
965
Eric Dumazet22588a02015-05-29 04:45:48 -0700966 if (!resolve_services)
967 goto do_numeric;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000968
Eric Dumazet22588a02015-05-29 04:45:48 -0700969 if (dg_proto == RAW_PROTO)
970 return inet_proto_n2a(port, buf, sizeof(buf));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000971
Eric Dumazet22588a02015-05-29 04:45:48 -0700972
973 hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS;
974
975 for (c = cache_htab[hash]; c; c = c->next) {
976 if (c->port == port && c->proto == dg_proto)
977 goto do_cache;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000978 }
979
Eric Dumazet22588a02015-05-29 04:45:48 -0700980 c = malloc(sizeof(*c));
981 if (!c)
982 goto do_numeric;
983 res = __resolve_service(port);
984 c->port = port;
985 c->name = res ? strdup(res) : NULL;
986 c->proto = dg_proto;
987 c->next = cache_htab[hash];
988 cache_htab[hash] = c;
989
990do_cache:
991 if (c->name)
992 return c->name;
993
994do_numeric:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000995 sprintf(buf, "%u", port);
996 return buf;
997}
998
Vadim Kochanb217df12015-02-13 22:14:03 +0200999static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001000{
1001 char buf[1024];
1002 const char *ap = buf;
Vadim Kochanb217df12015-02-13 22:14:03 +02001003 int est_len = addr_width;
1004 const char *ifname = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001005
1006 if (a->family == AF_INET) {
1007 if (a->data[0] == 0) {
1008 buf[0] = '*';
1009 buf[1] = 0;
1010 } else {
1011 ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
1012 }
1013 } else {
1014 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
1015 est_len = strlen(ap);
1016 if (est_len <= addr_width)
1017 est_len = addr_width;
1018 else
1019 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
1020 }
FX Le Bail7c8a3cf2014-02-11 10:06:23 +01001021
Vadim Kochanb217df12015-02-13 22:14:03 +02001022 if (ifindex) {
1023 ifname = ll_index_to_name(ifindex);
1024 est_len -= strlen(ifname) + 1; /* +1 for percent char */
Mike Saal4fcfb6b2015-08-26 11:59:17 -04001025 if (est_len < 0)
1026 est_len = 0;
Vadim Kochanb217df12015-02-13 22:14:03 +02001027 }
1028
1029 sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port),
1030 ifname);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001031}
1032
1033struct aafilter
1034{
1035 inet_prefix addr;
1036 int port;
1037 struct aafilter *next;
1038};
1039
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08001040static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
1041 int plen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001042{
1043 if (!inet_addr_match(a, p, plen))
1044 return 0;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001045
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001046 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1047 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1048 * sockets. Fair? */
1049 if (p->family == AF_INET && a->family == AF_INET6) {
1050 if (a->data[0] == 0 && a->data[1] == 0 &&
1051 a->data[2] == htonl(0xffff)) {
1052 inet_prefix tmp = *a;
1053 tmp.data[0] = a->data[3];
1054 return inet_addr_match(&tmp, p, plen);
1055 }
1056 }
1057 return 1;
1058}
1059
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08001060static int unix_match(const inet_prefix *a, const inet_prefix *p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001061{
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001062 char *addr, *pattern;
1063 memcpy(&addr, a->data, sizeof(addr));
1064 memcpy(&pattern, p->data, sizeof(pattern));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001065 if (pattern == NULL)
1066 return 1;
1067 if (addr == NULL)
1068 addr = "";
1069 return !fnmatch(pattern, addr, 0);
1070}
1071
Vadim Kochan055840f2015-02-13 22:13:58 +02001072static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001073{
1074 switch (f->type) {
1075 case SSF_S_AUTO:
1076 {
1077 static int low, high=65535;
1078
1079 if (s->local.family == AF_UNIX) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001080 char *p;
1081 memcpy(&p, s->local.data, sizeof(p));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001082 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001083 strspn(p+1, "0123456789abcdef") == 5);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001084 }
1085 if (s->local.family == AF_PACKET)
Maciej Żenczykowskibbd303d2015-06-25 02:03:03 -07001086 return s->lport == 0 && s->local.data[0] == 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001087 if (s->local.family == AF_NETLINK)
1088 return s->lport < 0;
1089
1090 if (!low) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001091 FILE *fp = ephemeral_ports_open();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001092 if (fp) {
1093 fscanf(fp, "%d%d", &low, &high);
1094 fclose(fp);
1095 }
1096 }
1097 return s->lport >= low && s->lport <= high;
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;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001126 } while ((a = a->next) != NULL);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001127 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
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001164/* Relocate external jumps by reloc. */
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001165static void ssfilter_patch(char *a, int len, int reloc)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001166{
1167 while (len > 0) {
shemminger351efcd2005-09-01 19:21:50 +00001168 struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001169 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
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001178static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001179{
1180 switch (f->type) {
1181 case SSF_S_AUTO:
1182 {
1183 if (!(*bytecode=malloc(4))) abort();
shemminger351efcd2005-09-01 19:21:50 +00001184 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
Eric Dumazetdf39de82011-06-20 14:31:51 -07001185 return 4;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001186 }
1187 case SSF_DCOND:
1188 case SSF_SCOND:
1189 {
1190 struct aafilter *a = (void*)f->pred;
1191 struct aafilter *b;
1192 char *ptr;
shemminger351efcd2005-09-01 19:21:50 +00001193 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001194 int len = 0;
1195
1196 for (b=a; b; b=b->next) {
shemminger351efcd2005-09-01 19:21:50 +00001197 len += 4 + sizeof(struct inet_diag_hostcond);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001198 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) {
shemminger351efcd2005-09-01 19:21:50 +00001208 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001209 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
shemminger351efcd2005-09-01 19:21:50 +00001210 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
1211 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001212
shemminger351efcd2005-09-01 19:21:50 +00001213 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001214 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) {
shemminger351efcd2005-09-01 19:21:50 +00001220 op = (struct inet_diag_bc_op *)ptr;
1221 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001222 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();
shemminger351efcd2005-09-01 19:21:50 +00001231 ((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 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001233 return 8;
1234 }
1235 case SSF_D_LE:
1236 {
1237 struct aafilter *x = (void*)f->pred;
1238 if (!(*bytecode=malloc(8))) abort();
shemminger351efcd2005-09-01 19:21:50 +00001239 ((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 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001241 return 8;
1242 }
1243 case SSF_S_GE:
1244 {
1245 struct aafilter *x = (void*)f->pred;
1246 if (!(*bytecode=malloc(8))) abort();
shemminger351efcd2005-09-01 19:21:50 +00001247 ((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 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001249 return 8;
1250 }
1251 case SSF_S_LE:
1252 {
1253 struct aafilter *x = (void*)f->pred;
1254 if (!(*bytecode=malloc(8))) abort();
shemminger351efcd2005-09-01 19:21:50 +00001255 ((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 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001257 return 8;
1258 }
1259
1260 case SSF_AND:
1261 {
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001262 char *a1, *a2, *a;
1263 int l1, l2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001264 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 {
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001276 char *a1, *a2, *a;
1277 int l1, l2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001278 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);
shemminger351efcd2005-09-01 19:21:50 +00001284 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001285 *bytecode = a;
1286 return l1+l2+4;
1287 }
1288 case SSF_NOT:
1289 {
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001290 char *a1, *a;
1291 int l1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001292 l1 = ssfilter_bytecompile(f->pred, &a1);
1293 if (!(a = malloc(l1+4))) abort();
1294 memcpy(a, a1, l1);
1295 free(a1);
shemminger351efcd2005-09-01 19:21:50 +00001296 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001297 *bytecode = a;
1298 return l1+4;
1299 }
1300 default:
1301 abort();
1302 }
1303}
1304
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001305static int remember_he(struct aafilter *a, struct hostent *he)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001306{
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001307 char **ptr = he->h_addr_list;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001308 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
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001337static int get_dns_host(struct aafilter *a, const char *addr, int fam)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001338{
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
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001359static int xll_initted = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001360
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001361static void xll_init(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001362{
1363 struct rtnl_handle rth;
Stephen Hemmingerd2468da2013-12-20 08:15:02 -08001364 if (rtnl_open(&rth, 0) < 0)
1365 exit(1);
1366
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001367 ll_init_map(&rth);
1368 rtnl_close(&rth);
1369 xll_initted = 1;
1370}
1371
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001372static const char *xll_index_to_name(int index)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001373{
1374 if (!xll_initted)
1375 xll_init();
1376 return ll_index_to_name(index);
1377}
1378
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001379static int xll_name_to_index(const char *dev)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001380{
1381 if (!xll_initted)
1382 xll_init();
1383 return ll_name_to_index(dev);
1384}
1385
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001386void *parse_hostcond(char *addr, bool is_port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001387{
1388 char *port = NULL;
Vadim Kochan1527a172015-02-13 13:01:08 +02001389 struct aafilter a = { .port = -1 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001390 struct aafilter *res;
Vadim Kochan1527a172015-02-13 13:01:08 +02001391 int fam = preferred_family;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001392 struct filter *f = &current_filter;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001393
Vadim Kochan1527a172015-02-13 13:01:08 +02001394 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001395 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);
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001401 memcpy(a.addr.data, &p, sizeof(p));
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001402 fam = AF_UNIX;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001403 goto out;
1404 }
1405
Vadim Kochan1527a172015-02-13 13:01:08 +02001406 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001407 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 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001428 fam = AF_PACKET;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001429 goto out;
1430 }
1431
Vadim Kochan1527a172015-02-13 13:01:08 +02001432 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001433 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;
vadimkb00daf62014-12-06 02:52:19 +02001451 if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
1452 return NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001453 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001454 fam = AF_NETLINK;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001455 goto out;
1456 }
1457
Vadim Kochan1527a172015-02-13 13:01:08 +02001458 if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001459 fam = AF_INET;
Vadim Kochan1527a172015-02-13 13:01:08 +02001460 if (!strncmp(addr, "inet:", 5))
1461 addr += 5;
1462 } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001463 fam = AF_INET6;
Vadim Kochan1527a172015-02-13 13:01:08 +02001464 if (!strncmp(addr, "inet6:", 6))
1465 addr += 6;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001466 }
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 }
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001479
1480 if (is_port)
1481 port = addr;
1482
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001483 if (port && *port) {
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001484 if (*port == ':')
1485 *port++ = 0;
1486
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001487 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 }
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001527 if (!is_port && addr && *addr && *addr != '*') {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001528 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
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001536out:
Vadim Kochan1527a172015-02-13 13:01:08 +02001537 if (fam != AF_UNSPEC) {
1538 f->families = 0;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001539 filter_af_set(f, fam);
Vadim Kochan57ff5a12015-04-30 07:30:24 +03001540 filter_states_set(f, 0);
Vadim Kochan1527a172015-02-13 13:01:08 +02001541 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001542
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001543 res = malloc(sizeof(*res));
1544 if (res)
1545 memcpy(res, &a, sizeof(a));
1546 return res;
1547}
1548
Vadim Kochan8250bc92015-01-20 16:14:24 +02001549static char *proto_name(int protocol)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001550{
Vadim Kochan8250bc92015-01-20 16:14:24 +02001551 switch (protocol) {
Nikolay Aleksandrov235c4452015-07-15 05:50:42 -07001552 case 0:
1553 return "raw";
Vadim Kochan8250bc92015-01-20 16:14:24 +02001554 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
Vadim Kochan055840f2015-02-13 22:13:58 +02001565static void inet_stats_print(struct sockstat *s, int protocol)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001566{
1567 char *buf = NULL;
1568
Vadim Kochan2d791bc2015-02-13 22:14:01 +02001569 sock_state_print(s, proto_name(protocol));
Vadim Kochan8250bc92015-01-20 16:14:24 +02001570
Vadim Kochanb217df12015-02-13 22:14:03 +02001571 inet_addr_print(&s->local, s->lport, s->iface);
1572 inet_addr_print(&s->remote, s->rport, 0);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001573
Vadim Kochan8250bc92015-01-20 16:14:24 +02001574 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
Vadim Kochan055840f2015-02-13 22:13:58 +02001589static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
1590 sockstat *s)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001591{
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
1617static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
1618{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001619 char *p;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001620
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001621 if ((p = strchr(line, ':')) == NULL)
1622 return -1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001623
Vadim Kochan8250bc92015-01-20 16:14:24 +02001624 *loc = p+2;
1625 if ((p = strchr(*loc, ':')) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001626 return -1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001627
Vadim Kochan8250bc92015-01-20 16:14:24 +02001628 p[5] = 0;
1629 *rem = p+6;
1630 if ((p = strchr(*rem, ':')) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001631 return -1;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001632
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001633 p[5] = 0;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001634 *data = p+6;
1635 return 0;
1636}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001637
Vadim Kochan8250bc92015-01-20 16:14:24 +02001638static 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);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001646
Vadim Kochan8250bc92015-01-20 16:14:24 +02001647 return buf;
1648}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001649
Vadim Kochan8250bc92015-01-20 16:14:24 +02001650static 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");
Eric Dumazetd2055ea2015-05-29 04:04:05 -07001664 if (s->cong_alg[0])
Vadim Kochan8250bc92015-01-20 16:14:24 +02001665 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);
Eric Dumazet3bf54452015-05-08 13:28:40 -07001684 if (s->cwnd)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001685 printf(" cwnd:%d", s->cwnd);
1686 if (s->ssthresh)
1687 printf(" ssthresh:%d", s->ssthresh);
1688
Eric Dumazet1a4dda72015-05-11 10:03:49 -07001689 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);
Craig Gallekecb435e2015-05-26 14:54:41 -04001693 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);
Eric Dumazet1a4dda72015-05-11 10:03:49 -07001697
Vadim Kochan8250bc92015-01-20 16:14:24 +02001698 if (s->dctcp && s->dctcp->enabled) {
1699 struct dctcpstat *dctcp = s->dctcp;
1700
Eric Dumazet3bf54452015-05-08 13:28:40 -07001701 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
Vadim Kochan8250bc92015-01-20 16:14:24 +02001702 dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
1703 dctcp->ab_tot);
1704 } else if (s->dctcp) {
Eric Dumazet3bf54452015-05-08 13:28:40 -07001705 printf(" dctcp:fallback_mode");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001706 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001707
Vadim Kochan8250bc92015-01-20 16:14:24 +02001708 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);
Vadim Kochan055840f2015-02-13 22:13:58 +02001730 if (s->sacked && s->ss.state != SS_LISTEN)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001731 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
Vadim Kochan055840f2015-02-13 22:13:58 +02001742static 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
Vadim Kochan8250bc92015-01-20 16:14:24 +02001754static 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
Vadim Kochan055840f2015-02-13 22:13:58 +02001770 proc_parse_inet_addr(loc, rem, family, &s.ss);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001771
Vadim Kochan055840f2015-02-13 22:13:58 +02001772 if (f->f && run_ssfilter(f->f, &s.ss) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001773 return 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001774
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001775 opt[0] = 0;
Stephen Hemmingere7113c62007-07-10 18:26:54 -07001776 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
Vadim Kochan055840f2015-02-13 22:13:58 +02001777 &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);
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001781
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001782 if (n < 17)
1783 opt[0] = 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001784
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001785 if (n < 12) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001786 rto = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001787 s.cwnd = 2;
1788 s.ssthresh = -1;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001789 ato = s.qack = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001790 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001791
Vadim Kochan8250bc92015-01-20 16:14:24 +02001792 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;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001799
Vadim Kochan055840f2015-02-13 22:13:58 +02001800 inet_stats_print(&s.ss, IPPROTO_TCP);
1801
1802 if (show_options)
1803 tcp_timer_print(&s);
Richard Haines116ac922014-03-07 10:36:52 +00001804
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001805 if (show_details) {
Vadim Kochanf1b39e12015-02-13 22:14:02 +02001806 sock_details_print(&s.ss);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001807 if (opt[0])
1808 printf(" opt:\"%s\"", opt);
1809 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001810
Vadim Kochan8250bc92015-01-20 16:14:24 +02001811 if (show_tcpinfo)
1812 tcp_stats_print(&s);
1813
1814 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001815 return 0;
1816}
1817
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001818static int generic_record_read(FILE *fp,
1819 int (*worker)(char*, const struct filter *, int),
1820 const struct filter *f, int fam)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001821{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001822 char line[256];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001823
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001824 /* skip header */
1825 if (fgets(line, sizeof(line), fp) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001826 goto outerr;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001827
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001828 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;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001833 }
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001834 line[n-1] = 0;
1835
1836 if (worker(line, f, fam) < 0)
1837 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001838 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001839outerr:
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001840
1841 return ferror(fp) ? -1 : 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001842}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001843
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001844static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1845{
1846 const __u32 *skmeminfo;
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001847
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 }
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001862 return;
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001863 }
1864
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001865 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
Vadim Kochan8250bc92015-01-20 16:14:24 +02001883#define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
1884
Pavel Emelyanov5b816042013-05-17 08:02:14 -07001885static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
1886 struct rtattr *tb[])
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001887{
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001888 double rtt = 0;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001889 struct tcpstat s = {};
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001890
Vadim Kochan055840f2015-02-13 22:13:58 +02001891 s.ss.state = r->idiag_state;
1892
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001893 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001894
shemminger351efcd2005-09-01 19:21:50 +00001895 if (tb[INET_DIAG_INFO]) {
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001896 struct tcp_info *info;
shemminger351efcd2005-09-01 19:21:50 +00001897 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001898
1899 /* workaround for older kernels with less fields */
1900 if (len < sizeof(*info)) {
1901 info = alloca(sizeof(*info));
shemminger351efcd2005-09-01 19:21:50 +00001902 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
Eric Dumazet656e8fd2015-05-06 11:33:23 -07001903 memset((char *)info + len, 0, sizeof(*info) - len);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001904 } else
shemminger351efcd2005-09-01 19:21:50 +00001905 info = RTA_DATA(tb[INET_DIAG_INFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001906
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001907 if (show_options) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001908 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);
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001913 }
shemminger52d5ac32005-07-05 22:11:37 +00001914
Eric Dumazetd2055ea2015-05-29 04:04:05 -07001915 if (tb[INET_DIAG_CONG])
1916 strncpy(s.cong_alg,
1917 rta_getattr_str(tb[INET_DIAG_CONG]),
1918 sizeof(s.cong_alg) - 1);
shemmingerea8fc102005-06-22 18:27:49 +00001919
Vadim Kochan8250bc92015-01-20 16:14:24 +02001920 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
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001926 if (info->tcpi_rto && info->tcpi_rto != 3000000)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001927 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;
Vadim Kochan11ba90f2015-02-13 22:14:04 +02001932 s.ato = (double)info->tcpi_ato / 1000;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001933 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
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001948 if (info->tcpi_snd_ssthresh < 0xFFFF)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001949 s.ssthresh = info->tcpi_snd_ssthresh;
shemminger52d5ac32005-07-05 22:11:37 +00001950
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001951 rtt = (double) info->tcpi_rtt;
shemminger351efcd2005-09-01 19:21:50 +00001952 if (tb[INET_DIAG_VEGASINFO]) {
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001953 const struct tcpvegas_info *vinfo
shemminger351efcd2005-09-01 19:21:50 +00001954 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001955
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001956 if (vinfo->tcpv_enabled &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02001957 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
shemmingerea8fc102005-06-22 18:27:49 +00001958 rtt = vinfo->tcpv_rtt;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001959 }
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001960
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02001961 if (tb[INET_DIAG_DCTCPINFO]) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001962 struct dctcpstat *dctcp = malloc(sizeof(struct
1963 dctcpstat));
1964
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02001965 const struct tcp_dctcp_info *dinfo
1966 = RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
1967
Vadim Kochan8250bc92015-01-20 16:14:24 +02001968 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;
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02001974 }
1975
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001976 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001977 s.send_bps = (double) info->tcpi_snd_cwnd *
1978 (double)info->tcpi_snd_mss * 8000000. / rtt;
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001979 }
1980
Eric Dumazeteb6028b2014-06-05 06:47:37 -07001981 if (info->tcpi_pacing_rate &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02001982 info->tcpi_pacing_rate != ~0ULL) {
1983 s.pacing_rate = info->tcpi_pacing_rate * 8.;
Eric Dumazeteb6028b2014-06-05 06:47:37 -07001984
1985 if (info->tcpi_max_pacing_rate &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02001986 info->tcpi_max_pacing_rate != ~0ULL)
1987 s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
Eric Dumazeteb6028b2014-06-05 06:47:37 -07001988 }
Eric Dumazet1a4dda72015-05-11 10:03:49 -07001989 s.bytes_acked = info->tcpi_bytes_acked;
1990 s.bytes_received = info->tcpi_bytes_received;
Craig Gallekecb435e2015-05-26 14:54:41 -04001991 s.segs_out = info->tcpi_segs_out;
1992 s.segs_in = info->tcpi_segs_in;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001993 tcp_stats_print(&s);
Stephen Hemminger92de1c22015-07-21 10:49:54 -07001994 free(s.dctcp);
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001995 }
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001996}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001997
Pavel Emelyanov77a8ca82014-01-28 10:24:15 +04001998static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001999{
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002000 struct rtattr * tb[INET_DIAG_MAX+1];
shemminger351efcd2005-09-01 19:21:50 +00002001 struct inet_diag_msg *r = NLMSG_DATA(nlh);
Vadim Kochan055840f2015-02-13 22:13:58 +02002002 struct sockstat s = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002003
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002004 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
2005 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2006
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002007 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]);
Vadim Kochan8250bc92015-01-20 16:14:24 +02002017
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002018 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 }
Vadim Kochan8250bc92015-01-20 16:14:24 +02002023
shemminger351efcd2005-09-01 19:21:50 +00002024 memcpy(s.local.data, r->id.idiag_src, s.local.bytelen);
2025 memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002026
2027 if (f && f->f && run_ssfilter(f->f, &s) == 0)
2028 return 0;
2029
Craig Gallek6885e3b2015-06-17 11:14:48 -04002030 if (tb[INET_DIAG_PROTOCOL])
2031 protocol = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]);
2032
Vadim Kochan8250bc92015-01-20 16:14:24 +02002033 inet_stats_print(&s, protocol);
Richard Haines116ac922014-03-07 10:36:52 +00002034
Vadim Kochan055840f2015-02-13 22:13:58 +02002035 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
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002044 if (show_details) {
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002045 sock_details_print(&s);
Phil Sutterf32dc742015-06-24 13:07:20 +02002046 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 }
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002051 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 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002056 }
Vadim Kochan8250bc92015-01-20 16:14:24 +02002057
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002058 if (show_mem || show_tcpinfo) {
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002059 printf("\n\t");
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002060 tcp_show_info(nlh, r, tb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002061 }
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002062
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002063 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002064 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002065}
2066
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002067static int tcpdiag_send(int fd, int protocol, struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002068{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002069 struct sockaddr_nl nladdr;
2070 struct {
2071 struct nlmsghdr nlh;
shemminger351efcd2005-09-01 19:21:50 +00002072 struct inet_diag_req r;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002073 } req;
2074 char *bc = NULL;
2075 int bclen;
2076 struct msghdr msg;
2077 struct rtattr rta;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002078 struct iovec iov[3];
2079
Pavel Emelyanov346f8ca2012-10-25 03:24:58 +00002080 if (protocol == IPPROTO_UDP)
2081 return -1;
2082
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002083 memset(&nladdr, 0, sizeof(nladdr));
2084 nladdr.nl_family = AF_NETLINK;
2085
2086 req.nlh.nlmsg_len = sizeof(req);
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00002087 if (protocol == IPPROTO_TCP)
2088 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
2089 else
2090 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002091 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
2092 req.nlh.nlmsg_pid = 0;
vadimk8a4025f2014-12-04 12:32:58 +02002093 req.nlh.nlmsg_seq = MAGIC_SEQ;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002094 memset(&req.r, 0, sizeof(req.r));
shemminger351efcd2005-09-01 19:21:50 +00002095 req.r.idiag_family = AF_INET;
2096 req.r.idiag_states = f->states;
Shan Wei910b0392012-05-03 16:39:52 +08002097 if (show_mem) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002098 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
Shan Wei910b0392012-05-03 16:39:52 +08002099 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2100 }
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002101
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002102 if (show_tcpinfo) {
shemminger351efcd2005-09-01 19:21:50 +00002103 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));
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002106 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002107
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002108 iov[0] = (struct iovec){
2109 .iov_base = &req,
2110 .iov_len = sizeof(req)
shemmingerea8fc102005-06-22 18:27:49 +00002111 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002112 if (f->f) {
2113 bclen = ssfilter_bytecompile(f->f, &bc);
shemminger351efcd2005-09-01 19:21:50 +00002114 rta.rta_type = INET_DIAG_REQ_BYTECODE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002115 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) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002122 .msg_name = (void*)&nladdr,
shemmingerea8fc102005-06-22 18:27:49 +00002123 .msg_namelen = sizeof(nladdr),
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002124 .msg_iov = iov,
shemmingerea8fc102005-06-22 18:27:49 +00002125 .msg_iovlen = f->f ? 3 : 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002126 };
2127
Eric Dumazet930a75f2012-04-10 09:00:16 -07002128 if (sendmsg(fd, &msg, 0) < 0) {
2129 close(fd);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002130 return -1;
Eric Dumazet930a75f2012-04-10 09:00:16 -07002131 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002132
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002133 return 0;
2134}
2135
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002136static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
2137{
2138 struct sockaddr_nl nladdr;
vadimk5fb421d2014-10-30 18:49:25 +02002139 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002140 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
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002152 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
Vadim Kochan486ccd92014-12-26 04:26:27 +02002195struct inet_diag_arg {
2196 struct filter *f;
2197 int protocol;
2198};
2199
2200static int show_one_inet_sock(const struct sockaddr_nl *addr,
2201 struct nlmsghdr *h, void *arg)
2202{
2203 int err;
2204 struct inet_diag_arg *diag_arg = arg;
2205 struct inet_diag_msg *r = NLMSG_DATA(h);
2206
2207 if (!(diag_arg->f->families & (1 << r->idiag_family)))
2208 return 0;
Craig Gallek6885e3b2015-06-17 11:14:48 -04002209 if ((err = inet_show_sock(h, diag_arg->f, diag_arg->protocol)) < 0)
Vadim Kochan486ccd92014-12-26 04:26:27 +02002210 return err;
2211
2212 return 0;
2213}
2214
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002215static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
2216{
Vadim Kochan486ccd92014-12-26 04:26:27 +02002217 int err = 0;
2218 struct rtnl_handle rth;
2219 int family = PF_INET;
2220 struct inet_diag_arg arg = { .f = f, .protocol = protocol };
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002221
Vadim Kochan486ccd92014-12-26 04:26:27 +02002222 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002223 return -1;
Vadim Kochan486ccd92014-12-26 04:26:27 +02002224 rth.dump = MAGIC_SEQ;
2225 rth.dump_fp = dump_fp;
Eric Dumazet518af1e2015-05-29 05:37:49 -07002226 if (preferred_family == PF_INET6)
2227 family = PF_INET6;
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002228
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002229again:
Vadim Kochan486ccd92014-12-26 04:26:27 +02002230 if ((err = sockdiag_send(family, rth.fd, protocol, f)))
2231 goto Exit;
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002232
Vadim Kochan486ccd92014-12-26 04:26:27 +02002233 if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) {
2234 if (family != PF_UNSPEC) {
2235 family = PF_UNSPEC;
2236 goto again;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002237 }
Vadim Kochan486ccd92014-12-26 04:26:27 +02002238 goto Exit;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002239 }
Eric Dumazet518af1e2015-05-29 05:37:49 -07002240 if (family == PF_INET && preferred_family != PF_INET) {
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002241 family = PF_INET6;
2242 goto again;
2243 }
2244
Vadim Kochan486ccd92014-12-26 04:26:27 +02002245Exit:
2246 rtnl_close(&rth);
2247 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002248}
2249
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002250static int tcp_show_netlink_file(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002251{
2252 FILE *fp;
Eric Dumazete5572122014-10-11 09:43:13 -07002253 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002254
2255 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
2256 perror("fopen($TCPDIAG_FILE)");
2257 return -1;
2258 }
2259
2260 while (1) {
2261 int status, err;
2262 struct nlmsghdr *h = (struct nlmsghdr*)buf;
2263
2264 status = fread(buf, 1, sizeof(*h), fp);
2265 if (status < 0) {
2266 perror("Reading header from $TCPDIAG_FILE");
2267 return -1;
2268 }
2269 if (status != sizeof(*h)) {
2270 perror("Unexpected EOF reading $TCPDIAG_FILE");
2271 return -1;
2272 }
2273
2274 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
2275
2276 if (status < 0) {
2277 perror("Reading $TCPDIAG_FILE");
2278 return -1;
2279 }
2280 if (status + sizeof(*h) < h->nlmsg_len) {
2281 perror("Unexpected EOF reading $TCPDIAG_FILE");
2282 return -1;
2283 }
2284
2285 /* The only legal exit point */
2286 if (h->nlmsg_type == NLMSG_DONE)
2287 return 0;
2288
2289 if (h->nlmsg_type == NLMSG_ERROR) {
2290 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
2291 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
2292 fprintf(stderr, "ERROR truncated\n");
2293 } else {
2294 errno = -err->error;
2295 perror("TCPDIAG answered");
2296 }
2297 return -1;
2298 }
2299
Pavel Emelyanov77a8ca82014-01-28 10:24:15 +04002300 err = inet_show_sock(h, f, IPPROTO_TCP);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002301 if (err < 0)
2302 return err;
2303 }
2304}
2305
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002306static int tcp_show(struct filter *f, int socktype)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002307{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002308 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002309 char *buf = NULL;
2310 int bufsize = 64*1024;
2311
Vadim Kochan1527a172015-02-13 13:01:08 +02002312 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2313 return 0;
2314
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002315 dg_proto = TCP_PROTO;
2316
2317 if (getenv("TCPDIAG_FILE"))
2318 return tcp_show_netlink_file(f);
2319
2320 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00002321 && inet_show_netlink(f, NULL, socktype) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002322 return 0;
2323
2324 /* Sigh... We have to parse /proc/net/tcp... */
2325
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002326
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002327 /* Estimate amount of sockets and try to allocate
2328 * huge buffer to read all the table at one read.
2329 * Limit it by 16MB though. The assumption is: as soon as
2330 * kernel was able to hold information about N connections,
2331 * it is able to give us some memory for snapshot.
2332 */
2333 if (1) {
Bryton Leea221d622015-02-12 14:16:04 +08002334 get_slabstat(&slabstat);
2335
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002336 int guess = slabstat.socks+slabstat.tcp_syns;
2337 if (f->states&(1<<SS_TIME_WAIT))
2338 guess += slabstat.tcp_tws;
2339 if (guess > (16*1024*1024)/128)
2340 guess = (16*1024*1024)/128;
2341 guess *= 128;
2342 if (guess > bufsize)
2343 bufsize = guess;
2344 }
2345 while (bufsize >= 64*1024) {
2346 if ((buf = malloc(bufsize)) != NULL)
2347 break;
2348 bufsize /= 2;
2349 }
2350 if (buf == NULL) {
2351 errno = ENOMEM;
2352 return -1;
2353 }
2354
2355 if (f->families & (1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002356 if ((fp = net_tcp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002357 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002358
2359 setbuffer(fp, buf, bufsize);
2360 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002361 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002362 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002363 }
2364
2365 if ((f->families & (1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002366 (fp = net_tcp6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002367 setbuffer(fp, buf, bufsize);
2368 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002369 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002370 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002371 }
2372
2373 free(buf);
2374 return 0;
2375
2376outerr:
2377 do {
2378 int saved_errno = errno;
Stephen Hemminger92de1c22015-07-21 10:49:54 -07002379 free(buf);
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002380 if (fp)
2381 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002382 errno = saved_errno;
2383 return -1;
2384 } while (0);
2385}
2386
2387
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002388static int dgram_show_line(char *line, const struct filter *f, int family)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002389{
Vadim Kochan055840f2015-02-13 22:13:58 +02002390 struct sockstat s = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002391 char *loc, *rem, *data;
2392 char opt[256];
2393 int n;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002394
Vadim Kochan8250bc92015-01-20 16:14:24 +02002395 if (proc_inet_split_line(line, &loc, &rem, &data))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002396 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002397
Vadim Kochan8250bc92015-01-20 16:14:24 +02002398 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
2399 if (!(f->states & (1 << state)))
2400 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002401
Vadim Kochan8250bc92015-01-20 16:14:24 +02002402 proc_parse_inet_addr(loc, rem, family, &s);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002403
2404 if (f->f && run_ssfilter(f->f, &s) == 0)
2405 return 0;
2406
2407 opt[0] = 0;
Stephen Hemmingere7113c62007-07-10 18:26:54 -07002408 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002409 &s.state, &s.wq, &s.rq,
2410 &s.uid, &s.ino,
2411 &s.refcnt, &s.sk, opt);
2412
2413 if (n < 9)
2414 opt[0] = 0;
2415
Nikolay Aleksandrov235c4452015-07-15 05:50:42 -07002416 inet_stats_print(&s, dg_proto == UDP_PROTO ? IPPROTO_UDP : 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002417
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002418 if (show_details && opt[0])
2419 printf(" opt:\"%s\"", opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002420
Vadim Kochan8250bc92015-01-20 16:14:24 +02002421 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002422 return 0;
2423}
2424
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002425static int udp_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002426{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002427 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002428
Vadim Kochan1527a172015-02-13 13:01:08 +02002429 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2430 return 0;
2431
Vadim Kochanace5cb32015-01-08 02:42:54 +02002432 dg_proto = UDP_PROTO;
2433
Pavel Emelyanov346f8ca2012-10-25 03:24:58 +00002434 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2435 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
2436 return 0;
2437
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002438 if (f->families&(1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002439 if ((fp = net_udp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002440 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002441 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002442 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002443 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002444 }
2445
2446 if ((f->families&(1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002447 (fp = net_udp6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002448 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002449 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002450 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002451 }
2452 return 0;
2453
2454outerr:
2455 do {
2456 int saved_errno = errno;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002457 if (fp)
2458 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002459 errno = saved_errno;
2460 return -1;
2461 } while (0);
2462}
2463
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002464static int raw_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002465{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002466 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002467
Vadim Kochan1527a172015-02-13 13:01:08 +02002468 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2469 return 0;
2470
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002471 dg_proto = RAW_PROTO;
2472
2473 if (f->families&(1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002474 if ((fp = net_raw_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002475 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002476 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002477 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002478 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002479 }
2480
2481 if ((f->families&(1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002482 (fp = net_raw6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002483 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002484 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002485 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002486 }
2487 return 0;
2488
2489outerr:
2490 do {
2491 int saved_errno = errno;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002492 if (fp)
2493 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002494 errno = saved_errno;
2495 return -1;
2496 } while (0);
2497}
2498
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002499int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2500 SS_ESTABLISHED, SS_CLOSING };
2501
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002502#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002503
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002504static void unix_list_free(struct sockstat *list)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002505{
2506 while (list) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002507 struct sockstat *s = list;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002508
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002509 list = list->next;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002510 free(s->name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002511 free(s);
2512 }
2513}
2514
Masatake YAMATO30b669d2014-01-08 20:13:46 +09002515static const char *unix_netid_name(int type)
2516{
2517 const char *netid;
2518
2519 switch (type) {
2520 case SOCK_STREAM:
2521 netid = "u_str";
2522 break;
2523 case SOCK_SEQPACKET:
2524 netid = "u_seq";
2525 break;
2526 case SOCK_DGRAM:
2527 default:
2528 netid = "u_dgr";
2529 break;
2530 }
2531 return netid;
2532}
2533
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002534static bool unix_type_skip(struct sockstat *s, struct filter *f)
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002535{
2536 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2537 return true;
2538 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2539 return true;
2540 if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB)))
2541 return true;
2542 return false;
2543}
2544
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002545static bool unix_use_proc(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002546{
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002547 return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT");
2548}
2549
2550static void unix_stats_print(struct sockstat *list, struct filter *f)
2551{
2552 struct sockstat *s;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002553 char *peer;
Vadim Kochanb217df12015-02-13 22:14:03 +02002554 char *ctx_buf = NULL;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002555 bool use_proc = unix_use_proc();
Vadim Kochanb217df12015-02-13 22:14:03 +02002556 char port_name[30] = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002557
2558 for (s = list; s; s = s->next) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002559 if (!(f->states & (1 << s->state)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002560 continue;
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002561 if (unix_type_skip(s, f))
Masatake YAMATO30b669d2014-01-08 20:13:46 +09002562 continue;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002563
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002564 peer = "*";
2565 if (s->peer_name)
2566 peer = s->peer_name;
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002567
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002568 if (s->rport && use_proc) {
2569 struct sockstat *p;
2570
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002571 for (p = list; p; p = p->next) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002572 if (s->rport == p->lport)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002573 break;
2574 }
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002575
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002576 if (!p) {
2577 peer = "?";
2578 } else {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002579 peer = p->name ? : "*";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002580 }
2581 }
2582
Vadim Kochan29999b02015-02-22 22:23:10 +02002583 if (use_proc && f->f) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002584 struct sockstat st;
2585 st.local.family = AF_UNIX;
2586 st.remote.family = AF_UNIX;
2587 memcpy(st.local.data, &s->name, sizeof(s->name));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002588 if (strcmp(peer, "*") == 0)
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002589 memset(st.remote.data, 0, sizeof(peer));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002590 else
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002591 memcpy(st.remote.data, &peer, sizeof(peer));
2592 if (run_ssfilter(f->f, &st) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002593 continue;
2594 }
2595
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002596 sock_state_print(s, unix_netid_name(s->type));
2597
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002598 sock_addr_print(s->name ?: "*", " ",
Vadim Kochanb217df12015-02-13 22:14:03 +02002599 int_to_str(s->lport, port_name), NULL);
2600 sock_addr_print(peer, " ", int_to_str(s->rport, port_name),
2601 NULL);
Richard Haines116ac922014-03-07 10:36:52 +00002602
2603 if (show_proc_ctx || show_sock_ctx) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002604 if (find_entry(s->ino, &ctx_buf,
Richard Haines116ac922014-03-07 10:36:52 +00002605 (show_proc_ctx & show_sock_ctx) ?
2606 PROC_SOCK_CTX : PROC_CTX) > 0) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002607 printf(" users:(%s)", ctx_buf);
2608 free(ctx_buf);
Richard Haines116ac922014-03-07 10:36:52 +00002609 }
2610 } else if (show_users) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002611 if (find_entry(s->ino, &ctx_buf, USERS) > 0) {
2612 printf(" users:(%s)", ctx_buf);
2613 free(ctx_buf);
Richard Haines116ac922014-03-07 10:36:52 +00002614 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002615 }
2616 printf("\n");
2617 }
2618}
2619
vadimk8a4025f2014-12-04 12:32:58 +02002620static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
2621 void *arg)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002622{
vadimk8a4025f2014-12-04 12:32:58 +02002623 struct filter *f = (struct filter *)arg;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002624 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2625 struct rtattr *tb[UNIX_DIAG_MAX+1];
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002626 char name[128];
2627 struct sockstat stat = { .name = "*", .peer_name = "*" };
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002628
2629 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
2630 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2631
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002632 stat.type = r->udiag_type;
2633 stat.state = r->udiag_state;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002634 stat.ino = stat.lport = r->udiag_ino;
2635 stat.local.family = stat.remote.family = AF_UNIX;
Masatake YAMATO0d2e01c2014-01-08 20:13:47 +09002636
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002637 if (unix_type_skip(&stat, f))
2638 return 0;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002639
Hannes Frederic Sowadefd61c2013-02-22 15:28:10 +00002640 if (tb[UNIX_DIAG_RQLEN]) {
2641 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002642 stat.rq = rql->udiag_rqueue;
2643 stat.wq = rql->udiag_wqueue;
Hannes Frederic Sowadefd61c2013-02-22 15:28:10 +00002644 }
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002645 if (tb[UNIX_DIAG_NAME]) {
2646 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2647
2648 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2649 name[len] = '\0';
2650 if (name[0] == '\0')
2651 name[0] = '@';
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002652 stat.name = &name[0];
2653 memcpy(stat.local.data, &stat.name, sizeof(stat.name));
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002654 }
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002655 if (tb[UNIX_DIAG_PEER])
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002656 stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002657
Vadim Kochan29999b02015-02-22 22:23:10 +02002658 if (f->f && run_ssfilter(f->f, &stat) == 0)
2659 return 0;
2660
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002661 unix_stats_print(&stat, f);
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002662
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00002663 if (show_mem) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002664 printf("\t");
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00002665 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2666 }
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002667 if (show_details) {
2668 if (tb[UNIX_DIAG_SHUTDOWN]) {
2669 unsigned char mask;
2670 mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]);
2671 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2672 }
2673 }
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002674 if (show_mem || show_details)
2675 printf("\n");
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002676
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002677 return 0;
2678}
2679
vadimk8a4025f2014-12-04 12:32:58 +02002680static int handle_netlink_request(struct filter *f, struct nlmsghdr *req,
2681 size_t size, rtnl_filter_t show_one_sock)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002682{
vadimk8a4025f2014-12-04 12:32:58 +02002683 int ret = -1;
2684 struct rtnl_handle rth;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002685
vadimk8a4025f2014-12-04 12:32:58 +02002686 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002687 return -1;
2688
vadimk8a4025f2014-12-04 12:32:58 +02002689 rth.dump = MAGIC_SEQ;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002690
vadimk8a4025f2014-12-04 12:32:58 +02002691 if (rtnl_send(&rth, req, size) < 0)
2692 goto Exit;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002693
vadimk8a4025f2014-12-04 12:32:58 +02002694 if (rtnl_dump_filter(&rth, show_one_sock, f))
2695 goto Exit;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002696
vadimk8a4025f2014-12-04 12:32:58 +02002697 ret = 0;
2698Exit:
2699 rtnl_close(&rth);
2700 return ret;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002701}
2702
vadimk8a4025f2014-12-04 12:32:58 +02002703static int unix_show_netlink(struct filter *f)
Andrey Vagind8402b92013-06-05 12:41:58 +04002704{
vadimk5fb421d2014-10-30 18:49:25 +02002705 DIAG_REQUEST(req, struct unix_diag_req r);
Andrey Vagind8402b92013-06-05 12:41:58 +04002706
2707 req.r.sdiag_family = AF_UNIX;
2708 req.r.udiag_states = f->states;
2709 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
2710 if (show_mem)
2711 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
2712
vadimk8a4025f2014-12-04 12:32:58 +02002713 return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
Andrey Vagind8402b92013-06-05 12:41:58 +04002714}
2715
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002716static int unix_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002717{
2718 FILE *fp;
2719 char buf[256];
2720 char name[128];
2721 int newformat = 0;
2722 int cnt;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002723 struct sockstat *list = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002724
Vadim Kochan9db7bf12015-01-04 22:18:40 +02002725 if (!filter_af_get(f, AF_UNIX))
2726 return 0;
2727
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002728 if (!unix_use_proc() && unix_show_netlink(f) == 0)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002729 return 0;
2730
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002731 if ((fp = net_unix_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002732 return -1;
2733 fgets(buf, sizeof(buf)-1, fp);
2734
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002735 if (memcmp(buf, "Peer", 4) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002736 newformat = 1;
2737 cnt = 0;
2738
2739 while (fgets(buf, sizeof(buf)-1, fp)) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002740 struct sockstat *u, **insp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002741 int flags;
2742
willy tarreau0ee90522015-10-06 12:09:33 +02002743 if (!(u = calloc(1, sizeof(*u))))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002744 break;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002745 u->name = NULL;
2746 u->peer_name = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002747
2748 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002749 &u->rport, &u->rq, &u->wq, &flags, &u->type,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002750 &u->state, &u->ino, name) < 8)
2751 name[0] = 0;
2752
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002753 u->lport = u->ino;
2754 u->local.family = u->remote.family = AF_UNIX;
2755
2756 if (flags & (1 << 16)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002757 u->state = SS_LISTEN;
2758 } else {
2759 u->state = unix_state_map[u->state-1];
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002760 if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002761 u->state = SS_ESTABLISHED;
2762 }
2763
2764 if (!newformat) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002765 u->rport = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002766 u->rq = 0;
2767 u->wq = 0;
2768 }
2769
2770 insp = &list;
2771 while (*insp) {
2772 if (u->type < (*insp)->type ||
2773 (u->type == (*insp)->type &&
2774 u->ino < (*insp)->ino))
2775 break;
2776 insp = &(*insp)->next;
2777 }
2778 u->next = *insp;
2779 *insp = u;
2780
2781 if (name[0]) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002782 if ((u->name = malloc(strlen(name)+1)) == NULL)
2783 break;
2784 strcpy(u->name, name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002785 }
2786 if (++cnt > MAX_UNIX_REMEMBER) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002787 unix_stats_print(list, f);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002788 unix_list_free(list);
2789 list = NULL;
2790 cnt = 0;
2791 }
2792 }
Eric Dumazeta3fd8e52012-01-30 17:05:45 +01002793 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002794 if (list) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002795 unix_stats_print(list, f);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002796 unix_list_free(list);
2797 list = NULL;
2798 cnt = 0;
2799 }
2800
2801 return 0;
2802}
2803
Vadim Kochan89f634f2015-02-13 22:13:59 +02002804static int packet_stats_print(struct sockstat *s, const struct filter *f)
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002805{
2806 char *buf = NULL;
Vadim Kochanb217df12015-02-13 22:14:03 +02002807 const char *addr, *port;
2808 char ll_name[16];
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002809
2810 if (f->f) {
Vadim Kochan89f634f2015-02-13 22:13:59 +02002811 s->local.family = AF_PACKET;
2812 s->remote.family = AF_PACKET;
Vadim Kochan89f634f2015-02-13 22:13:59 +02002813 s->local.data[0] = s->prot;
Vadim Kochan89f634f2015-02-13 22:13:59 +02002814 if (run_ssfilter(f->f, s) == 0)
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002815 return 1;
2816 }
2817
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002818 sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr");
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002819
Vadim Kochanb217df12015-02-13 22:14:03 +02002820 if (s->prot == 3)
2821 addr = "*";
2822 else
2823 addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name));
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002824
Vadim Kochanb217df12015-02-13 22:14:03 +02002825 if (s->iface == 0)
2826 port = "*";
2827 else
2828 port = xll_index_to_name(s->iface);
2829
2830 sock_addr_print(addr, ":", port, NULL);
2831 sock_addr_print("", "*", "", NULL);
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002832
2833 if (show_proc_ctx || show_sock_ctx) {
2834 if (find_entry(s->ino, &buf,
2835 (show_proc_ctx & show_sock_ctx) ?
2836 PROC_SOCK_CTX : PROC_CTX) > 0) {
2837 printf(" users:(%s)", buf);
2838 free(buf);
2839 }
2840 } else if (show_users) {
2841 if (find_entry(s->ino, &buf, USERS) > 0) {
2842 printf(" users:(%s)", buf);
2843 free(buf);
2844 }
2845 }
2846
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002847 if (show_details)
2848 sock_details_print(s);
2849
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002850 return 0;
2851}
2852
Vadim Kochan2631b852015-05-15 17:19:30 +03002853static void packet_show_ring(struct packet_diag_ring *ring)
2854{
2855 printf("blk_size:%d", ring->pdr_block_size);
2856 printf(",blk_nr:%d", ring->pdr_block_nr);
2857 printf(",frm_size:%d", ring->pdr_frame_size);
2858 printf(",frm_nr:%d", ring->pdr_frame_nr);
2859 printf(",tmo:%d", ring->pdr_retire_tmo);
2860 printf(",features:0x%x", ring->pdr_features);
2861}
2862
vadimk8a4025f2014-12-04 12:32:58 +02002863static int packet_show_sock(const struct sockaddr_nl *addr,
2864 struct nlmsghdr *nlh, void *arg)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002865{
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002866 const struct filter *f = arg;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002867 struct packet_diag_msg *r = NLMSG_DATA(nlh);
Vadim Kochan2631b852015-05-15 17:19:30 +03002868 struct packet_diag_info *pinfo = NULL;
2869 struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002870 struct rtattr *tb[PACKET_DIAG_MAX+1];
Vadim Kochan89f634f2015-02-13 22:13:59 +02002871 struct sockstat stat = {};
Vadim Kochan2631b852015-05-15 17:19:30 +03002872 uint32_t fanout = 0;
2873 bool has_fanout = false;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002874
2875 parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1),
2876 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2877
2878 /* use /proc/net/packet if all info are not available */
2879 if (!tb[PACKET_DIAG_MEMINFO])
2880 return -1;
2881
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002882 stat.type = r->pdiag_type;
2883 stat.prot = r->pdiag_num;
2884 stat.ino = r->pdiag_ino;
2885 stat.state = SS_CLOSE;
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002886 stat.sk = cookie_sk_get(&r->pdiag_cookie[0]);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002887
2888 if (tb[PACKET_DIAG_MEMINFO]) {
2889 __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002890 stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002891 }
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002892
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002893 if (tb[PACKET_DIAG_INFO]) {
Vadim Kochan2631b852015-05-15 17:19:30 +03002894 pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002895 stat.lport = stat.iface = pinfo->pdi_index;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002896 }
Richard Haines116ac922014-03-07 10:36:52 +00002897
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002898 if (tb[PACKET_DIAG_UID])
2899 stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]);
2900
Vadim Kochan2631b852015-05-15 17:19:30 +03002901 if (tb[PACKET_DIAG_RX_RING])
2902 ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
2903
2904 if (tb[PACKET_DIAG_TX_RING])
2905 ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
2906
2907 if (tb[PACKET_DIAG_FANOUT]) {
2908 has_fanout = true;
2909 fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]);
2910 }
2911
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002912 if (packet_stats_print(&stat, f))
2913 return 0;
2914
Vadim Kochan2631b852015-05-15 17:19:30 +03002915 if (show_details) {
2916 if (pinfo) {
2917 printf("\n\tver:%d", pinfo->pdi_version);
2918 printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
2919 printf(" flags( ");
2920 if (pinfo->pdi_flags & PDI_RUNNING)
2921 printf("running");
2922 if (pinfo->pdi_flags & PDI_AUXDATA)
2923 printf(" auxdata");
2924 if (pinfo->pdi_flags & PDI_ORIGDEV)
2925 printf(" origdev");
2926 if (pinfo->pdi_flags & PDI_VNETHDR)
2927 printf(" vnethdr");
2928 if (pinfo->pdi_flags & PDI_LOSS)
2929 printf(" loss");
2930 if (!pinfo->pdi_flags)
2931 printf("0");
2932 printf(" )");
2933 }
2934 if (ring_rx) {
2935 printf("\n\tring_rx(");
2936 packet_show_ring(ring_rx);
2937 printf(")");
2938 }
2939 if (ring_tx) {
2940 printf("\n\tring_tx(");
2941 packet_show_ring(ring_tx);
2942 printf(")");
2943 }
2944 if (has_fanout) {
2945 uint16_t type = (fanout >> 16) & 0xffff;
2946
2947 printf("\n\tfanout(");
2948 printf("id:%d,", fanout & 0xffff);
2949 printf("type:");
2950
2951 if (type == 0)
2952 printf("hash");
2953 else if (type == 1)
2954 printf("lb");
2955 else if (type == 2)
2956 printf("cpu");
2957 else if (type == 3)
2958 printf("roll");
2959 else if (type == 4)
2960 printf("random");
2961 else if (type == 5)
2962 printf("qm");
2963 else
2964 printf("0x%x", type);
2965
2966 printf(")");
2967 }
2968 }
2969
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002970 if (show_bpf && tb[PACKET_DIAG_FILTER]) {
2971 struct sock_filter *fil =
2972 RTA_DATA(tb[PACKET_DIAG_FILTER]);
2973 int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) /
2974 sizeof(struct sock_filter);
2975
2976 printf("\n\tbpf filter (%d): ", num);
2977 while (num) {
2978 printf(" 0x%02x %u %u %u,",
2979 fil->code, fil->jt, fil->jf, fil->k);
2980 num--;
2981 fil++;
2982 }
2983 }
2984 printf("\n");
2985 return 0;
2986}
2987
vadimk8a4025f2014-12-04 12:32:58 +02002988static int packet_show_netlink(struct filter *f)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002989{
vadimk5fb421d2014-10-30 18:49:25 +02002990 DIAG_REQUEST(req, struct packet_diag_req r);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002991
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002992 req.r.sdiag_family = AF_PACKET;
Vadim Kochan2631b852015-05-15 17:19:30 +03002993 req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
2994 PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002995
vadimk8a4025f2014-12-04 12:32:58 +02002996 return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07002997}
2998
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002999static int packet_show_line(char *buf, const struct filter *f, int fam)
3000{
3001 unsigned long long sk;
Vadim Kochan89f634f2015-02-13 22:13:59 +02003002 struct sockstat stat = {};
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003003 int type, prot, iface, state, rq, uid, ino;
3004
3005 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
3006 &sk,
3007 &type, &prot, &iface, &state,
3008 &rq, &uid, &ino);
3009
3010 if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
3011 return 0;
3012 if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
3013 return 0;
3014
3015 stat.type = type;
3016 stat.prot = prot;
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003017 stat.lport = stat.iface = iface;
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003018 stat.state = state;
3019 stat.rq = rq;
3020 stat.uid = uid;
3021 stat.ino = ino;
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003022 stat.state = SS_CLOSE;
3023
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003024 if (packet_stats_print(&stat, f))
3025 return 0;
3026
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003027 printf("\n");
3028 return 0;
3029}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003030
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003031static int packet_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003032{
3033 FILE *fp;
Phil Sutterb95d28c2015-08-06 14:24:34 +02003034 int rc = 0;
Stephen Hemminger3d0b7432014-12-20 15:47:17 -08003035
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003036 if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
Vadim Kochanb9ea4452014-12-20 15:40:55 -08003037 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003038
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003039 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3040 packet_show_netlink(f) == 0)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003041 return 0;
3042
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003043 if ((fp = net_packet_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003044 return -1;
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003045 if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
Phil Sutterb95d28c2015-08-06 14:24:34 +02003046 rc = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003047
Phil Sutterb95d28c2015-08-06 14:24:34 +02003048 fclose(fp);
3049 return rc;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003050}
3051
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003052static int netlink_show_one(struct filter *f,
Andrey Vagin129709a2013-06-05 12:41:59 +04003053 int prot, int pid, unsigned groups,
Andrey Vaginf271fe02013-06-05 12:42:00 +04003054 int state, int dst_pid, unsigned dst_group,
Andrey Vagin129709a2013-06-05 12:41:59 +04003055 int rq, int wq,
3056 unsigned long long sk, unsigned long long cb)
3057{
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003058 struct sockstat st;
Vadim Kochanb217df12015-02-13 22:14:03 +02003059 SPRINT_BUF(prot_buf) = {};
3060 const char *prot_name;
3061 char procname[64] = {};
vadimkeef43b52014-10-30 17:33:36 +02003062
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003063 st.state = SS_CLOSE;
3064 st.rq = rq;
3065 st.wq = wq;
3066
Andrey Vagin129709a2013-06-05 12:41:59 +04003067 if (f->f) {
Vadim Kochan055840f2015-02-13 22:13:58 +02003068 st.local.family = AF_NETLINK;
3069 st.remote.family = AF_NETLINK;
3070 st.rport = -1;
3071 st.lport = pid;
3072 st.local.data[0] = prot;
Vadim Kochan055840f2015-02-13 22:13:58 +02003073 if (run_ssfilter(f->f, &st) == 0)
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003074 return 1;
Andrey Vagin129709a2013-06-05 12:41:59 +04003075 }
3076
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003077 sock_state_print(&st, "nl");
vadimkeef43b52014-10-30 17:33:36 +02003078
Vadim Kochanb217df12015-02-13 22:14:03 +02003079 if (resolve_services)
3080 prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf));
3081 else
3082 prot_name = int_to_str(prot, prot_buf);
vadimkeef43b52014-10-30 17:33:36 +02003083
Andrey Vagin129709a2013-06-05 12:41:59 +04003084 if (pid == -1) {
Vadim Kochanb217df12015-02-13 22:14:03 +02003085 procname[0] = '*';
Andrey Vagin129709a2013-06-05 12:41:59 +04003086 } else if (resolve_services) {
3087 int done = 0;
3088 if (!pid) {
3089 done = 1;
Vadim Kochanb217df12015-02-13 22:14:03 +02003090 strncpy(procname, "kernel", 6);
Andrey Vagin129709a2013-06-05 12:41:59 +04003091 } else if (pid > 0) {
Andrey Vagin129709a2013-06-05 12:41:59 +04003092 FILE *fp;
willy tarreau0ee90522015-10-06 12:09:33 +02003093 snprintf(procname, sizeof(procname), "%s/%d/stat",
Andrey Vagin129709a2013-06-05 12:41:59 +04003094 getenv("PROC_ROOT") ? : "/proc", pid);
3095 if ((fp = fopen(procname, "r")) != NULL) {
3096 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
willy tarreau0ee90522015-10-06 12:09:33 +02003097 snprintf(procname+strlen(procname),
3098 sizeof(procname)-strlen(procname),
3099 "/%d", pid);
Andrey Vagin129709a2013-06-05 12:41:59 +04003100 done = 1;
3101 }
3102 fclose(fp);
3103 }
3104 }
3105 if (!done)
Vadim Kochanb217df12015-02-13 22:14:03 +02003106 int_to_str(pid, procname);
Andrey Vagin129709a2013-06-05 12:41:59 +04003107 } else {
Vadim Kochanb217df12015-02-13 22:14:03 +02003108 int_to_str(pid, procname);
Andrey Vagin129709a2013-06-05 12:41:59 +04003109 }
Andrey Vaginf271fe02013-06-05 12:42:00 +04003110
Vadim Kochanb217df12015-02-13 22:14:03 +02003111 sock_addr_print(prot_name, ":", procname, NULL);
3112
Andrey Vaginf271fe02013-06-05 12:42:00 +04003113 if (state == NETLINK_CONNECTED) {
Vadim Kochanb217df12015-02-13 22:14:03 +02003114 char dst_group_buf[30];
3115 char dst_pid_buf[30];
3116 sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
3117 int_to_str(dst_pid, dst_pid_buf), NULL);
Andrey Vaginf271fe02013-06-05 12:42:00 +04003118 } else {
Vadim Kochanb217df12015-02-13 22:14:03 +02003119 sock_addr_print("", "*", "", NULL);
Andrey Vaginf271fe02013-06-05 12:42:00 +04003120 }
Andrey Vagin129709a2013-06-05 12:41:59 +04003121
Richard Haines116ac922014-03-07 10:36:52 +00003122 char *pid_context = NULL;
3123 if (show_proc_ctx) {
3124 /* The pid value will either be:
3125 * 0 if destination kernel - show kernel initial context.
3126 * A valid process pid - use getpidcon.
3127 * A unique value allocated by the kernel or netlink user
3128 * to the process - show context as "not available".
3129 */
3130 if (!pid)
3131 security_get_initial_context("kernel", &pid_context);
3132 else if (pid > 0)
3133 getpidcon(pid, &pid_context);
3134
3135 if (pid_context != NULL) {
3136 printf("proc_ctx=%-*s ", serv_width, pid_context);
3137 free(pid_context);
3138 } else {
3139 printf("proc_ctx=%-*s ", serv_width, "unavailable");
3140 }
3141 }
3142
Andrey Vagin129709a2013-06-05 12:41:59 +04003143 if (show_details) {
3144 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
3145 }
3146 printf("\n");
3147
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003148 return 0;
Andrey Vagin129709a2013-06-05 12:41:59 +04003149}
3150
vadimk8a4025f2014-12-04 12:32:58 +02003151static int netlink_show_sock(const struct sockaddr_nl *addr,
3152 struct nlmsghdr *nlh, void *arg)
Andrey Vaginecb928c2013-06-05 12:42:01 +04003153{
vadimk8a4025f2014-12-04 12:32:58 +02003154 struct filter *f = (struct filter *)arg;
Andrey Vaginecb928c2013-06-05 12:42:01 +04003155 struct netlink_diag_msg *r = NLMSG_DATA(nlh);
3156 struct rtattr *tb[NETLINK_DIAG_MAX+1];
3157 int rq = 0, wq = 0;
3158 unsigned long groups = 0;
3159
3160 parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr*)(r+1),
3161 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3162
3163 if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
3164 groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]);
3165
3166 if (tb[NETLINK_DIAG_MEMINFO]) {
3167 const __u32 *skmeminfo;
3168 skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
3169
3170 rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3171 wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC];
3172 }
3173
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003174 if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups,
Andrey Vaginecb928c2013-06-05 12:42:01 +04003175 r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group,
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003176 rq, wq, 0, 0)) {
3177 return 0;
3178 }
Andrey Vaginecb928c2013-06-05 12:42:01 +04003179
3180 if (show_mem) {
3181 printf("\t");
3182 print_skmeminfo(tb, NETLINK_DIAG_MEMINFO);
3183 printf("\n");
3184 }
3185
3186 return 0;
3187}
3188
vadimk8a4025f2014-12-04 12:32:58 +02003189static int netlink_show_netlink(struct filter *f)
Andrey Vaginecb928c2013-06-05 12:42:01 +04003190{
vadimk5fb421d2014-10-30 18:49:25 +02003191 DIAG_REQUEST(req, struct netlink_diag_req r);
Andrey Vaginecb928c2013-06-05 12:42:01 +04003192
3193 req.r.sdiag_family = AF_NETLINK;
3194 req.r.sdiag_protocol = NDIAG_PROTO_ALL;
3195 req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO;
3196
vadimk8a4025f2014-12-04 12:32:58 +02003197 return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock);
Andrey Vaginecb928c2013-06-05 12:42:01 +04003198}
3199
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003200static int netlink_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003201{
3202 FILE *fp;
3203 char buf[256];
3204 int prot, pid;
3205 unsigned groups;
3206 int rq, wq, rc;
3207 unsigned long long sk, cb;
3208
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003209 if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
Vadim Kochanb9ea4452014-12-20 15:40:55 -08003210 return 0;
3211
Andrey Vagin129709a2013-06-05 12:41:59 +04003212 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
vadimk8a4025f2014-12-04 12:32:58 +02003213 netlink_show_netlink(f) == 0)
Andrey Vagin129709a2013-06-05 12:41:59 +04003214 return 0;
3215
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003216 if ((fp = net_netlink_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003217 return -1;
3218 fgets(buf, sizeof(buf)-1, fp);
3219
3220 while (fgets(buf, sizeof(buf)-1, fp)) {
3221 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
3222 &sk,
3223 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
3224
Andrey Vaginf271fe02013-06-05 12:42:00 +04003225 netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003226 }
3227
Phil Sutterb95d28c2015-08-06 14:24:34 +02003228 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003229 return 0;
3230}
3231
Craig Gallek6885e3b2015-06-17 11:14:48 -04003232struct sock_diag_msg {
3233 __u8 sdiag_family;
3234};
3235
3236static int generic_show_sock(const struct sockaddr_nl *addr,
3237 struct nlmsghdr *nlh, void *arg)
3238{
3239 struct sock_diag_msg *r = NLMSG_DATA(nlh);
3240 struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX };
3241
3242 switch (r->sdiag_family) {
3243 case AF_INET:
3244 case AF_INET6:
3245 return show_one_inet_sock(addr, nlh, &inet_arg);
3246 case AF_UNIX:
3247 return unix_show_sock(addr, nlh, arg);
3248 case AF_PACKET:
3249 return packet_show_sock(addr, nlh, arg);
3250 case AF_NETLINK:
3251 return netlink_show_sock(addr, nlh, arg);
3252 default:
3253 return -1;
3254 }
3255}
3256
3257static int handle_follow_request(struct filter *f)
3258{
3259 int ret = -1;
3260 int groups = 0;
3261 struct rtnl_handle rth;
3262
3263 if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB))
3264 groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1);
3265 if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB))
3266 groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1);
3267 if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB))
3268 groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1);
3269 if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB))
3270 groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1);
3271
3272 if (groups == 0)
3273 return -1;
3274
3275 if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG))
3276 return -1;
3277
3278 rth.dump = 0;
3279 rth.local.nl_pid = 0;
3280
3281 if (rtnl_dump_filter(&rth, generic_show_sock, f))
3282 goto Exit;
3283
3284 ret = 0;
3285Exit:
3286 rtnl_close(&rth);
3287 return ret;
3288}
3289
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003290struct snmpstat
3291{
3292 int tcp_estab;
3293};
3294
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003295static int get_snmp_int(char *proto, char *key, int *result)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003296{
3297 char buf[1024];
3298 FILE *fp;
3299 int protolen = strlen(proto);
3300 int keylen = strlen(key);
3301
3302 *result = 0;
3303
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003304 if ((fp = net_snmp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003305 return -1;
3306
3307 while (fgets(buf, sizeof(buf), fp) != NULL) {
3308 char *p = buf;
3309 int pos = 0;
3310 if (memcmp(buf, proto, protolen))
3311 continue;
3312 while ((p = strchr(p, ' ')) != NULL) {
3313 pos++;
3314 p++;
3315 if (memcmp(p, key, keylen) == 0 &&
3316 (p[keylen] == ' ' || p[keylen] == '\n'))
3317 break;
3318 }
3319 if (fgets(buf, sizeof(buf), fp) == NULL)
3320 break;
3321 if (memcmp(buf, proto, protolen))
3322 break;
3323 p = buf;
3324 while ((p = strchr(p, ' ')) != NULL) {
3325 p++;
3326 if (--pos == 0) {
3327 sscanf(p, "%d", result);
3328 fclose(fp);
3329 return 0;
3330 }
3331 }
3332 }
3333
3334 fclose(fp);
3335 errno = ESRCH;
3336 return -1;
3337}
3338
3339
3340/* Get stats from sockstat */
3341
Vadim Kochan055840f2015-02-13 22:13:58 +02003342struct ssummary
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003343{
3344 int socks;
3345 int tcp_mem;
3346 int tcp_total;
3347 int tcp_orphans;
3348 int tcp_tws;
3349 int tcp4_hashed;
3350 int udp4;
3351 int raw4;
3352 int frag4;
3353 int frag4_mem;
3354 int tcp6_hashed;
3355 int udp6;
3356 int raw6;
3357 int frag6;
3358 int frag6_mem;
3359};
3360
Vadim Kochan055840f2015-02-13 22:13:58 +02003361static void get_sockstat_line(char *line, struct ssummary *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003362{
3363 char id[256], rem[256];
3364
3365 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
3366 return;
3367
3368 if (strcmp(id, "sockets:") == 0)
3369 sscanf(rem, "%*s%d", &s->socks);
3370 else if (strcmp(id, "UDP:") == 0)
3371 sscanf(rem, "%*s%d", &s->udp4);
3372 else if (strcmp(id, "UDP6:") == 0)
3373 sscanf(rem, "%*s%d", &s->udp6);
3374 else if (strcmp(id, "RAW:") == 0)
3375 sscanf(rem, "%*s%d", &s->raw4);
3376 else if (strcmp(id, "RAW6:") == 0)
3377 sscanf(rem, "%*s%d", &s->raw6);
3378 else if (strcmp(id, "TCP6:") == 0)
3379 sscanf(rem, "%*s%d", &s->tcp6_hashed);
3380 else if (strcmp(id, "FRAG:") == 0)
3381 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
3382 else if (strcmp(id, "FRAG6:") == 0)
3383 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
3384 else if (strcmp(id, "TCP:") == 0)
3385 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
3386 &s->tcp4_hashed,
3387 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
3388}
3389
Vadim Kochan055840f2015-02-13 22:13:58 +02003390static int get_sockstat(struct ssummary *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003391{
3392 char buf[256];
3393 FILE *fp;
3394
3395 memset(s, 0, sizeof(*s));
3396
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003397 if ((fp = net_sockstat_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003398 return -1;
3399 while(fgets(buf, sizeof(buf), fp) != NULL)
3400 get_sockstat_line(buf, s);
3401 fclose(fp);
3402
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003403 if ((fp = net_sockstat6_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003404 return 0;
3405 while(fgets(buf, sizeof(buf), fp) != NULL)
3406 get_sockstat_line(buf, s);
3407 fclose(fp);
3408
3409 return 0;
3410}
3411
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003412static int print_summary(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003413{
Vadim Kochan055840f2015-02-13 22:13:58 +02003414 struct ssummary s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003415 struct snmpstat sn;
3416
3417 if (get_sockstat(&s) < 0)
3418 perror("ss: get_sockstat");
3419 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
3420 perror("ss: get_snmpstat");
3421
Bryton Leea221d622015-02-12 14:16:04 +08003422 get_slabstat(&slabstat);
3423
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003424 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
3425
3426 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
3427 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
3428 sn.tcp_estab,
3429 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
3430 s.tcp_orphans,
3431 slabstat.tcp_syns,
3432 s.tcp_tws, slabstat.tcp_tws,
3433 slabstat.tcp_ports
3434 );
3435
3436 printf("\n");
3437 printf("Transport Total IP IPv6\n");
3438 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
3439 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
3440 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
3441 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003442 printf("INET %-9d %-9d %-9d\n",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003443 s.raw4+s.udp4+s.tcp4_hashed+
3444 s.raw6+s.udp6+s.tcp6_hashed,
3445 s.raw4+s.udp4+s.tcp4_hashed,
3446 s.raw6+s.udp6+s.tcp6_hashed);
3447 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
3448
3449 printf("\n");
3450
3451 return 0;
3452}
3453
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003454static void _usage(FILE *dest)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003455{
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003456 fprintf(dest,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003457"Usage: ss [ OPTIONS ]\n"
3458" ss [ OPTIONS ] [ FILTER ]\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003459" -h, --help this message\n"
3460" -V, --version output version information\n"
3461" -n, --numeric don't resolve service names\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003462" -r, --resolve resolve host names\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003463" -a, --all display all sockets\n"
3464" -l, --listening display listening sockets\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003465" -o, --options show timer information\n"
3466" -e, --extended show detailed socket information\n"
3467" -m, --memory show socket memory usage\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003468" -p, --processes show process using socket\n"
3469" -i, --info show internal TCP information\n"
3470" -s, --summary show socket usage summary\n"
Rami Rosenb0f01cf2013-05-24 12:42:40 +03003471" -b, --bpf show bpf filter socket information\n"
Craig Gallek6885e3b2015-06-17 11:14:48 -04003472" -E, --events continually display sockets as they are destroyed\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003473" -Z, --context display process SELinux security contexts\n"
3474" -z, --contexts display process and socket SELinux security contexts\n"
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003475" -N, --net switch to the specified network namespace name\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003476"\n"
3477" -4, --ipv4 display only IP version 4 sockets\n"
3478" -6, --ipv6 display only IP version 6 sockets\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003479" -0, --packet display PACKET sockets\n"
3480" -t, --tcp display only TCP sockets\n"
3481" -u, --udp display only UDP sockets\n"
3482" -d, --dccp display only DCCP sockets\n"
3483" -w, --raw display only RAW sockets\n"
3484" -x, --unix display only Unix domain sockets\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003485" -f, --family=FAMILY display sockets of type FAMILY\n"
3486"\n"
Petr Sabata583de142011-10-06 14:45:32 +02003487" -A, --query=QUERY, --socket=QUERY\n"
Masatake YAMATO56dee732014-01-08 20:13:48 +09003488" QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003489"\n"
Petr Sabata583de142011-10-06 14:45:32 +02003490" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003491" -F, --filter=FILE read filter information from FILE\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003492" FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
3493" STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
3494" TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n"
3495" connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3496" synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3497" bucket := {syn-recv|time-wait}\n"
3498" big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003499 );
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003500}
3501
3502static void help(void) __attribute__((noreturn));
3503static void help(void)
3504{
3505 _usage(stdout);
3506 exit(0);
3507}
3508
3509static void usage(void) __attribute__((noreturn));
3510static void usage(void)
3511{
3512 _usage(stderr);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003513 exit(-1);
3514}
3515
3516
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003517static int scan_state(const char *state)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003518{
3519 int i;
3520 if (strcasecmp(state, "close") == 0 ||
3521 strcasecmp(state, "closed") == 0)
3522 return (1<<SS_CLOSE);
3523 if (strcasecmp(state, "syn-rcv") == 0)
3524 return (1<<SS_SYN_RECV);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003525 if (strcasecmp(state, "established") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003526 return (1<<SS_ESTABLISHED);
3527 if (strcasecmp(state, "all") == 0)
3528 return SS_ALL;
3529 if (strcasecmp(state, "connected") == 0)
3530 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003531 if (strcasecmp(state, "synchronized") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003532 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
3533 if (strcasecmp(state, "bucket") == 0)
3534 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
3535 if (strcasecmp(state, "big") == 0)
3536 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
3537 for (i=0; i<SS_MAX; i++) {
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003538 if (strcasecmp(state, sstate_namel[i]) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003539 return (1<<i);
3540 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003541
3542 fprintf(stderr, "ss: wrong state name: %s\n", state);
3543 exit(-1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003544}
3545
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003546static const struct option long_opts[] = {
3547 { "numeric", 0, 0, 'n' },
3548 { "resolve", 0, 0, 'r' },
3549 { "options", 0, 0, 'o' },
3550 { "extended", 0, 0, 'e' },
3551 { "memory", 0, 0, 'm' },
3552 { "info", 0, 0, 'i' },
3553 { "processes", 0, 0, 'p' },
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003554 { "bpf", 0, 0, 'b' },
Craig Gallek6885e3b2015-06-17 11:14:48 -04003555 { "events", 0, 0, 'E' },
shemminger351efcd2005-09-01 19:21:50 +00003556 { "dccp", 0, 0, 'd' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003557 { "tcp", 0, 0, 't' },
3558 { "udp", 0, 0, 'u' },
3559 { "raw", 0, 0, 'w' },
3560 { "unix", 0, 0, 'x' },
3561 { "all", 0, 0, 'a' },
3562 { "listening", 0, 0, 'l' },
3563 { "ipv4", 0, 0, '4' },
3564 { "ipv6", 0, 0, '6' },
3565 { "packet", 0, 0, '0' },
3566 { "family", 1, 0, 'f' },
3567 { "socket", 1, 0, 'A' },
Petr Sabata583de142011-10-06 14:45:32 +02003568 { "query", 1, 0, 'A' },
osdl.net!shemmingerc3f346b2005-01-18 22:31:42 +00003569 { "summary", 0, 0, 's' },
Petr Sabata583de142011-10-06 14:45:32 +02003570 { "diag", 1, 0, 'D' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003571 { "filter", 1, 0, 'F' },
3572 { "version", 0, 0, 'V' },
3573 { "help", 0, 0, 'h' },
Richard Haines116ac922014-03-07 10:36:52 +00003574 { "context", 0, 0, 'Z' },
3575 { "contexts", 0, 0, 'z' },
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003576 { "net", 1, 0, 'N' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003577 { 0 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003578
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003579};
3580
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003581int main(int argc, char *argv[])
3582{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003583 int saw_states = 0;
3584 int saw_query = 0;
3585 int do_summary = 0;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00003586 const char *dump_tcpdiag = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003587 FILE *filter_fp = NULL;
3588 int ch;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003589 int state_filter = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003590
Craig Gallek6885e3b2015-06-17 11:14:48 -04003591 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:",
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003592 long_opts, NULL)) != EOF) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003593 switch(ch) {
3594 case 'n':
3595 resolve_services = 0;
3596 break;
3597 case 'r':
3598 resolve_hosts = 1;
3599 break;
3600 case 'o':
3601 show_options = 1;
3602 break;
3603 case 'e':
3604 show_options = 1;
3605 show_details++;
3606 break;
3607 case 'm':
3608 show_mem = 1;
3609 break;
3610 case 'i':
3611 show_tcpinfo = 1;
3612 break;
3613 case 'p':
3614 show_users++;
Steve Finkfbc0f872010-06-09 11:42:38 -07003615 user_ent_hash_build();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003616 break;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003617 case 'b':
3618 show_options = 1;
3619 show_bpf++;
3620 break;
Craig Gallek6885e3b2015-06-17 11:14:48 -04003621 case 'E':
3622 follow_events = 1;
3623 break;
shemminger351efcd2005-09-01 19:21:50 +00003624 case 'd':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003625 filter_db_set(&current_filter, DCCP_DB);
shemminger351efcd2005-09-01 19:21:50 +00003626 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003627 case 't':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003628 filter_db_set(&current_filter, TCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003629 break;
3630 case 'u':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003631 filter_db_set(&current_filter, UDP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003632 break;
3633 case 'w':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003634 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003635 break;
3636 case 'x':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003637 filter_af_set(&current_filter, AF_UNIX);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003638 break;
3639 case 'a':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003640 state_filter = SS_ALL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003641 break;
3642 case 'l':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003643 state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003644 break;
3645 case '4':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003646 filter_af_set(&current_filter, AF_INET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003647 break;
3648 case '6':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003649 filter_af_set(&current_filter, AF_INET6);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003650 break;
3651 case '0':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003652 filter_af_set(&current_filter, AF_PACKET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003653 break;
3654 case 'f':
3655 if (strcmp(optarg, "inet") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003656 filter_af_set(&current_filter, AF_INET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003657 else if (strcmp(optarg, "inet6") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003658 filter_af_set(&current_filter, AF_INET6);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003659 else if (strcmp(optarg, "link") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003660 filter_af_set(&current_filter, AF_PACKET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003661 else if (strcmp(optarg, "unix") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003662 filter_af_set(&current_filter, AF_UNIX);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003663 else if (strcmp(optarg, "netlink") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003664 filter_af_set(&current_filter, AF_NETLINK);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003665 else if (strcmp(optarg, "help") == 0)
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003666 help();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003667 else {
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003668 fprintf(stderr, "ss: \"%s\" is invalid family\n",
3669 optarg);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003670 usage();
3671 }
3672 break;
3673 case 'A':
3674 {
3675 char *p, *p1;
3676 if (!saw_query) {
3677 current_filter.dbs = 0;
Phil Sutter7f9dddb2015-08-07 15:31:27 +02003678 state_filter = state_filter ?
3679 state_filter : SS_CONN;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003680 saw_query = 1;
3681 do_default = 0;
3682 }
3683 p = p1 = optarg;
3684 do {
3685 if ((p1 = strchr(p, ',')) != NULL)
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003686 *p1 = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003687 if (strcmp(p, "all") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003688 filter_default_dbs(&current_filter);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003689 } else if (strcmp(p, "inet") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003690 filter_db_set(&current_filter, UDP_DB);
3691 filter_db_set(&current_filter, DCCP_DB);
3692 filter_db_set(&current_filter, TCP_DB);
3693 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003694 } else if (strcmp(p, "udp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003695 filter_db_set(&current_filter, UDP_DB);
shemminger351efcd2005-09-01 19:21:50 +00003696 } else if (strcmp(p, "dccp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003697 filter_db_set(&current_filter, DCCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003698 } else if (strcmp(p, "tcp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003699 filter_db_set(&current_filter, TCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003700 } else if (strcmp(p, "raw") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003701 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003702 } else if (strcmp(p, "unix") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003703 filter_db_set(&current_filter, UNIX_ST_DB);
3704 filter_db_set(&current_filter, UNIX_DG_DB);
3705 filter_db_set(&current_filter, UNIX_SQ_DB);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003706 } else if (strcasecmp(p, "unix_stream") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003707 strcmp(p, "u_str") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003708 filter_db_set(&current_filter, UNIX_ST_DB);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003709 } else if (strcasecmp(p, "unix_dgram") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003710 strcmp(p, "u_dgr") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003711 filter_db_set(&current_filter, UNIX_DG_DB);
Masatake YAMATO30b669d2014-01-08 20:13:46 +09003712 } else if (strcasecmp(p, "unix_seqpacket") == 0 ||
3713 strcmp(p, "u_seq") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003714 filter_db_set(&current_filter, UNIX_SQ_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003715 } else if (strcmp(p, "packet") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003716 filter_db_set(&current_filter, PACKET_R_DB);
3717 filter_db_set(&current_filter, PACKET_DG_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003718 } else if (strcmp(p, "packet_raw") == 0 ||
3719 strcmp(p, "p_raw") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003720 filter_db_set(&current_filter, PACKET_R_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003721 } else if (strcmp(p, "packet_dgram") == 0 ||
3722 strcmp(p, "p_dgr") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003723 filter_db_set(&current_filter, PACKET_DG_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003724 } else if (strcmp(p, "netlink") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003725 filter_db_set(&current_filter, NETLINK_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003726 } else {
3727 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
3728 usage();
3729 }
3730 p = p1 + 1;
3731 } while (p1);
3732 break;
3733 }
3734 case 's':
3735 do_summary = 1;
3736 break;
3737 case 'D':
3738 dump_tcpdiag = optarg;
3739 break;
3740 case 'F':
3741 if (filter_fp) {
3742 fprintf(stderr, "More than one filter file\n");
3743 exit(-1);
3744 }
3745 if (optarg[0] == '-')
3746 filter_fp = stdin;
3747 else
3748 filter_fp = fopen(optarg, "r");
3749 if (!filter_fp) {
3750 perror("fopen filter file");
3751 exit(-1);
3752 }
3753 break;
3754 case 'v':
3755 case 'V':
3756 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
3757 exit(0);
Richard Haines116ac922014-03-07 10:36:52 +00003758 case 'z':
3759 show_sock_ctx++;
3760 case 'Z':
3761 if (is_selinux_enabled() <= 0) {
3762 fprintf(stderr, "ss: SELinux is not enabled.\n");
3763 exit(1);
3764 }
3765 show_proc_ctx++;
3766 user_ent_hash_build();
3767 break;
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003768 case 'N':
3769 if (netns_switch(optarg))
3770 exit(1);
3771 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003772 case 'h':
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003773 help();
Phil Sutterf73105a2015-10-15 21:01:16 +02003774 case '?':
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003775 default:
3776 usage();
3777 }
3778 }
3779
3780 argc -= optind;
3781 argv += optind;
3782
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003783 if (do_summary) {
3784 print_summary();
3785 if (do_default && argc == 0)
3786 exit(0);
3787 }
3788
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003789 while (argc > 0) {
3790 if (strcmp(*argv, "state") == 0) {
3791 NEXT_ARG();
3792 if (!saw_states)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003793 state_filter = 0;
3794 state_filter |= scan_state(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003795 saw_states = 1;
3796 } else if (strcmp(*argv, "exclude") == 0 ||
3797 strcmp(*argv, "excl") == 0) {
3798 NEXT_ARG();
3799 if (!saw_states)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003800 state_filter = SS_ALL;
3801 state_filter &= ~scan_state(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003802 saw_states = 1;
3803 } else {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003804 break;
3805 }
3806 argc--; argv++;
3807 }
3808
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003809 if (do_default) {
3810 state_filter = state_filter ? state_filter : SS_CONN;
3811 filter_default_dbs(&current_filter);
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003812 }
3813
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003814 filter_states_set(&current_filter, state_filter);
3815 filter_merge_defaults(&current_filter);
3816
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003817 if (resolve_services && resolve_hosts &&
3818 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
3819 init_service_resolver();
3820
3821
3822 if (current_filter.dbs == 0) {
3823 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
3824 exit(0);
3825 }
3826 if (current_filter.families == 0) {
3827 fprintf(stderr, "ss: no families to show with such filter.\n");
3828 exit(0);
3829 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003830 if (current_filter.states == 0) {
3831 fprintf(stderr, "ss: no socket states to show with such filter.\n");
3832 exit(0);
3833 }
3834
3835 if (dump_tcpdiag) {
3836 FILE *dump_fp = stdout;
3837 if (!(current_filter.dbs & (1<<TCP_DB))) {
3838 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
3839 exit(0);
3840 }
3841 if (dump_tcpdiag[0] != '-') {
3842 dump_fp = fopen(dump_tcpdiag, "w");
3843 if (!dump_tcpdiag) {
3844 perror("fopen dump file");
3845 exit(-1);
3846 }
3847 }
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00003848 inet_show_netlink(&current_filter, dump_fp, IPPROTO_TCP);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003849 fflush(dump_fp);
3850 exit(0);
3851 }
3852
Vadim Kochan1527a172015-02-13 13:01:08 +02003853 if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
3854 usage();
3855
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003856 netid_width = 0;
3857 if (current_filter.dbs&(current_filter.dbs-1))
3858 netid_width = 5;
3859
3860 state_width = 0;
3861 if (current_filter.states&(current_filter.states-1))
3862 state_width = 10;
3863
3864 screen_width = 80;
3865 if (isatty(STDOUT_FILENO)) {
3866 struct winsize w;
3867
3868 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
3869 if (w.ws_col > 0)
3870 screen_width = w.ws_col;
3871 }
3872 }
3873
3874 addrp_width = screen_width;
3875 addrp_width -= netid_width+1;
3876 addrp_width -= state_width+1;
3877 addrp_width -= 14;
3878
3879 if (addrp_width&1) {
3880 if (netid_width)
3881 netid_width++;
3882 else if (state_width)
3883 state_width++;
3884 }
3885
3886 addrp_width /= 2;
3887 addrp_width--;
3888
3889 serv_width = resolve_services ? 7 : 5;
3890
3891 if (addrp_width < 15+serv_width+1)
3892 addrp_width = 15+serv_width+1;
3893
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003894 addr_width = addrp_width - serv_width - 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003895
3896 if (netid_width)
3897 printf("%-*s ", netid_width, "Netid");
3898 if (state_width)
3899 printf("%-*s ", state_width, "State");
3900 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
3901
vadimkd68e00f2014-12-05 19:19:11 +02003902 /* Make enough space for the local/remote port field */
3903 addr_width -= 13;
3904 serv_width += 13;
vadimk2dc85482014-10-18 20:46:29 +03003905
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003906 printf("%*s:%-*s %*s:%-*s\n",
3907 addr_width, "Local Address", serv_width, "Port",
vadimkd68e00f2014-12-05 19:19:11 +02003908 addr_width, "Peer Address", serv_width, "Port");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003909
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003910 fflush(stdout);
3911
Craig Gallek6885e3b2015-06-17 11:14:48 -04003912 if (follow_events)
3913 exit(handle_follow_request(&current_filter));
3914
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003915 if (current_filter.dbs & (1<<NETLINK_DB))
3916 netlink_show(&current_filter);
3917 if (current_filter.dbs & PACKET_DBM)
3918 packet_show(&current_filter);
3919 if (current_filter.dbs & UNIX_DBM)
3920 unix_show(&current_filter);
3921 if (current_filter.dbs & (1<<RAW_DB))
3922 raw_show(&current_filter);
3923 if (current_filter.dbs & (1<<UDP_DB))
3924 udp_show(&current_filter);
3925 if (current_filter.dbs & (1<<TCP_DB))
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00003926 tcp_show(&current_filter, IPPROTO_TCP);
shemminger351efcd2005-09-01 19:21:50 +00003927 if (current_filter.dbs & (1<<DCCP_DB))
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00003928 tcp_show(&current_filter, IPPROTO_DCCP);
Richard Haines116ac922014-03-07 10:36:52 +00003929
3930 if (show_users || show_proc_ctx || show_sock_ctx)
3931 user_ent_destroy();
3932
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003933 return 0;
3934}