Show the "negotiable/non-negotiable" flags as such.
[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.26 2002/03/18 18:56:53 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 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #include <glib.h>
45 #include <epan/packet.h>
46 #include "ppptypes.h"
47 #include "etypes.h"
48
49 static gint proto_mpls = -1;
50
51 static gint ett_mpls = -1;
52
53 /* Special labels in MPLS */
54 enum {
55     IP4_EXPLICIT_NULL = 0,
56     ROUTER_ALERT,
57     IP6_EXPLICIT_NULL,
58     IMPLICIT_NULL,
59
60     MAX_RESERVED = 15
61 };
62
63 static const value_string special_labels[] = {
64     {IP4_EXPLICIT_NULL, "IPv4 Explicit-Null"},
65     {ROUTER_ALERT, "Router Alert"},
66     {IP6_EXPLICIT_NULL, "IPv6 Explicit-Null"},
67     {IMPLICIT_NULL, "Implicit-Null"},
68     {0, NULL }
69 };
70
71 /* MPLS filter values */
72 enum mpls_filter_keys {
73
74     /* Is the packet MPLS-encapsulated? */
75 /*    MPLSF_PACKET,*/
76
77     /* MPLS encap properties */
78     MPLSF_LABEL,
79     MPLSF_EXP,
80     MPLSF_BOTTOM_OF_STACK,
81     MPLSF_TTL,
82
83     MPLSF_MAX
84 };
85
86 static int mpls_filter[MPLSF_MAX];
87 static hf_register_info mplsf_info[] = {
88
89 /*    {&mpls_filter[MPLSF_PACKET], 
90      {"MPLS Label Switched Packet", "mpls", FT_UINT8, BASE_DEC, NULL, 0x0, 
91       "", HFILL }},*/
92
93     {&mpls_filter[MPLSF_LABEL], 
94      {"MPLS Label", "mpls.label", FT_UINT32, BASE_DEC, VALS(special_labels), 0x0, 
95       "", HFILL }},
96
97     {&mpls_filter[MPLSF_EXP], 
98      {"MPLS Experimental Bits", "mpls.exp", FT_UINT8, BASE_DEC, NULL, 0x0, 
99       "", HFILL }},
100
101     {&mpls_filter[MPLSF_BOTTOM_OF_STACK], 
102      {"MPLS Bottom Of Label Stack", "mpls.bottom", FT_UINT8, BASE_DEC, NULL, 0x0, 
103       "", HFILL }},
104
105     {&mpls_filter[MPLSF_TTL], 
106      {"MPLS TTL", "mpls.ttl", FT_UINT8, BASE_DEC, NULL, 0x0, 
107       "", HFILL }},
108 };
109
110 static dissector_handle_t ipv4_handle;
111 static dissector_handle_t ipv6_handle;
112 static dissector_handle_t eth_handle;
113
114 /*
115  * Given a 4-byte MPLS label starting at offset "offset", in tvbuff "tvb",
116  * decode it.
117  * Return the label in "label", EXP bits in "exp",
118  * bottom_of_stack in "bos", and TTL in "ttl"
119  */
120 void decode_mpls_label(tvbuff_t *tvb, int offset,
121                        guint32 *label, guint8 *exp,
122                        guint8 *bos, guint8 *ttl)
123 {
124     guint8 octet0 = tvb_get_guint8(tvb, offset+0);
125     guint8 octet1 = tvb_get_guint8(tvb, offset+1);
126     guint8 octet2 = tvb_get_guint8(tvb, offset+2);
127
128     *label = (octet0 << 12) + (octet1 << 4) + ((octet2 >> 4) & 0xff);
129     *exp = (octet2 >> 1) & 0x7;
130     *bos = (octet2 & 0x1);
131     *ttl = tvb_get_guint8(tvb, offset+3);
132 }
133
134 static void
135 dissect_mpls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
136 {
137     int offset = 0;
138     guint32 label;
139     guint8 exp;
140     guint8 bos;
141     guint8 ttl;
142     guint8 ipvers;
143
144     proto_tree  *mpls_tree;
145     proto_item  *ti;
146     tvbuff_t *next_tvb;
147
148     if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
149         col_set_str(pinfo->cinfo,COL_PROTOCOL, "MPLS");
150     }
151     
152     if (check_col(pinfo->cinfo,COL_INFO)) {
153         col_add_fstr(pinfo->cinfo,COL_INFO,"MPLS Label Switched Packet");
154     }
155
156     /* Start Decoding Here. */
157     while (tvb_reported_length_remaining(tvb, offset) > 0) {
158         decode_mpls_label(tvb, offset, &label, &exp, &bos, &ttl);
159
160         if (tree) {
161
162             ti = proto_tree_add_item(tree, proto_mpls, tvb, offset, 4, FALSE);
163             mpls_tree = proto_item_add_subtree(ti, ett_mpls);
164
165             if (label <= MAX_RESERVED)
166                 proto_tree_add_uint_format(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
167                                     offset, 3, label, "Label: %u (%s)", 
168                                     label, val_to_str(label, special_labels, 
169                                                       "Reserved - Unknown"));
170             else
171                 proto_tree_add_uint(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
172                                     offset, 3, label);
173
174             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_EXP], tvb, 
175                                 offset+2,1, exp);
176             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_BOTTOM_OF_STACK], tvb, 
177                                 offset+2,1, bos);
178             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_TTL], tvb, 
179                                 offset+3,1, ttl);
180         }
181         offset += 4;
182         if (bos) break;
183     }
184     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
185
186     ipvers = (tvb_get_guint8(tvb, offset) >> 4) & 0x0F;
187     if (ipvers == 6) {
188       call_dissector(ipv6_handle, next_tvb, pinfo, tree);
189     } else if (ipvers == 4) {
190       call_dissector(ipv4_handle, next_tvb, pinfo, tree);
191     } else {
192       call_dissector(eth_handle, next_tvb, pinfo, tree);
193     }
194 }
195
196 void
197 proto_register_mpls(void)
198 {
199         static gint *ett[] = {
200                 &ett_mpls,
201         };
202
203         proto_mpls = proto_register_protocol("MultiProtocol Label Switching Header",
204             "MPLS", "mpls");
205         proto_register_field_array(proto_mpls, mplsf_info, array_length(mplsf_info));
206         proto_register_subtree_array(ett, array_length(ett));
207 }
208
209 void
210 proto_reg_handoff_mpls(void)
211 {
212         dissector_handle_t mpls_handle;
213
214         /*
215          * Get a handle for the IPv4 and IPv6 dissectors.
216          */
217         ipv4_handle = find_dissector("ip");
218         ipv6_handle = find_dissector("ipv6");
219         eth_handle = find_dissector("eth");
220
221         mpls_handle = create_dissector_handle(dissect_mpls, proto_mpls);
222         dissector_add("ethertype", ETHERTYPE_MPLS, mpls_handle);
223         dissector_add("ppp.protocol", PPP_MPLS_UNI, mpls_handle);
224 }