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 * Original code by Hannes Gredler (hannes@juniper.net)
18 /* \summary: Resource Public Key Infrastructure (RPKI) to Router Protocol printer */
20 /* specification: RFC 6810 */
26 #include <netdissect-stdinc.h>
30 #include "netdissect.h"
32 #include "addrtoname.h"
34 static const char tstr
[] = "[|RPKI-RTR]";
37 * RPKI/Router PDU header
39 * Here's what the PDU header looks like.
40 * The length does include the version and length fields.
42 typedef struct rpki_rtr_pdu_
{
43 u_char version
; /* Version number */
44 u_char pdu_type
; /* PDU type */
46 u_char session_id
[2]; /* Session id */
47 u_char error_code
[2]; /* Error code */
51 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
56 typedef struct rpki_rtr_pdu_ipv4_prefix_
{
57 rpki_rtr_pdu pdu_header
;
64 } rpki_rtr_pdu_ipv4_prefix
;
69 typedef struct rpki_rtr_pdu_ipv6_prefix_
{
70 rpki_rtr_pdu pdu_header
;
77 } rpki_rtr_pdu_ipv6_prefix
;
82 typedef struct rpki_rtr_pdu_error_report_
{
83 rpki_rtr_pdu pdu_header
;
84 u_char encapsulated_pdu_length
[4]; /* Encapsulated PDU length */
85 } rpki_rtr_pdu_error_report
;
90 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
91 #define RPKI_RTR_SERIAL_QUERY_PDU 1
92 #define RPKI_RTR_RESET_QUERY_PDU 2
93 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
94 #define RPKI_RTR_IPV4_PREFIX_PDU 4
95 #define RPKI_RTR_IPV6_PREFIX_PDU 6
96 #define RPKI_RTR_END_OF_DATA_PDU 7
97 #define RPKI_RTR_CACHE_RESET_PDU 8
98 #define RPKI_RTR_ERROR_REPORT_PDU 10
100 static const struct tok rpki_rtr_pdu_values
[] = {
101 { RPKI_RTR_SERIAL_NOTIFY_PDU
, "Serial Notify" },
102 { RPKI_RTR_SERIAL_QUERY_PDU
, "Serial Query" },
103 { RPKI_RTR_RESET_QUERY_PDU
, "Reset Query" },
104 { RPKI_RTR_CACHE_RESPONSE_PDU
, "Cache Response" },
105 { RPKI_RTR_IPV4_PREFIX_PDU
, "IPV4 Prefix" },
106 { RPKI_RTR_IPV6_PREFIX_PDU
, "IPV6 Prefix" },
107 { RPKI_RTR_END_OF_DATA_PDU
, "End of Data" },
108 { RPKI_RTR_CACHE_RESET_PDU
, "Cache Reset" },
109 { RPKI_RTR_ERROR_REPORT_PDU
, "Error Report" },
113 static const struct tok rpki_rtr_error_codes
[] = {
114 { 0, "Corrupt Data" },
115 { 1, "Internal Error" },
116 { 2, "No Data Available" },
117 { 3, "Invalid Request" },
118 { 4, "Unsupported Protocol Version" },
119 { 5, "Unsupported PDU Type" },
120 { 6, "Withdrawal of Unknown Record" },
121 { 7, "Duplicate Announcement Received" },
126 * Build a indentation string for a given indentation level.
127 * XXX this should be really in util.c
130 indent_string (u_int indent
)
139 * Does the static buffer fit ?
141 if (sizeof(buf
) < ((indent
/8) + (indent
%8) + 2)) {
151 while (indent
>= 8) {
172 * Print a single PDU.
175 rpki_rtr_pdu_print (netdissect_options
*ndo
, const u_char
*tptr
, u_int indent
)
177 const rpki_rtr_pdu
*pdu_header
;
178 u_int pdu_type
, pdu_len
, hexdump
;
181 pdu_header
= (const rpki_rtr_pdu
*)tptr
;
182 pdu_type
= pdu_header
->pdu_type
;
183 pdu_len
= EXTRACT_32BITS(pdu_header
->length
);
184 ND_TCHECK2(*tptr
, pdu_len
);
187 ND_PRINT((ndo
, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
190 tok2str(rpki_rtr_pdu_values
, "Unknown", pdu_type
),
196 * The following PDUs share the message format.
198 case RPKI_RTR_SERIAL_NOTIFY_PDU
:
199 case RPKI_RTR_SERIAL_QUERY_PDU
:
200 case RPKI_RTR_END_OF_DATA_PDU
:
201 msg
= (const u_char
*)(pdu_header
+ 1);
202 ND_PRINT((ndo
, "%sSession ID: 0x%04x, Serial: %u",
203 indent_string(indent
+2),
204 EXTRACT_16BITS(pdu_header
->u
.session_id
),
205 EXTRACT_32BITS(msg
)));
209 * The following PDUs share the message format.
211 case RPKI_RTR_RESET_QUERY_PDU
:
212 case RPKI_RTR_CACHE_RESET_PDU
:
219 case RPKI_RTR_CACHE_RESPONSE_PDU
:
220 ND_PRINT((ndo
, "%sSession ID: 0x%04x",
221 indent_string(indent
+2),
222 EXTRACT_16BITS(pdu_header
->u
.session_id
)));
225 case RPKI_RTR_IPV4_PREFIX_PDU
:
227 const rpki_rtr_pdu_ipv4_prefix
*pdu
;
229 pdu
= (const rpki_rtr_pdu_ipv4_prefix
*)tptr
;
230 ND_PRINT((ndo
, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
231 indent_string(indent
+2),
232 ipaddr_string(ndo
, pdu
->prefix
),
233 pdu
->prefix_length
, pdu
->max_length
,
234 EXTRACT_32BITS(pdu
->as
), pdu
->flags
));
238 case RPKI_RTR_IPV6_PREFIX_PDU
:
240 const rpki_rtr_pdu_ipv6_prefix
*pdu
;
242 pdu
= (const rpki_rtr_pdu_ipv6_prefix
*)tptr
;
243 ND_PRINT((ndo
, "%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
244 indent_string(indent
+2),
245 ip6addr_string(ndo
, pdu
->prefix
),
246 pdu
->prefix_length
, pdu
->max_length
,
247 EXTRACT_32BITS(pdu
->as
), pdu
->flags
));
251 case RPKI_RTR_ERROR_REPORT_PDU
:
253 const rpki_rtr_pdu_error_report
*pdu
;
254 u_int encapsulated_pdu_length
, text_length
, tlen
, error_code
;
256 pdu
= (const rpki_rtr_pdu_error_report
*)tptr
;
257 encapsulated_pdu_length
= EXTRACT_32BITS(pdu
->encapsulated_pdu_length
);
258 ND_TCHECK2(*tptr
, encapsulated_pdu_length
);
261 error_code
= EXTRACT_16BITS(pdu
->pdu_header
.u
.error_code
);
262 ND_PRINT((ndo
, "%sError code: %s (%u), Encapsulated PDU length: %u",
263 indent_string(indent
+2),
264 tok2str(rpki_rtr_error_codes
, "Unknown", error_code
),
265 error_code
, encapsulated_pdu_length
));
267 tptr
+= sizeof(*pdu
);
268 tlen
-= sizeof(*pdu
);
271 * Recurse if there is an encapsulated PDU.
273 if (encapsulated_pdu_length
&&
274 (encapsulated_pdu_length
<= tlen
)) {
275 ND_PRINT((ndo
, "%s-----encapsulated PDU-----", indent_string(indent
+4)));
276 if (rpki_rtr_pdu_print(ndo
, tptr
, indent
+2))
280 tptr
+= encapsulated_pdu_length
;
281 tlen
-= encapsulated_pdu_length
;
284 * Extract, trail-zero and print the Error message.
288 text_length
= EXTRACT_32BITS(tptr
);
292 ND_TCHECK2(*tptr
, text_length
);
293 if (text_length
&& (text_length
<= tlen
)) {
294 ND_PRINT((ndo
, "%sError text: ", indent_string(indent
+2)));
295 if (fn_printn(ndo
, tptr
, text_length
, ndo
->ndo_snapend
))
304 * Unknown data, please hexdump.
309 /* do we also want to see a hex dump ? */
310 if (ndo
->ndo_vflag
> 1 || (ndo
->ndo_vflag
&& hexdump
)) {
311 print_unknown_data(ndo
,tptr
,"\n\t ", pdu_len
);
320 rpki_rtr_print(netdissect_options
*ndo
, register const u_char
*pptr
, register u_int len
)
322 u_int tlen
, pdu_type
, pdu_len
;
324 const rpki_rtr_pdu
*pdu_header
;
329 if (!ndo
->ndo_vflag
) {
330 ND_PRINT((ndo
, ", RPKI-RTR"));
334 while (tlen
>= sizeof(rpki_rtr_pdu
)) {
336 ND_TCHECK2(*tptr
, sizeof(rpki_rtr_pdu
));
338 pdu_header
= (const rpki_rtr_pdu
*)tptr
;
339 pdu_type
= pdu_header
->pdu_type
;
340 pdu_len
= EXTRACT_32BITS(pdu_header
->length
);
341 ND_TCHECK2(*tptr
, pdu_len
);
343 /* infinite loop check */
344 if (!pdu_type
|| !pdu_len
) {
348 if (tlen
< pdu_len
) {
355 if (rpki_rtr_pdu_print(ndo
, tptr
, 8))
363 ND_PRINT((ndo
, "\n\t%s", tstr
));
368 * c-style: whitesmith