aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-simulcrypt.c
blob: 97f09e087e68506a74dc549520bbab15817886ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/* packet-simulcrypt.c
* Simulcrypt protocol across SCS - ECMG interface as defined in ETSI TS 103.197 v 1.5.1
*
* David Castleford, Orange Labs / France Telecom R&D
* Oct 2008
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>
#include <epan/packet.h>
#include <epan/dissectors/packet-tcp.h>
#include <epan/prefs.h>

#define PROTO_TAG_SIMULCRYPT		"SIMULCRYPT"
#define CA_SYSTEM_ID_MIKEY		0x9999  /* CA_system_ID corresponding to MIKEY ECM */
#define CA_SYSTEM_ID_MIKEY_PROTO 	"mikey" /* Protocol name to be used to "decode as" ECMs with CA_SYSTEM_ID_MIKEY */

/* Tecm_interpretation links ca_system_id to ecmg port and protocol name for dissection of ecm_datagram in ECM_Response message
* Currently size is 1 as only have MIKEY protocol but could add extra protocols
* could add option in preferences for new ca_system_id for new protocol for example
*/
typedef struct Tecm_interpretation
{
	int ca_system_id;
	char *protocol_name;
	dissector_handle_t protocol_handle;
	guint ecmg_port;
} ecm_interpretation;

#define ECM_MIKEY_INDEX 0  /* must agree with tab_ecm_inter initialization */

static ecm_interpretation tab_ecm_inter[]={
	{CA_SYSTEM_ID_MIKEY, CA_SYSTEM_ID_MIKEY_PROTO, NULL, -1}
};

#define ECM_INTERPRETATION_SIZE (sizeof(tab_ecm_inter)/sizeof(ecm_interpretation))

static void  dissect_simulcrypt_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static guint get_simulcrypt_message_len(packet_info *pinfo, tvbuff_t *tvb, int offset);

/* Wireshark ID of the SIMULCRYPT protocol */
static guint proto_simulcrypt = -1;

/* Preferences (with default values) */
static guint global_simulcrypt_tcp_port = 0;   /* Simulcrypt registered only if pref set to non-zero value */
static int ca_system_id_mikey = CA_SYSTEM_ID_MIKEY; /* MIKEY ECM CA_system_ID */

/* MIKEY payload start bytes */
/*unsigned char mikey_start[3]={0x01,0x00,0x15};
* mikey_start[0]=0x01;	 first byte mikey payload (version)
* mikey_start[1]=0x00;	 second byte mikey payload (data type)
* mikey_start[2]=0x15;	 third byte (next payload)
*/

/* Simulcrypt ECMG - SCS */
#define SIMULCRYPT_CHANNEL_SETUP 			0x0001
#define SIMULCRYPT_CHANNEL_TEST 			0x0002
#define SIMULCRYPT_CHANNEL_STATUS			0x0003
#define SIMULCRYPT_CHANNEL_CLOSE			0x0004
#define SIMULCRYPT_CHANNEL_ERROR			0x0005
#define SIMULCRYPT_STREAM_SETUP 			0x0101
#define SIMULCRYPT_STREAM_TEST 				0x0102
#define SIMULCRYPT_STREAM_STATUS			0x0103
#define SIMULCRYPT_STREAM_CLOSE_REQUEST			0x0104
#define SIMULCRYPT_STREAM_CLOSE_RESPONSE		0x0105
#define SIMULCRYPT_STREAM_ERROR				0x0106
#define SIMULCRYPT_CW_PROVISION				0x0201
#define SIMULCRYPT_ECM_RESPONSE				0x0202

