Apply some of the patches from:
[obnox/wireshark/wip.git] / epan / dissectors / packet-dcp-etsi.c
1 /* packet-dcp-etsi.c
2  * Routines for ETSI Distribution & Communication Protocol
3  * Copyright 2006, British Broadcasting Corporation
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  * Protocol info
26  * Ref: ETSI DCP (ETSI TS 102 821)
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <gmodule.h>
34 #include <epan/packet.h>
35 #include <epan/reassemble.h>
36 #include <epan/crcdrm.h>
37 #include <epan/reedsolomon.h>
38 #include <epan/emem.h>
39 #include <string.h>
40
41 /* forward reference */
42
43 static gboolean dissect_dcp_etsi (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
44 static void dissect_af (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
45 static void dissect_pft (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
46 static void dissect_tpl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
47
48 static dissector_table_t dcp_dissector_table;
49 static dissector_table_t af_dissector_table;
50 static dissector_table_t tpl_dissector_table;
51
52 static int proto_dcp_etsi = -1;
53 static int proto_af = -1;
54 static int proto_pft = -1;
55 static int proto_tpl = -1;
56 static int hf_edcp_sync = -1;
57 static int hf_edcp_len = -1;
58 static int hf_edcp_seq = -1;
59 static int hf_edcp_crcflag = -1;
60 static int hf_edcp_maj = -1;
61 static int hf_edcp_min = -1;
62 static int hf_edcp_pt = -1;
63 static int hf_edcp_crc = -1;
64 static int hf_edcp_crc_ok = -1;
65 static int hf_edcp_pft_pt = -1;
66 static int hf_edcp_pseq = -1;
67 static int hf_edcp_findex = -1;
68 static int hf_edcp_fcount = -1;
69 static int hf_edcp_fecflag = -1;
70 static int hf_edcp_addrflag = -1;
71 static int hf_edcp_plen = -1;
72 static int hf_edcp_rsk = -1;
73 static int hf_edcp_rsz = -1;
74 static int hf_edcp_source = -1;
75 static int hf_edcp_dest = -1;
76 static int hf_edcp_hcrc = -1;
77 static int hf_edcp_hcrc_ok = -1;
78 static int hf_edcp_c_max = -1;
79 static int hf_edcp_rx_min = -1;
80 static int hf_edcp_rs_corrected = -1;
81 static int hf_edcp_rs_ok = -1;
82 static int hf_edcp_pft_payload = -1;
83
84 static int hf_tpl_tlv = -1;
85 static int hf_tpl_ptr = -1;
86
87 static int hf_edcp_fragments = -1;
88 static int hf_edcp_fragment = -1;
89 static int hf_edcp_fragment_overlap = -1;
90 static int hf_edcp_fragment_overlap_conflicts = -1;
91 static int hf_edcp_fragment_multiple_tails = -1;
92 static int hf_edcp_fragment_too_long_fragment = -1;
93 static int hf_edcp_fragment_error = -1;
94 static int hf_edcp_reassembled_in = -1;
95
96 /* Initialize the subtree pointers */
97 static gint ett_edcp = -1;
98 static gint ett_af = -1;
99 static gint ett_pft = -1;
100 static gint ett_tpl = -1;
101 static gint ett_edcp_fragment = -1;
102 static gint ett_edcp_fragments = -1;
103
104 static GHashTable *dcp_fragment_table = NULL;
105 static GHashTable *dcp_reassembled_table = NULL;
106
107 static const fragment_items dcp_frag_items = {
108 /* Fragment subtrees */
109   &ett_edcp_fragment,
110   &ett_edcp_fragments,
111 /* Fragment fields */
112   &hf_edcp_fragments,
113   &hf_edcp_fragment,
114   &hf_edcp_fragment_overlap,
115   &hf_edcp_fragment_overlap_conflicts,
116   &hf_edcp_fragment_multiple_tails,
117   &hf_edcp_fragment_too_long_fragment,
118   &hf_edcp_fragment_error,
119 /* Reassembled in field */
120   &hf_edcp_reassembled_in,
121 /* Tag */
122   "Message fragments"
123 };
124
125 /** initialise the DCP protocol. Details follow
126  *  here.
127  */
128 static void
129 dcp_init_protocol(void)
130 {
131   fragment_table_init (&dcp_fragment_table);
132   reassembled_table_init (&dcp_reassembled_table);
133 }
134
135
136 /** Dissect a DCP packet. Details follow
137  *  here.
138  *  \param[in,out] tvb The buffer containing the packet
139  *  \param[in,out] pinfo The packet info structure
140  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
141 static void
142  */
143 static gboolean
144 dissect_dcp_etsi (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
145 {
146   guint8 *sync;
147   proto_tree *dcp_tree = NULL;
148
149   /* 6.1 AF packet structure
150    *
151    * AF Header
152    * SYNC               LEN             SEQ             AR              PT
153    * 2 bytes    4 bytes 2 bytes 1 byte  1 byte
154    *
155    * SYNC: two-byte ASCII representation of "AF".
156    * LEN: length of the payload, in bytes.
157    * SEQ: sequence number
158    * AR: AF protocol Revision - a field combining the CF, MAJ and MIN fields
159    * CF: CRC Flag, 0 if the CRC field is not used
160    * MAJ: major revision of the AF protocol in use, see clause 6.2.
161    * MIN: minor revision of the AF protocol in use, see clause 6.2.
162    * Protocol Type (PT): single byte encoding the protocol of the data carried in the payload. For TAG Packets, the value
163    * shall be the ASCII representation of "T".
164    *
165    * 7.1 PFT fragment structure
166    * PFT Header
167    * 14, 16, 18 or 20 bytes (depending on options)                                                                              Optional present if FEC=1 Optional present if Addr = 1  
168    * Psync              Pseq            Findex          Fcount          FEC             HCRC            Addr    Plen    | RSk           RSz                     | Source        Dest
169    * 16 bits    16 bits         24 bits         24 bits         1 bit   16 bits         1 bit   14 bits | 8 bits        8 bits          | 16 bits       16 bits
170    *
171    * Psync: the ASCII string "PF" is used as the synchronization word for the PFT Layer
172    *
173    * Don't accept this packet unless at least a full AF header present(10 bytes).
174    * It should be possible to strengthen the heuristic further if need be.
175    */
176   if(tvb_length(tvb) < 11)
177     return FALSE;
178
179   sync = tvb_get_ephemeral_string (tvb, 0, 2);
180   if((sync[0]!='A' && sync[0]!='P') || sync[1]!='F')
181     return FALSE;
182
183   pinfo->current_proto = "DCP (ETSI)";
184
185   /* Clear out stuff in the info column */
186   if (check_col (pinfo->cinfo, COL_INFO)) {
187     col_clear (pinfo->cinfo, COL_INFO);
188   }
189   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
190     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP (ETSI)");
191     /*col_append_fstr (pinfo->cinfo, COL_INFO, " tvb %d", tvb_length(tvb));*/
192   }
193
194   if(tree) {
195     proto_item *ti = NULL;
196     ti = proto_tree_add_item (tree, proto_dcp_etsi, tvb, 0, -1, FALSE);
197     dcp_tree = proto_item_add_subtree (ti, ett_edcp);
198   }
199
200   dissector_try_string(dcp_dissector_table, (char*)sync, tvb, pinfo, dcp_tree);
201   return TRUE;
202 }
203
204 #define PFT_RS_N_MAX 207
205 #define PFT_RS_K 255
206 #define PFT_RS_P (PFT_RS_K - PFT_RS_N_MAX)
207
208
209 static
210 void rs_deinterleave(const guint8 *input, guint8 *output, guint16 plen, guint32 fcount)
211 {
212   guint fidx;
213   for(fidx=0; fidx<fcount; fidx++)
214   {
215     int r;
216     for (r=0; r<plen; r++)
217     {
218       output[fidx+r*fcount] = input[fidx*plen+r];
219     }
220   }
221 }
222
223 static
224 gboolean rs_correct_data(guint8 *deinterleaved, guint8 *output,
225  guint32 c_max, guint16 rsk, guint16 rsz _U_)
226 {
227   guint32 i, index_coded = 0, index_out = 0;
228   int err_corr;
229   for (i=0; i<c_max; i++)
230   {
231     memcpy(output+index_out, deinterleaved+index_coded, rsk);
232     index_coded += rsk;
233     memcpy(output+index_out+PFT_RS_N_MAX, deinterleaved+index_coded, PFT_RS_P);
234     index_coded += PFT_RS_P;
235     err_corr = eras_dec_rs(output+index_out, NULL, 0);
236     if (err_corr<0) {
237       return FALSE;
238     }
239     index_out += rsk;
240   }
241   return TRUE;
242 }
243
244 /* Don't attempt reassembly if we have a huge number of fragments. */
245 #define MAX_FRAGMENTS ((1 * 1024 * 1024) / sizeof(guint32))
246
247 static tvbuff_t *
248 dissect_pft_fec_detailed(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
249   guint32 findex,
250   guint32 fcount,
251   guint16 seq,
252   gint offset,
253   guint16 plen,
254   gboolean fec _U_,
255   guint16 rsk,
256   guint16 rsz,
257   fragment_data *fdx
258 )
259 {
260   guint16 decoded_size;
261   guint32 c_max;
262   guint32 rx_min;
263   gboolean first, last;
264   tvbuff_t *new_tvb=NULL;
265
266   if (fcount > MAX_FRAGMENTS) {
267     if (tree)
268       proto_tree_add_text(tree, tvb , 0, -1, "[Reassembly of %d fragments not attempted]", fcount);
269     return NULL;
270   }
271
272   first = findex == 0;
273   last = fcount == (findex+1);
274   decoded_size = fcount*plen;
275   c_max = fcount*plen/(rsk+PFT_RS_P);  /* rounded down */
276   rx_min = c_max*rsk/plen;
277   if(rx_min*plen<c_max*rsk)
278     rx_min++;
279   if (fdx)
280     new_tvb = process_reassembled_data (tvb, offset, pinfo,
281                                         "Reassembled Message",
282                                         fdx, &dcp_frag_items,
283                                         NULL, tree);
284   else {
285     guint fragments=0;
286     guint32 *got;
287     fragment_data *fd;
288     fragment_data *fd_head;
289
290     if(tree)
291       proto_tree_add_text (tree, tvb, 0, -1, "want %d, got %d need %d",
292                            fcount, fragments, rx_min
293         );
294     got = ep_alloc(fcount*sizeof(guint32));
295
296     /* make a list of the findex (offset) numbers of the fragments we have */
297     fd = fragment_get(pinfo, seq, dcp_fragment_table);
298     for (fd_head = fd; fd_head != NULL; fd_head = fd_head->next) {
299       if(fd_head->data) {
300         got[fragments++] = fd_head->offset; /* this is the findex of the fragment */
301       }
302     }
303     /* put a sentinel at the end */
304     got[fragments++] = fcount;
305     /* have we got enough for Reed Solomon to try to correct ? */
306     if(fragments>=rx_min) { /* yes, in theory */
307       guint i,current_findex;
308       fragment_data *frag=NULL;
309       guint8 *dummy_data = (guint8*) ep_alloc0 (plen);
310       tvbuff_t *dummytvb = tvb_new_real_data(dummy_data, plen, plen);
311       /* try and decode with missing fragments */
312       if(tree)
313           proto_tree_add_text (tree, tvb, 0, -1, "want %d, got %d need %d",
314                                fcount, fragments, rx_min
315             );
316       /* fill the fragment table with empty fragments */
317       current_findex = 0;
318       for(i=0; i<fragments; i++) {
319         guint next_fragment_we_have = got[i];
320         if (next_fragment_we_have > MAX_FRAGMENTS) {
321           if (tree)
322             proto_tree_add_text(tree, tvb , 0, -1, "[Reassembly of %d fragments not attempted]", next_fragment_we_have);
323           return NULL;
324         }
325         for(; current_findex<next_fragment_we_have; current_findex++) {
326           frag = fragment_add_seq_check (dummytvb, 0, pinfo, seq,
327                                          dcp_fragment_table, dcp_reassembled_table, 
328                                          current_findex, plen, (current_findex+1!=fcount));
329         }
330         current_findex++; /* skip over the fragment we have */
331       }
332       if(frag)
333         new_tvb = process_reassembled_data (tvb, offset, pinfo,
334                                             "Reassembled Message",
335                                             frag, &dcp_frag_items,
336                                             NULL, tree);
337     }
338   }
339   if(new_tvb) {
340     gboolean decoded = TRUE;
341     tvbuff_t *dtvb = NULL;
342     const guint8 *input = tvb_get_ptr(new_tvb, 0, -1);
343     guint16 reassembled_size = tvb_length(new_tvb);
344     guint8 *deinterleaved = (guint8*) g_malloc (reassembled_size);
345     guint8 *output = (guint8*) g_malloc (decoded_size);
346     rs_deinterleave(input, deinterleaved, plen, fcount);
347
348     dtvb = tvb_new_child_real_data(tvb, deinterleaved, reassembled_size, reassembled_size);
349     add_new_data_source(pinfo, dtvb, "Deinterleaved");
350     tvb_set_free_cb(dtvb, g_free);
351
352     decoded = rs_correct_data(deinterleaved, output, c_max, rsk, rsz);
353     if(tree)
354       proto_tree_add_boolean (tree, hf_edcp_rs_ok, tvb, offset, 2, decoded);
355
356     new_tvb = tvb_new_child_real_data(dtvb, output, decoded_size, decoded_size);
357     add_new_data_source(pinfo, new_tvb, "RS Error Corrected Data");
358     tvb_set_free_cb(new_tvb, g_free);
359   }
360   return new_tvb;
361 }
362
363
364 /** Handle a PFT packet which has the fragmentation header. This uses the
365  * standard wireshark methods for reassembling fragments. If FEC is used,
366  * the FEC is handled too. For the moment, all the fragments must be
367  * available but this could be improved.
368  *  \param[in,out] tvb The buffer containing the current fragment
369  *  \param[in,out] pinfo The packet info structure
370  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
371  *  \param[in] findex the fragment count
372  *  \param[in] fcount the number of fragments
373  *  \param[in] seq the sequence number of the reassembled packet
374  *  \param[in] offset the offset into the tvb of the fragment
375  *  \param[in] plen the length of each fragment
376  *  \param[in] fec is fec used
377  *  \param[in] rsk the number of useful bytes in each chunk
378  *  \param[in] rsz the number of padding bytes in each chunk
379  */
380 static tvbuff_t *
381 dissect_pft_fragmented(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
382   guint32 findex,
383   guint32 fcount,
384   guint16 seq,
385   gint offset,
386   guint16 plen,
387   gboolean fec,
388   guint16 rsk,
389   guint16 rsz
390 )
391 {
392   gboolean first, last;
393   tvbuff_t *new_tvb=NULL;
394   fragment_data *frag_edcp = NULL;
395   pinfo->fragmented = TRUE;
396   first = findex == 0;
397   last = fcount == (findex+1);
398   frag_edcp = fragment_add_seq_check (
399     tvb, offset, pinfo,
400     seq,
401     dcp_fragment_table, dcp_reassembled_table,
402     findex,
403     plen,
404     !last);
405   if(fec) {
406     new_tvb = dissect_pft_fec_detailed(
407       tvb, pinfo, tree, findex, fcount, seq, offset, plen, fec, rsk, rsz, frag_edcp
408       );
409   } else {
410     new_tvb = process_reassembled_data (tvb, offset, pinfo,
411                                         "Reassembled Message",
412                                         frag_edcp, &dcp_frag_items,
413                                         NULL, tree);
414   }
415   if (check_col (pinfo->cinfo, COL_INFO)) {
416     if(new_tvb) {
417       col_append_str (pinfo->cinfo, COL_INFO, " (Message Reassembled)");
418     } else {
419       if(last) {
420         col_append_str (pinfo->cinfo, COL_INFO, " (Message Reassembly failure)");
421       } else {
422         col_append_fstr (pinfo->cinfo, COL_INFO, " (Message fragment %u)", findex);
423       }
424     }
425     if(first)
426       col_append_str (pinfo->cinfo, COL_INFO, " (first)");
427     if(last)
428       col_append_str (pinfo->cinfo, COL_INFO, " (last)");
429   }
430   return new_tvb;
431 }
432
433 /** Dissect a PFT packet. Details follow
434  *  here.
435  *  \param[in,out] tvb The buffer containing the packet
436  *  \param[in,out] pinfo The packet info structure
437  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
438  */
439 static void
440 dissect_pft(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
441 {
442   guint16 plen;
443   gint offset = 0;
444   guint16 seq, payload_len, hcrc;
445   guint32 findex, fcount;
446   proto_tree *pft_tree = NULL;
447   proto_item *ti = NULL, *li = NULL;
448   tvbuff_t *next_tvb = NULL;
449   gboolean fec = FALSE;
450   guint16 rsk=0, rsz=0;
451
452   pinfo->current_proto = "DCP-PFT";
453   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
454     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-PFT");
455   }
456
457   if (tree) {                   /* we are being asked for details */
458     ti = proto_tree_add_item (tree, proto_pft, tvb, 0, -1, FALSE);
459     pft_tree = proto_item_add_subtree (ti, ett_pft);
460     proto_tree_add_item (pft_tree, hf_edcp_sync, tvb, offset, 2, FALSE);
461   }
462   offset += 2;
463   seq = tvb_get_ntohs (tvb, offset);
464   if (tree) {
465     proto_tree_add_item (pft_tree, hf_edcp_pseq, tvb, offset, 2, FALSE);
466   }
467   offset += 2;
468   findex = tvb_get_ntoh24 (tvb, offset);
469   if (tree) {
470     proto_tree_add_item (pft_tree, hf_edcp_findex, tvb, offset, 3, FALSE);
471   }
472   offset += 3;
473   fcount = tvb_get_ntoh24 (tvb, offset);
474   if (tree) {
475     proto_tree_add_item (pft_tree, hf_edcp_fcount, tvb, offset, 3, FALSE);
476   }
477   offset += 3;
478   plen = tvb_get_ntohs (tvb, offset);
479   payload_len = plen & 0x3fff;
480   if (tree) {
481     proto_tree_add_item (pft_tree, hf_edcp_fecflag, tvb, offset, 2, FALSE);
482     proto_tree_add_item (pft_tree, hf_edcp_addrflag, tvb, offset, 2, FALSE);
483     li = proto_tree_add_item (pft_tree, hf_edcp_plen, tvb, offset, 2, FALSE);
484   }
485   offset += 2;
486   if (plen & 0x8000) {
487     fec = TRUE;
488     rsk = tvb_get_guint8 (tvb, offset);
489     if (tree)
490           proto_tree_add_item (pft_tree, hf_edcp_rsk, tvb, offset, 1, FALSE);
491     offset += 1;
492     rsz = tvb_get_guint8 (tvb, offset);
493     if (tree)
494           proto_tree_add_item (pft_tree, hf_edcp_rsz, tvb, offset, 1, FALSE);
495     offset += 1;
496   }
497   if (plen & 0x4000) {
498     if (tree)
499       proto_tree_add_item (pft_tree, hf_edcp_source, tvb, offset, 2, FALSE);
500     offset += 2;
501     if (tree)
502           proto_tree_add_item (pft_tree, hf_edcp_dest, tvb, offset, 2, FALSE);
503     offset += 2;
504   }
505   if (tree) {
506     proto_item *ci = NULL;
507     guint header_len = offset+2;
508     const char *crc_buf = (const char *) tvb_get_ptr(tvb, 0, header_len);
509     unsigned long c = crc_drm(crc_buf, header_len, 16, 0x11021, 1);
510     ci = proto_tree_add_item (pft_tree, hf_edcp_hcrc, tvb, offset, 2, FALSE);
511     proto_item_append_text(ci, " (%s)", (c==0xe2f0)?"Ok":"bad");
512     proto_tree_add_boolean(pft_tree, hf_edcp_hcrc_ok, tvb, offset, 2, c==0xe2f0);
513   }
514   hcrc = tvb_get_ntohs (tvb, offset);
515   offset += 2;
516   if (fcount > 1) {             /* fragmented*/
517     gboolean save_fragmented = pinfo->fragmented;
518     guint16 real_len = tvb_length(tvb)-offset;
519     proto_tree_add_item (pft_tree, hf_edcp_pft_payload, tvb, offset, real_len, FALSE);
520     if(real_len != payload_len) {
521       if(li)
522         proto_item_append_text(li, " (length error (%d))", real_len);
523     }
524     next_tvb = dissect_pft_fragmented(tvb, pinfo, pft_tree,
525                                       findex, fcount, seq, offset, real_len,
526                                       fec, rsk, rsz
527                                       );
528     pinfo->fragmented = save_fragmented;
529   } else {
530     next_tvb = tvb_new_subset (tvb, offset, -1, -1);
531   }
532   if(next_tvb) {
533     dissect_af(next_tvb, pinfo, tree);
534   }
535 }
536
537 /** Dissect an AF Packet. Parse an AF packet, checking the CRC if the CRC valid
538  * flag is set and calling any registered sub dissectors on the payload type.
539  * Currently only a payload type 'T' is defined which is the tag packet layer.
540  * If any others are defined then they can register themselves.
541  *  \param[in,out] tvb The buffer containing the packet
542  *  \param[in,out] pinfo The packet info structure
543  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
544  */
545 static void
546 dissect_af (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
547 {
548   gint offset = 0;
549   proto_item *ti = NULL;
550   proto_item *li = NULL;
551   proto_item *ci = NULL;
552   proto_tree *af_tree = NULL;
553   guint8 ver, pt;
554   guint32 payload_len;
555   tvbuff_t *next_tvb = NULL;
556
557   pinfo->current_proto = "DCP-AF";
558   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
559     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-AF");
560   }
561
562   if (tree) {                   /* we are being asked for details */
563     ti = proto_tree_add_item (tree, proto_af, tvb, 0, -1, FALSE);
564     af_tree = proto_item_add_subtree (ti, ett_af);
565     proto_tree_add_item (af_tree, hf_edcp_sync, tvb, offset, 2, FALSE);
566   }
567   offset += 2;
568   payload_len = tvb_get_ntohl(tvb, offset);
569   if (tree) {
570     guint32 real_payload_len = tvb_length(tvb)-12;
571     li = proto_tree_add_item (af_tree, hf_edcp_len, tvb, offset, 4, FALSE);
572     if(real_payload_len < payload_len) {
573       proto_item_append_text (li, " (wrong len claims %d is %d)",
574       payload_len, real_payload_len
575       );
576     } else if(real_payload_len > payload_len) {
577       proto_item_append_text (li, " (%d bytes in packet after end of AF frame)",
578       real_payload_len-payload_len
579       );
580     }
581   }
582   offset += 4;
583   if (tree)
584     proto_tree_add_item (af_tree, hf_edcp_seq, tvb, offset, 2, FALSE);
585   offset += 2;
586   ver = tvb_get_guint8 (tvb, offset);
587   if (tree) {
588     proto_tree_add_item (af_tree, hf_edcp_crcflag, tvb, offset, 1, FALSE);
589     proto_tree_add_item (af_tree, hf_edcp_maj, tvb, offset, 1, FALSE);
590     proto_tree_add_item (af_tree, hf_edcp_min, tvb, offset, 1, FALSE);
591   }
592   offset += 1;
593   pt = tvb_get_guint8 (tvb, offset);
594   if (tree)
595     proto_tree_add_item (af_tree, hf_edcp_pt, tvb, offset, 1, FALSE);
596   offset += 1;
597   next_tvb = tvb_new_subset (tvb, offset, payload_len, -1);
598   offset += payload_len;
599   if (tree)
600     ci = proto_tree_add_item (af_tree, hf_edcp_crc, tvb, offset, 2, FALSE);
601   if (ver & 0x80) { /* crc valid */
602     guint len = offset+2;
603     const char *crc_buf = (const char *) tvb_get_ptr(tvb, 0, len);
604     unsigned long c = crc_drm(crc_buf, len, 16, 0x11021, 1);
605     if (tree) {
606           proto_item_append_text(ci, " (%s)", (c==0xe2f0)?"Ok":"bad");
607       proto_tree_add_boolean(af_tree, hf_edcp_crc_ok, tvb, offset, 2, c==0xe2f0);
608     }
609   }
610   offset += 2;
611   dissector_try_port(af_dissector_table, pt, next_tvb, pinfo, tree);
612 }
613
614 /** Dissect the Tag Packet Layer.
615  *  Split the AF packet into its tag items. Each tag item has a 4 character
616  *  tag, a length in bits and a value. The *ptr tag is dissected in the routine.
617  *  All other tags are listed and may be handled by other dissectors.
618  *  Child dissectors are tied to the parent tree, not to this tree, so that
619  *  they appear at the same level as DCP.
620  *  \param[in,out] tvb The buffer containing the packet
621  *  \param[in,out] pinfo The packet info structure
622  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
623  */
624 static void
625 dissect_tpl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
626 {
627   proto_tree *tpl_tree = NULL;
628   guint offset=0;
629   char *prot=NULL;
630   guint16 maj, min;
631
632   pinfo->current_proto = "DCP-TPL";
633   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
634     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-TPL");
635   }
636
637   if(tree) {
638     proto_item *ti = NULL;
639     ti = proto_tree_add_item (tree, proto_tpl, tvb, 0, -1, FALSE);
640     tpl_tree = proto_item_add_subtree (ti, ett_tpl);
641   }
642   while(offset<tvb_length(tvb)) {
643     guint32 bits;
644     guint32 bytes;
645     char *tag = (char*)tvb_get_ephemeral_string (tvb, offset, 4); offset += 4;
646     bits = tvb_get_ntohl(tvb, offset); offset += 4;
647     bytes = bits / 8;
648     if(bits % 8)
649       bytes++;
650     if(tree) {
651       proto_item *i = NULL;
652       const guint8 *p = tvb_get_ptr(tvb, offset, bytes);
653       if(strcmp(tag, "*ptr")==0) {
654         prot = (char*)tvb_get_ephemeral_string (tvb, offset, 4);
655         maj = tvb_get_ntohs(tvb, offset+4);
656         min = tvb_get_ntohs(tvb, offset+6);
657         i = proto_tree_add_bytes_format(tpl_tree, hf_tpl_tlv, tvb,
658               offset-8, bytes+8, p, "%s %s rev %d.%d", tag, prot, maj, min);
659       } else {
660         i = proto_tree_add_bytes_format(tpl_tree, hf_tpl_tlv, tvb,
661               offset-8, bytes+8, p, "%s (%u bits)", tag, bits);
662       }
663     }
664     offset += bytes;
665   }
666   if(prot) {  /* prot is non-NULL only if we have our tree. */
667     dissector_try_string(tpl_dissector_table, prot, tvb, pinfo, tree->parent);
668   }
669 }
670
671 void
672 proto_reg_handoff_dcp_etsi (void)
673 {
674   dissector_handle_t af_handle;
675   dissector_handle_t pft_handle;
676   dissector_handle_t tpl_handle;
677
678   af_handle = create_dissector_handle(dissect_af, proto_af);
679   pft_handle = create_dissector_handle(dissect_pft, proto_pft);
680   tpl_handle = create_dissector_handle(dissect_tpl, proto_tpl);
681   heur_dissector_add("udp", dissect_dcp_etsi, proto_dcp_etsi);
682   dissector_add_string("dcp-etsi.sync", "AF", af_handle);
683   dissector_add_string("dcp-etsi.sync", "PF", pft_handle);
684   /* if there are ever other payload types ...*/
685   dissector_add("dcp-af.pt", 'T', tpl_handle);
686 }
687
688 void
689 proto_register_dcp_etsi (void)
690 {
691   static hf_register_info hf_edcp[] = {
692     {&hf_edcp_sync,
693      {"sync", "dcp-etsi.sync",
694       FT_STRING, BASE_NONE, NULL, 0,
695       "AF or PF", HFILL}
696      }
697     };
698   static hf_register_info hf_af[] = {
699     {&hf_edcp_len,
700      {"length", "dcp-af.len",
701       FT_UINT32, BASE_DEC, NULL, 0,
702       "length in bytes of the payload", HFILL}
703      },
704     {&hf_edcp_seq,
705      {"frame count", "dcp-af.seq",
706       FT_UINT16, BASE_DEC, NULL, 0,
707       "Logical Frame Number", HFILL}
708      },
709     {&hf_edcp_crcflag,
710      {"crc flag", "dcp-af.crcflag",
711       FT_BOOLEAN, 8, NULL, 0x80,
712       "Frame is protected by CRC", HFILL}
713      },
714     {&hf_edcp_maj,
715      {"Major Revision", "dcp-af.maj",
716       FT_UINT8, BASE_DEC, NULL, 0x70,
717       "Major Protocol Revision", HFILL}
718      },
719     {&hf_edcp_min,
720      {"Minor Revision", "dcp-af.min",
721       FT_UINT8, BASE_DEC, NULL, 0x0f,
722       "Minor Protocol Revision", HFILL}
723      },
724     {&hf_edcp_pt,
725      {"Payload Type", "dcp-af.pt",
726       FT_STRING, BASE_NONE, NULL, 0,
727       "T means Tag Packets, all other values reserved", HFILL}
728      },
729     {&hf_edcp_crc,
730      {"CRC", "dcp-af.crc",
731       FT_UINT16, BASE_HEX, NULL, 0,
732       "CRC", HFILL}
733      },
734     {&hf_edcp_crc_ok,
735      {"CRC OK", "dcp-af.crc_ok",
736       FT_BOOLEAN, BASE_NONE, NULL, 0,
737       "AF CRC OK", HFILL}
738      }
739     };
740
741   static hf_register_info hf_pft[] = {
742     {&hf_edcp_pft_pt,
743      {"Sub-protocol", "dcp-pft.pt",
744       FT_UINT8, BASE_DEC, NULL, 0,
745       "Always AF", HFILL}
746      },
747     {&hf_edcp_pseq,
748      {"Sequence No", "dcp-pft.seq",
749       FT_UINT16, BASE_DEC, NULL, 0,
750       "PFT Sequence No", HFILL}
751      },
752     {&hf_edcp_findex,
753      {"Fragment Index", "dcp-pft.findex",
754       FT_UINT24, BASE_DEC, NULL, 0,
755       "Index of the fragment within one AF Packet", HFILL}
756      },
757     {&hf_edcp_fcount,
758      {"Fragment Count", "dcp-pft.fcount",
759       FT_UINT24, BASE_DEC, NULL, 0,
760       "Number of fragments produced from this AF Packet", HFILL}
761      },
762     {&hf_edcp_fecflag,
763      {"FEC", "dcp-pft.fec",
764       FT_BOOLEAN, 16, NULL, 0x8000,
765       "When set the optional RS header is present", HFILL}
766      },
767     {&hf_edcp_addrflag,
768      {"Addr", "dcp-pft.addr",
769       FT_BOOLEAN, 16, NULL, 0x4000,
770       "When set the optional transport header is present", HFILL}
771      },
772     {&hf_edcp_plen,
773      {"fragment length", "dcp-pft.len",
774       FT_UINT16, BASE_DEC, NULL, 0x3fff,
775       "length in bytes of the payload of this fragment", HFILL}
776      },
777     {&hf_edcp_rsk,
778      {"RSk", "dcp-pft.rsk",
779       FT_UINT8, BASE_DEC, NULL, 0,
780       "The length of the Reed Solomon data word", HFILL}
781      },
782     {&hf_edcp_rsz,
783      {"RSz", "dcp-pft.rsz",
784       FT_UINT8, BASE_DEC, NULL, 0,
785       "The number of padding bytes in the last Reed Solomon block", HFILL}
786      },
787     {&hf_edcp_source,
788      {"source addr", "dcp-pft.source",
789       FT_UINT16, BASE_DEC, NULL, 0,
790       "PFT source identifier", HFILL}
791      },
792     {&hf_edcp_dest,
793      {"dest addr", "dcp-pft.dest",
794       FT_UINT16, BASE_DEC, NULL, 0,
795       "PFT destination identifier", HFILL}
796      },
797     {&hf_edcp_hcrc,
798      {"header CRC", "dcp-pft.crc",
799       FT_UINT16, BASE_HEX, NULL, 0,
800       "PFT Header CRC", HFILL}
801      },
802     {&hf_edcp_hcrc_ok,
803      {"PFT CRC OK", "dcp-pft.crc_ok",
804       FT_BOOLEAN, BASE_NONE, NULL, 0,
805       "PFT Header CRC OK", HFILL}
806      },
807     {&hf_edcp_fragments,
808      {"Message fragments", "dcp-pft.fragments",
809       FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL}},
810     {&hf_edcp_fragment,
811      {"Message fragment", "dcp-pft.fragment",
812       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
813     {&hf_edcp_fragment_overlap,
814      {"Message fragment overlap", "dcp-pft.fragment.overlap",
815       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
816     {&hf_edcp_fragment_overlap_conflicts,
817      {"Message fragment overlapping with conflicting data",
818       "dcp-pft.fragment.overlap.conflicts",
819       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
820     {&hf_edcp_fragment_multiple_tails,
821      {"Message has multiple tail fragments",
822       "dcp-pft.fragment.multiple_tails",
823       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
824     {&hf_edcp_fragment_too_long_fragment,
825      {"Message fragment too long", "dcp-pft.fragment.too_long_fragment",
826       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
827     {&hf_edcp_fragment_error,
828      {"Message defragmentation error", "dcp-pft.fragment.error",
829       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
830     {&hf_edcp_reassembled_in,
831      {"Reassembled in", "dcp-pft.reassembled.in",
832       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
833     {&hf_edcp_c_max,
834      {"C max", "dcp-pft.cmax",
835       FT_UINT16, BASE_DEC, NULL, 0,
836       "Maximum number of RS chunks sent", HFILL}
837      },
838     {&hf_edcp_rx_min,
839      {"Rx min", "dcp-pft.rxmin",
840       FT_UINT16, BASE_DEC, NULL, 0,
841       "Minimum number of fragments needed for RS decode", HFILL}
842      },
843     {&hf_edcp_rs_corrected,
844      {"RS Symbols Corrected", "dcp-pft.rs_corrected",
845       FT_INT16, BASE_DEC, NULL, 0,
846       "Number of symbols corrected by RS decode or -1 for failure", HFILL}
847      },
848     {&hf_edcp_rs_ok,
849      {"RS decode OK", "dcp-pft.rs_ok",
850       FT_BOOLEAN, BASE_NONE, NULL, 0,
851       "successfully decoded RS blocks", HFILL}
852      },
853     {&hf_edcp_pft_payload,
854      {"payload", "dcp-pft.payload",
855       FT_BYTES, BASE_HEX, NULL, 0,
856       "PFT Payload", HFILL}
857     }
858   };
859
860   static hf_register_info hf_tpl[] = {
861     {&hf_tpl_tlv,
862      {"tag", "dcp-tpl.tlv",
863       FT_BYTES, BASE_HEX, NULL, 0,
864       "Tag Packet", HFILL}
865      },
866     {&hf_tpl_ptr,
867      {"Type", "dcp-tpl.ptr",
868       FT_STRING, BASE_NONE, NULL, 0,
869       "Protocol Type & Revision", HFILL}
870      }
871     };
872
873 /* Setup protocol subtree array */
874   static gint *ett[] = {
875     &ett_edcp,
876     &ett_af,
877     &ett_pft,
878     &ett_tpl,
879     &ett_edcp_fragment,
880     &ett_edcp_fragments
881   };
882
883   proto_dcp_etsi = proto_register_protocol ("ETSI Distribution & Communication Protocol (for DRM)",     /* name */
884                                             "DCP (ETSI)",       /* short name */
885                                             "dcp-etsi"  /* abbrev */
886     );
887   proto_af = proto_register_protocol ("DCP Application Framing Layer", "DCP-AF", "dcp-af");
888   proto_pft = proto_register_protocol ("DCP Protection, Fragmentation & Transport Layer", "DCP-PFT", "dcp-pft");
889   proto_tpl = proto_register_protocol ("DCP Tag Packet Layer", "DCP-TPL", "dcp-tpl");
890
891   proto_register_field_array (proto_dcp_etsi, hf_edcp, array_length (hf_edcp));
892   proto_register_field_array (proto_af, hf_af, array_length (hf_af));
893   proto_register_field_array (proto_pft, hf_pft, array_length (hf_pft));
894   proto_register_field_array (proto_tpl, hf_tpl, array_length (hf_tpl));
895   proto_register_subtree_array (ett, array_length (ett));
896
897   /* subdissector code */
898   dcp_dissector_table = register_dissector_table("dcp-etsi.sync",
899             "DCP Sync", FT_STRING, BASE_NONE);
900   af_dissector_table = register_dissector_table("dcp-af.pt",
901             "AF Payload Type", FT_UINT8, BASE_DEC);
902
903   tpl_dissector_table = register_dissector_table("dcp-tpl.ptr",
904             "AF Payload Type", FT_STRING, BASE_NONE);
905
906   register_init_routine(dcp_init_protocol);
907
908 }
909
910