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