static const value_string messagetypenames[] = {
	{ SIMULCRYPT_CHANNEL_SETUP, "CHANNEL_SETUP" },
	{ SIMULCRYPT_CHANNEL_TEST, "CHANNEL_TEST" },
	{ SIMULCRYPT_CHANNEL_STATUS, "CHANNEL_STATUS" },
	{ SIMULCRYPT_CHANNEL_CLOSE, "CHANNEL_CLOSE" },
	{ SIMULCRYPT_CHANNEL_ERROR, "CHANNEL_ERROR" },
	{ SIMULCRYPT_STREAM_SETUP, "STREAM_SETUP" },
	{ SIMULCRYPT_STREAM_TEST, "STREAM_TEST" },
	{ SIMULCRYPT_STREAM_STATUS, "STREAM_STATUS" },
	{ SIMULCRYPT_STREAM_CLOSE_REQUEST, "STREAM_CLOSE_REQUEST" },
	{ SIMULCRYPT_STREAM_CLOSE_RESPONSE, "STREAM_CLOSE_RESPONSE" },
	{ SIMULCRYPT_STREAM_ERROR, "STREAM_ERROR" },
	{ SIMULCRYPT_CW_PROVISION, "CW_PROVISION" },
	{ SIMULCRYPT_ECM_RESPONSE, "ECM_RESPONSE" },
	{ 0, NULL}
};	

/* Simulcrypt Parameter Types */
#define SIMULCRYPT_DVB_RESERVED 			0x0000
#define SIMULCRYPT_SUPER_CAS_ID 			0x0001
#define SIMULCRYPT_SECTION_TSPKT_FLAG 			0x0002
#define SIMULCRYPT_DELAY_START 				0x0003
#define SIMULCRYPT_DELAY_STOP	 			0x0004
#define SIMULCRYPT_TRANSITION_DELAY_START		0x0005
#define SIMULCRYPT_TRANSITION_DELAY_STOP		0x0006
#define SIMULCRYPT_ECM_REP_PERIOD	 		0x0007
#define SIMULCRYPT_MAX_STREAMS	 			0x0008
#define SIMULCRYPT_MIN_CP_DURATION 			0x0009
#define SIMULCRYPT_LEAD_CW	 			0x000A
#define SIMULCRYPT_CW_PER_MESSAGE 			0x000B
#define SIMULCRYPT_MAX_COMP_TIME 			0x000C
#define SIMULCRYPT_ACCESS_CRITERIA			0x000D
#define SIMULCRYPT_ECM_CHANNEL_ID			0x000E
#define SIMULCRYPT_ECM_STREAM_ID			0x000F
#define SIMULCRYPT_NOMINAL_CP_DURATION 			0x0010
#define SIMULCRYPT_ACCESS_CRITERIA_TRANSFER_MODE	0x0011
#define SIMULCRYPT_CP_NUMBER 				0x0012
#define SIMULCRYPT_CP_DURATION 				0x0013
#define SIMULCRYPT_CP_CW_COMBINATION 			0x0014
#define SIMULCRYPT_ECM_DATAGRAM				0x0015
#define SIMULCRYPT_AC_DELAY_START 			0x0016
#define SIMULCRYPT_AC_DELAY_STOP	 		0x0017
#define SIMULCRYPT_CW_ENCRYPTION	 		0x0018
#define SIMULCRYPT_ECM_ID				0x0019
#define SIMULCRYPT_ERROR_STATUS				0x7000
#define SIMULCRYPT_ERROR_INFORMATION			0x7001

