(on behalf of the PROFIBUS Nutzerorganisation e.V. Deutschland):
[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.\r
3  * This is the base for other PROFINET protocols like IO, CBA, DCP, ...\r
4  * (the "content subdissectors" will register themselves using a heuristic)
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 /* we do not use this dissector as a plugin any longer... */
30 #define __ETHEREAL_STATIC__
31
32 #ifndef __ETHEREAL_STATIC__
33 #include "plugins/plugin_api.h"
34 #include "moduleinfo.h"
35 #endif
36
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <gmodule.h>
49 #include <ctype.h>
50 #include <time.h>
51 #include <string.h>
52 #include <epan/packet.h>
53 #include <epan/addr_resolv.h>
54 #include "prefs.h"
55 #include <epan/strutil.h>\r
56 #include <etypes.h>\r
57
58
59 #ifndef __ETHEREAL_STATIC__
60 #include "plugins/plugin_api_defs.h"
61
62 G_MODULE_EXPORT const gchar version[] = VERSION;
63 #endif
64
65 void proto_reg_handoff_pn_rt(void);
66
67 /* Define the pn-rt proto */
68 static int proto_pn_rt     = -1;
69
70 /* Define many header fields for pn-rt */
71 static int hf_pn_rt_id = -1;
72 static int hf_pn_rt_data = -1;
73 static int hf_pn_rt_cycle_counter = -1;
74 static int hf_pn_rt_transfer_status = -1;
75 static int hf_pn_rt_data_status = -1;
76 static int hf_pn_rt_data_status_res67 = -1;
77 static int hf_pn_rt_data_status_ok = -1;
78 static int hf_pn_rt_data_status_operate = -1;
79 static int hf_pn_rt_data_status_res3 = -1;
80 static int hf_pn_rt_data_status_valid = -1;
81 static int hf_pn_rt_data_status_res1 = -1;
82 static int hf_pn_rt_data_status_primary = -1;
83 static int hf_pn_rt_malformed = -1;
84
85 /* 
86  * Define the trees for pn-rt
87  * We need one tree for pn-rt itself and one for the pn-rt data status subtree
88  */
89 static int ett_pn_rt = -1;
90 static int ett_pn_rt_data_status = -1;
91
92 /* 
93  * Here are the global variables associated with  
94  * the various user definable characteristics of the dissection
95  */
96 /* Place summary in proto tree */
97 static gboolean pn_rt_summary_in_tree = TRUE;
98
99 /* heuristic to find the right pn-rt payload dissector */\r
100 static heur_dissector_list_t heur_subdissector_list;\r
101 \r
102 /* the official "we don't know that data" dissector */\r
103 static dissector_handle_t data_handle;\r
104 \r
105
106 /*
107  * dissect_pn_rt - The dissector for the Soft-Real-Time protocol
108  */
109 static void
110 dissect_pn_rt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
111 {
112   gint tvb_len;
113   gint data_len;
114   guint16 u16FrameID;
115   guint8 u8DataStatus;
116   guint8 u8TransferStatus;
117   guint16 u16CycleCounter;
118   gchar *pszProtAddInfo;
119   gchar *pszProtShort;
120   gchar *pszProtSummary;
121   gchar *pszProtComment;
122   proto_item *item = NULL;
123   proto_tree *ds_tree = NULL;
124   proto_tree *pn_rt_tree, *ti;
125   gchar szFieldSummary[100];
126   tvbuff_t *next_tvb;
127   gboolean  bCyclic;
128
129
130   /* Initialize variables */
131   tvb_len = tvb_length(tvb);
132   pn_rt_tree = NULL;
133   ds_tree = NULL;
134   ti = NULL;
135   
136   /*
137    * Set the columns now, so that they'll be set correctly if we throw
138    * an exception.  We can set them (or append things) later again ....
139    */
140   if (check_col(pinfo->cinfo, COL_PROTOCOL))
141     col_add_str(pinfo->cinfo, COL_PROTOCOL, "PN-RT");
142   if (check_col(pinfo->cinfo, COL_INFO))
143     col_add_str(pinfo->cinfo, COL_INFO, "PROFINET Real-Time");
144
145   if (tvb_len < 6) {
146                 /* packet is too short, mark it as malformed */
147                 proto_tree_add_bytes(tree, hf_pn_rt_malformed, tvb, 0, 10000, 
148                         tvb_get_ptr(tvb, 0, 10000));
149                 return;
150   }
151
152   /* build protocol tree only, if tree is really used */
153   if (tree) {
154         /* build some "raw" data */
155                 u16FrameID = tvb_get_ntohs(tvb, 0);\r
156         if (u16FrameID < 0x0100) {
157                 pszProtShort    = "PN-RTC0";
158             pszProtAddInfo  = "Synchronization, ";
159                 pszProtSummary  = "Isochronous-Real-Time";
160                 pszProtComment  = "0x0000-0x00FF: Isochronous-Real-Time: Time-sync";
161             bCyclic         = TRUE;
162         } else if (u16FrameID < 0x8000){
163                 pszProtShort    = "PN-RTC3";
164             pszProtAddInfo  = "RTC3, ";
165                 pszProtSummary  = "Isochronous-Real-Time";
166                 pszProtComment  = "0x0100-0x7FFF: Isochronous-Real-Time(class=3): Cyclic";
167             bCyclic         = TRUE;
168         } else if (u16FrameID < 0xbf00){
169                 pszProtShort    = "PN-RTC2";
170             pszProtAddInfo  = "RTC2, ";
171                 pszProtSummary  = "cyclic Real-Time";
172                 pszProtComment  = "0x8000-0xBEFF: Real-Time(class=2): Cyclic";
173             bCyclic         = TRUE;
174         } else if (u16FrameID < 0xc000){
175                 pszProtShort    = "PN-RTC2";
176             pszProtAddInfo  = "Multicast, ";
177                 pszProtSummary  = "cyclic Real-Time";
178                 pszProtComment  = "0xBF00-0xBFFF: Real-Time(class=2 multicast): Cyclic";
179             bCyclic         = TRUE;
180         } else if (u16FrameID < 0xfb00){
181                 pszProtShort    = "PN-RTC1";
182             pszProtAddInfo  = "RTC1, ";
183                 pszProtSummary  = "cyclic Real-Time";
184                 pszProtComment  = "0xC000-0xFAFF: Real-Time(class=1): Cyclic";
185             bCyclic         = TRUE;
186         } else if (u16FrameID < 0xfc00){
187                 pszProtShort    = "PN-RTC1";
188             pszProtAddInfo  = "Multicast, ";
189                 pszProtSummary  = "cyclic Real-Time";
190                 pszProtComment  = "0xFB00-0xFBFF: Real-Time(class=1 multicast): Cyclic";
191             bCyclic         = TRUE;
192         } else if (u16FrameID < 0xfe00){
193                 pszProtShort    = "PN-RTA";
194             pszProtAddInfo  = "Reserved, ";
195                 pszProtSummary  = "acyclic Real-Time";
196                 pszProtComment  = "0xFC00-0xFDFF: Real-Time: Acyclic high priority";
197             bCyclic         = FALSE;
198                 if (u16FrameID == 0xfc01) {
199                                 pszProtShort    = "PN-RTA";
200                 pszProtAddInfo  = "Alarm High, ";
201                         pszProtSummary  = "acyclic Real-Time";
202                         pszProtComment  = "Real-Time: Acyclic PN-IO Alarm high priority";
203                 }
204         } else if (u16FrameID < 0xff00){
205                 pszProtShort    = "PN-RTA";
206             pszProtAddInfo  = "Reserved, ";
207                 pszProtSummary  = "acyclic Real-Time";
208                 pszProtComment  = "0xFE00-0xFEFF: Real-Time: Acyclic low priority";
209             bCyclic         = FALSE;
210                 if (u16FrameID == 0xfe01) {
211                                 pszProtShort    = "PN-RTA";
212                 pszProtAddInfo  = "Alarm Low, ";
213                         pszProtSummary  = "acyclic Real-Time";
214                         pszProtComment  = "Real-Time: Acyclic PN-IO Alarm low priority";
215                 }
216                 if (u16FrameID == 0xfefd) {
217                                 pszProtShort    = "PN-RTA";
218                 pszProtAddInfo  = "";
219                         pszProtSummary  = "acyclic Real-Time";
220                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol)";
221                 }
222                 if (u16FrameID == 0xfefe) {
223                                 pszProtShort    = "PN-RTA";
224                 pszProtAddInfo  = "";
225                         pszProtSummary  = "acyclic Real-Time";
226                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) multicast response";
227                 }
228                 if (u16FrameID == 0xfeff) {
229                                 pszProtShort    = "PN-RTA";
230                 pszProtAddInfo  = "";
231                         pszProtSummary  = "acyclic Real-Time";
232                         pszProtComment  = "Real-Time: DCP (Dynamic Configuration Protocol) multicast";
233                 }
234         } else {
235                         pszProtShort    = "PN-RT";
236             pszProtAddInfo  = "Reserved";
237                 pszProtSummary  = "Real-Time";
238                 pszProtComment  = "0xFF00-0xFFFF: reserved ID";
239             bCyclic         = FALSE;
240             }
241 \r
242         /* decode optional cyclic fields at the packet end and build the summary line */
243         if (bCyclic) {\r
244             /* cyclic transfer has cycle counter, data status and transfer status fields at the end */
245                     u16CycleCounter = tvb_get_ntohs(tvb, tvb_len - 4);
246                     u8DataStatus = tvb_get_guint8(tvb, tvb_len - 2);
247                 u8TransferStatus = tvb_get_guint8(tvb, tvb_len - 1);
248
249                     snprintf (szFieldSummary, sizeof(szFieldSummary),
250                                       "%sFrameID: 0x%04x, DataLen: %4u, Cycle: %5u (%s,%s,%s,%s)",
251                                     pszProtAddInfo, u16FrameID, tvb_len - 2 - 4, u16CycleCounter,
252                                 (u8DataStatus & 0x04) ? "Valid" : "Invalid",
253                                 (u8DataStatus & 0x01) ? "Primary" : "Backup",
254                                 (u8DataStatus & 0x20) ? "Ok" : "Problem",
255                                 (u8DataStatus & 0x10) ? "Run" : "Stop");
256 \r
257             /* user data length is packet len - frame id - optional cyclic status fields */\r
258             data_len = tvb_len - 2 - 4;\r
259         } else {
260             /* acyclic transfer has no fields at the end */\r
261                     snprintf (szFieldSummary, sizeof(szFieldSummary),
262                                       "%sFrameID: 0x%04x, DataLen: %4u",
263                                     pszProtAddInfo, u16FrameID, tvb_len - 2);
264 \r
265             /* user data length is packet len - frame id field */\r
266             data_len = tvb_len - 2;\r
267         }
268
269                 /* build pn_rt protocol tree with summary line */
270             if (pn_rt_summary_in_tree) {
271               ti = proto_tree_add_protocol_format(tree, proto_pn_rt, tvb, 0, -1,
272                                 "PROFINET %s, %s", pszProtSummary, szFieldSummary);
273             } else {
274                         ti = proto_tree_add_item(tree, proto_pn_rt, tvb, 0, -1, FALSE);
275             }
276                 pn_rt_tree = proto_item_add_subtree(ti, ett_pn_rt);
277
278                 /* add frame ID */
279         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_id, tvb,
280           0, 2, u16FrameID, "FrameID: 0x%04x (%s)", u16FrameID, pszProtComment);
281
282         if (bCyclic) {
283                     /* add cycle counter */
284             proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_cycle_counter, tvb,
285               tvb_len - 4, 2, u16CycleCounter, "CycleCounter: %u", u16CycleCounter);
286                     
287             /* add data status subtree */
288                     item = proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_data_status, 
289                             tvb, tvb_len - 2, 1, u8DataStatus,
290                             "DataStatus: 0x%02x (Frame: %s and %s, Provider: %s and %s)", 
291                             u8DataStatus, 
292                             (u8DataStatus & 0x04) ? "Valid" : "Invalid",
293                             (u8DataStatus & 0x01) ? "Primary" : "Backup",
294                             (u8DataStatus & 0x20) ? "Ok" : "Problem",
295                             (u8DataStatus & 0x10) ? "Run" : "Stop");
296                     ds_tree = proto_item_add_subtree(item, ett_pn_rt_data_status);
297                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res67, tvb, tvb_len - 2, 1, u8DataStatus);
298                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_ok, tvb, tvb_len - 2, 1, u8DataStatus);
299                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_operate, tvb, tvb_len - 2, 1, u8DataStatus);
300                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res3, tvb, tvb_len - 2, 1, u8DataStatus);
301                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_valid, tvb, tvb_len - 2, 1, u8DataStatus);
302                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_res1, tvb, tvb_len - 2, 1, u8DataStatus);
303                 proto_tree_add_uint(ds_tree, hf_pn_rt_data_status_primary, tvb, tvb_len - 2, 1, u8DataStatus);
304
305                 /* add transfer status */
306                 if (u8TransferStatus) {
307                         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
308                             tvb_len - 1, 1, u8TransferStatus, 
309                                     "TransferStatus: 0x%02x (ignore this frame)", u8TransferStatus);
310                     } else {
311                         proto_tree_add_uint_format(pn_rt_tree, hf_pn_rt_transfer_status, tvb,
312                             tvb_len - 1, 1, u8TransferStatus, 
313                                     "TransferStatus: 0x%02x (OK)", u8TransferStatus);
314                     }
315         }
316                 
317                 /* update column info now */
318         if (check_col(pinfo->cinfo, COL_INFO))
319           col_add_fstr(pinfo->cinfo, COL_INFO, szFieldSummary);\r
320                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
321                 col_add_str(pinfo->cinfo, COL_PROTOCOL, pszProtShort);
322 \r
323         pinfo->private_data = GINT_TO_POINTER(u16FrameID);\r
324 \r
325                 /* get frame user data tvb (without header and footer) */\r
326                 next_tvb = tvb_new_subset(tvb, 2, data_len, data_len);\r
327 \r
328         /* ask heuristics, if some sub-dissector is interested in this packet payload */\r
329         if(!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree)) {\r
330             /*if (check_col(pinfo->cinfo, COL_INFO))\r
331                   col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown");*/\r
332 \r
333             /* Oh, well, we don't know this; dissect it as data. */\r
334             call_dissector(data_handle, next_tvb, pinfo, tree);\r
335         }\r
336     }
337 }
338
339
340 /* Register all the bits needed by the filtering engine */
341 void 
342 proto_register_pn_rt(void)
343 {
344   static hf_register_info hf[] = {
345     { &hf_pn_rt_id,
346       { "FrameID", "pn_rt.frame_id", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
347     { &hf_pn_rt_data,
348       { "Data", "pn_rt.data", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }},
349         { &hf_pn_rt_cycle_counter, { 
350                 "CycleCounter", "pn_rt.cycle_counter", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
351         { &hf_pn_rt_data_status, { 
352                 "DataStatus", "pn_rt.ds", FT_UINT8, BASE_HEX, 0, 0x0, "", HFILL }},
353         { &hf_pn_rt_data_status_res67, { 
354                 "Reserved (should be zero)", "pn_rt.ds_res67", FT_UINT8, BASE_HEX, 0, 0xc0, "", HFILL }},
355         { &hf_pn_rt_data_status_ok, { 
356                 "StationProblemIndicator: 1:Ok/0:Problem", "pn_rt.ds_ok", FT_UINT8, BASE_HEX, 0, 0x20, "", HFILL }},
357         { &hf_pn_rt_data_status_operate, { 
358                 "ProviderState: 1:Run/0:Stop", "pn_rt.ds_operate", FT_UINT8, BASE_HEX, 0, 0x10, "", HFILL }},
359         { &hf_pn_rt_data_status_res3, { 
360                 "Reserved (should be zero)", "pn_rt.ds_res3", FT_UINT8, BASE_HEX, 0, 0x08, "", HFILL }},
361         { &hf_pn_rt_data_status_valid, { 
362                 "DataValid: 1:Valid/0:Invalid", "pn_rt.ds_valid", FT_UINT8, BASE_HEX, 0, 0x04, "", HFILL }},
363         { &hf_pn_rt_data_status_res1, { 
364                 "Reserved (should be zero)", "pn_rt.ds_res1", FT_UINT8, BASE_HEX, 0, 0x02, "", HFILL }},\r
365         { &hf_pn_rt_data_status_primary, { 
366                 "State: 1:Primary/0:Backup", "pn_rt.ds_primary", FT_UINT8, BASE_HEX, 0, 0x01, "", HFILL }},
367     { &hf_pn_rt_transfer_status,
368       { "TransferStatus", "pn_rt.transfer_status", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
369         { &hf_pn_rt_malformed,
370         { "Malformed", "pn_rt.malformed", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }}
371   };
372   static gint *ett[] = {
373     &ett_pn_rt,
374     &ett_pn_rt_data_status,
375   };
376   module_t *pn_rt_module; 
377
378   proto_pn_rt = proto_register_protocol("PROFINET Real-Time Protocol",
379                                        "PN-RT", "pn_rt");
380
381   proto_register_field_array(proto_pn_rt, hf, array_length(hf));
382   proto_register_subtree_array(ett, array_length(ett));
383
384   /* Register our configuration options */
385
386   pn_rt_module = prefs_register_protocol(proto_pn_rt, proto_reg_handoff_pn_rt);
387
388   prefs_register_bool_preference(pn_rt_module, "summary_in_tree",
389             "Show PN-RT summary in protocol tree",
390             "Whether the PN-RT summary line should be shown in the protocol tree",
391             &pn_rt_summary_in_tree);\r
392 \r
393   /* register heuristics anchor for payload dissectors */\r
394   register_heur_dissector_list("pn_rt", &heur_subdissector_list);\r
395 }
396
397
398 /* The registration hand-off routine 
399  * Is called at startup, and everytime the preferences of this protocol changed. */
400 void
401 proto_reg_handoff_pn_rt(void)
402 {
403   static int pn_rt_prefs_initialized = FALSE;
404   static dissector_handle_t pn_rt_handle;
405
406
407   if (!pn_rt_prefs_initialized) {
408     pn_rt_handle = create_dissector_handle(dissect_pn_rt, proto_pn_rt);
409     pn_rt_prefs_initialized = TRUE;
410   }
411   else {
412     dissector_delete("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);
413   }
414
415   dissector_add("ethertype", ETHERTYPE_PROFINET, pn_rt_handle);\r
416 \r
417         /* the official "we don't know that data" dissector */\r
418   data_handle = find_dissector("data");\r
419 }
420
421
422
423
424 /* Start the functions we need for the plugin stuff */
425
426 #ifndef __ETHEREAL_STATIC__
427
428 G_MODULE_EXPORT void
429 plugin_reg_handoff(void){
430   proto_reg_handoff_pn_rt();
431 }
432
433 G_MODULE_EXPORT void
434 plugin_init(plugin_address_table_t *pat
435 #ifndef PLUGINS_NEED_ADDRESS_TABLE
436 _U_
437 #endif
438 ){
439   /* initialise the table of pointers needed in Win32 DLLs */
440   plugin_address_table_init(pat);
441   /* register the new protocol, protocol fields, and subtrees */
442   if (proto_pn_rt == -1) { /* execute protocol initialization only once */
443     proto_register_pn_rt();
444   }
445 }
446
447 #endif /* __ETHEREAL_STATIC__ */
448
449 /* End the functions we need for plugin stuff */