summaryrefslogtreecommitdiffstats
path: root/src/csl_dha.c
blob: 34239e1bfdef29acdcf0dc9d9467be88c7166fc0 (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
/* ITU-T Q.77x TCAP / CSL - Component Sub-Layer */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 * (C) 2010 by On-Waves
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <netinet/in.h>

#include <osmocore/talloc.h>
#include <osmocore/msgb.h>

#include <asn1c/OBJECT_IDENTIFIER.h>

#include <osmocom/tcap/TCMessage.h>
#include <osmocom/tcap/ExternalPDU.h>
#include <osmocom/tcap/DialoguePDU.h>
#include <osmocom/tcap/UniDialoguePDU.h>
#include <osmocom/tcap/DialoguePortion.h>

#include "tcap.h"

static uint8_t _dial_version1 = 0x80;

static BIT_STRING_t dial_version1 = {
	.buf = &_dial_version1,
	.size = 1,
	.bits_unused = 7,
};

/* Extract the Application Context Name and User Info from a DialoguePDU */
static int extract_appctx_uinfo(OBJECT_IDENTIFIER_t **app_ctx_name, struct user_information **user_info,
				struct DialoguePDU *dial_pdu)
{
	switch (dial_pdu->present) {
	case DialoguePDU_PR_dialogueRequest:
		*app_ctx_name = &dial_pdu->choice.dialogueRequest.application_context_name;
		*user_info = dial_pdu->choice.dialogueRequest.user_information;
		break;
	case DialoguePDU_PR_dialogueResponse:
		*app_ctx_name = &dial_pdu->choice.dialogueResponse.application_context_name;
		*user_info = dial_pdu->choice.dialogueResponse.user_information;
		break;
	case DialoguePDU_PR_dialogueAbort:
		*app_ctx_name = NULL;
		*user_info = dial_pdu->choice.dialogueAbort.user_information;
		break;
	default:
		*app_ctx_name = NULL;
		*user_info = NULL;
		return -EINVAL;
	}

	return 0;
}


/* TC-UNI.req from TCU */
int tcap_csl_tc_uni_req(struct tcap_dialogue *td, OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info)
{
	struct TCMessage *tcm;
	ExternalPDU_t *ext;
	UniDialoguePDU_t *dial;
	ANY_t *any;
	int rc;

	tcm = talloc_zero(td, struct TCMessage);
	tcm->present = TCMessage_PR_unidirectional;

	if (app_ctx) {
		AUDT_apdu_t *audt;
		/* build AUDT apdu */
		dial = talloc_zero(td, UniDialoguePDU_t);
		dial->present = UniDialoguePDU_PR_unidialoguePDU;
		audt = &dial->choice.unidialoguePDU;
		if (user_info)
			audt->user_information = user_info;
		memcpy(&audt->application_context_name, app_ctx, sizeof(audt->application_context_name));
		audt->protocol_version = &dial_version1;

		fprintf(stdout, "\nTC-UNI.req Dialogue portion:\n");
		xer_fprint(stdout, &asn_DEF_UniDialoguePDU, dial);

		ext = talloc_zero(dial, ExternalPDU_t);
		ANY_fromType((ANY_t *) &ext->dialog, &asn_DEF_UniDialoguePDU, dial);
		any = ANY_new_fromType(&asn_DEF_ExternalPDU, ext);
		tcm->choice.unidirectional.dialoguePortion = (OCTET_STRING_t *) any;
	}
	/* Request components to CHA */
	/* Process components */
	/* Assemble TSL user data */
	memcpy(&tcm->choice.unidirectional.components, td->pend_comp, sizeof(*td->pend_comp));
	talloc_free(td->pend_comp);
	td->pend_comp = talloc_zero(td, struct ComponentPortion);

	/* TR-UNI-REQ to TSL */
	rc = tcap_tco_tr_uni_req(&td->trans, tcm);
	/* Dialogue terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	asn_DEF_TCMessage.free_struct(&asn_DEF_TCMessage, tcm, 0);

	return rc;
}

/* TC-BEGIN.req from TCU */
int tcap_csl_tc_begin_req(struct tcap_dialogue *td, OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info)
{
	struct TCMessage *tcm;
	ExternalPDU_t *ext;
	DialoguePDU_t *dial;
	struct tcap_invocation *ti;
	uint32_t trans_id;
	int rc;

	tcm = talloc_zero(td, struct TCMessage);
	tcm->present = TCMessage_PR_begin;

	if (app_ctx) {
		AARQ_apdu_t *aarq;
		ANY_t *any;

		dial = talloc_zero(tcm, DialoguePDU_t);
		dial->present = DialoguePDU_PR_dialogueRequest;

		aarq = &dial->choice.dialogueRequest;
		if (user_info)
			aarq->user_information = user_info;
		/* Set Application context mode */
		td->app_ctx_mode = 1;
		memcpy(&aarq->application_context_name, app_ctx, sizeof(aarq->application_context_name));
		/* Set protocol_version = 1 */
		aarq->protocol_version = &dial_version1;
		/* Build AARQ apdu */
		fprintf(stdout, "\nTC-BEGIN.req Dialogue portion:\n");
		xer_fprint(stdout, &asn_DEF_DialoguePDU, dial);

		ext = talloc_zero(dial, ExternalPDU_t);
		rc = ANY_fromType((ANY_t *) &ext->dialog, &asn_DEF_DialoguePDU, dial);
		if (rc < 0)
			fprintf(stderr, "Error encoding DialoguePDU portion\n");
		any = ANY_new_fromType(&asn_DEF_ExternalPDU, ext);
		tcm->choice.begin.dialoguePortion = (OCTET_STRING_t *) any;
	}
	/* Request components to CHA */
	/* Process components */
	/* Assemble TSL user data */
	tcm->choice.begin.components = td->pend_comp;
	td->pend_comp = talloc_zero(td, struct ComponentPortion);

	/* Assign local transaction ID */
	trans_id = ntohl(td->trans.tid_local);
	OCTET_STRING_fromBuf(&tcm->choice.begin.otid,
			     (const char *) &trans_id, sizeof(trans_id));

	/* TR-BEGIN-REQ to TSL */
	rc = tcap_tco_tr_begin_req(&td->trans, tcm);

	tcap_trans_set_state(&td->trans, TCAP_TS_INIT_SENT);

	asn_DEF_TCMessage.free_struct(&asn_DEF_TCMessage, tcm, 0);

	return rc;
}

/* Exteract and BER-decode the DialoguePDU contained in the ExternalPDU */
static struct DialoguePDU *unwrap_ext_dialg_pdu(DialoguePortion_t *dialg_por)
{
	struct DialoguePDU *dial_pdu = NULL;
	struct ExternalPDU *ext_pdu = NULL;
	asn_dec_rval_t adr;

	/* extract dialogue portion */
	adr = ber_decode(NULL, &asn_DEF_ExternalPDU, (void **) &ext_pdu, dialg_por->buf,
			 dialg_por->size);
	if (adr.code != RC_OK) {
		fprintf(stderr, "Error parsing ExternalPDU in Dialogue Portion\n");
		return NULL;
	}
	adr = ber_decode(NULL, &asn_DEF_DialoguePDU, (void **) &dial_pdu,
			 ext_pdu->dialog.buf, ext_pdu->dialog.size);
	if (adr.code != RC_OK) {
		fprintf(stderr, "Error parsing DialoguePDU in ExternalPDU\n");
		return NULL;
	}
	/* Release the External part */
	asn_DEF_ExternalPDU.free_struct(&asn_DEF_ExternalPDU, ext_pdu, 0);

	return dial_pdu;
}

/* TR-UNI.ind from TSL */
int tcap_csl_tr_uni_ind(struct tcap_transaction *tt, struct TCMessage *tcmsg, struct msgb *msg)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	struct Unidirectional *unimsg = &tcmsg->choice.unidirectional;
	struct DialoguePDU *dial_pdu = NULL;
	OBJECT_IDENTIFIER_t *app_ctx_name = NULL;
	struct user_information *user_info = NULL;
	int rc = 0;

	if (unimsg->dialoguePortion) {
		/* extract dialogue portion */
		dial_pdu = unwrap_ext_dialg_pdu(unimsg->dialoguePortion);
		/* Check for correctness */
		if (!dial_pdu) {
			/* FIXME */
		}
		/* Check version 1 */
		extract_appctx_uinfo(&app_ctx_name, &user_info, dial_pdu);
	}
	/* Assign Dialogue ID */
	td->dialogue_id = tcap_dialg_id_alloc();
	/* TC-UNI.ind to TCU */
	rc = tcap_tcu_uni_ind(td, app_ctx_name, user_info, 1);
	/* Components to CHA */
	rc = tcap_cha_proc_components(td, &unimsg->components);
	/* Dialogue Terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	if (unimsg->dialoguePortion && dial_pdu) {
		/* Release resources of the ber_decode */
		asn_DEF_DialoguePDU.free_struct(&asn_DEF_DialoguePDU, dial_pdu, 0);
	}

	return rc;
}