static const value_string parametertypenames[] = {
	{ SIMULCRYPT_DVB_RESERVED, "DVB_RESERVED" },
	{ SIMULCRYPT_SUPER_CAS_ID, "SUPER_CAS_ID" },
	{ SIMULCRYPT_SECTION_TSPKT_FLAG, "SECTION_TSPKT_FLAG" },
	{ SIMULCRYPT_DELAY_START, "DELAY_START" },
	{ SIMULCRYPT_DELAY_STOP, "DELAY_STOP" },
	{ SIMULCRYPT_TRANSITION_DELAY_START, "TRANSITION_DELAY_START" },
	{ SIMULCRYPT_TRANSITION_DELAY_STOP, "TRANSITION_DELAY_STOP" },
	{ SIMULCRYPT_ECM_REP_PERIOD, "ECM_REP_PERIOD" },
	{ SIMULCRYPT_MAX_STREAMS, "MAX_STREAMS" },
	{ SIMULCRYPT_MIN_CP_DURATION, "MIN_CP_DURATION" },
	{ SIMULCRYPT_LEAD_CW, "LEAD_CW" },
	{ SIMULCRYPT_CW_PER_MESSAGE, "CW_PER_MESSAGE" },
	{ SIMULCRYPT_MAX_COMP_TIME, "MAX_COMP_TIME" },
	{ SIMULCRYPT_ACCESS_CRITERIA, "ACCESS_CRITERIA" },
	{ SIMULCRYPT_ECM_CHANNEL_ID, "ECM_CHANNEL_ID" },
	{ SIMULCRYPT_ECM_STREAM_ID, "ECM_STREAM_ID" },
	{ SIMULCRYPT_NOMINAL_CP_DURATION, "NOMINAL_CP_DURATION" },
	{ SIMULCRYPT_ACCESS_CRITERIA_TRANSFER_MODE, "ACCESS_CRITERIA_TRANSFER_MODE" },
	{ SIMULCRYPT_CP_NUMBER, "CP_NUMBER" },
	{ SIMULCRYPT_CP_DURATION, "CP_DURATION" },
	{ SIMULCRYPT_CP_CW_COMBINATION, "CP_CW_COMBINATION" },
	{ SIMULCRYPT_ECM_DATAGRAM, "ECM_DATAGRAM" },
	{ SIMULCRYPT_AC_DELAY_START, "AC_DELAY_START" },
	{ SIMULCRYPT_AC_DELAY_STOP, "AC_DELAY_STOP" },
	{ SIMULCRYPT_CW_ENCRYPTION, "CW_ENCRYPTION" },
	{ SIMULCRYPT_ECM_ID, "ECM_ID" },
	{ SIMULCRYPT_ERROR_STATUS, "ERROR_STATUS" },
	{ SIMULCRYPT_ERROR_INFORMATION, "ERROR_INFORMATION" },
	{ 0, NULL}
};

/* The following hf_* variables are used to hold the Wireshark IDs of
* our header fields; they are filled out when we call
* proto_register_field_array() in proto_register_simulcrypt()
*/
static gint hf_simulcrypt_header = -1;
static gint hf_simulcrypt_version = -1;
static gint hf_simulcrypt_message_type = -1;
static gint hf_simulcrypt_message_length = -1;
static gint hf_simulcrypt_message = -1;
static gint hf_simulcrypt_parameter = -1;
static gint hf_simulcrypt_parameter_type = -1;
static gint hf_simulcrypt_parameter_length = -1;
static gint hf_simulcrypt_ca_system_id = -1;
static gint hf_simulcrypt_ca_subsystem_id = -1;
static gint hf_simulcrypt_super_cas_id = -1;
static gint hf_simulcrypt_section_tspkt_flag = -1;
static gint hf_simulcrypt_ecm_channel_id = -1;
static gint hf_simulcrypt_delay_start = -1;
static gint hf_simulcrypt_delay_stop = -1;
static gint hf_simulcrypt_ac_delay_start = -1;
static gint hf_simulcrypt_ac_delay_stop = -1;
static gint hf_simulcrypt_transition_delay_start = -1;
static gint hf_simulcrypt_transition_delay_stop = -1;
static gint hf_simulcrypt_ecm_rep_period = -1;
static gint hf_simulcrypt_max_streams = -1;
static gint hf_simulcrypt_min_cp_duration = -1;
static gint hf_simulcrypt_lead_cw = -1;
static gint hf_simulcrypt_cw_per_msg = -1;
static gint hf_simulcrypt_max_comp_time = -1;
static gint hf_simulcrypt_access_criteria = -1;
static gint hf_simulcrypt_ecm_stream_id = -1;
static gint hf_simulcrypt_nominal_cp_duration = -1;
static gint hf_simulcrypt_access_criteria_transfer_mode = -1;
static gint hf_simulcrypt_cp_number = -1;
static gint hf_simulcrypt_cp_duration = -1;
static gint hf_simulcrypt_cp_cw_combination = -1;
static gint hf_simulcrypt_ecm_datagram = -1;
static gint hf_simulcrypt_cw_encryption = -1;
static gint hf_simulcrypt_ecm_id = -1;
static gint hf_simulcrypt_error_status = -1;
static gint hf_simulcrypt_error_information = -1;

/* These are the ids of the subtrees that we may be creating */
static gint ett_simulcrypt = -1;
static gint ett_simulcrypt_header = -1;
static gint ett_simulcrypt_message = -1;
static gint ett_simulcrypt_parameter = -1;
static gint ett_simulcrypt_super_cas_id = -1;
static gint ett_simulcrypt_ecm_datagram = -1;


