Change dissect_deferred_pointer()
[obnox/wireshark/wip.git] / packet-fw1.c
1 /* packet-fw1.c
2  * Routines for Ethernet header disassembly of FW1 "monitor" files
3  * Copyright 2002,2003, Alfred Koebler <ako@icon.de>
4  *
5  * $Id: packet-fw1.c,v 1.9 2003/10/01 07:11:44 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Alfred Koebler <ako@icon.de>
9  * Copyright 2002,2003 Alfred Koebler
10  *
11  * To use this dissector use the command line option
12  * -o eth.interpret_as_fw1_monitor:TRUE
13  *
14  * At the moment the way with the option is the best one.
15  * A automatic way is not possible, because the file format isn't different
16  * to the snoop file.
17  *
18  * With "fw monitor" it is possible to collect packets on several places.
19  * The additional information:
20  * - is it a incoming or outgoing packet
21  * - is it before or after the firewall
22  *   i  incoming before the firewall
23  *   I  incoming after the firewall
24  *   o  outcoming before the firewall
25  *   O  outcoming after the firewall
26  * - the name of the interface
27  *
28  * What's the problem ?
29  * Think about one packet traveling across the firewall.
30  * With ethereal you will see 4 lines in the Top Pane.
31  * To analyze a problem it is helpful to see the additional information
32  * in the protocol tree of the Middle Pane.
33  *
34  * The presentation of the summary line is designed in the following way:
35  * Every time the next selected packet in the Top Pane includes a
36  * "new" interface name the name is added to the list in the summary line.
37  * The interface names are listed one after the other.
38  * The position of the interface names didn't change.
39  *
40  * And who are the 4 places represented ?
41  * The interface name represents the firewall module of the interface.
42  * On the left side of the interface name is the interface module.
43  * On the right side of the interface name is the "IP" module.
44  *
45  * Example for a ping from the firewall to another host:
46  * For the four lines in the Top Pane you will see the according lines
47  * in the Middle Pane:
48  *   El90x1 o
49  * O El90x1
50  * i El90x1
51  *   El90x1 I
52  *
53  * Example for a packet traversing through the Firewall, first through
54  * the inner side firewall module then through the outer side firewall module:
55  * i  El90x1        El90x2
56  *    El90x1 I      El90x2
57  *    El90x1      o E190x2
58  *    El90x1        E190x2 O
59  *
60  * 9.12.2002
61  * Add new column with summary of FW-1 interface/direction
62  *
63  * 11.8.2003
64  * Additional interpretation of field Chain Position.
65  * Show the chain position in the interface list.
66  * Support for new format of fw monitor file 
67  * writen by option -u | -s for UUID/SUUID.
68  * NOTICE: First paket will have UUID == 0 !
69  *
70  * This program is free software; you can redistribute it and/or
71  * modify it under the terms of the GNU General Public License
72  * as published by the Free Software Foundation; either version 2
73  * of the License, or (at your option) any later version.
74  *
75  * This program is distributed in the hope that it will be useful,
76  * but WITHOUT ANY WARRANTY; without even the implied warranty of
77  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
78  * GNU General Public License for more details.
79  *
80  * You should have received a copy of the GNU General Public License
81  * along with this program; if not, write to the Free Software
82  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
83  */
84
85 #ifdef HAVE_CONFIG_H
86 # include "config.h"
87 #endif
88
89 #include <string.h>
90
91 #include <glib.h>
92 #include <epan/packet.h>
93 #include "prefs.h"
94 #include "etypes.h"
95
96 /* Place FW1 summary in proto tree */
97 static gboolean fw1_summary_in_tree = TRUE;
98 static gboolean fw1_with_uuid = FALSE;
99 static gboolean fw1_iflist_with_chain = FALSE;
100
101 /* Initialize the protocol and registered fields */
102 static int proto_fw1 = -1;
103 static int hf_fw1_direction = -1;
104 static int hf_fw1_chain = -1;
105 static int hf_fw1_interface = -1;
106 static int hf_fw1_uuid = -1;
107 static int hf_fw1_type = -1;
108 static int hf_fw1_trailer = -1;
109
110 /* Initialize the subtree pointers */
111 static gint ett_fw1 = -1;
112
113 #define ETH_HEADER_SIZE 14
114
115 static dissector_handle_t eth_handle;
116
117 static void
118 dissect_fw1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
119 {
120   /* Set up structures needed to add the protocol subtree and manage it */
121   proto_item    *ti;
122   proto_tree    *volatile fh_tree = NULL;
123   char          direction[3];
124   char          chain[3];
125   char          interface_name[10+1];
126   guint32       uuid;
127   guint16       etype;
128   char          header[1000];
129   char          *p_header;
130   int           i;
131   int           found;
132
133   #define       MAX_INTERFACES  20
134   static char   *p_interfaces[MAX_INTERFACES];
135   static int    interface_anzahl=0;
136   static char   header1[] = "FW1 Monitor";
137
138 #ifdef  DID_NOT_FIND_A_PLACE_TO_START_THE_RESET_LIST_OF_INTERFACES
139   if (fw1_reset_list_of_if_on_next_begin) {
140     /* without FW1 summary delete all remembered names of interfaces */
141     for (i=0; i<interface_anzahl && i<MAX_INTERFACES; i++) {
142       free(p_interfaces[i]);
143     }
144     interface_anzahl = 0;
145     fw1_reset_list_of_if_on_next_begin = FALSE;
146   }
147 #endif
148
149   /* Make entries in Protocol column and Info column on summary display */
150   if (check_col(pinfo->cinfo, COL_PROTOCOL))
151     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FW1");
152   if (check_col(pinfo->cinfo, COL_INFO))
153     col_clear(pinfo->cinfo, COL_INFO);
154
155   etype = tvb_get_ntohs(tvb, 12);
156
157   sprintf(header, header1);
158
159   /* fetch info to local variable */
160   direction[0] = tvb_get_guint8(tvb, 0);
161   direction[1] = 0;
162   chain[0] = tvb_get_guint8(tvb, 1);
163   chain[1] = 0;
164
165   if (!fw1_with_uuid) {
166     tvb_get_nstringz0(tvb, 2, 6, interface_name);
167     uuid = 0;
168   } else {
169     tvb_get_nstringz0(tvb, 2, sizeof interface_name, interface_name);
170     uuid = tvb_get_ntohl(tvb, 8);
171   }
172
173   /* Known interface name - if not, remember it */
174   found=1;
175   for (i=0; i<interface_anzahl && i<MAX_INTERFACES; i++) {
176     if ( strcmp(p_interfaces[i], interface_name) == 0 ) {
177       found=0;
178     }
179   }
180   if (found == 1 ) {
181     p_interfaces[interface_anzahl] = strdup(interface_name);
182     interface_anzahl++;
183   }
184   /* display all interfaces always in the same order */
185   for (i=0; i<interface_anzahl; i++) {
186     found=1;
187     if ( strcmp(p_interfaces[i], interface_name) == 0 ) {
188       found=0;
189     }
190     p_header = header + strlen(header);
191     if (!fw1_iflist_with_chain) {
192       sprintf(p_header, "  %c %s %c",
193         found==0 ? (direction[0]=='i' ? 'i' : (direction[0]=='O' ? 'O' : ' ')) : ' ',
194         p_interfaces[i],
195         found==0 ? (direction[0]=='I' ? 'I' : (direction[0]=='o' ? 'o' : ' ')) : ' '
196         );
197     } else {
198       sprintf(p_header, "  %c%c %s %c%c",
199         found==0 ? (direction[0]=='i' ? 'i' : (direction[0]=='O' ? 'O' : ' ')) : ' ',
200         found==0 ? (direction[0]=='i' ? chain[0] : (direction[0]=='O' ? chain[0] : ' ')) : ' ',
201         p_interfaces[i],
202         found==0 ? (direction[0]=='I' ? 'I' : (direction[0]=='o' ? 'o' : ' ')) : ' ',
203         found==0 ? (direction[0]=='I' ? chain[0] : (direction[0]=='o' ? chain[0] : ' ')) : ' '
204         );
205     }
206   }
207
208   if (check_col(pinfo->cinfo, COL_IF_DIR))
209     col_add_str(pinfo->cinfo, COL_IF_DIR, header + strlen(header1) + 1);
210
211   if (tree) {
212     if (!fw1_summary_in_tree) {
213       /* Do not show the summary in Protocol Tree */
214       sprintf(header, header1);
215     }
216     ti = proto_tree_add_protocol_format(tree, proto_fw1, tvb, 0, ETH_HEADER_SIZE, header);
217
218     /* create display subtree for the protocol */
219     fh_tree = proto_item_add_subtree(ti, ett_fw1);
220
221     proto_tree_add_item(fh_tree, hf_fw1_direction, tvb, 0, 1, FALSE);
222     proto_tree_add_item(fh_tree, hf_fw1_chain, tvb, 1, 1, FALSE);
223
224     if (!fw1_with_uuid) {
225       proto_tree_add_string_format(fh_tree, hf_fw1_interface,
226         tvb, 2, 10,
227         interface_name, "Interface: %s", interface_name);
228     } else {
229       proto_tree_add_string_format(fh_tree, hf_fw1_interface,
230         tvb, 2, 6,
231         interface_name, "Interface: %s", interface_name);
232       proto_tree_add_item(fh_tree, hf_fw1_uuid, tvb, 8, 4, FALSE);
233     }
234   }
235   ethertype(etype, tvb, ETH_HEADER_SIZE, pinfo, tree, fh_tree, hf_fw1_type,
236           hf_fw1_trailer, 0);
237 }
238
239 void
240 proto_register_fw1(void)
241 {
242   static hf_register_info hf[] = {
243         { &hf_fw1_direction,
244         { "Direction",  "fw1.direction", FT_STRING, BASE_NONE, NULL, 0x0,
245                 "Direction", HFILL }},
246         { &hf_fw1_chain,
247         { "Chain Position",     "fw1.chain", FT_STRING, BASE_NONE, NULL, 0x0,
248                 "Chain Position", HFILL }},
249         { &hf_fw1_interface,
250         { "Interface",  "fw1.interface", FT_STRING, BASE_NONE, NULL, 0x0,
251                 "Interface", HFILL }},
252         { &hf_fw1_uuid,
253         { "UUID",       "fw1.uuid", FT_UINT32, BASE_DEC, NULL, 0x0,
254                 "UUID", HFILL }},
255                 /* registered here but handled in ethertype.c */
256         { &hf_fw1_type,
257         { "Type",               "fw1.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
258                 "", HFILL }},
259   };
260   /* Setup protocol subtree array */
261   static gint *ett[] = {
262         &ett_fw1,
263   };
264   module_t *fw1_module;
265
266   /* Register the protocol name and description */
267   proto_fw1 = proto_register_protocol("Checkpoint FW-1", "FW-1", "fw1");
268   /* Required function calls to register the header fields and subtrees used */
269   proto_register_field_array(proto_fw1, hf, array_length(hf));
270   proto_register_subtree_array(ett, array_length(ett));
271
272   /* Register configuration preferences */
273   fw1_module = prefs_register_protocol(proto_fw1, NULL);
274   prefs_register_bool_preference(fw1_module, "summary_in_tree",
275             "Show FireWall-1 summary in protocol tree",
276             "Whether the FireWall-1 summary line should be shown in the protocol tree",
277             &fw1_summary_in_tree);
278   prefs_register_bool_preference(fw1_module, "with_uuid",
279             "fw monitor file includes UUID",
280             "Whether the fw monitor file includes information of UUID",
281             &fw1_with_uuid);
282   prefs_register_bool_preference(fw1_module, "iflist_with_chain",
283             "Interface list with chain position",
284             "Whether the interface list includes chain position",
285             &fw1_iflist_with_chain);
286
287   register_dissector("fw1", dissect_fw1, proto_fw1);
288 }
289
290 void
291 proto_reg_handoff_fw1(void)
292 {
293   /*
294    * Get handles for the Ethernet dissectors.
295    */
296   eth_handle = find_dissector("eth");
297 }