]> granicus.if.org Git - strace/blob - nlattr.c
rtnl_rule: decode new FRA_* attributes
[strace] / nlattr.c
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4  * Copyright (c) 2016-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 <endian.h>
32 #include "netlink.h"
33 #include "nlattr.h"
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <linux/sock_diag.h>
37 #include "static_assert.h"
38
39 #include "xlat/netlink_sk_meminfo_indices.h"
40
41 static bool
42 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
43              const kernel_ulong_t addr, const unsigned int len,
44              const bool in_array)
45 {
46         if (len < sizeof(struct nlattr)) {
47                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
48                 return false;
49         }
50
51         if (tfetch_obj(tcp, addr, nlattr))
52                 return true;
53
54         if (in_array) {
55                 tprints("...");
56                 printaddr_comment(addr);
57         } else {
58                 printaddr(addr);
59         }
60
61         return false;
62 }
63
64 static void
65 print_nlattr(const struct nlattr *const nla,
66              const struct xlat *const table,
67              const char *const dflt)
68 {
69         static_assert(NLA_TYPE_MASK == ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER),
70                       "wrong NLA_TYPE_MASK");
71
72         tprintf("{nla_len=%u, nla_type=", nla->nla_len);
73         if (nla->nla_type & NLA_F_NESTED) {
74                 print_xlat(NLA_F_NESTED);
75                 tprints("|");
76         }
77         if (nla->nla_type & NLA_F_NET_BYTEORDER) {
78                 print_xlat(NLA_F_NET_BYTEORDER);
79                 tprints("|");
80         }
81         printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
82         tprints("}");
83 }
84
85 static void
86 decode_nlattr_with_data(struct tcb *const tcp,
87                         const struct nlattr *const nla,
88                         const kernel_ulong_t addr,
89                         const unsigned int len,
90                         const struct xlat *const table,
91                         const char *const dflt,
92                         const nla_decoder_t *const decoders,
93                         const unsigned int size,
94                         const void *const opaque_data)
95 {
96         const unsigned int nla_len = MIN(nla->nla_len, len);
97
98         if (nla_len > NLA_HDRLEN)
99                 tprints("{");
100
101         print_nlattr(nla, table, dflt);
102
103         if (nla_len > NLA_HDRLEN) {
104                 tprints(", ");
105                 if (!decoders
106                     || nla->nla_type >= size
107                     || !decoders[nla->nla_type]
108                     || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN,
109                                                 nla_len - NLA_HDRLEN,
110                                                 opaque_data))
111                         printstr_ex(tcp, addr + NLA_HDRLEN,
112                                     nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
113                 tprints("}");
114         }
115 }
116
117 void
118 decode_nlattr(struct tcb *const tcp,
119               kernel_ulong_t addr,
120               unsigned int len,
121               const struct xlat *const table,
122               const char *const dflt,
123               const nla_decoder_t *const decoders,
124               const unsigned int size,
125               const void *const opaque_data)
126 {
127         struct nlattr nla;
128         bool is_array = false;
129         unsigned int elt;
130
131         for (elt = 0; fetch_nlattr(tcp, &nla, addr, len, is_array); elt++) {
132                 if (abbrev(tcp) && elt == max_strlen) {
133                         tprints("...");
134                         break;
135                 }
136
137                 const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
138                 kernel_ulong_t next_addr = 0;
139                 unsigned int next_len = 0;
140
141                 if (nla.nla_len >= NLA_HDRLEN) {
142                         next_len = (len >= nla_len) ? len - nla_len : 0;
143
144                         if (next_len && addr + nla_len > addr)
145                                 next_addr = addr + nla_len;
146                 }
147
148                 if (!is_array && next_addr) {
149                         tprints("[");
150                         is_array = true;
151                 }
152
153                 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
154                                         decoders, size, opaque_data);
155
156                 if (!next_addr)
157                         break;
158
159                 tprints(", ");
160                 addr = next_addr;
161                 len = next_len;
162         }
163
164         if (is_array) {
165                 tprints("]");
166         }
167 }
168
169 bool
170 decode_nla_str(struct tcb *const tcp,
171                const kernel_ulong_t addr,
172                const unsigned int len,
173                const void *const opaque_data)
174 {
175         printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
176
177         return true;
178 }
179
180 bool
181 decode_nla_strn(struct tcb *const tcp,
182                 const kernel_ulong_t addr,
183                 const unsigned int len,
184                 const void *const opaque_data)
185 {
186         printstrn(tcp, addr, len);
187
188         return true;
189 }
190
191 bool
192 decode_nla_meminfo(struct tcb *const tcp,
193                    const kernel_ulong_t addr,
194                    const unsigned int len,
195                    const void *const opaque_data)
196 {
197         uint32_t mem;
198         const size_t nmemb = len / sizeof(mem);
199
200         if (!nmemb)
201                 return false;
202
203         unsigned int count = 0;
204         print_array_ex(tcp, addr, nmemb, &mem, sizeof(mem),
205                        tfetch_mem, print_uint32_array_member, &count,
206                        PAF_PRINT_INDICES | PAF_INDEX_XLAT_VALUE_INDEXED
207                         | XLAT_STYLE_FMT_U,
208                        ARRSZ_PAIR(netlink_sk_meminfo_indices),
209                        "SK_MEMINFO_???");
210
211         return true;
212 }
213
214 bool
215 decode_nla_fd(struct tcb *const tcp,
216               const kernel_ulong_t addr,
217               const unsigned int len,
218               const void *const opaque_data)
219 {
220         int fd;
221
222         if (len < sizeof(fd))
223                 return false;
224         else if (!umove_or_printaddr(tcp, addr, &fd))
225                 printfd(tcp, fd);
226
227         return true;
228 }
229
230 bool
231 decode_nla_ifindex(struct tcb *const tcp,
232                const kernel_ulong_t addr,
233                const unsigned int len,
234                const void *const opaque_data)
235 {
236         uint32_t ifindex;
237
238         if (len < sizeof(ifindex))
239                 return false;
240         else if (!umove_or_printaddr(tcp, addr, &ifindex))
241                 print_ifindex(ifindex);
242
243         return true;
244 }
245
246 bool
247 decode_nla_xval(struct tcb *const tcp,
248                 const kernel_ulong_t addr,
249                 const unsigned int len,
250                 const void *const opaque_data)
251 {
252         const struct decode_nla_xlat_opts * const opts = opaque_data;
253         union {
254                 uint64_t val;
255                 uint8_t  bytes[sizeof(uint64_t)];
256         } data;
257         const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
258
259         data.val = 0;
260
261         if (len > sizeof(data))
262                 return false;
263         else if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs))
264                 printxval_dispatch_ex(opts->xlat, opts->xlat_size, data.val,
265                                       opts->dflt, opts->xt, opts->style);
266
267         return true;
268 }
269
270 bool
271 decode_nla_ip_proto(struct tcb *const tcp,
272                     const kernel_ulong_t addr,
273                     const unsigned int len,
274                     const void *const opaque_data)
275 {
276         static const struct decode_nla_xlat_opts opts = {
277                 .xlat = inet_protocols, .dflt = "IPPROTO_???",
278         };
279
280         return decode_nla_xval(tcp, addr, len, &opts);
281 }
282
283 bool
284 decode_nla_be16(struct tcb *const tcp,
285                 const kernel_ulong_t addr,
286                 const unsigned int len,
287                 const void *const opaque_data)
288 {
289         uint16_t num;
290
291         if (len < sizeof(num))
292                 return false;
293         else if (!umove_or_printaddr(tcp, addr, &num))
294                 tprintf("htons(%u)", ntohs(num));
295
296         return true;
297 }
298
299 bool
300 decode_nla_be64(struct tcb *const tcp,
301                 const kernel_ulong_t addr,
302                 const unsigned int len,
303                 const void *const opaque_data)
304 {
305 #if defined HAVE_BE64TOH || defined be64toh
306         uint64_t num;
307
308         if (len < sizeof(num))
309                 return false;
310         else if (!umove_or_printaddr(tcp, addr, &num))
311                 tprintf("htobe64(%" PRIu64 ")", be64toh(num));
312
313         return true;
314 #else
315         return false;
316 #endif
317 }
318
319 #define DECODE_NLA_INTEGER(name, type, fmt)             \
320 bool                                                    \
321 decode_nla_ ## name(struct tcb *const tcp,              \
322                     const kernel_ulong_t addr,          \
323                     const unsigned int len,             \
324                     const void *const opaque_data)      \
325 {                                                       \
326         type num;                                       \
327                                                         \
328         if (len < sizeof(num))                          \
329                 return false;                           \
330         if (!umove_or_printaddr(tcp, addr, &num))       \
331                 tprintf(fmt, num);                      \
332         return true;                                    \
333 }
334
335 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
336 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
337 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
338 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
339 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
340 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
341 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
342 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)