#define FRAME_HEADER_LEN 8

/* The main dissecting routine */
static void dissect_simulcrypt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    tcp_dissect_pdus(tvb, pinfo, tree, TRUE, FRAME_HEADER_LEN,
                     get_simulcrypt_message_len, dissect_simulcrypt_message);
}

/* Informative tree structure is shown here:
* TREE 	-
*	- HEADER
*		version
*		message type
*		message length
*	- MESSAGE
*		- TYPE of parameter
*			length of parameter
			value of parameter
			- PARAMETER (optional branch for certain parameters only)
*				parameter value sub items here
* End informative tree structure 
*/

/* This method dissects fully reassembled messages */
static void dissect_simulcrypt_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{

	proto_item *simulcrypt_item;
	proto_tree *simulcrypt_tree;
	proto_tree *simulcrypt_header_tree;
	proto_tree *simulcrypt_message_tree;
	proto_tree *simulcrypt_parameter_tree;
	proto_tree *simulcrypt_super_cas_id_tree;
	proto_tree *simulcrypt_ecm_datagram_tree;
	guint16     type;
	tvbuff_t   *next_tvb;

	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_TAG_SIMULCRYPT);
	/* Clear out stuff in the info column */
	if(check_col(pinfo->cinfo,COL_INFO))
	{
		col_clear(pinfo->cinfo,COL_INFO);
	}

	/* get 2 byte type value */
	type =  tvb_get_ntohs(tvb, 1); /* 2 bytes starting at offset 1 are the message type */

	if (check_col(pinfo->cinfo, COL_INFO))
	{
		col_add_fstr(pinfo->cinfo, COL_INFO, "%d > %d Info Type:[%s]",
		pinfo->srcport, pinfo->destport, 
		val_to_str(type, messagetypenames, "Unknown Type:0x%02x"));
	}

	if (tree)
	{
		/* we are being asked for details */
		guint32 offset = 0;
		guint32 msg_length;
		
		simulcrypt_item = proto_tree_add_item(tree, proto_simulcrypt, tvb, 0, -1, FALSE);
		simulcrypt_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt);

		/* Simulcrypt_tree analysis */
		/* we are being asked for details */
		/* ADD HEADER BRANCH */
		simulcrypt_item = proto_tree_add_item(simulcrypt_tree, hf_simulcrypt_header, tvb, offset, 5, FALSE );
		simulcrypt_header_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt_header);
		proto_item_append_text(simulcrypt_header_tree, ", Length: %s", "5 bytes"); /* add text to Header tree indicating Length 5 bytes */

		/* Simulcrypt_header_tree analysis */
		/* Message Version 1 Byte */
		proto_tree_add_item(simulcrypt_header_tree, hf_simulcrypt_version, tvb, offset, 1, FALSE);		
		offset+=1;

		/* Message  Type 2 Bytes */
		proto_tree_add_item(simulcrypt_header_tree, hf_simulcrypt_message_type, tvb, offset, 2, FALSE);
		offset+=2;

		/* Message Length 2 Bytes */
		simulcrypt_item=proto_tree_add_item(simulcrypt_header_tree, hf_simulcrypt_message_length, tvb, offset, 2, FALSE);
		proto_item_append_text(simulcrypt_item, " (bytes)");
		msg_length = tvb_get_ntohs(tvb, offset); /* read 2 byte message length value */
		offset+=2;
	
		/* ADD MESSAGE BRANCH */
		simulcrypt_item = proto_tree_add_item( simulcrypt_tree, hf_simulcrypt_message, tvb, offset, -1, FALSE );
		simulcrypt_message_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt_message);
		proto_item_append_text(simulcrypt_message_tree, " containing TLV parameters"); /* add text to Message tree	*/	
		proto_item_append_text(simulcrypt_message_tree, ", Length: %d (bytes)", msg_length); /* add length info to message_tree */
		
		/* end header details */
 
		/* Simulcrypt_message_tree analysis */
		/*  we are being asked for details */
		/* Navigate through message after header to find one or more parameters */
		while (offset < (msg_length+5))  /* offset is from beginning of the 5 byte header */
		{
			guint16 plen;         /* parameter length */
			guint16 ptype;        /* parameter type */
			guint32 pvaluedec;    /* parameter decimal value */
			gchar  *pvalue_char;  /* parameter value string */
			int     ca_system_id;
			guint   i;
			
			/* Parameter  Type 2 Bytes */
			ptype= tvb_get_ntohs(tvb, offset); /* read 2 byte type value */
			/* Parameter  Length 2 Bytes */
			plen=tvb_get_ntohs(tvb, offset+2); /* read 2 byte length value */
			/* Parameter  Value plen Bytes */
			pvalue_char = tvb_bytes_to_str(tvb, offset+4, plen);

			simulcrypt_item = proto_tree_add_item( simulcrypt_message_tree, hf_simulcrypt_parameter, tvb, offset, plen+2+2, FALSE );

			/* add length and value info to type */
			proto_item_append_text(simulcrypt_item, ": Type=%s", val_to_str(ptype, parametertypenames, "Unknown Type:0x%02x"));
			proto_item_append_text(simulcrypt_item, ", Value Length=%d (bytes)", plen); /* add length info to parameter */
			proto_item_append_text(simulcrypt_item, ", Value=0x%s", pvalue_char); /* add value info to parameter */
			/* add subtree for parameter type, length and value items */
			simulcrypt_parameter_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt_parameter); /* add subtree for Length and Value */
			simulcrypt_item=proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_parameter_type, tvb, offset, 2, FALSE); /* parameter type */
			simulcrypt_item=proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_parameter_length, tvb, offset+2, 2, FALSE); /* length item */
			offset += 2+2;  /* offset --> parameter value */
			proto_item_append_text(simulcrypt_item, " (bytes)");
			
			switch(ptype)
			{
				case SIMULCRYPT_SUPER_CAS_ID:
					/* add super_cas_id item */
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_super_cas_id, tvb, offset, plen, FALSE); /* value item */
					simulcrypt_super_cas_id_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt_super_cas_id);
					
					/* Simulcrypt_super_cas_id_tree */
					simulcrypt_item = proto_tree_add_item(simulcrypt_super_cas_id_tree, hf_simulcrypt_ca_system_id, tvb, offset, 2, FALSE );
						
					/* Test for known CA_System_ID */
					ca_system_id = tvb_get_ntohs(tvb,offset);
					for(i=0;i<ECM_INTERPRETATION_SIZE;i++)
					{			
						if(tab_ecm_inter[i].ca_system_id==ca_system_id)
						{
							tab_ecm_inter[i].ecmg_port=pinfo->destport;
							proto_item_append_text(simulcrypt_item, ", Port %d, Protocol %s",tab_ecm_inter[i].ecmg_port, tab_ecm_inter[i].protocol_name);
							break;
						}
					}
					simulcrypt_item = proto_tree_add_item(simulcrypt_super_cas_id_tree, hf_simulcrypt_ca_subsystem_id, tvb, offset+2, 2, FALSE );
					break;
				case SIMULCRYPT_SECTION_TSPKT_FLAG:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_section_tspkt_flag, tvb, offset, plen, FALSE); /* value item */
					break;
				case SIMULCRYPT_ECM_CHANNEL_ID:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ecm_channel_id, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_DELAY_START:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_delay_start, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_DELAY_STOP:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_delay_stop, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms"); 
					break;
				case SIMULCRYPT_TRANSITION_DELAY_START:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_transition_delay_start, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_TRANSITION_DELAY_STOP:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_transition_delay_stop, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_AC_DELAY_START:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ac_delay_start, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_AC_DELAY_STOP:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ac_delay_stop, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_ECM_REP_PERIOD:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ecm_rep_period, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_MAX_STREAMS:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_max_streams, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_MIN_CP_DURATION:
					/* convert value to ms (in units 100 ms) */
					pvaluedec = tvb_get_ntohs(tvb, offset); /* read 2 byte min CP duration value */
					pvaluedec = pvaluedec*100; /* in ms now */
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_min_cp_duration, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " (%d ms)",pvaluedec);
					break;
				case SIMULCRYPT_LEAD_CW:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_lead_cw, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_CW_PER_MESSAGE:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_cw_per_msg, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_MAX_COMP_TIME:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_max_comp_time, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " ms");
					break;
				case SIMULCRYPT_ACCESS_CRITERIA:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_access_criteria, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_ECM_STREAM_ID:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ecm_stream_id, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_NOMINAL_CP_DURATION:
					/* convert value to ms (in units 100 ms) */
					pvaluedec = tvb_get_ntohs(tvb, offset); /* read 2 byte nominal CP duration value */
					pvaluedec = pvaluedec*100; /* in ms now */
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_nominal_cp_duration, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " (%d ms)", pvaluedec);
					break;
				case SIMULCRYPT_ACCESS_CRITERIA_TRANSFER_MODE:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_access_criteria_transfer_mode, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_CP_NUMBER:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_cp_number, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_CP_DURATION:
					/* convert value to ms (in units 100 ms) */
					pvaluedec = tvb_get_ntohs(tvb, offset); /* read 2 byte CP duration value */
					pvaluedec = pvaluedec*100; /* in ms now */
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_cp_duration, tvb, offset, plen, FALSE);
					proto_item_append_text(simulcrypt_item, " (%d ms)", pvaluedec);
					break;
				case SIMULCRYPT_CP_CW_COMBINATION:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_cp_cw_combination, tvb, offset, plen, FALSE); 
					break;
				case SIMULCRYPT_ECM_DATAGRAM:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ecm_datagram, tvb, offset, plen, FALSE); 
					/* Test srcport against table of ECMG ports & CA_System_ID for known protocol types */
					for(i=0;i<ECM_INTERPRETATION_SIZE;i++)
					{
						if(tab_ecm_inter[i].ecmg_port==pinfo->srcport) /* ECMG source port */
						{ /* recognise port & ca_system_id and hence protocol name for ECM datagram */
							next_tvb = tvb_new_subset(tvb, offset, -1, -1);
							simulcrypt_ecm_datagram_tree = proto_item_add_subtree(simulcrypt_item, ett_simulcrypt_ecm_datagram);
							if(tab_ecm_inter[i].protocol_handle != NULL)
							{
								call_dissector(tab_ecm_inter[i].protocol_handle, next_tvb,pinfo, simulcrypt_ecm_datagram_tree);
							}
							break;
						}
					}
					break;
				case SIMULCRYPT_CW_ENCRYPTION:
					simulcrypt_item = proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_cw_encryption, tvb, offset, plen, FALSE); 
					break;
				case SIMULCRYPT_ECM_ID:
					simulcrypt_item=proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_ecm_id, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_ERROR_STATUS:
					simulcrypt_item=proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_error_status, tvb, offset, plen, FALSE);
					break;
				case SIMULCRYPT_ERROR_INFORMATION:
					simulcrypt_item=proto_tree_add_item( simulcrypt_parameter_tree, hf_simulcrypt_error_information, tvb, offset, plen, FALSE);
					break;
				default:  /* Unknown parameter type */
					simulcrypt_item = proto_tree_add_text( simulcrypt_parameter_tree, tvb, offset, plen,
                                                                               "Parameter Value: %s", pvalue_char); 
					break;
			} /* end parameter type switch */
			offset += plen;
		} /* end parameter tree details */
			
	} /* end tree */
}	

/* determine PDU length of protocol foo */
static guint get_simulcrypt_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
{
	guint iLg;
	
	iLg = tvb_get_ntohs(tvb,offset+3); /*length is at offset 3 */
	iLg += 5; /* add 1 byte version + 2 byte type + 2 byte length (simulcrypt "header" */
	return iLg;
}	

/* Clean out the ecm_interpretation port association whenever            */
/* making a pass through a capture file to dissect all its packets       */
/*  (e.g., reading in a new capture file, changing a simulcrypt pref,    */
/*  or running a "filter packets" or "colorize packets" pass over the    */
/*  current capture file.                                                */

static void 
simulcrypt_init(void) 
{
	guint i;

	for(i=0;i<ECM_INTERPRETATION_SIZE;i++)
	{
		tab_ecm_inter[i].ecmg_port = -1;
	}
}

void proto_reg_handoff_simulcrypt(void);

void proto_register_simulcrypt (void)
{
	/* A header field is something you can search/filter on.
	* 
	* We create a structure to register our fields. It consists of an
	* array of hf_register_info structures, each of which are of the format
	* {&(field id), {name, abbrev, type, display, strings, bitmask, blurb, HFILL}}.
	*/
	static hf_register_info hf[] =
	{
		{ &hf_simulcrypt_header,
		{ "Header", "simulcrypt.header", FT_NONE, BASE_NONE, NULL, 0x0,
		 NULL, HFILL }},
		 
		{ &hf_simulcrypt_version,
		{ "Version", "simulcrypt.version", FT_UINT8, BASE_HEX, NULL, 0x0, 	/* version 1 byte */
		NULL, HFILL }},

		{ &hf_simulcrypt_message_type,
		{ "Message Type", "simulcrypt.message.type", FT_UINT16, BASE_HEX, VALS(messagetypenames), 0x0,		/* type 2 bytes */
		 NULL, HFILL }},

		{ &hf_simulcrypt_message_length,
		{ "Message Length", "simulcrypt.message.len", FT_UINT16, BASE_DEC, NULL, 0x0,		/* length 2 bytes, print as decimal value */
		NULL, HFILL }},

		{ &hf_simulcrypt_message,
		{ "Message", "simulcrypt.message", FT_NONE, BASE_NONE, NULL, 0x0,
		 NULL, HFILL }}, 
		 
		{ &hf_simulcrypt_parameter,
		{ "Parameter", "simulcrypt.parameter", FT_NONE, BASE_NONE, NULL, 0x0,
		 NULL, HFILL }}, 
		 
		{ &hf_simulcrypt_parameter_type,
		{ "Parameter Type", "simulcrypt.parameter.type", FT_UINT16, BASE_HEX, VALS(parametertypenames), 0x0,	/* type 2 bytes */
		 NULL, HFILL }},

		{ &hf_simulcrypt_parameter_length,
		{ "Parameter Length", "simulcrypt.parameter.len", FT_UINT16, BASE_DEC, NULL, 0x0,		/* length 2 bytes, print as decimal value */
		NULL, HFILL }},

		{ &hf_simulcrypt_ca_system_id,
		{ "CA System ID", "simulcrypt.parameter.ca_system_id", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }}, 

		{ &hf_simulcrypt_ca_subsystem_id,
		{ "CA Subsystem ID", "simulcrypt.parameter.ca_subsystem_id", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},
		 
		{ &hf_simulcrypt_super_cas_id,
		{ "SuperCAS ID", "simulcrypt.super_cas_id", FT_UINT32, BASE_HEX, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_section_tspkt_flag,
		{ "Section TS pkt flag", "simulcrypt.section_tspkt_flag", FT_UINT8, BASE_HEX, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_ecm_channel_id,
		{ "ECM channel ID", "simulcrypt.ecm_channel_id", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_delay_start,	
		{ "Delay start", "simulcrypt.delay_start", FT_INT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_delay_stop,	
		{ "Delay stop", "simulcrypt.delay_stop", FT_INT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_ac_delay_start,	
		{ "AC delay start", "simulcrypt.ac_delay_start", FT_INT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_ac_delay_stop,	
		{ "AC delay stop", "simulcrypt.ac_delay_stop", FT_INT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_transition_delay_start,	
		{ "Transition delay start", "simulcrypt.transition_delay_start", FT_INT16, BASE_DEC, NULL, 0x0,
		 "Transition delay start", HFILL }},

		{ &hf_simulcrypt_transition_delay_stop,	
		{ "Transition delay stop", "simulcrypt.transition_delay_stop", FT_INT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},	

		{ &hf_simulcrypt_ecm_rep_period,
		{ "ECM repetition period", "simulcrypt.ecm_rep_period", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_max_streams,
		{ "Max streams", "simulcrypt.max_streams", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_min_cp_duration,
		{ "Min CP duration", "simulcrypt.min_cp_duration", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_lead_cw,
		{ "Lead CW", "simulcrypt.lead_cw", FT_UINT8, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_cw_per_msg,
		{ "CW per msg", "simulcrypt.cw_per_msg", FT_UINT8, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_max_comp_time,
		{ "Max comp time", "simulcrypt.max_comp_time", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_access_criteria,
		{ "Access criteria", "simulcrypt.access_criteria", FT_BYTES, BASE_HEX, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_ecm_stream_id,
		{ "ECM stream ID", "simulcrypt.ecm_stream_id", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},	

		{ &hf_simulcrypt_nominal_cp_duration,
		{ "Nominal CP duration", "simulcrypt.nominal_cp_duration", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_access_criteria_transfer_mode,
		{ "AC transfer mode", "simulcrypt.access_criteria_transfer_mode", FT_BOOLEAN, 8, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_cp_number,
		{ "CP number", "simulcrypt.cp_number", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_cp_duration,
		{ "CP duration", "simulcrypt.cp_duration", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_cp_cw_combination,
		{ "CP CW combination", "simulcrypt.cp_cw_combination", FT_BYTES, BASE_HEX, NULL, 0x0,
		 NULL, HFILL }},
		 
		{ &hf_simulcrypt_ecm_datagram,
		{ "ECM datagram", "simulcrypt.ecm_datagram", FT_BYTES, BASE_HEX, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_cw_encryption,
		{ "CW encryption", "simulcrypt.cw_encryption", FT_NONE, BASE_NONE, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_ecm_id,
		{ "ECM ID", "simulcrypt.ecm_id", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_error_status,
		{ "Error status", "simulcrypt.error_status", FT_UINT16, BASE_DEC, NULL, 0x0,
		 NULL, HFILL }},

		{ &hf_simulcrypt_error_information,
		{ "Error information", "simulcrypt.error_information", FT_NONE, BASE_NONE, NULL, 0x0,
		 NULL, HFILL }}		 
	};
	
	static gint *ett[] =
	{
		&ett_simulcrypt,
		&ett_simulcrypt_header,
		&ett_simulcrypt_message,
		&ett_simulcrypt_parameter,
		&ett_simulcrypt_super_cas_id,
		&ett_simulcrypt_ecm_datagram
	};
	
	module_t *simulcrypt_module;
	
	/* execute protocol initialization only once */
	proto_simulcrypt = proto_register_protocol ("SIMULCRYPT Protocol", "SIMULCRYPT", "simulcrypt");

	proto_register_field_array (proto_simulcrypt, hf, array_length (hf));
	proto_register_subtree_array (ett, array_length (ett));
	
	register_init_routine(simulcrypt_init);

	/* Register our configuration options for Simulcrypt, particularly our port. */
	/* This registers our preferences; function proto_reg_handoff_simulcrypt is  */
	/*  called when preferences are applied.                                     */
	simulcrypt_module = prefs_register_protocol(proto_simulcrypt, proto_reg_handoff_simulcrypt);
	
	prefs_register_uint_preference(simulcrypt_module, "tcp.port", "Simulcrypt TCP Port",
				 "Set the port for Simulcrypt messages ('0' means no port is assigned)",
				 10, &global_simulcrypt_tcp_port);
									
	prefs_register_uint_preference(simulcrypt_module, "ca_system_id_mikey","MIKEY ECM CA_system_ID 0x (hex value)",
					"Set the CA_system_ID used to decode ECM datagram as MIKEY", 16, &ca_system_id_mikey);		
}

/* this is run every time preferences are changed and also during Wireshark initialization */
void proto_reg_handoff_simulcrypt(void)
{
	static gboolean initialized=FALSE;
	static dissector_handle_t simulcrypt_handle;
	static guint tcp_port;
	guint  i;

	if (!initialized) {
		simulcrypt_handle = create_dissector_handle( dissect_simulcrypt, proto_simulcrypt);
		for(i=0;i<ECM_INTERPRETATION_SIZE;i++)
		{
			tab_ecm_inter[i].protocol_handle = find_dissector(tab_ecm_inter[i].protocol_name);
		}
		initialized = TRUE;
	}
	else {
		if (tcp_port != 0) {
			dissector_delete("tcp.port", tcp_port, simulcrypt_handle);
		}
	}	
	if (global_simulcrypt_tcp_port != 0) {
		dissector_add("tcp.port", global_simulcrypt_tcp_port, simulcrypt_handle);
	}
	tcp_port = global_simulcrypt_tcp_port;

	/* update tab_ecm_inter table (always do this) */
	tab_ecm_inter[ECM_MIKEY_INDEX].ca_system_id=ca_system_id_mikey;
}