print-rpki-rtr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 1998-2011 The TCPDUMP project
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that: (1) source code
  6. * distributions retain the above copyright notice and this paragraph
  7. * in its entirety, and (2) distributions including binary code include
  8. * the above copyright notice and this paragraph in its entirety in
  9. * the documentation or other materials provided with the distribution.
  10. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  11. * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
  12. * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13. * FOR A PARTICULAR PURPOSE.
  14. *
  15. * Original code by Hannes Gredler (hannes@gredler.at)
  16. */
  17. /* \summary: Resource Public Key Infrastructure (RPKI) to Router Protocol printer */
  18. /* specification: RFC 6810 */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <netdissect-stdinc.h>
  23. #include <string.h>
  24. #include "netdissect.h"
  25. #include "extract.h"
  26. #include "addrtoname.h"
  27. static const char tstr[] = "[|RPKI-RTR]";
  28. /*
  29. * RPKI/Router PDU header
  30. *
  31. * Here's what the PDU header looks like.
  32. * The length does include the version and length fields.
  33. */
  34. typedef struct rpki_rtr_pdu_ {
  35. u_char version; /* Version number */
  36. u_char pdu_type; /* PDU type */
  37. union {
  38. u_char session_id[2]; /* Session id */
  39. u_char error_code[2]; /* Error code */
  40. } u;
  41. u_char length[4];
  42. } rpki_rtr_pdu;
  43. #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
  44. /*
  45. * IPv4 Prefix PDU.
  46. */
  47. typedef struct rpki_rtr_pdu_ipv4_prefix_ {
  48. rpki_rtr_pdu pdu_header;
  49. u_char flags;
  50. u_char prefix_length;
  51. u_char max_length;
  52. u_char zero;
  53. u_char prefix[4];
  54. u_char as[4];
  55. } rpki_rtr_pdu_ipv4_prefix;
  56. /*
  57. * IPv6 Prefix PDU.
  58. */
  59. typedef struct rpki_rtr_pdu_ipv6_prefix_ {
  60. rpki_rtr_pdu pdu_header;
  61. u_char flags;
  62. u_char prefix_length;
  63. u_char max_length;
  64. u_char zero;
  65. u_char prefix[16];
  66. u_char as[4];
  67. } rpki_rtr_pdu_ipv6_prefix;
  68. /*
  69. * Error report PDU.
  70. */
  71. typedef struct rpki_rtr_pdu_error_report_ {
  72. rpki_rtr_pdu pdu_header;
  73. u_char encapsulated_pdu_length[4]; /* Encapsulated PDU length */
  74. /* Copy of Erroneous PDU (variable, optional) */
  75. /* Length of Error Text (4 octets in network byte order) */
  76. /* Arbitrary Text of Error Diagnostic Message (variable, optional) */
  77. } rpki_rtr_pdu_error_report;
  78. /*
  79. * PDU type codes
  80. */
  81. #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
  82. #define RPKI_RTR_SERIAL_QUERY_PDU 1
  83. #define RPKI_RTR_RESET_QUERY_PDU 2
  84. #define RPKI_RTR_CACHE_RESPONSE_PDU 3
  85. #define RPKI_RTR_IPV4_PREFIX_PDU 4
  86. #define RPKI_RTR_IPV6_PREFIX_PDU 6
  87. #define RPKI_RTR_END_OF_DATA_PDU 7
  88. #define RPKI_RTR_CACHE_RESET_PDU 8
  89. #define RPKI_RTR_ERROR_REPORT_PDU 10
  90. static const struct tok rpki_rtr_pdu_values[] = {
  91. { RPKI_RTR_SERIAL_NOTIFY_PDU, "Serial Notify" },
  92. { RPKI_RTR_SERIAL_QUERY_PDU, "Serial Query" },
  93. { RPKI_RTR_RESET_QUERY_PDU, "Reset Query" },
  94. { RPKI_RTR_CACHE_RESPONSE_PDU, "Cache Response" },
  95. { RPKI_RTR_IPV4_PREFIX_PDU, "IPV4 Prefix" },
  96. { RPKI_RTR_IPV6_PREFIX_PDU, "IPV6 Prefix" },
  97. { RPKI_RTR_END_OF_DATA_PDU, "End of Data" },
  98. { RPKI_RTR_CACHE_RESET_PDU, "Cache Reset" },
  99. { RPKI_RTR_ERROR_REPORT_PDU, "Error Report" },
  100. { 0, NULL}
  101. };
  102. static const struct tok rpki_rtr_error_codes[] = {
  103. { 0, "Corrupt Data" },
  104. { 1, "Internal Error" },
  105. { 2, "No Data Available" },
  106. { 3, "Invalid Request" },
  107. { 4, "Unsupported Protocol Version" },
  108. { 5, "Unsupported PDU Type" },
  109. { 6, "Withdrawal of Unknown Record" },
  110. { 7, "Duplicate Announcement Received" },
  111. { 0, NULL}
  112. };
  113. /*
  114. * Build a indentation string for a given indentation level.
  115. * XXX this should be really in util.c
  116. */
  117. static char *
  118. indent_string (u_int indent)
  119. {
  120. static char buf[20];
  121. u_int idx;
  122. idx = 0;
  123. buf[idx] = '\0';
  124. /*
  125. * Does the static buffer fit ?
  126. */
  127. if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
  128. return buf;
  129. }
  130. /*
  131. * Heading newline.
  132. */
  133. buf[idx] = '\n';
  134. idx++;
  135. while (indent >= 8) {
  136. buf[idx] = '\t';
  137. idx++;
  138. indent -= 8;
  139. }
  140. while (indent > 0) {
  141. buf[idx] = ' ';
  142. idx++;
  143. indent--;
  144. }
  145. /*
  146. * Trailing zero.
  147. */
  148. buf[idx] = '\0';
  149. return buf;
  150. }
  151. /*
  152. * Print a single PDU.
  153. */
  154. static u_int
  155. rpki_rtr_pdu_print (netdissect_options *ndo, const u_char *tptr, const u_int len,
  156. const u_char recurse, const u_int indent)
  157. {
  158. const rpki_rtr_pdu *pdu_header;
  159. u_int pdu_type, pdu_len, hexdump;
  160. const u_char *msg;
  161. /* Protocol Version */
  162. ND_TCHECK_8BITS(tptr);
  163. if (*tptr != 0) {
  164. /* Skip the rest of the input buffer because even if this is
  165. * a well-formed PDU of a future RPKI-Router protocol version
  166. * followed by a well-formed PDU of RPKI-Router protocol
  167. * version 0, there is no way to know exactly how to skip the
  168. * current PDU.
  169. */
  170. ND_PRINT((ndo, "%sRPKI-RTRv%u (unknown)", indent_string(8), *tptr));
  171. return len;
  172. }
  173. if (len < sizeof(rpki_rtr_pdu)) {
  174. ND_PRINT((ndo, "(%u bytes is too few to decode)", len));
  175. goto invalid;
  176. }
  177. ND_TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
  178. pdu_header = (const rpki_rtr_pdu *)tptr;
  179. pdu_type = pdu_header->pdu_type;
  180. pdu_len = EXTRACT_32BITS(pdu_header->length);
  181. /* Do not check bounds with pdu_len yet, do it in the case blocks
  182. * below to make it possible to decode at least the beginning of
  183. * a truncated Error Report PDU or a truncated encapsulated PDU.
  184. */
  185. hexdump = FALSE;
  186. ND_PRINT((ndo, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
  187. indent_string(8),
  188. pdu_header->version,
  189. tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
  190. pdu_type, pdu_len));
  191. if (pdu_len < sizeof(rpki_rtr_pdu) || pdu_len > len)
  192. goto invalid;
  193. switch (pdu_type) {
  194. /*
  195. * The following PDUs share the message format.
  196. */
  197. case RPKI_RTR_SERIAL_NOTIFY_PDU:
  198. case RPKI_RTR_SERIAL_QUERY_PDU:
  199. case RPKI_RTR_END_OF_DATA_PDU:
  200. if (pdu_len != sizeof(rpki_rtr_pdu) + 4)
  201. goto invalid;
  202. ND_TCHECK2(*tptr, pdu_len);
  203. msg = (const u_char *)(pdu_header + 1);
  204. ND_PRINT((ndo, "%sSession ID: 0x%04x, Serial: %u",
  205. indent_string(indent+2),
  206. EXTRACT_16BITS(pdu_header->u.session_id),
  207. EXTRACT_32BITS(msg)));
  208. break;
  209. /*
  210. * The following PDUs share the message format.
  211. */
  212. case RPKI_RTR_RESET_QUERY_PDU:
  213. case RPKI_RTR_CACHE_RESET_PDU:
  214. if (pdu_len != sizeof(rpki_rtr_pdu))
  215. goto invalid;
  216. /* no additional boundary to check */
  217. /*
  218. * Zero payload PDUs.
  219. */
  220. break;
  221. case RPKI_RTR_CACHE_RESPONSE_PDU:
  222. if (pdu_len != sizeof(rpki_rtr_pdu))
  223. goto invalid;
  224. /* no additional boundary to check */
  225. ND_PRINT((ndo, "%sSession ID: 0x%04x",
  226. indent_string(indent+2),
  227. EXTRACT_16BITS(pdu_header->u.session_id)));
  228. break;
  229. case RPKI_RTR_IPV4_PREFIX_PDU:
  230. {
  231. const rpki_rtr_pdu_ipv4_prefix *pdu;
  232. if (pdu_len != sizeof(rpki_rtr_pdu) + 12)
  233. goto invalid;
  234. ND_TCHECK2(*tptr, pdu_len);
  235. pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
  236. ND_PRINT((ndo, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
  237. indent_string(indent+2),
  238. ipaddr_string(ndo, pdu->prefix),
  239. pdu->prefix_length, pdu->max_length,
  240. EXTRACT_32BITS(pdu->as), pdu->flags));
  241. }
  242. break;
  243. case RPKI_RTR_IPV6_PREFIX_PDU:
  244. {
  245. const rpki_rtr_pdu_ipv6_prefix *pdu;
  246. if (pdu_len != sizeof(rpki_rtr_pdu) + 24)
  247. goto invalid;
  248. ND_TCHECK2(*tptr, pdu_len);
  249. pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
  250. ND_PRINT((ndo, "%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
  251. indent_string(indent+2),
  252. ip6addr_string(ndo, pdu->prefix),
  253. pdu->prefix_length, pdu->max_length,
  254. EXTRACT_32BITS(pdu->as), pdu->flags));
  255. }
  256. break;
  257. case RPKI_RTR_ERROR_REPORT_PDU:
  258. {
  259. const rpki_rtr_pdu_error_report *pdu;
  260. u_int encapsulated_pdu_length, text_length, tlen, error_code;
  261. tlen = sizeof(rpki_rtr_pdu);
  262. /* Do not test for the "Length of Error Text" data element yet. */
  263. if (pdu_len < tlen + 4)
  264. goto invalid;
  265. ND_TCHECK2(*tptr, tlen + 4);
  266. /* Safe up to and including the "Length of Encapsulated PDU"
  267. * data element, more data elements may be present.
  268. */
  269. pdu = (const rpki_rtr_pdu_error_report *)tptr;
  270. encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
  271. tlen += 4;
  272. error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
  273. ND_PRINT((ndo, "%sError code: %s (%u), Encapsulated PDU length: %u",
  274. indent_string(indent+2),
  275. tok2str(rpki_rtr_error_codes, "Unknown", error_code),
  276. error_code, encapsulated_pdu_length));
  277. if (encapsulated_pdu_length) {
  278. /* Section 5.10 of RFC 6810 says:
  279. * "An Error Report PDU MUST NOT be sent for an Error Report PDU."
  280. *
  281. * However, as far as the protocol encoding goes Error Report PDUs can
  282. * happen to be nested in each other, however many times, in which case
  283. * the decoder should still print such semantically incorrect PDUs.
  284. *
  285. * That said, "the Erroneous PDU field MAY be truncated" (ibid), thus
  286. * to keep things simple this implementation decodes only the two
  287. * outermost layers of PDUs and makes bounds checks in the outer and
  288. * the inner PDU independently.
  289. */
  290. if (pdu_len < tlen + encapsulated_pdu_length)
  291. goto invalid;
  292. if (! recurse) {
  293. ND_TCHECK2(*tptr, tlen + encapsulated_pdu_length);
  294. }
  295. else {
  296. ND_PRINT((ndo, "%s-----encapsulated PDU-----", indent_string(indent+4)));
  297. rpki_rtr_pdu_print(ndo, tptr + tlen,
  298. encapsulated_pdu_length, 0, indent + 2);
  299. }
  300. tlen += encapsulated_pdu_length;
  301. }
  302. if (pdu_len < tlen + 4)
  303. goto invalid;
  304. ND_TCHECK2(*tptr, tlen + 4);
  305. /* Safe up to and including the "Length of Error Text" data element,
  306. * one more data element may be present.
  307. */
  308. /*
  309. * Extract, trail-zero and print the Error message.
  310. */
  311. text_length = EXTRACT_32BITS(tptr + tlen);
  312. tlen += 4;
  313. if (text_length) {
  314. if (pdu_len < tlen + text_length)
  315. goto invalid;
  316. /* fn_printn() makes the bounds check */
  317. ND_PRINT((ndo, "%sError text: ", indent_string(indent+2)));
  318. if (fn_printn(ndo, tptr + tlen, text_length, ndo->ndo_snapend))
  319. goto trunc;
  320. }
  321. }
  322. break;
  323. default:
  324. ND_TCHECK2(*tptr, pdu_len);
  325. /*
  326. * Unknown data, please hexdump.
  327. */
  328. hexdump = TRUE;
  329. }
  330. /* do we also want to see a hex dump ? */
  331. if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
  332. print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
  333. }
  334. return pdu_len;
  335. invalid:
  336. ND_PRINT((ndo, "%s", istr));
  337. ND_TCHECK2(*tptr, len);
  338. return len;
  339. trunc:
  340. ND_PRINT((ndo, "\n\t%s", tstr));
  341. return len;
  342. }
  343. void
  344. rpki_rtr_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
  345. {
  346. if (!ndo->ndo_vflag) {
  347. ND_PRINT((ndo, ", RPKI-RTR"));
  348. return;
  349. }
  350. while (len) {
  351. u_int pdu_len = rpki_rtr_pdu_print(ndo, pptr, len, 1, 8);
  352. len -= pdu_len;
  353. pptr += pdu_len;
  354. }
  355. }
  356. /*
  357. * Local Variables:
  358. * c-style: whitesmith
  359. * c-basic-offset: 4
  360. * End:
  361. */