/* TR-BEGIN.ind from TSL(TSM) -> CSL(DHA) */
int tcap_csl_tr_begin_ind(struct tcap_transaction *tt, struct TCMessage *tcmsg, struct msgb *msg)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	struct Begin *bgmsg = &tcmsg->choice.begin;
	struct DialoguePDU *dial_pdu = NULL;
	OBJECT_IDENTIFIER_t *app_ctx_name = NULL;
	struct user_information *user_info = NULL;
	int rc = 0;

	if (bgmsg->dialoguePortion) {
		struct AARQ_apdu *aarq;
		/* extract dialogue portion */
		dial_pdu = unwrap_ext_dialg_pdu(bgmsg->dialoguePortion);
		if (!dial_pdu) {
			fprintf(stderr, "TC-BEGIN.ind Error parsing Dialogue portion\n");
			/* Build ABORT apdu */
			/* Discard components */
			/* TR-U-ABPRT.req to TSL */
			rc = tcap_tco_tr_u_abort_req(tt, tcmsg);
			/* Dalogue terminated to CHA */
			tcap_cha_dialg_term(td);

			return rc;
		}

		fprintf(stdout, "\nTC-BEGIN.ind Dialogue portion:\n");
		xer_fprint(stdout, &asn_DEF_DialoguePDU, dial_pdu);

		aarq = &dial_pdu->choice.dialogueRequest;
		if (aarq->protocol_version && 0/* Check version 1 */) {
			/* Build AARE apdu */
			/* Discard components */
			/* TR-P-ABPRT.req to TSL */
			/* Dalogue terminated to CHA */
			tcap_cha_dialg_term(td);
		}
		/* Set application context mode */
		td->app_ctx_mode = 1;
		extract_appctx_uinfo(&app_ctx_name, &user_info, dial_pdu);
	}
	/* Assign Dialogue ID */
	td->dialogue_id = tcap_dialg_id_alloc();

	tcap_trans_set_state(tt, TCAP_TS_INIT_RECV);

	/* TC-BEGIN.ind to TCU */
	rc = tcap_tcu_begin_ind(td, app_ctx_name, user_info, bgmsg->components ? 1 : 0);
	if (bgmsg->components) {
		/* Components to CHA */
		rc = tcap_cha_proc_components(td, bgmsg->components);
	}

	if (bgmsg->dialoguePortion && dial_pdu) {
		/* Release resources of the ber_decode */
		asn_DEF_DialoguePDU.free_struct(&asn_DEF_DialoguePDU, dial_pdu, 0);
	}
	return rc;
}

