]> The Tcpdump Group git mirrors - tcpdump/blob - print-rpki-rtr.c
Add a summary comment in all other printers
[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@juniper.net)
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 * RPKI/Router PDU header
36 *
37 * Here's what the PDU header looks like.
38 * The length does include the version and length fields.
39 */
40 typedef struct rpki_rtr_pdu_ {
41 u_char version; /* Version number */
42 u_char pdu_type; /* PDU type */
43 union {
44 u_char session_id[2]; /* Session id */
45 u_char error_code[2]; /* Error code */
46 } u;
47 u_char length[4];
48 } rpki_rtr_pdu;
49 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
50
51 /*
52 * IPv4 Prefix PDU.
53 */
54 typedef struct rpki_rtr_pdu_ipv4_prefix_ {
55 rpki_rtr_pdu pdu_header;
56 u_char flags;
57 u_char prefix_length;
58 u_char max_length;
59 u_char zero;
60 u_char prefix[4];
61 u_char as[4];
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 u_char flags;
70 u_char prefix_length;
71 u_char max_length;
72 u_char zero;
73 u_char prefix[16];
74 u_char as[4];
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 u_char encapsulated_pdu_length[4]; /* Encapsulated PDU length */
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 void
173 rpki_rtr_pdu_print (netdissect_options *ndo, const u_char *tptr, 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
179 pdu_header = (const rpki_rtr_pdu *)tptr;
180 pdu_type = pdu_header->pdu_type;
181 pdu_len = EXTRACT_32BITS(pdu_header->length);
182 ND_TCHECK2(*tptr, pdu_len);
183 hexdump = FALSE;
184
185 ND_PRINT((ndo, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
186 indent_string(8),
187 pdu_header->version,
188 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
189 pdu_type, pdu_len));
190
191 switch (pdu_type) {
192
193 /*
194 * The following PDUs share the message format.
195 */
196 case RPKI_RTR_SERIAL_NOTIFY_PDU:
197 case RPKI_RTR_SERIAL_QUERY_PDU:
198 case RPKI_RTR_END_OF_DATA_PDU:
199 msg = (const u_char *)(pdu_header + 1);
200 ND_PRINT((ndo, "%sSession ID: 0x%04x, Serial: %u",
201 indent_string(indent+2),
202 EXTRACT_16BITS(pdu_header->u.session_id),
203 EXTRACT_32BITS(msg)));
204 break;
205
206 /*
207 * The following PDUs share the message format.
208 */
209 case RPKI_RTR_RESET_QUERY_PDU:
210 case RPKI_RTR_CACHE_RESET_PDU:
211
212 /*
213 * Zero payload PDUs.
214 */
215 break;
216
217 case RPKI_RTR_CACHE_RESPONSE_PDU:
218 ND_PRINT((ndo, "%sSession ID: 0x%04x",
219 indent_string(indent+2),
220 EXTRACT_16BITS(pdu_header->u.session_id)));
221 break;
222
223 case RPKI_RTR_IPV4_PREFIX_PDU:
224 {
225 const rpki_rtr_pdu_ipv4_prefix *pdu;
226
227 pdu = (const rpki_rtr_pdu_ipv4_prefix *)tptr;
228 ND_PRINT((ndo, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
229 indent_string(indent+2),
230 ipaddr_string(ndo, pdu->prefix),
231 pdu->prefix_length, pdu->max_length,
232 EXTRACT_32BITS(pdu->as), pdu->flags));
233 }
234 break;
235
236 case RPKI_RTR_IPV6_PREFIX_PDU:
237 {
238 const rpki_rtr_pdu_ipv6_prefix *pdu;
239
240 pdu = (const rpki_rtr_pdu_ipv6_prefix *)tptr;
241 ND_PRINT((ndo, "%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
242 indent_string(indent+2),
243 ip6addr_string(ndo, pdu->prefix),
244 pdu->prefix_length, pdu->max_length,
245 EXTRACT_32BITS(pdu->as), pdu->flags));
246 }
247 break;
248
249 case RPKI_RTR_ERROR_REPORT_PDU:
250 {
251 const rpki_rtr_pdu_error_report *pdu;
252 u_int encapsulated_pdu_length, text_length, tlen, error_code;
253
254 pdu = (const rpki_rtr_pdu_error_report *)tptr;
255 encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
256 ND_TCHECK2(*tptr, encapsulated_pdu_length);
257 tlen = pdu_len;
258
259 error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
260 ND_PRINT((ndo, "%sError code: %s (%u), Encapsulated PDU length: %u",
261 indent_string(indent+2),
262 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
263 error_code, encapsulated_pdu_length));
264
265 tptr += sizeof(*pdu);
266 tlen -= sizeof(*pdu);
267
268 /*
269 * Recurse if there is an encapsulated PDU.
270 */
271 if (encapsulated_pdu_length &&
272 (encapsulated_pdu_length <= tlen)) {
273 ND_PRINT((ndo, "%s-----encapsulated PDU-----", indent_string(indent+4)));
274 rpki_rtr_pdu_print(ndo, tptr, indent+2);
275 }
276
277 tptr += encapsulated_pdu_length;
278 tlen -= encapsulated_pdu_length;
279
280 /*
281 * Extract, trail-zero and print the Error message.
282 */
283 text_length = 0;
284 if (tlen > 4) {
285 text_length = EXTRACT_32BITS(tptr);
286 tptr += 4;
287 tlen -= 4;
288 }
289 ND_TCHECK2(*tptr, text_length);
290 if (text_length && (text_length <= tlen )) {
291 ND_PRINT((ndo, "%sError text: ", indent_string(indent+2)));
292 if (fn_printn(ndo, tptr, text_length, ndo->ndo_snapend))
293 goto trunc;
294 }
295 }
296 break;
297
298 default:
299
300 /*
301 * Unknown data, please hexdump.
302 */
303 hexdump = TRUE;
304 }
305
306 /* do we also want to see a hex dump ? */
307 if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
308 print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
309 }
310 return;
311
312 trunc:
313 ND_PRINT((ndo, "|trunc"));
314 return;
315 }
316
317 void
318 rpki_rtr_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
319 {
320 u_int tlen, pdu_type, pdu_len;
321 const u_char *tptr;
322 const rpki_rtr_pdu *pdu_header;
323
324 tptr = pptr;
325 tlen = len;
326
327 if (!ndo->ndo_vflag) {
328 ND_PRINT((ndo, ", RPKI-RTR"));
329 return;
330 }
331
332 while (tlen >= sizeof(rpki_rtr_pdu)) {
333
334 ND_TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
335
336 pdu_header = (const rpki_rtr_pdu *)tptr;
337 pdu_type = pdu_header->pdu_type;
338 pdu_len = EXTRACT_32BITS(pdu_header->length);
339 ND_TCHECK2(*tptr, pdu_len);
340
341 /* infinite loop check */
342 if (!pdu_type || !pdu_len) {
343 break;
344 }
345
346 if (tlen < pdu_len) {
347 goto trunc;
348 }
349
350 /*
351 * Print the PDU.
352 */
353 rpki_rtr_pdu_print(ndo, tptr, 8);
354
355 tlen -= pdu_len;
356 tptr += pdu_len;
357 }
358 return;
359 trunc:
360 ND_PRINT((ndo, "\n\t[|RPKI-RTR]"));
361 }
362
363 /*
364 * Local Variables:
365 * c-style: whitesmith
366 * c-basic-offset: 4
367 * End:
368 */