]>
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 | ||
5b816047 PE |
1357 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
1358 | struct rtattr *tb[]) | |
7d105b56 | 1359 | { |
b4b0b7d5 SH |
1360 | char b1[64]; |
1361 | double rtt = 0; | |
7d105b56 | 1362 | |
910b0397 | 1363 | if (tb[INET_DIAG_SKMEMINFO]) { |
51ff9f24 | 1364 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
c6d6c92c | 1365 | } else if (tb[INET_DIAG_MEMINFO]) { |
351efcde SH |
1366 | const struct inet_diag_meminfo *minfo |
1367 | = RTA_DATA(tb[INET_DIAG_MEMINFO]); | |
7d105b56 | 1368 | printf(" mem:(r%u,w%u,f%u,t%u)", |
351efcde SH |
1369 | minfo->idiag_rmem, |
1370 | minfo->idiag_wmem, | |
1371 | minfo->idiag_fmem, | |
1372 | minfo->idiag_tmem); | |
7d105b56 SH |
1373 | } |
1374 | ||
351efcde | 1375 | if (tb[INET_DIAG_INFO]) { |
05e18118 | 1376 | struct tcp_info *info; |
351efcde | 1377 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
05e18118 SH |
1378 | |
1379 | /* workaround for older kernels with less fields */ | |
1380 | if (len < sizeof(*info)) { | |
1381 | info = alloca(sizeof(*info)); | |
1382 | memset(info, 0, sizeof(*info)); | |
351efcde | 1383 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
05e18118 | 1384 | } else |
351efcde | 1385 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
05e18118 | 1386 | |
b4b0b7d5 SH |
1387 | if (show_options) { |
1388 | if (info->tcpi_options & TCPI_OPT_TIMESTAMPS) | |
1389 | printf(" ts"); | |
1390 | if (info->tcpi_options & TCPI_OPT_SACK) | |
1391 | printf(" sack"); | |
1392 | if (info->tcpi_options & TCPI_OPT_ECN) | |
1393 | printf(" ecn"); | |
719b958b ED |
1394 | if (info->tcpi_options & TCPI_OPT_ECN_SEEN) |
1395 | printf(" ecnseen"); | |
9cb1eccf ED |
1396 | if (info->tcpi_options & TCPI_OPT_SYN_DATA) |
1397 | printf(" fastopen"); | |
b4b0b7d5 | 1398 | } |
52d5ac3f | 1399 | |
351efcde | 1400 | if (tb[INET_DIAG_CONG]) |
ff24746c | 1401 | printf(" %s", rta_getattr_str(tb[INET_DIAG_CONG])); |
ea8fc104 | 1402 | |
ae665a52 | 1403 | if (info->tcpi_options & TCPI_OPT_WSCALE) |
05e18118 SH |
1404 | printf(" wscale:%d,%d", info->tcpi_snd_wscale, |
1405 | info->tcpi_rcv_wscale); | |
7d105b56 SH |
1406 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
1407 | printf(" rto:%g", (double)info->tcpi_rto/1000); | |
1408 | if (info->tcpi_rtt) | |
1409 | printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000, | |
1410 | (double)info->tcpi_rttvar/1000); | |
1411 | if (info->tcpi_ato) | |
1412 | printf(" ato:%g", (double)info->tcpi_ato/1000); | |
4d354347 BH |
1413 | if (info->tcpi_snd_mss) |
1414 | printf(" mss:%d", info->tcpi_snd_mss); | |
7d105b56 SH |
1415 | if (info->tcpi_snd_cwnd != 2) |
1416 | printf(" cwnd:%d", info->tcpi_snd_cwnd); | |
1417 | if (info->tcpi_snd_ssthresh < 0xFFFF) | |
1418 | printf(" ssthresh:%d", info->tcpi_snd_ssthresh); | |
52d5ac3f | 1419 | |
b4b0b7d5 | 1420 | rtt = (double) info->tcpi_rtt; |
351efcde | 1421 | if (tb[INET_DIAG_VEGASINFO]) { |
05e18118 | 1422 | const struct tcpvegas_info *vinfo |
351efcde | 1423 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
7d105b56 | 1424 | |
ae665a52 | 1425 | if (vinfo->tcpv_enabled && |
ea8fc104 SH |
1426 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
1427 | rtt = vinfo->tcpv_rtt; | |
b4b0b7d5 SH |
1428 | } |
1429 | ||
1430 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { | |
1431 | printf(" send %sbps", | |
1432 | sprint_bw(b1, (double) info->tcpi_snd_cwnd * | |
1433 | (double) info->tcpi_snd_mss * 8000000. | |
1434 | / rtt)); | |
7d105b56 | 1435 | } |
b4b0b7d5 SH |
1436 | |
1437 | if (info->tcpi_rcv_rtt) | |
1438 | printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000); | |
1439 | if (info->tcpi_rcv_space) | |
1440 | printf(" rcv_space:%d", info->tcpi_rcv_space); | |
1441 | ||
7d105b56 | 1442 | } |
7d105b56 | 1443 | } |
aba5acdf | 1444 | |
3fe5b534 | 1445 | static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f) |
aba5acdf | 1446 | { |
5b816047 | 1447 | struct rtattr * tb[INET_DIAG_MAX+1]; |
351efcde | 1448 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
aba5acdf SH |
1449 | struct tcpstat s; |
1450 | ||
5b816047 PE |
1451 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), |
1452 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
1453 | ||
351efcde SH |
1454 | s.state = r->idiag_state; |
1455 | s.local.family = s.remote.family = r->idiag_family; | |
1456 | s.lport = ntohs(r->id.idiag_sport); | |
1457 | s.rport = ntohs(r->id.idiag_dport); | |
aba5acdf SH |
1458 | if (s.local.family == AF_INET) { |
1459 | s.local.bytelen = s.remote.bytelen = 4; | |
1460 | } else { | |
1461 | s.local.bytelen = s.remote.bytelen = 16; | |
1462 | } | |
351efcde SH |
1463 | memcpy(s.local.data, r->id.idiag_src, s.local.bytelen); |
1464 | memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen); | |
aba5acdf SH |
1465 | |
1466 | if (f && f->f && run_ssfilter(f->f, &s) == 0) | |
1467 | return 0; | |
1468 | ||
1469 | if (netid_width) | |
1470 | printf("%-*s ", netid_width, "tcp"); | |
1471 | if (state_width) | |
1472 | printf("%-*s ", state_width, sstate_name[s.state]); | |
1473 | ||
351efcde | 1474 | printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue); |
aba5acdf SH |
1475 | |
1476 | formatted_print(&s.local, s.lport); | |
1477 | formatted_print(&s.remote, s.rport); | |
1478 | ||
1479 | if (show_options) { | |
351efcde SH |
1480 | if (r->idiag_timer) { |
1481 | if (r->idiag_timer > 4) | |
1482 | r->idiag_timer = 5; | |
aba5acdf | 1483 | printf(" timer:(%s,%s,%d)", |
351efcde SH |
1484 | tmr_name[r->idiag_timer], |
1485 | print_ms_timer(r->idiag_expires), | |
1486 | r->idiag_retrans); | |
aba5acdf SH |
1487 | } |
1488 | } | |
1489 | if (show_users) { | |
1490 | char ubuf[4096]; | |
351efcde | 1491 | if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0) |
aba5acdf SH |
1492 | printf(" users:(%s)", ubuf); |
1493 | } | |
1494 | if (show_details) { | |
351efcde SH |
1495 | if (r->idiag_uid) |
1496 | printf(" uid:%u", (unsigned)r->idiag_uid); | |
e7113c61 | 1497 | printf(" ino:%u", r->idiag_inode); |
bbe32053 | 1498 | printf(" sk:"); |
351efcde SH |
1499 | if (r->id.idiag_cookie[1] != 0) |
1500 | printf("%08x", r->id.idiag_cookie[1]); | |
bbe32053 | 1501 | printf("%08x", r->id.idiag_cookie[0]); |
5b816047 PE |
1502 | if (tb[INET_DIAG_SHUTDOWN]) { |
1503 | unsigned char mask; | |
1504 | mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]); | |
1505 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
1506 | } | |
aba5acdf SH |
1507 | } |
1508 | if (show_mem || show_tcpinfo) { | |
7d105b56 | 1509 | printf("\n\t"); |
5b816047 | 1510 | tcp_show_info(nlh, r, tb); |
aba5acdf | 1511 | } |
7d105b56 | 1512 | |
aba5acdf SH |
1513 | printf("\n"); |
1514 | ||
1515 | return 0; | |
aba5acdf SH |
1516 | } |
1517 | ||
746a695f | 1518 | static int tcpdiag_send(int fd, int protocol, struct filter *f) |
aba5acdf | 1519 | { |
aba5acdf SH |
1520 | struct sockaddr_nl nladdr; |
1521 | struct { | |
1522 | struct nlmsghdr nlh; | |
351efcde | 1523 | struct inet_diag_req r; |
aba5acdf SH |
1524 | } req; |
1525 | char *bc = NULL; | |
1526 | int bclen; | |
1527 | struct msghdr msg; | |
1528 | struct rtattr rta; | |
aba5acdf SH |
1529 | struct iovec iov[3]; |
1530 | ||
346f8ca8 PE |
1531 | if (protocol == IPPROTO_UDP) |
1532 | return -1; | |
1533 | ||
aba5acdf SH |
1534 | memset(&nladdr, 0, sizeof(nladdr)); |
1535 | nladdr.nl_family = AF_NETLINK; | |
1536 | ||
1537 | req.nlh.nlmsg_len = sizeof(req); | |
3fe5b534 PE |
1538 | if (protocol == IPPROTO_TCP) |
1539 | req.nlh.nlmsg_type = TCPDIAG_GETSOCK; | |
1540 | else | |
1541 | req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; | |
aba5acdf SH |
1542 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
1543 | req.nlh.nlmsg_pid = 0; | |
1544 | req.nlh.nlmsg_seq = 123456; | |
1545 | memset(&req.r, 0, sizeof(req.r)); | |
351efcde SH |
1546 | req.r.idiag_family = AF_INET; |
1547 | req.r.idiag_states = f->states; | |
910b0397 | 1548 | if (show_mem) { |
ae665a52 | 1549 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
910b0397 SW |
1550 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
1551 | } | |
b4b0b7d5 | 1552 | |
7d105b56 | 1553 | if (show_tcpinfo) { |
351efcde SH |
1554 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
1555 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
1556 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
7d105b56 | 1557 | } |
aba5acdf | 1558 | |
ae665a52 SH |
1559 | iov[0] = (struct iovec){ |
1560 | .iov_base = &req, | |
1561 | .iov_len = sizeof(req) | |
ea8fc104 | 1562 | }; |
aba5acdf SH |
1563 | if (f->f) { |
1564 | bclen = ssfilter_bytecompile(f->f, &bc); | |
351efcde | 1565 | rta.rta_type = INET_DIAG_REQ_BYTECODE; |
aba5acdf SH |
1566 | rta.rta_len = RTA_LENGTH(bclen); |
1567 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
1568 | iov[2] = (struct iovec){ bc, bclen }; | |
1569 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
1570 | } | |
1571 | ||
1572 | msg = (struct msghdr) { | |
ae665a52 | 1573 | .msg_name = (void*)&nladdr, |
ea8fc104 | 1574 | .msg_namelen = sizeof(nladdr), |
ae665a52 | 1575 | .msg_iov = iov, |
ea8fc104 | 1576 | .msg_iovlen = f->f ? 3 : 1, |
aba5acdf SH |
1577 | }; |
1578 | ||
930a75f9 ED |
1579 | if (sendmsg(fd, &msg, 0) < 0) { |
1580 | close(fd); | |
aba5acdf | 1581 | return -1; |
930a75f9 | 1582 | } |
aba5acdf | 1583 | |
746a695f PE |
1584 | return 0; |
1585 | } | |
1586 | ||
886d19d6 PE |
1587 | static int sockdiag_send(int family, int fd, int protocol, struct filter *f) |
1588 | { | |
1589 | struct sockaddr_nl nladdr; | |
1590 | struct { | |
1591 | struct nlmsghdr nlh; | |
1592 | struct inet_diag_req_v2 r; | |
1593 | } req; | |
1594 | char *bc = NULL; | |
1595 | int bclen; | |
1596 | struct msghdr msg; | |
1597 | struct rtattr rta; | |
1598 | struct iovec iov[3]; | |
1599 | ||
1600 | if (family == PF_UNSPEC) | |
1601 | return tcpdiag_send(fd, protocol, f); | |
1602 | ||
1603 | memset(&nladdr, 0, sizeof(nladdr)); | |
1604 | nladdr.nl_family = AF_NETLINK; | |
1605 | ||
1606 | req.nlh.nlmsg_len = sizeof(req); | |
1607 | req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; | |
1608 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; | |
1609 | req.nlh.nlmsg_pid = 0; | |
1610 | req.nlh.nlmsg_seq = 123456; | |
1611 | memset(&req.r, 0, sizeof(req.r)); | |
1612 | req.r.sdiag_family = family; | |
1613 | req.r.sdiag_protocol = protocol; | |
1614 | req.r.idiag_states = f->states; | |
1615 | if (show_mem) { | |
1616 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); | |
1617 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); | |
1618 | } | |
1619 | ||
1620 | if (show_tcpinfo) { | |
1621 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); | |
1622 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
1623 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
1624 | } | |
1625 | ||
1626 | iov[0] = (struct iovec){ | |
1627 | .iov_base = &req, | |
1628 | .iov_len = sizeof(req) | |
1629 | }; | |
1630 | if (f->f) { | |
1631 | bclen = ssfilter_bytecompile(f->f, &bc); | |
1632 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
1633 | rta.rta_len = RTA_LENGTH(bclen); | |
1634 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
1635 | iov[2] = (struct iovec){ bc, bclen }; | |
1636 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
1637 | } | |
1638 | ||
1639 | msg = (struct msghdr) { | |
1640 | .msg_name = (void*)&nladdr, | |
1641 | .msg_namelen = sizeof(nladdr), | |
1642 | .msg_iov = iov, | |
1643 | .msg_iovlen = f->f ? 3 : 1, | |
1644 | }; | |
1645 | ||
1646 | if (sendmsg(fd, &msg, 0) < 0) { | |
1647 | close(fd); | |
1648 | return -1; | |
1649 | } | |
1650 | ||
1651 | return 0; | |
1652 | } | |
1653 | ||
746a695f PE |
1654 | static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) |
1655 | { | |
886d19d6 | 1656 | int fd, family; |
746a695f PE |
1657 | struct sockaddr_nl nladdr; |
1658 | struct msghdr msg; | |
1659 | char buf[8192]; | |
1660 | struct iovec iov[3]; | |
1661 | ||
1662 | if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0) | |
1663 | return -1; | |
1664 | ||
886d19d6 PE |
1665 | family = PF_INET; |
1666 | again: | |
1667 | if (sockdiag_send(family, fd, protocol, f)) | |
746a695f PE |
1668 | return -1; |
1669 | ||
1670 | memset(&nladdr, 0, sizeof(nladdr)); | |
1671 | nladdr.nl_family = AF_NETLINK; | |
1672 | ||
ae665a52 SH |
1673 | iov[0] = (struct iovec){ |
1674 | .iov_base = buf, | |
1675 | .iov_len = sizeof(buf) | |
ea8fc104 | 1676 | }; |
aba5acdf SH |
1677 | |
1678 | while (1) { | |
1679 | int status; | |
1680 | struct nlmsghdr *h; | |
1681 | ||
1682 | msg = (struct msghdr) { | |
1683 | (void*)&nladdr, sizeof(nladdr), | |
1684 | iov, 1, | |
1685 | NULL, 0, | |
1686 | 0 | |
1687 | }; | |
1688 | ||
1689 | status = recvmsg(fd, &msg, 0); | |
1690 | ||
1691 | if (status < 0) { | |
1692 | if (errno == EINTR) | |
1693 | continue; | |
1694 | perror("OVERRUN"); | |
1695 | continue; | |
1696 | } | |
1697 | if (status == 0) { | |
1698 | fprintf(stderr, "EOF on netlink\n"); | |
c51577cd | 1699 | close(fd); |
aba5acdf SH |
1700 | return 0; |
1701 | } | |
1702 | ||
1703 | if (dump_fp) | |
1704 | fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp); | |
1705 | ||
1706 | h = (struct nlmsghdr*)buf; | |
1707 | while (NLMSG_OK(h, status)) { | |
1708 | int err; | |
a37b01c1 | 1709 | struct inet_diag_msg *r = NLMSG_DATA(h); |
aba5acdf SH |
1710 | |
1711 | if (/*h->nlmsg_pid != rth->local.nl_pid ||*/ | |
1712 | h->nlmsg_seq != 123456) | |
1713 | goto skip_it; | |
1714 | ||
886d19d6 PE |
1715 | if (h->nlmsg_type == NLMSG_DONE) |
1716 | goto done; | |
1717 | ||
aba5acdf SH |
1718 | if (h->nlmsg_type == NLMSG_ERROR) { |
1719 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
1720 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
1721 | fprintf(stderr, "ERROR truncated\n"); | |
1722 | } else { | |
886d19d6 PE |
1723 | if (family != PF_UNSPEC) { |
1724 | family = PF_UNSPEC; | |
1725 | goto again; | |
1726 | } | |
1727 | ||
aba5acdf | 1728 | errno = -err->error; |
930a75f9 ED |
1729 | if (errno == EOPNOTSUPP) { |
1730 | close(fd); | |
1731 | return -1; | |
1732 | } | |
aba5acdf SH |
1733 | perror("TCPDIAG answers"); |
1734 | } | |
886d19d6 PE |
1735 | |
1736 | goto done; | |
aba5acdf SH |
1737 | } |
1738 | if (!dump_fp) { | |
a37b01c1 LY |
1739 | if (!(f->families & (1<<r->idiag_family))) { |
1740 | h = NLMSG_NEXT(h, status); | |
1741 | continue; | |
1742 | } | |
3fe5b534 | 1743 | err = inet_show_sock(h, NULL); |
c51577cd MT |
1744 | if (err < 0) { |
1745 | close(fd); | |
aba5acdf | 1746 | return err; |
c51577cd | 1747 | } |
aba5acdf SH |
1748 | } |
1749 | ||
1750 | skip_it: | |
1751 | h = NLMSG_NEXT(h, status); | |
1752 | } | |
1753 | if (msg.msg_flags & MSG_TRUNC) { | |
1754 | fprintf(stderr, "Message truncated\n"); | |
1755 | continue; | |
1756 | } | |
1757 | if (status) { | |
1758 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
1759 | exit(1); | |
1760 | } | |
1761 | } | |
886d19d6 PE |
1762 | done: |
1763 | if (family == PF_INET) { | |
1764 | family = PF_INET6; | |
1765 | goto again; | |
1766 | } | |
1767 | ||
c51577cd | 1768 | close(fd); |
aba5acdf SH |
1769 | return 0; |
1770 | } | |
1771 | ||
ab01dbbb | 1772 | static int tcp_show_netlink_file(struct filter *f) |
aba5acdf SH |
1773 | { |
1774 | FILE *fp; | |
1775 | char buf[8192]; | |
1776 | ||
1777 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { | |
1778 | perror("fopen($TCPDIAG_FILE)"); | |
1779 | return -1; | |
1780 | } | |
1781 | ||
1782 | while (1) { | |
1783 | int status, err; | |
1784 | struct nlmsghdr *h = (struct nlmsghdr*)buf; | |
1785 | ||
1786 | status = fread(buf, 1, sizeof(*h), fp); | |
1787 | if (status < 0) { | |
1788 | perror("Reading header from $TCPDIAG_FILE"); | |
1789 | return -1; | |
1790 | } | |
1791 | if (status != sizeof(*h)) { | |
1792 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
1793 | return -1; | |
1794 | } | |
1795 | ||
1796 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); | |
1797 | ||
1798 | if (status < 0) { | |
1799 | perror("Reading $TCPDIAG_FILE"); | |
1800 | return -1; | |
1801 | } | |
1802 | if (status + sizeof(*h) < h->nlmsg_len) { | |
1803 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
1804 | return -1; | |
1805 | } | |
1806 | ||
1807 | /* The only legal exit point */ | |
1808 | if (h->nlmsg_type == NLMSG_DONE) | |
1809 | return 0; | |
1810 | ||
1811 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1812 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
1813 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
1814 | fprintf(stderr, "ERROR truncated\n"); | |
1815 | } else { | |
1816 | errno = -err->error; | |
1817 | perror("TCPDIAG answered"); | |
1818 | } | |
1819 | return -1; | |
1820 | } | |
1821 | ||
3fe5b534 | 1822 | err = inet_show_sock(h, f); |
aba5acdf SH |
1823 | if (err < 0) |
1824 | return err; | |
1825 | } | |
1826 | } | |
1827 | ||
ab01dbbb | 1828 | static int tcp_show(struct filter *f, int socktype) |
aba5acdf | 1829 | { |
ab01dbbb | 1830 | FILE *fp = NULL; |
aba5acdf SH |
1831 | char *buf = NULL; |
1832 | int bufsize = 64*1024; | |
1833 | ||
1834 | dg_proto = TCP_PROTO; | |
1835 | ||
1836 | if (getenv("TCPDIAG_FILE")) | |
1837 | return tcp_show_netlink_file(f); | |
1838 | ||
1839 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") | |
3fe5b534 | 1840 | && inet_show_netlink(f, NULL, socktype) == 0) |
aba5acdf SH |
1841 | return 0; |
1842 | ||
1843 | /* Sigh... We have to parse /proc/net/tcp... */ | |
1844 | ||
ab01dbbb | 1845 | |
aba5acdf SH |
1846 | /* Estimate amount of sockets and try to allocate |
1847 | * huge buffer to read all the table at one read. | |
1848 | * Limit it by 16MB though. The assumption is: as soon as | |
1849 | * kernel was able to hold information about N connections, | |
1850 | * it is able to give us some memory for snapshot. | |
1851 | */ | |
1852 | if (1) { | |
1853 | int guess = slabstat.socks+slabstat.tcp_syns; | |
1854 | if (f->states&(1<<SS_TIME_WAIT)) | |
1855 | guess += slabstat.tcp_tws; | |
1856 | if (guess > (16*1024*1024)/128) | |
1857 | guess = (16*1024*1024)/128; | |
1858 | guess *= 128; | |
1859 | if (guess > bufsize) | |
1860 | bufsize = guess; | |
1861 | } | |
1862 | while (bufsize >= 64*1024) { | |
1863 | if ((buf = malloc(bufsize)) != NULL) | |
1864 | break; | |
1865 | bufsize /= 2; | |
1866 | } | |
1867 | if (buf == NULL) { | |
1868 | errno = ENOMEM; | |
1869 | return -1; | |
1870 | } | |
1871 | ||
1872 | if (f->families & (1<<AF_INET)) { | |
69cae645 | 1873 | if ((fp = net_tcp_open()) == NULL) |
aba5acdf | 1874 | goto outerr; |
ab01dbbb SH |
1875 | |
1876 | setbuffer(fp, buf, bufsize); | |
1877 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) | |
aba5acdf | 1878 | goto outerr; |
ab01dbbb | 1879 | fclose(fp); |
aba5acdf SH |
1880 | } |
1881 | ||
1882 | if ((f->families & (1<<AF_INET6)) && | |
69cae645 | 1883 | (fp = net_tcp6_open()) != NULL) { |
ab01dbbb SH |
1884 | setbuffer(fp, buf, bufsize); |
1885 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) | |
aba5acdf | 1886 | goto outerr; |
ab01dbbb | 1887 | fclose(fp); |
aba5acdf SH |
1888 | } |
1889 | ||
1890 | free(buf); | |
1891 | return 0; | |
1892 | ||
1893 | outerr: | |
1894 | do { | |
1895 | int saved_errno = errno; | |
1896 | if (buf) | |
1897 | free(buf); | |
ab01dbbb SH |
1898 | if (fp) |
1899 | fclose(fp); | |
aba5acdf SH |
1900 | errno = saved_errno; |
1901 | return -1; | |
1902 | } while (0); | |
1903 | } | |
1904 | ||
1905 | ||
d1f28cf1 | 1906 | static int dgram_show_line(char *line, const struct filter *f, int family) |
aba5acdf SH |
1907 | { |
1908 | struct tcpstat s; | |
1909 | char *loc, *rem, *data; | |
1910 | char opt[256]; | |
1911 | int n; | |
1912 | char *p; | |
1913 | ||
1914 | if ((p = strchr(line, ':')) == NULL) | |
1915 | return -1; | |
1916 | loc = p+2; | |
1917 | ||
1918 | if ((p = strchr(loc, ':')) == NULL) | |
1919 | return -1; | |
1920 | p[5] = 0; | |
1921 | rem = p+6; | |
1922 | ||
1923 | if ((p = strchr(rem, ':')) == NULL) | |
1924 | return -1; | |
1925 | p[5] = 0; | |
1926 | data = p+6; | |
1927 | ||
1928 | do { | |
1929 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
1930 | ||
1931 | if (!(f->states & (1<<state))) | |
1932 | return 0; | |
1933 | } while (0); | |
1934 | ||
1935 | s.local.family = s.remote.family = family; | |
1936 | if (family == AF_INET) { | |
1937 | sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport); | |
1938 | sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport); | |
1939 | s.local.bytelen = s.remote.bytelen = 4; | |
1940 | } else { | |
1941 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
1942 | s.local.data, | |
1943 | s.local.data+1, | |
1944 | s.local.data+2, | |
1945 | s.local.data+3, | |
1946 | &s.lport); | |
1947 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
1948 | s.remote.data, | |
1949 | s.remote.data+1, | |
1950 | s.remote.data+2, | |
1951 | s.remote.data+3, | |
1952 | &s.rport); | |
1953 | s.local.bytelen = s.remote.bytelen = 16; | |
1954 | } | |
1955 | ||
1956 | if (f->f && run_ssfilter(f->f, &s) == 0) | |
1957 | return 0; | |
1958 | ||
1959 | opt[0] = 0; | |
e7113c61 | 1960 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
aba5acdf SH |
1961 | &s.state, &s.wq, &s.rq, |
1962 | &s.uid, &s.ino, | |
1963 | &s.refcnt, &s.sk, opt); | |
1964 | ||
1965 | if (n < 9) | |
1966 | opt[0] = 0; | |
1967 | ||
1968 | if (netid_width) | |
1969 | printf("%-*s ", netid_width, dg_proto); | |
1970 | if (state_width) | |
1971 | printf("%-*s ", state_width, sstate_name[s.state]); | |
1972 | ||
1973 | printf("%-6d %-6d ", s.rq, s.wq); | |
1974 | ||
1975 | formatted_print(&s.local, s.lport); | |
1976 | formatted_print(&s.remote, s.rport); | |
1977 | ||
1978 | if (show_users) { | |
1979 | char ubuf[4096]; | |
1980 | if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0) | |
1981 | printf(" users:(%s)", ubuf); | |
1982 | } | |
1983 | ||
1984 | if (show_details) { | |
1985 | if (s.uid) | |
1986 | printf(" uid=%u", (unsigned)s.uid); | |
e7113c61 | 1987 | printf(" ino=%u", s.ino); |
aba5acdf SH |
1988 | printf(" sk=%llx", s.sk); |
1989 | if (opt[0]) | |
1990 | printf(" opt:\"%s\"", opt); | |
1991 | } | |
1992 | printf("\n"); | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
1997 | ||
d1f28cf1 | 1998 | static int udp_show(struct filter *f) |
aba5acdf | 1999 | { |
ab01dbbb | 2000 | FILE *fp = NULL; |
aba5acdf | 2001 | |
346f8ca8 PE |
2002 | if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") |
2003 | && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) | |
2004 | return 0; | |
2005 | ||
aba5acdf SH |
2006 | dg_proto = UDP_PROTO; |
2007 | ||
2008 | if (f->families&(1<<AF_INET)) { | |
69cae645 | 2009 | if ((fp = net_udp_open()) == NULL) |
aba5acdf | 2010 | goto outerr; |
ab01dbbb | 2011 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 2012 | goto outerr; |
ab01dbbb | 2013 | fclose(fp); |
aba5acdf SH |
2014 | } |
2015 | ||
2016 | if ((f->families&(1<<AF_INET6)) && | |
69cae645 | 2017 | (fp = net_udp6_open()) != NULL) { |
ab01dbbb | 2018 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 2019 | goto outerr; |
ab01dbbb | 2020 | fclose(fp); |
aba5acdf SH |
2021 | } |
2022 | return 0; | |
2023 | ||
2024 | outerr: | |
2025 | do { | |
2026 | int saved_errno = errno; | |
ab01dbbb SH |
2027 | if (fp) |
2028 | fclose(fp); | |
aba5acdf SH |
2029 | errno = saved_errno; |
2030 | return -1; | |
2031 | } while (0); | |
2032 | } | |
2033 | ||
d1f28cf1 | 2034 | static int raw_show(struct filter *f) |
aba5acdf | 2035 | { |
ab01dbbb | 2036 | FILE *fp = NULL; |
aba5acdf SH |
2037 | |
2038 | dg_proto = RAW_PROTO; | |
2039 | ||
2040 | if (f->families&(1<<AF_INET)) { | |
69cae645 | 2041 | if ((fp = net_raw_open()) == NULL) |
aba5acdf | 2042 | goto outerr; |
ab01dbbb | 2043 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
aba5acdf | 2044 | goto outerr; |
ab01dbbb | 2045 | fclose(fp); |
aba5acdf SH |
2046 | } |
2047 | ||
2048 | if ((f->families&(1<<AF_INET6)) && | |
69cae645 | 2049 | (fp = net_raw6_open()) != NULL) { |
ab01dbbb | 2050 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
aba5acdf | 2051 | goto outerr; |
ab01dbbb | 2052 | fclose(fp); |
aba5acdf SH |
2053 | } |
2054 | return 0; | |
2055 | ||
2056 | outerr: | |
2057 | do { | |
2058 | int saved_errno = errno; | |
ab01dbbb SH |
2059 | if (fp) |
2060 | fclose(fp); | |
aba5acdf SH |
2061 | errno = saved_errno; |
2062 | return -1; | |
2063 | } while (0); | |
2064 | } | |
2065 | ||
2066 | ||
2067 | struct unixstat | |
2068 | { | |
2069 | struct unixstat *next; | |
2070 | int ino; | |
2071 | int peer; | |
2072 | int rq; | |
2073 | int wq; | |
2074 | int state; | |
2075 | int type; | |
2076 | char *name; | |
2077 | }; | |
2078 | ||
2079 | ||
2080 | ||
2081 | int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, | |
2082 | SS_ESTABLISHED, SS_CLOSING }; | |
2083 | ||
2084 | ||
2085 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat)) | |
2086 | ||
d1f28cf1 | 2087 | static void unix_list_free(struct unixstat *list) |
aba5acdf SH |
2088 | { |
2089 | while (list) { | |
2090 | struct unixstat *s = list; | |
2091 | list = list->next; | |
2092 | if (s->name) | |
2093 | free(s->name); | |
2094 | free(s); | |
2095 | } | |
2096 | } | |
2097 | ||
d1f28cf1 | 2098 | static void unix_list_print(struct unixstat *list, struct filter *f) |
aba5acdf SH |
2099 | { |
2100 | struct unixstat *s; | |
2101 | char *peer; | |
2102 | ||
2103 | for (s = list; s; s = s->next) { | |
2104 | if (!(f->states & (1<<s->state))) | |
2105 | continue; | |
2106 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) | |
2107 | continue; | |
2108 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) | |
2109 | continue; | |
2110 | ||
2111 | peer = "*"; | |
2112 | if (s->peer) { | |
2113 | struct unixstat *p; | |
2114 | for (p = list; p; p = p->next) { | |
2115 | if (s->peer == p->ino) | |
2116 | break; | |
2117 | } | |
2118 | if (!p) { | |
2119 | peer = "?"; | |
2120 | } else { | |
2121 | peer = p->name ? : "*"; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | if (f->f) { | |
2126 | struct tcpstat tst; | |
2127 | tst.local.family = AF_UNIX; | |
2128 | tst.remote.family = AF_UNIX; | |
2129 | memcpy(tst.local.data, &s->name, sizeof(s->name)); | |
2130 | if (strcmp(peer, "*") == 0) | |
2131 | memset(tst.remote.data, 0, sizeof(peer)); | |
2132 | else | |
ae665a52 | 2133 | memcpy(tst.remote.data, &peer, sizeof(peer)); |
aba5acdf SH |
2134 | if (run_ssfilter(f->f, &tst) == 0) |
2135 | continue; | |
2136 | } | |
2137 | ||
2138 | if (netid_width) | |
ae665a52 | 2139 | printf("%-*s ", netid_width, |
aba5acdf SH |
2140 | s->type == SOCK_STREAM ? "u_str" : "u_dgr"); |
2141 | if (state_width) | |
2142 | printf("%-*s ", state_width, sstate_name[s->state]); | |
2143 | printf("%-6d %-6d ", s->rq, s->wq); | |
2144 | printf("%*s %-*d %*s %-*d", | |
2145 | addr_width, s->name ? : "*", serv_width, s->ino, | |
2146 | addr_width, peer, serv_width, s->peer); | |
2147 | if (show_users) { | |
2148 | char ubuf[4096]; | |
2149 | if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0) | |
2150 | printf(" users:(%s)", ubuf); | |
2151 | } | |
2152 | printf("\n"); | |
2153 | } | |
2154 | } | |
2155 | ||
dfbaa90d PE |
2156 | static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f) |
2157 | { | |
2158 | struct unix_diag_msg *r = NLMSG_DATA(nlh); | |
2159 | struct rtattr *tb[UNIX_DIAG_MAX+1]; | |
2160 | char name[128]; | |
2161 | int peer_ino; | |
defd61ca | 2162 | __u32 rqlen, wqlen; |
dfbaa90d PE |
2163 | |
2164 | parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1), | |
2165 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
2166 | ||
2167 | if (netid_width) | |
2168 | printf("%-*s ", netid_width, | |
2169 | r->udiag_type == SOCK_STREAM ? "u_str" : "u_dgr"); | |
2170 | if (state_width) | |
2171 | printf("%-*s ", state_width, sstate_name[r->udiag_state]); | |
2172 | ||
defd61ca HFS |
2173 | if (tb[UNIX_DIAG_RQLEN]) { |
2174 | struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); | |
2175 | rqlen = rql->udiag_rqueue; | |
2176 | wqlen = rql->udiag_wqueue; | |
2177 | } else { | |
dfbaa90d | 2178 | rqlen = 0; |
defd61ca HFS |
2179 | wqlen = 0; |
2180 | } | |
dfbaa90d | 2181 | |
defd61ca | 2182 | printf("%-6u %-6u ", rqlen, wqlen); |
dfbaa90d PE |
2183 | |
2184 | if (tb[UNIX_DIAG_NAME]) { | |
2185 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); | |
2186 | ||
2187 | memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); | |
2188 | name[len] = '\0'; | |
2189 | if (name[0] == '\0') | |
2190 | name[0] = '@'; | |
2191 | } else | |
2192 | sprintf(name, "*"); | |
2193 | ||
2194 | if (tb[UNIX_DIAG_PEER]) | |
5048f9a0 | 2195 | peer_ino = rta_getattr_u32(tb[UNIX_DIAG_PEER]); |
dfbaa90d PE |
2196 | else |
2197 | peer_ino = 0; | |
2198 | ||
2199 | printf("%*s %-*d %*s %-*d", | |
2200 | addr_width, name, | |
2201 | serv_width, r->udiag_ino, | |
2202 | addr_width, "*", /* FIXME */ | |
2203 | serv_width, peer_ino); | |
2204 | ||
2205 | if (show_users) { | |
2206 | char ubuf[4096]; | |
2207 | if (find_users(r->udiag_ino, ubuf, sizeof(ubuf)) > 0) | |
2208 | printf(" users:(%s)", ubuf); | |
2209 | } | |
2210 | ||
51ff9f24 HFS |
2211 | if (show_mem) { |
2212 | printf("\n\t"); | |
2213 | print_skmeminfo(tb, UNIX_DIAG_MEMINFO); | |
2214 | } | |
2215 | ||
5b816047 PE |
2216 | if (show_details) { |
2217 | if (tb[UNIX_DIAG_SHUTDOWN]) { | |
2218 | unsigned char mask; | |
2219 | mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]); | |
2220 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
2221 | } | |
2222 | } | |
2223 | ||
dfbaa90d PE |
2224 | printf("\n"); |
2225 | ||
2226 | return 0; | |
2227 | } | |
2228 | ||
d8402b96 AV |
2229 | static int handle_netlink_request(struct filter *f, FILE *dump_fp, |
2230 | struct nlmsghdr *req, size_t size, | |
2231 | int (* show_one_sock)(struct nlmsghdr *nlh, struct filter *f)) | |
dfbaa90d PE |
2232 | { |
2233 | int fd; | |
dfbaa90d | 2234 | char buf[8192]; |
dfbaa90d PE |
2235 | |
2236 | if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0) | |
2237 | return -1; | |
2238 | ||
d8402b96 | 2239 | if (send(fd, req, size, 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 | 2283 | if (errno != ENOENT) |
d8402b96 | 2284 | fprintf(stderr, "DIAG answers %d\n", errno); |
dfbaa90d | 2285 | } |
a3fd8e58 ED |
2286 | close(fd); |
2287 | return -1; | |
dfbaa90d PE |
2288 | } |
2289 | if (!dump_fp) { | |
d8402b96 | 2290 | err = show_one_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 | ||
d8402b96 AV |
2312 | static int unix_show_netlink(struct filter *f, FILE *dump_fp) |
2313 | { | |
2314 | struct { | |
2315 | struct nlmsghdr nlh; | |
2316 | struct unix_diag_req r; | |
2317 | } req; | |
2318 | ||
2319 | memset(&req, 0, sizeof(req)); | |
2320 | req.nlh.nlmsg_len = sizeof(req); | |
2321 | req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; | |
2322 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; | |
2323 | req.nlh.nlmsg_seq = 123456; | |
2324 | ||
2325 | req.r.sdiag_family = AF_UNIX; | |
2326 | req.r.udiag_states = f->states; | |
2327 | req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; | |
2328 | if (show_mem) | |
2329 | req.r.udiag_show |= UDIAG_SHOW_MEMINFO; | |
2330 | ||
2331 | return handle_netlink_request(f, dump_fp, &req.nlh, | |
2332 | sizeof(req), unix_show_sock); | |
2333 | } | |
2334 | ||
d1f28cf1 | 2335 | static int unix_show(struct filter *f) |
aba5acdf SH |
2336 | { |
2337 | FILE *fp; | |
2338 | char buf[256]; | |
2339 | char name[128]; | |
2340 | int newformat = 0; | |
2341 | int cnt; | |
2342 | struct unixstat *list = NULL; | |
2343 | ||
dfbaa90d PE |
2344 | if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT") |
2345 | && unix_show_netlink(f, NULL) == 0) | |
2346 | return 0; | |
2347 | ||
ab01dbbb | 2348 | if ((fp = net_unix_open()) == NULL) |
aba5acdf SH |
2349 | return -1; |
2350 | fgets(buf, sizeof(buf)-1, fp); | |
2351 | ||
ae665a52 | 2352 | if (memcmp(buf, "Peer", 4) == 0) |
aba5acdf SH |
2353 | newformat = 1; |
2354 | cnt = 0; | |
2355 | ||
2356 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
2357 | struct unixstat *u, **insp; | |
2358 | int flags; | |
2359 | ||
2360 | if (!(u = malloc(sizeof(*u)))) | |
2361 | break; | |
2362 | u->name = NULL; | |
2363 | ||
2364 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", | |
2365 | &u->peer, &u->rq, &u->wq, &flags, &u->type, | |
2366 | &u->state, &u->ino, name) < 8) | |
2367 | name[0] = 0; | |
2368 | ||
2369 | if (flags&(1<<16)) { | |
2370 | u->state = SS_LISTEN; | |
2371 | } else { | |
2372 | u->state = unix_state_map[u->state-1]; | |
2373 | if (u->type == SOCK_DGRAM && | |
2374 | u->state == SS_CLOSE && | |
2375 | u->peer) | |
2376 | u->state = SS_ESTABLISHED; | |
2377 | } | |
2378 | ||
2379 | if (!newformat) { | |
2380 | u->peer = 0; | |
2381 | u->rq = 0; | |
2382 | u->wq = 0; | |
2383 | } | |
2384 | ||
2385 | insp = &list; | |
2386 | while (*insp) { | |
2387 | if (u->type < (*insp)->type || | |
2388 | (u->type == (*insp)->type && | |
2389 | u->ino < (*insp)->ino)) | |
2390 | break; | |
2391 | insp = &(*insp)->next; | |
2392 | } | |
2393 | u->next = *insp; | |
2394 | *insp = u; | |
2395 | ||
2396 | if (name[0]) { | |
2397 | if ((u->name = malloc(strlen(name)+1)) == NULL) | |
2398 | break; | |
2399 | strcpy(u->name, name); | |
2400 | } | |
2401 | if (++cnt > MAX_UNIX_REMEMBER) { | |
2402 | unix_list_print(list, f); | |
2403 | unix_list_free(list); | |
2404 | list = NULL; | |
2405 | cnt = 0; | |
2406 | } | |
2407 | } | |
a3fd8e58 | 2408 | fclose(fp); |
aba5acdf SH |
2409 | if (list) { |
2410 | unix_list_print(list, f); | |
2411 | unix_list_free(list); | |
2412 | list = NULL; | |
2413 | cnt = 0; | |
2414 | } | |
2415 | ||
2416 | return 0; | |
2417 | } | |
2418 | ||
372c30d2 ND |
2419 | static int packet_show_sock(struct nlmsghdr *nlh, struct filter *f) |
2420 | { | |
2421 | struct packet_diag_msg *r = NLMSG_DATA(nlh); | |
2422 | struct rtattr *tb[PACKET_DIAG_MAX+1]; | |
2423 | __u32 rq; | |
2424 | ||
2425 | parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1), | |
2426 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
2427 | ||
2428 | /* use /proc/net/packet if all info are not available */ | |
2429 | if (!tb[PACKET_DIAG_MEMINFO]) | |
2430 | return -1; | |
2431 | ||
2432 | if (netid_width) | |
2433 | printf("%-*s ", netid_width, | |
2434 | r->pdiag_type == SOCK_RAW ? "p_raw" : "p_dgr"); | |
2435 | if (state_width) | |
2436 | printf("%-*s ", state_width, "UNCONN"); | |
2437 | ||
2438 | if (tb[PACKET_DIAG_MEMINFO]) { | |
2439 | __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); | |
2440 | ||
2441 | rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
2442 | } else | |
2443 | rq = 0; | |
2444 | printf("%-6d %-6d ", rq, 0); | |
2445 | ||
2446 | if (r->pdiag_num == 3) { | |
2447 | printf("%*s:", addr_width, "*"); | |
2448 | } else { | |
2449 | char tb2[16]; | |
2450 | printf("%*s:", addr_width, | |
2451 | ll_proto_n2a(htons(r->pdiag_num), tb2, sizeof(tb2))); | |
2452 | } | |
2453 | if (tb[PACKET_DIAG_INFO]) { | |
2454 | struct packet_diag_info *pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); | |
2455 | ||
2456 | if (pinfo->pdi_index == 0) | |
2457 | printf("%-*s ", serv_width, "*"); | |
2458 | else | |
2459 | printf("%-*s ", serv_width, xll_index_to_name(pinfo->pdi_index)); | |
2460 | } else | |
2461 | printf("%-*s ", serv_width, "*"); | |
2462 | ||
2463 | printf("%*s*%-*s", | |
2464 | addr_width, "", serv_width, ""); | |
2465 | ||
2466 | if (show_users) { | |
2467 | char ubuf[4096]; | |
2468 | if (find_users(r->pdiag_ino, ubuf, sizeof(ubuf)) > 0) | |
2469 | printf(" users:(%s)", ubuf); | |
2470 | } | |
2471 | if (show_details) { | |
2472 | __u32 uid = 0; | |
2473 | ||
2474 | if (tb[PACKET_DIAG_UID]) | |
2475 | uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]); | |
2476 | ||
2477 | printf(" ino=%u uid=%u sk=", r->pdiag_ino, uid); | |
2478 | if (r->pdiag_cookie[1] != 0) | |
2479 | printf("%08x", r->pdiag_cookie[1]); | |
2480 | printf("%08x", r->pdiag_cookie[0]); | |
2481 | } | |
2482 | ||
2483 | if (show_bpf && tb[PACKET_DIAG_FILTER]) { | |
2484 | struct sock_filter *fil = | |
2485 | RTA_DATA(tb[PACKET_DIAG_FILTER]); | |
2486 | int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / | |
2487 | sizeof(struct sock_filter); | |
2488 | ||
2489 | printf("\n\tbpf filter (%d): ", num); | |
2490 | while (num) { | |
2491 | printf(" 0x%02x %u %u %u,", | |
2492 | fil->code, fil->jt, fil->jf, fil->k); | |
2493 | num--; | |
2494 | fil++; | |
2495 | } | |
2496 | } | |
2497 | printf("\n"); | |
2498 | return 0; | |
2499 | } | |
2500 | ||
2501 | static int packet_show_netlink(struct filter *f, FILE *dump_fp) | |
2502 | { | |
2503 | int fd; | |
2504 | struct { | |
2505 | struct nlmsghdr nlh; | |
2506 | struct packet_diag_req r; | |
2507 | } req; | |
2508 | char buf[8192]; | |
2509 | ||
2510 | if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0) | |
2511 | return -1; | |
2512 | ||
2513 | memset(&req, 0, sizeof(req)); | |
2514 | req.nlh.nlmsg_len = sizeof(req); | |
2515 | req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; | |
2516 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; | |
2517 | req.nlh.nlmsg_seq = 123456; | |
2518 | ||
2519 | req.r.sdiag_family = AF_PACKET; | |
2520 | req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | PACKET_SHOW_FILTER; | |
2521 | ||
2522 | if (send(fd, &req, sizeof(req), 0) < 0) { | |
2523 | close(fd); | |
2524 | return -1; | |
2525 | } | |
2526 | ||
2527 | while (1) { | |
2528 | ssize_t status; | |
2529 | struct nlmsghdr *h; | |
2530 | struct sockaddr_nl nladdr; | |
2531 | socklen_t slen = sizeof(nladdr); | |
2532 | ||
2533 | status = recvfrom(fd, buf, sizeof(buf), 0, | |
2534 | (struct sockaddr *) &nladdr, &slen); | |
2535 | if (status < 0) { | |
2536 | if (errno == EINTR) | |
2537 | continue; | |
2538 | perror("OVERRUN"); | |
2539 | continue; | |
2540 | } | |
2541 | if (status == 0) { | |
2542 | fprintf(stderr, "EOF on netlink\n"); | |
2543 | goto close_it; | |
2544 | } | |
2545 | ||
2546 | if (dump_fp) | |
2547 | fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp); | |
2548 | ||
2549 | h = (struct nlmsghdr*)buf; | |
2550 | while (NLMSG_OK(h, status)) { | |
2551 | int err; | |
2552 | ||
2553 | if (h->nlmsg_seq != 123456) | |
2554 | goto skip_it; | |
2555 | ||
2556 | if (h->nlmsg_type == NLMSG_DONE) | |
2557 | goto close_it; | |
2558 | ||
2559 | if (h->nlmsg_type == NLMSG_ERROR) { | |
2560 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
2561 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
2562 | fprintf(stderr, "ERROR truncated\n"); | |
2563 | } else { | |
2564 | errno = -err->error; | |
2565 | if (errno != ENOENT) | |
2566 | fprintf(stderr, "UDIAG answers %d\n", errno); | |
2567 | } | |
2568 | close(fd); | |
2569 | return -1; | |
2570 | } | |
2571 | if (!dump_fp) { | |
2572 | err = packet_show_sock(h, f); | |
2573 | if (err < 0) { | |
2574 | close(fd); | |
2575 | return err; | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | skip_it: | |
2580 | h = NLMSG_NEXT(h, status); | |
2581 | } | |
2582 | ||
2583 | if (status) { | |
2584 | fprintf(stderr, "!!!Remnant of size %zd\n", status); | |
2585 | exit(1); | |
2586 | } | |
2587 | } | |
2588 | ||
2589 | close_it: | |
2590 | close(fd); | |
2591 | return 0; | |
2592 | } | |
2593 | ||
aba5acdf | 2594 | |
d1f28cf1 | 2595 | static int packet_show(struct filter *f) |
aba5acdf SH |
2596 | { |
2597 | FILE *fp; | |
2598 | char buf[256]; | |
2599 | int type; | |
2600 | int prot; | |
2601 | int iface; | |
2602 | int state; | |
2603 | int rq; | |
2604 | int uid; | |
2605 | int ino; | |
2606 | unsigned long long sk; | |
2607 | ||
2608 | if (!(f->states & (1<<SS_CLOSE))) | |
2609 | return 0; | |
2610 | ||
372c30d2 ND |
2611 | if (packet_show_netlink(f, NULL) == 0) |
2612 | return 0; | |
2613 | ||
ab01dbbb | 2614 | if ((fp = net_packet_open()) == NULL) |
aba5acdf SH |
2615 | return -1; |
2616 | fgets(buf, sizeof(buf)-1, fp); | |
2617 | ||
2618 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
2619 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", | |
2620 | &sk, | |
2621 | &type, &prot, &iface, &state, | |
2622 | &rq, &uid, &ino); | |
2623 | ||
2624 | if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) | |
2625 | continue; | |
2626 | if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) | |
2627 | continue; | |
2628 | if (f->f) { | |
2629 | struct tcpstat tst; | |
2630 | tst.local.family = AF_PACKET; | |
2631 | tst.remote.family = AF_PACKET; | |
2632 | tst.rport = 0; | |
2633 | tst.lport = iface; | |
2634 | tst.local.data[0] = prot; | |
2635 | tst.remote.data[0] = 0; | |
2636 | if (run_ssfilter(f->f, &tst) == 0) | |
2637 | continue; | |
2638 | } | |
2639 | ||
2640 | if (netid_width) | |
ae665a52 | 2641 | printf("%-*s ", netid_width, |
aba5acdf SH |
2642 | type == SOCK_RAW ? "p_raw" : "p_dgr"); |
2643 | if (state_width) | |
2644 | printf("%-*s ", state_width, "UNCONN"); | |
2645 | printf("%-6d %-6d ", rq, 0); | |
2646 | if (prot == 3) { | |
2647 | printf("%*s:", addr_width, "*"); | |
2648 | } else { | |
2649 | char tb[16]; | |
ae665a52 | 2650 | printf("%*s:", addr_width, |
aba5acdf SH |
2651 | ll_proto_n2a(htons(prot), tb, sizeof(tb))); |
2652 | } | |
2653 | if (iface == 0) { | |
2654 | printf("%-*s ", serv_width, "*"); | |
2655 | } else { | |
2656 | printf("%-*s ", serv_width, xll_index_to_name(iface)); | |
2657 | } | |
2658 | printf("%*s*%-*s", | |
2659 | addr_width, "", serv_width, ""); | |
2660 | ||
2661 | if (show_users) { | |
2662 | char ubuf[4096]; | |
2663 | if (find_users(ino, ubuf, sizeof(ubuf)) > 0) | |
2664 | printf(" users:(%s)", ubuf); | |
2665 | } | |
2666 | if (show_details) { | |
2667 | printf(" ino=%u uid=%u sk=%llx", ino, uid, sk); | |
2668 | } | |
2669 | printf("\n"); | |
2670 | } | |
2671 | ||
2672 | return 0; | |
2673 | } | |
2674 | ||
d1f28cf1 | 2675 | static int netlink_show(struct filter *f) |
aba5acdf SH |
2676 | { |
2677 | FILE *fp; | |
2678 | char buf[256]; | |
2679 | int prot, pid; | |
2680 | unsigned groups; | |
2681 | int rq, wq, rc; | |
2682 | unsigned long long sk, cb; | |
2683 | ||
2684 | if (!(f->states & (1<<SS_CLOSE))) | |
2685 | return 0; | |
2686 | ||
ab01dbbb | 2687 | if ((fp = net_netlink_open()) == NULL) |
aba5acdf SH |
2688 | return -1; |
2689 | fgets(buf, sizeof(buf)-1, fp); | |
2690 | ||
2691 | while (fgets(buf, sizeof(buf)-1, fp)) { | |
2692 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", | |
2693 | &sk, | |
2694 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); | |
2695 | ||
2696 | if (f->f) { | |
2697 | struct tcpstat tst; | |
2698 | tst.local.family = AF_NETLINK; | |
2699 | tst.remote.family = AF_NETLINK; | |
2700 | tst.rport = -1; | |
2701 | tst.lport = pid; | |
2702 | tst.local.data[0] = prot; | |
2703 | tst.remote.data[0] = 0; | |
2704 | if (run_ssfilter(f->f, &tst) == 0) | |
2705 | continue; | |
2706 | } | |
2707 | ||
2708 | if (netid_width) | |
ae665a52 | 2709 | printf("%-*s ", netid_width, "nl"); |
aba5acdf SH |
2710 | if (state_width) |
2711 | printf("%-*s ", state_width, "UNCONN"); | |
2712 | printf("%-6d %-6d ", rq, wq); | |
2713 | if (resolve_services && prot == 0) | |
2714 | printf("%*s:", addr_width, "rtnl"); | |
2715 | else if (resolve_services && prot == 3) | |
2716 | printf("%*s:", addr_width, "fw"); | |
2717 | else if (resolve_services && prot == 4) | |
2718 | printf("%*s:", addr_width, "tcpdiag"); | |
2719 | else | |
2720 | printf("%*d:", addr_width, prot); | |
2721 | if (pid == -1) { | |
2722 | printf("%-*s ", serv_width, "*"); | |
2723 | } else if (resolve_services) { | |
2724 | int done = 0; | |
2725 | if (!pid) { | |
2726 | done = 1; | |
2727 | printf("%-*s ", serv_width, "kernel"); | |
2728 | } else if (pid > 0) { | |
2729 | char procname[64]; | |
2730 | FILE *fp; | |
2731 | sprintf(procname, "%s/%d/stat", | |
2732 | getenv("PROC_ROOT") ? : "/proc", pid); | |
2733 | if ((fp = fopen(procname, "r")) != NULL) { | |
2734 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { | |
ae665a52 | 2735 | sprintf(procname+strlen(procname), "/%d", pid); |
aba5acdf SH |
2736 | printf("%-*s ", serv_width, procname); |
2737 | done = 1; | |
2738 | } | |
2739 | fclose(fp); | |
2740 | } | |
2741 | } | |
2742 | if (!done) | |
2743 | printf("%-*d ", serv_width, pid); | |
2744 | } else { | |
2745 | printf("%-*d ", serv_width, pid); | |
2746 | } | |
2747 | printf("%*s*%-*s", | |
2748 | addr_width, "", serv_width, ""); | |
2749 | ||
2750 | if (show_details) { | |
2751 | printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); | |
2752 | } | |
2753 | printf("\n"); | |
2754 | } | |
2755 | ||
2756 | return 0; | |
2757 | } | |
2758 | ||
2759 | struct snmpstat | |
2760 | { | |
2761 | int tcp_estab; | |
2762 | }; | |
2763 | ||
d1f28cf1 | 2764 | static int get_snmp_int(char *proto, char *key, int *result) |
aba5acdf SH |
2765 | { |
2766 | char buf[1024]; | |
2767 | FILE *fp; | |
2768 | int protolen = strlen(proto); | |
2769 | int keylen = strlen(key); | |
2770 | ||
2771 | *result = 0; | |
2772 | ||
ab01dbbb | 2773 | if ((fp = net_snmp_open()) == NULL) |
aba5acdf SH |
2774 | return -1; |
2775 | ||
2776 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
2777 | char *p = buf; | |
2778 | int pos = 0; | |
2779 | if (memcmp(buf, proto, protolen)) | |
2780 | continue; | |
2781 | while ((p = strchr(p, ' ')) != NULL) { | |
2782 | pos++; | |
2783 | p++; | |
2784 | if (memcmp(p, key, keylen) == 0 && | |
2785 | (p[keylen] == ' ' || p[keylen] == '\n')) | |
2786 | break; | |
2787 | } | |
2788 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
2789 | break; | |
2790 | if (memcmp(buf, proto, protolen)) | |
2791 | break; | |
2792 | p = buf; | |
2793 | while ((p = strchr(p, ' ')) != NULL) { | |
2794 | p++; | |
2795 | if (--pos == 0) { | |
2796 | sscanf(p, "%d", result); | |
2797 | fclose(fp); | |
2798 | return 0; | |
2799 | } | |
2800 | } | |
2801 | } | |
2802 | ||
2803 | fclose(fp); | |
2804 | errno = ESRCH; | |
2805 | return -1; | |
2806 | } | |
2807 | ||
2808 | ||
2809 | /* Get stats from sockstat */ | |
2810 | ||
2811 | struct sockstat | |
2812 | { | |
2813 | int socks; | |
2814 | int tcp_mem; | |
2815 | int tcp_total; | |
2816 | int tcp_orphans; | |
2817 | int tcp_tws; | |
2818 | int tcp4_hashed; | |
2819 | int udp4; | |
2820 | int raw4; | |
2821 | int frag4; | |
2822 | int frag4_mem; | |
2823 | int tcp6_hashed; | |
2824 | int udp6; | |
2825 | int raw6; | |
2826 | int frag6; | |
2827 | int frag6_mem; | |
2828 | }; | |
2829 | ||
2830 | static void get_sockstat_line(char *line, struct sockstat *s) | |
2831 | { | |
2832 | char id[256], rem[256]; | |
2833 | ||
2834 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) | |
2835 | return; | |
2836 | ||
2837 | if (strcmp(id, "sockets:") == 0) | |
2838 | sscanf(rem, "%*s%d", &s->socks); | |
2839 | else if (strcmp(id, "UDP:") == 0) | |
2840 | sscanf(rem, "%*s%d", &s->udp4); | |
2841 | else if (strcmp(id, "UDP6:") == 0) | |
2842 | sscanf(rem, "%*s%d", &s->udp6); | |
2843 | else if (strcmp(id, "RAW:") == 0) | |
2844 | sscanf(rem, "%*s%d", &s->raw4); | |
2845 | else if (strcmp(id, "RAW6:") == 0) | |
2846 | sscanf(rem, "%*s%d", &s->raw6); | |
2847 | else if (strcmp(id, "TCP6:") == 0) | |
2848 | sscanf(rem, "%*s%d", &s->tcp6_hashed); | |
2849 | else if (strcmp(id, "FRAG:") == 0) | |
2850 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); | |
2851 | else if (strcmp(id, "FRAG6:") == 0) | |
2852 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); | |
2853 | else if (strcmp(id, "TCP:") == 0) | |
2854 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", | |
2855 | &s->tcp4_hashed, | |
2856 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); | |
2857 | } | |
2858 | ||
d1f28cf1 | 2859 | static int get_sockstat(struct sockstat *s) |
aba5acdf SH |
2860 | { |
2861 | char buf[256]; | |
2862 | FILE *fp; | |
2863 | ||
2864 | memset(s, 0, sizeof(*s)); | |
2865 | ||
ab01dbbb | 2866 | if ((fp = net_sockstat_open()) == NULL) |
aba5acdf SH |
2867 | return -1; |
2868 | while(fgets(buf, sizeof(buf), fp) != NULL) | |
2869 | get_sockstat_line(buf, s); | |
2870 | fclose(fp); | |
2871 | ||
ab01dbbb | 2872 | if ((fp = net_sockstat6_open()) == NULL) |
aba5acdf SH |
2873 | return 0; |
2874 | while(fgets(buf, sizeof(buf), fp) != NULL) | |
2875 | get_sockstat_line(buf, s); | |
2876 | fclose(fp); | |
2877 | ||
2878 | return 0; | |
2879 | } | |
2880 | ||
d1f28cf1 | 2881 | static int print_summary(void) |
aba5acdf SH |
2882 | { |
2883 | struct sockstat s; | |
2884 | struct snmpstat sn; | |
2885 | ||
2886 | if (get_sockstat(&s) < 0) | |
2887 | perror("ss: get_sockstat"); | |
2888 | if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0) | |
2889 | perror("ss: get_snmpstat"); | |
2890 | ||
2891 | printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks); | |
2892 | ||
2893 | printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n", | |
2894 | s.tcp_total + slabstat.tcp_syns + s.tcp_tws, | |
2895 | sn.tcp_estab, | |
2896 | s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws), | |
2897 | s.tcp_orphans, | |
2898 | slabstat.tcp_syns, | |
2899 | s.tcp_tws, slabstat.tcp_tws, | |
2900 | slabstat.tcp_ports | |
2901 | ); | |
2902 | ||
2903 | printf("\n"); | |
2904 | printf("Transport Total IP IPv6\n"); | |
2905 | printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-"); | |
2906 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); | |
2907 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); | |
2908 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); | |
ae665a52 | 2909 | printf("INET %-9d %-9d %-9d\n", |
aba5acdf SH |
2910 | s.raw4+s.udp4+s.tcp4_hashed+ |
2911 | s.raw6+s.udp6+s.tcp6_hashed, | |
2912 | s.raw4+s.udp4+s.tcp4_hashed, | |
2913 | s.raw6+s.udp6+s.tcp6_hashed); | |
2914 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); | |
2915 | ||
2916 | printf("\n"); | |
2917 | ||
2918 | return 0; | |
2919 | } | |
2920 | ||
7a96e199 | 2921 | static void _usage(FILE *dest) |
aba5acdf | 2922 | { |
7a96e199 | 2923 | fprintf(dest, |
aba5acdf SH |
2924 | "Usage: ss [ OPTIONS ]\n" |
2925 | " ss [ OPTIONS ] [ FILTER ]\n" | |
ab61159a SH |
2926 | " -h, --help this message\n" |
2927 | " -V, --version output version information\n" | |
2928 | " -n, --numeric don't resolve service names\n" | |
2929 | " -r, --resolve resolve host names\n" | |
2930 | " -a, --all display all sockets\n" | |
2931 | " -l, --listening display listening sockets\n" | |
2932 | " -o, --options show timer information\n" | |
2933 | " -e, --extended show detailed socket information\n" | |
2934 | " -m, --memory show socket memory usage\n" | |
2935 | " -p, --processes show process using socket\n" | |
2936 | " -i, --info show internal TCP information\n" | |
2937 | " -s, --summary show socket usage summary\n" | |
b0f01cf6 | 2938 | " -b, --bpf show bpf filter socket information\n" |
ab61159a SH |
2939 | "\n" |
2940 | " -4, --ipv4 display only IP version 4 sockets\n" | |
2941 | " -6, --ipv6 display only IP version 6 sockets\n" | |
2942 | " -0, --packet display PACKET sockets\n" | |
2943 | " -t, --tcp display only TCP sockets\n" | |
2944 | " -u, --udp display only UDP sockets\n" | |
351efcde | 2945 | " -d, --dccp display only DCCP sockets\n" |
ab61159a SH |
2946 | " -w, --raw display only RAW sockets\n" |
2947 | " -x, --unix display only Unix domain sockets\n" | |
2948 | " -f, --family=FAMILY display sockets of type FAMILY\n" | |
2949 | "\n" | |
583de149 | 2950 | " -A, --query=QUERY, --socket=QUERY\n" |
aba5acdf | 2951 | " QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n" |
ab61159a | 2952 | "\n" |
583de149 | 2953 | " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" |
ab61159a | 2954 | " -F, --filter=FILE read filter information from FILE\n" |
aba5acdf | 2955 | " FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n" |
ab61159a | 2956 | ); |
7a96e199 AH |
2957 | } |
2958 | ||
2959 | static void help(void) __attribute__((noreturn)); | |
2960 | static void help(void) | |
2961 | { | |
2962 | _usage(stdout); | |
2963 | exit(0); | |
2964 | } | |
2965 | ||
2966 | static void usage(void) __attribute__((noreturn)); | |
2967 | static void usage(void) | |
2968 | { | |
2969 | _usage(stderr); | |
aba5acdf SH |
2970 | exit(-1); |
2971 | } | |
2972 | ||
2973 | ||
d1f28cf1 | 2974 | static int scan_state(const char *state) |
aba5acdf SH |
2975 | { |
2976 | int i; | |
2977 | if (strcasecmp(state, "close") == 0 || | |
2978 | strcasecmp(state, "closed") == 0) | |
2979 | return (1<<SS_CLOSE); | |
2980 | if (strcasecmp(state, "syn-rcv") == 0) | |
2981 | return (1<<SS_SYN_RECV); | |
1a5bad5a | 2982 | if (strcasecmp(state, "established") == 0) |
aba5acdf SH |
2983 | return (1<<SS_ESTABLISHED); |
2984 | if (strcasecmp(state, "all") == 0) | |
2985 | return SS_ALL; | |
2986 | if (strcasecmp(state, "connected") == 0) | |
2987 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); | |
1a5bad5a | 2988 | if (strcasecmp(state, "synchronized") == 0) |
aba5acdf SH |
2989 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
2990 | if (strcasecmp(state, "bucket") == 0) | |
2991 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); | |
2992 | if (strcasecmp(state, "big") == 0) | |
2993 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); | |
2994 | for (i=0; i<SS_MAX; i++) { | |
1a5bad5a | 2995 | if (strcasecmp(state, sstate_namel[i]) == 0) |
aba5acdf SH |
2996 | return (1<<i); |
2997 | } | |
2998 | return 0; | |
2999 | } | |
3000 | ||
ab61159a SH |
3001 | static const struct option long_opts[] = { |
3002 | { "numeric", 0, 0, 'n' }, | |
3003 | { "resolve", 0, 0, 'r' }, | |
3004 | { "options", 0, 0, 'o' }, | |
3005 | { "extended", 0, 0, 'e' }, | |
3006 | { "memory", 0, 0, 'm' }, | |
3007 | { "info", 0, 0, 'i' }, | |
3008 | { "processes", 0, 0, 'p' }, | |
372c30d2 | 3009 | { "bpf", 0, 0, 'b' }, |
351efcde | 3010 | { "dccp", 0, 0, 'd' }, |
ab61159a SH |
3011 | { "tcp", 0, 0, 't' }, |
3012 | { "udp", 0, 0, 'u' }, | |
3013 | { "raw", 0, 0, 'w' }, | |
3014 | { "unix", 0, 0, 'x' }, | |
3015 | { "all", 0, 0, 'a' }, | |
3016 | { "listening", 0, 0, 'l' }, | |
3017 | { "ipv4", 0, 0, '4' }, | |
3018 | { "ipv6", 0, 0, '6' }, | |
3019 | { "packet", 0, 0, '0' }, | |
3020 | { "family", 1, 0, 'f' }, | |
3021 | { "socket", 1, 0, 'A' }, | |
583de149 | 3022 | { "query", 1, 0, 'A' }, |
c3f346b0 | 3023 | { "summary", 0, 0, 's' }, |
583de149 | 3024 | { "diag", 1, 0, 'D' }, |
ab61159a SH |
3025 | { "filter", 1, 0, 'F' }, |
3026 | { "version", 0, 0, 'V' }, | |
3027 | { "help", 0, 0, 'h' }, | |
3028 | { 0 } | |
ae665a52 | 3029 | |
ab61159a SH |
3030 | }; |
3031 | ||
aba5acdf SH |
3032 | int main(int argc, char *argv[]) |
3033 | { | |
3034 | int do_default = 1; | |
3035 | int saw_states = 0; | |
3036 | int saw_query = 0; | |
3037 | int do_summary = 0; | |
7d105b56 | 3038 | const char *dump_tcpdiag = NULL; |
aba5acdf SH |
3039 | FILE *filter_fp = NULL; |
3040 | int ch; | |
3041 | ||
3042 | memset(¤t_filter, 0, sizeof(current_filter)); | |
3043 | ||
3044 | current_filter.states = default_filter.states; | |
3045 | ||
372c30d2 | 3046 | while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbf:miA:D:F:vV", |
ab61159a | 3047 | long_opts, NULL)) != EOF) { |
aba5acdf SH |
3048 | switch(ch) { |
3049 | case 'n': | |
3050 | resolve_services = 0; | |
3051 | break; | |
3052 | case 'r': | |
3053 | resolve_hosts = 1; | |
3054 | break; | |
3055 | case 'o': | |
3056 | show_options = 1; | |
3057 | break; | |
3058 | case 'e': | |
3059 | show_options = 1; | |
3060 | show_details++; | |
3061 | break; | |
3062 | case 'm': | |
3063 | show_mem = 1; | |
3064 | break; | |
3065 | case 'i': | |
3066 | show_tcpinfo = 1; | |
3067 | break; | |
3068 | case 'p': | |
3069 | show_users++; | |
fbc0f876 | 3070 | user_ent_hash_build(); |
aba5acdf | 3071 | break; |
372c30d2 ND |
3072 | case 'b': |
3073 | show_options = 1; | |
3074 | show_bpf++; | |
3075 | break; | |
351efcde SH |
3076 | case 'd': |
3077 | current_filter.dbs |= (1<<DCCP_DB); | |
3078 | do_default = 0; | |
3079 | break; | |
aba5acdf SH |
3080 | case 't': |
3081 | current_filter.dbs |= (1<<TCP_DB); | |
3082 | do_default = 0; | |
3083 | break; | |
3084 | case 'u': | |
3085 | current_filter.dbs |= (1<<UDP_DB); | |
3086 | do_default = 0; | |
3087 | break; | |
3088 | case 'w': | |
3089 | current_filter.dbs |= (1<<RAW_DB); | |
3090 | do_default = 0; | |
3091 | break; | |
3092 | case 'x': | |
3093 | current_filter.dbs |= UNIX_DBM; | |
3094 | do_default = 0; | |
3095 | break; | |
3096 | case 'a': | |
3097 | current_filter.states = SS_ALL; | |
3098 | break; | |
3099 | case 'l': | |
16963ce6 | 3100 | current_filter.states = (1<<SS_LISTEN) | (1<<SS_CLOSE); |
aba5acdf SH |
3101 | break; |
3102 | case '4': | |
3103 | preferred_family = AF_INET; | |
3104 | break; | |
3105 | case '6': | |
3106 | preferred_family = AF_INET6; | |
3107 | break; | |
3108 | case '0': | |
3109 | preferred_family = AF_PACKET; | |
3110 | break; | |
3111 | case 'f': | |
3112 | if (strcmp(optarg, "inet") == 0) | |
3113 | preferred_family = AF_INET; | |
3114 | else if (strcmp(optarg, "inet6") == 0) | |
3115 | preferred_family = AF_INET6; | |
3116 | else if (strcmp(optarg, "link") == 0) | |
3117 | preferred_family = AF_PACKET; | |
3118 | else if (strcmp(optarg, "unix") == 0) | |
3119 | preferred_family = AF_UNIX; | |
3120 | else if (strcmp(optarg, "netlink") == 0) | |
3121 | preferred_family = AF_NETLINK; | |
3122 | else if (strcmp(optarg, "help") == 0) | |
7a96e199 | 3123 | help(); |
aba5acdf SH |
3124 | else { |
3125 | fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg); | |
3126 | usage(); | |
3127 | } | |
3128 | break; | |
3129 | case 'A': | |
3130 | { | |
3131 | char *p, *p1; | |
3132 | if (!saw_query) { | |
3133 | current_filter.dbs = 0; | |
3134 | saw_query = 1; | |
3135 | do_default = 0; | |
3136 | } | |
3137 | p = p1 = optarg; | |
3138 | do { | |
3139 | if ((p1 = strchr(p, ',')) != NULL) | |
ae665a52 | 3140 | *p1 = 0; |
aba5acdf SH |
3141 | if (strcmp(p, "all") == 0) { |
3142 | current_filter.dbs = ALL_DB; | |
3143 | } else if (strcmp(p, "inet") == 0) { | |
351efcde | 3144 | current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB); |
aba5acdf SH |
3145 | } else if (strcmp(p, "udp") == 0) { |
3146 | current_filter.dbs |= (1<<UDP_DB); | |
351efcde SH |
3147 | } else if (strcmp(p, "dccp") == 0) { |
3148 | current_filter.dbs |= (1<<DCCP_DB); | |
aba5acdf SH |
3149 | } else if (strcmp(p, "tcp") == 0) { |
3150 | current_filter.dbs |= (1<<TCP_DB); | |
3151 | } else if (strcmp(p, "raw") == 0) { | |
3152 | current_filter.dbs |= (1<<RAW_DB); | |
3153 | } else if (strcmp(p, "unix") == 0) { | |
3154 | current_filter.dbs |= UNIX_DBM; | |
1a5bad5a | 3155 | } else if (strcasecmp(p, "unix_stream") == 0 || |
aba5acdf SH |
3156 | strcmp(p, "u_str") == 0) { |
3157 | current_filter.dbs |= (1<<UNIX_ST_DB); | |
1a5bad5a | 3158 | } else if (strcasecmp(p, "unix_dgram") == 0 || |
aba5acdf SH |
3159 | strcmp(p, "u_dgr") == 0) { |
3160 | current_filter.dbs |= (1<<UNIX_DG_DB); | |
3161 | } else if (strcmp(p, "packet") == 0) { | |
3162 | current_filter.dbs |= PACKET_DBM; | |
3163 | } else if (strcmp(p, "packet_raw") == 0 || | |
3164 | strcmp(p, "p_raw") == 0) { | |
3165 | current_filter.dbs |= (1<<PACKET_R_DB); | |
3166 | } else if (strcmp(p, "packet_dgram") == 0 || | |
3167 | strcmp(p, "p_dgr") == 0) { | |
3168 | current_filter.dbs |= (1<<PACKET_DG_DB); | |
3169 | } else if (strcmp(p, "netlink") == 0) { | |
3170 | current_filter.dbs |= (1<<NETLINK_DB); | |
3171 | } else { | |
3172 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); | |
3173 | usage(); | |
3174 | } | |
3175 | p = p1 + 1; | |
3176 | } while (p1); | |
3177 | break; | |
3178 | } | |
3179 | case 's': | |
3180 | do_summary = 1; | |
3181 | break; | |
3182 | case 'D': | |
3183 | dump_tcpdiag = optarg; | |
3184 | break; | |
3185 | case 'F': | |
3186 | if (filter_fp) { | |
3187 | fprintf(stderr, "More than one filter file\n"); | |
3188 | exit(-1); | |
3189 | } | |
3190 | if (optarg[0] == '-') | |
3191 | filter_fp = stdin; | |
3192 | else | |
3193 | filter_fp = fopen(optarg, "r"); | |
3194 | if (!filter_fp) { | |
3195 | perror("fopen filter file"); | |
3196 | exit(-1); | |
3197 | } | |
3198 | break; | |
3199 | case 'v': | |
3200 | case 'V': | |
3201 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); | |
3202 | exit(0); | |
3203 | case 'h': | |
3204 | case '?': | |
7a96e199 | 3205 | help(); |
aba5acdf SH |
3206 | default: |
3207 | usage(); | |
3208 | } | |
3209 | } | |
3210 | ||
3211 | argc -= optind; | |
3212 | argv += optind; | |
3213 | ||
3214 | get_slabstat(&slabstat); | |
3215 | ||
3216 | if (do_summary) { | |
3217 | print_summary(); | |
3218 | if (do_default && argc == 0) | |
3219 | exit(0); | |
3220 | } | |
3221 | ||
3222 | if (do_default) | |
3223 | current_filter.dbs = default_filter.dbs; | |
3224 | ||
3225 | if (preferred_family == AF_UNSPEC) { | |
3226 | if (!(current_filter.dbs&~UNIX_DBM)) | |
3227 | preferred_family = AF_UNIX; | |
3228 | else if (!(current_filter.dbs&~PACKET_DBM)) | |
3229 | preferred_family = AF_PACKET; | |
3230 | else if (!(current_filter.dbs&~(1<<NETLINK_DB))) | |
3231 | preferred_family = AF_NETLINK; | |
3232 | } | |
3233 | ||
3234 | if (preferred_family != AF_UNSPEC) { | |
3235 | int mask2; | |
3236 | if (preferred_family == AF_INET || | |
3237 | preferred_family == AF_INET6) { | |
f70d96a4 | 3238 | mask2= current_filter.dbs; |
aba5acdf SH |
3239 | } else if (preferred_family == AF_PACKET) { |
3240 | mask2 = PACKET_DBM; | |
3241 | } else if (preferred_family == AF_UNIX) { | |
3242 | mask2 = UNIX_DBM; | |
3243 | } else if (preferred_family == AF_NETLINK) { | |
3244 | mask2 = (1<<NETLINK_DB); | |
3245 | } else { | |
3246 | mask2 = 0; | |
3247 | } | |
3248 | ||
3249 | if (do_default) | |
3250 | current_filter.dbs = mask2; | |
3251 | else | |
3252 | current_filter.dbs &= mask2; | |
3253 | current_filter.families = (1<<preferred_family); | |
3254 | } else { | |
3255 | if (!do_default) | |
3256 | current_filter.families = ~0; | |
3257 | else | |
3258 | current_filter.families = default_filter.families; | |
3259 | } | |
3260 | if (current_filter.dbs == 0) { | |
3261 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); | |
3262 | exit(0); | |
3263 | } | |
3264 | if (current_filter.families == 0) { | |
3265 | fprintf(stderr, "ss: no families to show with such filter.\n"); | |
3266 | exit(0); | |
3267 | } | |
3268 | ||
3269 | if (resolve_services && resolve_hosts && | |
351efcde | 3270 | (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)))) |
aba5acdf SH |
3271 | init_service_resolver(); |
3272 | ||
3273 | /* Now parse filter... */ | |
3274 | if (argc == 0 && filter_fp) { | |
3275 | if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp)) | |
3276 | usage(); | |
3277 | } | |
3278 | ||
3279 | while (argc > 0) { | |
3280 | if (strcmp(*argv, "state") == 0) { | |
3281 | NEXT_ARG(); | |
3282 | if (!saw_states) | |
3283 | current_filter.states = 0; | |
3284 | current_filter.states |= scan_state(*argv); | |
3285 | saw_states = 1; | |
3286 | } else if (strcmp(*argv, "exclude") == 0 || | |
3287 | strcmp(*argv, "excl") == 0) { | |
3288 | NEXT_ARG(); | |
3289 | if (!saw_states) | |
3290 | current_filter.states = SS_ALL; | |
3291 | current_filter.states &= ~scan_state(*argv); | |
3292 | saw_states = 1; | |
3293 | } else { | |
3294 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) | |
3295 | usage(); | |
3296 | break; | |
3297 | } | |
3298 | argc--; argv++; | |
3299 | } | |
3300 | ||
3301 | if (current_filter.states == 0) { | |
3302 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); | |
3303 | exit(0); | |
3304 | } | |
3305 | ||
3306 | if (dump_tcpdiag) { | |
3307 | FILE *dump_fp = stdout; | |
3308 | if (!(current_filter.dbs & (1<<TCP_DB))) { | |
3309 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); | |
3310 | exit(0); | |
3311 | } | |
3312 | if (dump_tcpdiag[0] != '-') { | |
3313 | dump_fp = fopen(dump_tcpdiag, "w"); | |
3314 | if (!dump_tcpdiag) { | |
3315 | perror("fopen dump file"); | |
3316 | exit(-1); | |
3317 | } | |
3318 | } | |
3fe5b534 | 3319 | inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); |
aba5acdf SH |
3320 | fflush(dump_fp); |
3321 | exit(0); | |
3322 | } | |
3323 | ||
3324 | netid_width = 0; | |
3325 | if (current_filter.dbs&(current_filter.dbs-1)) | |
3326 | netid_width = 5; | |
3327 | ||
3328 | state_width = 0; | |
3329 | if (current_filter.states&(current_filter.states-1)) | |
3330 | state_width = 10; | |
3331 | ||
3332 | screen_width = 80; | |
3333 | if (isatty(STDOUT_FILENO)) { | |
3334 | struct winsize w; | |
3335 | ||
3336 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { | |
3337 | if (w.ws_col > 0) | |
3338 | screen_width = w.ws_col; | |
3339 | } | |
3340 | } | |
3341 | ||
3342 | addrp_width = screen_width; | |
3343 | addrp_width -= netid_width+1; | |
3344 | addrp_width -= state_width+1; | |
3345 | addrp_width -= 14; | |
3346 | ||
3347 | if (addrp_width&1) { | |
3348 | if (netid_width) | |
3349 | netid_width++; | |
3350 | else if (state_width) | |
3351 | state_width++; | |
3352 | } | |
3353 | ||
3354 | addrp_width /= 2; | |
3355 | addrp_width--; | |
3356 | ||
3357 | serv_width = resolve_services ? 7 : 5; | |
3358 | ||
3359 | if (addrp_width < 15+serv_width+1) | |
3360 | addrp_width = 15+serv_width+1; | |
3361 | ||
ae665a52 | 3362 | addr_width = addrp_width - serv_width - 1; |
aba5acdf SH |
3363 | |
3364 | if (netid_width) | |
3365 | printf("%-*s ", netid_width, "Netid"); | |
3366 | if (state_width) | |
3367 | printf("%-*s ", state_width, "State"); | |
3368 | printf("%-6s %-6s ", "Recv-Q", "Send-Q"); | |
3369 | ||
3370 | printf("%*s:%-*s %*s:%-*s\n", | |
3371 | addr_width, "Local Address", serv_width, "Port", | |
3372 | addr_width, "Peer Address", serv_width, "Port"); | |
3373 | ||
aba5acdf SH |
3374 | fflush(stdout); |
3375 | ||
3376 | if (current_filter.dbs & (1<<NETLINK_DB)) | |
3377 | netlink_show(¤t_filter); | |
3378 | if (current_filter.dbs & PACKET_DBM) | |
3379 | packet_show(¤t_filter); | |
3380 | if (current_filter.dbs & UNIX_DBM) | |
3381 | unix_show(¤t_filter); | |
3382 | if (current_filter.dbs & (1<<RAW_DB)) | |
3383 | raw_show(¤t_filter); | |
3384 | if (current_filter.dbs & (1<<UDP_DB)) | |
3385 | udp_show(¤t_filter); | |
3386 | if (current_filter.dbs & (1<<TCP_DB)) | |
3fe5b534 | 3387 | tcp_show(¤t_filter, IPPROTO_TCP); |
351efcde | 3388 | if (current_filter.dbs & (1<<DCCP_DB)) |
3fe5b534 | 3389 | tcp_show(¤t_filter, IPPROTO_DCCP); |
aba5acdf SH |
3390 | return 0; |
3391 | } |