static int gen_ext_AARE(struct DialoguePDU *dial, ANY_t *any, ExternalPDU_t *ext,
			OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info)
{
	AARE_apdu_t *aare;
	int rc;

	memset(any, 0, sizeof(*any));
	memset(ext, 0, sizeof(*ext));
	memset(dial, 0, sizeof(*dial));

	dial->present = DialoguePDU_PR_dialogueResponse;
	aare = &dial->choice.dialogueResponse;
	/* Set protocol version 1 */
	aare->protocol_version = &dial_version1;
	/* Build AARE-apdu (accepted) */
	memcpy(&aare->application_context_name, app_ctx, sizeof(aare->application_context_name));
	asn_long2INTEGER(&aare->result, Associate_result_accepted);
	aare->user_information = user_info;
	aare->result_source_diagnostic.present = Associate_source_diagnostic_PR_dialogue_service_user;
	asn_long2INTEGER(&aare->result_source_diagnostic.choice.dialogue_service_user,
			 dialogue_service_user_no_reason_given);
	/* Link Dialogue into External PDU */
	rc = ANY_fromType((ANY_t *) &ext->dialog, &asn_DEF_DialoguePDU, dial);
	if (rc < 0) {
		fprintf(stderr, "Error generating ExternalPDU from DialoguePDU\n");
		return rc;
	}
	/* Link External PDU into Dialogue Portion */
	rc = ANY_fromType(any, &asn_DEF_ExternalPDU, ext);
	if (rc < 0) {
		fprintf(stderr, "Error generating ANY_t from ExternalPDU\n");
		return rc;
	}

	return 0;
}

