]>
Commit | Line | Data |
---|---|---|
1 | /* | |
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 <fcntl.h> | |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/socket.h> | |
18 | #include <sys/uio.h> | |
19 | #include <netinet/in.h> | |
20 | #include <string.h> | |
21 | #include <errno.h> | |
22 | #include <netdb.h> | |
23 | #include <arpa/inet.h> | |
24 | #include <dirent.h> | |
25 | #include <fnmatch.h> | |
26 | #include <getopt.h> | |
27 | #include <stdbool.h> | |
28 | #include <limits.h> | |
29 | #include <stdarg.h> | |
30 | ||
31 | #include "utils.h" | |
32 | #include "rt_names.h" | |
33 | #include "ll_map.h" | |
34 | #include "libnetlink.h" | |
35 | #include "namespace.h" | |
36 | #include "SNAPSHOT.h" | |
37 | ||
38 | #include <linux/tcp.h> | |
39 | #include <linux/sock_diag.h> | |
40 | #include <linux/inet_diag.h> | |
41 | #include <linux/unix_diag.h> | |
42 | #include <linux/netdevice.h> /* for MAX_ADDR_LEN */ | |
43 | #include <linux/filter.h> | |
44 | #include <linux/packet_diag.h> | |
45 | #include <linux/netlink_diag.h> | |
46 | #include <linux/sctp.h> | |
47 | #include <linux/vm_sockets_diag.h> | |
48 | #include <linux/net.h> | |
49 | #include <linux/tipc.h> | |
50 | #include <linux/tipc_netlink.h> | |
51 | #include <linux/tipc_sockets_diag.h> | |
52 | ||
53 | #define MAGIC_SEQ 123456 | |
54 | #define BUF_CHUNK (1024 * 1024) | |
55 | #define LEN_ALIGN(x) (((x) + 1) & ~1) | |
56 | ||
57 | #define DIAG_REQUEST(_req, _r) \ | |
58 | struct { \ | |
59 | struct nlmsghdr nlh; \ | |
60 | _r; \ | |
61 | } _req = { \ | |
62 | .nlh = { \ | |
63 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, \ | |
64 | .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\ | |
65 | .nlmsg_seq = MAGIC_SEQ, \ | |
66 | .nlmsg_len = sizeof(_req), \ | |
67 | }, \ | |
68 | } | |
69 | ||
70 | #if HAVE_SELINUX | |
71 | #include <selinux/selinux.h> | |
72 | #else | |
73 | /* Stubs for SELinux functions */ | |
74 | static int is_selinux_enabled(void) | |
75 | { | |
76 | return -1; | |
77 | } | |
78 | ||
79 | static int getpidcon(pid_t pid, char **context) | |
80 | { | |
81 | *context = NULL; | |
82 | return -1; | |
83 | } | |
84 | ||
85 | static int getfilecon(char *path, char **context) | |
86 | { | |
87 | *context = NULL; | |
88 | return -1; | |
89 | } | |
90 | ||
91 | static int security_get_initial_context(char *name, char **context) | |
92 | { | |
93 | *context = NULL; | |
94 | return -1; | |
95 | } | |
96 | #endif | |
97 | ||
98 | int resolve_services = 1; | |
99 | int preferred_family = AF_UNSPEC; | |
100 | int show_options; | |
101 | int show_details; | |
102 | int show_users; | |
103 | int show_mem; | |
104 | int show_tcpinfo; | |
105 | int show_bpf; | |
106 | int show_proc_ctx; | |
107 | int show_sock_ctx; | |
108 | int show_header = 1; | |
109 | int follow_events; | |
110 | int sctp_ino; | |
111 | int show_tipcinfo; | |
112 | ||
113 | enum col_id { | |
114 | COL_NETID, | |
115 | COL_STATE, | |
116 | COL_RECVQ, | |
117 | COL_SENDQ, | |
118 | COL_ADDR, | |
119 | COL_SERV, | |
120 | COL_RADDR, | |
121 | COL_RSERV, | |
122 | COL_EXT, | |
123 | COL_MAX | |
124 | }; | |
125 | ||
126 | enum col_align { | |
127 | ALIGN_LEFT, | |
128 | ALIGN_CENTER, | |
129 | ALIGN_RIGHT | |
130 | }; | |
131 | ||
132 | struct column { | |
133 | const enum col_align align; | |
134 | const char *header; | |
135 | const char *ldelim; | |
136 | int disabled; | |
137 | int width; /* Calculated, including additional layout spacing */ | |
138 | int max_len; /* Measured maximum field length in this column */ | |
139 | }; | |
140 | ||
141 | static struct column columns[] = { | |
142 | { ALIGN_LEFT, "Netid", "", 0, 0, 0 }, | |
143 | { ALIGN_LEFT, "State", " ", 0, 0, 0 }, | |
144 | { ALIGN_LEFT, "Recv-Q", " ", 0, 0, 0 }, | |
145 | { ALIGN_LEFT, "Send-Q", " ", 0, 0, 0 }, | |
146 | { ALIGN_RIGHT, "Local Address:", " ", 0, 0, 0 }, | |
147 | { ALIGN_LEFT, "Port", "", 0, 0, 0 }, | |
148 | { ALIGN_RIGHT, "Peer Address:", " ", 0, 0, 0 }, | |
149 | { ALIGN_LEFT, "Port", "", 0, 0, 0 }, | |
150 | { ALIGN_LEFT, "", "", 0, 0, 0 }, | |
151 | }; | |
152 | ||
153 | static struct column *current_field = columns; | |
154 | ||
155 | /* Output buffer: chained chunks of BUF_CHUNK bytes. Each field is written to | |
156 | * the buffer as a variable size token. A token consists of a 16 bits length | |
157 | * field, followed by a string which is not NULL-terminated. | |
158 | * | |
159 | * A new chunk is allocated and linked when the current chunk doesn't have | |
160 | * enough room to store the current token as a whole. | |
161 | */ | |
162 | struct buf_chunk { | |
163 | struct buf_chunk *next; /* Next chained chunk */ | |
164 | char *end; /* Current end of content */ | |
165 | char data[0]; | |
166 | }; | |
167 | ||
168 | struct buf_token { | |
169 | uint16_t len; /* Data length, excluding length descriptor */ | |
170 | char data[0]; | |
171 | }; | |
172 | ||
173 | static struct { | |
174 | struct buf_token *cur; /* Position of current token in chunk */ | |
175 | struct buf_chunk *head; /* First chunk */ | |
176 | struct buf_chunk *tail; /* Current chunk */ | |
177 | } buffer; | |
178 | ||
179 | static const char *TCP_PROTO = "tcp"; | |
180 | static const char *SCTP_PROTO = "sctp"; | |
181 | static const char *UDP_PROTO = "udp"; | |
182 | static const char *RAW_PROTO = "raw"; | |
183 | static const char *dg_proto; | |
184 | ||
185 | enum { | |
186 | TCP_DB, | |
187 | DCCP_DB, | |
188 | UDP_DB, | |
189 | RAW_DB, | |
190 | UNIX_DG_DB, | |
191 | UNIX_ST_DB, | |
192 | UNIX_SQ_DB, | |
193 | PACKET_DG_DB, | |
194 | PACKET_R_DB, | |
195 | NETLINK_DB, | |
196 | SCTP_DB, | |
197 | VSOCK_ST_DB, | |
198 | VSOCK_DG_DB, | |
199 | TIPC_DB, | |
200 | MAX_DB | |
201 | }; | |
202 | ||
203 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) | |
204 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB)) | |
205 | #define ALL_DB ((1<<MAX_DB)-1) | |
206 | #define INET_L4_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<SCTP_DB)) | |
207 | #define INET_DBM (INET_L4_DBM | (1<<RAW_DB)) | |
208 | #define VSOCK_DBM ((1<<VSOCK_ST_DB)|(1<<VSOCK_DG_DB)) | |
209 | ||
210 | enum { | |
211 | SS_UNKNOWN, | |
212 | SS_ESTABLISHED, | |
213 | SS_SYN_SENT, | |
214 | SS_SYN_RECV, | |
215 | SS_FIN_WAIT1, | |
216 | SS_FIN_WAIT2, | |
217 | SS_TIME_WAIT, | |
218 | SS_CLOSE, | |
219 | SS_CLOSE_WAIT, | |
220 | SS_LAST_ACK, | |
221 | SS_LISTEN, | |
222 | SS_CLOSING, | |
223 | SS_MAX | |
224 | }; | |
225 | ||
226 | enum { | |
227 | SCTP_STATE_CLOSED = 0, | |
228 | SCTP_STATE_COOKIE_WAIT = 1, | |
229 | SCTP_STATE_COOKIE_ECHOED = 2, | |
230 | SCTP_STATE_ESTABLISHED = 3, | |
231 | SCTP_STATE_SHUTDOWN_PENDING = 4, | |
232 | SCTP_STATE_SHUTDOWN_SENT = 5, | |
233 | SCTP_STATE_SHUTDOWN_RECEIVED = 6, | |
234 | SCTP_STATE_SHUTDOWN_ACK_SENT = 7, | |
235 | }; | |
236 | ||
237 | #define SS_ALL ((1 << SS_MAX) - 1) | |
238 | #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV))) | |
239 | #define TIPC_SS_CONN ((1<<SS_ESTABLISHED)|(1<<SS_LISTEN)|(1<<SS_CLOSE)) | |
240 | ||
241 | #include "ssfilter.h" | |
242 | ||
243 | struct filter { | |
244 | int dbs; | |
245 | int states; | |
246 | uint64_t families; | |
247 | struct ssfilter *f; | |
248 | bool kill; | |
249 | struct rtnl_handle *rth_for_killing; | |
250 | }; | |
251 | ||
252 | #define FAMILY_MASK(family) ((uint64_t)1 << (family)) | |
253 | ||
254 | static const struct filter default_dbs[MAX_DB] = { | |
255 | [TCP_DB] = { | |
256 | .states = SS_CONN, | |
257 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
258 | }, | |
259 | [DCCP_DB] = { | |
260 | .states = SS_CONN, | |
261 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
262 | }, | |
263 | [UDP_DB] = { | |
264 | .states = (1 << SS_ESTABLISHED), | |
265 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
266 | }, | |
267 | [RAW_DB] = { | |
268 | .states = (1 << SS_ESTABLISHED), | |
269 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
270 | }, | |
271 | [UNIX_DG_DB] = { | |
272 | .states = (1 << SS_CLOSE), | |
273 | .families = FAMILY_MASK(AF_UNIX), | |
274 | }, | |
275 | [UNIX_ST_DB] = { | |
276 | .states = SS_CONN, | |
277 | .families = FAMILY_MASK(AF_UNIX), | |
278 | }, | |
279 | [UNIX_SQ_DB] = { | |
280 | .states = SS_CONN, | |
281 | .families = FAMILY_MASK(AF_UNIX), | |
282 | }, | |
283 | [PACKET_DG_DB] = { | |
284 | .states = (1 << SS_CLOSE), | |
285 | .families = FAMILY_MASK(AF_PACKET), | |
286 | }, | |
287 | [PACKET_R_DB] = { | |
288 | .states = (1 << SS_CLOSE), | |
289 | .families = FAMILY_MASK(AF_PACKET), | |
290 | }, | |
291 | [NETLINK_DB] = { | |
292 | .states = (1 << SS_CLOSE), | |
293 | .families = FAMILY_MASK(AF_NETLINK), | |
294 | }, | |
295 | [SCTP_DB] = { | |
296 | .states = SS_CONN, | |
297 | .families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6), | |
298 | }, | |
299 | [VSOCK_ST_DB] = { | |
300 | .states = SS_CONN, | |
301 | .families = FAMILY_MASK(AF_VSOCK), | |
302 | }, | |
303 | [VSOCK_DG_DB] = { | |
304 | .states = SS_CONN, | |
305 | .families = FAMILY_MASK(AF_VSOCK), | |
306 | }, | |
307 | [TIPC_DB] = { | |
308 | .states = TIPC_SS_CONN, | |
309 | .families = FAMILY_MASK(AF_TIPC), | |
310 | }, | |
311 | }; | |
312 | ||
313 | static const struct filter default_afs[AF_MAX] = { | |
314 | [AF_INET] = { | |
315 | .dbs = INET_DBM, | |
316 | .states = SS_CONN, | |
317 | }, | |
318 | [AF_INET6] = { | |
319 | .dbs = INET_DBM, | |
320 | .states = SS_CONN, | |
321 | }, | |
322 | [AF_UNIX] = { | |
323 | .dbs = UNIX_DBM, | |
324 | .states = SS_CONN, | |
325 | }, | |
326 | [AF_PACKET] = { | |
327 | .dbs = PACKET_DBM, | |
328 | .states = (1 << SS_CLOSE), | |
329 | }, | |
330 | [AF_NETLINK] = { | |
331 | .dbs = (1 << NETLINK_DB), | |
332 | .states = (1 << SS_CLOSE), | |
333 | }, | |
334 | [AF_VSOCK] = { | |
335 | .dbs = VSOCK_DBM, | |
336 | .states = SS_CONN, | |
337 | }, | |
338 | [AF_TIPC] = { | |
339 | .dbs = (1 << TIPC_DB), | |
340 | .states = TIPC_SS_CONN, | |
341 | }, | |
342 | }; | |
343 | ||
344 | static int do_default = 1; | |
345 | static struct filter current_filter; | |
346 | ||
347 | static void filter_db_set(struct filter *f, int db, bool enable) | |
348 | { | |
349 | if (enable) { | |
350 | f->states |= default_dbs[db].states; | |
351 | f->dbs |= 1 << db; | |
352 | } else { | |
353 | f->dbs &= ~(1 << db); | |
354 | } | |
355 | do_default = 0; | |
356 | } | |
357 | ||
358 | static int filter_db_parse(struct filter *f, const char *s) | |
359 | { | |
360 | const struct { | |
361 | const char *name; | |
362 | int dbs[MAX_DB + 1]; | |
363 | } db_name_tbl[] = { | |
364 | #define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } } | |
365 | ENTRY(all, UDP_DB, DCCP_DB, TCP_DB, RAW_DB, | |
366 | UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB, | |
367 | PACKET_R_DB, PACKET_DG_DB, NETLINK_DB, | |
368 | SCTP_DB, VSOCK_ST_DB, VSOCK_DG_DB), | |
369 | ENTRY(inet, UDP_DB, DCCP_DB, TCP_DB, SCTP_DB, RAW_DB), | |
370 | ENTRY(udp, UDP_DB), | |
371 | ENTRY(dccp, DCCP_DB), | |
372 | ENTRY(tcp, TCP_DB), | |
373 | ENTRY(sctp, SCTP_DB), | |
374 | ENTRY(raw, RAW_DB), | |
375 | ENTRY(unix, UNIX_ST_DB, UNIX_DG_DB, UNIX_SQ_DB), | |
376 | ENTRY(unix_stream, UNIX_ST_DB), | |
377 | ENTRY(u_str, UNIX_ST_DB), /* alias for unix_stream */ | |
378 | ENTRY(unix_dgram, UNIX_DG_DB), | |
379 | ENTRY(u_dgr, UNIX_DG_DB), /* alias for unix_dgram */ | |
380 | ENTRY(unix_seqpacket, UNIX_SQ_DB), | |
381 | ENTRY(u_seq, UNIX_SQ_DB), /* alias for unix_seqpacket */ | |
382 | ENTRY(packet, PACKET_R_DB, PACKET_DG_DB), | |
383 | ENTRY(packet_raw, PACKET_R_DB), | |
384 | ENTRY(p_raw, PACKET_R_DB), /* alias for packet_raw */ | |
385 | ENTRY(packet_dgram, PACKET_DG_DB), | |
386 | ENTRY(p_dgr, PACKET_DG_DB), /* alias for packet_dgram */ | |
387 | ENTRY(netlink, NETLINK_DB), | |
388 | ENTRY(vsock, VSOCK_ST_DB, VSOCK_DG_DB), | |
389 | ENTRY(vsock_stream, VSOCK_ST_DB), | |
390 | ENTRY(v_str, VSOCK_ST_DB), /* alias for vsock_stream */ | |
391 | ENTRY(vsock_dgram, VSOCK_DG_DB), | |
392 | ENTRY(v_dgr, VSOCK_DG_DB), /* alias for vsock_dgram */ | |
393 | #undef ENTRY | |
394 | }; | |
395 | bool enable = true; | |
396 | unsigned int i; | |
397 | const int *dbp; | |
398 | ||
399 | if (s[0] == '!') { | |
400 | enable = false; | |
401 | s++; | |
402 | } | |
403 | for (i = 0; i < ARRAY_SIZE(db_name_tbl); i++) { | |
404 | if (strcmp(s, db_name_tbl[i].name)) | |
405 | continue; | |
406 | for (dbp = db_name_tbl[i].dbs; *dbp != MAX_DB; dbp++) | |
407 | filter_db_set(f, *dbp, enable); | |
408 | return 0; | |
409 | } | |
410 | return -1; | |
411 | } | |
412 | ||
413 | static void filter_af_set(struct filter *f, int af) | |
414 | { | |
415 | f->states |= default_afs[af].states; | |
416 | f->families |= FAMILY_MASK(af); | |
417 | do_default = 0; | |
418 | preferred_family = af; | |
419 | } | |
420 | ||
421 | static int filter_af_get(struct filter *f, int af) | |
422 | { | |
423 | return !!(f->families & FAMILY_MASK(af)); | |
424 | } | |
425 | ||
426 | static void filter_states_set(struct filter *f, int states) | |
427 | { | |
428 | if (states) | |
429 | f->states = states; | |
430 | } | |
431 | ||
432 | static void filter_merge_defaults(struct filter *f) | |
433 | { | |
434 | int db; | |
435 | int af; | |
436 | ||
437 | for (db = 0; db < MAX_DB; db++) { | |
438 | if (!(f->dbs & (1 << db))) | |
439 | continue; | |
440 | ||
441 | if (!(default_dbs[db].families & f->families)) | |
442 | f->families |= default_dbs[db].families; | |
443 | } | |
444 | for (af = 0; af < AF_MAX; af++) { | |
445 | if (!(f->families & FAMILY_MASK(af))) | |
446 | continue; | |
447 | ||
448 | if (!(default_afs[af].dbs & f->dbs)) | |
449 | f->dbs |= default_afs[af].dbs; | |
450 | } | |
451 | } | |
452 | ||
453 | static FILE *generic_proc_open(const char *env, const char *name) | |
454 | { | |
455 | const char *p = getenv(env); | |
456 | char store[128]; | |
457 | ||
458 | if (!p) { | |
459 | p = getenv("PROC_ROOT") ? : "/proc"; | |
460 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
461 | p = store; | |
462 | } | |
463 | ||
464 | return fopen(p, "r"); | |
465 | } | |
466 | #define net_tcp_open() generic_proc_open("PROC_NET_TCP", "net/tcp") | |
467 | #define net_tcp6_open() generic_proc_open("PROC_NET_TCP6", "net/tcp6") | |
468 | #define net_udp_open() generic_proc_open("PROC_NET_UDP", "net/udp") | |
469 | #define net_udp6_open() generic_proc_open("PROC_NET_UDP6", "net/udp6") | |
470 | #define net_raw_open() generic_proc_open("PROC_NET_RAW", "net/raw") | |
471 | #define net_raw6_open() generic_proc_open("PROC_NET_RAW6", "net/raw6") | |
472 | #define net_unix_open() generic_proc_open("PROC_NET_UNIX", "net/unix") | |
473 | #define net_packet_open() generic_proc_open("PROC_NET_PACKET", \ | |
474 | "net/packet") | |
475 | #define net_netlink_open() generic_proc_open("PROC_NET_NETLINK", \ | |
476 | "net/netlink") | |
477 | #define net_sockstat_open() generic_proc_open("PROC_NET_SOCKSTAT", \ | |
478 | "net/sockstat") | |
479 | #define net_sockstat6_open() generic_proc_open("PROC_NET_SOCKSTAT6", \ | |
480 | "net/sockstat6") | |
481 | #define net_snmp_open() generic_proc_open("PROC_NET_SNMP", "net/snmp") | |
482 | #define ephemeral_ports_open() generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", \ | |
483 | "sys/net/ipv4/ip_local_port_range") | |
484 | ||
485 | struct user_ent { | |
486 | struct user_ent *next; | |
487 | unsigned int ino; | |
488 | int pid; | |
489 | int fd; | |
490 | char *process; | |
491 | char *process_ctx; | |
492 | char *socket_ctx; | |
493 | }; | |
494 | ||
495 | #define USER_ENT_HASH_SIZE 256 | |
496 | struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; | |
497 | ||
498 | static int user_ent_hashfn(unsigned int ino) | |
499 | { | |
500 | int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino; | |
501 | ||
502 | return val & (USER_ENT_HASH_SIZE - 1); | |
503 | } | |
504 | ||
505 | static void user_ent_add(unsigned int ino, char *process, | |
506 | int pid, int fd, | |
507 | char *proc_ctx, | |
508 | char *sock_ctx) | |
509 | { | |
510 | struct user_ent *p, **pp; | |
511 | ||
512 | p = malloc(sizeof(struct user_ent)); | |
513 | if (!p) { | |
514 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
515 | abort(); | |
516 | } | |
517 | p->next = NULL; | |
518 | p->ino = ino; | |
519 | p->pid = pid; | |
520 | p->fd = fd; | |
521 | p->process = strdup(process); | |
522 | p->process_ctx = strdup(proc_ctx); | |
523 | p->socket_ctx = strdup(sock_ctx); | |
524 | ||
525 | pp = &user_ent_hash[user_ent_hashfn(ino)]; | |
526 | p->next = *pp; | |
527 | *pp = p; | |
528 | } | |
529 | ||
530 | static void user_ent_destroy(void) | |
531 | { | |
532 | struct user_ent *p, *p_next; | |
533 | int cnt = 0; | |
534 | ||
535 | while (cnt != USER_ENT_HASH_SIZE) { | |
536 | p = user_ent_hash[cnt]; | |
537 | while (p) { | |
538 | free(p->process); | |
539 | free(p->process_ctx); | |
540 | free(p->socket_ctx); | |
541 | p_next = p->next; | |
542 | free(p); | |
543 | p = p_next; | |
544 | } | |
545 | cnt++; | |
546 | } | |
547 | } | |
548 | ||
549 | static void user_ent_hash_build(void) | |
550 | { | |
551 | const char *root = getenv("PROC_ROOT") ? : "/proc/"; | |
552 | struct dirent *d; | |
553 | char name[1024]; | |
554 | int nameoff; | |
555 | DIR *dir; | |
556 | char *pid_context; | |
557 | char *sock_context; | |
558 | const char *no_ctx = "unavailable"; | |
559 | static int user_ent_hash_build_init; | |
560 | ||
561 | /* If show_users & show_proc_ctx set only do this once */ | |
562 | if (user_ent_hash_build_init != 0) | |
563 | return; | |
564 | ||
565 | user_ent_hash_build_init = 1; | |
566 | ||
567 | strlcpy(name, root, sizeof(name)); | |
568 | ||
569 | if (strlen(name) == 0 || name[strlen(name)-1] != '/') | |
570 | strcat(name, "/"); | |
571 | ||
572 | nameoff = strlen(name); | |
573 | ||
574 | dir = opendir(name); | |
575 | if (!dir) | |
576 | return; | |
577 | ||
578 | while ((d = readdir(dir)) != NULL) { | |
579 | struct dirent *d1; | |
580 | char process[16]; | |
581 | char *p; | |
582 | int pid, pos; | |
583 | DIR *dir1; | |
584 | char crap; | |
585 | ||
586 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) | |
587 | continue; | |
588 | ||
589 | if (getpidcon(pid, &pid_context) != 0) | |
590 | pid_context = strdup(no_ctx); | |
591 | ||
592 | snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); | |
593 | pos = strlen(name); | |
594 | if ((dir1 = opendir(name)) == NULL) { | |
595 | free(pid_context); | |
596 | continue; | |
597 | } | |
598 | ||
599 | process[0] = '\0'; | |
600 | p = process; | |
601 | ||
602 | while ((d1 = readdir(dir1)) != NULL) { | |
603 | const char *pattern = "socket:["; | |
604 | unsigned int ino; | |
605 | char lnk[64]; | |
606 | int fd; | |
607 | ssize_t link_len; | |
608 | char tmp[1024]; | |
609 | ||
610 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) | |
611 | continue; | |
612 | ||
613 | snprintf(name+pos, sizeof(name) - pos, "%d", fd); | |
614 | ||
615 | link_len = readlink(name, lnk, sizeof(lnk)-1); | |
616 | if (link_len == -1) | |
617 | continue; | |
618 | lnk[link_len] = '\0'; | |
619 | ||
620 | if (strncmp(lnk, pattern, strlen(pattern))) | |
621 | continue; | |
622 | ||
623 | sscanf(lnk, "socket:[%u]", &ino); | |
624 | ||
625 | snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s", | |
626 | root, pid, d1->d_name); | |
627 | ||
628 | if (getfilecon(tmp, &sock_context) <= 0) | |
629 | sock_context = strdup(no_ctx); | |
630 | ||
631 | if (*p == '\0') { | |
632 | FILE *fp; | |
633 | ||
634 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", | |
635 | root, pid); | |
636 | if ((fp = fopen(tmp, "r")) != NULL) { | |
637 | if (fscanf(fp, "%*d (%[^)])", p) < 1) | |
638 | ; /* ignore */ | |
639 | fclose(fp); | |
640 | } | |
641 | } | |
642 | user_ent_add(ino, p, pid, fd, | |
643 | pid_context, sock_context); | |
644 | free(sock_context); | |
645 | } | |
646 | free(pid_context); | |
647 | closedir(dir1); | |
648 | } | |
649 | closedir(dir); | |
650 | } | |
651 | ||
652 | enum entry_types { | |
653 | USERS, | |
654 | PROC_CTX, | |
655 | PROC_SOCK_CTX | |
656 | }; | |
657 | ||
658 | #define ENTRY_BUF_SIZE 512 | |
659 | static int find_entry(unsigned int ino, char **buf, int type) | |
660 | { | |
661 | struct user_ent *p; | |
662 | int cnt = 0; | |
663 | char *ptr; | |
664 | char *new_buf; | |
665 | int len, new_buf_len; | |
666 | int buf_used = 0; | |
667 | int buf_len = 0; | |
668 | ||
669 | if (!ino) | |
670 | return 0; | |
671 | ||
672 | p = user_ent_hash[user_ent_hashfn(ino)]; | |
673 | ptr = *buf = NULL; | |
674 | while (p) { | |
675 | if (p->ino != ino) | |
676 | goto next; | |
677 | ||
678 | while (1) { | |
679 | ptr = *buf + buf_used; | |
680 | switch (type) { | |
681 | case USERS: | |
682 | len = snprintf(ptr, buf_len - buf_used, | |
683 | "(\"%s\",pid=%d,fd=%d),", | |
684 | p->process, p->pid, p->fd); | |
685 | break; | |
686 | case PROC_CTX: | |
687 | len = snprintf(ptr, buf_len - buf_used, | |
688 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),", | |
689 | p->process, p->pid, | |
690 | p->process_ctx, p->fd); | |
691 | break; | |
692 | case PROC_SOCK_CTX: | |
693 | len = snprintf(ptr, buf_len - buf_used, | |
694 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),", | |
695 | p->process, p->pid, | |
696 | p->process_ctx, p->fd, | |
697 | p->socket_ctx); | |
698 | break; | |
699 | default: | |
700 | fprintf(stderr, "ss: invalid type: %d\n", type); | |
701 | abort(); | |
702 | } | |
703 | ||
704 | if (len < 0 || len >= buf_len - buf_used) { | |
705 | new_buf_len = buf_len + ENTRY_BUF_SIZE; | |
706 | new_buf = realloc(*buf, new_buf_len); | |
707 | if (!new_buf) { | |
708 | fprintf(stderr, "ss: failed to malloc buffer\n"); | |
709 | abort(); | |
710 | } | |
711 | *buf = new_buf; | |
712 | buf_len = new_buf_len; | |
713 | continue; | |
714 | } else { | |
715 | buf_used += len; | |
716 | break; | |
717 | } | |
718 | } | |
719 | cnt++; | |
720 | next: | |
721 | p = p->next; | |
722 | } | |
723 | if (buf_used) { | |
724 | ptr = *buf + buf_used; | |
725 | ptr[-1] = '\0'; | |
726 | } | |
727 | return cnt; | |
728 | } | |
729 | ||
730 | static unsigned long long cookie_sk_get(const uint32_t *cookie) | |
731 | { | |
732 | return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0]; | |
733 | } | |
734 | ||
735 | static const char *sctp_sstate_name[] = { | |
736 | [SCTP_STATE_CLOSED] = "CLOSED", | |
737 | [SCTP_STATE_COOKIE_WAIT] = "COOKIE_WAIT", | |
738 | [SCTP_STATE_COOKIE_ECHOED] = "COOKIE_ECHOED", | |
739 | [SCTP_STATE_ESTABLISHED] = "ESTAB", | |
740 | [SCTP_STATE_SHUTDOWN_PENDING] = "SHUTDOWN_PENDING", | |
741 | [SCTP_STATE_SHUTDOWN_SENT] = "SHUTDOWN_SENT", | |
742 | [SCTP_STATE_SHUTDOWN_RECEIVED] = "SHUTDOWN_RECEIVED", | |
743 | [SCTP_STATE_SHUTDOWN_ACK_SENT] = "ACK_SENT", | |
744 | }; | |
745 | ||
746 | static const char * const stype_nameg[] = { | |
747 | "UNKNOWN", | |
748 | [SOCK_STREAM] = "STREAM", | |
749 | [SOCK_DGRAM] = "DGRAM", | |
750 | [SOCK_RDM] = "RDM", | |
751 | [SOCK_SEQPACKET] = "SEQPACKET", | |
752 | }; | |
753 | ||
754 | struct sockstat { | |
755 | struct sockstat *next; | |
756 | unsigned int type; | |
757 | uint16_t prot; | |
758 | uint16_t raw_prot; | |
759 | inet_prefix local; | |
760 | inet_prefix remote; | |
761 | int lport; | |
762 | int rport; | |
763 | int state; | |
764 | int rq, wq; | |
765 | unsigned int ino; | |
766 | unsigned int uid; | |
767 | int refcnt; | |
768 | unsigned int iface; | |
769 | unsigned long long sk; | |
770 | char *name; | |
771 | char *peer_name; | |
772 | __u32 mark; | |
773 | }; | |
774 | ||
775 | struct dctcpstat { | |
776 | unsigned int ce_state; | |
777 | unsigned int alpha; | |
778 | unsigned int ab_ecn; | |
779 | unsigned int ab_tot; | |
780 | bool enabled; | |
781 | }; | |
782 | ||
783 | struct tcpstat { | |
784 | struct sockstat ss; | |
785 | unsigned int timer; | |
786 | unsigned int timeout; | |
787 | int probes; | |
788 | char cong_alg[16]; | |
789 | double rto, ato, rtt, rttvar; | |
790 | int qack, ssthresh, backoff; | |
791 | double send_bps; | |
792 | int snd_wscale; | |
793 | int rcv_wscale; | |
794 | int mss; | |
795 | int rcv_mss; | |
796 | int advmss; | |
797 | unsigned int pmtu; | |
798 | unsigned int cwnd; | |
799 | unsigned int lastsnd; | |
800 | unsigned int lastrcv; | |
801 | unsigned int lastack; | |
802 | double pacing_rate; | |
803 | double pacing_rate_max; | |
804 | double delivery_rate; | |
805 | unsigned long long bytes_acked; | |
806 | unsigned long long bytes_received; | |
807 | unsigned int segs_out; | |
808 | unsigned int segs_in; | |
809 | unsigned int data_segs_out; | |
810 | unsigned int data_segs_in; | |
811 | unsigned int unacked; | |
812 | unsigned int retrans; | |
813 | unsigned int retrans_total; | |
814 | unsigned int lost; | |
815 | unsigned int sacked; | |
816 | unsigned int fackets; | |
817 | unsigned int reordering; | |
818 | unsigned int not_sent; | |
819 | double rcv_rtt; | |
820 | double min_rtt; | |
821 | int rcv_space; | |
822 | unsigned int rcv_ssthresh; | |
823 | unsigned long long busy_time; | |
824 | unsigned long long rwnd_limited; | |
825 | unsigned long long sndbuf_limited; | |
826 | bool has_ts_opt; | |
827 | bool has_sack_opt; | |
828 | bool has_ecn_opt; | |
829 | bool has_ecnseen_opt; | |
830 | bool has_fastopen_opt; | |
831 | bool has_wscale_opt; | |
832 | bool app_limited; | |
833 | struct dctcpstat *dctcp; | |
834 | struct tcp_bbr_info *bbr_info; | |
835 | }; | |
836 | ||
837 | /* SCTP assocs share the same inode number with their parent endpoint. So if we | |
838 | * have seen the inode number before, it must be an assoc instead of the next | |
839 | * endpoint. */ | |
840 | static bool is_sctp_assoc(struct sockstat *s, const char *sock_name) | |
841 | { | |
842 | if (strcmp(sock_name, "sctp")) | |
843 | return false; | |
844 | if (!sctp_ino || sctp_ino != s->ino) | |
845 | return false; | |
846 | return true; | |
847 | } | |
848 | ||
849 | static const char *unix_netid_name(int type) | |
850 | { | |
851 | switch (type) { | |
852 | case SOCK_STREAM: | |
853 | return "u_str"; | |
854 | case SOCK_SEQPACKET: | |
855 | return "u_seq"; | |
856 | case SOCK_DGRAM: | |
857 | default: | |
858 | return "u_dgr"; | |
859 | } | |
860 | } | |
861 | ||
862 | static const char *proto_name(int protocol) | |
863 | { | |
864 | switch (protocol) { | |
865 | case 0: | |
866 | return "raw"; | |
867 | case IPPROTO_UDP: | |
868 | return "udp"; | |
869 | case IPPROTO_TCP: | |
870 | return "tcp"; | |
871 | case IPPROTO_SCTP: | |
872 | return "sctp"; | |
873 | case IPPROTO_DCCP: | |
874 | return "dccp"; | |
875 | case IPPROTO_ICMPV6: | |
876 | return "icmp6"; | |
877 | } | |
878 | ||
879 | return "???"; | |
880 | } | |
881 | ||
882 | static const char *vsock_netid_name(int type) | |
883 | { | |
884 | switch (type) { | |
885 | case SOCK_STREAM: | |
886 | return "v_str"; | |
887 | case SOCK_DGRAM: | |
888 | return "v_dgr"; | |
889 | default: | |
890 | return "???"; | |
891 | } | |
892 | } | |
893 | ||
894 | static const char *tipc_netid_name(int type) | |
895 | { | |
896 | switch (type) { | |
897 | case SOCK_STREAM: | |
898 | return "ti_st"; | |
899 | case SOCK_DGRAM: | |
900 | return "ti_dg"; | |
901 | case SOCK_RDM: | |
902 | return "ti_rd"; | |
903 | case SOCK_SEQPACKET: | |
904 | return "ti_sq"; | |
905 | default: | |
906 | return "???"; | |
907 | } | |
908 | } | |
909 | ||
910 | /* Allocate and initialize a new buffer chunk */ | |
911 | static struct buf_chunk *buf_chunk_new(void) | |
912 | { | |
913 | struct buf_chunk *new = malloc(BUF_CHUNK); | |
914 | ||
915 | if (!new) | |
916 | abort(); | |
917 | ||
918 | new->next = NULL; | |
919 | ||
920 | /* This is also the last block */ | |
921 | buffer.tail = new; | |
922 | ||
923 | /* Next token will be stored at the beginning of chunk data area, and | |
924 | * its initial length is zero. | |
925 | */ | |
926 | buffer.cur = (struct buf_token *)new->data; | |
927 | buffer.cur->len = 0; | |
928 | ||
929 | new->end = buffer.cur->data; | |
930 | ||
931 | return new; | |
932 | } | |
933 | ||
934 | /* Return available tail room in given chunk */ | |
935 | static int buf_chunk_avail(struct buf_chunk *chunk) | |
936 | { | |
937 | return BUF_CHUNK - offsetof(struct buf_chunk, data) - | |
938 | (chunk->end - chunk->data); | |
939 | } | |
940 | ||
941 | /* Update end pointer and token length, link new chunk if we hit the end of the | |
942 | * current one. Return -EAGAIN if we got a new chunk, caller has to print again. | |
943 | */ | |
944 | static int buf_update(int len) | |
945 | { | |
946 | struct buf_chunk *chunk = buffer.tail; | |
947 | struct buf_token *t = buffer.cur; | |
948 | ||
949 | /* Claim success if new content fits in the current chunk, and anyway | |
950 | * if this is the first token in the chunk: in the latter case, | |
951 | * allocating a new chunk won't help, so we'll just cut the output. | |
952 | */ | |
953 | if ((len < buf_chunk_avail(chunk) && len != -1 /* glibc < 2.0.6 */) || | |
954 | t == (struct buf_token *)chunk->data) { | |
955 | len = min(len, buf_chunk_avail(chunk)); | |
956 | ||
957 | /* Total field length can't exceed 2^16 bytes, cut as needed */ | |
958 | len = min(len, USHRT_MAX - t->len); | |
959 | ||
960 | chunk->end += len; | |
961 | t->len += len; | |
962 | return 0; | |
963 | } | |
964 | ||
965 | /* Content truncated, time to allocate more */ | |
966 | chunk->next = buf_chunk_new(); | |
967 | ||
968 | /* Copy current token over to new chunk, including length descriptor */ | |
969 | memcpy(chunk->next->data, t, sizeof(t->len) + t->len); | |
970 | chunk->next->end += t->len; | |
971 | ||
972 | /* Discard partially written field in old chunk */ | |
973 | chunk->end -= t->len + sizeof(t->len); | |
974 | ||
975 | return -EAGAIN; | |
976 | } | |
977 | ||
978 | /* Append content to buffer as part of the current field */ | |
979 | static void out(const char *fmt, ...) | |
980 | { | |
981 | struct column *f = current_field; | |
982 | va_list args; | |
983 | char *pos; | |
984 | int len; | |
985 | ||
986 | if (f->disabled) | |
987 | return; | |
988 | ||
989 | if (!buffer.head) | |
990 | buffer.head = buf_chunk_new(); | |
991 | ||
992 | again: /* Append to buffer: if we have a new chunk, print again */ | |
993 | ||
994 | pos = buffer.cur->data + buffer.cur->len; | |
995 | va_start(args, fmt); | |
996 | ||
997 | /* Limit to tail room. If we hit the limit, buf_update() will tell us */ | |
998 | len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, args); | |
999 | va_end(args); | |
1000 | ||
1001 | if (buf_update(len)) | |
1002 | goto again; | |
1003 | } | |
1004 | ||
1005 | static int print_left_spacing(struct column *f, int stored, int printed) | |
1006 | { | |
1007 | int s; | |
1008 | ||
1009 | if (!f->width || f->align == ALIGN_LEFT) | |
1010 | return 0; | |
1011 | ||
1012 | s = f->width - stored - printed; | |
1013 | if (f->align == ALIGN_CENTER) | |
1014 | /* If count of total spacing is odd, shift right by one */ | |
1015 | s = (s + 1) / 2; | |
1016 | ||
1017 | if (s > 0) | |
1018 | return printf("%*c", s, ' '); | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | static void print_right_spacing(struct column *f, int printed) | |
1024 | { | |
1025 | int s; | |
1026 | ||
1027 | if (!f->width || f->align == ALIGN_RIGHT) | |
1028 | return; | |
1029 | ||
1030 | s = f->width - printed; | |
1031 | if (f->align == ALIGN_CENTER) | |
1032 | s /= 2; | |
1033 | ||
1034 | if (s > 0) | |
1035 | printf("%*c", s, ' '); | |
1036 | } | |
1037 | ||
1038 | /* Done with field: update buffer pointer, start new token after current one */ | |
1039 | static void field_flush(struct column *f) | |
1040 | { | |
1041 | struct buf_chunk *chunk; | |
1042 | unsigned int pad; | |
1043 | ||
1044 | if (f->disabled) | |
1045 | return; | |
1046 | ||
1047 | chunk = buffer.tail; | |
1048 | pad = buffer.cur->len % 2; | |
1049 | ||
1050 | if (buffer.cur->len > f->max_len) | |
1051 | f->max_len = buffer.cur->len; | |
1052 | ||
1053 | /* We need a new chunk if we can't store the next length descriptor. | |
1054 | * Mind the gap between end of previous token and next aligned position | |
1055 | * for length descriptor. | |
1056 | */ | |
1057 | if (buf_chunk_avail(chunk) - pad < sizeof(buffer.cur->len)) { | |
1058 | chunk->end += pad; | |
1059 | chunk->next = buf_chunk_new(); | |
1060 | return; | |
1061 | } | |
1062 | ||
1063 | buffer.cur = (struct buf_token *)(buffer.cur->data + | |
1064 | LEN_ALIGN(buffer.cur->len)); | |
1065 | buffer.cur->len = 0; | |
1066 | buffer.tail->end = buffer.cur->data; | |
1067 | } | |
1068 | ||
1069 | static int field_is_last(struct column *f) | |
1070 | { | |
1071 | return f - columns == COL_MAX - 1; | |
1072 | } | |
1073 | ||
1074 | static void field_next(void) | |
1075 | { | |
1076 | field_flush(current_field); | |
1077 | ||
1078 | if (field_is_last(current_field)) | |
1079 | current_field = columns; | |
1080 | else | |
1081 | current_field++; | |
1082 | } | |
1083 | ||
1084 | /* Walk through fields and flush them until we reach the desired one */ | |
1085 | static void field_set(enum col_id id) | |
1086 | { | |
1087 | while (id != current_field - columns) | |
1088 | field_next(); | |
1089 | } | |
1090 | ||
1091 | /* Print header for all non-empty columns */ | |
1092 | static void print_header(void) | |
1093 | { | |
1094 | while (!field_is_last(current_field)) { | |
1095 | if (!current_field->disabled) | |
1096 | out(current_field->header); | |
1097 | field_next(); | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | /* Get the next available token in the buffer starting from the current token */ | |
1102 | static struct buf_token *buf_token_next(struct buf_token *cur) | |
1103 | { | |
1104 | struct buf_chunk *chunk = buffer.tail; | |
1105 | ||
1106 | /* If we reached the end of chunk contents, get token from next chunk */ | |
1107 | if (cur->data + LEN_ALIGN(cur->len) == chunk->end) { | |
1108 | buffer.tail = chunk = chunk->next; | |
1109 | return chunk ? (struct buf_token *)chunk->data : NULL; | |
1110 | } | |
1111 | ||
1112 | return (struct buf_token *)(cur->data + LEN_ALIGN(cur->len)); | |
1113 | } | |
1114 | ||
1115 | /* Free up all allocated buffer chunks */ | |
1116 | static void buf_free_all(void) | |
1117 | { | |
1118 | struct buf_chunk *tmp; | |
1119 | ||
1120 | for (buffer.tail = buffer.head; buffer.tail; ) { | |
1121 | tmp = buffer.tail; | |
1122 | buffer.tail = buffer.tail->next; | |
1123 | free(tmp); | |
1124 | } | |
1125 | buffer.head = NULL; | |
1126 | } | |
1127 | ||
1128 | /* Get current screen width, default to 80 columns if TIOCGWINSZ fails */ | |
1129 | static int render_screen_width(void) | |
1130 | { | |
1131 | int width = 80; | |
1132 | ||
1133 | if (isatty(STDOUT_FILENO)) { | |
1134 | struct winsize w; | |
1135 | ||
1136 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { | |
1137 | if (w.ws_col > 0) | |
1138 | width = w.ws_col; | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | return width; | |
1143 | } | |
1144 | ||
1145 | /* Calculate column width from contents length. If columns don't fit on one | |
1146 | * line, break them into the least possible amount of lines and keep them | |
1147 | * aligned across lines. Available screen space is equally spread between fields | |
1148 | * as additional spacing. | |
1149 | */ | |
1150 | static void render_calc_width(void) | |
1151 | { | |
1152 | int screen_width = render_screen_width(); | |
1153 | struct column *c, *eol = columns - 1; | |
1154 | int first, len = 0, linecols = 0; | |
1155 | ||
1156 | /* First pass: set width for each column to measured content length */ | |
1157 | for (first = 1, c = columns; c - columns < COL_MAX; c++) { | |
1158 | if (c->disabled) | |
1159 | continue; | |
1160 | ||
1161 | if (!first && c->max_len) | |
1162 | c->width = c->max_len + strlen(c->ldelim); | |
1163 | else | |
1164 | c->width = c->max_len; | |
1165 | ||
1166 | /* But don't exceed screen size. If we exceed the screen size | |
1167 | * for even a single field, it will just start on a line of its | |
1168 | * own and then naturally wrap. | |
1169 | */ | |
1170 | c->width = min(c->width, screen_width); | |
1171 | ||
1172 | if (c->width) | |
1173 | first = 0; | |
1174 | } | |
1175 | ||
1176 | /* Second pass: find out newlines and distribute available spacing */ | |
1177 | for (c = columns; c - columns < COL_MAX; c++) { | |
1178 | int pad, spacing, rem, last; | |
1179 | struct column *tmp; | |
1180 | ||
1181 | if (!c->width) | |
1182 | continue; | |
1183 | ||
1184 | linecols++; | |
1185 | len += c->width; | |
1186 | ||
1187 | for (last = 1, tmp = c + 1; tmp - columns < COL_MAX; tmp++) { | |
1188 | if (tmp->width) { | |
1189 | last = 0; | |
1190 | break; | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | if (!last && len < screen_width) { | |
1195 | /* Columns fit on screen so far, nothing to do yet */ | |
1196 | continue; | |
1197 | } | |
1198 | ||
1199 | if (len == screen_width) { | |
1200 | /* Exact fit, just start with new line */ | |
1201 | goto newline; | |
1202 | } | |
1203 | ||
1204 | if (len > screen_width) { | |
1205 | /* Screen width exceeded: go back one column */ | |
1206 | len -= c->width; | |
1207 | c--; | |
1208 | linecols--; | |
1209 | } | |
1210 | ||
1211 | /* Distribute remaining space to columns on this line */ | |
1212 | pad = screen_width - len; | |
1213 | spacing = pad / linecols; | |
1214 | rem = pad % linecols; | |
1215 | for (tmp = c; tmp > eol; tmp--) { | |
1216 | if (!tmp->width) | |
1217 | continue; | |
1218 | ||
1219 | tmp->width += spacing; | |
1220 | if (rem) { | |
1221 | tmp->width++; | |
1222 | rem--; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | newline: | |
1227 | /* Line break: reset line counters, mark end-of-line */ | |
1228 | eol = c; | |
1229 | len = 0; | |
1230 | linecols = 0; | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | /* Render buffered output with spacing and delimiters, then free up buffers */ | |
1235 | static void render(void) | |
1236 | { | |
1237 | struct buf_token *token; | |
1238 | int printed, line_started = 0; | |
1239 | struct column *f; | |
1240 | ||
1241 | if (!buffer.head) | |
1242 | return; | |
1243 | ||
1244 | token = (struct buf_token *)buffer.head->data; | |
1245 | ||
1246 | /* Ensure end alignment of last token, it wasn't necessarily flushed */ | |
1247 | buffer.tail->end += buffer.cur->len % 2; | |
1248 | ||
1249 | render_calc_width(); | |
1250 | ||
1251 | /* Rewind and replay */ | |
1252 | buffer.tail = buffer.head; | |
1253 | ||
1254 | f = columns; | |
1255 | while (!f->width) | |
1256 | f++; | |
1257 | ||
1258 | while (token) { | |
1259 | /* Print left delimiter only if we already started a line */ | |
1260 | if (line_started++) | |
1261 | printed = printf("%s", current_field->ldelim); | |
1262 | else | |
1263 | printed = 0; | |
1264 | ||
1265 | /* Print field content from token data with spacing */ | |
1266 | printed += print_left_spacing(f, token->len, printed); | |
1267 | printed += fwrite(token->data, 1, token->len, stdout); | |
1268 | print_right_spacing(f, printed); | |
1269 | ||
1270 | /* Go to next non-empty field, deal with end-of-line */ | |
1271 | do { | |
1272 | if (field_is_last(f)) { | |
1273 | printf("\n"); | |
1274 | f = columns; | |
1275 | line_started = 0; | |
1276 | } else { | |
1277 | f++; | |
1278 | } | |
1279 | } while (f->disabled); | |
1280 | ||
1281 | token = buf_token_next(token); | |
1282 | } | |
1283 | ||
1284 | buf_free_all(); | |
1285 | current_field = columns; | |
1286 | } | |
1287 | ||
1288 | static void sock_state_print(struct sockstat *s) | |
1289 | { | |
1290 | const char *sock_name; | |
1291 | static const char * const sstate_name[] = { | |
1292 | "UNKNOWN", | |
1293 | [SS_ESTABLISHED] = "ESTAB", | |
1294 | [SS_SYN_SENT] = "SYN-SENT", | |
1295 | [SS_SYN_RECV] = "SYN-RECV", | |
1296 | [SS_FIN_WAIT1] = "FIN-WAIT-1", | |
1297 | [SS_FIN_WAIT2] = "FIN-WAIT-2", | |
1298 | [SS_TIME_WAIT] = "TIME-WAIT", | |
1299 | [SS_CLOSE] = "UNCONN", | |
1300 | [SS_CLOSE_WAIT] = "CLOSE-WAIT", | |
1301 | [SS_LAST_ACK] = "LAST-ACK", | |
1302 | [SS_LISTEN] = "LISTEN", | |
1303 | [SS_CLOSING] = "CLOSING", | |
1304 | }; | |
1305 | ||
1306 | switch (s->local.family) { | |
1307 | case AF_UNIX: | |
1308 | sock_name = unix_netid_name(s->type); | |
1309 | break; | |
1310 | case AF_INET: | |
1311 | case AF_INET6: | |
1312 | sock_name = proto_name(s->type); | |
1313 | break; | |
1314 | case AF_PACKET: | |
1315 | sock_name = s->type == SOCK_RAW ? "p_raw" : "p_dgr"; | |
1316 | break; | |
1317 | case AF_NETLINK: | |
1318 | sock_name = "nl"; | |
1319 | break; | |
1320 | case AF_TIPC: | |
1321 | sock_name = tipc_netid_name(s->type); | |
1322 | break; | |
1323 | case AF_VSOCK: | |
1324 | sock_name = vsock_netid_name(s->type); | |
1325 | break; | |
1326 | default: | |
1327 | sock_name = "unknown"; | |
1328 | } | |
1329 | ||
1330 | if (is_sctp_assoc(s, sock_name)) { | |
1331 | field_set(COL_STATE); /* Empty Netid field */ | |
1332 | out("`- %s", sctp_sstate_name[s->state]); | |
1333 | } else { | |
1334 | field_set(COL_NETID); | |
1335 | out("%s", sock_name); | |
1336 | field_set(COL_STATE); | |
1337 | out("%s", sstate_name[s->state]); | |
1338 | } | |
1339 | ||
1340 | field_set(COL_RECVQ); | |
1341 | out("%-6d", s->rq); | |
1342 | field_set(COL_SENDQ); | |
1343 | out("%-6d", s->wq); | |
1344 | field_set(COL_ADDR); | |
1345 | } | |
1346 | ||
1347 | static void sock_details_print(struct sockstat *s) | |
1348 | { | |
1349 | if (s->uid) | |
1350 | out(" uid:%u", s->uid); | |
1351 | ||
1352 | out(" ino:%u", s->ino); | |
1353 | out(" sk:%llx", s->sk); | |
1354 | ||
1355 | if (s->mark) | |
1356 | out(" fwmark:0x%x", s->mark); | |
1357 | } | |
1358 | ||
1359 | static void sock_addr_print(const char *addr, char *delim, const char *port, | |
1360 | const char *ifname) | |
1361 | { | |
1362 | if (ifname) | |
1363 | out("%s" "%%" "%s%s", addr, ifname, delim); | |
1364 | else | |
1365 | out("%s%s", addr, delim); | |
1366 | ||
1367 | field_next(); | |
1368 | out("%s", port); | |
1369 | field_next(); | |
1370 | } | |
1371 | ||
1372 | static const char *print_ms_timer(unsigned int timeout) | |
1373 | { | |
1374 | static char buf[64]; | |
1375 | int secs, msecs, minutes; | |
1376 | ||
1377 | secs = timeout/1000; | |
1378 | minutes = secs/60; | |
1379 | secs = secs%60; | |
1380 | msecs = timeout%1000; | |
1381 | buf[0] = 0; | |
1382 | if (minutes) { | |
1383 | msecs = 0; | |
1384 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); | |
1385 | if (minutes > 9) | |
1386 | secs = 0; | |
1387 | } | |
1388 | if (secs) { | |
1389 | if (secs > 9) | |
1390 | msecs = 0; | |
1391 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); | |
1392 | } | |
1393 | if (msecs) | |
1394 | sprintf(buf+strlen(buf), "%03dms", msecs); | |
1395 | return buf; | |
1396 | } | |
1397 | ||
1398 | struct scache { | |
1399 | struct scache *next; | |
1400 | int port; | |
1401 | char *name; | |
1402 | const char *proto; | |
1403 | }; | |
1404 | ||
1405 | struct scache *rlist; | |
1406 | ||
1407 | static void init_service_resolver(void) | |
1408 | { | |
1409 | char buf[128]; | |
1410 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); | |
1411 | ||
1412 | if (!fp) | |
1413 | return; | |
1414 | ||
1415 | if (!fgets(buf, sizeof(buf), fp)) { | |
1416 | pclose(fp); | |
1417 | return; | |
1418 | } | |
1419 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
1420 | unsigned int progn, port; | |
1421 | char proto[128], prog[128] = "rpc."; | |
1422 | struct scache *c; | |
1423 | ||
1424 | if (sscanf(buf, "%u %*d %s %u %s", | |
1425 | &progn, proto, &port, prog+4) != 4) | |
1426 | continue; | |
1427 | ||
1428 | if (!(c = malloc(sizeof(*c)))) | |
1429 | continue; | |
1430 | ||
1431 | c->port = port; | |
1432 | c->name = strdup(prog); | |
1433 | if (strcmp(proto, TCP_PROTO) == 0) | |
1434 | c->proto = TCP_PROTO; | |
1435 | else if (strcmp(proto, UDP_PROTO) == 0) | |
1436 | c->proto = UDP_PROTO; | |
1437 | else if (strcmp(proto, SCTP_PROTO) == 0) | |
1438 | c->proto = SCTP_PROTO; | |
1439 | else | |
1440 | c->proto = NULL; | |
1441 | c->next = rlist; | |
1442 | rlist = c; | |
1443 | } | |
1444 | pclose(fp); | |
1445 | } | |
1446 | ||
1447 | /* Even do not try default linux ephemeral port ranges: | |
1448 | * default /etc/services contains so much of useless crap | |
1449 | * wouldbe "allocated" to this area that resolution | |
1450 | * is really harmful. I shrug each time when seeing | |
1451 | * "socks" or "cfinger" in dumps. | |
1452 | */ | |
1453 | static int is_ephemeral(int port) | |
1454 | { | |
1455 | static int min = 0, max; | |
1456 | ||
1457 | if (!min) { | |
1458 | FILE *f = ephemeral_ports_open(); | |
1459 | ||
1460 | if (!f || fscanf(f, "%d %d", &min, &max) < 2) { | |
1461 | min = 1024; | |
1462 | max = 4999; | |
1463 | } | |
1464 | if (f) | |
1465 | fclose(f); | |
1466 | } | |
1467 | return port >= min && port <= max; | |
1468 | } | |
1469 | ||
1470 | ||
1471 | static const char *__resolve_service(int port) | |
1472 | { | |
1473 | struct scache *c; | |
1474 | ||
1475 | for (c = rlist; c; c = c->next) { | |
1476 | if (c->port == port && c->proto == dg_proto) | |
1477 | return c->name; | |
1478 | } | |
1479 | ||
1480 | if (!is_ephemeral(port)) { | |
1481 | static int notfirst; | |
1482 | struct servent *se; | |
1483 | ||
1484 | if (!notfirst) { | |
1485 | setservent(1); | |
1486 | notfirst = 1; | |
1487 | } | |
1488 | se = getservbyport(htons(port), dg_proto); | |
1489 | if (se) | |
1490 | return se->s_name; | |
1491 | } | |
1492 | ||
1493 | return NULL; | |
1494 | } | |
1495 | ||
1496 | #define SCACHE_BUCKETS 1024 | |
1497 | static struct scache *cache_htab[SCACHE_BUCKETS]; | |
1498 | ||
1499 | static const char *resolve_service(int port) | |
1500 | { | |
1501 | static char buf[128]; | |
1502 | struct scache *c; | |
1503 | const char *res; | |
1504 | int hash; | |
1505 | ||
1506 | if (port == 0) { | |
1507 | buf[0] = '*'; | |
1508 | buf[1] = 0; | |
1509 | return buf; | |
1510 | } | |
1511 | ||
1512 | if (!resolve_services) | |
1513 | goto do_numeric; | |
1514 | ||
1515 | if (dg_proto == RAW_PROTO) | |
1516 | return inet_proto_n2a(port, buf, sizeof(buf)); | |
1517 | ||
1518 | ||
1519 | hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS; | |
1520 | ||
1521 | for (c = cache_htab[hash]; c; c = c->next) { | |
1522 | if (c->port == port && c->proto == dg_proto) | |
1523 | goto do_cache; | |
1524 | } | |
1525 | ||
1526 | c = malloc(sizeof(*c)); | |
1527 | if (!c) | |
1528 | goto do_numeric; | |
1529 | res = __resolve_service(port); | |
1530 | c->port = port; | |
1531 | c->name = res ? strdup(res) : NULL; | |
1532 | c->proto = dg_proto; | |
1533 | c->next = cache_htab[hash]; | |
1534 | cache_htab[hash] = c; | |
1535 | ||
1536 | do_cache: | |
1537 | if (c->name) | |
1538 | return c->name; | |
1539 | ||
1540 | do_numeric: | |
1541 | sprintf(buf, "%u", port); | |
1542 | return buf; | |
1543 | } | |
1544 | ||
1545 | static void inet_addr_print(const inet_prefix *a, int port, | |
1546 | unsigned int ifindex, bool v6only) | |
1547 | { | |
1548 | char buf[1024]; | |
1549 | const char *ap = buf; | |
1550 | const char *ifname = NULL; | |
1551 | ||
1552 | if (a->family == AF_INET) { | |
1553 | ap = format_host(AF_INET, 4, a->data); | |
1554 | } else { | |
1555 | if (!v6only && | |
1556 | !memcmp(a->data, &in6addr_any, sizeof(in6addr_any))) { | |
1557 | buf[0] = '*'; | |
1558 | buf[1] = 0; | |
1559 | } else { | |
1560 | ap = format_host(a->family, 16, a->data); | |
1561 | ||
1562 | /* Numeric IPv6 addresses should be bracketed */ | |
1563 | if (strchr(ap, ':')) { | |
1564 | snprintf(buf, sizeof(buf), | |
1565 | "[%s]", ap); | |
1566 | ap = buf; | |
1567 | } | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | if (ifindex) | |
1572 | ifname = ll_index_to_name(ifindex); | |
1573 | ||
1574 | sock_addr_print(ap, ":", resolve_service(port), ifname); | |
1575 | } | |
1576 | ||
1577 | struct aafilter { | |
1578 | inet_prefix addr; | |
1579 | int port; | |
1580 | unsigned int iface; | |
1581 | __u32 mark; | |
1582 | __u32 mask; | |
1583 | struct aafilter *next; | |
1584 | }; | |
1585 | ||
1586 | static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, | |
1587 | int plen) | |
1588 | { | |
1589 | if (!inet_addr_match(a, p, plen)) | |
1590 | return 0; | |
1591 | ||
1592 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches | |
1593 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped | |
1594 | * sockets. Fair? */ | |
1595 | if (p->family == AF_INET && a->family == AF_INET6) { | |
1596 | if (a->data[0] == 0 && a->data[1] == 0 && | |
1597 | a->data[2] == htonl(0xffff)) { | |
1598 | inet_prefix tmp = *a; | |
1599 | ||
1600 | tmp.data[0] = a->data[3]; | |
1601 | return inet_addr_match(&tmp, p, plen); | |
1602 | } | |
1603 | } | |
1604 | return 1; | |
1605 | } | |
1606 | ||
1607 | static int unix_match(const inet_prefix *a, const inet_prefix *p) | |
1608 | { | |
1609 | char *addr, *pattern; | |
1610 | ||
1611 | memcpy(&addr, a->data, sizeof(addr)); | |
1612 | memcpy(&pattern, p->data, sizeof(pattern)); | |
1613 | if (pattern == NULL) | |
1614 | return 1; | |
1615 | if (addr == NULL) | |
1616 | addr = ""; | |
1617 | return !fnmatch(pattern, addr, 0); | |
1618 | } | |
1619 | ||
1620 | static int run_ssfilter(struct ssfilter *f, struct sockstat *s) | |
1621 | { | |
1622 | switch (f->type) { | |
1623 | case SSF_S_AUTO: | |
1624 | { | |
1625 | if (s->local.family == AF_UNIX) { | |
1626 | char *p; | |
1627 | ||
1628 | memcpy(&p, s->local.data, sizeof(p)); | |
1629 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && | |
1630 | strspn(p+1, "0123456789abcdef") == 5); | |
1631 | } | |
1632 | if (s->local.family == AF_PACKET) | |
1633 | return s->lport == 0 && s->local.data[0] == 0; | |
1634 | if (s->local.family == AF_NETLINK) | |
1635 | return s->lport < 0; | |
1636 | if (s->local.family == AF_VSOCK) | |
1637 | return s->lport > 1023; | |
1638 | ||
1639 | return is_ephemeral(s->lport); | |
1640 | } | |
1641 | case SSF_DCOND: | |
1642 | { | |
1643 | struct aafilter *a = (void *)f->pred; | |
1644 | ||
1645 | if (a->addr.family == AF_UNIX) | |
1646 | return unix_match(&s->remote, &a->addr); | |
1647 | if (a->port != -1 && a->port != s->rport) | |
1648 | return 0; | |
1649 | if (a->addr.bitlen) { | |
1650 | do { | |
1651 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) | |
1652 | return 1; | |
1653 | } while ((a = a->next) != NULL); | |
1654 | return 0; | |
1655 | } | |
1656 | return 1; | |
1657 | } | |
1658 | case SSF_SCOND: | |
1659 | { | |
1660 | struct aafilter *a = (void *)f->pred; | |
1661 | ||
1662 | if (a->addr.family == AF_UNIX) | |
1663 | return unix_match(&s->local, &a->addr); | |
1664 | if (a->port != -1 && a->port != s->lport) | |
1665 | return 0; | |
1666 | if (a->addr.bitlen) { | |
1667 | do { | |
1668 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) | |
1669 | return 1; | |
1670 | } while ((a = a->next) != NULL); | |
1671 | return 0; | |
1672 | } | |
1673 | return 1; | |
1674 | } | |
1675 | case SSF_D_GE: | |
1676 | { | |
1677 | struct aafilter *a = (void *)f->pred; | |
1678 | ||
1679 | return s->rport >= a->port; | |
1680 | } | |
1681 | case SSF_D_LE: | |
1682 | { | |
1683 | struct aafilter *a = (void *)f->pred; | |
1684 | ||
1685 | return s->rport <= a->port; | |
1686 | } | |
1687 | case SSF_S_GE: | |
1688 | { | |
1689 | struct aafilter *a = (void *)f->pred; | |
1690 | ||
1691 | return s->lport >= a->port; | |
1692 | } | |
1693 | case SSF_S_LE: | |
1694 | { | |
1695 | struct aafilter *a = (void *)f->pred; | |
1696 | ||
1697 | return s->lport <= a->port; | |
1698 | } | |
1699 | case SSF_DEVCOND: | |
1700 | { | |
1701 | struct aafilter *a = (void *)f->pred; | |
1702 | ||
1703 | return s->iface == a->iface; | |
1704 | } | |
1705 | case SSF_MARKMASK: | |
1706 | { | |
1707 | struct aafilter *a = (void *)f->pred; | |
1708 | ||
1709 | return (s->mark & a->mask) == a->mark; | |
1710 | } | |
1711 | /* Yup. It is recursion. Sorry. */ | |
1712 | case SSF_AND: | |
1713 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); | |
1714 | case SSF_OR: | |
1715 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); | |
1716 | case SSF_NOT: | |
1717 | return !run_ssfilter(f->pred, s); | |
1718 | default: | |
1719 | abort(); | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | /* Relocate external jumps by reloc. */ | |
1724 | static void ssfilter_patch(char *a, int len, int reloc) | |
1725 | { | |
1726 | while (len > 0) { | |
1727 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a; | |
1728 | ||
1729 | if (op->no == len+4) | |
1730 | op->no += reloc; | |
1731 | len -= op->yes; | |
1732 | a += op->yes; | |
1733 | } | |
1734 | if (len < 0) | |
1735 | abort(); | |
1736 | } | |
1737 | ||
1738 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) | |
1739 | { | |
1740 | switch (f->type) { | |
1741 | case SSF_S_AUTO: | |
1742 | { | |
1743 | if (!(*bytecode = malloc(4))) abort(); | |
1744 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; | |
1745 | return 4; | |
1746 | } | |
1747 | case SSF_DCOND: | |
1748 | case SSF_SCOND: | |
1749 | { | |
1750 | struct aafilter *a = (void *)f->pred; | |
1751 | struct aafilter *b; | |
1752 | char *ptr; | |
1753 | int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); | |
1754 | int len = 0; | |
1755 | ||
1756 | for (b = a; b; b = b->next) { | |
1757 | len += 4 + sizeof(struct inet_diag_hostcond); | |
1758 | if (a->addr.family == AF_INET6) | |
1759 | len += 16; | |
1760 | else | |
1761 | len += 4; | |
1762 | if (b->next) | |
1763 | len += 4; | |
1764 | } | |
1765 | if (!(ptr = malloc(len))) abort(); | |
1766 | *bytecode = ptr; | |
1767 | for (b = a; b; b = b->next) { | |
1768 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; | |
1769 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); | |
1770 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); | |
1771 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4); | |
1772 | ||
1773 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; | |
1774 | cond->family = a->addr.family; | |
1775 | cond->port = a->port; | |
1776 | cond->prefix_len = a->addr.bitlen; | |
1777 | memcpy(cond->addr, a->addr.data, alen); | |
1778 | ptr += oplen; | |
1779 | if (b->next) { | |
1780 | op = (struct inet_diag_bc_op *)ptr; | |
1781 | *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; | |
1782 | ptr += 4; | |
1783 | } | |
1784 | } | |
1785 | return ptr - *bytecode; | |
1786 | } | |
1787 | case SSF_D_GE: | |
1788 | { | |
1789 | struct aafilter *x = (void *)f->pred; | |
1790 | ||
1791 | if (!(*bytecode = malloc(8))) abort(); | |
1792 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; | |
1793 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
1794 | return 8; | |
1795 | } | |
1796 | case SSF_D_LE: | |
1797 | { | |
1798 | struct aafilter *x = (void *)f->pred; | |
1799 | ||
1800 | if (!(*bytecode = malloc(8))) abort(); | |
1801 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; | |
1802 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
1803 | return 8; | |
1804 | } | |
1805 | case SSF_S_GE: | |
1806 | { | |
1807 | struct aafilter *x = (void *)f->pred; | |
1808 | ||
1809 | if (!(*bytecode = malloc(8))) abort(); | |
1810 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; | |
1811 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
1812 | return 8; | |
1813 | } | |
1814 | case SSF_S_LE: | |
1815 | { | |
1816 | struct aafilter *x = (void *)f->pred; | |
1817 | ||
1818 | if (!(*bytecode = malloc(8))) abort(); | |
1819 | ((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; | |
1820 | ((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; | |
1821 | return 8; | |
1822 | } | |
1823 | ||
1824 | case SSF_AND: | |
1825 | { | |
1826 | char *a1 = NULL, *a2 = NULL, *a; | |
1827 | int l1, l2; | |
1828 | ||
1829 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
1830 | l2 = ssfilter_bytecompile(f->post, &a2); | |
1831 | if (!l1 || !l2) { | |
1832 | free(a1); | |
1833 | free(a2); | |
1834 | return 0; | |
1835 | } | |
1836 | if (!(a = malloc(l1+l2))) abort(); | |
1837 | memcpy(a, a1, l1); | |
1838 | memcpy(a+l1, a2, l2); | |
1839 | free(a1); free(a2); | |
1840 | ssfilter_patch(a, l1, l2); | |
1841 | *bytecode = a; | |
1842 | return l1+l2; | |
1843 | } | |
1844 | case SSF_OR: | |
1845 | { | |
1846 | char *a1 = NULL, *a2 = NULL, *a; | |
1847 | int l1, l2; | |
1848 | ||
1849 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
1850 | l2 = ssfilter_bytecompile(f->post, &a2); | |
1851 | if (!l1 || !l2) { | |
1852 | free(a1); | |
1853 | free(a2); | |
1854 | return 0; | |
1855 | } | |
1856 | if (!(a = malloc(l1+l2+4))) abort(); | |
1857 | memcpy(a, a1, l1); | |
1858 | memcpy(a+l1+4, a2, l2); | |
1859 | free(a1); free(a2); | |
1860 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; | |
1861 | *bytecode = a; | |
1862 | return l1+l2+4; | |
1863 | } | |
1864 | case SSF_NOT: | |
1865 | { | |
1866 | char *a1 = NULL, *a; | |
1867 | int l1; | |
1868 | ||
1869 | l1 = ssfilter_bytecompile(f->pred, &a1); | |
1870 | if (!l1) { | |
1871 | free(a1); | |
1872 | return 0; | |
1873 | } | |
1874 | if (!(a = malloc(l1+4))) abort(); | |
1875 | memcpy(a, a1, l1); | |
1876 | free(a1); | |
1877 | *(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; | |
1878 | *bytecode = a; | |
1879 | return l1+4; | |
1880 | } | |
1881 | case SSF_DEVCOND: | |
1882 | { | |
1883 | /* bytecompile for SSF_DEVCOND not supported yet */ | |
1884 | return 0; | |
1885 | } | |
1886 | case SSF_MARKMASK: | |
1887 | { | |
1888 | struct aafilter *a = (void *)f->pred; | |
1889 | struct instr { | |
1890 | struct inet_diag_bc_op op; | |
1891 | struct inet_diag_markcond cond; | |
1892 | }; | |
1893 | int inslen = sizeof(struct instr); | |
1894 | ||
1895 | if (!(*bytecode = malloc(inslen))) abort(); | |
1896 | ((struct instr *)*bytecode)[0] = (struct instr) { | |
1897 | { INET_DIAG_BC_MARK_COND, inslen, inslen + 4 }, | |
1898 | { a->mark, a->mask}, | |
1899 | }; | |
1900 | ||
1901 | return inslen; | |
1902 | } | |
1903 | default: | |
1904 | abort(); | |
1905 | } | |
1906 | } | |
1907 | ||
1908 | static int remember_he(struct aafilter *a, struct hostent *he) | |
1909 | { | |
1910 | char **ptr = he->h_addr_list; | |
1911 | int cnt = 0; | |
1912 | int len; | |
1913 | ||
1914 | if (he->h_addrtype == AF_INET) | |
1915 | len = 4; | |
1916 | else if (he->h_addrtype == AF_INET6) | |
1917 | len = 16; | |
1918 | else | |
1919 | return 0; | |
1920 | ||
1921 | while (*ptr) { | |
1922 | struct aafilter *b = a; | |
1923 | ||
1924 | if (a->addr.bitlen) { | |
1925 | if ((b = malloc(sizeof(*b))) == NULL) | |
1926 | return cnt; | |
1927 | *b = *a; | |
1928 | a->next = b; | |
1929 | } | |
1930 | memcpy(b->addr.data, *ptr, len); | |
1931 | b->addr.bytelen = len; | |
1932 | b->addr.bitlen = len*8; | |
1933 | b->addr.family = he->h_addrtype; | |
1934 | ptr++; | |
1935 | cnt++; | |
1936 | } | |
1937 | return cnt; | |
1938 | } | |
1939 | ||
1940 | static int get_dns_host(struct aafilter *a, const char *addr, int fam) | |
1941 | { | |
1942 | static int notfirst; | |
1943 | int cnt = 0; | |
1944 | struct hostent *he; | |
1945 | ||
1946 | a->addr.bitlen = 0; | |
1947 | if (!notfirst) { | |
1948 | sethostent(1); | |
1949 | notfirst = 1; | |
1950 | } | |
1951 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); | |
1952 | if (he) | |
1953 | cnt = remember_he(a, he); | |
1954 | if (fam == AF_UNSPEC) { | |
1955 | he = gethostbyname2(addr, AF_INET6); | |
1956 | if (he) | |
1957 | cnt += remember_he(a, he); | |
1958 | } | |
1959 | return !cnt; | |
1960 | } | |
1961 | ||
1962 | static int xll_initted; | |
1963 | ||
1964 | static void xll_init(void) | |
1965 | { | |
1966 | struct rtnl_handle rth; | |
1967 | ||
1968 | if (rtnl_open(&rth, 0) < 0) | |
1969 | exit(1); | |
1970 | ||
1971 | ll_init_map(&rth); | |
1972 | rtnl_close(&rth); | |
1973 | xll_initted = 1; | |
1974 | } | |
1975 | ||
1976 | static const char *xll_index_to_name(int index) | |
1977 | { | |
1978 | if (!xll_initted) | |
1979 | xll_init(); | |
1980 | return ll_index_to_name(index); | |
1981 | } | |
1982 | ||
1983 | static int xll_name_to_index(const char *dev) | |
1984 | { | |
1985 | if (!xll_initted) | |
1986 | xll_init(); | |
1987 | return ll_name_to_index(dev); | |
1988 | } | |
1989 | ||
1990 | void *parse_devcond(char *name) | |
1991 | { | |
1992 | struct aafilter a = { .iface = 0 }; | |
1993 | struct aafilter *res; | |
1994 | ||
1995 | a.iface = xll_name_to_index(name); | |
1996 | if (a.iface == 0) { | |
1997 | char *end; | |
1998 | unsigned long n; | |
1999 | ||
2000 | n = strtoul(name, &end, 0); | |
2001 | if (!end || end == name || *end || n > UINT_MAX) | |
2002 | return NULL; | |
2003 | ||
2004 | a.iface = n; | |
2005 | } | |
2006 | ||
2007 | res = malloc(sizeof(*res)); | |
2008 | *res = a; | |
2009 | ||
2010 | return res; | |
2011 | } | |
2012 | ||
2013 | static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid) | |
2014 | { | |
2015 | *a = (inet_prefix){ | |
2016 | .bytelen = sizeof(cid), | |
2017 | .family = AF_VSOCK, | |
2018 | }; | |
2019 | memcpy(a->data, &cid, sizeof(cid)); | |
2020 | } | |
2021 | ||
2022 | void *parse_hostcond(char *addr, bool is_port) | |
2023 | { | |
2024 | char *port = NULL; | |
2025 | struct aafilter a = { .port = -1 }; | |
2026 | struct aafilter *res; | |
2027 | int fam = preferred_family; | |
2028 | struct filter *f = ¤t_filter; | |
2029 | ||
2030 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { | |
2031 | char *p; | |
2032 | ||
2033 | a.addr.family = AF_UNIX; | |
2034 | if (strncmp(addr, "unix:", 5) == 0) | |
2035 | addr += 5; | |
2036 | p = strdup(addr); | |
2037 | a.addr.bitlen = 8*strlen(p); | |
2038 | memcpy(a.addr.data, &p, sizeof(p)); | |
2039 | fam = AF_UNIX; | |
2040 | goto out; | |
2041 | } | |
2042 | ||
2043 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { | |
2044 | a.addr.family = AF_PACKET; | |
2045 | a.addr.bitlen = 0; | |
2046 | if (strncmp(addr, "link:", 5) == 0) | |
2047 | addr += 5; | |
2048 | port = strchr(addr, ':'); | |
2049 | if (port) { | |
2050 | *port = 0; | |
2051 | if (port[1] && strcmp(port+1, "*")) { | |
2052 | if (get_integer(&a.port, port+1, 0)) { | |
2053 | if ((a.port = xll_name_to_index(port+1)) <= 0) | |
2054 | return NULL; | |
2055 | } | |
2056 | } | |
2057 | } | |
2058 | if (addr[0] && strcmp(addr, "*")) { | |
2059 | unsigned short tmp; | |
2060 | ||
2061 | a.addr.bitlen = 32; | |
2062 | if (ll_proto_a2n(&tmp, addr)) | |
2063 | return NULL; | |
2064 | a.addr.data[0] = ntohs(tmp); | |
2065 | } | |
2066 | fam = AF_PACKET; | |
2067 | goto out; | |
2068 | } | |
2069 | ||
2070 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { | |
2071 | a.addr.family = AF_NETLINK; | |
2072 | a.addr.bitlen = 0; | |
2073 | if (strncmp(addr, "netlink:", 8) == 0) | |
2074 | addr += 8; | |
2075 | port = strchr(addr, ':'); | |
2076 | if (port) { | |
2077 | *port = 0; | |
2078 | if (port[1] && strcmp(port+1, "*")) { | |
2079 | if (get_integer(&a.port, port+1, 0)) { | |
2080 | if (strcmp(port+1, "kernel") == 0) | |
2081 | a.port = 0; | |
2082 | else | |
2083 | return NULL; | |
2084 | } | |
2085 | } | |
2086 | } | |
2087 | if (addr[0] && strcmp(addr, "*")) { | |
2088 | a.addr.bitlen = 32; | |
2089 | if (nl_proto_a2n(&a.addr.data[0], addr) == -1) | |
2090 | return NULL; | |
2091 | } | |
2092 | fam = AF_NETLINK; | |
2093 | goto out; | |
2094 | } | |
2095 | ||
2096 | if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) { | |
2097 | __u32 cid = ~(__u32)0; | |
2098 | ||
2099 | a.addr.family = AF_VSOCK; | |
2100 | if (strncmp(addr, "vsock:", 6) == 0) | |
2101 | addr += 6; | |
2102 | ||
2103 | if (is_port) | |
2104 | port = addr; | |
2105 | else { | |
2106 | port = strchr(addr, ':'); | |
2107 | if (port) { | |
2108 | *port = '\0'; | |
2109 | port++; | |
2110 | } | |
2111 | } | |
2112 | ||
2113 | if (port && strcmp(port, "*") && | |
2114 | get_u32((__u32 *)&a.port, port, 0)) | |
2115 | return NULL; | |
2116 | ||
2117 | if (addr[0] && strcmp(addr, "*")) { | |
2118 | a.addr.bitlen = 32; | |
2119 | if (get_u32(&cid, addr, 0)) | |
2120 | return NULL; | |
2121 | } | |
2122 | vsock_set_inet_prefix(&a.addr, cid); | |
2123 | fam = AF_VSOCK; | |
2124 | goto out; | |
2125 | } | |
2126 | ||
2127 | if (fam == AF_INET || !strncmp(addr, "inet:", 5)) { | |
2128 | fam = AF_INET; | |
2129 | if (!strncmp(addr, "inet:", 5)) | |
2130 | addr += 5; | |
2131 | } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) { | |
2132 | fam = AF_INET6; | |
2133 | if (!strncmp(addr, "inet6:", 6)) | |
2134 | addr += 6; | |
2135 | } | |
2136 | ||
2137 | /* URL-like literal [] */ | |
2138 | if (addr[0] == '[') { | |
2139 | addr++; | |
2140 | if ((port = strchr(addr, ']')) == NULL) | |
2141 | return NULL; | |
2142 | *port++ = 0; | |
2143 | } else if (addr[0] == '*') { | |
2144 | port = addr+1; | |
2145 | } else { | |
2146 | port = strrchr(strchr(addr, '/') ? : addr, ':'); | |
2147 | } | |
2148 | ||
2149 | if (is_port) | |
2150 | port = addr; | |
2151 | ||
2152 | if (port && *port) { | |
2153 | if (*port == ':') | |
2154 | *port++ = 0; | |
2155 | ||
2156 | if (*port && *port != '*') { | |
2157 | if (get_integer(&a.port, port, 0)) { | |
2158 | struct servent *se1 = NULL; | |
2159 | struct servent *se2 = NULL; | |
2160 | ||
2161 | if (current_filter.dbs&(1<<UDP_DB)) | |
2162 | se1 = getservbyname(port, UDP_PROTO); | |
2163 | if (current_filter.dbs&(1<<TCP_DB)) | |
2164 | se2 = getservbyname(port, TCP_PROTO); | |
2165 | if (se1 && se2 && se1->s_port != se2->s_port) { | |
2166 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
2167 | return NULL; | |
2168 | } | |
2169 | if (!se1) | |
2170 | se1 = se2; | |
2171 | if (se1) { | |
2172 | a.port = ntohs(se1->s_port); | |
2173 | } else { | |
2174 | struct scache *s; | |
2175 | ||
2176 | for (s = rlist; s; s = s->next) { | |
2177 | if ((s->proto == UDP_PROTO && | |
2178 | (current_filter.dbs&(1<<UDP_DB))) || | |
2179 | (s->proto == TCP_PROTO && | |
2180 | (current_filter.dbs&(1<<TCP_DB)))) { | |
2181 | if (s->name && strcmp(s->name, port) == 0) { | |
2182 | if (a.port > 0 && a.port != s->port) { | |
2183 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); | |
2184 | return NULL; | |
2185 | } | |
2186 | a.port = s->port; | |
2187 | } | |
2188 | } | |
2189 | } | |
2190 | if (a.port <= 0) { | |
2191 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); | |
2192 | return NULL; | |
2193 | } | |
2194 | } | |
2195 | } | |
2196 | } | |
2197 | } | |
2198 | if (!is_port && *addr && *addr != '*') { | |
2199 | if (get_prefix_1(&a.addr, addr, fam)) { | |
2200 | if (get_dns_host(&a, addr, fam)) { | |
2201 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); | |
2202 | return NULL; | |
2203 | } | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | out: | |
2208 | if (fam != AF_UNSPEC) { | |
2209 | int states = f->states; | |
2210 | f->families = 0; | |
2211 | filter_af_set(f, fam); | |
2212 | filter_states_set(f, states); | |
2213 | } | |
2214 | ||
2215 | res = malloc(sizeof(*res)); | |
2216 | if (res) | |
2217 | memcpy(res, &a, sizeof(a)); | |
2218 | return res; | |
2219 | } | |
2220 | ||
2221 | void *parse_markmask(const char *markmask) | |
2222 | { | |
2223 | struct aafilter a, *res; | |
2224 | ||
2225 | if (strchr(markmask, '/')) { | |
2226 | if (sscanf(markmask, "%i/%i", &a.mark, &a.mask) != 2) | |
2227 | return NULL; | |
2228 | } else { | |
2229 | a.mask = 0xffffffff; | |
2230 | if (sscanf(markmask, "%i", &a.mark) != 1) | |
2231 | return NULL; | |
2232 | } | |
2233 | ||
2234 | res = malloc(sizeof(*res)); | |
2235 | if (res) | |
2236 | memcpy(res, &a, sizeof(a)); | |
2237 | return res; | |
2238 | } | |
2239 | ||
2240 | static void proc_ctx_print(struct sockstat *s) | |
2241 | { | |
2242 | char *buf; | |
2243 | ||
2244 | if (show_proc_ctx || show_sock_ctx) { | |
2245 | if (find_entry(s->ino, &buf, | |
2246 | (show_proc_ctx & show_sock_ctx) ? | |
2247 | PROC_SOCK_CTX : PROC_CTX) > 0) { | |
2248 | out(" users:(%s)", buf); | |
2249 | free(buf); | |
2250 | } | |
2251 | } else if (show_users) { | |
2252 | if (find_entry(s->ino, &buf, USERS) > 0) { | |
2253 | out(" users:(%s)", buf); | |
2254 | free(buf); | |
2255 | } | |
2256 | } | |
2257 | } | |
2258 | ||
2259 | static void inet_stats_print(struct sockstat *s, bool v6only) | |
2260 | { | |
2261 | sock_state_print(s); | |
2262 | ||
2263 | inet_addr_print(&s->local, s->lport, s->iface, v6only); | |
2264 | inet_addr_print(&s->remote, s->rport, 0, v6only); | |
2265 | ||
2266 | proc_ctx_print(s); | |
2267 | } | |
2268 | ||
2269 | static int proc_parse_inet_addr(char *loc, char *rem, int family, struct | |
2270 | sockstat * s) | |
2271 | { | |
2272 | s->local.family = s->remote.family = family; | |
2273 | if (family == AF_INET) { | |
2274 | sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport); | |
2275 | sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport); | |
2276 | s->local.bytelen = s->remote.bytelen = 4; | |
2277 | return 0; | |
2278 | } else { | |
2279 | sscanf(loc, "%08x%08x%08x%08x:%x", | |
2280 | s->local.data, | |
2281 | s->local.data + 1, | |
2282 | s->local.data + 2, | |
2283 | s->local.data + 3, | |
2284 | &s->lport); | |
2285 | sscanf(rem, "%08x%08x%08x%08x:%x", | |
2286 | s->remote.data, | |
2287 | s->remote.data + 1, | |
2288 | s->remote.data + 2, | |
2289 | s->remote.data + 3, | |
2290 | &s->rport); | |
2291 | s->local.bytelen = s->remote.bytelen = 16; | |
2292 | return 0; | |
2293 | } | |
2294 | return -1; | |
2295 | } | |
2296 | ||
2297 | static int proc_inet_split_line(char *line, char **loc, char **rem, char **data) | |
2298 | { | |
2299 | char *p; | |
2300 | ||
2301 | if ((p = strchr(line, ':')) == NULL) | |
2302 | return -1; | |
2303 | ||
2304 | *loc = p+2; | |
2305 | if ((p = strchr(*loc, ':')) == NULL) | |
2306 | return -1; | |
2307 | ||
2308 | p[5] = 0; | |
2309 | *rem = p+6; | |
2310 | if ((p = strchr(*rem, ':')) == NULL) | |
2311 | return -1; | |
2312 | ||
2313 | p[5] = 0; | |
2314 | *data = p+6; | |
2315 | return 0; | |
2316 | } | |
2317 | ||
2318 | static char *sprint_bw(char *buf, double bw) | |
2319 | { | |
2320 | if (bw > 1000000.) | |
2321 | sprintf(buf, "%.1fM", bw / 1000000.); | |
2322 | else if (bw > 1000.) | |
2323 | sprintf(buf, "%.1fK", bw / 1000.); | |
2324 | else | |
2325 | sprintf(buf, "%g", bw); | |
2326 | ||
2327 | return buf; | |
2328 | } | |
2329 | ||
2330 | static void sctp_stats_print(struct sctp_info *s) | |
2331 | { | |
2332 | if (s->sctpi_tag) | |
2333 | out(" tag:%x", s->sctpi_tag); | |
2334 | if (s->sctpi_state) | |
2335 | out(" state:%s", sctp_sstate_name[s->sctpi_state]); | |
2336 | if (s->sctpi_rwnd) | |
2337 | out(" rwnd:%d", s->sctpi_rwnd); | |
2338 | if (s->sctpi_unackdata) | |
2339 | out(" unackdata:%d", s->sctpi_unackdata); | |
2340 | if (s->sctpi_penddata) | |
2341 | out(" penddata:%d", s->sctpi_penddata); | |
2342 | if (s->sctpi_instrms) | |
2343 | out(" instrms:%d", s->sctpi_instrms); | |
2344 | if (s->sctpi_outstrms) | |
2345 | out(" outstrms:%d", s->sctpi_outstrms); | |
2346 | if (s->sctpi_inqueue) | |
2347 | out(" inqueue:%d", s->sctpi_inqueue); | |
2348 | if (s->sctpi_outqueue) | |
2349 | out(" outqueue:%d", s->sctpi_outqueue); | |
2350 | if (s->sctpi_overall_error) | |
2351 | out(" overerr:%d", s->sctpi_overall_error); | |
2352 | if (s->sctpi_max_burst) | |
2353 | out(" maxburst:%d", s->sctpi_max_burst); | |
2354 | if (s->sctpi_maxseg) | |
2355 | out(" maxseg:%d", s->sctpi_maxseg); | |
2356 | if (s->sctpi_peer_rwnd) | |
2357 | out(" prwnd:%d", s->sctpi_peer_rwnd); | |
2358 | if (s->sctpi_peer_tag) | |
2359 | out(" ptag:%x", s->sctpi_peer_tag); | |
2360 | if (s->sctpi_peer_capable) | |
2361 | out(" pcapable:%d", s->sctpi_peer_capable); | |
2362 | if (s->sctpi_peer_sack) | |
2363 | out(" psack:%d", s->sctpi_peer_sack); | |
2364 | if (s->sctpi_s_autoclose) | |
2365 | out(" autoclose:%d", s->sctpi_s_autoclose); | |
2366 | if (s->sctpi_s_adaptation_ind) | |
2367 | out(" adapind:%d", s->sctpi_s_adaptation_ind); | |
2368 | if (s->sctpi_s_pd_point) | |
2369 | out(" pdpoint:%d", s->sctpi_s_pd_point); | |
2370 | if (s->sctpi_s_nodelay) | |
2371 | out(" nodealy:%d", s->sctpi_s_nodelay); | |
2372 | if (s->sctpi_s_disable_fragments) | |
2373 | out(" nofrag:%d", s->sctpi_s_disable_fragments); | |
2374 | if (s->sctpi_s_v4mapped) | |
2375 | out(" v4mapped:%d", s->sctpi_s_v4mapped); | |
2376 | if (s->sctpi_s_frag_interleave) | |
2377 | out(" fraginl:%d", s->sctpi_s_frag_interleave); | |
2378 | } | |
2379 | ||
2380 | static void tcp_stats_print(struct tcpstat *s) | |
2381 | { | |
2382 | char b1[64]; | |
2383 | ||
2384 | if (s->has_ts_opt) | |
2385 | out(" ts"); | |
2386 | if (s->has_sack_opt) | |
2387 | out(" sack"); | |
2388 | if (s->has_ecn_opt) | |
2389 | out(" ecn"); | |
2390 | if (s->has_ecnseen_opt) | |
2391 | out(" ecnseen"); | |
2392 | if (s->has_fastopen_opt) | |
2393 | out(" fastopen"); | |
2394 | if (s->cong_alg[0]) | |
2395 | out(" %s", s->cong_alg); | |
2396 | if (s->has_wscale_opt) | |
2397 | out(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale); | |
2398 | if (s->rto) | |
2399 | out(" rto:%g", s->rto); | |
2400 | if (s->backoff) | |
2401 | out(" backoff:%u", s->backoff); | |
2402 | if (s->rtt) | |
2403 | out(" rtt:%g/%g", s->rtt, s->rttvar); | |
2404 | if (s->ato) | |
2405 | out(" ato:%g", s->ato); | |
2406 | ||
2407 | if (s->qack) | |
2408 | out(" qack:%d", s->qack); | |
2409 | if (s->qack & 1) | |
2410 | out(" bidir"); | |
2411 | ||
2412 | if (s->mss) | |
2413 | out(" mss:%d", s->mss); | |
2414 | if (s->pmtu) | |
2415 | out(" pmtu:%u", s->pmtu); | |
2416 | if (s->rcv_mss) | |
2417 | out(" rcvmss:%d", s->rcv_mss); | |
2418 | if (s->advmss) | |
2419 | out(" advmss:%d", s->advmss); | |
2420 | if (s->cwnd) | |
2421 | out(" cwnd:%u", s->cwnd); | |
2422 | if (s->ssthresh) | |
2423 | out(" ssthresh:%d", s->ssthresh); | |
2424 | ||
2425 | if (s->bytes_acked) | |
2426 | out(" bytes_acked:%llu", s->bytes_acked); | |
2427 | if (s->bytes_received) | |
2428 | out(" bytes_received:%llu", s->bytes_received); | |
2429 | if (s->segs_out) | |
2430 | out(" segs_out:%u", s->segs_out); | |
2431 | if (s->segs_in) | |
2432 | out(" segs_in:%u", s->segs_in); | |
2433 | if (s->data_segs_out) | |
2434 | out(" data_segs_out:%u", s->data_segs_out); | |
2435 | if (s->data_segs_in) | |
2436 | out(" data_segs_in:%u", s->data_segs_in); | |
2437 | ||
2438 | if (s->dctcp && s->dctcp->enabled) { | |
2439 | struct dctcpstat *dctcp = s->dctcp; | |
2440 | ||
2441 | out(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)", | |
2442 | dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn, | |
2443 | dctcp->ab_tot); | |
2444 | } else if (s->dctcp) { | |
2445 | out(" dctcp:fallback_mode"); | |
2446 | } | |
2447 | ||
2448 | if (s->bbr_info) { | |
2449 | __u64 bw; | |
2450 | ||
2451 | bw = s->bbr_info->bbr_bw_hi; | |
2452 | bw <<= 32; | |
2453 | bw |= s->bbr_info->bbr_bw_lo; | |
2454 | ||
2455 | out(" bbr:(bw:%sbps,mrtt:%g", | |
2456 | sprint_bw(b1, bw * 8.0), | |
2457 | (double)s->bbr_info->bbr_min_rtt / 1000.0); | |
2458 | if (s->bbr_info->bbr_pacing_gain) | |
2459 | out(",pacing_gain:%g", | |
2460 | (double)s->bbr_info->bbr_pacing_gain / 256.0); | |
2461 | if (s->bbr_info->bbr_cwnd_gain) | |
2462 | out(",cwnd_gain:%g", | |
2463 | (double)s->bbr_info->bbr_cwnd_gain / 256.0); | |
2464 | out(")"); | |
2465 | } | |
2466 | ||
2467 | if (s->send_bps) | |
2468 | out(" send %sbps", sprint_bw(b1, s->send_bps)); | |
2469 | if (s->lastsnd) | |
2470 | out(" lastsnd:%u", s->lastsnd); | |
2471 | if (s->lastrcv) | |
2472 | out(" lastrcv:%u", s->lastrcv); | |
2473 | if (s->lastack) | |
2474 | out(" lastack:%u", s->lastack); | |
2475 | ||
2476 | if (s->pacing_rate) { | |
2477 | out(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate)); | |
2478 | if (s->pacing_rate_max) | |
2479 | out("/%sbps", sprint_bw(b1, s->pacing_rate_max)); | |
2480 | } | |
2481 | ||
2482 | if (s->delivery_rate) | |
2483 | out(" delivery_rate %sbps", sprint_bw(b1, s->delivery_rate)); | |
2484 | if (s->app_limited) | |
2485 | out(" app_limited"); | |
2486 | ||
2487 | if (s->busy_time) { | |
2488 | out(" busy:%llums", s->busy_time / 1000); | |
2489 | if (s->rwnd_limited) | |
2490 | out(" rwnd_limited:%llums(%.1f%%)", | |
2491 | s->rwnd_limited / 1000, | |
2492 | 100.0 * s->rwnd_limited / s->busy_time); | |
2493 | if (s->sndbuf_limited) | |
2494 | out(" sndbuf_limited:%llums(%.1f%%)", | |
2495 | s->sndbuf_limited / 1000, | |
2496 | 100.0 * s->sndbuf_limited / s->busy_time); | |
2497 | } | |
2498 | ||
2499 | if (s->unacked) | |
2500 | out(" unacked:%u", s->unacked); | |
2501 | if (s->retrans || s->retrans_total) | |
2502 | out(" retrans:%u/%u", s->retrans, s->retrans_total); | |
2503 | if (s->lost) | |
2504 | out(" lost:%u", s->lost); | |
2505 | if (s->sacked && s->ss.state != SS_LISTEN) | |
2506 | out(" sacked:%u", s->sacked); | |
2507 | if (s->fackets) | |
2508 | out(" fackets:%u", s->fackets); | |
2509 | if (s->reordering != 3) | |
2510 | out(" reordering:%d", s->reordering); | |
2511 | if (s->rcv_rtt) | |
2512 | out(" rcv_rtt:%g", s->rcv_rtt); | |
2513 | if (s->rcv_space) | |
2514 | out(" rcv_space:%d", s->rcv_space); | |
2515 | if (s->rcv_ssthresh) | |
2516 | out(" rcv_ssthresh:%u", s->rcv_ssthresh); | |
2517 | if (s->not_sent) | |
2518 | out(" notsent:%u", s->not_sent); | |
2519 | if (s->min_rtt) | |
2520 | out(" minrtt:%g", s->min_rtt); | |
2521 | } | |
2522 | ||
2523 | static void tcp_timer_print(struct tcpstat *s) | |
2524 | { | |
2525 | static const char * const tmr_name[] = { | |
2526 | "off", | |
2527 | "on", | |
2528 | "keepalive", | |
2529 | "timewait", | |
2530 | "persist", | |
2531 | "unknown" | |
2532 | }; | |
2533 | ||
2534 | if (s->timer) { | |
2535 | if (s->timer > 4) | |
2536 | s->timer = 5; | |
2537 | out(" timer:(%s,%s,%d)", | |
2538 | tmr_name[s->timer], | |
2539 | print_ms_timer(s->timeout), | |
2540 | s->retrans); | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | static void sctp_timer_print(struct tcpstat *s) | |
2545 | { | |
2546 | if (s->timer) | |
2547 | out(" timer:(T3_RTX,%s,%d)", | |
2548 | print_ms_timer(s->timeout), s->retrans); | |
2549 | } | |
2550 | ||
2551 | static int tcp_show_line(char *line, const struct filter *f, int family) | |
2552 | { | |
2553 | int rto = 0, ato = 0; | |
2554 | struct tcpstat s = {}; | |
2555 | char *loc, *rem, *data; | |
2556 | char opt[256]; | |
2557 | int n; | |
2558 | int hz = get_user_hz(); | |
2559 | ||
2560 | if (proc_inet_split_line(line, &loc, &rem, &data)) | |
2561 | return -1; | |
2562 | ||
2563 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
2564 | ||
2565 | if (!(f->states & (1 << state))) | |
2566 | return 0; | |
2567 | ||
2568 | proc_parse_inet_addr(loc, rem, family, &s.ss); | |
2569 | ||
2570 | if (f->f && run_ssfilter(f->f, &s.ss) == 0) | |
2571 | return 0; | |
2572 | ||
2573 | opt[0] = 0; | |
2574 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %u %d %[^\n]\n", | |
2575 | &s.ss.state, &s.ss.wq, &s.ss.rq, | |
2576 | &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, | |
2577 | &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, | |
2578 | &s.ssthresh, opt); | |
2579 | ||
2580 | if (n < 17) | |
2581 | opt[0] = 0; | |
2582 | ||
2583 | if (n < 12) { | |
2584 | rto = 0; | |
2585 | s.cwnd = 2; | |
2586 | s.ssthresh = -1; | |
2587 | ato = s.qack = 0; | |
2588 | } | |
2589 | ||
2590 | s.retrans = s.timer != 1 ? s.probes : s.retrans; | |
2591 | s.timeout = (s.timeout * 1000 + hz - 1) / hz; | |
2592 | s.ato = (double)ato / hz; | |
2593 | s.qack /= 2; | |
2594 | s.rto = (double)rto; | |
2595 | s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; | |
2596 | s.rto = s.rto != 3 * hz ? s.rto / hz : 0; | |
2597 | s.ss.type = IPPROTO_TCP; | |
2598 | ||
2599 | inet_stats_print(&s.ss, false); | |
2600 | ||
2601 | if (show_options) | |
2602 | tcp_timer_print(&s); | |
2603 | ||
2604 | if (show_details) { | |
2605 | sock_details_print(&s.ss); | |
2606 | if (opt[0]) | |
2607 | out(" opt:\"%s\"", opt); | |
2608 | } | |
2609 | ||
2610 | if (show_tcpinfo) | |
2611 | tcp_stats_print(&s); | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | static int generic_record_read(FILE *fp, | |
2617 | int (*worker)(char*, const struct filter *, int), | |
2618 | const struct filter *f, int fam) | |
2619 | { | |
2620 | char line[256]; | |
2621 | ||
2622 | /* skip header */ | |
2623 | if (fgets(line, sizeof(line), fp) == NULL) | |
2624 | goto outerr; | |
2625 | ||
2626 | while (fgets(line, sizeof(line), fp) != NULL) { | |
2627 | int n = strlen(line); | |
2628 | ||
2629 | if (n == 0 || line[n-1] != '\n') { | |
2630 | errno = -EINVAL; | |
2631 | return -1; | |
2632 | } | |
2633 | line[n-1] = 0; | |
2634 | ||
2635 | if (worker(line, f, fam) < 0) | |
2636 | return 0; | |
2637 | } | |
2638 | outerr: | |
2639 | ||
2640 | return ferror(fp) ? -1 : 0; | |
2641 | } | |
2642 | ||
2643 | static void print_skmeminfo(struct rtattr *tb[], int attrtype) | |
2644 | { | |
2645 | const __u32 *skmeminfo; | |
2646 | ||
2647 | if (!tb[attrtype]) { | |
2648 | if (attrtype == INET_DIAG_SKMEMINFO) { | |
2649 | if (!tb[INET_DIAG_MEMINFO]) | |
2650 | return; | |
2651 | ||
2652 | const struct inet_diag_meminfo *minfo = | |
2653 | RTA_DATA(tb[INET_DIAG_MEMINFO]); | |
2654 | ||
2655 | out(" mem:(r%u,w%u,f%u,t%u)", | |
2656 | minfo->idiag_rmem, | |
2657 | minfo->idiag_wmem, | |
2658 | minfo->idiag_fmem, | |
2659 | minfo->idiag_tmem); | |
2660 | } | |
2661 | return; | |
2662 | } | |
2663 | ||
2664 | skmeminfo = RTA_DATA(tb[attrtype]); | |
2665 | ||
2666 | out(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u", | |
2667 | skmeminfo[SK_MEMINFO_RMEM_ALLOC], | |
2668 | skmeminfo[SK_MEMINFO_RCVBUF], | |
2669 | skmeminfo[SK_MEMINFO_WMEM_ALLOC], | |
2670 | skmeminfo[SK_MEMINFO_SNDBUF], | |
2671 | skmeminfo[SK_MEMINFO_FWD_ALLOC], | |
2672 | skmeminfo[SK_MEMINFO_WMEM_QUEUED], | |
2673 | skmeminfo[SK_MEMINFO_OPTMEM]); | |
2674 | ||
2675 | if (RTA_PAYLOAD(tb[attrtype]) >= | |
2676 | (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32)) | |
2677 | out(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]); | |
2678 | ||
2679 | if (RTA_PAYLOAD(tb[attrtype]) >= | |
2680 | (SK_MEMINFO_DROPS + 1) * sizeof(__u32)) | |
2681 | out(",d%u", skmeminfo[SK_MEMINFO_DROPS]); | |
2682 | ||
2683 | out(")"); | |
2684 | } | |
2685 | ||
2686 | static void print_md5sig(struct tcp_diag_md5sig *sig) | |
2687 | { | |
2688 | out("%s/%d=", | |
2689 | format_host(sig->tcpm_family, | |
2690 | sig->tcpm_family == AF_INET6 ? 16 : 4, | |
2691 | &sig->tcpm_addr), | |
2692 | sig->tcpm_prefixlen); | |
2693 | print_escape_buf(sig->tcpm_key, sig->tcpm_keylen, " ,"); | |
2694 | } | |
2695 | ||
2696 | #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt)) | |
2697 | ||
2698 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, | |
2699 | struct rtattr *tb[]) | |
2700 | { | |
2701 | double rtt = 0; | |
2702 | struct tcpstat s = {}; | |
2703 | ||
2704 | s.ss.state = r->idiag_state; | |
2705 | ||
2706 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); | |
2707 | ||
2708 | if (tb[INET_DIAG_INFO]) { | |
2709 | struct tcp_info *info; | |
2710 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); | |
2711 | ||
2712 | /* workaround for older kernels with less fields */ | |
2713 | if (len < sizeof(*info)) { | |
2714 | info = alloca(sizeof(*info)); | |
2715 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); | |
2716 | memset((char *)info + len, 0, sizeof(*info) - len); | |
2717 | } else | |
2718 | info = RTA_DATA(tb[INET_DIAG_INFO]); | |
2719 | ||
2720 | if (show_options) { | |
2721 | s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS); | |
2722 | s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK); | |
2723 | s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN); | |
2724 | s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN); | |
2725 | s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA); | |
2726 | } | |
2727 | ||
2728 | if (tb[INET_DIAG_CONG]) | |
2729 | strncpy(s.cong_alg, | |
2730 | rta_getattr_str(tb[INET_DIAG_CONG]), | |
2731 | sizeof(s.cong_alg) - 1); | |
2732 | ||
2733 | if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) { | |
2734 | s.has_wscale_opt = true; | |
2735 | s.snd_wscale = info->tcpi_snd_wscale; | |
2736 | s.rcv_wscale = info->tcpi_rcv_wscale; | |
2737 | } | |
2738 | ||
2739 | if (info->tcpi_rto && info->tcpi_rto != 3000000) | |
2740 | s.rto = (double)info->tcpi_rto / 1000; | |
2741 | ||
2742 | s.backoff = info->tcpi_backoff; | |
2743 | s.rtt = (double)info->tcpi_rtt / 1000; | |
2744 | s.rttvar = (double)info->tcpi_rttvar / 1000; | |
2745 | s.ato = (double)info->tcpi_ato / 1000; | |
2746 | s.mss = info->tcpi_snd_mss; | |
2747 | s.rcv_mss = info->tcpi_rcv_mss; | |
2748 | s.advmss = info->tcpi_advmss; | |
2749 | s.rcv_space = info->tcpi_rcv_space; | |
2750 | s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000; | |
2751 | s.lastsnd = info->tcpi_last_data_sent; | |
2752 | s.lastrcv = info->tcpi_last_data_recv; | |
2753 | s.lastack = info->tcpi_last_ack_recv; | |
2754 | s.unacked = info->tcpi_unacked; | |
2755 | s.retrans = info->tcpi_retrans; | |
2756 | s.retrans_total = info->tcpi_total_retrans; | |
2757 | s.lost = info->tcpi_lost; | |
2758 | s.sacked = info->tcpi_sacked; | |
2759 | s.fackets = info->tcpi_fackets; | |
2760 | s.reordering = info->tcpi_reordering; | |
2761 | s.rcv_ssthresh = info->tcpi_rcv_ssthresh; | |
2762 | s.cwnd = info->tcpi_snd_cwnd; | |
2763 | s.pmtu = info->tcpi_pmtu; | |
2764 | ||
2765 | if (info->tcpi_snd_ssthresh < 0xFFFF) | |
2766 | s.ssthresh = info->tcpi_snd_ssthresh; | |
2767 | ||
2768 | rtt = (double) info->tcpi_rtt; | |
2769 | if (tb[INET_DIAG_VEGASINFO]) { | |
2770 | const struct tcpvegas_info *vinfo | |
2771 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); | |
2772 | ||
2773 | if (vinfo->tcpv_enabled && | |
2774 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) | |
2775 | rtt = vinfo->tcpv_rtt; | |
2776 | } | |
2777 | ||
2778 | if (tb[INET_DIAG_DCTCPINFO]) { | |
2779 | struct dctcpstat *dctcp = malloc(sizeof(struct | |
2780 | dctcpstat)); | |
2781 | ||
2782 | const struct tcp_dctcp_info *dinfo | |
2783 | = RTA_DATA(tb[INET_DIAG_DCTCPINFO]); | |
2784 | ||
2785 | dctcp->enabled = !!dinfo->dctcp_enabled; | |
2786 | dctcp->ce_state = dinfo->dctcp_ce_state; | |
2787 | dctcp->alpha = dinfo->dctcp_alpha; | |
2788 | dctcp->ab_ecn = dinfo->dctcp_ab_ecn; | |
2789 | dctcp->ab_tot = dinfo->dctcp_ab_tot; | |
2790 | s.dctcp = dctcp; | |
2791 | } | |
2792 | ||
2793 | if (tb[INET_DIAG_BBRINFO]) { | |
2794 | const void *bbr_info = RTA_DATA(tb[INET_DIAG_BBRINFO]); | |
2795 | int len = min(RTA_PAYLOAD(tb[INET_DIAG_BBRINFO]), | |
2796 | sizeof(*s.bbr_info)); | |
2797 | ||
2798 | s.bbr_info = calloc(1, sizeof(*s.bbr_info)); | |
2799 | if (s.bbr_info && bbr_info) | |
2800 | memcpy(s.bbr_info, bbr_info, len); | |
2801 | } | |
2802 | ||
2803 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { | |
2804 | s.send_bps = (double) info->tcpi_snd_cwnd * | |
2805 | (double)info->tcpi_snd_mss * 8000000. / rtt; | |
2806 | } | |
2807 | ||
2808 | if (info->tcpi_pacing_rate && | |
2809 | info->tcpi_pacing_rate != ~0ULL) { | |
2810 | s.pacing_rate = info->tcpi_pacing_rate * 8.; | |
2811 | ||
2812 | if (info->tcpi_max_pacing_rate && | |
2813 | info->tcpi_max_pacing_rate != ~0ULL) | |
2814 | s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.; | |
2815 | } | |
2816 | s.bytes_acked = info->tcpi_bytes_acked; | |
2817 | s.bytes_received = info->tcpi_bytes_received; | |
2818 | s.segs_out = info->tcpi_segs_out; | |
2819 | s.segs_in = info->tcpi_segs_in; | |
2820 | s.data_segs_out = info->tcpi_data_segs_out; | |
2821 | s.data_segs_in = info->tcpi_data_segs_in; | |
2822 | s.not_sent = info->tcpi_notsent_bytes; | |
2823 | if (info->tcpi_min_rtt && info->tcpi_min_rtt != ~0U) | |
2824 | s.min_rtt = (double) info->tcpi_min_rtt / 1000; | |
2825 | s.delivery_rate = info->tcpi_delivery_rate * 8.; | |
2826 | s.app_limited = info->tcpi_delivery_rate_app_limited; | |
2827 | s.busy_time = info->tcpi_busy_time; | |
2828 | s.rwnd_limited = info->tcpi_rwnd_limited; | |
2829 | s.sndbuf_limited = info->tcpi_sndbuf_limited; | |
2830 | tcp_stats_print(&s); | |
2831 | free(s.dctcp); | |
2832 | free(s.bbr_info); | |
2833 | } | |
2834 | if (tb[INET_DIAG_MD5SIG]) { | |
2835 | struct tcp_diag_md5sig *sig = RTA_DATA(tb[INET_DIAG_MD5SIG]); | |
2836 | int len = RTA_PAYLOAD(tb[INET_DIAG_MD5SIG]); | |
2837 | ||
2838 | out(" md5keys:"); | |
2839 | print_md5sig(sig++); | |
2840 | for (len -= sizeof(*sig); len > 0; len -= sizeof(*sig)) { | |
2841 | out(","); | |
2842 | print_md5sig(sig++); | |
2843 | } | |
2844 | } | |
2845 | } | |
2846 | ||
2847 | static const char *format_host_sa(struct sockaddr_storage *sa) | |
2848 | { | |
2849 | union { | |
2850 | struct sockaddr_in sin; | |
2851 | struct sockaddr_in6 sin6; | |
2852 | } *saddr = (void *)sa; | |
2853 | ||
2854 | switch (sa->ss_family) { | |
2855 | case AF_INET: | |
2856 | return format_host(AF_INET, 4, &saddr->sin.sin_addr); | |
2857 | case AF_INET6: | |
2858 | return format_host(AF_INET6, 16, &saddr->sin6.sin6_addr); | |
2859 | default: | |
2860 | return ""; | |
2861 | } | |
2862 | } | |
2863 | ||
2864 | static void sctp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, | |
2865 | struct rtattr *tb[]) | |
2866 | { | |
2867 | struct sockaddr_storage *sa; | |
2868 | int len; | |
2869 | ||
2870 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); | |
2871 | ||
2872 | if (tb[INET_DIAG_LOCALS]) { | |
2873 | len = RTA_PAYLOAD(tb[INET_DIAG_LOCALS]); | |
2874 | sa = RTA_DATA(tb[INET_DIAG_LOCALS]); | |
2875 | ||
2876 | out("locals:%s", format_host_sa(sa)); | |
2877 | for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) | |
2878 | out(",%s", format_host_sa(sa)); | |
2879 | ||
2880 | } | |
2881 | if (tb[INET_DIAG_PEERS]) { | |
2882 | len = RTA_PAYLOAD(tb[INET_DIAG_PEERS]); | |
2883 | sa = RTA_DATA(tb[INET_DIAG_PEERS]); | |
2884 | ||
2885 | out(" peers:%s", format_host_sa(sa)); | |
2886 | for (sa++, len -= sizeof(*sa); len > 0; sa++, len -= sizeof(*sa)) | |
2887 | out(",%s", format_host_sa(sa)); | |
2888 | } | |
2889 | if (tb[INET_DIAG_INFO]) { | |
2890 | struct sctp_info *info; | |
2891 | len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); | |
2892 | ||
2893 | /* workaround for older kernels with less fields */ | |
2894 | if (len < sizeof(*info)) { | |
2895 | info = alloca(sizeof(*info)); | |
2896 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); | |
2897 | memset((char *)info + len, 0, sizeof(*info) - len); | |
2898 | } else | |
2899 | info = RTA_DATA(tb[INET_DIAG_INFO]); | |
2900 | ||
2901 | sctp_stats_print(info); | |
2902 | } | |
2903 | } | |
2904 | ||
2905 | static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s) | |
2906 | { | |
2907 | struct rtattr *tb[INET_DIAG_MAX+1]; | |
2908 | struct inet_diag_msg *r = NLMSG_DATA(nlh); | |
2909 | ||
2910 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), | |
2911 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
2912 | ||
2913 | s->state = r->idiag_state; | |
2914 | s->local.family = s->remote.family = r->idiag_family; | |
2915 | s->lport = ntohs(r->id.idiag_sport); | |
2916 | s->rport = ntohs(r->id.idiag_dport); | |
2917 | s->wq = r->idiag_wqueue; | |
2918 | s->rq = r->idiag_rqueue; | |
2919 | s->ino = r->idiag_inode; | |
2920 | s->uid = r->idiag_uid; | |
2921 | s->iface = r->id.idiag_if; | |
2922 | s->sk = cookie_sk_get(&r->id.idiag_cookie[0]); | |
2923 | ||
2924 | s->mark = 0; | |
2925 | if (tb[INET_DIAG_MARK]) | |
2926 | s->mark = rta_getattr_u32(tb[INET_DIAG_MARK]); | |
2927 | if (tb[INET_DIAG_PROTOCOL]) | |
2928 | s->raw_prot = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); | |
2929 | else | |
2930 | s->raw_prot = 0; | |
2931 | ||
2932 | if (s->local.family == AF_INET) | |
2933 | s->local.bytelen = s->remote.bytelen = 4; | |
2934 | else | |
2935 | s->local.bytelen = s->remote.bytelen = 16; | |
2936 | ||
2937 | memcpy(s->local.data, r->id.idiag_src, s->local.bytelen); | |
2938 | memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen); | |
2939 | } | |
2940 | ||
2941 | static int inet_show_sock(struct nlmsghdr *nlh, | |
2942 | struct sockstat *s) | |
2943 | { | |
2944 | struct rtattr *tb[INET_DIAG_MAX+1]; | |
2945 | struct inet_diag_msg *r = NLMSG_DATA(nlh); | |
2946 | unsigned char v6only = 0; | |
2947 | ||
2948 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1), | |
2949 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
2950 | ||
2951 | if (tb[INET_DIAG_PROTOCOL]) | |
2952 | s->type = rta_getattr_u8(tb[INET_DIAG_PROTOCOL]); | |
2953 | ||
2954 | if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) | |
2955 | v6only = rta_getattr_u8(tb[INET_DIAG_SKV6ONLY]); | |
2956 | ||
2957 | inet_stats_print(s, v6only); | |
2958 | ||
2959 | if (show_options) { | |
2960 | struct tcpstat t = {}; | |
2961 | ||
2962 | t.timer = r->idiag_timer; | |
2963 | t.timeout = r->idiag_expires; | |
2964 | t.retrans = r->idiag_retrans; | |
2965 | if (s->type == IPPROTO_SCTP) | |
2966 | sctp_timer_print(&t); | |
2967 | else | |
2968 | tcp_timer_print(&t); | |
2969 | } | |
2970 | ||
2971 | if (show_details) { | |
2972 | sock_details_print(s); | |
2973 | if (s->local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) | |
2974 | out(" v6only:%u", v6only); | |
2975 | ||
2976 | if (tb[INET_DIAG_SHUTDOWN]) { | |
2977 | unsigned char mask; | |
2978 | ||
2979 | mask = rta_getattr_u8(tb[INET_DIAG_SHUTDOWN]); | |
2980 | out(" %c-%c", | |
2981 | mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
2982 | } | |
2983 | } | |
2984 | ||
2985 | if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) { | |
2986 | out("\n\t"); | |
2987 | if (s->type == IPPROTO_SCTP) | |
2988 | sctp_show_info(nlh, r, tb); | |
2989 | else | |
2990 | tcp_show_info(nlh, r, tb); | |
2991 | } | |
2992 | sctp_ino = s->ino; | |
2993 | ||
2994 | return 0; | |
2995 | } | |
2996 | ||
2997 | static int tcpdiag_send(int fd, int protocol, struct filter *f) | |
2998 | { | |
2999 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; | |
3000 | struct { | |
3001 | struct nlmsghdr nlh; | |
3002 | struct inet_diag_req r; | |
3003 | } req = { | |
3004 | .nlh.nlmsg_len = sizeof(req), | |
3005 | .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST, | |
3006 | .nlh.nlmsg_seq = MAGIC_SEQ, | |
3007 | .r.idiag_family = AF_INET, | |
3008 | .r.idiag_states = f->states, | |
3009 | }; | |
3010 | char *bc = NULL; | |
3011 | int bclen; | |
3012 | struct msghdr msg; | |
3013 | struct rtattr rta; | |
3014 | struct iovec iov[3]; | |
3015 | int iovlen = 1; | |
3016 | ||
3017 | if (protocol == IPPROTO_UDP) | |
3018 | return -1; | |
3019 | ||
3020 | if (protocol == IPPROTO_TCP) | |
3021 | req.nlh.nlmsg_type = TCPDIAG_GETSOCK; | |
3022 | else | |
3023 | req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; | |
3024 | if (show_mem) { | |
3025 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); | |
3026 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); | |
3027 | } | |
3028 | ||
3029 | if (show_tcpinfo) { | |
3030 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); | |
3031 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
3032 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
3033 | } | |
3034 | ||
3035 | iov[0] = (struct iovec){ | |
3036 | .iov_base = &req, | |
3037 | .iov_len = sizeof(req) | |
3038 | }; | |
3039 | if (f->f) { | |
3040 | bclen = ssfilter_bytecompile(f->f, &bc); | |
3041 | if (bclen) { | |
3042 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
3043 | rta.rta_len = RTA_LENGTH(bclen); | |
3044 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
3045 | iov[2] = (struct iovec){ bc, bclen }; | |
3046 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
3047 | iovlen = 3; | |
3048 | } | |
3049 | } | |
3050 | ||
3051 | msg = (struct msghdr) { | |
3052 | .msg_name = (void *)&nladdr, | |
3053 | .msg_namelen = sizeof(nladdr), | |
3054 | .msg_iov = iov, | |
3055 | .msg_iovlen = iovlen, | |
3056 | }; | |
3057 | ||
3058 | if (sendmsg(fd, &msg, 0) < 0) { | |
3059 | close(fd); | |
3060 | return -1; | |
3061 | } | |
3062 | ||
3063 | return 0; | |
3064 | } | |
3065 | ||
3066 | static int sockdiag_send(int family, int fd, int protocol, struct filter *f) | |
3067 | { | |
3068 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; | |
3069 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); | |
3070 | char *bc = NULL; | |
3071 | int bclen; | |
3072 | struct msghdr msg; | |
3073 | struct rtattr rta; | |
3074 | struct iovec iov[3]; | |
3075 | int iovlen = 1; | |
3076 | ||
3077 | if (family == PF_UNSPEC) | |
3078 | return tcpdiag_send(fd, protocol, f); | |
3079 | ||
3080 | memset(&req.r, 0, sizeof(req.r)); | |
3081 | req.r.sdiag_family = family; | |
3082 | req.r.sdiag_protocol = protocol; | |
3083 | req.r.idiag_states = f->states; | |
3084 | if (show_mem) { | |
3085 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); | |
3086 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); | |
3087 | } | |
3088 | ||
3089 | if (show_tcpinfo) { | |
3090 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); | |
3091 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); | |
3092 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); | |
3093 | } | |
3094 | ||
3095 | iov[0] = (struct iovec){ | |
3096 | .iov_base = &req, | |
3097 | .iov_len = sizeof(req) | |
3098 | }; | |
3099 | if (f->f) { | |
3100 | bclen = ssfilter_bytecompile(f->f, &bc); | |
3101 | if (bclen) { | |
3102 | rta.rta_type = INET_DIAG_REQ_BYTECODE; | |
3103 | rta.rta_len = RTA_LENGTH(bclen); | |
3104 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; | |
3105 | iov[2] = (struct iovec){ bc, bclen }; | |
3106 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); | |
3107 | iovlen = 3; | |
3108 | } | |
3109 | } | |
3110 | ||
3111 | msg = (struct msghdr) { | |
3112 | .msg_name = (void *)&nladdr, | |
3113 | .msg_namelen = sizeof(nladdr), | |
3114 | .msg_iov = iov, | |
3115 | .msg_iovlen = iovlen, | |
3116 | }; | |
3117 | ||
3118 | if (sendmsg(fd, &msg, 0) < 0) { | |
3119 | close(fd); | |
3120 | return -1; | |
3121 | } | |
3122 | ||
3123 | return 0; | |
3124 | } | |
3125 | ||
3126 | struct inet_diag_arg { | |
3127 | struct filter *f; | |
3128 | int protocol; | |
3129 | struct rtnl_handle *rth; | |
3130 | }; | |
3131 | ||
3132 | static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s) | |
3133 | { | |
3134 | struct inet_diag_msg *d = NLMSG_DATA(h); | |
3135 | struct inet_diag_arg *diag_arg = arg; | |
3136 | struct rtnl_handle *rth = diag_arg->rth; | |
3137 | ||
3138 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); | |
3139 | ||
3140 | req.nlh.nlmsg_type = SOCK_DESTROY; | |
3141 | req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; | |
3142 | req.nlh.nlmsg_seq = ++rth->seq; | |
3143 | req.r.sdiag_family = d->idiag_family; | |
3144 | req.r.sdiag_protocol = diag_arg->protocol; | |
3145 | req.r.id = d->id; | |
3146 | ||
3147 | if (diag_arg->protocol == IPPROTO_RAW) { | |
3148 | struct inet_diag_req_raw *raw = (void *)&req.r; | |
3149 | ||
3150 | BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw)); | |
3151 | raw->sdiag_raw_protocol = s->raw_prot; | |
3152 | } | |
3153 | ||
3154 | return rtnl_talk(rth, &req.nlh, NULL); | |
3155 | } | |
3156 | ||
3157 | static int show_one_inet_sock(const struct sockaddr_nl *addr, | |
3158 | struct nlmsghdr *h, void *arg) | |
3159 | { | |
3160 | int err; | |
3161 | struct inet_diag_arg *diag_arg = arg; | |
3162 | struct inet_diag_msg *r = NLMSG_DATA(h); | |
3163 | struct sockstat s = {}; | |
3164 | ||
3165 | if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family))) | |
3166 | return 0; | |
3167 | ||
3168 | parse_diag_msg(h, &s); | |
3169 | s.type = diag_arg->protocol; | |
3170 | ||
3171 | if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0) | |
3172 | return 0; | |
3173 | ||
3174 | if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) { | |
3175 | if (errno == EOPNOTSUPP || errno == ENOENT) { | |
3176 | /* Socket can't be closed, or is already closed. */ | |
3177 | return 0; | |
3178 | } else { | |
3179 | perror("SOCK_DESTROY answers"); | |
3180 | return -1; | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | err = inet_show_sock(h, &s); | |
3185 | if (err < 0) | |
3186 | return err; | |
3187 | ||
3188 | return 0; | |
3189 | } | |
3190 | ||
3191 | static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) | |
3192 | { | |
3193 | int err = 0; | |
3194 | struct rtnl_handle rth, rth2; | |
3195 | int family = PF_INET; | |
3196 | struct inet_diag_arg arg = { .f = f, .protocol = protocol }; | |
3197 | ||
3198 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) | |
3199 | return -1; | |
3200 | ||
3201 | if (f->kill) { | |
3202 | if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) { | |
3203 | rtnl_close(&rth); | |
3204 | return -1; | |
3205 | } | |
3206 | arg.rth = &rth2; | |
3207 | } | |
3208 | ||
3209 | rth.dump = MAGIC_SEQ; | |
3210 | rth.dump_fp = dump_fp; | |
3211 | if (preferred_family == PF_INET6) | |
3212 | family = PF_INET6; | |
3213 | ||
3214 | again: | |
3215 | if ((err = sockdiag_send(family, rth.fd, protocol, f))) | |
3216 | goto Exit; | |
3217 | ||
3218 | if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) { | |
3219 | if (family != PF_UNSPEC) { | |
3220 | family = PF_UNSPEC; | |
3221 | goto again; | |
3222 | } | |
3223 | goto Exit; | |
3224 | } | |
3225 | if (family == PF_INET && preferred_family != PF_INET) { | |
3226 | family = PF_INET6; | |
3227 | goto again; | |
3228 | } | |
3229 | ||
3230 | Exit: | |
3231 | rtnl_close(&rth); | |
3232 | if (arg.rth) | |
3233 | rtnl_close(arg.rth); | |
3234 | return err; | |
3235 | } | |
3236 | ||
3237 | static int tcp_show_netlink_file(struct filter *f) | |
3238 | { | |
3239 | FILE *fp; | |
3240 | char buf[16384]; | |
3241 | int err = -1; | |
3242 | ||
3243 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { | |
3244 | perror("fopen($TCPDIAG_FILE)"); | |
3245 | return err; | |
3246 | } | |
3247 | ||
3248 | while (1) { | |
3249 | int status, err2; | |
3250 | struct nlmsghdr *h = (struct nlmsghdr *)buf; | |
3251 | struct sockstat s = {}; | |
3252 | ||
3253 | status = fread(buf, 1, sizeof(*h), fp); | |
3254 | if (status < 0) { | |
3255 | perror("Reading header from $TCPDIAG_FILE"); | |
3256 | break; | |
3257 | } | |
3258 | if (status != sizeof(*h)) { | |
3259 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
3260 | break; | |
3261 | } | |
3262 | ||
3263 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); | |
3264 | ||
3265 | if (status < 0) { | |
3266 | perror("Reading $TCPDIAG_FILE"); | |
3267 | break; | |
3268 | } | |
3269 | if (status + sizeof(*h) < h->nlmsg_len) { | |
3270 | perror("Unexpected EOF reading $TCPDIAG_FILE"); | |
3271 | break; | |
3272 | } | |
3273 | ||
3274 | /* The only legal exit point */ | |
3275 | if (h->nlmsg_type == NLMSG_DONE) { | |
3276 | err = 0; | |
3277 | break; | |
3278 | } | |
3279 | ||
3280 | if (h->nlmsg_type == NLMSG_ERROR) { | |
3281 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); | |
3282 | ||
3283 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
3284 | fprintf(stderr, "ERROR truncated\n"); | |
3285 | } else { | |
3286 | errno = -err->error; | |
3287 | perror("TCPDIAG answered"); | |
3288 | } | |
3289 | break; | |
3290 | } | |
3291 | ||
3292 | parse_diag_msg(h, &s); | |
3293 | s.type = IPPROTO_TCP; | |
3294 | ||
3295 | if (f && f->f && run_ssfilter(f->f, &s) == 0) | |
3296 | continue; | |
3297 | ||
3298 | err2 = inet_show_sock(h, &s); | |
3299 | if (err2 < 0) { | |
3300 | err = err2; | |
3301 | break; | |
3302 | } | |
3303 | } | |
3304 | ||
3305 | fclose(fp); | |
3306 | return err; | |
3307 | } | |
3308 | ||
3309 | static int tcp_show(struct filter *f) | |
3310 | { | |
3311 | FILE *fp = NULL; | |
3312 | char *buf = NULL; | |
3313 | int bufsize = 1024*1024; | |
3314 | ||
3315 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3316 | return 0; | |
3317 | ||
3318 | dg_proto = TCP_PROTO; | |
3319 | ||
3320 | if (getenv("TCPDIAG_FILE")) | |
3321 | return tcp_show_netlink_file(f); | |
3322 | ||
3323 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") | |
3324 | && inet_show_netlink(f, NULL, IPPROTO_TCP) == 0) | |
3325 | return 0; | |
3326 | ||
3327 | /* Sigh... We have to parse /proc/net/tcp... */ | |
3328 | while (bufsize >= 64*1024) { | |
3329 | if ((buf = malloc(bufsize)) != NULL) | |
3330 | break; | |
3331 | bufsize /= 2; | |
3332 | } | |
3333 | if (buf == NULL) { | |
3334 | errno = ENOMEM; | |
3335 | return -1; | |
3336 | } | |
3337 | ||
3338 | if (f->families & FAMILY_MASK(AF_INET)) { | |
3339 | if ((fp = net_tcp_open()) == NULL) | |
3340 | goto outerr; | |
3341 | ||
3342 | setbuffer(fp, buf, bufsize); | |
3343 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) | |
3344 | goto outerr; | |
3345 | fclose(fp); | |
3346 | } | |
3347 | ||
3348 | if ((f->families & FAMILY_MASK(AF_INET6)) && | |
3349 | (fp = net_tcp6_open()) != NULL) { | |
3350 | setbuffer(fp, buf, bufsize); | |
3351 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) | |
3352 | goto outerr; | |
3353 | fclose(fp); | |
3354 | } | |
3355 | ||
3356 | free(buf); | |
3357 | return 0; | |
3358 | ||
3359 | outerr: | |
3360 | do { | |
3361 | int saved_errno = errno; | |
3362 | ||
3363 | free(buf); | |
3364 | if (fp) | |
3365 | fclose(fp); | |
3366 | errno = saved_errno; | |
3367 | return -1; | |
3368 | } while (0); | |
3369 | } | |
3370 | ||
3371 | static int dccp_show(struct filter *f) | |
3372 | { | |
3373 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3374 | return 0; | |
3375 | ||
3376 | if (!getenv("PROC_NET_DCCP") && !getenv("PROC_ROOT") | |
3377 | && inet_show_netlink(f, NULL, IPPROTO_DCCP) == 0) | |
3378 | return 0; | |
3379 | ||
3380 | return 0; | |
3381 | } | |
3382 | ||
3383 | static int sctp_show(struct filter *f) | |
3384 | { | |
3385 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3386 | return 0; | |
3387 | ||
3388 | if (!getenv("PROC_NET_SCTP") && !getenv("PROC_ROOT") | |
3389 | && inet_show_netlink(f, NULL, IPPROTO_SCTP) == 0) | |
3390 | return 0; | |
3391 | ||
3392 | return 0; | |
3393 | } | |
3394 | ||
3395 | static int dgram_show_line(char *line, const struct filter *f, int family) | |
3396 | { | |
3397 | struct sockstat s = {}; | |
3398 | char *loc, *rem, *data; | |
3399 | char opt[256]; | |
3400 | int n; | |
3401 | ||
3402 | if (proc_inet_split_line(line, &loc, &rem, &data)) | |
3403 | return -1; | |
3404 | ||
3405 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); | |
3406 | ||
3407 | if (!(f->states & (1 << state))) | |
3408 | return 0; | |
3409 | ||
3410 | proc_parse_inet_addr(loc, rem, family, &s); | |
3411 | ||
3412 | if (f->f && run_ssfilter(f->f, &s) == 0) | |
3413 | return 0; | |
3414 | ||
3415 | opt[0] = 0; | |
3416 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", | |
3417 | &s.state, &s.wq, &s.rq, | |
3418 | &s.uid, &s.ino, | |
3419 | &s.refcnt, &s.sk, opt); | |
3420 | ||
3421 | if (n < 9) | |
3422 | opt[0] = 0; | |
3423 | ||
3424 | s.type = dg_proto == UDP_PROTO ? IPPROTO_UDP : 0; | |
3425 | inet_stats_print(&s, false); | |
3426 | ||
3427 | if (show_details && opt[0]) | |
3428 | out(" opt:\"%s\"", opt); | |
3429 | ||
3430 | return 0; | |
3431 | } | |
3432 | ||
3433 | static int udp_show(struct filter *f) | |
3434 | { | |
3435 | FILE *fp = NULL; | |
3436 | ||
3437 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3438 | return 0; | |
3439 | ||
3440 | dg_proto = UDP_PROTO; | |
3441 | ||
3442 | if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") | |
3443 | && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) | |
3444 | return 0; | |
3445 | ||
3446 | if (f->families&FAMILY_MASK(AF_INET)) { | |
3447 | if ((fp = net_udp_open()) == NULL) | |
3448 | goto outerr; | |
3449 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) | |
3450 | goto outerr; | |
3451 | fclose(fp); | |
3452 | } | |
3453 | ||
3454 | if ((f->families&FAMILY_MASK(AF_INET6)) && | |
3455 | (fp = net_udp6_open()) != NULL) { | |
3456 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) | |
3457 | goto outerr; | |
3458 | fclose(fp); | |
3459 | } | |
3460 | return 0; | |
3461 | ||
3462 | outerr: | |
3463 | do { | |
3464 | int saved_errno = errno; | |
3465 | ||
3466 | if (fp) | |
3467 | fclose(fp); | |
3468 | errno = saved_errno; | |
3469 | return -1; | |
3470 | } while (0); | |
3471 | } | |
3472 | ||
3473 | static int raw_show(struct filter *f) | |
3474 | { | |
3475 | FILE *fp = NULL; | |
3476 | ||
3477 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) | |
3478 | return 0; | |
3479 | ||
3480 | dg_proto = RAW_PROTO; | |
3481 | ||
3482 | if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") && | |
3483 | inet_show_netlink(f, NULL, IPPROTO_RAW) == 0) | |
3484 | return 0; | |
3485 | ||
3486 | if (f->families&FAMILY_MASK(AF_INET)) { | |
3487 | if ((fp = net_raw_open()) == NULL) | |
3488 | goto outerr; | |
3489 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) | |
3490 | goto outerr; | |
3491 | fclose(fp); | |
3492 | } | |
3493 | ||
3494 | if ((f->families&FAMILY_MASK(AF_INET6)) && | |
3495 | (fp = net_raw6_open()) != NULL) { | |
3496 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) | |
3497 | goto outerr; | |
3498 | fclose(fp); | |
3499 | } | |
3500 | return 0; | |
3501 | ||
3502 | outerr: | |
3503 | do { | |
3504 | int saved_errno = errno; | |
3505 | ||
3506 | if (fp) | |
3507 | fclose(fp); | |
3508 | errno = saved_errno; | |
3509 | return -1; | |
3510 | } while (0); | |
3511 | } | |
3512 | ||
3513 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) | |
3514 | ||
3515 | static void unix_list_drop_first(struct sockstat **list) | |
3516 | { | |
3517 | struct sockstat *s = *list; | |
3518 | ||
3519 | (*list) = (*list)->next; | |
3520 | free(s->name); | |
3521 | free(s); | |
3522 | } | |
3523 | ||
3524 | static bool unix_type_skip(struct sockstat *s, struct filter *f) | |
3525 | { | |
3526 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) | |
3527 | return true; | |
3528 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) | |
3529 | return true; | |
3530 | if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB))) | |
3531 | return true; | |
3532 | return false; | |
3533 | } | |
3534 | ||
3535 | static void unix_stats_print(struct sockstat *s, struct filter *f) | |
3536 | { | |
3537 | char port_name[30] = {}; | |
3538 | ||
3539 | sock_state_print(s); | |
3540 | ||
3541 | sock_addr_print(s->name ?: "*", " ", | |
3542 | int_to_str(s->lport, port_name), NULL); | |
3543 | sock_addr_print(s->peer_name ?: "*", " ", | |
3544 | int_to_str(s->rport, port_name), NULL); | |
3545 | ||
3546 | proc_ctx_print(s); | |
3547 | } | |
3548 | ||
3549 | static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, | |
3550 | void *arg) | |
3551 | { | |
3552 | struct filter *f = (struct filter *)arg; | |
3553 | struct unix_diag_msg *r = NLMSG_DATA(nlh); | |
3554 | struct rtattr *tb[UNIX_DIAG_MAX+1]; | |
3555 | char name[128]; | |
3556 | struct sockstat stat = { .name = "*", .peer_name = "*" }; | |
3557 | ||
3558 | parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1), | |
3559 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
3560 | ||
3561 | stat.type = r->udiag_type; | |
3562 | stat.state = r->udiag_state; | |
3563 | stat.ino = stat.lport = r->udiag_ino; | |
3564 | stat.local.family = stat.remote.family = AF_UNIX; | |
3565 | ||
3566 | if (unix_type_skip(&stat, f)) | |
3567 | return 0; | |
3568 | ||
3569 | if (tb[UNIX_DIAG_RQLEN]) { | |
3570 | struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); | |
3571 | ||
3572 | stat.rq = rql->udiag_rqueue; | |
3573 | stat.wq = rql->udiag_wqueue; | |
3574 | } | |
3575 | if (tb[UNIX_DIAG_NAME]) { | |
3576 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); | |
3577 | ||
3578 | memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); | |
3579 | name[len] = '\0'; | |
3580 | if (name[0] == '\0') { | |
3581 | int i; | |
3582 | for (i = 0; i < len; i++) | |
3583 | if (name[i] == '\0') | |
3584 | name[i] = '@'; | |
3585 | } | |
3586 | stat.name = &name[0]; | |
3587 | memcpy(stat.local.data, &stat.name, sizeof(stat.name)); | |
3588 | } | |
3589 | if (tb[UNIX_DIAG_PEER]) | |
3590 | stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); | |
3591 | ||
3592 | if (f->f && run_ssfilter(f->f, &stat) == 0) | |
3593 | return 0; | |
3594 | ||
3595 | unix_stats_print(&stat, f); | |
3596 | ||
3597 | if (show_mem) | |
3598 | print_skmeminfo(tb, UNIX_DIAG_MEMINFO); | |
3599 | if (show_details) { | |
3600 | if (tb[UNIX_DIAG_SHUTDOWN]) { | |
3601 | unsigned char mask; | |
3602 | ||
3603 | mask = rta_getattr_u8(tb[UNIX_DIAG_SHUTDOWN]); | |
3604 | out(" %c-%c", | |
3605 | mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); | |
3606 | } | |
3607 | } | |
3608 | ||
3609 | return 0; | |
3610 | } | |
3611 | ||
3612 | static int handle_netlink_request(struct filter *f, struct nlmsghdr *req, | |
3613 | size_t size, rtnl_filter_t show_one_sock) | |
3614 | { | |
3615 | int ret = -1; | |
3616 | struct rtnl_handle rth; | |
3617 | ||
3618 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) | |
3619 | return -1; | |
3620 | ||
3621 | rth.dump = MAGIC_SEQ; | |
3622 | ||
3623 | if (rtnl_send(&rth, req, size) < 0) | |
3624 | goto Exit; | |
3625 | ||
3626 | if (rtnl_dump_filter(&rth, show_one_sock, f)) | |
3627 | goto Exit; | |
3628 | ||
3629 | ret = 0; | |
3630 | Exit: | |
3631 | rtnl_close(&rth); | |
3632 | return ret; | |
3633 | } | |
3634 | ||
3635 | static int unix_show_netlink(struct filter *f) | |
3636 | { | |
3637 | DIAG_REQUEST(req, struct unix_diag_req r); | |
3638 | ||
3639 | req.r.sdiag_family = AF_UNIX; | |
3640 | req.r.udiag_states = f->states; | |
3641 | req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; | |
3642 | if (show_mem) | |
3643 | req.r.udiag_show |= UDIAG_SHOW_MEMINFO; | |
3644 | ||
3645 | return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock); | |
3646 | } | |
3647 | ||
3648 | static int unix_show(struct filter *f) | |
3649 | { | |
3650 | FILE *fp; | |
3651 | char buf[256]; | |
3652 | char name[128]; | |
3653 | int newformat = 0; | |
3654 | int cnt; | |
3655 | struct sockstat *list = NULL; | |
3656 | const int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, | |
3657 | SS_ESTABLISHED, SS_CLOSING }; | |
3658 | ||
3659 | if (!filter_af_get(f, AF_UNIX)) | |
3660 | return 0; | |
3661 | ||
3662 | if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT") | |
3663 | && unix_show_netlink(f) == 0) | |
3664 | return 0; | |
3665 | ||
3666 | if ((fp = net_unix_open()) == NULL) | |
3667 | return -1; | |
3668 | if (!fgets(buf, sizeof(buf), fp)) { | |
3669 | fclose(fp); | |
3670 | return -1; | |
3671 | } | |
3672 | ||
3673 | if (memcmp(buf, "Peer", 4) == 0) | |
3674 | newformat = 1; | |
3675 | cnt = 0; | |
3676 | ||
3677 | while (fgets(buf, sizeof(buf), fp)) { | |
3678 | struct sockstat *u, **insp; | |
3679 | int flags; | |
3680 | ||
3681 | if (!(u = calloc(1, sizeof(*u)))) | |
3682 | break; | |
3683 | ||
3684 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", | |
3685 | &u->rport, &u->rq, &u->wq, &flags, &u->type, | |
3686 | &u->state, &u->ino, name) < 8) | |
3687 | name[0] = 0; | |
3688 | ||
3689 | u->lport = u->ino; | |
3690 | u->local.family = u->remote.family = AF_UNIX; | |
3691 | ||
3692 | if (flags & (1 << 16)) { | |
3693 | u->state = SS_LISTEN; | |
3694 | } else if (u->state > 0 && | |
3695 | u->state <= ARRAY_SIZE(unix_state_map)) { | |
3696 | u->state = unix_state_map[u->state-1]; | |
3697 | if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) | |
3698 | u->state = SS_ESTABLISHED; | |
3699 | } | |
3700 | if (unix_type_skip(u, f) || | |
3701 | !(f->states & (1 << u->state))) { | |
3702 | free(u); | |
3703 | continue; | |
3704 | } | |
3705 | ||
3706 | if (!newformat) { | |
3707 | u->rport = 0; | |
3708 | u->rq = 0; | |
3709 | u->wq = 0; | |
3710 | } | |
3711 | ||
3712 | if (name[0]) { | |
3713 | u->name = strdup(name); | |
3714 | if (!u->name) { | |
3715 | free(u); | |
3716 | break; | |
3717 | } | |
3718 | } | |
3719 | ||
3720 | if (u->rport) { | |
3721 | struct sockstat *p; | |
3722 | ||
3723 | for (p = list; p; p = p->next) { | |
3724 | if (u->rport == p->lport) | |
3725 | break; | |
3726 | } | |
3727 | if (!p) | |
3728 | u->peer_name = "?"; | |
3729 | else | |
3730 | u->peer_name = p->name ? : "*"; | |
3731 | } | |
3732 | ||
3733 | if (f->f) { | |
3734 | struct sockstat st = { | |
3735 | .local.family = AF_UNIX, | |
3736 | .remote.family = AF_UNIX, | |
3737 | }; | |
3738 | ||
3739 | memcpy(st.local.data, &u->name, sizeof(u->name)); | |
3740 | /* when parsing the old format rport is set to 0 and | |
3741 | * therefore peer_name remains NULL | |
3742 | */ | |
3743 | if (u->peer_name && strcmp(u->peer_name, "*")) | |
3744 | memcpy(st.remote.data, &u->peer_name, | |
3745 | sizeof(u->peer_name)); | |
3746 | if (run_ssfilter(f->f, &st) == 0) { | |
3747 | free(u->name); | |
3748 | free(u); | |
3749 | continue; | |
3750 | } | |
3751 | } | |
3752 | ||
3753 | insp = &list; | |
3754 | while (*insp) { | |
3755 | if (u->type < (*insp)->type || | |
3756 | (u->type == (*insp)->type && | |
3757 | u->ino < (*insp)->ino)) | |
3758 | break; | |
3759 | insp = &(*insp)->next; | |
3760 | } | |
3761 | u->next = *insp; | |
3762 | *insp = u; | |
3763 | ||
3764 | if (++cnt > MAX_UNIX_REMEMBER) { | |
3765 | while (list) { | |
3766 | unix_stats_print(list, f); | |
3767 | unix_list_drop_first(&list); | |
3768 | } | |
3769 | cnt = 0; | |
3770 | } | |
3771 | } | |
3772 | fclose(fp); | |
3773 | while (list) { | |
3774 | unix_stats_print(list, f); | |
3775 | unix_list_drop_first(&list); | |
3776 | } | |
3777 | ||
3778 | return 0; | |
3779 | } | |
3780 | ||
3781 | static int packet_stats_print(struct sockstat *s, const struct filter *f) | |
3782 | { | |
3783 | const char *addr, *port; | |
3784 | char ll_name[16]; | |
3785 | ||
3786 | s->local.family = s->remote.family = AF_PACKET; | |
3787 | ||
3788 | if (f->f) { | |
3789 | s->local.data[0] = s->prot; | |
3790 | if (run_ssfilter(f->f, s) == 0) | |
3791 | return 1; | |
3792 | } | |
3793 | ||
3794 | sock_state_print(s); | |
3795 | ||
3796 | if (s->prot == 3) | |
3797 | addr = "*"; | |
3798 | else | |
3799 | addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); | |
3800 | ||
3801 | if (s->iface == 0) | |
3802 | port = "*"; | |
3803 | else | |
3804 | port = xll_index_to_name(s->iface); | |
3805 | ||
3806 | sock_addr_print(addr, ":", port, NULL); | |
3807 | sock_addr_print("", "*", "", NULL); | |
3808 | ||
3809 | proc_ctx_print(s); | |
3810 | ||
3811 | if (show_details) | |
3812 | sock_details_print(s); | |
3813 | ||
3814 | return 0; | |
3815 | } | |
3816 | ||
3817 | static void packet_show_ring(struct packet_diag_ring *ring) | |
3818 | { | |
3819 | out("blk_size:%d", ring->pdr_block_size); | |
3820 | out(",blk_nr:%d", ring->pdr_block_nr); | |
3821 | out(",frm_size:%d", ring->pdr_frame_size); | |
3822 | out(",frm_nr:%d", ring->pdr_frame_nr); | |
3823 | out(",tmo:%d", ring->pdr_retire_tmo); | |
3824 | out(",features:0x%x", ring->pdr_features); | |
3825 | } | |
3826 | ||
3827 | static int packet_show_sock(const struct sockaddr_nl *addr, | |
3828 | struct nlmsghdr *nlh, void *arg) | |
3829 | { | |
3830 | const struct filter *f = arg; | |
3831 | struct packet_diag_msg *r = NLMSG_DATA(nlh); | |
3832 | struct packet_diag_info *pinfo = NULL; | |
3833 | struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL; | |
3834 | struct rtattr *tb[PACKET_DIAG_MAX+1]; | |
3835 | struct sockstat stat = {}; | |
3836 | uint32_t fanout = 0; | |
3837 | bool has_fanout = false; | |
3838 | ||
3839 | parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1), | |
3840 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
3841 | ||
3842 | /* use /proc/net/packet if all info are not available */ | |
3843 | if (!tb[PACKET_DIAG_MEMINFO]) | |
3844 | return -1; | |
3845 | ||
3846 | stat.type = r->pdiag_type; | |
3847 | stat.prot = r->pdiag_num; | |
3848 | stat.ino = r->pdiag_ino; | |
3849 | stat.state = SS_CLOSE; | |
3850 | stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); | |
3851 | ||
3852 | if (tb[PACKET_DIAG_MEMINFO]) { | |
3853 | __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); | |
3854 | ||
3855 | stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
3856 | } | |
3857 | ||
3858 | if (tb[PACKET_DIAG_INFO]) { | |
3859 | pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); | |
3860 | stat.lport = stat.iface = pinfo->pdi_index; | |
3861 | } | |
3862 | ||
3863 | if (tb[PACKET_DIAG_UID]) | |
3864 | stat.uid = rta_getattr_u32(tb[PACKET_DIAG_UID]); | |
3865 | ||
3866 | if (tb[PACKET_DIAG_RX_RING]) | |
3867 | ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]); | |
3868 | ||
3869 | if (tb[PACKET_DIAG_TX_RING]) | |
3870 | ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]); | |
3871 | ||
3872 | if (tb[PACKET_DIAG_FANOUT]) { | |
3873 | has_fanout = true; | |
3874 | fanout = rta_getattr_u32(tb[PACKET_DIAG_FANOUT]); | |
3875 | } | |
3876 | ||
3877 | if (packet_stats_print(&stat, f)) | |
3878 | return 0; | |
3879 | ||
3880 | if (show_details) { | |
3881 | if (pinfo) { | |
3882 | out("\n\tver:%d", pinfo->pdi_version); | |
3883 | out(" cpy_thresh:%d", pinfo->pdi_copy_thresh); | |
3884 | out(" flags( "); | |
3885 | if (pinfo->pdi_flags & PDI_RUNNING) | |
3886 | out("running"); | |
3887 | if (pinfo->pdi_flags & PDI_AUXDATA) | |
3888 | out(" auxdata"); | |
3889 | if (pinfo->pdi_flags & PDI_ORIGDEV) | |
3890 | out(" origdev"); | |
3891 | if (pinfo->pdi_flags & PDI_VNETHDR) | |
3892 | out(" vnethdr"); | |
3893 | if (pinfo->pdi_flags & PDI_LOSS) | |
3894 | out(" loss"); | |
3895 | if (!pinfo->pdi_flags) | |
3896 | out("0"); | |
3897 | out(" )"); | |
3898 | } | |
3899 | if (ring_rx) { | |
3900 | out("\n\tring_rx("); | |
3901 | packet_show_ring(ring_rx); | |
3902 | out(")"); | |
3903 | } | |
3904 | if (ring_tx) { | |
3905 | out("\n\tring_tx("); | |
3906 | packet_show_ring(ring_tx); | |
3907 | out(")"); | |
3908 | } | |
3909 | if (has_fanout) { | |
3910 | uint16_t type = (fanout >> 16) & 0xffff; | |
3911 | ||
3912 | out("\n\tfanout("); | |
3913 | out("id:%d,", fanout & 0xffff); | |
3914 | out("type:"); | |
3915 | ||
3916 | if (type == 0) | |
3917 | out("hash"); | |
3918 | else if (type == 1) | |
3919 | out("lb"); | |
3920 | else if (type == 2) | |
3921 | out("cpu"); | |
3922 | else if (type == 3) | |
3923 | out("roll"); | |
3924 | else if (type == 4) | |
3925 | out("random"); | |
3926 | else if (type == 5) | |
3927 | out("qm"); | |
3928 | else | |
3929 | out("0x%x", type); | |
3930 | ||
3931 | out(")"); | |
3932 | } | |
3933 | } | |
3934 | ||
3935 | if (show_bpf && tb[PACKET_DIAG_FILTER]) { | |
3936 | struct sock_filter *fil = | |
3937 | RTA_DATA(tb[PACKET_DIAG_FILTER]); | |
3938 | int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / | |
3939 | sizeof(struct sock_filter); | |
3940 | ||
3941 | out("\n\tbpf filter (%d): ", num); | |
3942 | while (num) { | |
3943 | out(" 0x%02x %u %u %u,", | |
3944 | fil->code, fil->jt, fil->jf, fil->k); | |
3945 | num--; | |
3946 | fil++; | |
3947 | } | |
3948 | } | |
3949 | ||
3950 | if (show_mem) | |
3951 | print_skmeminfo(tb, PACKET_DIAG_MEMINFO); | |
3952 | return 0; | |
3953 | } | |
3954 | ||
3955 | static int packet_show_netlink(struct filter *f) | |
3956 | { | |
3957 | DIAG_REQUEST(req, struct packet_diag_req r); | |
3958 | ||
3959 | req.r.sdiag_family = AF_PACKET; | |
3960 | req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | | |
3961 | PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT; | |
3962 | ||
3963 | return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock); | |
3964 | } | |
3965 | ||
3966 | static int packet_show_line(char *buf, const struct filter *f, int fam) | |
3967 | { | |
3968 | unsigned long long sk; | |
3969 | struct sockstat stat = {}; | |
3970 | int type, prot, iface, state, rq, uid, ino; | |
3971 | ||
3972 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", | |
3973 | &sk, | |
3974 | &type, &prot, &iface, &state, | |
3975 | &rq, &uid, &ino); | |
3976 | ||
3977 | if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) | |
3978 | return 0; | |
3979 | if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) | |
3980 | return 0; | |
3981 | ||
3982 | stat.type = type; | |
3983 | stat.prot = prot; | |
3984 | stat.lport = stat.iface = iface; | |
3985 | stat.state = state; | |
3986 | stat.rq = rq; | |
3987 | stat.uid = uid; | |
3988 | stat.ino = ino; | |
3989 | stat.state = SS_CLOSE; | |
3990 | ||
3991 | if (packet_stats_print(&stat, f)) | |
3992 | return 0; | |
3993 | ||
3994 | return 0; | |
3995 | } | |
3996 | ||
3997 | static int packet_show(struct filter *f) | |
3998 | { | |
3999 | FILE *fp; | |
4000 | int rc = 0; | |
4001 | ||
4002 | if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE))) | |
4003 | return 0; | |
4004 | ||
4005 | if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") && | |
4006 | packet_show_netlink(f) == 0) | |
4007 | return 0; | |
4008 | ||
4009 | if ((fp = net_packet_open()) == NULL) | |
4010 | return -1; | |
4011 | if (generic_record_read(fp, packet_show_line, f, AF_PACKET)) | |
4012 | rc = -1; | |
4013 | ||
4014 | fclose(fp); | |
4015 | return rc; | |
4016 | } | |
4017 | ||
4018 | static int netlink_show_one(struct filter *f, | |
4019 | int prot, int pid, unsigned int groups, | |
4020 | int state, int dst_pid, unsigned int dst_group, | |
4021 | int rq, int wq, | |
4022 | unsigned long long sk, unsigned long long cb) | |
4023 | { | |
4024 | struct sockstat st = { | |
4025 | .state = SS_CLOSE, | |
4026 | .rq = rq, | |
4027 | .wq = wq, | |
4028 | .local.family = AF_NETLINK, | |
4029 | .remote.family = AF_NETLINK, | |
4030 | }; | |
4031 | ||
4032 | SPRINT_BUF(prot_buf) = {}; | |
4033 | const char *prot_name; | |
4034 | char procname[64] = {}; | |
4035 | ||
4036 | if (f->f) { | |
4037 | st.rport = -1; | |
4038 | st.lport = pid; | |
4039 | st.local.data[0] = prot; | |
4040 | if (run_ssfilter(f->f, &st) == 0) | |
4041 | return 1; | |
4042 | } | |
4043 | ||
4044 | sock_state_print(&st); | |
4045 | ||
4046 | if (resolve_services) | |
4047 | prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); | |
4048 | else | |
4049 | prot_name = int_to_str(prot, prot_buf); | |
4050 | ||
4051 | if (pid == -1) { | |
4052 | procname[0] = '*'; | |
4053 | } else if (resolve_services) { | |
4054 | int done = 0; | |
4055 | ||
4056 | if (!pid) { | |
4057 | done = 1; | |
4058 | strncpy(procname, "kernel", 7); | |
4059 | } else if (pid > 0) { | |
4060 | FILE *fp; | |
4061 | ||
4062 | snprintf(procname, sizeof(procname), "%s/%d/stat", | |
4063 | getenv("PROC_ROOT") ? : "/proc", pid); | |
4064 | if ((fp = fopen(procname, "r")) != NULL) { | |
4065 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { | |
4066 | snprintf(procname+strlen(procname), | |
4067 | sizeof(procname)-strlen(procname), | |
4068 | "/%d", pid); | |
4069 | done = 1; | |
4070 | } | |
4071 | fclose(fp); | |
4072 | } | |
4073 | } | |
4074 | if (!done) | |
4075 | int_to_str(pid, procname); | |
4076 | } else { | |
4077 | int_to_str(pid, procname); | |
4078 | } | |
4079 | ||
4080 | sock_addr_print(prot_name, ":", procname, NULL); | |
4081 | ||
4082 | if (state == NETLINK_CONNECTED) { | |
4083 | char dst_group_buf[30]; | |
4084 | char dst_pid_buf[30]; | |
4085 | ||
4086 | sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", | |
4087 | int_to_str(dst_pid, dst_pid_buf), NULL); | |
4088 | } else { | |
4089 | sock_addr_print("", "*", "", NULL); | |
4090 | } | |
4091 | ||
4092 | char *pid_context = NULL; | |
4093 | ||
4094 | if (show_proc_ctx) { | |
4095 | /* The pid value will either be: | |
4096 | * 0 if destination kernel - show kernel initial context. | |
4097 | * A valid process pid - use getpidcon. | |
4098 | * A unique value allocated by the kernel or netlink user | |
4099 | * to the process - show context as "not available". | |
4100 | */ | |
4101 | if (!pid) | |
4102 | security_get_initial_context("kernel", &pid_context); | |
4103 | else if (pid > 0) | |
4104 | getpidcon(pid, &pid_context); | |
4105 | ||
4106 | out(" proc_ctx=%s", pid_context ? : "unavailable"); | |
4107 | free(pid_context); | |
4108 | } | |
4109 | ||
4110 | if (show_details) { | |
4111 | out(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); | |
4112 | } | |
4113 | ||
4114 | return 0; | |
4115 | } | |
4116 | ||
4117 | static int netlink_show_sock(const struct sockaddr_nl *addr, | |
4118 | struct nlmsghdr *nlh, void *arg) | |
4119 | { | |
4120 | struct filter *f = (struct filter *)arg; | |
4121 | struct netlink_diag_msg *r = NLMSG_DATA(nlh); | |
4122 | struct rtattr *tb[NETLINK_DIAG_MAX+1]; | |
4123 | int rq = 0, wq = 0; | |
4124 | unsigned long groups = 0; | |
4125 | ||
4126 | parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1), | |
4127 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
4128 | ||
4129 | if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS])) | |
4130 | groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]); | |
4131 | ||
4132 | if (tb[NETLINK_DIAG_MEMINFO]) { | |
4133 | const __u32 *skmeminfo; | |
4134 | ||
4135 | skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]); | |
4136 | ||
4137 | rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; | |
4138 | wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC]; | |
4139 | } | |
4140 | ||
4141 | if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups, | |
4142 | r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group, | |
4143 | rq, wq, 0, 0)) { | |
4144 | return 0; | |
4145 | } | |
4146 | ||
4147 | if (show_mem) { | |
4148 | out("\t"); | |
4149 | print_skmeminfo(tb, NETLINK_DIAG_MEMINFO); | |
4150 | } | |
4151 | ||
4152 | return 0; | |
4153 | } | |
4154 | ||
4155 | static int netlink_show_netlink(struct filter *f) | |
4156 | { | |
4157 | DIAG_REQUEST(req, struct netlink_diag_req r); | |
4158 | ||
4159 | req.r.sdiag_family = AF_NETLINK; | |
4160 | req.r.sdiag_protocol = NDIAG_PROTO_ALL; | |
4161 | req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO; | |
4162 | ||
4163 | return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock); | |
4164 | } | |
4165 | ||
4166 | static int netlink_show(struct filter *f) | |
4167 | { | |
4168 | FILE *fp; | |
4169 | char buf[256]; | |
4170 | int prot, pid; | |
4171 | unsigned int groups; | |
4172 | int rq, wq, rc; | |
4173 | unsigned long long sk, cb; | |
4174 | ||
4175 | if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE))) | |
4176 | return 0; | |
4177 | ||
4178 | if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") && | |
4179 | netlink_show_netlink(f) == 0) | |
4180 | return 0; | |
4181 | ||
4182 | if ((fp = net_netlink_open()) == NULL) | |
4183 | return -1; | |
4184 | if (!fgets(buf, sizeof(buf), fp)) { | |
4185 | fclose(fp); | |
4186 | return -1; | |
4187 | } | |
4188 | ||
4189 | while (fgets(buf, sizeof(buf), fp)) { | |
4190 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", | |
4191 | &sk, | |
4192 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); | |
4193 | ||
4194 | netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb); | |
4195 | } | |
4196 | ||
4197 | fclose(fp); | |
4198 | return 0; | |
4199 | } | |
4200 | ||
4201 | static bool vsock_type_skip(struct sockstat *s, struct filter *f) | |
4202 | { | |
4203 | if (s->type == SOCK_STREAM && !(f->dbs & (1 << VSOCK_ST_DB))) | |
4204 | return true; | |
4205 | if (s->type == SOCK_DGRAM && !(f->dbs & (1 << VSOCK_DG_DB))) | |
4206 | return true; | |
4207 | return false; | |
4208 | } | |
4209 | ||
4210 | static void vsock_addr_print(inet_prefix *a, __u32 port) | |
4211 | { | |
4212 | char cid_str[sizeof("4294967295")]; | |
4213 | char port_str[sizeof("4294967295")]; | |
4214 | __u32 cid; | |
4215 | ||
4216 | memcpy(&cid, a->data, sizeof(cid)); | |
4217 | ||
4218 | if (cid == ~(__u32)0) | |
4219 | snprintf(cid_str, sizeof(cid_str), "*"); | |
4220 | else | |
4221 | snprintf(cid_str, sizeof(cid_str), "%u", cid); | |
4222 | ||
4223 | if (port == ~(__u32)0) | |
4224 | snprintf(port_str, sizeof(port_str), "*"); | |
4225 | else | |
4226 | snprintf(port_str, sizeof(port_str), "%u", port); | |
4227 | ||
4228 | sock_addr_print(cid_str, ":", port_str, NULL); | |
4229 | } | |
4230 | ||
4231 | static void vsock_stats_print(struct sockstat *s, struct filter *f) | |
4232 | { | |
4233 | sock_state_print(s); | |
4234 | ||
4235 | vsock_addr_print(&s->local, s->lport); | |
4236 | vsock_addr_print(&s->remote, s->rport); | |
4237 | ||
4238 | proc_ctx_print(s); | |
4239 | } | |
4240 | ||
4241 | static int vsock_show_sock(const struct sockaddr_nl *addr, | |
4242 | struct nlmsghdr *nlh, void *arg) | |
4243 | { | |
4244 | struct filter *f = (struct filter *)arg; | |
4245 | struct vsock_diag_msg *r = NLMSG_DATA(nlh); | |
4246 | struct sockstat stat = { | |
4247 | .type = r->vdiag_type, | |
4248 | .lport = r->vdiag_src_port, | |
4249 | .rport = r->vdiag_dst_port, | |
4250 | .state = r->vdiag_state, | |
4251 | .ino = r->vdiag_ino, | |
4252 | }; | |
4253 | ||
4254 | vsock_set_inet_prefix(&stat.local, r->vdiag_src_cid); | |
4255 | vsock_set_inet_prefix(&stat.remote, r->vdiag_dst_cid); | |
4256 | ||
4257 | if (vsock_type_skip(&stat, f)) | |
4258 | return 0; | |
4259 | ||
4260 | if (f->f && run_ssfilter(f->f, &stat) == 0) | |
4261 | return 0; | |
4262 | ||
4263 | vsock_stats_print(&stat, f); | |
4264 | ||
4265 | return 0; | |
4266 | } | |
4267 | ||
4268 | static int vsock_show(struct filter *f) | |
4269 | { | |
4270 | DIAG_REQUEST(req, struct vsock_diag_req r); | |
4271 | ||
4272 | if (!filter_af_get(f, AF_VSOCK)) | |
4273 | return 0; | |
4274 | ||
4275 | req.r.sdiag_family = AF_VSOCK; | |
4276 | req.r.vdiag_states = f->states; | |
4277 | ||
4278 | return handle_netlink_request(f, &req.nlh, sizeof(req), vsock_show_sock); | |
4279 | } | |
4280 | ||
4281 | static void tipc_sock_addr_print(struct rtattr *net_addr, struct rtattr *id) | |
4282 | { | |
4283 | uint32_t node = rta_getattr_u32(net_addr); | |
4284 | uint32_t identity = rta_getattr_u32(id); | |
4285 | ||
4286 | SPRINT_BUF(addr) = {}; | |
4287 | SPRINT_BUF(port) = {}; | |
4288 | ||
4289 | sprintf(addr, "%u", node); | |
4290 | sprintf(port, "%u", identity); | |
4291 | sock_addr_print(addr, ":", port, NULL); | |
4292 | ||
4293 | } | |
4294 | ||
4295 | static int tipc_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, | |
4296 | void *arg) | |
4297 | { | |
4298 | struct rtattr *stat[TIPC_NLA_SOCK_STAT_MAX + 1] = {}; | |
4299 | struct rtattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {}; | |
4300 | struct rtattr *con[TIPC_NLA_CON_MAX + 1] = {}; | |
4301 | struct rtattr *info[TIPC_NLA_MAX + 1] = {}; | |
4302 | struct rtattr *msg_ref; | |
4303 | struct sockstat ss = {}; | |
4304 | ||
4305 | parse_rtattr(info, TIPC_NLA_MAX, NLMSG_DATA(nlh), | |
4306 | NLMSG_PAYLOAD(nlh, 0)); | |
4307 | ||
4308 | if (!info[TIPC_NLA_SOCK]) | |
4309 | return 0; | |
4310 | ||
4311 | msg_ref = info[TIPC_NLA_SOCK]; | |
4312 | parse_rtattr(attrs, TIPC_NLA_SOCK_MAX, RTA_DATA(msg_ref), | |
4313 | RTA_PAYLOAD(msg_ref)); | |
4314 | ||
4315 | msg_ref = attrs[TIPC_NLA_SOCK_STAT]; | |
4316 | parse_rtattr(stat, TIPC_NLA_SOCK_STAT_MAX, | |
4317 | RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); | |
4318 | ||
4319 | ||
4320 | ss.local.family = AF_TIPC; | |
4321 | ss.type = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TYPE]); | |
4322 | ss.state = rta_getattr_u32(attrs[TIPC_NLA_SOCK_TIPC_STATE]); | |
4323 | ss.uid = rta_getattr_u32(attrs[TIPC_NLA_SOCK_UID]); | |
4324 | ss.ino = rta_getattr_u32(attrs[TIPC_NLA_SOCK_INO]); | |
4325 | ss.rq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_RCVQ]); | |
4326 | ss.wq = rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_SENDQ]); | |
4327 | ss.sk = rta_getattr_u64(attrs[TIPC_NLA_SOCK_COOKIE]); | |
4328 | ||
4329 | sock_state_print (&ss); | |
4330 | ||
4331 | tipc_sock_addr_print(attrs[TIPC_NLA_SOCK_ADDR], | |
4332 | attrs[TIPC_NLA_SOCK_REF]); | |
4333 | ||
4334 | msg_ref = attrs[TIPC_NLA_SOCK_CON]; | |
4335 | if (msg_ref) { | |
4336 | parse_rtattr(con, TIPC_NLA_CON_MAX, | |
4337 | RTA_DATA(msg_ref), RTA_PAYLOAD(msg_ref)); | |
4338 | ||
4339 | tipc_sock_addr_print(con[TIPC_NLA_CON_NODE], | |
4340 | con[TIPC_NLA_CON_SOCK]); | |
4341 | } else | |
4342 | sock_addr_print("", "-", "", NULL); | |
4343 | ||
4344 | if (show_details) | |
4345 | sock_details_print(&ss); | |
4346 | ||
4347 | proc_ctx_print(&ss); | |
4348 | ||
4349 | if (show_tipcinfo) { | |
4350 | out("\n type:%s", stype_nameg[ss.type]); | |
4351 | out(" cong:%s ", | |
4352 | stat[TIPC_NLA_SOCK_STAT_LINK_CONG] ? "link" : | |
4353 | stat[TIPC_NLA_SOCK_STAT_CONN_CONG] ? "conn" : "none"); | |
4354 | out(" drop:%d ", | |
4355 | rta_getattr_u32(stat[TIPC_NLA_SOCK_STAT_DROP])); | |
4356 | ||
4357 | if (attrs[TIPC_NLA_SOCK_HAS_PUBL]) | |
4358 | out(" publ"); | |
4359 | ||
4360 | if (con[TIPC_NLA_CON_FLAG]) | |
4361 | out(" via {%u,%u} ", | |
4362 | rta_getattr_u32(con[TIPC_NLA_CON_TYPE]), | |
4363 | rta_getattr_u32(con[TIPC_NLA_CON_INST])); | |
4364 | } | |
4365 | ||
4366 | return 0; | |
4367 | } | |
4368 | ||
4369 | static int tipc_show(struct filter *f) | |
4370 | { | |
4371 | DIAG_REQUEST(req, struct tipc_sock_diag_req r); | |
4372 | ||
4373 | memset(&req.r, 0, sizeof(req.r)); | |
4374 | req.r.sdiag_family = AF_TIPC; | |
4375 | req.r.tidiag_states = f->states; | |
4376 | ||
4377 | return handle_netlink_request(f, &req.nlh, sizeof(req), tipc_show_sock); | |
4378 | } | |
4379 | ||
4380 | struct sock_diag_msg { | |
4381 | __u8 sdiag_family; | |
4382 | }; | |
4383 | ||
4384 | static int generic_show_sock(const struct sockaddr_nl *addr, | |
4385 | struct nlmsghdr *nlh, void *arg) | |
4386 | { | |
4387 | struct sock_diag_msg *r = NLMSG_DATA(nlh); | |
4388 | struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX }; | |
4389 | int ret; | |
4390 | ||
4391 | switch (r->sdiag_family) { | |
4392 | case AF_INET: | |
4393 | case AF_INET6: | |
4394 | inet_arg.rth = inet_arg.f->rth_for_killing; | |
4395 | ret = show_one_inet_sock(addr, nlh, &inet_arg); | |
4396 | break; | |
4397 | case AF_UNIX: | |
4398 | ret = unix_show_sock(addr, nlh, arg); | |
4399 | break; | |
4400 | case AF_PACKET: | |
4401 | ret = packet_show_sock(addr, nlh, arg); | |
4402 | break; | |
4403 | case AF_NETLINK: | |
4404 | ret = netlink_show_sock(addr, nlh, arg); | |
4405 | break; | |
4406 | case AF_VSOCK: | |
4407 | ret = vsock_show_sock(addr, nlh, arg); | |
4408 | break; | |
4409 | default: | |
4410 | ret = -1; | |
4411 | } | |
4412 | ||
4413 | render(); | |
4414 | ||
4415 | return ret; | |
4416 | } | |
4417 | ||
4418 | static int handle_follow_request(struct filter *f) | |
4419 | { | |
4420 | int ret = 0; | |
4421 | int groups = 0; | |
4422 | struct rtnl_handle rth, rth2; | |
4423 | ||
4424 | if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << TCP_DB)) | |
4425 | groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1); | |
4426 | if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << UDP_DB)) | |
4427 | groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1); | |
4428 | if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << TCP_DB)) | |
4429 | groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1); | |
4430 | if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << UDP_DB)) | |
4431 | groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1); | |
4432 | ||
4433 | if (groups == 0) | |
4434 | return -1; | |
4435 | ||
4436 | if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG)) | |
4437 | return -1; | |
4438 | ||
4439 | rth.dump = 0; | |
4440 | rth.local.nl_pid = 0; | |
4441 | ||
4442 | if (f->kill) { | |
4443 | if (rtnl_open_byproto(&rth2, groups, NETLINK_SOCK_DIAG)) { | |
4444 | rtnl_close(&rth); | |
4445 | return -1; | |
4446 | } | |
4447 | f->rth_for_killing = &rth2; | |
4448 | } | |
4449 | ||
4450 | if (rtnl_dump_filter(&rth, generic_show_sock, f)) | |
4451 | ret = -1; | |
4452 | ||
4453 | rtnl_close(&rth); | |
4454 | if (f->rth_for_killing) | |
4455 | rtnl_close(f->rth_for_killing); | |
4456 | return ret; | |
4457 | } | |
4458 | ||
4459 | static int get_snmp_int(char *proto, char *key, int *result) | |
4460 | { | |
4461 | char buf[1024]; | |
4462 | FILE *fp; | |
4463 | int protolen = strlen(proto); | |
4464 | int keylen = strlen(key); | |
4465 | ||
4466 | *result = 0; | |
4467 | ||
4468 | if ((fp = net_snmp_open()) == NULL) | |
4469 | return -1; | |
4470 | ||
4471 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
4472 | char *p = buf; | |
4473 | int pos = 0; | |
4474 | ||
4475 | if (memcmp(buf, proto, protolen)) | |
4476 | continue; | |
4477 | while ((p = strchr(p, ' ')) != NULL) { | |
4478 | pos++; | |
4479 | p++; | |
4480 | if (memcmp(p, key, keylen) == 0 && | |
4481 | (p[keylen] == ' ' || p[keylen] == '\n')) | |
4482 | break; | |
4483 | } | |
4484 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
4485 | break; | |
4486 | if (memcmp(buf, proto, protolen)) | |
4487 | break; | |
4488 | p = buf; | |
4489 | while ((p = strchr(p, ' ')) != NULL) { | |
4490 | p++; | |
4491 | if (--pos == 0) { | |
4492 | sscanf(p, "%d", result); | |
4493 | fclose(fp); | |
4494 | return 0; | |
4495 | } | |
4496 | } | |
4497 | } | |
4498 | ||
4499 | fclose(fp); | |
4500 | errno = ESRCH; | |
4501 | return -1; | |
4502 | } | |
4503 | ||
4504 | ||
4505 | /* Get stats from sockstat */ | |
4506 | ||
4507 | struct ssummary { | |
4508 | int socks; | |
4509 | int tcp_mem; | |
4510 | int tcp_total; | |
4511 | int tcp_orphans; | |
4512 | int tcp_tws; | |
4513 | int tcp4_hashed; | |
4514 | int udp4; | |
4515 | int raw4; | |
4516 | int frag4; | |
4517 | int frag4_mem; | |
4518 | int tcp6_hashed; | |
4519 | int udp6; | |
4520 | int raw6; | |
4521 | int frag6; | |
4522 | int frag6_mem; | |
4523 | }; | |
4524 | ||
4525 | static void get_sockstat_line(char *line, struct ssummary *s) | |
4526 | { | |
4527 | char id[256], rem[256]; | |
4528 | ||
4529 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) | |
4530 | return; | |
4531 | ||
4532 | if (strcmp(id, "sockets:") == 0) | |
4533 | sscanf(rem, "%*s%d", &s->socks); | |
4534 | else if (strcmp(id, "UDP:") == 0) | |
4535 | sscanf(rem, "%*s%d", &s->udp4); | |
4536 | else if (strcmp(id, "UDP6:") == 0) | |
4537 | sscanf(rem, "%*s%d", &s->udp6); | |
4538 | else if (strcmp(id, "RAW:") == 0) | |
4539 | sscanf(rem, "%*s%d", &s->raw4); | |
4540 | else if (strcmp(id, "RAW6:") == 0) | |
4541 | sscanf(rem, "%*s%d", &s->raw6); | |
4542 | else if (strcmp(id, "TCP6:") == 0) | |
4543 | sscanf(rem, "%*s%d", &s->tcp6_hashed); | |
4544 | else if (strcmp(id, "FRAG:") == 0) | |
4545 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); | |
4546 | else if (strcmp(id, "FRAG6:") == 0) | |
4547 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); | |
4548 | else if (strcmp(id, "TCP:") == 0) | |
4549 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", | |
4550 | &s->tcp4_hashed, | |
4551 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); | |
4552 | } | |
4553 | ||
4554 | static int get_sockstat(struct ssummary *s) | |
4555 | { | |
4556 | char buf[256]; | |
4557 | FILE *fp; | |
4558 | ||
4559 | memset(s, 0, sizeof(*s)); | |
4560 | ||
4561 | if ((fp = net_sockstat_open()) == NULL) | |
4562 | return -1; | |
4563 | while (fgets(buf, sizeof(buf), fp) != NULL) | |
4564 | get_sockstat_line(buf, s); | |
4565 | fclose(fp); | |
4566 | ||
4567 | if ((fp = net_sockstat6_open()) == NULL) | |
4568 | return 0; | |
4569 | while (fgets(buf, sizeof(buf), fp) != NULL) | |
4570 | get_sockstat_line(buf, s); | |
4571 | fclose(fp); | |
4572 | ||
4573 | return 0; | |
4574 | } | |
4575 | ||
4576 | static int print_summary(void) | |
4577 | { | |
4578 | struct ssummary s; | |
4579 | int tcp_estab; | |
4580 | ||
4581 | if (get_sockstat(&s) < 0) | |
4582 | perror("ss: get_sockstat"); | |
4583 | if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab) < 0) | |
4584 | perror("ss: get_snmpstat"); | |
4585 | ||
4586 | printf("Total: %d\n", s.socks); | |
4587 | ||
4588 | printf("TCP: %d (estab %d, closed %d, orphaned %d, timewait %d)\n", | |
4589 | s.tcp_total + s.tcp_tws, tcp_estab, | |
4590 | s.tcp_total - (s.tcp4_hashed + s.tcp6_hashed - s.tcp_tws), | |
4591 | s.tcp_orphans, s.tcp_tws); | |
4592 | ||
4593 | printf("\n"); | |
4594 | printf("Transport Total IP IPv6\n"); | |
4595 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); | |
4596 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); | |
4597 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); | |
4598 | printf("INET %-9d %-9d %-9d\n", | |
4599 | s.raw4+s.udp4+s.tcp4_hashed+ | |
4600 | s.raw6+s.udp6+s.tcp6_hashed, | |
4601 | s.raw4+s.udp4+s.tcp4_hashed, | |
4602 | s.raw6+s.udp6+s.tcp6_hashed); | |
4603 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); | |
4604 | ||
4605 | printf("\n"); | |
4606 | ||
4607 | return 0; | |
4608 | } | |
4609 | ||
4610 | static void _usage(FILE *dest) | |
4611 | { | |
4612 | fprintf(dest, | |
4613 | "Usage: ss [ OPTIONS ]\n" | |
4614 | " ss [ OPTIONS ] [ FILTER ]\n" | |
4615 | " -h, --help this message\n" | |
4616 | " -V, --version output version information\n" | |
4617 | " -n, --numeric don't resolve service names\n" | |
4618 | " -r, --resolve resolve host names\n" | |
4619 | " -a, --all display all sockets\n" | |
4620 | " -l, --listening display listening sockets\n" | |
4621 | " -o, --options show timer information\n" | |
4622 | " -e, --extended show detailed socket information\n" | |
4623 | " -m, --memory show socket memory usage\n" | |
4624 | " -p, --processes show process using socket\n" | |
4625 | " -i, --info show internal TCP information\n" | |
4626 | " --tipcinfo show internal tipc socket information\n" | |
4627 | " -s, --summary show socket usage summary\n" | |
4628 | " -b, --bpf show bpf filter socket information\n" | |
4629 | " -E, --events continually display sockets as they are destroyed\n" | |
4630 | " -Z, --context display process SELinux security contexts\n" | |
4631 | " -z, --contexts display process and socket SELinux security contexts\n" | |
4632 | " -N, --net switch to the specified network namespace name\n" | |
4633 | "\n" | |
4634 | " -4, --ipv4 display only IP version 4 sockets\n" | |
4635 | " -6, --ipv6 display only IP version 6 sockets\n" | |
4636 | " -0, --packet display PACKET sockets\n" | |
4637 | " -t, --tcp display only TCP sockets\n" | |
4638 | " -S, --sctp display only SCTP sockets\n" | |
4639 | " -u, --udp display only UDP sockets\n" | |
4640 | " -d, --dccp display only DCCP sockets\n" | |
4641 | " -w, --raw display only RAW sockets\n" | |
4642 | " -x, --unix display only Unix domain sockets\n" | |
4643 | " --tipc display only TIPC sockets\n" | |
4644 | " --vsock display only vsock sockets\n" | |
4645 | " -f, --family=FAMILY display sockets of type FAMILY\n" | |
4646 | " FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|help}\n" | |
4647 | "\n" | |
4648 | " -K, --kill forcibly close sockets, display what was closed\n" | |
4649 | " -H, --no-header Suppress header line\n" | |
4650 | "\n" | |
4651 | " -A, --query=QUERY, --socket=QUERY\n" | |
4652 | " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY]\n" | |
4653 | "\n" | |
4654 | " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" | |
4655 | " -F, --filter=FILE read filter information from FILE\n" | |
4656 | " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n" | |
4657 | " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n" | |
4658 | " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}\n" | |
4659 | " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" | |
4660 | " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" | |
4661 | " bucket := {syn-recv|time-wait}\n" | |
4662 | " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}\n" | |
4663 | ); | |
4664 | } | |
4665 | ||
4666 | static void help(void) __attribute__((noreturn)); | |
4667 | static void help(void) | |
4668 | { | |
4669 | _usage(stdout); | |
4670 | exit(0); | |
4671 | } | |
4672 | ||
4673 | static void usage(void) __attribute__((noreturn)); | |
4674 | static void usage(void) | |
4675 | { | |
4676 | _usage(stderr); | |
4677 | exit(-1); | |
4678 | } | |
4679 | ||
4680 | ||
4681 | static int scan_state(const char *state) | |
4682 | { | |
4683 | static const char * const sstate_namel[] = { | |
4684 | "UNKNOWN", | |
4685 | [SS_ESTABLISHED] = "established", | |
4686 | [SS_SYN_SENT] = "syn-sent", | |
4687 | [SS_SYN_RECV] = "syn-recv", | |
4688 | [SS_FIN_WAIT1] = "fin-wait-1", | |
4689 | [SS_FIN_WAIT2] = "fin-wait-2", | |
4690 | [SS_TIME_WAIT] = "time-wait", | |
4691 | [SS_CLOSE] = "unconnected", | |
4692 | [SS_CLOSE_WAIT] = "close-wait", | |
4693 | [SS_LAST_ACK] = "last-ack", | |
4694 | [SS_LISTEN] = "listening", | |
4695 | [SS_CLOSING] = "closing", | |
4696 | }; | |
4697 | int i; | |
4698 | ||
4699 | if (strcasecmp(state, "close") == 0 || | |
4700 | strcasecmp(state, "closed") == 0) | |
4701 | return (1<<SS_CLOSE); | |
4702 | if (strcasecmp(state, "syn-rcv") == 0) | |
4703 | return (1<<SS_SYN_RECV); | |
4704 | if (strcasecmp(state, "established") == 0) | |
4705 | return (1<<SS_ESTABLISHED); | |
4706 | if (strcasecmp(state, "all") == 0) | |
4707 | return SS_ALL; | |
4708 | if (strcasecmp(state, "connected") == 0) | |
4709 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); | |
4710 | if (strcasecmp(state, "synchronized") == 0) | |
4711 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); | |
4712 | if (strcasecmp(state, "bucket") == 0) | |
4713 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); | |
4714 | if (strcasecmp(state, "big") == 0) | |
4715 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); | |
4716 | for (i = 0; i < SS_MAX; i++) { | |
4717 | if (strcasecmp(state, sstate_namel[i]) == 0) | |
4718 | return (1<<i); | |
4719 | } | |
4720 | ||
4721 | fprintf(stderr, "ss: wrong state name: %s\n", state); | |
4722 | exit(-1); | |
4723 | } | |
4724 | ||
4725 | /* Values 'v' and 'V' are already used so a non-character is used */ | |
4726 | #define OPT_VSOCK 256 | |
4727 | ||
4728 | /* Values of 't' are already used so a non-character is used */ | |
4729 | #define OPT_TIPCSOCK 257 | |
4730 | #define OPT_TIPCINFO 258 | |
4731 | ||
4732 | static const struct option long_opts[] = { | |
4733 | { "numeric", 0, 0, 'n' }, | |
4734 | { "resolve", 0, 0, 'r' }, | |
4735 | { "options", 0, 0, 'o' }, | |
4736 | { "extended", 0, 0, 'e' }, | |
4737 | { "memory", 0, 0, 'm' }, | |
4738 | { "info", 0, 0, 'i' }, | |
4739 | { "processes", 0, 0, 'p' }, | |
4740 | { "bpf", 0, 0, 'b' }, | |
4741 | { "events", 0, 0, 'E' }, | |
4742 | { "dccp", 0, 0, 'd' }, | |
4743 | { "tcp", 0, 0, 't' }, | |
4744 | { "sctp", 0, 0, 'S' }, | |
4745 | { "udp", 0, 0, 'u' }, | |
4746 | { "raw", 0, 0, 'w' }, | |
4747 | { "unix", 0, 0, 'x' }, | |
4748 | { "tipc", 0, 0, OPT_TIPCSOCK}, | |
4749 | { "vsock", 0, 0, OPT_VSOCK }, | |
4750 | { "all", 0, 0, 'a' }, | |
4751 | { "listening", 0, 0, 'l' }, | |
4752 | { "ipv4", 0, 0, '4' }, | |
4753 | { "ipv6", 0, 0, '6' }, | |
4754 | { "packet", 0, 0, '0' }, | |
4755 | { "family", 1, 0, 'f' }, | |
4756 | { "socket", 1, 0, 'A' }, | |
4757 | { "query", 1, 0, 'A' }, | |
4758 | { "summary", 0, 0, 's' }, | |
4759 | { "diag", 1, 0, 'D' }, | |
4760 | { "filter", 1, 0, 'F' }, | |
4761 | { "version", 0, 0, 'V' }, | |
4762 | { "help", 0, 0, 'h' }, | |
4763 | { "context", 0, 0, 'Z' }, | |
4764 | { "contexts", 0, 0, 'z' }, | |
4765 | { "net", 1, 0, 'N' }, | |
4766 | { "tipcinfo", 0, 0, OPT_TIPCINFO}, | |
4767 | { "kill", 0, 0, 'K' }, | |
4768 | { "no-header", 0, 0, 'H' }, | |
4769 | { 0 } | |
4770 | ||
4771 | }; | |
4772 | ||
4773 | int main(int argc, char *argv[]) | |
4774 | { | |
4775 | int saw_states = 0; | |
4776 | int saw_query = 0; | |
4777 | int do_summary = 0; | |
4778 | const char *dump_tcpdiag = NULL; | |
4779 | FILE *filter_fp = NULL; | |
4780 | int ch; | |
4781 | int state_filter = 0; | |
4782 | ||
4783 | while ((ch = getopt_long(argc, argv, | |
4784 | "dhaletuwxnro460spbEf:miA:D:F:vVzZN:KHS", | |
4785 | long_opts, NULL)) != EOF) { | |
4786 | switch (ch) { | |
4787 | case 'n': | |
4788 | resolve_services = 0; | |
4789 | break; | |
4790 | case 'r': | |
4791 | resolve_hosts = 1; | |
4792 | break; | |
4793 | case 'o': | |
4794 | show_options = 1; | |
4795 | break; | |
4796 | case 'e': | |
4797 | show_options = 1; | |
4798 | show_details++; | |
4799 | break; | |
4800 | case 'm': | |
4801 | show_mem = 1; | |
4802 | break; | |
4803 | case 'i': | |
4804 | show_tcpinfo = 1; | |
4805 | break; | |
4806 | case 'p': | |
4807 | show_users++; | |
4808 | user_ent_hash_build(); | |
4809 | break; | |
4810 | case 'b': | |
4811 | show_options = 1; | |
4812 | show_bpf++; | |
4813 | break; | |
4814 | case 'E': | |
4815 | follow_events = 1; | |
4816 | break; | |
4817 | case 'd': | |
4818 | filter_db_set(¤t_filter, DCCP_DB, true); | |
4819 | break; | |
4820 | case 't': | |
4821 | filter_db_set(¤t_filter, TCP_DB, true); | |
4822 | break; | |
4823 | case 'S': | |
4824 | filter_db_set(¤t_filter, SCTP_DB, true); | |
4825 | break; | |
4826 | case 'u': | |
4827 | filter_db_set(¤t_filter, UDP_DB, true); | |
4828 | break; | |
4829 | case 'w': | |
4830 | filter_db_set(¤t_filter, RAW_DB, true); | |
4831 | break; | |
4832 | case 'x': | |
4833 | filter_af_set(¤t_filter, AF_UNIX); | |
4834 | break; | |
4835 | case OPT_VSOCK: | |
4836 | filter_af_set(¤t_filter, AF_VSOCK); | |
4837 | break; | |
4838 | case OPT_TIPCSOCK: | |
4839 | filter_af_set(¤t_filter, AF_TIPC); | |
4840 | break; | |
4841 | case 'a': | |
4842 | state_filter = SS_ALL; | |
4843 | break; | |
4844 | case 'l': | |
4845 | state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE); | |
4846 | break; | |
4847 | case '4': | |
4848 | filter_af_set(¤t_filter, AF_INET); | |
4849 | break; | |
4850 | case '6': | |
4851 | filter_af_set(¤t_filter, AF_INET6); | |
4852 | break; | |
4853 | case '0': | |
4854 | filter_af_set(¤t_filter, AF_PACKET); | |
4855 | break; | |
4856 | case 'f': | |
4857 | if (strcmp(optarg, "inet") == 0) | |
4858 | filter_af_set(¤t_filter, AF_INET); | |
4859 | else if (strcmp(optarg, "inet6") == 0) | |
4860 | filter_af_set(¤t_filter, AF_INET6); | |
4861 | else if (strcmp(optarg, "link") == 0) | |
4862 | filter_af_set(¤t_filter, AF_PACKET); | |
4863 | else if (strcmp(optarg, "unix") == 0) | |
4864 | filter_af_set(¤t_filter, AF_UNIX); | |
4865 | else if (strcmp(optarg, "netlink") == 0) | |
4866 | filter_af_set(¤t_filter, AF_NETLINK); | |
4867 | else if (strcmp(optarg, "tipc") == 0) | |
4868 | filter_af_set(¤t_filter, AF_TIPC); | |
4869 | else if (strcmp(optarg, "vsock") == 0) | |
4870 | filter_af_set(¤t_filter, AF_VSOCK); | |
4871 | else if (strcmp(optarg, "help") == 0) | |
4872 | help(); | |
4873 | else { | |
4874 | fprintf(stderr, "ss: \"%s\" is invalid family\n", | |
4875 | optarg); | |
4876 | usage(); | |
4877 | } | |
4878 | break; | |
4879 | case 'A': | |
4880 | { | |
4881 | char *p, *p1; | |
4882 | ||
4883 | if (!saw_query) { | |
4884 | current_filter.dbs = 0; | |
4885 | state_filter = state_filter ? | |
4886 | state_filter : SS_CONN; | |
4887 | saw_query = 1; | |
4888 | do_default = 0; | |
4889 | } | |
4890 | p = p1 = optarg; | |
4891 | do { | |
4892 | if ((p1 = strchr(p, ',')) != NULL) | |
4893 | *p1 = 0; | |
4894 | if (filter_db_parse(¤t_filter, p)) { | |
4895 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); | |
4896 | usage(); | |
4897 | } | |
4898 | p = p1 + 1; | |
4899 | } while (p1); | |
4900 | break; | |
4901 | } | |
4902 | case 's': | |
4903 | do_summary = 1; | |
4904 | break; | |
4905 | case 'D': | |
4906 | dump_tcpdiag = optarg; | |
4907 | break; | |
4908 | case 'F': | |
4909 | if (filter_fp) { | |
4910 | fprintf(stderr, "More than one filter file\n"); | |
4911 | exit(-1); | |
4912 | } | |
4913 | if (optarg[0] == '-') | |
4914 | filter_fp = stdin; | |
4915 | else | |
4916 | filter_fp = fopen(optarg, "r"); | |
4917 | if (!filter_fp) { | |
4918 | perror("fopen filter file"); | |
4919 | exit(-1); | |
4920 | } | |
4921 | break; | |
4922 | case 'v': | |
4923 | case 'V': | |
4924 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); | |
4925 | exit(0); | |
4926 | case 'z': | |
4927 | show_sock_ctx++; | |
4928 | /* fall through */ | |
4929 | case 'Z': | |
4930 | if (is_selinux_enabled() <= 0) { | |
4931 | fprintf(stderr, "ss: SELinux is not enabled.\n"); | |
4932 | exit(1); | |
4933 | } | |
4934 | show_proc_ctx++; | |
4935 | user_ent_hash_build(); | |
4936 | break; | |
4937 | case 'N': | |
4938 | if (netns_switch(optarg)) | |
4939 | exit(1); | |
4940 | break; | |
4941 | case OPT_TIPCINFO: | |
4942 | show_tipcinfo = 1; | |
4943 | break; | |
4944 | case 'K': | |
4945 | current_filter.kill = 1; | |
4946 | break; | |
4947 | case 'H': | |
4948 | show_header = 0; | |
4949 | break; | |
4950 | case 'h': | |
4951 | help(); | |
4952 | case '?': | |
4953 | default: | |
4954 | usage(); | |
4955 | } | |
4956 | } | |
4957 | ||
4958 | argc -= optind; | |
4959 | argv += optind; | |
4960 | ||
4961 | if (do_summary) { | |
4962 | print_summary(); | |
4963 | if (do_default && argc == 0) | |
4964 | exit(0); | |
4965 | } | |
4966 | ||
4967 | while (argc > 0) { | |
4968 | if (strcmp(*argv, "state") == 0) { | |
4969 | NEXT_ARG(); | |
4970 | if (!saw_states) | |
4971 | state_filter = 0; | |
4972 | state_filter |= scan_state(*argv); | |
4973 | saw_states = 1; | |
4974 | } else if (strcmp(*argv, "exclude") == 0 || | |
4975 | strcmp(*argv, "excl") == 0) { | |
4976 | NEXT_ARG(); | |
4977 | if (!saw_states) | |
4978 | state_filter = SS_ALL; | |
4979 | state_filter &= ~scan_state(*argv); | |
4980 | saw_states = 1; | |
4981 | } else { | |
4982 | break; | |
4983 | } | |
4984 | argc--; argv++; | |
4985 | } | |
4986 | ||
4987 | if (do_default) { | |
4988 | state_filter = state_filter ? state_filter : SS_CONN; | |
4989 | filter_db_parse(¤t_filter, "all"); | |
4990 | } | |
4991 | ||
4992 | filter_states_set(¤t_filter, state_filter); | |
4993 | filter_merge_defaults(¤t_filter); | |
4994 | ||
4995 | if (resolve_services && resolve_hosts && | |
4996 | (current_filter.dbs & (UNIX_DBM|INET_L4_DBM))) | |
4997 | init_service_resolver(); | |
4998 | ||
4999 | if (current_filter.dbs == 0) { | |
5000 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); | |
5001 | exit(0); | |
5002 | } | |
5003 | if (current_filter.families == 0) { | |
5004 | fprintf(stderr, "ss: no families to show with such filter.\n"); | |
5005 | exit(0); | |
5006 | } | |
5007 | if (current_filter.states == 0) { | |
5008 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); | |
5009 | exit(0); | |
5010 | } | |
5011 | ||
5012 | if (dump_tcpdiag) { | |
5013 | FILE *dump_fp = stdout; | |
5014 | ||
5015 | if (!(current_filter.dbs & (1<<TCP_DB))) { | |
5016 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); | |
5017 | exit(0); | |
5018 | } | |
5019 | if (dump_tcpdiag[0] != '-') { | |
5020 | dump_fp = fopen(dump_tcpdiag, "w"); | |
5021 | if (!dump_tcpdiag) { | |
5022 | perror("fopen dump file"); | |
5023 | exit(-1); | |
5024 | } | |
5025 | } | |
5026 | inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); | |
5027 | fflush(dump_fp); | |
5028 | exit(0); | |
5029 | } | |
5030 | ||
5031 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) | |
5032 | usage(); | |
5033 | ||
5034 | if (!(current_filter.dbs & (current_filter.dbs - 1))) | |
5035 | columns[COL_NETID].disabled = 1; | |
5036 | ||
5037 | if (!(current_filter.states & (current_filter.states - 1))) | |
5038 | columns[COL_STATE].disabled = 1; | |
5039 | ||
5040 | if (show_header) | |
5041 | print_header(); | |
5042 | ||
5043 | fflush(stdout); | |
5044 | ||
5045 | if (follow_events) | |
5046 | exit(handle_follow_request(¤t_filter)); | |
5047 | ||
5048 | if (current_filter.dbs & (1<<NETLINK_DB)) | |
5049 | netlink_show(¤t_filter); | |
5050 | if (current_filter.dbs & PACKET_DBM) | |
5051 | packet_show(¤t_filter); | |
5052 | if (current_filter.dbs & UNIX_DBM) | |
5053 | unix_show(¤t_filter); | |
5054 | if (current_filter.dbs & (1<<RAW_DB)) | |
5055 | raw_show(¤t_filter); | |
5056 | if (current_filter.dbs & (1<<UDP_DB)) | |
5057 | udp_show(¤t_filter); | |
5058 | if (current_filter.dbs & (1<<TCP_DB)) | |
5059 | tcp_show(¤t_filter); | |
5060 | if (current_filter.dbs & (1<<DCCP_DB)) | |
5061 | dccp_show(¤t_filter); | |
5062 | if (current_filter.dbs & (1<<SCTP_DB)) | |
5063 | sctp_show(¤t_filter); | |
5064 | if (current_filter.dbs & VSOCK_DBM) | |
5065 | vsock_show(¤t_filter); | |
5066 | if (current_filter.dbs & (1<<TIPC_DB)) | |
5067 | tipc_show(¤t_filter); | |
5068 | ||
5069 | if (show_users || show_proc_ctx || show_sock_ctx) | |
5070 | user_ent_destroy(); | |
5071 | ||
5072 | render(); | |
5073 | ||
5074 | return 0; | |
5075 | } |