From Benjamin Meyer:
[obnox/wireshark/wip.git] / plugins / profinet / packet-pn-rt.c
1 /* packet-pn-rt.c
2  * Routines for pn-rt (PROFINET Real-Time) packet dissection.
3  * This is the base for other PROFINET protocols like IO, CBA, DCP, ...
4  * (the "content subdissectors" will register themselves using a heuristic)
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1999 Gerald Combs
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 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <gmodule.h>
42 #include <ctype.h>
43 #include <time.h>
44 #include <string.h>
45 #include <epan/packet.h>
46 #include <epan/addr_resolv.h>
47 #include <epan/prefs.h>
48 #include <epan/strutil.h>
49 #include <epan/etypes.h>
50 #include <epan/expert.h>
51 #include <epan/dissectors/packet-dcerpc.h>
52 #include <epan/crc16.h>
53
54 #include "packet-pn.h"
55
56 /* Define the pn-rt proto */
57 static int proto_pn_rt     = -1;
58
59 /* Define many header fields for pn-rt */
60 static int hf_pn_rt_frame_id = -1;
61 static int hf_pn_rt_cycle_counter = -1;
62 static int hf_pn_rt_transfer_status = -1;
63 static int hf_pn_rt_data_status = -1;
64 static int hf_pn_rt_data_status_ignore = -1;
65 static int hf_pn_rt_data_status_subframe_sender_mode = -1;
66 static int hf_pn_rt_data_status_ok = -1;
67 static int hf_pn_rt_data_status_operate = -1;
68 static int hf_pn_rt_data_status_res3 = -1;
69 static int hf_pn_rt_data_status_valid = -1;
70 static int hf_pn_rt_data_status_res1 = -1;
71 static int hf_pn_rt_data_status_primary = -1;
72
73 static int hf_pn_rt_sf_crc16 = -1;
74 static int hf_pn_rt_sf = -1;
75 static int hf_pn_rt_sf_position = -1;
76 static int hf_pn_rt_sf_position_control = -1;
77 static int hf_pn_rt_sf_data_length = -1;
78 static int hf_pn_rt_sf_cycle_counter = -1;
79
80 static int hf_pn_rt_frag = -1;
81 static int hf_pn_rt_frag_data_length = -1;
82 static int hf_pn_rt_frag_status = -1;
83 static int hf_pn_rt_frag_status_more_follows = -1;
84 static int hf_pn_rt_frag_status_error = -1;
85 static int hf_pn_rt_frag_status_fragment_number = -1;
86 static int hf_pn_rt_frag_data = -1;
87
88
89 /* 
90  * Define the trees for pn-rt
91  * We need one tree for pn-rt itself and one for the pn-rt data status subtree
92  */
93 static int ett_pn_rt = -1;
94 static int ett_pn_rt_data_status = -1;
95 static int ett_pn_rt_sf = -1;
96 static int ett_pn_rt_frag = -1;
97 static int ett_pn_rt_frag_status = -1;
98
99 /* 
100  * Here are the global variables associated with  
101  * the various user definable characteristics of the dissection
102  */
103 /* Place summary in proto tree */
104 static gboolean pn_rt_summary_in_tree = TRUE;
105
106 /* heuristic to find the right pn-rt payload dissector */
107 static heur_dissector_list_t heur_subdissector_list;
108
109
110 static const value_string pn_rt_position_control[] = {
111         { 0x00, "CRC16 and CycleCounter shall not be checked" },
112         { 0x80, "CRC16 and CycleCounter valid" },
113     { 0, NULL }
114 };
115
116 static const value_string pn_rt_frag_status_error[] = {
117         { 0x00, "No error" },
118         { 0x01, "An error occured, all earlier fragments shall be dropped" },
119     { 0, NULL }
120 };
121
122 static const value_string pn_rt_frag_status_more_follows[] = {
123         { 0x00, "Last fragment" },
124         { 0x01, "More fragments follow" },
125     { 0, NULL }
126 };
127
128
129
130 static void
131 dissect_DataStatus(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 u8DataStatus)
132 {
133     proto_item *sub_item;
134     proto_tree *sub_tree;
135
136     sub_item = proto_tree_add_uint_format(tree, hf_pn_rt_data_status, 
137             tvb, offset, 1, u8DataStatus,
138             "DataStatus: 0x%02x (Frame: %s and %s, Provider: %s and %s)", 
139             u8DataStatus, 
140             (u8DataStatus & 0x04) ? "Valid" : "Invalid",
141             (u8DataStatus & 0x01) ? "Primary" : "Backup",
142             (u8DataStatus & 0x20) ? "Ok" : "Problem",
143             (u8DataStatus & 0x10) ? "Run" : "Stop");
144     sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_data_status);
145     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_ignore, tvb, offset, 1, u8DataStatus);
146     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_subframe_sender_mode, tvb, offset, 1, u8DataStatus);
147     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_ok, tvb, offset, 1, u8DataStatus);
148     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_operate, tvb, offset, 1, u8DataStatus);
149     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_res3, tvb, offset, 1, u8DataStatus);
150     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_valid, tvb, offset, 1, u8DataStatus);
151     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_res1, tvb, offset, 1, u8DataStatus);
152     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_primary, tvb, offset, 1, u8DataStatus);
153 }
154
155
156 /* possibly dissect a CSF_SDU related PN-RT packet */
157 static gboolean
158 dissect_CSF_SDU_heur(tvbuff_t *tvb,
159         packet_info *pinfo, proto_tree *tree)
160 {
161         guint16 u16FrameID;
162         guint16 u16SFCRC16;
163         guint8  u8SFPosition;
164         guint8  u8SFDataLength = 255;
165         guint8  u8SFCycleCounter;
166         guint8  u8SFDataStatus;
167         int offset = 0;
168         guint32 u32SubStart;
169     proto_item *sub_item;
170     proto_tree *sub_tree;
171     proto_item *item;
172         guint16 crc;
173
174
175     /* the sub tvb will NOT contain the frame_id here! */
176     u16FrameID = GPOINTER_TO_UINT(pinfo->private_data);
177
178         /* possible FrameID ranges for DFP */
179         if ((u16FrameID >= 0x0500 && u16FrameID < 0x05ff) ||
180             (u16FrameID >= 0x0600 && u16FrameID < 0x07ff) ||
181             (u16FrameID >= 0x4800 && u16FrameID < 0x4fff) ||
182             (u16FrameID >= 0x5800 && u16FrameID < 0x5fff) ||
183             (u16FrameID >= 0x6800 && u16FrameID < 0x6fff) ||
184             (u16FrameID >= 0x7800 && u16FrameID < 0x7fff)) {
185                 /* can't check this CRC, as the checked data bytes are not available */
186                 u16SFCRC16 = tvb_get_letohs(tvb, offset);
187                 proto_tree_add_uint(tree, hf_pn_rt_sf_crc16, tvb, offset, 2, u16SFCRC16);
188                 offset += 2;
189
190                 while(1) {
191                         sub_item = proto_tree_add_item(tree, hf_pn_rt_sf, tvb, offset, 0, FALSE);
192                         sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_sf);
193                         u32SubStart = offset;
194
195                         u8SFPosition = tvb_get_guint8(tvb, offset);
196                         proto_tree_add_uint(sub_tree, hf_pn_rt_sf_position_control, tvb, offset, 1, u8SFPosition);
197                         proto_tree_add_uint(sub_tree, hf_pn_rt_sf_position, tvb, offset, 1, u8SFPosition);
198                         offset += 1;
199
200                         u8SFDataLength = tvb_get_guint8(tvb, offset);
201                         proto_tree_add_uint(sub_tree, hf_pn_rt_sf_data_length, tvb, offset, 1, u8SFDataLength);
202                         offset += 1;
203
204                         if(u8SFDataLength == 0) {
205                                 proto_item_append_text(sub_item, ": Pos:%u, Length:%u", u8SFPosition, u8SFDataLength);
206                                 proto_item_set_len(sub_item, offset - u32SubStart);
207                                 break;
208                         }
209
210                         u8SFCycleCounter = tvb_get_guint8(tvb, offset);
211                         proto_tree_add_uint(sub_tree, hf_pn_rt_sf_cycle_counter, tvb, offset, 1, u8SFCycleCounter);
212                         offset += 1;
213
214                         u8SFDataStatus = tvb_get_guint8(tvb, offset);
215                         dissect_DataStatus(tvb, offset, sub_tree, u8SFDataStatus);
216                         offset += 1;
217
218                         offset = dissect_pn_user_data(tvb, offset, pinfo, sub_tree, u8SFDataLength, "DataItem");
219
220                         u16SFCRC16 = tvb_get_letohs(tvb, offset);
221                         item = proto_tree_add_uint(sub_tree, hf_pn_rt_sf_crc16, tvb, offset, 2, u16SFCRC16);
222
223                         if(u8SFPosition & 0x80) {
224                                 crc = crc16_plain_tvb_offset(tvb, u32SubStart, offset-u32SubStart);
225                                 if(crc != u16SFCRC16) {
226                                         proto_item_append_text(item, " [Preliminary check: incorrect, should be: %u]", crc);
227                                         expert_add_info_format(pinfo, item, PI_CHECKSUM, PI_ERROR, "Bad checksum");
228                                 } else {
229                                         proto_item_append_text(item, " [Preliminary check: Correct]");
230                                 }
231                         } else {
232                                 proto_item_append_text(item, " [No preliminary check, Control bit not set]");
233                         }
234                         offset += 2;
235
236                         proto_item_append_text(sub_item, ": Pos:%u, Length:%u, Cycle:%u, Status: 0x%02x (%s,%s,%s,%s)",
237                                 u8SFPosition, u8SFDataLength, u8SFCycleCounter, u8SFDataStatus,
238                                 (u8SFDataStatus & 0x04) ? "Valid" : "Invalid",
239                                 (u8SFDataStatus & 0x01) ? "Primary" : "Backup",
240                                 (u8SFDataStatus & 0x20) ? "Ok" : "Problem",
241                                 (u8SFDataStatus & 0x10) ? "Run" : "Stop");
242
243                         proto_item_set_len(sub_item, offset - u32SubStart);
244                 }
245
246         return TRUE;
247     }
248
249     return FALSE;
250
251 }
252
253
254 /* possibly dissect a FRAG_PDU related PN-RT packet */
255 static gboolean
256 dissect_FRAG_PDU_heur(tvbuff_t *tvb,
257         packet_info *pinfo, proto_tree *tree)
258 {
259         guint16 u16FrameID;
260         int offset = 0;
261     proto_item *sub_item;
262     proto_tree *sub_tree;
263         guint8  u8FragDataLength;
264     proto_item *status_item;
265     proto_tree *status_tree;
266         guint8  u8FragStatus;
267
268
269     /* the sub tvb will NOT contain the frame_id here! */
270     u16FrameID = GPOINTER_TO_UINT(pinfo->private_data);
271
272         /* possible FrameID ranges for FRAG_PDU */
273         if (u16FrameID >= 0xFF80 && u16FrameID < 0xFF8F) {
274                 sub_item = proto_tree_add_item(tree, hf_pn_rt_frag, tvb, offset, 0, FALSE);
275                 sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_frag);
276
277                 u8FragDataLength = tvb_get_guint8(tvb, offset);
278                 proto_tree_add_uint(sub_tree, hf_pn_rt_frag_data_length, tvb, offset, 1, u8FragDataLength);
279                 offset += 1;
280
281                 status_item = proto_tree_add_item(sub_tree, hf_pn_rt_frag_status, tvb, offset, 1, FALSE);
282                 status_tree = proto_item_add_subtree(status_item, ett_pn_rt_frag_status);
283
284                 u8FragStatus = tvb_get_guint8(tvb, offset);
285                 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_more_follows, tvb, offset, 1, u8FragStatus);
286                 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_error, tvb, offset, 1, u8FragStatus);
287                 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_fragment_number, tvb, offset, 1, u8FragStatus);
288                 offset += 1;
289                 proto_item_append_text(status_item, ": Number: %u, %s, %s",
290                         u8FragStatus & 0x3F,
291                         val_to_str( (u8FragStatus & 0x80) >> 7, pn_rt_frag_status_more_follows, "Unknown"),
292                         val_to_str( (u8FragStatus & 0x40) >> 6, pn_rt_frag_status_error, "Unknown"));
293
294
295                 proto_tree_add_string_format(sub_tree, hf_pn_rt_frag_data, tvb, offset, tvb_length(tvb) - offset, "data", 
296                         "FragData: %d bytes", tvb_length(tvb) - offset);
297
298                 /* note: the actual defragmentation implementation is still missing here */
299                 dissect_pn_undecoded(tvb, offset, pinfo, sub_tree, tvb_length(tvb) - offset);
300
301         return TRUE;
302     }
303
304     return FALSE;
305
306 }
307
308
309 /*
310  * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
311  */
312 static void
313 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
314 {
315   gint tvb_len;
316   gint data_len;
317   guint16 u16FrameID;
318   guint8 u8DataStatus;
319   guint8 u8TransferStatus;
320   guint16 u16CycleCounter;
321   const gchar *pszProtAddInfo;
322   const gchar *pszProtShort;
323   const gchar *pszProtSummary;
324   const gchar *pszProtComment;
325   proto_tree *pn_rt_tree, *ti;
326   gchar szFieldSummary[100];
327   tvbuff_t *next_tvb;
328   gboolean  bCyclic;
329
330
331   /* The PN-RT protocol uses status values at the end of the Ethernet frame.
332    * Unfortunately it doesn't contain a length field in the PN-RT protocol itself,
333    * so we must depend on the tvb length. This is sometimes is a bit confusing
334    * whether the length of the tvb contains the optional FCS at the end or not
335    * therefore the following heuristic ... */
336
337   if(pinfo->fd->lnk_t == WTAP_ENCAP_IEEE_802_11_WITH_RADIO) {
338     /* 802.11: at least when using AiroPeek to capture,
339      * the 802.11 dissector already has stripped the FCS from the tvb.
340      * XXX - we might need to add other 802.11 encaps here as well */
341     tvb_len = tvb_length(tvb);
342   } else {
343         /* Ethernet: subtract (optional) FCS or trailer len
344      * (fcs_len -1 means we don't know if FCS is appended, we assume it's not) */
345     tvb_len = tvb_length(tvb) -
346           ((pinfo->pseudo_header->eth.fcs_len != -1) ? pinfo->pseudo_header->eth.fcs_len : 0);
347   }
348   tvb_set_reported_length(tvb, tvb_len);
349
350   /* Initialize variables */
351   pn_rt_tree = NULL;
352   ti = NULL;
353   
354   /*
355    * Set the columns now, so that they'll be set correctly if we throw
356    * an exception.  We can set them (or append things) later again ....
357    */
358   if (check_col(pinfo->cinfo, COL_PROTOCOL))
359     col_set_str(pinfo->cinfo, COL_PROTOCOL, "PN-RT");
360   if (check_col(pinfo->cinfo, COL_INFO))
361     col_set_str(pinfo->cinfo, COL_INFO, "PROFINET Real-Time");
362
363   if (tvb_len < 6) {
364     dissect_pn_malformed(tvb, 0, pinfo, tree, tvb_len);
365     return;
366   }
367
368
369     /* build some "raw" data */
370         u16FrameID = tvb_get_ntohs(tvb, 0);
371     if (u16FrameID <= 0x001F) {
372         pszProtShort    = "PN-RT";
373         pszProtAddInfo  = "reserved, ";
374         pszProtSummary  = "Real-Time";
375         pszProtComment  = "0x0000-0x001F: Reserved ID";
376         bCyclic         = FALSE;
377     } else if (u16FrameID <= 0x0021) {
378         pszProtShort    = "PN-PTCP";
379         pszProtAddInfo  = "Synchronization, ";
380         pszProtSummary  = "Real-Time";
381         pszProtComment  = "0x0020-0x0021: Real-Time: Sync (with follow up)";
382         bCyclic         = FALSE;
383     } else if (u16FrameID <= 0x007F) {
384         pszProtShort    = "PN-RT";
385         pszProtAddInfo  = "reserved, ";
386         pszProtSummary  = "Real-Time";
387         pszProtComment  = "0x0022-0x007F: Reserved ID";
388         bCyclic         = FALSE;
389     } else if (u16FrameID <= 0x0081) {
390         pszProtShort    = "PN-PTCP";
391         pszProtAddInfo  = "Synchronization, ";
392         pszProtSummary  = "Isochronous-Real-Time";
393         pszProtComment  = "0x0080-0x0081: Real-Time: Sync (without follow up)";
394         bCyclic         = FALSE;
395     } else if (u16FrameID <= 0x00FF) {
396         pszProtShort    = "PN-RT";
397         pszProtAddInfo  = "reserved, ";
398         pszProtSummary  = "Real-Time";
399         pszProtComment  = "0x0082-0x00FF: Reserved ID";
400         bCyclic         = FALSE;
401
402         } else if (u16FrameID <= 0x04FF){
403         pszProtShort    = "PN-RTC3";
404         pszProtAddInfo  = "RTC3, ";
405         pszProtSummary  = "Isochronous-Real-Time";
406         pszProtComment  = "0x0100-0x04FF: Isochronous-Real-Time(class=3): non redundant, normal";
407         bCyclic         = TRUE;
408     } else if (u16FrameID <= 0x05FF){
409         pszProtShort    = "PN-RTC3";
410         pszProtAddInfo  = "RTC3, ";
411         pszProtSummary  = "Isochronous-Real-Time";
412         pszProtComment  = "0x0500-0x05FF: Isochronous-Real-Time(class=3): non redundant, DFP";
413         bCyclic         = TRUE;
414     } else if (u16FrameID <= 0x07FF){
415         pszProtShort    = "PN-RTC3";
416         pszProtAddInfo  = "RTC3, ";
417         pszProtSummary  = "Isochronous-Real-Time";
418         pszProtComment  = "0x0600-0x07FF: Isochronous-Real-Time(class=3): redundant, DFP";
419         bCyclic         = TRUE;
420     } else if (u16FrameID <= 0x0FFF){
421         pszProtShort    = "PN-RTC3";
422         pszProtAddInfo  = "RTC3, ";
423         pszProtSummary  = "Isochronous-Real-Time";
424         pszProtComment  = "0x0800-0x0FFF: Isochronous-Real-Time(class=3): redundant, normal";
425         bCyclic         = TRUE;
426     } else if (u16FrameID <= 0x47FF) {
427         pszProtShort    = "PN-RT";
428         pszProtAddInfo  = "reserved, ";
429         pszProtSummary  = "Real-Time";
430         pszProtComment  = "0x1000-0x47FF: Reserved ID";
431         bCyclic         = FALSE;
432     } else if (u16FrameID <= 0x4FFF){
433         pszProtShort    = "PN-RTC2";
434         pszProtAddInfo  = "RTC2, ";
435         pszProtSummary  = "cyclic Real-Time";
436         pszProtComment  = "0x4800-0x4FFF: Real-Time(class=2): redundant, DFP";
437         bCyclic         = TRUE;
438     } else if (u16FrameID < 0x57FF){
439         pszProtShort    = "PN-RTC2";
440         pszProtAddInfo  = "RTC2, ";
441         pszProtSummary  = "cyclic Real-Time";
442         pszProtComment  = "0x5000-0x57FF: Real-Time(class=2): redundant, normal";
443         bCyclic         = TRUE;
444         } else if (u16FrameID <= 0x5FFF){
445         pszProtShort    = "PN-RTC2";
446         pszProtAddInfo  = "RTC2, ";
447         pszProtSummary  = "cyclic Real-Time";
448         pszProtComment  = "0x5800-0x5FFF: Real-Time(class=2): non redundant, DFP";
449         bCyclic         = TRUE;
450     } else if (u16FrameID <= 0x67FF){
451         pszProtShort    = "PN-RTC2";
452         pszProtAddInfo  = "RTC2, ";
453         pszProtSummary  = "cyclic Real-Time";
454         pszProtComment  = "0x6000-0x67FF: Real-Time(class=2): non redundant, normal";
455         bCyclic         = TRUE;
456         } else if (u16FrameID <= 0x6FFF){
457         pszProtShort    = "PN-RTC2";
458         pszProtAddInfo  = "RTC2, ";
459         pszProtSummary  = "cyclic Real-Time";
460         pszProtComment  = "0x6800-0x6FFF: Real-Time(class=2): redundant, DFP";
461         bCyclic         = TRUE;
462     } else if (u16FrameID <= 0x77FF){
463         pszProtShort    = "PN-RTC2";
464         pszProtAddInfo  = "RTC2, ";
465         pszProtSummary  = "cyclic Real-Time";
466         pszProtComment  = "0x7000-0x77FF: Real-Time(class=2): redundant, normal";
467         bCyclic         = TRUE;
468         } else if (u16FrameID <= 0x7FFF){
469         pszProtShort    = "PN-RTC2";
470         pszProtAddInfo  = "RTC2, ";
471         pszProtSummary  = "cyclic Real-Time";
472         pszProtComment  = "0x7800-0x7FFF: Real-Time(class=2): non redundant, DFP";
473         bCyclic         = TRUE;
474     } else if (u16FrameID <= 0xBBFF){
475         pszProtShort    = "PN-RTC2";
476         pszProtAddInfo  = "RTC2, ";
477         pszProtSummary  = "cyclic Real-Time";
478         pszProtComment  = "0x8000-0xBBFF: Real-Time(class=2): non redundant, normal";
479         bCyclic         = TRUE;
480     } else if (u16FrameID <= 0xBFFF){
481         pszProtShort    = "PN-RTC2";
482         pszProtAddInfo  = "RTC2, ";
483         pszProtSummary  = "cyclic Real-Time";
484         pszProtComment  = "0xBC00-0xBFFF: Real-Time(class=2 multicast): non redundant, normal";
485         bCyclic         = TRUE;
486         } else if (u16FrameID <= 0xF7FF){
487         pszProtShort    = "PN-RTC1/UDP";
488         pszProtAddInfo  = "RTC1/UDP, ";
489         pszProtSummary  = "cyclic Real-Time";
490         pszProtComment  = "0xC000-0xF7FF: Real-Time(class=1/UDP): Cyclic";
491         bCyclic         = TRUE;
492     } else if (u16FrameID <= 0xFBFF){
493         pszProtShort    = "PN-RTC1/UDP";
494         pszProtAddInfo  = "Multicast, ";
495         pszProtSummary  = "cyclic Real-Time";
496         pszProtComment  = "0xF800-0xFBFF: Real-Time(class=1/UDP multicast): Cyclic";
497         bCyclic         = TRUE;
498         } else if (u16FrameID <= 0xFDFF){
499         pszProtShort    = "PN-RTA";
500         pszProtAddInfo  = "Reserved, ";
501         pszProtSummary  = "acyclic Real-Time";
502         pszProtComment  = "0xFC00-0xFDFF: Reserved";
503         bCyclic         = FALSE;
504         if (u16FrameID == 0xfc01) {
505                         pszProtShort    = "PN-RTA";
506             pszProtAddInfo  = "Alarm High, ";
507                 pszProtSummary  = "acyclic Real-Time";
508                 pszProtComment  = "Real-Time: Acyclic PN-IO Alarm high priority";
509         }
510
511         } else if (u16FrameID <= 0xFEFF){
512         pszProtShort    = "PN-RTA";
513         pszProtAddInfo  = "Reserved, ";
514         pszProtSummary  = "acyclic Real-Time";
515         pszProtComment  = "0xFE00-0xFEFF: Real-Time: Reserved";
516         bCyclic         = FALSE;
517         if (u16FrameID == 0xFE01) {
518                         pszProtShort    = "PN-RTA";
519             pszProtAddInfo  = "Alarm Low, ";
520                 pszProtSummary  = "acyclic Real-Time";
521                 pszProtComment  = "Real-Time: Acyclic PN-IO Alarm low priority";
522         }
523         if (u16FrameID == FRAME_ID_DCP_HELLO) {
524                         pszProtShort    = "PN-RTA";
525             pszProtAddInfo  = "";
526                 pszProtSummary  = "acyclic Real-Time";
527                 pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) hello";
528         }
529         if (u16FrameID == FRAME_ID_DCP_GETORSET) {
530                         pszProtShort    = "PN-RTA";
531             pszProtAddInfo  = "";
532                 pszProtSummary  = "acyclic Real-Time";
533                 pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) get/set";
534         }
535         if (u16FrameID == FRAME_ID_DCP_IDENT_REQ) {
536                         pszProtShort    = "PN-RTA";
537             pszProtAddInfo  = "";
538                 pszProtSummary  = "acyclic Real-Time";
539                 pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) identify multicast request";
540         }
541         if (u16FrameID == FRAME_ID_DCP_IDENT_RES) {
542                         pszProtShort    = "PN-RTA";
543             pszProtAddInfo  = "";
544                 pszProtSummary  = "acyclic Real-Time";
545                 pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) identify response";
546         }
547     } else if (u16FrameID <= 0xFF01){
548                 pszProtShort    = "PN-PTCP";
549         pszProtAddInfo  = "RTA Sync, ";
550         pszProtSummary  = "acyclic Real-Time";
551         pszProtComment  = "0xFF00-0xFF01: PTCP Announce";
552         bCyclic         = FALSE;
553     } else if (u16FrameID <= 0xFF1F){
554                 pszProtShort    = "PN-PTCP";
555         pszProtAddInfo  = "RTA Sync, ";
556         pszProtSummary  = "acyclic Real-Time";
557         pszProtComment  = "0xFF02-0xFF1F: Reserved";
558         bCyclic         = FALSE;
559     } else if (u16FrameID <= 0xFF21){
560                 pszProtShort    = "PN-PTCP";
561         pszProtAddInfo  = "Follow Up, ";
562         pszProtSummary  = "acyclic Real-Time";
563         pszProtComment  = "0xFF20-0xFF21: PTCP Follow Up";
564         bCyclic         = FALSE;
565     } else if (u16FrameID <= 0xFF22){
566                 pszProtShort    = "PN-PTCP";
567         pszProtAddInfo  = "Follow Up, ";
568         pszProtSummary  = "acyclic Real-Time";
569         pszProtComment  = "0xFF22-0xFF3F: Reserved";
570         bCyclic         = FALSE;
571     } else if (u16FrameID <= 0xFF43){
572                 pszProtShort    = "PN-PTCP";
573         pszProtAddInfo  = "Delay, ";
574         pszProtSummary  = "acyclic Real-Time";
575         pszProtComment  = "0xFF40-0xFF43: Acyclic Real-Time: Delay";
576         bCyclic         = FALSE;
577     } else if (u16FrameID <= 0xFF7F){
578                 pszProtShort    = "PN-RT";
579         pszProtAddInfo  = "Reserved, ";
580         pszProtSummary  = "Real-Time";
581         pszProtComment  = "0xFF44-0xFF7F: reserved ID";
582         bCyclic         = FALSE;
583     } else if (u16FrameID <= 0xFF8F){
584                 pszProtShort    = "PN-RT";
585         pszProtAddInfo  = "Fragmentation, ";
586         pszProtSummary  = "Real-Time";
587         pszProtComment  = "0xFF80-0xFF8F: Fragmentation";
588         bCyclic         = FALSE;
589     } else {
590                 pszProtShort    = "PN-RT";
591         pszProtAddInfo  = "Reserved, ";
592         pszProtSummary  = "Real-Time";
593         pszProtComment  = "0xFF90-0xFFFF: reserved ID";
594         bCyclic         = FALSE;
595         }
596
597     /* decode optional cyclic fields at the packet end and build the summary line */
598     if (bCyclic) {
599         /* cyclic transfer has cycle counter, data status and transfer status fields at the end */
600                 u16CycleCounter = tvb_get_ntohs(tvb, tvb_len - 4);
601                 u8DataStatus = tvb_get_guint8(tvb, tvb_len - 2);
602             u8TransferStatus = tvb_get_guint8(tvb, tvb_len - 1);
603
604                 g_snprintf (szFieldSummary, sizeof(szFieldSummary),
605                                   "%sID:0x%04x, Len:%4u, Cycle:%5u (%s,%s,%s,%s)",
606                                 pszProtAddInfo, u16FrameID, tvb_len - 2 - 4, u16CycleCounter,
607                             (u8DataStatus & 0x04) ? "Valid" : "Invalid",
608                             (u8DataStatus & 0x01) ? "Primary" : "Backup",
609                             (u8DataStatus & 0x20) ? "Ok" : "Problem",
610                             (u8DataStatus & 0x10) ? "Run" : "Stop");
611
612         /* user data length is packet len - frame id - optional cyclic status fields */
613         data_len = tvb_len - 2 - 4;
614     } else {
615         /* satisfy the gcc compiler, so it won't throw an "uninitialized" warning */
616                 u16CycleCounter     = 0;
617                 u8DataStatus        = 0;
618             u8TransferStatus    = 0;
619
620         /* acyclic transfer has no fields at the end */
621                 g_snprintf (szFieldSummary, sizeof(szFieldSummary),
622                                   "%sID:0x%04x, Len:%4u",
623                                 pszProtAddInfo, u16FrameID, tvb_len - 2);
624
625         /* user data length is packet len - frame id field */
626         data_len = tvb_len - 2;
627     }
628
629     /* build protocol tree only, if tree is really used */
630     if (tree) {
631                 /* build pn_rt protocol tree with summary line */
632             if (pn_rt_summary_in_tree) {
633               ti = proto_tree_add_protocol_format(tree, proto_pn_rt, tvb, 0, tvb_len,
634                                 "PROFINET %s, %s", pszProtSummary, szFieldSummary);
635             } else {
636                         ti = proto_tree_add_item(tree, proto_pn_rt, tvb, 0, tvb_len, FALSE);
637             }
638                 pn_rt_tree = proto_item_add_subtree(ti, ett_pn_rt);
639
640                 /* add frame ID */
641         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_frame_id, tvb,
642           0, 2, u16FrameID, "FrameID: 0x%04x (%s)", u16FrameID, pszProtComment);
643
644         if (bCyclic) {
645                     /* add cycle counter */
646             proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_cycle_counter, tvb,
647               tvb_len - 4, 2, u16CycleCounter, "CycleCounter: %u", u16CycleCounter);
648                     
649             /* add data status subtree */
650                         dissect_DataStatus(tvb, tvb_len - 2, tree, u8DataStatus);
651
652                 /* add transfer status */
653                 if (u8TransferStatus) {
654                         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
655                             tvb_len - 1, 1, u8TransferStatus, 
656                                     "TransferStatus: 0x%02x (ignore this frame)", u8TransferStatus);
657                     } else {
658                         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
659                             tvb_len - 1, 1, u8TransferStatus, 
660                                     "TransferStatus: 0x%02x (OK)", u8TransferStatus);
661                     }
662         }
663     }
664                 
665         /* update column info now */
666     if (check_col(pinfo->cinfo, COL_INFO))
667       col_add_str(pinfo->cinfo, COL_INFO, szFieldSummary);
668         if (check_col(pinfo->cinfo, COL_PROTOCOL))
669             col_add_str(pinfo->cinfo, COL_PROTOCOL, pszProtShort);
670
671     pinfo->private_data = GUINT_TO_POINTER( (guint32) u16FrameID);
672
673         /* get frame user data tvb (without header and footer) */
674         next_tvb = tvb_new_subset(tvb, 2, data_len, data_len);
675
676     /* ask heuristics, if some sub-dissector is interested in this packet payload */
677     if(!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree)) {
678         /*if (check_col(pinfo->cinfo, COL_INFO))
679               col_set_str(pinfo->cinfo, COL_INFO, "Unknown");*/
680
681         /* Oh, well, we don't know this; dissect it as data. */
682         dissect_pn_undecoded(next_tvb, 0, pinfo, tree, tvb_length(next_tvb));
683     }
684 }
685
686
687 /* Register all the bits needed by the filtering engine */
688 void 
689 proto_register_pn_rt(void)
690 {
691   static hf_register_info hf[] = {
692     { &hf_pn_rt_frame_id,
693       { "FrameID", "pn_rt.frame_id", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
694         { &hf_pn_rt_cycle_counter, { 
695                 "CycleCounter", "pn_rt.cycle_counter", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
696         { &hf_pn_rt_data_status, { 
697                 "DataStatus", "pn_rt.ds", FT_UINT8, BASE_HEX, 0, 0x0, NULL, HFILL }},
698         { &hf_pn_rt_data_status_ignore, { 
699                 "Ignore (1:Ignore/0:Evaluate)", "pn_rt.ds_ignore", FT_UINT8, BASE_HEX, 0, 0x80, NULL, HFILL }},
700         { &hf_pn_rt_data_status_subframe_sender_mode, { 
701                 "SubFrameSenderMode", "pn_rt.ds_subframe_sender_mode", FT_UINT8, BASE_HEX, 0, 0x40, NULL, HFILL }},
702         { &hf_pn_rt_data_status_ok, { 
703                 "StationProblemIndicator (1:Ok/0:Problem)", "pn_rt.ds_ok", FT_UINT8, BASE_HEX, 0, 0x20, NULL, HFILL }},
704         { &hf_pn_rt_data_status_operate, { 
705                 "ProviderState (1:Run/0:Stop)", "pn_rt.ds_operate", FT_UINT8, BASE_HEX, 0, 0x10, NULL, HFILL }},
706         { &hf_pn_rt_data_status_res3, { 
707                 "Reserved (should be zero)", "pn_rt.ds_res3", FT_UINT8, BASE_HEX, 0, 0x08, NULL, HFILL }},
708         { &hf_pn_rt_data_status_valid, { 
709                 "DataValid (1:Valid/0:Invalid)", "pn_rt.ds_valid", FT_UINT8, BASE_HEX, 0, 0x04, NULL, HFILL }},
710         { &hf_pn_rt_data_status_res1, { 
711                 "Reserved (should be zero)", "pn_rt.ds_res1", FT_UINT8, BASE_HEX, 0, 0x02, NULL, HFILL }},
712         { &hf_pn_rt_data_status_primary, { 
713                 "State (1:Primary/0:Backup)", "pn_rt.ds_primary", FT_UINT8, BASE_HEX, 0, 0x01, NULL, HFILL }},
714     { &hf_pn_rt_transfer_status,
715       { "TransferStatus", "pn_rt.transfer_status", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
716         { &hf_pn_rt_sf, { 
717                 "SubFrame", "pn_rt.sf", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
718         { &hf_pn_rt_sf_crc16, { 
719                 "CRC16", "pn_rt.sf.crc16", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
720         { &hf_pn_rt_sf_position, { 
721                 "Position", "pn_rt.sf.position", FT_UINT8, BASE_DEC, NULL, 0x7F, NULL, HFILL }},
722         { &hf_pn_rt_sf_position_control, { 
723                 "Control", "pn_rt.sf.position_control", FT_UINT8, BASE_DEC, VALS(pn_rt_position_control), 0x80, NULL, HFILL }},
724         { &hf_pn_rt_sf_data_length, { 
725                 "DataLength", "pn_rt.sf.data_length", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
726         { &hf_pn_rt_sf_cycle_counter, { 
727                 "CycleCounter", "pn_rt.sf.cycle_counter", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
728         { &hf_pn_rt_frag, { 
729                 "PROFINET Real-Time Fragment", "pn_rt.frag", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
730         { &hf_pn_rt_frag_data_length, { 
731                 "FragDataLength", "pn_rt.frag_data_length", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
732         { &hf_pn_rt_frag_status, { 
733                 "FragStatus", "pn_rt.frag_status", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
734         { &hf_pn_rt_frag_status_more_follows, { 
735                 "MoreFollows", "pn_rt.frag_status.more_follows", FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_more_follows), 0x80, NULL, HFILL }},
736         { &hf_pn_rt_frag_status_error, { 
737                 "Error", "pn_rt.frag_status.error", FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_error), 0x40, NULL, HFILL }},
738         { &hf_pn_rt_frag_status_fragment_number, { 
739                 "FragmentNumber (zero based)", "pn_rt.frag_status.fragment_number", FT_UINT8, BASE_DEC, NULL, 0x3F, NULL, HFILL }},
740         { &hf_pn_rt_frag_data, { 
741                 "FragData", "pn_rt.frag_data", FT_STRING, BASE_NONE, NULL, 0x00, NULL, HFILL }},
742   };
743   static gint *ett[] = {
744     &ett_pn_rt,
745     &ett_pn_rt_data_status,
746         &ett_pn_rt_sf,
747         &ett_pn_rt_frag,
748         &ett_pn_rt_frag_status
749   };
750   module_t *pn_rt_module; 
751
752   proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
753                                        "PN-RT", "pn_rt");
754
755   proto_register_field_array(proto_pn_rt, hf, array_length(hf));
756   proto_register_subtree_array(ett, array_length(ett));
757
758   /* Register our configuration options */
759
760   pn_rt_module = prefs_register_protocol(proto_pn_rt, NULL);
761
762   prefs_register_bool_preference(pn_rt_module, "summary_in_tree",
763             "Show PN-RT summary in protocol tree",
764             "Whether the PN-RT summary line should be shown in the protocol tree",
765             &pn_rt_summary_in_tree);
766
767   /* register heuristics anchor for payload dissectors */
768   register_heur_dissector_list("pn_rt", &heur_subdissector_list);
769
770   init_pn (proto_pn_rt);
771 }
772
773
774 /* The registration hand-off routine is called at startup */
775 void
776 proto_reg_handoff_pn_rt(void)
777 {
778   dissector_handle_t pn_rt_handle;
779
780   pn_rt_handle = create_dissector_handle(dissect_pn_rt, proto_pn_rt);
781
782   dissector_add("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
783   dissector_add("udp.port", 0x8892, pn_rt_handle);
784
785   heur_dissector_add("pn_rt", dissect_CSF_SDU_heur, proto_pn_rt);
786   heur_dissector_add("pn_rt", dissect_FRAG_PDU_heur, proto_pn_rt);  
787 }
788