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