]> granicus.if.org Git - strace/blob - netlink_inet_diag.c
netlink_inet_diag: prepare decode_inet_addr for export
[strace] / netlink_inet_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 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 <arpa/inet.h>
37
38 #include <linux/sock_diag.h>
39 #include <linux/inet_diag.h>
40
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"
45
46 #include "xlat/tcp_states.h"
47 #include "xlat/tcp_state_flags.h"
48
49 void
50 print_inet_diag_sockid(const struct inet_diag_sockid *id, const uint8_t family)
51 {
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);
58         tprints("}");
59 }
60
61 static bool
62 decode_inet_addr(struct tcb *const tcp,
63                  const kernel_ulong_t addr,
64                  const unsigned int len,
65                  const int family,
66                  const char *const var_name)
67 {
68         union {
69                 struct in_addr  a4;
70                 struct in6_addr a6;
71         } addrbuf;
72         size_t size = 0;
73
74         switch (family) {
75         case AF_INET:
76                 size = sizeof(addrbuf.a4);
77                 break;
78         case AF_INET6:
79                 size = sizeof(addrbuf.a6);
80                 break;
81         }
82
83         if (!size || len < size) {
84                 if (var_name)
85                         tprintf("%s=", var_name);
86                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
87                 return false;
88         }
89
90         if (umoven(tcp, addr, size, &addrbuf) < 0) {
91                 if (var_name)
92                         tprintf("%s=", var_name);
93                 printaddr(addr);
94                 return false;
95         }
96
97         return print_inet_addr(family, &addrbuf, size, var_name);
98 }
99
100 static void
101 decode_inet_diag_hostcond(struct tcb *const tcp,
102                           const kernel_ulong_t addr,
103                           const unsigned int len)
104 {
105         struct inet_diag_hostcond cond;
106
107         if (len < sizeof(cond)) {
108                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
109                 return;
110         }
111         if (umove_or_printaddr(tcp, addr, &cond))
112                 return;
113
114         PRINT_FIELD_XVAL("{", cond, family, addrfams, "AF_???");
115         PRINT_FIELD_U(", ", cond, prefix_len);
116         PRINT_FIELD_U(", ", cond, port);
117
118         if (len > sizeof(cond)) {
119                 tprints(", ");
120                 decode_inet_addr(tcp, addr + sizeof(cond),
121                                  len - sizeof(cond), cond.family, "addr");
122         }
123         tprints("}");
124 }
125
126 static void
127 print_inet_diag_bc_op(const struct inet_diag_bc_op *const op)
128 {
129         PRINT_FIELD_XVAL("{", *op, code, inet_diag_bytecodes,
130                          "INET_DIAG_BC_???");
131         PRINT_FIELD_U(", ", *op, yes);
132         PRINT_FIELD_U(", ", *op, no);
133         tprints("}");
134 }
135
136 static void
137 decode_inet_diag_markcond(struct tcb *const tcp,
138                           const kernel_ulong_t addr,
139                           const unsigned int len)
140 {
141         struct inet_diag_markcond markcond;
142
143         if (len < sizeof(markcond))
144                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
145         else if (!umove_or_printaddr(tcp, addr, &markcond)) {
146                 PRINT_FIELD_U("{", markcond, mark);
147                 PRINT_FIELD_U(", ", markcond, mask);
148                 tprints("}");
149         }
150 }
151
152 static void
153 decode_bytecode_data(struct tcb *const tcp,
154                      const kernel_ulong_t addr,
155                      const unsigned int len,
156                      const unsigned char code)
157 {
158         switch (code) {
159         case INET_DIAG_BC_S_COND:
160         case INET_DIAG_BC_D_COND:
161                 decode_inet_diag_hostcond(tcp, addr, len);
162                 break;
163         case INET_DIAG_BC_DEV_COND: {
164                 uint32_t ifindex;
165
166                 if (len < sizeof(ifindex))
167                         printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
168                 else if (!umove_or_printaddr(tcp, addr, &ifindex))
169                         print_ifindex(ifindex);
170                 break;
171         }
172         case INET_DIAG_BC_S_GE:
173         case INET_DIAG_BC_S_LE:
174         case INET_DIAG_BC_D_GE:
175         case INET_DIAG_BC_D_LE: {
176                 struct inet_diag_bc_op op;
177
178                 if (len < sizeof(op))
179                         printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
180                 else if (!umove_or_printaddr(tcp, addr, &op))
181                         print_inet_diag_bc_op(&op);
182                 break;
183         }
184         case INET_DIAG_BC_MARK_COND:
185                 decode_inet_diag_markcond(tcp, addr, len);
186                 break;
187         case INET_DIAG_BC_AUTO:
188         case INET_DIAG_BC_JMP:
189         case INET_DIAG_BC_NOP:
190         default:
191                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
192                 break;
193         }
194 }
195
196 static bool
197 decode_inet_diag_bc_op(struct tcb *const tcp,
198                        const kernel_ulong_t addr,
199                        const unsigned int len,
200                        const void *const opaque_data)
201 {
202         struct inet_diag_bc_op op;
203
204         if (len < sizeof(op))
205                 return false;
206         if (umove_or_printaddr(tcp, addr, &op))
207                 return true;
208
209         if (len > sizeof(op))
210                 tprints("{");
211
212         print_inet_diag_bc_op(&op);
213
214         if (len > sizeof(op)) {
215                 tprints(", ");
216                 decode_bytecode_data(tcp, addr + sizeof(op),
217                                      len - sizeof(op), op.code);
218                 tprints("}");
219         }
220
221         return true;
222 }
223
224 static const nla_decoder_t inet_diag_req_nla_decoders[] = {
225         [INET_DIAG_REQ_BYTECODE] = decode_inet_diag_bc_op
226 };
227
228 static void
229 decode_inet_diag_req_compat(struct tcb *const tcp,
230                             const struct nlmsghdr *const nlmsghdr,
231                             const uint8_t family,
232                             const kernel_ulong_t addr,
233                             const unsigned int len)
234 {
235         struct inet_diag_req req = { .idiag_family = family };
236         size_t offset = sizeof(req.idiag_family);
237         bool decode_nla = false;
238
239         PRINT_FIELD_XVAL("{", req, idiag_family, addrfams, "AF_???");
240         tprints(", ");
241         if (len >= sizeof(req)) {
242                 if (!umoven_or_printaddr(tcp, addr + offset,
243                                          sizeof(req) - offset,
244                                          (void *) &req + offset)) {
245                         PRINT_FIELD_U("", req, idiag_src_len);
246                         PRINT_FIELD_U(", ", req, idiag_dst_len);
247                         PRINT_FIELD_FLAGS(", ", req, idiag_ext,
248                                           inet_diag_extended_flags,
249                                           "1<<INET_DIAG_\?\?\?-1");
250                         PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
251                                                      req.idiag_family);
252                         PRINT_FIELD_FLAGS(", ", req, idiag_states,
253                                           tcp_state_flags, "1<<TCP_???");
254                         PRINT_FIELD_U(", ", req, idiag_dbs);
255                         decode_nla = true;
256                 }
257         } else
258                 tprints("...");
259         tprints("}");
260
261         offset = NLMSG_ALIGN(sizeof(req));
262         if (decode_nla && len > offset) {
263                 tprints(", ");
264                 decode_nlattr(tcp, addr + offset, len - offset,
265                               inet_diag_req_attrs, "INET_DIAG_REQ_???",
266                               inet_diag_req_nla_decoders,
267                               ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
268         }
269 }
270
271 static void
272 decode_inet_diag_req_v2(struct tcb *const tcp,
273                         const struct nlmsghdr *const nlmsghdr,
274                         const uint8_t family,
275                         const kernel_ulong_t addr,
276                         const unsigned int len)
277 {
278         struct inet_diag_req_v2 req = { .sdiag_family = family };
279         size_t offset = sizeof(req.sdiag_family);
280         bool decode_nla = false;
281
282         PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
283         tprints(", ");
284         if (len >= sizeof(req)) {
285                 if (!umoven_or_printaddr(tcp, addr + offset,
286                                          sizeof(req) - offset,
287                                          (void *) &req + offset)) {
288                         PRINT_FIELD_XVAL("", req, sdiag_protocol,
289                                          inet_protocols, "IPPROTO_???");
290                         PRINT_FIELD_FLAGS(", ", req, idiag_ext,
291                                           inet_diag_extended_flags,
292                                           "1<<INET_DIAG_\?\?\?-1");
293                         PRINT_FIELD_FLAGS(", ", req, idiag_states,
294                                           tcp_state_flags, "1<<TCP_???");
295                         PRINT_FIELD_INET_DIAG_SOCKID(", ", req, id,
296                                                      req.sdiag_family);
297                         decode_nla = true;
298                 }
299         } else
300                 tprints("...");
301         tprints("}");
302
303         offset = NLMSG_ALIGN(sizeof(req));
304         if (decode_nla && len > offset) {
305                 tprints(", ");
306                 decode_nlattr(tcp, addr + offset, len - offset,
307                               inet_diag_req_attrs, "INET_DIAG_REQ_???",
308                               inet_diag_req_nla_decoders,
309                               ARRAY_SIZE(inet_diag_req_nla_decoders), NULL);
310         }
311 }
312
313 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_req)
314 {
315         if (nlmsghdr->nlmsg_type == TCPDIAG_GETSOCK
316             || nlmsghdr->nlmsg_type == DCCPDIAG_GETSOCK)
317                 return decode_inet_diag_req_compat(tcp, nlmsghdr,
318                                                    family, addr, len);
319         else
320                 return decode_inet_diag_req_v2(tcp, nlmsghdr,
321                                                family, addr, len);
322 }
323
324 static bool
325 decode_inet_diag_meminfo(struct tcb *const tcp,
326                          const kernel_ulong_t addr,
327                          const unsigned int len,
328                          const void *const opaque_data)
329 {
330         struct inet_diag_meminfo minfo;
331
332         if (len < sizeof(minfo))
333                 return false;
334         if (umove_or_printaddr(tcp, addr, &minfo))
335                 return true;
336
337         PRINT_FIELD_U("{", minfo, idiag_rmem);
338         PRINT_FIELD_U(", ", minfo, idiag_wmem);
339         PRINT_FIELD_U(", ", minfo, idiag_fmem);
340         PRINT_FIELD_U(", ", minfo, idiag_tmem);
341         tprints("}");
342
343         return true;
344 }
345
346 static bool
347 decode_tcpvegas_info(struct tcb *const tcp,
348                      const kernel_ulong_t addr,
349                      const unsigned int len,
350                      const void *const opaque_data)
351 {
352         struct tcpvegas_info vegas;
353
354         if (len < sizeof(vegas))
355                 return false;
356         if (umove_or_printaddr(tcp, addr, &vegas))
357                 return true;
358
359         PRINT_FIELD_U("{", vegas, tcpv_enabled);
360         PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
361         PRINT_FIELD_U(", ", vegas, tcpv_rtt);
362         PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
363         tprints("}");
364
365         return true;
366 }
367
368 static bool
369 decode_tcp_dctcp_info(struct tcb *const tcp,
370                       const kernel_ulong_t addr,
371                       const unsigned int len,
372                       const void *const opaque_data)
373 {
374         struct tcp_dctcp_info dctcp;
375
376         if (len < sizeof(dctcp))
377                 return false;
378         if (umove_or_printaddr(tcp, addr, &dctcp))
379                 return true;
380
381         PRINT_FIELD_U("{", dctcp, dctcp_enabled);
382         PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
383         PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
384         PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
385         PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
386         tprints("}");
387
388         return true;
389 }
390
391 static bool
392 decode_tcp_bbr_info(struct tcb *const tcp,
393                     const kernel_ulong_t addr,
394                     const unsigned int len,
395                     const void *const opaque_data)
396 {
397         struct tcp_bbr_info bbr;
398
399         if (len < sizeof(bbr))
400                 return false;
401         if (umove_or_printaddr(tcp, addr, &bbr))
402                 return true;
403
404         PRINT_FIELD_X("{", bbr, bbr_bw_lo);
405         PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
406         PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
407         PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
408         PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
409         tprints("}");
410
411         return true;
412 }
413
414 static const nla_decoder_t inet_diag_msg_nla_decoders[] = {
415         [INET_DIAG_MEMINFO]     = decode_inet_diag_meminfo,
416         [INET_DIAG_INFO]        = NULL,                 /* unimplemented */
417         [INET_DIAG_VEGASINFO]   = decode_tcpvegas_info,
418         [INET_DIAG_CONG]        = decode_nla_str,
419         [INET_DIAG_TOS]         = decode_nla_u8,
420         [INET_DIAG_TCLASS]      = decode_nla_u8,
421         [INET_DIAG_SKMEMINFO]   = decode_nla_meminfo,
422         [INET_DIAG_SHUTDOWN]    = decode_nla_u8,
423         [INET_DIAG_DCTCPINFO]   = decode_tcp_dctcp_info,
424         [INET_DIAG_PROTOCOL]    = decode_nla_u8,
425         [INET_DIAG_SKV6ONLY]    = decode_nla_u8,
426         [INET_DIAG_LOCALS]      = NULL,                 /* unimplemented */
427         [INET_DIAG_PEERS]       = NULL,                 /* unimplemented */
428         [INET_DIAG_PAD]         = NULL,
429         [INET_DIAG_MARK]        = decode_nla_u32,
430         [INET_DIAG_BBRINFO]     = decode_tcp_bbr_info
431 };
432
433 DECL_NETLINK_DIAG_DECODER(decode_inet_diag_msg)
434 {
435         struct inet_diag_msg msg = { .idiag_family = family };
436         size_t offset = sizeof(msg.idiag_family);
437         bool decode_nla = false;
438
439         PRINT_FIELD_XVAL("{", msg, idiag_family, addrfams, "AF_???");
440         tprints(", ");
441         if (len >= sizeof(msg)) {
442                 if (!umoven_or_printaddr(tcp, addr + offset,
443                                          sizeof(msg) - offset,
444                                          (void *) &msg + offset)) {
445                         PRINT_FIELD_XVAL("", msg, idiag_state,
446                                          tcp_states, "TCP_???");
447                         PRINT_FIELD_U(", ", msg, idiag_timer);
448                         PRINT_FIELD_U(", ", msg, idiag_retrans);
449                         PRINT_FIELD_INET_DIAG_SOCKID(", ", msg, id,
450                                                      msg.idiag_family);
451                         PRINT_FIELD_U(", ", msg, idiag_expires);
452                         PRINT_FIELD_U(", ", msg, idiag_rqueue);
453                         PRINT_FIELD_U(", ", msg, idiag_wqueue);
454                         PRINT_FIELD_U(", ", msg, idiag_uid);
455                         PRINT_FIELD_U(", ", msg, idiag_inode);
456                         decode_nla = true;
457                 }
458         } else
459                 tprints("...");
460         tprints("}");
461
462         offset = NLMSG_ALIGN(sizeof(msg));
463         if (decode_nla && len > offset) {
464                 tprints(", ");
465                 decode_nlattr(tcp, addr + offset, len - offset,
466                               inet_diag_attrs, "INET_DIAG_???",
467                               inet_diag_msg_nla_decoders,
468                               ARRAY_SIZE(inet_diag_msg_nla_decoders), NULL);
469         }
470 }