Move the STRING dissector to packet-dcerpc-nt.c and add one more parameter
[obnox/wireshark/wip.git] / packet-sll.c
1 /* packet-sll.c
2  * Routines for disassembly of packets from Linux "cooked mode" captures
3  *
4  * $Id: packet-sll.c,v 1.16 2002/01/21 07:36:42 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include "packet-sll.h"
38 #include "packet-ipx.h"
39 #include "packet-llc.h"
40 #include <epan/resolv.h>
41 #include "etypes.h"
42
43 static int proto_sll = -1;
44 static int hf_sll_pkttype = -1;
45 static int hf_sll_hatype = -1;
46 static int hf_sll_halen = -1;
47 static int hf_sll_src_eth = -1;
48 static int hf_sll_src_other = -1;
49 static int hf_sll_ltype = -1;
50 static int hf_sll_etype = -1;
51 static int hf_sll_trailer = -1;
52
53 static gint ett_sll = -1;
54
55 /*
56  * A DLT_LINUX_SLL fake link-layer header.
57  */
58 #define SLL_HEADER_SIZE 16              /* total header length */
59 #define SLL_ADDRLEN     8               /* length of address field */
60
61 /*
62  * The LINUX_SLL_ values for "sll_pkttype".
63  */
64 #define LINUX_SLL_HOST          0
65 #define LINUX_SLL_BROADCAST     1
66 #define LINUX_SLL_MULTICAST     2
67 #define LINUX_SLL_OTHERHOST     3
68 #define LINUX_SLL_OUTGOING      4
69
70 static const value_string packet_type_vals[] = {
71         { LINUX_SLL_HOST,       "Unicast to us" },
72         { LINUX_SLL_BROADCAST,  "Broadcast" },
73         { LINUX_SLL_MULTICAST,  "Multicast" },
74         { LINUX_SLL_OTHERHOST,  "Unicast to another host" },
75         { LINUX_SLL_OUTGOING,   "Sent by us" },
76         { 0,                    NULL }
77 };
78
79 /*
80  * The LINUX_SLL_ values for "sll_protocol".
81  */
82 #define LINUX_SLL_P_802_3       0x0001  /* Novell 802.3 frames without 802.2 LLC header */
83 #define LINUX_SLL_P_802_2       0x0004  /* 802.2 frames (not D/I/X Ethernet) */
84
85 static const value_string ltype_vals[] = {
86         { LINUX_SLL_P_802_3,    "Raw 802.3" },
87         { LINUX_SLL_P_802_2,    "802.2 LLC" },
88         { 0,                    NULL }
89 };
90
91 static dissector_handle_t ipx_handle;
92 static dissector_handle_t llc_handle;
93 static dissector_handle_t data_handle;
94
95 void
96 capture_sll(const u_char *pd, int len, packet_counts *ld)
97 {
98         guint16 protocol;
99
100         if (!BYTES_ARE_IN_FRAME(0, len, SLL_HEADER_SIZE)) {
101                 ld->other++;
102                 return;
103         }
104         protocol = pntohs(&pd[14]);
105         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
106                 /*
107                  * "proto" is *not* a length field, it's a Linux internal
108                  * protocol type.
109                  */
110                 switch (protocol) {
111
112                 case LINUX_SLL_P_802_2:
113                         /*
114                          * 802.2 LLC.
115                          */
116                         capture_llc(pd, len, SLL_HEADER_SIZE, ld);
117                         break;
118
119                 case LINUX_SLL_P_802_3:
120                         /*
121                          * Novell IPX inside 802.3 with no 802.2 LLC
122                          * header.
123                          */
124                         capture_ipx(pd, len, SLL_HEADER_SIZE, ld);
125                         break;
126
127                 default:
128                         ld->other++;
129                         break;
130                 }
131         } else
132                 capture_ethertype(protocol, pd, SLL_HEADER_SIZE, len, ld);
133 }
134
135 static void
136 dissect_sll(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
137 {
138         guint16 pkttype;
139         guint16 protocol;
140         guint16 hatype, halen;
141         const guint8 *src;
142         proto_item *ti;
143         tvbuff_t *next_tvb;
144         proto_tree *fh_tree = NULL;
145
146         if (check_col(pinfo->cinfo, COL_PROTOCOL))
147                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLL");
148         if (check_col(pinfo->cinfo, COL_INFO))
149                 col_clear(pinfo->cinfo, COL_INFO);
150
151         pkttype = tvb_get_ntohs(tvb, 0);
152
153         if (check_col(pinfo->cinfo, COL_INFO))
154                 col_add_str(pinfo->cinfo, COL_INFO,
155                     val_to_str(pkttype, packet_type_vals, "Unknown (%u)"));
156
157         if (tree) {
158                 ti = proto_tree_add_protocol_format(tree, proto_sll, tvb, 0,
159                     SLL_HEADER_SIZE, "Linux cooked capture");
160                 fh_tree = proto_item_add_subtree(ti, ett_sll);
161                 proto_tree_add_item(fh_tree, hf_sll_pkttype, tvb, 0, 2, FALSE);
162         }
163
164         /*
165          * XXX - check the link-layer address type value?
166          * For now, we just assume 6 means Ethernet.
167          */
168         hatype = tvb_get_ntohs(tvb, 2);
169         halen = tvb_get_ntohs(tvb, 4);
170         if (tree) {
171                 proto_tree_add_uint(fh_tree, hf_sll_hatype, tvb, 2, 2, hatype);
172                 proto_tree_add_uint(fh_tree, hf_sll_halen, tvb, 4, 2, halen);
173         }
174         if (halen == 6) {
175                 src = tvb_get_ptr(tvb, 6, 6);
176                 SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, src);
177                 SET_ADDRESS(&pinfo->src, AT_ETHER, 6, src);
178                 if (tree) {
179                         proto_tree_add_ether(fh_tree, hf_sll_src_eth, tvb,
180                             6, 6, src);
181                 }
182         } else {
183                 if (tree) {
184                         proto_tree_add_item(fh_tree, hf_sll_src_other, tvb,
185                             6, halen, FALSE);
186                 }
187         }
188
189         protocol = tvb_get_ntohs(tvb, 14);
190         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
191                 /*
192                  * "proto" is *not* a length field, it's a Linux internal
193                  * protocol type.
194                  * We therefore cannot say how much of the packet will
195                  * be trailer data.
196                  * XXX - do the same thing we do for packets with Ethertypes?
197                  */
198                 proto_tree_add_uint(fh_tree, hf_sll_ltype, tvb, 14, 2,
199                     protocol);
200
201                 next_tvb = tvb_new_subset(tvb, SLL_HEADER_SIZE, -1, -1);
202                 switch (protocol) {
203
204                 case LINUX_SLL_P_802_2:
205                         /*
206                          * 802.2 LLC.
207                          */
208                         call_dissector(llc_handle, next_tvb, pinfo, tree);
209                         break;
210
211                 case LINUX_SLL_P_802_3:
212                         /*
213                          * Novell IPX inside 802.3 with no 802.2 LLC
214                          * header.
215                          */
216                         call_dissector(ipx_handle, next_tvb, pinfo, tree);
217                         break;
218
219                 default:
220                         call_dissector(data_handle,next_tvb, pinfo, tree);
221                         break;
222                 }
223         } else {
224                 ethertype(protocol, tvb, SLL_HEADER_SIZE, pinfo, tree,
225                     fh_tree, hf_sll_etype, hf_sll_trailer);
226         }
227 }
228
229 void
230 proto_register_sll(void)
231 {
232         static hf_register_info hf[] = {
233                 { &hf_sll_pkttype,
234                 { "Packet type",        "sll.pkttype", FT_UINT16, BASE_DEC,
235                   VALS(packet_type_vals), 0x0, "Packet type", HFILL }},
236
237                 /* ARP hardware type?  With Linux extensions? */
238                 { &hf_sll_hatype,
239                 { "Link-layer address type",    "sll.hatype", FT_UINT16, BASE_DEC,
240                   NULL, 0x0, "Link-layer address type", HFILL }},
241
242                 { &hf_sll_halen,
243                 { "Link-layer address length",  "sll.halen", FT_UINT16, BASE_DEC,
244                   NULL, 0x0, "Link-layer address length", HFILL }},
245
246                 /* Source address if it's an Ethernet-type address */
247                 { &hf_sll_src_eth,
248                 { "Source",     "sll.src.eth", FT_ETHER, BASE_NONE, NULL, 0x0,
249                         "Source link-layer address", HFILL }},
250
251                 /* Source address if it's not an Ethernet-type address */
252                 { &hf_sll_src_other,
253                 { "Source",     "sll.src.other", FT_BYTES, BASE_HEX, NULL, 0x0,
254                         "Source link-layer address", HFILL }},
255
256                 /* if the protocol field is an internal Linux protocol type */
257                 { &hf_sll_ltype,
258                 { "Protocol",   "sll.ltype", FT_UINT16, BASE_HEX,
259                    VALS(ltype_vals), 0x0, "Linux protocol type", HFILL }},
260
261                 /* registered here but handled in ethertype.c */
262                 { &hf_sll_etype,
263                 { "Protocol",   "sll.etype", FT_UINT16, BASE_HEX,
264                    VALS(etype_vals), 0x0, "Ethernet protocol type", HFILL }},
265
266                 { &hf_sll_trailer,
267                 { "Trailer", "sll.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
268                         "Trailer", HFILL }},
269         };
270         static gint *ett[] = {
271                 &ett_sll,
272         };
273
274         proto_sll = proto_register_protocol("Linux cooked-mode capture",
275             "SLL", "sll" );
276         proto_register_field_array(proto_sll, hf, array_length(hf));
277         proto_register_subtree_array(ett, array_length(ett));
278 }
279
280 void
281 proto_reg_handoff_sll(void)
282 {
283         dissector_handle_t sll_handle;
284
285         /*
286          * Get handles for the IPX and LLC dissectors.
287          */
288         llc_handle = find_dissector("llc");
289         ipx_handle = find_dissector("ipx");
290         data_handle = find_dissector("data");
291
292         sll_handle = create_dissector_handle(dissect_sll, proto_sll);
293         dissector_add("wtap_encap", WTAP_ENCAP_SLL, sll_handle);
294 }