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