xref: /freebsd/contrib/tcpdump/print-mpls.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1a90e161bSBill Fenner /*
2a90e161bSBill Fenner  * Copyright (C) 2001 WIDE Project.  All rights reserved.
3a90e161bSBill Fenner  *
4a90e161bSBill Fenner  * Redistribution and use in source and binary forms, with or without
5a90e161bSBill Fenner  * modification, are permitted provided that the following conditions
6a90e161bSBill Fenner  * are met:
7a90e161bSBill Fenner  * 1. Redistributions of source code must retain the above copyright
8a90e161bSBill Fenner  *    notice, this list of conditions and the following disclaimer.
9a90e161bSBill Fenner  * 2. Redistributions in binary form must reproduce the above copyright
10a90e161bSBill Fenner  *    notice, this list of conditions and the following disclaimer in the
11a90e161bSBill Fenner  *    documentation and/or other materials provided with the distribution.
12a90e161bSBill Fenner  * 3. Neither the name of the project nor the names of its contributors
13a90e161bSBill Fenner  *    may be used to endorse or promote products derived from this software
14a90e161bSBill Fenner  *    without specific prior written permission.
15a90e161bSBill Fenner  *
16a90e161bSBill Fenner  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17a90e161bSBill Fenner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a90e161bSBill Fenner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a90e161bSBill Fenner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20a90e161bSBill Fenner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a90e161bSBill Fenner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a90e161bSBill Fenner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a90e161bSBill Fenner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a90e161bSBill Fenner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a90e161bSBill Fenner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a90e161bSBill Fenner  * SUCH DAMAGE.
27a90e161bSBill Fenner  */
28a90e161bSBill Fenner 
293340d773SGleb Smirnoff /* \summary: Multi-Protocol Label Switching (MPLS) printer */
303340d773SGleb Smirnoff 
31*ee67461eSJoseph Mingrone #include <config.h>
32a90e161bSBill Fenner 
33*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
34a90e161bSBill Fenner 
35*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
363340d773SGleb Smirnoff #include "netdissect.h"
373340d773SGleb Smirnoff #include "extract.h"
381de50e9fSSam Leffler #include "mpls.h"
39a90e161bSBill Fenner 
40a90e161bSBill Fenner static const char *mpls_labelname[] = {
41a90e161bSBill Fenner /*0*/	"IPv4 explicit NULL", "router alert", "IPv6 explicit NULL",
42a90e161bSBill Fenner 	"implicit NULL", "rsvd",
43a90e161bSBill Fenner /*5*/	"rsvd", "rsvd", "rsvd", "rsvd", "rsvd",
44a90e161bSBill Fenner /*10*/	"rsvd", "rsvd", "rsvd", "rsvd", "rsvd",
45a90e161bSBill Fenner /*15*/	"rsvd",
46a90e161bSBill Fenner };
47a90e161bSBill Fenner 
48cac3dcd5SXin LI enum mpls_packet_type {
49cac3dcd5SXin LI 	PT_UNKNOWN,
50cac3dcd5SXin LI 	PT_IPV4,
51cac3dcd5SXin LI 	PT_IPV6,
52cac3dcd5SXin LI 	PT_OSI
53cac3dcd5SXin LI };
54cac3dcd5SXin LI 
55a90e161bSBill Fenner /*
56a90e161bSBill Fenner  * RFC3032: MPLS label stack encoding
57a90e161bSBill Fenner  */
58a90e161bSBill Fenner void
mpls_print(netdissect_options * ndo,const u_char * bp,u_int length)593c602fabSXin LI mpls_print(netdissect_options *ndo, const u_char *bp, u_int length)
60a90e161bSBill Fenner {
61a90e161bSBill Fenner 	const u_char *p;
623c602fabSXin LI 	uint32_t label_entry;
633c602fabSXin LI 	uint16_t label_stack_depth = 0;
64*ee67461eSJoseph Mingrone 	uint8_t first;
65cac3dcd5SXin LI 	enum mpls_packet_type pt = PT_UNKNOWN;
66a90e161bSBill Fenner 
67*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "mpls";
68a90e161bSBill Fenner 	p = bp;
69*ee67461eSJoseph Mingrone 	nd_print_protocol_caps(ndo);
709afd0c29SBill Fenner 	do {
71*ee67461eSJoseph Mingrone 		if (length < sizeof(label_entry))
72*ee67461eSJoseph Mingrone 			goto invalid;
73*ee67461eSJoseph Mingrone 		label_entry = GET_BE_U_4(p);
74*ee67461eSJoseph Mingrone 		ND_PRINT("%s(label %u",
753c602fabSXin LI 		       (label_stack_depth && ndo->ndo_vflag) ? "\n\t" : " ",
76*ee67461eSJoseph Mingrone 			MPLS_LABEL(label_entry));
77f4d0c64aSSam Leffler 		label_stack_depth++;
783c602fabSXin LI 		if (ndo->ndo_vflag &&
79f4d0c64aSSam Leffler 		    MPLS_LABEL(label_entry) < sizeof(mpls_labelname) / sizeof(mpls_labelname[0]))
80*ee67461eSJoseph Mingrone 			ND_PRINT(" (%s)", mpls_labelname[MPLS_LABEL(label_entry)]);
81*ee67461eSJoseph Mingrone 		ND_PRINT(", tc %u", MPLS_TC(label_entry));
82f4d0c64aSSam Leffler 		if (MPLS_STACK(label_entry))
83*ee67461eSJoseph Mingrone 			ND_PRINT(", [S]");
84*ee67461eSJoseph Mingrone 		ND_PRINT(", ttl %u)", MPLS_TTL(label_entry));
85a90e161bSBill Fenner 
86f4d0c64aSSam Leffler 		p += sizeof(label_entry);
873340d773SGleb Smirnoff 		length -= sizeof(label_entry);
88f4d0c64aSSam Leffler 	} while (!MPLS_STACK(label_entry));
89a90e161bSBill Fenner 
90cac3dcd5SXin LI 	/*
91cac3dcd5SXin LI 	 * Try to figure out the packet type.
92cac3dcd5SXin LI 	 */
93f4d0c64aSSam Leffler 	switch (MPLS_LABEL(label_entry)) {
94cac3dcd5SXin LI 
95a90e161bSBill Fenner 	case 0:	/* IPv4 explicit NULL label */
965b0fe478SBruce M Simpson 	case 3:	/* IPv4 implicit NULL label */
97cac3dcd5SXin LI 		pt = PT_IPV4;
98a90e161bSBill Fenner 		break;
99cac3dcd5SXin LI 
100a90e161bSBill Fenner 	case 2:	/* IPv6 explicit NULL label */
101cac3dcd5SXin LI 		pt = PT_IPV6;
102a90e161bSBill Fenner 		break;
103cac3dcd5SXin LI 
104a90e161bSBill Fenner 	default:
105a90e161bSBill Fenner 		/*
1065b0fe478SBruce M Simpson 		 * Generally there's no indication of protocol in MPLS label
107cac3dcd5SXin LI 		 * encoding.
108cac3dcd5SXin LI 		 *
109cac3dcd5SXin LI 		 * However, draft-hsmit-isis-aal5mux-00.txt describes a
110cac3dcd5SXin LI 		 * technique for encapsulating IS-IS and IP traffic on the
111cac3dcd5SXin LI 		 * same ATM virtual circuit; you look at the first payload
112cac3dcd5SXin LI 		 * byte to determine the network layer protocol, based on
113cac3dcd5SXin LI 		 * the fact that
114cac3dcd5SXin LI 		 *
115cac3dcd5SXin LI 		 *	1) the first byte of an IP header is 0x45-0x4f
116cac3dcd5SXin LI 		 *	   for IPv4 and 0x60-0x6f for IPv6;
117cac3dcd5SXin LI 		 *
118cac3dcd5SXin LI 		 *	2) the first byte of an OSI CLNP packet is 0x81,
119cac3dcd5SXin LI 		 *	   the first byte of an OSI ES-IS packet is 0x82,
120cac3dcd5SXin LI 		 *	   and the first byte of an OSI IS-IS packet is
121cac3dcd5SXin LI 		 *	   0x83;
122cac3dcd5SXin LI 		 *
123cac3dcd5SXin LI 		 * so the network layer protocol can be inferred from the
124cac3dcd5SXin LI 		 * first byte of the packet, if the protocol is one of the
125cac3dcd5SXin LI 		 * ones listed above.
126cac3dcd5SXin LI 		 *
127cac3dcd5SXin LI 		 * Cisco sends control-plane traffic MPLS-encapsulated in
128cac3dcd5SXin LI 		 * this fashion.
129a90e161bSBill Fenner 		 */
1303340d773SGleb Smirnoff 		if (length < 1) {
1313340d773SGleb Smirnoff 			/* nothing to print */
1323340d773SGleb Smirnoff 			return;
1333340d773SGleb Smirnoff 		}
134*ee67461eSJoseph Mingrone 		first = GET_U_1(p);
135*ee67461eSJoseph Mingrone 		pt =
136*ee67461eSJoseph Mingrone 			(first >= 0x45 && first <= 0x4f) ? PT_IPV4 :
137*ee67461eSJoseph Mingrone 			(first >= 0x60 && first <= 0x6f) ? PT_IPV6 :
138*ee67461eSJoseph Mingrone 			(first >= 0x81 && first <= 0x83) ? PT_OSI :
1395b0fe478SBruce M Simpson 			/* ok bail out - we did not figure out what it is*/
140*ee67461eSJoseph Mingrone 			PT_UNKNOWN;
1415b0fe478SBruce M Simpson 	}
142cac3dcd5SXin LI 
143cac3dcd5SXin LI 	/*
144cac3dcd5SXin LI 	 * Print the payload.
145cac3dcd5SXin LI 	 */
146*ee67461eSJoseph Mingrone 	switch (pt) {
147*ee67461eSJoseph Mingrone 	case PT_UNKNOWN:
1483c602fabSXin LI 		if (!ndo->ndo_suppress_default_print)
1493340d773SGleb Smirnoff 			ND_DEFAULTPRINT(p, length);
150*ee67461eSJoseph Mingrone 		break;
151cac3dcd5SXin LI 
152cac3dcd5SXin LI 	case PT_IPV4:
153*ee67461eSJoseph Mingrone 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
1543340d773SGleb Smirnoff 		ip_print(ndo, p, length);
155cac3dcd5SXin LI 		break;
156cac3dcd5SXin LI 
157cac3dcd5SXin LI 	case PT_IPV6:
158*ee67461eSJoseph Mingrone 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
1593340d773SGleb Smirnoff 		ip6_print(ndo, p, length);
160cac3dcd5SXin LI 		break;
161cac3dcd5SXin LI 
162cac3dcd5SXin LI 	case PT_OSI:
163*ee67461eSJoseph Mingrone 		ND_PRINT(ndo->ndo_vflag ? "\n\t" : " ");
1640bff6a5aSEd Maste 		isoclns_print(ndo, p, length);
165cac3dcd5SXin LI 		break;
166cac3dcd5SXin LI 	}
167cac3dcd5SXin LI 	return;
168a90e161bSBill Fenner 
169*ee67461eSJoseph Mingrone invalid:
170*ee67461eSJoseph Mingrone 	nd_print_invalid(ndo);
171*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, length);
172a90e161bSBill Fenner }
173