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