Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rtnl_route.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include "defs.h"
  10. #include "netlink_route.h"
  11. #include "nlattr.h"
  12. #include "print_fields.h"
  13. #include <linux/ip.h>
  14. #include "netlink.h"
  15. #include <linux/rtnetlink.h>
  16. #include "xlat/ip_type_of_services.h"
  17. #include "xlat/lwtunnel_encap_types.h"
  18. #include "xlat/route_nexthop_flags.h"
  19. #include "xlat/routing_flags.h"
  20. #include "xlat/routing_protocols.h"
  21. #include "xlat/routing_table_ids.h"
  22. #include "xlat/routing_types.h"
  23. #include "xlat/rtnl_route_attrs.h"
  24. #include "xlat/rtnl_rta_metrics_attrs.h"
  25. bool
  26. decode_nla_rt_class(struct tcb *const tcp,
  27. const kernel_ulong_t addr,
  28. const unsigned int len,
  29. const void *const opaque_data)
  30. {
  31. uint32_t num;
  32. if (len < sizeof(num))
  33. return false;
  34. if (!umove_or_printaddr(tcp, addr, &num))
  35. printxval(routing_table_ids, num, NULL);
  36. return true;
  37. }
  38. bool
  39. decode_nla_rt_proto(struct tcb *const tcp,
  40. const kernel_ulong_t addr,
  41. const unsigned int len,
  42. const void *const opaque_data)
  43. {
  44. uint8_t num;
  45. if (len < sizeof(num))
  46. return false;
  47. if (!umove_or_printaddr(tcp, addr, &num))
  48. printxval(routing_protocols, num, "RTPROT_???");
  49. return true;
  50. }
  51. static bool
  52. decode_route_addr(struct tcb *const tcp,
  53. const kernel_ulong_t addr,
  54. const unsigned int len,
  55. const void *const opaque_data)
  56. {
  57. const struct rtmsg *const rtmsg = opaque_data;
  58. decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
  59. return true;
  60. }
  61. static const nla_decoder_t rta_metrics_nla_decoders[] = {
  62. [RTAX_LOCK] = decode_nla_u32,
  63. [RTAX_MTU] = decode_nla_u32,
  64. [RTAX_WINDOW] = decode_nla_u32,
  65. [RTAX_RTT] = decode_nla_u32,
  66. [RTAX_RTTVAR] = decode_nla_u32,
  67. [RTAX_SSTHRESH] = decode_nla_u32,
  68. [RTAX_CWND] = decode_nla_u32,
  69. [RTAX_ADVMSS] = decode_nla_u32,
  70. [RTAX_REORDERING] = decode_nla_u32,
  71. [RTAX_HOPLIMIT] = decode_nla_u32,
  72. [RTAX_INITCWND] = decode_nla_u32,
  73. [RTAX_FEATURES] = decode_nla_u32,
  74. [RTAX_RTO_MIN] = decode_nla_u32,
  75. [RTAX_INITRWND] = decode_nla_u32,
  76. [RTAX_QUICKACK] = decode_nla_u32,
  77. [RTAX_CC_ALGO] = decode_nla_str
  78. };
  79. static bool
  80. decode_rta_metrics(struct tcb *const tcp,
  81. const kernel_ulong_t addr,
  82. const unsigned int len,
  83. const void *const opaque_data)
  84. {
  85. decode_nlattr(tcp, addr, len, rtnl_rta_metrics_attrs,
  86. "RTAX_???", rta_metrics_nla_decoders,
  87. ARRAY_SIZE(rta_metrics_nla_decoders), opaque_data);
  88. return true;
  89. }
  90. static bool
  91. decode_rta_multipath(struct tcb *const tcp,
  92. const kernel_ulong_t addr,
  93. const unsigned int len,
  94. const void *const opaque_data);
  95. static bool
  96. decode_rta_cacheinfo(struct tcb *const tcp,
  97. const kernel_ulong_t addr,
  98. const unsigned int len,
  99. const void *const opaque_data)
  100. {
  101. struct rta_cacheinfo ci;
  102. if (len < sizeof(ci))
  103. return false;
  104. else if (!umove_or_printaddr(tcp, addr, &ci)) {
  105. PRINT_FIELD_U("{", ci, rta_clntref);
  106. PRINT_FIELD_U(", ", ci, rta_lastuse);
  107. PRINT_FIELD_U(", ", ci, rta_expires);
  108. PRINT_FIELD_U(", ", ci, rta_error);
  109. PRINT_FIELD_U(", ", ci, rta_used);
  110. PRINT_FIELD_X(", ", ci, rta_id);
  111. PRINT_FIELD_U(", ", ci, rta_ts);
  112. PRINT_FIELD_U(", ", ci, rta_tsage);
  113. tprints("}");
  114. }
  115. return true;
  116. }
  117. static bool
  118. decode_rta_mfc_stats(struct tcb *const tcp,
  119. const kernel_ulong_t addr,
  120. const unsigned int len,
  121. const void *const opaque_data)
  122. {
  123. #ifdef HAVE_STRUCT_RTA_MFC_STATS
  124. struct rta_mfc_stats mfcs;
  125. if (len < sizeof(mfcs))
  126. return false;
  127. else if (!umove_or_printaddr(tcp, addr, &mfcs)) {
  128. PRINT_FIELD_U("{", mfcs, mfcs_packets);
  129. PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
  130. PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
  131. tprints("}");
  132. }
  133. return true;
  134. #else
  135. return false;
  136. #endif
  137. }
  138. static bool
  139. decode_rtvia(struct tcb *const tcp,
  140. const kernel_ulong_t addr,
  141. const unsigned int len,
  142. const void *const opaque_data)
  143. {
  144. #ifdef HAVE_STRUCT_RTVIA
  145. struct rtvia via;
  146. if (len < sizeof(via))
  147. return false;
  148. else if (!umove_or_printaddr(tcp, addr, &via)) {
  149. PRINT_FIELD_XVAL("{", via, rtvia_family, addrfams, "AF_???");
  150. const unsigned int offset = offsetof(struct rtvia, rtvia_addr);
  151. if (len > offset) {
  152. tprints(", ");
  153. decode_inet_addr(tcp, addr + offset, len - offset,
  154. via.rtvia_family, "rtvia_addr");
  155. }
  156. tprints("}");
  157. }
  158. return true;
  159. #else
  160. return false;
  161. #endif
  162. }
  163. static bool
  164. decode_rta_encap_type(struct tcb *const tcp,
  165. const kernel_ulong_t addr,
  166. const unsigned int len,
  167. const void *const opaque_data)
  168. {
  169. uint16_t type;
  170. if (len < sizeof(type))
  171. return false;
  172. else if (!umove_or_printaddr(tcp, addr, &type))
  173. printxval(lwtunnel_encap_types, type, "LWTUNNEL_ENCAP_???");
  174. return true;
  175. }
  176. static const nla_decoder_t rtmsg_nla_decoders[] = {
  177. [RTA_DST] = decode_route_addr,
  178. [RTA_SRC] = decode_route_addr,
  179. [RTA_IIF] = decode_nla_ifindex,
  180. [RTA_OIF] = decode_nla_ifindex,
  181. [RTA_GATEWAY] = decode_route_addr,
  182. [RTA_PRIORITY] = decode_nla_u32,
  183. [RTA_PREFSRC] = decode_route_addr,
  184. [RTA_METRICS] = decode_rta_metrics,
  185. [RTA_MULTIPATH] = decode_rta_multipath,
  186. [RTA_PROTOINFO] = decode_nla_u32,
  187. [RTA_FLOW] = decode_nla_u32,
  188. [RTA_CACHEINFO] = decode_rta_cacheinfo,
  189. [RTA_SESSION] = NULL, /* unused */
  190. [RTA_MP_ALGO] = decode_nla_u32,
  191. [RTA_TABLE] = decode_nla_rt_class,
  192. [RTA_MARK] = decode_nla_u32,
  193. [RTA_MFC_STATS] = decode_rta_mfc_stats,
  194. [RTA_VIA] = decode_rtvia,
  195. [RTA_NEWDST] = decode_route_addr,
  196. [RTA_PREF] = decode_nla_u8,
  197. [RTA_ENCAP_TYPE] = decode_rta_encap_type,
  198. [RTA_ENCAP] = NULL, /* unimplemented */
  199. [RTA_EXPIRES] = decode_nla_u64,
  200. [RTA_PAD] = NULL,
  201. [RTA_UID] = decode_nla_u32,
  202. [RTA_TTL_PROPAGATE] = decode_nla_u8,
  203. [RTA_IP_PROTO] = decode_nla_u8,
  204. [RTA_SPORT] = decode_nla_u16,
  205. [RTA_DPORT] = decode_nla_u16
  206. };
  207. static bool
  208. decode_rta_multipath(struct tcb *const tcp,
  209. const kernel_ulong_t addr,
  210. const unsigned int len,
  211. const void *const opaque_data)
  212. {
  213. struct rtnexthop nh;
  214. if (len < sizeof(nh))
  215. return false;
  216. else if (!umove_or_printaddr(tcp, addr, &nh)) {
  217. /* print the whole structure regardless of its rtnh_len */
  218. PRINT_FIELD_U("{", nh, rtnh_len);
  219. PRINT_FIELD_FLAGS(", ", nh, rtnh_flags,
  220. route_nexthop_flags, "RTNH_F_???");
  221. PRINT_FIELD_U(", ", nh, rtnh_hops);
  222. PRINT_FIELD_IFINDEX(", ", nh, rtnh_ifindex);
  223. tprints("}");
  224. const unsigned short rtnh_len = MIN(len, nh.rtnh_len);
  225. const size_t offset = RTNH_ALIGN(sizeof(nh));
  226. if (rtnh_len > offset) {
  227. tprints(", ");
  228. decode_nlattr(tcp, addr + offset, rtnh_len - offset,
  229. rtnl_route_attrs, "RTA_???",
  230. rtmsg_nla_decoders,
  231. ARRAY_SIZE(rtmsg_nla_decoders),
  232. opaque_data);
  233. }
  234. }
  235. return true;
  236. }
  237. DECL_NETLINK_ROUTE_DECODER(decode_rtmsg)
  238. {
  239. struct rtmsg rtmsg = { .rtm_family = family };
  240. size_t offset = sizeof(rtmsg.rtm_family);
  241. bool decode_nla = false;
  242. PRINT_FIELD_XVAL("{", rtmsg, rtm_family, addrfams, "AF_???");
  243. tprints(", ");
  244. if (len >= sizeof(rtmsg)) {
  245. if (!umoven_or_printaddr(tcp, addr + offset,
  246. sizeof(rtmsg) - offset,
  247. (char *) &rtmsg + offset)) {
  248. PRINT_FIELD_U("", rtmsg, rtm_dst_len);
  249. PRINT_FIELD_U(", ", rtmsg, rtm_src_len);
  250. PRINT_FIELD_FLAGS(", ", rtmsg, rtm_tos,
  251. ip_type_of_services, "IPTOS_TOS_???");
  252. PRINT_FIELD_XVAL(", ", rtmsg, rtm_table,
  253. routing_table_ids, NULL);
  254. PRINT_FIELD_XVAL(", ", rtmsg, rtm_protocol,
  255. routing_protocols, "RTPROT_???");
  256. PRINT_FIELD_XVAL(", ", rtmsg, rtm_scope,
  257. routing_scopes, NULL);
  258. PRINT_FIELD_XVAL(", ", rtmsg, rtm_type,
  259. routing_types, "RTN_???");
  260. PRINT_FIELD_FLAGS(", ", rtmsg, rtm_flags,
  261. routing_flags, "RTM_F_???");
  262. decode_nla = true;
  263. }
  264. } else
  265. tprints("...");
  266. tprints("}");
  267. offset = NLMSG_ALIGN(sizeof(rtmsg));
  268. if (decode_nla && len > offset) {
  269. tprints(", ");
  270. decode_nlattr(tcp, addr + offset, len - offset,
  271. rtnl_route_attrs, "RTA_???",
  272. rtmsg_nla_decoders,
  273. ARRAY_SIZE(rtmsg_nla_decoders), &rtmsg);
  274. }
  275. }