Bump the version to 0.9.10.
[obnox/wireshark/wip.git] / packet-mpls.c
1 /* packet-mpls.c
2  * Routines for MPLS data packet disassembly
3  *
4  * (c) Copyright Ashok Narayanan <ashokn@cisco.com>
5  *
6  * $Id: packet-mpls.c,v 1.29 2003/01/27 19:28:52 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*
28  * NOTES
29  *
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
33  */
34
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <glib.h>
41 #include <epan/packet.h>
42 #include "ppptypes.h"
43 #include "etypes.h"
44
45 static gint proto_mpls = -1;
46
47 static gint ett_mpls = -1;
48
49 /* Special labels in MPLS */
50 enum {
51     IP4_EXPLICIT_NULL = 0,
52     ROUTER_ALERT,
53     IP6_EXPLICIT_NULL,
54     IMPLICIT_NULL,
55
56     MAX_RESERVED = 15
57 };
58
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"},
64     {0, NULL }
65 };
66
67 /* MPLS filter values */
68 enum mpls_filter_keys {
69
70     /* Is the packet MPLS-encapsulated? */
71 /*    MPLSF_PACKET,*/
72
73     /* MPLS encap properties */
74     MPLSF_LABEL,
75     MPLSF_EXP,
76     MPLSF_BOTTOM_OF_STACK,
77     MPLSF_TTL,
78
79     MPLSF_MAX
80 };
81
82 static int mpls_filter[MPLSF_MAX];
83 static hf_register_info mplsf_info[] = {
84
85 /*    {&mpls_filter[MPLSF_PACKET],
86      {"MPLS Label Switched Packet", "mpls", FT_UINT8, BASE_DEC, NULL, 0x0,
87       "", HFILL }},*/
88
89     {&mpls_filter[MPLSF_LABEL],
90      {"MPLS Label", "mpls.label", FT_UINT32, BASE_DEC, VALS(special_labels), 0x0,
91       "", HFILL }},
92
93     {&mpls_filter[MPLSF_EXP],
94      {"MPLS Experimental Bits", "mpls.exp", FT_UINT8, BASE_DEC, NULL, 0x0,
95       "", HFILL }},
96
97     {&mpls_filter[MPLSF_BOTTOM_OF_STACK],
98      {"MPLS Bottom Of Label Stack", "mpls.bottom", FT_UINT8, BASE_DEC, NULL, 0x0,
99       "", HFILL }},
100
101     {&mpls_filter[MPLSF_TTL],
102      {"MPLS TTL", "mpls.ttl", FT_UINT8, BASE_DEC, NULL, 0x0,
103       "", HFILL }},
104 };
105
106 static dissector_handle_t ipv4_handle;
107 static dissector_handle_t ipv6_handle;
108 static dissector_handle_t eth_handle;
109
110 /*
111  * Given a 4-byte MPLS label starting at offset "offset", in tvbuff "tvb",
112  * decode it.
113  * Return the label in "label", EXP bits in "exp",
114  * bottom_of_stack in "bos", and TTL in "ttl"
115  */
116 void decode_mpls_label(tvbuff_t *tvb, int offset,
117                        guint32 *label, guint8 *exp,
118                        guint8 *bos, guint8 *ttl)
119 {
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);
123
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);
128 }
129
130 static void
131 dissect_mpls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
132 {
133     int offset = 0;
134     guint32 label;
135     guint8 exp;
136     guint8 bos;
137     guint8 ttl;
138     guint8 ipvers;
139
140     proto_tree  *mpls_tree;
141     proto_item  *ti;
142     tvbuff_t *next_tvb;
143
144     if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
145         col_set_str(pinfo->cinfo,COL_PROTOCOL, "MPLS");
146     }
147
148     if (check_col(pinfo->cinfo,COL_INFO)) {
149         col_add_fstr(pinfo->cinfo,COL_INFO,"MPLS Label Switched Packet");
150     }
151
152     /* Start Decoding Here. */
153     while (tvb_reported_length_remaining(tvb, offset) > 0) {
154         decode_mpls_label(tvb, offset, &label, &exp, &bos, &ttl);
155
156         if (tree) {
157
158             ti = proto_tree_add_item(tree, proto_mpls, tvb, offset, 4, FALSE);
159             mpls_tree = proto_item_add_subtree(ti, ett_mpls);
160
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"));
166             else
167                 proto_tree_add_uint(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
168                                     offset, 3, label);
169
170             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_EXP], tvb,
171                                 offset+2,1, exp);
172             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_BOTTOM_OF_STACK], tvb,
173                                 offset+2,1, bos);
174             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_TTL], tvb,
175                                 offset+3,1, ttl);
176         }
177         offset += 4;
178         if (bos) break;
179     }
180     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
181
182     ipvers = (tvb_get_guint8(tvb, offset) >> 4) & 0x0F;
183     if (ipvers == 6) {
184       call_dissector(ipv6_handle, next_tvb, pinfo, tree);
185     } else if (ipvers == 4) {
186       call_dissector(ipv4_handle, next_tvb, pinfo, tree);
187     } else {
188       call_dissector(eth_handle, next_tvb, pinfo, tree);
189     }
190 }
191
192 void
193 proto_register_mpls(void)
194 {
195         static gint *ett[] = {
196                 &ett_mpls,
197         };
198
199         proto_mpls = proto_register_protocol("MultiProtocol Label Switching Header",
200             "MPLS", "mpls");
201         proto_register_field_array(proto_mpls, mplsf_info, array_length(mplsf_info));
202         proto_register_subtree_array(ett, array_length(ett));
203 }
204
205 void
206 proto_reg_handoff_mpls(void)
207 {
208         dissector_handle_t mpls_handle;
209
210         /*
211          * Get a handle for the IPv4 and IPv6 dissectors.
212          */
213         ipv4_handle = find_dissector("ip");
214         ipv6_handle = find_dissector("ipv6");
215         eth_handle = find_dissector("eth");
216
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);
220         dissector_add("chdlctype", ETHERTYPE_MPLS, mpls_handle);
221         dissector_add("chdlctype", ETHERTYPE_MPLS_MULTI, mpls_handle);
222 }