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