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