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 Protocol as per draft-ietf-sidr-rpki-rtr-12
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 cache_nonce
[2]; /* Cache Nonce */
50 u_char error_code
[2]; /* Error code */
53 u_char msg
[0]; /* message body */
54 } __attribute__((packed
)) rpki_rtr_pdu
;
55 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
60 typedef struct rpki_rtr_pdu_ipv4_prefix_
{
61 rpki_rtr_pdu pdu_header
;
68 } __attribute__((packed
)) rpki_rtr_pdu_ipv4_prefix
;
73 typedef struct rpki_rtr_pdu_ipv6_prefix_
{
74 rpki_rtr_pdu pdu_header
;
81 } __attribute__((packed
)) rpki_rtr_pdu_ipv6_prefix
;
86 typedef struct rpki_rtr_pdu_error_report_
{
87 rpki_rtr_pdu pdu_header
;
88 u_char encapsulated_pdu_length
[4]; /* Encapsulated PDU length */
90 } __attribute__((packed
)) rpki_rtr_pdu_error_report
;
95 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
96 #define RPKI_RTR_SERIAL_QUERY_PDU 1
97 #define RPKI_RTR_RESET_QUERY_PDU 2
98 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
99 #define RPKI_RTR_IPV4_PREFIX_PDU 4
100 #define RPKI_RTR_IPV6_PREFIX_PDU 6
101 #define RPKI_RTR_END_OF_DATA_PDU 7
102 #define RPKI_RTR_CACHE_RESET_PDU 8
103 #define RPKI_RTR_ERROR_REPORT_PDU 10
105 static const struct tok rpki_rtr_pdu_values
[] = {
106 { RPKI_RTR_SERIAL_NOTIFY_PDU
, "Serial Notify" },
107 { RPKI_RTR_SERIAL_QUERY_PDU
, "Serial Query" },
108 { RPKI_RTR_RESET_QUERY_PDU
, "Reset Query" },
109 { RPKI_RTR_CACHE_RESPONSE_PDU
, "Cache Response" },
110 { RPKI_RTR_IPV4_PREFIX_PDU
, "IPV4 Prefix" },
111 { RPKI_RTR_IPV6_PREFIX_PDU
, "IPV6 Prefix" },
112 { RPKI_RTR_END_OF_DATA_PDU
, "End of Data" },
113 { RPKI_RTR_CACHE_RESET_PDU
, "Cache Reset" },
114 { RPKI_RTR_ERROR_REPORT_PDU
, "Error Report" },
118 static const struct tok rpki_rtr_error_codes
[] = {
119 { 0, "Corrupt Data" },
120 { 1, "Internal Error" },
121 { 2, "No Data Available" },
122 { 3, "Invalid Request" },
123 { 4, "Unsupported Protocol Version" },
124 { 5, "Unsupported PDU Type" },
125 { 6, "Withdrawal of Unknown Record" },
126 { 7, "Duplicate Announcement Received" },
131 * Build a identation string for a given identation level.
132 * XXX this should be really in util.c
135 indent_string (u_int indent
)
144 * Does the static buffer fit ?
146 if (sizeof(buf
) < ((indent
/8) + (indent
%8) + 2)) {
156 while (indent
>= 8) {
177 * Print a single PDU.
180 rpki_rtr_pdu_print (const u_char
*tptr
, u_int indent
)
182 const rpki_rtr_pdu
*pdu_header
;
183 u_int pdu_type
, pdu_len
, hexdump
;
185 pdu_header
= (rpki_rtr_pdu
*)tptr
;
186 pdu_type
= pdu_header
->pdu_type
;
187 pdu_len
= EXTRACT_32BITS(pdu_header
->length
);
190 printf("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
193 tok2str(rpki_rtr_pdu_values
, "Unknown", pdu_type
),
199 * The following PDUs share the message format.
201 case RPKI_RTR_SERIAL_NOTIFY_PDU
:
202 case RPKI_RTR_SERIAL_QUERY_PDU
:
203 case RPKI_RTR_END_OF_DATA_PDU
:
204 printf("%sCache-Nonce: 0x%04x, Serial: %u",
205 indent_string(indent
+2),
206 EXTRACT_16BITS(pdu_header
->cache_nonce
),
207 EXTRACT_32BITS(pdu_header
->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("%sCache-Nonce: 0x%04x",
223 indent_string(indent
+2),
224 EXTRACT_16BITS(pdu_header
->cache_nonce
));
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
.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