For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-pw-cesopsn.c
1 /* packet-pw-satop.c
2  * Routines for CESoPSN PW dissection as per RFC5086.
3  * Copyright 2009, Dmitry Trebich, Artem Tamazov <artem.tamazov@tellabs.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  * History:
26  * ---------------------------------
27  * 16.03.2009 initial implementation for MPLS
28  * 14.08.2009 added: support for IP/UDP demultiplexing
29  * Not supported yet:
30  * - All PW modes, except Basic NxDS0 mode.
31  * - <Optional> RTP Headers (RFC3550)
32  * - Decoding of PW payload
33  */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <stdlib.h>
40 #include <glib.h>
41 #include <epan/packet.h>
42 #include <epan/expert.h>
43
44 #include "packet-mpls.h"
45 #include "packet-pw-common.h"
46
47 static gint proto = -1;
48 static gint ett = -1;
49
50 static int hf_cw = -1;
51 static int hf_cw_bits03 = -1;
52 static int hf_cw_lm = -1;
53 static int hf_cw_r = -1;
54 static int hf_cw_frg = -1;
55 static int hf_cw_len = -1;
56 static int hf_cw_seq = -1;
57 static int hf_payload = -1;
58 static int hf_payload_l = -1;
59
60 static dissector_handle_t data_handle;
61 static dissector_handle_t pw_padding_handle;
62
63 const char pwc_longname_pw_cesopsn[] = "CESoPSN basic NxDS0 mode (no RTP support)";
64 static const char shortname[] = "CESoPSN basic (no RTP)";
65
66 static const value_string vals_cw_lm[] = {
67         /* note that bitmask in hs_register_info is 0xb == 1011B */
68         /* this is why 0x8 comes just after 0x3 */
69         { 0x0,  "Normal situation - no AC faults" },
70         /*{ 0x1,        "Reserved combination" },*/
71         { 0x2,  "AC Fault - RDI condition" },
72         { 0x3,  "Reserved for CESoPSN signaling" },
73         { 0x8,  "AC Fault - TDM data is invalid" },
74         /*{ 0x9,        "Reserved combination" },*/
75         /*{ 0xa,        "Reserved combination" },*/
76         /*{ 0xb,        "Reserved combination" },*/
77         { 0,    NULL }
78 };
79
80
81 static
82 void dissect_pw_cesopsn( tvbuff_t * tvb_original
83                                                 ,packet_info * pinfo
84                                                 ,proto_tree * tree
85                                                 ,pwc_demux_type_t demux)
86 {
87         const int encaps_size = 4; /*RTP header in encapsulation is not supported yet*/
88         gint packet_size;
89         gint payload_size;
90         gint padding_size;
91         pwc_packet_properties_t properties;
92
93         packet_size = tvb_reported_length_remaining(tvb_original, 0);
94
95         /*
96          * FIXME
97          * "4" below should be replaced by something like "min_packet_size_this_dissector"
98          * Also call to dissect_try_cw_first_nibble() should be moved before this block
99          */
100         if (packet_size < 4) /* 4 is smallest size which may be sensible (for PWACH dissector) */
101         {
102                 if (tree)
103                 {
104                         proto_item  *item;
105                         item = proto_tree_add_item(tree, proto, tvb_original, 0, -1, ENC_NA);
106                         expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
107                                 "PW packet size (%d) is too small to carry sensible information"
108                                 ,(int)packet_size);
109                 }
110                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
111                 {
112                         col_set_str(pinfo->cinfo, COL_PROTOCOL, shortname);
113                 }
114                 col_set_str(pinfo->cinfo, COL_INFO, "Malformed: PW packet is too small");
115                 return;
116         }
117
118         switch (demux)
119         {
120         case PWC_DEMUX_MPLS:
121                 if (dissect_try_cw_first_nibble(tvb_original, pinfo, tree))
122                 {
123                         return;
124                 }
125                 break;
126         case PWC_DEMUX_UDP:
127                 break;
128         default:
129                 DISSECTOR_ASSERT_NOT_REACHED();
130                 return;
131         }
132
133         /* check how "good" is this packet */
134         /* also decide payload length from packet size and CW */
135         properties = PWC_PACKET_PROPERTIES_T_INITIALIZER;
136         if (0 != (tvb_get_guint8(tvb_original, 0) & 0xf0 /*bits03*/))
137         {
138                 properties |= PWC_CW_BAD_BITS03;
139         }
140         if (0 != (tvb_get_guint8(tvb_original, 1) & 0xc0 /*frag*/))
141         {
142                 properties |= PWC_CW_BAD_FRAG;
143         }
144         {
145                 /* RFC5086:
146                  * [LEN (bits (10 to 15) MAY be used to carry the length of the CESoPSN
147                  * packet (defined as the size of the CESoPSN header + the payload size)
148                  * if it is less than 64 bytes, and MUST be set to zero otherwise.
149                  * Note:  If fixed RTP header is used in the encapsulation, it is
150                  * considered part of the CESoPSN header.]
151                  *
152                  * Note that this differs from RFC4385's definition of length:
153                  * [ If the MPLS payload is less than 64 bytes, the length field
154                  * MUST be set to the length of the PW payload...]
155                  *
156                  * We will use RFC5086's definition here.
157                  */
158                 int cw_len;
159                 gint payload_size_from_packet;
160
161                 cw_len = tvb_get_guint8(tvb_original, 1) & 0x3f;
162                 payload_size_from_packet = packet_size - encaps_size;
163                 if (cw_len != 0)
164                 {
165                         gint payload_size_from_cw;
166                         payload_size_from_cw = cw_len - encaps_size;
167                         /*
168                          * Assumptions for error case,
169                          * will be overwritten if no errors found:
170                          */
171                         payload_size = payload_size_from_packet;
172                         padding_size = 0;
173
174                         if (payload_size_from_cw < 0)
175                         {
176                                 properties |= PWC_CW_BAD_PAYLEN_LT_0;
177                         }
178                         else if (payload_size_from_cw > payload_size_from_packet)
179                         {
180                                 properties |= PWC_CW_BAD_PAYLEN_GT_PACKET;
181                         }
182                         else if (payload_size_from_packet >= 64)
183                         {
184                                 properties |= PWC_CW_BAD_LEN_MUST_BE_0;
185                         }
186                         else /* ok */
187                         {
188                                 payload_size = payload_size_from_cw;
189                                 padding_size = payload_size_from_packet - payload_size_from_cw; /* >=0 */
190                         }
191                 }
192                 else
193                 {
194                         payload_size = payload_size_from_packet;
195                         padding_size = 0;
196                 }
197         }
198
199         {
200                 guint8 cw_lm;
201                 cw_lm = tvb_get_guint8(tvb_original, 0) & 0x0b /*l+mod*/;
202                 if (NULL == match_strval(cw_lm, vals_cw_lm))
203                 {
204                         properties |= PWC_CW_SUSPECT_LM;
205                 }
206
207                 {
208                         guint8 l_bit, m_bits;
209                         l_bit  = (cw_lm & 0x08) >> 3;
210                         m_bits = (cw_lm & 0x03) >> 0;
211                         if ((l_bit == 0 && m_bits == 0x0) /*CESoPSN data packet - normal situation*/
212                             ||(l_bit == 0 && m_bits == 0x2) /*CESoPSN data packet - RDI on the AC*/ )
213                         {
214                                 if ((payload_size == 0) || ((payload_size % 8) != 0))
215                                 {
216                                         properties |= PWC_PAY_SIZE_BAD;
217                                 }
218                         }
219                         else if (l_bit == 1 && m_bits == 0x0) /*TDM data is invalid; payload MAY be omitted*/
220                         {
221                                 /*allow any size of payload*/
222                         }
223                         else /*reserved combinations*/
224                         {
225                                 /*allow any size of payload*/
226                         }
227                 }
228         }
229
230         /* fill up columns*/
231         if (check_col(pinfo->cinfo, COL_PROTOCOL))
232         {
233                 col_set_str(pinfo->cinfo, COL_PROTOCOL, shortname);
234         }
235         if (check_col(pinfo->cinfo, COL_INFO))
236         {
237                 col_clear(pinfo->cinfo, COL_INFO);
238                 if (properties & PWC_ANYOF_CW_BAD)
239                 {
240                         col_append_str(pinfo->cinfo, COL_INFO, "CW:Bad, ");
241                 }
242                 else if (properties & PWC_ANYOF_CW_SUSPECT)
243                 {
244                         col_append_str(pinfo->cinfo, COL_INFO, "CW:Suspect, ");
245                 }
246
247                 if (properties & PWC_PAY_SIZE_BAD)
248                 {
249                         col_append_str(pinfo->cinfo, COL_INFO, "Payload size:Bad, ");
250                 }
251
252                 col_append_fstr(pinfo->cinfo, COL_INFO, "TDM octets:%d", (int)payload_size);
253
254                 if (padding_size != 0)
255                 {
256                         col_append_fstr(pinfo->cinfo, COL_INFO, ", Padding:%d", (int)padding_size);
257                 }
258         }
259
260         {
261                 proto_item* item;
262                 item = proto_tree_add_item(tree, proto, tvb_original, 0, -1, ENC_NA);
263                 pwc_item_append_cw(item,tvb_get_ntohl(tvb_original, 0),TRUE);
264                 pwc_item_append_text_n_items(item,(int)payload_size,"octet");
265                 {
266                         proto_tree* tree2;
267                         tree2 = proto_item_add_subtree(item, ett);
268                         {
269                                 tvbuff_t* tvb;
270                                 proto_item* item2;
271                                 tvb = tvb_new_subset(tvb_original, 0, PWC_SIZEOF_CW, PWC_SIZEOF_CW);
272                                 item2 = proto_tree_add_item(tree2, hf_cw, tvb, 0, -1, ENC_NA);
273                                 pwc_item_append_cw(item2,tvb_get_ntohl(tvb, 0),FALSE);
274                                 {
275                                         proto_tree* tree3;
276                                         tree3 = proto_item_add_subtree(item, ett);
277                                         {
278                                                 proto_item* item3;
279                                                 if (properties & PWC_CW_BAD_BITS03) /*display only if value is wrong*/
280                                                 {
281                                                         item3 = proto_tree_add_item(tree3, hf_cw_bits03, tvb, 0, 1, ENC_BIG_ENDIAN);
282                                                         expert_add_info_format(pinfo, item3, PI_MALFORMED, PI_ERROR
283                                                                 ,"Bits 0..3 of Control Word must be 0");
284                                                 }
285
286                                                 item3 = proto_tree_add_item(tree3, hf_cw_lm,  tvb, 0, 1, ENC_BIG_ENDIAN);
287                                                 if (properties & PWC_CW_SUSPECT_LM)
288                                                 {
289                                                         expert_add_info_format(pinfo, item3, PI_UNDECODED, PI_WARN
290                                                                 ,"Reserved combination of L and Modifier bits");
291                                                 }
292
293                                                 proto_tree_add_item(tree3, hf_cw_r, tvb, 0, 1, ENC_BIG_ENDIAN);
294
295                                                 item3 = proto_tree_add_item(tree3, hf_cw_frg, tvb, 1, 1, ENC_BIG_ENDIAN);
296                                                 if (properties & PWC_CW_BAD_FRAG)
297                                                 {
298                                                         expert_add_info_format(pinfo, item3, PI_MALFORMED, PI_ERROR
299                                                                 ,"Fragmentation of payload is not allowed"
300                                                                 " for basic CESoPSN mode");
301                                                 }
302
303                                                 item3 = proto_tree_add_item(tree3, hf_cw_len, tvb, 1, 1, ENC_BIG_ENDIAN);
304                                                 if (properties & PWC_CW_BAD_PAYLEN_LT_0)
305                                                 {
306                                                         expert_add_info_format(pinfo, item3, PI_MALFORMED, PI_ERROR
307                                                                 ,"Bad Length: too small, must be > %d"
308                                                                 ,(int)encaps_size);
309                                                 }
310                                                 if (properties & PWC_CW_BAD_PAYLEN_GT_PACKET)
311                                                 {
312                                                         expert_add_info_format(pinfo, item3, PI_MALFORMED, PI_ERROR
313                                                                 ,"Bad Length: must be <= than PSN packet size (%d)"
314                                                                 ,(int)packet_size);
315                                                 }
316                                                 if (properties & PWC_CW_BAD_LEN_MUST_BE_0)
317                                                 {
318                                                         expert_add_info_format(pinfo, item3, PI_MALFORMED, PI_ERROR
319                                                                 ,"Bad Length: must be 0 if CESoPSN packet size (%d) is > 64"
320                                                                 ,(int)packet_size);
321                                                 }
322
323                                                 proto_tree_add_item(tree3, hf_cw_seq, tvb, 2, 2, ENC_BIG_ENDIAN);
324
325                                         }
326                                 }
327                         }
328                 }
329
330                 /* payload */
331                 if (payload_size == 0)
332                 {
333                         if (properties & PWC_PAY_SIZE_BAD)
334                         {
335                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR
336                                         ,"CESoPSN payload: none found. Size of payload must be <> 0");
337                         }
338                         else
339                         {
340                                 expert_add_info_format(pinfo, item, PI_UNDECODED, PI_NOTE
341                                         ,"CESoPSN payload: omitted to conserve bandwidth");
342                         }
343                 }
344                 else
345                 {
346                         proto_tree* tree2;
347                         tree2 = proto_item_add_subtree(item, ett);
348                         {
349                                 proto_item* item2;
350                                 tvbuff_t* tvb;
351                                 tvb = tvb_new_subset(tvb_original, PWC_SIZEOF_CW, payload_size, payload_size);
352                                 item2 = proto_tree_add_item(tree2, hf_payload, tvb, 0, -1, ENC_NA);
353                                 pwc_item_append_text_n_items(item2,(int)payload_size,"octet");
354                                 if (properties & PWC_PAY_SIZE_BAD)
355                                 {
356                                         expert_add_info_format(pinfo, item2, PI_MALFORMED, PI_ERROR
357                                                 ,"CESoPSN packet payload size must be multiple of 8");
358                                 }
359                                 tree2 = proto_item_add_subtree(item2, ett);
360                                 call_dissector(data_handle, tvb, pinfo, tree2);
361                                 item2 = proto_tree_add_int(tree2, hf_payload_l, tvb, 0, 0
362                                         ,(int)payload_size); /* allow filtering */
363                                 PROTO_ITEM_SET_HIDDEN(item2);
364                         }
365                 }
366
367                 /* padding */
368                 if (padding_size > 0)
369                 {
370                         proto_tree* tree2;
371                         tree2 = proto_item_add_subtree(item, ett);
372                         {
373                                 tvbuff_t* tvb;
374                                 tvb = tvb_new_subset(tvb_original, PWC_SIZEOF_CW + payload_size, padding_size, -1);
375                                 call_dissector(pw_padding_handle, tvb, pinfo, tree2);
376                         }
377                 }
378         }
379         return;
380 }
381
382
383 static
384 void dissect_pw_cesopsn_mpls( tvbuff_t * tvb_original, packet_info * pinfo, proto_tree * tree)
385 {
386         dissect_pw_cesopsn(tvb_original,pinfo,tree,PWC_DEMUX_MPLS);
387         return;
388 }
389
390
391 static
392 void dissect_pw_cesopsn_udp( tvbuff_t * tvb_original, packet_info * pinfo, proto_tree * tree)
393 {
394         dissect_pw_cesopsn(tvb_original,pinfo,tree,PWC_DEMUX_UDP);
395         return;
396 }
397
398
399 void proto_register_pw_cesopsn(void)
400 {
401         static hf_register_info hf[] = {
402                 { &hf_cw        ,{"Control Word"                ,"pwcesopsn.cw"
403                                 ,FT_NONE                        ,BASE_NONE              ,NULL
404                                 ,0                              ,NULL                   ,HFILL }}
405                 ,{&hf_cw_bits03,{"Bits 0 to 3"                  ,"pwcesopsn.cw.bits03"
406                                 ,FT_UINT8                       ,BASE_DEC               ,NULL
407                                 ,0xf0                           ,NULL                   ,HFILL }}
408                 ,{ &hf_cw_lm,   {"L+M bits"                     ,"pwcesopsn.cw.lm"
409                                 ,FT_UINT8                       ,BASE_HEX               ,VALS(vals_cw_lm)
410                                 ,0x0b                           ,NULL                   ,HFILL }}
411                 ,{&hf_cw_r,     {"R bit: Local CE-bound IWF"    ,"pwcesopsn.cw.rbit"
412                                 ,FT_UINT8                       ,BASE_DEC               ,VALS(pwc_vals_cw_r_bit)
413                                 ,0x04                           ,NULL                   ,HFILL }}
414                 ,{&hf_cw_frg,   {"Fragmentation"                ,"pwcesopsn.cw.frag"
415                                 ,FT_UINT8                       ,BASE_DEC               ,VALS(pwc_vals_cw_frag)
416                                 ,0xc0                           ,NULL                   ,HFILL }}
417                 ,{&hf_cw_len,   {"Length"                       ,"pwcesopsn.cw.length"
418                                 ,FT_UINT8                       ,BASE_DEC               ,NULL
419                                 ,0x3f                           ,NULL                   ,HFILL }}
420                 ,{&hf_cw_seq,   {"Sequence number"              ,"pwcesopsn.cw.seqno"
421                                 ,FT_UINT16                      ,BASE_DEC               ,NULL
422                                 ,0                              ,NULL                   ,HFILL }}
423                 ,{&hf_payload   ,{"TDM payload"                 ,"pwcesopsn.payload"
424                                 ,FT_BYTES                       ,BASE_NONE              ,NULL
425                                 ,0                              ,NULL                   ,HFILL }}
426                 ,{&hf_payload_l ,{"TDM payload length"          ,"pwcesopsn.payload.len"
427                                 ,FT_INT32                       ,BASE_DEC               ,NULL
428                                 ,0                              ,NULL                   ,HFILL }}
429         };
430
431         static gint *ett_array[] = {
432                 &ett
433         };
434
435         proto = proto_register_protocol(pwc_longname_pw_cesopsn, shortname, "pwcesopsn");
436         proto_register_field_array(proto, hf, array_length(hf));
437         proto_register_subtree_array(ett_array, array_length(ett_array));
438         register_dissector("pw_cesopsn_mpls", dissect_pw_cesopsn_mpls, proto);
439         register_dissector("pw_cesopsn_udp", dissect_pw_cesopsn_udp, proto);
440         return;
441 }
442
443
444 void proto_reg_handoff_pw_cesopsn(void)
445 {
446         data_handle = find_dissector("data");
447         pw_padding_handle = find_dissector("pw_padding");
448         dissector_add_uint("mpls.label", LABEL_INVALID, find_dissector("pw_cesopsn_mpls"));
449         dissector_add_handle("udp.port", find_dissector("pw_cesopsn_udp")); /* For Decode-As */
450         return;
451 }