#include <string.h> and/or #include <stdio.h> not needed.
[obnox/wireshark/wip.git] / epan / dissectors / 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$
6  *
7  * Wireshark - 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 wireshark 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 <epan/prefs.h>
94 #include <epan/emem.h>
95 #include <epan/etypes.h>
96
97 /* Place FW1 summary in proto tree */
98 static gboolean fw1_summary_in_tree = TRUE;
99 static gboolean fw1_with_uuid = FALSE;
100 static gboolean fw1_iflist_with_chain = FALSE;
101
102 /* Initialize the protocol and registered fields */
103 static int proto_fw1 = -1;
104 static int hf_fw1_direction = -1;
105 static int hf_fw1_chain = -1;
106 static int hf_fw1_interface = -1;
107 static int hf_fw1_uuid = -1;
108 static int hf_fw1_type = -1;
109 static int hf_fw1_trailer = -1;
110
111 /* Initialize the subtree pointers */
112 static gint ett_fw1 = -1;
113
114 #define ETH_HEADER_SIZE 14
115
116 #define MAX_INTERFACES  20
117 static char     *p_interfaces[MAX_INTERFACES];
118 static int      interface_anzahl=0;
119
120 static void
121 fw1_init(void)
122 {
123   int           i;
124
125   for (i=0; i<interface_anzahl; i++) {
126     g_free(p_interfaces[i]);
127   }
128   interface_anzahl = 0;
129 }
130
131 static void
132 dissect_fw1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
133 {
134   /* Set up structures needed to add the protocol subtree and manage it */
135   proto_item    *ti;
136   proto_tree    *volatile fh_tree = NULL;
137   char          direction;
138   char  chain;
139   char          *interface_name;
140   guint32       iface_len = 10;
141   guint16       etype;
142   emem_strbuf_t *header;
143   int           i;
144   gboolean      found;
145   static const char     fw1_header[] = "FW1 Monitor";
146
147   header = ep_strbuf_new_label(fw1_header);
148
149   /* Make entries in Protocol column and Info column on summary display */
150   col_set_str(pinfo->cinfo, COL_PROTOCOL, "FW1");
151   col_clear(pinfo->cinfo, COL_INFO);
152
153
154   /* g_snprintf(header, sizeof(header), fw1_header); */
155
156   /* fetch info to local variable */
157   direction = tvb_get_guint8(tvb, 0);
158
159   if (!fw1_iflist_with_chain)
160         chain = ' ';
161   else
162         chain = tvb_get_guint8(tvb, 1);
163
164   if (fw1_with_uuid)
165         iface_len = 6;
166
167   interface_name=ep_alloc(iface_len+1);
168   tvb_get_nstringz0(tvb, 2, iface_len+1, interface_name);
169
170   /* Known interface name - if not, remember it */
171   found=FALSE;
172   for (i=0; i<interface_anzahl; i++) {
173     if ( strcmp(p_interfaces[i], interface_name) == 0 ) {
174       found=TRUE;
175       break;
176     }
177   }
178   if (!found && interface_anzahl < MAX_INTERFACES) {
179     p_interfaces[interface_anzahl] = g_strdup(interface_name);
180     interface_anzahl++;
181   }
182
183   /* display all interfaces always in the same order */
184   for (i=0; i<interface_anzahl; i++) {
185     if ( strcmp(p_interfaces[i], interface_name) == 0 ) {
186         ep_strbuf_append_printf(header, "  %c%c %s %c%c",
187                         direction == 'i' ? 'i' : (direction == 'O' ? 'O' : ' '),
188                         (direction == 'i' || direction == 'O') ? chain : ' ',
189                         p_interfaces[i],
190                         direction == 'I' ? 'I' : (direction == 'o' ? 'o' : ' '),
191                         (direction == 'I' || direction == 'o') ? chain : ' '
192                 );
193     } else {
194         ep_strbuf_append_printf(header, "    %s  ", p_interfaces[i]);
195     }
196   }
197
198   if (check_col(pinfo->cinfo, COL_IF_DIR))
199     col_add_str(pinfo->cinfo, COL_IF_DIR, header->str + sizeof(fw1_header) + 1);
200
201   if (tree) {
202     if (!fw1_summary_in_tree)
203       /* Do not show the summary in Protocol Tree */
204       ti = proto_tree_add_protocol_format(tree, proto_fw1, tvb, 0, ETH_HEADER_SIZE, "%s", fw1_header);
205     else
206       ti = proto_tree_add_protocol_format(tree, proto_fw1, tvb, 0, ETH_HEADER_SIZE, "%s", header->str);
207
208     /* create display subtree for the protocol */
209     fh_tree = proto_item_add_subtree(ti, ett_fw1);
210
211     proto_tree_add_item(fh_tree, hf_fw1_direction, tvb, 0, 1, FALSE);
212
213     if (fw1_iflist_with_chain)
214       proto_tree_add_item(fh_tree, hf_fw1_chain, tvb, 1, 1, FALSE);
215
216     proto_tree_add_item(fh_tree, hf_fw1_interface, tvb, 2, iface_len, FALSE);
217
218     if (fw1_with_uuid)
219       proto_tree_add_item(fh_tree, hf_fw1_uuid, tvb, 8, 4, FALSE);
220   }
221
222   etype = tvb_get_ntohs(tvb, 12);
223   ethertype(etype, tvb, ETH_HEADER_SIZE, pinfo, tree, fh_tree, hf_fw1_type, hf_fw1_trailer, 0);
224 }
225
226 void
227 proto_register_fw1(void)
228 {
229   static hf_register_info hf[] = {
230         { &hf_fw1_direction,
231         { "Direction",  "fw1.direction", FT_STRING, BASE_NONE, NULL, 0x0,
232                 NULL, HFILL }},
233         { &hf_fw1_chain,
234         { "Chain Position",     "fw1.chain", FT_STRING, BASE_NONE, NULL, 0x0,
235                 NULL, HFILL }},
236         { &hf_fw1_interface,
237         { "Interface",  "fw1.interface", FT_STRING, BASE_NONE, NULL, 0x0,
238                 NULL, HFILL }},
239         { &hf_fw1_uuid,
240         { "UUID",       "fw1.uuid", FT_UINT32, BASE_DEC, NULL, 0x0,
241                 NULL, HFILL }},
242                 /* registered here but handled in ethertype.c */
243         { &hf_fw1_type,
244         { "Type",               "fw1.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
245                 NULL, HFILL }},
246   };
247   /* Setup protocol subtree array */
248   static gint *ett[] = {
249         &ett_fw1,
250   };
251   module_t *fw1_module;
252
253   /* Register the protocol name and description */
254   proto_fw1 = proto_register_protocol("Checkpoint FW-1", "FW-1", "fw1");
255   /* Required function calls to register the header fields and subtrees used */
256   proto_register_field_array(proto_fw1, hf, array_length(hf));
257   proto_register_subtree_array(ett, array_length(ett));
258
259   /* Register configuration preferences */
260   fw1_module = prefs_register_protocol(proto_fw1, NULL);
261   prefs_register_bool_preference(fw1_module, "summary_in_tree",
262             "Show FireWall-1 summary in protocol tree",
263             "Whether the FireWall-1 summary line should be shown in the protocol tree",
264             &fw1_summary_in_tree);
265   prefs_register_bool_preference(fw1_module, "with_uuid",
266             "Monitor file includes UUID",
267             "Whether the Firewall-1 monitor file includes UUID information",
268             &fw1_with_uuid);
269   prefs_register_bool_preference(fw1_module, "iflist_with_chain",
270             "Interface list includes chain position",
271             "Whether the interface list includes the chain position",
272             &fw1_iflist_with_chain);
273
274   register_dissector("fw1", dissect_fw1, proto_fw1);
275
276   register_init_routine(fw1_init);
277 }