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