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 8.4KB

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