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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include "netlink_route.h"
33 #include "print_fields.h"
37 #include <linux/rtnetlink.h>
39 #include "xlat/ip_type_of_services.h"
40 #include "xlat/lwtunnel_encap_types.h"
41 #include "xlat/route_nexthop_flags.h"
42 #include "xlat/routing_flags.h"
43 #include "xlat/routing_protocols.h"
44 #include "xlat/routing_table_ids.h"
45 #include "xlat/routing_types.h"
46 #include "xlat/rtnl_route_attrs.h"
47 #include "xlat/rtnl_rta_metrics_attrs.h"
50 decode_nla_rt_class(struct tcb *const tcp,
51 const kernel_ulong_t addr,
52 const unsigned int len,
53 const void *const opaque_data)
57 if (len < sizeof(num))
59 if (!umove_or_printaddr(tcp, addr, &num))
60 printxval(routing_table_ids, num, NULL);
65 decode_nla_rt_proto(struct tcb *const tcp,
66 const kernel_ulong_t addr,
67 const unsigned int len,
68 const void *const opaque_data)
72 if (len < sizeof(num))
74 if (!umove_or_printaddr(tcp, addr, &num))
75 printxval_search(routing_protocols, num, "RTPROT_???");
80 decode_route_addr(struct tcb *const tcp,
81 const kernel_ulong_t addr,
82 const unsigned int len,
83 const void *const opaque_data)
85 const struct rtmsg *const rtmsg = opaque_data;
87 decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
92 static const nla_decoder_t rta_metrics_nla_decoders[] = {
93 [RTAX_LOCK] = decode_nla_u32,
94 [RTAX_MTU] = decode_nla_u32,
95 [RTAX_WINDOW] = decode_nla_u32,
96 [RTAX_RTT] = decode_nla_u32,
97 [RTAX_RTTVAR] = decode_nla_u32,
98 [RTAX_SSTHRESH] = decode_nla_u32,
99 [RTAX_CWND] = decode_nla_u32,
100 [RTAX_ADVMSS] = decode_nla_u32,
101 [RTAX_REORDERING] = decode_nla_u32,
102 [RTAX_HOPLIMIT] = decode_nla_u32,
103 [RTAX_INITCWND] = decode_nla_u32,
104 [RTAX_FEATURES] = decode_nla_u32,
105 [RTAX_RTO_MIN] = decode_nla_u32,
106 [RTAX_INITRWND] = decode_nla_u32,
107 [RTAX_QUICKACK] = decode_nla_u32,
108 [RTAX_CC_ALGO] = decode_nla_str
112 decode_rta_metrics(struct tcb *const tcp,
113 const kernel_ulong_t addr,
114 const unsigned int len,
115 const void *const opaque_data)
117 decode_nlattr(tcp, addr, len, rtnl_rta_metrics_attrs,
118 "RTAX_???", rta_metrics_nla_decoders,
119 ARRAY_SIZE(rta_metrics_nla_decoders), opaque_data);
125 decode_rta_multipath(struct tcb *const tcp,
126 const kernel_ulong_t addr,
127 const unsigned int len,
128 const void *const opaque_data);
131 decode_rta_cacheinfo(struct tcb *const tcp,
132 const kernel_ulong_t addr,
133 const unsigned int len,
134 const void *const opaque_data)
136 struct rta_cacheinfo ci;
138 if (len < sizeof(ci))
140 else if (!umove_or_printaddr(tcp, addr, &ci)) {
141 PRINT_FIELD_U("{", ci, rta_clntref);
142 PRINT_FIELD_U(", ", ci, rta_lastuse);
143 PRINT_FIELD_U(", ", ci, rta_expires);
144 PRINT_FIELD_U(", ", ci, rta_error);
145 PRINT_FIELD_U(", ", ci, rta_used);
146 PRINT_FIELD_X(", ", ci, rta_id);
147 PRINT_FIELD_U(", ", ci, rta_ts);
148 PRINT_FIELD_U(", ", ci, rta_tsage);
156 decode_rta_mfc_stats(struct tcb *const tcp,
157 const kernel_ulong_t addr,
158 const unsigned int len,
159 const void *const opaque_data)
161 #ifdef HAVE_STRUCT_RTA_MFC_STATS
162 struct rta_mfc_stats mfcs;
164 if (len < sizeof(mfcs))
166 else if (!umove_or_printaddr(tcp, addr, &mfcs)) {
167 PRINT_FIELD_U("{", mfcs, mfcs_packets);
168 PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
169 PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
180 decode_rtvia(struct tcb *const tcp,
181 const kernel_ulong_t addr,
182 const unsigned int len,
183 const void *const opaque_data)
185 #ifdef HAVE_STRUCT_RTVIA
188 if (len < sizeof(via))
190 else if (!umove_or_printaddr(tcp, addr, &via)) {
191 PRINT_FIELD_XVAL("{", via, rtvia_family, addrfams, "AF_???");
193 const unsigned int offset = offsetof(struct rtvia, rtvia_addr);
197 decode_inet_addr(tcp, addr + offset, len - offset,
198 via.rtvia_family, "rtvia_addr");
210 decode_rta_encap_type(struct tcb *const tcp,
211 const kernel_ulong_t addr,
212 const unsigned int len,
213 const void *const opaque_data)
217 if (len < sizeof(type))
219 else if (!umove_or_printaddr(tcp, addr, &type))
220 printxval(lwtunnel_encap_types, type, "LWTUNNEL_ENCAP_???");
225 static const nla_decoder_t rtmsg_nla_decoders[] = {
226 [RTA_DST] = decode_route_addr,
227 [RTA_SRC] = decode_route_addr,
228 [RTA_IIF] = decode_nla_ifindex,
229 [RTA_OIF] = decode_nla_ifindex,
230 [RTA_GATEWAY] = decode_route_addr,
231 [RTA_PRIORITY] = decode_nla_u32,
232 [RTA_PREFSRC] = decode_route_addr,
233 [RTA_METRICS] = decode_rta_metrics,
234 [RTA_MULTIPATH] = decode_rta_multipath,
235 [RTA_PROTOINFO] = decode_nla_u32,
236 [RTA_FLOW] = decode_nla_u32,
237 [RTA_CACHEINFO] = decode_rta_cacheinfo,
238 [RTA_SESSION] = NULL, /* unused */
239 [RTA_MP_ALGO] = decode_nla_u32,
240 [RTA_TABLE] = decode_nla_rt_class,
241 [RTA_MARK] = decode_nla_u32,
242 [RTA_MFC_STATS] = decode_rta_mfc_stats,
243 [RTA_VIA] = decode_rtvia,
244 [RTA_NEWDST] = decode_route_addr,
245 [RTA_PREF] = decode_nla_u8,
246 [RTA_ENCAP_TYPE] = decode_rta_encap_type,
247 [RTA_ENCAP] = NULL, /* unimplemented */
248 [RTA_EXPIRES] = decode_nla_u64,
250 [RTA_UID] = decode_nla_u32,
251 [RTA_TTL_PROPAGATE] = decode_nla_u8
255 decode_rta_multipath(struct tcb *const tcp,
256 const kernel_ulong_t addr,
257 const unsigned int len,
258 const void *const opaque_data)
262 if (len < sizeof(nh))
264 else if (!umove_or_printaddr(tcp, addr, &nh)) {
265 /* print the whole structure regardless of its rtnh_len */
266 PRINT_FIELD_U("{", nh, rtnh_len);
267 PRINT_FIELD_FLAGS(", ", nh, rtnh_flags,
268 route_nexthop_flags, "RTNH_F_???");
269 PRINT_FIELD_U(", ", nh, rtnh_hops);
270 PRINT_FIELD_IFINDEX(", ", nh, rtnh_ifindex);
273 const unsigned short rtnh_len = MIN(len, nh.rtnh_len);
274 const size_t offset = RTNH_ALIGN(sizeof(nh));
275 if (rtnh_len > offset) {
277 decode_nlattr(tcp, addr + offset, rtnh_len - offset,
278 rtnl_route_attrs, "RTA_???",
280 ARRAY_SIZE(rtmsg_nla_decoders),
288 DECL_NETLINK_ROUTE_DECODER(decode_rtmsg)
290 struct rtmsg rtmsg = { .rtm_family = family };
291 size_t offset = sizeof(rtmsg.rtm_family);
292 bool decode_nla = false;
294 PRINT_FIELD_XVAL("{", rtmsg, rtm_family, addrfams, "AF_???");
297 if (len >= sizeof(rtmsg)) {
298 if (!umoven_or_printaddr(tcp, addr + offset,
299 sizeof(rtmsg) - offset,
300 (char *) &rtmsg + offset)) {
301 PRINT_FIELD_U("", rtmsg, rtm_dst_len);
302 PRINT_FIELD_U(", ", rtmsg, rtm_src_len);
303 PRINT_FIELD_FLAGS(", ", rtmsg, rtm_tos,
304 ip_type_of_services, "IPTOS_TOS_???");
305 PRINT_FIELD_XVAL(", ", rtmsg, rtm_table,
306 routing_table_ids, NULL);
307 PRINT_FIELD_XVAL(", ", rtmsg, rtm_protocol,
308 routing_protocols, "RTPROT_???");
309 PRINT_FIELD_XVAL(", ", rtmsg, rtm_scope,
310 routing_scopes, NULL);
311 PRINT_FIELD_XVAL(", ", rtmsg, rtm_type,
312 routing_types, "RTN_???");
313 PRINT_FIELD_FLAGS(", ", rtmsg, rtm_flags,
314 routing_flags, "RTM_F_???");
321 offset = NLMSG_ALIGN(sizeof(rtmsg));
322 if (decode_nla && len > offset) {
324 decode_nlattr(tcp, addr + offset, len - offset,
325 rtnl_route_attrs, "RTA_???",
327 ARRAY_SIZE(rtmsg_nla_decoders), &rtmsg);