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)
24 #include <tcpdump-stdinc.h>
30 #include "interface.h"
32 #include "addrtoname.h"
35 * RPKI/Router PDU header
37 * Here's what the PDU header looks like.
38 * The length does include the version and length fields.
40 typedef struct rpki_rtr_pdu_
{
41 u_char version
; /* Version number */
42 u_char pdu_type
; /* PDU type */
44 u_char session_id
[2]; /* Session id */
45 u_char error_code
[2]; /* Error code */
49 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
54 typedef struct rpki_rtr_pdu_ipv4_prefix_
{
55 rpki_rtr_pdu pdu_header
;
62 } rpki_rtr_pdu_ipv4_prefix
;
67 typedef struct rpki_rtr_pdu_ipv6_prefix_
{
68 rpki_rtr_pdu pdu_header
;
75 } rpki_rtr_pdu_ipv6_prefix
;
80 typedef struct rpki_rtr_pdu_error_report_
{
81 rpki_rtr_pdu pdu_header
;
82 u_char encapsulated_pdu_length
[4]; /* Encapsulated PDU length */
83 } rpki_rtr_pdu_error_report
;
88 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
89 #define RPKI_RTR_SERIAL_QUERY_PDU 1
90 #define RPKI_RTR_RESET_QUERY_PDU 2
91 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
92 #define RPKI_RTR_IPV4_PREFIX_PDU 4
93 #define RPKI_RTR_IPV6_PREFIX_PDU 6
94 #define RPKI_RTR_END_OF_DATA_PDU 7
95 #define RPKI_RTR_CACHE_RESET_PDU 8
96 #define RPKI_RTR_ERROR_REPORT_PDU 10
98 static const struct tok rpki_rtr_pdu_values
[] = {
99 { RPKI_RTR_SERIAL_NOTIFY_PDU
, "Serial Notify" },
100 { RPKI_RTR_SERIAL_QUERY_PDU
, "Serial Query" },
101 { RPKI_RTR_RESET_QUERY_PDU
, "Reset Query" },
102 { RPKI_RTR_CACHE_RESPONSE_PDU
, "Cache Response" },
103 { RPKI_RTR_IPV4_PREFIX_PDU
, "IPV4 Prefix" },
104 { RPKI_RTR_IPV6_PREFIX_PDU
, "IPV6 Prefix" },
105 { RPKI_RTR_END_OF_DATA_PDU
, "End of Data" },
106 { RPKI_RTR_CACHE_RESET_PDU
, "Cache Reset" },
107 { RPKI_RTR_ERROR_REPORT_PDU
, "Error Report" },
111 static const struct tok rpki_rtr_error_codes
[] = {
112 { 0, "Corrupt Data" },
113 { 1, "Internal Error" },
114 { 2, "No Data Available" },
115 { 3, "Invalid Request" },
116 { 4, "Unsupported Protocol Version" },
117 { 5, "Unsupported PDU Type" },
118 { 6, "Withdrawal of Unknown Record" },
119 { 7, "Duplicate Announcement Received" },
124 * Build a identation string for a given identation level.
125 * XXX this should be really in util.c
128 indent_string (u_int indent
)
137 * Does the static buffer fit ?
139 if (sizeof(buf
) < ((indent
/8) + (indent
%8) + 2)) {
149 while (indent
>= 8) {
170 * Print a single PDU.
173 rpki_rtr_pdu_print (const u_char
*tptr
, u_int indent
)
175 const rpki_rtr_pdu
*pdu_header
;
176 u_int pdu_type
, pdu_len
, hexdump
;
179 pdu_header
= (rpki_rtr_pdu
*)tptr
;
180 pdu_type
= pdu_header
->pdu_type
;
181 pdu_len
= EXTRACT_32BITS(pdu_header
->length
);
184 printf("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
187 tok2str(rpki_rtr_pdu_values
, "Unknown", pdu_type
),
193 * The following PDUs share the message format.
195 case RPKI_RTR_SERIAL_NOTIFY_PDU
:
196 case RPKI_RTR_SERIAL_QUERY_PDU
:
197 case RPKI_RTR_END_OF_DATA_PDU
:
198 msg
= (const u_char
*)(pdu_header
+ 1);
199 printf("%sSession ID: 0x%04x, Serial: %u",
200 indent_string(indent
+2),
201 EXTRACT_16BITS(pdu_header
->u
.session_id
),
202 EXTRACT_32BITS(msg
));
206 * The following PDUs share the message format.
208 case RPKI_RTR_RESET_QUERY_PDU
:
209 case RPKI_RTR_CACHE_RESET_PDU
:
216 case RPKI_RTR_CACHE_RESPONSE_PDU
:
217 printf("%sSession ID: 0x%04x",
218 indent_string(indent
+2),
219 EXTRACT_16BITS(pdu_header
->u
.session_id
));
222 case RPKI_RTR_IPV4_PREFIX_PDU
:
224 rpki_rtr_pdu_ipv4_prefix
*pdu
;
226 pdu
= (rpki_rtr_pdu_ipv4_prefix
*)tptr
;
227 printf("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
228 indent_string(indent
+2),
229 ipaddr_string(pdu
->prefix
),
230 pdu
->prefix_length
, pdu
->max_length
,
231 EXTRACT_32BITS(pdu
->as
), pdu
->flags
);
236 case RPKI_RTR_IPV6_PREFIX_PDU
:
238 rpki_rtr_pdu_ipv6_prefix
*pdu
;
240 pdu
= (rpki_rtr_pdu_ipv6_prefix
*)tptr
;
241 printf("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
242 indent_string(indent
+2),
243 ip6addr_string(pdu
->prefix
),
244 pdu
->prefix_length
, pdu
->max_length
,
245 EXTRACT_32BITS(pdu
->as
), pdu
->flags
);
250 case RPKI_RTR_ERROR_REPORT_PDU
:
252 rpki_rtr_pdu_error_report
*pdu
;
253 u_int encapsulated_pdu_length
, text_length
, tlen
, error_code
;
256 pdu
= (rpki_rtr_pdu_error_report
*)tptr
;
257 encapsulated_pdu_length
= EXTRACT_32BITS(pdu
->encapsulated_pdu_length
);
260 error_code
= EXTRACT_16BITS(pdu
->pdu_header
.u
.error_code
);
261 printf("%sError code: %s (%u), Encapsulated PDU length: %u",
262 indent_string(indent
+2),
263 tok2str(rpki_rtr_error_codes
, "Unknown", error_code
),
264 error_code
, encapsulated_pdu_length
);
266 tptr
+= sizeof(*pdu
);
267 tlen
-= sizeof(*pdu
);
270 * Recurse if there is an encapsulated PDU.
272 if (encapsulated_pdu_length
&&
273 (encapsulated_pdu_length
<= tlen
)) {
274 printf("%s-----encapsulated PDU-----", indent_string(indent
+4));
275 rpki_rtr_pdu_print(tptr
, indent
+2);
278 tptr
+= encapsulated_pdu_length
;
279 tlen
-= encapsulated_pdu_length
;
282 * Extract, trail-zero and print the Error message.
286 text_length
= EXTRACT_32BITS(tptr
);
290 if (text_length
&& (text_length
<= tlen
)) {
291 memcpy(buf
, tptr
, MIN(sizeof(buf
)-1, text_length
));
292 buf
[text_length
] = '\0';
293 printf("%sError text: %s", indent_string(indent
+2), buf
);
301 * Unknown data, please hexdump.
306 /* do we also want to see a hex dump ? */
307 if (vflag
> 1 || (vflag
&& hexdump
)) {
308 print_unknown_data(gndo
,tptr
,"\n\t ", pdu_len
);
313 rpki_rtr_print(register const u_char
*pptr
, register u_int len
) {
315 u_int tlen
, pdu_type
, pdu_len
;
317 const rpki_rtr_pdu
*pdu_header
;
323 printf(", RPKI-RTR");
327 while (tlen
>= sizeof(rpki_rtr_pdu
)) {
329 TCHECK2(*tptr
, sizeof(rpki_rtr_pdu
));
331 pdu_header
= (rpki_rtr_pdu
*)tptr
;
332 pdu_type
= pdu_header
->pdu_type
;
333 pdu_len
= EXTRACT_32BITS(pdu_header
->length
);
335 /* infinite loop check */
336 if (!pdu_type
|| !pdu_len
) {
340 TCHECK2(*tptr
, pdu_len
);
341 if (tlen
< pdu_len
) {
348 rpki_rtr_pdu_print(tptr
, 8);
355 printf("\n\t[|RPKI-RTR]");
360 * c-style: whitesmith