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_diag_hostcond(struct tcb *const tcp,
63 const kernel_ulong_t addr,
64 const unsigned int len)
66 struct inet_diag_hostcond cond;
68 if (len < sizeof(cond)) {
69 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
72 if (umove_or_printaddr(tcp, addr, &cond))
75 PRINT_FIELD_XVAL("{", cond, family, addrfams, "AF_???");
76 PRINT_FIELD_U(", ", cond, prefix_len);
77 PRINT_FIELD_U(", ", cond, port);
79 if (len > sizeof(cond)) {
81 decode_inet_addr(tcp, addr + sizeof(cond),
82 len - sizeof(cond), cond.family, "addr");
88 print_inet_diag_bc_op(const struct inet_diag_bc_op *const op)
90 PRINT_FIELD_XVAL("{", *op, code, inet_diag_bytecodes,
92 PRINT_FIELD_U(", ", *op, yes);
93 PRINT_FIELD_U(", ", *op, no);
98 decode_inet_diag_markcond(struct tcb *const tcp,
99 const kernel_ulong_t addr,
100 const unsigned int len)
102 struct inet_diag_markcond markcond;
104 if (len < sizeof(markcond))
105 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
106 else if (!umove_or_printaddr(tcp, addr, &markcond)) {
107 PRINT_FIELD_U("{", markcond, mark);
108 PRINT_FIELD_U(", ", markcond, mask);
114 decode_bytecode_data(struct tcb *const tcp,
115 const kernel_ulong_t addr,
116 const unsigned int len,
117 const unsigned char code)
120 case INET_DIAG_BC_S_COND:
121 case INET_DIAG_BC_D_COND:
122 decode_inet_diag_hostcond(tcp, addr, len);
124 case INET_DIAG_BC_DEV_COND: {
127 if (len < sizeof(ifindex))
128 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
129 else if (!umove_or_printaddr(tcp, addr, &ifindex))
130 print_ifindex(ifindex);
133 case INET_DIAG_BC_S_GE:
134 case INET_DIAG_BC_S_LE:
135 case INET_DIAG_BC_D_GE:
136 case INET_DIAG_BC_D_LE: {
137 struct inet_diag_bc_op op;
139 if (len < sizeof(op))
140 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
141 else if (!umove_or_printaddr(tcp, addr, &op))
142 print_inet_diag_bc_op(&op);
145 case INET_DIAG_BC_MARK_COND:
146 decode_inet_diag_markcond(tcp, addr, len);
148 case INET_DIAG_BC_AUTO:
149 case INET_DIAG_BC_JMP:
150 case INET_DIAG_BC_NOP:
152 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
158 decode_inet_diag_bc_op(struct tcb *const tcp,
159 const kernel_ulong_t addr,
160 const unsigned int len,
161 const void *const opaque_data)
163 struct inet_diag_bc_op op;
165 if (len < sizeof(op))
167 if (umove_or_printaddr(tcp, addr, &op))
170 if (len > sizeof(op))
173 print_inet_diag_bc_op(&op);
175 if (len > sizeof(op)) {
177 decode_bytecode_data(tcp, addr + sizeof(op),
178 len - sizeof(op), op.code);
185 static const nla_decoder_t inet_diag_req_nla_decoders[] = {
186 [INET_DIAG_REQ_BYTECODE] = decode_inet_diag_bc_op
190 decode_inet_diag_req_compat(struct tcb *const tcp,
191 const struct nlmsghdr *const nlmsghdr,
192 const uint8_t family,
193 const kernel_ulong_t addr,
194 const unsigned int len)
196 struct inet_diag_req req = { .idiag_family = family };
197 size_t offset = sizeof(req.idiag_family);
198 bool decode_nla = false;
200 PRINT_FIELD_XVAL("{", req, idiag_family, addrfams, "AF_???");
202 if (len >= sizeof(req)) {
203 if (!umoven_or_printaddr(tcp, addr + offset,
204 sizeof(req) - offset,
205 (void *) &req + offset)) {
206 PRINT_FIELD_U("", req, idiag_src_len);
207 PRINT_FIELD_U(", ", req, idiag_dst_len);
208 PRINT_FIELD_FLAGS(", ", req, idiag_ext,
209 inet_diag_extended_flags,
210 "1<<INET_DIAG_\?\?\?-1");
211 PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
213 PRINT_FIELD_FLAGS(", ", req, idiag_states,
214 tcp_state_flags, "1<<TCP_???");
215 PRINT_FIELD_U(", ", req, idiag_dbs);
222 offset = NLMSG_ALIGN(sizeof(req));
223 if (decode_nla && len > offset) {
225 decode_nlattr(tcp, addr + offset, len - offset,
226 inet_diag_req_attrs, "INET_DIAG_REQ_???",
227 inet_diag_req_nla_decoders,
228 ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
233 decode_inet_diag_req_v2(struct tcb *const tcp,
234 const struct nlmsghdr *const nlmsghdr,
235 const uint8_t family,
236 const kernel_ulong_t addr,
237 const unsigned int len)
239 struct inet_diag_req_v2 req = { .sdiag_family = family };
240 size_t offset = sizeof(req.sdiag_family);
241 bool decode_nla = false;
243 PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
245 if (len >= sizeof(req)) {
246 if (!umoven_or_printaddr(tcp, addr + offset,
247 sizeof(req) - offset,
248 (void *) &req + offset)) {
249 PRINT_FIELD_XVAL("", req, sdiag_protocol,
250 inet_protocols, "IPPROTO_???");
251 PRINT_FIELD_FLAGS(", ", req, idiag_ext,
252 inet_diag_extended_flags,
253 "1<<INET_DIAG_\?\?\?-1");
254 PRINT_FIELD_FLAGS(", ", req, idiag_states,
255 tcp_state_flags, "1<<TCP_???");
256 PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
264 offset = NLMSG_ALIGN(sizeof(req));
265 if (decode_nla && len > offset) {
267 decode_nlattr(tcp, addr + offset, len - offset,
268 inet_diag_req_attrs, "INET_DIAG_REQ_???",
269 inet_diag_req_nla_decoders,
270 ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
274 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_req)
276 if (nlmsghdr->nlmsg_type == TCPDIAG_GETSOCK
277 || nlmsghdr->nlmsg_type == DCCPDIAG_GETSOCK)
278 return decode_inet_diag_req_compat(tcp, nlmsghdr,
281 return decode_inet_diag_req_v2(tcp, nlmsghdr,
286 decode_inet_diag_meminfo(struct tcb *const tcp,
287 const kernel_ulong_t addr,
288 const unsigned int len,
289 const void *const opaque_data)
291 struct inet_diag_meminfo minfo;
293 if (len < sizeof(minfo))
295 if (umove_or_printaddr(tcp, addr, &minfo))
298 PRINT_FIELD_U("{", minfo, idiag_rmem);
299 PRINT_FIELD_U(", ", minfo, idiag_wmem);
300 PRINT_FIELD_U(", ", minfo, idiag_fmem);
301 PRINT_FIELD_U(", ", minfo, idiag_tmem);
308 decode_tcpvegas_info(struct tcb *const tcp,
309 const kernel_ulong_t addr,
310 const unsigned int len,
311 const void *const opaque_data)
313 struct tcpvegas_info vegas;
315 if (len < sizeof(vegas))
317 if (umove_or_printaddr(tcp, addr, &vegas))
320 PRINT_FIELD_U("{", vegas, tcpv_enabled);
321 PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
322 PRINT_FIELD_U(", ", vegas, tcpv_rtt);
323 PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
330 decode_tcp_dctcp_info(struct tcb *const tcp,
331 const kernel_ulong_t addr,
332 const unsigned int len,
333 const void *const opaque_data)
335 struct tcp_dctcp_info dctcp;
337 if (len < sizeof(dctcp))
339 if (umove_or_printaddr(tcp, addr, &dctcp))
342 PRINT_FIELD_U("{", dctcp, dctcp_enabled);
343 PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
344 PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
345 PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
346 PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
353 decode_tcp_bbr_info(struct tcb *const tcp,
354 const kernel_ulong_t addr,
355 const unsigned int len,
356 const void *const opaque_data)
358 struct tcp_bbr_info bbr;
360 if (len < sizeof(bbr))
362 if (umove_or_printaddr(tcp, addr, &bbr))
365 PRINT_FIELD_X("{", bbr, bbr_bw_lo);
366 PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
367 PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
368 PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
369 PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
375 static const nla_decoder_t inet_diag_msg_nla_decoders[] = {
376 [INET_DIAG_MEMINFO] = decode_inet_diag_meminfo,
377 [INET_DIAG_INFO] = NULL, /* unimplemented */
378 [INET_DIAG_VEGASINFO] = decode_tcpvegas_info,
379 [INET_DIAG_CONG] = decode_nla_str,
380 [INET_DIAG_TOS] = decode_nla_u8,
381 [INET_DIAG_TCLASS] = decode_nla_u8,
382 [INET_DIAG_SKMEMINFO] = decode_nla_meminfo,
383 [INET_DIAG_SHUTDOWN] = decode_nla_u8,
384 [INET_DIAG_DCTCPINFO] = decode_tcp_dctcp_info,
385 [INET_DIAG_PROTOCOL] = decode_nla_u8,
386 [INET_DIAG_SKV6ONLY] = decode_nla_u8,
387 [INET_DIAG_LOCALS] = NULL, /* unimplemented */
388 [INET_DIAG_PEERS] = NULL, /* unimplemented */
389 [INET_DIAG_PAD] = NULL,
390 [INET_DIAG_MARK] = decode_nla_u32,
391 [INET_DIAG_BBRINFO] = decode_tcp_bbr_info
394 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_msg)
396 struct inet_diag_msg msg = { .idiag_family = family };
397 size_t offset = sizeof(msg.idiag_family);
398 bool decode_nla = false;
400 PRINT_FIELD_XVAL("{", msg, idiag_family, addrfams, "AF_???");
402 if (len >= sizeof(msg)) {
403 if (!umoven_or_printaddr(tcp, addr + offset,
404 sizeof(msg) - offset,
405 (void *) &msg + offset)) {
406 PRINT_FIELD_XVAL("", msg, idiag_state,
407 tcp_states, "TCP_???");
408 PRINT_FIELD_U(", ", msg, idiag_timer);
409 PRINT_FIELD_U(", ", msg, idiag_retrans);
410 PRINT_FIELD_INET_DIAG_SOCKID(", ", msg, id,
412 PRINT_FIELD_U(", ", msg, idiag_expires);
413 PRINT_FIELD_U(", ", msg, idiag_rqueue);
414 PRINT_FIELD_U(", ", msg, idiag_wqueue);
415 PRINT_FIELD_U(", ", msg, idiag_uid);
416 PRINT_FIELD_U(", ", msg, idiag_inode);
423 offset = NLMSG_ALIGN(sizeof(msg));
424 if (decode_nla && len > offset) {
426 decode_nlattr(tcp, addr + offset, len - offset,
427 inet_diag_attrs, "INET_DIAG_???",
428 inet_diag_msg_nla_decoders,
429 ARRAY_SIZE(inet_diag_msg_nla_decoders), NULL);