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@gredler.at)
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"
36 * RPKI/Router PDU header
38 * Here's what the PDU header looks like.
39 * The length does include the version and length fields.
41 typedef struct rpki_rtr_pdu_
{
42 u_char version
; /* Version number */
43 u_char pdu_type
; /* PDU type */
45 u_char session_id
[2]; /* Session id */
46 u_char error_code
[2]; /* Error code */
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 /* Copy of Erroneous PDU (variable, optional) */
84 /* Length of Error Text (4 octets in network byte order) */
85 /* Arbitrary Text of Error Diagnostic Message (variable, optional) */
86 } rpki_rtr_pdu_error_report
;
91 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
92 #define RPKI_RTR_SERIAL_QUERY_PDU 1
93 #define RPKI_RTR_RESET_QUERY_PDU 2
94 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
95 #define RPKI_RTR_IPV4_PREFIX_PDU 4
96 #define RPKI_RTR_IPV6_PREFIX_PDU 6
97 #define RPKI_RTR_END_OF_DATA_PDU 7
98 #define RPKI_RTR_CACHE_RESET_PDU 8
99 #define RPKI_RTR_ERROR_REPORT_PDU 10
101 static const struct tok rpki_rtr_pdu_values
[] = {
102 { RPKI_RTR_SERIAL_NOTIFY_PDU
, "Serial Notify" },
103 { RPKI_RTR_SERIAL_QUERY_PDU
, "Serial Query" },
104 { RPKI_RTR_RESET_QUERY_PDU
, "Reset Query" },
105 { RPKI_RTR_CACHE_RESPONSE_PDU
, "Cache Response" },
106 { RPKI_RTR_IPV4_PREFIX_PDU
, "IPV4 Prefix" },
107 { RPKI_RTR_IPV6_PREFIX_PDU
, "IPV6 Prefix" },
108 { RPKI_RTR_END_OF_DATA_PDU
, "End of Data" },
109 { RPKI_RTR_CACHE_RESET_PDU
, "Cache Reset" },
110 { RPKI_RTR_ERROR_REPORT_PDU
, "Error Report" },
114 static const struct tok rpki_rtr_error_codes
[] = {
115 { 0, "Corrupt Data" },
116 { 1, "Internal Error" },
117 { 2, "No Data Available" },
118 { 3, "Invalid Request" },
119 { 4, "Unsupported Protocol Version" },
120 { 5, "Unsupported PDU Type" },
121 { 6, "Withdrawal of Unknown Record" },
122 { 7, "Duplicate Announcement Received" },
127 * Build a indentation string for a given indentation level.
128 * XXX this should be really in util.c
131 indent_string (u_int indent
)
140 * Does the static buffer fit ?
142 if (sizeof(buf
) < ((indent
/8) + (indent
%8) + 2)) {
152 while (indent
>= 8) {
173 * Print a single PDU.
176 rpki_rtr_pdu_print(netdissect_options
*ndo
, const u_char
*tptr
, const u_int len
,
177 const u_char recurse
, const u_int indent
)
179 const rpki_rtr_pdu
*pdu_header
;
180 u_int pdu_type
, pdu_len
, hexdump
;
183 /* Protocol Version */
185 if (GET_U_1(tptr
) != 0) {
186 /* Skip the rest of the input buffer because even if this is
187 * a well-formed PDU of a future RPKI-Router protocol version
188 * followed by a well-formed PDU of RPKI-Router protocol
189 * version 0, there is no way to know exactly how to skip the
192 ND_PRINT("%sRPKI-RTRv%u (unknown)", indent_string(8), GET_U_1(tptr
));
195 if (len
< sizeof(rpki_rtr_pdu
)) {
196 ND_PRINT("(%u bytes is too few to decode)", len
);
199 ND_TCHECK_LEN(tptr
, sizeof(rpki_rtr_pdu
));
200 pdu_header
= (const rpki_rtr_pdu
*)tptr
;
201 pdu_type
= pdu_header
->pdu_type
;
202 pdu_len
= GET_BE_U_4(pdu_header
->length
);
203 /* Do not check bounds with pdu_len yet, do it in the case blocks
204 * below to make it possible to decode at least the beginning of
205 * a truncated Error Report PDU or a truncated encapsulated PDU.
209 ND_PRINT("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
212 tok2str(rpki_rtr_pdu_values
, "Unknown", pdu_type
),
214 if (pdu_len
< sizeof(rpki_rtr_pdu
) || pdu_len
> len
)
220 * The following PDUs share the message format.
222 case RPKI_RTR_SERIAL_NOTIFY_PDU
:
223 case RPKI_RTR_SERIAL_QUERY_PDU
:
224 case RPKI_RTR_END_OF_DATA_PDU
:
225 if (pdu_len
!= sizeof(rpki_rtr_pdu
) + 4)
227 ND_TCHECK_LEN(tptr
, pdu_len
);
228 msg
= (const u_char
*)(pdu_header
+ 1);
229 ND_PRINT("%sSession ID: 0x%04x, Serial: %u",
230 indent_string(indent
+2),
231 GET_BE_U_2(pdu_header
->u
.session_id
),
236 * The following PDUs share the message format.
238 case RPKI_RTR_RESET_QUERY_PDU
:
239 case RPKI_RTR_CACHE_RESET_PDU
:
240 if (pdu_len
!= sizeof(rpki_rtr_pdu
))
242 /* no additional boundary to check */
249 case RPKI_RTR_CACHE_RESPONSE_PDU
:
250 if (pdu_len
!= sizeof(rpki_rtr_pdu
))
252 /* no additional boundary to check */
253 ND_PRINT("%sSession ID: 0x%04x",
254 indent_string(indent
+2),
255 GET_BE_U_2(pdu_header
->u
.session_id
));
258 case RPKI_RTR_IPV4_PREFIX_PDU
:
260 const rpki_rtr_pdu_ipv4_prefix
*pdu
;
262 if (pdu_len
!= sizeof(rpki_rtr_pdu
) + 12)
264 ND_TCHECK_LEN(tptr
, pdu_len
);
265 pdu
= (const rpki_rtr_pdu_ipv4_prefix
*)tptr
;
266 ND_PRINT("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
267 indent_string(indent
+2),
268 ipaddr_string(ndo
, pdu
->prefix
),
269 pdu
->prefix_length
, pdu
->max_length
,
270 GET_BE_U_4(pdu
->as
), pdu
->flags
);
274 case RPKI_RTR_IPV6_PREFIX_PDU
:
276 const rpki_rtr_pdu_ipv6_prefix
*pdu
;
278 if (pdu_len
!= sizeof(rpki_rtr_pdu
) + 24)
280 ND_TCHECK_LEN(tptr
, pdu_len
);
281 pdu
= (const rpki_rtr_pdu_ipv6_prefix
*)tptr
;
282 ND_PRINT("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
283 indent_string(indent
+2),
284 ip6addr_string(ndo
, pdu
->prefix
),
285 pdu
->prefix_length
, pdu
->max_length
,
286 GET_BE_U_4(pdu
->as
), pdu
->flags
);
290 case RPKI_RTR_ERROR_REPORT_PDU
:
292 const rpki_rtr_pdu_error_report
*pdu
;
293 u_int encapsulated_pdu_length
, text_length
, tlen
, error_code
;
295 tlen
= sizeof(rpki_rtr_pdu
);
296 /* Do not test for the "Length of Error Text" data element yet. */
297 if (pdu_len
< tlen
+ 4)
299 ND_TCHECK_LEN(tptr
, tlen
+ 4);
300 /* Safe up to and including the "Length of Encapsulated PDU"
301 * data element, more data elements may be present.
303 pdu
= (const rpki_rtr_pdu_error_report
*)tptr
;
304 encapsulated_pdu_length
= GET_BE_U_4(pdu
->encapsulated_pdu_length
);
307 error_code
= GET_BE_U_2(pdu
->pdu_header
.u
.error_code
);
308 ND_PRINT("%sError code: %s (%u), Encapsulated PDU length: %u",
309 indent_string(indent
+2),
310 tok2str(rpki_rtr_error_codes
, "Unknown", error_code
),
311 error_code
, encapsulated_pdu_length
);
313 if (encapsulated_pdu_length
) {
314 /* Section 5.10 of RFC 6810 says:
315 * "An Error Report PDU MUST NOT be sent for an Error Report PDU."
317 * However, as far as the protocol encoding goes Error Report PDUs can
318 * happen to be nested in each other, however many times, in which case
319 * the decoder should still print such semantically incorrect PDUs.
321 * That said, "the Erroneous PDU field MAY be truncated" (ibid), thus
322 * to keep things simple this implementation decodes only the two
323 * outermost layers of PDUs and makes bounds checks in the outer and
324 * the inner PDU independently.
326 if (pdu_len
< tlen
+ encapsulated_pdu_length
)
329 ND_TCHECK_LEN(tptr
, tlen
+ encapsulated_pdu_length
);
332 ND_PRINT("%s-----encapsulated PDU-----", indent_string(indent
+4));
333 rpki_rtr_pdu_print(ndo
, tptr
+ tlen
,
334 encapsulated_pdu_length
, 0, indent
+ 2);
336 tlen
+= encapsulated_pdu_length
;
339 if (pdu_len
< tlen
+ 4)
341 ND_TCHECK_LEN(tptr
, tlen
+ 4);
342 /* Safe up to and including the "Length of Error Text" data element,
343 * one more data element may be present.
347 * Extract, trail-zero and print the Error message.
349 text_length
= GET_BE_U_4(tptr
+ tlen
);
353 if (pdu_len
< tlen
+ text_length
)
355 /* nd_printn() makes the bounds check */
356 ND_PRINT("%sError text: ", indent_string(indent
+2));
357 if (nd_printn(ndo
, tptr
+ tlen
, text_length
, ndo
->ndo_snapend
))
364 ND_TCHECK_LEN(tptr
, pdu_len
);
367 * Unknown data, please hexdump.
372 /* do we also want to see a hex dump ? */
373 if (ndo
->ndo_vflag
> 1 || (ndo
->ndo_vflag
&& hexdump
)) {
374 print_unknown_data(ndo
,tptr
,"\n\t ", pdu_len
);
379 nd_print_invalid(ndo
);
380 ND_TCHECK_LEN(tptr
, len
);
388 rpki_rtr_print(netdissect_options
*ndo
, const u_char
*pptr
, u_int len
)
390 ndo
->ndo_protocol
= "rpki_rtr";
391 if (!ndo
->ndo_vflag
) {
392 ND_PRINT(", RPKI-RTR");
396 u_int pdu_len
= rpki_rtr_pdu_print(ndo
, pptr
, len
, 1, 8);