Updates from Ed Warnicke.
[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.23 2000/11/29 05:16:15 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
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 "packet.h"
38 #include "packet-raw.h"
39 #include "packet-ip.h"
40 #include "packet-ppp.h"
41
42 static gint ett_raw = -1;
43
44 static const char zeroes[10];
45
46 static dissector_handle_t ip_handle;
47 static dissector_handle_t ppp_handle;
48
49 void
50 capture_raw(const u_char *pd, 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,2) && pd[0] == 0xff && pd[1] == 0x03) {
61     capture_ppp(pd, 0, 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,8) && pd[6] == 0xff && pd[7] == 0x03) {
66     capture_ppp(pd, 6, ld);
67   }
68   /* ...except when it just puts out one byte before the PPP header... */
69   else if (BYTES_ARE_IN_FRAME(0,3) && pd[1] == 0xff && pd[2] == 0x03) {
70     capture_ppp(pd, 1, 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,10) && memcmp(pd, zeroes, 10) == 0) {
75     capture_ip(pd, 10, ld);
76   }
77   else {
78     capture_ip(pd, 0, ld);
79   }
80 }
81
82 static void
83 dissect_raw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
84 {
85   proto_tree    *fh_tree;
86   proto_item    *ti;
87   tvbuff_t      *next_tvb;
88
89   /* load the top pane info. This should be overwritten by
90      the next protocol in the stack */
91   if(check_col(pinfo->fd, COL_RES_DL_SRC))
92     col_set_str(pinfo->fd, COL_RES_DL_SRC, "N/A" );
93   if(check_col(pinfo->fd, COL_RES_DL_DST))
94     col_set_str(pinfo->fd, COL_RES_DL_DST, "N/A" );
95   if(check_col(pinfo->fd, COL_PROTOCOL))
96     col_set_str(pinfo->fd, COL_PROTOCOL, "N/A" );
97   if(check_col(pinfo->fd, COL_INFO))
98     col_set_str(pinfo->fd, COL_INFO, "Raw packet data" );
99
100   /* populate a tree in the second pane with the status of the link
101      layer (ie none) */
102   if (tree) {
103     ti = proto_tree_add_text(tree, tvb, 0, 0, "Raw packet data" );
104     fh_tree = proto_item_add_subtree(ti, ett_raw);
105     proto_tree_add_text(fh_tree, tvb, 0, 0, "No link information available");
106   }
107
108   /* So far, the only time we get raw connection types are with Linux and
109    * Irix PPP connections.  We can't tell what type of data is coming down
110    * the line, so our safest bet is IP. - GCC
111    */
112    
113   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
114    * sometimes.  This check should be removed when 2.2 is out.
115    */
116   if (tvb_get_ntohs(tvb, 0) == 0xff03) {
117         call_dissector(ppp_handle, tvb, pinfo, tree);
118         return;
119   }
120   /* The Linux ISDN driver sends a fake MAC address before the PPP header
121    * on its ippp interfaces... */
122   else if (tvb_get_ntohs(tvb, 6) == 0xff03) {
123         next_tvb = tvb_new_subset(tvb, 6, -1, -1);
124         call_dissector(ppp_handle, next_tvb, pinfo, tree);
125         return;
126   }
127   /* ...except when it just puts out one byte before the PPP header... */
128   else if (tvb_get_ntohs(tvb, 1) == 0xff03) {
129         next_tvb = tvb_new_subset(tvb, 1, -1, -1);
130         call_dissector(ppp_handle, next_tvb, pinfo, tree);
131         return;
132   }
133   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
134    * instead of a fake MAC address and PPP header. */
135   else if (memcmp(tvb_get_ptr(tvb, 0, 10), zeroes, 10) == 0) {
136         next_tvb = tvb_new_subset(tvb, 10, -1, -1);
137         call_dissector(ip_handle, next_tvb, pinfo, tree);
138         return;
139   }
140   else {
141         next_tvb = tvb_new_subset(tvb, 0, -1, -1);
142         call_dissector(ip_handle, next_tvb, pinfo, tree);
143         return;
144   }
145   g_assert_not_reached();
146 }
147
148 void
149 proto_register_raw(void)
150 {
151   static gint *ett[] = {
152     &ett_raw,
153   };
154
155   proto_register_subtree_array(ett, array_length(ett));
156 }
157
158 void
159 proto_reg_handoff_raw(void)
160 {
161   /*
162    * Get handles for the IP and PPP dissectors.
163    */
164   ip_handle = find_dissector("ip");
165   ppp_handle = find_dissector("ppp");
166   dissector_add("wtap_encap", WTAP_ENCAP_RAW_IP, dissect_raw);
167 }