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)
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* we do not use this dissector as a plugin any longer... */
32 #define __ETHEREAL_STATIC__
34 #ifndef __ETHEREAL_STATIC__
35 #include "plugins/plugin_api.h"
36 #include "moduleinfo.h"
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
54 #include <epan/packet.h>
55 #include <epan/addr_resolv.h>
57 #include <epan/strutil.h>
61 #ifndef __ETHEREAL_STATIC__
62 #include "plugins/plugin_api_defs.h"
64 G_MODULE_EXPORT const gchar version[] = VERSION;
67 void proto_reg_handoff_pn_rt(void);
69 /* Define the pn-rt proto */
70 static int proto_pn_rt = -1;
72 /* Define many header fields for pn-rt */
73 static int hf_pn_rt_id = -1;
74 static int hf_pn_rt_data = -1;
75 static int hf_pn_rt_cycle_counter = -1;
76 static int hf_pn_rt_transfer_status = -1;
77 static int hf_pn_rt_data_status = -1;
78 static int hf_pn_rt_data_status_res67 = -1;
79 static int hf_pn_rt_data_status_ok = -1;
80 static int hf_pn_rt_data_status_operate = -1;
81 static int hf_pn_rt_data_status_res3 = -1;
82 static int hf_pn_rt_data_status_valid = -1;
83 static int hf_pn_rt_data_status_res1 = -1;
84 static int hf_pn_rt_data_status_primary = -1;
85 static int hf_pn_rt_malformed = -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;
95 * Here are the global variables associated with
96 * the various user definable characteristics of the dissection
98 /* Place summary in proto tree */
99 static gboolean pn_rt_summary_in_tree = TRUE;
101 /* heuristic to find the right pn-rt payload dissector */
102 static heur_dissector_list_t heur_subdissector_list;
104 /* the official "we don't know that data" dissector */
105 static dissector_handle_t data_handle;
109 * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
112 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
118 guint8 u8TransferStatus;
119 guint16 u16CycleCounter;
120 gchar *pszProtAddInfo;
122 gchar *pszProtSummary;
123 gchar *pszProtComment;
124 proto_item *item = NULL;
125 proto_tree *ds_tree = NULL;
126 proto_tree *pn_rt_tree, *ti;
127 gchar szFieldSummary[100];
132 /* Initialize variables */
133 tvb_len = tvb_length(tvb);
139 * Set the columns now, so that they'll be set correctly if we throw
140 * an exception. We can set them (or append things) later again ....
142 if (check_col(pinfo->cinfo, COL_PROTOCOL))
143 col_add_str(pinfo->cinfo, COL_PROTOCOL, "PN-RT");
144 if (check_col(pinfo->cinfo, COL_INFO))
145 col_add_str(pinfo->cinfo, COL_INFO, "PROFINET Real-Time");
148 /* packet is too short, mark it as malformed */
149 proto_tree_add_bytes(tree, hf_pn_rt_malformed, tvb, 0, 10000,
150 tvb_get_ptr(tvb, 0, 10000));
154 /* build protocol tree only, if tree is really used */
156 /* build some "raw" data */
157 u16FrameID = tvb_get_ntohs(tvb, 0);
158 if (u16FrameID < 0x0100) {
159 pszProtShort = "PN-RTC0";
160 pszProtAddInfo = "Synchronization, ";
161 pszProtSummary = "Isochronous-Real-Time";
162 pszProtComment = "0x0000-0x00FF: Isochronous-Real-Time: Time-sync";
164 } else if (u16FrameID < 0x8000){
165 pszProtShort = "PN-RTC3";
166 pszProtAddInfo = "RTC3, ";
167 pszProtSummary = "Isochronous-Real-Time";
168 pszProtComment = "0x0100-0x7FFF: Isochronous-Real-Time(class=3): Cyclic";
170 } else if (u16FrameID < 0xbf00){
171 pszProtShort = "PN-RTC2";
172 pszProtAddInfo = "RTC2, ";
173 pszProtSummary = "cyclic Real-Time";
174 pszProtComment = "0x8000-0xBEFF: Real-Time(class=2): Cyclic";
176 } else if (u16FrameID < 0xc000){
177 pszProtShort = "PN-RTC2";
178 pszProtAddInfo = "Multicast, ";
179 pszProtSummary = "cyclic Real-Time";
180 pszProtComment = "0xBF00-0xBFFF: Real-Time(class=2 multicast): Cyclic";
182 } else if (u16FrameID < 0xfb00){
183 pszProtShort = "PN-RTC1";
184 pszProtAddInfo = "RTC1, ";
185 pszProtSummary = "cyclic Real-Time";
186 pszProtComment = "0xC000-0xFAFF: Real-Time(class=1): Cyclic";
188 } else if (u16FrameID < 0xfc00){
189 pszProtShort = "PN-RTC1";
190 pszProtAddInfo = "Multicast, ";
191 pszProtSummary = "cyclic Real-Time";
192 pszProtComment = "0xFB00-0xFBFF: Real-Time(class=1 multicast): Cyclic";
194 } else if (u16FrameID < 0xfe00){
195 pszProtShort = "PN-RTA";
196 pszProtAddInfo = "Reserved, ";
197 pszProtSummary = "acyclic Real-Time";
198 pszProtComment = "0xFC00-0xFDFF: Real-Time: Acyclic high priority";
200 if (u16FrameID == 0xfc01) {
201 pszProtShort = "PN-RTA";
202 pszProtAddInfo = "Alarm High, ";
203 pszProtSummary = "acyclic Real-Time";
204 pszProtComment = "Real-Time: Acyclic PN-IO Alarm high priority";
206 } else if (u16FrameID < 0xff00){
207 pszProtShort = "PN-RTA";
208 pszProtAddInfo = "Reserved, ";
209 pszProtSummary = "acyclic Real-Time";
210 pszProtComment = "0xFE00-0xFEFF: Real-Time: Acyclic low priority";
212 if (u16FrameID == 0xfe01) {
213 pszProtShort = "PN-RTA";
214 pszProtAddInfo = "Alarm Low, ";
215 pszProtSummary = "acyclic Real-Time";
216 pszProtComment = "Real-Time: Acyclic PN-IO Alarm low priority";
218 if (u16FrameID == 0xfefd) {
219 pszProtShort = "PN-RTA";
221 pszProtSummary = "acyclic Real-Time";
222 pszProtComment = "Real-Time: DCP (Dynamic Configuration Protocol)";
224 if (u16FrameID == 0xfefe) {
225 pszProtShort = "PN-RTA";
227 pszProtSummary = "acyclic Real-Time";
228 pszProtComment = "Real-Time: DCP (Dynamic Configuration Protocol) multicast response";
230 if (u16FrameID == 0xfeff) {
231 pszProtShort = "PN-RTA";
233 pszProtSummary = "acyclic Real-Time";
234 pszProtComment = "Real-Time: DCP (Dynamic Configuration Protocol) multicast";
237 pszProtShort = "PN-RT";
238 pszProtAddInfo = "Reserved";
239 pszProtSummary = "Real-Time";
240 pszProtComment = "0xFF00-0xFFFF: reserved ID";
244 /* decode optional cyclic fields at the packet end and build the summary line */
246 /* cyclic transfer has cycle counter, data status and transfer status fields at the end */
247 u16CycleCounter = tvb_get_ntohs(tvb, tvb_len - 4);
248 u8DataStatus = tvb_get_guint8(tvb, tvb_len - 2);
249 u8TransferStatus = tvb_get_guint8(tvb, tvb_len - 1);
251 snprintf (szFieldSummary, sizeof(szFieldSummary),
252 "%sFrameID: 0x%04x, DataLen: %4u, Cycle: %5u (%s,%s,%s,%s)",
253 pszProtAddInfo, u16FrameID, tvb_len - 2 - 4, u16CycleCounter,
254 (u8DataStatus & 0x04) ? "Valid" : "Invalid",
255 (u8DataStatus & 0x01) ? "Primary" : "Backup",
256 (u8DataStatus & 0x20) ? "Ok" : "Problem",
257 (u8DataStatus & 0x10) ? "Run" : "Stop");
259 /* user data length is packet len - frame id - optional cyclic status fields */
260 data_len = tvb_len - 2 - 4;
262 /* satisfy the gcc compiler, so it won't throw an "uninitialized" warning */
265 u8TransferStatus = 0;
267 /* acyclic transfer has no fields at the end */
268 snprintf (szFieldSummary, sizeof(szFieldSummary),
269 "%sFrameID: 0x%04x, DataLen: %4u",
270 pszProtAddInfo, u16FrameID, tvb_len - 2);
272 /* user data length is packet len - frame id field */
273 data_len = tvb_len - 2;
276 /* build pn_rt protocol tree with summary line */
277 if (pn_rt_summary_in_tree) {
278 ti = proto_tree_add_protocol_format(tree, proto_pn_rt, tvb, 0, -1,
279 "PROFINET %s, %s", pszProtSummary, szFieldSummary);
281 ti = proto_tree_add_item(tree, proto_pn_rt, tvb, 0, -1, FALSE);
283 pn_rt_tree = proto_item_add_subtree(ti, ett_pn_rt);
286 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_id, tvb,
287 0, 2, u16FrameID, "FrameID: 0x%04x (%s)", u16FrameID, pszProtComment);
290 /* add cycle counter */
291 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_cycle_counter, tvb,
292 tvb_len - 4, 2, u16CycleCounter, "CycleCounter: %u", u16CycleCounter);
294 /* add data status subtree */
295 item = proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_data_status,
296 tvb, tvb_len - 2, 1, u8DataStatus,
297 "DataStatus: 0x%02x (Frame: %s and %s, Provider: %s and %s)",
299 (u8DataStatus & 0x04) ? "Valid" : "Invalid",
300 (u8DataStatus & 0x01) ? "Primary" : "Backup",
301 (u8DataStatus & 0x20) ? "Ok" : "Problem",
302 (u8DataStatus & 0x10) ? "Run" : "Stop");
303 ds_tree = proto_item_add_subtree(item, ett_pn_rt_data_status);
304 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res67, tvb, tvb_len - 2, 1, u8DataStatus);
305 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_ok, tvb, tvb_len - 2, 1, u8DataStatus);
306 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_operate, tvb, tvb_len - 2, 1, u8DataStatus);
307 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res3, tvb, tvb_len - 2, 1, u8DataStatus);
308 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_valid, tvb, tvb_len - 2, 1, u8DataStatus);
309 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res1, tvb, tvb_len - 2, 1, u8DataStatus);
310 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_primary, tvb, tvb_len - 2, 1, u8DataStatus);
312 /* add transfer status */
313 if (u8TransferStatus) {
314 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
315 tvb_len - 1, 1, u8TransferStatus,
316 "TransferStatus: 0x%02x (ignore this frame)", u8TransferStatus);
318 proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
319 tvb_len - 1, 1, u8TransferStatus,
320 "TransferStatus: 0x%02x (OK)", u8TransferStatus);
324 /* update column info now */
325 if (check_col(pinfo->cinfo, COL_INFO))
326 col_add_fstr(pinfo->cinfo, COL_INFO, szFieldSummary);
327 if (check_col(pinfo->cinfo, COL_PROTOCOL))
328 col_add_str(pinfo->cinfo, COL_PROTOCOL, pszProtShort);
330 pinfo->private_data = GUINT_TO_POINTER(u16FrameID);
332 /* get frame user data tvb (without header and footer) */
333 next_tvb = tvb_new_subset(tvb, 2, data_len, data_len);
335 /* ask heuristics, if some sub-dissector is interested in this packet payload */
336 if(!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree)) {
337 /*if (check_col(pinfo->cinfo, COL_INFO))
338 col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown");*/
340 /* Oh, well, we don't know this; dissect it as data. */
341 call_dissector(data_handle, next_tvb, pinfo, tree);
347 /* Register all the bits needed by the filtering engine */
349 proto_register_pn_rt(void)
351 static hf_register_info hf[] = {
353 { "FrameID", "pn_rt.frame_id", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
355 { "Data", "pn_rt.data", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }},
356 { &hf_pn_rt_cycle_counter, {
357 "CycleCounter", "pn_rt.cycle_counter", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
358 { &hf_pn_rt_data_status, {
359 "DataStatus", "pn_rt.ds", FT_UINT8, BASE_HEX, 0, 0x0, "", HFILL }},
360 { &hf_pn_rt_data_status_res67, {
361 "Reserved (should be zero)", "pn_rt.ds_res67", FT_UINT8, BASE_HEX, 0, 0xc0, "", HFILL }},
362 { &hf_pn_rt_data_status_ok, {
363 "StationProblemIndicator: 1:Ok/0:Problem", "pn_rt.ds_ok", FT_UINT8, BASE_HEX, 0, 0x20, "", HFILL }},
364 { &hf_pn_rt_data_status_operate, {
365 "ProviderState: 1:Run/0:Stop", "pn_rt.ds_operate", FT_UINT8, BASE_HEX, 0, 0x10, "", HFILL }},
366 { &hf_pn_rt_data_status_res3, {
367 "Reserved (should be zero)", "pn_rt.ds_res3", FT_UINT8, BASE_HEX, 0, 0x08, "", HFILL }},
368 { &hf_pn_rt_data_status_valid, {
369 "DataValid: 1:Valid/0:Invalid", "pn_rt.ds_valid", FT_UINT8, BASE_HEX, 0, 0x04, "", HFILL }},
370 { &hf_pn_rt_data_status_res1, {
371 "Reserved (should be zero)", "pn_rt.ds_res1", FT_UINT8, BASE_HEX, 0, 0x02, "", HFILL }},
372 { &hf_pn_rt_data_status_primary, {
373 "State: 1:Primary/0:Backup", "pn_rt.ds_primary", FT_UINT8, BASE_HEX, 0, 0x01, "", HFILL }},
374 { &hf_pn_rt_transfer_status,
375 { "TransferStatus", "pn_rt.transfer_status", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
376 { &hf_pn_rt_malformed,
377 { "Malformed", "pn_rt.malformed", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }}
379 static gint *ett[] = {
381 &ett_pn_rt_data_status,
383 module_t *pn_rt_module;
385 proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
388 proto_register_field_array(proto_pn_rt, hf, array_length(hf));
389 proto_register_subtree_array(ett, array_length(ett));
391 /* Register our configuration options */
393 pn_rt_module = prefs_register_protocol(proto_pn_rt, proto_reg_handoff_pn_rt);
395 prefs_register_bool_preference(pn_rt_module, "summary_in_tree",
396 "Show PN-RT summary in protocol tree",
397 "Whether the PN-RT summary line should be shown in the protocol tree",
398 &pn_rt_summary_in_tree);
400 /* register heuristics anchor for payload dissectors */
401 register_heur_dissector_list("pn_rt", &heur_subdissector_list);
405 /* The registration hand-off routine
406 * Is called at startup, and everytime the preferences of this protocol changed. */
408 proto_reg_handoff_pn_rt(void)
410 static int pn_rt_prefs_initialized = FALSE;
411 static dissector_handle_t pn_rt_handle;
414 if (!pn_rt_prefs_initialized) {
415 pn_rt_handle = create_dissector_handle(dissect_pn_rt, proto_pn_rt);
416 pn_rt_prefs_initialized = TRUE;
419 dissector_delete("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
422 dissector_add("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
424 /* the official "we don't know that data" dissector */
425 data_handle = find_dissector("data");
431 /* Start the functions we need for the plugin stuff */
433 #ifndef __ETHEREAL_STATIC__
436 plugin_reg_handoff(void){
437 proto_reg_handoff_pn_rt();
441 plugin_init(plugin_address_table_t *pat
442 #ifndef PLUGINS_NEED_ADDRESS_TABLE
446 /* initialise the table of pointers needed in Win32 DLLs */
447 plugin_address_table_init(pat);
448 /* register the new protocol, protocol fields, and subtrees */
449 if (proto_pn_rt == -1) { /* execute protocol initialization only once */
450 proto_register_pn_rt();
454 #endif /* __ETHEREAL_STATIC__ */
456 /* End the functions we need for plugin stuff */