xref: /freebsd/contrib/tcpdump/print-rpki-rtr.c (revision e53a21abdf2953714e44e3c54b4bb78557cb096c)
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  * XXX this should be really in util.c
126  */
127 static char *
128 indent_string (u_int indent)
129 {
130     static char buf[20];
131     u_int idx;
132 
133     idx = 0;
134     buf[idx] = '\0';
135 
136     /*
137      * Does the static buffer fit ?
138      */
139     if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
140 	return buf;
141     }
142 
143     /*
144      * Heading newline.
145      */
146     buf[idx] = '\n';
147     idx++;
148 
149     while (indent >= 8) {
150 	buf[idx] = '\t';
151 	idx++;
152 	indent -= 8;
153     }
154 
155     while (indent > 0) {
156 	buf[idx] = ' ';
157 	idx++;
158 	indent--;
159     }
160 
161     /*
162      * Trailing zero.
163      */
164     buf[idx] = '\0';
165 
166     return buf;
167 }
168 
169 /*
170  * Print a single PDU.
171  */
172 static u_int
173 rpki_rtr_pdu_print(netdissect_options *ndo, const u_char *tptr, const u_int len,
174 		   const u_char recurse, const u_int indent)
175 {
176     const rpki_rtr_pdu *pdu_header;
177     u_int pdu_type, pdu_len, hexdump;
178     const u_char *msg;
179     uint8_t pdu_ver;
180 
181     if (len < sizeof(rpki_rtr_pdu)) {
182 	ND_PRINT("(%u bytes is too few to decode)", len);
183 	goto invalid;
184     }
185     pdu_header = (const rpki_rtr_pdu *)tptr;
186     pdu_ver = GET_U_1(pdu_header->version);
187     if (pdu_ver != 0) {
188 	/* Skip the rest of the input buffer because even if this is
189 	 * a well-formed PDU of a future RPKI-Router protocol version
190 	 * followed by a well-formed PDU of RPKI-Router protocol
191 	 * version 0, there is no way to know exactly how to skip the
192 	 * current PDU.
193 	 */
194 	ND_PRINT("%sRPKI-RTRv%u (unknown)", indent_string(8), pdu_ver);
195 	return len;
196     }
197     pdu_type = GET_U_1(pdu_header->pdu_type);
198     pdu_len = GET_BE_U_4(pdu_header->length);
199     /* Do not check bounds with pdu_len yet, do it in the case blocks
200      * below to make it possible to decode at least the beginning of
201      * a truncated Error Report PDU or a truncated encapsulated PDU.
202      */
203     hexdump = FALSE;
204 
205     ND_PRINT("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
206 	   indent_string(8),
207 	   pdu_ver,
208 	   tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
209 	   pdu_type, pdu_len);
210     if (pdu_len < sizeof(rpki_rtr_pdu) || pdu_len > len)
211 	goto invalid;
212 
213     switch (pdu_type) {
214 
215 	/*
216 	 * The following PDUs share the message format.
217 	 */
218     case RPKI_RTR_SERIAL_NOTIFY_PDU:
219     case RPKI_RTR_SERIAL_QUERY_PDU:
220     case RPKI_RTR_END_OF_DATA_PDU:
221 	if (pdu_len != sizeof(rpki_rtr_pdu) + 4)
222 	    goto invalid;
223         msg = (const u_char *)(pdu_header + 1);
224 	ND_PRINT("%sSession ID: 0x%04x, Serial: %u",
225 	       indent_string(indent+2),
226 	       GET_BE_U_2(pdu_header->u.session_id),
227 	       GET_BE_U_4(msg));
228 	break;
229 
230 	/*
231 	 * The following PDUs share the message format.
232 	 */
233     case RPKI_RTR_RESET_QUERY_PDU:
234     case RPKI_RTR_CACHE_RESET_PDU:
235 	if (pdu_len != sizeof(rpki_rtr_pdu))
236 	    goto invalid;
237 	/* no additional boundary to check */
238 
239 	/*
240 	 * Zero payload PDUs.
241 	 */
242 	break;
243 
244     case RPKI_RTR_CACHE_RESPONSE_PDU:
245 	if (pdu_len != sizeof(rpki_rtr_pdu))
246 	    goto invalid;
247 	/* no additional boundary to check */
248 	ND_PRINT("%sSession ID: 0x%04x",
249 	       indent_string(indent+2),
250 	       GET_BE_U_2(pdu_header->u.session_id));
251 	break;
252 
253     case RPKI_RTR_IPV4_PREFIX_PDU:
254 	{
255 	    const rpki_rtr_pdu_ipv4_prefix *pdu;
256 
257 	    if (pdu_len != sizeof(rpki_rtr_pdu_ipv4_prefix))
258 		goto invalid;
259 	    pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
260 	    ND_PRINT("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
261 		   indent_string(indent+2),
262 		   GET_IPADDR_STRING(pdu->prefix),
263 		   GET_U_1(pdu->prefix_length), GET_U_1(pdu->max_length),
264 		   GET_BE_U_4(pdu->as), GET_U_1(pdu->flags));
265 	}
266 	break;
267 
268     case RPKI_RTR_IPV6_PREFIX_PDU:
269 	{
270 	    const rpki_rtr_pdu_ipv6_prefix *pdu;
271 
272 	    if (pdu_len != sizeof(rpki_rtr_pdu_ipv6_prefix))
273 		goto invalid;
274 	    pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
275 	    ND_PRINT("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
276 		   indent_string(indent+2),
277 		   GET_IP6ADDR_STRING(pdu->prefix),
278 		   GET_U_1(pdu->prefix_length), GET_U_1(pdu->max_length),
279 		   GET_BE_U_4(pdu->as), GET_U_1(pdu->flags));
280 	}
281 	break;
282 
283     case RPKI_RTR_ERROR_REPORT_PDU:
284 	{
285 	    const rpki_rtr_pdu_error_report *pdu;
286 	    u_int encapsulated_pdu_length, text_length, tlen, error_code;
287 
288 	    tlen = sizeof(rpki_rtr_pdu);
289 	    /* Do not test for the "Length of Error Text" data element yet. */
290 	    if (pdu_len < tlen + 4)
291 		goto invalid;
292 	    pdu = (const rpki_rtr_pdu_error_report *)tptr;
293 	    encapsulated_pdu_length = GET_BE_U_4(pdu->encapsulated_pdu_length);
294 	    tlen += 4;
295 	    /* Safe up to and including the "Length of Encapsulated PDU"
296 	     * data element, more data elements may be present.
297 	     */
298 
299 	    error_code = GET_BE_U_2(pdu->pdu_header.u.error_code);
300 	    ND_PRINT("%sError code: %s (%u), Encapsulated PDU length: %u",
301 		   indent_string(indent+2),
302 		   tok2str(rpki_rtr_error_codes, "Unknown", error_code),
303 		   error_code, encapsulated_pdu_length);
304 
305 	    if (encapsulated_pdu_length) {
306 		/* Section 5.10 of RFC 6810 says:
307 		 * "An Error Report PDU MUST NOT be sent for an Error Report PDU."
308 		 *
309 		 * However, as far as the protocol encoding goes Error Report PDUs can
310 		 * happen to be nested in each other, however many times, in which case
311 		 * the decoder should still print such semantically incorrect PDUs.
312 		 *
313 		 * That said, "the Erroneous PDU field MAY be truncated" (ibid), thus
314 		 * to keep things simple this implementation decodes only the two
315 		 * outermost layers of PDUs and makes bounds checks in the outer and
316 		 * the inner PDU independently.
317 		 */
318 		if (pdu_len < tlen + encapsulated_pdu_length)
319 		    goto invalid;
320 		if (! recurse) {
321 		    ND_TCHECK_LEN(tptr, tlen + encapsulated_pdu_length);
322 		} else {
323 		    ND_PRINT("%s-----encapsulated PDU-----", indent_string(indent+4));
324 		    rpki_rtr_pdu_print(ndo, tptr + tlen,
325 			encapsulated_pdu_length, 0, indent + 2);
326 		}
327 		tlen += encapsulated_pdu_length;
328 	    }
329 
330 	    if (pdu_len < tlen + 4)
331 		goto invalid;
332 	    /*
333 	     * Extract, trail-zero and print the Error message.
334 	     */
335 	    text_length = GET_BE_U_4(tptr + tlen);
336 	    tlen += 4;
337 	    /* Safe up to and including the "Length of Error Text" data element,
338 	     * one more data element may be present.
339 	     */
340 
341 	    if (text_length) {
342 		if (pdu_len < tlen + text_length)
343 		    goto invalid;
344 		/* nd_printn() makes the bounds check */
345 		ND_PRINT("%sError text: ", indent_string(indent+2));
346 		(void)nd_printn(ndo, tptr + tlen, text_length, NULL);
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 }
386