]> granicus.if.org Git - strace/blob - netlink_packet_diag.c
xlat: treat socketlayers as a sorted array
[strace] / netlink_packet_diag.c
1 /*
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.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  */
29
30 #include "defs.h"
31 #include "netlink.h"
32 #include "netlink_sock_diag.h"
33 #include "nlattr.h"
34 #include "print_fields.h"
35
36 #include <linux/filter.h>
37 #include <linux/sock_diag.h>
38 #include <linux/packet_diag.h>
39
40 #include "xlat/packet_diag_attrs.h"
41 #include "xlat/packet_diag_info_flags.h"
42 #include "xlat/packet_diag_show.h"
43
44 DECL_NETLINK_DIAG_DECODER(decode_packet_diag_req)
45 {
46         struct packet_diag_req req = { .sdiag_family = family };
47         const size_t offset = sizeof(req.sdiag_family);
48
49         PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
50         tprints(", ");
51         if (len >= sizeof(req)) {
52                 if (!umoven_or_printaddr(tcp, addr + offset,
53                                          sizeof(req) - offset,
54                                          (char *) &req + offset)) {
55                         tprints("sdiag_protocol=");
56                         printxval_searchn(ethernet_protocols,
57                                           ethernet_protocols_size,
58                                           req.sdiag_protocol, "ETH_P_???");
59                         PRINT_FIELD_U(", ", req, pdiag_ino);
60                         PRINT_FIELD_FLAGS(", ", req, pdiag_show,
61                                           packet_diag_show, "PACKET_SHOW_???");
62                         PRINT_FIELD_COOKIE(", ", req, pdiag_cookie);
63                 }
64         } else
65                 tprints("...");
66         tprints("}");
67 }
68
69 static bool
70 decode_packet_diag_info(struct tcb *const tcp,
71                         const kernel_ulong_t addr,
72                         const unsigned int len,
73                         const void *const opaque_data)
74 {
75         struct packet_diag_info pinfo;
76
77         if (len < sizeof(pinfo))
78                 return false;
79         if (umove_or_printaddr(tcp, addr, &pinfo))
80                 return true;
81
82         PRINT_FIELD_U("{", pinfo, pdi_index);
83         PRINT_FIELD_U(", ", pinfo, pdi_version);
84         PRINT_FIELD_U(", ", pinfo, pdi_reserve);
85         PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh);
86         PRINT_FIELD_U(", ", pinfo, pdi_tstamp);
87         PRINT_FIELD_FLAGS(", ", pinfo, pdi_flags,
88                           packet_diag_info_flags, "PDI_???");
89         tprints("}");
90
91         return true;
92 }
93
94 static bool
95 print_packet_diag_mclist(struct tcb *const tcp, void *const elem_buf,
96                          const size_t elem_size, void *const opaque_data)
97 {
98         struct packet_diag_mclist *dml = elem_buf;
99         uint16_t alen = dml->pdmc_alen > sizeof(dml->pdmc_addr) ?
100                 sizeof(dml->pdmc_addr) : dml->pdmc_alen;
101
102         PRINT_FIELD_IFINDEX("{", *dml, pdmc_index);
103         PRINT_FIELD_U(", ", *dml, pdmc_count);
104         PRINT_FIELD_U(", ", *dml, pdmc_type);
105         PRINT_FIELD_U(", ", *dml, pdmc_alen);
106         PRINT_FIELD_STRING(", ", *dml, pdmc_addr, alen, QUOTE_FORCE_HEX);
107         tprints("}");
108
109         return true;
110 }
111
112 static bool
113 decode_packet_diag_mclist(struct tcb *const tcp,
114                           const kernel_ulong_t addr,
115                           const unsigned int len,
116                           const void *const opaque_data)
117 {
118         struct packet_diag_mclist dml;
119         const size_t nmemb = len / sizeof(dml);
120
121         if (!nmemb)
122                 return false;
123
124         print_array(tcp, addr, nmemb, &dml, sizeof(dml),
125                     umoven_or_printaddr, print_packet_diag_mclist, 0);
126
127         return true;
128 }
129
130 static bool
131 decode_packet_diag_ring(struct tcb *const tcp,
132                         const kernel_ulong_t addr,
133                         const unsigned int len,
134                         const void *const opaque_data)
135 {
136         struct packet_diag_ring pdr;
137
138         if (len < sizeof(pdr))
139                 return false;
140         if (umove_or_printaddr(tcp, addr, &pdr))
141                 return true;
142
143         PRINT_FIELD_U("{", pdr, pdr_block_size);
144         PRINT_FIELD_U(", ", pdr, pdr_block_nr);
145         PRINT_FIELD_U(", ", pdr, pdr_frame_size);
146         PRINT_FIELD_U(", ", pdr, pdr_frame_nr);
147         PRINT_FIELD_U(", ", pdr, pdr_retire_tmo);
148         PRINT_FIELD_U(", ", pdr, pdr_sizeof_priv);
149         PRINT_FIELD_U(", ", pdr, pdr_features);
150         tprints("}");
151
152         return true;
153 }
154
155 static bool
156 decode_packet_diag_filter(struct tcb *const tcp,
157                           const kernel_ulong_t addr,
158                           const unsigned int len,
159                           const void *const opaque_data)
160 {
161         const unsigned int nmemb = len / sizeof(struct sock_filter);
162         if (!nmemb || (unsigned short) nmemb != nmemb)
163                 return false;
164
165         print_sock_fprog(tcp, addr, nmemb);
166
167         return true;
168 }
169
170 static const nla_decoder_t packet_diag_msg_nla_decoders[] = {
171         [PACKET_DIAG_INFO]      = decode_packet_diag_info,
172         [PACKET_DIAG_MCLIST]    = decode_packet_diag_mclist,
173         [PACKET_DIAG_RX_RING]   = decode_packet_diag_ring,
174         [PACKET_DIAG_TX_RING]   = decode_packet_diag_ring,
175         [PACKET_DIAG_FANOUT]    = decode_nla_u32,
176         [PACKET_DIAG_UID]       = decode_nla_u32,
177         [PACKET_DIAG_MEMINFO]   = decode_nla_meminfo,
178         [PACKET_DIAG_FILTER]    = decode_packet_diag_filter
179 };
180
181 DECL_NETLINK_DIAG_DECODER(decode_packet_diag_msg)
182 {
183         struct packet_diag_msg msg = { .pdiag_family = family };
184         size_t offset = sizeof(msg.pdiag_family);
185         bool decode_nla = false;
186
187         PRINT_FIELD_XVAL("{", msg, pdiag_family, addrfams, "AF_???");
188         tprints(", ");
189         if (len >= sizeof(msg)) {
190                 if (!umoven_or_printaddr(tcp, addr + offset,
191                                          sizeof(msg) - offset,
192                                          (char *) &msg + offset)) {
193                         PRINT_FIELD_XVAL("", msg, pdiag_type,
194                                          socktypes, "SOCK_???");
195                         PRINT_FIELD_U(", ", msg, pdiag_num);
196                         PRINT_FIELD_U(", ", msg, pdiag_ino);
197                         PRINT_FIELD_COOKIE(", ", msg, pdiag_cookie);
198                         decode_nla = true;
199                 }
200         } else
201                 tprints("...");
202         tprints("}");
203
204         offset = NLMSG_ALIGN(sizeof(msg));
205         if (decode_nla && len > offset) {
206                 tprints(", ");
207                 decode_nlattr(tcp, addr + offset, len - offset,
208                               packet_diag_attrs, "PACKET_DIAG_???",
209                               packet_diag_msg_nla_decoders,
210                               ARRAY_SIZE(packet_diag_msg_nla_decoders), NULL);
211         }
212 }