drm/i915: Use the spin_lock_irq() family of functions
[dragonfly.git] / contrib / tcpdump / print-rpki-rtr.c
blob8679759ef8dfa8413b386315ca8d726556a33863
1 /*
2 * Copyright (c) 1998-2011 The TCPDUMP project
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.
15 * support for the The RPKI/Router Protocol Protocol as per draft-ietf-sidr-rpki-rtr-12
17 * Original code by Hannes Gredler (hannes@juniper.net)
20 #ifndef lint
21 static const char rcsid[] _U_ =
22 "@(#) $Header: /tcpdump/master/tcpdump/print-rpki_rtr.c,v 1.10 2008-03-20 09:30:56 hannes Exp $";
23 #endif
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <tcpdump-stdinc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "interface.h"
36 #include "extract.h"
37 #include "addrtoname.h"
40 * RPKI/Router PDU header
42 * Here's what the PDU header looks like.
43 * The length does include the version and length fields.
45 typedef struct rpki_rtr_pdu_ {
46 u_char version; /* Version number */
47 u_char pdu_type; /* PDU type */
48 union {
49 u_char cache_nonce[2]; /* Cache Nonce */
50 u_char error_code[2]; /* Error code */
51 } u;
52 u_char length[4];
53 } rpki_rtr_pdu;
54 #define RPKI_RTR_PDU_OVERHEAD (offsetof(rpki_rtr_pdu, rpki_rtr_pdu_msg))
57 * IPv4 Prefix PDU.
59 typedef struct rpki_rtr_pdu_ipv4_prefix_ {
60 rpki_rtr_pdu pdu_header;
61 u_char flags;
62 u_char prefix_length;
63 u_char max_length;
64 u_char zero;
65 u_char prefix[4];
66 u_char as[4];
67 } rpki_rtr_pdu_ipv4_prefix;
70 * IPv6 Prefix PDU.
72 typedef struct rpki_rtr_pdu_ipv6_prefix_ {
73 rpki_rtr_pdu pdu_header;
74 u_char flags;
75 u_char prefix_length;
76 u_char max_length;
77 u_char zero;
78 u_char prefix[16];
79 u_char as[4];
80 } rpki_rtr_pdu_ipv6_prefix;
83 * Error report PDU.
85 typedef struct rpki_rtr_pdu_error_report_ {
86 rpki_rtr_pdu pdu_header;
87 u_char encapsulated_pdu_length[4]; /* Encapsulated PDU length */
88 } rpki_rtr_pdu_error_report;
91 * PDU type codes
93 #define RPKI_RTR_SERIAL_NOTIFY_PDU 0
94 #define RPKI_RTR_SERIAL_QUERY_PDU 1
95 #define RPKI_RTR_RESET_QUERY_PDU 2
96 #define RPKI_RTR_CACHE_RESPONSE_PDU 3
97 #define RPKI_RTR_IPV4_PREFIX_PDU 4
98 #define RPKI_RTR_IPV6_PREFIX_PDU 6
99 #define RPKI_RTR_END_OF_DATA_PDU 7
100 #define RPKI_RTR_CACHE_RESET_PDU 8
101 #define RPKI_RTR_ERROR_REPORT_PDU 10
103 static const struct tok rpki_rtr_pdu_values[] = {
104 { RPKI_RTR_SERIAL_NOTIFY_PDU, "Serial Notify" },
105 { RPKI_RTR_SERIAL_QUERY_PDU, "Serial Query" },
106 { RPKI_RTR_RESET_QUERY_PDU, "Reset Query" },
107 { RPKI_RTR_CACHE_RESPONSE_PDU, "Cache Response" },
108 { RPKI_RTR_IPV4_PREFIX_PDU, "IPV4 Prefix" },
109 { RPKI_RTR_IPV6_PREFIX_PDU, "IPV6 Prefix" },
110 { RPKI_RTR_END_OF_DATA_PDU, "End of Data" },
111 { RPKI_RTR_CACHE_RESET_PDU, "Cache Reset" },
112 { RPKI_RTR_ERROR_REPORT_PDU, "Error Report" },
113 { 0, NULL}
116 static const struct tok rpki_rtr_error_codes[] = {
117 { 0, "Corrupt Data" },
118 { 1, "Internal Error" },
119 { 2, "No Data Available" },
120 { 3, "Invalid Request" },
121 { 4, "Unsupported Protocol Version" },
122 { 5, "Unsupported PDU Type" },
123 { 6, "Withdrawal of Unknown Record" },
124 { 7, "Duplicate Announcement Received" },
125 { 0, NULL}
129 * Build a identation string for a given identation level.
130 * XXX this should be really in util.c
132 static char *
133 indent_string (u_int indent)
135 static char buf[20];
136 u_int idx;
138 idx = 0;
139 buf[idx] = '\0';
142 * Does the static buffer fit ?
144 if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
145 return buf;
149 * Heading newline.
151 buf[idx] = '\n';
152 idx++;
154 while (indent >= 8) {
155 buf[idx] = '\t';
156 idx++;
157 indent -= 8;
160 while (indent > 0) {
161 buf[idx] = ' ';
162 idx++;
163 indent--;
167 * Trailing zero.
169 buf[idx] = '\0';
171 return buf;
175 * Print a single PDU.
177 static void
178 rpki_rtr_pdu_print (const u_char *tptr, u_int indent)
180 const rpki_rtr_pdu *pdu_header;
181 u_int pdu_type, pdu_len, hexdump;
182 const u_char *msg;
184 pdu_header = (rpki_rtr_pdu *)tptr;
185 pdu_type = pdu_header->pdu_type;
186 pdu_len = EXTRACT_32BITS(pdu_header->length);
187 hexdump = FALSE;
189 printf("%sRPKI-RTRv%u, %s PDU (%u), length: %u",
190 indent_string(8),
191 pdu_header->version,
192 tok2str(rpki_rtr_pdu_values, "Unknown", pdu_type),
193 pdu_type, pdu_len);
195 switch (pdu_type) {
198 * The following PDUs share the message format.
200 case RPKI_RTR_SERIAL_NOTIFY_PDU:
201 case RPKI_RTR_SERIAL_QUERY_PDU:
202 case RPKI_RTR_END_OF_DATA_PDU:
203 msg = (const u_char *)(pdu_header + 1);
204 printf("%sCache-Nonce: 0x%04x, Serial: %u",
205 indent_string(indent+2),
206 EXTRACT_16BITS(pdu_header->u.cache_nonce),
207 EXTRACT_32BITS(msg));
208 break;
211 * The following PDUs share the message format.
213 case RPKI_RTR_RESET_QUERY_PDU:
214 case RPKI_RTR_CACHE_RESET_PDU:
217 * Zero payload PDUs.
219 break;
221 case RPKI_RTR_CACHE_RESPONSE_PDU:
222 printf("%sCache-Nonce: 0x%04x",
223 indent_string(indent+2),
224 EXTRACT_16BITS(pdu_header->u.cache_nonce));
225 break;
227 case RPKI_RTR_IPV4_PREFIX_PDU:
229 rpki_rtr_pdu_ipv4_prefix *pdu;
231 pdu = (rpki_rtr_pdu_ipv4_prefix *)tptr;
232 printf("%sIPv4 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
233 indent_string(indent+2),
234 ipaddr_string(pdu->prefix),
235 pdu->prefix_length, pdu->max_length,
236 EXTRACT_32BITS(pdu->as), pdu->flags);
238 break;
240 #ifdef INET6
241 case RPKI_RTR_IPV6_PREFIX_PDU:
243 rpki_rtr_pdu_ipv6_prefix *pdu;
245 pdu = (rpki_rtr_pdu_ipv6_prefix *)tptr;
246 printf("%sIPv6 Prefix %s/%u-%u, origin-as %u, flags 0x%02x",
247 indent_string(indent+2),
248 ip6addr_string(pdu->prefix),
249 pdu->prefix_length, pdu->max_length,
250 EXTRACT_32BITS(pdu->as), pdu->flags);
252 break;
253 #endif
255 case RPKI_RTR_ERROR_REPORT_PDU:
257 rpki_rtr_pdu_error_report *pdu;
258 u_int encapsulated_pdu_length, text_length, tlen, error_code;
259 u_char buf[80];
261 pdu = (rpki_rtr_pdu_error_report *)tptr;
262 encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
263 tlen = pdu_len;
265 error_code = EXTRACT_16BITS(pdu->pdu_header.u.error_code);
266 printf("%sError code: %s (%u), Encapsulated PDU length: %u",
267 indent_string(indent+2),
268 tok2str(rpki_rtr_error_codes, "Unknown", error_code),
269 error_code, encapsulated_pdu_length);
271 tptr += sizeof(*pdu);
272 tlen -= sizeof(*pdu);
275 * Recurse if there is an encapsulated PDU.
277 if (encapsulated_pdu_length &&
278 (encapsulated_pdu_length <= tlen)) {
279 printf("%s-----encapsulated PDU-----", indent_string(indent+4));
280 rpki_rtr_pdu_print(tptr, indent+2);
283 tptr += encapsulated_pdu_length;
284 tlen -= encapsulated_pdu_length;
287 * Extract, trail-zero and print the Error message.
289 text_length = 0;
290 if (tlen > 4) {
291 text_length = EXTRACT_32BITS(tptr);
292 tptr += 4;
293 tlen -= 4;
295 if (text_length && (text_length <= tlen )) {
296 memcpy(buf, tptr, MIN(sizeof(buf)-1, text_length));
297 buf[text_length] = '\0';
298 printf("%sError text: %s", indent_string(indent+2), buf);
301 break;
303 default:
306 * Unknown data, please hexdump.
308 hexdump = TRUE;
311 /* do we also want to see a hex dump ? */
312 if (vflag > 1 || (vflag && hexdump)) {
313 print_unknown_data(tptr,"\n\t ", pdu_len);
317 void
318 rpki_rtr_print(register const u_char *pptr, register u_int len) {
320 u_int tlen, pdu_type, pdu_len;
321 const u_char *tptr;
322 const rpki_rtr_pdu *pdu_header;
324 tptr = pptr;
325 tlen = len;
327 if (!vflag) {
328 printf(", RPKI-RTR");
329 return;
332 while (tlen >= sizeof(rpki_rtr_pdu)) {
334 TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
336 pdu_header = (rpki_rtr_pdu *)tptr;
337 pdu_type = pdu_header->pdu_type;
338 pdu_len = EXTRACT_32BITS(pdu_header->length);
340 /* infinite loop check */
341 if (!pdu_type || !pdu_len) {
342 break;
345 TCHECK2(*tptr, pdu_len);
346 if (tlen < pdu_len) {
347 goto trunc;
351 * Print the PDU.
353 rpki_rtr_pdu_print(tptr, 8);
355 tlen -= pdu_len;
356 tptr += pdu_len;
358 return;
359 trunc:
360 printf("\n\t[|RPKI-RTR]");
364 * Local Variables:
365 * c-style: whitesmith
366 * c-basic-offset: 4
367 * End: