Try to squeeze some bytes out of the frame_data structure.
[gd/wireshark/.git] / epan / dissectors / packet-mp2t.c
1 /* packet-mp2t.c
2  *
3  * Routines for RFC 2250 MPEG2 (ISO/IEC 13818-1) Transport Stream dissection
4  *
5  * Copyright 2006, Erwin Rol <erwin@erwinrol.com>
6  * Copyright 2012-2014, Guy Martin <gmsoft@tuxicoman.be>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14
15 #include "config.h"
16
17 #include <epan/packet.h>
18 #include <wiretap/wtap.h>
19
20 #include <epan/rtp_pt.h>
21
22 #include <epan/conversation.h>
23 #include <epan/expert.h>
24 #include <epan/reassemble.h>
25 #include <epan/address_types.h>
26 #include <epan/proto_data.h>
27 #include <epan/exceptions.h>
28 #include <epan/show_exception.h>
29 #include "packet-l2tp.h"
30
31 void proto_register_mp2t(void);
32 void proto_reg_handoff_mp2t(void);
33
34 /* The MPEG2 TS packet size */
35 #define MP2T_PACKET_SIZE 188
36 #define MP2T_SYNC_BYTE   0x47
37
38 #define MP2T_PID_DOCSIS  0x1FFE
39 #define MP2T_PID_NULL    0x1FFF
40
41 static dissector_handle_t mp2t_handle;
42
43 static dissector_handle_t docsis_handle;
44 static dissector_handle_t mpeg_pes_handle;
45 static dissector_handle_t mpeg_sect_handle;
46
47 static heur_dissector_list_t heur_subdissector_list;
48
49 static int proto_mp2t = -1;
50 static gint ett_mp2t = -1;
51 static gint ett_mp2t_header = -1;
52 static gint ett_mp2t_af = -1;
53 static gint ett_mp2t_analysis = -1;
54 static gint ett_stuff = -1;
55
56 static int hf_mp2t_header = -1;
57 static int hf_mp2t_sync_byte = -1;
58 static int hf_mp2t_tei = -1;
59 static int hf_mp2t_pusi = -1;
60 static int hf_mp2t_tp = -1;
61 static int hf_mp2t_pid = -1;
62 static int hf_mp2t_tsc = -1;
63 static int hf_mp2t_afc = -1;
64 static int hf_mp2t_cc = -1;
65
66 /* static int hf_mp2t_analysis_flags = -1; */
67 static int hf_mp2t_analysis_skips = -1;
68 static int hf_mp2t_analysis_drops = -1;
69
70 #define MP2T_SYNC_BYTE_MASK  0xFF000000
71 #define MP2T_TEI_MASK        0x00800000
72 #define MP2T_PUSI_MASK       0x00400000
73 #define MP2T_TP_MASK         0x00200000
74 #define MP2T_PID_MASK        0x001FFF00
75 #define MP2T_TSC_MASK        0x000000C0
76 #define MP2T_AFC_MASK        0x00000030
77 #define MP2T_CC_MASK         0x0000000F
78
79 #define MP2T_SYNC_BYTE_SHIFT  24
80 #define MP2T_TEI_SHIFT        23
81 #define MP2T_PUSI_SHIFT       22
82 #define MP2T_TP_SHIFT         21
83 #define MP2T_PID_SHIFT         8
84 #define MP2T_TSC_SHIFT         6
85 #define MP2T_AFC_SHIFT         4
86 #define MP2T_CC_SHIFT          0
87
88 static int hf_mp2t_af = -1;
89 static int hf_mp2t_af_length = -1;
90 static int hf_mp2t_af_di = -1;
91 static int hf_mp2t_af_rai = -1;
92 static int hf_mp2t_af_espi = -1;
93 static int hf_mp2t_af_pcr_flag = -1;
94 static int hf_mp2t_af_opcr_flag = -1;
95 static int hf_mp2t_af_sp_flag = -1;
96 static int hf_mp2t_af_tpd_flag = -1;
97 static int hf_mp2t_af_afe_flag = -1;
98
99 #define MP2T_AF_DI_MASK     0x80
100 #define MP2T_AF_RAI_MASK    0x40
101 #define MP2T_AF_ESPI_MASK   0x20
102 #define MP2T_AF_PCR_MASK    0x10
103 #define MP2T_AF_OPCR_MASK   0x08
104 #define MP2T_AF_SP_MASK     0x04
105 #define MP2T_AF_TPD_MASK    0x02
106 #define MP2T_AF_AFE_MASK    0x01
107
108 #define MP2T_AF_DI_SHIFT     7
109 #define MP2T_AF_RAI_SHIFT    6
110 #define MP2T_AF_ESPI_SHIFT   5
111 #define MP2T_AF_PCR_SHIFT    4
112 #define MP2T_AF_OPCR_SHIFT   3
113 #define MP2T_AF_SP_SHIFT     2
114 #define MP2T_AF_TPD_SHIFT    1
115 #define MP2T_AF_AFE_SHIFT    0
116
117 static int hf_mp2t_af_pcr = -1;
118 static int hf_mp2t_af_opcr = -1;
119
120 static int hf_mp2t_af_sc = -1;
121
122 static int hf_mp2t_af_tpd_length = -1;
123 static int hf_mp2t_af_tpd = -1;
124
125 static int hf_mp2t_af_e_length = -1;
126 static int hf_mp2t_af_e_ltw_flag = -1;
127 static int hf_mp2t_af_e_pr_flag = -1;
128 static int hf_mp2t_af_e_ss_flag = -1;
129 static int hf_mp2t_af_e_reserved = -1;
130
131 #define MP2T_AF_E_LTW_FLAG_MASK   0x80
132 #define MP2T_AF_E_PR_FLAG_MASK    0x40
133 #define MP2T_AF_E_SS_FLAG_MASK    0x20
134
135 static int hf_mp2t_af_e_reserved_bytes = -1;
136 static int hf_mp2t_af_stuffing_bytes = -1;
137
138 static int hf_mp2t_af_e_ltwv_flag = -1;
139 static int hf_mp2t_af_e_ltwo = -1;
140
141 static int hf_mp2t_af_e_pr_reserved = -1;
142 static int hf_mp2t_af_e_pr = -1;
143
144 static int hf_mp2t_af_e_st = -1;
145 static int hf_mp2t_af_e_dnau_32_30 = -1;
146 static int hf_mp2t_af_e_m_1 = -1;
147 static int hf_mp2t_af_e_dnau_29_15 = -1;
148 static int hf_mp2t_af_e_m_2 = -1;
149 static int hf_mp2t_af_e_dnau_14_0 = -1;
150 static int hf_mp2t_af_e_m_3 = -1;
151
152 /* static int hf_mp2t_payload = -1; */
153 static int hf_mp2t_stuff_bytes = -1;
154 static int hf_mp2t_pointer = -1;
155
156 static int mp2t_no_address_type = -1;
157
158 static const value_string mp2t_sync_byte_vals[] = {
159     { MP2T_SYNC_BYTE, "Correct" },
160     { 0, NULL }
161 };
162
163 static const value_string mp2t_pid_vals[] = {
164     { 0x0000, "Program Association Table" },
165     { 0x0001, "Conditional Access Table" },
166     { 0x0002, "Transport Stream Description Table" },
167     { 0x0003, "Reserved" },
168     { 0x0004, "Reserved" },
169     { 0x0005, "Reserved" },
170     { 0x0006, "Reserved" },
171     { 0x0007, "Reserved" },
172     { 0x0008, "Reserved" },
173     { 0x0009, "Reserved" },
174     { 0x000A, "Reserved" },
175     { 0x000B, "Reserved" },
176     { 0x000C, "Reserved" },
177     { 0x000D, "Reserved" },
178     { 0x000E, "Reserved" },
179     { 0x000F, "Reserved" },
180     { 0x1FFE, "DOCSIS Data-over-cable well-known PID" },
181     { 0x1FFF, "Null packet" },
182     { 0, NULL }
183 };
184
185
186 /* Values below according ETSI ETR 289 */
187 static const value_string mp2t_tsc_vals[] = {
188     { 0, "Not scrambled" },
189     { 1, "Reserved" },
190     { 2, "Packet scrambled with Even Key" },
191     { 3, "Packet scrambled with Odd Key" },
192     { 0, NULL }
193 };
194
195 static const value_string mp2t_afc_vals[] = {
196     { 0, "Reserved" },
197     { 1, "Payload only" },
198     { 2, "Adaptation Field only" },
199     { 3, "Adaptation Field and Payload" },
200     { 0, NULL }
201 };
202
203 static gint ett_msg_fragment = -1;
204 static gint ett_msg_fragments = -1;
205 static int hf_msg_fragments = -1;
206 static int hf_msg_fragment = -1;
207 static int hf_msg_fragment_overlap = -1;
208 static int hf_msg_fragment_overlap_conflicts = -1;
209 static int hf_msg_fragment_multiple_tails = -1;
210 static int hf_msg_fragment_too_long_fragment = -1;
211 static int hf_msg_fragment_error = -1;
212 static int hf_msg_fragment_count = -1;
213 static int hf_msg_reassembled_in = -1;
214 static int hf_msg_reassembled_length = -1;
215
216 static int hf_msg_ts_packet_reassembled = -1;
217
218 static expert_field ei_mp2t_pointer = EI_INIT;
219 static expert_field ei_mp2t_cc_drop = EI_INIT;
220 static expert_field ei_mp2t_invalid_afc = EI_INIT;
221
222 static const fragment_items mp2t_msg_frag_items = {
223     /* Fragment subtrees */
224     &ett_msg_fragment,
225     &ett_msg_fragments,
226     /* Fragment fields */
227     &hf_msg_fragments,
228     &hf_msg_fragment,
229     &hf_msg_fragment_overlap,
230     &hf_msg_fragment_overlap_conflicts,
231     &hf_msg_fragment_multiple_tails,
232     &hf_msg_fragment_too_long_fragment,
233     &hf_msg_fragment_error,
234     &hf_msg_fragment_count,
235     /* Reassembled in field */
236     &hf_msg_reassembled_in,
237     /* Reassembled length field */
238     &hf_msg_reassembled_length,
239     /* Reassembled data field */
240     NULL,
241     /* Tag */
242     "Message fragments"
243 };
244
245
246 /* Data structure used for detecting CC drops
247  *
248  *  conversation
249  *    |
250  *    +-> mp2t_analysis_data
251  *          |
252  *          +-> pid_table (RB tree) (key: pid)
253  *          |     |
254  *          |     +-> pid_analysis_data (per pid)
255  *          |     +-> pid_analysis_data
256  *          |     +-> pid_analysis_data
257  *          |
258  *          +-> frame_table (RB tree) (key: pinfo->num)
259  *                |
260  *                +-> frame_analysis_data (only created if drop detected)
261  *                      |
262  *                      +-> ts_table (RB tree)
263  *                            |
264  *                            +-> ts_analysis_data (per TS subframe)
265  *                            +-> ts_analysis_data
266  *                            +-> ts_analysis_data
267  */
268
269 typedef struct mp2t_analysis_data {
270
271     /* This structure contains a tree containing data for the
272      * individual pid's, this is only used when packets are
273      * processed sequentially.
274      */
275     wmem_tree_t    *pid_table;
276
277     /* When detecting a CC drop, store that information for the
278      * given frame.  This info is needed, when clicking around in
279      * wireshark, as the pid table data only makes sense during
280      * sequential processing. The flag pinfo->fd->visited is
281      * used to tell the difference.
282      *
283      */
284     wmem_tree_t    *frame_table;
285
286     /* Total counters per conversation / multicast stream */
287     guint32 total_skips;
288     guint32 total_discontinuity;
289
290 } mp2t_analysis_data_t;
291
292 enum pid_payload_type {
293     pid_pload_unknown,
294     pid_pload_docsis,
295     pid_pload_pes,
296     pid_pload_sect,
297     pid_pload_null
298 };
299
300 typedef struct subpacket_analysis_data {
301     guint32     frag_cur_pos;
302     guint32     frag_tot_len;
303     gboolean    fragmentation;
304     guint32     frag_id;
305 } subpacket_analysis_data_t;
306
307 typedef struct packet_analysis_data {
308
309     /* Contain information for each MPEG2-TS packet in the current big packet */
310     wmem_tree_t *subpacket_table;
311 } packet_analysis_data_t;
312
313 /* Analysis TS frame info needed during sequential processing */
314 typedef struct pid_analysis_data {
315     guint16                  pid;
316     gint8                    cc_prev;      /* Previous CC number */
317     enum pid_payload_type    pload_type;
318
319     /* Fragments information used for first pass */
320     gboolean                 fragmentation;
321     guint32                  frag_cur_pos;
322     guint32                  frag_tot_len;
323     guint32                  frag_id;
324 } pid_analysis_data_t;
325
326 /* Analysis info stored for a TS frame */
327 typedef struct ts_analysis_data {
328     guint16  pid;
329     gint8    cc_prev;      /* Previous CC number */
330     guint8   skips;          /* Skips between Ccs max 14 */
331 } ts_analysis_data_t;
332
333
334 typedef struct frame_analysis_data {
335
336     /* As each frame has several pid's, thus need a pid data
337      * structure per TS frame.
338      */
339     wmem_tree_t    *ts_table;
340
341 } frame_analysis_data_t;
342
343 static mp2t_analysis_data_t *
344 init_mp2t_conversation_data(void)
345 {
346     mp2t_analysis_data_t *mp2t_data;
347
348     mp2t_data = wmem_new0(wmem_file_scope(), struct mp2t_analysis_data);
349
350     mp2t_data->pid_table = wmem_tree_new(wmem_file_scope());
351
352     mp2t_data->frame_table = wmem_tree_new(wmem_file_scope());
353
354     mp2t_data->total_skips = 0;
355     mp2t_data->total_discontinuity = 0;
356
357     return mp2t_data;
358 }
359
360 static mp2t_analysis_data_t *
361 get_mp2t_conversation_data(conversation_t *conv)
362 {
363     mp2t_analysis_data_t *mp2t_data;
364
365     mp2t_data = (mp2t_analysis_data_t *)conversation_get_proto_data(conv, proto_mp2t);
366     if (!mp2t_data) {
367         mp2t_data = init_mp2t_conversation_data();
368         conversation_add_proto_data(conv, proto_mp2t, mp2t_data);
369     }
370
371     return mp2t_data;
372 }
373
374 static frame_analysis_data_t *
375 init_frame_analysis_data(mp2t_analysis_data_t *mp2t_data, packet_info *pinfo)
376 {
377     frame_analysis_data_t *frame_analysis_data_p;
378
379     frame_analysis_data_p = wmem_new0(wmem_file_scope(), struct frame_analysis_data);
380     frame_analysis_data_p->ts_table = wmem_tree_new(wmem_file_scope());
381     /* Insert into mp2t tree */
382     wmem_tree_insert32(mp2t_data->frame_table, pinfo->num,
383             (void *)frame_analysis_data_p);
384
385     return frame_analysis_data_p;
386 }
387
388
389 static frame_analysis_data_t *
390 get_frame_analysis_data(mp2t_analysis_data_t *mp2t_data, packet_info *pinfo)
391 {
392     frame_analysis_data_t *frame_analysis_data_p;
393     frame_analysis_data_p = (frame_analysis_data_t *)wmem_tree_lookup32(mp2t_data->frame_table, pinfo->num);
394     return frame_analysis_data_p;
395 }
396
397 static pid_analysis_data_t *
398 get_pid_analysis(mp2t_analysis_data_t *mp2t_data, guint32 pid)
399 {
400     pid_analysis_data_t  *pid_data;
401
402     pid_data = (pid_analysis_data_t *)wmem_tree_lookup32(mp2t_data->pid_table, pid);
403     if (!pid_data) {
404         pid_data          = wmem_new0(wmem_file_scope(), struct pid_analysis_data);
405         pid_data->cc_prev = -1;
406         pid_data->pid     = pid;
407         pid_data->frag_id = (pid << (32 - 13)) | 0x1;
408
409         wmem_tree_insert32(mp2t_data->pid_table, pid, (void *)pid_data);
410     }
411     return pid_data;
412 }
413
414 /* Structure to handle packets, spanned across
415  * multiple MPEG packets
416  */
417 static reassembly_table mp2t_reassembly_table;
418
419 static void
420 mp2t_dissect_packet(tvbuff_t *tvb, enum pid_payload_type pload_type,
421             packet_info *pinfo, proto_tree *tree)
422 {
423     dissector_handle_t handle = NULL;
424
425     switch (pload_type) {
426         case pid_pload_docsis:
427             handle = docsis_handle;
428             break;
429         case pid_pload_pes:
430             handle = mpeg_pes_handle;
431             break;
432         case pid_pload_sect:
433             handle = mpeg_sect_handle;
434             break;
435         default:
436             /* Should not happen */
437             break;
438     }
439
440     if (handle)
441         call_dissector(handle, tvb, pinfo, tree);
442     else
443         call_data_dissector(tvb, pinfo, tree);
444 }
445
446 static guint
447 mp2t_get_packet_length(tvbuff_t *tvb, guint offset, packet_info *pinfo,
448             guint32 frag_id, enum pid_payload_type pload_type)
449 {
450     fragment_head *frag;
451     tvbuff_t      *len_tvb = NULL, *frag_tvb = NULL, *data_tvb = NULL;
452     gint           pkt_len = 0;
453     guint          remaining_len;
454
455     frag = fragment_get(&mp2t_reassembly_table, pinfo, frag_id, NULL);
456     if (frag)
457         frag = frag->next;
458
459     if (!frag) { /* First frame */
460         remaining_len = tvb_reported_length_remaining(tvb, offset);
461         if ( (pload_type == pid_pload_docsis && remaining_len < 4) ||
462                 (pload_type == pid_pload_sect && remaining_len < 3) ||
463                 (pload_type == pid_pload_pes && remaining_len < 5) ) {
464             /* Not enough info to determine the size of the encapsulated packet */
465             /* Just add the fragment and we'll check out the length later */
466             return -1;
467         }
468
469         len_tvb = tvb;
470     } else {
471         /* Create a composite tvb out of the two */
472         frag_tvb = tvb_new_subset_remaining(frag->tvb_data, 0);
473         len_tvb = tvb_new_composite();
474         tvb_composite_append(len_tvb, frag_tvb);
475
476         data_tvb = tvb_new_subset_remaining(tvb, offset);
477         tvb_composite_append(len_tvb, data_tvb);
478         tvb_composite_finalize(len_tvb);
479
480         offset = frag->offset;
481     }
482
483     /* Get the next packet's size if possible */
484     switch (pload_type) {
485         case pid_pload_docsis:
486             pkt_len = tvb_get_ntohs(len_tvb, offset + 2) + 6;
487             break;
488         case pid_pload_pes:
489             pkt_len = tvb_get_ntohs(len_tvb, offset + 4);
490             if (pkt_len) /* A size of 0 means size not bounded */
491                 pkt_len += 6;
492             break;
493         case pid_pload_sect:
494             pkt_len = (tvb_get_ntohs(len_tvb, offset + 1) & 0xFFF) + 3;
495             break;
496         default:
497             /* Should not happen */
498             break;
499     }
500
501     if (frag_tvb)
502         tvb_free(frag_tvb);
503
504     return pkt_len;
505 }
506
507 static void
508 mp2t_fragment_handle(tvbuff_t *tvb, guint offset, packet_info *pinfo,
509         proto_tree *tree, guint32 frag_id,
510         guint frag_offset, guint frag_len,
511         gboolean fragment_last, enum pid_payload_type pload_type)
512 {
513     /* proto_item *ti; */
514     fragment_head *frag_msg;
515     tvbuff_t      *new_tvb;
516     gboolean       save_fragmented;
517     address        save_src, save_dst;
518
519     save_fragmented = pinfo->fragmented;
520     pinfo->fragmented = TRUE;
521     copy_address_shallow(&save_src, &pinfo->src);
522     copy_address_shallow(&save_dst, &pinfo->dst);
523
524     /* It's possible that a fragment in the same packet set an address already
525      * This will change the hash value, we need to make sure it's NULL */
526
527     set_address(&pinfo->src, mp2t_no_address_type, 0, NULL);
528     set_address(&pinfo->dst, mp2t_no_address_type, 0, NULL);
529
530     /* check length; send frame for reassembly */
531     frag_msg = fragment_add_check(&mp2t_reassembly_table,
532             tvb, offset, pinfo, frag_id, NULL,
533             frag_offset,
534             frag_len,
535             !fragment_last);
536
537     new_tvb = process_reassembled_data(tvb, offset, pinfo,
538             "Reassembled MP2T",
539             frag_msg, &mp2t_msg_frag_items,
540             NULL, tree);
541
542     copy_address_shallow(&pinfo->src, &save_src);
543     copy_address_shallow(&pinfo->dst, &save_dst);
544
545     if (new_tvb) {
546         /* ti = */ proto_tree_add_item(tree, hf_msg_ts_packet_reassembled, tvb, 0, 0, ENC_NA);
547         mp2t_dissect_packet(new_tvb, pload_type, pinfo, tree);
548     } else {
549         col_set_str(pinfo->cinfo, COL_INFO, "[MP2T fragment of a reassembled packet]");
550     }
551
552     pinfo->fragmented = save_fragmented;
553 }
554
555
556 /*
557  * Reassembly of various payload types.
558  *
559  * DOCSIS MAC frames, PES packets, etc. may begin anywhere within an MPEG-TS
560  * packet or span multiple MPEG packets.
561  *
562  * The payload_unit_start_indicator bit in the MPEG-TS header, and the pointer
563  * field, are used to reassemble fragmented frames from MPEG-TS packets.
564  *
565  * If that bit is set, a higher-level packet begins in this MPEG-TS
566  * packet, and the MPEG-TS header is followed by a 1-octet pointer field.
567  * The value of the pointer field indicates at which byte the higher-
568  * level packet begins.  If that bit is not set, the packet begun in
569  * an earlier MPEG-TS packet continues in this packet, with the data
570  * in the payload going after the data in the previous MPEG-TS packet
571  * (there can be more than one continuing packet).
572  *
573  * If the pointer field is non-zero, this MPEG-TS packet contains
574  * the conclusion of one higher-level packet and the beginning of
575  * the next packet.
576  *
577  * As the MPEG-TS packets are of a fixed size, stuff bytes are used
578  * as padding before the first byte of a higher-level packet as
579  * necessary.
580  *
581  * This diagram is from Data-Over-Cable Service Interface Specifications,
582  * Downstream RF Interface Specification, CM-SP-DRFI-I16-170111, section 7
583  * "DOWNSTREAM TRANSMISSION CONVERGENCE SUBLAYER", and shows how the
584  * higher-level packets are transported over the MPEG Transport Stream:
585  *
586  *+--------------------------------------------------------------------------------+
587  *|MPEG Header | pointer_field | stuff_bytes | Start of Packet #1                  |
588  *|(PUSI = 1)  | (= 0)         | (0 or more) | (up to 183 bytes)                   |
589  *+--------------------------------------------------------------------------------+
590  *+--------------------------------------------------------------------------------+
591  *|MPEG Header |  Continuation of Packet #1                                        |
592  *|(PUSI = 0)  |  (up to 183 bytes)                                                |
593  *+--------------------------------------------------------------------------------+
594  *+---------------------------------------------------------------------------------+
595  *|MPEG Header | pointer_field |Tail of Packet #1 | stuff_bytes |Start of Packet #2 |
596  *|(PUSI = 1)  | (= M)         |(M bytes)         | (0 or more) |(N bytes)          |
597  *+---------------------------------------------------------------------------------+
598  *
599  * For PES and PSI, see ISO/IEC 13818-1 / ITU-T Rec. H.222.0 (05/2006),
600  * section 2.4.3.3 "Semantic definition of fields in Transport Stream packet
601  * layer", which says much the same thing.
602  */
603 static void
604 mp2t_process_fragmented_payload(tvbuff_t *tvb, gint offset, guint remaining_len, packet_info *pinfo,
605         proto_tree *tree, proto_tree *header_tree, guint32 pusi_flag,
606         pid_analysis_data_t *pid_analysis)
607 {
608     tvbuff_t                  *next_tvb;
609     guint8                     pointer       = 0;
610     proto_item                *pi;
611     guint                      stuff_len     = 0;
612     proto_tree                *stuff_tree;
613     packet_analysis_data_t    *pdata         = NULL;
614     subpacket_analysis_data_t *spdata        = NULL;
615     guint32                    frag_cur_pos  = 0, frag_tot_len = 0;
616     gboolean                   fragmentation = FALSE;
617     guint32                    frag_id       = 0;
618
619     if (pusi_flag && pid_analysis->pload_type == pid_pload_unknown
620             && remaining_len > 3) {
621         /* We should already have identified if it was a DOCSIS packet
622          * Remaining possibility is PES or SECT */
623         if (tvb_get_ntoh24(tvb, offset) == 0x000001) {
624             /* Looks like a PES packet to me ... */
625             pid_analysis->pload_type = pid_pload_pes;
626         } else {
627             /* Most probably a SECT packet */
628             pid_analysis->pload_type = pid_pload_sect;
629         }
630     }
631
632     /* Unable to determine the payload type, do nothing */
633     if (pid_analysis->pload_type == pid_pload_unknown)
634         return;
635
636     /* PES packet don't have pointer fields, others do */
637     if (pusi_flag && pid_analysis->pload_type != pid_pload_pes) {
638         pointer = tvb_get_guint8(tvb, offset);
639         pi = proto_tree_add_item(header_tree, hf_mp2t_pointer, tvb, offset, 1, ENC_BIG_ENDIAN);
640         offset++;
641         remaining_len--;
642         if (pointer > remaining_len) {
643             /* Bogus pointer */
644             expert_add_info_format(pinfo, pi, &ei_mp2t_pointer,
645                     "Pointer value is too large (> remaining data length %u)",
646                     remaining_len);
647         }
648     }
649
650     if (!pinfo->fd->visited) {
651         /* Get values from our current PID analysis */
652         frag_cur_pos = pid_analysis->frag_cur_pos;
653         frag_tot_len = pid_analysis->frag_tot_len;
654         fragmentation = pid_analysis->fragmentation;
655         frag_id = pid_analysis->frag_id;
656         pdata = (packet_analysis_data_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto_mp2t, 0);
657         if (!pdata) {
658             pdata = wmem_new0(wmem_file_scope(), packet_analysis_data_t);
659             pdata->subpacket_table = wmem_tree_new(wmem_file_scope());
660             p_add_proto_data(wmem_file_scope(), pinfo, proto_mp2t, 0, pdata);
661
662         } else {
663             spdata = (subpacket_analysis_data_t *)wmem_tree_lookup32(pdata->subpacket_table, offset);
664         }
665
666         if (!spdata) {
667             spdata = wmem_new0(wmem_file_scope(), subpacket_analysis_data_t);
668             /* Save the info into pdata from pid_analysis */
669             spdata->frag_cur_pos = frag_cur_pos;
670             spdata->frag_tot_len = frag_tot_len;
671             spdata->fragmentation = fragmentation;
672             spdata->frag_id = frag_id;
673             wmem_tree_insert32(pdata->subpacket_table, offset, (void *)spdata);
674         }
675     } else {
676         /* Get saved values */
677         pdata = (packet_analysis_data_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto_mp2t, 0);
678         if (!pdata) {
679             /* Occurs for the first packets in the capture which cannot be reassembled */
680             return;
681         }
682
683         spdata = (subpacket_analysis_data_t *)wmem_tree_lookup32(pdata->subpacket_table, offset);
684         if (!spdata) {
685             /* Occurs for the first sub packets in the capture which cannot be reassembled */
686             return;
687         }
688
689         frag_cur_pos = spdata->frag_cur_pos;
690         frag_tot_len = spdata->frag_tot_len;
691         fragmentation = spdata->fragmentation;
692         frag_id = spdata->frag_id;
693     }
694
695     if (frag_tot_len == (guint)-1) {
696         frag_tot_len = mp2t_get_packet_length(tvb, offset, pinfo, frag_id, pid_analysis->pload_type);
697
698         if (frag_tot_len == (guint)-1) {
699             return;
700         }
701     }
702
703
704     /* The beginning of a new packet is present */
705     if (pusi_flag) {
706         if (pointer > remaining_len) {
707             /*
708              * Quit, so we don't use the bogus pointer value;
709              * that could cause remaining_len to become
710              * "negative", meaning it becomes a very large
711              * positive value.
712              */
713             return;
714         }
715
716         /* "pointer" contains the number of bytes until the
717          * start of the new section
718          *
719          * if the new section does not start immediately after the
720          * pointer field (i.e. pointer>0), the remaining bytes before the
721          * start of the section are another fragment of the
722          * current packet
723          *
724          * if pointer is 0, a new upper-layer packet starts at the
725          * beginning of this TS packet
726          * if we have pending fragments, the last TS packet contained the
727          * last fragment and at the time we processed it, we couldn't figure
728          * out that it is the last fragment
729          * this is the case e.g. for PES packets with a 0 length field
730          * ("unbounded length")
731          * to handle this case, we add an empty fragment (pointer==0)
732          * and reassemble, then we process the current TS packet as
733          * usual
734          */
735         if (fragmentation) {
736             mp2t_fragment_handle(tvb, offset, pinfo, tree, frag_id, frag_cur_pos,
737                     pointer, TRUE, pid_analysis->pload_type);
738             frag_id++;
739         }
740
741         offset += pointer;
742         remaining_len -= pointer;
743         fragmentation = FALSE;
744         frag_cur_pos = 0;
745         frag_tot_len = 0;
746
747         if (!remaining_len) {
748             /* Shouldn't happen */
749             goto save_state;
750         }
751
752         while (remaining_len > 0) {
753             /* Don't like subsequent packets overwrite the Info column */
754             col_append_str(pinfo->cinfo, COL_INFO, " ");
755             col_set_fence(pinfo->cinfo, COL_INFO);
756
757             /* Skip stuff bytes */
758             stuff_len = 0;
759             while ((tvb_get_guint8(tvb, offset + stuff_len) == 0xFF)) {
760                 stuff_len++;
761                 if (stuff_len >= remaining_len) {
762                     remaining_len = 0;
763                     break;
764                 }
765             }
766
767             if (stuff_len) {
768                 stuff_tree = proto_tree_add_subtree_format(tree, tvb, offset, stuff_len, ett_stuff, NULL, "Stuffing");
769                 proto_tree_add_item(stuff_tree, hf_mp2t_stuff_bytes, tvb, offset, stuff_len, ENC_NA);
770                 offset += stuff_len;
771                 if (stuff_len >= remaining_len) {
772                     goto save_state;
773                 }
774                 remaining_len -= stuff_len;
775             }
776
777             /* Get the next packet's size if possible */
778             frag_tot_len = mp2t_get_packet_length(tvb, offset, pinfo, frag_id, pid_analysis->pload_type);
779             if (frag_tot_len == (guint)-1 || !frag_tot_len) {
780                 mp2t_fragment_handle(tvb, offset, pinfo, tree, frag_id, 0, remaining_len, FALSE, pid_analysis->pload_type);
781                 fragmentation = TRUE;
782                 /*offset += remaining_len;*/
783                 frag_cur_pos += remaining_len;
784                 goto save_state;
785             }
786
787             /* Check for full packets within this TS frame */
788             if (frag_tot_len &&
789                     frag_tot_len <= remaining_len) {
790                 next_tvb = tvb_new_subset_length(tvb, offset, frag_tot_len);
791                 mp2t_dissect_packet(next_tvb, pid_analysis->pload_type, pinfo, tree);
792                 remaining_len -= frag_tot_len;
793                 offset += frag_tot_len;
794                 frag_tot_len = 0;
795                 frag_id++;
796             } else {
797                 break;
798             }
799         }
800
801         if (remaining_len == 0) {
802             pid_analysis->frag_cur_pos = 0;
803             pid_analysis->frag_tot_len = 0;
804             goto save_state;
805
806         }
807
808     }
809
810     /* There are remaining bytes. Add them to the fragment list */
811
812     if ((frag_tot_len && frag_cur_pos + remaining_len >= frag_tot_len) || (!frag_tot_len && pusi_flag)) {
813         mp2t_fragment_handle(tvb, offset, pinfo, tree, frag_id, frag_cur_pos, remaining_len, TRUE, pid_analysis->pload_type);
814         frag_id++;
815         fragmentation = FALSE;
816         frag_cur_pos = 0;
817         frag_tot_len = 0;
818     } else {
819         mp2t_fragment_handle(tvb, offset, pinfo, tree, frag_id, frag_cur_pos, remaining_len, FALSE, pid_analysis->pload_type);
820         fragmentation = TRUE;
821         frag_cur_pos += remaining_len;
822     }
823
824 save_state:
825     pid_analysis->fragmentation = fragmentation;
826     pid_analysis->frag_cur_pos = frag_cur_pos;
827     pid_analysis->frag_tot_len = frag_tot_len;
828     pid_analysis->frag_id = frag_id;
829 }
830
831
832
833 /* Calc the number of skipped CC numbers. Note that this can easy
834  * overflow, and a value above 7 indicate several network packets
835  * could be lost.
836  */
837 static guint32
838 calc_skips(gint32 curr, gint32 prev)
839 {
840     int res;
841
842     /* Only count the missing TS frames in between prev and curr.
843      * The "prev" frame CC number seen is confirmed received, it's
844      * the next frames CC counter which is the first known missing
845      * TS frame
846      */
847     prev += 1;
848
849     /* Calc missing TS frame 'skips' */
850     res = curr - prev;
851
852     /* Handle wrap around */
853     if (res < 0)
854         res += 16;
855
856     return res;
857 }
858
859 #define KEY(pid, cc) ((pid << 4)|cc)
860
861 static guint32
862 detect_cc_drops(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo,
863         guint32 pid, gint32 cc_curr, mp2t_analysis_data_t *mp2t_data)
864 {
865     gint32 cc_prev = -1;
866     pid_analysis_data_t   *pid_data              = NULL;
867     ts_analysis_data_t    *ts_data               = NULL;
868     frame_analysis_data_t *frame_analysis_data_p = NULL;
869     proto_item            *flags_item;
870
871     gboolean detected_drop = FALSE;
872     guint32 skips = 0;
873
874     /* The initial sequential processing stage */
875     if (!pinfo->fd->visited) {
876         /* This is the sequential processing stage */
877         pid_data = get_pid_analysis(mp2t_data, pid);
878
879         cc_prev = pid_data->cc_prev;
880         pid_data->cc_prev = cc_curr;
881
882         /* Null packet always have a CC value equal 0 */
883         if (pid == 0x1fff)
884             return 0;
885
886         /* Its allowed that (cc_prev == cc_curr) if adaptation field */
887         if (cc_prev == cc_curr)
888             return 0;
889
890         /* Have not seen this pid before */
891         if (cc_prev == -1)
892             return 0;
893
894         /* Detect if CC is not increasing by one all the time */
895         if (cc_curr != ((cc_prev+1) & MP2T_CC_MASK)) {
896             detected_drop = TRUE;
897
898             skips = calc_skips(cc_curr, cc_prev);
899
900             mp2t_data->total_skips += skips;
901             mp2t_data->total_discontinuity++;
902             /* TODO: if (skips > 7) signal_loss++; ??? */
903         }
904     }
905
906     /* Save the info about the dropped packet */
907     if (detected_drop && !pinfo->fd->visited) {
908         /* Lookup frame data, contains TS pid data objects */
909         frame_analysis_data_p = get_frame_analysis_data(mp2t_data, pinfo);
910         if (!frame_analysis_data_p)
911             frame_analysis_data_p = init_frame_analysis_data(mp2t_data, pinfo);
912
913         /* Create and store a new TS frame pid_data object.
914            This indicate that we have a drop
915          */
916         ts_data = wmem_new0(wmem_file_scope(), struct ts_analysis_data);
917         ts_data->cc_prev = cc_prev;
918         ts_data->pid = pid;
919         ts_data->skips = skips;
920         wmem_tree_insert32(frame_analysis_data_p->ts_table, KEY(pid, cc_curr),
921                  (void *)ts_data);
922     }
923
924     /* See if we stored info about drops */
925     if (pinfo->fd->visited) {
926
927         /* Lookup frame data, contains TS pid data objects */
928         frame_analysis_data_p = get_frame_analysis_data(mp2t_data, pinfo);
929         if (!frame_analysis_data_p)
930             return 0; /* No stored frame data -> no drops*/
931         else {
932             ts_data = (struct ts_analysis_data *)wmem_tree_lookup32(frame_analysis_data_p->ts_table,
933                            KEY(pid, cc_curr));
934
935             if (ts_data) {
936                 if (ts_data->skips > 0) {
937                     detected_drop = TRUE;
938                     cc_prev = ts_data->cc_prev;
939                     skips   = ts_data->skips;
940                 }
941             }
942         }
943     }
944
945     /* Add info to the proto tree about drops */
946     if (detected_drop) {
947         expert_add_info_format(pinfo, tree, &ei_mp2t_cc_drop,
948                 "Detected %d missing TS frames before this (last_cc:%d total skips:%d discontinuity:%d)",
949                 skips, cc_prev,
950                 mp2t_data->total_skips,
951                 mp2t_data->total_discontinuity
952                 );
953
954         flags_item = proto_tree_add_uint(tree, hf_mp2t_analysis_skips,
955                 tvb, 0, 0, skips);
956         PROTO_ITEM_SET_GENERATED(flags_item);
957
958         flags_item = proto_tree_add_uint(tree, hf_mp2t_analysis_drops,
959                 tvb, 0, 0, 1);
960         PROTO_ITEM_SET_GENERATED(flags_item);
961     }
962     return skips;
963 }
964
965 static gint
966 dissect_mp2t_adaptation_field(tvbuff_t *tvb, gint offset, proto_tree *tree)
967 {
968     gint        af_start_offset;
969     proto_item *hi;
970     proto_tree *mp2t_af_tree;
971     guint8      af_length;
972     guint8      af_flags;
973     gint        stuffing_len;
974
975     af_length = tvb_get_guint8(tvb, offset);
976     proto_tree_add_item(tree, hf_mp2t_af_length, tvb, offset, 1, ENC_BIG_ENDIAN);
977     offset += 1;
978     /* fix issues where afc==3 but af_length==0
979      *  Adaptaion field...spec section 2.4.3.5: The value 0 is for inserting a single
980      *  stuffing byte in a Transport Stream packet. When the adaptation_field_control
981      *  value is '11', the value of the adaptation_field_length shall be in the range 0 to 182.
982      */
983     if (af_length == 0)
984         return offset;
985
986     af_start_offset = offset;
987
988     hi = proto_tree_add_item( tree, hf_mp2t_af, tvb, offset, af_length, ENC_NA);
989     mp2t_af_tree = proto_item_add_subtree( hi, ett_mp2t_af );
990
991     af_flags = tvb_get_guint8(tvb, offset);
992     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_di, tvb, offset, 1, ENC_BIG_ENDIAN);
993     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_rai, tvb, offset, 1, ENC_BIG_ENDIAN);
994     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_espi, tvb, offset, 1, ENC_BIG_ENDIAN);
995     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_pcr_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
996     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_opcr_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
997     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_sp_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
998     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_tpd_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
999     proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_afe_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1000     offset += 1;
1001
1002     if (af_flags &  MP2T_AF_PCR_MASK) {
1003         guint64 pcr_base;
1004         guint16 pcr_ext;
1005
1006         /* 33 bit PCR base, 6 bit reserved, 9 bit PCR ext */
1007         pcr_base = tvb_get_ntoh48(tvb, offset) >> (48-33);
1008         pcr_ext  = (guint16)(tvb_get_ntoh48(tvb, offset) & 0x1FF);
1009
1010          proto_tree_add_uint64(mp2t_af_tree, hf_mp2t_af_pcr, tvb, offset, 6,
1011                 pcr_base*300 + pcr_ext);
1012
1013         offset += 6;
1014     }
1015
1016     if (af_flags &  MP2T_AF_OPCR_MASK) {
1017         guint64 opcr_base;
1018         guint16 opcr_ext;
1019
1020         /* the same format as PCR above */
1021         opcr_base = tvb_get_ntoh48(tvb, offset) >> (48-33);
1022         opcr_ext  = (guint16)(tvb_get_ntoh48(tvb, offset) & 0x1FF);
1023
1024         proto_tree_add_uint64(mp2t_af_tree, hf_mp2t_af_opcr, tvb, offset, 6,
1025                 opcr_base*300 + opcr_ext);
1026
1027         offset += 6;
1028     }
1029
1030     if (af_flags &  MP2T_AF_SP_MASK) {
1031         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_sc, tvb, offset, 1, ENC_BIG_ENDIAN);
1032         offset += 1;
1033     }
1034
1035     if (af_flags &  MP2T_AF_TPD_MASK) {
1036         guint8 tpd_len;
1037
1038         tpd_len = tvb_get_guint8(tvb, offset);
1039         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_tpd_length, tvb, offset, 1, ENC_BIG_ENDIAN);
1040         offset += 1;
1041
1042         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_tpd, tvb, offset, tpd_len, ENC_NA);
1043         offset += tpd_len;
1044     }
1045
1046     if (af_flags &  MP2T_AF_AFE_MASK) {
1047         guint8 e_len;
1048         guint8 e_flags;
1049         gint e_start_offset = offset;
1050         gint reserved_len = 0;
1051
1052         e_len = tvb_get_guint8(tvb, offset);
1053         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_length, tvb, offset, 1, ENC_BIG_ENDIAN);
1054         offset += 1;
1055
1056         e_flags = tvb_get_guint8(tvb, offset);
1057         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_ltw_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1058         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_pr_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1059         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_ss_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1060         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_reserved, tvb, offset, 1, ENC_BIG_ENDIAN);
1061         offset += 1;
1062
1063         if (e_flags & MP2T_AF_E_LTW_FLAG_MASK) {
1064             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_ltwv_flag, tvb, offset, 2, ENC_BIG_ENDIAN);
1065             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_ltwo, tvb, offset, 2, ENC_BIG_ENDIAN);
1066             offset += 2;
1067         }
1068
1069         if (e_flags & MP2T_AF_E_PR_FLAG_MASK) {
1070             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_pr_reserved, tvb, offset, 3, ENC_BIG_ENDIAN);
1071             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_pr, tvb, offset, 3, ENC_BIG_ENDIAN);
1072             offset += 3;
1073         }
1074
1075         if (e_flags & MP2T_AF_E_SS_FLAG_MASK) {
1076             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_st, tvb, offset, 1, ENC_BIG_ENDIAN);
1077             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_dnau_32_30, tvb, offset, 1, ENC_BIG_ENDIAN);
1078             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_m_1, tvb, offset, 1, ENC_BIG_ENDIAN);
1079             offset += 1;
1080             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_dnau_29_15, tvb, offset, 2, ENC_BIG_ENDIAN);
1081             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_m_2, tvb, offset, 2, ENC_BIG_ENDIAN);
1082             offset += 2;
1083             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_dnau_14_0, tvb, offset, 2, ENC_BIG_ENDIAN);
1084             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_m_3, tvb, offset, 2, ENC_BIG_ENDIAN);
1085             offset += 2;
1086         }
1087
1088         reserved_len = (e_len + 1) - (offset - e_start_offset);
1089         if (reserved_len > 0) {
1090             proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_e_reserved_bytes, tvb, offset, reserved_len, ENC_NA);
1091             offset += reserved_len;
1092         }
1093     }
1094
1095     stuffing_len = af_length - (offset - af_start_offset);
1096     if (stuffing_len > 0) {
1097         proto_tree_add_item( mp2t_af_tree, hf_mp2t_af_stuffing_bytes, tvb, offset, stuffing_len, ENC_NA);
1098         offset += stuffing_len;
1099     }
1100
1101     return offset;
1102 }
1103
1104 static void
1105 dissect_tsp(tvbuff_t *tvb, gint offset, packet_info *pinfo,
1106         proto_tree *tree, conversation_t *conv)
1107 {
1108     guint32              header;
1109     guint                afc;
1110     gint                 start_offset = offset;
1111     gint                 payload_len;
1112     mp2t_analysis_data_t *mp2t_data;
1113     pid_analysis_data_t *pid_analysis;
1114
1115     guint32     skips;
1116     guint32     pid;
1117     guint32     cc;
1118     guint32     pusi_flag;
1119
1120     guint32 tsc;
1121
1122     proto_item *ti;
1123     proto_item *hi;
1124     proto_item *item = NULL;
1125     proto_tree *mp2t_tree;
1126     proto_tree *mp2t_header_tree;
1127     proto_tree *mp2t_analysis_tree;
1128     proto_item *afci;
1129
1130     ti = proto_tree_add_item( tree, proto_mp2t, tvb, offset, MP2T_PACKET_SIZE, ENC_NA );
1131     mp2t_tree = proto_item_add_subtree( ti, ett_mp2t );
1132
1133     header = tvb_get_ntohl(tvb, offset);
1134     pusi_flag = (header & 0x00400000);
1135     pid = (header & MP2T_PID_MASK) >> MP2T_PID_SHIFT;
1136     tsc = (header & MP2T_TSC_MASK);
1137     afc = (header & MP2T_AFC_MASK) >> MP2T_AFC_SHIFT;
1138     cc  = (header & MP2T_CC_MASK)  >> MP2T_CC_SHIFT;
1139
1140     proto_item_append_text(ti, " PID=0x%x CC=%d", pid, cc);
1141     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MPEG TS");
1142
1143     hi = proto_tree_add_item( mp2t_tree, hf_mp2t_header, tvb, offset, 4, ENC_BIG_ENDIAN);
1144     mp2t_header_tree = proto_item_add_subtree( hi, ett_mp2t_header );
1145
1146     proto_tree_add_item( mp2t_header_tree, hf_mp2t_sync_byte, tvb, offset, 4, ENC_BIG_ENDIAN);
1147     proto_tree_add_item( mp2t_header_tree, hf_mp2t_tei, tvb, offset, 4, ENC_BIG_ENDIAN);
1148     proto_tree_add_item( mp2t_header_tree, hf_mp2t_pusi, tvb, offset, 4, ENC_BIG_ENDIAN);
1149     proto_tree_add_item( mp2t_header_tree, hf_mp2t_tp, tvb, offset, 4, ENC_BIG_ENDIAN);
1150     proto_tree_add_item( mp2t_header_tree, hf_mp2t_pid, tvb, offset, 4, ENC_BIG_ENDIAN);
1151     proto_tree_add_item( mp2t_header_tree, hf_mp2t_tsc, tvb, offset, 4, ENC_BIG_ENDIAN);
1152     afci = proto_tree_add_item( mp2t_header_tree, hf_mp2t_afc, tvb, offset, 4, ENC_BIG_ENDIAN);
1153     proto_tree_add_item( mp2t_header_tree, hf_mp2t_cc, tvb, offset, 4, ENC_BIG_ENDIAN);
1154
1155     mp2t_data = get_mp2t_conversation_data(conv);
1156
1157     pid_analysis = get_pid_analysis(mp2t_data, pid);
1158
1159     if (pid_analysis->pload_type == pid_pload_unknown) {
1160         if (pid == MP2T_PID_NULL) {
1161             pid_analysis->pload_type = pid_pload_null;
1162         } else if (pid == MP2T_PID_DOCSIS) {
1163             pid_analysis->pload_type = pid_pload_docsis;
1164         }
1165     }
1166
1167     if (pid_analysis->pload_type == pid_pload_docsis && (afc != 1)) {
1168         /* DOCSIS packets should not have an adaptation field */
1169         if (afc != 1) {
1170             expert_add_info_format(pinfo, afci, &ei_mp2t_invalid_afc,
1171                     "Adaptation Field Control for DOCSIS packets must be 0x01");
1172         }
1173     }
1174
1175     if (pid_analysis->pload_type == pid_pload_null) {
1176         col_set_str(pinfo->cinfo, COL_INFO, "NULL packet");
1177         if (afc != 1) {
1178             expert_add_info_format(pinfo, afci, &ei_mp2t_invalid_afc,
1179                     "Adaptation Field Control for NULL packets must be 0x01");
1180         }
1181         /* Nothing more to do */
1182         return;
1183     }
1184
1185     offset += 4;
1186
1187     /* Create a subtree for analysis stuff */
1188     mp2t_analysis_tree = proto_tree_add_subtree_format(mp2t_tree, tvb, offset, 0, ett_mp2t_analysis, &item, "MPEG2 PCR Analysis");
1189     PROTO_ITEM_SET_GENERATED(item);
1190
1191     skips = detect_cc_drops(tvb, mp2t_analysis_tree, pinfo, pid, cc, mp2t_data);
1192
1193     if (skips > 0)
1194         proto_item_append_text(ti, " skips=%d", skips);
1195
1196     if (afc == 2 || afc == 3)
1197         offset = dissect_mp2t_adaptation_field(tvb, offset, mp2t_tree);
1198
1199     if ((offset - start_offset) < MP2T_PACKET_SIZE)
1200         payload_len = MP2T_PACKET_SIZE - (offset - start_offset);
1201     else
1202         payload_len = 0;
1203
1204     if (!payload_len)
1205         return;
1206
1207     if (afc == 2) {
1208         col_set_str(pinfo->cinfo, COL_INFO, "Adaptation field only");
1209         /* The rest of the packet is stuffing bytes */
1210         proto_tree_add_item( mp2t_tree, hf_mp2t_stuff_bytes, tvb, offset, payload_len, ENC_NA);
1211         offset += payload_len;
1212     }
1213
1214     if (!tsc) {
1215         mp2t_process_fragmented_payload(tvb, offset, payload_len, pinfo, tree, mp2t_tree, pusi_flag, pid_analysis);
1216     } else {
1217         /* Payload is scrambled */
1218         col_set_str(pinfo->cinfo, COL_INFO, "Scrambled TS payload");
1219     }
1220 }
1221
1222
1223 static int
1224 dissect_mp2t( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_ )
1225 {
1226     volatile guint offset = 0;
1227     conversation_t *conv;
1228     const char     *saved_proto;
1229
1230     conv = find_or_create_conversation(pinfo);
1231
1232     for (; tvb_reported_length_remaining(tvb, offset) >= MP2T_PACKET_SIZE; offset += MP2T_PACKET_SIZE) {
1233         /*
1234          * Dissect the TSP.
1235          *
1236          * If it gets an error that means there's no point in
1237          * dissecting any more TSPs, rethrow the exception in
1238          * question.
1239          *
1240          * If it gets any other error, report it and continue, as that
1241          * means that TSP got an error, but that doesn't mean we should
1242          * stop dissecting TSPs within this frame or chunk of reassembled
1243          * data.
1244          */
1245         saved_proto = pinfo->current_proto;
1246         TRY {
1247             dissect_tsp(tvb, offset, pinfo, tree, conv);
1248         }
1249         CATCH_NONFATAL_ERRORS {
1250             show_exception(tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE);
1251
1252             /*
1253              * Restore the saved protocol as well; we do this after
1254              * show_exception(), so that the "Malformed packet" indication
1255              * shows the protocol for which dissection failed.
1256              */
1257             pinfo->current_proto = saved_proto;
1258         }
1259         ENDTRY;
1260     }
1261     return tvb_captured_length(tvb);
1262 }
1263
1264 static gboolean
1265 heur_dissect_mp2t( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_ )
1266 {
1267     gint length;
1268     guint offset = 0;
1269
1270     length = tvb_reported_length_remaining(tvb, offset);
1271     if (length == 0) {
1272         /* Nothing to check for */
1273         return FALSE;
1274     }
1275     if ((length % MP2T_PACKET_SIZE) != 0) {
1276         /* Not a multiple of the MPEG-2 transport packet size */
1277         return FALSE;
1278     } else {
1279         while (tvb_offset_exists(tvb, offset)) {
1280             if (tvb_get_guint8(tvb, offset) != MP2T_SYNC_BYTE) {
1281                 /* No sync byte at the appropriate offset */
1282                 return FALSE;
1283             }
1284             offset += MP2T_PACKET_SIZE;
1285         }
1286     }
1287
1288     dissect_mp2t(tvb, pinfo, tree, data);
1289     return TRUE;
1290 }
1291
1292
1293 void
1294 proto_register_mp2t(void)
1295 {
1296     static hf_register_info hf[] = {
1297         { &hf_mp2t_header, {
1298             "Header", "mp2t.header",
1299             FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL
1300         } } ,
1301         { &hf_mp2t_sync_byte, {
1302             "Sync Byte", "mp2t.sync_byte",
1303             FT_UINT32, BASE_HEX, VALS(mp2t_sync_byte_vals), MP2T_SYNC_BYTE_MASK, NULL, HFILL
1304         } } ,
1305         { &hf_mp2t_tei, {
1306             "Transport Error Indicator", "mp2t.tei",
1307             FT_UINT32, BASE_DEC, NULL, MP2T_TEI_MASK, NULL, HFILL
1308         } } ,
1309         { &hf_mp2t_pusi, {
1310             "Payload Unit Start Indicator", "mp2t.pusi",
1311             FT_UINT32, BASE_DEC, NULL, MP2T_PUSI_MASK, NULL, HFILL
1312         } } ,
1313         { &hf_mp2t_tp, {
1314             "Transport Priority", "mp2t.tp",
1315             FT_UINT32, BASE_DEC, NULL, MP2T_TP_MASK, NULL, HFILL
1316         } } ,
1317         { &hf_mp2t_pid, {
1318             "PID", "mp2t.pid",
1319             FT_UINT32, BASE_HEX, VALS(mp2t_pid_vals), MP2T_PID_MASK, NULL, HFILL
1320         } } ,
1321         { &hf_mp2t_tsc, {
1322             "Transport Scrambling Control", "mp2t.tsc",
1323             FT_UINT32, BASE_HEX, VALS(mp2t_tsc_vals), MP2T_TSC_MASK, NULL, HFILL
1324         } } ,
1325         { &hf_mp2t_afc, {
1326             "Adaptation Field Control", "mp2t.afc",
1327             FT_UINT32, BASE_HEX, VALS(mp2t_afc_vals) , MP2T_AFC_MASK, NULL, HFILL
1328         } } ,
1329         { &hf_mp2t_cc, {
1330             "Continuity Counter", "mp2t.cc",
1331             FT_UINT32, BASE_DEC, NULL, MP2T_CC_MASK, NULL, HFILL
1332         } } ,
1333 #if 0
1334         { &hf_mp2t_analysis_flags, {
1335             "MPEG2-TS Analysis Flags", "mp2t.analysis.flags",
1336             FT_NONE, BASE_NONE, NULL, 0x0,
1337             "This frame has some of the MPEG2 analysis flags set", HFILL
1338         } } ,
1339 #endif
1340         { &hf_mp2t_analysis_skips, {
1341             "TS Continuity Counter Skips", "mp2t.analysis.skips",
1342             FT_UINT8, BASE_DEC, NULL, 0x0,
1343             "Missing TS frames according to CC counter values", HFILL
1344         } } ,
1345         { &hf_mp2t_analysis_drops, {
1346             "Some frames dropped", "mp2t.analysis.drops",
1347             FT_UINT8, BASE_DEC, NULL, 0x0,
1348             "Discontinuity: A number of TS frames were dropped", HFILL
1349         } } ,
1350         { &hf_mp2t_af, {
1351             "Adaptation Field", "mp2t.af",
1352             FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL
1353         } } ,
1354         { &hf_mp2t_af_length, {
1355             "Adaptation Field Length", "mp2t.af.length",
1356             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
1357         } } ,
1358         { &hf_mp2t_af_di, {
1359             "Discontinuity Indicator", "mp2t.af.di",
1360             FT_UINT8, BASE_DEC, NULL, MP2T_AF_DI_MASK, NULL, HFILL
1361         } } ,
1362         { &hf_mp2t_af_rai, {
1363             "Random Access Indicator", "mp2t.af.rai",
1364             FT_UINT8, BASE_DEC, NULL, MP2T_AF_RAI_MASK, NULL, HFILL
1365         } } ,
1366         { &hf_mp2t_af_espi, {
1367             "Elementary Stream Priority Indicator", "mp2t.af.espi",
1368             FT_UINT8, BASE_DEC, NULL, MP2T_AF_ESPI_MASK, NULL, HFILL
1369         } } ,
1370         { &hf_mp2t_af_pcr_flag, {
1371             "PCR Flag", "mp2t.af.pcr_flag",
1372             FT_UINT8, BASE_DEC, NULL, MP2T_AF_PCR_MASK, NULL, HFILL
1373         } } ,
1374         { &hf_mp2t_af_opcr_flag, {
1375             "OPCR Flag", "mp2t.af.opcr_flag",
1376             FT_UINT8, BASE_DEC, NULL, MP2T_AF_OPCR_MASK, NULL, HFILL
1377         } } ,
1378         { &hf_mp2t_af_sp_flag, {
1379             "Splicing Point Flag", "mp2t.af.sp_flag",
1380             FT_UINT8, BASE_DEC, NULL, MP2T_AF_SP_MASK, NULL, HFILL
1381         } } ,
1382         { &hf_mp2t_af_tpd_flag, {
1383             "Transport Private Data Flag", "mp2t.af.tpd_flag",
1384             FT_UINT8, BASE_DEC, NULL, MP2T_AF_TPD_MASK, NULL, HFILL
1385         } } ,
1386         { &hf_mp2t_af_afe_flag, {
1387             "Adaptation Field Extension Flag", "mp2t.af.afe_flag",
1388             FT_UINT8, BASE_DEC, NULL, MP2T_AF_AFE_MASK, NULL, HFILL
1389         } } ,
1390         { &hf_mp2t_af_pcr, {
1391             "Program Clock Reference", "mp2t.af.pcr",
1392             FT_UINT64, BASE_HEX, NULL, 0, NULL, HFILL
1393         } } ,
1394         { &hf_mp2t_af_opcr, {
1395             "Original Program Clock Reference", "mp2t.af.opcr",
1396             FT_UINT64, BASE_HEX, NULL, 0, NULL, HFILL
1397         } } ,
1398         { &hf_mp2t_af_sc, {
1399             "Splice Countdown", "mp2t.af.sc",
1400             FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1401         } } ,
1402         { &hf_mp2t_af_tpd_length, {
1403             "Transport Private Data Length", "mp2t.af.tpd_length",
1404             FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1405         } } ,
1406         { &hf_mp2t_af_tpd, {
1407             "Transport Private Data", "mp2t.af.tpd",
1408             FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL
1409         } } ,
1410         { &hf_mp2t_af_e_length, {
1411             "Adaptation Field Extension Length", "mp2t.af.e_length",
1412             FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1413         } } ,
1414         { &hf_mp2t_af_e_ltw_flag, {
1415             "LTW Flag", "mp2t.af.e.ltw_flag",
1416             FT_UINT8, BASE_DEC, NULL, MP2T_AF_E_LTW_FLAG_MASK, NULL, HFILL
1417         } } ,
1418         { &hf_mp2t_af_e_pr_flag, {
1419             "Piecewise Rate Flag", "mp2t.af.e.pr_flag",
1420             FT_UINT8, BASE_DEC, NULL, MP2T_AF_E_PR_FLAG_MASK, NULL, HFILL
1421         } } ,
1422         { &hf_mp2t_af_e_ss_flag, {
1423             "Seamless Splice Flag", "mp2t.af.e.ss_flag",
1424             FT_UINT8, BASE_DEC, NULL, MP2T_AF_E_SS_FLAG_MASK, NULL, HFILL
1425         } } ,
1426         { &hf_mp2t_af_e_reserved, {
1427             "Reserved", "mp2t.af.e.reserved",
1428             FT_UINT8, BASE_DEC, NULL, 0x1F, NULL, HFILL
1429         } } ,
1430         { &hf_mp2t_af_e_reserved_bytes, {
1431             "Reserved", "mp2t.af.e.reserved_bytes",
1432             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL
1433         } } ,
1434         { &hf_mp2t_af_stuffing_bytes, {
1435             "Stuffing", "mp2t.af.stuffing_bytes",
1436             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL
1437         } } ,
1438         { &hf_mp2t_af_e_ltwv_flag, {
1439             "LTW Valid Flag", "mp2t.af.e.ltwv_flag",
1440             FT_UINT16, BASE_DEC, NULL, 0x8000, NULL, HFILL
1441         } } ,
1442         { &hf_mp2t_af_e_ltwo, {
1443             "LTW Offset", "mp2t.af.e.ltwo",
1444             FT_UINT16, BASE_DEC, NULL, 0x7FFF, NULL, HFILL
1445         } } ,
1446         { &hf_mp2t_af_e_pr_reserved, {
1447             "Reserved", "mp2t.af.e.pr_reserved",
1448             FT_UINT24, BASE_DEC, NULL, 0xC00000, NULL, HFILL
1449         } } ,
1450         { &hf_mp2t_af_e_pr, {
1451             "Piecewise Rate", "mp2t.af.e.pr",
1452             FT_UINT24, BASE_DEC, NULL, 0x3FFFFF, NULL, HFILL
1453         } } ,
1454         { &hf_mp2t_af_e_st, {
1455             "Splice Type", "mp2t.af.e.st",
1456             FT_UINT8, BASE_DEC, NULL, 0xF0, NULL, HFILL
1457         } } ,
1458         { &hf_mp2t_af_e_dnau_32_30, {
1459             "DTS Next AU[32...30]", "mp2t.af.e.dnau_32_30",
1460             FT_UINT8, BASE_DEC, NULL, 0x0E, NULL, HFILL
1461         } } ,
1462         { &hf_mp2t_af_e_m_1, {
1463             "Marker Bit", "mp2t.af.e.m_1",
1464             FT_UINT8, BASE_DEC, NULL, 0x01, NULL, HFILL
1465         } } ,
1466         { &hf_mp2t_af_e_dnau_29_15, {
1467             "DTS Next AU[29...15]", "mp2t.af.e.dnau_29_15",
1468             FT_UINT16, BASE_DEC, NULL, 0xFFFE, NULL, HFILL
1469         } } ,
1470         { &hf_mp2t_af_e_m_2, {
1471             "Marker Bit", "mp2t.af.e.m_2",
1472             FT_UINT16, BASE_DEC, NULL, 0x0001, NULL, HFILL
1473         } } ,
1474         { &hf_mp2t_af_e_dnau_14_0, {
1475             "DTS Next AU[14...0]", "mp2t.af.e.dnau_14_0",
1476             FT_UINT16, BASE_DEC, NULL, 0xFFFE, NULL, HFILL
1477         } } ,
1478         { &hf_mp2t_af_e_m_3, {
1479             "Marker Bit", "mp2t.af.e.m_3",
1480             FT_UINT16, BASE_DEC, NULL, 0x0001, NULL, HFILL
1481         } } ,
1482 #if 0
1483         { &hf_mp2t_payload, {
1484             "Payload", "mp2t.payload",
1485             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL
1486         } } ,
1487 #endif
1488         { &hf_mp2t_stuff_bytes, {
1489             "Stuffing", "mp2t.stuff_bytes",
1490             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL
1491         } },
1492         { &hf_mp2t_pointer, {
1493             "Pointer", "mp2t.pointer",
1494             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
1495         } },
1496         {  &hf_msg_fragments, {
1497             "Message fragments", "mp2t.msg.fragments",
1498             FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL
1499         } },
1500         {  &hf_msg_fragment, {
1501             "Message fragment", "mp2t.msg.fragment",
1502             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL
1503         } },
1504         {  &hf_msg_fragment_overlap, {
1505             "Message fragment overlap", "mp2t.msg.fragment.overlap",
1506             FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL
1507         } },
1508         {  &hf_msg_fragment_overlap_conflicts, {
1509             "Message fragment overlapping with conflicting data",
1510             "mp2t.msg.fragment.overlap.conflicts",
1511             FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL
1512         } },
1513         {  &hf_msg_fragment_multiple_tails, {
1514             "Message has multiple tail fragments",
1515             "mp2t.msg.fragment.multiple_tails",
1516             FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL
1517         } },
1518         {  &hf_msg_fragment_too_long_fragment, {
1519             "Message fragment too long", "mp2t.msg.fragment.too_long_fragment",
1520             FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL
1521         } },
1522         {  &hf_msg_fragment_error, {
1523             "Message defragmentation error", "mp2t.msg.fragment.error",
1524             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL
1525         } },
1526         {  &hf_msg_fragment_count, {
1527             "Message fragment count", "mp2t.msg.fragment.count",
1528             FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL
1529         } },
1530         {  &hf_msg_reassembled_in, {
1531             "Reassembled in", "mp2t.msg.reassembled.in",
1532             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL
1533         } },
1534         {  &hf_msg_reassembled_length, {
1535             "Reassembled MP2T length", "mp2t.msg.reassembled.length",
1536             FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL
1537         } },
1538         {  &hf_msg_ts_packet_reassembled, {
1539             "MPEG TS Packet (reassembled)", "mp2t.ts_packet_reassembled",
1540             FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL
1541         } },
1542     };
1543
1544     static gint *ett[] =
1545     {
1546         &ett_mp2t,
1547         &ett_mp2t_header,
1548         &ett_mp2t_af,
1549         &ett_mp2t_analysis,
1550         &ett_stuff,
1551         &ett_msg_fragment,
1552         &ett_msg_fragments
1553     };
1554
1555     static ei_register_info ei[] = {
1556         { &ei_mp2t_pointer, { "mp2t.pointer_too_large", PI_MALFORMED, PI_ERROR, "Pointer value is too large", EXPFILL }},
1557         { &ei_mp2t_cc_drop, { "mp2t.cc.drop", PI_MALFORMED, PI_ERROR, "Detected missing TS frames", EXPFILL }},
1558         { &ei_mp2t_invalid_afc, { "mp2t.afc.invalid", PI_PROTOCOL, PI_WARN,
1559                                     "Adaptation Field Control contains an invalid value", EXPFILL }}
1560     };
1561
1562     expert_module_t* expert_mp2t;
1563
1564     proto_mp2t = proto_register_protocol("ISO/IEC 13818-1", "MP2T", "mp2t");
1565
1566     mp2t_handle = register_dissector("mp2t", dissect_mp2t, proto_mp2t);
1567
1568     proto_register_field_array(proto_mp2t, hf, array_length(hf));
1569     proto_register_subtree_array(ett, array_length(ett));
1570     expert_mp2t = expert_register_protocol(proto_mp2t);
1571     expert_register_field_array(expert_mp2t, ei, array_length(ei));
1572
1573     mp2t_no_address_type = address_type_dissector_register("AT_MP2T_NONE", "No MP2T Address", none_addr_to_str, none_addr_str_len, NULL, NULL, none_addr_len, NULL, NULL);
1574
1575     heur_subdissector_list = register_heur_dissector_list("mp2t.pid", proto_mp2t);
1576     /* Register init of processing of fragmented DEPI packets */
1577     reassembly_table_register(&mp2t_reassembly_table,
1578         &addresses_reassembly_table_functions);
1579 }
1580
1581
1582
1583 void
1584 proto_reg_handoff_mp2t(void)
1585 {
1586     heur_dissector_add("udp", heur_dissect_mp2t, "MP2T over UDP", "mp2t_udp", proto_mp2t, HEURISTIC_ENABLE);
1587
1588     dissector_add_uint("rtp.pt", PT_MP2T, mp2t_handle);
1589     dissector_add_for_decode_as_with_preference("tcp.port", mp2t_handle);
1590     dissector_add_for_decode_as_with_preference("udp.port", mp2t_handle);
1591     heur_dissector_add("usb.bulk", heur_dissect_mp2t, "MP2T USB bulk endpoint", "mp2t_usb_bulk", proto_mp2t, HEURISTIC_ENABLE);
1592     dissector_add_uint("wtap_encap", WTAP_ENCAP_MPEG_2_TS, mp2t_handle);
1593     dissector_add_uint("l2tp.pw_type", L2TPv3_PROTOCOL_DOCSIS_DMPT, mp2t_handle);
1594     dissector_add_string("media_type", "video/mp2t", mp2t_handle);
1595
1596     docsis_handle = find_dissector("docsis");
1597     mpeg_pes_handle = find_dissector("mpeg-pes");
1598     mpeg_sect_handle = find_dissector("mpeg_sect");
1599 }
1600
1601 /*
1602  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1603  *
1604  * Local variables:
1605  * c-basic-offset: 4
1606  * tab-width: 8
1607  * indent-tabs-mode: nil
1608  * End:
1609  *
1610  * vi: set shiftwidth=4 tabstop=8 expandtab:
1611  * :indentSize=4:tabSize=8:noTabs=true:
1612  */