Update from Paul Ionescu to set the reported length of the tvbuff for
[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.3 2001/01/03 10:34:41 guy 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 llc_handle;
91
92 void
93 capture_sll(const u_char *pd, packet_counts *ld)
94 {
95         guint16 protocol;
96
97         if (!BYTES_ARE_IN_FRAME(0, 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, SLL_HEADER_SIZE, ld);
114                         break;
115
116                 case LINUX_SLL_P_802_3:
117                         /*
118                          * Novell IPX inside 802.3 with no 802.2 LLC
119                          * header.
120                          */
121                         capture_ipx(pd, SLL_HEADER_SIZE, ld);
122                         break;
123
124                 default:
125                         ld->other++;
126                         break;
127                 }
128         } else
129                 capture_ethertype(protocol, SLL_HEADER_SIZE, pd, ld);
130 }
131
132 static void
133 dissect_sll(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
134 {
135         guint16 pkttype;
136         guint16 protocol;
137         guint16 hatype, halen;
138         guint8 *src;
139         proto_item *ti;
140         volatile guint16 length;
141         tvbuff_t *volatile next_tvb;
142         tvbuff_t *volatile trailer_tvb;
143         proto_tree *volatile fh_tree = NULL;
144         guint length_before;
145
146         CHECK_DISPLAY_AS_DATA(proto_sll, tvb, pinfo, tree);
147
148         pinfo->current_proto = "SLL";
149         if (check_col(pinfo->fd, COL_PROTOCOL))
150                 col_set_str(pinfo->fd, COL_PROTOCOL, "SLL");
151
152         pkttype = tvb_get_ntohs(tvb, 0);
153
154         if (check_col(pinfo->fd, COL_INFO))
155                 col_add_str(pinfo->fd, COL_INFO,
156                     val_to_str(pkttype, packet_type_vals, "Unknown (%u)"));
157
158         if (tree) {
159                 ti = proto_tree_add_protocol_format(tree, proto_sll, tvb, 0,
160                     SLL_HEADER_SIZE, "Linux cooked capture");
161                 fh_tree = proto_item_add_subtree(ti, ett_sll);
162                 proto_tree_add_item(fh_tree, hf_sll_pkttype, tvb, 0, 2, FALSE);
163         }
164
165         /*
166          * XXX - check the link-layer address type value?
167          * For now, we just assume 6 means Ethernet.
168          */
169         hatype = tvb_get_ntohs(tvb, 2);
170         halen = tvb_get_ntohs(tvb, 4);
171         if (tree) {
172                 proto_tree_add_uint(fh_tree, hf_sll_hatype, tvb, 2, 2, hatype);
173                 proto_tree_add_uint(fh_tree, hf_sll_halen, tvb, 4, 2, halen);
174         }
175         if (halen == 6) {
176                 src = tvb_get_ptr(tvb, 6, 6);
177                 SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, src);
178                 SET_ADDRESS(&pinfo->src, AT_ETHER, 6, src);
179                 if (tree) {
180                         proto_tree_add_ether(fh_tree, hf_sll_src_eth, tvb,
181                             6, 6, src);
182                 }
183         } else {
184                 if (tree) {
185                         proto_tree_add_bytes(fh_tree, hf_sll_src_other, tvb,
186                             6, halen, tvb_get_ptr(tvb, 6, halen));
187                 }
188         }
189
190         protocol = tvb_get_ntohs(tvb, 14);
191         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
192                 /*
193                  * "proto" is *not* a length field, it's a Linux internal
194                  * protocol type.
195                  * We therefore cannot say how much of the packet will
196                  * be trailer data.
197                  * XXX - do the same thing we do for packets with Ethertypes?
198                  */
199                 proto_tree_add_uint(fh_tree, hf_sll_ltype, tvb, 14, 2,
200                     protocol);
201
202                 next_tvb = tvb_new_subset(tvb, SLL_HEADER_SIZE, -1, -1);
203                 trailer_tvb = NULL;
204                 switch (protocol) {
205
206                 case LINUX_SLL_P_802_2:
207                         /*
208                          * 802.2 LLC.
209                          */
210                         call_dissector(llc_handle, next_tvb, pinfo, tree);
211                         break;
212
213                 case LINUX_SLL_P_802_3:
214                         /*
215                          * Novell IPX inside 802.3 with no 802.2 LLC
216                          * header.
217                          */
218                         dissect_ipx(next_tvb, pinfo, tree);
219                         break;
220
221                 default:
222                         dissect_data(next_tvb, 0, pinfo, tree);
223                         break;
224                 }
225         } else {
226                 length_before = tvb_reported_length(tvb);
227                 length = ethertype(protocol, tvb, SLL_HEADER_SIZE, pinfo, tree,
228                     fh_tree, hf_sll_etype) + SLL_HEADER_SIZE;
229                 if (length < length_before) {
230                         /*
231                          * Create a tvbuff for the padding.
232                          */
233                         TRY {
234                                 trailer_tvb = tvb_new_subset(tvb, length, -1,
235                                     -1);
236                         }
237                         CATCH2(BoundsError, ReportedBoundsError) {
238                                 /* The packet doesn't have "length" bytes
239                                    worth of captured data left in it.  No
240                                    trailer to display. */
241                                 trailer_tvb = NULL;
242                         }
243                         ENDTRY;
244                 } else {
245                         /*
246                          * There is no padding.
247                          */
248                         trailer_tvb = NULL;
249                 }
250         }
251
252         /* If there's some bytes left over, mark them. */
253         if (trailer_tvb && tree) {
254                 guint trailer_length;
255
256                 trailer_length = tvb_length(trailer_tvb);
257                 if (trailer_length != 0) {
258                         proto_tree_add_item(fh_tree, hf_sll_trailer,
259                             trailer_tvb, 0, trailer_length, FALSE);
260                 }
261         }
262 }
263
264 void
265 proto_register_sll(void)
266 {
267         static hf_register_info hf[] = {
268                 { &hf_sll_pkttype,
269                 { "Packet type",        "sll.pkttype", FT_UINT16, BASE_DEC,
270                   VALS(packet_type_vals), 0x0, "Packet type" }},
271
272                 /* ARP hardware type?  With Linux extensions? */
273                 { &hf_sll_hatype,
274                 { "Link-layer address type",    "sll.hatype", FT_UINT16, BASE_DEC,
275                   NULL, 0x0, "Link-layer address type" }},
276
277                 { &hf_sll_halen,
278                 { "Link-layer address length",  "sll.halen", FT_UINT16, BASE_DEC,
279                   NULL, 0x0, "Link-layer address length" }},
280
281                 /* Source address if it's an Ethernet-type address */
282                 { &hf_sll_src_eth,
283                 { "Source",     "sll.src.eth", FT_ETHER, BASE_NONE, NULL, 0x0,
284                         "Source link-layer address" }},
285
286                 /* Source address if it's not an Ethernet-type address */
287                 { &hf_sll_src_other,
288                 { "Source",     "sll.src.other", FT_BYTES, BASE_HEX, NULL, 0x0,
289                         "Source link-layer address" }},
290
291                 /* if the protocol field is an internal Linux protocol type */
292                 { &hf_sll_ltype,
293                 { "Protocol",   "sll.ltype", FT_UINT16, BASE_HEX,
294                    VALS(ltype_vals), 0x0, "Linux protocol type" }},
295
296                 /* registered here but handled in ethertype.c */
297                 { &hf_sll_etype,
298                 { "Protocol",   "sll.etype", FT_UINT16, BASE_HEX,
299                    VALS(etype_vals), 0x0, "Ethernet protocol type" }},
300
301                 { &hf_sll_trailer,
302                 { "Trailer", "sll.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
303                         "Trailer" }},
304         };
305         static gint *ett[] = {
306                 &ett_sll,
307         };
308
309         proto_sll = proto_register_protocol("Linux cooked-mode capture",
310             "SLL", "sll" );
311         proto_register_field_array(proto_sll, hf, array_length(hf));
312         proto_register_subtree_array(ett, array_length(ett));
313 }
314
315 void
316 proto_reg_handoff_sll(void)
317 {
318         /*
319          * Get a handle for the LLC dissector.
320          */
321         llc_handle = find_dissector("llc");
322
323         dissector_add("wtap_encap", WTAP_ENCAP_SLL, dissect_sll);
324 }