123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <netdissect-stdinc.h>
- #include <string.h>
- #include "netdissect.h"
- #include "extract.h"
- #include "addrtoname.h"
- static const char tstr[] = "[|RPKI-RTR]";
- typedef struct rpki_rtr_pdu_ {
- u_char version;
- u_char pdu_type;
- union {
- u_char session_id[2];
- u_char error_code[2];
- } u;
- u_char length[4];
- } rpki_rtr_pdu;
- #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
- typedef struct rpki_rtr_pdu_ipv4_prefix_ {
- rpki_rtr_pdu pdu_header;
- u_char flags;
- u_char prefix_length;
- u_char max_length;
- u_char zero;
- u_char prefix[4];
- u_char as[4];
- } rpki_rtr_pdu_ipv4_prefix;
- typedef struct rpki_rtr_pdu_ipv6_prefix_ {
- rpki_rtr_pdu pdu_header;
- u_char flags;
- u_char prefix_length;
- u_char max_length;
- u_char zero;
- u_char prefix[16];
- u_char as[4];
- } rpki_rtr_pdu_ipv6_prefix;
- typedef struct rpki_rtr_pdu_error_report_ {
- rpki_rtr_pdu pdu_header;
- u_char encapsulated_pdu_length[4];
-
-
-
- } rpki_rtr_pdu_error_report;
- #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
- #define RPKI_RTR_SERIAL_QUERY_PDU 1
- #define RPKI_RTR_RESET_QUERY_PDU 2
- #define RPKI_RTR_CACHE_RESPONSE_PDU 3
- #define RPKI_RTR_IPV4_PREFIX_PDU 4
- #define RPKI_RTR_IPV6_PREFIX_PDU 6
- #define RPKI_RTR_END_OF_DATA_PDU 7
- #define RPKI_RTR_CACHE_RESET_PDU 8
- #define RPKI_RTR_ERROR_REPORT_PDU 10
- static const struct tok rpki_rtr_pdu_values[] = {
- { RPKI_RTR_SERIAL_NOTIFY_PDU, "Serial Notify" },
- { RPKI_RTR_SERIAL_QUERY_PDU, "Serial Query" },
- { RPKI_RTR_RESET_QUERY_PDU, "Reset Query" },
- { RPKI_RTR_CACHE_RESPONSE_PDU, "Cache Response" },
- { RPKI_RTR_IPV4_PREFIX_PDU, "IPV4 Prefix" },
- { RPKI_RTR_IPV6_PREFIX_PDU, "IPV6 Prefix" },
- { RPKI_RTR_END_OF_DATA_PDU, "End of Data" },
- { RPKI_RTR_CACHE_RESET_PDU, "Cache Reset" },
- { RPKI_RTR_ERROR_REPORT_PDU, "Error Report" },
- { 0, NULL}
- };
- static const struct tok rpki_rtr_error_codes[] = {
- { 0, "Corrupt Data" },
- { 1, "Internal Error" },
- { 2, "No Data Available" },
- { 3, "Invalid Request" },
- { 4, "Unsupported Protocol Version" },
- { 5, "Unsupported PDU Type" },
- { 6, "Withdrawal of Unknown Record" },
- { 7, "Duplicate Announcement Received" },
- { 0, NULL}
- };
- static char *
- indent_string (u_int indent)
- {
- static char buf[20];
- u_int idx;
- idx = 0;
- buf[idx] = '\0';
-
- if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
- return buf;
- }
-
- buf[idx] = '\n';
- idx++;
- while (indent >= 8) {
- buf[idx] = '\t';
- idx++;
- indent -= 8;
- }
- while (indent > 0) {
- buf[idx] = ' ';
- idx++;
- indent--;
- }
-
- buf[idx] = '\0';
- return buf;
- }
- static u_int
- rpki_rtr_pdu_print (netdissect_options *ndo, const u_char *tptr, const u_int len,
- const u_char recurse, const u_int indent)
- {
- const rpki_rtr_pdu *pdu_header;
- u_int pdu_type, pdu_len, hexdump;
- const u_char *msg;
-
- ND_TCHECK_8BITS(tptr);
- if (*tptr != 0) {
-
- ND_PRINT((ndo, "%sRPKI-RTRv%u (unknown)", indent_string(8), *tptr));
- return len;
- }
- if (len < sizeof(rpki_rtr_pdu)) {
- ND_PRINT((ndo, "(%u bytes is too few to decode)", len));
- goto invalid;
- }
- ND_TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
- pdu_header = (const rpki_rtr_pdu *)tptr;
- pdu_type = pdu_header->pdu_type;
- pdu_len = EXTRACT_32BITS(pdu_header->length);
-
- hexdump = FALSE;
- ND_PRINT((ndo, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
- indent_string(8),
- pdu_header->version,
- tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
- pdu_type, pdu_len));
- if (pdu_len < sizeof(rpki_rtr_pdu) || pdu_len > len)
- goto invalid;
- switch (pdu_type) {
-
- case RPKI_RTR_SERIAL_NOTIFY_PDU:
- case RPKI_RTR_SERIAL_QUERY_PDU:
- case RPKI_RTR_END_OF_DATA_PDU:
- if (pdu_len != sizeof(rpki_rtr_pdu) + 4)
- goto invalid;
- ND_TCHECK2(*tptr, pdu_len);
- msg = (const u_char *)(pdu_header + 1);
- ND_PRINT((ndo, "%sSession ID: 0x%04x, Serial: %u",
- indent_string(indent+2),
- EXTRACT_16BITS(pdu_header->u.session_id),
- EXTRACT_32BITS(msg)));
- break;
-
- case RPKI_RTR_RESET_QUERY_PDU:
- case RPKI_RTR_CACHE_RESET_PDU:
- if (pdu_len != sizeof(rpki_rtr_pdu))
- goto invalid;
-
-
- break;
- case RPKI_RTR_CACHE_RESPONSE_PDU:
- if (pdu_len != sizeof(rpki_rtr_pdu))
- goto invalid;
-
- ND_PRINT((ndo, "%sSession ID: 0x%04x",
- indent_string(indent+2),
- EXTRACT_16BITS(pdu_header->u.session_id)));
- break;
- case RPKI_RTR_IPV4_PREFIX_PDU:
- {
- const rpki_rtr_pdu_ipv4_prefix *pdu;
- if (pdu_len != sizeof(rpki_rtr_pdu) + 12)
- goto invalid;
- ND_TCHECK2(*tptr, pdu_len);
- pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
- ND_PRINT((ndo, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
- indent_string(indent+2),
- ipaddr_string(ndo, pdu->prefix),
- pdu->prefix_length, pdu->max_length,
- EXTRACT_32BITS(pdu->as), pdu->flags));
- }
- break;
- case RPKI_RTR_IPV6_PREFIX_PDU:
- {
- const rpki_rtr_pdu_ipv6_prefix *pdu;
- if (pdu_len != sizeof(rpki_rtr_pdu) + 24)
- goto invalid;
- ND_TCHECK2(*tptr, pdu_len);
- pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
- ND_PRINT((ndo, "%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
- indent_string(indent+2),
- ip6addr_string(ndo, pdu->prefix),
- pdu->prefix_length, pdu->max_length,
- EXTRACT_32BITS(pdu->as), pdu->flags));
- }
- break;
- case RPKI_RTR_ERROR_REPORT_PDU:
- {
- const rpki_rtr_pdu_error_report *pdu;
- u_int encapsulated_pdu_length, text_length, tlen, error_code;
- tlen = sizeof(rpki_rtr_pdu);
-
- if (pdu_len < tlen + 4)
- goto invalid;
- ND_TCHECK2(*tptr, tlen + 4);
-
- pdu = (const rpki_rtr_pdu_error_report *)tptr;
- encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
- tlen += 4;
- error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
- ND_PRINT((ndo, "%sError code: %s (%u), Encapsulated PDU length: %u",
- indent_string(indent+2),
- tok2str(rpki_rtr_error_codes, "Unknown", error_code),
- error_code, encapsulated_pdu_length));
- if (encapsulated_pdu_length) {
-
- if (pdu_len < tlen + encapsulated_pdu_length)
- goto invalid;
- if (! recurse) {
- ND_TCHECK2(*tptr, tlen + encapsulated_pdu_length);
- }
- else {
- ND_PRINT((ndo, "%s-----encapsulated PDU-----", indent_string(indent+4)));
- rpki_rtr_pdu_print(ndo, tptr + tlen,
- encapsulated_pdu_length, 0, indent + 2);
- }
- tlen += encapsulated_pdu_length;
- }
- if (pdu_len < tlen + 4)
- goto invalid;
- ND_TCHECK2(*tptr, tlen + 4);
-
-
- text_length = EXTRACT_32BITS(tptr + tlen);
- tlen += 4;
- if (text_length) {
- if (pdu_len < tlen + text_length)
- goto invalid;
-
- ND_PRINT((ndo, "%sError text: ", indent_string(indent+2)));
- if (fn_printn(ndo, tptr + tlen, text_length, ndo->ndo_snapend))
- goto trunc;
- }
- }
- break;
- default:
- ND_TCHECK2(*tptr, pdu_len);
-
- hexdump = TRUE;
- }
-
- if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
- print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
- }
- return pdu_len;
- invalid:
- ND_PRINT((ndo, "%s", istr));
- ND_TCHECK2(*tptr, len);
- return len;
- trunc:
- ND_PRINT((ndo, "\n\t%s", tstr));
- return len;
- }
- void
- rpki_rtr_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
- {
- if (!ndo->ndo_vflag) {
- ND_PRINT((ndo, ", RPKI-RTR"));
- return;
- }
- while (len) {
- u_int pdu_len = rpki_rtr_pdu_print(ndo, pptr, len, 1, 8);
- len -= pdu_len;
- pptr += pdu_len;
- }
- }
|