Fix for bug 5425:
[metze/wireshark/wip.git] / epan / dissectors / packet-sll.c
1 /* packet-sll.c
2  * Routines for disassembly of packets from Linux "cooked mode" captures
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30 #include <epan/arptypes.h>
31 #include <epan/prefs.h>
32 #include <epan/packet.h>
33 #include "packet-sll.h"
34 #include "packet-ipx.h"
35 #include "packet-llc.h"
36 #include "packet-eth.h"
37 #include "packet-ppp.h"
38 #include "packet-gre.h"
39 #include <epan/addr_resolv.h>
40 #include <epan/etypes.h>
41
42 static int proto_sll = -1;
43 static int hf_sll_pkttype = -1;
44 static int hf_sll_hatype = -1;
45 static int hf_sll_halen = -1;
46 static int hf_sll_src_eth = -1;
47 static int hf_sll_src_ipv4 = -1;
48 static int hf_sll_src_other = -1;
49 static int hf_sll_ltype = -1;
50 static int hf_sll_gretype = -1;
51 static int hf_sll_etype = -1;
52 static int hf_sll_trailer = -1;
53
54 static gint ett_sll = -1;
55
56 /*
57  * A DLT_LINUX_SLL fake link-layer header.
58  */
59 #define SLL_HEADER_SIZE 16              /* total header length */
60 #define SLL_ADDRLEN     8               /* length of address field */
61
62 /*
63  * The LINUX_SLL_ values for "sll_pkttype".
64  */
65 #define LINUX_SLL_HOST          0
66 #define LINUX_SLL_BROADCAST     1
67 #define LINUX_SLL_MULTICAST     2
68 #define LINUX_SLL_OTHERHOST     3
69 #define LINUX_SLL_OUTGOING      4
70
71 static const value_string packet_type_vals[] = {
72         { LINUX_SLL_HOST,       "Unicast to us" },
73         { LINUX_SLL_BROADCAST,  "Broadcast" },
74         { LINUX_SLL_MULTICAST,  "Multicast" },
75         { LINUX_SLL_OTHERHOST,  "Unicast to another host" },
76         { LINUX_SLL_OUTGOING,   "Sent by us" },
77         { 0,                    NULL }
78 };
79
80 static const value_string ltype_vals[] = {
81         { LINUX_SLL_P_802_3,    "Raw 802.3" },
82         { LINUX_SLL_P_ETHERNET, "Ethernet" },
83         { LINUX_SLL_P_802_2,    "802.2 LLC" },
84         { LINUX_SLL_P_PPPHDLC,  "PPP (HDLC)" },
85         { LINUX_SLL_P_CAN,      "CAN" },
86         { 0,                    NULL }
87 };
88
89 static dissector_table_t sll_linux_dissector_table;
90 static dissector_table_t gre_dissector_table;
91 static dissector_handle_t data_handle;
92
93 void
94 capture_sll(const guchar *pd, int len, packet_counts *ld)
95 {
96         guint16 protocol;
97
98         if (!BYTES_ARE_IN_FRAME(0, len, SLL_HEADER_SIZE)) {
99                 ld->other++;
100                 return;
101         }
102         protocol = pntohs(&pd[14]);
103         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
104                 /*
105                  * "proto" is *not* a length field, it's a Linux internal
106                  * protocol type.
107                  */
108                 switch (protocol) {
109
110                 case LINUX_SLL_P_802_2:
111                         /*
112                          * 802.2 LLC.
113                          */
114                         capture_llc(pd, len, SLL_HEADER_SIZE, ld);
115                         break;
116
117                 case LINUX_SLL_P_ETHERNET:
118                         /*
119                          * Ethernet.
120                          */
121                         capture_eth(pd, SLL_HEADER_SIZE, len, ld);
122                         break;
123
124                 case LINUX_SLL_P_802_3:
125                         /*
126                          * Novell IPX inside 802.3 with no 802.2 LLC
127                          * header.
128                          */
129                         capture_ipx(ld);
130                         break;
131
132                 case LINUX_SLL_P_PPPHDLC:
133                         /*
134                          * PPP HDLC.
135                          */
136                         capture_ppp_hdlc(pd, len, SLL_HEADER_SIZE, ld);
137                         break;
138
139                 default:
140                         ld->other++;
141                         break;
142                 }
143         } else
144                 capture_ethertype(protocol, pd, SLL_HEADER_SIZE, len, ld);
145 }
146
147 static void
148 dissect_sll(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
149 {
150         guint16 pkttype;
151         guint16 protocol;
152         guint16 hatype, halen;
153         const guint8 *src;
154         proto_item *ti;
155         tvbuff_t *next_tvb;
156         proto_tree *fh_tree = NULL;
157
158         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLL");
159         col_clear(pinfo->cinfo, COL_INFO);
160
161         pkttype = tvb_get_ntohs(tvb, 0);
162
163         /*
164          * Set "pinfo->p2p_dir" if the packet wasn't received
165          * promiscuously.
166          */
167         switch (pkttype) {
168
169         case LINUX_SLL_HOST:
170         case LINUX_SLL_BROADCAST:
171         case LINUX_SLL_MULTICAST:
172                 pinfo->p2p_dir = P2P_DIR_RECV;
173                 break;
174
175         case LINUX_SLL_OUTGOING:
176                 pinfo->p2p_dir = P2P_DIR_SENT;
177                 break;
178         }
179
180         if (check_col(pinfo->cinfo, COL_INFO))
181                 col_add_str(pinfo->cinfo, COL_INFO,
182                     val_to_str(pkttype, packet_type_vals, "Unknown (%u)"));
183
184         if (tree) {
185                 ti = proto_tree_add_protocol_format(tree, proto_sll, tvb, 0,
186                     SLL_HEADER_SIZE, "Linux cooked capture");
187                 fh_tree = proto_item_add_subtree(ti, ett_sll);
188                 proto_tree_add_item(fh_tree, hf_sll_pkttype, tvb, 0, 2, FALSE);
189         }
190
191         /*
192          * XXX - check the link-layer address type value?
193          * For now, we just assume 6 means Ethernet.
194          */
195         hatype = tvb_get_ntohs(tvb, 2);
196         halen = tvb_get_ntohs(tvb, 4);
197         if (tree) {
198                 proto_tree_add_uint(fh_tree, hf_sll_hatype, tvb, 2, 2, hatype);
199                 proto_tree_add_uint(fh_tree, hf_sll_halen, tvb, 4, 2, halen);
200         }
201         switch (halen) {
202         case 4:
203                 src = tvb_get_ptr(tvb, 6, 4);
204                 SET_ADDRESS(&pinfo->dl_src, AT_IPv4, 4, src);
205                 SET_ADDRESS(&pinfo->src, AT_IPv4, 4, src);
206                 if (tree) {
207                         proto_tree_add_ipv4(fh_tree, hf_sll_src_ipv4, tvb,
208                             6, 4, FALSE);
209                 }
210                 break;
211         case 6:
212                 src = tvb_get_ptr(tvb, 6, 6);
213                 SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, src);
214                 SET_ADDRESS(&pinfo->src, AT_ETHER, 6, src);
215                 if (tree) {
216                         proto_tree_add_ether(fh_tree, hf_sll_src_eth, tvb,
217                             6, 6, src);
218                 }
219                 break;
220         case 0:
221                 break;
222         default:
223                 if (tree) {
224                         proto_tree_add_item(fh_tree, hf_sll_src_other, tvb,
225                             6, halen > 8 ? 8 : halen, FALSE);
226                 }
227                 break;
228         }
229
230         protocol = tvb_get_ntohs(tvb, 14);
231         next_tvb = tvb_new_subset_remaining(tvb, SLL_HEADER_SIZE);
232         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
233                 /*
234                  * "proto" is *not* a length field, it's a Linux internal
235                  * protocol type.
236                  * We therefore cannot say how much of the packet will
237                  * be trailer data.
238                  * XXX - do the same thing we do for packets with Ethertypes?
239                  */
240                 proto_tree_add_uint(fh_tree, hf_sll_ltype, tvb, 14, 2,
241                     protocol);
242
243                 if(!dissector_try_port(sll_linux_dissector_table, protocol,
244                         next_tvb, pinfo, tree)) {
245                         call_dissector(data_handle, next_tvb, pinfo, tree);
246                 }
247         } else {
248                 switch (hatype) {
249                 case ARPHRD_IPGRE:
250                         proto_tree_add_uint(fh_tree, hf_sll_gretype, tvb, 14, 2,
251                             protocol);
252                         dissector_try_port(gre_dissector_table,
253                                            protocol, next_tvb, pinfo, tree);
254                         break;
255                 default:
256                         ethertype(protocol, tvb, SLL_HEADER_SIZE, pinfo, tree,
257                                   fh_tree, hf_sll_etype, hf_sll_trailer, 0);
258                         break;
259                 }
260         }
261 }
262
263 void
264 proto_register_sll(void)
265 {
266         static hf_register_info hf[] = {
267                 { &hf_sll_pkttype,
268                 { "Packet type",        "sll.pkttype", FT_UINT16, BASE_DEC,
269                   VALS(packet_type_vals), 0x0, NULL, HFILL }},
270
271                 /* ARP hardware type?  With Linux extensions? */
272                 { &hf_sll_hatype,
273                 { "Link-layer address type",    "sll.hatype", FT_UINT16, BASE_DEC,
274                   NULL, 0x0, NULL, HFILL }},
275
276                 { &hf_sll_halen,
277                 { "Link-layer address length",  "sll.halen", FT_UINT16, BASE_DEC,
278                   NULL, 0x0, NULL, HFILL }},
279
280                 /* Source address if it's an Ethernet-type address */
281                 { &hf_sll_src_eth,
282                 { "Source",     "sll.src.eth", FT_ETHER, BASE_NONE, NULL, 0x0,
283                         "Source link-layer address", HFILL }},
284
285                 /* Source address if it's an IPv4 address */
286                 { &hf_sll_src_ipv4,
287                 { "Source",     "sll.src.ipv4", FT_IPv4, BASE_NONE, NULL, 0x0,
288                         "Source link-layer address", HFILL }},
289
290                 /* Source address if it's not an Ethernet-type address */
291                 { &hf_sll_src_other,
292                 { "Source",     "sll.src.other", FT_BYTES, BASE_NONE, NULL, 0x0,
293                         "Source link-layer address", HFILL }},
294
295                 /* if the protocol field is an internal Linux protocol type */
296                 { &hf_sll_ltype,
297                 { "Protocol",   "sll.ltype", FT_UINT16, BASE_HEX,
298                    VALS(ltype_vals), 0x0, "Linux protocol type", HFILL }},
299
300                 /* if the protocol field is a GRE protocol type */
301                 { &hf_sll_gretype,
302                 { "Protocol",   "sll.gretype", FT_UINT16, BASE_HEX,
303                    VALS(gre_typevals), 0x0, "GRE protocol type", HFILL }},
304
305                 /* registered here but handled in ethertype.c */
306                 { &hf_sll_etype,
307                 { "Protocol",   "sll.etype", FT_UINT16, BASE_HEX,
308                    VALS(etype_vals), 0x0, "Ethernet protocol type", HFILL }},
309
310                 { &hf_sll_trailer,
311                 { "Trailer", "sll.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
312                         NULL, HFILL }}
313         };
314         static gint *ett[] = {
315                 &ett_sll
316         };
317
318         proto_sll = proto_register_protocol("Linux cooked-mode capture",
319             "SLL", "sll" );
320         proto_register_field_array(proto_sll, hf, array_length(hf));
321         proto_register_subtree_array(ett, array_length(ett));
322
323         sll_linux_dissector_table = register_dissector_table (
324                 "sll.ltype",
325                 "Linux protocol type",
326                 FT_UINT16,
327                 BASE_HEX
328         );
329 }
330
331 void
332 proto_reg_handoff_sll(void)
333 {
334         dissector_handle_t sll_handle;
335
336         /*
337          * Get handles for the IPX and LLC dissectors.
338          */
339         gre_dissector_table = find_dissector_table("gre.proto");
340         data_handle = find_dissector("data");
341
342         sll_handle = create_dissector_handle(dissect_sll, proto_sll);
343         dissector_add("wtap_encap", WTAP_ENCAP_SLL, sll_handle);
344 }