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