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