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  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1999 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include <epan/reassemble.h>
17 #include <epan/prefs.h>
18 #include <epan/etypes.h>
19 #include <epan/expert.h>
20 #include <epan/crc16-tvb.h>
21 #include <epan/dissectors/packet-dcerpc.h>
22 
23 #include <wsutil/crc16-plain.h>
24 #include "packet-pn.h"
25 
26 
27 void proto_register_pn_rt(void);
28 void proto_reg_handoff_pn_rt(void);
29 
30 #define PROFINET_UDP_PORT 0x8892
31 
32 /* Define the pn-rt proto */
33 static int proto_pn_rt     = -1;
34 static gboolean pnio_desegment = TRUE;
35 
36 /* Define many header fields for pn-rt */
37 static int hf_pn_rt_frame_id = -1;
38 static int hf_pn_rt_cycle_counter = -1;
39 static int hf_pn_rt_transfer_status = -1;
40 static int hf_pn_rt_data_status = -1;
41 static int hf_pn_rt_data_status_ignore = -1;
42 static int hf_pn_rt_frame_info_type = -1;
43 static int hf_pn_rt_frame_info_function_meaning_input_conv = -1;
44 static int hf_pn_rt_frame_info_function_meaning_output_conv = -1;
45 static int hf_pn_rt_data_status_Reserved_2 = -1;
46 static int hf_pn_rt_data_status_ok = -1;
47 static int hf_pn_rt_data_status_operate = -1;
48 static int hf_pn_rt_data_status_res3 = -1;
49 static int hf_pn_rt_data_status_valid = -1;
50 static int hf_pn_rt_data_status_redundancy = -1;
51 static int hf_pn_rt_data_status_redundancy_output_cr = -1;
52 static int hf_pn_rt_data_status_redundancy_input_cr_state_is_backup = -1;
53 static int hf_pn_rt_data_status_redundancy_input_cr_state_is_primary = -1;
54 static int hf_pn_rt_data_status_primary = -1;
55 
56 static int hf_pn_rt_sf_crc16 = -1;
57 static int hf_pn_rt_sf_crc16_status = -1;
58 static int hf_pn_rt_sf = -1;
59 static int hf_pn_rt_sf_position = -1;
60 /* static int hf_pn_rt_sf_position_control = -1; */
61 static int hf_pn_rt_sf_data_length = -1;
62 static int hf_pn_rt_sf_cycle_counter = -1;
63 
64 static int hf_pn_rt_frag = -1;
65 static int hf_pn_rt_frag_data_length = -1;
66 static int hf_pn_rt_frag_status = -1;
67 static int hf_pn_rt_frag_status_more_follows = -1;
68 static int hf_pn_rt_frag_status_error = -1;
69 static int hf_pn_rt_frag_status_fragment_number = -1;
70 static int hf_pn_rt_frag_data = -1;
71 
72 
73 /*
74  * Define the trees for pn-rt
75  * We need one tree for pn-rt itself and one for the pn-rt data status subtree
76  */
77 static int ett_pn_rt = -1;
78 static int ett_pn_rt_data_status = -1;
79 static int ett_pn_rt_sf = -1;
80 static int ett_pn_rt_frag = -1;
81 static int ett_pn_rt_frag_status = -1;
82 
83 static expert_field ei_pn_rt_sf_crc16 = EI_INIT;
84 
85 /*
86  * Here are the global variables associated with
87  * the various user definable characteristics of the dissection
88  */
89 /* Place summary in proto tree */
90 static gboolean pn_rt_summary_in_tree = TRUE;
91 
92 /* heuristic to find the right pn-rt payload dissector */
93 static heur_dissector_list_t heur_subdissector_list;
94 
95 
96 #if 0
97 static const value_string pn_rt_position_control[] = {
98     { 0x00, "CRC16 and CycleCounter shall not be checked" },
99     { 0x80, "CRC16 and CycleCounter valid" },
100     { 0, NULL }
101 };
102 #endif
103 
104 static const true_false_string tfs_pn_rt_ds_redundancy_output_cr =
105     { "Unknown", "Redundancy has no meaning for OutputCRs, it is set to the fixed value of zero" };
106 
107 static const true_false_string tfs_pn_rt_ds_redundancy_input_cr_state_is_backup =
108     { "None primary AR of a given AR-set is present", "Default - One primary AR of a given AR-set is present" };
109 
110 static const true_false_string tfs_pn_rt_ds_redundancy_input_cr_state_is_primary =
111     { "The ARState from the IO device point of view is Backup", "Default - The ARState from the IO device point of view is Primary" };
112 
113 static const value_string pn_rt_frame_info_function_meaning_input_conv[] = {
114     {0x00, "Backup Acknowledge without actual data" },
115     {0x02, "Primary Missing without actual data" },
116     {0x04, "Backup Acknowledge with actual data independent from the Arstate" },
117     {0x05, "Primary Acknowledge"},
118     {0x06, "Primary Missing with actual data independent from the Arstate" },
119     {0x07, "Primary Fault" },
120     {0, NULL}
121 };
122 
123 static const value_string pn_rt_frame_info_function_meaning_output_conv[] = {
124     { 0x04, "Backup Request" },
125     { 0x05, "Primary Request" },
126     { 0, NULL }
127 };
128 
129 static const true_false_string tfs_pn_rt_ds_redundancy =
130     { "None primary AR of a given AR-set is present",  "Redundancy has no meaning for OutputCRs / One primary AR of a given AR-set is present" };
131 
132 static const value_string pn_rt_frag_status_error[] = {
133     { 0x00, "reserved" },
134     { 0x01, "reserved: invalid should be zero" },
135     { 0, NULL }
136 };
137 
138 static const value_string pn_rt_frag_status_more_follows[] = {
139     { 0x00, "Last fragment" },
140     { 0x01, "More fragments follow" },
141     { 0, NULL }
142 };
143 
144 /* Copied and renamed from proto.c because global value_strings don't work for plugins */
145 static const value_string plugin_proto_checksum_vals[] = {
146 	{ PROTO_CHECKSUM_E_BAD,        "Bad"  },
147 	{ PROTO_CHECKSUM_E_GOOD,       "Good" },
148 	{ PROTO_CHECKSUM_E_UNVERIFIED, "Unverified" },
149 	{ PROTO_CHECKSUM_E_NOT_PRESENT, "Not present" },
150 
151 	{ 0,        NULL }
152 };
153 
154 static void
dissect_DataStatus(tvbuff_t * tvb,int offset,proto_tree * tree,packet_info * pinfo,guint8 u8DataStatus)155 dissect_DataStatus(tvbuff_t *tvb, int offset, proto_tree *tree, packet_info *pinfo, guint8 u8DataStatus)
156 {
157     proto_item *sub_item;
158     proto_tree *sub_tree;
159     guint8 u8DataValid;
160     guint8 u8Redundancy;
161     guint8 u8State;
162     conversation_t    *conversation;
163     gboolean    inputFlag = FALSE;
164     gboolean    outputFlag = FALSE;
165     apduStatusSwitch *apdu_status_switch;
166 
167     u8State = (u8DataStatus & 0x01);
168     u8Redundancy = (u8DataStatus >> 1) & 0x01;
169     u8DataValid = (u8DataStatus >> 2) & 0x01;
170 
171     /* if PN Connect Request has been read, IOC mac is dl_src and IOD mac is dl_dst */
172     conversation = find_conversation(pinfo->num, &pinfo->dl_src, &pinfo->dl_dst, ENDPOINT_UDP, 0, 0, 0);
173 
174     if (conversation != NULL) {
175         apdu_status_switch = (apduStatusSwitch*)conversation_get_proto_data(conversation, proto_pn_io_apdu_status);
176         if (apdu_status_switch != NULL && apdu_status_switch->isRedundancyActive) {
177             /* IOC -> IOD: OutputCR */
178             if (addresses_equal(&(pinfo->src), conversation_key_addr1(conversation->key_ptr)) && addresses_equal(&(pinfo->dst), conversation_key_addr2(conversation->key_ptr))) {
179                 outputFlag = TRUE;
180                 inputFlag = FALSE;
181             }
182             /* IOD -> IOC: InputCR */
183             if (addresses_equal(&(pinfo->dst), conversation_key_addr1(conversation->key_ptr)) && addresses_equal(&(pinfo->src), conversation_key_addr2(conversation->key_ptr))) {
184                 inputFlag = TRUE;
185                 outputFlag = FALSE;
186             }
187         }
188     }
189 
190     /* input conversation is found */
191     if (inputFlag)
192     {
193         proto_tree_add_string_format_value(tree, hf_pn_rt_frame_info_type, tvb,
194             offset, 0, "Input", "Input Frame (IO_Device -> IO_Controller)");
195     }
196     /* output conversation is found. */
197     else if (outputFlag)
198     {
199         proto_tree_add_string_format_value(tree, hf_pn_rt_frame_info_type, tvb,
200             offset, 0, "Output", "Output Frame (IO_Controller -> IO_Device)");
201     }
202 
203     sub_item = proto_tree_add_uint_format(tree, hf_pn_rt_data_status,
204         tvb, offset, 1, u8DataStatus,
205         "DataStatus: 0x%02x (Frame: %s and %s, Provider: %s and %s)",
206         u8DataStatus,
207         (u8DataStatus & 0x04) ? "Valid"   : "Invalid",
208         (u8DataStatus & 0x01) ? "Primary" : "Backup",
209         (u8DataStatus & 0x20) ? "Ok"      : "Problem",
210         (u8DataStatus & 0x10) ? "Run"     : "Stop");
211     sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_data_status);
212     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_ignore,     tvb, offset, 1, u8DataStatus);
213     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_Reserved_2, tvb, offset, 1, u8DataStatus);
214     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_ok,         tvb, offset, 1, u8DataStatus);
215     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_operate,    tvb, offset, 1, u8DataStatus);
216     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_res3,       tvb, offset, 1, u8DataStatus);
217     /* input conversation is found */
218     if (inputFlag)
219     {
220         proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_valid, tvb, offset, 1, u8DataStatus);
221         proto_tree_add_item(tree, hf_pn_rt_frame_info_function_meaning_input_conv, tvb, offset, 1, u8DataStatus);
222         if (u8State == 0 && u8Redundancy == 0 && u8DataValid == 1)
223         {
224             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_backup, tvb, offset, 1, u8DataStatus);
225         }
226         else if (u8State == 0 && u8Redundancy == 0 && u8DataValid == 0)
227         {
228             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_backup, tvb, offset, 1, u8DataStatus);
229         }
230         else if (u8State == 0 && u8Redundancy == 1 && u8DataValid == 1)
231         {
232             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_backup, tvb, offset, 1, u8DataStatus);
233         }
234         else if (u8State == 0 && u8Redundancy == 1 && u8DataValid == 0)
235         {
236             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_backup, tvb, offset, 1, u8DataStatus);
237         }
238         else if (u8State == 1 && u8Redundancy == 0 && u8DataValid == 1)
239         {
240             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_primary, tvb, offset, 1, u8DataStatus);
241         }
242         else if (u8State == 1 && u8Redundancy == 1 && u8DataValid == 1)
243         {
244             proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_input_cr_state_is_primary, tvb, offset, 1, u8DataStatus);
245         }
246 
247         proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_primary, tvb, offset, 1, u8DataStatus);
248         return;
249     }
250     // output conversation is found.
251     else if (outputFlag)
252     {
253         proto_tree_add_item(tree, hf_pn_rt_frame_info_function_meaning_output_conv, tvb, offset, 1, u8DataStatus);
254 
255         proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_valid, tvb, offset, 1, u8DataStatus);
256         proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy_output_cr, tvb, offset, 1, u8DataStatus);
257         proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_primary, tvb, offset, 1, u8DataStatus);
258 
259         return;
260     }
261 
262     // If no conversation is found
263     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_valid,      tvb, offset, 1, u8DataStatus);
264     proto_tree_add_boolean(sub_tree, hf_pn_rt_data_status_redundancy, tvb, offset, 1, u8DataStatus);
265     proto_tree_add_uint(sub_tree, hf_pn_rt_data_status_primary,    tvb, offset, 1, u8DataStatus);
266 }
267 
268 
269 static gboolean
IsDFP_Frame(tvbuff_t * tvb,packet_info * pinfo,guint16 u16FrameID)270 IsDFP_Frame(tvbuff_t *tvb, packet_info *pinfo, guint16 u16FrameID)
271 {
272     guint16       u16SFCRC16;
273     guint8        u8SFPosition;
274     guint8        u8SFDataLength   = 255;
275     int           offset           = 0;
276     guint32       u32SubStart;
277     guint16       crc;
278     gint          tvb_len          = 0;
279     unsigned char virtualFramebuffer[16];
280 
281     /* try to build a temporaray buffer for generating this CRC */
282     if (!pinfo->src.data || !pinfo->dst.data ||
283             pinfo->dst.type != AT_ETHER || pinfo->src.type != AT_ETHER) {
284         /* if we don't have src/dst mac addresses then we assume it's not
285          * to avoid various crashes */
286         return FALSE;
287     }
288     memcpy(&virtualFramebuffer[0], pinfo->dst.data, 6);
289     memcpy(&virtualFramebuffer[6], pinfo->src.data, 6);
290     virtualFramebuffer[12] = 0x88;
291     virtualFramebuffer[13] = 0x92;
292     virtualFramebuffer[15] = (unsigned char) (u16FrameID &0xff);
293     virtualFramebuffer[14] = (unsigned char) (u16FrameID>>8);
294     crc = crc16_plain_init();
295     crc = crc16_plain_update(crc, &virtualFramebuffer[0], 16);
296     crc = crc16_plain_finalize(crc);
297     /* can check this CRC only by having built a temporary data buffer out of the pinfo data */
298     u16SFCRC16 = tvb_get_letohs(tvb, offset);
299     if (u16SFCRC16 != 0) /* no crc! */
300     {
301         if (u16SFCRC16 != crc)
302         {
303             return(FALSE);
304         }
305     }
306     /* end of first CRC check */
307 
308     offset += 2;    /*Skip first crc */
309     tvb_len = tvb_captured_length(tvb);
310     if (offset + 4 > tvb_len)
311         return FALSE;
312     if (tvb_get_letohs(tvb, offset) == 0)
313         return FALSE;   /* no valid DFP frame */
314     while (1) {
315         u32SubStart = offset;
316 
317         u8SFPosition = tvb_get_guint8(tvb, offset);
318         offset += 1;
319 
320         u8SFDataLength = tvb_get_guint8(tvb, offset);
321         offset += 1;
322 
323         if (u8SFDataLength == 0) {
324             break;
325         }
326 
327         offset += 2;
328 
329         offset += u8SFDataLength;
330        if (offset > tvb_len)
331            return /*TRUE; */FALSE;
332 
333         u16SFCRC16 = tvb_get_letohs(tvb, offset);
334         if (u16SFCRC16 != 0) {
335             if (u8SFPosition & 0x80) {
336                 crc = crc16_plain_tvb_offset_seed(tvb, u32SubStart, offset-u32SubStart, 0);
337                 if (crc != u16SFCRC16) {
338                     return FALSE;
339                 } else {
340                 }
341             } else {
342             }
343         }
344         offset += 2;
345     }
346     return TRUE;
347 }
348 
349 /* possibly dissect a CSF_SDU related PN-RT packet */
350 gboolean
dissect_CSF_SDU_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)351 dissect_CSF_SDU_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
352 {
353     /* the sub tvb will NOT contain the frame_id here! */
354     guint16     u16FrameID = GPOINTER_TO_UINT(data);
355     guint16     u16SFCRC16;
356     guint8      u8SFPosition;
357     guint8      u8SFDataLength = 255;
358     guint8      u8SFCycleCounter;
359     guint8      u8SFDataStatus;
360     gint        offset         = 0;
361     guint32     u32SubStart;
362     proto_item *sub_item;
363     proto_tree *sub_tree;
364     guint16     crc;
365 
366 
367     /* possible FrameID ranges for DFP */
368     if ((u16FrameID < 0x100) || (u16FrameID > 0x0FFF))
369         return (FALSE);
370     if (IsDFP_Frame(tvb, pinfo, u16FrameID)) {
371         /* can't check this CRC, as the checked data bytes are not available */
372         u16SFCRC16 = tvb_get_letohs(tvb, offset);
373         if (u16SFCRC16 != 0) {
374             /* Checksum verify will always succeed */
375             /* XXX - should we combine the two calls to always show "unverified"? */
376             proto_tree_add_checksum(tree, tvb, offset, hf_pn_rt_sf_crc16, hf_pn_rt_sf_crc16_status, &ei_pn_rt_sf_crc16, pinfo, u16SFCRC16,
377                             ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_VERIFY);
378         }
379         else {
380             proto_tree_add_checksum(tree, tvb, offset, hf_pn_rt_sf_crc16, hf_pn_rt_sf_crc16_status, &ei_pn_rt_sf_crc16, pinfo, 0,
381                             ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
382         }
383         offset += 2;
384 
385         while (1) {
386             sub_item = proto_tree_add_item(tree, hf_pn_rt_sf, tvb, offset, 0, ENC_NA);
387             sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_sf);
388             u32SubStart = offset;
389 
390             u8SFPosition = tvb_get_guint8(tvb, offset);
391             proto_tree_add_uint(sub_tree, hf_pn_rt_sf_position, tvb, offset, 1, u8SFPosition);
392             offset += 1;
393 
394             u8SFDataLength = tvb_get_guint8(tvb, offset);
395             proto_tree_add_uint(sub_tree, hf_pn_rt_sf_data_length, tvb, offset, 1, u8SFDataLength);
396             offset += 1;
397 
398             if (u8SFDataLength == 0) {
399                 proto_item_append_text(sub_item, ": Pos:%u, Length:%u", u8SFPosition, u8SFDataLength);
400                 proto_item_set_len(sub_item, offset - u32SubStart);
401                 break;
402             }
403 
404             u8SFCycleCounter = tvb_get_guint8(tvb, offset);
405             proto_tree_add_uint(sub_tree, hf_pn_rt_sf_cycle_counter, tvb, offset, 1, u8SFCycleCounter);
406             offset += 1;
407 
408             u8SFDataStatus = tvb_get_guint8(tvb, offset);
409             dissect_DataStatus(tvb, offset, sub_tree, pinfo, u8SFDataStatus);
410             offset += 1;
411 
412             offset = dissect_pn_user_data(tvb, offset, pinfo, sub_tree, u8SFDataLength, "DataItem");
413 
414             u16SFCRC16 = tvb_get_letohs(tvb, offset);
415 
416             if (u16SFCRC16 != 0 /* "old check": u8SFPosition & 0x80 */) {
417                 crc = crc16_plain_tvb_offset_seed(tvb, u32SubStart, offset-u32SubStart, 0);
418                 proto_tree_add_checksum(tree, tvb, offset, hf_pn_rt_sf_crc16, hf_pn_rt_sf_crc16_status, &ei_pn_rt_sf_crc16, pinfo, crc,
419                             ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_VERIFY);
420             } else {
421                 proto_tree_add_checksum(tree, tvb, offset, hf_pn_rt_sf_crc16, hf_pn_rt_sf_crc16_status, &ei_pn_rt_sf_crc16, pinfo, 0,
422                             ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
423             }
424             offset += 2;
425 
426             proto_item_append_text(sub_item, ": Pos:%u, Length:%u, Cycle:%u, Status: 0x%02x (%s,%s,%s,%s)",
427                 u8SFPosition, u8SFDataLength, u8SFCycleCounter, u8SFDataStatus,
428                 (u8SFDataStatus & 0x04) ? "Valid" : "Invalid",
429                 (u8SFDataStatus & 0x01) ? "Primary" : "Backup",
430                 (u8SFDataStatus & 0x20) ? "Ok" : "Problem",
431                 (u8SFDataStatus & 0x10) ? "Run" : "Stop");
432 
433             proto_item_set_len(sub_item, offset - u32SubStart);
434         }
435 
436         return TRUE;
437     }
438 
439     else {
440         dissect_pn_user_data(tvb, offset, pinfo, tree, tvb_captured_length_remaining(tvb, offset),
441                  "PROFINET IO Cyclic Service Data Unit");
442     }
443 
444     return FALSE;
445 
446 }
447 
448 /* for reasemble processing we need some inits.. */
449 /* Register PNIO defrag table init routine.      */
450 
451 static reassembly_table pdu_reassembly_table;
452 static GHashTable *reasembled_frag_table = NULL;
453 
454 static dissector_table_t ethertype_subdissector_table;
455 
456 static guint32 start_frag_OR_ID[16];
457 
458 
459 static void
pnio_defragment_init(void)460 pnio_defragment_init(void)
461 {
462     guint32 i;
463     for (i=0; i < 16; i++)    /* init  the reasemble help array */
464         start_frag_OR_ID[i] = 0;
465     reasembled_frag_table = g_hash_table_new(NULL, NULL);
466 }
467 
468 static void
pnio_defragment_cleanup(void)469 pnio_defragment_cleanup(void)
470 {
471     g_hash_table_destroy(reasembled_frag_table);
472 }
473 
474 /* possibly dissect a FRAG_PDU related PN-RT packet */
475 static gboolean
dissect_FRAG_PDU_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)476 dissect_FRAG_PDU_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
477 {
478     /* the sub tvb will NOT contain the frame_id here! */
479     guint16 u16FrameID = GPOINTER_TO_UINT(data);
480     int     offset = 0;
481 
482 
483     /* possible FrameID ranges for FRAG_PDU */
484     if (u16FrameID >= 0xFF80 && u16FrameID <= 0xFF8F) {
485         proto_item *sub_item;
486         proto_tree *sub_tree;
487         proto_item *status_item;
488         proto_tree *status_tree;
489         guint8      u8FragDataLength;
490         guint8      u8FragStatus;
491         gboolean    bMoreFollows;
492         guint8      uFragNumber;
493 
494         sub_item = proto_tree_add_item(tree, hf_pn_rt_frag, tvb, offset, 0, ENC_NA);
495         sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_frag);
496 
497         u8FragDataLength = tvb_get_guint8(tvb, offset);
498         proto_tree_add_uint(sub_tree, hf_pn_rt_frag_data_length, tvb, offset, 1, u8FragDataLength);
499         offset += 1;
500 
501         status_item = proto_tree_add_item(sub_tree, hf_pn_rt_frag_status, tvb, offset, 1, ENC_NA);
502         status_tree = proto_item_add_subtree(status_item, ett_pn_rt_frag_status);
503 
504         u8FragStatus = tvb_get_guint8(tvb, offset);
505         proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_more_follows, tvb, offset, 1, u8FragStatus);
506         proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_error, tvb, offset, 1, u8FragStatus);
507         proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_fragment_number, tvb, offset, 1, u8FragStatus);
508         offset += 1;
509         uFragNumber = u8FragStatus & 0x3F; /* bits 0 to 5 */
510         bMoreFollows = (u8FragStatus & 0x80) != 0;
511         proto_item_append_text(status_item, ": Number: %u, %s",
512             uFragNumber,
513             val_to_str( (u8FragStatus & 0x80) >> 7, pn_rt_frag_status_more_follows, "Unknown"));
514 
515         /* Is this a string or a bunch of bytes? Should it be FT_BYTES? */
516         proto_tree_add_string_format(sub_tree, hf_pn_rt_frag_data, tvb, offset, tvb_captured_length_remaining(tvb, offset), "data",
517             "Fragment Length: %d bytes", tvb_captured_length_remaining(tvb, offset));
518         col_append_fstr(pinfo->cinfo, COL_INFO, " Fragment Length: %d bytes", tvb_captured_length_remaining(tvb, offset));
519 
520         dissect_pn_user_data_bytes(tvb, offset, pinfo, sub_tree, tvb_captured_length_remaining(tvb, offset), FRAG_DATA);
521         if ((guint)tvb_captured_length_remaining(tvb, offset) < (guint)(u8FragDataLength *8)) {
522             proto_item_append_text(status_item, ": FragDataLength out of Framerange -> discarding!");
523             return (TRUE);
524         }
525         /* defragmentation starts here */
526         if (pnio_desegment)
527         {
528             guint32 u32FragID;
529             guint32 u32ReasembleID /*= 0xfedc ??*/;
530             fragment_head *pdu_frag;
531 
532             u32FragID = (u16FrameID & 0xf);
533             if (uFragNumber == 0)
534             { /* this is the first "new" fragment, so set up a new key Id */
535                 guint32 u32FrameKey;
536                 u32FrameKey = (pinfo->num << 2) | u32FragID;
537                 /* store it in the array */
538                 start_frag_OR_ID[u32FragID] = u32FrameKey;
539             }
540             u32ReasembleID = start_frag_OR_ID[u32FragID];
541             /* use frame data instead of "pnio fraglen" which sets 8 octet steps */
542             pdu_frag = fragment_add_seq(&pdu_reassembly_table, tvb, offset,
543                                         pinfo, u32ReasembleID, NULL, uFragNumber,
544                                         (tvb_captured_length_remaining(tvb, offset))/*u8FragDataLength*8*/, bMoreFollows, 0);
545 
546             if (pdu_frag && !bMoreFollows) /* PDU is complete! and last fragment */
547             {   /* store this fragment as the completed fragment in hash table */
548                 g_hash_table_insert(reasembled_frag_table, GUINT_TO_POINTER(pinfo->num), pdu_frag);
549                 start_frag_OR_ID[u32FragID] = 0; /* reset the starting frame counter */
550             }
551             if (!bMoreFollows) /* last fragment */
552             {
553                 pdu_frag = (fragment_head *)g_hash_table_lookup(reasembled_frag_table, GUINT_TO_POINTER(pinfo->num));
554                 if (pdu_frag)    /* found a matching fragment; dissect it */
555                 {
556                     guint16   type;
557                     tvbuff_t *pdu_tvb;
558 
559                     /* create the new tvb for defragmented frame */
560                     pdu_tvb = tvb_new_chain(tvb, pdu_frag->tvb_data);
561                     /* add the defragmented data to the data source list */
562                     add_new_data_source(pinfo, pdu_tvb, "Reassembled Profinet Frame");
563                     /* PDU is complete: look for the Ethertype and give it to the appropriate dissection routine */
564                     type = tvb_get_ntohs(pdu_tvb, 0);
565                     pdu_tvb = tvb_new_subset_remaining(pdu_tvb, 2);
566                     if (!dissector_try_uint(ethertype_subdissector_table, type, pdu_tvb, pinfo, tree))
567                         call_data_dissector(pdu_tvb, pinfo, tree);
568                 }
569             }
570             return TRUE;
571         }
572         else
573             return TRUE;
574     }
575     return FALSE;
576 }
577 
578 
579 /*
580  * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
581  */
582 static int
dissect_pn_rt(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)583 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
584 {
585     gint         pdu_len;
586     gint         data_len;
587     guint16      u16FrameID;
588     guint8       u8DataStatus;
589     guint8       u8TransferStatus;
590     guint16      u16CycleCounter;
591     const gchar *pszProtAddInfo;
592     const gchar *pszProtShort;
593     const gchar *pszProtSummary;
594     const gchar *pszProtComment;
595     proto_tree  *pn_rt_tree, *ti;
596     gchar        szFieldSummary[100];
597     tvbuff_t    *next_tvb;
598     gboolean     bCyclic;
599     heur_dtbl_entry_t *hdtbl_entry;
600 
601 
602     /* If the link-layer dissector for the protocol above us knows whether
603      * the packet, as handed to it, includes a link-layer FCS, what it
604      * hands to us should not include the FCS; if that's not the case,
605      * that's a bug in that dissector, and should be fixed there.
606      *
607      * If the link-layer dissector for the protocol above us doesn't know
608      * whether the packet, as handed to us, includes a link-layer FCS,
609      * there are limits as to what can be done there; the dissector
610      * ultimately needs a "yes, it has an FCS" preference setting, which
611      * both the Ethernet and 802.11 dissectors do.  If that's not the case
612      * for a dissector, that's a deficiency in that dissector, and should
613      * be fixed there.
614      *
615      * Therefore, we assume we are not handed a packet that includes an
616      * FCS.  If we are ever handed such a packet, either the link-layer
617      * dissector needs to be fixed or the link-layer dissector's preference
618      * needs to be set for your capture (even if that means adding such
619      * a preference).  This dissector (and other dissectors for protcols
620      * running atop the link layer) should not attempt to process the
621      * FCS themselves, as that will just break things. */
622 
623     /* Initialize variables */
624     pn_rt_tree = NULL;
625     ti         = NULL;
626 
627     /*
628      * Set the columns now, so that they'll be set correctly if we throw
629      * an exception.  We can set them (or append things) later again ....
630      */
631 
632     col_set_str(pinfo->cinfo, COL_PROTOCOL, "PN-RT");
633     col_set_str(pinfo->cinfo, COL_INFO, "PROFINET Real-Time");
634 
635     pdu_len = tvb_reported_length(tvb);
636     if (pdu_len < 6) {
637         dissect_pn_malformed(tvb, 0, pinfo, tree, pdu_len);
638         return 0;
639     }
640 
641     /* build some "raw" data */
642     u16FrameID = tvb_get_ntohs(tvb, 0);
643     if (u16FrameID <= 0x001F) {
644         pszProtShort    = "PN-RT";
645         pszProtAddInfo  = "reserved, ";
646         pszProtSummary  = "Real-Time";
647         pszProtComment  = "0x0000-0x001F: Reserved ID";
648         bCyclic         = FALSE;
649     } else if (u16FrameID <= 0x0021) {
650         pszProtShort    = "PN-PTCP";
651         pszProtAddInfo  = "Synchronization, ";
652         pszProtSummary  = "Real-Time";
653         pszProtComment  = "0x0020-0x0021: Real-Time: Sync (with follow up)";
654         bCyclic         = FALSE;
655     } else if (u16FrameID <= 0x007F) {
656         pszProtShort    = "PN-RT";
657         pszProtAddInfo  = "reserved, ";
658         pszProtSummary  = "Real-Time";
659         pszProtComment  = "0x0022-0x007F: Reserved ID";
660         bCyclic         = FALSE;
661     } else if (u16FrameID <= 0x0081) {
662         pszProtShort    = "PN-PTCP";
663         pszProtAddInfo  = "Synchronization, ";
664         pszProtSummary  = "Isochronous-Real-Time";
665         pszProtComment  = "0x0080-0x0081: Real-Time: Sync (without follow up)";
666         bCyclic         = FALSE;
667     } else if (u16FrameID <= 0x00FF) {
668         pszProtShort    = "PN-RT";
669         pszProtAddInfo  = "reserved, ";
670         pszProtSummary  = "Real-Time";
671         pszProtComment  = "0x0082-0x00FF: Reserved ID";
672         bCyclic         = FALSE;
673     } else if (u16FrameID <= 0x6FF) {
674         pszProtShort    = "PN-RTC3";
675         pszProtAddInfo  = "RTC3, ";
676         pszProtSummary  = "Isochronous-Real-Time";
677         pszProtComment  = "0x0100-0x06FF: RED: Real-Time(class=3): non redundant, normal or DFP";
678         bCyclic         = TRUE;
679     } else if (u16FrameID <= 0x0FFF) {
680         pszProtShort    = "PN-RTC3";
681         pszProtAddInfo  = "RTC3, ";
682         pszProtSummary  = "Isochronous-Real-Time";
683         pszProtComment  = "0x0700-0x0FFF: RED: Real-Time(class=3): redundant, normal or DFP";
684         bCyclic         = TRUE;
685     } else if (u16FrameID <= 0x7FFF) {
686         pszProtShort    = "PN-RT";
687         pszProtAddInfo  = "reserved, ";
688         pszProtSummary  = "Real-Time";
689         pszProtComment  = "0x1000-0x7FFF: Reserved ID";
690         bCyclic         = FALSE;
691     } else if (u16FrameID <= 0xBBFF) {
692         pszProtShort    = "PN-RTC1";
693         pszProtAddInfo  = "RTC1, ";
694         pszProtSummary  = "cyclic Real-Time";
695         pszProtComment  = "0x8000-0xBBFF: Real-Time(class=1 unicast): non redundant, normal";
696         bCyclic         = TRUE;
697     } else if (u16FrameID <= 0xBFFF) {
698         pszProtShort    = "PN-RTC1";
699         pszProtAddInfo  = "RTC1, ";
700         pszProtSummary  = "cyclic Real-Time";
701         pszProtComment  = "0xBC00-0xBFFF: Real-Time(class=1 multicast): non redundant, normal";
702         bCyclic         = TRUE;
703     } else if (u16FrameID <= 0xF7FF) {
704         /* check if udp frame on PNIO port */
705         if (pinfo->destport == 0x8892)
706         { /* UDP frame */
707             pszProtShort = "PN-RTCUDP,";
708             pszProtAddInfo = "RT_CLASS_UDP, ";
709             pszProtComment = "0xC000-0xF7FF: Real-Time(UDP unicast): Cyclic";
710         }
711         else
712         { /* layer 2 frame */
713             pszProtShort = "PN-RT";
714             pszProtAddInfo = "RTC1(legacy), ";
715             pszProtComment = "0xC000-0xF7FF: Real-Time(class=1 unicast): Cyclic";
716         }
717         pszProtSummary  = "cyclic Real-Time";
718         bCyclic         = TRUE;
719     } else if (u16FrameID <= 0xFBFF) {
720         if (pinfo->destport == 0x8892)
721         { /* UDP frame */
722             pszProtShort = "PN-RTCUDP,";
723             pszProtAddInfo = "RT_CLASS_UDP, ";
724             pszProtComment = "0xF800-0xFBFF:: Real-Time(UDP multicast): Cyclic";
725         }
726         else
727         { /* layer 2 frame */
728             pszProtShort = "PN-RT";
729             pszProtAddInfo = "RTC1(legacy), ";
730             pszProtComment = "0xF800-0xFBFF: Real-Time(class=1 multicast): Cyclic";
731          }
732         pszProtSummary  = "cyclic Real-Time";
733         bCyclic         = TRUE;
734     } else if (u16FrameID <= 0xFDFF) {
735         pszProtShort    = "PN-RTA";
736         pszProtAddInfo  = "Reserved, ";
737         pszProtSummary  = "acyclic Real-Time";
738         pszProtComment  = "0xFC00-0xFDFF: Reserved";
739         bCyclic         = FALSE;
740         if (u16FrameID == 0xfc01) {
741             pszProtShort    = "PN-RTA";
742             pszProtAddInfo  = "Alarm High, ";
743             pszProtSummary  = "acyclic Real-Time";
744             pszProtComment  = "Real-Time: Acyclic PN-IO Alarm high priority";
745         }
746 
747     } else if (u16FrameID <= 0xFEFF) {
748         pszProtShort    = "PN-RTA";
749         pszProtAddInfo  = "Reserved, ";
750         pszProtSummary  = "acyclic Real-Time";
751         pszProtComment  = "0xFE00-0xFEFF: Real-Time: Reserved";
752         bCyclic         = FALSE;
753         if (u16FrameID == 0xFE01) {
754             pszProtShort    = "PN-RTA";
755             pszProtAddInfo  = "Alarm Low, ";
756             pszProtSummary  = "acyclic Real-Time";
757             pszProtComment  = "Real-Time: Acyclic PN-IO Alarm low priority";
758         }
759         if (u16FrameID == 0xFE02) {
760             pszProtShort = "PN-RSI";
761             pszProtAddInfo = "";
762             pszProtSummary = "acyclic Real-Time";
763             pszProtComment = "Real-Time: Acyclic PN-IO RSI";
764         }
765         if (u16FrameID == FRAME_ID_DCP_HELLO) {
766             pszProtShort    = "PN-RTA";
767             pszProtAddInfo  = "";
768             pszProtSummary  = "acyclic Real-Time";
769             pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) hello";
770         }
771         if (u16FrameID == FRAME_ID_DCP_GETORSET) {
772             pszProtShort    = "PN-RTA";
773             pszProtAddInfo  = "";
774             pszProtSummary  = "acyclic Real-Time";
775             pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) get/set";
776         }
777         if (u16FrameID == FRAME_ID_DCP_IDENT_REQ) {
778             pszProtShort    = "PN-RTA";
779             pszProtAddInfo  = "";
780             pszProtSummary  = "acyclic Real-Time";
781             pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) identify multicast request";
782         }
783         if (u16FrameID == FRAME_ID_DCP_IDENT_RES) {
784             pszProtShort    = "PN-RTA";
785             pszProtAddInfo  = "";
786             pszProtSummary  = "acyclic Real-Time";
787             pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) identify response";
788         }
789     } else if (u16FrameID <= 0xFF01) {
790         pszProtShort    = "PN-PTCP";
791         pszProtAddInfo  = "RTA Sync, ";
792         pszProtSummary  = "acyclic Real-Time";
793         pszProtComment  = "0xFF00-0xFF01: PTCP Announce";
794         bCyclic         = FALSE;
795     } else if (u16FrameID <= 0xFF1F) {
796         pszProtShort    = "PN-PTCP";
797         pszProtAddInfo  = "RTA Sync, ";
798         pszProtSummary  = "acyclic Real-Time";
799         pszProtComment  = "0xFF02-0xFF1F: Reserved";
800         bCyclic         = FALSE;
801     } else if (u16FrameID <= 0xFF21) {
802         pszProtShort    = "PN-PTCP";
803         pszProtAddInfo  = "Follow Up, ";
804         pszProtSummary  = "acyclic Real-Time";
805         pszProtComment  = "0xFF20-0xFF21: PTCP Follow Up";
806         bCyclic         = FALSE;
807     } else if (u16FrameID <= 0xFF22) {
808         pszProtShort    = "PN-PTCP";
809         pszProtAddInfo  = "Follow Up, ";
810         pszProtSummary  = "acyclic Real-Time";
811         pszProtComment  = "0xFF22-0xFF3F: Reserved";
812         bCyclic         = FALSE;
813     } else if (u16FrameID <= 0xFF43) {
814         pszProtShort    = "PN-PTCP";
815         pszProtAddInfo  = "Delay, ";
816         pszProtSummary  = "acyclic Real-Time";
817         pszProtComment  = "0xFF40-0xFF43: Acyclic Real-Time: Delay";
818         bCyclic         = FALSE;
819     } else if (u16FrameID <= 0xFF7F) {
820         pszProtShort    = "PN-RT";
821         pszProtAddInfo  = "Reserved, ";
822         pszProtSummary  = "Real-Time";
823         pszProtComment  = "0xFF44-0xFF7F: reserved ID";
824         bCyclic         = FALSE;
825     } else if (u16FrameID <= 0xFF8F) {
826         pszProtShort    = "PN-RT";
827         pszProtAddInfo  = "";
828         pszProtSummary  = "Fragmentation";
829         pszProtComment  = "0xFF80-0xFF8F: Fragmentation";
830         bCyclic         = FALSE;
831     } else {
832         pszProtShort    = "PN-RT";
833         pszProtAddInfo  = "Reserved, ";
834         pszProtSummary  = "Real-Time";
835         pszProtComment  = "0xFF90-0xFFFF: reserved ID";
836         bCyclic         = FALSE;
837     }
838 
839     /* decode optional cyclic fields at the packet end and build the summary line */
840     if (bCyclic) {
841         /* cyclic transfer has cycle counter, data status and transfer status fields at the end */
842         u16CycleCounter  = tvb_get_ntohs(tvb, pdu_len - 4);
843         u8DataStatus     = tvb_get_guint8(tvb, pdu_len - 2);
844         u8TransferStatus = tvb_get_guint8(tvb, pdu_len - 1);
845 
846         g_snprintf (szFieldSummary, sizeof(szFieldSummary),
847                 "%sID:0x%04x, Len:%4u, Cycle:%5u (%s,%s,%s,%s)",
848                 pszProtAddInfo, u16FrameID, pdu_len - 2 - 4, u16CycleCounter,
849                 (u8DataStatus & 0x04) ? "Valid"   : "Invalid",
850                 (u8DataStatus & 0x01) ? "Primary" : "Backup",
851                 (u8DataStatus & 0x20) ? "Ok"      : "Problem",
852                 (u8DataStatus & 0x10) ? "Run"     : "Stop");
853 
854         /* user data length is packet len - frame id - optional cyclic status fields */
855         data_len = pdu_len - 2 - 4;
856     } else {
857         /* satisfy the gcc compiler, so it won't throw an "uninitialized" warning */
858         u16CycleCounter     = 0;
859         u8DataStatus        = 0;
860         u8TransferStatus    = 0;
861 
862         /* acyclic transfer has no fields at the end */
863         g_snprintf (szFieldSummary, sizeof(szFieldSummary),
864                   "%sID:0x%04x, Len:%4u",
865                 pszProtAddInfo, u16FrameID, pdu_len - 2);
866 
867         /* user data length is packet len - frame id field */
868         data_len = pdu_len - 2;
869     }
870 
871     /* build protocol tree only, if tree is really used */
872     if (tree) {
873         /* build pn_rt protocol tree with summary line */
874         if (pn_rt_summary_in_tree) {
875           ti = proto_tree_add_protocol_format(tree, proto_pn_rt, tvb, 0, pdu_len,
876                 "PROFINET %s, %s", pszProtSummary, szFieldSummary);
877         } else {
878             ti = proto_tree_add_item(tree, proto_pn_rt, tvb, 0, pdu_len, ENC_NA);
879         }
880         pn_rt_tree = proto_item_add_subtree(ti, ett_pn_rt);
881 
882         /* add frame ID */
883         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_frame_id, tvb,
884           0, 2, u16FrameID, "FrameID: 0x%04x (%s)", u16FrameID, pszProtComment);
885 
886         if (bCyclic) {
887             /* add cycle counter */
888             proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_cycle_counter, tvb,
889               pdu_len - 4, 2, u16CycleCounter, "CycleCounter: %u", u16CycleCounter);
890 
891             /* add data status subtree */
892             dissect_DataStatus(tvb, pdu_len - 2, pn_rt_tree, pinfo, u8DataStatus);
893 
894             /* add transfer status */
895             if (u8TransferStatus) {
896                 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
897                     pdu_len - 1, 1, u8TransferStatus,
898                     "TransferStatus: 0x%02x (ignore this frame)", u8TransferStatus);
899             } else {
900                 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
901                     pdu_len - 1, 1, u8TransferStatus,
902                     "TransferStatus: 0x%02x (OK)", u8TransferStatus);
903             }
904         }
905     }
906 
907     /* update column info now */
908     if (u16FrameID == 0xFE02)
909     {
910         g_snprintf(szFieldSummary, sizeof(szFieldSummary), "%s", "");
911     }
912     col_add_str(pinfo->cinfo, COL_INFO, szFieldSummary);
913     col_set_str(pinfo->cinfo, COL_PROTOCOL, pszProtShort);
914 
915     /* get frame user data tvb (without header and footer) */
916     next_tvb = tvb_new_subset_length(tvb, 2, data_len);
917 
918     /* ask heuristics, if some sub-dissector is interested in this packet payload */
919     if (!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, GUINT_TO_POINTER( (guint32) u16FrameID))) {
920         /*col_set_str(pinfo->cinfo, COL_INFO, "Unknown");*/
921 
922         /* Oh, well, we don't know this; dissect it as data. */
923         dissect_pn_undecoded(next_tvb, 0, pinfo, tree, tvb_captured_length(next_tvb));
924     }
925     return tvb_captured_length(tvb);
926 }
927 
928 
929 /* Register all the bits needed by the filtering engine */
930 void
proto_register_pn_rt(void)931 proto_register_pn_rt(void)
932 {
933     static hf_register_info hf[] = {
934         { &hf_pn_rt_frame_id,
935           { "FrameID", "pn_rt.frame_id",
936             FT_UINT16, BASE_DEC, NULL, 0x0,
937             NULL, HFILL }},
938 
939         { &hf_pn_rt_cycle_counter,
940           { "CycleCounter", "pn_rt.cycle_counter",
941             FT_UINT16, BASE_DEC, NULL, 0x0,
942             NULL, HFILL }},
943 
944         { &hf_pn_rt_data_status,
945           { "DataStatus", "pn_rt.ds",
946             FT_UINT8, BASE_HEX, 0, 0x0,
947             NULL, HFILL }},
948 
949         { &hf_pn_rt_data_status_ignore,
950           { "Ignore (1:Ignore/0:Evaluate)", "pn_rt.ds_ignore", FT_UINT8, BASE_HEX, 0, 0x80,
951             NULL, HFILL }},
952 
953         { &hf_pn_rt_frame_info_type,
954           { "PN Frame Type", "pn_rt.ds_frame_info_type", FT_STRING, BASE_NONE, NULL, 0x0,
955             NULL, HFILL }},
956 
957         { &hf_pn_rt_frame_info_function_meaning_input_conv,
958           { "Function/Meaning", "pn_rt.ds_frame_info_meaning",
959             FT_UINT8, BASE_HEX, VALS(pn_rt_frame_info_function_meaning_input_conv), 0x7,
960             NULL, HFILL } },
961 
962         { &hf_pn_rt_frame_info_function_meaning_output_conv,
963           { "Function/Meaning", "pn_rt.ds_frame_info_meaning",
964             FT_UINT8, BASE_HEX, VALS(pn_rt_frame_info_function_meaning_output_conv), 0x7,
965             NULL, HFILL } },
966 
967         { &hf_pn_rt_data_status_Reserved_2,
968           { "Reserved_2 (should be zero)", "pn_rt.ds_Reserved_2",
969             FT_UINT8, BASE_HEX, 0, 0x40,
970             NULL, HFILL }},
971 
972         { &hf_pn_rt_data_status_ok,
973           { "StationProblemIndicator (1:Ok/0:Problem)", "pn_rt.ds_ok",
974             FT_UINT8, BASE_HEX, 0, 0x20,
975             NULL, HFILL }},
976 
977         { &hf_pn_rt_data_status_operate,
978           { "ProviderState (1:Run/0:Stop)", "pn_rt.ds_operate",
979             FT_UINT8, BASE_HEX, 0, 0x10,
980             NULL, HFILL }},
981 
982         { &hf_pn_rt_data_status_res3,
983           { "Reserved_1 (should be zero)", "pn_rt.ds_res3",
984             FT_UINT8, BASE_HEX, 0, 0x08,
985             NULL, HFILL }},
986 
987         { &hf_pn_rt_data_status_valid,
988           { "DataValid (1:Valid/0:Invalid)", "pn_rt.ds_valid",
989             FT_UINT8, BASE_HEX, 0, 0x04,
990             NULL, HFILL }},
991 
992         { &hf_pn_rt_data_status_redundancy,
993           { "Redundancy", "pn_rt.ds_redundancy",
994             FT_BOOLEAN, 8, TFS(&tfs_pn_rt_ds_redundancy), 0x02,
995             NULL, HFILL }},
996 
997         { &hf_pn_rt_data_status_redundancy_output_cr,
998           { "Redundancy", "pn_rt.ds_redundancy",
999             FT_BOOLEAN, 8, TFS(&tfs_pn_rt_ds_redundancy_output_cr), 0x02,
1000             NULL, HFILL }},
1001 
1002         { &hf_pn_rt_data_status_redundancy_input_cr_state_is_backup,
1003           { "Redundancy", "pn_rt.ds_redundancy",
1004             FT_BOOLEAN, 8, TFS(&tfs_pn_rt_ds_redundancy_input_cr_state_is_backup), 0x02,
1005             NULL, HFILL }},
1006 
1007         { &hf_pn_rt_data_status_redundancy_input_cr_state_is_primary,
1008           { "Redundancy", "pn_rt.ds_redundancy",
1009             FT_BOOLEAN, 8, TFS(&tfs_pn_rt_ds_redundancy_input_cr_state_is_primary), 0x02,
1010             NULL, HFILL }},
1011 
1012         { &hf_pn_rt_data_status_primary,
1013           { "State (1:Primary/0:Backup)", "pn_rt.ds_primary",
1014             FT_UINT8, BASE_HEX, 0, 0x01,
1015             NULL, HFILL }},
1016 
1017         { &hf_pn_rt_transfer_status,
1018           { "TransferStatus", "pn_rt.transfer_status",
1019             FT_UINT8, BASE_DEC, NULL, 0x0,
1020             NULL, HFILL }},
1021 
1022         { &hf_pn_rt_sf,
1023           { "SubFrame", "pn_rt.sf",
1024             FT_NONE, BASE_NONE, NULL, 0x0,
1025             NULL, HFILL }},
1026 
1027         { &hf_pn_rt_sf_crc16,
1028           { "SFCRC16", "pn_rt.sf.crc16",
1029             FT_UINT16, BASE_HEX, NULL, 0x0,
1030             NULL, HFILL }},
1031 
1032         { &hf_pn_rt_sf_crc16_status,
1033           { "SFCRC16 status", "pn_rt.sf.crc16.status",
1034             FT_UINT8, BASE_NONE, VALS(plugin_proto_checksum_vals), 0x0,
1035             NULL, HFILL }},
1036 
1037         { &hf_pn_rt_sf_position,
1038           { "Position", "pn_rt.sf.position",
1039             FT_UINT8, BASE_DEC, NULL, 0x7F,
1040             NULL, HFILL }},
1041 
1042 #if 0
1043         { &hf_pn_rt_sf_position_control,
1044           { "Control", "pn_rt.sf.position_control",
1045             FT_UINT8, BASE_DEC, VALS(pn_rt_position_control), 0x80,
1046             NULL, HFILL }},
1047 #endif
1048 
1049         { &hf_pn_rt_sf_data_length,
1050           { "DataLength", "pn_rt.sf.data_length",
1051             FT_UINT8, BASE_DEC, NULL, 0x0,
1052             NULL, HFILL }},
1053 
1054         { &hf_pn_rt_sf_cycle_counter,
1055           { "CycleCounter", "pn_rt.sf.cycle_counter",
1056             FT_UINT8, BASE_DEC, NULL, 0x0,
1057             NULL, HFILL }},
1058 
1059         { &hf_pn_rt_frag,
1060           { "PROFINET Fragment", "pn_rt.frag",
1061             FT_NONE, BASE_NONE, NULL, 0x0,
1062             NULL, HFILL }},
1063 
1064         { &hf_pn_rt_frag_data_length,
1065           { "FragDataLength", "pn_rt.frag_data_length",
1066             FT_UINT8, BASE_DEC, NULL, 0x0,
1067             NULL, HFILL }},
1068 
1069         { &hf_pn_rt_frag_status,
1070           { "FragStatus", "pn_rt.frag_status",
1071             FT_NONE, BASE_NONE, NULL, 0x0,
1072             NULL, HFILL }},
1073 
1074         { &hf_pn_rt_frag_status_more_follows,
1075           { "MoreFollows", "pn_rt.frag_status.more_follows",
1076             FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_more_follows), 0x80,
1077             NULL, HFILL }},
1078 
1079         { &hf_pn_rt_frag_status_error,
1080           { "Reserved", "pn_rt.frag_status.error",
1081             FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_error), 0x40,
1082             NULL, HFILL }},
1083 
1084         { &hf_pn_rt_frag_status_fragment_number,
1085           { "FragmentNumber (zero based)", "pn_rt.frag_status.fragment_number",
1086             FT_UINT8, BASE_DEC, NULL, 0x3F,
1087             NULL, HFILL }},
1088 
1089         /* Is this a string or a bunch of bytes? Should it be FT_BYTES? */
1090         { &hf_pn_rt_frag_data,
1091           { "FragData", "pn_rt.frag_data",
1092             FT_STRING, BASE_NONE, NULL, 0x00,
1093             NULL, HFILL }},
1094 
1095     };
1096     static gint *ett[] = {
1097         &ett_pn_rt,
1098         &ett_pn_rt_data_status,
1099         &ett_pn_rt_sf,
1100         &ett_pn_rt_frag,
1101         &ett_pn_rt_frag_status
1102     };
1103 
1104     static ei_register_info ei[] = {
1105         { &ei_pn_rt_sf_crc16, { "pn_rt.sf.crc16_bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
1106     };
1107 
1108     module_t *pn_rt_module;
1109     expert_module_t* expert_pn_rt;
1110 
1111     proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
1112                                           "PN-RT", "pn_rt");
1113 
1114     proto_register_field_array(proto_pn_rt, hf, array_length(hf));
1115     proto_register_subtree_array(ett, array_length(ett));
1116     expert_pn_rt = expert_register_protocol(proto_pn_rt);
1117     expert_register_field_array(expert_pn_rt, ei, array_length(ei));
1118 
1119     /* Register our configuration options */
1120 
1121     pn_rt_module = prefs_register_protocol(proto_pn_rt, NULL);
1122 
1123     prefs_register_bool_preference(pn_rt_module, "summary_in_tree",
1124                                    "Show PN-RT summary in protocol tree",
1125                                    "Whether the PN-RT summary line should be shown in the protocol tree",
1126                                    &pn_rt_summary_in_tree);
1127 
1128     prefs_register_bool_preference(pn_rt_module, "desegment",
1129                                    "reassemble PNIO Fragments",
1130                                    "Reassemble PNIO Fragments and get them decoded",
1131                                    &pnio_desegment);
1132 
1133     /* register heuristics anchor for payload dissectors */
1134     heur_subdissector_list = register_heur_dissector_list("pn_rt", proto_pn_rt);
1135 
1136     init_pn (proto_pn_rt);
1137     register_init_routine(pnio_defragment_init);
1138     register_cleanup_routine(pnio_defragment_cleanup);
1139     reassembly_table_register(&pdu_reassembly_table,
1140                           &addresses_reassembly_table_functions);
1141 }
1142 
1143 
1144 /* The registration hand-off routine is called at startup */
1145 void
proto_reg_handoff_pn_rt(void)1146 proto_reg_handoff_pn_rt(void)
1147 {
1148     dissector_handle_t pn_rt_handle;
1149 
1150     pn_rt_handle = create_dissector_handle(dissect_pn_rt, proto_pn_rt);
1151 
1152     dissector_add_uint("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
1153     dissector_add_uint_with_preference("udp.port", PROFINET_UDP_PORT, pn_rt_handle);
1154 
1155     heur_dissector_add("pn_rt", dissect_CSF_SDU_heur, "PROFINET CSF_SDU IO", "pn_csf_sdu_pn_rt", proto_pn_rt, HEURISTIC_ENABLE);
1156     heur_dissector_add("pn_rt", dissect_FRAG_PDU_heur, "PROFINET Frag PDU IO", "pn_frag_pn_rt", proto_pn_rt, HEURISTIC_ENABLE);
1157 
1158     ethertype_subdissector_table = find_dissector_table("ethertype");
1159 }
1160 
1161 
1162 /*
1163  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
1164  *
1165  * Local variables:
1166  * c-basic-offset: 4
1167  * tab-width: 8
1168  * indent-tabs-mode: nil
1169  * End:
1170  *
1171  * vi: set shiftwidth=4 tabstop=8 expandtab:
1172  * :indentSize=4:tabSize=8:noTabs=true:
1173  */
1174