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