• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "netlink_route.h"
32 #include "nlattr.h"
33 #include "print_fields.h"
34 
35 #include <linux/ip.h>
36 #include "netlink.h"
37 #include <linux/rtnetlink.h>
38 
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"
48 
49 bool
decode_nla_rt_class(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
54 {
55 	uint32_t num;
56 
57 	if (len < sizeof(num))
58 		return false;
59 	if (!umove_or_printaddr(tcp, addr, &num))
60 		printxval(routing_table_ids, num, NULL);
61 	return true;
62 }
63 
64 bool
decode_nla_rt_proto(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
69 {
70 	uint8_t num;
71 
72 	if (len < sizeof(num))
73 		return false;
74 	if (!umove_or_printaddr(tcp, addr, &num))
75 		printxval_search(routing_protocols, num, "RTPROT_???");
76 	return true;
77 }
78 
79 static bool
decode_route_addr(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
84 {
85 	const struct rtmsg *const rtmsg = opaque_data;
86 
87 	decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
88 
89 	return true;
90 }
91 
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
109 };
110 
111 static bool
decode_rta_metrics(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
116 {
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);
120 
121 	return true;
122 }
123 
124 static bool
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);
129 
130 static bool
decode_rta_cacheinfo(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,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)
135 {
136 	struct rta_cacheinfo ci;
137 
138 	if (len < sizeof(ci))
139 		return false;
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);
149 		tprints("}");
150 	}
151 
152 	return true;
153 }
154 
155 static bool
decode_rta_mfc_stats(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
160 {
161 #ifdef HAVE_STRUCT_RTA_MFC_STATS
162 	struct rta_mfc_stats mfcs;
163 
164 	if (len < sizeof(mfcs))
165 		return false;
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);
170 		tprints("}");
171 	}
172 
173 	return true;
174 #else
175 	return false;
176 #endif
177 }
178 
179 static bool
decode_rtvia(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)180 decode_rtvia(struct tcb *const tcp,
181 	     const kernel_ulong_t addr,
182 	     const unsigned int len,
183 	     const void *const opaque_data)
184 {
185 #ifdef HAVE_STRUCT_RTVIA
186 	struct rtvia via;
187 
188 	if (len < sizeof(via))
189 		return false;
190 	else if (!umove_or_printaddr(tcp, addr, &via)) {
191 		PRINT_FIELD_XVAL("{", via, rtvia_family, addrfams, "AF_???");
192 
193 		const unsigned int offset = offsetof(struct rtvia, rtvia_addr);
194 
195 		if (len > offset) {
196 			tprints(", ");
197 			decode_inet_addr(tcp, addr + offset, len - offset,
198 					 via.rtvia_family, "rtvia_addr");
199 		}
200 		tprints("}");
201 	}
202 
203 	return true;
204 #else
205 	return false;
206 #endif
207 }
208 
209 static bool
decode_rta_encap_type(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)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)
214 {
215 	uint16_t type;
216 
217 	if (len < sizeof(type))
218 		return false;
219 	else if (!umove_or_printaddr(tcp, addr, &type))
220 		printxval(lwtunnel_encap_types, type, "LWTUNNEL_ENCAP_???");
221 
222 	return true;
223 }
224 
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,
249 	[RTA_PAD]		= NULL,
250 	[RTA_UID]		= decode_nla_u32,
251 	[RTA_TTL_PROPAGATE]	= decode_nla_u8,
252 	[RTA_IP_PROTO]		= decode_nla_u8,
253 	[RTA_SPORT]		= decode_nla_u16,
254 	[RTA_DPORT]		= decode_nla_u16
255 };
256 
257 static bool
decode_rta_multipath(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)258 decode_rta_multipath(struct tcb *const tcp,
259 		     const kernel_ulong_t addr,
260 		     const unsigned int len,
261 		     const void *const opaque_data)
262 {
263 	struct rtnexthop nh;
264 
265 	if (len < sizeof(nh))
266 		return false;
267 	else if (!umove_or_printaddr(tcp, addr, &nh)) {
268 		/* print the whole structure regardless of its rtnh_len */
269 		PRINT_FIELD_U("{", nh, rtnh_len);
270 		PRINT_FIELD_FLAGS(", ", nh, rtnh_flags,
271 				  route_nexthop_flags, "RTNH_F_???");
272 		PRINT_FIELD_U(", ", nh, rtnh_hops);
273 		PRINT_FIELD_IFINDEX(", ", nh, rtnh_ifindex);
274 		tprints("}");
275 
276 		const unsigned short rtnh_len = MIN(len, nh.rtnh_len);
277 		const size_t offset = RTNH_ALIGN(sizeof(nh));
278 		if (rtnh_len > offset) {
279 			tprints(", ");
280 			decode_nlattr(tcp, addr + offset, rtnh_len - offset,
281 				      rtnl_route_attrs, "RTA_???",
282 				      rtmsg_nla_decoders,
283 				      ARRAY_SIZE(rtmsg_nla_decoders),
284 				      opaque_data);
285 		}
286 	}
287 
288 	return true;
289 }
290 
DECL_NETLINK_ROUTE_DECODER(decode_rtmsg)291 DECL_NETLINK_ROUTE_DECODER(decode_rtmsg)
292 {
293 	struct rtmsg rtmsg = { .rtm_family = family };
294 	size_t offset = sizeof(rtmsg.rtm_family);
295 	bool decode_nla = false;
296 
297 	PRINT_FIELD_XVAL("{", rtmsg, rtm_family, addrfams, "AF_???");
298 
299 	tprints(", ");
300 	if (len >= sizeof(rtmsg)) {
301 		if (!umoven_or_printaddr(tcp, addr + offset,
302 					 sizeof(rtmsg) - offset,
303 					 (char *) &rtmsg + offset)) {
304 			PRINT_FIELD_U("", rtmsg, rtm_dst_len);
305 			PRINT_FIELD_U(", ", rtmsg, rtm_src_len);
306 			PRINT_FIELD_FLAGS(", ", rtmsg, rtm_tos,
307 					  ip_type_of_services, "IPTOS_TOS_???");
308 			PRINT_FIELD_XVAL(", ", rtmsg, rtm_table,
309 					 routing_table_ids, NULL);
310 			PRINT_FIELD_XVAL(", ", rtmsg, rtm_protocol,
311 					 routing_protocols, "RTPROT_???");
312 			PRINT_FIELD_XVAL(", ", rtmsg, rtm_scope,
313 					 routing_scopes, NULL);
314 			PRINT_FIELD_XVAL(", ", rtmsg, rtm_type,
315 					 routing_types, "RTN_???");
316 			PRINT_FIELD_FLAGS(", ", rtmsg, rtm_flags,
317 					  routing_flags, "RTM_F_???");
318 			decode_nla = true;
319 		}
320 	} else
321 		tprints("...");
322 	tprints("}");
323 
324 	offset = NLMSG_ALIGN(sizeof(rtmsg));
325 	if (decode_nla && len > offset) {
326 		tprints(", ");
327 		decode_nlattr(tcp, addr + offset, len - offset,
328 			      rtnl_route_attrs, "RTA_???",
329 			      rtmsg_nla_decoders,
330 			      ARRAY_SIZE(rtmsg_nla_decoders), &rtmsg);
331 	}
332 }
333