/* TC-CONTINUE.req from TCU */
int tcap_csl_tc_cont_req(struct tcap_dialogue *td, OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info)
{
	struct TCMessage tcm;
	struct ComponentPortion cp;
	ExternalPDU_t ext;
	DialoguePDU_t dial;
	ANY_t any;
	uint32_t trans_id;
	int rc = 0;

	memset(&tcm, 0, sizeof(tcm));
	tcm.present = TCMessage_PR_continue;

	switch (td->trans.state) {
	case TCAP_TS_INIT_RECV:
		if (app_ctx) {
			gen_ext_AARE(&dial, &any, &ext, app_ctx, user_info);
			fprintf(stdout, "\nTC-CONTINUE.req Dialogue portion:\n");
			xer_fprint(stdout, &asn_DEF_DialoguePDU, &dial);
			tcm.choice.Continue.dialoguePortion = (OCTET_STRING_t *) &any;
		}
		break;
	case TCAP_TS_ACTIVE:
		break;
	default:
		fprintf(stderr, "TC-CONTNUE.req in invalid state %s\n",
			tcap_trans_state_name(td->trans.state));
		return -EINVAL;
	}
	/* Request components to CHA */
	/* Process components */
	/* Assemble TSL user data */
	tcm.choice.Continue.components = td->pend_comp;
	td->pend_comp = talloc_zero(td, struct ComponentPortion);

	/* Assign local transaction ID */
	trans_id = htonl(td->trans.tid_local);
	OCTET_STRING_fromBuf(&tcm.choice.Continue.otid,
			     (const char *) &trans_id, sizeof(trans_id));

	/* Assign remote transaction ID */
	trans_id = htonl(td->trans.tid_remote);
	OCTET_STRING_fromBuf(&tcm.choice.Continue.dtid,
			     (const char *) &trans_id, sizeof(trans_id));


	/* TR-CONTINUE to TSL */
	rc = tcap_tco_tr_continue_req(&td->trans, &tcm);

	tcap_trans_set_state(&td->trans, TCAP_TS_ACTIVE);

	return rc;
}

