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