]> The Tcpdump Group git mirrors - tcpdump/blob - print-rpki-rtr.c
NDOize DCCP, Linux socket and RPKI-Router decoders
[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 <tcpdump-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 identation string for a given identation 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 = (rpki_rtr_pdu *)tptr;
178 pdu_type = pdu_header->pdu_type;
179 pdu_len = EXTRACT_32BITS(pdu_header->length);
180 hexdump = FALSE;
181
182 ND_PRINT((ndo, "%sRPKI-RTRv%u, %s PDU (%u), length: %u",
183 indent_string(8),
184 pdu_header->version,
185 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
186 pdu_type, pdu_len));
187
188 switch (pdu_type) {
189
190 /*
191 * The following PDUs share the message format.
192 */
193 case RPKI_RTR_SERIAL_NOTIFY_PDU:
194 case RPKI_RTR_SERIAL_QUERY_PDU:
195 case RPKI_RTR_END_OF_DATA_PDU:
196 msg = (const u_char *)(pdu_header + 1);
197 ND_PRINT((ndo, "%sSession ID: 0x%04x, Serial: %u",
198 indent_string(indent+2),
199 EXTRACT_16BITS(pdu_header->u.session_id),
200 EXTRACT_32BITS(msg)));
201 break;
202
203 /*
204 * The following PDUs share the message format.
205 */
206 case RPKI_RTR_RESET_QUERY_PDU:
207 case RPKI_RTR_CACHE_RESET_PDU:
208
209 /*
210 * Zero payload PDUs.
211 */
212 break;
213
214 case RPKI_RTR_CACHE_RESPONSE_PDU:
215 ND_PRINT((ndo, "%sSession ID: 0x%04x",
216 indent_string(indent+2),
217 EXTRACT_16BITS(pdu_header->u.session_id)));
218 break;
219
220 case RPKI_RTR_IPV4_PREFIX_PDU:
221 {
222 rpki_rtr_pdu_ipv4_prefix *pdu;
223
224 pdu = (rpki_rtr_pdu_ipv4_prefix *)tptr;
225 ND_PRINT((ndo, "%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
226 indent_string(indent+2),
227 ipaddr_string(pdu->prefix),
228 pdu->prefix_length, pdu->max_length,
229 EXTRACT_32BITS(pdu->as), pdu->flags));
230 }
231 break;
232
233 #ifdef INET6
234 case RPKI_RTR_IPV6_PREFIX_PDU:
235 {
236 rpki_rtr_pdu_ipv6_prefix *pdu;
237
238 pdu = (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(pdu->prefix),
242 pdu->prefix_length, pdu->max_length,
243 EXTRACT_32BITS(pdu->as), pdu->flags));
244 }
245 break;
246 #endif
247
248 case RPKI_RTR_ERROR_REPORT_PDU:
249 {
250 rpki_rtr_pdu_error_report *pdu;
251 u_int encapsulated_pdu_length, text_length, tlen, error_code;
252 u_char buf[80];
253
254 pdu = (rpki_rtr_pdu_error_report *)tptr;
255 encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
256 tlen = pdu_len;
257
258 error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
259 ND_PRINT((ndo, "%sError code: %s (%u), Encapsulated PDU length: %u",
260 indent_string(indent+2),
261 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
262 error_code, encapsulated_pdu_length));
263
264 tptr += sizeof(*pdu);
265 tlen -= sizeof(*pdu);
266
267 /*
268 * Recurse if there is an encapsulated PDU.
269 */
270 if (encapsulated_pdu_length &&
271 (encapsulated_pdu_length <= tlen)) {
272 ND_PRINT((ndo, "%s-----encapsulated PDU-----", indent_string(indent+4)));
273 rpki_rtr_pdu_print(ndo, tptr, indent+2);
274 }
275
276 tptr += encapsulated_pdu_length;
277 tlen -= encapsulated_pdu_length;
278
279 /*
280 * Extract, trail-zero and print the Error message.
281 */
282 text_length = 0;
283 if (tlen > 4) {
284 text_length = EXTRACT_32BITS(tptr);
285 tptr += 4;
286 tlen -= 4;
287 }
288 if (text_length && (text_length <= tlen )) {
289 memcpy(buf, tptr, MIN(sizeof(buf)-1, text_length));
290 buf[text_length] = '\0';
291 ND_PRINT((ndo, "%sError text: %s", indent_string(indent+2), buf));
292 }
293 }
294 break;
295
296 default:
297
298 /*
299 * Unknown data, please hexdump.
300 */
301 hexdump = TRUE;
302 }
303
304 /* do we also want to see a hex dump ? */
305 if (ndo->ndo_vflag > 1 || (ndo->ndo_vflag && hexdump)) {
306 print_unknown_data(ndo,tptr,"\n\t ", pdu_len);
307 }
308 }
309
310 void
311 rpki_rtr_print(netdissect_options *ndo, register const u_char *pptr, register u_int len) {
312
313 u_int tlen, pdu_type, pdu_len;
314 const u_char *tptr;
315 const rpki_rtr_pdu *pdu_header;
316
317 tptr = pptr;
318 tlen = len;
319
320 if (!ndo->ndo_vflag) {
321 ND_PRINT((ndo, ", RPKI-RTR"));
322 return;
323 }
324
325 while (tlen >= sizeof(rpki_rtr_pdu)) {
326
327 ND_TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
328
329 pdu_header = (rpki_rtr_pdu *)tptr;
330 pdu_type = pdu_header->pdu_type;
331 pdu_len = EXTRACT_32BITS(pdu_header->length);
332
333 /* infinite loop check */
334 if (!pdu_type || !pdu_len) {
335 break;
336 }
337
338 ND_TCHECK2(*tptr, pdu_len);
339 if (tlen < pdu_len) {
340 goto trunc;
341 }
342
343 /*
344 * Print the PDU.
345 */
346 rpki_rtr_pdu_print(ndo, tptr, 8);
347
348 tlen -= pdu_len;
349 tptr += pdu_len;
350 }
351 return;
352 trunc:
353 ND_PRINT((ndo, "\n\t[|RPKI-RTR]"));
354 }
355
356 /*
357 * Local Variables:
358 * c-style: whitesmith
359 * c-basic-offset: 4
360 * End:
361 */