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