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