]> granicus.if.org Git - strace/blob - nlattr.c
xlat: add NETLINK_SMC
[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 static bool
40 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
41              const kernel_ulong_t addr, const unsigned int len)
42 {
43         if (len < sizeof(struct nlattr)) {
44                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
45                 return false;
46         }
47
48         if (umove_or_printaddr(tcp, addr, nlattr))
49                 return false;
50
51         return true;
52 }
53
54 static void
55 print_nlattr(const struct nlattr *const nla,
56              const struct xlat *const table,
57              const char *const dflt)
58 {
59         static_assert(NLA_TYPE_MASK == ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER),
60                       "wrong NLA_TYPE_MASK");
61
62         tprintf("{nla_len=%u, nla_type=", nla->nla_len);
63         if (nla->nla_type & NLA_F_NESTED) {
64                 print_xlat(NLA_F_NESTED);
65                 tprints("|");
66         }
67         if (nla->nla_type & NLA_F_NET_BYTEORDER) {
68                 print_xlat(NLA_F_NET_BYTEORDER);
69                 tprints("|");
70         }
71         printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
72         tprints("}");
73 }
74
75 static void
76 decode_nlattr_with_data(struct tcb *const tcp,
77                         const struct nlattr *const nla,
78                         const kernel_ulong_t addr,
79                         const unsigned int len,
80                         const struct xlat *const table,
81                         const char *const dflt,
82                         const nla_decoder_t *const decoders,
83                         const unsigned int size,
84                         const void *const opaque_data)
85 {
86         const unsigned int nla_len = nla->nla_len > len ? len : nla->nla_len;
87
88         if (nla_len > NLA_HDRLEN)
89                 tprints("{");
90
91         print_nlattr(nla, table, dflt);
92
93         if (nla_len > NLA_HDRLEN) {
94                 tprints(", ");
95                 if (!decoders
96                     || nla->nla_type >= size
97                     || !decoders[nla->nla_type]
98                     || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN,
99                                                 nla_len - NLA_HDRLEN,
100                                                 opaque_data))
101                         printstr_ex(tcp, addr + NLA_HDRLEN,
102                                     nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
103                 tprints("}");
104         }
105 }
106
107 void
108 decode_nlattr(struct tcb *const tcp,
109               kernel_ulong_t addr,
110               unsigned int len,
111               const struct xlat *const table,
112               const char *const dflt,
113               const nla_decoder_t *const decoders,
114               const unsigned int size,
115               const void *const opaque_data)
116 {
117         struct nlattr nla;
118         bool print_array = false;
119         unsigned int elt;
120
121         for (elt = 0; fetch_nlattr(tcp, &nla, addr, len); elt++) {
122                 if (abbrev(tcp) && elt == max_strlen) {
123                         tprints("...");
124                         break;
125                 }
126
127                 const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
128                 kernel_ulong_t next_addr = 0;
129                 unsigned int next_len = 0;
130
131                 if (nla.nla_len >= NLA_HDRLEN) {
132                         next_len = (len >= nla_len) ? len - nla_len : 0;
133
134                         if (next_len && addr + nla_len > addr)
135                                 next_addr = addr + nla_len;
136                 }
137
138                 if (!print_array && next_addr) {
139                         tprints("[");
140                         print_array = true;
141                 }
142
143                 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
144                                         decoders, size, opaque_data);
145
146                 if (!next_addr)
147                         break;
148
149                 tprints(", ");
150                 addr = next_addr;
151                 len = next_len;
152         }
153
154         if (print_array) {
155                 tprints("]");
156         }
157 }
158
159 bool
160 decode_nla_str(struct tcb *const tcp,
161                const kernel_ulong_t addr,
162                const unsigned int len,
163                const void *const opaque_data)
164 {
165         printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
166
167         return true;
168 }
169
170 bool
171 decode_nla_strn(struct tcb *const tcp,
172                 const kernel_ulong_t addr,
173                 const unsigned int len,
174                 const void *const opaque_data)
175 {
176         printstrn(tcp, addr, len);
177
178         return true;
179 }
180
181 static bool
182 print_meminfo(struct tcb *const tcp,
183               void *const elem_buf,
184               const size_t elem_size,
185               void *const opaque_data)
186 {
187         unsigned int *const count = opaque_data;
188
189         if ((*count)++ >= SK_MEMINFO_VARS) {
190                 tprints("...");
191                 return false;
192         }
193
194         tprintf("%" PRIu32, *(uint32_t *) elem_buf);
195
196         return true;
197 }
198
199 bool
200 decode_nla_meminfo(struct tcb *const tcp,
201                    const kernel_ulong_t addr,
202                    const unsigned int len,
203                    const void *const opaque_data)
204 {
205         uint32_t mem;
206         const size_t nmemb = len / sizeof(mem);
207
208         if (!nmemb)
209                 return false;
210
211         unsigned int count = 0;
212         print_array(tcp, addr, nmemb, &mem, sizeof(mem),
213                     umoven_or_printaddr, print_meminfo, &count);
214
215         return true;
216 }
217
218 bool
219 decode_nla_fd(struct tcb *const tcp,
220               const kernel_ulong_t addr,
221               const unsigned int len,
222               const void *const opaque_data)
223 {
224         int fd;
225
226         if (len < sizeof(fd))
227                 return false;
228         else if (!umove_or_printaddr(tcp, addr, &fd))
229                 printfd(tcp, fd);
230
231         return true;
232 }
233
234 bool
235 decode_nla_ifindex(struct tcb *const tcp,
236                const kernel_ulong_t addr,
237                const unsigned int len,
238                const void *const opaque_data)
239 {
240         uint32_t ifindex;
241
242         if (len < sizeof(ifindex))
243                 return false;
244         else if (!umove_or_printaddr(tcp, addr, &ifindex))
245                 print_ifindex(ifindex);
246
247         return true;
248 }
249
250 bool
251 decode_nla_be16(struct tcb *const tcp,
252                 const kernel_ulong_t addr,
253                 const unsigned int len,
254                 const void *const opaque_data)
255 {
256         uint16_t num;
257
258         if (len < sizeof(num))
259                 return false;
260         else if (!umove_or_printaddr(tcp, addr, &num))
261                 tprintf("htons(%u)", ntohs(num));
262
263         return true;
264 }
265
266 bool
267 decode_nla_be64(struct tcb *const tcp,
268                 const kernel_ulong_t addr,
269                 const unsigned int len,
270                 const void *const opaque_data)
271 {
272 #if defined HAVE_BE64TOH || defined be64toh
273         uint64_t num;
274
275         if (len < sizeof(num))
276                 return false;
277         else if (!umove_or_printaddr(tcp, addr, &num))
278                 tprintf("htobe64(%" PRIu64 ")", be64toh(num));
279
280         return true;
281 #else
282         return false;
283 #endif
284 }
285
286 #define DECODE_NLA_INTEGER(name, type, fmt)             \
287 bool                                                    \
288 decode_nla_ ## name(struct tcb *const tcp,              \
289                     const kernel_ulong_t addr,          \
290                     const unsigned int len,             \
291                     const void *const opaque_data)      \
292 {                                                       \
293         type num;                                       \
294                                                         \
295         if (len < sizeof(num))                          \
296                 return false;                           \
297         if (!umove_or_printaddr(tcp, addr, &num))       \
298                 tprintf(fmt, num);                      \
299         return true;                                    \
300 }
301
302 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
303 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
304 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
305 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
306 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
307 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
308 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
309 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)