aboutsummaryrefslogtreecommitdiffstats
path: root/asn1/atn-ulcs/packet-atn-ulcs-template.c
blob: 0e43332f4c5e0ee7ce15c26bb850619f95977cee (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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: t
 * vi: set shiftwidth=2 tabstop=2 noexpandtab:
 * :indentSize=2:tabSize=2:noTabs=false:
 */

/* packet-atn-ulcs.c
 * By Mathias Guettler <guettler@web.de>
 * Copyright 2013
 *
 * $Id$
 *
 * Routines for ATN upper layer
 * protocol packet disassembly

 * ATN upper layers are embedded within OSI Layer 4 (COTP).
 *
 * ATN upper layers contain:
 * Session Layer (NUL protocol option)
 * Presentation Layer (NUL protocol option)
 * ATN upper Layer/Application (ACSE PDU or PDV-list PDU)

 * ATN applications protocols (i.e. CM or CPDLC) are contained within
 * ACSE user-information or PDV presentation data.

 * details see:
 * http://en.wikipedia.org/wiki/CPDLC
 * http://members.optusnet.com.au/~cjr/introduction.htm

 * standards:
 * http://legacy.icao.int/anb/panels/acp/repository.cfm

 * note:
 * We are dealing with ATN/ULCS aka ICAO Doc 9705 Ed2 here
 * (don't think there is an ULCS equivalent for "FANS-1/A ").

 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 developper comments:
why not using existing ses, pres and acse dissectors ?
		ATN upper layers are derived from OSI standards for session,
		presentation and application but the encoding differs
		(it's PER instead of BER encoding to save bandwith).
		Session and presentation use the "null" encoding option,
		meaning that they are only present at connection establishment
		and ommitted otherwise.
		Instead of adapting existing dissectors it seemed simpler and cleaner
		to implement everything the new atn-ulcs dissector.
		
why using conversations ?
		PER encoded user data is ambigous; the same encoding may apply to a CM or
		CPDLC PDU. The workaround is to decode on a transport connection basis.
		I use my own version of conversations to identify
		the transport connection the PDU belongs to for the standard functions
		from "conversation.h" didn't work out.

what is the use of AARQ/AARE data ?
		Converstions should be maintained on the COTP layer in a standard way
		for there are usually more packets available than in the layers above.
		In the worst case my dissector is called from a DT packet which
		has destination references but no source reference.
		I have to guess the reference used the other way round
		(curently I am using ACSE PDU'S used during OSI connection establishment for that).
		The idea is that each ACSE AARQ is answered by ACSE AARE and having this sequence
		I have all the source/destination references for this transport connection.
		I use AARQ/AARE data to store the source/destination reference of AARQ as well
		as the optional ae-qualifier which tells me the application and
		the dissector I have to use.
		This approach donesn't work well when there are interleaving AARQ/AARE sequences for
		the same aircraft.

which ATN standard is supported ?
		The dissector has been tested with ICAO doc9705 Edition2 compliant traffic.
		No ATN Secutity is supported.
		note:
		The ATN upper layers are derived from OSI standards (ICAO DOC 9705)
		while ATN/IPS (ICAO DOC 9896) which is entirely based on IPV6.
		
*/

/*
 known defects/deficiencies:

-	user-information within AARE is sometines not decoded due to an unset flag
		(the field is optional). As far as I can tell asn2wrs is right here,
		but on the other hand I know that in all of this cases user-information
		is present and is processed by the ATN end system.
		Maybe a true ATN expert may help me out here.

	- The conversation handling is based on src/dst addresses as well as
		source or destination references depending on the TP4 packet type.
		This means that after some time these references get reused for
		new conversations. This almost certain happens for traces longer
		than one day rendering this dissector unsuitable for captures exceeding
		this one day. 
		
*/

#include "config.h"

#include <glib.h>
#include <epan/packet.h>
#include <epan/dissectors/packet-ber.h>
#include <epan/dissectors/packet-per.h>
#include <epan/wmem/wmem.h>
#include <epan/address.h>
#include <epan/conversation.h>

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "packet-atn-ulcs.h"

#define ATN_ACSE_PROTO "ICAO Doc9705 ULCS ACSE (ISO 8649/8650-1:1996)"
#define ATN_ULCS_PROTO "ICAO Doc9705 ULCS"

static heur_dissector_list_t atn_ulcs_heur_subdissector_list;

/* presentation subdissectors i.e. CM, CPDLC */
static dissector_handle_t atn_cm_handle = NULL;
static dissector_handle_t atn_cpdlc_handle = NULL;

static int proto_atn_ulcs          = -1;
static guint32 ulcs_context_value = 0;
static const char *object_identifier_id;

static wmem_tree_t *aarq_data_tree = NULL;
static wmem_tree_t *atn_conversation_tree = NULL;


static proto_tree *root_tree = NULL;

/* forward declarations for functions generated from asn1 */
static int dissect_atn_ulcs_T_externalt_encoding_single_asn1_type(
		tvbuff_t *tvb _U_,
		int offset _U_,
		asn1_ctx_t *actx _U_,
		proto_tree *tree _U_,
		int hf_index
		_U_);

static int dissect_atn_ulcs_T_externalt_encoding_octet_aligned(
		tvbuff_t *tvb _U_,
		int offset _U_,
		asn1_ctx_t *actx _U_,
		proto_tree *tree _U_,
		int hf_index _U_);

static int dissect_atn_ulcs_T_externalt_encoding_arbitrary(
		tvbuff_t *tvb _U_,
		int offset _U_,
		asn1_ctx_t *actx _U_,
		proto_tree *tree _U_,
		int hf_index _U_);
		
static void dissect_ACSE_apdu_PDU(
		tvbuff_t *tvb _U_,
		packet_info *pinfo _U_,
		proto_tree *tree _U_);
		
guint32 dissect_per_object_descriptor_t(
		tvbuff_t *tvb,
		guint32 offset,
		asn1_ctx_t *actx,
		proto_tree *tree,
		int hf_index,
		tvbuff_t **value_tvb);

static gint	dissect_atn_ulcs(
		tvbuff_t *tvb,
		packet_info *pinfo,
		proto_tree	*tree,
		void *data _U_);

#include "packet-atn-ulcs-hf.c"

#include "packet-atn-ulcs-ett.c"
static gint ett_atn_ulcs = -1;
static gint ett_atn_acse = -1;

#include "packet-atn-ulcs-fn.c"

#if 0
/* re-implementing external data: packet-per.c */
static const value_string per_External_encoding_vals[] = {
{   0, "single-ASN1-type" },
{   1, "octet-aligned" },
{   2, "arbitrary" },
{ 0, NULL }
};
#endif

/* re-implementing external data: packet-per.c */
static const per_choice_t External_encoding_choice[] =
{
		{		0,
				&hf_atn_ulcs_externalt_encoding_single_asn1_type,
				ASN1_NO_EXTENSIONS,
				dissect_atn_ulcs_T_externalt_encoding_single_asn1_type
		},
		{		1,
				&hf_atn_ulcs_externalt_encoding_octet_aligned,
				ASN1_NO_EXTENSIONS,
				dissect_atn_ulcs_T_externalt_encoding_octet_aligned
		},
		{		2,
				&hf_atn_ulcs_externalt_encoding_arbitrary,
				ASN1_NO_EXTENSIONS,
				dissect_atn_ulcs_T_externalt_encoding_arbitrary
		},
		{		0,
				NULL,
				0,
				NULL
		}
};

/* ATN Session layer */
#define SES_PDU_TYPE_MASK			0xf8
#define SES_PARAM_IND_MASK		0x04 
#define SES_PARAM_B2_MASK			0x02
#define SES_PARAM_B1_MASK			0x01

static int hf_atn_ses_type = -1;
static int hf_atn_ses_param_ind = -1;
static int hf_atn_ses_param_b1 = -1;
static int hf_atn_ses_param_b2 = -1;

static gint ett_atn_ses = -1;

#define ATN_SES_PROTO "ICAO Doc9705 ULCS Session (ISO 8326/8327-1:1994)"

const value_string atn_ses_param_ind[] =
{
		{0,	"No Parameter Indication "},
		{1,	"Parameter Indication "},
		{0,	NULL }
};

const value_string srf_b2[] =
{
		{0,	"Transport Connection is kept"},
		{1,	"Transport Connection is released" },
		{0,	NULL }
};

const value_string srf_b1[] =
{
		{0,	"Transport Connection is transient"},
		{1,	"Transport Connection is persistent"},
		{0,	NULL }
};

#define SES_ATN_SCN				0xe8
#define SES_ATN_SCNC			0xf8
#define SES_ATN_SAC				0xf0
#define SES_ATN_SACC			0xd8
#define SES_ATN_SRF				0xe0
#define SES_ATN_SRFC			0xa0

const value_string atn_ses_type[] =
{
		{ 0x1d,	"Short Connect (SCN) SPDU" },
		{ 0x1f,	"Short Connect Accept (SAC) SPDU" },
		{ 0x1e,	"Short Connect Accept Continue (SACC) SPDU" },
		{ 0x1c,	"Short Refuse (SRF) SPDU" },
		{ 0x14,	"Short Refuse Continue (SRFC) SPDU" },
		{0,	NULL }
};

/* ATN Presentation layer */
#define ATN_PRES_PROTO "ICAO Doc9705 ULCS Presentation (ISO 8822/8823-1:1994)"

static int hf_atn_pres_err	 = -1;
static gint ett_atn_pres		= -1;

#define ATN_SES_PRES_MASK 0xf803
#define PRES_CPR_ER_MASK		0x70

/* type determined by SPDU and PPDU */
const value_string atn_pres_vals[] =
{
		{ 0xe802, "Short Presentation Connect PPDU (CP) " },
		{ 0xf802, "Short Presentation Connect PPDU (CP) " },
		{ 0xf002, "Short Presentation Connect Accept PPDU (CPA)" },
		{ 0xd802, "Short Presentation Connect Accept PPDU (CPA)" },
		{ 0xe002, "Short Presentation Connect Reject PPDU (CPR)" },
		{ 0xa002, "Short Presentation Connect Reject PPDU (CPR)" },
		{0,					NULL }
};

/* Short Presentation Connect Reject PPDU's 0yyy 00zz */
const value_string atn_pres_err[] =
{
		{ 0x00, "Presentation-user" },
		{ 0x01, "Reason not specified (transient)"},
		{ 0x02,	"Temporary congestion (transient)"},
		{ 0x03,	"Local limit exceeded (transient)"},
		{ 0x04, "Called presentation-address unknown (permanent)"},
		{ 0x05,	"Protocol version not supported (permanent)"},
		{ 0x06,	"Default context not supported (permanent)"},
		{ 0x07,	"User data not readable (permanent)"},
		{ 0,					NULL }
};

/* re-implementing external data: packet-per.c */
static int	atn_ulcs_Externalt_encoding(
		tvbuff_t *tvb _U_,
		int offset _U_,
		asn1_ctx_t *actx _U_,
		proto_tree *tree _U_,
		int hf_index _U_)
{
		offset = dissect_per_choice(
				tvb,
				offset,
				actx,
				tree,
				hf_index,
				ett_atn_ulcs_EXTERNALt,
				External_encoding_choice,
				&actx->external.encoding);

		return offset;
}

/* re-implementing external data: packet-per.c */
guint32	atn_per_external_type(
		tvbuff_t *tvb _U_,
		guint32 offset,
		asn1_ctx_t *actx,
		proto_tree *tree _U_,
		int hf_index _U_,
		per_type_fn type_cb)
{
		memset(&actx->external, '\0', sizeof(actx->external));
		actx->external.hf_index = -1;
		actx->external.encoding = -1;

		actx->external.u.per.type_cb = type_cb;
		offset = atn_ulcs_Externalt_encoding(
				tvb,
				offset,
				actx,
				tree,
				hf_index);

		memset(
				&actx->external,
				'\0',
				sizeof(actx->external));
				
		actx->external.hf_index = -1;
		actx->external.encoding = -1;

		return offset;
}

/* determine 24-bit aircraft address(ARS) */
/* from 20-byte ATN NSAP. */
guint32 get_aircraft_24_bit_address_from_nsap(
		packet_info *pinfo)
{
		guint8* addr = NULL;
		guint32 ars =0;
		guint32 adr_prefix =0;
	
		/* check NSAP address type*/
		if( (pinfo->src.type != AT_OSI) ||
				(pinfo->dst.type != AT_OSI)) {
				return ars; }

		/* 20 octets address length required */
		/* for ATN */
		if( (pinfo->src.len != 20) ||
				(pinfo->dst.len != 20)) {
				return ars; }

		/* first try source address */
		/* if the src address originates */
		/* from an aircraft it's downlink */
		
		/* convert addr into 32-bit integer */
		addr = (guint8*) pinfo->src.data;
		adr_prefix =
				((addr[0]<<24) |
				(addr[1]<<16) |
				(addr[2]<<8) |
				addr[3] );

		/* according to ICAO doc9507 Ed2 SV5  */
		/* clause 5.4.3.8.1.5 and  5.4.3.8.1.3 */
		/* mobile addresses contain "c1" of "41" */
		/* in the VER subfield of the NSAP */
		if((adr_prefix == 0x470027c1) ||
				(adr_prefix == 0x47002741)) {
			/* ICAO doc9507 Ed2 SV5 5.4.3.8.4.4 */
			/* states that the ARS subfield containes */
			/* the  24-bitaddress of the aircraft */
				ars = ((addr[8])<<16) |
						((addr[9])<<8) |
						(addr[10]);
		}

		/* try destination address */
		/* if the src address originates */
		/* from an aircraft it's downlink */

		/* convert addr into 32-bit integer */
		addr = (guint8*) pinfo->dst.data;
		adr_prefix = ((addr[0]<<24) |
				(addr[1]<<16) |
				(addr[2]<<8) |
				addr[3] );
				
		/* according to ICAO doc9507 Ed2 SV5  */
		/* clause 5.4.3.8.1.5 and  5.4.3.8.1.3 */
		/* mobile addresses contain "c1" of "41" */
		/* in the VER subfield of the NSAP */
		if((adr_prefix == 0x470027c1) ||
				(adr_prefix == 0x47002741)) {
			/* ICAO doc9507 Ed2 SV5 5.4.3.8.4.4 */
			/* states that the ARS subfield containes */
			/* the  24-bitaddress of the aircraft */
			ars = ((addr[8])<<16) |
						((addr[9])<<8) |
						(addr[10]);
		}
		return ars;
}

/* determine whether a PDU is uplink or downlink */
/* by checking for known aircraft  address prefices*/
int check_heur_msg_type(packet_info *pinfo  _U_)
{
		int t = no_msg;
		guint8* addr = NULL;
		guint32 adr_prefix =0;

		/* check NSAP address type*/
		if( (pinfo->src.type != AT_OSI) || (pinfo->dst.type != AT_OSI)) {
				return t; }

		/* check NSAP address length; 20 octets address length required */
		if( (pinfo->src.len != 20) || (pinfo->dst.len != 20)) {
				return t; }

		addr = (guint8*) pinfo->src.data;

		/* convert address to 32-bit integer  */
		adr_prefix = ((addr[0]<<24) | (addr[1]<<16) | (addr[2]<<8) | addr[3] );

		/* According to the published ATN NSAP adddressing scheme */
		/* in ICAO doc9705 Ed2 SV5 5.4.3.8.1.3 and 5.4.3.8.1.5  */
		/* the "VER" field shall be 0x41 ("all Mobile AINSC") or */
		/* 0xc1 ("all Mobile ATSC") for mobile stations (aka aircraft).*/
		if((adr_prefix == 0x470027c1) || (adr_prefix == 0x47002741)) {
				t = dm; /* source is an aircraft: it's a downlink PDU */
		}

		addr = (guint8*) pinfo->dst.data;

		/* convert address to 32-bit integer  */
		adr_prefix = ((addr[0]<<24) | (addr[1]<<16) | (addr[2]<<8) | addr[3] );

		/* According to the published ATN NSAP adddressing scheme */
		/* in ICAO doc9705 Ed2 SV5 5.4.3.8.1.3 and 5.4.3.8.1.5  */
		/* the "VER" field shall be 0x41 ("all Mobile AINSC") or */
		/* 0xc1 ("all Mobile ATSC") for mobile stations (aka aircraft).*/
		if((adr_prefix == 0x470027c1) || (adr_prefix == 0x47002741)) {
				t = um; /* destination is aircraft: uplink PDU */
		}

		return t;
}

/* conversation may be used by other dissectors  */
wmem_tree_t *get_atn_conversation_tree(void){
		return atn_conversation_tree;
}


/* find a atn conversation tree node by an endpoint  */
/* an endpoint is identified by atn src and dst addresses */
/* and srcref or dstref (depends on the transport packet type) */
/* IMHO it's a hack - conversations should be maintained */
/* at transport layer (cotp) but this isn't working yet. */
atn_conversation_t * find_atn_conversation(
		address *address1,
		guint16 clnp_ref1,
		address *address2 )
{
		atn_conversation_t *cv = NULL;
		guint32	key = 0;
		guint32	tmp = 0;

		ADD_ADDRESS_TO_HASH( tmp, address1);
		key = (tmp << 16) | clnp_ref1 ;

		ADD_ADDRESS_TO_HASH( tmp, address2);
		key = (tmp << 24) | key ;

		/* search for atn conversation */
		cv = (atn_conversation_t *)
				wmem_tree_lookup32(get_atn_conversation_tree(),key);

		return cv;
}

/* create a atn conversation tree node  */
/* conversation data is to be allocated externally */
/* a conversation may be referenced from both endpoints */
atn_conversation_t * create_atn_conversation(
		address *address1,
		guint16 clnp_ref1,
		address *address2,
		atn_conversation_t *conversation)
{
		atn_conversation_t *cv = NULL;
		guint32	key = 0;
		guint32	tmp = 0;

		ADD_ADDRESS_TO_HASH( tmp, address1);
		key = (tmp << 16) | clnp_ref1 ;

		ADD_ADDRESS_TO_HASH( tmp, address2);
		key = (tmp << 24) | key ;

		/* search for aircraft entry */
		cv = (atn_conversation_t *)
		wmem_tree_lookup32(
				get_atn_conversation_tree(),
				key);

		/* tree node  already present  */
		if(cv) {
			return NULL; }

		/* insert conversation data in tree*/
		wmem_tree_insert32(
				get_atn_conversation_tree(),
				key,
				(void*)conversation);

		return conversation;
}

static int
dissect_atn_ulcs(
		tvbuff_t *tvb,
		packet_info *pinfo,
		proto_tree *tree,
		void *data _U_)
{
		int offset = 0;
		proto_item *ti = NULL;
		proto_tree *atn_ulcs_tree = NULL;
		guint8 value_pres = 0;
		guint8 value_ses = 0;
		guint16 value_ses_pres = 0; 

		root_tree = tree;

		/* data pointer */
		/* decode as PDV-list */
		if ( (int)(intptr_t)  data == FALSE )
		{
				ti = proto_tree_add_item(
						tree,
						proto_atn_ulcs,
						tvb,
						0,
						0 ,
						FALSE);
						
				atn_ulcs_tree = proto_item_add_subtree(
						ti,
						ett_atn_ulcs);
		
				dissect_Fully_encoded_data_PDU(
						tvb,
						pinfo,
						atn_ulcs_tree);
						
				return offset +
					tvb_reported_length_remaining(tvb, offset ) ;
		}
		
		/* decode as SPDU, PPDU and ACSE PDU */
		if ( (int)(intptr_t)  data == TRUE )
		{
				/* get session and presentation PDU's */
				value_ses_pres = tvb_get_ntohs(tvb, offset);
		
				/* SPDU: dissect session layer */
				ti = proto_tree_add_text(
						tree,
						tvb,
						offset,
						0,
						ATN_SES_PROTO );

				atn_ulcs_tree = proto_item_add_subtree(
						ti,
						ett_atn_ses);

				/* get SPDU (1 octet) */
				value_ses = tvb_get_guint8(tvb, offset);

				/* SPDU type/identifier  */
				proto_tree_add_item(atn_ulcs_tree,
						hf_atn_ses_type,
						tvb,
						offset,
						1,
						ENC_BIG_ENDIAN );

				/* SPDU parameters may be present in Short Refuse */
				/* or Short Refuse Continue SPDU's */
				switch(value_ses & SES_PDU_TYPE_MASK){
						case SES_ATN_SRF:
						case SES_ATN_SRFC:

								/* SPDU parameter presence */
								proto_tree_add_item(atn_ulcs_tree,
										hf_atn_ses_param_ind,
										tvb,
										offset,
										1,
										ENC_BIG_ENDIAN );

								/* parameter B2 */
								proto_tree_add_item(atn_ulcs_tree,
										hf_atn_ses_param_b2,
										tvb,
										offset,
										1,
										ENC_BIG_ENDIAN );

								/* parameter B1 */
								proto_tree_add_item(atn_ulcs_tree,
										hf_atn_ses_param_b1,
										tvb,
										offset,
										1,
										ENC_BIG_ENDIAN );

							break;
						default:
							break;
				}
				offset++;
		
				/* PPDU: dissect presentation layer */
				ti = proto_tree_add_text(
						tree,
						tvb,
						offset,
						0,
						ATN_PRES_PROTO );
						
				atn_ulcs_tree = proto_item_add_subtree(ti, ett_atn_pres);

				value_pres = tvb_get_guint8(tvb, offset);

				/* need session context to identify PPDU type */
				/* note: */
				/* it is *unfeasible* to use proto_tree_add_item here: */
				/* presentation type is always the same constant but its type */
				/* is implicitly determined by preceding session context */
				proto_tree_add_text(atn_ulcs_tree,
						tvb,
						offset,
						1,
						"%s (0x%02x)",
						val_to_str( value_ses_pres & ATN_SES_PRES_MASK , atn_pres_vals, "?"),
						value_pres);
			
				/* PPDU errorcode in case of SRF/CPR */
				switch(value_ses & SES_PDU_TYPE_MASK){
						case SES_ATN_SRF:
						case SES_ATN_SRFC:
								proto_tree_add_item(
										atn_ulcs_tree,
										hf_atn_pres_err,
										tvb,
										offset,
										1,
										ENC_BIG_ENDIAN );
								break;
						default:
								break;
				}

				offset++;

				/* ACSE PDU: dissect application layer */
				ti = proto_tree_add_text(
						tree,
						tvb,
						offset,
						0,
						ATN_ACSE_PROTO );
				
				atn_ulcs_tree = proto_item_add_subtree(
						ti,
						ett_atn_acse);

				dissect_ACSE_apdu_PDU(
						tvb_new_subset_remaining(tvb, offset),
						pinfo,
						atn_ulcs_tree);

				return offset +
						tvb_reported_length_remaining(tvb, offset );
		}
		return offset;
}

static gboolean dissect_atn_ulcs_heur(
		tvbuff_t *tvb,
		packet_info *pinfo,
		proto_tree *tree,
		void *data _U_)
{	
		/* do we have enough data*/
		/* at least session + presentation data or pdv-list */
		if (tvb_reported_length_remaining(tvb, 0) < 2){
				return FALSE; }
	
		/* check for session/presentation/ACSE PDU's  */
		/* SPDU and PPDU are one octet each */
		switch( tvb_get_ntohs(tvb, 0) & 0xf8ff ){
				case 0xe802: /* SCN + CP*/
				case 0xf802: /* SCNC + CP */
				case 0xf002: /* SAC + CPA */
				case 0xd802: /* SACC + CPA */
				case 0xe002: /* SRF + CPR + R0 */
				case 0xe012: /* SRF + CPR + R1 */
				case 0xe022: /* SRF + CPR + R2 */
				case 0xe032: /* SRF + CPR + R3 */
				case 0xe042: /* SRF + CPR + R4 */
				case 0xe052: /* SRF + CPR + R5 */
				case 0xe062: /* SRF + CPR + R6 */
				case 0xe072: /* SRF + CPR + R7 */
				case 0xa002: /* SRFC + CPR + R0*/
				case 0xa012: /* SRFC + CPR + R1*/
				case 0xa022: /* SRFC + CPR + R2*/
				case 0xa032: /* SRFC + CPR + R3*/
				case 0xa042: /* SRFC + CPR + R4*/
				case 0xa052: /* SRFC + CPR + R5*/
				case 0xa062: /* SRFC + CPR + R6*/
				case 0xa072: /* SRFC + CPR + R7*/
						/* indicate to dissector routine */
						/* that a least SPDU, PPDU and */
						/* ACSE PDU is present */
						dissect_atn_ulcs(
								tvb,
								pinfo,
								tree,
								(void*) TRUE);
						return TRUE;
				default:	/* no SPDU */
						break;
		}

		/* try to detect "Fully-encoded-data" heuristically */
		/* the constants listed match the ASN.1 PER encoding */
		/* of PDV-List */
		switch(  tvb_get_ntohs(tvb, 0) & 0xfff0 ){
				case 0x0020: /* acse-apdu */
				case 0x00a0: /* user-ase-apdu */
				/* indicate to dissector routine */
				/* that a PDV-list PDU is present */
				/*  */
				/* PDV-list PDU may contain */
				/* application protocol data (CM, CPDLC) */
				/* or an ACSE PDU */
						dissect_atn_ulcs(tvb, pinfo, tree, (void*) FALSE);
						return TRUE;
						break;
				default:	/* no or unsupported PDU */
						break;
		}
		return FALSE;
}

void proto_register_atn_ulcs (void)
{
		static hf_register_info hf_atn_ulcs[] = {
				#include "packet-atn-ulcs-hfarr.c"
				{&hf_atn_ses_type,
				{ "SPDU Type",
					"atn-ulcs.ses.type",
					FT_UINT8,
					BASE_HEX,
					VALS(atn_ses_type),
					0xf8,
					"Indicates presence of session parameters",
					HFILL}},
				{&hf_atn_ses_param_ind,
				{ "SPDU Parameter Indication",
					"atn-ulcs.ses.parameter-indication",
					FT_UINT8,
					BASE_HEX,
					VALS(atn_ses_param_ind),
					SES_PARAM_IND_MASK,
					"Indicates presence of session parameters",
					HFILL}},
			{&hf_atn_ses_param_b1,
				{ "SRF Parameter B1",
					"atn-ulcs.ses.srf-b1",
					FT_UINT8,
					BASE_HEX,
					VALS(srf_b1),
					0x01,
					"Determines if transport connection reject is \
					transient or persistent",
					HFILL}},
			{&hf_atn_ses_param_b2,
				{ "SRF Parameter B2",
					"atn-ulcs.ses.srf-b2",
					FT_UINT8,
					BASE_HEX,
					VALS(srf_b2),
					0x02,
					"Determines if transport connection is \
					retained or released",
					HFILL}},
			{ &hf_atn_pres_err,
				{ "Error Code", "atn-ulcs.pres.cpr-error",
					FT_UINT8,
					BASE_HEX,
					VALS(atn_pres_err),
					PRES_CPR_ER_MASK,
					NULL,
					HFILL}},
		};

		static gint *ett[] = {
				#include "packet-atn-ulcs-ettarr.c"
				&ett_atn_ses,
				&ett_atn_pres,
				&ett_atn_acse,
				&ett_atn_ulcs
    };

		proto_atn_ulcs = proto_register_protocol (
				ATN_ULCS_PROTO ,
				"ATN-ULCS",
				"atn-ulcs");

		proto_register_field_array (
				proto_atn_ulcs,
				hf_atn_ulcs,
				array_length(hf_atn_ulcs));

		proto_register_subtree_array (
				ett,
				array_length (ett));

		new_register_dissector(
				"atn-ulcs",
				dissect_atn_ulcs,
				proto_atn_ulcs);

		atn_cm_handle = find_dissector("atn-cm");
		atn_cpdlc_handle = find_dissector("atn-cpdlc");

		/* initiate sub dissector list */
		register_heur_dissector_list(
				"atn-ulcs",
				&atn_ulcs_heur_subdissector_list);

		/* init aare/aare data */
		aarq_data_tree = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());

		atn_conversation_tree = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
}

void proto_reg_handoff_atn_ulcs(void)
{
		/* add session dissector to cotp dissector list dissector list*/
		heur_dissector_add(
				"cotp",
				dissect_atn_ulcs_heur,
				proto_atn_ulcs);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 2
 * tab-width: 2
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=2 tabstop=2 noexpandtab:
 * :indentSize=2:tabSize=2:noTabs=false:
 */