From Tobias Klauser:
[obnox/wireshark/wip.git] / epan / dissectors / packet-prp.c
1 /* packet-prp.c
2  * Routines for PRP (Parallel Redundancy Protocol; IEC62439 Part 3) dissection
3  * Copyright 2007, Sven Meier <msv[AT]zhwin.ch>
4  *
5  * $Id$
6  *
7  * Revisions:
8  * -
9  *
10  * A plugin for:
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald[AT]wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/etypes.h>
39 #include <epan/prefs.h>
40
41 /**********************************************************/
42 /* Offsets of fields within a PRP packet.          */
43 /**********************************************************/
44 #define    PRP_VERSION_OFFSET                      0
45 #define    PRP_TYPE_OFFSET                         2
46 #define    PRP_LENGTH_OFFSET                       3
47 #define    PRP_SOURCEMACADDRESSA_OFFSET            4
48 #define    PRP_SOURCEMACADDRESSB_OFFSET            10
49 #define    PRP_TYPE2_OFFSET                        16
50 #define    PRP_LENGTH2_OFFSET                      17
51 #define    PRP_REDBOXVDANMACADDRESS_OFFSET         18
52
53 /**********************************************************/
54 /* Lengths of fields within a PRP packet.          */
55 /**********************************************************/
56 #define    PRP_VERSION_LENGTH                      2
57 #define    PRP_TYPE_LENGTH                         1
58 #define    PRP_LENGTH_LENGTH                       1
59 #define    PRP_SOURCE_LENGTH                       6
60 #define    PRP_TOTAL_LENGTH                        24
61
62 /**********************************************************/
63 /* Channel values for the PRP_TYPE field          */
64 /**********************************************************/
65 #define    PRP_TYPE_DUPLICATE_ACCEPT               21
66 #define    PRP_TYPE_DUPLICATE_DISCARD              20
67 #define    PRP_TYPE_REDBOX                         30
68 #define    PRP_TYPE_VDAN                           31
69
70 static const value_string prp_type_vals[] = {
71   {PRP_TYPE_DUPLICATE_ACCEPT,     "Duplicate Accept"},
72   {PRP_TYPE_DUPLICATE_DISCARD,    "Duplicate Discard"},
73   {PRP_TYPE_REDBOX,               "Redundancy Box"},
74   {PRP_TYPE_VDAN,                 "Virtual Dual Attached Node"},
75   {0,                NULL          } };
76
77
78 #define    PRP_LAN_A                               10
79 #define    PRP_LAN_B                               11
80
81 static const value_string prp_lan_vals[] = {
82   {PRP_LAN_A,    "LAN A"},
83   {PRP_LAN_B,    "LAN B"},
84   {0,        NULL } };
85
86 /**********************************************************/
87 /* Initialize the protocol and registered fields      */
88 /**********************************************************/
89
90 void proto_reg_handoff_prp(void);
91 static int proto_prp = -1;
92 static module_t *prp_module;
93
94 /* Initialize supervision frame fields */
95 static int hf_prp_supervision_frame_version = -1;
96 static int hf_prp_supervision_frame_type = -1;
97 static int hf_prp_supervision_frame_length = -1;
98 static int hf_prp_supervision_frame_source_mac_address_A = -1;
99 static int hf_prp_supervision_frame_source_mac_address_B = -1;
100 static int hf_prp_supervision_frame_type2 = -1;
101 static int hf_prp_supervision_frame_length2 = -1;
102 static int hf_prp_supervision_frame_red_box_mac_address = -1;
103 static int hf_prp_supervision_frame_vdan_mac_address = -1;
104
105 /* Initialize trailer fields */
106 static int hf_prp_redundancy_control_trailer_sequence_nr = -1;
107 static int hf_prp_redundancy_control_trailer_lan = -1;
108 static int hf_prp_redundancy_control_trailer_size = -1;
109
110
111 /* Initialize the subtree pointers */
112 static gint ett_prp_supervision_frame = -1;
113 static gint ett_prp_redundancy_control_trailer = -1;
114
115
116 /*  Post dissectors (such as the trailer dissector for this protocol)
117  *  get called for every single frame anyone loads into Wireshark.
118  *  Since this protocol is not of general interest we disable this
119  *  protocol by default.
120  *
121  *  This is done separately from the disabled protocols list mainly so
122  *  we can disable it by default.  XXX Maybe there's a better way.
123  */
124 static gboolean prp_enable_dissector = FALSE;
125
126
127 /* Code to actually dissect the packets */
128 static void
129 dissect_prp_supervision_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
130 {
131     proto_item *ti;
132     proto_tree *prp_tree;
133     guint16 tlv2;
134
135     col_set_str(pinfo->cinfo, COL_PROTOCOL, "PRP");
136
137     col_set_str(pinfo->cinfo, COL_INFO, "Supervision Frame");
138
139     if (!tree)
140         return;
141
142     /* create display subtree for the protocol */
143     ti = proto_tree_add_item(tree, proto_prp, tvb, 0, PRP_TOTAL_LENGTH,
144                  FALSE);
145
146     prp_tree = proto_item_add_subtree(ti, ett_prp_supervision_frame);
147
148     proto_tree_add_item(prp_tree, hf_prp_supervision_frame_version,
149                         tvb, PRP_VERSION_OFFSET, PRP_VERSION_LENGTH, FALSE);
150
151     proto_tree_add_item(prp_tree, hf_prp_supervision_frame_type,
152                         tvb, PRP_TYPE_OFFSET, PRP_TYPE_LENGTH, FALSE);
153
154     proto_tree_add_item(prp_tree, hf_prp_supervision_frame_length,
155                         tvb, PRP_LENGTH_OFFSET, PRP_LENGTH_LENGTH, FALSE);
156
157     proto_tree_add_item(prp_tree, hf_prp_supervision_frame_source_mac_address_A,
158                         tvb, PRP_SOURCEMACADDRESSA_OFFSET, PRP_SOURCE_LENGTH,
159                        FALSE);
160
161     proto_tree_add_item(prp_tree, hf_prp_supervision_frame_source_mac_address_B,
162                         tvb, PRP_SOURCEMACADDRESSB_OFFSET, PRP_SOURCE_LENGTH,
163                         FALSE);
164
165
166     tlv2 = tvb_get_ntohs(tvb, PRP_TYPE2_OFFSET);
167
168     if((tlv2 == 0x1e06) || (tlv2 == 0x1f06))
169     {
170         proto_tree_add_item(prp_tree, hf_prp_supervision_frame_type2,
171                             tvb, PRP_TYPE2_OFFSET, PRP_TYPE_LENGTH, FALSE);
172
173         proto_tree_add_item(prp_tree, hf_prp_supervision_frame_length2,
174                             tvb, PRP_LENGTH2_OFFSET, PRP_LENGTH_LENGTH, FALSE);
175
176         if(tlv2 == 0x1e06)
177         {
178             proto_tree_add_item(prp_tree, hf_prp_supervision_frame_red_box_mac_address,
179                                 tvb, PRP_REDBOXVDANMACADDRESS_OFFSET, PRP_SOURCE_LENGTH,
180                                 FALSE);
181         }
182         else
183         {
184             proto_tree_add_item(prp_tree, hf_prp_supervision_frame_vdan_mac_address,
185                                 tvb, PRP_REDBOXVDANMACADDRESS_OFFSET, PRP_SOURCE_LENGTH,
186                                 FALSE);
187         }
188
189      }
190 }
191
192 static void
193 dissect_prp_redundancy_control_trailer(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
194 {
195     proto_item *ti;
196     proto_tree *prp_tree;
197     guint i;
198     guint length;
199     guint offset;
200     guint16 lan_size;
201     guint trailer_offset;
202
203     if (!tree)
204         return;
205
206     trailer_offset = 0;
207     length = tvb_reported_length(tvb);
208
209     if(length < 14)
210     {
211         return;
212     }
213
214     if(ETHERTYPE_VLAN == tvb_get_ntohs(tvb, 12)) /* tagged frame */
215     {
216         offset = 18;
217     }
218     else /* untagged */
219     {
220         offset = 14;
221     }
222
223     if(length <= 64)
224     {
225         for(i=length; i>=(offset+4); i--)  /* search trailer */
226         {
227             lan_size = tvb_get_ntohs(tvb, (i-2));
228             if((lan_size == (0xa000 | ((i-offset) & 0x0fff)))
229                || (lan_size == (0xb000 | ((i-offset) & 0x0fff))))
230             {
231                 trailer_offset = i;
232             }
233         }
234     }
235     else if(length > 64)
236     {
237         lan_size = tvb_get_ntohs(tvb, (length-2));
238         if((lan_size == (0xa000 | ((length-offset) & 0x0fff)))
239             || (lan_size == (0xb000 | ((length-offset) & 0x0fff))))
240         {
241             trailer_offset = length;
242         }
243     }
244
245     if(trailer_offset != 0)
246     {
247         /* create display subtree for the protocol */
248         ti = proto_tree_add_item(tree, proto_prp, tvb, trailer_offset - 4,
249                      trailer_offset, FALSE);
250
251         prp_tree = proto_item_add_subtree(ti, ett_prp_redundancy_control_trailer);
252
253         proto_tree_add_item(prp_tree, hf_prp_redundancy_control_trailer_sequence_nr,
254                             tvb, (trailer_offset-4), 2, FALSE);
255
256         proto_tree_add_item(prp_tree, hf_prp_redundancy_control_trailer_lan,
257                             tvb, (trailer_offset-2), 2, FALSE);
258
259         proto_tree_add_item(prp_tree, hf_prp_redundancy_control_trailer_size,
260                             tvb, (trailer_offset-2), 2, FALSE);
261     }
262 }
263
264 /* Register the protocol with Wireshark */
265 void proto_register_prp(void)
266 {
267     static hf_register_info hf[] = {
268
269         /*supervision frame*/
270         { &hf_prp_supervision_frame_version,
271             { "version", "prp.supervision_frame.version",
272             FT_UINT16, BASE_DEC, NULL, 0x00,
273             NULL, HFILL }
274         },
275         { &hf_prp_supervision_frame_type,
276             { "type", "prp.supervision_frame.type",
277             FT_UINT8, BASE_DEC, VALS(prp_type_vals), 0x00,
278             NULL, HFILL }
279         },
280         { &hf_prp_supervision_frame_length,
281             { "length", "prp.supervision_frame.length",
282             FT_UINT8, BASE_DEC, NULL, 0x00,
283             NULL, HFILL }
284         },
285         { &hf_prp_supervision_frame_source_mac_address_A,
286             { "sourceMacAddressA", "prp.supervision_frame.prp_source_mac_address_A",
287             FT_ETHER, BASE_NONE, NULL, 0x00,
288             NULL, HFILL }
289         },
290         { &hf_prp_supervision_frame_source_mac_address_B,
291             { "sourceMacAddressB", "prp.supervision_frame.prp_source_mac_address_B",
292             FT_ETHER, BASE_NONE, NULL, 0x00,
293             NULL, HFILL }
294         },
295         { &hf_prp_supervision_frame_type2,
296             { "type2", "prp.supervision_frame.type2",
297             FT_UINT8, BASE_DEC, VALS(prp_type_vals), 0x00,
298             NULL, HFILL }
299         },
300         { &hf_prp_supervision_frame_length2,
301             { "length2", "prp.supervision_frame.length2",
302             FT_UINT8, BASE_DEC, NULL, 0x00,
303             NULL, HFILL }
304         },
305         { &hf_prp_supervision_frame_red_box_mac_address,
306             { "redBoxMacAddress", "prp.supervision_frame.prp_red_box_mac_address",
307             FT_ETHER, BASE_NONE, NULL, 0x00,
308             NULL, HFILL }
309         },
310         { &hf_prp_supervision_frame_vdan_mac_address,
311             { "vdanMacAddress", "prp.supervision_frame.prp_vdan_mac_address",
312             FT_ETHER, BASE_NONE, NULL, 0x00,
313             NULL, HFILL }
314         },
315
316         /*trailer*/
317         { &hf_prp_redundancy_control_trailer_sequence_nr,
318             { "sequenceNr", "prp.trailer.prp_sequence_nr",
319             FT_UINT16, BASE_DEC, NULL, 0x00,
320             NULL, HFILL }
321         },
322
323         { &hf_prp_redundancy_control_trailer_lan,
324             { "lan", "prp.trailer.prp_lan",
325             FT_UINT16, BASE_DEC, VALS(prp_lan_vals), 0xf000,
326             NULL, HFILL }
327         },
328
329         { &hf_prp_redundancy_control_trailer_size,
330             { "size", "prp.trailer.prp_size",
331             FT_UINT16, BASE_DEC, NULL, 0x0fff,
332             NULL, HFILL }
333         }
334     };
335
336     static gint *ett[] = {
337         &ett_prp_supervision_frame,
338         &ett_prp_redundancy_control_trailer,
339     };
340
341
342     /* Register the protocol name and description */
343     proto_prp = proto_register_protocol("Parallel Redundancy Protocol (IEC62439 Part 3)",
344                         "PRP", "prp");
345     prp_module = prefs_register_protocol(proto_prp, proto_reg_handoff_prp);
346
347     prefs_register_bool_preference(prp_module, "enable", "Enable dissector",
348                        "Enable this dissector (default is false)",
349                        &prp_enable_dissector);
350
351     /* Required function calls to register the header fields and subtree used */
352     proto_register_field_array(proto_prp, hf, array_length(hf));
353     proto_register_subtree_array(ett, array_length(ett));
354
355 }
356
357 void proto_reg_handoff_prp(void)
358 {
359     static gboolean prefs_initialized = FALSE;
360
361     if (!prefs_initialized) {
362         dissector_handle_t prp_supervision_frame_handle;
363         dissector_handle_t prp_redundancy_control_trailer_handle;
364
365         prp_supervision_frame_handle = create_dissector_handle(dissect_prp_supervision_frame, proto_prp);
366         dissector_add_uint("ethertype", ETHERTYPE_PRP, prp_supervision_frame_handle);
367
368         prp_redundancy_control_trailer_handle = create_dissector_handle(dissect_prp_redundancy_control_trailer, proto_prp);
369         register_postdissector(prp_redundancy_control_trailer_handle);
370
371         prefs_initialized = TRUE;
372     }
373
374       proto_set_decoding(proto_prp, prp_enable_dissector);
375 }