]> granicus.if.org Git - strace/blob - rtnl_rule.c
rtnl_link: use internal rtnl_link_stats* and ifla_port_vsi definitions
[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         struct /* fib_rule_uid_range */ {
46                 uint32_t start;
47                 uint32_t end;
48         } range;
49
50         if (len < sizeof(range))
51                 return false;
52         else if (!umove_or_printaddr(tcp, addr, &range)) {
53                 PRINT_FIELD_U("{", range, start);
54                 PRINT_FIELD_U(", ", range, end);
55                 tprints("}");
56         }
57
58         return true;
59 }
60
61 static bool
62 decode_rule_port_range(struct tcb *const tcp,
63                        const kernel_ulong_t addr,
64                        const unsigned int len,
65                        const void *const opaque_data)
66 {
67         struct /* fib_rule_port_range */ {
68                 uint16_t start;
69                 uint16_t end;
70         } range;
71
72         if (len < sizeof(range))
73                 return false;
74         else if (!umove_or_printaddr(tcp, addr, &range)) {
75                 PRINT_FIELD_U("{", range, start);
76                 PRINT_FIELD_U(", ", range, end);
77                 tprints("}");
78         }
79
80         return true;
81 }
82
83 static const nla_decoder_t fib_rule_hdr_nla_decoders[] = {
84         [FRA_DST]                       = decode_rule_addr,
85         [FRA_SRC]                       = decode_rule_addr,
86         [FRA_IIFNAME]                   = decode_nla_str,
87         [FRA_GOTO]                      = decode_nla_u32,
88         [FRA_PRIORITY]                  = decode_nla_u32,
89         [FRA_FWMARK]                    = decode_nla_u32,
90         [FRA_FLOW]                      = decode_nla_u32,
91         [FRA_TUN_ID]                    = decode_nla_be64,
92         [FRA_SUPPRESS_IFGROUP]          = decode_nla_u32,
93         [FRA_SUPPRESS_PREFIXLEN]        = decode_nla_u32,
94         [FRA_TABLE]                     = decode_nla_rt_class,
95         [FRA_FWMASK]                    = decode_nla_u32,
96         [FRA_OIFNAME]                   = decode_nla_str,
97         [FRA_PAD]                       = NULL,
98         [FRA_L3MDEV]                    = decode_nla_u8,
99         [FRA_UID_RANGE]                 = decode_fib_rule_uid_range,
100         [FRA_PROTOCOL]                  = decode_nla_rt_proto,
101         [FRA_IP_PROTO]                  = decode_nla_ip_proto,
102         [FRA_SPORT_RANGE]               = decode_rule_port_range,
103         [FRA_DPORT_RANGE]               = decode_rule_port_range,
104 };
105
106 DECL_NETLINK_ROUTE_DECODER(decode_fib_rule_hdr)
107 {
108         /*
109          * struct rtmsg and struct fib_rule_hdr are essentially
110          * the same structure, use struct rtmsg but treat it as
111          * struct fib_rule_hdr.
112          */
113         struct rtmsg msg = { .rtm_family = family };
114         size_t offset = sizeof(msg.rtm_family);
115         bool decode_nla = false;
116
117         tprints("{family=");
118         printxval(addrfams, msg.rtm_family, "AF_???");
119
120         tprints(", ");
121         if (len >= sizeof(msg)) {
122                 if (!umoven_or_printaddr(tcp, addr + offset,
123                                          sizeof(msg) - offset,
124                                          (char *) &msg + offset)) {
125                         tprintf("dst_len=%u, src_len=%u",
126                                 msg.rtm_dst_len, msg.rtm_src_len);
127                         tprints(", tos=");
128                         printflags(ip_type_of_services, msg.rtm_tos,
129                                    "IPTOS_TOS_???");
130                         tprints(", table=");
131                         printxval(routing_table_ids, msg.rtm_table, NULL);
132                         tprints(", action=");
133                         printxval(fib_rule_actions, msg.rtm_type, "FR_ACT_???");
134                         tprints(", flags=");
135                         printflags(fib_rule_flags, msg.rtm_flags,
136                                    "FIB_RULE_???");
137                         decode_nla = true;
138                 }
139         } else
140                 tprints("...");
141         tprints("}");
142
143         offset = NLMSG_ALIGN(sizeof(msg));
144         if (decode_nla && len > offset) {
145                 tprints(", ");
146                 decode_nlattr(tcp, addr + offset, len - offset,
147                               rtnl_rule_attrs, "FRA_???",
148                               fib_rule_hdr_nla_decoders,
149                               ARRAY_SIZE(fib_rule_hdr_nla_decoders), &msg);
150         }
151 }