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