"col_format_to_pref_str()" is used only in "prefs.c", and knows about
[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.11 2001/07/16 05:16:58 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 #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
94 void
95 capture_sll(const u_char *pd, packet_counts *ld)
96 {
97         guint16 protocol;
98
99         if (!BYTES_ARE_IN_FRAME(0, SLL_HEADER_SIZE)) {
100                 ld->other++;
101                 return;
102         }
103         protocol = pntohs(&pd[14]);
104         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
105                 /*
106                  * "proto" is *not* a length field, it's a Linux internal
107                  * protocol type.
108                  */
109                 switch (protocol) {
110
111                 case LINUX_SLL_P_802_2:
112                         /*
113                          * 802.2 LLC.
114                          */
115                         capture_llc(pd, SLL_HEADER_SIZE, ld);
116                         break;
117
118                 case LINUX_SLL_P_802_3:
119                         /*
120                          * Novell IPX inside 802.3 with no 802.2 LLC
121                          * header.
122                          */
123                         capture_ipx(pd, SLL_HEADER_SIZE, ld);
124                         break;
125
126                 default:
127                         ld->other++;
128                         break;
129                 }
130         } else
131                 capture_ethertype(protocol, SLL_HEADER_SIZE, pd, ld);
132 }
133
134 static void
135 dissect_sll(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
136 {
137         guint16 pkttype;
138         guint16 protocol;
139         guint16 hatype, halen;
140         const guint8 *src;
141         proto_item *ti;
142         tvbuff_t *next_tvb;
143         proto_tree *fh_tree = NULL;
144
145         if (check_col(pinfo->fd, COL_PROTOCOL))
146                 col_set_str(pinfo->fd, COL_PROTOCOL, "SLL");
147         if (check_col(pinfo->fd, COL_INFO))
148                 col_clear(pinfo->fd, COL_INFO);
149
150         pkttype = tvb_get_ntohs(tvb, 0);
151
152         if (check_col(pinfo->fd, COL_INFO))
153                 col_add_str(pinfo->fd, COL_INFO,
154                     val_to_str(pkttype, packet_type_vals, "Unknown (%u)"));
155
156         if (tree) {
157                 ti = proto_tree_add_protocol_format(tree, proto_sll, tvb, 0,
158                     SLL_HEADER_SIZE, "Linux cooked capture");
159                 fh_tree = proto_item_add_subtree(ti, ett_sll);
160                 proto_tree_add_item(fh_tree, hf_sll_pkttype, tvb, 0, 2, FALSE);
161         }
162
163         /*
164          * XXX - check the link-layer address type value?
165          * For now, we just assume 6 means Ethernet.
166          */
167         hatype = tvb_get_ntohs(tvb, 2);
168         halen = tvb_get_ntohs(tvb, 4);
169         if (tree) {
170                 proto_tree_add_uint(fh_tree, hf_sll_hatype, tvb, 2, 2, hatype);
171                 proto_tree_add_uint(fh_tree, hf_sll_halen, tvb, 4, 2, halen);
172         }
173         if (halen == 6) {
174                 src = tvb_get_ptr(tvb, 6, 6);
175                 SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, src);
176                 SET_ADDRESS(&pinfo->src, AT_ETHER, 6, src);
177                 if (tree) {
178                         proto_tree_add_ether(fh_tree, hf_sll_src_eth, tvb,
179                             6, 6, src);
180                 }
181         } else {
182                 if (tree) {
183                         proto_tree_add_item(fh_tree, hf_sll_src_other, tvb,
184                             6, halen, FALSE);
185                 }
186         }
187
188         protocol = tvb_get_ntohs(tvb, 14);
189         if (protocol <= 1536) { /* yes, 1536 - that's how Linux does it */
190                 /*
191                  * "proto" is *not* a length field, it's a Linux internal
192                  * protocol type.
193                  * We therefore cannot say how much of the packet will
194                  * be trailer data.
195                  * XXX - do the same thing we do for packets with Ethertypes?
196                  */
197                 proto_tree_add_uint(fh_tree, hf_sll_ltype, tvb, 14, 2,
198                     protocol);
199
200                 next_tvb = tvb_new_subset(tvb, SLL_HEADER_SIZE, -1, -1);
201                 switch (protocol) {
202
203                 case LINUX_SLL_P_802_2:
204                         /*
205                          * 802.2 LLC.
206                          */
207                         call_dissector(llc_handle, next_tvb, pinfo, tree);
208                         break;
209
210                 case LINUX_SLL_P_802_3:
211                         /*
212                          * Novell IPX inside 802.3 with no 802.2 LLC
213                          * header.
214                          */
215                         call_dissector(ipx_handle, next_tvb, pinfo, tree);
216                         break;
217
218                 default:
219                         dissect_data(next_tvb, 0, pinfo, tree);
220                         break;
221                 }
222         } else {
223                 ethertype(protocol, tvb, SLL_HEADER_SIZE, pinfo, tree,
224                     fh_tree, hf_sll_etype, hf_sll_trailer);
225         }
226 }
227
228 void
229 proto_register_sll(void)
230 {
231         static hf_register_info hf[] = {
232                 { &hf_sll_pkttype,
233                 { "Packet type",        "sll.pkttype", FT_UINT16, BASE_DEC,
234                   VALS(packet_type_vals), 0x0, "Packet type", HFILL }},
235
236                 /* ARP hardware type?  With Linux extensions? */
237                 { &hf_sll_hatype,
238                 { "Link-layer address type",    "sll.hatype", FT_UINT16, BASE_DEC,
239                   NULL, 0x0, "Link-layer address type", HFILL }},
240
241                 { &hf_sll_halen,
242                 { "Link-layer address length",  "sll.halen", FT_UINT16, BASE_DEC,
243                   NULL, 0x0, "Link-layer address length", HFILL }},
244
245                 /* Source address if it's an Ethernet-type address */
246                 { &hf_sll_src_eth,
247                 { "Source",     "sll.src.eth", FT_ETHER, BASE_NONE, NULL, 0x0,
248                         "Source link-layer address", HFILL }},
249
250                 /* Source address if it's not an Ethernet-type address */
251                 { &hf_sll_src_other,
252                 { "Source",     "sll.src.other", FT_BYTES, BASE_HEX, NULL, 0x0,
253                         "Source link-layer address", HFILL }},
254
255                 /* if the protocol field is an internal Linux protocol type */
256                 { &hf_sll_ltype,
257                 { "Protocol",   "sll.ltype", FT_UINT16, BASE_HEX,
258                    VALS(ltype_vals), 0x0, "Linux protocol type", HFILL }},
259
260                 /* registered here but handled in ethertype.c */
261                 { &hf_sll_etype,
262                 { "Protocol",   "sll.etype", FT_UINT16, BASE_HEX,
263                    VALS(etype_vals), 0x0, "Ethernet protocol type", HFILL }},
264
265                 { &hf_sll_trailer,
266                 { "Trailer", "sll.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
267                         "Trailer", HFILL }},
268         };
269         static gint *ett[] = {
270                 &ett_sll,
271         };
272
273         proto_sll = proto_register_protocol("Linux cooked-mode capture",
274             "SLL", "sll" );
275         proto_register_field_array(proto_sll, hf, array_length(hf));
276         proto_register_subtree_array(ett, array_length(ett));
277 }
278
279 void
280 proto_reg_handoff_sll(void)
281 {
282         /*
283          * Get handles for the IPX and LLC dissectors.
284          */
285         llc_handle = find_dissector("llc");
286         ipx_handle = find_dissector("ipx");
287
288         dissector_add("wtap_encap", WTAP_ENCAP_SLL, dissect_sll, proto_sll);
289 }