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