]> The Tcpdump Group git mirrors - tcpdump/blob - print-rpki-rtr.c
Put "}" at beginning of line with "else" to keep a consistent style
[tcpdump] / print-rpki-rtr.c
1 /*
2 * Copyright (c) 1998-2011 The TCPDUMP project
3 *
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.
14 *
15 * Original code by Hannes Gredler (hannes@gredler.at)
16 */
17
18 /* \summary: Resource Public Key Infrastructure (RPKI) to Router Protocol printer */
19
20 /* specification: RFC 6810 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "netdissect-stdinc.h"
27
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "extract.h"
31 #include "addrtoname.h"
32
33
34 /*
35 * RPKI/Router PDU header
36 *
37 * Here's what the PDU header looks like.
38 * The length does include the version and length fields.
39 */
40 typedef struct rpki_rtr_pdu_ {
41 nd_uint8_t version; /* Version number */
42 nd_uint8_t pdu_type; /* PDU type */
43 union {
44 nd_uint16_t session_id; /* Session id */
45 nd_uint16_t error_code; /* Error code */
46 } u;
47 nd_uint32_t length;
48 } rpki_rtr_pdu;
49
50 /*
51 * IPv4 Prefix PDU.
52 */
53 typedef struct rpki_rtr_pdu_ipv4_prefix_ {
54 rpki_rtr_pdu pdu_header;
55 nd_uint8_t flags;
56 nd_uint8_t prefix_length;
57 nd_uint8_t max_length;
58 nd_uint8_t zero;
59 nd_ipv4 prefix;
60 nd_uint32_t as;
61 } rpki_rtr_pdu_ipv4_prefix;
62
63 /*
64 * IPv6 Prefix PDU.
65 */
66 typedef struct rpki_rtr_pdu_ipv6_prefix_ {
67 rpki_rtr_pdu pdu_header;
68 nd_uint8_t flags;
69 nd_uint8_t prefix_length;
70 nd_uint8_t max_length;
71 nd_uint8_t zero;
72 nd_ipv6 prefix;
73 nd_uint32_t as;
74 } rpki_rtr_pdu_ipv6_prefix;
75
76 /*
77 * Error report PDU.
78 */
79 typedef struct rpki_rtr_pdu_error_report_ {
80 rpki_rtr_pdu pdu_header;
81 nd_uint32_t encapsulated_pdu_length; /* Encapsulated PDU length */
82 /* Copy of Erroneous PDU (variable, optional) */
83 /* Length of Error Text (4 octets in network byte order) */
84 /* Arbitrary Text of Error Diagnostic Message (variable, optional) */
85 } rpki_rtr_pdu_error_report;
86
87 /*
88 * PDU type codes
89 */
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
99
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" },
110 { 0, NULL}
111 };
112
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" },
122 { 0, NULL}
123 };
124
125 /*
126 * Build a indentation string for a given indentation level.
127 */
128 static char *
129 indent_string (u_int indent)
130 {
131 static char buf[20];
132 u_int idx;
133
134 idx = 0;
135 buf[idx] = '\0';
136
137 /*
138 * Does the static buffer fit ?
139 */
140 if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
141 return buf;
142 }
143
144 /*
145 * Heading newline.
146 */
147 buf[idx] = '\n';
148 idx++;
149
150 while (indent >= 8) {
151 buf[idx] = '\t';
152 idx++;
153 indent -= 8;
154 }
155
156 while (indent > 0) {
157 buf[idx] = ' ';
158 idx++;
159 indent--;
160 }
161
162 /*
163 * Trailing zero.
164 */
165 buf[idx] = '\0';
166
167 return buf;
168 }
169
170 /*
171 * Print a single PDU.
172 */
173 static u_int
174 rpki_rtr_pdu_print(netdissect_options *ndo, const u_char *tptr, const u_int len,
175 const u_char recurse, const u_int indent)
176 {
177 const rpki_rtr_pdu *pdu_header;
178 u_int pdu_type, pdu_len, hexdump;
179 const u_char *msg;
180 uint8_t pdu_ver;
181
182 if (len < sizeof(rpki_rtr_pdu)) {
183 ND_PRINT("(%u bytes is too few to decode)", len);
184 goto invalid;
185 }
186 pdu_header = (const rpki_rtr_pdu *)tptr;
187 pdu_ver = GET_U_1(pdu_header->version);
188 if (pdu_ver != 0) {
189 /* Skip the rest of the input buffer because even if this is
190 * a well-formed PDU of a future RPKI-Router protocol version
191 * followed by a well-formed PDU of RPKI-Router protocol
192 * version 0, there is no way to know exactly how to skip the
193 * current PDU.
194 */
195 ND_PRINT("%sRPKI-RTRv%u (unknown)", indent_string(8), pdu_ver);
196 return len;
197 }
198 pdu_type = GET_U_1(pdu_header->pdu_type);
199 pdu_len = GET_BE_U_4(pdu_header->length);
200 /* Do not check bounds with pdu_len yet, do it in the case blocks
201 * below to make it possible to decode at least the beginning of
202 * a truncated Error Report PDU or a truncated encapsulated PDU.
203 */
204 hexdump = FALSE;
205
206 ND_PRINT("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
207 indent_string(8),
208 pdu_ver,
209 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
210 pdu_type, pdu_len);
211 if (pdu_len < sizeof(rpki_rtr_pdu) || pdu_len > len)
212 goto invalid;
213
214 switch (pdu_type) {
215
216 /*
217 * The following PDUs share the message format.
218 */
219 case RPKI_RTR_SERIAL_NOTIFY_PDU:
220 case RPKI_RTR_SERIAL_QUERY_PDU:
221 case RPKI_RTR_END_OF_DATA_PDU:
222 if (pdu_len != sizeof(rpki_rtr_pdu) + 4)
223 goto invalid;
224 msg = (const u_char *)(pdu_header + 1);
225 ND_PRINT("%sSession ID: 0x%04x, Serial: %u",
226 indent_string(indent+2),
227 GET_BE_U_2(pdu_header->u.session_id),
228 GET_BE_U_4(msg));
229 break;
230
231 /*
232 * The following PDUs share the message format.
233 */
234 case RPKI_RTR_RESET_QUERY_PDU:
235 case RPKI_RTR_CACHE_RESET_PDU:
236 if (pdu_len != sizeof(rpki_rtr_pdu))
237 goto invalid;
238 /* no additional boundary to check */
239
240 /*
241 * Zero payload PDUs.
242 */
243 break;
244
245 case RPKI_RTR_CACHE_RESPONSE_PDU:
246 if (pdu_len != sizeof(rpki_rtr_pdu))
247 goto invalid;
248 /* no additional boundary to check */
249 ND_PRINT("%sSession ID: 0x%04x",
250 indent_string(indent+2),
251 GET_BE_U_2(pdu_header->u.session_id));
252 break;
253
254 case RPKI_RTR_IPV4_PREFIX_PDU:
255 {
256 const rpki_rtr_pdu_ipv4_prefix *pdu;
257
258 if (pdu_len != sizeof(rpki_rtr_pdu_ipv4_prefix))
259 goto invalid;
260 pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
261 ND_PRINT("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
262 indent_string(indent+2),
263 GET_IPADDR_STRING(pdu->prefix),
264 GET_U_1(pdu->prefix_length), GET_U_1(pdu->max_length),
265 GET_BE_U_4(pdu->as), GET_U_1(pdu->flags));
266 }
267 break;
268
269 case RPKI_RTR_IPV6_PREFIX_PDU:
270 {
271 const rpki_rtr_pdu_ipv6_prefix *pdu;
272
273 if (pdu_len != sizeof(rpki_rtr_pdu_ipv6_prefix))
274 goto invalid;
275 pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
276 ND_PRINT("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
277 indent_string(indent+2),
278 GET_IP6ADDR_STRING(pdu->prefix),
279 GET_U_1(pdu->prefix_length), GET_U_1(pdu->max_length),
280 GET_BE_U_4(pdu->as), GET_U_1(pdu->flags));
281 }
282 break;
283
284 case RPKI_RTR_ERROR_REPORT_PDU:
285 {
286 const rpki_rtr_pdu_error_report *pdu;
287 u_int encapsulated_pdu_length, text_length, tlen, error_code;
288
289 tlen = sizeof(rpki_rtr_pdu);
290 /* Do not test for the "Length of Error Text" data element yet. */
291 if (pdu_len < tlen + 4)
292 goto invalid;
293 pdu = (const rpki_rtr_pdu_error_report *)tptr;
294 encapsulated_pdu_length = GET_BE_U_4(pdu->encapsulated_pdu_length);
295 tlen += 4;
296 /* Safe up to and including the "Length of Encapsulated PDU"
297 * data element, more data elements may be present.
298 */
299
300 error_code = GET_BE_U_2(pdu->pdu_header.u.error_code);
301 ND_PRINT("%sError code: %s (%u), Encapsulated PDU length: %u",
302 indent_string(indent+2),
303 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
304 error_code, encapsulated_pdu_length);
305
306 if (encapsulated_pdu_length) {
307 /* Section 5.10 of RFC 6810 says:
308 * "An Error Report PDU MUST NOT be sent for an Error Report PDU."
309 *
310 * However, as far as the protocol encoding goes Error Report PDUs can
311 * happen to be nested in each other, however many times, in which case
312 * the decoder should still print such semantically incorrect PDUs.
313 *
314 * That said, "the Erroneous PDU field MAY be truncated" (ibid), thus
315 * to keep things simple this implementation decodes only the two
316 * outermost layers of PDUs and makes bounds checks in the outer and
317 * the inner PDU independently.
318 */
319 if (pdu_len < tlen + encapsulated_pdu_length)
320 goto invalid;
321 if (! recurse) {
322 ND_TCHECK_LEN(tptr, tlen + encapsulated_pdu_length);
323 } else {
324 ND_PRINT("%s-----encapsulated PDU-----", indent_string(indent+4));
325 rpki_rtr_pdu_print(ndo, tptr + tlen,
326 encapsulated_pdu_length, 0, indent + 2);
327 }
328 tlen += encapsulated_pdu_length;
329 }
330
331 if (pdu_len < tlen + 4)
332 goto invalid;
333 /*
334 * Extract, trail-zero and print the Error message.
335 */
336 text_length = GET_BE_U_4(tptr + tlen);
337 tlen += 4;
338 /* Safe up to and including the "Length of Error Text" data element,
339 * one more data element may be present.
340 */
341
342 if (text_length) {
343 if (pdu_len < tlen + text_length)
344 goto invalid;
345 ND_PRINT("%sError text: ", indent_string(indent+2));
346 nd_printjn(ndo, tptr + tlen, text_length);
347 }
348 }
349 break;
350
351 default:
352 ND_TCHECK_LEN(tptr, pdu_len);
353
354 /*
355 * Unknown data, please hexdump.
356 */
357 hexdump = TRUE;
358 }
359
360 /* do we also want to see a hex dump ? */
361 if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
362 print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
363 }
364 return pdu_len;
365
366 invalid:
367 nd_print_invalid(ndo);
368 ND_TCHECK_LEN(tptr, len);
369 return len;
370 }
371
372 void
373 rpki_rtr_print(netdissect_options *ndo, const u_char *pptr, u_int len)
374 {
375 ndo->ndo_protocol = "rpki_rtr";
376 if (!ndo->ndo_vflag) {
377 ND_PRINT(", RPKI-RTR");
378 return;
379 }
380 while (len) {
381 u_int pdu_len = rpki_rtr_pdu_print(ndo, pptr, len, 1, 8);
382 len -= pdu_len;
383 pptr += pdu_len;
384 }
385 }