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