blob: 38205b0e8c288bf5caa6b9233bf1e56c0fafd16b [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
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070089int resolve_hosts;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000090int resolve_services = 1;
91int preferred_family = AF_UNSPEC;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070092int show_options;
93int show_details;
94int show_users;
95int show_mem;
96int show_tcpinfo;
97int show_bpf;
98int show_proc_ctx;
99int show_sock_ctx;
David Ahern7a4559f2016-07-15 15:45:39 -0700100int show_header = 1;
Richard Haines116ac922014-03-07 10:36:52 +0000101/* If show_users & show_proc_ctx only do user_ent_hash_build() once */
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700102int user_ent_hash_build_init;
103int follow_events;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104
105int netid_width;
106int state_width;
107int addrp_width;
108int addr_width;
109int serv_width;
110int screen_width;
111
112static const char *TCP_PROTO = "tcp";
113static const char *UDP_PROTO = "udp";
114static const char *RAW_PROTO = "raw";
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700115static const char *dg_proto;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700117enum {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118 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
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700157struct filter {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158 int dbs;
159 int states;
160 int families;
161 struct ssfilter *f;
Lorenzo Colittifb2594c2016-01-08 17:32:37 +0900162 bool kill;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163};
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)
Phil Sutter9d320e12016-04-13 22:07:04 +0200271 f->states = states;
Vadim Kochan57ff5a12015-04-30 07:30:24 +0300272}
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) {
Phil Sutterd572ed42015-11-28 01:00:04 +0100531 if (fscanf(fp, "%*d (%[^)])", p) < 1)
532 ; /* ignore */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000533 fclose(fp);
534 }
535 }
Richard Haines116ac922014-03-07 10:36:52 +0000536 user_ent_add(ino, p, pid, fd,
537 pid_context, sock_context);
538 free(sock_context);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000539 }
Richard Haines116ac922014-03-07 10:36:52 +0000540 free(pid_context);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000541 closedir(dir1);
542 }
543 closedir(dir);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000544}
545
Richard Haines116ac922014-03-07 10:36:52 +0000546enum entry_types {
547 USERS,
548 PROC_CTX,
549 PROC_SOCK_CTX
550};
551
552#define ENTRY_BUF_SIZE 512
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700553static int find_entry(unsigned int ino, char **buf, int type)
Steve Finkfbc0f872010-06-09 11:42:38 -0700554{
555 struct user_ent *p;
556 int cnt = 0;
557 char *ptr;
Phil Sutter532ca402015-08-06 14:24:33 +0200558 char *new_buf;
Richard Haines116ac922014-03-07 10:36:52 +0000559 int len, new_buf_len;
560 int buf_used = 0;
561 int buf_len = 0;
Steve Finkfbc0f872010-06-09 11:42:38 -0700562
563 if (!ino)
564 return 0;
565
566 p = user_ent_hash[user_ent_hashfn(ino)];
Richard Haines116ac922014-03-07 10:36:52 +0000567 ptr = *buf = NULL;
Steve Finkfbc0f872010-06-09 11:42:38 -0700568 while (p) {
569 if (p->ino != ino)
570 goto next;
571
Richard Haines116ac922014-03-07 10:36:52 +0000572 while (1) {
573 ptr = *buf + buf_used;
574 switch (type) {
575 case USERS:
576 len = snprintf(ptr, buf_len - buf_used,
577 "(\"%s\",pid=%d,fd=%d),",
578 p->process, p->pid, p->fd);
579 break;
580 case PROC_CTX:
581 len = snprintf(ptr, buf_len - buf_used,
582 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),",
583 p->process, p->pid,
584 p->process_ctx, p->fd);
585 break;
586 case PROC_SOCK_CTX:
587 len = snprintf(ptr, buf_len - buf_used,
588 "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),",
589 p->process, p->pid,
590 p->process_ctx, p->fd,
591 p->socket_ctx);
592 break;
593 default:
594 fprintf(stderr, "ss: invalid type: %d\n", type);
595 abort();
596 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700597
Richard Haines116ac922014-03-07 10:36:52 +0000598 if (len < 0 || len >= buf_len - buf_used) {
599 new_buf_len = buf_len + ENTRY_BUF_SIZE;
Phil Sutter532ca402015-08-06 14:24:33 +0200600 new_buf = realloc(*buf, new_buf_len);
Richard Haines116ac922014-03-07 10:36:52 +0000601 if (!new_buf) {
602 fprintf(stderr, "ss: failed to malloc buffer\n");
603 abort();
604 }
Phil Sutter532ca402015-08-06 14:24:33 +0200605 *buf = new_buf;
Richard Haines116ac922014-03-07 10:36:52 +0000606 buf_len = new_buf_len;
607 continue;
608 } else {
609 buf_used += len;
610 break;
611 }
612 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700613 cnt++;
Richard Haines116ac922014-03-07 10:36:52 +0000614next:
Steve Finkfbc0f872010-06-09 11:42:38 -0700615 p = p->next;
616 }
Richard Haines116ac922014-03-07 10:36:52 +0000617 if (buf_used) {
618 ptr = *buf + buf_used;
Steve Finkfbc0f872010-06-09 11:42:38 -0700619 ptr[-1] = '\0';
Richard Haines116ac922014-03-07 10:36:52 +0000620 }
Steve Finkfbc0f872010-06-09 11:42:38 -0700621 return cnt;
622}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000623
624/* Get stats from slab */
625
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700626struct slabstat {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000627 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 Hemmingeracd1e432016-03-21 11:56:36 -0700636static const char *slabstat_ids[] = {
637
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000638 "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
Phil Sutterd572ed42015-11-28 01:00:04 +0100663 if (!fgets(buf, sizeof(buf), fp)) {
664 fclose(fp);
665 return -1;
666 }
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700667 while (fgets(buf, sizeof(buf), fp) != NULL) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000668 int i;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700669
Phil Sutter62000e52016-06-28 18:42:15 +0200670 for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000671 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
672 sscanf(buf, "%*s%d", ((int *)s) + i);
673 cnt--;
674 break;
675 }
676 }
677 if (cnt <= 0)
678 break;
679 }
680
Bryton Leea221d622015-02-12 14:16:04 +0800681 slabstat_valid = 1;
682
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000683 fclose(fp);
684 return 0;
685}
686
Eric Dumazet2e7e8052015-03-10 09:56:47 -0700687static unsigned long long cookie_sk_get(const uint32_t *cookie)
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200688{
Eric Dumazet2e7e8052015-03-10 09:56:47 -0700689 return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200690}
691
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000692static const char *sstate_name[] = {
693 "UNKNOWN",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700694 [SS_ESTABLISHED] = "ESTAB",
695 [SS_SYN_SENT] = "SYN-SENT",
696 [SS_SYN_RECV] = "SYN-RECV",
697 [SS_FIN_WAIT1] = "FIN-WAIT-1",
698 [SS_FIN_WAIT2] = "FIN-WAIT-2",
699 [SS_TIME_WAIT] = "TIME-WAIT",
700 [SS_CLOSE] = "UNCONN",
701 [SS_CLOSE_WAIT] = "CLOSE-WAIT",
702 [SS_LAST_ACK] = "LAST-ACK",
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700703 [SS_LISTEN] = "LISTEN",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700704 [SS_CLOSING] = "CLOSING",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000705};
706
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000707static const char *sstate_namel[] = {
708 "UNKNOWN",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700709 [SS_ESTABLISHED] = "established",
710 [SS_SYN_SENT] = "syn-sent",
711 [SS_SYN_RECV] = "syn-recv",
712 [SS_FIN_WAIT1] = "fin-wait-1",
713 [SS_FIN_WAIT2] = "fin-wait-2",
714 [SS_TIME_WAIT] = "time-wait",
715 [SS_CLOSE] = "unconnected",
716 [SS_CLOSE_WAIT] = "close-wait",
717 [SS_LAST_ACK] = "last-ack",
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700718 [SS_LISTEN] = "listening",
Eric Dumazet9cb1ecc2013-05-03 20:48:00 -0700719 [SS_CLOSING] = "closing",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000720};
721
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700722struct sockstat {
Vadim Kochanec4d0d82015-02-13 22:14:00 +0200723 struct sockstat *next;
724 unsigned int type;
Vadim Kochan89f634f2015-02-13 22:13:59 +0200725 uint16_t prot;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200726 inet_prefix local;
727 inet_prefix remote;
728 int lport;
729 int rport;
730 int state;
731 int rq, wq;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700732 unsigned int ino;
733 unsigned int uid;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200734 int refcnt;
735 unsigned int iface;
736 unsigned long long sk;
Vadim Kochan99bb68f2015-07-21 10:53:19 -0700737 char *name;
738 char *peer_name;
Vadim Kochan055840f2015-02-13 22:13:58 +0200739};
740
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700741struct dctcpstat {
Vadim Kochan055840f2015-02-13 22:13:58 +0200742 unsigned int ce_state;
743 unsigned int alpha;
744 unsigned int ab_ecn;
745 unsigned int ab_tot;
746 bool enabled;
747};
748
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700749struct tcpstat {
Vadim Kochan055840f2015-02-13 22:13:58 +0200750 struct sockstat ss;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200751 int timer;
752 int timeout;
753 int probes;
Eric Dumazetd2055ea2015-05-29 04:04:05 -0700754 char cong_alg[16];
Vadim Kochan8250bc92015-01-20 16:14:24 +0200755 double rto, ato, rtt, rttvar;
756 int qack, cwnd, ssthresh, backoff;
757 double send_bps;
758 int snd_wscale;
759 int rcv_wscale;
760 int mss;
761 unsigned int lastsnd;
762 unsigned int lastrcv;
763 unsigned int lastack;
764 double pacing_rate;
765 double pacing_rate_max;
Eric Dumazet1a4dda72015-05-11 10:03:49 -0700766 unsigned long long bytes_acked;
767 unsigned long long bytes_received;
Craig Gallekecb435e2015-05-26 14:54:41 -0400768 unsigned int segs_out;
769 unsigned int segs_in;
Martin KaFai Lau414aeec2016-06-17 17:38:53 -0700770 unsigned int data_segs_out;
771 unsigned int data_segs_in;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200772 unsigned int unacked;
773 unsigned int retrans;
774 unsigned int retrans_total;
775 unsigned int lost;
776 unsigned int sacked;
777 unsigned int fackets;
778 unsigned int reordering;
Stephen Hemminger9e99e492016-02-17 17:43:59 -0800779 unsigned int not_sent;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200780 double rcv_rtt;
Stephen Hemminger9e99e492016-02-17 17:43:59 -0800781 double min_rtt;
Vadim Kochan8250bc92015-01-20 16:14:24 +0200782 int rcv_space;
783 bool has_ts_opt;
784 bool has_sack_opt;
785 bool has_ecn_opt;
786 bool has_ecnseen_opt;
787 bool has_fastopen_opt;
788 bool has_wscale_opt;
789 struct dctcpstat *dctcp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000790};
791
Vadim Kochan2d791bc2015-02-13 22:14:01 +0200792static void sock_state_print(struct sockstat *s, const char *sock_name)
793{
794 if (netid_width)
795 printf("%-*s ", netid_width, sock_name);
796 if (state_width)
797 printf("%-*s ", state_width, sstate_name[s->state]);
798
799 printf("%-6d %-6d ", s->rq, s->wq);
800}
801
Vadim Kochanf1b39e12015-02-13 22:14:02 +0200802static void sock_details_print(struct sockstat *s)
803{
804 if (s->uid)
805 printf(" uid:%u", s->uid);
806
807 printf(" ino:%u", s->ino);
808 printf(" sk:%llx", s->sk);
809}
810
Vadim Kochanb217df12015-02-13 22:14:03 +0200811static void sock_addr_print_width(int addr_len, const char *addr, char *delim,
812 int port_len, const char *port, const char *ifname)
813{
814 if (ifname) {
815 printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim,
816 port_len, port);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700817 } else {
Vadim Kochanb217df12015-02-13 22:14:03 +0200818 printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port);
819 }
820}
821
822static void sock_addr_print(const char *addr, char *delim, const char *port,
823 const char *ifname)
824{
825 sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname);
826}
827
osdl.org!shemminger7d105b52004-06-02 20:22:08 +0000828static const char *tmr_name[] = {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000829 "off",
830 "on",
831 "keepalive",
832 "timewait",
833 "persist",
834 "unknown"
835};
836
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800837static const char *print_ms_timer(int timeout)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000838{
839 static char buf[64];
840 int secs, msecs, minutes;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700841
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000842 if (timeout < 0)
843 timeout = 0;
844 secs = timeout/1000;
845 minutes = secs/60;
846 secs = secs%60;
847 msecs = timeout%1000;
848 buf[0] = 0;
849 if (minutes) {
850 msecs = 0;
851 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
852 if (minutes > 9)
853 secs = 0;
854 }
855 if (secs) {
856 if (secs > 9)
857 msecs = 0;
858 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
859 }
860 if (msecs)
861 sprintf(buf+strlen(buf), "%03dms", msecs);
862 return buf;
Stephen Hemmingere7113c62007-07-10 18:26:54 -0700863}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000864
Eric Dumazet22588a02015-05-29 04:45:48 -0700865struct scache {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000866 struct scache *next;
867 int port;
868 char *name;
869 const char *proto;
870};
871
872struct scache *rlist;
873
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800874static void init_service_resolver(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000875{
876 char buf[128];
877 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
Phil Sutter596307e2015-11-28 01:00:02 +0100878
879 if (!fp)
880 return;
881
882 if (!fgets(buf, sizeof(buf), fp)) {
Thomas Jarosch2bcc3c12011-10-03 05:22:27 +0000883 pclose(fp);
Phil Sutter596307e2015-11-28 01:00:02 +0100884 return;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000885 }
Phil Sutter596307e2015-11-28 01:00:02 +0100886 while (fgets(buf, sizeof(buf), fp) != NULL) {
887 unsigned int progn, port;
888 char proto[128], prog[128] = "rpc.";
889 struct scache *c;
890
891 if (sscanf(buf, "%u %*d %s %u %s",
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700892 &progn, proto, &port, prog+4) != 4)
Phil Sutter596307e2015-11-28 01:00:02 +0100893 continue;
894
895 if (!(c = malloc(sizeof(*c))))
896 continue;
897
898 c->port = port;
899 c->name = strdup(prog);
900 if (strcmp(proto, TCP_PROTO) == 0)
901 c->proto = TCP_PROTO;
902 else if (strcmp(proto, UDP_PROTO) == 0)
903 c->proto = UDP_PROTO;
904 else
905 c->proto = NULL;
906 c->next = rlist;
907 rlist = c;
908 }
909 pclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000910}
911
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000912/* Even do not try default linux ephemeral port ranges:
913 * default /etc/services contains so much of useless crap
914 * wouldbe "allocated" to this area that resolution
915 * is really harmful. I shrug each time when seeing
916 * "socks" or "cfinger" in dumps.
917 */
918static int is_ephemeral(int port)
919{
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700920 static int min = 0, max;
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000921
Phil Sutterc29d3792015-11-28 01:00:03 +0100922 if (!min) {
923 FILE *f = ephemeral_ports_open();
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700924
Phil Sutterc29d3792015-11-28 01:00:03 +0100925 if (!f || fscanf(f, "%d %d", &min, &max) < 2) {
926 min = 1024;
927 max = 4999;
928 }
929 if (f)
930 fclose(f);
931 }
932 return port >= min && port <= max;
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000933}
934
935
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800936static const char *__resolve_service(int port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000937{
938 struct scache *c;
939
940 for (c = rlist; c; c = c->next) {
941 if (c->port == port && c->proto == dg_proto)
942 return c->name;
943 }
944
osdl.org!shemmingerab611592004-06-09 21:30:13 +0000945 if (!is_ephemeral(port)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000946 static int notfirst;
947 struct servent *se;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700948
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000949 if (!notfirst) {
950 setservent(1);
951 notfirst = 1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800952 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000953 se = getservbyport(htons(port), dg_proto);
954 if (se)
955 return se->s_name;
956 }
957
958 return NULL;
959}
960
Eric Dumazet22588a02015-05-29 04:45:48 -0700961#define SCACHE_BUCKETS 1024
962static struct scache *cache_htab[SCACHE_BUCKETS];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000963
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800964static const char *resolve_service(int port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000965{
966 static char buf[128];
Eric Dumazet22588a02015-05-29 04:45:48 -0700967 struct scache *c;
968 const char *res;
969 int hash;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000970
971 if (port == 0) {
972 buf[0] = '*';
973 buf[1] = 0;
974 return buf;
975 }
976
Eric Dumazet22588a02015-05-29 04:45:48 -0700977 if (!resolve_services)
978 goto do_numeric;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000979
Eric Dumazet22588a02015-05-29 04:45:48 -0700980 if (dg_proto == RAW_PROTO)
981 return inet_proto_n2a(port, buf, sizeof(buf));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000982
Eric Dumazet22588a02015-05-29 04:45:48 -0700983
984 hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS;
985
986 for (c = cache_htab[hash]; c; c = c->next) {
987 if (c->port == port && c->proto == dg_proto)
988 goto do_cache;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000989 }
990
Eric Dumazet22588a02015-05-29 04:45:48 -0700991 c = malloc(sizeof(*c));
992 if (!c)
993 goto do_numeric;
994 res = __resolve_service(port);
995 c->port = port;
996 c->name = res ? strdup(res) : NULL;
997 c->proto = dg_proto;
998 c->next = cache_htab[hash];
999 cache_htab[hash] = c;
1000
1001do_cache:
1002 if (c->name)
1003 return c->name;
1004
1005do_numeric:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001006 sprintf(buf, "%u", port);
1007 return buf;
1008}
1009
Vadim Kochanb217df12015-02-13 22:14:03 +02001010static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001011{
1012 char buf[1024];
1013 const char *ap = buf;
Vadim Kochanb217df12015-02-13 22:14:03 +02001014 int est_len = addr_width;
1015 const char *ifname = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001016
1017 if (a->family == AF_INET) {
1018 if (a->data[0] == 0) {
1019 buf[0] = '*';
1020 buf[1] = 0;
1021 } else {
Phil Suttera418e452016-03-22 19:35:15 +01001022 ap = format_host(AF_INET, 4, a->data);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001023 }
1024 } else {
Phil Suttera418e452016-03-22 19:35:15 +01001025 ap = format_host(a->family, 16, a->data);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001026 est_len = strlen(ap);
1027 if (est_len <= addr_width)
1028 est_len = addr_width;
1029 else
1030 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
1031 }
FX Le Bail7c8a3cf2014-02-11 10:06:23 +01001032
Vadim Kochanb217df12015-02-13 22:14:03 +02001033 if (ifindex) {
1034 ifname = ll_index_to_name(ifindex);
1035 est_len -= strlen(ifname) + 1; /* +1 for percent char */
Mike Saal4fcfb6b2015-08-26 11:59:17 -04001036 if (est_len < 0)
1037 est_len = 0;
Vadim Kochanb217df12015-02-13 22:14:03 +02001038 }
1039
1040 sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port),
1041 ifname);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001042}
1043
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001044struct aafilter {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001045 inet_prefix addr;
1046 int port;
David Ahern2d293212016-06-27 11:34:25 -07001047 unsigned int iface;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001048 struct aafilter *next;
1049};
1050
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08001051static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
1052 int plen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001053{
1054 if (!inet_addr_match(a, p, plen))
1055 return 0;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001056
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001057 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
1058 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
1059 * sockets. Fair? */
1060 if (p->family == AF_INET && a->family == AF_INET6) {
1061 if (a->data[0] == 0 && a->data[1] == 0 &&
1062 a->data[2] == htonl(0xffff)) {
1063 inet_prefix tmp = *a;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001064
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001065 tmp.data[0] = a->data[3];
1066 return inet_addr_match(&tmp, p, plen);
1067 }
1068 }
1069 return 1;
1070}
1071
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08001072static int unix_match(const inet_prefix *a, const inet_prefix *p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001073{
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001074 char *addr, *pattern;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001075
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001076 memcpy(&addr, a->data, sizeof(addr));
1077 memcpy(&pattern, p->data, sizeof(pattern));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001078 if (pattern == NULL)
1079 return 1;
1080 if (addr == NULL)
1081 addr = "";
1082 return !fnmatch(pattern, addr, 0);
1083}
1084
Vadim Kochan055840f2015-02-13 22:13:58 +02001085static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001086{
1087 switch (f->type) {
1088 case SSF_S_AUTO:
1089 {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001090 if (s->local.family == AF_UNIX) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001091 char *p;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001092
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001093 memcpy(&p, s->local.data, sizeof(p));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001094 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001095 strspn(p+1, "0123456789abcdef") == 5);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001096 }
1097 if (s->local.family == AF_PACKET)
Maciej Żenczykowskibbd303d2015-06-25 02:03:03 -07001098 return s->lport == 0 && s->local.data[0] == 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001099 if (s->local.family == AF_NETLINK)
1100 return s->lport < 0;
1101
Phil Sutterc29d3792015-11-28 01:00:03 +01001102 return is_ephemeral(s->lport);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001103 }
1104 case SSF_DCOND:
1105 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001106 struct aafilter *a = (void *)f->pred;
1107
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001108 if (a->addr.family == AF_UNIX)
1109 return unix_match(&s->remote, &a->addr);
1110 if (a->port != -1 && a->port != s->rport)
1111 return 0;
1112 if (a->addr.bitlen) {
1113 do {
1114 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
1115 return 1;
1116 } while ((a = a->next) != NULL);
1117 return 0;
1118 }
1119 return 1;
1120 }
1121 case SSF_SCOND:
1122 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001123 struct aafilter *a = (void *)f->pred;
1124
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001125 if (a->addr.family == AF_UNIX)
1126 return unix_match(&s->local, &a->addr);
1127 if (a->port != -1 && a->port != s->lport)
1128 return 0;
1129 if (a->addr.bitlen) {
1130 do {
1131 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
1132 return 1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001133 } while ((a = a->next) != NULL);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001134 return 0;
1135 }
1136 return 1;
1137 }
1138 case SSF_D_GE:
1139 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001140 struct aafilter *a = (void *)f->pred;
1141
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001142 return s->rport >= a->port;
1143 }
1144 case SSF_D_LE:
1145 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001146 struct aafilter *a = (void *)f->pred;
1147
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001148 return s->rport <= a->port;
1149 }
1150 case SSF_S_GE:
1151 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001152 struct aafilter *a = (void *)f->pred;
1153
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001154 return s->lport >= a->port;
1155 }
1156 case SSF_S_LE:
1157 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001158 struct aafilter *a = (void *)f->pred;
1159
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001160 return s->lport <= a->port;
1161 }
David Ahern2d293212016-06-27 11:34:25 -07001162 case SSF_DEVCOND:
1163 {
1164 struct aafilter *a = (void *)f->pred;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001165
David Ahern2d293212016-06-27 11:34:25 -07001166 return s->iface == a->iface;
1167 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001168 /* Yup. It is recursion. Sorry. */
1169 case SSF_AND:
1170 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
1171 case SSF_OR:
1172 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
1173 case SSF_NOT:
1174 return !run_ssfilter(f->pred, s);
1175 default:
1176 abort();
1177 }
1178}
1179
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001180/* Relocate external jumps by reloc. */
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001181static void ssfilter_patch(char *a, int len, int reloc)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001182{
1183 while (len > 0) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001184 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a;
1185
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001186 if (op->no == len+4)
1187 op->no += reloc;
1188 len -= op->yes;
1189 a += op->yes;
1190 }
1191 if (len < 0)
1192 abort();
1193}
1194
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001195static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001196{
1197 switch (f->type) {
1198 case SSF_S_AUTO:
1199 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001200 if (!(*bytecode = malloc(4))) abort();
1201 ((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 -07001202 return 4;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001203 }
1204 case SSF_DCOND:
1205 case SSF_SCOND:
1206 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001207 struct aafilter *a = (void *)f->pred;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001208 struct aafilter *b;
1209 char *ptr;
shemminger351efcd2005-09-01 19:21:50 +00001210 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001211 int len = 0;
1212
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001213 for (b = a; b; b = b->next) {
shemminger351efcd2005-09-01 19:21:50 +00001214 len += 4 + sizeof(struct inet_diag_hostcond);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001215 if (a->addr.family == AF_INET6)
1216 len += 16;
1217 else
1218 len += 4;
1219 if (b->next)
1220 len += 4;
1221 }
1222 if (!(ptr = malloc(len))) abort();
1223 *bytecode = ptr;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001224 for (b = a; b; b = b->next) {
shemminger351efcd2005-09-01 19:21:50 +00001225 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001226 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
shemminger351efcd2005-09-01 19:21:50 +00001227 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001228 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001229
shemminger351efcd2005-09-01 19:21:50 +00001230 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001231 cond->family = a->addr.family;
1232 cond->port = a->port;
1233 cond->prefix_len = a->addr.bitlen;
1234 memcpy(cond->addr, a->addr.data, alen);
1235 ptr += oplen;
1236 if (b->next) {
shemminger351efcd2005-09-01 19:21:50 +00001237 op = (struct inet_diag_bc_op *)ptr;
1238 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001239 ptr += 4;
1240 }
1241 }
1242 return ptr - *bytecode;
1243 }
1244 case SSF_D_GE:
1245 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001246 struct aafilter *x = (void *)f->pred;
1247
1248 if (!(*bytecode = malloc(8))) abort();
1249 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
1250 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001251 return 8;
1252 }
1253 case SSF_D_LE:
1254 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001255 struct aafilter *x = (void *)f->pred;
1256
1257 if (!(*bytecode = malloc(8))) abort();
1258 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
1259 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001260 return 8;
1261 }
1262 case SSF_S_GE:
1263 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001264 struct aafilter *x = (void *)f->pred;
1265
1266 if (!(*bytecode = malloc(8))) abort();
1267 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
1268 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001269 return 8;
1270 }
1271 case SSF_S_LE:
1272 {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001273 struct aafilter *x = (void *)f->pred;
1274
1275 if (!(*bytecode = malloc(8))) abort();
1276 ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
1277 ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001278 return 8;
1279 }
1280
1281 case SSF_AND:
1282 {
David Ahern376fb862016-06-27 11:34:24 -07001283 char *a1 = NULL, *a2 = NULL, *a;
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001284 int l1, l2;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001285
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001286 l1 = ssfilter_bytecompile(f->pred, &a1);
1287 l2 = ssfilter_bytecompile(f->post, &a2);
David Ahern376fb862016-06-27 11:34:24 -07001288 if (!l1 || !l2) {
1289 free(a1);
1290 free(a2);
1291 return 0;
1292 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001293 if (!(a = malloc(l1+l2))) abort();
1294 memcpy(a, a1, l1);
1295 memcpy(a+l1, a2, l2);
1296 free(a1); free(a2);
1297 ssfilter_patch(a, l1, l2);
1298 *bytecode = a;
1299 return l1+l2;
1300 }
1301 case SSF_OR:
1302 {
David Ahern376fb862016-06-27 11:34:24 -07001303 char *a1 = NULL, *a2 = NULL, *a;
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001304 int l1, l2;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001305
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001306 l1 = ssfilter_bytecompile(f->pred, &a1);
1307 l2 = ssfilter_bytecompile(f->post, &a2);
David Ahern376fb862016-06-27 11:34:24 -07001308 if (!l1 || !l2) {
1309 free(a1);
1310 free(a2);
1311 return 0;
1312 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001313 if (!(a = malloc(l1+l2+4))) abort();
1314 memcpy(a, a1, l1);
1315 memcpy(a+l1+4, a2, l2);
1316 free(a1); free(a2);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001317 *(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 +00001318 *bytecode = a;
1319 return l1+l2+4;
1320 }
1321 case SSF_NOT:
1322 {
David Ahern376fb862016-06-27 11:34:24 -07001323 char *a1 = NULL, *a;
Andreas Henriksson2a4fa1c2013-11-13 09:46:42 +01001324 int l1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001325
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001326 l1 = ssfilter_bytecompile(f->pred, &a1);
David Ahern376fb862016-06-27 11:34:24 -07001327 if (!l1) {
1328 free(a1);
1329 return 0;
1330 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001331 if (!(a = malloc(l1+4))) abort();
1332 memcpy(a, a1, l1);
1333 free(a1);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001334 *(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 +00001335 *bytecode = a;
1336 return l1+4;
1337 }
David Ahern2d293212016-06-27 11:34:25 -07001338 case SSF_DEVCOND:
1339 {
1340 /* bytecompile for SSF_DEVCOND not supported yet */
1341 return 0;
1342 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001343 default:
1344 abort();
1345 }
1346}
1347
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001348static int remember_he(struct aafilter *a, struct hostent *he)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001349{
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001350 char **ptr = he->h_addr_list;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001351 int cnt = 0;
1352 int len;
1353
1354 if (he->h_addrtype == AF_INET)
1355 len = 4;
1356 else if (he->h_addrtype == AF_INET6)
1357 len = 16;
1358 else
1359 return 0;
1360
1361 while (*ptr) {
1362 struct aafilter *b = a;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001363
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001364 if (a->addr.bitlen) {
1365 if ((b = malloc(sizeof(*b))) == NULL)
1366 return cnt;
1367 *b = *a;
1368 b->next = a->next;
1369 a->next = b;
1370 }
1371 memcpy(b->addr.data, *ptr, len);
1372 b->addr.bytelen = len;
1373 b->addr.bitlen = len*8;
1374 b->addr.family = he->h_addrtype;
1375 ptr++;
1376 cnt++;
1377 }
1378 return cnt;
1379}
1380
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001381static int get_dns_host(struct aafilter *a, const char *addr, int fam)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001382{
1383 static int notfirst;
1384 int cnt = 0;
1385 struct hostent *he;
1386
1387 a->addr.bitlen = 0;
1388 if (!notfirst) {
1389 sethostent(1);
1390 notfirst = 1;
1391 }
1392 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
1393 if (he)
1394 cnt = remember_he(a, he);
1395 if (fam == AF_UNSPEC) {
1396 he = gethostbyname2(addr, AF_INET6);
1397 if (he)
1398 cnt += remember_he(a, he);
1399 }
1400 return !cnt;
1401}
1402
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001403static int xll_initted;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001404
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001405static void xll_init(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001406{
1407 struct rtnl_handle rth;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001408
Stephen Hemmingerd2468da2013-12-20 08:15:02 -08001409 if (rtnl_open(&rth, 0) < 0)
1410 exit(1);
1411
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001412 ll_init_map(&rth);
1413 rtnl_close(&rth);
1414 xll_initted = 1;
1415}
1416
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001417static const char *xll_index_to_name(int index)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001418{
1419 if (!xll_initted)
1420 xll_init();
1421 return ll_index_to_name(index);
1422}
1423
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001424static int xll_name_to_index(const char *dev)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001425{
1426 if (!xll_initted)
1427 xll_init();
1428 return ll_name_to_index(dev);
1429}
1430
David Ahern2d293212016-06-27 11:34:25 -07001431void *parse_devcond(char *name)
1432{
1433 struct aafilter a = { .iface = 0 };
1434 struct aafilter *res;
1435
1436 a.iface = xll_name_to_index(name);
1437 if (a.iface == 0) {
1438 char *end;
David Ahern930d3f22016-07-15 15:41:35 -07001439 unsigned long n;
David Ahern2d293212016-06-27 11:34:25 -07001440
David Ahern930d3f22016-07-15 15:41:35 -07001441 n = strtoul(name, &end, 0);
1442 if (!end || end == name || *end || n > UINT_MAX)
David Ahern2d293212016-06-27 11:34:25 -07001443 return NULL;
David Ahern930d3f22016-07-15 15:41:35 -07001444
1445 a.iface = n;
David Ahern2d293212016-06-27 11:34:25 -07001446 }
1447
1448 res = malloc(sizeof(*res));
1449 *res = a;
1450
1451 return res;
1452}
1453
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001454void *parse_hostcond(char *addr, bool is_port)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001455{
1456 char *port = NULL;
Vadim Kochan1527a172015-02-13 13:01:08 +02001457 struct aafilter a = { .port = -1 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001458 struct aafilter *res;
Vadim Kochan1527a172015-02-13 13:01:08 +02001459 int fam = preferred_family;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001460 struct filter *f = &current_filter;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001461
Vadim Kochan1527a172015-02-13 13:01:08 +02001462 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001463 char *p;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001464
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001465 a.addr.family = AF_UNIX;
1466 if (strncmp(addr, "unix:", 5) == 0)
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001467 addr += 5;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001468 p = strdup(addr);
1469 a.addr.bitlen = 8*strlen(p);
Vadim Kochan99bb68f2015-07-21 10:53:19 -07001470 memcpy(a.addr.data, &p, sizeof(p));
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001471 fam = AF_UNIX;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001472 goto out;
1473 }
1474
Vadim Kochan1527a172015-02-13 13:01:08 +02001475 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001476 a.addr.family = AF_PACKET;
1477 a.addr.bitlen = 0;
1478 if (strncmp(addr, "link:", 5) == 0)
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001479 addr += 5;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001480 port = strchr(addr, ':');
1481 if (port) {
1482 *port = 0;
1483 if (port[1] && strcmp(port+1, "*")) {
1484 if (get_integer(&a.port, port+1, 0)) {
1485 if ((a.port = xll_name_to_index(port+1)) <= 0)
1486 return NULL;
1487 }
1488 }
1489 }
1490 if (addr[0] && strcmp(addr, "*")) {
1491 unsigned short tmp;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001492
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001493 a.addr.bitlen = 32;
1494 if (ll_proto_a2n(&tmp, addr))
1495 return NULL;
1496 a.addr.data[0] = ntohs(tmp);
1497 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001498 fam = AF_PACKET;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001499 goto out;
1500 }
1501
Vadim Kochan1527a172015-02-13 13:01:08 +02001502 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001503 a.addr.family = AF_NETLINK;
1504 a.addr.bitlen = 0;
1505 if (strncmp(addr, "netlink:", 8) == 0)
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001506 addr += 8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001507 port = strchr(addr, ':');
1508 if (port) {
1509 *port = 0;
1510 if (port[1] && strcmp(port+1, "*")) {
1511 if (get_integer(&a.port, port+1, 0)) {
1512 if (strcmp(port+1, "kernel") == 0)
1513 a.port = 0;
1514 else
1515 return NULL;
1516 }
1517 }
1518 }
1519 if (addr[0] && strcmp(addr, "*")) {
1520 a.addr.bitlen = 32;
vadimkb00daf62014-12-06 02:52:19 +02001521 if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
1522 return NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001523 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001524 fam = AF_NETLINK;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001525 goto out;
1526 }
1527
Vadim Kochan1527a172015-02-13 13:01:08 +02001528 if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001529 fam = AF_INET;
Vadim Kochan1527a172015-02-13 13:01:08 +02001530 if (!strncmp(addr, "inet:", 5))
1531 addr += 5;
1532 } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001533 fam = AF_INET6;
Vadim Kochan1527a172015-02-13 13:01:08 +02001534 if (!strncmp(addr, "inet6:", 6))
1535 addr += 6;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001536 }
1537
1538 /* URL-like literal [] */
1539 if (addr[0] == '[') {
1540 addr++;
1541 if ((port = strchr(addr, ']')) == NULL)
1542 return NULL;
1543 *port++ = 0;
1544 } else if (addr[0] == '*') {
1545 port = addr+1;
1546 } else {
1547 port = strrchr(strchr(addr, '/') ? : addr, ':');
1548 }
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001549
1550 if (is_port)
1551 port = addr;
1552
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001553 if (port && *port) {
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001554 if (*port == ':')
1555 *port++ = 0;
1556
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001557 if (*port && *port != '*') {
1558 if (get_integer(&a.port, port, 0)) {
1559 struct servent *se1 = NULL;
1560 struct servent *se2 = NULL;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001561
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001562 if (current_filter.dbs&(1<<UDP_DB))
1563 se1 = getservbyname(port, UDP_PROTO);
1564 if (current_filter.dbs&(1<<TCP_DB))
1565 se2 = getservbyname(port, TCP_PROTO);
1566 if (se1 && se2 && se1->s_port != se2->s_port) {
1567 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1568 return NULL;
1569 }
1570 if (!se1)
1571 se1 = se2;
1572 if (se1) {
1573 a.port = ntohs(se1->s_port);
1574 } else {
1575 struct scache *s;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001576
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001577 for (s = rlist; s; s = s->next) {
1578 if ((s->proto == UDP_PROTO &&
1579 (current_filter.dbs&(1<<UDP_DB))) ||
1580 (s->proto == TCP_PROTO &&
1581 (current_filter.dbs&(1<<TCP_DB)))) {
1582 if (s->name && strcmp(s->name, port) == 0) {
1583 if (a.port > 0 && a.port != s->port) {
1584 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1585 return NULL;
1586 }
1587 a.port = s->port;
1588 }
1589 }
1590 }
1591 if (a.port <= 0) {
1592 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1593 return NULL;
1594 }
1595 }
1596 }
1597 }
1598 }
Vadim Kochan7871f7d2015-02-27 23:54:36 +02001599 if (!is_port && addr && *addr && *addr != '*') {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001600 if (get_prefix_1(&a.addr, addr, fam)) {
1601 if (get_dns_host(&a, addr, fam)) {
1602 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1603 return NULL;
1604 }
1605 }
1606 }
1607
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001608out:
Vadim Kochan1527a172015-02-13 13:01:08 +02001609 if (fam != AF_UNSPEC) {
Phil Suttere56a9592016-04-13 22:07:05 +02001610 int states = f->states;
Vadim Kochan1527a172015-02-13 13:01:08 +02001611 f->families = 0;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001612 filter_af_set(f, fam);
Phil Suttere56a9592016-04-13 22:07:05 +02001613 filter_states_set(f, states);
Vadim Kochan1527a172015-02-13 13:01:08 +02001614 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02001615
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001616 res = malloc(sizeof(*res));
1617 if (res)
1618 memcpy(res, &a, sizeof(a));
1619 return res;
1620}
1621
Vadim Kochan8250bc92015-01-20 16:14:24 +02001622static char *proto_name(int protocol)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001623{
Vadim Kochan8250bc92015-01-20 16:14:24 +02001624 switch (protocol) {
Nikolay Aleksandrov235c4452015-07-15 05:50:42 -07001625 case 0:
1626 return "raw";
Vadim Kochan8250bc92015-01-20 16:14:24 +02001627 case IPPROTO_UDP:
1628 return "udp";
1629 case IPPROTO_TCP:
1630 return "tcp";
1631 case IPPROTO_DCCP:
1632 return "dccp";
1633 }
1634
1635 return "???";
1636}
1637
Vadim Kochan055840f2015-02-13 22:13:58 +02001638static void inet_stats_print(struct sockstat *s, int protocol)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001639{
1640 char *buf = NULL;
1641
Vadim Kochan2d791bc2015-02-13 22:14:01 +02001642 sock_state_print(s, proto_name(protocol));
Vadim Kochan8250bc92015-01-20 16:14:24 +02001643
Vadim Kochanb217df12015-02-13 22:14:03 +02001644 inet_addr_print(&s->local, s->lport, s->iface);
1645 inet_addr_print(&s->remote, s->rport, 0);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001646
Vadim Kochan8250bc92015-01-20 16:14:24 +02001647 if (show_proc_ctx || show_sock_ctx) {
1648 if (find_entry(s->ino, &buf,
1649 (show_proc_ctx & show_sock_ctx) ?
1650 PROC_SOCK_CTX : PROC_CTX) > 0) {
1651 printf(" users:(%s)", buf);
1652 free(buf);
1653 }
1654 } else if (show_users) {
1655 if (find_entry(s->ino, &buf, USERS) > 0) {
1656 printf(" users:(%s)", buf);
1657 free(buf);
1658 }
1659 }
1660}
1661
Vadim Kochan055840f2015-02-13 22:13:58 +02001662static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001663 sockstat * s)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001664{
1665 s->local.family = s->remote.family = family;
1666 if (family == AF_INET) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001667 sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
1668 sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001669 s->local.bytelen = s->remote.bytelen = 4;
1670 return 0;
1671 } else {
1672 sscanf(loc, "%08x%08x%08x%08x:%x",
1673 s->local.data,
1674 s->local.data + 1,
1675 s->local.data + 2,
1676 s->local.data + 3,
1677 &s->lport);
1678 sscanf(rem, "%08x%08x%08x%08x:%x",
1679 s->remote.data,
1680 s->remote.data + 1,
1681 s->remote.data + 2,
1682 s->remote.data + 3,
1683 &s->rport);
1684 s->local.bytelen = s->remote.bytelen = 16;
1685 return 0;
1686 }
1687 return -1;
1688}
1689
1690static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
1691{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001692 char *p;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001693
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001694 if ((p = strchr(line, ':')) == NULL)
1695 return -1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001696
Vadim Kochan8250bc92015-01-20 16:14:24 +02001697 *loc = p+2;
1698 if ((p = strchr(*loc, ':')) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001699 return -1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001700
Vadim Kochan8250bc92015-01-20 16:14:24 +02001701 p[5] = 0;
1702 *rem = p+6;
1703 if ((p = strchr(*rem, ':')) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001704 return -1;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001705
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001706 p[5] = 0;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001707 *data = p+6;
1708 return 0;
1709}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001710
Vadim Kochan8250bc92015-01-20 16:14:24 +02001711static char *sprint_bw(char *buf, double bw)
1712{
1713 if (bw > 1000000.)
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001714 sprintf(buf, "%.1fM", bw / 1000000.);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001715 else if (bw > 1000.)
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001716 sprintf(buf, "%.1fK", bw / 1000.);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001717 else
1718 sprintf(buf, "%g", bw);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001719
Vadim Kochan8250bc92015-01-20 16:14:24 +02001720 return buf;
1721}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001722
Vadim Kochan8250bc92015-01-20 16:14:24 +02001723static void tcp_stats_print(struct tcpstat *s)
1724{
1725 char b1[64];
1726
1727 if (s->has_ts_opt)
1728 printf(" ts");
1729 if (s->has_sack_opt)
1730 printf(" sack");
1731 if (s->has_ecn_opt)
1732 printf(" ecn");
1733 if (s->has_ecnseen_opt)
1734 printf(" ecnseen");
1735 if (s->has_fastopen_opt)
1736 printf(" fastopen");
Eric Dumazetd2055ea2015-05-29 04:04:05 -07001737 if (s->cong_alg[0])
Vadim Kochan8250bc92015-01-20 16:14:24 +02001738 printf(" %s", s->cong_alg);
1739 if (s->has_wscale_opt)
1740 printf(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale);
1741 if (s->rto)
1742 printf(" rto:%g", s->rto);
1743 if (s->backoff)
1744 printf(" backoff:%u", s->backoff);
1745 if (s->rtt)
1746 printf(" rtt:%g/%g", s->rtt, s->rttvar);
1747 if (s->ato)
1748 printf(" ato:%g", s->ato);
1749
1750 if (s->qack)
1751 printf(" qack:%d", s->qack);
1752 if (s->qack & 1)
1753 printf(" bidir");
1754
1755 if (s->mss)
1756 printf(" mss:%d", s->mss);
Eric Dumazet3bf54452015-05-08 13:28:40 -07001757 if (s->cwnd)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001758 printf(" cwnd:%d", s->cwnd);
1759 if (s->ssthresh)
1760 printf(" ssthresh:%d", s->ssthresh);
1761
Eric Dumazet1a4dda72015-05-11 10:03:49 -07001762 if (s->bytes_acked)
1763 printf(" bytes_acked:%llu", s->bytes_acked);
1764 if (s->bytes_received)
1765 printf(" bytes_received:%llu", s->bytes_received);
Craig Gallekecb435e2015-05-26 14:54:41 -04001766 if (s->segs_out)
1767 printf(" segs_out:%u", s->segs_out);
1768 if (s->segs_in)
1769 printf(" segs_in:%u", s->segs_in);
Martin KaFai Lau414aeec2016-06-17 17:38:53 -07001770 if (s->data_segs_out)
1771 printf(" data_segs_out:%u", s->data_segs_out);
1772 if (s->data_segs_in)
1773 printf(" data_segs_in:%u", s->data_segs_in);
Eric Dumazet1a4dda72015-05-11 10:03:49 -07001774
Vadim Kochan8250bc92015-01-20 16:14:24 +02001775 if (s->dctcp && s->dctcp->enabled) {
1776 struct dctcpstat *dctcp = s->dctcp;
1777
Eric Dumazet3bf54452015-05-08 13:28:40 -07001778 printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)",
Vadim Kochan8250bc92015-01-20 16:14:24 +02001779 dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn,
1780 dctcp->ab_tot);
1781 } else if (s->dctcp) {
Eric Dumazet3bf54452015-05-08 13:28:40 -07001782 printf(" dctcp:fallback_mode");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001783 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001784
Vadim Kochan8250bc92015-01-20 16:14:24 +02001785 if (s->send_bps)
1786 printf(" send %sbps", sprint_bw(b1, s->send_bps));
1787 if (s->lastsnd)
1788 printf(" lastsnd:%u", s->lastsnd);
1789 if (s->lastrcv)
1790 printf(" lastrcv:%u", s->lastrcv);
1791 if (s->lastack)
1792 printf(" lastack:%u", s->lastack);
1793
1794 if (s->pacing_rate) {
1795 printf(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate));
1796 if (s->pacing_rate_max)
1797 printf("/%sbps", sprint_bw(b1,
1798 s->pacing_rate_max));
1799 }
1800
1801 if (s->unacked)
1802 printf(" unacked:%u", s->unacked);
1803 if (s->retrans || s->retrans_total)
1804 printf(" retrans:%u/%u", s->retrans, s->retrans_total);
1805 if (s->lost)
1806 printf(" lost:%u", s->lost);
Vadim Kochan055840f2015-02-13 22:13:58 +02001807 if (s->sacked && s->ss.state != SS_LISTEN)
Vadim Kochan8250bc92015-01-20 16:14:24 +02001808 printf(" sacked:%u", s->sacked);
1809 if (s->fackets)
1810 printf(" fackets:%u", s->fackets);
1811 if (s->reordering != 3)
1812 printf(" reordering:%d", s->reordering);
1813 if (s->rcv_rtt)
1814 printf(" rcv_rtt:%g", s->rcv_rtt);
1815 if (s->rcv_space)
1816 printf(" rcv_space:%d", s->rcv_space);
Stephen Hemminger9e99e492016-02-17 17:43:59 -08001817 if (s->not_sent)
1818 printf(" notsent:%u", s->not_sent);
1819 if (s->min_rtt)
1820 printf(" minrtt:%g", s->min_rtt);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001821}
1822
Vadim Kochan055840f2015-02-13 22:13:58 +02001823static void tcp_timer_print(struct tcpstat *s)
1824{
1825 if (s->timer) {
1826 if (s->timer > 4)
1827 s->timer = 5;
1828 printf(" timer:(%s,%s,%d)",
1829 tmr_name[s->timer],
1830 print_ms_timer(s->timeout),
1831 s->retrans);
1832 }
1833}
1834
Vadim Kochan8250bc92015-01-20 16:14:24 +02001835static int tcp_show_line(char *line, const struct filter *f, int family)
1836{
1837 int rto = 0, ato = 0;
1838 struct tcpstat s = {};
1839 char *loc, *rem, *data;
1840 char opt[256];
1841 int n;
1842 int hz = get_user_hz();
1843
1844 if (proc_inet_split_line(line, &loc, &rem, &data))
1845 return -1;
1846
1847 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001848
Vadim Kochan8250bc92015-01-20 16:14:24 +02001849 if (!(f->states & (1 << state)))
1850 return 0;
1851
Vadim Kochan055840f2015-02-13 22:13:58 +02001852 proc_parse_inet_addr(loc, rem, family, &s.ss);
Vadim Kochan8250bc92015-01-20 16:14:24 +02001853
Vadim Kochan055840f2015-02-13 22:13:58 +02001854 if (f->f && run_ssfilter(f->f, &s.ss) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001855 return 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001856
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001857 opt[0] = 0;
Stephen Hemmingere7113c62007-07-10 18:26:54 -07001858 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 +02001859 &s.ss.state, &s.ss.wq, &s.ss.rq,
1860 &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes,
1861 &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd,
1862 &s.ssthresh, opt);
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001863
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001864 if (n < 17)
1865 opt[0] = 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001866
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001867 if (n < 12) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001868 rto = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001869 s.cwnd = 2;
1870 s.ssthresh = -1;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001871 ato = s.qack = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001872 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001873
Vadim Kochan8250bc92015-01-20 16:14:24 +02001874 s.retrans = s.timer != 1 ? s.probes : s.retrans;
1875 s.timeout = (s.timeout * 1000 + hz - 1) / hz;
1876 s.ato = (double)ato / hz;
1877 s.qack /= 2;
1878 s.rto = (double)rto;
1879 s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh;
1880 s.rto = s.rto != 3 * hz ? s.rto / hz : 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001881
Vadim Kochan055840f2015-02-13 22:13:58 +02001882 inet_stats_print(&s.ss, IPPROTO_TCP);
1883
1884 if (show_options)
1885 tcp_timer_print(&s);
Richard Haines116ac922014-03-07 10:36:52 +00001886
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001887 if (show_details) {
Vadim Kochanf1b39e12015-02-13 22:14:02 +02001888 sock_details_print(&s.ss);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001889 if (opt[0])
1890 printf(" opt:\"%s\"", opt);
1891 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001892
Vadim Kochan8250bc92015-01-20 16:14:24 +02001893 if (show_tcpinfo)
1894 tcp_stats_print(&s);
1895
1896 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001897 return 0;
1898}
1899
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001900static int generic_record_read(FILE *fp,
1901 int (*worker)(char*, const struct filter *, int),
1902 const struct filter *f, int fam)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001903{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001904 char line[256];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001905
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001906 /* skip header */
1907 if (fgets(line, sizeof(line), fp) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001908 goto outerr;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001909
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001910 while (fgets(line, sizeof(line), fp) != NULL) {
1911 int n = strlen(line);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07001912
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001913 if (n == 0 || line[n-1] != '\n') {
1914 errno = -EINVAL;
1915 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001916 }
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001917 line[n-1] = 0;
1918
1919 if (worker(line, f, fam) < 0)
1920 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001921 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001922outerr:
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01001923
1924 return ferror(fp) ? -1 : 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001925}
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001926
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001927static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1928{
1929 const __u32 *skmeminfo;
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001930
1931 if (!tb[attrtype]) {
1932 if (attrtype == INET_DIAG_SKMEMINFO) {
1933 if (!tb[INET_DIAG_MEMINFO])
1934 return;
1935
1936 const struct inet_diag_meminfo *minfo =
1937 RTA_DATA(tb[INET_DIAG_MEMINFO]);
1938
1939 printf(" mem:(r%u,w%u,f%u,t%u)",
1940 minfo->idiag_rmem,
1941 minfo->idiag_wmem,
1942 minfo->idiag_fmem,
1943 minfo->idiag_tmem);
1944 }
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001945 return;
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001946 }
1947
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001948 skmeminfo = RTA_DATA(tb[attrtype]);
1949
1950 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
1951 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
1952 skmeminfo[SK_MEMINFO_RCVBUF],
1953 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
1954 skmeminfo[SK_MEMINFO_SNDBUF],
1955 skmeminfo[SK_MEMINFO_FWD_ALLOC],
1956 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
1957 skmeminfo[SK_MEMINFO_OPTMEM]);
1958
1959 if (RTA_PAYLOAD(tb[attrtype]) >=
1960 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
1961 printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
1962
Eric Dumazet6df9c7a2016-04-21 05:19:04 -07001963 if (RTA_PAYLOAD(tb[attrtype]) >=
1964 (SK_MEMINFO_DROPS + 1) * sizeof(__u32))
1965 printf(",d%u", skmeminfo[SK_MEMINFO_DROPS]);
1966
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00001967 printf(")");
1968}
1969
Vadim Kochan8250bc92015-01-20 16:14:24 +02001970#define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt))
1971
Pavel Emelyanov5b816042013-05-17 08:02:14 -07001972static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
1973 struct rtattr *tb[])
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001974{
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001975 double rtt = 0;
Vadim Kochan8250bc92015-01-20 16:14:24 +02001976 struct tcpstat s = {};
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001977
Vadim Kochan055840f2015-02-13 22:13:58 +02001978 s.ss.state = r->idiag_state;
1979
Vadim Kochandb08bdb2015-01-20 16:14:23 +02001980 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00001981
shemminger351efcd2005-09-01 19:21:50 +00001982 if (tb[INET_DIAG_INFO]) {
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001983 struct tcp_info *info;
shemminger351efcd2005-09-01 19:21:50 +00001984 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001985
1986 /* workaround for older kernels with less fields */
1987 if (len < sizeof(*info)) {
1988 info = alloca(sizeof(*info));
shemminger351efcd2005-09-01 19:21:50 +00001989 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
Eric Dumazet656e8fd2015-05-06 11:33:23 -07001990 memset((char *)info + len, 0, sizeof(*info) - len);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001991 } else
shemminger351efcd2005-09-01 19:21:50 +00001992 info = RTA_DATA(tb[INET_DIAG_INFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00001993
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00001994 if (show_options) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02001995 s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS);
1996 s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK);
1997 s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN);
1998 s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN);
1999 s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA);
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002000 }
shemminger52d5ac32005-07-05 22:11:37 +00002001
Eric Dumazetd2055ea2015-05-29 04:04:05 -07002002 if (tb[INET_DIAG_CONG])
2003 strncpy(s.cong_alg,
2004 rta_getattr_str(tb[INET_DIAG_CONG]),
2005 sizeof(s.cong_alg) - 1);
shemmingerea8fc102005-06-22 18:27:49 +00002006
Vadim Kochan8250bc92015-01-20 16:14:24 +02002007 if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) {
2008 s.has_wscale_opt = true;
2009 s.snd_wscale = info->tcpi_snd_wscale;
2010 s.rcv_wscale = info->tcpi_rcv_wscale;
2011 }
2012
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002013 if (info->tcpi_rto && info->tcpi_rto != 3000000)
Vadim Kochan8250bc92015-01-20 16:14:24 +02002014 s.rto = (double)info->tcpi_rto / 1000;
2015
2016 s.backoff = info->tcpi_backoff;
2017 s.rtt = (double)info->tcpi_rtt / 1000;
2018 s.rttvar = (double)info->tcpi_rttvar / 1000;
Vadim Kochan11ba90f2015-02-13 22:14:04 +02002019 s.ato = (double)info->tcpi_ato / 1000;
Vadim Kochan8250bc92015-01-20 16:14:24 +02002020 s.mss = info->tcpi_snd_mss;
2021 s.rcv_space = info->tcpi_rcv_space;
2022 s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000;
2023 s.lastsnd = info->tcpi_last_data_sent;
2024 s.lastrcv = info->tcpi_last_data_recv;
2025 s.lastack = info->tcpi_last_ack_recv;
2026 s.unacked = info->tcpi_unacked;
2027 s.retrans = info->tcpi_retrans;
2028 s.retrans_total = info->tcpi_total_retrans;
2029 s.lost = info->tcpi_lost;
2030 s.sacked = info->tcpi_sacked;
2031 s.reordering = info->tcpi_reordering;
2032 s.rcv_space = info->tcpi_rcv_space;
2033 s.cwnd = info->tcpi_snd_cwnd;
2034
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002035 if (info->tcpi_snd_ssthresh < 0xFFFF)
Vadim Kochan8250bc92015-01-20 16:14:24 +02002036 s.ssthresh = info->tcpi_snd_ssthresh;
shemminger52d5ac32005-07-05 22:11:37 +00002037
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002038 rtt = (double) info->tcpi_rtt;
shemminger351efcd2005-09-01 19:21:50 +00002039 if (tb[INET_DIAG_VEGASINFO]) {
osdl.org!shemminger05e18112004-06-08 20:33:44 +00002040 const struct tcpvegas_info *vinfo
shemminger351efcd2005-09-01 19:21:50 +00002041 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
osdl.org!shemminger05e18112004-06-08 20:33:44 +00002042
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002043 if (vinfo->tcpv_enabled &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02002044 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
shemmingerea8fc102005-06-22 18:27:49 +00002045 rtt = vinfo->tcpv_rtt;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002046 }
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002047
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02002048 if (tb[INET_DIAG_DCTCPINFO]) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02002049 struct dctcpstat *dctcp = malloc(sizeof(struct
2050 dctcpstat));
2051
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02002052 const struct tcp_dctcp_info *dinfo
2053 = RTA_DATA(tb[INET_DIAG_DCTCPINFO]);
2054
Vadim Kochan8250bc92015-01-20 16:14:24 +02002055 dctcp->enabled = !!dinfo->dctcp_enabled;
2056 dctcp->ce_state = dinfo->dctcp_ce_state;
2057 dctcp->alpha = dinfo->dctcp_alpha;
2058 dctcp->ab_ecn = dinfo->dctcp_ab_ecn;
2059 dctcp->ab_tot = dinfo->dctcp_ab_tot;
2060 s.dctcp = dctcp;
Daniel Borkmann907e1ac2014-09-29 10:47:32 +02002061 }
2062
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002063 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
Vadim Kochan8250bc92015-01-20 16:14:24 +02002064 s.send_bps = (double) info->tcpi_snd_cwnd *
2065 (double)info->tcpi_snd_mss * 8000000. / rtt;
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002066 }
2067
Eric Dumazeteb6028b2014-06-05 06:47:37 -07002068 if (info->tcpi_pacing_rate &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02002069 info->tcpi_pacing_rate != ~0ULL) {
2070 s.pacing_rate = info->tcpi_pacing_rate * 8.;
Eric Dumazeteb6028b2014-06-05 06:47:37 -07002071
2072 if (info->tcpi_max_pacing_rate &&
Vadim Kochan8250bc92015-01-20 16:14:24 +02002073 info->tcpi_max_pacing_rate != ~0ULL)
2074 s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.;
Eric Dumazeteb6028b2014-06-05 06:47:37 -07002075 }
Eric Dumazet1a4dda72015-05-11 10:03:49 -07002076 s.bytes_acked = info->tcpi_bytes_acked;
2077 s.bytes_received = info->tcpi_bytes_received;
Craig Gallekecb435e2015-05-26 14:54:41 -04002078 s.segs_out = info->tcpi_segs_out;
2079 s.segs_in = info->tcpi_segs_in;
Martin KaFai Lau414aeec2016-06-17 17:38:53 -07002080 s.data_segs_out = info->tcpi_data_segs_out;
2081 s.data_segs_in = info->tcpi_data_segs_in;
Stephen Hemminger9e99e492016-02-17 17:43:59 -08002082 s.not_sent = info->tcpi_notsent_bytes;
Eric Dumazetd9ba8872016-04-13 15:18:38 -07002083 if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U)
2084 s.min_rtt = (double) info->tcpi_min_rtt / 1000;
Vadim Kochan8250bc92015-01-20 16:14:24 +02002085 tcp_stats_print(&s);
Stephen Hemminger92de1c22015-07-21 10:49:54 -07002086 free(s.dctcp);
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002087 }
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002088}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002089
David Ahern82d73ea2016-06-27 11:34:23 -07002090static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002091{
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002092 struct rtattr *tb[INET_DIAG_MAX+1];
shemminger351efcd2005-09-01 19:21:50 +00002093 struct inet_diag_msg *r = NLMSG_DATA(nlh);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002094
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002095 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002096 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2097
David Ahern82d73ea2016-06-27 11:34:23 -07002098 s->state = r->idiag_state;
2099 s->local.family = s->remote.family = r->idiag_family;
2100 s->lport = ntohs(r->id.idiag_sport);
2101 s->rport = ntohs(r->id.idiag_dport);
2102 s->wq = r->idiag_wqueue;
2103 s->rq = r->idiag_rqueue;
2104 s->ino = r->idiag_inode;
2105 s->uid = r->idiag_uid;
2106 s->iface = r->id.idiag_if;
2107 s->sk = cookie_sk_get(&r->id.idiag_cookie[0]);
Vadim Kochan8250bc92015-01-20 16:14:24 +02002108
David Ahern82d73ea2016-06-27 11:34:23 -07002109 if (s->local.family == AF_INET)
2110 s->local.bytelen = s->remote.bytelen = 4;
2111 else
2112 s->local.bytelen = s->remote.bytelen = 16;
Vadim Kochan8250bc92015-01-20 16:14:24 +02002113
David Ahern82d73ea2016-06-27 11:34:23 -07002114 memcpy(s->local.data, r->id.idiag_src, s->local.bytelen);
2115 memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen);
2116}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002117
David Ahern82d73ea2016-06-27 11:34:23 -07002118static int inet_show_sock(struct nlmsghdr *nlh,
2119 struct sockstat *s,
2120 int protocol)
2121{
2122 struct rtattr *tb[INET_DIAG_MAX+1];
2123 struct inet_diag_msg *r = NLMSG_DATA(nlh);
2124
2125 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
2126 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002127
Craig Gallek6885e3b2015-06-17 11:14:48 -04002128 if (tb[INET_DIAG_PROTOCOL])
2129 protocol = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]);
2130
David Ahern82d73ea2016-06-27 11:34:23 -07002131 inet_stats_print(s, protocol);
Richard Haines116ac922014-03-07 10:36:52 +00002132
Vadim Kochan055840f2015-02-13 22:13:58 +02002133 if (show_options) {
2134 struct tcpstat t = {};
2135
2136 t.timer = r->idiag_timer;
2137 t.timeout = r->idiag_expires;
2138 t.retrans = r->idiag_retrans;
2139 tcp_timer_print(&t);
2140 }
2141
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002142 if (show_details) {
David Ahern82d73ea2016-06-27 11:34:23 -07002143 sock_details_print(s);
2144 if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) {
Phil Sutterf32dc742015-06-24 13:07:20 +02002145 unsigned char v6only;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002146
Phil Sutterf32dc742015-06-24 13:07:20 +02002147 v6only = *(__u8 *)RTA_DATA(tb[INET_DIAG_SKV6ONLY]);
2148 printf(" v6only:%u", v6only);
2149 }
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002150 if (tb[INET_DIAG_SHUTDOWN]) {
2151 unsigned char mask;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002152
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002153 mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]);
2154 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2155 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002156 }
Vadim Kochan8250bc92015-01-20 16:14:24 +02002157
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002158 if (show_mem || show_tcpinfo) {
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002159 printf("\n\t");
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002160 tcp_show_info(nlh, r, tb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002161 }
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002162
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002163 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002164 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002165}
2166
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002167static int tcpdiag_send(int fd, int protocol, struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002168{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002169 struct sockaddr_nl nladdr;
2170 struct {
2171 struct nlmsghdr nlh;
shemminger351efcd2005-09-01 19:21:50 +00002172 struct inet_diag_req r;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002173 } req;
2174 char *bc = NULL;
2175 int bclen;
2176 struct msghdr msg;
2177 struct rtattr rta;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002178 struct iovec iov[3];
David Ahern376fb862016-06-27 11:34:24 -07002179 int iovlen = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002180
Pavel Emelyanov346f8ca2012-10-25 03:24:58 +00002181 if (protocol == IPPROTO_UDP)
2182 return -1;
2183
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002184 memset(&nladdr, 0, sizeof(nladdr));
2185 nladdr.nl_family = AF_NETLINK;
2186
2187 req.nlh.nlmsg_len = sizeof(req);
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00002188 if (protocol == IPPROTO_TCP)
2189 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
2190 else
2191 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002192 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
2193 req.nlh.nlmsg_pid = 0;
vadimk8a4025f2014-12-04 12:32:58 +02002194 req.nlh.nlmsg_seq = MAGIC_SEQ;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002195 memset(&req.r, 0, sizeof(req.r));
shemminger351efcd2005-09-01 19:21:50 +00002196 req.r.idiag_family = AF_INET;
2197 req.r.idiag_states = f->states;
Shan Wei910b0392012-05-03 16:39:52 +08002198 if (show_mem) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002199 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
Shan Wei910b0392012-05-03 16:39:52 +08002200 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2201 }
osdl.net!shemmingerb4b0b7d2004-09-28 18:14:03 +00002202
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002203 if (show_tcpinfo) {
shemminger351efcd2005-09-01 19:21:50 +00002204 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2205 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2206 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00002207 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002208
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002209 iov[0] = (struct iovec){
2210 .iov_base = &req,
2211 .iov_len = sizeof(req)
shemmingerea8fc102005-06-22 18:27:49 +00002212 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002213 if (f->f) {
2214 bclen = ssfilter_bytecompile(f->f, &bc);
David Ahern376fb862016-06-27 11:34:24 -07002215 if (bclen) {
2216 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2217 rta.rta_len = RTA_LENGTH(bclen);
2218 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2219 iov[2] = (struct iovec){ bc, bclen };
2220 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2221 iovlen = 3;
2222 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002223 }
2224
2225 msg = (struct msghdr) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002226 .msg_name = (void *)&nladdr,
shemmingerea8fc102005-06-22 18:27:49 +00002227 .msg_namelen = sizeof(nladdr),
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002228 .msg_iov = iov,
David Ahern376fb862016-06-27 11:34:24 -07002229 .msg_iovlen = iovlen,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002230 };
2231
Eric Dumazet930a75f2012-04-10 09:00:16 -07002232 if (sendmsg(fd, &msg, 0) < 0) {
2233 close(fd);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002234 return -1;
Eric Dumazet930a75f2012-04-10 09:00:16 -07002235 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002236
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002237 return 0;
2238}
2239
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002240static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
2241{
2242 struct sockaddr_nl nladdr;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002243
vadimk5fb421d2014-10-30 18:49:25 +02002244 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002245 char *bc = NULL;
2246 int bclen;
2247 struct msghdr msg;
2248 struct rtattr rta;
2249 struct iovec iov[3];
David Ahern376fb862016-06-27 11:34:24 -07002250 int iovlen = 1;
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002251
2252 if (family == PF_UNSPEC)
2253 return tcpdiag_send(fd, protocol, f);
2254
2255 memset(&nladdr, 0, sizeof(nladdr));
2256 nladdr.nl_family = AF_NETLINK;
2257
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002258 memset(&req.r, 0, sizeof(req.r));
2259 req.r.sdiag_family = family;
2260 req.r.sdiag_protocol = protocol;
2261 req.r.idiag_states = f->states;
2262 if (show_mem) {
2263 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
2264 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
2265 }
2266
2267 if (show_tcpinfo) {
2268 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
2269 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
2270 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
2271 }
2272
2273 iov[0] = (struct iovec){
2274 .iov_base = &req,
2275 .iov_len = sizeof(req)
2276 };
2277 if (f->f) {
2278 bclen = ssfilter_bytecompile(f->f, &bc);
David Ahern376fb862016-06-27 11:34:24 -07002279 if (bclen) {
2280 rta.rta_type = INET_DIAG_REQ_BYTECODE;
2281 rta.rta_len = RTA_LENGTH(bclen);
2282 iov[1] = (struct iovec){ &rta, sizeof(rta) };
2283 iov[2] = (struct iovec){ bc, bclen };
2284 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
2285 iovlen = 3;
2286 }
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002287 }
2288
2289 msg = (struct msghdr) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002290 .msg_name = (void *)&nladdr,
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002291 .msg_namelen = sizeof(nladdr),
2292 .msg_iov = iov,
David Ahern376fb862016-06-27 11:34:24 -07002293 .msg_iovlen = iovlen,
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002294 };
2295
2296 if (sendmsg(fd, &msg, 0) < 0) {
2297 close(fd);
2298 return -1;
2299 }
2300
2301 return 0;
2302}
2303
Vadim Kochan486ccd92014-12-26 04:26:27 +02002304struct inet_diag_arg {
2305 struct filter *f;
2306 int protocol;
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002307 struct rtnl_handle *rth;
Vadim Kochan486ccd92014-12-26 04:26:27 +02002308};
2309
subashab@codeaurora.orgb38e7402016-05-09 14:54:36 -06002310static int kill_inet_sock(struct nlmsghdr *h, void *arg)
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002311{
2312 struct inet_diag_msg *d = NLMSG_DATA(h);
2313 struct inet_diag_arg *diag_arg = arg;
2314 struct rtnl_handle *rth = diag_arg->rth;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002315
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002316 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
2317
2318 req.nlh.nlmsg_type = SOCK_DESTROY;
2319 req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2320 req.nlh.nlmsg_seq = ++rth->seq;
2321 req.r.sdiag_family = d->idiag_family;
2322 req.r.sdiag_protocol = diag_arg->protocol;
2323 req.r.id = d->id;
2324
2325 return rtnl_talk(rth, &req.nlh, NULL, 0);
2326}
2327
Vadim Kochan486ccd92014-12-26 04:26:27 +02002328static int show_one_inet_sock(const struct sockaddr_nl *addr,
2329 struct nlmsghdr *h, void *arg)
2330{
2331 int err;
2332 struct inet_diag_arg *diag_arg = arg;
2333 struct inet_diag_msg *r = NLMSG_DATA(h);
David Ahern82d73ea2016-06-27 11:34:23 -07002334 struct sockstat s = {};
Vadim Kochan486ccd92014-12-26 04:26:27 +02002335
2336 if (!(diag_arg->f->families & (1 << r->idiag_family)))
2337 return 0;
David Ahern82d73ea2016-06-27 11:34:23 -07002338
2339 parse_diag_msg(h, &s);
2340
2341 if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0)
2342 return 0;
2343
subashab@codeaurora.orgb38e7402016-05-09 14:54:36 -06002344 if (diag_arg->f->kill && kill_inet_sock(h, arg) != 0) {
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002345 if (errno == EOPNOTSUPP || errno == ENOENT) {
2346 /* Socket can't be closed, or is already closed. */
2347 return 0;
2348 } else {
2349 perror("SOCK_DESTROY answers");
2350 return -1;
2351 }
2352 }
David Ahern82d73ea2016-06-27 11:34:23 -07002353
2354 err = inet_show_sock(h, &s, diag_arg->protocol);
2355 if (err < 0)
Vadim Kochan486ccd92014-12-26 04:26:27 +02002356 return err;
2357
2358 return 0;
2359}
2360
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002361static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
2362{
Vadim Kochan486ccd92014-12-26 04:26:27 +02002363 int err = 0;
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002364 struct rtnl_handle rth, rth2;
Vadim Kochan486ccd92014-12-26 04:26:27 +02002365 int family = PF_INET;
2366 struct inet_diag_arg arg = { .f = f, .protocol = protocol };
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002367
Vadim Kochan486ccd92014-12-26 04:26:27 +02002368 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002369 return -1;
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002370
2371 if (f->kill) {
2372 if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) {
2373 rtnl_close(&rth);
2374 return -1;
2375 }
2376 arg.rth = &rth2;
2377 }
2378
Vadim Kochan486ccd92014-12-26 04:26:27 +02002379 rth.dump = MAGIC_SEQ;
2380 rth.dump_fp = dump_fp;
Eric Dumazet518af1e2015-05-29 05:37:49 -07002381 if (preferred_family == PF_INET6)
2382 family = PF_INET6;
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002383
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002384again:
Vadim Kochan486ccd92014-12-26 04:26:27 +02002385 if ((err = sockdiag_send(family, rth.fd, protocol, f)))
2386 goto Exit;
Pavel Emelyanov746a6952012-10-25 03:21:39 +00002387
Vadim Kochan486ccd92014-12-26 04:26:27 +02002388 if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) {
2389 if (family != PF_UNSPEC) {
2390 family = PF_UNSPEC;
2391 goto again;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002392 }
Vadim Kochan486ccd92014-12-26 04:26:27 +02002393 goto Exit;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002394 }
Eric Dumazet518af1e2015-05-29 05:37:49 -07002395 if (family == PF_INET && preferred_family != PF_INET) {
Pavel Emelyanov886d19d2012-10-25 03:23:36 +00002396 family = PF_INET6;
2397 goto again;
2398 }
2399
Vadim Kochan486ccd92014-12-26 04:26:27 +02002400Exit:
2401 rtnl_close(&rth);
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09002402 if (arg.rth)
2403 rtnl_close(arg.rth);
Vadim Kochan486ccd92014-12-26 04:26:27 +02002404 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002405}
2406
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002407static int tcp_show_netlink_file(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002408{
2409 FILE *fp;
Eric Dumazete5572122014-10-11 09:43:13 -07002410 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002411
2412 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
2413 perror("fopen($TCPDIAG_FILE)");
2414 return -1;
2415 }
2416
2417 while (1) {
2418 int status, err;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002419 struct nlmsghdr *h = (struct nlmsghdr *)buf;
David Ahern82d73ea2016-06-27 11:34:23 -07002420 struct sockstat s = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002421
2422 status = fread(buf, 1, sizeof(*h), fp);
2423 if (status < 0) {
2424 perror("Reading header from $TCPDIAG_FILE");
2425 return -1;
2426 }
2427 if (status != sizeof(*h)) {
2428 perror("Unexpected EOF reading $TCPDIAG_FILE");
2429 return -1;
2430 }
2431
2432 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
2433
2434 if (status < 0) {
2435 perror("Reading $TCPDIAG_FILE");
2436 return -1;
2437 }
2438 if (status + sizeof(*h) < h->nlmsg_len) {
2439 perror("Unexpected EOF reading $TCPDIAG_FILE");
2440 return -1;
2441 }
2442
2443 /* The only legal exit point */
2444 if (h->nlmsg_type == NLMSG_DONE)
2445 return 0;
2446
2447 if (h->nlmsg_type == NLMSG_ERROR) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002448 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
2449
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002450 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
2451 fprintf(stderr, "ERROR truncated\n");
2452 } else {
2453 errno = -err->error;
2454 perror("TCPDIAG answered");
2455 }
2456 return -1;
2457 }
2458
David Ahern82d73ea2016-06-27 11:34:23 -07002459 parse_diag_msg(h, &s);
2460
2461 if (f && f->f && run_ssfilter(f->f, &s) == 0)
2462 continue;
2463
2464 err = inet_show_sock(h, &s, IPPROTO_TCP);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002465 if (err < 0)
2466 return err;
2467 }
2468}
2469
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002470static int tcp_show(struct filter *f, int socktype)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002471{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002472 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002473 char *buf = NULL;
2474 int bufsize = 64*1024;
2475
Vadim Kochan1527a172015-02-13 13:01:08 +02002476 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2477 return 0;
2478
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002479 dg_proto = TCP_PROTO;
2480
2481 if (getenv("TCPDIAG_FILE"))
2482 return tcp_show_netlink_file(f);
2483
2484 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00002485 && inet_show_netlink(f, NULL, socktype) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002486 return 0;
2487
2488 /* Sigh... We have to parse /proc/net/tcp... */
2489
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002490
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002491 /* Estimate amount of sockets and try to allocate
2492 * huge buffer to read all the table at one read.
2493 * Limit it by 16MB though. The assumption is: as soon as
2494 * kernel was able to hold information about N connections,
2495 * it is able to give us some memory for snapshot.
2496 */
2497 if (1) {
Bryton Leea221d622015-02-12 14:16:04 +08002498 get_slabstat(&slabstat);
2499
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002500 int guess = slabstat.socks+slabstat.tcp_syns;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002501
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002502 if (f->states&(1<<SS_TIME_WAIT))
2503 guess += slabstat.tcp_tws;
2504 if (guess > (16*1024*1024)/128)
2505 guess = (16*1024*1024)/128;
2506 guess *= 128;
2507 if (guess > bufsize)
2508 bufsize = guess;
2509 }
2510 while (bufsize >= 64*1024) {
2511 if ((buf = malloc(bufsize)) != NULL)
2512 break;
2513 bufsize /= 2;
2514 }
2515 if (buf == NULL) {
2516 errno = ENOMEM;
2517 return -1;
2518 }
2519
2520 if (f->families & (1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002521 if ((fp = net_tcp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002522 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002523
2524 setbuffer(fp, buf, bufsize);
2525 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002526 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002527 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002528 }
2529
2530 if ((f->families & (1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002531 (fp = net_tcp6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002532 setbuffer(fp, buf, bufsize);
2533 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002534 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002535 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002536 }
2537
2538 free(buf);
2539 return 0;
2540
2541outerr:
2542 do {
2543 int saved_errno = errno;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002544
Stephen Hemminger92de1c22015-07-21 10:49:54 -07002545 free(buf);
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002546 if (fp)
2547 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002548 errno = saved_errno;
2549 return -1;
2550 } while (0);
2551}
2552
2553
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002554static int dgram_show_line(char *line, const struct filter *f, int family)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002555{
Vadim Kochan055840f2015-02-13 22:13:58 +02002556 struct sockstat s = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002557 char *loc, *rem, *data;
2558 char opt[256];
2559 int n;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002560
Vadim Kochan8250bc92015-01-20 16:14:24 +02002561 if (proc_inet_split_line(line, &loc, &rem, &data))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002562 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002563
Vadim Kochan8250bc92015-01-20 16:14:24 +02002564 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002565
Vadim Kochan8250bc92015-01-20 16:14:24 +02002566 if (!(f->states & (1 << state)))
2567 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002568
Vadim Kochan8250bc92015-01-20 16:14:24 +02002569 proc_parse_inet_addr(loc, rem, family, &s);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002570
2571 if (f->f && run_ssfilter(f->f, &s) == 0)
2572 return 0;
2573
2574 opt[0] = 0;
Stephen Hemmingere7113c62007-07-10 18:26:54 -07002575 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002576 &s.state, &s.wq, &s.rq,
2577 &s.uid, &s.ino,
2578 &s.refcnt, &s.sk, opt);
2579
2580 if (n < 9)
2581 opt[0] = 0;
2582
Nikolay Aleksandrov235c4452015-07-15 05:50:42 -07002583 inet_stats_print(&s, dg_proto == UDP_PROTO ? IPPROTO_UDP : 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002584
Vadim Kochanf1b39e12015-02-13 22:14:02 +02002585 if (show_details && opt[0])
2586 printf(" opt:\"%s\"", opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002587
Vadim Kochan8250bc92015-01-20 16:14:24 +02002588 printf("\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002589 return 0;
2590}
2591
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002592static int udp_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002593{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002594 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002595
Vadim Kochan1527a172015-02-13 13:01:08 +02002596 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2597 return 0;
2598
Vadim Kochanace5cb32015-01-08 02:42:54 +02002599 dg_proto = UDP_PROTO;
2600
Pavel Emelyanov346f8ca2012-10-25 03:24:58 +00002601 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2602 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
2603 return 0;
2604
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002605 if (f->families&(1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002606 if ((fp = net_udp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002607 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002608 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002609 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002610 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002611 }
2612
2613 if ((f->families&(1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002614 (fp = net_udp6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002615 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002616 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002617 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002618 }
2619 return 0;
2620
2621outerr:
2622 do {
2623 int saved_errno = errno;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002624
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002625 if (fp)
2626 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002627 errno = saved_errno;
2628 return -1;
2629 } while (0);
2630}
2631
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002632static int raw_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002633{
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002634 FILE *fp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002635
Vadim Kochan1527a172015-02-13 13:01:08 +02002636 if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
2637 return 0;
2638
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002639 dg_proto = RAW_PROTO;
2640
2641 if (f->families&(1<<AF_INET)) {
Björn Steinbrink69cae642008-04-03 11:42:41 +02002642 if ((fp = net_raw_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002643 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002644 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002645 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002646 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002647 }
2648
2649 if ((f->families&(1<<AF_INET6)) &&
Björn Steinbrink69cae642008-04-03 11:42:41 +02002650 (fp = net_raw6_open()) != NULL) {
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002651 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002652 goto outerr;
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002653 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002654 }
2655 return 0;
2656
2657outerr:
2658 do {
2659 int saved_errno = errno;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002660
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002661 if (fp)
2662 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002663 errno = saved_errno;
2664 return -1;
2665 } while (0);
2666}
2667
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002668int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2669 SS_ESTABLISHED, SS_CLOSING };
2670
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002671#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002672
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002673static void unix_list_free(struct sockstat *list)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002674{
2675 while (list) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002676 struct sockstat *s = list;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002677
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002678 list = list->next;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002679 free(s->name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002680 free(s);
2681 }
2682}
2683
Masatake YAMATO30b669d2014-01-08 20:13:46 +09002684static const char *unix_netid_name(int type)
2685{
2686 const char *netid;
2687
2688 switch (type) {
2689 case SOCK_STREAM:
2690 netid = "u_str";
2691 break;
2692 case SOCK_SEQPACKET:
2693 netid = "u_seq";
2694 break;
2695 case SOCK_DGRAM:
2696 default:
2697 netid = "u_dgr";
2698 break;
2699 }
2700 return netid;
2701}
2702
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002703static bool unix_type_skip(struct sockstat *s, struct filter *f)
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002704{
2705 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2706 return true;
2707 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2708 return true;
2709 if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB)))
2710 return true;
2711 return false;
2712}
2713
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002714static bool unix_use_proc(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002715{
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002716 return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT");
2717}
2718
2719static void unix_stats_print(struct sockstat *list, struct filter *f)
2720{
2721 struct sockstat *s;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002722 char *peer;
Vadim Kochanb217df12015-02-13 22:14:03 +02002723 char *ctx_buf = NULL;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002724 bool use_proc = unix_use_proc();
Vadim Kochanb217df12015-02-13 22:14:03 +02002725 char port_name[30] = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002726
2727 for (s = list; s; s = s->next) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002728 if (!(f->states & (1 << s->state)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002729 continue;
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002730 if (unix_type_skip(s, f))
Masatake YAMATO30b669d2014-01-08 20:13:46 +09002731 continue;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002732
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002733 peer = "*";
2734 if (s->peer_name)
2735 peer = s->peer_name;
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002736
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002737 if (s->rport && use_proc) {
2738 struct sockstat *p;
2739
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002740 for (p = list; p; p = p->next) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002741 if (s->rport == p->lport)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002742 break;
2743 }
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002744
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002745 if (!p) {
2746 peer = "?";
2747 } else {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002748 peer = p->name ? : "*";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002749 }
2750 }
2751
Vadim Kochan29999b02015-02-22 22:23:10 +02002752 if (use_proc && f->f) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002753 struct sockstat st;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002754
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002755 st.local.family = AF_UNIX;
2756 st.remote.family = AF_UNIX;
2757 memcpy(st.local.data, &s->name, sizeof(s->name));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002758 if (strcmp(peer, "*") == 0)
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002759 memset(st.remote.data, 0, sizeof(peer));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002760 else
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002761 memcpy(st.remote.data, &peer, sizeof(peer));
2762 if (run_ssfilter(f->f, &st) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002763 continue;
2764 }
2765
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002766 sock_state_print(s, unix_netid_name(s->type));
2767
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002768 sock_addr_print(s->name ?: "*", " ",
Vadim Kochanb217df12015-02-13 22:14:03 +02002769 int_to_str(s->lport, port_name), NULL);
2770 sock_addr_print(peer, " ", int_to_str(s->rport, port_name),
2771 NULL);
Richard Haines116ac922014-03-07 10:36:52 +00002772
2773 if (show_proc_ctx || show_sock_ctx) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002774 if (find_entry(s->ino, &ctx_buf,
Richard Haines116ac922014-03-07 10:36:52 +00002775 (show_proc_ctx & show_sock_ctx) ?
2776 PROC_SOCK_CTX : PROC_CTX) > 0) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002777 printf(" users:(%s)", ctx_buf);
2778 free(ctx_buf);
Richard Haines116ac922014-03-07 10:36:52 +00002779 }
2780 } else if (show_users) {
Vadim Kochanb217df12015-02-13 22:14:03 +02002781 if (find_entry(s->ino, &ctx_buf, USERS) > 0) {
2782 printf(" users:(%s)", ctx_buf);
2783 free(ctx_buf);
Richard Haines116ac922014-03-07 10:36:52 +00002784 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002785 }
2786 printf("\n");
2787 }
2788}
2789
vadimk8a4025f2014-12-04 12:32:58 +02002790static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
2791 void *arg)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002792{
vadimk8a4025f2014-12-04 12:32:58 +02002793 struct filter *f = (struct filter *)arg;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002794 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2795 struct rtattr *tb[UNIX_DIAG_MAX+1];
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002796 char name[128];
2797 struct sockstat stat = { .name = "*", .peer_name = "*" };
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002798
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002799 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1),
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002800 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2801
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002802 stat.type = r->udiag_type;
2803 stat.state = r->udiag_state;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002804 stat.ino = stat.lport = r->udiag_ino;
2805 stat.local.family = stat.remote.family = AF_UNIX;
Masatake YAMATO0d2e01c2014-01-08 20:13:47 +09002806
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002807 if (unix_type_skip(&stat, f))
2808 return 0;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002809
Hannes Frederic Sowadefd61c2013-02-22 15:28:10 +00002810 if (tb[UNIX_DIAG_RQLEN]) {
2811 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002812
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002813 stat.rq = rql->udiag_rqueue;
2814 stat.wq = rql->udiag_wqueue;
Hannes Frederic Sowadefd61c2013-02-22 15:28:10 +00002815 }
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002816 if (tb[UNIX_DIAG_NAME]) {
2817 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2818
2819 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2820 name[len] = '\0';
2821 if (name[0] == '\0')
2822 name[0] = '@';
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002823 stat.name = &name[0];
2824 memcpy(stat.local.data, &stat.name, sizeof(stat.name));
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002825 }
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002826 if (tb[UNIX_DIAG_PEER])
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002827 stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002828
Vadim Kochan29999b02015-02-22 22:23:10 +02002829 if (f->f && run_ssfilter(f->f, &stat) == 0)
2830 return 0;
2831
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002832 unix_stats_print(&stat, f);
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002833
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00002834 if (show_mem) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002835 printf("\t");
Hannes Frederic Sowa51ff9f22013-02-22 15:28:18 +00002836 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2837 }
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002838 if (show_details) {
2839 if (tb[UNIX_DIAG_SHUTDOWN]) {
2840 unsigned char mask;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07002841
Pavel Emelyanov5b816042013-05-17 08:02:14 -07002842 mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]);
2843 printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
2844 }
2845 }
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002846 if (show_mem || show_details)
2847 printf("\n");
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002848
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002849 return 0;
2850}
2851
vadimk8a4025f2014-12-04 12:32:58 +02002852static int handle_netlink_request(struct filter *f, struct nlmsghdr *req,
2853 size_t size, rtnl_filter_t show_one_sock)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002854{
vadimk8a4025f2014-12-04 12:32:58 +02002855 int ret = -1;
2856 struct rtnl_handle rth;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002857
vadimk8a4025f2014-12-04 12:32:58 +02002858 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002859 return -1;
2860
vadimk8a4025f2014-12-04 12:32:58 +02002861 rth.dump = MAGIC_SEQ;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002862
vadimk8a4025f2014-12-04 12:32:58 +02002863 if (rtnl_send(&rth, req, size) < 0)
2864 goto Exit;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002865
vadimk8a4025f2014-12-04 12:32:58 +02002866 if (rtnl_dump_filter(&rth, show_one_sock, f))
2867 goto Exit;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002868
vadimk8a4025f2014-12-04 12:32:58 +02002869 ret = 0;
2870Exit:
2871 rtnl_close(&rth);
2872 return ret;
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002873}
2874
vadimk8a4025f2014-12-04 12:32:58 +02002875static int unix_show_netlink(struct filter *f)
Andrey Vagind8402b92013-06-05 12:41:58 +04002876{
vadimk5fb421d2014-10-30 18:49:25 +02002877 DIAG_REQUEST(req, struct unix_diag_req r);
Andrey Vagind8402b92013-06-05 12:41:58 +04002878
2879 req.r.sdiag_family = AF_UNIX;
2880 req.r.udiag_states = f->states;
2881 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
2882 if (show_mem)
2883 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
2884
vadimk8a4025f2014-12-04 12:32:58 +02002885 return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
Andrey Vagind8402b92013-06-05 12:41:58 +04002886}
2887
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08002888static int unix_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002889{
2890 FILE *fp;
2891 char buf[256];
2892 char name[128];
2893 int newformat = 0;
2894 int cnt;
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002895 struct sockstat *list = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002896
Vadim Kochan9db7bf12015-01-04 22:18:40 +02002897 if (!filter_af_get(f, AF_UNIX))
2898 return 0;
2899
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002900 if (!unix_use_proc() && unix_show_netlink(f) == 0)
Pavel Emelyanovdfbaa902011-12-15 03:28:15 +00002901 return 0;
2902
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01002903 if ((fp = net_unix_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002904 return -1;
Phil Sutter61170fd2015-11-28 01:00:05 +01002905 if (!fgets(buf, sizeof(buf), fp)) {
Phil Sutterd572ed42015-11-28 01:00:04 +01002906 fclose(fp);
2907 return -1;
2908 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002909
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002910 if (memcmp(buf, "Peer", 4) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002911 newformat = 1;
2912 cnt = 0;
2913
Phil Sutter61170fd2015-11-28 01:00:05 +01002914 while (fgets(buf, sizeof(buf), fp)) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002915 struct sockstat *u, **insp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002916 int flags;
2917
willy tarreau0ee90522015-10-06 12:09:33 +02002918 if (!(u = calloc(1, sizeof(*u))))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002919 break;
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002920 u->name = NULL;
2921 u->peer_name = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002922
2923 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002924 &u->rport, &u->rq, &u->wq, &flags, &u->type,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002925 &u->state, &u->ino, name) < 8)
2926 name[0] = 0;
2927
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002928 u->lport = u->ino;
2929 u->local.family = u->remote.family = AF_UNIX;
2930
2931 if (flags & (1 << 16)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002932 u->state = SS_LISTEN;
2933 } else {
2934 u->state = unix_state_map[u->state-1];
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002935 if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002936 u->state = SS_ESTABLISHED;
2937 }
2938
2939 if (!newformat) {
Vadim Kochanec4d0d82015-02-13 22:14:00 +02002940 u->rport = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002941 u->rq = 0;
2942 u->wq = 0;
2943 }
2944
2945 insp = &list;
2946 while (*insp) {
2947 if (u->type < (*insp)->type ||
2948 (u->type == (*insp)->type &&
2949 u->ino < (*insp)->ino))
2950 break;
2951 insp = &(*insp)->next;
2952 }
2953 u->next = *insp;
2954 *insp = u;
2955
2956 if (name[0]) {
Vadim Kochan99bb68f2015-07-21 10:53:19 -07002957 if ((u->name = malloc(strlen(name)+1)) == NULL)
2958 break;
2959 strcpy(u->name, name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002960 }
2961 if (++cnt > MAX_UNIX_REMEMBER) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002962 unix_stats_print(list, f);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002963 unix_list_free(list);
2964 list = NULL;
2965 cnt = 0;
2966 }
2967 }
Eric Dumazeta3fd8e52012-01-30 17:05:45 +01002968 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002969 if (list) {
Vadim Kochanbf4ceee2015-01-04 22:18:38 +02002970 unix_stats_print(list, f);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002971 unix_list_free(list);
2972 list = NULL;
2973 cnt = 0;
2974 }
2975
2976 return 0;
2977}
2978
Vadim Kochan89f634f2015-02-13 22:13:59 +02002979static int packet_stats_print(struct sockstat *s, const struct filter *f)
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002980{
2981 char *buf = NULL;
Vadim Kochanb217df12015-02-13 22:14:03 +02002982 const char *addr, *port;
2983 char ll_name[16];
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002984
2985 if (f->f) {
Vadim Kochan89f634f2015-02-13 22:13:59 +02002986 s->local.family = AF_PACKET;
2987 s->remote.family = AF_PACKET;
Vadim Kochan89f634f2015-02-13 22:13:59 +02002988 s->local.data[0] = s->prot;
Vadim Kochan89f634f2015-02-13 22:13:59 +02002989 if (run_ssfilter(f->f, s) == 0)
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002990 return 1;
2991 }
2992
Vadim Kochan2d791bc2015-02-13 22:14:01 +02002993 sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr");
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002994
Vadim Kochanb217df12015-02-13 22:14:03 +02002995 if (s->prot == 3)
2996 addr = "*";
2997 else
2998 addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name));
Vadim Kochan4a0053b2015-01-04 22:18:39 +02002999
Vadim Kochanb217df12015-02-13 22:14:03 +02003000 if (s->iface == 0)
3001 port = "*";
3002 else
3003 port = xll_index_to_name(s->iface);
3004
3005 sock_addr_print(addr, ":", port, NULL);
3006 sock_addr_print("", "*", "", NULL);
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003007
3008 if (show_proc_ctx || show_sock_ctx) {
3009 if (find_entry(s->ino, &buf,
3010 (show_proc_ctx & show_sock_ctx) ?
3011 PROC_SOCK_CTX : PROC_CTX) > 0) {
3012 printf(" users:(%s)", buf);
3013 free(buf);
3014 }
3015 } else if (show_users) {
3016 if (find_entry(s->ino, &buf, USERS) > 0) {
3017 printf(" users:(%s)", buf);
3018 free(buf);
3019 }
3020 }
3021
Vadim Kochanf1b39e12015-02-13 22:14:02 +02003022 if (show_details)
3023 sock_details_print(s);
3024
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003025 return 0;
3026}
3027
Vadim Kochan2631b852015-05-15 17:19:30 +03003028static void packet_show_ring(struct packet_diag_ring *ring)
3029{
3030 printf("blk_size:%d", ring->pdr_block_size);
3031 printf(",blk_nr:%d", ring->pdr_block_nr);
3032 printf(",frm_size:%d", ring->pdr_frame_size);
3033 printf(",frm_nr:%d", ring->pdr_frame_nr);
3034 printf(",tmo:%d", ring->pdr_retire_tmo);
3035 printf(",features:0x%x", ring->pdr_features);
3036}
3037
vadimk8a4025f2014-12-04 12:32:58 +02003038static int packet_show_sock(const struct sockaddr_nl *addr,
3039 struct nlmsghdr *nlh, void *arg)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003040{
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003041 const struct filter *f = arg;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003042 struct packet_diag_msg *r = NLMSG_DATA(nlh);
Vadim Kochan2631b852015-05-15 17:19:30 +03003043 struct packet_diag_info *pinfo = NULL;
3044 struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003045 struct rtattr *tb[PACKET_DIAG_MAX+1];
Vadim Kochan89f634f2015-02-13 22:13:59 +02003046 struct sockstat stat = {};
Vadim Kochan2631b852015-05-15 17:19:30 +03003047 uint32_t fanout = 0;
3048 bool has_fanout = false;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003049
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003050 parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1),
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003051 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3052
3053 /* use /proc/net/packet if all info are not available */
3054 if (!tb[PACKET_DIAG_MEMINFO])
3055 return -1;
3056
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003057 stat.type = r->pdiag_type;
3058 stat.prot = r->pdiag_num;
3059 stat.ino = r->pdiag_ino;
3060 stat.state = SS_CLOSE;
Vadim Kochanf1b39e12015-02-13 22:14:02 +02003061 stat.sk = cookie_sk_get(&r->pdiag_cookie[0]);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003062
3063 if (tb[PACKET_DIAG_MEMINFO]) {
3064 __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003065
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003066 stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003067 }
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003068
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003069 if (tb[PACKET_DIAG_INFO]) {
Vadim Kochan2631b852015-05-15 17:19:30 +03003070 pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003071 stat.lport = stat.iface = pinfo->pdi_index;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003072 }
Richard Haines116ac922014-03-07 10:36:52 +00003073
Vadim Kochanf1b39e12015-02-13 22:14:02 +02003074 if (tb[PACKET_DIAG_UID])
3075 stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]);
3076
Vadim Kochan2631b852015-05-15 17:19:30 +03003077 if (tb[PACKET_DIAG_RX_RING])
3078 ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
3079
3080 if (tb[PACKET_DIAG_TX_RING])
3081 ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
3082
3083 if (tb[PACKET_DIAG_FANOUT]) {
3084 has_fanout = true;
3085 fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]);
3086 }
3087
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003088 if (packet_stats_print(&stat, f))
3089 return 0;
3090
Vadim Kochan2631b852015-05-15 17:19:30 +03003091 if (show_details) {
3092 if (pinfo) {
3093 printf("\n\tver:%d", pinfo->pdi_version);
3094 printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
3095 printf(" flags( ");
3096 if (pinfo->pdi_flags & PDI_RUNNING)
3097 printf("running");
3098 if (pinfo->pdi_flags & PDI_AUXDATA)
3099 printf(" auxdata");
3100 if (pinfo->pdi_flags & PDI_ORIGDEV)
3101 printf(" origdev");
3102 if (pinfo->pdi_flags & PDI_VNETHDR)
3103 printf(" vnethdr");
3104 if (pinfo->pdi_flags & PDI_LOSS)
3105 printf(" loss");
3106 if (!pinfo->pdi_flags)
3107 printf("0");
3108 printf(" )");
3109 }
3110 if (ring_rx) {
3111 printf("\n\tring_rx(");
3112 packet_show_ring(ring_rx);
3113 printf(")");
3114 }
3115 if (ring_tx) {
3116 printf("\n\tring_tx(");
3117 packet_show_ring(ring_tx);
3118 printf(")");
3119 }
3120 if (has_fanout) {
3121 uint16_t type = (fanout >> 16) & 0xffff;
3122
3123 printf("\n\tfanout(");
3124 printf("id:%d,", fanout & 0xffff);
3125 printf("type:");
3126
3127 if (type == 0)
3128 printf("hash");
3129 else if (type == 1)
3130 printf("lb");
3131 else if (type == 2)
3132 printf("cpu");
3133 else if (type == 3)
3134 printf("roll");
3135 else if (type == 4)
3136 printf("random");
3137 else if (type == 5)
3138 printf("qm");
3139 else
3140 printf("0x%x", type);
3141
3142 printf(")");
3143 }
3144 }
3145
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003146 if (show_bpf && tb[PACKET_DIAG_FILTER]) {
3147 struct sock_filter *fil =
3148 RTA_DATA(tb[PACKET_DIAG_FILTER]);
3149 int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) /
3150 sizeof(struct sock_filter);
3151
3152 printf("\n\tbpf filter (%d): ", num);
3153 while (num) {
3154 printf(" 0x%02x %u %u %u,",
3155 fil->code, fil->jt, fil->jf, fil->k);
3156 num--;
3157 fil++;
3158 }
3159 }
3160 printf("\n");
3161 return 0;
3162}
3163
vadimk8a4025f2014-12-04 12:32:58 +02003164static int packet_show_netlink(struct filter *f)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003165{
vadimk5fb421d2014-10-30 18:49:25 +02003166 DIAG_REQUEST(req, struct packet_diag_req r);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003167
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003168 req.r.sdiag_family = AF_PACKET;
Vadim Kochan2631b852015-05-15 17:19:30 +03003169 req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
3170 PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003171
vadimk8a4025f2014-12-04 12:32:58 +02003172 return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003173}
3174
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003175static int packet_show_line(char *buf, const struct filter *f, int fam)
3176{
3177 unsigned long long sk;
Vadim Kochan89f634f2015-02-13 22:13:59 +02003178 struct sockstat stat = {};
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003179 int type, prot, iface, state, rq, uid, ino;
3180
3181 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
3182 &sk,
3183 &type, &prot, &iface, &state,
3184 &rq, &uid, &ino);
3185
3186 if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
3187 return 0;
3188 if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
3189 return 0;
3190
3191 stat.type = type;
3192 stat.prot = prot;
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003193 stat.lport = stat.iface = iface;
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003194 stat.state = state;
3195 stat.rq = rq;
3196 stat.uid = uid;
3197 stat.ino = ino;
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003198 stat.state = SS_CLOSE;
3199
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003200 if (packet_stats_print(&stat, f))
3201 return 0;
3202
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003203 printf("\n");
3204 return 0;
3205}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003206
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003207static int packet_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003208{
3209 FILE *fp;
Phil Sutterb95d28c2015-08-06 14:24:34 +02003210 int rc = 0;
Stephen Hemminger3d0b7432014-12-20 15:47:17 -08003211
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003212 if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
Vadim Kochanb9ea4452014-12-20 15:40:55 -08003213 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003214
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003215 if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
3216 packet_show_netlink(f) == 0)
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003217 return 0;
3218
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003219 if ((fp = net_packet_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003220 return -1;
Vadim Kochan4a0053b2015-01-04 22:18:39 +02003221 if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
Phil Sutterb95d28c2015-08-06 14:24:34 +02003222 rc = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003223
Phil Sutterb95d28c2015-08-06 14:24:34 +02003224 fclose(fp);
3225 return rc;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003226}
3227
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003228static int netlink_show_one(struct filter *f,
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003229 int prot, int pid, unsigned int groups,
3230 int state, int dst_pid, unsigned int dst_group,
Andrey Vagin129709a2013-06-05 12:41:59 +04003231 int rq, int wq,
3232 unsigned long long sk, unsigned long long cb)
3233{
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003234 struct sockstat st;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003235
Vadim Kochanb217df12015-02-13 22:14:03 +02003236 SPRINT_BUF(prot_buf) = {};
3237 const char *prot_name;
3238 char procname[64] = {};
vadimkeef43b52014-10-30 17:33:36 +02003239
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003240 st.state = SS_CLOSE;
3241 st.rq = rq;
3242 st.wq = wq;
3243
Andrey Vagin129709a2013-06-05 12:41:59 +04003244 if (f->f) {
Vadim Kochan055840f2015-02-13 22:13:58 +02003245 st.local.family = AF_NETLINK;
3246 st.remote.family = AF_NETLINK;
3247 st.rport = -1;
3248 st.lport = pid;
3249 st.local.data[0] = prot;
Vadim Kochan055840f2015-02-13 22:13:58 +02003250 if (run_ssfilter(f->f, &st) == 0)
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003251 return 1;
Andrey Vagin129709a2013-06-05 12:41:59 +04003252 }
3253
Vadim Kochan2d791bc2015-02-13 22:14:01 +02003254 sock_state_print(&st, "nl");
vadimkeef43b52014-10-30 17:33:36 +02003255
Vadim Kochanb217df12015-02-13 22:14:03 +02003256 if (resolve_services)
3257 prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf));
3258 else
3259 prot_name = int_to_str(prot, prot_buf);
vadimkeef43b52014-10-30 17:33:36 +02003260
Andrey Vagin129709a2013-06-05 12:41:59 +04003261 if (pid == -1) {
Vadim Kochanb217df12015-02-13 22:14:03 +02003262 procname[0] = '*';
Andrey Vagin129709a2013-06-05 12:41:59 +04003263 } else if (resolve_services) {
3264 int done = 0;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003265
Andrey Vagin129709a2013-06-05 12:41:59 +04003266 if (!pid) {
3267 done = 1;
Vadim Kochanb217df12015-02-13 22:14:03 +02003268 strncpy(procname, "kernel", 6);
Andrey Vagin129709a2013-06-05 12:41:59 +04003269 } else if (pid > 0) {
Andrey Vagin129709a2013-06-05 12:41:59 +04003270 FILE *fp;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003271
willy tarreau0ee90522015-10-06 12:09:33 +02003272 snprintf(procname, sizeof(procname), "%s/%d/stat",
Andrey Vagin129709a2013-06-05 12:41:59 +04003273 getenv("PROC_ROOT") ? : "/proc", pid);
3274 if ((fp = fopen(procname, "r")) != NULL) {
3275 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
willy tarreau0ee90522015-10-06 12:09:33 +02003276 snprintf(procname+strlen(procname),
3277 sizeof(procname)-strlen(procname),
3278 "/%d", pid);
Andrey Vagin129709a2013-06-05 12:41:59 +04003279 done = 1;
3280 }
3281 fclose(fp);
3282 }
3283 }
3284 if (!done)
Vadim Kochanb217df12015-02-13 22:14:03 +02003285 int_to_str(pid, procname);
Andrey Vagin129709a2013-06-05 12:41:59 +04003286 } else {
Vadim Kochanb217df12015-02-13 22:14:03 +02003287 int_to_str(pid, procname);
Andrey Vagin129709a2013-06-05 12:41:59 +04003288 }
Andrey Vaginf271fe02013-06-05 12:42:00 +04003289
Vadim Kochanb217df12015-02-13 22:14:03 +02003290 sock_addr_print(prot_name, ":", procname, NULL);
3291
Andrey Vaginf271fe02013-06-05 12:42:00 +04003292 if (state == NETLINK_CONNECTED) {
Vadim Kochanb217df12015-02-13 22:14:03 +02003293 char dst_group_buf[30];
3294 char dst_pid_buf[30];
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003295
Vadim Kochanb217df12015-02-13 22:14:03 +02003296 sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
3297 int_to_str(dst_pid, dst_pid_buf), NULL);
Andrey Vaginf271fe02013-06-05 12:42:00 +04003298 } else {
Vadim Kochanb217df12015-02-13 22:14:03 +02003299 sock_addr_print("", "*", "", NULL);
Andrey Vaginf271fe02013-06-05 12:42:00 +04003300 }
Andrey Vagin129709a2013-06-05 12:41:59 +04003301
Richard Haines116ac922014-03-07 10:36:52 +00003302 char *pid_context = NULL;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003303
Richard Haines116ac922014-03-07 10:36:52 +00003304 if (show_proc_ctx) {
3305 /* The pid value will either be:
3306 * 0 if destination kernel - show kernel initial context.
3307 * A valid process pid - use getpidcon.
3308 * A unique value allocated by the kernel or netlink user
3309 * to the process - show context as "not available".
3310 */
3311 if (!pid)
3312 security_get_initial_context("kernel", &pid_context);
3313 else if (pid > 0)
3314 getpidcon(pid, &pid_context);
3315
3316 if (pid_context != NULL) {
3317 printf("proc_ctx=%-*s ", serv_width, pid_context);
3318 free(pid_context);
3319 } else {
3320 printf("proc_ctx=%-*s ", serv_width, "unavailable");
3321 }
3322 }
3323
Andrey Vagin129709a2013-06-05 12:41:59 +04003324 if (show_details) {
3325 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
3326 }
3327 printf("\n");
3328
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003329 return 0;
Andrey Vagin129709a2013-06-05 12:41:59 +04003330}
3331
vadimk8a4025f2014-12-04 12:32:58 +02003332static int netlink_show_sock(const struct sockaddr_nl *addr,
3333 struct nlmsghdr *nlh, void *arg)
Andrey Vaginecb928c2013-06-05 12:42:01 +04003334{
vadimk8a4025f2014-12-04 12:32:58 +02003335 struct filter *f = (struct filter *)arg;
Andrey Vaginecb928c2013-06-05 12:42:01 +04003336 struct netlink_diag_msg *r = NLMSG_DATA(nlh);
3337 struct rtattr *tb[NETLINK_DIAG_MAX+1];
3338 int rq = 0, wq = 0;
3339 unsigned long groups = 0;
3340
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003341 parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1),
Andrey Vaginecb928c2013-06-05 12:42:01 +04003342 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
3343
3344 if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
3345 groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]);
3346
3347 if (tb[NETLINK_DIAG_MEMINFO]) {
3348 const __u32 *skmeminfo;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003349
Andrey Vaginecb928c2013-06-05 12:42:01 +04003350 skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
3351
3352 rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
3353 wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC];
3354 }
3355
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003356 if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups,
Andrey Vaginecb928c2013-06-05 12:42:01 +04003357 r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group,
Vadim Kochan5f24ec02015-02-22 22:23:11 +02003358 rq, wq, 0, 0)) {
3359 return 0;
3360 }
Andrey Vaginecb928c2013-06-05 12:42:01 +04003361
3362 if (show_mem) {
3363 printf("\t");
3364 print_skmeminfo(tb, NETLINK_DIAG_MEMINFO);
3365 printf("\n");
3366 }
3367
3368 return 0;
3369}
3370
vadimk8a4025f2014-12-04 12:32:58 +02003371static int netlink_show_netlink(struct filter *f)
Andrey Vaginecb928c2013-06-05 12:42:01 +04003372{
vadimk5fb421d2014-10-30 18:49:25 +02003373 DIAG_REQUEST(req, struct netlink_diag_req r);
Andrey Vaginecb928c2013-06-05 12:42:01 +04003374
3375 req.r.sdiag_family = AF_NETLINK;
3376 req.r.sdiag_protocol = NDIAG_PROTO_ALL;
3377 req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO;
3378
vadimk8a4025f2014-12-04 12:32:58 +02003379 return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock);
Andrey Vaginecb928c2013-06-05 12:42:01 +04003380}
3381
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003382static int netlink_show(struct filter *f)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003383{
3384 FILE *fp;
3385 char buf[256];
3386 int prot, pid;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003387 unsigned int groups;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003388 int rq, wq, rc;
3389 unsigned long long sk, cb;
3390
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003391 if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
Vadim Kochanb9ea4452014-12-20 15:40:55 -08003392 return 0;
3393
Andrey Vagin129709a2013-06-05 12:41:59 +04003394 if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
vadimk8a4025f2014-12-04 12:32:58 +02003395 netlink_show_netlink(f) == 0)
Andrey Vagin129709a2013-06-05 12:41:59 +04003396 return 0;
3397
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003398 if ((fp = net_netlink_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003399 return -1;
Phil Sutter61170fd2015-11-28 01:00:05 +01003400 if (!fgets(buf, sizeof(buf), fp)) {
Phil Sutterd572ed42015-11-28 01:00:04 +01003401 fclose(fp);
3402 return -1;
3403 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003404
Phil Sutter61170fd2015-11-28 01:00:05 +01003405 while (fgets(buf, sizeof(buf), fp)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003406 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
3407 &sk,
3408 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
3409
Andrey Vaginf271fe02013-06-05 12:42:00 +04003410 netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003411 }
3412
Phil Sutterb95d28c2015-08-06 14:24:34 +02003413 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003414 return 0;
3415}
3416
Craig Gallek6885e3b2015-06-17 11:14:48 -04003417struct sock_diag_msg {
3418 __u8 sdiag_family;
3419};
3420
3421static int generic_show_sock(const struct sockaddr_nl *addr,
3422 struct nlmsghdr *nlh, void *arg)
3423{
3424 struct sock_diag_msg *r = NLMSG_DATA(nlh);
3425 struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX };
3426
3427 switch (r->sdiag_family) {
3428 case AF_INET:
3429 case AF_INET6:
3430 return show_one_inet_sock(addr, nlh, &inet_arg);
3431 case AF_UNIX:
3432 return unix_show_sock(addr, nlh, arg);
3433 case AF_PACKET:
3434 return packet_show_sock(addr, nlh, arg);
3435 case AF_NETLINK:
3436 return netlink_show_sock(addr, nlh, arg);
3437 default:
3438 return -1;
3439 }
3440}
3441
3442static int handle_follow_request(struct filter *f)
3443{
3444 int ret = -1;
3445 int groups = 0;
3446 struct rtnl_handle rth;
3447
3448 if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB))
3449 groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1);
3450 if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB))
3451 groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1);
3452 if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB))
3453 groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1);
3454 if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB))
3455 groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1);
3456
3457 if (groups == 0)
3458 return -1;
3459
3460 if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG))
3461 return -1;
3462
3463 rth.dump = 0;
3464 rth.local.nl_pid = 0;
3465
3466 if (rtnl_dump_filter(&rth, generic_show_sock, f))
3467 goto Exit;
3468
3469 ret = 0;
3470Exit:
3471 rtnl_close(&rth);
3472 return ret;
3473}
3474
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003475struct snmpstat {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003476 int tcp_estab;
3477};
3478
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003479static int get_snmp_int(char *proto, char *key, int *result)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003480{
3481 char buf[1024];
3482 FILE *fp;
3483 int protolen = strlen(proto);
3484 int keylen = strlen(key);
3485
3486 *result = 0;
3487
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003488 if ((fp = net_snmp_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003489 return -1;
3490
3491 while (fgets(buf, sizeof(buf), fp) != NULL) {
3492 char *p = buf;
3493 int pos = 0;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003494
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003495 if (memcmp(buf, proto, protolen))
3496 continue;
3497 while ((p = strchr(p, ' ')) != NULL) {
3498 pos++;
3499 p++;
3500 if (memcmp(p, key, keylen) == 0 &&
3501 (p[keylen] == ' ' || p[keylen] == '\n'))
3502 break;
3503 }
3504 if (fgets(buf, sizeof(buf), fp) == NULL)
3505 break;
3506 if (memcmp(buf, proto, protolen))
3507 break;
3508 p = buf;
3509 while ((p = strchr(p, ' ')) != NULL) {
3510 p++;
3511 if (--pos == 0) {
3512 sscanf(p, "%d", result);
3513 fclose(fp);
3514 return 0;
3515 }
3516 }
3517 }
3518
3519 fclose(fp);
3520 errno = ESRCH;
3521 return -1;
3522}
3523
3524
3525/* Get stats from sockstat */
3526
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003527struct ssummary {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003528 int socks;
3529 int tcp_mem;
3530 int tcp_total;
3531 int tcp_orphans;
3532 int tcp_tws;
3533 int tcp4_hashed;
3534 int udp4;
3535 int raw4;
3536 int frag4;
3537 int frag4_mem;
3538 int tcp6_hashed;
3539 int udp6;
3540 int raw6;
3541 int frag6;
3542 int frag6_mem;
3543};
3544
Vadim Kochan055840f2015-02-13 22:13:58 +02003545static void get_sockstat_line(char *line, struct ssummary *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003546{
3547 char id[256], rem[256];
3548
3549 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
3550 return;
3551
3552 if (strcmp(id, "sockets:") == 0)
3553 sscanf(rem, "%*s%d", &s->socks);
3554 else if (strcmp(id, "UDP:") == 0)
3555 sscanf(rem, "%*s%d", &s->udp4);
3556 else if (strcmp(id, "UDP6:") == 0)
3557 sscanf(rem, "%*s%d", &s->udp6);
3558 else if (strcmp(id, "RAW:") == 0)
3559 sscanf(rem, "%*s%d", &s->raw4);
3560 else if (strcmp(id, "RAW6:") == 0)
3561 sscanf(rem, "%*s%d", &s->raw6);
3562 else if (strcmp(id, "TCP6:") == 0)
3563 sscanf(rem, "%*s%d", &s->tcp6_hashed);
3564 else if (strcmp(id, "FRAG:") == 0)
3565 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
3566 else if (strcmp(id, "FRAG6:") == 0)
3567 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
3568 else if (strcmp(id, "TCP:") == 0)
3569 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
3570 &s->tcp4_hashed,
3571 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
3572}
3573
Vadim Kochan055840f2015-02-13 22:13:58 +02003574static int get_sockstat(struct ssummary *s)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003575{
3576 char buf[256];
3577 FILE *fp;
3578
3579 memset(s, 0, sizeof(*s));
3580
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003581 if ((fp = net_sockstat_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003582 return -1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003583 while (fgets(buf, sizeof(buf), fp) != NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003584 get_sockstat_line(buf, s);
3585 fclose(fp);
3586
Stephen Hemmingerab01dbb2007-07-18 15:31:29 +01003587 if ((fp = net_sockstat6_open()) == NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003588 return 0;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003589 while (fgets(buf, sizeof(buf), fp) != NULL)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003590 get_sockstat_line(buf, s);
3591 fclose(fp);
3592
3593 return 0;
3594}
3595
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003596static int print_summary(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003597{
Vadim Kochan055840f2015-02-13 22:13:58 +02003598 struct ssummary s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003599 struct snmpstat sn;
3600
3601 if (get_sockstat(&s) < 0)
3602 perror("ss: get_sockstat");
3603 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
3604 perror("ss: get_snmpstat");
3605
Bryton Leea221d622015-02-12 14:16:04 +08003606 get_slabstat(&slabstat);
3607
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003608 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
3609
3610 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
3611 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
3612 sn.tcp_estab,
3613 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
3614 s.tcp_orphans,
3615 slabstat.tcp_syns,
3616 s.tcp_tws, slabstat.tcp_tws,
3617 slabstat.tcp_ports
3618 );
3619
3620 printf("\n");
3621 printf("Transport Total IP IPv6\n");
3622 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
3623 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
3624 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
3625 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 -08003626 printf("INET %-9d %-9d %-9d\n",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003627 s.raw4+s.udp4+s.tcp4_hashed+
3628 s.raw6+s.udp6+s.tcp6_hashed,
3629 s.raw4+s.udp4+s.tcp4_hashed,
3630 s.raw6+s.udp6+s.tcp6_hashed);
3631 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
3632
3633 printf("\n");
3634
3635 return 0;
3636}
3637
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003638static void _usage(FILE *dest)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003639{
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003640 fprintf(dest,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003641"Usage: ss [ OPTIONS ]\n"
3642" ss [ OPTIONS ] [ FILTER ]\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003643" -h, --help this message\n"
3644" -V, --version output version information\n"
3645" -n, --numeric don't resolve service names\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003646" -r, --resolve resolve host names\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003647" -a, --all display all sockets\n"
3648" -l, --listening display listening sockets\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003649" -o, --options show timer information\n"
3650" -e, --extended show detailed socket information\n"
3651" -m, --memory show socket memory usage\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003652" -p, --processes show process using socket\n"
3653" -i, --info show internal TCP information\n"
3654" -s, --summary show socket usage summary\n"
Rami Rosenb0f01cf2013-05-24 12:42:40 +03003655" -b, --bpf show bpf filter socket information\n"
Craig Gallek6885e3b2015-06-17 11:14:48 -04003656" -E, --events continually display sockets as they are destroyed\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003657" -Z, --context display process SELinux security contexts\n"
3658" -z, --contexts display process and socket SELinux security contexts\n"
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003659" -N, --net switch to the specified network namespace name\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003660"\n"
3661" -4, --ipv4 display only IP version 4 sockets\n"
3662" -6, --ipv6 display only IP version 6 sockets\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003663" -0, --packet display PACKET sockets\n"
3664" -t, --tcp display only TCP sockets\n"
3665" -u, --udp display only UDP sockets\n"
3666" -d, --dccp display only DCCP sockets\n"
3667" -w, --raw display only RAW sockets\n"
3668" -x, --unix display only Unix domain sockets\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003669" -f, --family=FAMILY display sockets of type FAMILY\n"
Fabien Sironebef3172016-06-06 14:53:38 +00003670" FAMILY := {inet|inet6|link|unix|netlink|help}\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003671"\n"
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09003672" -K, --kill forcibly close sockets, display what was closed\n"
David Ahern7a4559f2016-07-15 15:45:39 -07003673" -H, --no-header Suppress header line\n"
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09003674"\n"
Petr Sabata583de142011-10-06 14:45:32 +02003675" -A, --query=QUERY, --socket=QUERY\n"
Masatake YAMATO56dee732014-01-08 20:13:48 +09003676" 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 +00003677"\n"
Petr Sabata583de142011-10-06 14:45:32 +02003678" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003679" -F, --filter=FILE read filter information from FILE\n"
Vadim Kochanff041f12015-01-08 04:30:43 +02003680" FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n"
3681" STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n"
3682" TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n"
3683" connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3684" synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n"
3685" bucket := {syn-recv|time-wait}\n"
3686" big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n"
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003687 );
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003688}
3689
3690static void help(void) __attribute__((noreturn));
3691static void help(void)
3692{
3693 _usage(stdout);
3694 exit(0);
3695}
3696
3697static void usage(void) __attribute__((noreturn));
3698static void usage(void)
3699{
3700 _usage(stderr);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003701 exit(-1);
3702}
3703
3704
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -08003705static int scan_state(const char *state)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003706{
3707 int i;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003708
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003709 if (strcasecmp(state, "close") == 0 ||
3710 strcasecmp(state, "closed") == 0)
3711 return (1<<SS_CLOSE);
3712 if (strcasecmp(state, "syn-rcv") == 0)
3713 return (1<<SS_SYN_RECV);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003714 if (strcasecmp(state, "established") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003715 return (1<<SS_ESTABLISHED);
3716 if (strcasecmp(state, "all") == 0)
3717 return SS_ALL;
3718 if (strcasecmp(state, "connected") == 0)
3719 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003720 if (strcasecmp(state, "synchronized") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003721 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
3722 if (strcasecmp(state, "bucket") == 0)
3723 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
3724 if (strcasecmp(state, "big") == 0)
3725 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003726 for (i = 0; i < SS_MAX; i++) {
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003727 if (strcasecmp(state, sstate_namel[i]) == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003728 return (1<<i);
3729 }
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003730
3731 fprintf(stderr, "ss: wrong state name: %s\n", state);
3732 exit(-1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003733}
3734
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003735static const struct option long_opts[] = {
3736 { "numeric", 0, 0, 'n' },
3737 { "resolve", 0, 0, 'r' },
3738 { "options", 0, 0, 'o' },
3739 { "extended", 0, 0, 'e' },
3740 { "memory", 0, 0, 'm' },
3741 { "info", 0, 0, 'i' },
3742 { "processes", 0, 0, 'p' },
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003743 { "bpf", 0, 0, 'b' },
Craig Gallek6885e3b2015-06-17 11:14:48 -04003744 { "events", 0, 0, 'E' },
shemminger351efcd2005-09-01 19:21:50 +00003745 { "dccp", 0, 0, 'd' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003746 { "tcp", 0, 0, 't' },
3747 { "udp", 0, 0, 'u' },
3748 { "raw", 0, 0, 'w' },
3749 { "unix", 0, 0, 'x' },
3750 { "all", 0, 0, 'a' },
3751 { "listening", 0, 0, 'l' },
3752 { "ipv4", 0, 0, '4' },
3753 { "ipv6", 0, 0, '6' },
3754 { "packet", 0, 0, '0' },
3755 { "family", 1, 0, 'f' },
3756 { "socket", 1, 0, 'A' },
Petr Sabata583de142011-10-06 14:45:32 +02003757 { "query", 1, 0, 'A' },
osdl.net!shemmingerc3f346b2005-01-18 22:31:42 +00003758 { "summary", 0, 0, 's' },
Petr Sabata583de142011-10-06 14:45:32 +02003759 { "diag", 1, 0, 'D' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003760 { "filter", 1, 0, 'F' },
3761 { "version", 0, 0, 'V' },
3762 { "help", 0, 0, 'h' },
Richard Haines116ac922014-03-07 10:36:52 +00003763 { "context", 0, 0, 'Z' },
3764 { "contexts", 0, 0, 'z' },
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003765 { "net", 1, 0, 'N' },
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09003766 { "kill", 0, 0, 'K' },
David Ahern7a4559f2016-07-15 15:45:39 -07003767 { "no-header", 0, 0, 'H' },
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003768 { 0 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003769
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003770};
3771
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003772int main(int argc, char *argv[])
3773{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003774 int saw_states = 0;
3775 int saw_query = 0;
3776 int do_summary = 0;
osdl.org!shemminger7d105b52004-06-02 20:22:08 +00003777 const char *dump_tcpdiag = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003778 FILE *filter_fp = NULL;
3779 int ch;
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003780 int state_filter = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003781
David Ahern7a4559f2016-07-15 15:45:39 -07003782 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KH",
osdl.org!shemmingerab611592004-06-09 21:30:13 +00003783 long_opts, NULL)) != EOF) {
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003784 switch (ch) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003785 case 'n':
3786 resolve_services = 0;
3787 break;
3788 case 'r':
3789 resolve_hosts = 1;
3790 break;
3791 case 'o':
3792 show_options = 1;
3793 break;
3794 case 'e':
3795 show_options = 1;
3796 show_details++;
3797 break;
3798 case 'm':
3799 show_mem = 1;
3800 break;
3801 case 'i':
3802 show_tcpinfo = 1;
3803 break;
3804 case 'p':
3805 show_users++;
Steve Finkfbc0f872010-06-09 11:42:38 -07003806 user_ent_hash_build();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003807 break;
Nicolas Dichtel372c30d2013-05-17 08:42:34 -07003808 case 'b':
3809 show_options = 1;
3810 show_bpf++;
3811 break;
Craig Gallek6885e3b2015-06-17 11:14:48 -04003812 case 'E':
3813 follow_events = 1;
3814 break;
shemminger351efcd2005-09-01 19:21:50 +00003815 case 'd':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003816 filter_db_set(&current_filter, DCCP_DB);
shemminger351efcd2005-09-01 19:21:50 +00003817 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003818 case 't':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003819 filter_db_set(&current_filter, TCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003820 break;
3821 case 'u':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003822 filter_db_set(&current_filter, UDP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003823 break;
3824 case 'w':
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003825 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003826 break;
3827 case 'x':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003828 filter_af_set(&current_filter, AF_UNIX);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003829 break;
3830 case 'a':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003831 state_filter = SS_ALL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003832 break;
3833 case 'l':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003834 state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003835 break;
3836 case '4':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003837 filter_af_set(&current_filter, AF_INET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003838 break;
3839 case '6':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003840 filter_af_set(&current_filter, AF_INET6);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003841 break;
3842 case '0':
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003843 filter_af_set(&current_filter, AF_PACKET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003844 break;
3845 case 'f':
3846 if (strcmp(optarg, "inet") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003847 filter_af_set(&current_filter, AF_INET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003848 else if (strcmp(optarg, "inet6") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003849 filter_af_set(&current_filter, AF_INET6);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003850 else if (strcmp(optarg, "link") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003851 filter_af_set(&current_filter, AF_PACKET);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003852 else if (strcmp(optarg, "unix") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003853 filter_af_set(&current_filter, AF_UNIX);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003854 else if (strcmp(optarg, "netlink") == 0)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003855 filter_af_set(&current_filter, AF_NETLINK);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003856 else if (strcmp(optarg, "help") == 0)
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003857 help();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003858 else {
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003859 fprintf(stderr, "ss: \"%s\" is invalid family\n",
3860 optarg);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003861 usage();
3862 }
3863 break;
3864 case 'A':
3865 {
3866 char *p, *p1;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003867
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003868 if (!saw_query) {
3869 current_filter.dbs = 0;
Phil Sutter7f9dddb2015-08-07 15:31:27 +02003870 state_filter = state_filter ?
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07003871 state_filter : SS_CONN;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003872 saw_query = 1;
3873 do_default = 0;
3874 }
3875 p = p1 = optarg;
3876 do {
3877 if ((p1 = strchr(p, ',')) != NULL)
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003878 *p1 = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003879 if (strcmp(p, "all") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003880 filter_default_dbs(&current_filter);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003881 } else if (strcmp(p, "inet") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003882 filter_db_set(&current_filter, UDP_DB);
3883 filter_db_set(&current_filter, DCCP_DB);
3884 filter_db_set(&current_filter, TCP_DB);
3885 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003886 } else if (strcmp(p, "udp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003887 filter_db_set(&current_filter, UDP_DB);
shemminger351efcd2005-09-01 19:21:50 +00003888 } else if (strcmp(p, "dccp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003889 filter_db_set(&current_filter, DCCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003890 } else if (strcmp(p, "tcp") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003891 filter_db_set(&current_filter, TCP_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003892 } else if (strcmp(p, "raw") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003893 filter_db_set(&current_filter, RAW_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003894 } else if (strcmp(p, "unix") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003895 filter_db_set(&current_filter, UNIX_ST_DB);
3896 filter_db_set(&current_filter, UNIX_DG_DB);
3897 filter_db_set(&current_filter, UNIX_SQ_DB);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003898 } else if (strcasecmp(p, "unix_stream") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003899 strcmp(p, "u_str") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003900 filter_db_set(&current_filter, UNIX_ST_DB);
osdl.net!shemminger1a5bad52004-07-07 17:06:35 +00003901 } else if (strcasecmp(p, "unix_dgram") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003902 strcmp(p, "u_dgr") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003903 filter_db_set(&current_filter, UNIX_DG_DB);
Masatake YAMATO30b669d2014-01-08 20:13:46 +09003904 } else if (strcasecmp(p, "unix_seqpacket") == 0 ||
3905 strcmp(p, "u_seq") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003906 filter_db_set(&current_filter, UNIX_SQ_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003907 } else if (strcmp(p, "packet") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003908 filter_db_set(&current_filter, PACKET_R_DB);
3909 filter_db_set(&current_filter, PACKET_DG_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003910 } else if (strcmp(p, "packet_raw") == 0 ||
3911 strcmp(p, "p_raw") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003912 filter_db_set(&current_filter, PACKET_R_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003913 } else if (strcmp(p, "packet_dgram") == 0 ||
3914 strcmp(p, "p_dgr") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003915 filter_db_set(&current_filter, PACKET_DG_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003916 } else if (strcmp(p, "netlink") == 0) {
Vadim Kochan57ff5a12015-04-30 07:30:24 +03003917 filter_db_set(&current_filter, NETLINK_DB);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003918 } else {
3919 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
3920 usage();
3921 }
3922 p = p1 + 1;
3923 } while (p1);
3924 break;
3925 }
3926 case 's':
3927 do_summary = 1;
3928 break;
3929 case 'D':
3930 dump_tcpdiag = optarg;
3931 break;
3932 case 'F':
3933 if (filter_fp) {
3934 fprintf(stderr, "More than one filter file\n");
3935 exit(-1);
3936 }
3937 if (optarg[0] == '-')
3938 filter_fp = stdin;
3939 else
3940 filter_fp = fopen(optarg, "r");
3941 if (!filter_fp) {
3942 perror("fopen filter file");
3943 exit(-1);
3944 }
3945 break;
3946 case 'v':
3947 case 'V':
3948 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
3949 exit(0);
Richard Haines116ac922014-03-07 10:36:52 +00003950 case 'z':
3951 show_sock_ctx++;
3952 case 'Z':
3953 if (is_selinux_enabled() <= 0) {
3954 fprintf(stderr, "ss: SELinux is not enabled.\n");
3955 exit(1);
3956 }
3957 show_proc_ctx++;
3958 user_ent_hash_build();
3959 break;
Vadim Kochan95ce04b2015-02-08 08:58:43 +02003960 case 'N':
3961 if (netns_switch(optarg))
3962 exit(1);
3963 break;
Lorenzo Colittifb2594c2016-01-08 17:32:37 +09003964 case 'K':
3965 current_filter.kill = 1;
3966 break;
David Ahern7a4559f2016-07-15 15:45:39 -07003967 case 'H':
3968 show_header = 0;
3969 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003970 case 'h':
Andreas Henriksson7a96e192009-12-07 13:12:36 +01003971 help();
Phil Sutterf73105a2015-10-15 21:01:16 +02003972 case '?':
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003973 default:
3974 usage();
3975 }
3976 }
3977
3978 argc -= optind;
3979 argv += optind;
3980
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003981 if (do_summary) {
3982 print_summary();
3983 if (do_default && argc == 0)
3984 exit(0);
3985 }
3986
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003987 while (argc > 0) {
3988 if (strcmp(*argv, "state") == 0) {
3989 NEXT_ARG();
3990 if (!saw_states)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003991 state_filter = 0;
3992 state_filter |= scan_state(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00003993 saw_states = 1;
3994 } else if (strcmp(*argv, "exclude") == 0 ||
3995 strcmp(*argv, "excl") == 0) {
3996 NEXT_ARG();
3997 if (!saw_states)
Vadim Kochan9db7bf12015-01-04 22:18:40 +02003998 state_filter = SS_ALL;
3999 state_filter &= ~scan_state(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004000 saw_states = 1;
4001 } else {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004002 break;
4003 }
4004 argc--; argv++;
4005 }
4006
Vadim Kochan9db7bf12015-01-04 22:18:40 +02004007 if (do_default) {
4008 state_filter = state_filter ? state_filter : SS_CONN;
4009 filter_default_dbs(&current_filter);
Vadim Kochan9db7bf12015-01-04 22:18:40 +02004010 }
4011
Vadim Kochan57ff5a12015-04-30 07:30:24 +03004012 filter_states_set(&current_filter, state_filter);
4013 filter_merge_defaults(&current_filter);
4014
Vadim Kochan9db7bf12015-01-04 22:18:40 +02004015 if (resolve_services && resolve_hosts &&
4016 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
4017 init_service_resolver();
4018
4019
4020 if (current_filter.dbs == 0) {
4021 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
4022 exit(0);
4023 }
4024 if (current_filter.families == 0) {
4025 fprintf(stderr, "ss: no families to show with such filter.\n");
4026 exit(0);
4027 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004028 if (current_filter.states == 0) {
4029 fprintf(stderr, "ss: no socket states to show with such filter.\n");
4030 exit(0);
4031 }
4032
4033 if (dump_tcpdiag) {
4034 FILE *dump_fp = stdout;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -07004035
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004036 if (!(current_filter.dbs & (1<<TCP_DB))) {
4037 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
4038 exit(0);
4039 }
4040 if (dump_tcpdiag[0] != '-') {
4041 dump_fp = fopen(dump_tcpdiag, "w");
4042 if (!dump_tcpdiag) {
4043 perror("fopen dump file");
4044 exit(-1);
4045 }
4046 }
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00004047 inet_show_netlink(&current_filter, dump_fp, IPPROTO_TCP);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004048 fflush(dump_fp);
4049 exit(0);
4050 }
4051
Vadim Kochan1527a172015-02-13 13:01:08 +02004052 if (ssfilter_parse(&current_filter.f, argc, argv, filter_fp))
4053 usage();
4054
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004055 netid_width = 0;
4056 if (current_filter.dbs&(current_filter.dbs-1))
4057 netid_width = 5;
4058
4059 state_width = 0;
4060 if (current_filter.states&(current_filter.states-1))
4061 state_width = 10;
4062
4063 screen_width = 80;
4064 if (isatty(STDOUT_FILENO)) {
4065 struct winsize w;
4066
4067 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
4068 if (w.ws_col > 0)
4069 screen_width = w.ws_col;
4070 }
4071 }
4072
4073 addrp_width = screen_width;
4074 addrp_width -= netid_width+1;
4075 addrp_width -= state_width+1;
4076 addrp_width -= 14;
4077
4078 if (addrp_width&1) {
4079 if (netid_width)
4080 netid_width++;
4081 else if (state_width)
4082 state_width++;
4083 }
4084
4085 addrp_width /= 2;
4086 addrp_width--;
4087
4088 serv_width = resolve_services ? 7 : 5;
4089
4090 if (addrp_width < 15+serv_width+1)
4091 addrp_width = 15+serv_width+1;
4092
Stephen Hemmingerae665a52006-12-05 10:10:22 -08004093 addr_width = addrp_width - serv_width - 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004094
David Ahern7a4559f2016-07-15 15:45:39 -07004095 if (show_header) {
4096 if (netid_width)
4097 printf("%-*s ", netid_width, "Netid");
4098 if (state_width)
4099 printf("%-*s ", state_width, "State");
4100 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
4101 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004102
vadimkd68e00f2014-12-05 19:19:11 +02004103 /* Make enough space for the local/remote port field */
4104 addr_width -= 13;
4105 serv_width += 13;
vadimk2dc85482014-10-18 20:46:29 +03004106
David Ahern7a4559f2016-07-15 15:45:39 -07004107 if (show_header) {
4108 printf("%*s:%-*s %*s:%-*s\n",
4109 addr_width, "Local Address", serv_width, "Port",
4110 addr_width, "Peer Address", serv_width, "Port");
4111 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004112
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004113 fflush(stdout);
4114
Craig Gallek6885e3b2015-06-17 11:14:48 -04004115 if (follow_events)
4116 exit(handle_follow_request(&current_filter));
4117
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004118 if (current_filter.dbs & (1<<NETLINK_DB))
4119 netlink_show(&current_filter);
4120 if (current_filter.dbs & PACKET_DBM)
4121 packet_show(&current_filter);
4122 if (current_filter.dbs & UNIX_DBM)
4123 unix_show(&current_filter);
4124 if (current_filter.dbs & (1<<RAW_DB))
4125 raw_show(&current_filter);
4126 if (current_filter.dbs & (1<<UDP_DB))
4127 udp_show(&current_filter);
4128 if (current_filter.dbs & (1<<TCP_DB))
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00004129 tcp_show(&current_filter, IPPROTO_TCP);
shemminger351efcd2005-09-01 19:21:50 +00004130 if (current_filter.dbs & (1<<DCCP_DB))
Pavel Emelyanov3fe5b532012-10-25 03:18:31 +00004131 tcp_show(&current_filter, IPPROTO_DCCP);
Richard Haines116ac922014-03-07 10:36:52 +00004132
4133 if (show_users || show_proc_ctx || show_sock_ctx)
4134 user_ent_destroy();
4135
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00004136 return 0;
4137}