2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2017-2018 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 <linux/sock_diag.h>
37 #include <linux/netlink_diag.h>
39 #include "xlat/netlink_diag_attrs.h"
40 #include "xlat/netlink_diag_show.h"
41 #include "xlat/netlink_socket_flags.h"
42 #include "xlat/netlink_states.h"
44 DECL_NETLINK_DIAG_DECODER(decode_netlink_diag_req)
46 struct netlink_diag_req req = { .sdiag_family = family };
47 const size_t offset = sizeof(req.sdiag_family);
49 PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
51 if (len >= sizeof(req)) {
52 if (!umoven_or_printaddr(tcp, addr + offset,
54 (char *) &req + offset)) {
55 if (NDIAG_PROTO_ALL == req.sdiag_protocol)
57 "sdiag_protocol", "NDIAG_PROTO_ALL");
59 PRINT_FIELD_XVAL("", req, sdiag_protocol,
62 PRINT_FIELD_U(", ", req, ndiag_ino);
63 PRINT_FIELD_FLAGS(", ", req, ndiag_show,
64 netlink_diag_show, "NDIAG_SHOW_???");
65 PRINT_FIELD_COOKIE(", ", req, ndiag_cookie);
73 print_group(struct tcb *const tcp,
75 const size_t elem_size,
76 void *const opaque_data)
78 if (elem_size < sizeof(kernel_ulong_t))
79 tprintf("%#0*x", (int) elem_size * 2 + 2,
80 *(unsigned int *) elem_buf);
82 tprintf("%#0*" PRI_klx, (int) elem_size * 2 + 2,
83 *(kernel_ulong_t *) elem_buf);
89 decode_netlink_diag_groups(struct tcb *const tcp,
90 const kernel_ulong_t addr,
91 const unsigned int len,
92 const void *const opaque_data)
95 const size_t nmemb = len / current_wordsize;
100 print_array(tcp, addr, nmemb, &buf, current_wordsize,
101 umoven_or_printaddr, print_group, 0);
107 decode_netlink_diag_ring(struct tcb *const tcp,
108 const kernel_ulong_t addr,
109 const unsigned int len,
110 const void *const opaque_data)
112 struct netlink_diag_ring ndr;
114 if (len < sizeof(ndr))
116 if (umove_or_printaddr(tcp, addr, &ndr))
119 PRINT_FIELD_U("{", ndr, ndr_block_size);
120 PRINT_FIELD_U(", ", ndr, ndr_block_nr);
121 PRINT_FIELD_U(", ", ndr, ndr_frame_size);
122 PRINT_FIELD_U(", ", ndr, ndr_frame_nr);
129 decode_netlink_diag_flags(struct tcb *const tcp,
130 const kernel_ulong_t addr,
131 const unsigned int len,
132 const void *const opaque_data)
136 if (len < sizeof(flags))
138 if (umove_or_printaddr(tcp, addr, &flags))
141 printflags(netlink_socket_flags, flags, "NDIAG_FLAG_???");
146 static const nla_decoder_t netlink_diag_msg_nla_decoders[] = {
147 [NETLINK_DIAG_MEMINFO] = decode_nla_meminfo,
148 [NETLINK_DIAG_GROUPS] = decode_netlink_diag_groups,
149 [NETLINK_DIAG_RX_RING] = decode_netlink_diag_ring,
150 [NETLINK_DIAG_TX_RING] = decode_netlink_diag_ring,
151 [NETLINK_DIAG_FLAGS] = decode_netlink_diag_flags
154 DECL_NETLINK_DIAG_DECODER(decode_netlink_diag_msg)
156 struct netlink_diag_msg msg = { .ndiag_family = family };
157 size_t offset = sizeof(msg.ndiag_family);
158 bool decode_nla = false;
160 PRINT_FIELD_XVAL("{", msg, ndiag_family, addrfams, "AF_???");
162 if (len >= sizeof(msg)) {
163 if (!umoven_or_printaddr(tcp, addr + offset,
164 sizeof(msg) - offset,
165 (char *) &msg + offset)) {
166 PRINT_FIELD_XVAL("", msg, ndiag_type,
167 socktypes, "SOCK_???");
168 PRINT_FIELD_XVAL(", ", msg, ndiag_protocol,
169 netlink_protocols, "NETLINK_???");
170 PRINT_FIELD_XVAL(", ", msg, ndiag_state,
171 netlink_states, "NETLINK_???");
172 PRINT_FIELD_U(", ", msg, ndiag_portid);
173 PRINT_FIELD_U(", ", msg, ndiag_dst_portid);
174 PRINT_FIELD_U(", ", msg, ndiag_dst_group);
175 PRINT_FIELD_U(", ", msg, ndiag_ino);
176 PRINT_FIELD_COOKIE(", ", msg, ndiag_cookie);
183 offset = NLMSG_ALIGN(sizeof(msg));
184 if (decode_nla && len > offset) {
186 decode_nlattr(tcp, addr + offset, len - offset,
187 netlink_diag_attrs, "NETLINK_DIAG_???",
188 netlink_diag_msg_nla_decoders,
189 ARRAY_SIZE(netlink_diag_msg_nla_decoders), NULL);