Get rid of unnecessary include.
[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.19 2001/04/19 23:02:44 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 /*
29  * NOTES
30  *
31  * This module defines routines to handle Ethernet-encapsulated MPLS IP packets.
32  * It should implement all the functionality in <draft-ietf-mpls-label-encaps-07.txt>
33  * Multicast MPLS support is not tested yet
34  */
35
36
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44
45 #include <glib.h>
46 #include "packet.h"
47 #include "packet-ip.h"
48 #include "ppptypes.h"
49 #include "etypes.h"
50
51 static gint proto_mpls = -1;
52
53 static gint ett_mpls = -1;
54
55 /* Special labels in MPLS */
56 enum {
57     IP4_EXPLICIT_NULL = 0,
58     ROUTER_ALERT,
59     IP6_EXPLICIT_NULL,
60     IMPLICIT_NULL,
61
62     MAX_RESERVED = 15
63 };
64
65 static const value_string special_labels[] = {
66     {IP4_EXPLICIT_NULL, "IPv4 Explicit-Null"},
67     {ROUTER_ALERT, "Router Alert"},
68     {IP6_EXPLICIT_NULL, "IPv6 Explicit-Null"},
69     {IMPLICIT_NULL, "Implicit-Null"},
70     {0, NULL }
71 };
72
73 /* MPLS filter values */
74 enum mpls_filter_keys {
75
76     /* Is the packet MPLS-encapsulated? */
77 /*    MPLSF_PACKET,*/
78
79     /* MPLS encap properties */
80     MPLSF_LABEL,
81     MPLSF_EXP,
82     MPLSF_BOTTOM_OF_STACK,
83     MPLSF_TTL,
84
85     MPLSF_MAX
86 };
87
88 static int mpls_filter[MPLSF_MAX];
89 static hf_register_info mplsf_info[] = {
90
91 /*    {&mpls_filter[MPLSF_PACKET], 
92      {"MPLS Label Switched Packet", "mpls", FT_UINT8, BASE_DEC, NULL, 0x0, 
93       "" }},*/
94
95     {&mpls_filter[MPLSF_LABEL], 
96      {"MPLS Label", "mpls.label", FT_UINT32, BASE_DEC, VALS(special_labels), 0x0, 
97       "" }},
98
99     {&mpls_filter[MPLSF_EXP], 
100      {"MPLS Experimental Bits", "mpls.exp", FT_UINT8, BASE_DEC, NULL, 0x0, 
101       "" }},
102
103     {&mpls_filter[MPLSF_BOTTOM_OF_STACK], 
104      {"MPLS Bottom Of Label Stack", "mpls.bottom", FT_UINT8, BASE_DEC, NULL, 0x0, 
105       "" }},
106
107     {&mpls_filter[MPLSF_TTL], 
108      {"MPLS TTL", "mpls.ttl", FT_UINT8, BASE_DEC, NULL, 0x0, 
109       "" }},
110 };
111
112 static dissector_handle_t ip_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
143     proto_tree  *mpls_tree;
144     proto_item  *ti;
145     tvbuff_t *next_tvb;
146
147     if (check_col(pinfo->fd, COL_PROTOCOL)) {
148         col_set_str(pinfo->fd,COL_PROTOCOL, "MPLS");
149     }
150     
151     if (check_col(pinfo->fd,COL_INFO)) {
152         col_add_fstr(pinfo->fd,COL_INFO,"MPLS Label Switched Packet");
153     }
154
155     /* Start Decoding Here. */
156     while (tvb_reported_length_remaining(tvb, offset) > 0) {
157         decode_mpls_label(tvb, offset, &label, &exp, &bos, &ttl);
158
159         if (tree) {
160
161             ti = proto_tree_add_item(tree, proto_mpls, tvb, offset, 4, FALSE);
162             mpls_tree = proto_item_add_subtree(ti, ett_mpls);
163
164             if (label <= MAX_RESERVED)
165                 proto_tree_add_uint_format(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
166                                     offset, 3, label, "Label: %u (%s)", 
167                                     label, val_to_str(label, special_labels, 
168                                                       "Reserved - Unknown"));
169             else
170                 proto_tree_add_uint(mpls_tree, mpls_filter[MPLSF_LABEL], tvb,
171                                     offset, 3, label);
172
173             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_EXP], tvb, 
174                                 offset+2,1, exp);
175             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_BOTTOM_OF_STACK], tvb, 
176                                 offset+2,1, bos);
177             proto_tree_add_uint(mpls_tree,mpls_filter[MPLSF_TTL], tvb, 
178                                 offset+3,1, ttl);
179         }
180         offset += 4;
181         if (bos) break;
182     }
183     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
184     call_dissector(ip_handle, next_tvb, pinfo, tree);
185 }
186
187 void
188 proto_register_mpls(void)
189 {
190         static gint *ett[] = {
191                 &ett_mpls,
192         };
193
194         proto_mpls = proto_register_protocol("MultiProtocol Label Switching Header",
195             "MPLS", "mpls");
196         proto_register_field_array(proto_mpls, mplsf_info, array_length(mplsf_info));
197         proto_register_subtree_array(ett, array_length(ett));
198 }
199
200 void
201 proto_reg_handoff_mpls(void)
202 {
203         /*
204          * Get a handle for the IP dissector.
205          */
206         ip_handle = find_dissector("ip");
207
208         dissector_add("ethertype", ETHERTYPE_MPLS, dissect_mpls, proto_mpls);
209         dissector_add("ppp.protocol", PPP_MPLS_UNI, dissect_mpls, proto_mpls);
210 }