Clean up white space.
[obnox/wireshark/wip.git] / packet-raw.c
1 /* packet-raw.c
2  * Routines for raw packet disassembly
3  *
4  * $Id: packet-raw.c,v 1.33 2002/02/05 00:09:45 guy Exp $
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 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include "packet-raw.h"
39 #include "packet-ip.h"
40 #include "packet-ppp.h"
41
42 static int proto_raw = -1;
43 static gint ett_raw = -1;
44
45 static const char zeroes[10];
46
47 static dissector_handle_t ip_handle;
48 static dissector_handle_t ipv6_handle;
49 static dissector_handle_t data_handle;
50 static dissector_handle_t ppp_hdlc_handle;
51
52 void
53 capture_raw(const u_char *pd, int len, packet_counts *ld)
54 {
55   /* So far, the only time we get raw connection types are with Linux and
56    * Irix PPP connections.  We can't tell what type of data is coming down
57    * the line, so our safest bet is IP. - GCC
58    */
59    
60   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
61    * sometimes.  This check should be removed when 2.2 is out.
62    */
63   if (BYTES_ARE_IN_FRAME(0,len,2) && pd[0] == 0xff && pd[1] == 0x03) {
64     capture_ppp_hdlc(pd, 0, len, ld);
65   }
66   /* The Linux ISDN driver sends a fake MAC address before the PPP header
67    * on its ippp interfaces... */
68   else if (BYTES_ARE_IN_FRAME(0,len,8) && pd[6] == 0xff && pd[7] == 0x03) {
69     capture_ppp_hdlc(pd, 6, len, ld);
70   }
71   /* ...except when it just puts out one byte before the PPP header... */
72   else if (BYTES_ARE_IN_FRAME(0,len,3) && pd[1] == 0xff && pd[2] == 0x03) {
73     capture_ppp_hdlc(pd, 1, len, ld);
74   }
75   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
76    * instead of a fake MAC address and PPP header. */
77   else if (BYTES_ARE_IN_FRAME(0,len,10) && memcmp(pd, zeroes, 10) == 0) {
78     capture_ip(pd, 10, len, ld);
79   }
80   else {
81     /* 
82      * OK, is this IPv4 or IPv6?
83      */
84     if (BYTES_ARE_IN_FRAME(0,len,1)) {
85       switch (pd[0] & 0xF0) {
86
87       case 0x40:
88         /* IPv4 */
89         capture_ip(pd, 0, len, ld);
90         break;
91
92 #if 0
93       case 0x60:
94         /* IPv6 */
95         capture_ipv6(pd, 0, len, ld);
96         break;
97 #endif
98       }
99     }
100   }
101 }
102
103 static void
104 dissect_raw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
105 {
106   proto_tree    *fh_tree;
107   proto_item    *ti;
108   tvbuff_t      *next_tvb;
109
110   /* load the top pane info. This should be overwritten by
111      the next protocol in the stack */
112   if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
113     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A" );
114   if(check_col(pinfo->cinfo, COL_RES_DL_DST))
115     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A" );
116   if(check_col(pinfo->cinfo, COL_PROTOCOL))
117     col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A" );
118   if(check_col(pinfo->cinfo, COL_INFO))
119     col_set_str(pinfo->cinfo, COL_INFO, "Raw packet data" );
120
121   /* populate a tree in the second pane with the status of the link
122      layer (ie none) */
123   if (tree) {
124     ti = proto_tree_add_item(tree, proto_raw, tvb, 0, 0, FALSE);
125     fh_tree = proto_item_add_subtree(ti, ett_raw);
126     proto_tree_add_text(fh_tree, tvb, 0, 0, "No link information available");
127   }
128
129   /* So far, the only time we get raw connection types are with Linux and
130    * Irix PPP connections.  We can't tell what type of data is coming down
131    * the line, so our safest bet is IP. - GCC
132    */
133    
134   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
135    * sometimes.  This check should be removed when 2.2 is out.
136    */
137   if (tvb_get_ntohs(tvb, 0) == 0xff03) {
138         call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
139   }
140   /* The Linux ISDN driver sends a fake MAC address before the PPP header
141    * on its ippp interfaces... */
142   else if (tvb_get_ntohs(tvb, 6) == 0xff03) {
143         next_tvb = tvb_new_subset(tvb, 6, -1, -1);
144         call_dissector(ppp_hdlc_handle, next_tvb, pinfo, tree);
145   }
146   /* ...except when it just puts out one byte before the PPP header... */
147   else if (tvb_get_ntohs(tvb, 1) == 0xff03) {
148         next_tvb = tvb_new_subset(tvb, 1, -1, -1);
149         call_dissector(ppp_hdlc_handle, next_tvb, pinfo, tree);
150   }
151   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
152    * instead of a fake MAC address and PPP header. */
153   else if (memcmp(tvb_get_ptr(tvb, 0, 10), zeroes, 10) == 0) {
154         next_tvb = tvb_new_subset(tvb, 10, -1, -1);
155         call_dissector(ip_handle, next_tvb, pinfo, tree);
156   }
157   else {
158         /*
159          * OK, is this IPv4 or IPv6?
160          */
161         switch (tvb_get_guint8(tvb, 0) & 0xF0) {
162
163         case 0x40:
164           /* IPv4 */
165           call_dissector(ip_handle, tvb, pinfo, tree);
166           break;
167
168         case 0x60:
169           /* IPv6 */
170           call_dissector(ipv6_handle, tvb, pinfo, tree);
171           break;
172
173         default:
174           /* None of the above. */
175           call_dissector(data_handle, tvb, pinfo, tree);
176           break;
177         }
178   }
179 }
180
181 void
182 proto_register_raw(void)
183 {
184   static gint *ett[] = {
185     &ett_raw,
186   };
187
188   proto_raw = proto_register_protocol("Raw packet data", "Raw", "raw");
189   proto_register_subtree_array(ett, array_length(ett));
190 }
191
192 void
193 proto_reg_handoff_raw(void)
194 {
195   dissector_handle_t raw_handle;
196
197   /*
198    * Get handles for the IP, IPv6, undissected-data, and
199    * PPP-in-HDLC-like-framing dissectors.
200    */
201   ip_handle = find_dissector("ip");
202   ipv6_handle = find_dissector("ipv6");
203   data_handle = find_dissector("data");
204   ppp_hdlc_handle = find_dissector("ppp_hdlc");
205   raw_handle = create_dissector_handle(dissect_raw, proto_raw);
206   dissector_add("wtap_encap", WTAP_ENCAP_RAW_IP, raw_handle);
207 }