xref: /dflybsd-src/contrib/tcpdump/print-mpls.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (C) 2001 WIDE Project.  All rights reserved.
341c99275SPeter Avalos  *
441c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
541c99275SPeter Avalos  * modification, are permitted provided that the following conditions
641c99275SPeter Avalos  * are met:
741c99275SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
841c99275SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
941c99275SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1041c99275SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1141c99275SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1241c99275SPeter Avalos  * 3. Neither the name of the project nor the names of its contributors
1341c99275SPeter Avalos  *    may be used to endorse or promote products derived from this software
1441c99275SPeter Avalos  *    without specific prior written permission.
1541c99275SPeter Avalos  *
1641c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1741c99275SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1841c99275SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1941c99275SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2041c99275SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2141c99275SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2241c99275SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2341c99275SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2441c99275SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2541c99275SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2641c99275SPeter Avalos  * SUCH DAMAGE.
2741c99275SPeter Avalos  */
2841c99275SPeter Avalos 
29411677aeSAaron LI /* \summary: Multi-Protocol Label Switching (MPLS) printer */
3041c99275SPeter Avalos 
3141c99275SPeter Avalos #ifdef HAVE_CONFIG_H
32*ed775ee7SAntonio Huete Jimenez #include <config.h>
3341c99275SPeter Avalos #endif
3441c99275SPeter Avalos 
35*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
3641c99275SPeter Avalos 
37*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
38411677aeSAaron LI #include "netdissect.h"
39411677aeSAaron LI #include "extract.h"
4041c99275SPeter Avalos #include "mpls.h"
4141c99275SPeter Avalos 
4241c99275SPeter Avalos static const char *mpls_labelname[] = {
4341c99275SPeter Avalos /*0*/	"IPv4 explicit NULL", "router alert", "IPv6 explicit NULL",
4441c99275SPeter Avalos 	"implicit NULL", "rsvd",
4541c99275SPeter Avalos /*5*/	"rsvd", "rsvd", "rsvd", "rsvd", "rsvd",
4641c99275SPeter Avalos /*10*/	"rsvd", "rsvd", "rsvd", "rsvd", "rsvd",
4741c99275SPeter Avalos /*15*/	"rsvd",
4841c99275SPeter Avalos };
4941c99275SPeter Avalos 
5027bfbee1SPeter Avalos enum mpls_packet_type {
5127bfbee1SPeter Avalos 	PT_UNKNOWN,
5227bfbee1SPeter Avalos 	PT_IPV4,
5327bfbee1SPeter Avalos 	PT_IPV6,
5427bfbee1SPeter Avalos 	PT_OSI
5527bfbee1SPeter Avalos };
5627bfbee1SPeter Avalos 
5741c99275SPeter Avalos /*
5841c99275SPeter Avalos  * RFC3032: MPLS label stack encoding
5941c99275SPeter Avalos  */
6041c99275SPeter Avalos void
mpls_print(netdissect_options * ndo,const u_char * bp,u_int length)61411677aeSAaron LI mpls_print(netdissect_options *ndo, const u_char *bp, u_int length)
6241c99275SPeter Avalos {
6341c99275SPeter Avalos 	const u_char *p;
64411677aeSAaron LI 	uint32_t label_entry;
65411677aeSAaron LI 	uint16_t label_stack_depth = 0;
66*ed775ee7SAntonio Huete Jimenez 	uint8_t first;
6727bfbee1SPeter Avalos 	enum mpls_packet_type pt = PT_UNKNOWN;
6841c99275SPeter Avalos 
69*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "mpls";
7041c99275SPeter Avalos 	p = bp;
71*ed775ee7SAntonio Huete Jimenez 	nd_print_protocol_caps(ndo);
7241c99275SPeter Avalos 	do {
73*ed775ee7SAntonio Huete Jimenez 		if (length < sizeof(label_entry))
74*ed775ee7SAntonio Huete Jimenez 			goto invalid;
75*ed775ee7SAntonio Huete Jimenez 		label_entry = GET_BE_U_4(p);
76*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s(label %u",
77411677aeSAaron LI 		       (label_stack_depth && ndo->ndo_vflag) ? "\n\t" : " ",
78*ed775ee7SAntonio Huete Jimenez        		       MPLS_LABEL(label_entry));
7941c99275SPeter Avalos 		label_stack_depth++;
80411677aeSAaron LI 		if (ndo->ndo_vflag &&
8141c99275SPeter Avalos 		    MPLS_LABEL(label_entry) < sizeof(mpls_labelname) / sizeof(mpls_labelname[0]))
82*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(" (%s)", mpls_labelname[MPLS_LABEL(label_entry)]);
83*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", exp %u", MPLS_EXP(label_entry));
8441c99275SPeter Avalos 		if (MPLS_STACK(label_entry))
85*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(", [S]");
86*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", ttl %u)", MPLS_TTL(label_entry));
8741c99275SPeter Avalos 
8841c99275SPeter Avalos 		p += sizeof(label_entry);
89411677aeSAaron LI 		length -= sizeof(label_entry);
9041c99275SPeter Avalos 	} while (!MPLS_STACK(label_entry));
9141c99275SPeter Avalos 
9227bfbee1SPeter Avalos 	/*
9327bfbee1SPeter Avalos 	 * Try to figure out the packet type.
9427bfbee1SPeter Avalos 	 */
9541c99275SPeter Avalos 	switch (MPLS_LABEL(label_entry)) {
9627bfbee1SPeter Avalos 
9741c99275SPeter Avalos 	case 0:	/* IPv4 explicit NULL label */
9841c99275SPeter Avalos 	case 3:	/* IPv4 implicit NULL label */
9927bfbee1SPeter Avalos 		pt = PT_IPV4;
10041c99275SPeter Avalos 		break;
10127bfbee1SPeter Avalos 
10241c99275SPeter Avalos 	case 2:	/* IPv6 explicit NULL label */
10327bfbee1SPeter Avalos 		pt = PT_IPV6;
10441c99275SPeter Avalos 		break;
10527bfbee1SPeter Avalos 
10641c99275SPeter Avalos 	default:
10741c99275SPeter Avalos 		/*
10841c99275SPeter Avalos 		 * Generally there's no indication of protocol in MPLS label
10927bfbee1SPeter Avalos 		 * encoding.
11027bfbee1SPeter Avalos 		 *
11127bfbee1SPeter Avalos 		 * However, draft-hsmit-isis-aal5mux-00.txt describes a
11227bfbee1SPeter Avalos 		 * technique for encapsulating IS-IS and IP traffic on the
11327bfbee1SPeter Avalos 		 * same ATM virtual circuit; you look at the first payload
11427bfbee1SPeter Avalos 		 * byte to determine the network layer protocol, based on
11527bfbee1SPeter Avalos 		 * the fact that
11627bfbee1SPeter Avalos 		 *
11727bfbee1SPeter Avalos 		 *	1) the first byte of an IP header is 0x45-0x4f
11827bfbee1SPeter Avalos 		 *	   for IPv4 and 0x60-0x6f for IPv6;
11927bfbee1SPeter Avalos 		 *
12027bfbee1SPeter Avalos 		 *	2) the first byte of an OSI CLNP packet is 0x81,
12127bfbee1SPeter Avalos 		 *	   the first byte of an OSI ES-IS packet is 0x82,
12227bfbee1SPeter Avalos 		 *	   and the first byte of an OSI IS-IS packet is
12327bfbee1SPeter Avalos 		 *	   0x83;
12427bfbee1SPeter Avalos 		 *
12527bfbee1SPeter Avalos 		 * so the network layer protocol can be inferred from the
12627bfbee1SPeter Avalos 		 * first byte of the packet, if the protocol is one of the
12727bfbee1SPeter Avalos 		 * ones listed above.
12827bfbee1SPeter Avalos 		 *
12927bfbee1SPeter Avalos 		 * Cisco sends control-plane traffic MPLS-encapsulated in
13027bfbee1SPeter Avalos 		 * this fashion.
13141c99275SPeter Avalos 		 */
132411677aeSAaron LI 		if (length < 1) {
133411677aeSAaron LI 			/* nothing to print */
134411677aeSAaron LI 			return;
135411677aeSAaron LI 		}
136*ed775ee7SAntonio Huete Jimenez 		first = GET_U_1(p);
137*ed775ee7SAntonio Huete Jimenez 		pt =
138*ed775ee7SAntonio Huete Jimenez 			(first >= 0x45 && first <= 0x4f) ? PT_IPV4 :
139*ed775ee7SAntonio Huete Jimenez 			(first >= 0x60 && first <= 0x6f) ? PT_IPV6 :
140*ed775ee7SAntonio Huete Jimenez 			(first >= 0x81 && first <= 0x83) ? PT_OSI :
14141c99275SPeter Avalos 			/* ok bail out - we did not figure out what it is*/
142*ed775ee7SAntonio Huete Jimenez 			PT_UNKNOWN;
14341c99275SPeter Avalos 	}
14427bfbee1SPeter Avalos 
14527bfbee1SPeter Avalos 	/*
14627bfbee1SPeter Avalos 	 * Print the payload.
14727bfbee1SPeter Avalos 	 */
148*ed775ee7SAntonio Huete Jimenez 	switch (pt) {
149*ed775ee7SAntonio Huete Jimenez 	case PT_UNKNOWN:
150411677aeSAaron LI 		if (!ndo->ndo_suppress_default_print)
151411677aeSAaron LI 			ND_DEFAULTPRINT(p, length);
152*ed775ee7SAntonio Huete Jimenez 		break;
15327bfbee1SPeter Avalos 
15427bfbee1SPeter Avalos 	case PT_IPV4:
155*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
156411677aeSAaron LI 		ip_print(ndo, p, length);
15727bfbee1SPeter Avalos 		break;
15827bfbee1SPeter Avalos 
15927bfbee1SPeter Avalos 	case PT_IPV6:
160*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
161411677aeSAaron LI 		ip6_print(ndo, p, length);
16227bfbee1SPeter Avalos 		break;
16327bfbee1SPeter Avalos 
16427bfbee1SPeter Avalos 	case PT_OSI:
165*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
166411677aeSAaron LI 		isoclns_print(ndo, p, length);
16727bfbee1SPeter Avalos 		break;
16827bfbee1SPeter Avalos 	}
16927bfbee1SPeter Avalos 	return;
17041c99275SPeter Avalos 
171*ed775ee7SAntonio Huete Jimenez invalid:
172*ed775ee7SAntonio Huete Jimenez 	nd_print_invalid(ndo);
173*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, length);
17441c99275SPeter Avalos }
175