2 * Routines for MPLS data packet disassembly
4 * (c) Copyright Ashok Narayanan <ashokn@cisco.com>
6 * $Id: packet-mpls.c,v 1.28 2002/08/28 21:00:22 jmayer Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * This module defines routines to handle Ethernet-encapsulated MPLS IP packets.
31 * It should implement all the functionality in <draft-ietf-mpls-label-encaps-07.txt>
32 * Multicast MPLS support is not tested yet
41 #include <epan/packet.h>
45 static gint proto_mpls = -1;
47 static gint ett_mpls = -1;
49 /* Special labels in MPLS */
51 IP4_EXPLICIT_NULL = 0,
59 static const value_string special_labels[] = {
60 {IP4_EXPLICIT_NULL, "IPv4 Explicit-Null"},
61 {ROUTER_ALERT, "Router Alert"},
62 {IP6_EXPLICIT_NULL, "IPv6 Explicit-Null"},
63 {IMPLICIT_NULL, "Implicit-Null"},
67 /* MPLS filter values */
68 enum mpls_filter_keys {
70 /* Is the packet MPLS-encapsulated? */
73 /* MPLS encap properties */
76 MPLSF_BOTTOM_OF_STACK,
82 static int mpls_filter[MPLSF_MAX];
83 static hf_register_info mplsf_info[] = {
85 /* {&mpls_filter[MPLSF_PACKET],
86 {"MPLS Label Switched Packet", "mpls", FT_UINT8, BASE_DEC, NULL, 0x0,
89 {&mpls_filter[MPLSF_LABEL],
90 {"MPLS Label", "mpls.label", FT_UINT32, BASE_DEC, VALS(special_labels), 0x0,
93 {&mpls_filter[MPLSF_EXP],
94 {"MPLS Experimental Bits", "mpls.exp", FT_UINT8, BASE_DEC, NULL, 0x0,
97 {&mpls_filter[MPLSF_BOTTOM_OF_STACK],
98 {"MPLS Bottom Of Label Stack", "mpls.bottom", FT_UINT8, BASE_DEC, NULL, 0x0,
101 {&mpls_filter[MPLSF_TTL],
102 {"MPLS TTL", "mpls.ttl", FT_UINT8, BASE_DEC, NULL, 0x0,
106 static dissector_handle_t ipv4_handle;
107 static dissector_handle_t ipv6_handle;
108 static dissector_handle_t eth_handle;
111 * Given a 4-byte MPLS label starting at offset "offset", in tvbuff "tvb",
113 * Return the label in "label", EXP bits in "exp",
114 * bottom_of_stack in "bos", and TTL in "ttl"
116 void decode_mpls_label(tvbuff_t *tvb, int offset,
117 guint32 *label, guint8 *exp,
118 guint8 *bos, guint8 *ttl)
120 guint8 octet0 = tvb_get_guint8(tvb, offset+0);
121 guint8 octet1 = tvb_get_guint8(tvb, offset+1);
122 guint8 octet2 = tvb_get_guint8(tvb, offset+2);
124 *label = (octet0 << 12) + (octet1 << 4) + ((octet2 >> 4) & 0xff);
125 *exp = (octet2 >> 1) & 0x7;
126 *bos = (octet2 & 0x1);
127 *ttl = tvb_get_guint8(tvb, offset+3);
131 dissect_mpls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
140 proto_tree *mpls_tree;
144 if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
145 col_set_str(pinfo->cinfo,COL_PROTOCOL, "MPLS");
148 if (check_col(pinfo->cinfo,COL_INFO)) {
149 col_add_fstr(pinfo->cinfo,COL_INFO,"MPLS Label Switched Packet");
152 /* Start Decoding Here. */
153 while (tvb_reported_length_remaining(tvb, offset) > 0) {
154 decode_mpls_label(tvb, offset, &label, &exp, &bos, &ttl);
158 ti = proto_tree_add_item(tree, proto_mpls, tvb, offset, 4, FALSE);
159 mpls_tree = proto_item_add_subtree(ti, ett_mpls);
161 if (label <= MAX_RESERVED)
162 proto_tree_add_uint_format(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
163 offset, 3, label, "Label: %u (%s)",
164 label, val_to_str(label, special_labels,
165 "Reserved - Unknown"));
167 proto_tree_add_uint(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
170 proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_EXP], tvb,
172 proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_BOTTOM_OF_STACK], tvb,
174 proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_TTL], tvb,
180 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
182 ipvers = (tvb_get_guint8(tvb, offset) >> 4) & 0x0F;
184 call_dissector(ipv6_handle, next_tvb, pinfo, tree);
185 } else if (ipvers == 4) {
186 call_dissector(ipv4_handle, next_tvb, pinfo, tree);
188 call_dissector(eth_handle, next_tvb, pinfo, tree);
193 proto_register_mpls(void)
195 static gint *ett[] = {
199 proto_mpls = proto_register_protocol("MultiProtocol Label Switching Header",
201 proto_register_field_array(proto_mpls, mplsf_info, array_length(mplsf_info));
202 proto_register_subtree_array(ett, array_length(ett));
206 proto_reg_handoff_mpls(void)
208 dissector_handle_t mpls_handle;
211 * Get a handle for the IPv4 and IPv6 dissectors.
213 ipv4_handle = find_dissector("ip");
214 ipv6_handle = find_dissector("ipv6");
215 eth_handle = find_dissector("eth");
217 mpls_handle = create_dissector_handle(dissect_mpls, proto_mpls);
218 dissector_add("ethertype", ETHERTYPE_MPLS, mpls_handle);
219 dissector_add("ppp.protocol", PPP_MPLS_UNI, mpls_handle);