2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2017 The strace developers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "netlink_sock_diag.h"
34 #include "print_fields.h"
36 #include <arpa/inet.h>
38 #include <linux/sock_diag.h>
39 #include <linux/inet_diag.h>
41 #include "xlat/inet_diag_attrs.h"
42 #include "xlat/inet_diag_bytecodes.h"
43 #include "xlat/inet_diag_extended_flags.h"
44 #include "xlat/inet_diag_req_attrs.h"
46 #include "xlat/tcp_states.h"
47 #include "xlat/tcp_state_flags.h"
50 print_inet_diag_sockid(const struct inet_diag_sockid *id, const uint8_t family)
52 PRINT_FIELD_NET_PORT("{", *id, idiag_sport);
53 PRINT_FIELD_NET_PORT(", ", *id, idiag_dport);
54 PRINT_FIELD_INET_ADDR(", ", *id, idiag_src, family);
55 PRINT_FIELD_INET_ADDR(", ", *id, idiag_dst, family);
56 PRINT_FIELD_IFINDEX(", ", *id, idiag_if);
57 PRINT_FIELD_COOKIE(", ", *id, idiag_cookie);
62 decode_inet_addr(struct tcb *const tcp,
63 const kernel_ulong_t addr,
64 const unsigned int len,
66 const char *const var_name)
76 size = sizeof(addrbuf.a4);
79 size = sizeof(addrbuf.a6);
83 if (!size || len < size) {
85 tprintf("%s=", var_name);
86 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
90 if (umoven(tcp, addr, size, &addrbuf) < 0) {
92 tprintf("%s=", var_name);
97 return print_inet_addr(family, &addrbuf, size, var_name);
101 decode_inet_diag_hostcond(struct tcb *const tcp,
102 const kernel_ulong_t addr,
103 const unsigned int len)
105 struct inet_diag_hostcond cond;
107 if (len < sizeof(cond)) {
108 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
111 if (umove_or_printaddr(tcp, addr, &cond))
114 PRINT_FIELD_XVAL("{", cond, family, addrfams, "AF_???");
115 PRINT_FIELD_U(", ", cond, prefix_len);
116 PRINT_FIELD_U(", ", cond, port);
118 if (len > sizeof(cond)) {
120 decode_inet_addr(tcp, addr + sizeof(cond),
121 len - sizeof(cond), cond.family, "addr");
127 print_inet_diag_bc_op(const struct inet_diag_bc_op *const op)
129 PRINT_FIELD_XVAL("{", *op, code, inet_diag_bytecodes,
131 PRINT_FIELD_U(", ", *op, yes);
132 PRINT_FIELD_U(", ", *op, no);
137 decode_inet_diag_markcond(struct tcb *const tcp,
138 const kernel_ulong_t addr,
139 const unsigned int len)
141 struct inet_diag_markcond markcond;
143 if (len < sizeof(markcond))
144 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
145 else if (!umove_or_printaddr(tcp, addr, &markcond)) {
146 PRINT_FIELD_U("{", markcond, mark);
147 PRINT_FIELD_U(", ", markcond, mask);
153 decode_bytecode_data(struct tcb *const tcp,
154 const kernel_ulong_t addr,
155 const unsigned int len,
156 const unsigned char code)
159 case INET_DIAG_BC_S_COND:
160 case INET_DIAG_BC_D_COND:
161 decode_inet_diag_hostcond(tcp, addr, len);
163 case INET_DIAG_BC_DEV_COND: {
166 if (len < sizeof(ifindex))
167 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
168 else if (!umove_or_printaddr(tcp, addr, &ifindex))
169 print_ifindex(ifindex);
172 case INET_DIAG_BC_S_GE:
173 case INET_DIAG_BC_S_LE:
174 case INET_DIAG_BC_D_GE:
175 case INET_DIAG_BC_D_LE: {
176 struct inet_diag_bc_op op;
178 if (len < sizeof(op))
179 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
180 else if (!umove_or_printaddr(tcp, addr, &op))
181 print_inet_diag_bc_op(&op);
184 case INET_DIAG_BC_MARK_COND:
185 decode_inet_diag_markcond(tcp, addr, len);
187 case INET_DIAG_BC_AUTO:
188 case INET_DIAG_BC_JMP:
189 case INET_DIAG_BC_NOP:
191 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
197 decode_inet_diag_bc_op(struct tcb *const tcp,
198 const kernel_ulong_t addr,
199 const unsigned int len,
200 const void *const opaque_data)
202 struct inet_diag_bc_op op;
204 if (len < sizeof(op))
206 if (umove_or_printaddr(tcp, addr, &op))
209 if (len > sizeof(op))
212 print_inet_diag_bc_op(&op);
214 if (len > sizeof(op)) {
216 decode_bytecode_data(tcp, addr + sizeof(op),
217 len - sizeof(op), op.code);
224 static const nla_decoder_t inet_diag_req_nla_decoders[] = {
225 [INET_DIAG_REQ_BYTECODE] = decode_inet_diag_bc_op
229 decode_inet_diag_req_compat(struct tcb *const tcp,
230 const struct nlmsghdr *const nlmsghdr,
231 const uint8_t family,
232 const kernel_ulong_t addr,
233 const unsigned int len)
235 struct inet_diag_req req = { .idiag_family = family };
236 size_t offset = sizeof(req.idiag_family);
237 bool decode_nla = false;
239 PRINT_FIELD_XVAL("{", req, idiag_family, addrfams, "AF_???");
241 if (len >= sizeof(req)) {
242 if (!umoven_or_printaddr(tcp, addr + offset,
243 sizeof(req) - offset,
244 (void *) &req + offset)) {
245 PRINT_FIELD_U("", req, idiag_src_len);
246 PRINT_FIELD_U(", ", req, idiag_dst_len);
247 PRINT_FIELD_FLAGS(", ", req, idiag_ext,
248 inet_diag_extended_flags,
249 "1<<INET_DIAG_\?\?\?-1");
250 PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
252 PRINT_FIELD_FLAGS(", ", req, idiag_states,
253 tcp_state_flags, "1<<TCP_???");
254 PRINT_FIELD_U(", ", req, idiag_dbs);
261 offset = NLMSG_ALIGN(sizeof(req));
262 if (decode_nla && len > offset) {
264 decode_nlattr(tcp, addr + offset, len - offset,
265 inet_diag_req_attrs, "INET_DIAG_REQ_???",
266 inet_diag_req_nla_decoders,
267 ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
272 decode_inet_diag_req_v2(struct tcb *const tcp,
273 const struct nlmsghdr *const nlmsghdr,
274 const uint8_t family,
275 const kernel_ulong_t addr,
276 const unsigned int len)
278 struct inet_diag_req_v2 req = { .sdiag_family = family };
279 size_t offset = sizeof(req.sdiag_family);
280 bool decode_nla = false;
282 PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
284 if (len >= sizeof(req)) {
285 if (!umoven_or_printaddr(tcp, addr + offset,
286 sizeof(req) - offset,
287 (void *) &req + offset)) {
288 PRINT_FIELD_XVAL("", req, sdiag_protocol,
289 inet_protocols, "IPPROTO_???");
290 PRINT_FIELD_FLAGS(", ", req, idiag_ext,
291 inet_diag_extended_flags,
292 "1<<INET_DIAG_\?\?\?-1");
293 PRINT_FIELD_FLAGS(", ", req, idiag_states,
294 tcp_state_flags, "1<<TCP_???");
295 PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
303 offset = NLMSG_ALIGN(sizeof(req));
304 if (decode_nla && len > offset) {
306 decode_nlattr(tcp, addr + offset, len - offset,
307 inet_diag_req_attrs, "INET_DIAG_REQ_???",
308 inet_diag_req_nla_decoders,
309 ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
313 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_req)
315 if (nlmsghdr->nlmsg_type == TCPDIAG_GETSOCK
316 || nlmsghdr->nlmsg_type == DCCPDIAG_GETSOCK)
317 return decode_inet_diag_req_compat(tcp, nlmsghdr,
320 return decode_inet_diag_req_v2(tcp, nlmsghdr,
325 decode_inet_diag_meminfo(struct tcb *const tcp,
326 const kernel_ulong_t addr,
327 const unsigned int len,
328 const void *const opaque_data)
330 struct inet_diag_meminfo minfo;
332 if (len < sizeof(minfo))
334 if (umove_or_printaddr(tcp, addr, &minfo))
337 PRINT_FIELD_U("{", minfo, idiag_rmem);
338 PRINT_FIELD_U(", ", minfo, idiag_wmem);
339 PRINT_FIELD_U(", ", minfo, idiag_fmem);
340 PRINT_FIELD_U(", ", minfo, idiag_tmem);
347 decode_tcpvegas_info(struct tcb *const tcp,
348 const kernel_ulong_t addr,
349 const unsigned int len,
350 const void *const opaque_data)
352 struct tcpvegas_info vegas;
354 if (len < sizeof(vegas))
356 if (umove_or_printaddr(tcp, addr, &vegas))
359 PRINT_FIELD_U("{", vegas, tcpv_enabled);
360 PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
361 PRINT_FIELD_U(", ", vegas, tcpv_rtt);
362 PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
369 decode_tcp_dctcp_info(struct tcb *const tcp,
370 const kernel_ulong_t addr,
371 const unsigned int len,
372 const void *const opaque_data)
374 struct tcp_dctcp_info dctcp;
376 if (len < sizeof(dctcp))
378 if (umove_or_printaddr(tcp, addr, &dctcp))
381 PRINT_FIELD_U("{", dctcp, dctcp_enabled);
382 PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
383 PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
384 PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
385 PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
392 decode_tcp_bbr_info(struct tcb *const tcp,
393 const kernel_ulong_t addr,
394 const unsigned int len,
395 const void *const opaque_data)
397 struct tcp_bbr_info bbr;
399 if (len < sizeof(bbr))
401 if (umove_or_printaddr(tcp, addr, &bbr))
404 PRINT_FIELD_X("{", bbr, bbr_bw_lo);
405 PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
406 PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
407 PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
408 PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
414 static const nla_decoder_t inet_diag_msg_nla_decoders[] = {
415 [INET_DIAG_MEMINFO] = decode_inet_diag_meminfo,
416 [INET_DIAG_INFO] = NULL, /* unimplemented */
417 [INET_DIAG_VEGASINFO] = decode_tcpvegas_info,
418 [INET_DIAG_CONG] = decode_nla_str,
419 [INET_DIAG_TOS] = decode_nla_u8,
420 [INET_DIAG_TCLASS] = decode_nla_u8,
421 [INET_DIAG_SKMEMINFO] = decode_nla_meminfo,
422 [INET_DIAG_SHUTDOWN] = decode_nla_u8,
423 [INET_DIAG_DCTCPINFO] = decode_tcp_dctcp_info,
424 [INET_DIAG_PROTOCOL] = decode_nla_u8,
425 [INET_DIAG_SKV6ONLY] = decode_nla_u8,
426 [INET_DIAG_LOCALS] = NULL, /* unimplemented */
427 [INET_DIAG_PEERS] = NULL, /* unimplemented */
428 [INET_DIAG_PAD] = NULL,
429 [INET_DIAG_MARK] = decode_nla_u32,
430 [INET_DIAG_BBRINFO] = decode_tcp_bbr_info
433 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_msg)
435 struct inet_diag_msg msg = { .idiag_family = family };
436 size_t offset = sizeof(msg.idiag_family);
437 bool decode_nla = false;
439 PRINT_FIELD_XVAL("{", msg, idiag_family, addrfams, "AF_???");
441 if (len >= sizeof(msg)) {
442 if (!umoven_or_printaddr(tcp, addr + offset,
443 sizeof(msg) - offset,
444 (void *) &msg + offset)) {
445 PRINT_FIELD_XVAL("", msg, idiag_state,
446 tcp_states, "TCP_???");
447 PRINT_FIELD_U(", ", msg, idiag_timer);
448 PRINT_FIELD_U(", ", msg, idiag_retrans);
449 PRINT_FIELD_INET_DIAG_SOCKID(", ", msg, id,
451 PRINT_FIELD_U(", ", msg, idiag_expires);
452 PRINT_FIELD_U(", ", msg, idiag_rqueue);
453 PRINT_FIELD_U(", ", msg, idiag_wqueue);
454 PRINT_FIELD_U(", ", msg, idiag_uid);
455 PRINT_FIELD_U(", ", msg, idiag_inode);
462 offset = NLMSG_ALIGN(sizeof(msg));
463 if (decode_nla && len > offset) {
465 decode_nlattr(tcp, addr + offset, len - offset,
466 inet_diag_attrs, "INET_DIAG_???",
467 inet_diag_msg_nla_decoders,
468 ARRAY_SIZE(inet_diag_msg_nla_decoders), NULL);