]> granicus.if.org Git - strace/blob - rtnl_rule.c
Add PAF_ARRAY_TRUNCATED flag for print_array_ex
[strace] / rtnl_rule.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  * SPDX-License-Identifier: LGPL-2.1-or-later
8  */
9
10 #include "defs.h"
11
12 #include "netlink_route.h"
13 #include "nlattr.h"
14 #include "print_fields.h"
15
16 #include "netlink.h"
17 #include <linux/rtnetlink.h>
18 #ifdef HAVE_LINUX_FIB_RULES_H
19 # include <linux/fib_rules.h>
20 #endif
21
22 #include "xlat/fib_rule_actions.h"
23 #include "xlat/fib_rule_flags.h"
24 #include "xlat/rtnl_rule_attrs.h"
25
26 static bool
27 decode_rule_addr(struct tcb *const tcp,
28                  const kernel_ulong_t addr,
29                  const unsigned int len,
30                  const void *const opaque_data)
31 {
32         const struct rtmsg *const rtmsg = opaque_data;
33
34         decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
35
36         return true;
37 }
38
39 static bool
40 decode_fib_rule_uid_range(struct tcb *const tcp,
41                           const kernel_ulong_t addr,
42                           const unsigned int len,
43                           const void *const opaque_data)
44 {
45 #ifdef HAVE_STRUCT_FIB_RULE_UID_RANGE
46         struct fib_rule_uid_range range;
47
48         if (len < sizeof(range))
49                 return false;
50         else if (!umove_or_printaddr(tcp, addr, &range)) {
51                 PRINT_FIELD_U("{", range, start);
52                 PRINT_FIELD_U(", ", range, end);
53                 tprints("}");
54         }
55
56         return true;
57 #else
58         return false;
59 #endif
60 }
61
62 static bool
63 decode_rule_port_range(struct tcb *const tcp,
64                        const kernel_ulong_t addr,
65                        const unsigned int len,
66                        const void *const opaque_data)
67 {
68         struct /* fib_rule_port_range */ {
69                 uint16_t start;
70                 uint16_t end;
71         } range;
72
73         if (len < sizeof(range))
74                 return false;
75         else if (!umove_or_printaddr(tcp, addr, &range)) {
76                 PRINT_FIELD_U("{", range, start);
77                 PRINT_FIELD_U(", ", range, end);
78                 tprints("}");
79         }
80
81         return true;
82 }
83
84 static const nla_decoder_t fib_rule_hdr_nla_decoders[] = {
85         [FRA_DST]                       = decode_rule_addr,
86         [FRA_SRC]                       = decode_rule_addr,
87         [FRA_IIFNAME]                   = decode_nla_str,
88         [FRA_GOTO]                      = decode_nla_u32,
89         [FRA_PRIORITY]                  = decode_nla_u32,
90         [FRA_FWMARK]                    = decode_nla_u32,
91         [FRA_FLOW]                      = decode_nla_u32,
92         [FRA_TUN_ID]                    = decode_nla_be64,
93         [FRA_SUPPRESS_IFGROUP]          = decode_nla_u32,
94         [FRA_SUPPRESS_PREFIXLEN]        = decode_nla_u32,
95         [FRA_TABLE]                     = decode_nla_rt_class,
96         [FRA_FWMASK]                    = decode_nla_u32,
97         [FRA_OIFNAME]                   = decode_nla_str,
98         [FRA_PAD]                       = NULL,
99         [FRA_L3MDEV]                    = decode_nla_u8,
100         [FRA_UID_RANGE]                 = decode_fib_rule_uid_range,
101         [FRA_PROTOCOL]                  = decode_nla_rt_proto,
102         [FRA_IP_PROTO]                  = decode_nla_ip_proto,
103         [FRA_SPORT_RANGE]               = decode_rule_port_range,
104         [FRA_DPORT_RANGE]               = decode_rule_port_range,
105 };
106
107 DECL_NETLINK_ROUTE_DECODER(decode_fib_rule_hdr)
108 {
109         /*
110          * struct rtmsg and struct fib_rule_hdr are essentially
111          * the same structure, use struct rtmsg but treat it as
112          * struct fib_rule_hdr.
113          */
114         struct rtmsg msg = { .rtm_family = family };
115         size_t offset = sizeof(msg.rtm_family);
116         bool decode_nla = false;
117
118         tprints("{family=");
119         printxval(addrfams, msg.rtm_family, "AF_???");
120
121         tprints(", ");
122         if (len >= sizeof(msg)) {
123                 if (!umoven_or_printaddr(tcp, addr + offset,
124                                          sizeof(msg) - offset,
125                                          (char *) &msg + offset)) {
126                         tprintf("dst_len=%u, src_len=%u",
127                                 msg.rtm_dst_len, msg.rtm_src_len);
128                         tprints(", tos=");
129                         printflags(ip_type_of_services, msg.rtm_tos,
130                                    "IPTOS_TOS_???");
131                         tprints(", table=");
132                         printxval(routing_table_ids, msg.rtm_table, NULL);
133                         tprints(", action=");
134                         printxval(fib_rule_actions, msg.rtm_type, "FR_ACT_???");
135                         tprints(", flags=");
136                         printflags(fib_rule_flags, msg.rtm_flags,
137                                    "FIB_RULE_???");
138                         decode_nla = true;
139                 }
140         } else
141                 tprints("...");
142         tprints("}");
143
144         offset = NLMSG_ALIGN(sizeof(msg));
145         if (decode_nla && len > offset) {
146                 tprints(", ");
147                 decode_nlattr(tcp, addr + offset, len - offset,
148                               rtnl_rule_attrs, "FRA_???",
149                               fib_rule_hdr_nla_decoders,
150                               ARRAY_SIZE(fib_rule_hdr_nla_decoders), &msg);
151         }
152 }