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