Make tvb_get_ptr() return 'const guint8*', and clean up all the
[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.8 2001/03/13 21:34:23 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <glib.h>
37 #include "packet.h"
38 #include "packet-ipx.h"
39 #include "packet-llc.h"
40 #include "resolv.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_other = -1;
48 static int hf_sll_ltype = -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 /*
79  * The LINUX_SLL_ values for "sll_protocol".
80  */
81 #define LINUX_SLL_P_802_3       0x0001  /* Novell 802.3 frames without 802.2 LLC header */
82 #define LINUX_SLL_P_802_2       0x0004  /* 802.2 frames (not D/I/X Ethernet) */
83
84 static const value_string ltype_vals[] = {
85         { LINUX_SLL_P_802_3,    "Raw 802.3" },
86         { LINUX_SLL_P_802_2,    "802.2 LLC" },
87         { 0,                    NULL }
88 };
89
90 static dissector_handle_t ipx_handle;
91 static dissector_handle_t llc_handle;
92
93 void
94 capture_sll(const u_char *pd, packet_counts *ld)
95 {
96         guint16 protocol;
97
98         if (!BYTES_ARE_IN_FRAME(0, 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, SLL_HEADER_SIZE, ld);
115                         break;
116
117                 case LINUX_SLL_P_802_3:
118                         /*
119                          * Novell IPX inside 802.3 with no 802.2 LLC
120                          * header.
121                          */
122                         capture_ipx(pd, SLL_HEADER_SIZE, ld);
123                         break;
124
125                 default:
126                         ld->other++;
127                         break;
128                 }
129         } else
130                 capture_ethertype(protocol, SLL_HEADER_SIZE, pd, ld);
131 }
132
133 static void
134 dissect_sll(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
135 {
136         guint16 pkttype;
137         guint16 protocol;
138         guint16 hatype, halen;
139         const guint8 *src;
140         proto_item *ti;
141         tvbuff_t *next_tvb;
142         proto_tree *fh_tree = NULL;
143
144         if (check_col(pinfo->fd, COL_PROTOCOL))
145                 col_set_str(pinfo->fd, COL_PROTOCOL, "SLL");
146         if (check_col(pinfo->fd, COL_INFO))
147                 col_clear(pinfo->fd, COL_INFO);
148
149         pkttype = tvb_get_ntohs(tvb, 0);
150
151         if (check_col(pinfo->fd, COL_INFO))
152                 col_add_str(pinfo->fd, COL_INFO,
153                     val_to_str(pkttype, packet_type_vals, "Unknown (%u)"));
154
155         if (tree) {
156                 ti = proto_tree_add_protocol_format(tree, proto_sll, tvb, 0,
157                     SLL_HEADER_SIZE, "Linux cooked capture");
158                 fh_tree = proto_item_add_subtree(ti, ett_sll);
159                 proto_tree_add_item(fh_tree, hf_sll_pkttype, tvb, 0, 2, FALSE);
160         }
161
162         /*
163          * XXX - check the link-layer address type value?
164          * For now, we just assume 6 means Ethernet.
165          */
166         hatype = tvb_get_ntohs(tvb, 2);
167         halen = tvb_get_ntohs(tvb, 4);
168         if (tree) {
169                 proto_tree_add_uint(fh_tree, hf_sll_hatype, tvb, 2, 2, hatype);
170                 proto_tree_add_uint(fh_tree, hf_sll_halen, tvb, 4, 2, halen);
171         }
172         if (halen == 6) {
173                 src = tvb_get_ptr(tvb, 6, 6);
174                 SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, src);
175                 SET_ADDRESS(&pinfo->src, AT_ETHER, 6, src);
176                 if (tree) {
177                         proto_tree_add_ether(fh_tree, hf_sll_src_eth, tvb,
178                             6, 6, src);
179                 }
180         } else {
181                 if (tree) {
182                         proto_tree_add_bytes(fh_tree, hf_sll_src_other, tvb,
183                             6, halen, tvb_get_ptr(tvb, 6, halen));
184                 }
185         }
186
187         protocol = tvb_get_ntohs(tvb, 14);
188         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
189                 /*
190                  * "proto" is *not* a length field, it's a Linux internal
191                  * protocol type.
192                  * We therefore cannot say how much of the packet will
193                  * be trailer data.
194                  * XXX - do the same thing we do for packets with Ethertypes?
195                  */
196                 proto_tree_add_uint(fh_tree, hf_sll_ltype, tvb, 14, 2,
197                     protocol);
198
199                 next_tvb = tvb_new_subset(tvb, SLL_HEADER_SIZE, -1, -1);
200                 switch (protocol) {
201
202                 case LINUX_SLL_P_802_2:
203                         /*
204                          * 802.2 LLC.
205                          */
206                         call_dissector(llc_handle, next_tvb, pinfo, tree);
207                         break;
208
209                 case LINUX_SLL_P_802_3:
210                         /*
211                          * Novell IPX inside 802.3 with no 802.2 LLC
212                          * header.
213                          */
214                         call_dissector(ipx_handle, next_tvb, pinfo, tree);
215                         break;
216
217                 default:
218                         dissect_data(next_tvb, 0, pinfo, tree);
219                         break;
220                 }
221         } else {
222                 ethertype(protocol, tvb, SLL_HEADER_SIZE, pinfo, tree,
223                     fh_tree, hf_sll_etype, hf_sll_trailer);
224         }
225 }
226
227 void
228 proto_register_sll(void)
229 {
230         static hf_register_info hf[] = {
231                 { &hf_sll_pkttype,
232                 { "Packet type",        "sll.pkttype", FT_UINT16, BASE_DEC,
233                   VALS(packet_type_vals), 0x0, "Packet type" }},
234
235                 /* ARP hardware type?  With Linux extensions? */
236                 { &hf_sll_hatype,
237                 { "Link-layer address type",    "sll.hatype", FT_UINT16, BASE_DEC,
238                   NULL, 0x0, "Link-layer address type" }},
239
240                 { &hf_sll_halen,
241                 { "Link-layer address length",  "sll.halen", FT_UINT16, BASE_DEC,
242                   NULL, 0x0, "Link-layer address length" }},
243
244                 /* Source address if it's an Ethernet-type address */
245                 { &hf_sll_src_eth,
246                 { "Source",     "sll.src.eth", FT_ETHER, BASE_NONE, NULL, 0x0,
247                         "Source link-layer address" }},
248
249                 /* Source address if it's not an Ethernet-type address */
250                 { &hf_sll_src_other,
251                 { "Source",     "sll.src.other", FT_BYTES, BASE_HEX, NULL, 0x0,
252                         "Source link-layer address" }},
253
254                 /* if the protocol field is an internal Linux protocol type */
255                 { &hf_sll_ltype,
256                 { "Protocol",   "sll.ltype", FT_UINT16, BASE_HEX,
257                    VALS(ltype_vals), 0x0, "Linux protocol type" }},
258
259                 /* registered here but handled in ethertype.c */
260                 { &hf_sll_etype,
261                 { "Protocol",   "sll.etype", FT_UINT16, BASE_HEX,
262                    VALS(etype_vals), 0x0, "Ethernet protocol type" }},
263
264                 { &hf_sll_trailer,
265                 { "Trailer", "sll.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
266                         "Trailer" }},
267         };
268         static gint *ett[] = {
269                 &ett_sll,
270         };
271
272         proto_sll = proto_register_protocol("Linux cooked-mode capture",
273             "SLL", "sll" );
274         proto_register_field_array(proto_sll, hf, array_length(hf));
275         proto_register_subtree_array(ett, array_length(ett));
276 }
277
278 void
279 proto_reg_handoff_sll(void)
280 {
281         /*
282          * Get handles for the IPX and LLC dissectors.
283          */
284         llc_handle = find_dissector("llc");
285         ipx_handle = find_dissector("ipx");
286
287         dissector_add("wtap_encap", WTAP_ENCAP_SLL, dissect_sll, proto_sll);
288 }