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