]> The Tcpdump Group git mirrors - tcpdump/blob - print-rpki-rtr.c
We have to set the filter on every new file.
[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 * support for the The RPKI/Router Protocol as RFC6810
16 *
17 * Original code by Hannes Gredler (hannes@juniper.net)
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <netdissect-stdinc.h>
25
26 #include <string.h>
27
28 #include "netdissect.h"
29 #include "extract.h"
30 #include "addrtoname.h"
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 u_char version; /* Version number */
40 u_char pdu_type; /* PDU type */
41 union {
42 u_char session_id[2]; /* Session id */
43 u_char error_code[2]; /* Error code */
44 } u;
45 u_char length[4];
46 } rpki_rtr_pdu;
47 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
48
49 /*
50 * IPv4 Prefix PDU.
51 */
52 typedef struct rpki_rtr_pdu_ipv4_prefix_ {
53 rpki_rtr_pdu pdu_header;
54 u_char flags;
55 u_char prefix_length;
56 u_char max_length;
57 u_char zero;
58 u_char prefix[4];
59 u_char as[4];
60 } rpki_rtr_pdu_ipv4_prefix;
61
62 /*
63 * IPv6 Prefix PDU.
64 */
65 typedef struct rpki_rtr_pdu_ipv6_prefix_ {
66 rpki_rtr_pdu pdu_header;
67 u_char flags;
68 u_char prefix_length;
69 u_char max_length;
70 u_char zero;
71 u_char prefix[16];
72 u_char as[4];
73 } rpki_rtr_pdu_ipv6_prefix;
74
75 /*
76 * Error report PDU.
77 */
78 typedef struct rpki_rtr_pdu_error_report_ {
79 rpki_rtr_pdu pdu_header;
80 u_char encapsulated_pdu_length[4]; /* Encapsulated PDU length */
81 } rpki_rtr_pdu_error_report;
82
83 /*
84 * PDU type codes
85 */
86 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
87 #define RPKI_RTR_SERIAL_QUERY_PDU 1
88 #define RPKI_RTR_RESET_QUERY_PDU 2
89 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
90 #define RPKI_RTR_IPV4_PREFIX_PDU 4
91 #define RPKI_RTR_IPV6_PREFIX_PDU 6
92 #define RPKI_RTR_END_OF_DATA_PDU 7
93 #define RPKI_RTR_CACHE_RESET_PDU 8
94 #define RPKI_RTR_ERROR_REPORT_PDU 10
95
96 static const struct tok rpki_rtr_pdu_values[] = {
97 { RPKI_RTR_SERIAL_NOTIFY_PDU, "Serial Notify" },
98 { RPKI_RTR_SERIAL_QUERY_PDU, "Serial Query" },
99 { RPKI_RTR_RESET_QUERY_PDU, "Reset Query" },
100 { RPKI_RTR_CACHE_RESPONSE_PDU, "Cache Response" },
101 { RPKI_RTR_IPV4_PREFIX_PDU, "IPV4 Prefix" },
102 { RPKI_RTR_IPV6_PREFIX_PDU, "IPV6 Prefix" },
103 { RPKI_RTR_END_OF_DATA_PDU, "End of Data" },
104 { RPKI_RTR_CACHE_RESET_PDU, "Cache Reset" },
105 { RPKI_RTR_ERROR_REPORT_PDU, "Error Report" },
106 { 0, NULL}
107 };
108
109 static const struct tok rpki_rtr_error_codes[] = {
110 { 0, "Corrupt Data" },
111 { 1, "Internal Error" },
112 { 2, "No Data Available" },
113 { 3, "Invalid Request" },
114 { 4, "Unsupported Protocol Version" },
115 { 5, "Unsupported PDU Type" },
116 { 6, "Withdrawal of Unknown Record" },
117 { 7, "Duplicate Announcement Received" },
118 { 0, NULL}
119 };
120
121 /*
122 * Build a indentation string for a given indentation level.
123 * XXX this should be really in util.c
124 */
125 static char *
126 indent_string (u_int indent)
127 {
128 static char buf[20];
129 u_int idx;
130
131 idx = 0;
132 buf[idx] = '\0';
133
134 /*
135 * Does the static buffer fit ?
136 */
137 if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
138 return buf;
139 }
140
141 /*
142 * Heading newline.
143 */
144 buf[idx] = '\n';
145 idx++;
146
147 while (indent >= 8) {
148 buf[idx] = '\t';
149 idx++;
150 indent -= 8;
151 }
152
153 while (indent > 0) {
154 buf[idx] = ' ';
155 idx++;
156 indent--;
157 }
158
159 /*
160 * Trailing zero.
161 */
162 buf[idx] = '\0';
163
164 return buf;
165 }
166
167 /*
168 * Print a single PDU.
169 */
170 static void
171 rpki_rtr_pdu_print (netdissect_options *ndo, const u_char *tptr, u_int indent)
172 {
173 const rpki_rtr_pdu *pdu_header;
174 u_int pdu_type, pdu_len, hexdump;
175 const u_char *msg;
176
177 pdu_header = (const rpki_rtr_pdu *)tptr;
178 pdu_type = pdu_header->pdu_type;
179 pdu_len = EXTRACT_32BITS(pdu_header->length);
180 ND_TCHECK2(*tptr, pdu_len);
181 hexdump = FALSE;
182
183 ND_PRINT((ndo, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
184 indent_string(8),
185 pdu_header->version,
186 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
187 pdu_type, pdu_len));
188
189 switch (pdu_type) {
190
191 /*
192 * The following PDUs share the message format.
193 */
194 case RPKI_RTR_SERIAL_NOTIFY_PDU:
195 case RPKI_RTR_SERIAL_QUERY_PDU:
196 case RPKI_RTR_END_OF_DATA_PDU:
197 msg = (const u_char *)(pdu_header + 1);
198 ND_PRINT((ndo, "%sSession ID: 0x%04x, Serial: %u",
199 indent_string(indent+2),
200 EXTRACT_16BITS(pdu_header->u.session_id),
201 EXTRACT_32BITS(msg)));
202 break;
203
204 /*
205 * The following PDUs share the message format.
206 */
207 case RPKI_RTR_RESET_QUERY_PDU:
208 case RPKI_RTR_CACHE_RESET_PDU:
209
210 /*
211 * Zero payload PDUs.
212 */
213 break;
214
215 case RPKI_RTR_CACHE_RESPONSE_PDU:
216 ND_PRINT((ndo, "%sSession ID: 0x%04x",
217 indent_string(indent+2),
218 EXTRACT_16BITS(pdu_header->u.session_id)));
219 break;
220
221 case RPKI_RTR_IPV4_PREFIX_PDU:
222 {
223 const rpki_rtr_pdu_ipv4_prefix *pdu;
224
225 pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
226 ND_PRINT((ndo, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
227 indent_string(indent+2),
228 ipaddr_string(ndo, pdu->prefix),
229 pdu->prefix_length, pdu->max_length,
230 EXTRACT_32BITS(pdu->as), pdu->flags));
231 }
232 break;
233
234 case RPKI_RTR_IPV6_PREFIX_PDU:
235 {
236 const rpki_rtr_pdu_ipv6_prefix *pdu;
237
238 pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
239 ND_PRINT((ndo, "%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
240 indent_string(indent+2),
241 ip6addr_string(ndo, pdu->prefix),
242 pdu->prefix_length, pdu->max_length,
243 EXTRACT_32BITS(pdu->as), pdu->flags));
244 }
245 break;
246
247 case RPKI_RTR_ERROR_REPORT_PDU:
248 {
249 const rpki_rtr_pdu_error_report *pdu;
250 u_int encapsulated_pdu_length, text_length, tlen, error_code;
251
252 pdu = (const rpki_rtr_pdu_error_report *)tptr;
253 encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
254 ND_TCHECK2(*tptr, encapsulated_pdu_length);
255 tlen = pdu_len;
256
257 error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
258 ND_PRINT((ndo, "%sError code: %s (%u), Encapsulated PDU length: %u",
259 indent_string(indent+2),
260 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
261 error_code, encapsulated_pdu_length));
262
263 tptr += sizeof(*pdu);
264 tlen -= sizeof(*pdu);
265
266 /*
267 * Recurse if there is an encapsulated PDU.
268 */
269 if (encapsulated_pdu_length &&
270 (encapsulated_pdu_length <= tlen)) {
271 ND_PRINT((ndo, "%s-----encapsulated PDU-----", indent_string(indent+4)));
272 rpki_rtr_pdu_print(ndo, tptr, indent+2);
273 }
274
275 tptr += encapsulated_pdu_length;
276 tlen -= encapsulated_pdu_length;
277
278 /*
279 * Extract, trail-zero and print the Error message.
280 */
281 text_length = 0;
282 if (tlen > 4) {
283 text_length = EXTRACT_32BITS(tptr);
284 tptr += 4;
285 tlen -= 4;
286 }
287 ND_TCHECK2(*tptr, text_length);
288 if (text_length && (text_length <= tlen )) {
289 ND_PRINT((ndo, "%sError text: ", indent_string(indent+2)));
290 fn_printn(ndo, tptr, text_length, ndo->ndo_snapend);
291 }
292 }
293 break;
294
295 default:
296
297 /*
298 * Unknown data, please hexdump.
299 */
300 hexdump = TRUE;
301 }
302
303 /* do we also want to see a hex dump ? */
304 if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
305 print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
306 }
307 return;
308
309 trunc:
310 ND_PRINT((ndo, "|trunc"));
311 return;
312 }
313
314 void
315 rpki_rtr_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
316 {
317 u_int tlen, pdu_type, pdu_len;
318 const u_char *tptr;
319 const rpki_rtr_pdu *pdu_header;
320
321 tptr = pptr;
322 tlen = len;
323
324 if (!ndo->ndo_vflag) {
325 ND_PRINT((ndo, ", RPKI-RTR"));
326 return;
327 }
328
329 while (tlen >= sizeof(rpki_rtr_pdu)) {
330
331 ND_TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
332
333 pdu_header = (const rpki_rtr_pdu *)tptr;
334 pdu_type = pdu_header->pdu_type;
335 pdu_len = EXTRACT_32BITS(pdu_header->length);
336 ND_TCHECK2(*tptr, pdu_len);
337
338 /* infinite loop check */
339 if (!pdu_type || !pdu_len) {
340 break;
341 }
342
343 if (tlen < pdu_len) {
344 goto trunc;
345 }
346
347 /*
348 * Print the PDU.
349 */
350 rpki_rtr_pdu_print(ndo, tptr, 8);
351
352 tlen -= pdu_len;
353 tptr += pdu_len;
354 }
355 return;
356 trunc:
357 ND_PRINT((ndo, "\n\t[|RPKI-RTR]"));
358 }
359
360 /*
361 * Local Variables:
362 * c-style: whitesmith
363 * c-basic-offset: 4
364 * End:
365 */