Home | History | Annotate | Download | only in tcpdump
      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 (at) juniper.net)
     18  */
     19 
     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
     24 
     25 #ifdef HAVE_CONFIG_H
     26 #include "config.h"
     27 #endif
     28 
     29 #include <tcpdump-stdinc.h>
     30 
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 
     35 #include "interface.h"
     36 #include "extract.h"
     37 #include "addrtoname.h"
     38 
     39 /*
     40  * RPKI/Router PDU header
     41  *
     42  * Here's what the PDU header looks like.
     43  * The length does include the version and length fields.
     44  */
     45 typedef struct rpki_rtr_pdu_ {
     46     u_char version;		/* Version number */
     47     u_char pdu_type;		/* PDU type */
     48     union {
     49 	u_char session_id[2];	/* Session id */
     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))
     55 
     56 /*
     57  * IPv4 Prefix PDU.
     58  */
     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;
     68 
     69 /*
     70  * IPv6 Prefix PDU.
     71  */
     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;
     81 
     82 /*
     83  * Error report PDU.
     84  */
     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;
     89 
     90 /*
     91  * PDU type codes
     92  */
     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
    102 
    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}
    114 };
    115 
    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}
    126 };
    127 
    128 /*
    129  * Build a identation string for a given identation level.
    130  * XXX this should be really in util.c
    131  */
    132 static char *
    133 indent_string (u_int indent)
    134 {
    135     static char buf[20];
    136     u_int idx;
    137 
    138     idx = 0;
    139     buf[idx] = '\0';
    140 
    141     /*
    142      * Does the static buffer fit ?
    143      */
    144     if (sizeof(buf) < ((indent/8) + (indent %8) + 2)) {
    145 	return buf;
    146     }
    147 
    148     /*
    149      * Heading newline.
    150      */
    151     buf[idx] = '\n';
    152     idx++;
    153 
    154     while (indent >= 8) {
    155 	buf[idx] = '\t';
    156 	idx++;
    157 	indent -= 8;
    158     }
    159 
    160     while (indent > 0) {
    161 	buf[idx] = ' ';
    162 	idx++;
    163 	indent--;
    164     }
    165 
    166     /*
    167      * Trailing zero.
    168      */
    169     buf[idx] = '\0';
    170 
    171     return buf;
    172 }
    173 
    174 /*
    175  * Print a single PDU.
    176  */
    177 static void
    178 rpki_rtr_pdu_print (const u_char *tptr, u_int indent)
    179 {
    180     const rpki_rtr_pdu *pdu_header;
    181     u_int pdu_type, pdu_len, hexdump;
    182     const u_char *msg;
    183 
    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;
    188 
    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);
    194 
    195     switch (pdu_type) {
    196 
    197 	/*
    198 	 * The following PDUs share the message format.
    199 	 */
    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("%sSession ID: 0x%04x, Serial: %u",
    205 	       indent_string(indent+2),
    206 	       EXTRACT_16BITS(pdu_header->u.session_id),
    207 	       EXTRACT_32BITS(msg));
    208 	break;
    209 
    210 	/*
    211 	 * The following PDUs share the message format.
    212 	 */
    213     case RPKI_RTR_RESET_QUERY_PDU:
    214     case RPKI_RTR_CACHE_RESET_PDU:
    215 
    216 	/*
    217 	 * Zero payload PDUs.
    218 	 */
    219 	break;
    220 
    221     case RPKI_RTR_CACHE_RESPONSE_PDU:
    222 	printf("%sSession ID: 0x%04x",
    223 	       indent_string(indent+2),
    224 	       EXTRACT_16BITS(pdu_header->u.session_id));
    225 	break;
    226 
    227     case RPKI_RTR_IPV4_PREFIX_PDU:
    228 	{
    229 	    rpki_rtr_pdu_ipv4_prefix *pdu;
    230 
    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);
    237 	}
    238 	break;
    239 
    240 #ifdef INET6
    241     case RPKI_RTR_IPV6_PREFIX_PDU:
    242 	{
    243 	    rpki_rtr_pdu_ipv6_prefix *pdu;
    244 
    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);
    251 	}
    252 	break;
    253 #endif
    254 
    255     case RPKI_RTR_ERROR_REPORT_PDU:
    256 	{
    257 	    rpki_rtr_pdu_error_report *pdu;
    258 	    u_int encapsulated_pdu_length, text_length, tlen, error_code;
    259 	    u_char buf[80];
    260 
    261 	    pdu = (rpki_rtr_pdu_error_report *)tptr;
    262 	    encapsulated_pdu_length = EXTRACT_32BITS(pdu->encapsulated_pdu_length);
    263 	    tlen = pdu_len;
    264 
    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);
    270 
    271 	    tptr += sizeof(*pdu);
    272 	    tlen -= sizeof(*pdu);
    273 
    274 	    /*
    275 	     * Recurse if there is an encapsulated PDU.
    276 	     */
    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);
    281 	    }
    282 
    283 	    tptr += encapsulated_pdu_length;
    284 	    tlen -= encapsulated_pdu_length;
    285 
    286 	    /*
    287 	     * Extract, trail-zero and print the Error message.
    288 	     */
    289 	    text_length = 0;
    290 	    if (tlen > 4) {
    291 		text_length = EXTRACT_32BITS(tptr);
    292 		tptr += 4;
    293 		tlen -= 4;
    294 	    }
    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);
    299 	    }
    300 	}
    301 	break;
    302 
    303     default:
    304 
    305 	/*
    306 	 * Unknown data, please hexdump.
    307 	 */
    308 	hexdump = TRUE;
    309     }
    310 
    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);
    314     }
    315 }
    316 
    317 void
    318 rpki_rtr_print(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 (!vflag) {
    328 	printf(", RPKI-RTR");
    329 	return;
    330     }
    331 
    332     while (tlen >= sizeof(rpki_rtr_pdu)) {
    333 
    334         TCHECK2(*tptr, sizeof(rpki_rtr_pdu));
    335 
    336 	pdu_header = (rpki_rtr_pdu *)tptr;
    337         pdu_type = pdu_header->pdu_type;
    338         pdu_len = EXTRACT_32BITS(pdu_header->length);
    339 
    340         /* infinite loop check */
    341         if (!pdu_type || !pdu_len) {
    342             break;
    343         }
    344 
    345         TCHECK2(*tptr, pdu_len);
    346         if (tlen < pdu_len) {
    347             goto trunc;
    348         }
    349 
    350 	/*
    351 	 * Print the PDU.
    352 	 */
    353 	rpki_rtr_pdu_print(tptr, 8);
    354 
    355         tlen -= pdu_len;
    356         tptr += pdu_len;
    357     }
    358     return;
    359  trunc:
    360     printf("\n\t[|RPKI-RTR]");
    361 }
    362 
    363 /*
    364  * Local Variables:
    365  * c-style: whitesmith
    366  * c-basic-offset: 4
    367  * End:
    368  */
    369