/* TC-END.req from TCU */
int tcap_csl_tc_end_req(struct tcap_dialogue *td, OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info, int prearranged)
{
	struct TCMessage tcm;
	struct ComponentPortion cp;
	ExternalPDU_t ext;
	DialoguePDU_t dial;
	ANY_t any;
	int rc = 0;

	memset(&tcm, 0, sizeof(tcm));
	tcm.present = TCMessage_PR_end;

	switch (td->trans.state) {
	case TCAP_TS_INIT_RECV:
	case TCAP_TS_ACTIVE:
		break;
	case TCAP_TS_INIT_SENT:
		/* TR-END.req to TSL */
		rc = tcap_tco_tr_end_req(&td->trans, &tcm);
		/* Dialogue terminated to CHA */
		tcap_cha_dialg_term(td);
		return rc;
	default:
		fprintf(stderr, "TC-END.req in invalid state %s\n",
			tcap_trans_state_name(td->trans.state));
		return -EINVAL;
	}

	if (prearranged) {
		/* TR-END.req to TSL */
		rc = tcap_tco_tr_end_req(&td->trans, &tcm);
		/* Dialogue terminated to CHA */
		tcap_cha_dialg_term(td);
		/* Free Dialogue ID */
		tcap_dialg_free(td);
		return rc;
	}
	if (td->trans.state == TCAP_TS_INIT_RECV && (app_ctx && user_info)) {
		gen_ext_AARE(&dial, &any, &ext, app_ctx, user_info);
		fprintf(stdout, "\nTC-END.req Dialogue portion:\n");
		xer_fprint(stdout, &asn_DEF_DialoguePDU, &dial);
		tcm.choice.end.dialoguePortion = (OCTET_STRING_t *) &any;
	}
	/* Request component to CHA */
	/* Process components */
	/* Assemble TLS user data */
	tcm.choice.end.components = td->pend_comp;
	talloc_zero(td, struct ComponentPortion);
	/* TR-END.req to TSL */
	rc = tcap_tco_tr_end_req(&td->trans, &tcm);
	/* Dialogue terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	return rc;
}

/* TC-U-APORT.req from TCU */
int tcap_csl_tc_u_abort_req(struct tcap_dialogue *td, uint32_t *abrt_reason,
			    OBJECT_IDENTIFIER_t *app_ctx, struct user_information *user_info)
{
	struct tcap_transaction *tt = &td->trans;
	void *app_ctx_mode;

	switch (tt->state) {
	case TCAP_TS_INIT_RECV:
	case TCAP_TS_ACTIVE:
		break;
	case TCAP_TS_INIT_SENT:
		app_ctx_mode = NULL;
		break;
	default:
		fprintf(stderr, "TC-U-ABORT.req in invalid state %s\n",
			tcap_trans_state_name(td->trans.state));
		return -EINVAL;
	}

	if (app_ctx_mode) {
		if (tt->state != TCAP_TS_ACTIVE && 0 /* Abort reason present && AC-name not supported || dialogure refused */) {
			/* Set protocol version 1 */
			/* Build AARE apdu (rejected) */
		} else {
			/* Build ABRT apdu (source = dialogue-service-user) */
		}
	}
	/* FIXME: TR-U-ABORT.req to TSL */
	//tcap_tco_tr_u_abort_req(tt, tcmsg);
	/* Dialogue terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	return 0;
}

/* TR-END.ind from TSL */
int tcap_csl_tr_end_ind(struct tcap_transaction *tt, struct TCMessage *tcmsg, struct msgb *msg)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	struct End *endmsg = &tcmsg->choice.end;
	struct DialoguePDU *dial_pdu;
	OBJECT_IDENTIFIER_t *app_ctx_name = NULL;
	struct user_information *user_info = NULL;
	int rc = 0;

	switch (td->trans.state) {
	case TCAP_TS_ACTIVE:
		if (endmsg->dialoguePortion) {
			/* FIXME: Discard components */
			/* TC-P-ABORT.ind to TCU */
			//rc = tcap_tcu_abort_ind(td, app_ctx_name, user_info);
		} else {
			/* TC-END.ind to TCU */
			rc = tcap_tcu_end_ind(td, NULL, NULL, endmsg->components ? 1 : 0);
			if (endmsg->components) {
				/* Components to CHA */
				rc = tcap_cha_proc_components(td, endmsg->components);
			}
		}
		break;
	case TCAP_TS_INIT_SENT:
		if (endmsg->dialoguePortion) {
			if (td->app_ctx_mode) {
				/* Extract dialogue portion */
				dial_pdu = unwrap_ext_dialg_pdu(endmsg->dialoguePortion);
				/* Is dialogue portion corect? */
				if (0 /*!correct */) {
					/* TC-P-ABORT.ind to TCU */
					rc = tcap_tcu_abort_ind(td, app_ctx_name, user_info);
				}
				extract_appctx_uinfo(&app_ctx_name, &user_info, dial_pdu);
				/* TC-END.ind to TCU */
				rc = tcap_tcu_end_ind(td, app_ctx_name, user_info, endmsg->components ? 1 : 0);
				if (endmsg->components) {
					/* Components to CHA */
					rc = tcap_cha_proc_components(td, endmsg->components);
				}
			}
		} else {
			if (td->app_ctx_mode) {
				/* Discard components */
				/* TC-P-ABORT.ind to TCU */
				rc = tcap_tcu_abort_ind(td, app_ctx_name, user_info);
			}
		}
		break;
	default:
		return -EINVAL;
	}

	/* Dialogue terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	return 0;
}

/* TR-NOTICE.ind from TSL */
int tcap_csl_tr_notice_ind(struct tcap_transaction *tt)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	uint32_t cause = 0;

	switch (tt->state) {
	case TCAP_TS_INIT_SENT:
	case TCAP_TS_ACTIVE:
		break;
	default:
		return -EINVAL;
	}

	/* TR-NOTICE.ind to TCU */
	return tcap_tcu_notice_ind(td, cause);
}

/* TR-CONTINUE.ind from TSL */
int tcap_csl_tr_continue_ind(struct tcap_transaction *tt, struct TCMessage *tcmsg, struct msgb *msg)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	struct Continue *ctmsg = &tcmsg->choice.Continue;
	struct DialoguePDU *dial_pdu = NULL;
	OBJECT_IDENTIFIER_t *app_ctx_name = NULL;
	struct user_information *user_info = NULL;
	int rc = 0;

	switch (tt->state) {
	case TCAP_TS_INIT_SENT:
		if (ctmsg->dialoguePortion) {
			if (td->app_ctx_mode) {
				/* Extract dialogue portion */
				dial_pdu = unwrap_ext_dialg_pdu(ctmsg->dialoguePortion);
				if (!dial_pdu /*!correct*/) {
err_discard:
					/* FIXME Discard components */
					/* TC-P-ABORT.ind to TCU */
					rc = tcap_tcu_abort_ind(td, app_ctx_name, user_info);
					/* FIXME Build ABRT apdu */
					/* TR-U-ABORT.req to TSL */
					rc = tcap_tco_tr_u_abort_req(tt, tcmsg);
					/* Dialogue terminated to CHA */
					tcap_cha_dialg_term(td);
					/* Free Dialogue ID */
					tcap_dialg_free(td);
					return rc;
				}
				fprintf(stdout, "\nTC-CONTINUE.ind Dialogue portion:\n");
				xer_fprint(stdout, &asn_DEF_DialoguePDU, dial_pdu);
				extract_appctx_uinfo(&app_ctx_name, &user_info, dial_pdu);
			} else
				goto err_discard;
		} else {
			if (td->app_ctx_mode)
				goto err_discard;
		}
		break;
	case TCAP_TS_ACTIVE:
		if (ctmsg->dialoguePortion) {
			/* FIXME: Is abstract syntax = dialoguePDU-AS ?? */
			if (!td->app_ctx_mode)
				goto err_discard;
		}
		break;
	default:
		return -EINVAL;
	}

	tcap_trans_set_state(tt, TCAP_TS_ACTIVE);

	/* TC-CONTINUE.ind to TCU */
	rc = tcap_tcu_cont_ind(td, app_ctx_name, user_info, ctmsg->components ? 1 : 0);
	if (rc < 0)
		return rc;
	if (ctmsg->components) {
		/* Components to CHA */
		rc = tcap_cha_proc_components(td, ctmsg->components);
		if (rc < 0)
			return rc;
	}
	return rc;
}

/* TR-U-ABORT.ind from TSL */
int tcap_csl_tr_u_abort_ind(struct tcap_transaction *tt, struct TCMessage *tcmsg, struct msgb *msg)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);

	switch (tt->state) {
	case TCAP_TS_INIT_SENT:
		break;
	/* FIXME: TCAP_TS_ACTIVE */
	default:
		return -EINVAL;
	}

	/* FIXME */
}

/* TR-P-ABORT.ind from TSL */
int tcap_csl_tr_p_abort_ind(struct tcap_transaction *tt)
{
	struct tcap_dialogue *td = dialg_by_trans(tt);
	OBJECT_IDENTIFIER_t *app_ctx_name = NULL;
	struct user_information *user_data = NULL;
	int rc;

	switch (tt->state) {
	case TCAP_TS_INIT_SENT:
	case TCAP_TS_ACTIVE:
		break;
	default:
		return -EINVAL;
	}

	/* TC-P-ABORT.ind to TCU */
	rc = tcap_tcu_abort_ind(td, app_ctx_name, user_data);
	/* Dialog terminated to CHA */
	tcap_cha_dialg_term(td);
	/* Free Dialogue ID */
	tcap_dialg_free(td);

	return rc;
}