2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2016-2017 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_route_addr(struct tcb *const tcp,
66 const kernel_ulong_t addr,
67 const unsigned int len,
68 const void *const opaque_data)
70 const struct rtmsg *const rtmsg = opaque_data;
72 decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
77 static const nla_decoder_t rta_metrics_nla_decoders[] = {
78 [RTAX_LOCK] = decode_nla_u32,
79 [RTAX_MTU] = decode_nla_u32,
80 [RTAX_WINDOW] = decode_nla_u32,
81 [RTAX_RTT] = decode_nla_u32,
82 [RTAX_RTTVAR] = decode_nla_u32,
83 [RTAX_SSTHRESH] = decode_nla_u32,
84 [RTAX_CWND] = decode_nla_u32,
85 [RTAX_ADVMSS] = decode_nla_u32,
86 [RTAX_REORDERING] = decode_nla_u32,
87 [RTAX_HOPLIMIT] = decode_nla_u32,
88 [RTAX_INITCWND] = decode_nla_u32,
89 [RTAX_FEATURES] = decode_nla_u32,
90 [RTAX_RTO_MIN] = decode_nla_u32,
91 [RTAX_INITRWND] = decode_nla_u32,
92 [RTAX_QUICKACK] = decode_nla_u32,
93 [RTAX_CC_ALGO] = decode_nla_str
97 decode_rta_metrics(struct tcb *const tcp,
98 const kernel_ulong_t addr,
99 const unsigned int len,
100 const void *const opaque_data)
102 decode_nlattr(tcp, addr, len, rtnl_rta_metrics_attrs,
103 "RTAX_???", rta_metrics_nla_decoders,
104 ARRAY_SIZE(rta_metrics_nla_decoders), opaque_data);
110 decode_rta_multipath(struct tcb *const tcp,
111 const kernel_ulong_t addr,
112 const unsigned int len,
113 const void *const opaque_data);
116 decode_rta_cacheinfo(struct tcb *const tcp,
117 const kernel_ulong_t addr,
118 const unsigned int len,
119 const void *const opaque_data)
121 struct rta_cacheinfo ci;
123 if (len < sizeof(ci))
125 else if (!umove_or_printaddr(tcp, addr, &ci)) {
126 PRINT_FIELD_U("{", ci, rta_clntref);
127 PRINT_FIELD_U(", ", ci, rta_lastuse);
128 PRINT_FIELD_U(", ", ci, rta_expires);
129 PRINT_FIELD_U(", ", ci, rta_error);
130 PRINT_FIELD_U(", ", ci, rta_used);
131 PRINT_FIELD_X(", ", ci, rta_id);
132 PRINT_FIELD_U(", ", ci, rta_ts);
133 PRINT_FIELD_U(", ", ci, rta_tsage);
141 decode_rta_mfc_stats(struct tcb *const tcp,
142 const kernel_ulong_t addr,
143 const unsigned int len,
144 const void *const opaque_data)
146 #ifdef HAVE_STRUCT_RTA_MFC_STATS
147 struct rta_mfc_stats mfcs;
149 if (len < sizeof(mfcs))
151 else if (!umove_or_printaddr(tcp, addr, &mfcs)) {
152 PRINT_FIELD_U("{", mfcs, mfcs_packets);
153 PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
154 PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
165 decode_rtvia(struct tcb *const tcp,
166 const kernel_ulong_t addr,
167 const unsigned int len,
168 const void *const opaque_data)
170 #ifdef HAVE_STRUCT_RTVIA
173 if (len < sizeof(via))
175 else if (!umove_or_printaddr(tcp, addr, &via)) {
176 PRINT_FIELD_XVAL("{", via, rtvia_family, addrfams, "AF_???");
178 const unsigned int offset = offsetof(struct rtvia, rtvia_addr);
182 decode_inet_addr(tcp, addr + offset, len - offset,
183 via.rtvia_family, "rtvia_addr");
195 decode_rta_encap_type(struct tcb *const tcp,
196 const kernel_ulong_t addr,
197 const unsigned int len,
198 const void *const opaque_data)
202 if (len < sizeof(type))
204 else if (!umove_or_printaddr(tcp, addr, &type))
205 printxval(lwtunnel_encap_types, type, "LWTUNNEL_ENCAP_???");
210 static const nla_decoder_t rtmsg_nla_decoders[] = {
211 [RTA_DST] = decode_route_addr,
212 [RTA_SRC] = decode_route_addr,
213 [RTA_IIF] = decode_nla_ifindex,
214 [RTA_OIF] = decode_nla_ifindex,
215 [RTA_GATEWAY] = decode_route_addr,
216 [RTA_PRIORITY] = decode_nla_u32,
217 [RTA_PREFSRC] = decode_route_addr,
218 [RTA_METRICS] = decode_rta_metrics,
219 [RTA_MULTIPATH] = decode_rta_multipath,
220 [RTA_PROTOINFO] = decode_nla_u32,
221 [RTA_FLOW] = decode_nla_u32,
222 [RTA_CACHEINFO] = decode_rta_cacheinfo,
223 [RTA_SESSION] = NULL, /* unused */
224 [RTA_MP_ALGO] = decode_nla_u32,
225 [RTA_TABLE] = decode_nla_rt_class,
226 [RTA_MARK] = decode_nla_u32,
227 [RTA_MFC_STATS] = decode_rta_mfc_stats,
228 [RTA_VIA] = decode_rtvia,
229 [RTA_NEWDST] = decode_route_addr,
230 [RTA_PREF] = decode_nla_u8,
231 [RTA_ENCAP_TYPE] = decode_rta_encap_type,
232 [RTA_ENCAP] = NULL, /* unimplemented */
233 [RTA_EXPIRES] = decode_nla_u64,
235 [RTA_UID] = decode_nla_u32,
236 [RTA_TTL_PROPAGATE] = decode_nla_u8
240 decode_rta_multipath(struct tcb *const tcp,
241 const kernel_ulong_t addr,
242 const unsigned int len,
243 const void *const opaque_data)
247 if (len < sizeof(nh))
249 else if (!umove_or_printaddr(tcp, addr, &nh)) {
250 /* print the whole structure regardless of its rtnh_len */
251 PRINT_FIELD_U("{", nh, rtnh_len);
252 PRINT_FIELD_FLAGS(", ", nh, rtnh_flags,
253 route_nexthop_flags, "RTNH_F_???");
254 PRINT_FIELD_U(", ", nh, rtnh_hops);
255 PRINT_FIELD_IFINDEX(", ", nh, rtnh_ifindex);
258 const unsigned short rtnh_len =
259 len < nh.rtnh_len ? len : nh.rtnh_len;
260 const size_t offset = RTNH_ALIGN(sizeof(nh));
261 if (rtnh_len > offset) {
263 decode_nlattr(tcp, addr + offset, rtnh_len - offset,
264 rtnl_route_attrs, "RTA_???",
266 ARRAY_SIZE(rtmsg_nla_decoders),
274 DECL_NETLINK_ROUTE_DECODER(decode_rtmsg)
276 struct rtmsg rtmsg = { .rtm_family = family };
277 size_t offset = sizeof(rtmsg.rtm_family);
278 bool decode_nla = false;
280 PRINT_FIELD_XVAL("{", rtmsg, rtm_family, addrfams, "AF_???");
283 if (len >= sizeof(rtmsg)) {
284 if (!umoven_or_printaddr(tcp, addr + offset,
285 sizeof(rtmsg) - offset,
286 (void *) &rtmsg + offset)) {
287 PRINT_FIELD_U("", rtmsg, rtm_dst_len);
288 PRINT_FIELD_U(", ", rtmsg, rtm_src_len);
289 PRINT_FIELD_FLAGS(", ", rtmsg, rtm_tos,
290 ip_type_of_services, "IPTOS_TOS_???");
291 PRINT_FIELD_XVAL(", ", rtmsg, rtm_table,
292 routing_table_ids, NULL);
293 PRINT_FIELD_XVAL(", ", rtmsg, rtm_protocol,
294 routing_protocols, "RTPROT_???");
295 PRINT_FIELD_XVAL(", ", rtmsg, rtm_scope,
296 routing_scopes, NULL);
297 PRINT_FIELD_XVAL(", ", rtmsg, rtm_type,
298 routing_types, "RTN_???");
299 PRINT_FIELD_FLAGS(", ", rtmsg, rtm_flags,
300 routing_flags, "RTM_F_???");
307 offset = NLMSG_ALIGN(sizeof(rtmsg));
308 if (decode_nla && len > offset) {
310 decode_nlattr(tcp, addr + offset, len - offset,
311 rtnl_route_attrs, "RTA_???",
313 ARRAY_SIZE(rtmsg_nla_decoders), &rtmsg);