nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24  
25 #include "config.h"
26  
27 #include <epan/packet.h>
28 #include <epan/reassemble.h>
29 #include <epan/prefs.h>
30 #include <epan/etypes.h>
31 #include <epan/expert.h>
32 #include <epan/crc16-tvb.h>
33 #include <epan/dissectors/packet-dcerpc.h>
34  
35 #include <wsutil/crc16-plain.h>
36 #include "packet-pn.h"
37  
38  
39 void proto_register_pn_rt(void);
40 void proto_reg_handoff_pn_rt(void);
41  
42 /* Define the pn-rt proto */
43 static int proto_pn_rt = -1;
44 static gboolean pnio_desegment = TRUE;
45  
46 /* Define many header fields for pn-rt */
47 static int hf_pn_rt_frame_id = -1;
48 static int hf_pn_rt_cycle_counter = -1;
49 static int hf_pn_rt_transfer_status = -1;
50 static int hf_pn_rt_data_status = -1;
51 static int hf_pn_rt_data_status_ignore = -1;
52 static int hf_pn_rt_data_status_Reserved_2 = -1;
53 static int hf_pn_rt_data_status_ok = -1;
54 static int hf_pn_rt_data_status_operate = -1;
55 static int hf_pn_rt_data_status_res3 = -1;
56 static int hf_pn_rt_data_status_valid = -1;
57 static int hf_pn_rt_data_status_redundancy = -1;
58 static int hf_pn_rt_data_status_primary = -1;
59  
60 static int hf_pn_rt_sf_crc16 = -1;
61 static int hf_pn_rt_sf_crc16_status = -1;
62 static int hf_pn_rt_sf = -1;
63 static int hf_pn_rt_sf_position = -1;
64 /* static int hf_pn_rt_sf_position_control = -1; */
65 static int hf_pn_rt_sf_data_length = -1;
66 static int hf_pn_rt_sf_cycle_counter = -1;
67  
68 static int hf_pn_rt_frag = -1;
69 static int hf_pn_rt_frag_data_length = -1;
70 static int hf_pn_rt_frag_status = -1;
71 static int hf_pn_rt_frag_status_more_follows = -1;
72 static int hf_pn_rt_frag_status_error = -1;
73 static int hf_pn_rt_frag_status_fragment_number = -1;
74 static int hf_pn_rt_frag_data = -1;
75  
76  
77 /*
78 * Define the trees for pn-rt
79 * We need one tree for pn-rt itself and one for the pn-rt data status subtree
80 */
81 static int ett_pn_rt = -1;
82 static int ett_pn_rt_data_status = -1;
83 static int ett_pn_rt_sf = -1;
84 static int ett_pn_rt_frag = -1;
85 static int ett_pn_rt_frag_status = -1;
86  
87 static expert_field ei_pn_rt_sf_crc16 = EI_INIT;
88  
89 /*
90 * Here are the global variables associated with
91 * the various user definable characteristics of the dissection
92 */
93 /* Place summary in proto tree */
94 static gboolean pn_rt_summary_in_tree = TRUE;
95  
96 /* heuristic to find the right pn-rt payload dissector */
97 static heur_dissector_list_t heur_subdissector_list;
98  
99  
100 #if 0
101 static const value_string pn_rt_position_control[] = {
102 { 0x00, "CRC16 and CycleCounter shall not be checked" },
103 { 0x80, "CRC16 and CycleCounter valid" },
104 { 0, NULL }
105 };
106 #endif
107  
108 static const value_string pn_rt_ds_redundancy[] = {
109 { 0x00, "One primary AR of a given AR-set is present" },
110 { 0x01, "None primary AR of a given AR-set is present" },
111 { 0, NULL }
112 };
113  
114 static const value_string pn_rt_frag_status_error[] = {
115 { 0x00, "reserved" },
116 { 0x01, "reserved: invalid should be zero" },
117 { 0, NULL }
118 };
119  
120 static const value_string pn_rt_frag_status_more_follows[] = {
121 { 0x00, "Last fragment" },
122 { 0x01, "More fragments follow" },
123 { 0, NULL }
124 };
125  
126 /* Copied and renamed from proto.c because global value_strings don't work for plugins */
127 static const value_string plugin_proto_checksum_vals[] = {
128 { PROTO_CHECKSUM_E_BAD, "Bad" },
129 { PROTO_CHECKSUM_E_GOOD, "Good" },
130 { PROTO_CHECKSUM_E_UNVERIFIED, "Unverified" },
131 { PROTO_CHECKSUM_E_NOT_PRESENT, "Not present" },
132  
133 { 0, NULL }
134 };
135  
136 static void
137 dissect_DataStatus(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 u8DataStatus)
138 {
139 proto_item *sub_item;
140 proto_tree *sub_tree;
141  
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);
159 }
160  
161  
162 static gboolean
163 IsDFP_Frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint16 u16FrameID)
164 {
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  
174 /* try to build a temporaray buffer for generating this CRC */
175 if (!pinfo->src.data || !pinfo->dst.data ||
176 pinfo->dst.type != AT_ETHER || pinfo->src.type != AT_ETHER) {
177 /* if we don't have src/dst mac addresses then we assume it's not
178 * to avoid various crashes */
179 return FALSE;
180 }
181 memcpy(&virtualFramebuffer[0], pinfo->dst.data, 6);
182 memcpy(&virtualFramebuffer[6], pinfo->src.data, 6);
183 virtualFramebuffer[12] = 0x88;
184 virtualFramebuffer[13] = 0x92;
185 virtualFramebuffer[15] = (unsigned char) (u16FrameID &0xff);
186 virtualFramebuffer[14] = (unsigned char) (u16FrameID>>8);
187 crc = crc16_plain_init();
188 crc = crc16_plain_update(crc, &virtualFramebuffer[0], 16);
189 crc = crc16_plain_finalize(crc);
190 /* can check this CRC only by having built a temporary data buffer out of the pinfo data */
191 u16SFCRC16 = tvb_get_letohs(tvb, offset);
192 if (u16SFCRC16 != 0) /* no crc! */
193 {
194 if (u16SFCRC16 != crc)
195 {
196 proto_item_append_text(tree, ", no packed frame: SFCRC16 is 0x%x should be 0x%x", u16SFCRC16, crc);
197 return(FALSE);
198 }
199 }
200 /* end of first CRC check */
201  
202 offset += 2; /*Skip first crc */
203 tvb_len = tvb_captured_length(tvb);
204 if (offset + 4 > tvb_len)
205 return FALSE;
206 if (tvb_get_letohs(tvb, offset) == 0)
207 return FALSE; /* no valid DFP frame */
208 while (1) {
209 u32SubStart = offset;
210  
211 u8SFPosition = tvb_get_guint8(tvb, offset);
212 offset += 1;
213  
214 u8SFDataLength = tvb_get_guint8(tvb, offset);
215 offset += 1;
216  
217 if (u8SFDataLength == 0) {
218 break;
219 }
220  
221 offset += 2;
222  
223 offset += u8SFDataLength;
224 if (offset > tvb_len)
225 return /*TRUE; */FALSE;
226  
227 u16SFCRC16 = tvb_get_letohs(tvb, offset);
228 if (u16SFCRC16 != 0) {
229 if (u8SFPosition & 0x80) {
230 crc = crc16_plain_tvb_offset_seed(tvb, u32SubStart, offset-u32SubStart, 0);
231 if (crc != u16SFCRC16) {
232 return FALSE;
233 } else {
234 }
235 } else {
236 }
237 }
238 offset += 2;
239 }
240 return TRUE;
241 }
242  
243 /* possibly dissect a CSF_SDU related PN-RT packet */
244 gboolean
245 dissect_CSF_SDU_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
246 {
247 /* the sub tvb will NOT contain the frame_id here! */
248 guint16 u16FrameID = GPOINTER_TO_UINT(data);
249 guint16 u16SFCRC16;
250 guint8 u8SFPosition;
251 guint8 u8SFDataLength = 255;
252 guint8 u8SFCycleCounter;
253 guint8 u8SFDataStatus;
254 gint offset = 0;
255 guint32 u32SubStart;
256 proto_item *sub_item;
257 proto_tree *sub_tree;
258 guint16 crc;
259  
260  
261 /* possible FrameID ranges for DFP */
262 if ((u16FrameID < 0x100) || (u16FrameID > 0x0FFF))
263 return (FALSE);
264 if (IsDFP_Frame(tvb, pinfo, tree, u16FrameID)) {
265 /* can't check this CRC, as the checked data bytes are not available */
266 u16SFCRC16 = tvb_get_letohs(tvb, offset);
267 if (u16SFCRC16 != 0) {
268 /* Checksum verify will always succeed */
269 /* XXX - should we combine the two calls to always show "unverified"? */
270 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,
271 ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_VERIFY);
272 }
273 else {
274 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,
275 ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
276 }
277 offset += 2;
278  
279 while (1) {
280 sub_item = proto_tree_add_item(tree, hf_pn_rt_sf, tvb, offset, 0, ENC_NA);
281 sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_sf);
282 u32SubStart = offset;
283  
284 u8SFPosition = tvb_get_guint8(tvb, offset);
285 proto_tree_add_uint(sub_tree, hf_pn_rt_sf_position, tvb, offset, 1, u8SFPosition);
286 offset += 1;
287  
288 u8SFDataLength = tvb_get_guint8(tvb, offset);
289 proto_tree_add_uint(sub_tree, hf_pn_rt_sf_data_length, tvb, offset, 1, u8SFDataLength);
290 offset += 1;
291  
292 if (u8SFDataLength == 0) {
293 proto_item_append_text(sub_item, ": Pos:%u, Length:%u", u8SFPosition, u8SFDataLength);
294 proto_item_set_len(sub_item, offset - u32SubStart);
295 break;
296 }
297  
298 u8SFCycleCounter = tvb_get_guint8(tvb, offset);
299 proto_tree_add_uint(sub_tree, hf_pn_rt_sf_cycle_counter, tvb, offset, 1, u8SFCycleCounter);
300 offset += 1;
301  
302 u8SFDataStatus = tvb_get_guint8(tvb, offset);
303 dissect_DataStatus(tvb, offset, sub_tree, u8SFDataStatus);
304 offset += 1;
305  
306 offset = dissect_pn_user_data(tvb, offset, pinfo, sub_tree, u8SFDataLength, "DataItem");
307  
308 u16SFCRC16 = tvb_get_letohs(tvb, offset);
309  
310 if (u16SFCRC16 != 0 /* "old check": u8SFPosition & 0x80 */) {
311 crc = crc16_plain_tvb_offset_seed(tvb, u32SubStart, offset-u32SubStart, 0);
312 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,
313 ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_VERIFY);
314 } else {
315 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,
316 ENC_LITTLE_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
317 }
318 offset += 2;
319  
320 proto_item_append_text(sub_item, ": Pos:%u, Length:%u, Cycle:%u, Status: 0x%02x (%s,%s,%s,%s)",
321 u8SFPosition, u8SFDataLength, u8SFCycleCounter, u8SFDataStatus,
322 (u8SFDataStatus & 0x04) ? "Valid" : "Invalid",
323 (u8SFDataStatus & 0x01) ? "Primary" : "Backup",
324 (u8SFDataStatus & 0x20) ? "Ok" : "Problem",
325 (u8SFDataStatus & 0x10) ? "Run" : "Stop");
326  
327 proto_item_set_len(sub_item, offset - u32SubStart);
328 }
329  
330 return TRUE;
331 }
332  
333 return FALSE;
334  
335 }
336  
337 /* for reasemble processing we need some inits.. */
338 /* Register PNIO defrag table init routine. */
339  
340 static reassembly_table pdu_reassembly_table;
341 static GHashTable *reasembled_frag_table = NULL;
342  
343 static dissector_table_t ethertype_subdissector_table;
344  
345 static guint32 start_frag_OR_ID[16];
346  
347  
348 static void
349 pnio_defragment_init(void)
350 {
351 guint32 i;
352 for (i=0; i < 16; i++) /* init the reasemble help array */
353 start_frag_OR_ID[i] = 0;
354 reassembly_table_init(&pdu_reassembly_table,
355 &addresses_reassembly_table_functions);
356 reasembled_frag_table = g_hash_table_new(NULL, NULL);
357 }
358  
359 static void
360 pnio_defragment_cleanup(void)
361 {
362 g_hash_table_destroy(reasembled_frag_table);
363 reassembly_table_destroy(&pdu_reassembly_table);
364 }
365  
366 /* possibly dissect a FRAG_PDU related PN-RT packet */
367 static gboolean
368 dissect_FRAG_PDU_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
369 {
370 /* the sub tvb will NOT contain the frame_id here! */
371 guint16 u16FrameID = GPOINTER_TO_UINT(data);
372 int offset = 0;
373  
374  
375 /* possible FrameID ranges for FRAG_PDU */
376 if (u16FrameID >= 0xFF80 && u16FrameID <= 0xFF8F) {
377 proto_item *sub_item;
378 proto_tree *sub_tree;
379 proto_item *status_item;
380 proto_tree *status_tree;
381 guint8 u8FragDataLength;
382 guint8 u8FragStatus;
383 gboolean bMoreFollows;
384 guint8 uFragNumber;
385  
386 sub_item = proto_tree_add_item(tree, hf_pn_rt_frag, tvb, offset, 0, ENC_NA);
387 sub_tree = proto_item_add_subtree(sub_item, ett_pn_rt_frag);
388  
389 u8FragDataLength = tvb_get_guint8(tvb, offset);
390 proto_tree_add_uint(sub_tree, hf_pn_rt_frag_data_length, tvb, offset, 1, u8FragDataLength);
391 offset += 1;
392  
393 status_item = proto_tree_add_item(sub_tree, hf_pn_rt_frag_status, tvb, offset, 1, ENC_NA);
394 status_tree = proto_item_add_subtree(status_item, ett_pn_rt_frag_status);
395  
396 u8FragStatus = tvb_get_guint8(tvb, offset);
397 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_more_follows, tvb, offset, 1, u8FragStatus);
398 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_error, tvb, offset, 1, u8FragStatus);
399 proto_tree_add_uint(status_tree, hf_pn_rt_frag_status_fragment_number, tvb, offset, 1, u8FragStatus);
400 offset += 1;
401 uFragNumber = u8FragStatus & 0x3F; /* bits 0 to 5 */
402 bMoreFollows = (u8FragStatus & 0x80) != 0;
403 proto_item_append_text(status_item, ": Number: %u, %s",
404 uFragNumber,
405 val_to_str( (u8FragStatus & 0x80) >> 7, pn_rt_frag_status_more_follows, "Unknown"));
406  
407 /* Is this a string or a bunch of bytes? Should it be FT_BYTES? */
408 proto_tree_add_string_format(sub_tree, hf_pn_rt_frag_data, tvb, offset, tvb_captured_length_remaining(tvb, offset), "data",
409 "Fragment Length: %d bytes", tvb_captured_length_remaining(tvb, offset));
410 col_append_fstr(pinfo->cinfo, COL_INFO, " Fragment Length: %d bytes", tvb_captured_length_remaining(tvb, offset));
411  
412 dissect_pn_user_data_bytes(tvb, offset, pinfo, sub_tree, tvb_captured_length_remaining(tvb, offset), FRAG_DATA);
413 if ((guint)tvb_captured_length_remaining(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_head *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->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(&pdu_reassembly_table, tvb, offset,
435 pinfo, u32ReasembleID, NULL, uFragNumber,
436 (tvb_captured_length_remaining(tvb, offset))/*u8FragDataLength*8*/, bMoreFollows, 0);
437  
438 if (pdu_frag && !bMoreFollows) /* PDU is complete! and last fragment */
439 { /* store this fragment as the completed fragment in hash table */
440 g_hash_table_insert(reasembled_frag_table, GUINT_TO_POINTER(pinfo->num), pdu_frag);
441 start_frag_OR_ID[u32FragID] = 0; /* reset the starting frame counter */
442 }
443 if (!bMoreFollows) /* last fragment */
444 {
445 pdu_frag = (fragment_head *)g_hash_table_lookup(reasembled_frag_table, GUINT_TO_POINTER(pinfo->num));
446 if (pdu_frag) /* found a matching fragment; dissect it */
447 {
448 guint16 type;
449 tvbuff_t *pdu_tvb;
450  
451 /* create the new tvb for defragmented frame */
452 pdu_tvb = tvb_new_chain(tvb, pdu_frag->tvb_data);
453 /* add the defragmented data to the data source list */
454 add_new_data_source(pinfo, pdu_tvb, "Reassembled Profinet Frame");
455 /* PDU is complete: look for the Ethertype and give it to the appropriate dissection routine */
456 type = tvb_get_ntohs(pdu_tvb, 0);
457 pdu_tvb = tvb_new_subset_remaining(pdu_tvb, 2);
458 if (!dissector_try_uint(ethertype_subdissector_table, type, pdu_tvb, pinfo, tree))
459 call_data_dissector(pdu_tvb, pinfo, tree);
460 }
461 }
462 return TRUE;
463 }
464 else
465 return TRUE;
466 }
467 return FALSE;
468 }
469  
470  
471 /*
472 * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
473 */
474 static int
475 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
476 {
477 gint pdu_len;
478 gint data_len;
479 guint16 u16FrameID;
480 guint8 u8DataStatus;
481 guint8 u8TransferStatus;
482 guint16 u16CycleCounter;
483 const gchar *pszProtAddInfo;
484 const gchar *pszProtShort;
485 const gchar *pszProtSummary;
486 const gchar *pszProtComment;
487 proto_tree *pn_rt_tree, *ti;
488 gchar szFieldSummary[100];
489 tvbuff_t *next_tvb;
490 gboolean bCyclic;
491 heur_dtbl_entry_t *hdtbl_entry;
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 0;
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 /* get frame user data tvb (without header and footer) */
798 next_tvb = tvb_new_subset_length(tvb, 2, data_len);
799  
800 /* ask heuristics, if some sub-dissector is interested in this packet payload */
801 if (!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, GUINT_TO_POINTER( (guint32) u16FrameID))) {
802 /*col_set_str(pinfo->cinfo, COL_INFO, "Unknown");*/
803  
804 /* Oh, well, we don't know this; dissect it as data. */
805 dissect_pn_undecoded(next_tvb, 0, pinfo, tree, tvb_captured_length(next_tvb));
806 }
807 return tvb_captured_length(tvb);
808 }
809  
810  
811 /* Register all the bits needed by the filtering engine */
812 void
813 proto_register_pn_rt(void)
814 {
815 static hf_register_info hf[] = {
816 { &hf_pn_rt_frame_id,
817 { "FrameID", "pn_rt.frame_id",
818 FT_UINT16, BASE_DEC, NULL, 0x0,
819 NULL, HFILL }},
820  
821 { &hf_pn_rt_cycle_counter,
822 { "CycleCounter", "pn_rt.cycle_counter",
823 FT_UINT16, BASE_DEC, NULL, 0x0,
824 NULL, HFILL }},
825  
826 { &hf_pn_rt_data_status,
827 { "DataStatus", "pn_rt.ds",
828 FT_UINT8, BASE_HEX, 0, 0x0,
829 NULL, HFILL }},
830  
831 { &hf_pn_rt_data_status_ignore,
832 { "Ignore (1:Ignore/0:Evaluate)", "pn_rt.ds_ignore", FT_UINT8, BASE_HEX, 0, 0x80,
833 NULL, HFILL }},
834  
835 { &hf_pn_rt_data_status_Reserved_2,
836 { "Reserved_2 (should be zero)", "pn_rt.ds_Reserved_2",
837 FT_UINT8, BASE_HEX, 0, 0x40,
838 NULL, HFILL }},
839  
840 { &hf_pn_rt_data_status_ok,
841 { "StationProblemIndicator (1:Ok/0:Problem)", "pn_rt.ds_ok",
842 FT_UINT8, BASE_HEX, 0, 0x20,
843 NULL, HFILL }},
844  
845 { &hf_pn_rt_data_status_operate,
846 { "ProviderState (1:Run/0:Stop)", "pn_rt.ds_operate",
847 FT_UINT8, BASE_HEX, 0, 0x10,
848 NULL, HFILL }},
849  
850 { &hf_pn_rt_data_status_res3,
851 { "Reserved_1 (should be zero)", "pn_rt.ds_res3",
852 FT_UINT8, BASE_HEX, 0, 0x08,
853 NULL, HFILL }},
854  
855 { &hf_pn_rt_data_status_valid,
856 { "DataValid (1:Valid/0:Invalid)", "pn_rt.ds_valid",
857 FT_UINT8, BASE_HEX, 0, 0x04,
858 NULL, HFILL }},
859  
860 { &hf_pn_rt_data_status_redundancy,
861 { "Redundancy", "pn_rt.ds_redundancy",
862 FT_UINT8, BASE_HEX, VALS(pn_rt_ds_redundancy), 0x02,
863 NULL, HFILL }},
864  
865 { &hf_pn_rt_data_status_primary,
866 { "State (1:Primary/0:Backup)", "pn_rt.ds_primary",
867 FT_UINT8, BASE_HEX, 0, 0x01,
868 NULL, HFILL }},
869  
870 { &hf_pn_rt_transfer_status,
871 { "TransferStatus", "pn_rt.transfer_status",
872 FT_UINT8, BASE_DEC, NULL, 0x0,
873 NULL, HFILL }},
874  
875 { &hf_pn_rt_sf,
876 { "SubFrame", "pn_rt.sf",
877 FT_NONE, BASE_NONE, NULL, 0x0,
878 NULL, HFILL }},
879  
880 { &hf_pn_rt_sf_crc16,
881 { "SFCRC16", "pn_rt.sf.crc16",
882 FT_UINT16, BASE_HEX, NULL, 0x0,
883 NULL, HFILL }},
884  
885 { &hf_pn_rt_sf_crc16_status,
886 { "SFCRC16 status", "pn_rt.sf.crc16.status",
887 FT_UINT8, BASE_NONE, VALS(plugin_proto_checksum_vals), 0x0,
888 NULL, HFILL }},
889  
890 { &hf_pn_rt_sf_position,
891 { "Position", "pn_rt.sf.position",
892 FT_UINT8, BASE_DEC, NULL, 0x7F,
893 NULL, HFILL }},
894  
895 #if 0
896 { &hf_pn_rt_sf_position_control,
897 { "Control", "pn_rt.sf.position_control",
898 FT_UINT8, BASE_DEC, VALS(pn_rt_position_control), 0x80,
899 NULL, HFILL }},
900 #endif
901  
902 { &hf_pn_rt_sf_data_length,
903 { "DataLength", "pn_rt.sf.data_length",
904 FT_UINT8, BASE_DEC, NULL, 0x0,
905 NULL, HFILL }},
906  
907 { &hf_pn_rt_sf_cycle_counter,
908 { "CycleCounter", "pn_rt.sf.cycle_counter",
909 FT_UINT8, BASE_DEC, NULL, 0x0,
910 NULL, HFILL }},
911  
912 { &hf_pn_rt_frag,
913 { "PROFINET Fragment", "pn_rt.frag",
914 FT_NONE, BASE_NONE, NULL, 0x0,
915 NULL, HFILL }},
916  
917 { &hf_pn_rt_frag_data_length,
918 { "FragDataLength", "pn_rt.frag_data_length",
919 FT_UINT8, BASE_DEC, NULL, 0x0,
920 NULL, HFILL }},
921  
922 { &hf_pn_rt_frag_status,
923 { "FragStatus", "pn_rt.frag_status",
924 FT_NONE, BASE_NONE, NULL, 0x0,
925 NULL, HFILL }},
926  
927 { &hf_pn_rt_frag_status_more_follows,
928 { "MoreFollows", "pn_rt.frag_status.more_follows",
929 FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_more_follows), 0x80,
930 NULL, HFILL }},
931  
932 { &hf_pn_rt_frag_status_error,
933 { "Reserved", "pn_rt.frag_status.error",
934 FT_UINT8, BASE_HEX, VALS(pn_rt_frag_status_error), 0x40,
935 NULL, HFILL }},
936  
937 { &hf_pn_rt_frag_status_fragment_number,
938 { "FragmentNumber (zero based)", "pn_rt.frag_status.fragment_number",
939 FT_UINT8, BASE_DEC, NULL, 0x3F,
940 NULL, HFILL }},
941  
942 /* Is this a string or a bunch of bytes? Should it be FT_BYTES? */
943 { &hf_pn_rt_frag_data,
944 { "FragData", "pn_rt.frag_data",
945 FT_STRING, BASE_NONE, NULL, 0x00,
946 NULL, HFILL }},
947  
948 };
949 static gint *ett[] = {
950 &ett_pn_rt,
951 &ett_pn_rt_data_status,
952 &ett_pn_rt_sf,
953 &ett_pn_rt_frag,
954 &ett_pn_rt_frag_status
955 };
956  
957 static ei_register_info ei[] = {
958 { &ei_pn_rt_sf_crc16, { "pn_rt.sf.crc16_bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
959 };
960  
961 module_t *pn_rt_module;
962 expert_module_t* expert_pn_rt;
963  
964 proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
965 "PN-RT", "pn_rt");
966  
967 proto_register_field_array(proto_pn_rt, hf, array_length(hf));
968 proto_register_subtree_array(ett, array_length(ett));
969 expert_pn_rt = expert_register_protocol(proto_pn_rt);
970 expert_register_field_array(expert_pn_rt, ei, array_length(ei));
971  
972 /* Register our configuration options */
973  
974 pn_rt_module = prefs_register_protocol(proto_pn_rt, NULL);
975  
976 prefs_register_bool_preference(pn_rt_module, "summary_in_tree",
977 "Show PN-RT summary in protocol tree",
978 "Whether the PN-RT summary line should be shown in the protocol tree",
979 &pn_rt_summary_in_tree);
980  
981 prefs_register_bool_preference(pn_rt_module, "desegment",
982 "reassemble PNIO Fragments",
983 "Reassemble PNIO Fragments and get them decoded",
984 &pnio_desegment);
985  
986 /* register heuristics anchor for payload dissectors */
987 heur_subdissector_list = register_heur_dissector_list("pn_rt", proto_pn_rt);
988  
989 init_pn (proto_pn_rt);
990 register_init_routine(pnio_defragment_init);
991 register_cleanup_routine(pnio_defragment_cleanup);
992 }
993  
994  
995 /* The registration hand-off routine is called at startup */
996 void
997 proto_reg_handoff_pn_rt(void)
998 {
999 dissector_handle_t pn_rt_handle;
1000  
1001 pn_rt_handle = create_dissector_handle(dissect_pn_rt, proto_pn_rt);
1002  
1003 dissector_add_uint("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
1004 dissector_add_uint("udp.port", 0x8892, pn_rt_handle);
1005  
1006 heur_dissector_add("pn_rt", dissect_CSF_SDU_heur, "PROFINET CSF_SDU IO", "pn_csf_sdu_pn_rt", proto_pn_rt, HEURISTIC_ENABLE);
1007 heur_dissector_add("pn_rt", dissect_FRAG_PDU_heur, "PROFINET Frag PDU IO", "pn_frag_pn_rt", proto_pn_rt, HEURISTIC_ENABLE);
1008  
1009 ethertype_subdissector_table = find_dissector_table("ethertype");
1010 }
1011  
1012  
1013 /*
1014 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1015 *
1016 * Local variables:
1017 * c-basic-offset: 4
1018 * tab-width: 8
1019 * indent-tabs-mode: nil
1020 * End:
1021 *
1022 * vi: set shiftwidth=4 tabstop=8 expandtab:
1023 * :indentSize=4:tabSize=8:noTabs=true:
1024 */