079fdad4e4b3a599d48801cc2cecf944545ac363
[obnox/wireshark/wip.git] / epan / dissectors / 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  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 /* we do not use this dissector as a plugin any longer... */
32 #define __ETHEREAL_STATIC__
33
34 #ifndef __ETHEREAL_STATIC__
35 #include "plugins/plugin_api.h"
36 #include "moduleinfo.h"
37 #endif
38
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <gmodule.h>
51 #include <ctype.h>
52 #include <time.h>
53 #include <string.h>
54 #include <epan/packet.h>
55 #include <epan/addr_resolv.h>
56 #include "prefs.h"
57 #include <epan/strutil.h>
58 #include <etypes.h>
59
60
61 #ifndef __ETHEREAL_STATIC__
62 #include "plugins/plugin_api_defs.h"
63
64 G_MODULE_EXPORT const gchar version[] = VERSION;
65 #endif
66
67 void proto_reg_handoff_pn_rt(void);
68
69 /* Define the pn-rt proto */
70 static int proto_pn_rt     = -1;
71
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;
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
94 /* 
95  * Here are the global variables associated with  
96  * the various user definable characteristics of the dissection
97  */
98 /* Place summary in proto tree */
99 static gboolean pn_rt_summary_in_tree = TRUE;
100
101 /* heuristic to find the right pn-rt payload dissector */
102 static heur_dissector_list_t heur_subdissector_list;
103
104 /* the official "we don't know that data" dissector */
105 static dissector_handle_t data_handle;
106
107
108 /*
109  * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
110  */
111 static void
112 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
113 {
114   gint tvb_len;
115   gint data_len;
116   guint16 u16FrameID;
117   guint8 u8DataStatus;
118   guint8 u8TransferStatus;
119   guint16 u16CycleCounter;
120   gchar *pszProtAddInfo;
121   gchar *pszProtShort;
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];
128   tvbuff_t *next_tvb;
129   gboolean  bCyclic;
130
131
132   /* Initialize variables */
133   tvb_len = tvb_length(tvb);
134   pn_rt_tree = NULL;
135   ds_tree = NULL;
136   ti = NULL;
137   
138   /*
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 ....
141    */
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");
146
147   if (tvb_len < 6) {
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));
151                 return;
152   }
153
154   /* build protocol tree only, if tree is really used */
155   if (tree) {
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";
163             bCyclic         = TRUE;
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";
169             bCyclic         = TRUE;
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";
175             bCyclic         = TRUE;
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";
181             bCyclic         = TRUE;
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";
187             bCyclic         = TRUE;
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";
193             bCyclic         = TRUE;
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";
199             bCyclic         = FALSE;
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";
205                 }
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";
211             bCyclic         = FALSE;
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";
217                 }
218                 if (u16FrameID == 0xfefd) {
219                                 pszProtShort    = "PN-RTA";
220                 pszProtAddInfo  = "";
221                         pszProtSummary  = "acyclic Real-Time";
222                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol)";
223                 }
224                 if (u16FrameID == 0xfefe) {
225                                 pszProtShort    = "PN-RTA";
226                 pszProtAddInfo  = "";
227                         pszProtSummary  = "acyclic Real-Time";
228                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) multicast response";
229                 }
230                 if (u16FrameID == 0xfeff) {
231                                 pszProtShort    = "PN-RTA";
232                 pszProtAddInfo  = "";
233                         pszProtSummary  = "acyclic Real-Time";
234                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) multicast";
235                 }
236         } else {
237                         pszProtShort    = "PN-RT";
238             pszProtAddInfo  = "Reserved";
239                 pszProtSummary  = "Real-Time";
240                 pszProtComment  = "0xFF00-0xFFFF: reserved ID";
241             bCyclic         = FALSE;
242             }
243
244         /* decode optional cyclic fields at the packet end and build the summary line */
245         if (bCyclic) {
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);
250
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");
258
259             /* user data length is packet len - frame id - optional cyclic status fields */
260             data_len = tvb_len - 2 - 4;
261         } else {
262             /* satisfy the gcc compiler, so it won't throw an "uninitialized" warning */
263                     u16CycleCounter     = 0;
264                     u8DataStatus        = 0;
265                 u8TransferStatus    = 0;
266
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);
271
272             /* user data length is packet len - frame id field */
273             data_len = tvb_len - 2;
274         }
275
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);
280             } else {
281                         ti = proto_tree_add_item(tree, proto_pn_rt, tvb, 0, -1, FALSE);
282             }
283                 pn_rt_tree = proto_item_add_subtree(ti, ett_pn_rt);
284
285                 /* add frame ID */
286         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_id, tvb,
287           0, 2, u16FrameID, "FrameID: 0x%04x (%s)", u16FrameID, pszProtComment);
288
289         if (bCyclic) {
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);
293                     
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)", 
298                             u8DataStatus, 
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);
311
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);
317                     } else {
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);
321                     }
322         }
323                 
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);
329
330         pinfo->private_data = GUINT_TO_POINTER(u16FrameID);
331
332                 /* get frame user data tvb (without header and footer) */
333                 next_tvb = tvb_new_subset(tvb, 2, data_len, data_len);
334
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");*/
339
340             /* Oh, well, we don't know this; dissect it as data. */
341             call_dissector(data_handle, next_tvb, pinfo, tree);
342         }
343     }
344 }
345
346
347 /* Register all the bits needed by the filtering engine */
348 void 
349 proto_register_pn_rt(void)
350 {
351   static hf_register_info hf[] = {
352     { &hf_pn_rt_id,
353       { "FrameID", "pn_rt.frame_id", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
354     { &hf_pn_rt_data,
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 }}
378   };
379   static gint *ett[] = {
380     &ett_pn_rt,
381     &ett_pn_rt_data_status,
382   };
383   module_t *pn_rt_module; 
384
385   proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
386                                        "PN-RT", "pn_rt");
387
388   proto_register_field_array(proto_pn_rt, hf, array_length(hf));
389   proto_register_subtree_array(ett, array_length(ett));
390
391   /* Register our configuration options */
392
393   pn_rt_module = prefs_register_protocol(proto_pn_rt, proto_reg_handoff_pn_rt);
394
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);
399
400   /* register heuristics anchor for payload dissectors */
401   register_heur_dissector_list("pn_rt", &heur_subdissector_list);
402 }
403
404
405 /* The registration hand-off routine 
406  * Is called at startup, and everytime the preferences of this protocol changed. */
407 void
408 proto_reg_handoff_pn_rt(void)
409 {
410   static int pn_rt_prefs_initialized = FALSE;
411   static dissector_handle_t pn_rt_handle;
412
413
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;
417   }
418   else {
419     dissector_delete("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
420   }
421
422   dissector_add("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
423
424         /* the official "we don't know that data" dissector */
425   data_handle = find_dissector("data");
426 }
427
428
429
430
431 /* Start the functions we need for the plugin stuff */
432
433 #ifndef __ETHEREAL_STATIC__
434
435 G_MODULE_EXPORT void
436 plugin_reg_handoff(void){
437   proto_reg_handoff_pn_rt();
438 }
439
440 G_MODULE_EXPORT void
441 plugin_init(plugin_address_table_t *pat
442 #ifndef PLUGINS_NEED_ADDRESS_TABLE
443 _U_
444 #endif
445 ){
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();
451   }
452 }
453
454 #endif /* __ETHEREAL_STATIC__ */
455
456 /* End the functions we need for plugin stuff */