2 * Copyright (c) 1998-2011 The TCPDUMP project
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.
15 * support for the The RPKI/Router Protocol as RFC6810
17 * Original code by Hannes Gredler (hannes@juniper.net)
21 static const char rcsid[] _U_ =
22 "@(#) $Header: /tcpdump/master/tcpdump/print-rpki_rtr.c,v 1.10 2008-03-20 09:30:56 hannes Exp $";
29 #include <tcpdump-stdinc.h>
35 #include "interface.h"
37 #include "addrtoname.h"
40 * RPKI/Router PDU header
42 * Here's what the PDU header looks like.
43 * The length does include the version and length fields.
45 typedef struct rpki_rtr_pdu_ {
46 u_char version; /* Version number */
47 u_char pdu_type; /* PDU type */
49 u_char session_id[2]; /* Session id */
50 u_char error_code[2]; /* Error code */
54 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
59 typedef struct rpki_rtr_pdu_ipv4_prefix_ {
60 rpki_rtr_pdu pdu_header;
67 } rpki_rtr_pdu_ipv4_prefix;
72 typedef struct rpki_rtr_pdu_ipv6_prefix_ {
73 rpki_rtr_pdu pdu_header;
80 } rpki_rtr_pdu_ipv6_prefix;
85 typedef struct rpki_rtr_pdu_error_report_ {
86 rpki_rtr_pdu pdu_header;
87 u_char encapsulated_pdu_length[4]; /* Encapsulated PDU length */
88 } rpki_rtr_pdu_error_report;
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
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" },
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" },
129 * Build a identation string for a given identation level.
130 * XXX this should be really in util.c
133 indent_string (u_int indent)
142 * Does the static buffer fit ?
144 if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
154 while (indent >= 8) {
175 * Print a single PDU.
178 rpki_rtr_pdu_print (const u_char *tptr, u_int indent)
180 const rpki_rtr_pdu *pdu_header;
181 u_int pdu_type, pdu_len, hexdump;
184 pdu_header = (rpki_rtr_pdu *)tptr;
185 pdu_type = pdu_header->pdu_type;
186 pdu_len = EXTRACT_32BITS(pdu_header->length);
189 printf("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
192 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
198 * The following PDUs share the message format.
200 case RPKI_RTR_SERIAL_NOTIFY_PDU:
201 case RPKI_RTR_SERIAL_QUERY_PDU:
202 case RPKI_RTR_END_OF_DATA_PDU:
203 msg = (const u_char *)(pdu_header + 1);
204 printf("%sSession ID: 0x%04x, Serial: %u",
205 indent_string(indent+2),
206 EXTRACT_16BITS(pdu_header->u.session_id),
207 EXTRACT_32BITS(msg));
211 * The following PDUs share the message format.
213 case RPKI_RTR_RESET_QUERY_PDU:
214 case RPKI_RTR_CACHE_RESET_PDU:
221 case RPKI_RTR_CACHE_RESPONSE_PDU:
222 printf("%sSession ID: 0x%04x",
223 indent_string(indent+2),
224 EXTRACT_16BITS(pdu_header->u.session_id));
227 case RPKI_RTR_IPV4_PREFIX_PDU:
229 rpki_rtr_pdu_ipv4_prefix *pdu;
231 pdu = (rpki_rtr_pdu_ipv4_prefix *)tptr;
232 printf("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
233 indent_string(indent+2),
234 ipaddr_string(pdu->prefix),
235 pdu->prefix_length, pdu->max_length,
236 EXTRACT_32BITS(pdu->as), pdu->flags);
241 case RPKI_RTR_IPV6_PREFIX_PDU:
243 rpki_rtr_pdu_ipv6_prefix *pdu;
245 pdu = (rpki_rtr_pdu_ipv6_prefix *)tptr;
246 printf("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
247 indent_string(indent+2),
248 ip6addr_string(pdu->prefix),
249 pdu->prefix_length, pdu->max_length,
250 EXTRACT_32BITS(pdu->as), pdu->flags);
255 case RPKI_RTR_ERROR_REPORT_PDU:
257 rpki_rtr_pdu_error_report *pdu;
258 u_int encapsulated_pdu_length, text_length, tlen, error_code;
261 pdu = (rpki_rtr_pdu_error_report *)tptr;
262 encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
265 error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
266 printf("%sError code: %s (%u), Encapsulated PDU length: %u",
267 indent_string(indent+2),
268 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
269 error_code, encapsulated_pdu_length);
271 tptr += sizeof(*pdu);
272 tlen -= sizeof(*pdu);
275 * Recurse if there is an encapsulated PDU.
277 if (encapsulated_pdu_length &&
278 (encapsulated_pdu_length <= tlen)) {
279 printf("%s-----encapsulated PDU-----", indent_string(indent+4));
280 rpki_rtr_pdu_print(tptr, indent+2);
283 tptr += encapsulated_pdu_length;
284 tlen -= encapsulated_pdu_length;
287 * Extract, trail-zero and print the Error message.
291 text_length = EXTRACT_32BITS(tptr);
295 if (text_length && (text_length <= tlen )) {
296 memcpy(buf, tptr, MIN(sizeof(buf)-1, text_length));
297 buf[text_length] = '\0';
298 printf("%sError text: %s", indent_string(indent+2), buf);
306 * Unknown data, please hexdump.
311 /* do we also want to see a hex dump ? */
312 if (vflag > 1 || (vflag && hexdump)) {
313 print_unknown_data(tptr,"\n\t ", pdu_len);
318 rpki_rtr_print(register const u_char *pptr, register u_int len) {
320 u_int tlen, pdu_type, pdu_len;
322 const rpki_rtr_pdu *pdu_header;
328 printf(", RPKI-RTR");
332 while (tlen >= sizeof(rpki_rtr_pdu)) {
334 TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
336 pdu_header = (rpki_rtr_pdu *)tptr;
337 pdu_type = pdu_header->pdu_type;
338 pdu_len = EXTRACT_32BITS(pdu_header->length);
340 /* infinite loop check */
341 if (!pdu_type || !pdu_len) {
345 TCHECK2(*tptr, pdu_len);
346 if (tlen < pdu_len) {
353 rpki_rtr_pdu_print(tptr, 8);
360 printf("\n\t[|RPKI-RTR]");
365 * c-style: whitesmith