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