]>
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> | |
25 | #include <resolv.h> | |
26 | #include <dirent.h> | |
27 | #include <fnmatch.h> | |
ab61159a | 28 | #include <getopt.h> |
aba5acdf SH |
29 | |
30 | #include "utils.h" | |
31 | #include "rt_names.h" | |
32 | #include "ll_map.h" | |
33 | #include "libnetlink.h" | |
aba5acdf SH |
34 | #include "SNAPSHOT.h" |
35 | ||
77aa4d03 | 36 | #include <netinet/tcp.h> |
351efcde | 37 | #include <linux/inet_diag.h> |
aba5acdf SH |
38 | |
39 | int resolve_hosts = 0; | |
40 | int resolve_services = 1; | |
41 | int preferred_family = AF_UNSPEC; | |
42 | int show_options = 0; | |
43 | int show_details = 0; | |
44 | int show_users = 0; | |
45 | int show_mem = 0; | |
46 | int show_tcpinfo = 0; | |
47 | ||
48 | int netid_width; | |
49 | int state_width; | |
50 | int addrp_width; | |
51 | int addr_width; | |
52 | int serv_width; | |
53 | int screen_width; | |
54 | ||
55 | static const char *TCP_PROTO = "tcp"; | |
56 | static const char *UDP_PROTO = "udp"; | |
57 | static const char *RAW_PROTO = "raw"; | |
58 | static const char *dg_proto = NULL; | |
59 | ||
60 | enum | |
61 | { | |
62 | TCP_DB, | |
351efcde | 63 | DCCP_DB, |
aba5acdf SH |
64 | UDP_DB, |
65 | RAW_DB, | |
66 | UNIX_DG_DB, | |
67 | UNIX_ST_DB, | |
68 | PACKET_DG_DB, | |
69 | PACKET_R_DB, | |
70 | NETLINK_DB, | |
71 | MAX_DB | |
72 | }; | |
73 | ||
74 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) | |
75 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)) | |
76 | #define ALL_DB ((1<<MAX_DB)-1) | |
77 | ||
78 | enum { | |
7d105b56 SH |
79 | SS_UNKNOWN, |
80 | SS_ESTABLISHED, | |
81 | SS_SYN_SENT, | |
82 | SS_SYN_RECV, | |
83 | SS_FIN_WAIT1, | |
84 | SS_FIN_WAIT2, | |
85 | SS_TIME_WAIT, | |
86 | SS_CLOSE, | |
87 | SS_CLOSE_WAIT, | |
88 | SS_LAST_ACK, | |
89 | SS_LISTEN, | |
90 | SS_CLOSING, | |
91 | SS_MAX | |
aba5acdf SH |
92 | }; |
93 | ||
94 | #define SS_ALL ((1<<SS_MAX)-1) | |
95 | ||
96 | #include "ssfilter.h" | |
97 | ||
98 | struct filter | |
99 | { | |
100 | int dbs; | |
101 | int states; | |
102 | int families; | |
103 | struct ssfilter *f; | |
104 | }; | |
105 | ||
106 | struct filter default_filter = { | |
e7113c61 SH |
107 | .dbs = (1<<TCP_DB), |
108 | .states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)), | |
109 | .families= (1<<AF_INET)|(1<<AF_INET6), | |
aba5acdf SH |
110 | }; |
111 | ||
112 | struct filter current_filter; | |
113 | ||
ab01dbbb | 114 | static FILE *generic_proc_open(const char *env, const char *name) |
aba5acdf | 115 | { |
ab01dbbb | 116 | const char *p = getenv(env); |
aba5acdf | 117 | char store[128]; |
ab01dbbb | 118 | |
aba5acdf SH |
119 | if (!p) { |
120 | p = getenv("PROC_ROOT") ? : "/proc"; | |
121 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
122 | p = store; | |
123 | } | |
ab01dbbb SH |
124 | |
125 | return fopen(p, "r"); | |
aba5acdf SH |
126 | } |
127 | ||
ab01dbbb | 128 | static FILE *net_tcp_open(void) |
aba5acdf SH |
129 | { |
130 | return generic_proc_open("PROC_NET_TCP", "net/tcp"); | |
131 | } | |
132 | ||
ab01dbbb | 133 | static FILE *net_tcp6_open(void) |
aba5acdf SH |
134 | { |
135 | return generic_proc_open("PROC_NET_TCP6", "net/tcp6"); | |
136 | } | |
137 | ||
ab01dbbb | 138 | static FILE *net_udp_open(void) |
aba5acdf SH |
139 | { |
140 | return generic_proc_open("PROC_NET_UDP", "net/udp"); | |
141 | } | |
142 | ||
ab01dbbb | 143 | static FILE *net_udp6_open(void) |
aba5acdf SH |
144 | { |
145 | return generic_proc_open("PROC_NET_UDP6", "net/udp6"); | |
146 | } | |
147 | ||
ab01dbbb | 148 | static FILE *net_raw_open(void) |
aba5acdf SH |
149 | { |
150 | return generic_proc_open("PROC_NET_RAW", "net/raw"); | |
151 | } | |
152 | ||
ab01dbbb | 153 | static FILE *net_raw6_open(void) |
aba5acdf SH |
154 | { |
155 | return generic_proc_open("PROC_NET_RAW6", "net/raw6"); | |
156 | } | |
157 | ||
ab01dbbb | 158 | static FILE *net_unix_open(void) |
aba5acdf SH |
159 | { |
160 | return generic_proc_open("PROC_NET_UNIX", "net/unix"); | |
161 | } | |
162 | ||
ab01dbbb | 163 | static FILE *net_packet_open(void) |
aba5acdf SH |
164 | { |
165 | return generic_proc_open("PROC_NET_PACKET", "net/packet"); | |
166 | } | |
167 | ||
ab01dbbb | 168 | static FILE *net_netlink_open(void) |
aba5acdf SH |
169 | { |
170 | return generic_proc_open("PROC_NET_NETLINK", "net/netlink"); | |
171 | } | |
172 | ||
ab01dbbb | 173 | static FILE *slabinfo_open(void) |
aba5acdf SH |
174 | { |
175 | return generic_proc_open("PROC_SLABINFO", "slabinfo"); | |
176 | } | |
177 | ||
ab01dbbb | 178 | static FILE *net_sockstat_open(void) |
aba5acdf SH |
179 | { |
180 | return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat"); | |
181 | } | |
182 | ||
ab01dbbb | 183 | static FILE *net_sockstat6_open(void) |
aba5acdf SH |
184 | { |
185 | return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6"); | |
186 | } | |
187 | ||
ab01dbbb | 188 | static FILE *net_snmp_open(void) |
aba5acdf SH |
189 | { |
190 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); | |
191 | } | |
192 | ||
ab01dbbb | 193 | static FILE *ephemeral_ports_open(void) |
aba5acdf SH |
194 | { |
195 | return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range"); | |
196 | } | |
197 | ||
e7113c61 | 198 | int find_users(unsigned ino, char *buf, int buflen) |
aba5acdf SH |
199 | { |
200 | char pattern[64]; | |
201 | int pattern_len; | |
202 | char *ptr = buf; | |
203 | char name[1024]; | |
204 | DIR *dir; | |
205 | struct dirent *d; | |
206 | int cnt = 0; | |
207 | int nameoff; | |
208 | ||
209 | if (!ino) | |
210 | return 0; | |
211 | ||
e7113c61 | 212 | sprintf(pattern, "socket:[%u]", ino); |
aba5acdf SH |
213 | pattern_len = strlen(pattern); |
214 | ||
215 | strncpy(name, getenv("PROC_ROOT") ? : "/proc/", sizeof(name)/2); | |
216 | name[sizeof(name)/2] = 0; | |
217 | if (strlen(name) == 0 || | |
218 | name[strlen(name)-1] != '/') | |
219 | strcat(name, "/"); | |
220 | nameoff = strlen(name); | |
221 | if ((dir = opendir(name)) == NULL) | |
222 | return 0; | |
223 | ||
224 | while ((d = readdir(dir)) != NULL) { | |
225 | DIR *dir1; | |
226 | struct dirent *d1; | |
227 | int pid; | |
228 | int pos; | |
229 | char crap; | |
230 | char process[16]; | |
231 | ||
232 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) | |
233 | continue; | |
234 | ||
235 | sprintf(name+nameoff, "%d/fd/", pid); | |
236 | pos = strlen(name); | |
237 | if ((dir1 = opendir(name)) == NULL) | |
238 | continue; | |
239 | ||
240 | process[0] = 0; | |
241 | ||
242 | while ((d1 = readdir(dir1)) != NULL) { | |
243 | int fd, n; | |
244 | char lnk[64]; | |
245 | ||
246 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) | |
247 | continue; | |
248 | ||
249 | sprintf(name+pos, "%d", fd); | |
250 | n = readlink(name, lnk, sizeof(lnk)-1); | |
251 | if (n != pattern_len || | |
252 | memcmp(lnk, pattern, n)) | |
253 | continue; | |
254 | ||
255 | if (ptr-buf >= buflen-1) | |
256 | break; | |
257 | ||
258 | if (process[0] == 0) { | |
259 | char tmp[1024]; | |
260 | FILE *fp; | |
261 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", | |
262 | getenv("PROC_ROOT") ? : "/proc", pid); | |
263 | if ((fp = fopen(tmp, "r")) != NULL) { | |
264 | fscanf(fp, "%*d (%[^)])", process); | |
265 | fclose(fp); | |
266 | } | |
267 | } | |
268 | ||
269 | snprintf(ptr, buflen-(ptr-buf), "(\"%s\",%d,%d),", process, pid, fd); | |
270 | ptr += strlen(ptr); | |
271 | cnt++; | |
272 | } | |
273 | closedir(dir1); | |
274 | } | |
275 | closedir(dir); | |
276 | if (ptr != buf) | |
277 | ptr[-1] = 0; | |
278 | return cnt; | |
279 | } | |
280 | ||
281 | ||
282 | /* Get stats from slab */ | |
283 | ||
284 | struct slabstat | |
285 | { | |
286 | int socks; | |
287 | int tcp_ports; | |
288 | int tcp_tws; | |
289 | int tcp_syns; | |
290 | int skbs; | |
291 | }; | |
292 | ||
293 | struct slabstat slabstat; | |
294 | ||
ae665a52 | 295 | static const char *slabstat_ids[] = |
aba5acdf SH |
296 | { |
297 | "sock", | |
298 | "tcp_bind_bucket", | |
299 | "tcp_tw_bucket", | |
300 | "tcp_open_request", | |
301 | "skbuff_head_cache", | |
302 | }; | |
303 | ||
304 | int get_slabstat(struct slabstat *s) | |
305 | { | |
306 | char buf[256]; | |
307 | FILE *fp; | |
308 | int cnt; | |
309 | ||
310 | memset(s, 0, sizeof(*s)); | |
311 | ||
ab01dbbb SH |
312 | fp = slabinfo_open(); |
313 | if (!fp) | |
aba5acdf SH |
314 | return -1; |
315 | ||
316 | cnt = sizeof(*s)/sizeof(int); | |
317 | ||
318 | fgets(buf, sizeof(buf), fp); | |
319 | while(fgets(buf, sizeof(buf), fp) != NULL) { | |
320 | int i; | |
321 | for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) { | |
322 | if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) { | |
323 | sscanf(buf, "%*s%d", ((int *)s) + i); | |
324 | cnt--; | |
325 | break; | |
326 | } | |
327 | } | |
328 | if (cnt <= 0) | |
329 | break; | |
330 | } | |
331 | ||
332 | fclose(fp); | |
333 | return 0; | |
334 | } | |
335 | ||
7d105b56 SH |
336 | static const char *sstate_name[] = { |
337 | "UNKNOWN", | |
338 | [TCP_ESTABLISHED] = "ESTAB", | |
339 | [TCP_SYN_SENT] = "SYN-SENT", | |
340 | [TCP_SYN_RECV] = "SYN-RECV", | |
341 | [TCP_FIN_WAIT1] = "FIN-WAIT-1", | |
342 | [TCP_FIN_WAIT2] = "FIN-WAIT-2", | |
343 | [TCP_TIME_WAIT] = "TIME-WAIT", | |
344 | [TCP_CLOSE] = "UNCONN", | |
345 | [TCP_CLOSE_WAIT] = "CLOSE-WAIT", | |
346 | [TCP_LAST_ACK] = "LAST-ACK", | |
347 | [TCP_LISTEN] = "LISTEN", | |
348 | [TCP_CLOSING] = "CLOSING", | |
aba5acdf SH |
349 | }; |
350 | ||
7d105b56 SH |
351 | static const char *sstate_namel[] = { |
352 | "UNKNOWN", | |
353 | [TCP_ESTABLISHED] = "established", | |
354 | [TCP_SYN_SENT] = "syn-sent", | |
355 | [TCP_SYN_RECV] = "syn-recv", | |
356 | [TCP_FIN_WAIT1] = "fin-wait-1", | |
357 | [TCP_FIN_WAIT2] = "fin-wait-2", | |
358 | [TCP_TIME_WAIT] = "time-wait", | |
359 | [TCP_CLOSE] = "unconnected", | |
360 | [TCP_CLOSE_WAIT] = "close-wait", | |
361 | [TCP_LAST_ACK] = "last-ack", | |
362 | [TCP_LISTEN] = "listening", | |
363 | [TCP_CLOSING] = "closing", | |
aba5acdf SH |
364 | }; |
365 | ||
366 | struct tcpstat | |
367 | { | |
368 | inet_prefix local; | |
369 | inet_prefix remote; | |
370 | int lport; | |
371 | int rport; | |
372 | int state; | |
373 | int rq, wq; | |
374 | int timer; | |
375 | int timeout; | |
376 | int retrs; | |
e7113c61 | 377 | unsigned ino; |
aba5acdf | 378 | int probes; |
e7113c61 | 379 | unsigned uid; |
aba5acdf SH |
380 | int refcnt; |
381 | unsigned long long sk; | |
382 | int rto, ato, qack, cwnd, ssthresh; | |
383 | }; | |
384 | ||
7d105b56 | 385 | static const char *tmr_name[] = { |
aba5acdf SH |
386 | "off", |
387 | "on", | |
388 | "keepalive", | |
389 | "timewait", | |
390 | "persist", | |
391 | "unknown" | |
392 | }; | |
393 | ||
05e18118 | 394 | const char *print_ms_timer(int timeout) |
aba5acdf SH |
395 | { |
396 | static char buf[64]; | |
397 | int secs, msecs, minutes; | |
398 | if (timeout < 0) | |
399 | timeout = 0; | |
400 | secs = timeout/1000; | |
401 | minutes = secs/60; | |
402 | secs = secs%60; | |
403 | msecs = timeout%1000; | |
404 | buf[0] = 0; | |
405 | if (minutes) { | |
406 | msecs = 0; | |
407 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); | |
408 | if (minutes > 9) | |
409 | secs = 0; | |
410 | } | |
411 | if (secs) { | |
412 | if (secs > 9) | |
413 | msecs = 0; | |
414 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); | |
415 | } | |
416 | if (msecs) | |
417 | sprintf(buf+strlen(buf), "%03dms", msecs); | |
418 | return buf; | |
e7113c61 | 419 | } |
aba5acdf | 420 | |
05e18118 | 421 | const char *print_hz_timer(int timeout) |
aba5acdf SH |
422 | { |
423 | int hz = get_hz(); | |
424 | return print_ms_timer(((timeout*1000) + hz-1)/hz); | |
e7113c61 | 425 | } |
aba5acdf SH |
426 | |
427 | struct scache | |
428 | { | |
429 | struct scache *next; | |
430 | int port; | |
431 | char *name; | |
432 | const char *proto; | |
433 | }; | |
434 | ||
435 | struct scache *rlist; | |
436 | ||
437 | void init_service_resolver(void) | |
438 | { | |
439 | char buf[128]; | |
440 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); | |
441 | if (fp) { | |
442 | fgets(buf, sizeof(buf), fp); | |
443 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
444 | unsigned int progn, port; | |
445 | char proto[128], prog[128]; | |
446 | if (sscanf(buf, "%u %*d %s %u %s", &progn, proto, | |
447 | &port, prog+4) == 4) { | |
448 | struct scache *c = malloc(sizeof(*c)); | |
449 | if (c) { | |
450 | c->port = port; | |
451 | memcpy(prog, "rpc.", 4); | |
452 | c->name = strdup(prog); | |
453 | if (strcmp(proto, TCP_PROTO) == 0) | |
454 | c->proto = TCP_PROTO; | |
455 | else if (strcmp(proto, UDP_PROTO) == 0) | |
456 | c->proto = UDP_PROTO; | |
457 | else | |
458 | c->proto = NULL; | |
459 | c->next = rlist; | |
460 | rlist = c; | |
461 | } | |
462 | } | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
ab61159a SH |
467 | static int ip_local_port_min, ip_local_port_max; |
468 | ||
469 | /* Even do not try default linux ephemeral port ranges: | |
470 | * default /etc/services contains so much of useless crap | |
471 | * wouldbe "allocated" to this area that resolution | |
472 | * is really harmful. I shrug each time when seeing | |
473 | * "socks" or "cfinger" in dumps. | |
474 | */ | |
475 | static int is_ephemeral(int port) | |
476 | { | |
477 | if (!ip_local_port_min) { | |
ab01dbbb | 478 | FILE *f = ephemeral_ports_open(); |
ab61159a | 479 | if (f) { |
ae665a52 | 480 | fscanf(f, "%d %d", |
ab61159a SH |
481 | &ip_local_port_min, &ip_local_port_max); |
482 | fclose(f); | |
483 | } else { | |
484 | ip_local_port_min = 1024; | |
485 | ip_local_port_max = 4999; | |
486 | } | |
487 | } | |
488 | ||
489 | return (port >= ip_local_port_min && port<= ip_local_port_max); | |
490 | } | |
491 | ||
492 | ||
aba5acdf SH |
493 | const char *__resolve_service(int port) |
494 | { | |
495 | struct scache *c; | |
496 | ||
497 | for (c = rlist; c; c = c->next) { | |
498 | if (c->port == port && c->proto == dg_proto) | |
499 | return c->name; | |
500 | } | |
501 | ||
ab61159a | 502 | if (!is_ephemeral(port)) { |
aba5acdf SH |
503 | static int notfirst; |
504 | struct servent *se; | |
505 | if (!notfirst) { | |
506 | setservent(1); | |
507 | notfirst = 1; | |
ae665a52 | 508 | } |
aba5acdf SH |
509 | se = getservbyport(htons(port), dg_proto); |
510 | if (se) | |
511 | return se->s_name; | |
512 | } | |
513 | ||
514 | return NULL; | |
515 | } | |
516 | ||
517 | ||
518 | const char *resolve_service(int port) | |
519 | { | |
520 | static char buf[128]; | |
521 | static struct scache cache[256]; | |
522 | ||
523 | if (port == 0) { | |
524 | buf[0] = '*'; | |
525 | buf[1] = 0; | |
526 | return buf; | |
527 | } | |
528 | ||
529 | if (resolve_services) { | |
530 | if (dg_proto == RAW_PROTO) { | |
531 | return inet_proto_n2a(port, buf, sizeof(buf)); | |
532 | } else { | |
533 | struct scache *c; | |
534 | const char *res; | |
535 | int hash = (port^(((unsigned long)dg_proto)>>2))&255; | |
536 | ||
ae665a52 | 537 | for (c = &cache[hash]; c; c = c->next) { |
aba5acdf SH |
538 | if (c->port == port && |
539 | c->proto == dg_proto) { | |
540 | if (c->name) | |
541 | return c->name; | |
542 | goto do_numeric; | |
543 | } | |
544 | } | |
545 | ||
546 | if ((res = __resolve_service(port)) != NULL) { | |
547 | if ((c = malloc(sizeof(*c))) == NULL) | |
548 | goto do_numeric; | |
549 | } else { | |
550 | c = &cache[hash]; | |
551 | if (c->name) | |
552 | free(c->name); | |
553 | } | |
554 | c->port = port; | |
555 | c->name = NULL; | |
556 | c->proto = dg_proto; | |
557 | if (res) { | |
558 | c->name = strdup(res); | |
559 | c->next = cache[hash].next; | |
560 | cache[hash].next = c; | |
561 | } | |
562 | if (c->name) | |
563 | return c->name; | |
564 | } | |
565 | } | |
566 | ||
567 | do_numeric: | |
568 | sprintf(buf, "%u", port); | |
569 | return buf; | |
570 | } | |
571 | ||
7d105b56 | 572 | void formatted_print(const inet_prefix *a, int port) |
aba5acdf SH |
573 | { |
574 | char buf[1024]; | |
575 | const char *ap = buf; | |
576 | int est_len; | |
577 | ||
578 | est_len = addr_width; | |
579 | ||
580 | if (a->family == AF_INET) { | |
581 | if (a->data[0] == 0) { | |
582 | buf[0] = '*'; | |
583 | buf[1] = 0; | |
584 | } else { | |
585 | ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf)); | |
586 | } | |
587 | } else { | |
588 | ap = format_host(a->family, 16, a->data, buf, sizeof(buf)); | |
589 | est_len = strlen(ap); | |
590 | if (est_len <= addr_width) | |
591 | est_len = addr_width; | |
592 | else | |
593 | est_len = addr_width + ((est_len-addr_width+3)/4)*4; | |
594 | } | |
595 | printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port)); | |
596 | } | |
597 | ||
598 | struct aafilter | |
599 | { | |
600 | inet_prefix addr; | |
601 | int port; | |
602 | struct aafilter *next; | |
603 | }; | |
604 | ||
7d105b56 | 605 | int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, int plen) |
aba5acdf SH |
606 | { |
607 | if (!inet_addr_match(a, p, plen)) | |
608 | return 0; | |
7d105b56 | 609 | |
aba5acdf SH |
610 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches |
611 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped | |
612 | * sockets. Fair? */ | |
613 | if (p->family == AF_INET && a->family == AF_INET6) { | |
614 | if (a->data[0] == 0 && a->data[1] == 0 && | |
615 | a->data[2] == htonl(0xffff)) { | |
616 | inet_prefix tmp = *a; | |
617 | tmp.data[0] = a->data[3]; | |
618 | return inet_addr_match(&tmp, p, plen); | |
619 | } | |
620 | } | |
621 | return 1; | |
622 | } | |
623 | ||
7d105b56 | 624 | int unix_match(const inet_prefix *a, const inet_prefix *p) |
aba5acdf SH |
625 | { |
626 | char *addr, *pattern; | |
627 | memcpy(&addr, a->data, sizeof(addr)); | |
628 | memcpy(&pattern, p->data, sizeof(pattern)); | |
629 | if (pattern == NULL) | |
630 | return 1; | |
631 | if (addr == NULL) | |
632 | addr = ""; | |
633 | return !fnmatch(pattern, addr, 0); | |
634 | } | |
635 | ||
636 | int run_ssfilter(struct ssfilter *f, struct tcpstat *s) | |
637 | { | |
638 | switch (f->type) { | |
639 | case SSF_S_AUTO: | |
640 | { | |
641 | static int low, high=65535; | |
642 | ||
643 | if (s->local.family == AF_UNIX) { | |
644 | char *p; | |
645 | memcpy(&p, s->local.data, sizeof(p)); | |
646 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && | |
ae665a52 | 647 | strspn(p+1, "0123456789abcdef") == 5); |
aba5acdf SH |
648 | } |
649 | if (s->local.family == AF_PACKET) | |
650 | return s->lport == 0 && s->local.data == 0; | |
651 | if (s->local.family == AF_NETLINK) | |
652 | return s->lport < 0; | |
653 | ||
654 | if (!low) { | |
ab01dbbb | 655 | FILE *fp = ephemeral_ports_open(); |
aba5acdf SH |
656 | if (fp) { |
657 | fscanf(fp, "%d%d", &low, &high); | |
658 | fclose(fp); | |
659 | } | |
660 | } | |
661 | return s->lport >= low && s->lport <= high; | |
662 | } | |
663 | case SSF_DCOND: | |
664 | { | |
665 | struct aafilter *a = (void*)f->pred; | |
666 | if (a->addr.family == AF_UNIX) | |
667 | return unix_match(&s->remote, &a->addr); | |
668 | if (a->port != -1 && a->port != s->rport) | |
669 | return 0; | |
670 | if (a->addr.bitlen) { | |
671 | do { | |
672 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) | |
673 | return 1; | |
674 | } while ((a = a->next) != NULL); | |
675 | return 0; | |
676 | } | |
677 | return 1; | |
678 | } | |
679 | case SSF_SCOND: | |
680 | { | |
681 | struct aafilter *a = (void*)f->pred; | |
682 | if (a->addr.family == AF_UNIX) | |
683 | return unix_match(&s->local, &a->addr); | |
684 | if (a->port != -1 && a->port != s->lport) | |
685 | return 0; | |
686 | if (a->addr.bitlen) { | |
687 | do { | |
688 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) | |
689 | return 1; | |
ae665a52 | 690 | } while ((a = a->next) != NULL); |
aba5acdf SH |
691 | return 0; |
692 | } | |
693 | return 1; | |
694 | } | |
695 | case SSF_D_GE: | |
696 | { | |
697 | struct aafilter *a = (void*)f->pred; | |
698 | return s->rport >= a->port; | |
699 | } | |
700 | case SSF_D_LE: | |
701 | { | |
702 | struct aafilter *a = (void*)f->pred; | |
703 | return s->rport <= a->port; | |
704 | } | |
705 | case SSF_S_GE: | |
706 | { | |
707 | struct aafilter *a = (void*)f->pred; | |
708 | return s->lport >= a->port; | |
709 | } | |
710 | case SSF_S_LE: | |
711 | { | |
712 | struct aafilter *a = (void*)f->pred; | |
713 | return s->lport <= a->port; | |
714 | } | |
715 | ||
716 | /* Yup. It is recursion. Sorry. */ | |
717 | case SSF_AND: | |
718 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); | |
719 | case SSF_OR: | |
720 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); | |
721 | case SSF_NOT: | |
722 | return !run_ssfilter(f->pred, s); | |
723 | default: | |
724 | abort(); | |
725 | } | |
726 | } | |
727 | ||
ae665a52 | 728 | /* Relocate external jumps by reloc. */ |
b4b0b7d5 | 729 | static void ssfilter_patch(char *a, int len, int reloc) |
aba5acdf SH |
730 | { |
731 | while (len > 0) { | |
351efcde | 732 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a; |
aba5acdf SH |
733 | if (op->no == len+4) |
734 | op->no += reloc; | |
735 | len -= op->yes; | |
736 | a += op->yes; | |
737 | } | |
738 | if (len < 0) | |
739 | abort(); | |
740 | } | |
741 | ||
b4b0b7d5 | 742 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
aba5acdf SH |
743 | { |
744 | switch (f->type) { | |
745 | case SSF_S_AUTO: | |
746 | { | |
747 | if (!(*bytecode=malloc(4))) abort(); | |
351efcde | 748 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; |
aba5acdf SH |
749 | return 8; |
750 | } | |
751 | case SSF_DCOND: | |
752 | case SSF_SCOND: | |
753 | { | |
754 | struct aafilter *a = (void*)f->pred; | |
755 | struct aafilter *b; | |
756 | char *ptr; | |
351efcde | 757 | int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); |
aba5acdf SH |
758 | int len = 0; |
759 | ||
760 | for (b=a; b; b=b->next) { | |
351efcde | 761 | len += 4 + sizeof(struct inet_diag_hostcond); |
aba5acdf SH |
762 | if (a->addr.family == AF_INET6) |
763 | len += 16; | |
764 | else | |
765 | len += 4; | |
766 | if (b->next) | |
767 | len += 4; | |
768 | } | |
769 | if (!(ptr = malloc(len))) abort(); | |
770 | *bytecode = ptr; | |
771 | for (b=a; b; b=b->next) { | |
351efcde | 772 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; |
aba5acdf | 773 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
351efcde SH |
774 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
775 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4); | |
aba5acdf | 776 | |
351efcde | 777 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
aba5acdf SH |
778 | cond->family = a->addr.family; |
779 | cond->port = a->port; | |
780 | cond->prefix_len = a->addr.bitlen; | |
781 | memcpy(cond->addr, a->addr.data, alen); | |
782 | ptr += oplen; | |
783 | if (b->next) { | |
351efcde SH |
784 | op = (struct inet_diag_bc_op *)ptr; |
785 | *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; | |
aba5acdf SH |
786 | ptr += 4; |
787 | } | |
788 | } | |
789 | return ptr - *bytecode; | |
790 | } | |
791 | case SSF_D_GE: | |
792 | { | |
793 | struct aafilter *x = (void*)f->pred; | |
794 | if (!(*bytecode=malloc(8))) abort(); | |
351efcde SH |
795 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; |
796 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
797 | return 8; |
798 | } | |
799 | case SSF_D_LE: | |
800 | { | |
801 | struct aafilter *x = (void*)f->pred; | |
802 | if (!(*bytecode=malloc(8))) abort(); | |
351efcde SH |
803 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; |
804 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
805 | return 8; |
806 | } | |
807 | case SSF_S_GE: | |
808 | { | |
809 | struct aafilter *x = (void*)f->pred; | |
810 | if (!(*bytecode=malloc(8))) abort(); | |
351efcde SH |
811 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; |
812 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
813 | return 8; |
814 | } | |
815 | case SSF_S_LE: | |
816 | { | |
817 | struct aafilter *x = (void*)f->pred; | |
818 | if (!(*bytecode=malloc(8))) abort(); | |
351efcde SH |
819 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; |
820 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
aba5acdf SH |
821 | return 8; |
822 | } | |
823 | ||
824 | case SSF_AND: | |
825 | { | |
826 | char *a1, *a2, *a, l1, l2; | |
827 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
828 | l2 = ssfilter_bytecompile(f->post, &a2); | |
829 | if (!(a = malloc(l1+l2))) abort(); | |
830 | memcpy(a, a1, l1); | |
831 | memcpy(a+l1, a2, l2); | |
832 | free(a1); free(a2); | |
833 | ssfilter_patch(a, l1, l2); | |
834 | *bytecode = a; | |
835 | return l1+l2; | |
836 | } | |
837 | case SSF_OR: | |
838 | { | |
839 | char *a1, *a2, *a, l1, l2; | |
840 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
841 | l2 = ssfilter_bytecompile(f->post, &a2); | |
842 | if (!(a = malloc(l1+l2+4))) abort(); | |
843 | memcpy(a, a1, l1); | |
844 | memcpy(a+l1+4, a2, l2); | |
845 | free(a1); free(a2); | |
351efcde | 846 | *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; |
aba5acdf SH |
847 | *bytecode = a; |
848 | return l1+l2+4; | |
849 | } | |
850 | case SSF_NOT: | |
851 | { | |
852 | char *a1, *a, l1; | |
853 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
854 | if (!(a = malloc(l1+4))) abort(); | |
855 | memcpy(a, a1, l1); | |
856 | free(a1); | |
351efcde | 857 | *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; |
aba5acdf SH |
858 | *bytecode = a; |
859 | return l1+4; | |
860 | } | |
861 | default: | |
862 | abort(); | |
863 | } | |
864 | } | |
865 | ||
b4b0b7d5 | 866 | static int remember_he(struct aafilter *a, struct hostent *he) |
aba5acdf | 867 | { |
ae665a52 | 868 | char **ptr = he->h_addr_list; |
aba5acdf SH |
869 | int cnt = 0; |
870 | int len; | |
871 | ||
872 | if (he->h_addrtype == AF_INET) | |
873 | len = 4; | |
874 | else if (he->h_addrtype == AF_INET6) | |
875 | len = 16; | |
876 | else | |
877 | return 0; | |
878 | ||
879 | while (*ptr) { | |
880 | struct aafilter *b = a; | |
881 | if (a->addr.bitlen) { | |
882 | if ((b = malloc(sizeof(*b))) == NULL) | |
883 | return cnt; | |
884 | *b = *a; | |
885 | b->next = a->next; | |
886 | a->next = b; | |
887 | } | |
888 | memcpy(b->addr.data, *ptr, len); | |
889 | b->addr.bytelen = len; | |
890 | b->addr.bitlen = len*8; | |
891 | b->addr.family = he->h_addrtype; | |
892 | ptr++; | |
893 | cnt++; | |
894 | } | |
895 | return cnt; | |
896 | } | |
897 | ||
b4b0b7d5 | 898 | static int get_dns_host(struct aafilter *a, const char *addr, int fam) |
aba5acdf SH |
899 | { |
900 | static int notfirst; | |
901 | int cnt = 0; | |
902 | struct hostent *he; | |
903 | ||
904 | a->addr.bitlen = 0; | |
905 | if (!notfirst) { | |
906 | sethostent(1); | |
907 | notfirst = 1; | |
908 | } | |
909 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); | |
910 | if (he) | |
911 | cnt = remember_he(a, he); | |
912 | if (fam == AF_UNSPEC) { | |
913 | he = gethostbyname2(addr, AF_INET6); | |
914 | if (he) | |
915 | cnt += remember_he(a, he); | |
916 | } | |
917 | return !cnt; | |
918 | } | |
919 | ||
b4b0b7d5 | 920 | static int xll_initted = 0; |
aba5acdf | 921 | |
b4b0b7d5 | 922 | static void xll_init(void) |
aba5acdf SH |
923 | { |
924 | struct rtnl_handle rth; | |
925 | rtnl_open(&rth, 0); | |
926 | ll_init_map(&rth); | |
927 | rtnl_close(&rth); | |
928 | xll_initted = 1; | |
929 | } | |
930 | ||
b4b0b7d5 | 931 | static const char *xll_index_to_name(int index) |
aba5acdf SH |
932 | { |
933 | if (!xll_initted) | |
934 | xll_init(); | |
935 | return ll_index_to_name(index); | |
936 | } | |
937 | ||
b4b0b7d5 | 938 | static int xll_name_to_index(const char *dev) |
aba5acdf SH |
939 | { |
940 | if (!xll_initted) | |
941 | xll_init(); | |
942 | return ll_name_to_index(dev); | |
943 | } | |
944 | ||
945 | void *parse_hostcond(char *addr) | |
946 | { | |
947 | char *port = NULL; | |
948 | struct aafilter a; | |
949 | struct aafilter *res; | |
950 | int fam = preferred_family; | |
951 | ||
952 | memset(&a, 0, sizeof(a)); | |
953 | a.port = -1; | |
954 | ||
955 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { | |
956 | char *p; | |
957 | a.addr.family = AF_UNIX; | |
958 | if (strncmp(addr, "unix:", 5) == 0) | |
959 | addr+=5; | |
960 | p = strdup(addr); | |
961 | a.addr.bitlen = 8*strlen(p); | |
962 | memcpy(a.addr.data, &p, sizeof(p)); | |
963 | goto out; | |
964 | } | |
965 | ||
966 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { | |
967 | a.addr.family = AF_PACKET; | |
968 | a.addr.bitlen = 0; | |
969 | if (strncmp(addr, "link:", 5) == 0) | |
970 | addr+=5; | |
971 | port = strchr(addr, ':'); | |
972 | if (port) { | |
973 | *port = 0; | |
974 | if (port[1] && strcmp(port+1, "*")) { | |
975 | if (get_integer(&a.port, port+1, 0)) { | |
976 | if ((a.port = xll_name_to_index(port+1)) <= 0) | |
977 | return NULL; | |
978 | } | |
979 | } | |
980 | } | |
981 | if (addr[0] && strcmp(addr, "*")) { | |
982 | unsigned short tmp; | |
983 | a.addr.bitlen = 32; | |
984 | if (ll_proto_a2n(&tmp, addr)) | |
985 | return NULL; | |
986 | a.addr.data[0] = ntohs(tmp); | |
987 | } | |
988 | goto out; | |
989 | } | |
990 | ||
991 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { | |
992 | a.addr.family = AF_NETLINK; | |
993 | a.addr.bitlen = 0; | |
994 | if (strncmp(addr, "netlink:", 8) == 0) | |
995 | addr+=8; | |
996 | port = strchr(addr, ':'); | |
997 | if (port) { | |
998 | *port = 0; | |
999 | if (port[1] && strcmp(port+1, "*")) { | |
1000 | if (get_integer(&a.port, port+1, 0)) { | |
1001 | if (strcmp(port+1, "kernel") == 0) | |
1002 | a.port = 0; | |
1003 | else | |
1004 | return NULL; | |
1005 | } | |
1006 | } | |
1007 | } | |
1008 | if (addr[0] && strcmp(addr, "*")) { | |
1009 | a.addr.bitlen = 32; | |
1010 | if (get_u32(a.addr.data, addr, 0)) { | |
1011 | if (strcmp(addr, "rtnl") == 0) | |
1012 | a.addr.data[0] = 0; | |
1013 | else if (strcmp(addr, "fw") == 0) | |
1014 | a.addr.data[0] = 3; | |
1015 | else if (strcmp(addr, "tcpdiag") == 0) | |
1016 | a.addr.data[0] = 4; | |
1017 | else | |
1018 | return NULL; | |
1019 | } | |
1020 | } | |
1021 | goto out; | |
1022 | } | |
1023 | ||
1024 | if (strncmp(addr, "inet:", 5) == 0) { | |
1025 | addr += 5; | |
1026 | fam = AF_INET; | |
1027 | } else if (strncmp(addr, "inet6:", 6) == 0) { | |
1028 | addr += 6; | |
1029 | fam = AF_INET6; | |
1030 | } | |
1031 | ||
1032 | /* URL-like literal [] */ | |
1033 | if (addr[0] == '[') { | |
1034 | addr++; | |
1035 | if ((port = strchr(addr, ']')) == NULL) | |
1036 | return NULL; | |
1037 | *port++ = 0; | |
1038 | } else if (addr[0] == '*') { | |
1039 | port = addr+1; | |
1040 | } else { | |
1041 | port = strrchr(strchr(addr, '/') ? : addr, ':'); | |
1042 | } | |
1043 | if (port && *port) { | |
1044 | if (*port != ':') | |
1045 | return NULL; | |
1046 | *port++ = 0; | |
1047 | if (*port && *port != '*') { | |
1048 | if (get_integer(&a.port, port, 0)) { | |
1049 | struct servent *se1 = NULL; | |
1050 | struct servent *se2 = NULL; | |
1051 | if (current_filter.dbs&(1<<UDP_DB)) | |
1052 | se1 = getservbyname(port, UDP_PROTO); | |
1053 | if (current_filter.dbs&(1<<TCP_DB)) | |
1054 | se2 = getservbyname(port, TCP_PROTO); | |
1055 | if (se1 && se2 && se1->s_port != se2->s_port) { | |
1056 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
1057 | return NULL; | |
1058 | } | |
1059 | if (!se1) | |
1060 | se1 = se2; | |
1061 | if (se1) { | |
1062 | a.port = ntohs(se1->s_port); | |
1063 | } else { | |
1064 | struct scache *s; | |
1065 | for (s = rlist; s; s = s->next) { | |
1066 | if ((s->proto == UDP_PROTO && | |
1067 | (current_filter.dbs&(1<<UDP_DB))) || | |
1068 | (s->proto == TCP_PROTO && | |
1069 | (current_filter.dbs&(1<<TCP_DB)))) { | |
1070 | if (s->name && strcmp(s->name, port) == 0) { | |
1071 | if (a.port > 0 && a.port != s->port) { | |
1072 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
1073 | return NULL; | |
1074 | } | |
1075 | a.port = s->port; | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | if (a.port <= 0) { | |
1080 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); | |
1081 | return NULL; | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | } | |
1086 | } | |
1087 | if (addr && *addr && *addr != '*') { | |
1088 | if (get_prefix_1(&a.addr, addr, fam)) { | |
1089 | if (get_dns_host(&a, addr, fam)) { | |
1090 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); | |
1091 | return NULL; | |
1092 | } | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | out: | |
1097 | res = malloc(sizeof(*res)); | |
1098 | if (res) | |
1099 | memcpy(res, &a, sizeof(a)); | |
1100 | return res; | |
1101 | } | |
1102 | ||
ab01dbbb | 1103 | static int tcp_show_line(char *line, const struct filter *f, int family) |
aba5acdf SH |
1104 | { |
1105 | struct tcpstat s; | |
1106 | char *loc, *rem, *data; | |
1107 | char opt[256]; | |
1108 | int n; | |
1109 | char *p; | |
ae665a52 | 1110 | |
aba5acdf SH |
1111 | if ((p = strchr(line, ':')) == NULL) |
1112 | return -1; | |
1113 | loc = p+2; | |
ae665a52 | 1114 | |
aba5acdf SH |
1115 | if ((p = strchr(loc, ':')) == NULL) |
1116 | return -1; | |
1117 | p[5] = 0; | |
1118 | rem = p+6; | |
ae665a52 | 1119 | |
aba5acdf SH |
1120 | if ((p = strchr(rem, ':')) == NULL) |
1121 | return -1; | |
1122 | p[5] = 0; | |
1123 | data = p+6; | |
ae665a52 | 1124 | |
aba5acdf SH |
1125 | do { |
1126 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
1127 | ||
1128 | if (!(f->states & (1<<state))) | |
1129 | return 0; | |
1130 | } while (0); | |
ae665a52 | 1131 | |
aba5acdf SH |
1132 | s.local.family = s.remote.family = family; |
1133 | if (family == AF_INET) { | |
1134 | sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport); | |
1135 | sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport); | |
1136 | s.local.bytelen = s.remote.bytelen = 4; | |
1137 | } else { | |
1138 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
1139 | s.local.data, | |
1140 | s.local.data+1, | |
1141 | s.local.data+2, | |
1142 | s.local.data+3, | |
1143 | &s.lport); | |
1144 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
1145 | s.remote.data, | |
1146 | s.remote.data+1, | |
1147 | s.remote.data+2, | |
1148 | s.remote.data+3, | |
1149 | &s.rport); | |
1150 | s.local.bytelen = s.remote.bytelen = 16; | |
1151 | } | |
ae665a52 | 1152 | |
aba5acdf SH |
1153 | if (f->f && run_ssfilter(f->f, &s) == 0) |
1154 | return 0; | |
ae665a52 | 1155 | |
aba5acdf | 1156 | opt[0] = 0; |
e7113c61 | 1157 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n", |
aba5acdf SH |
1158 | &s.state, &s.wq, &s.rq, |
1159 | &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino, | |
1160 | &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack, | |
1161 | &s.cwnd, &s.ssthresh, opt); | |
ae665a52 | 1162 | |
aba5acdf SH |
1163 | if (n < 17) |
1164 | opt[0] = 0; | |
ae665a52 | 1165 | |
aba5acdf SH |
1166 | if (n < 12) { |
1167 | s.rto = 0; | |
1168 | s.cwnd = 2; | |
1169 | s.ssthresh = -1; | |
1170 | s.ato = s.qack = 0; | |
1171 | } | |
ae665a52 | 1172 | |
aba5acdf SH |
1173 | if (netid_width) |
1174 | printf("%-*s ", netid_width, "tcp"); | |
1175 | if (state_width) | |
1176 | printf("%-*s ", state_width, sstate_name[s.state]); | |
ae665a52 | 1177 | |
aba5acdf | 1178 | printf("%-6d %-6d ", s.rq, s.wq); |
ae665a52 | 1179 | |
aba5acdf SH |
1180 | formatted_print(&s.local, s.lport); |
1181 | formatted_print(&s.remote, s.rport); | |
ae665a52 | 1182 | |
aba5acdf SH |
1183 | if (show_options) { |
1184 | if (s.timer) { | |
1185 | if (s.timer > 4) | |
1186 | s.timer = 5; | |
1187 | printf(" timer:(%s,%s,%d)", | |
1188 | tmr_name[s.timer], | |
1189 | print_hz_timer(s.timeout), | |
1190 | s.timer != 1 ? s.probes : s.retrs); | |
1191 | } | |
1192 | } | |
1193 | if (show_tcpinfo) { | |
1194 | if (s.rto && s.rto != 3*get_hz()) | |
1195 | printf(" rto:%g", (double)s.rto/get_hz()); | |
1196 | if (s.ato) | |
1197 | printf(" ato:%g", (double)s.ato/get_hz()); | |
1198 | if (s.cwnd != 2) | |
1199 | printf(" cwnd:%d", s.cwnd); | |
1200 | if (s.ssthresh != -1) | |
1201 | printf(" ssthresh:%d", s.ssthresh); | |
1202 | if (s.qack/2) | |
1203 | printf(" qack:%d", s.qack/2); | |
1204 | if (s.qack&1) | |
1205 | printf(" bidir"); | |
1206 | } | |
1207 | if (show_users) { | |
1208 | char ubuf[4096]; | |
1209 | if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0) | |
1210 | printf(" users:(%s)", ubuf); | |
1211 | } | |
1212 | if (show_details) { | |
1213 | if (s.uid) | |
1214 | printf(" uid:%u", (unsigned)s.uid); | |
e7113c61 | 1215 | printf(" ino:%u", s.ino); |
aba5acdf SH |
1216 | printf(" sk:%llx", s.sk); |
1217 | if (opt[0]) | |
1218 | printf(" opt:\"%s\"", opt); | |
1219 | } | |
1220 | printf("\n"); | |
1221 | ||
1222 | return 0; | |
1223 | } | |
1224 | ||
ab01dbbb SH |
1225 | static int generic_record_read(FILE *fp, |
1226 | int (*worker)(char*, const struct filter *, int), | |
1227 | const struct filter *f, int fam) | |
aba5acdf | 1228 | { |
ab01dbbb | 1229 | char line[256]; |
aba5acdf | 1230 | |
ab01dbbb SH |
1231 | /* skip header */ |
1232 | if (fgets(line, sizeof(line), fp) == NULL) | |
aba5acdf | 1233 | goto outerr; |
ab01dbbb SH |
1234 | |
1235 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1236 | int n = strlen(line); | |
1237 | if (n == 0 || line[n-1] != '\n') { | |
1238 | errno = -EINVAL; | |
1239 | return -1; | |
aba5acdf | 1240 | } |
ab01dbbb | 1241 | line[n-1] = 0; |
aba5acdf | 1242 | |
ab01dbbb SH |
1243 | if (worker(line, f, fam) < 0) |
1244 | return 0; | |
1245 | } | |
aba5acdf | 1246 | outerr: |
ab01dbbb SH |
1247 | |
1248 | return ferror(fp) ? -1 : 0; | |
aba5acdf | 1249 | } |
ae665a52 | 1250 | |
b4b0b7d5 SH |
1251 | static char *sprint_bw(char *buf, double bw) |
1252 | { | |
ae665a52 | 1253 | if (bw > 1000000.) |
b4b0b7d5 SH |
1254 | sprintf(buf,"%.1fM", bw / 1000000.); |
1255 | else if (bw > 1000.) | |
1256 | sprintf(buf,"%.1fK", bw / 1000.); | |
1257 | else | |
1258 | sprintf(buf, "%g", bw); | |
1259 | ||
1260 | return buf; | |
1261 | } | |
aba5acdf | 1262 | |
351efcde | 1263 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r) |
7d105b56 | 1264 | { |
351efcde | 1265 | struct rtattr * tb[INET_DIAG_MAX+1]; |
b4b0b7d5 SH |
1266 | char b1[64]; |
1267 | double rtt = 0; | |
7d105b56 | 1268 | |
351efcde | 1269 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), |
7d105b56 SH |
1270 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
1271 | ||
351efcde SH |
1272 | if (tb[INET_DIAG_MEMINFO]) { |
1273 | const struct inet_diag_meminfo *minfo | |
1274 | = RTA_DATA(tb[INET_DIAG_MEMINFO]); | |
7d105b56 | 1275 | printf(" mem:(r%u,w%u,f%u,t%u)", |
351efcde SH |
1276 | minfo->idiag_rmem, |
1277 | minfo->idiag_wmem, | |
1278 | minfo->idiag_fmem, | |
1279 | minfo->idiag_tmem); | |
7d105b56 SH |
1280 | } |
1281 | ||
351efcde | 1282 | if (tb[INET_DIAG_INFO]) { |
05e18118 | 1283 | struct tcp_info *info; |
351efcde | 1284 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
05e18118 SH |
1285 | |
1286 | /* workaround for older kernels with less fields */ | |
1287 | if (len < sizeof(*info)) { | |
1288 | info = alloca(sizeof(*info)); | |
1289 | memset(info, 0, sizeof(*info)); | |
351efcde | 1290 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
05e18118 | 1291 | } else |
351efcde | 1292 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
05e18118 | 1293 | |
b4b0b7d5 SH |
1294 | if (show_options) { |
1295 | if (info->tcpi_options & TCPI_OPT_TIMESTAMPS) | |
1296 | printf(" ts"); | |
1297 | if (info->tcpi_options & TCPI_OPT_SACK) | |
1298 | printf(" sack"); | |
1299 | if (info->tcpi_options & TCPI_OPT_ECN) | |
1300 | printf(" ecn"); | |
1301 | } | |
52d5ac3f | 1302 | |
351efcde SH |
1303 | if (tb[INET_DIAG_CONG]) |
1304 | printf("%s", (char *) RTA_DATA(tb[INET_DIAG_CONG])); | |
ea8fc104 | 1305 | |
ae665a52 | 1306 | if (info->tcpi_options & TCPI_OPT_WSCALE) |
05e18118 SH |
1307 | printf(" wscale:%d,%d", info->tcpi_snd_wscale, |
1308 | info->tcpi_rcv_wscale); | |
7d105b56 SH |
1309 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
1310 | printf(" rto:%g", (double)info->tcpi_rto/1000); | |
1311 | if (info->tcpi_rtt) | |
1312 | printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000, | |
1313 | (double)info->tcpi_rttvar/1000); | |
1314 | if (info->tcpi_ato) | |
1315 | printf(" ato:%g", (double)info->tcpi_ato/1000); | |
1316 | if (info->tcpi_snd_cwnd != 2) | |
1317 | printf(" cwnd:%d", info->tcpi_snd_cwnd); | |
1318 | if (info->tcpi_snd_ssthresh < 0xFFFF) | |
1319 | printf(" ssthresh:%d", info->tcpi_snd_ssthresh); | |
52d5ac3f | 1320 | |
b4b0b7d5 | 1321 | rtt = (double) info->tcpi_rtt; |
351efcde | 1322 | if (tb[INET_DIAG_VEGASINFO]) { |
05e18118 | 1323 | const struct tcpvegas_info *vinfo |
351efcde | 1324 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
7d105b56 | 1325 | |
ae665a52 | 1326 | if (vinfo->tcpv_enabled && |
ea8fc104 SH |
1327 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
1328 | rtt = vinfo->tcpv_rtt; | |
b4b0b7d5 SH |
1329 | } |
1330 | ||
1331 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { | |
1332 | printf(" send %sbps", | |
1333 | sprint_bw(b1, (double) info->tcpi_snd_cwnd * | |
1334 | (double) info->tcpi_snd_mss * 8000000. | |
1335 | / rtt)); | |
7d105b56 | 1336 | } |
b4b0b7d5 SH |
1337 | |
1338 | if (info->tcpi_rcv_rtt) | |
1339 | printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000); | |
1340 | if (info->tcpi_rcv_space) | |
1341 | printf(" rcv_space:%d", info->tcpi_rcv_space); | |
1342 | ||
7d105b56 | 1343 | } |
7d105b56 | 1344 | } |
aba5acdf | 1345 | |
ab01dbbb | 1346 | static int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f) |
aba5acdf | 1347 | { |
351efcde | 1348 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
aba5acdf SH |
1349 | struct tcpstat s; |
1350 | ||
351efcde SH |
1351 | s.state = r->idiag_state; |
1352 | s.local.family = s.remote.family = r->idiag_family; | |
1353 | s.lport = ntohs(r->id.idiag_sport); | |
1354 | s.rport = ntohs(r->id.idiag_dport); | |
aba5acdf SH |
1355 | if (s.local.family == AF_INET) { |
1356 | s.local.bytelen = s.remote.bytelen = 4; | |
1357 | } else { | |
1358 | s.local.bytelen = s.remote.bytelen = 16; | |
1359 | } | |
351efcde SH |
1360 | memcpy(s.local.data, r->id.idiag_src, s.local.bytelen); |
1361 | memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen); | |
aba5acdf SH |
1362 | |
1363 | if (f && f->f && run_ssfilter(f->f, &s) == 0) | |
1364 | return 0; | |
1365 | ||
1366 | if (netid_width) | |
1367 | printf("%-*s ", netid_width, "tcp"); | |
1368 | if (state_width) | |
1369 | printf("%-*s ", state_width, sstate_name[s.state]); | |
1370 | ||
351efcde | 1371 | printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue); |
aba5acdf SH |
1372 | |
1373 | formatted_print(&s.local, s.lport); | |
1374 | formatted_print(&s.remote, s.rport); | |
1375 | ||
1376 | if (show_options) { | |
351efcde SH |
1377 | if (r->idiag_timer) { |
1378 | if (r->idiag_timer > 4) | |
1379 | r->idiag_timer = 5; | |
aba5acdf | 1380 | printf(" timer:(%s,%s,%d)", |
351efcde SH |
1381 | tmr_name[r->idiag_timer], |
1382 | print_ms_timer(r->idiag_expires), | |
1383 | r->idiag_retrans); | |
aba5acdf SH |
1384 | } |
1385 | } | |
1386 | if (show_users) { | |
1387 | char ubuf[4096]; | |
351efcde | 1388 | if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0) |
aba5acdf SH |
1389 | printf(" users:(%s)", ubuf); |
1390 | } | |
1391 | if (show_details) { | |
351efcde SH |
1392 | if (r->idiag_uid) |
1393 | printf(" uid:%u", (unsigned)r->idiag_uid); | |
e7113c61 | 1394 | printf(" ino:%u", r->idiag_inode); |
351efcde SH |
1395 | printf(" sk:%08x", r->id.idiag_cookie[0]); |
1396 | if (r->id.idiag_cookie[1] != 0) | |
1397 | printf("%08x", r->id.idiag_cookie[1]); | |
aba5acdf SH |
1398 | } |
1399 | if (show_mem || show_tcpinfo) { | |
7d105b56 SH |
1400 | printf("\n\t"); |
1401 | tcp_show_info(nlh, r); | |
aba5acdf | 1402 | } |
7d105b56 | 1403 | |
aba5acdf SH |
1404 | printf("\n"); |
1405 | ||
1406 | return 0; | |
aba5acdf SH |
1407 | } |
1408 | ||
ab01dbbb | 1409 | static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype) |
aba5acdf SH |
1410 | { |
1411 | int fd; | |
1412 | struct sockaddr_nl nladdr; | |
1413 | struct { | |
1414 | struct nlmsghdr nlh; | |
351efcde | 1415 | struct inet_diag_req r; |
aba5acdf SH |
1416 | } req; |
1417 | char *bc = NULL; | |
1418 | int bclen; | |
1419 | struct msghdr msg; | |
1420 | struct rtattr rta; | |
1421 | char buf[8192]; | |
1422 | struct iovec iov[3]; | |
1423 | ||
351efcde | 1424 | if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0) |
aba5acdf SH |
1425 | return -1; |
1426 | ||
1427 | memset(&nladdr, 0, sizeof(nladdr)); | |
1428 | nladdr.nl_family = AF_NETLINK; | |
1429 | ||
1430 | req.nlh.nlmsg_len = sizeof(req); | |
351efcde | 1431 | req.nlh.nlmsg_type = socktype; |
aba5acdf SH |
1432 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
1433 | req.nlh.nlmsg_pid = 0; | |
1434 | req.nlh.nlmsg_seq = 123456; | |
1435 | memset(&req.r, 0, sizeof(req.r)); | |
351efcde SH |
1436 | req.r.idiag_family = AF_INET; |
1437 | req.r.idiag_states = f->states; | |
aba5acdf | 1438 | if (show_mem) |
ae665a52 | 1439 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
b4b0b7d5 | 1440 | |
7d105b56 | 1441 | if (show_tcpinfo) { |
351efcde SH |
1442 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
1443 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
1444 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
7d105b56 | 1445 | } |
aba5acdf | 1446 | |
ae665a52 SH |
1447 | iov[0] = (struct iovec){ |
1448 | .iov_base = &req, | |
1449 | .iov_len = sizeof(req) | |
ea8fc104 | 1450 | }; |
aba5acdf SH |
1451 | if (f->f) { |
1452 | bclen = ssfilter_bytecompile(f->f, &bc); | |
351efcde | 1453 | rta.rta_type = INET_DIAG_REQ_BYTECODE; |
aba5acdf SH |
1454 | rta.rta_len = RTA_LENGTH(bclen); |
1455 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
1456 | iov[2] = (struct iovec){ bc, bclen }; | |
1457 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
1458 | } | |
1459 | ||
1460 | msg = (struct msghdr) { | |
ae665a52 | 1461 | .msg_name = (void*)&nladdr, |
ea8fc104 | 1462 | .msg_namelen = sizeof(nladdr), |
ae665a52 | 1463 | .msg_iov = iov, |
ea8fc104 | 1464 | .msg_iovlen = f->f ? 3 : 1, |
aba5acdf SH |
1465 | }; |
1466 | ||
1467 | if (sendmsg(fd, &msg, 0) < 0) | |
1468 | return -1; | |
1469 | ||
ae665a52 SH |
1470 | iov[0] = (struct iovec){ |
1471 | .iov_base = buf, | |
1472 | .iov_len = sizeof(buf) | |
ea8fc104 | 1473 | }; |
aba5acdf SH |
1474 | |
1475 | while (1) { | |
1476 | int status; | |
1477 | struct nlmsghdr *h; | |
1478 | ||
1479 | msg = (struct msghdr) { | |
1480 | (void*)&nladdr, sizeof(nladdr), | |
1481 | iov, 1, | |
1482 | NULL, 0, | |
1483 | 0 | |
1484 | }; | |
1485 | ||
1486 | status = recvmsg(fd, &msg, 0); | |
1487 | ||
1488 | if (status < 0) { | |
1489 | if (errno == EINTR) | |
1490 | continue; | |
1491 | perror("OVERRUN"); | |
1492 | continue; | |
1493 | } | |
1494 | if (status == 0) { | |
1495 | fprintf(stderr, "EOF on netlink\n"); | |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | if (dump_fp) | |
1500 | fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp); | |
1501 | ||
1502 | h = (struct nlmsghdr*)buf; | |
1503 | while (NLMSG_OK(h, status)) { | |
1504 | int err; | |
1505 | ||
1506 | if (/*h->nlmsg_pid != rth->local.nl_pid ||*/ | |
1507 | h->nlmsg_seq != 123456) | |
1508 | goto skip_it; | |
1509 | ||
1510 | if (h->nlmsg_type == NLMSG_DONE) | |
1511 | return 0; | |
1512 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1513 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
1514 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
1515 | fprintf(stderr, "ERROR truncated\n"); | |
1516 | } else { | |
1517 | errno = -err->error; | |
1518 | perror("TCPDIAG answers"); | |
1519 | } | |
1520 | return 0; | |
1521 | } | |
1522 | if (!dump_fp) { | |
1523 | err = tcp_show_sock(h, NULL); | |
1524 | if (err < 0) | |
1525 | return err; | |
1526 | } | |
1527 | ||
1528 | skip_it: | |
1529 | h = NLMSG_NEXT(h, status); | |
1530 | } | |
1531 | if (msg.msg_flags & MSG_TRUNC) { | |
1532 | fprintf(stderr, "Message truncated\n"); | |
1533 | continue; | |
1534 | } | |
1535 | if (status) { | |
1536 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
1537 | exit(1); | |
1538 | } | |
1539 | } | |
1540 | return 0; | |
1541 | } | |
1542 | ||
ab01dbbb | 1543 | static int tcp_show_netlink_file(struct filter *f) |
aba5acdf SH |
1544 | { |
1545 | FILE *fp; | |
1546 | char buf[8192]; | |
1547 | ||
1548 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { | |
1549 | perror("fopen($TCPDIAG_FILE)"); | |
1550 | return -1; | |
1551 | } | |
1552 | ||
1553 | while (1) { | |
1554 | int status, err; | |
1555 | struct nlmsghdr *h = (struct nlmsghdr*)buf; | |
1556 | ||
1557 | status = fread(buf, 1, sizeof(*h), fp); | |
1558 | if (status < 0) { | |
1559 | perror("Reading header from $TCPDIAG_FILE"); | |
1560 | return -1; | |
1561 | } | |
1562 | if (status != sizeof(*h)) { | |
1563 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
1564 | return -1; | |
1565 | } | |
1566 | ||
1567 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); | |
1568 | ||
1569 | if (status < 0) { | |
1570 | perror("Reading $TCPDIAG_FILE"); | |
1571 | return -1; | |
1572 | } | |
1573 | if (status + sizeof(*h) < h->nlmsg_len) { | |
1574 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
1575 | return -1; | |
1576 | } | |
1577 | ||
1578 | /* The only legal exit point */ | |
1579 | if (h->nlmsg_type == NLMSG_DONE) | |
1580 | return 0; | |
1581 | ||
1582 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1583 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
1584 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
1585 | fprintf(stderr, "ERROR truncated\n"); | |
1586 | } else { | |
1587 | errno = -err->error; | |
1588 | perror("TCPDIAG answered"); | |
1589 | } | |
1590 | return -1; | |
1591 | } | |
1592 | ||
1593 | err = tcp_show_sock(h, f); | |
1594 | if (err < 0) | |
1595 | return err; | |
1596 | } | |
1597 | } | |
1598 | ||
ab01dbbb | 1599 | static int tcp_show(struct filter *f, int socktype) |
aba5acdf | 1600 | { |
ab01dbbb | 1601 | FILE *fp = NULL; |
aba5acdf SH |
1602 | char *buf = NULL; |
1603 | int bufsize = 64*1024; | |
1604 | ||
1605 | dg_proto = TCP_PROTO; | |
1606 | ||
1607 | if (getenv("TCPDIAG_FILE")) | |
1608 | return tcp_show_netlink_file(f); | |
1609 | ||
1610 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") | |
351efcde | 1611 | && tcp_show_netlink(f, NULL, socktype) == 0) |
aba5acdf SH |
1612 | return 0; |
1613 | ||
1614 | /* Sigh... We have to parse /proc/net/tcp... */ | |
1615 | ||
ab01dbbb | 1616 | |
aba5acdf SH |
1617 | /* Estimate amount of sockets and try to allocate |
1618 | * huge buffer to read all the table at one read. | |
1619 | * Limit it by 16MB though. The assumption is: as soon as | |
1620 | * kernel was able to hold information about N connections, | |
1621 | * it is able to give us some memory for snapshot. | |
1622 | */ | |
1623 | if (1) { | |
1624 | int guess = slabstat.socks+slabstat.tcp_syns; | |
1625 | if (f->states&(1<<SS_TIME_WAIT)) | |
1626 | guess += slabstat.tcp_tws; | |
1627 | if (guess > (16*1024*1024)/128) | |
1628 | guess = (16*1024*1024)/128; | |
1629 | guess *= 128; | |
1630 | if (guess > bufsize) | |
1631 | bufsize = guess; | |
1632 | } | |
1633 | while (bufsize >= 64*1024) { | |
1634 | if ((buf = malloc(bufsize)) != NULL) | |
1635 | break; | |
1636 | bufsize /= 2; | |
1637 | } | |
1638 | if (buf == NULL) { | |
1639 | errno = ENOMEM; | |
1640 | return -1; | |
1641 | } | |
1642 | ||
1643 | if (f->families & (1<<AF_INET)) { | |
ab01dbbb | 1644 | if ((fp = net_tcp_open()) < 0) |
aba5acdf | 1645 | goto outerr; |
ab01dbbb SH |
1646 | |
1647 | setbuffer(fp, buf, bufsize); | |
1648 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) | |
aba5acdf | 1649 | goto outerr; |
ab01dbbb | 1650 | fclose(fp); |
aba5acdf SH |
1651 | } |
1652 | ||
1653 | if ((f->families & (1<<AF_INET6)) && | |
ab01dbbb SH |
1654 | (fp = net_tcp6_open()) >= 0) { |
1655 | setbuffer(fp, buf, bufsize); | |
1656 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) | |
aba5acdf | 1657 | goto outerr; |
ab01dbbb | 1658 | fclose(fp); |
aba5acdf SH |
1659 | } |
1660 | ||
1661 | free(buf); | |
1662 | return 0; | |
1663 | ||
1664 | outerr: | |
1665 | do { | |
1666 | int saved_errno = errno; | |
1667 | if (buf) | |
1668 | free(buf); | |
ab01dbbb SH |
1669 | if (fp) |
1670 | fclose(fp); | |
aba5acdf SH |
1671 | errno = saved_errno; |
1672 | return -1; | |
1673 | } while (0); | |
1674 | } | |
1675 | ||
1676 | ||
ab01dbbb | 1677 | int dgram_show_line(char *line, const struct filter *f, int family) |
aba5acdf SH |
1678 | { |
1679 | struct tcpstat s; | |
1680 | char *loc, *rem, *data; | |
1681 | char opt[256]; | |
1682 | int n; | |
1683 | char *p; | |
1684 | ||
1685 | if ((p = strchr(line, ':')) == NULL) | |
1686 | return -1; | |
1687 | loc = p+2; | |
1688 | ||
1689 | if ((p = strchr(loc, ':')) == NULL) | |
1690 | return -1; | |
1691 | p[5] = 0; | |
1692 | rem = p+6; | |
1693 | ||
1694 | if ((p = strchr(rem, ':')) == NULL) | |
1695 | return -1; | |
1696 | p[5] = 0; | |
1697 | data = p+6; | |
1698 | ||
1699 | do { | |
1700 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
1701 | ||
1702 | if (!(f->states & (1<<state))) | |
1703 | return 0; | |
1704 | } while (0); | |
1705 | ||
1706 | s.local.family = s.remote.family = family; | |
1707 | if (family == AF_INET) { | |
1708 | sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport); | |
1709 | sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport); | |
1710 | s.local.bytelen = s.remote.bytelen = 4; | |
1711 | } else { | |
1712 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
1713 | s.local.data, | |
1714 | s.local.data+1, | |
1715 | s.local.data+2, | |
1716 | s.local.data+3, | |
1717 | &s.lport); | |
1718 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
1719 | s.remote.data, | |
1720 | s.remote.data+1, | |
1721 | s.remote.data+2, | |
1722 | s.remote.data+3, | |
1723 | &s.rport); | |
1724 | s.local.bytelen = s.remote.bytelen = 16; | |
1725 | } | |
1726 | ||
1727 | if (f->f && run_ssfilter(f->f, &s) == 0) | |
1728 | return 0; | |
1729 | ||
1730 | opt[0] = 0; | |
e7113c61 | 1731 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
aba5acdf SH |
1732 | &s.state, &s.wq, &s.rq, |
1733 | &s.uid, &s.ino, | |
1734 | &s.refcnt, &s.sk, opt); | |
1735 | ||
1736 | if (n < 9) | |
1737 | opt[0] = 0; | |
1738 | ||
1739 | if (netid_width) | |
1740 | printf("%-*s ", netid_width, dg_proto); | |
1741 | if (state_width) | |
1742 | printf("%-*s ", state_width, sstate_name[s.state]); | |
1743 | ||
1744 | printf("%-6d %-6d ", s.rq, s.wq); | |
1745 | ||
1746 | formatted_print(&s.local, s.lport); | |
1747 | formatted_print(&s.remote, s.rport); | |
1748 | ||
1749 | if (show_users) { | |
1750 | char ubuf[4096]; | |
1751 | if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0) | |
1752 | printf(" users:(%s)", ubuf); | |
1753 | } | |
1754 | ||
1755 | if (show_details) { | |
1756 | if (s.uid) | |
1757 | printf(" uid=%u", (unsigned)s.uid); | |
e7113c61 | 1758 | printf(" ino=%u", s.ino); |
aba5acdf SH |
1759 | printf(" sk=%llx", s.sk); |
1760 | if (opt[0]) | |
1761 | printf(" opt:\"%s\"", opt); | |
1762 | } | |
1763 | printf("\n"); | |
1764 | ||
1765 | return 0; | |
1766 | } | |
1767 | ||
1768 | ||
1769 | int udp_show(struct filter *f) | |
1770 | { | |
ab01dbbb | 1771 | FILE *fp = NULL; |
aba5acdf SH |
1772 | |
1773 | dg_proto = UDP_PROTO; | |
1774 | ||
1775 | if (f->families&(1<<AF_INET)) { | |
ab01dbbb | 1776 | if ((fp = net_udp_open()) < 0) |
aba5acdf | 1777 | goto outerr; |
ab01dbbb | 1778 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 1779 | goto outerr; |
ab01dbbb | 1780 | fclose(fp); |
aba5acdf SH |
1781 | } |
1782 | ||
1783 | if ((f->families&(1<<AF_INET6)) && | |
ab01dbbb SH |
1784 | (fp = net_udp6_open()) >= 0) { |
1785 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) | |
aba5acdf | 1786 | goto outerr; |
ab01dbbb | 1787 | fclose(fp); |
aba5acdf SH |
1788 | } |
1789 | return 0; | |
1790 | ||
1791 | outerr: | |
1792 | do { | |
1793 | int saved_errno = errno; | |
ab01dbbb SH |
1794 | if (fp) |
1795 | fclose(fp); | |
aba5acdf SH |
1796 | errno = saved_errno; |
1797 | return -1; | |
1798 | } while (0); | |
1799 | } | |
1800 | ||
1801 | int raw_show(struct filter *f) | |
1802 | { | |
ab01dbbb | 1803 | FILE *fp = NULL; |
aba5acdf SH |
1804 | |
1805 | dg_proto = RAW_PROTO; | |
1806 | ||
1807 | if (f->families&(1<<AF_INET)) { | |
ab01dbbb | 1808 | if ((fp = net_raw_open()) < 0) |
aba5acdf | 1809 | goto outerr; |
ab01dbbb | 1810 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 1811 | goto outerr; |
ab01dbbb | 1812 | fclose(fp); |
aba5acdf SH |
1813 | } |
1814 | ||
1815 | if ((f->families&(1<<AF_INET6)) && | |
ab01dbbb SH |
1816 | (fp = net_raw6_open()) >= 0) { |
1817 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) | |
aba5acdf | 1818 | goto outerr; |
ab01dbbb | 1819 | fclose(fp); |
aba5acdf SH |
1820 | } |
1821 | return 0; | |
1822 | ||
1823 | outerr: | |
1824 | do { | |
1825 | int saved_errno = errno; | |
ab01dbbb SH |
1826 | if (fp) |
1827 | fclose(fp); | |
aba5acdf SH |
1828 | errno = saved_errno; |
1829 | return -1; | |
1830 | } while (0); | |
1831 | } | |
1832 | ||
1833 | ||
1834 | struct unixstat | |
1835 | { | |
1836 | struct unixstat *next; | |
1837 | int ino; | |
1838 | int peer; | |
1839 | int rq; | |
1840 | int wq; | |
1841 | int state; | |
1842 | int type; | |
1843 | char *name; | |
1844 | }; | |
1845 | ||
1846 | ||
1847 | ||
1848 | int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, | |
1849 | SS_ESTABLISHED, SS_CLOSING }; | |
1850 | ||
1851 | ||
1852 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat)) | |
1853 | ||
1854 | void unix_list_free(struct unixstat *list) | |
1855 | { | |
1856 | while (list) { | |
1857 | struct unixstat *s = list; | |
1858 | list = list->next; | |
1859 | if (s->name) | |
1860 | free(s->name); | |
1861 | free(s); | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | void unix_list_print(struct unixstat *list, struct filter *f) | |
1866 | { | |
1867 | struct unixstat *s; | |
1868 | char *peer; | |
1869 | ||
1870 | for (s = list; s; s = s->next) { | |
1871 | if (!(f->states & (1<<s->state))) | |
1872 | continue; | |
1873 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) | |
1874 | continue; | |
1875 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) | |
1876 | continue; | |
1877 | ||
1878 | peer = "*"; | |
1879 | if (s->peer) { | |
1880 | struct unixstat *p; | |
1881 | for (p = list; p; p = p->next) { | |
1882 | if (s->peer == p->ino) | |
1883 | break; | |
1884 | } | |
1885 | if (!p) { | |
1886 | peer = "?"; | |
1887 | } else { | |
1888 | peer = p->name ? : "*"; | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | if (f->f) { | |
1893 | struct tcpstat tst; | |
1894 | tst.local.family = AF_UNIX; | |
1895 | tst.remote.family = AF_UNIX; | |
1896 | memcpy(tst.local.data, &s->name, sizeof(s->name)); | |
1897 | if (strcmp(peer, "*") == 0) | |
1898 | memset(tst.remote.data, 0, sizeof(peer)); | |
1899 | else | |
ae665a52 | 1900 | memcpy(tst.remote.data, &peer, sizeof(peer)); |
aba5acdf SH |
1901 | if (run_ssfilter(f->f, &tst) == 0) |
1902 | continue; | |
1903 | } | |
1904 | ||
1905 | if (netid_width) | |
ae665a52 | 1906 | printf("%-*s ", netid_width, |
aba5acdf SH |
1907 | s->type == SOCK_STREAM ? "u_str" : "u_dgr"); |
1908 | if (state_width) | |
1909 | printf("%-*s ", state_width, sstate_name[s->state]); | |
1910 | printf("%-6d %-6d ", s->rq, s->wq); | |
1911 | printf("%*s %-*d %*s %-*d", | |
1912 | addr_width, s->name ? : "*", serv_width, s->ino, | |
1913 | addr_width, peer, serv_width, s->peer); | |
1914 | if (show_users) { | |
1915 | char ubuf[4096]; | |
1916 | if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0) | |
1917 | printf(" users:(%s)", ubuf); | |
1918 | } | |
1919 | printf("\n"); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | int unix_show(struct filter *f) | |
1924 | { | |
1925 | FILE *fp; | |
1926 | char buf[256]; | |
1927 | char name[128]; | |
1928 | int newformat = 0; | |
1929 | int cnt; | |
1930 | struct unixstat *list = NULL; | |
1931 | ||
ab01dbbb | 1932 | if ((fp = net_unix_open()) == NULL) |
aba5acdf SH |
1933 | return -1; |
1934 | fgets(buf, sizeof(buf)-1, fp); | |
1935 | ||
ae665a52 | 1936 | if (memcmp(buf, "Peer", 4) == 0) |
aba5acdf SH |
1937 | newformat = 1; |
1938 | cnt = 0; | |
1939 | ||
1940 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
1941 | struct unixstat *u, **insp; | |
1942 | int flags; | |
1943 | ||
1944 | if (!(u = malloc(sizeof(*u)))) | |
1945 | break; | |
1946 | u->name = NULL; | |
1947 | ||
1948 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", | |
1949 | &u->peer, &u->rq, &u->wq, &flags, &u->type, | |
1950 | &u->state, &u->ino, name) < 8) | |
1951 | name[0] = 0; | |
1952 | ||
1953 | if (flags&(1<<16)) { | |
1954 | u->state = SS_LISTEN; | |
1955 | } else { | |
1956 | u->state = unix_state_map[u->state-1]; | |
1957 | if (u->type == SOCK_DGRAM && | |
1958 | u->state == SS_CLOSE && | |
1959 | u->peer) | |
1960 | u->state = SS_ESTABLISHED; | |
1961 | } | |
1962 | ||
1963 | if (!newformat) { | |
1964 | u->peer = 0; | |
1965 | u->rq = 0; | |
1966 | u->wq = 0; | |
1967 | } | |
1968 | ||
1969 | insp = &list; | |
1970 | while (*insp) { | |
1971 | if (u->type < (*insp)->type || | |
1972 | (u->type == (*insp)->type && | |
1973 | u->ino < (*insp)->ino)) | |
1974 | break; | |
1975 | insp = &(*insp)->next; | |
1976 | } | |
1977 | u->next = *insp; | |
1978 | *insp = u; | |
1979 | ||
1980 | if (name[0]) { | |
1981 | if ((u->name = malloc(strlen(name)+1)) == NULL) | |
1982 | break; | |
1983 | strcpy(u->name, name); | |
1984 | } | |
1985 | if (++cnt > MAX_UNIX_REMEMBER) { | |
1986 | unix_list_print(list, f); | |
1987 | unix_list_free(list); | |
1988 | list = NULL; | |
1989 | cnt = 0; | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | if (list) { | |
1994 | unix_list_print(list, f); | |
1995 | unix_list_free(list); | |
1996 | list = NULL; | |
1997 | cnt = 0; | |
1998 | } | |
1999 | ||
2000 | return 0; | |
2001 | } | |
2002 | ||
2003 | ||
2004 | int packet_show(struct filter *f) | |
2005 | { | |
2006 | FILE *fp; | |
2007 | char buf[256]; | |
2008 | int type; | |
2009 | int prot; | |
2010 | int iface; | |
2011 | int state; | |
2012 | int rq; | |
2013 | int uid; | |
2014 | int ino; | |
2015 | unsigned long long sk; | |
2016 | ||
2017 | if (!(f->states & (1<<SS_CLOSE))) | |
2018 | return 0; | |
2019 | ||
ab01dbbb | 2020 | if ((fp = net_packet_open()) == NULL) |
aba5acdf SH |
2021 | return -1; |
2022 | fgets(buf, sizeof(buf)-1, fp); | |
2023 | ||
2024 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
2025 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", | |
2026 | &sk, | |
2027 | &type, &prot, &iface, &state, | |
2028 | &rq, &uid, &ino); | |
2029 | ||
2030 | if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) | |
2031 | continue; | |
2032 | if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) | |
2033 | continue; | |
2034 | if (f->f) { | |
2035 | struct tcpstat tst; | |
2036 | tst.local.family = AF_PACKET; | |
2037 | tst.remote.family = AF_PACKET; | |
2038 | tst.rport = 0; | |
2039 | tst.lport = iface; | |
2040 | tst.local.data[0] = prot; | |
2041 | tst.remote.data[0] = 0; | |
2042 | if (run_ssfilter(f->f, &tst) == 0) | |
2043 | continue; | |
2044 | } | |
2045 | ||
2046 | if (netid_width) | |
ae665a52 | 2047 | printf("%-*s ", netid_width, |
aba5acdf SH |
2048 | type == SOCK_RAW ? "p_raw" : "p_dgr"); |
2049 | if (state_width) | |
2050 | printf("%-*s ", state_width, "UNCONN"); | |
2051 | printf("%-6d %-6d ", rq, 0); | |
2052 | if (prot == 3) { | |
2053 | printf("%*s:", addr_width, "*"); | |
2054 | } else { | |
2055 | char tb[16]; | |
ae665a52 | 2056 | printf("%*s:", addr_width, |
aba5acdf SH |
2057 | ll_proto_n2a(htons(prot), tb, sizeof(tb))); |
2058 | } | |
2059 | if (iface == 0) { | |
2060 | printf("%-*s ", serv_width, "*"); | |
2061 | } else { | |
2062 | printf("%-*s ", serv_width, xll_index_to_name(iface)); | |
2063 | } | |
2064 | printf("%*s*%-*s", | |
2065 | addr_width, "", serv_width, ""); | |
2066 | ||
2067 | if (show_users) { | |
2068 | char ubuf[4096]; | |
2069 | if (find_users(ino, ubuf, sizeof(ubuf)) > 0) | |
2070 | printf(" users:(%s)", ubuf); | |
2071 | } | |
2072 | if (show_details) { | |
2073 | printf(" ino=%u uid=%u sk=%llx", ino, uid, sk); | |
2074 | } | |
2075 | printf("\n"); | |
2076 | } | |
2077 | ||
2078 | return 0; | |
2079 | } | |
2080 | ||
2081 | int netlink_show(struct filter *f) | |
2082 | { | |
2083 | FILE *fp; | |
2084 | char buf[256]; | |
2085 | int prot, pid; | |
2086 | unsigned groups; | |
2087 | int rq, wq, rc; | |
2088 | unsigned long long sk, cb; | |
2089 | ||
2090 | if (!(f->states & (1<<SS_CLOSE))) | |
2091 | return 0; | |
2092 | ||
ab01dbbb | 2093 | if ((fp = net_netlink_open()) == NULL) |
aba5acdf SH |
2094 | return -1; |
2095 | fgets(buf, sizeof(buf)-1, fp); | |
2096 | ||
2097 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
2098 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", | |
2099 | &sk, | |
2100 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); | |
2101 | ||
2102 | if (f->f) { | |
2103 | struct tcpstat tst; | |
2104 | tst.local.family = AF_NETLINK; | |
2105 | tst.remote.family = AF_NETLINK; | |
2106 | tst.rport = -1; | |
2107 | tst.lport = pid; | |
2108 | tst.local.data[0] = prot; | |
2109 | tst.remote.data[0] = 0; | |
2110 | if (run_ssfilter(f->f, &tst) == 0) | |
2111 | continue; | |
2112 | } | |
2113 | ||
2114 | if (netid_width) | |
ae665a52 | 2115 | printf("%-*s ", netid_width, "nl"); |
aba5acdf SH |
2116 | if (state_width) |
2117 | printf("%-*s ", state_width, "UNCONN"); | |
2118 | printf("%-6d %-6d ", rq, wq); | |
2119 | if (resolve_services && prot == 0) | |
2120 | printf("%*s:", addr_width, "rtnl"); | |
2121 | else if (resolve_services && prot == 3) | |
2122 | printf("%*s:", addr_width, "fw"); | |
2123 | else if (resolve_services && prot == 4) | |
2124 | printf("%*s:", addr_width, "tcpdiag"); | |
2125 | else | |
2126 | printf("%*d:", addr_width, prot); | |
2127 | if (pid == -1) { | |
2128 | printf("%-*s ", serv_width, "*"); | |
2129 | } else if (resolve_services) { | |
2130 | int done = 0; | |
2131 | if (!pid) { | |
2132 | done = 1; | |
2133 | printf("%-*s ", serv_width, "kernel"); | |
2134 | } else if (pid > 0) { | |
2135 | char procname[64]; | |
2136 | FILE *fp; | |
2137 | sprintf(procname, "%s/%d/stat", | |
2138 | getenv("PROC_ROOT") ? : "/proc", pid); | |
2139 | if ((fp = fopen(procname, "r")) != NULL) { | |
2140 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { | |
ae665a52 | 2141 | sprintf(procname+strlen(procname), "/%d", pid); |
aba5acdf SH |
2142 | printf("%-*s ", serv_width, procname); |
2143 | done = 1; | |
2144 | } | |
2145 | fclose(fp); | |
2146 | } | |
2147 | } | |
2148 | if (!done) | |
2149 | printf("%-*d ", serv_width, pid); | |
2150 | } else { | |
2151 | printf("%-*d ", serv_width, pid); | |
2152 | } | |
2153 | printf("%*s*%-*s", | |
2154 | addr_width, "", serv_width, ""); | |
2155 | ||
2156 | if (show_details) { | |
2157 | printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); | |
2158 | } | |
2159 | printf("\n"); | |
2160 | } | |
2161 | ||
2162 | return 0; | |
2163 | } | |
2164 | ||
2165 | struct snmpstat | |
2166 | { | |
2167 | int tcp_estab; | |
2168 | }; | |
2169 | ||
2170 | int get_snmp_int(char *proto, char *key, int *result) | |
2171 | { | |
2172 | char buf[1024]; | |
2173 | FILE *fp; | |
2174 | int protolen = strlen(proto); | |
2175 | int keylen = strlen(key); | |
2176 | ||
2177 | *result = 0; | |
2178 | ||
ab01dbbb | 2179 | if ((fp = net_snmp_open()) == NULL) |
aba5acdf SH |
2180 | return -1; |
2181 | ||
2182 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
2183 | char *p = buf; | |
2184 | int pos = 0; | |
2185 | if (memcmp(buf, proto, protolen)) | |
2186 | continue; | |
2187 | while ((p = strchr(p, ' ')) != NULL) { | |
2188 | pos++; | |
2189 | p++; | |
2190 | if (memcmp(p, key, keylen) == 0 && | |
2191 | (p[keylen] == ' ' || p[keylen] == '\n')) | |
2192 | break; | |
2193 | } | |
2194 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
2195 | break; | |
2196 | if (memcmp(buf, proto, protolen)) | |
2197 | break; | |
2198 | p = buf; | |
2199 | while ((p = strchr(p, ' ')) != NULL) { | |
2200 | p++; | |
2201 | if (--pos == 0) { | |
2202 | sscanf(p, "%d", result); | |
2203 | fclose(fp); | |
2204 | return 0; | |
2205 | } | |
2206 | } | |
2207 | } | |
2208 | ||
2209 | fclose(fp); | |
2210 | errno = ESRCH; | |
2211 | return -1; | |
2212 | } | |
2213 | ||
2214 | ||
2215 | /* Get stats from sockstat */ | |
2216 | ||
2217 | struct sockstat | |
2218 | { | |
2219 | int socks; | |
2220 | int tcp_mem; | |
2221 | int tcp_total; | |
2222 | int tcp_orphans; | |
2223 | int tcp_tws; | |
2224 | int tcp4_hashed; | |
2225 | int udp4; | |
2226 | int raw4; | |
2227 | int frag4; | |
2228 | int frag4_mem; | |
2229 | int tcp6_hashed; | |
2230 | int udp6; | |
2231 | int raw6; | |
2232 | int frag6; | |
2233 | int frag6_mem; | |
2234 | }; | |
2235 | ||
2236 | static void get_sockstat_line(char *line, struct sockstat *s) | |
2237 | { | |
2238 | char id[256], rem[256]; | |
2239 | ||
2240 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) | |
2241 | return; | |
2242 | ||
2243 | if (strcmp(id, "sockets:") == 0) | |
2244 | sscanf(rem, "%*s%d", &s->socks); | |
2245 | else if (strcmp(id, "UDP:") == 0) | |
2246 | sscanf(rem, "%*s%d", &s->udp4); | |
2247 | else if (strcmp(id, "UDP6:") == 0) | |
2248 | sscanf(rem, "%*s%d", &s->udp6); | |
2249 | else if (strcmp(id, "RAW:") == 0) | |
2250 | sscanf(rem, "%*s%d", &s->raw4); | |
2251 | else if (strcmp(id, "RAW6:") == 0) | |
2252 | sscanf(rem, "%*s%d", &s->raw6); | |
2253 | else if (strcmp(id, "TCP6:") == 0) | |
2254 | sscanf(rem, "%*s%d", &s->tcp6_hashed); | |
2255 | else if (strcmp(id, "FRAG:") == 0) | |
2256 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); | |
2257 | else if (strcmp(id, "FRAG6:") == 0) | |
2258 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); | |
2259 | else if (strcmp(id, "TCP:") == 0) | |
2260 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", | |
2261 | &s->tcp4_hashed, | |
2262 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); | |
2263 | } | |
2264 | ||
2265 | int get_sockstat(struct sockstat *s) | |
2266 | { | |
2267 | char buf[256]; | |
2268 | FILE *fp; | |
2269 | ||
2270 | memset(s, 0, sizeof(*s)); | |
2271 | ||
ab01dbbb | 2272 | if ((fp = net_sockstat_open()) == NULL) |
aba5acdf SH |
2273 | return -1; |
2274 | while(fgets(buf, sizeof(buf), fp) != NULL) | |
2275 | get_sockstat_line(buf, s); | |
2276 | fclose(fp); | |
2277 | ||
ab01dbbb | 2278 | if ((fp = net_sockstat6_open()) == NULL) |
aba5acdf SH |
2279 | return 0; |
2280 | while(fgets(buf, sizeof(buf), fp) != NULL) | |
2281 | get_sockstat_line(buf, s); | |
2282 | fclose(fp); | |
2283 | ||
2284 | return 0; | |
2285 | } | |
2286 | ||
2287 | int print_summary(void) | |
2288 | { | |
2289 | struct sockstat s; | |
2290 | struct snmpstat sn; | |
2291 | ||
2292 | if (get_sockstat(&s) < 0) | |
2293 | perror("ss: get_sockstat"); | |
2294 | if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0) | |
2295 | perror("ss: get_snmpstat"); | |
2296 | ||
2297 | printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks); | |
2298 | ||
2299 | printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n", | |
2300 | s.tcp_total + slabstat.tcp_syns + s.tcp_tws, | |
2301 | sn.tcp_estab, | |
2302 | s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws), | |
2303 | s.tcp_orphans, | |
2304 | slabstat.tcp_syns, | |
2305 | s.tcp_tws, slabstat.tcp_tws, | |
2306 | slabstat.tcp_ports | |
2307 | ); | |
2308 | ||
2309 | printf("\n"); | |
2310 | printf("Transport Total IP IPv6\n"); | |
2311 | printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-"); | |
2312 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); | |
2313 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); | |
2314 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); | |
ae665a52 | 2315 | printf("INET %-9d %-9d %-9d\n", |
aba5acdf SH |
2316 | s.raw4+s.udp4+s.tcp4_hashed+ |
2317 | s.raw6+s.udp6+s.tcp6_hashed, | |
2318 | s.raw4+s.udp4+s.tcp4_hashed, | |
2319 | s.raw6+s.udp6+s.tcp6_hashed); | |
2320 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); | |
2321 | ||
2322 | printf("\n"); | |
2323 | ||
2324 | return 0; | |
2325 | } | |
2326 | ||
2327 | ||
2328 | static void usage(void) __attribute__((noreturn)); | |
2329 | ||
2330 | static void usage(void) | |
2331 | { | |
2332 | fprintf(stderr, | |
2333 | "Usage: ss [ OPTIONS ]\n" | |
2334 | " ss [ OPTIONS ] [ FILTER ]\n" | |
ab61159a SH |
2335 | " -h, --help this message\n" |
2336 | " -V, --version output version information\n" | |
2337 | " -n, --numeric don't resolve service names\n" | |
2338 | " -r, --resolve resolve host names\n" | |
2339 | " -a, --all display all sockets\n" | |
2340 | " -l, --listening display listening sockets\n" | |
2341 | " -o, --options show timer information\n" | |
2342 | " -e, --extended show detailed socket information\n" | |
2343 | " -m, --memory show socket memory usage\n" | |
2344 | " -p, --processes show process using socket\n" | |
2345 | " -i, --info show internal TCP information\n" | |
2346 | " -s, --summary show socket usage summary\n" | |
2347 | "\n" | |
2348 | " -4, --ipv4 display only IP version 4 sockets\n" | |
2349 | " -6, --ipv6 display only IP version 6 sockets\n" | |
2350 | " -0, --packet display PACKET sockets\n" | |
2351 | " -t, --tcp display only TCP sockets\n" | |
2352 | " -u, --udp display only UDP sockets\n" | |
351efcde | 2353 | " -d, --dccp display only DCCP sockets\n" |
ab61159a SH |
2354 | " -w, --raw display only RAW sockets\n" |
2355 | " -x, --unix display only Unix domain sockets\n" | |
2356 | " -f, --family=FAMILY display sockets of type FAMILY\n" | |
2357 | "\n" | |
2358 | " -A, --query=QUERY\n" | |
aba5acdf | 2359 | " QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n" |
ab61159a SH |
2360 | "\n" |
2361 | " -F, --filter=FILE read filter information from FILE\n" | |
aba5acdf | 2362 | " FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n" |
ab61159a | 2363 | ); |
aba5acdf SH |
2364 | exit(-1); |
2365 | } | |
2366 | ||
2367 | ||
7d105b56 | 2368 | int scan_state(const char *state) |
aba5acdf SH |
2369 | { |
2370 | int i; | |
2371 | if (strcasecmp(state, "close") == 0 || | |
2372 | strcasecmp(state, "closed") == 0) | |
2373 | return (1<<SS_CLOSE); | |
2374 | if (strcasecmp(state, "syn-rcv") == 0) | |
2375 | return (1<<SS_SYN_RECV); | |
1a5bad5a | 2376 | if (strcasecmp(state, "established") == 0) |
aba5acdf SH |
2377 | return (1<<SS_ESTABLISHED); |
2378 | if (strcasecmp(state, "all") == 0) | |
2379 | return SS_ALL; | |
2380 | if (strcasecmp(state, "connected") == 0) | |
2381 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); | |
1a5bad5a | 2382 | if (strcasecmp(state, "synchronized") == 0) |
aba5acdf SH |
2383 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
2384 | if (strcasecmp(state, "bucket") == 0) | |
2385 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); | |
2386 | if (strcasecmp(state, "big") == 0) | |
2387 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); | |
2388 | for (i=0; i<SS_MAX; i++) { | |
1a5bad5a | 2389 | if (strcasecmp(state, sstate_namel[i]) == 0) |
aba5acdf SH |
2390 | return (1<<i); |
2391 | } | |
2392 | return 0; | |
2393 | } | |
2394 | ||
ab61159a SH |
2395 | static const struct option long_opts[] = { |
2396 | { "numeric", 0, 0, 'n' }, | |
2397 | { "resolve", 0, 0, 'r' }, | |
2398 | { "options", 0, 0, 'o' }, | |
2399 | { "extended", 0, 0, 'e' }, | |
2400 | { "memory", 0, 0, 'm' }, | |
2401 | { "info", 0, 0, 'i' }, | |
2402 | { "processes", 0, 0, 'p' }, | |
351efcde | 2403 | { "dccp", 0, 0, 'd' }, |
ab61159a SH |
2404 | { "tcp", 0, 0, 't' }, |
2405 | { "udp", 0, 0, 'u' }, | |
2406 | { "raw", 0, 0, 'w' }, | |
2407 | { "unix", 0, 0, 'x' }, | |
2408 | { "all", 0, 0, 'a' }, | |
2409 | { "listening", 0, 0, 'l' }, | |
2410 | { "ipv4", 0, 0, '4' }, | |
2411 | { "ipv6", 0, 0, '6' }, | |
2412 | { "packet", 0, 0, '0' }, | |
2413 | { "family", 1, 0, 'f' }, | |
2414 | { "socket", 1, 0, 'A' }, | |
c3f346b0 | 2415 | { "summary", 0, 0, 's' }, |
ab61159a SH |
2416 | { "diag", 0, 0, 'D' }, |
2417 | { "filter", 1, 0, 'F' }, | |
2418 | { "version", 0, 0, 'V' }, | |
2419 | { "help", 0, 0, 'h' }, | |
2420 | { 0 } | |
ae665a52 | 2421 | |
ab61159a SH |
2422 | }; |
2423 | ||
aba5acdf SH |
2424 | int main(int argc, char *argv[]) |
2425 | { | |
2426 | int do_default = 1; | |
2427 | int saw_states = 0; | |
2428 | int saw_query = 0; | |
2429 | int do_summary = 0; | |
7d105b56 | 2430 | const char *dump_tcpdiag = NULL; |
aba5acdf SH |
2431 | FILE *filter_fp = NULL; |
2432 | int ch; | |
2433 | ||
2434 | memset(¤t_filter, 0, sizeof(current_filter)); | |
2435 | ||
2436 | current_filter.states = default_filter.states; | |
2437 | ||
351efcde | 2438 | while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spf:miA:D:F:vV", |
ab61159a | 2439 | long_opts, NULL)) != EOF) { |
aba5acdf SH |
2440 | switch(ch) { |
2441 | case 'n': | |
2442 | resolve_services = 0; | |
2443 | break; | |
2444 | case 'r': | |
2445 | resolve_hosts = 1; | |
2446 | break; | |
2447 | case 'o': | |
2448 | show_options = 1; | |
2449 | break; | |
2450 | case 'e': | |
2451 | show_options = 1; | |
2452 | show_details++; | |
2453 | break; | |
2454 | case 'm': | |
2455 | show_mem = 1; | |
2456 | break; | |
2457 | case 'i': | |
2458 | show_tcpinfo = 1; | |
2459 | break; | |
2460 | case 'p': | |
2461 | show_users++; | |
2462 | break; | |
351efcde SH |
2463 | case 'd': |
2464 | current_filter.dbs |= (1<<DCCP_DB); | |
2465 | do_default = 0; | |
2466 | break; | |
aba5acdf SH |
2467 | case 't': |
2468 | current_filter.dbs |= (1<<TCP_DB); | |
2469 | do_default = 0; | |
2470 | break; | |
2471 | case 'u': | |
2472 | current_filter.dbs |= (1<<UDP_DB); | |
2473 | do_default = 0; | |
2474 | break; | |
2475 | case 'w': | |
2476 | current_filter.dbs |= (1<<RAW_DB); | |
2477 | do_default = 0; | |
2478 | break; | |
2479 | case 'x': | |
2480 | current_filter.dbs |= UNIX_DBM; | |
2481 | do_default = 0; | |
2482 | break; | |
2483 | case 'a': | |
2484 | current_filter.states = SS_ALL; | |
2485 | break; | |
2486 | case 'l': | |
2487 | current_filter.states = (1<<SS_LISTEN); | |
2488 | break; | |
2489 | case '4': | |
2490 | preferred_family = AF_INET; | |
2491 | break; | |
2492 | case '6': | |
2493 | preferred_family = AF_INET6; | |
2494 | break; | |
2495 | case '0': | |
2496 | preferred_family = AF_PACKET; | |
2497 | break; | |
2498 | case 'f': | |
2499 | if (strcmp(optarg, "inet") == 0) | |
2500 | preferred_family = AF_INET; | |
2501 | else if (strcmp(optarg, "inet6") == 0) | |
2502 | preferred_family = AF_INET6; | |
2503 | else if (strcmp(optarg, "link") == 0) | |
2504 | preferred_family = AF_PACKET; | |
2505 | else if (strcmp(optarg, "unix") == 0) | |
2506 | preferred_family = AF_UNIX; | |
2507 | else if (strcmp(optarg, "netlink") == 0) | |
2508 | preferred_family = AF_NETLINK; | |
2509 | else if (strcmp(optarg, "help") == 0) | |
2510 | usage(); | |
2511 | else { | |
2512 | fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg); | |
2513 | usage(); | |
2514 | } | |
2515 | break; | |
2516 | case 'A': | |
2517 | { | |
2518 | char *p, *p1; | |
2519 | if (!saw_query) { | |
2520 | current_filter.dbs = 0; | |
2521 | saw_query = 1; | |
2522 | do_default = 0; | |
2523 | } | |
2524 | p = p1 = optarg; | |
2525 | do { | |
2526 | if ((p1 = strchr(p, ',')) != NULL) | |
ae665a52 | 2527 | *p1 = 0; |
aba5acdf SH |
2528 | if (strcmp(p, "all") == 0) { |
2529 | current_filter.dbs = ALL_DB; | |
2530 | } else if (strcmp(p, "inet") == 0) { | |
351efcde | 2531 | current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB); |
aba5acdf SH |
2532 | } else if (strcmp(p, "udp") == 0) { |
2533 | current_filter.dbs |= (1<<UDP_DB); | |
351efcde SH |
2534 | } else if (strcmp(p, "dccp") == 0) { |
2535 | current_filter.dbs |= (1<<DCCP_DB); | |
aba5acdf SH |
2536 | } else if (strcmp(p, "tcp") == 0) { |
2537 | current_filter.dbs |= (1<<TCP_DB); | |
2538 | } else if (strcmp(p, "raw") == 0) { | |
2539 | current_filter.dbs |= (1<<RAW_DB); | |
2540 | } else if (strcmp(p, "unix") == 0) { | |
2541 | current_filter.dbs |= UNIX_DBM; | |
1a5bad5a | 2542 | } else if (strcasecmp(p, "unix_stream") == 0 || |
aba5acdf SH |
2543 | strcmp(p, "u_str") == 0) { |
2544 | current_filter.dbs |= (1<<UNIX_ST_DB); | |
1a5bad5a | 2545 | } else if (strcasecmp(p, "unix_dgram") == 0 || |
aba5acdf SH |
2546 | strcmp(p, "u_dgr") == 0) { |
2547 | current_filter.dbs |= (1<<UNIX_DG_DB); | |
2548 | } else if (strcmp(p, "packet") == 0) { | |
2549 | current_filter.dbs |= PACKET_DBM; | |
2550 | } else if (strcmp(p, "packet_raw") == 0 || | |
2551 | strcmp(p, "p_raw") == 0) { | |
2552 | current_filter.dbs |= (1<<PACKET_R_DB); | |
2553 | } else if (strcmp(p, "packet_dgram") == 0 || | |
2554 | strcmp(p, "p_dgr") == 0) { | |
2555 | current_filter.dbs |= (1<<PACKET_DG_DB); | |
2556 | } else if (strcmp(p, "netlink") == 0) { | |
2557 | current_filter.dbs |= (1<<NETLINK_DB); | |
2558 | } else { | |
2559 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); | |
2560 | usage(); | |
2561 | } | |
2562 | p = p1 + 1; | |
2563 | } while (p1); | |
2564 | break; | |
2565 | } | |
2566 | case 's': | |
2567 | do_summary = 1; | |
2568 | break; | |
2569 | case 'D': | |
2570 | dump_tcpdiag = optarg; | |
2571 | break; | |
2572 | case 'F': | |
2573 | if (filter_fp) { | |
2574 | fprintf(stderr, "More than one filter file\n"); | |
2575 | exit(-1); | |
2576 | } | |
2577 | if (optarg[0] == '-') | |
2578 | filter_fp = stdin; | |
2579 | else | |
2580 | filter_fp = fopen(optarg, "r"); | |
2581 | if (!filter_fp) { | |
2582 | perror("fopen filter file"); | |
2583 | exit(-1); | |
2584 | } | |
2585 | break; | |
2586 | case 'v': | |
2587 | case 'V': | |
2588 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); | |
2589 | exit(0); | |
2590 | case 'h': | |
2591 | case '?': | |
2592 | default: | |
2593 | usage(); | |
2594 | } | |
2595 | } | |
2596 | ||
2597 | argc -= optind; | |
2598 | argv += optind; | |
2599 | ||
2600 | get_slabstat(&slabstat); | |
2601 | ||
2602 | if (do_summary) { | |
2603 | print_summary(); | |
2604 | if (do_default && argc == 0) | |
2605 | exit(0); | |
2606 | } | |
2607 | ||
2608 | if (do_default) | |
2609 | current_filter.dbs = default_filter.dbs; | |
2610 | ||
2611 | if (preferred_family == AF_UNSPEC) { | |
2612 | if (!(current_filter.dbs&~UNIX_DBM)) | |
2613 | preferred_family = AF_UNIX; | |
2614 | else if (!(current_filter.dbs&~PACKET_DBM)) | |
2615 | preferred_family = AF_PACKET; | |
2616 | else if (!(current_filter.dbs&~(1<<NETLINK_DB))) | |
2617 | preferred_family = AF_NETLINK; | |
2618 | } | |
2619 | ||
2620 | if (preferred_family != AF_UNSPEC) { | |
2621 | int mask2; | |
2622 | if (preferred_family == AF_INET || | |
2623 | preferred_family == AF_INET6) { | |
2624 | mask2= (1<<TCP_DB); | |
2625 | if (!do_default) | |
2626 | mask2 = (1<<UDP_DB)|(1<<RAW_DB); | |
2627 | } else if (preferred_family == AF_PACKET) { | |
2628 | mask2 = PACKET_DBM; | |
2629 | } else if (preferred_family == AF_UNIX) { | |
2630 | mask2 = UNIX_DBM; | |
2631 | } else if (preferred_family == AF_NETLINK) { | |
2632 | mask2 = (1<<NETLINK_DB); | |
2633 | } else { | |
2634 | mask2 = 0; | |
2635 | } | |
2636 | ||
2637 | if (do_default) | |
2638 | current_filter.dbs = mask2; | |
2639 | else | |
2640 | current_filter.dbs &= mask2; | |
2641 | current_filter.families = (1<<preferred_family); | |
2642 | } else { | |
2643 | if (!do_default) | |
2644 | current_filter.families = ~0; | |
2645 | else | |
2646 | current_filter.families = default_filter.families; | |
2647 | } | |
2648 | if (current_filter.dbs == 0) { | |
2649 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); | |
2650 | exit(0); | |
2651 | } | |
2652 | if (current_filter.families == 0) { | |
2653 | fprintf(stderr, "ss: no families to show with such filter.\n"); | |
2654 | exit(0); | |
2655 | } | |
2656 | ||
2657 | if (resolve_services && resolve_hosts && | |
351efcde | 2658 | (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)))) |
aba5acdf SH |
2659 | init_service_resolver(); |
2660 | ||
2661 | /* Now parse filter... */ | |
2662 | if (argc == 0 && filter_fp) { | |
2663 | if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp)) | |
2664 | usage(); | |
2665 | } | |
2666 | ||
2667 | while (argc > 0) { | |
2668 | if (strcmp(*argv, "state") == 0) { | |
2669 | NEXT_ARG(); | |
2670 | if (!saw_states) | |
2671 | current_filter.states = 0; | |
2672 | current_filter.states |= scan_state(*argv); | |
2673 | saw_states = 1; | |
2674 | } else if (strcmp(*argv, "exclude") == 0 || | |
2675 | strcmp(*argv, "excl") == 0) { | |
2676 | NEXT_ARG(); | |
2677 | if (!saw_states) | |
2678 | current_filter.states = SS_ALL; | |
2679 | current_filter.states &= ~scan_state(*argv); | |
2680 | saw_states = 1; | |
2681 | } else { | |
2682 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) | |
2683 | usage(); | |
2684 | break; | |
2685 | } | |
2686 | argc--; argv++; | |
2687 | } | |
2688 | ||
2689 | if (current_filter.states == 0) { | |
2690 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); | |
2691 | exit(0); | |
2692 | } | |
2693 | ||
2694 | if (dump_tcpdiag) { | |
2695 | FILE *dump_fp = stdout; | |
2696 | if (!(current_filter.dbs & (1<<TCP_DB))) { | |
2697 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); | |
2698 | exit(0); | |
2699 | } | |
2700 | if (dump_tcpdiag[0] != '-') { | |
2701 | dump_fp = fopen(dump_tcpdiag, "w"); | |
2702 | if (!dump_tcpdiag) { | |
2703 | perror("fopen dump file"); | |
2704 | exit(-1); | |
2705 | } | |
2706 | } | |
351efcde | 2707 | tcp_show_netlink(¤t_filter, dump_fp, TCPDIAG_GETSOCK); |
aba5acdf SH |
2708 | fflush(dump_fp); |
2709 | exit(0); | |
2710 | } | |
2711 | ||
2712 | netid_width = 0; | |
2713 | if (current_filter.dbs&(current_filter.dbs-1)) | |
2714 | netid_width = 5; | |
2715 | ||
2716 | state_width = 0; | |
2717 | if (current_filter.states&(current_filter.states-1)) | |
2718 | state_width = 10; | |
2719 | ||
2720 | screen_width = 80; | |
2721 | if (isatty(STDOUT_FILENO)) { | |
2722 | struct winsize w; | |
2723 | ||
2724 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { | |
2725 | if (w.ws_col > 0) | |
2726 | screen_width = w.ws_col; | |
2727 | } | |
2728 | } | |
2729 | ||
2730 | addrp_width = screen_width; | |
2731 | addrp_width -= netid_width+1; | |
2732 | addrp_width -= state_width+1; | |
2733 | addrp_width -= 14; | |
2734 | ||
2735 | if (addrp_width&1) { | |
2736 | if (netid_width) | |
2737 | netid_width++; | |
2738 | else if (state_width) | |
2739 | state_width++; | |
2740 | } | |
2741 | ||
2742 | addrp_width /= 2; | |
2743 | addrp_width--; | |
2744 | ||
2745 | serv_width = resolve_services ? 7 : 5; | |
2746 | ||
2747 | if (addrp_width < 15+serv_width+1) | |
2748 | addrp_width = 15+serv_width+1; | |
2749 | ||
ae665a52 | 2750 | addr_width = addrp_width - serv_width - 1; |
aba5acdf SH |
2751 | |
2752 | if (netid_width) | |
2753 | printf("%-*s ", netid_width, "Netid"); | |
2754 | if (state_width) | |
2755 | printf("%-*s ", state_width, "State"); | |
2756 | printf("%-6s %-6s ", "Recv-Q", "Send-Q"); | |
2757 | ||
2758 | printf("%*s:%-*s %*s:%-*s\n", | |
2759 | addr_width, "Local Address", serv_width, "Port", | |
2760 | addr_width, "Peer Address", serv_width, "Port"); | |
2761 | ||
aba5acdf SH |
2762 | fflush(stdout); |
2763 | ||
2764 | if (current_filter.dbs & (1<<NETLINK_DB)) | |
2765 | netlink_show(¤t_filter); | |
2766 | if (current_filter.dbs & PACKET_DBM) | |
2767 | packet_show(¤t_filter); | |
2768 | if (current_filter.dbs & UNIX_DBM) | |
2769 | unix_show(¤t_filter); | |
2770 | if (current_filter.dbs & (1<<RAW_DB)) | |
2771 | raw_show(¤t_filter); | |
2772 | if (current_filter.dbs & (1<<UDP_DB)) | |
2773 | udp_show(¤t_filter); | |
2774 | if (current_filter.dbs & (1<<TCP_DB)) | |
351efcde SH |
2775 | tcp_show(¤t_filter, TCPDIAG_GETSOCK); |
2776 | if (current_filter.dbs & (1<<DCCP_DB)) | |
2777 | tcp_show(¤t_filter, DCCPDIAG_GETSOCK); | |
aba5acdf SH |
2778 | return 0; |
2779 | } |