As the gtk2 directory is no longer needed (GTK1 and 2 are using the same sources...
[obnox/wireshark/wip.git] / packet-raw.c
1 /* packet-raw.c
2  * Routines for raw packet disassembly
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  *
9  * This file created and by Mike Hall <mlh@io.com>
10  * Copyright 1998
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <string.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include "prefs.h"
35 #include "packet-raw.h"
36 #include "packet-ip.h"
37 #include "packet-ppp.h"
38
39 static int proto_raw = -1;
40 static gint ett_raw = -1;
41
42 static const char zeroes[10];
43
44 static dissector_handle_t ip_handle;
45 static dissector_handle_t ipv6_handle;
46 static dissector_handle_t data_handle;
47 static dissector_handle_t ppp_hdlc_handle;
48
49 void
50 capture_raw(const guchar *pd, int len, packet_counts *ld)
51 {
52   /* So far, the only time we get raw connection types are with Linux and
53    * Irix PPP connections.  We can't tell what type of data is coming down
54    * the line, so our safest bet is IP. - GCC
55    */
56
57   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
58    * sometimes.  This check should be removed when 2.2 is out.
59    */
60   if (BYTES_ARE_IN_FRAME(0,len,2) && pd[0] == 0xff && pd[1] == 0x03) {
61     capture_ppp_hdlc(pd, 0, len, ld);
62   }
63   /* The Linux ISDN driver sends a fake MAC address before the PPP header
64    * on its ippp interfaces... */
65   else if (BYTES_ARE_IN_FRAME(0,len,8) && pd[6] == 0xff && pd[7] == 0x03) {
66     capture_ppp_hdlc(pd, 6, len, ld);
67   }
68   /* ...except when it just puts out one byte before the PPP header... */
69   else if (BYTES_ARE_IN_FRAME(0,len,3) && pd[1] == 0xff && pd[2] == 0x03) {
70     capture_ppp_hdlc(pd, 1, len, ld);
71   }
72   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
73    * instead of a fake MAC address and PPP header. */
74   else if (BYTES_ARE_IN_FRAME(0,len,10) && memcmp(pd, zeroes, 10) == 0) {
75     capture_ip(pd, 10, len, ld);
76   }
77   else {
78     /*
79      * OK, is this IPv4 or IPv6?
80      */
81     if (BYTES_ARE_IN_FRAME(0,len,1)) {
82       switch (pd[0] & 0xF0) {
83
84       case 0x40:
85         /* IPv4 */
86         capture_ip(pd, 0, len, ld);
87         break;
88
89 #if 0
90       case 0x60:
91         /* IPv6 */
92         capture_ipv6(pd, 0, len, ld);
93         break;
94 #endif
95       }
96     }
97   }
98 }
99
100 static void
101 dissect_raw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
102 {
103   proto_tree    *fh_tree;
104   proto_item    *ti;
105   tvbuff_t      *next_tvb;
106
107   /* load the top pane info. This should be overwritten by
108      the next protocol in the stack */
109   if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
110     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A" );
111   if(check_col(pinfo->cinfo, COL_RES_DL_DST))
112     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A" );
113   if(check_col(pinfo->cinfo, COL_PROTOCOL))
114     col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A" );
115   if(check_col(pinfo->cinfo, COL_INFO))
116     col_set_str(pinfo->cinfo, COL_INFO, "Raw packet data" );
117
118   /* populate a tree in the second pane with the status of the link
119      layer (ie none) */
120   if (tree) {
121     ti = proto_tree_add_item(tree, proto_raw, tvb, 0, 0, FALSE);
122     fh_tree = proto_item_add_subtree(ti, ett_raw);
123     proto_tree_add_text(fh_tree, tvb, 0, 0, "No link information available");
124   }
125
126   /* So far, the only time we get raw connection types are with Linux and
127    * Irix PPP connections.  We can't tell what type of data is coming down
128    * the line, so our safest bet is IP. - GCC
129    */
130
131   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
132    * sometimes.  This check should be removed when 2.2 is out.
133    */
134   if (tvb_get_ntohs(tvb, 0) == 0xff03) {
135         call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
136   }
137   /* The Linux ISDN driver sends a fake MAC address before the PPP header
138    * on its ippp interfaces... */
139   else if (tvb_get_ntohs(tvb, 6) == 0xff03) {
140         next_tvb = tvb_new_subset(tvb, 6, -1, -1);
141         call_dissector(ppp_hdlc_handle, next_tvb, pinfo, tree);
142   }
143   /* ...except when it just puts out one byte before the PPP header... */
144   else if (tvb_get_ntohs(tvb, 1) == 0xff03) {
145         next_tvb = tvb_new_subset(tvb, 1, -1, -1);
146         call_dissector(ppp_hdlc_handle, next_tvb, pinfo, tree);
147   }
148   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
149    * instead of a fake MAC address and PPP header. */
150   else if (memcmp(tvb_get_ptr(tvb, 0, 10), zeroes, 10) == 0) {
151         next_tvb = tvb_new_subset(tvb, 10, -1, -1);
152         call_dissector(ip_handle, next_tvb, pinfo, tree);
153   }
154   else {
155         /*
156          * OK, is this IPv4 or IPv6?
157          */
158         switch (tvb_get_guint8(tvb, 0) & 0xF0) {
159
160         case 0x40:
161           /* IPv4 */
162           call_dissector(ip_handle, tvb, pinfo, tree);
163           break;
164
165         case 0x60:
166           /* IPv6 */
167           call_dissector(ipv6_handle, tvb, pinfo, tree);
168           break;
169
170         default:
171           /* None of the above. */
172           call_dissector(data_handle, tvb, pinfo, tree);
173           break;
174         }
175   }
176 }
177
178 void
179 proto_register_raw(void)
180 {
181   static gint *ett[] = {
182     &ett_raw,
183   };
184
185   proto_raw = proto_register_protocol("Raw packet data", "Raw", "raw");
186   proto_register_subtree_array(ett, array_length(ett));
187 }
188
189 void
190 proto_reg_handoff_raw(void)
191 {
192   dissector_handle_t raw_handle;
193
194   /*
195    * Get handles for the IP, IPv6, undissected-data, and
196    * PPP-in-HDLC-like-framing dissectors.
197    */
198   ip_handle = find_dissector("ip");
199   ipv6_handle = find_dissector("ipv6");
200   data_handle = find_dissector("data");
201   ppp_hdlc_handle = find_dissector("ppp_hdlc");
202   raw_handle = create_dissector_handle(dissect_raw, proto_raw);
203   dissector_add("wtap_encap", WTAP_ENCAP_RAW_IP, raw_handle);
204 }