aboutsummaryrefslogtreecommitdiffstats
path: root/src/sccp2sua.c
blob: 811cc555827c34766eb9d833d0abcd4160e1fbf9 (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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
/* SCCP <-> SUA transcoding routines */

/* (C) 2017 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * based on my 2011 Erlang implementation osmo_ss7/src/sua_sccp_conv.erl
 *
 * References: ITU-T Q.713 and IETF RFC 3868
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>

#include <osmocom/sccp/sccp.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/logging.h>
#include <osmocom/sigtran/sccp_sap.h>
#include <osmocom/sigtran/sccp_helpers.h>
#include <osmocom/sigtran/protocol/sua.h>
#include <osmocom/sigtran/xua_msg.h>

#include "xua_internal.h"
#include "sccp_internal.h"

/* libosmocore candidates */

static void msgb_put_u24be(struct msgb *msg, uint32_t val)
{
	msgb_put_u8(msg, (val >> 16) & 0xff);
	msgb_put_u8(msg, (val >> 8) & 0xff);
	msgb_put_u8(msg, val & 0xff);
}

static void msgb_put_u16le(struct msgb *msg, uint16_t val)
{
	msgb_put_u8(msg, val & 0xff);
	msgb_put_u8(msg, (val >> 8) & 0xff);
}

/*! \brief load a 24bit value as big-endian */
static uint32_t load_24be(const void *ptr)
{
	const uint8_t *data = ptr;
	return (data[0] << 16) | (data[1] << 8) | data[2];
}



/*! \brief Parse ISUP style address of BCD digets
 *  \param[out] out_digits user-allocated buffer for ASCII digits
 *  \param[in] in BCD-encoded digits
 *  \param[in] in_num_bytes Size of \ref in in bytes
 *  \param[in] odd Odd (true) or even (false) number of digits
 *  \returns number of digits generated
 * */
int osmo_isup_party_parse(char *out_digits, const uint8_t *in,
			    unsigned int in_num_bytes, bool odd)
{
	char *out = out_digits;
	unsigned int i;

	for (i = 0; i < in_num_bytes; i++) {
		*out_digits++ = osmo_bcd2char(in[i] & 0x0F);
		if (i+1 == in_num_bytes && odd)
			break;
		*out_digits++ = osmo_bcd2char(in[i] >> 4);
	}
	*out_digits = '\0';
	return (out_digits - out);
}

/*! \brief Encode an ISUP style address of BCD digits
 *  \param[out] msg Message to which the encoded address is appended
 *  \param[in] in_digits NUL-terminated ASCII string of digits
 *  \returns number of octets used for encoding \ref in_digits */
int osmo_isup_party_encode(struct msgb *msg, const char *in_digits)
{
	unsigned int num_digits = strlen(in_digits);
	unsigned int i, num_octets = num_digits/2;
	const char *cur_digit = in_digits;
	uint8_t *cur;

	if (num_digits & 1)
		num_octets++;

	cur = msgb_put(msg, num_octets);

	for (i = 0; i < num_octets;  i++) {
		cur[i] = osmo_char2bcd(*cur_digit++);
		if (cur_digit - in_digits < num_digits)
			cur[i] |= osmo_char2bcd(*cur_digit++) << 4;
	}
	return num_octets;
}

/*! \brief Parse wire-encoded SCCP address into osmo_sccp_addr
 *  \param[out] out user-allocated output data structure
 *  \param[in] addr wire-encoded SCCP address
 *  \param[in] addrlen Size of \ref addr in bytes
 *  \returns 0 in case of success, negative on error
 * According to Q.713/3.4 and RFC3868/3.10.2 */
int osmo_sccp_addr_parse(struct osmo_sccp_addr *out,
				const uint8_t *addr, unsigned int addrlen)
{
	struct sccp_called_party_address *sca;
	uint8_t *cur;
	uint8_t encoding;
	bool odd;
	int rc;

	memset(out, 0, sizeof(*out));

	sca = (struct sccp_called_party_address *) addr;
	cur = sca->data;

	if (sca->routing_indicator)
		out->ri = OSMO_SCCP_RI_SSN_PC;
	else
		out->ri = OSMO_SCCP_RI_GT;

	if (sca->point_code_indicator) {
		out->presence |= OSMO_SCCP_ADDR_T_PC;
		out->pc = (uint16_t) (cur[1] & 0x3f) << 8;
		out->pc |= cur[0];
		cur += 2;
	}

	if (sca->ssn_indicator) {
		out->presence |= OSMO_SCCP_ADDR_T_SSN;
		out->ssn = *cur;
		cur += 1;
	}

	switch (sca->global_title_indicator) {
	case SCCP_TITLE_IND_NONE:
		out->gt.gti = OSMO_SCCP_GTI_NO_GT;
		return 0;
	case SCCP_TITLE_IND_NATURE_ONLY:
		out->presence |= OSMO_SCCP_ADDR_T_GT;
		out->gt.gti = OSMO_SCCP_GTI_NAI_ONLY;
		out->gt.nai = *cur & 0x7f;
		if (*cur++ & 0x80)
			odd = true;
		else
			odd = false;
		break;
	case SCCP_TITLE_IND_TRANSLATION_ONLY:
		out->presence |= OSMO_SCCP_ADDR_T_GT;
		out->gt.gti = OSMO_SCCP_GTI_TT_ONLY;
		out->gt.tt = *cur++;
		/* abort, for national use only */
		LOGP(DLSUA, LOGL_ERROR, "Unsupported national GTI %u\n", sca->global_title_indicator);
		return -EINVAL;
	case SCCP_TITLE_IND_TRANS_NUM_ENC:
		out->presence |= OSMO_SCCP_ADDR_T_GT;
		out->gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC;
		out->gt.tt = *cur++;
		out->gt.npi = *cur >> 4;
		encoding = *cur++ & 0xF;
		switch (encoding) {
		case 1:
			odd = true;
			break;
		case 2:
			odd = false;
			break;
		default:
			LOGP(DLSUA, LOGL_ERROR, "Unknown GT encoding 0x%x\n", encoding);
			return -1;
		}
		break;
	case SCCP_TITLE_IND_TRANS_NUM_ENC_NATURE:
		out->presence |= OSMO_SCCP_ADDR_T_GT;
		out->gt.gti = OSMO_SCCP_GTI_TT_NPL_ENC_NAI;
		out->gt.tt = *cur++;
		out->gt.npi = *cur >> 4;
		encoding = *cur++ & 0xF;
		switch (encoding) {
		case 1:
			odd = true;
			break;
		case 2:
			odd = false;
			break;
		default:
			LOGP(DLSUA, LOGL_ERROR, "Unknown GT encoding 0x%x\n",
				encoding);
			return -EINVAL;
		}
		out->gt.nai = *cur++ & 0x7f;
		break;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unknown GTI %u in SCCP message\n",
			sca->global_title_indicator);
		return -EINVAL;
	}
	rc = osmo_isup_party_parse(out->gt.digits, cur, (addr+addrlen-cur), odd);
	if (rc < 0)
		return rc;

	return 0;
}

/*! \brief encode a SCCP address from parsed format to wire format
 *  \param[out] msg message buffer to which address is to be appended
 *  \param[in] in data structure describing SCCP address
 *  \returns number of bytes written to \ref msg */
int osmo_sccp_addr_encode(struct msgb *msg, const struct osmo_sccp_addr *in)
{
	struct sccp_called_party_address *sca;
	bool odd;

	sca = (struct sccp_called_party_address *) msgb_put(msg, sizeof(*sca));
	switch (in->ri) {
	case OSMO_SCCP_RI_SSN_PC:
		sca->routing_indicator = 1;
		break;
	case OSMO_SCCP_RI_GT:
		sca->routing_indicator = 0;
		break;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unknown CCP Routing Indicator %u"
			" requested\n", in->ri);
		return -EINVAL;
	}

	if (in->presence & OSMO_SCCP_ADDR_T_PC) {
		sca->point_code_indicator = 1;
		/* ITU-T Q.713 states that signalling point codes are 14bit */
		if (in->pc > 0x3fff) {
			LOGP(DLSUA, LOGL_ERROR, "Invalid Point Code %u requested\n", in->pc);
			return -EINVAL;
		}
		msgb_put_u16le(msg, in->pc & 0x3fff);
	}

	if (in->presence & OSMO_SCCP_ADDR_T_SSN) {
		sca->ssn_indicator = 1;
		if (in->ssn > 0xff) {
			LOGP(DLSUA, LOGL_ERROR, "Invalid SSN %u requested\n", in->ssn);
			return -EINVAL;
		}
		msgb_put_u8(msg, in->ssn);
	}

	if (!(in->presence & OSMO_SCCP_ADDR_T_GT)) {
		sca->global_title_indicator = SCCP_TITLE_IND_NONE;
		goto out;
	}

	if (in->gt.npi && (in->gt.npi > 0xF)) {
		LOGP(DLSUA, LOGL_ERROR, "Unsupported Numbering Plan %u", in->gt.npi);
		return -EINVAL;
	}

	if (in->gt.nai && (in->gt.nai > 0x7F)) {
		LOGP(DLSUA, LOGL_ERROR, "Unsupported Nature of Address %u", in->gt.nai);
		return -EINVAL;
	}

	odd = strlen(in->gt.digits) & 1;
	switch (in->gt.gti) {
	case OSMO_SCCP_GTI_NO_GT:
		sca->global_title_indicator = SCCP_TITLE_IND_NONE;
		goto out;
	case OSMO_SCCP_GTI_NAI_ONLY:
		sca->global_title_indicator = SCCP_TITLE_IND_NATURE_ONLY;
		msgb_put_u8(msg, (odd << 7) | (in->gt.nai & 0x7f));
		break;
	case OSMO_SCCP_GTI_TT_ONLY:
		sca->global_title_indicator = SCCP_TITLE_IND_TRANSLATION_ONLY;
		msgb_put_u8(msg, in->gt.tt);
		/* abort, for national use only */
		LOGP(DLSUA, LOGL_ERROR, "Unsupported Translation Type %u"
			"requested\n", in->gt.gti);
		return -EINVAL;
	case OSMO_SCCP_GTI_TT_NPL_ENC:
		sca->global_title_indicator = SCCP_TITLE_IND_TRANS_NUM_ENC;
		msgb_put_u8(msg, in->gt.tt);
		msgb_put_u8(msg, (in->gt.npi << 4) | (odd ? 1 : 2));
		break;
	case OSMO_SCCP_GTI_TT_NPL_ENC_NAI:
		sca->global_title_indicator = SCCP_TITLE_IND_TRANS_NUM_ENC_NATURE;
		msgb_put_u8(msg, in->gt.tt);
		msgb_put_u8(msg, (in->gt.npi << 4) | (odd ? 1 : 2));
		msgb_put_u8(msg, in->gt.nai & 0x7f);
		break;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unsupported GTI %u requested\n", in->gt.gti);
		return -EINVAL;
	}
	osmo_isup_party_encode(msg, in->gt.digits);

out:
	/* return number of bytes written */
	return msg->tail - (uint8_t *)sca;
}

/*! \brief convert SCCP address to SUA address
 *  \param xua user-provided xUA message to which address shall be added
 *  \param[in] iei SUA Information Element Identifier for address
 *  \param[in] addr SCCP wire format binary address
 *  \param[in] addrlen Size of \ref addr in bytes
 *  \returns 0 in case of success; negative on error */
static int sccp_addr_to_sua(struct xua_msg *xua, uint16_t iei, const uint8_t *addr,
			    unsigned int addrlen)
{
	struct osmo_sccp_addr osa;
	int rc;

	/* First decode the address from SCCP wire format to
	 * osmo_sccp_addr */
	rc = osmo_sccp_addr_parse(&osa, addr, addrlen);
	if (rc < 0)
		return rc;

	LOGP(DLSUA, LOGL_DEBUG, "IEI %u: Parsed Addr: %s\n", iei, osmo_sccp_addr_dump(&osa));

	/* Then re-encode it as SUA address */
	return xua_msg_add_sccp_addr(xua, iei, &osa);
}

/*! \brief convenience wrapper around sccp_addr_to_sua() for variable mandatory addresses */
static int sccp_addr_to_sua_ptr(struct xua_msg *xua, uint16_t iei, const uint8_t *ptr_addr, bool ptr_addr_is_long)
{
	const uint8_t *addr;
	unsigned int addrlen;

	if (ptr_addr_is_long) /* +1: Distance from MSB of pointer */
		addr = ptr_addr + 1 + osmo_load16le(ptr_addr);
	else
		addr = ptr_addr + *ptr_addr;
	addrlen = *addr;
	addr++;

	return sccp_addr_to_sua(xua, iei, addr, addrlen);
}

/*! \brief convert SUA address to SCCP address
 *  \param msg user-provided message buffer to which address shall be *  appended
 *  \param[in] part SUA wire format binary address
 *  \returns 0 in case of success; negative on error */
static int sua_addr_to_sccp(struct msgb *msg, const struct xua_msg_part *part)
{
	struct osmo_sccp_addr osa;
	int rc;

	/* First decode the address from SUA wire format to
	 * osmo_sccp_addr */
	rc = sua_addr_parse_part(&osa, part);
	if (rc < 0)
		return rc;

	/* Then re-encode it as SCCP address */
	return osmo_sccp_addr_encode(msg, &osa);
}

/*! \brief Add a "SCCP Variable Mandatory Part" (Address format) to the given msgb
 *  \param msg Message buffer to which part shall be added
 *  \param[out] var_ptr pointer to relative pointer in SCCP header
 *  \param[in] var_ptr_is_long Whether the var_ptr field is 2 bytes long (network order)
 *  \param[in] xua xUA message from which to use address
 *  \param[in] iei xUA information element identifier of address */
static int sccp_add_var_addr(struct msgb *msg, uint8_t *var_ptr, bool var_ptr_is_long, const struct xua_msg *xua, uint16_t iei)
{
	struct xua_msg_part *part = xua_msg_find_tag(xua, iei);
	uint8_t *lenbyte;
	int rc;
	if (!part) {
		LOGP(DLSUA, LOGL_ERROR, "Cannot find IEI %u in SUA message\n", iei);
		return -ENODEV;
	}

	/* first allocate one byte for the length */
	lenbyte = msgb_put(msg, 1);
	/* update the relative pointer to the length byte */
	if (var_ptr_is_long) /* -1: Distance from MSB of pointer */
		osmo_store16le(lenbyte - var_ptr - 1, var_ptr);
	else
		*var_ptr = lenbyte - var_ptr;

	/* then append the encoded SCCP address */
	rc = sua_addr_to_sccp(msg, part);
	if (rc < 0)
		return rc;

	/* store the encoded length of the address */
	*lenbyte = rc;

	return rc;
}

/*! \brief Add a "SCCP Variable Mandatory Part" to the given msgb
 *  \param msg Message buffer to which part shall be added
 *  \param[out] var_ptr pointer to relative pointer in SCCP header
 *  \param[in] xua xUA message from which to use source data
 *  \param[in] iei xUA information element identifier of source data */
static int sccp_add_variable_part(struct msgb *msg, uint8_t *var_ptr, const struct xua_msg *xua, uint16_t iei)
{
	struct xua_msg_part *part = xua_msg_find_tag(xua, iei);
	uint8_t *lenbyte;
	uint8_t *cur;
	if (!part) {
		LOGP(DLSUA, LOGL_ERROR, "Cannot find IEI %u in SUA message\n", iei);
		return -ENODEV;
	}

	/* first allocate one byte for the length */
	lenbyte = msgb_put(msg, 1);
	/* update the relative pointer to the length byte */
	*var_ptr = lenbyte - var_ptr;

	/* then append the encoded SCCP address */
	cur = msgb_put(msg, part->len);
	memcpy(cur, part->dat, part->len);

	/* store the encoded length of the address */
	*lenbyte = part->len;

	return part->len;
}

/*! \brief Add a "SCCP Long Variable Mandatory Part" to the given msgb
 *  \param msg Message buffer to which part shall be added
 *  \param[out] var_ptr pointer to relative pointer in SCCP header
 *  \param[in] xua xUA message from which to use source data
 *  \param[in] iei xUA information element identifier of source data */
static int sccp_add_long_variable_part(struct msgb *msg, uint8_t *var_ptr, const struct xua_msg *xua, uint16_t iei)
{
	struct xua_msg_part *part = xua_msg_find_tag(xua, iei);
	uint8_t *lenbyte;
	uint8_t *cur;
	if (!part) {
		LOGP(DLSUA, LOGL_ERROR, "Cannot find IEI %u in SUA message\n", iei);
		return -ENODEV;
	}

	/* first allocate 2 bytes for the length */
	lenbyte = msgb_put(msg, 2);
	/* update the relative pointer to the length byte.
	 * This is only used in LUDT and LUDTS, so the pointer is always 2 bytes.
	 * -1: Distance from MSB of pointer */
	osmo_store16le(lenbyte - var_ptr - 1, var_ptr);

	/* then append the encoded SCCP address */
	cur = msgb_put(msg, part->len);
	memcpy(cur, part->dat, part->len);

	/* store the encoded length of the address, LSB first */
	osmo_store16le(part->len, lenbyte);

	return part->len;
}

/*! \brief validate that SCCP part with pointer + length doesn't exceed msg tail
 *  \param[in] msg Message containing SCCP address
 *  \param[in] ptr_addr pointer to byte with relative SCCP pointer
 *  \returns true if OK; false if message inconsistent */
static bool sccp_ptr_part_consistent(const struct msgb *msg, const uint8_t *ptr_addr)
{
	const uint8_t *ptr;

	/* check the address of the relative pointer is within msg */
	if (ptr_addr < msg->data || ptr_addr > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr_addr outside msg boundary\n");
		return false;
	}

	ptr = ptr_addr + *ptr_addr;
	if (ptr > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr points outside msg boundary\n");
		return false;
	}

	/* at destination of relative pointer is the length */
	if (ptr + 1 + *ptr > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr + len points outside msg boundary\n");
		return false;
	}
	return true;
}

/*! \brief validate that SCCP part with long pointer (2 bytes) + length doesn't exceed msg tail
 *  \param[in] msg Message containing SCCP address (LUDT or LUDTS)
 *  \param[in] ptr_addr pointer to byte with relative SCCP long pointer (uint16_t, 2 bytes in network order)
 *  \param[in] len_is_long whether the length field at the starting of the value field pointer to by ptr_addr is 2 bytes long.
 *  \returns true if OK; false if message inconsistent */
static bool sccp_longptr_part_consistent(const struct msgb *msg, const uint8_t *ptr_addr, bool len_is_long)
{
	const uint8_t *ptr;
	uint8_t offs;
	uint16_t len;

	/* check the address of the relative pointer is within msg */
	if (ptr_addr < msg->data || ptr_addr > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr_addr outside msg boundary\n");
		return false;
	}

	/* +1: Distance from MSB of pointer */
	ptr = ptr_addr + 1 + osmo_load16le(ptr_addr);
	if (ptr > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr %p points outside msg boundary %p\n", ptr, msg->tail);
		return false;
	}

	/* at destination of relative pointer is the length */
	if (len_is_long) {
		offs = 2;
		len = osmo_load16le(ptr);
	} else {
		offs = 1;
		len = *ptr;
	}
	if (ptr + offs + len > msg->tail) {
		LOGP(DLSUA, LOGL_ERROR, "ptr + len points outside msg boundary\n");
		return false;
	}
	return true;
}

/*! \brief convenience wrapper around xua_msg_add_data() for variable mandatory data */
static int sccp_data_to_sua_ptr(struct xua_msg *xua, uint16_t iei, const uint8_t *ptr_addr)
{
	const uint8_t *addr = ptr_addr + *ptr_addr + 1;
	unsigned int addrlen = *(ptr_addr + *ptr_addr);

	return xua_msg_add_data(xua, iei, addrlen, addr);
}

/*! \brief convenience wrapper around xua_msg_add_data() for variable mandatory data */
static int sccp_longdata_to_sua_ptr(struct xua_msg *xua, uint16_t iei, const uint8_t *ptr_addr)
{
	const uint16_t ptr_value = osmo_load16le(ptr_addr);
	/* +1: Distance from MSB of pointer */
	const uint8_t *addrlen_ptr = ptr_addr + 1 + ptr_value;
	/* +2: size of length field */
	const uint8_t *addr = addrlen_ptr + 2;
	unsigned int addrlen = osmo_load16le(addrlen_ptr);

	return xua_msg_add_data(xua, iei, addrlen, addr);
}


/*! \brief Convert a given SCCP option to SUA and add it to given xua_msg
 *  \param xua caller-provided xUA message to which option is to be  added
 *  \param[in] sccp_opt_type SCCP option type (PNC)
 *  \param[in] opt_len size of \ref opt in bytes
 *  \param[in] opt pointer to wire-format encoded SCCP option data
 *  \returns 0 in case of success; negative on error */
static int xua_msg_add_sccp_opt(struct xua_msg *xua, uint8_t sccp_opt_type,
				uint16_t opt_len, const uint8_t *opt)
{
	switch (sccp_opt_type) {
	case SCCP_PNC_DESTINATION_LOCAL_REFERENCE:
		if (opt_len != 3)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(opt));
		break;
	case SCCP_PNC_SOURCE_LOCAL_REFERENCE:
		if (opt_len != 3)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(opt));
		break;
	case SCCP_PNC_CALLED_PARTY_ADDRESS:
		if (opt_len < 3)
			return -EINVAL;
		sccp_addr_to_sua(xua, SUA_IEI_DEST_ADDR, opt, opt_len);
		break;
	case SCCP_PNC_CALLING_PARTY_ADDRESS:
		if (opt_len < 3)
			return -EINVAL;
		sccp_addr_to_sua(xua, SUA_IEI_SRC_ADDR, opt, opt_len);
		break;
	case SCCP_PNC_PROTOCOL_CLASS:
		if (opt_len < 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, *opt);
		break;
	case SCCP_PNC_CREDIT:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_IMPORTANCE, *opt & 0x7);
		break;
	case SCCP_PNC_RELEASE_CAUSE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RELEASE | *opt);
		break;
	case SCCP_PNC_RETURN_CAUSE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RETURN | *opt);
		break;
	case SCCP_PNC_RESET_CAUSE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RESET | *opt);
		break;
	case SCCP_PNC_ERROR_CAUSE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_ERROR | *opt);
		break;
	case SCCP_PNC_REFUSAL_CAUSE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_REFUSAL | *opt);
		break;
	case SCCP_PNC_DATA:
		xua_msg_add_data(xua, SUA_IEI_DATA, opt_len, opt);
		break;
	case SCCP_PNC_HOP_COUNTER:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_S7_HOP_CTR, *opt);
		break;
	case SCCP_PNC_IMPORTANCE:
		if (opt_len != 1)
			return -EINVAL;
		xua_msg_add_u32(xua, SUA_IEI_IMPORTANCE, *opt & 0x7);
		break;
	case SCCP_PNC_LONG_DATA:
		xua_msg_add_data(xua, SUA_IEI_DATA, opt_len, opt);
		break;
	case SCCP_PNC_SEGMENTATION:
	case SCCP_PNC_SEGMENTING:
	case SCCP_PNC_RECEIVE_SEQ_NUMBER:
		/* only in class 3 */
	case SCCP_PNC_SEQUENCING:
		/* only in class 3 */
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unsupported SCCP option type %u\n",
			sccp_opt_type);
		return -1;
	}
	return 0;
}

/*! \brief append a SCCP option header to the given message
 *  \param msg Message to which header is to be appended
 *  \param[in] pnc PNC of the option header
 *  \param[in] len length of the option, excluding the header */
static void msgb_put_sccp_opt_hdr(struct msgb *msg, uint8_t pnc, uint8_t len)
{
	msgb_put_u8(msg, pnc);
	msgb_put_u8(msg, len);
}

/*! \brief append a SCCP option to the given message
 *  \param msg Message to which option is to be appended
 *  \param[in] pnc PNC of the option header
 *  \param[in] len length of the option, excluding the header
 *  \param[in] data actual data to be appended */
static void msgb_put_sccp_opt(struct msgb *msg, uint8_t pnc, uint8_t len, const uint8_t *data)
{
	uint8_t *cur;

	msgb_put_sccp_opt_hdr(msg, pnc, len);
	cur = msgb_put(msg, len);
	memcpy(cur, data, len);
}

/*! \brief Convert a given SUA option/IE to SCCP and add it to given * msgb
 *  \param msg caller-provided message buffer to which option is to be appended
 *  \param[in] opt xUA option/IE (messge part) to be converted+added
 *  \returns 0 in case of success; negative on error */
static int sccp_msg_add_sua_opt(enum sccp_message_types type, struct msgb *msg, const struct xua_msg_part *opt)
{
	uint32_t tmp32;
	uint8_t pnc, *lenptr;
	int rc;

	switch (opt->tag) {
	case SUA_IEI_DEST_REF:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_DESTINATION_LOCAL_REFERENCE, 3);
		msgb_put_u24be(msg, xua_msg_part_get_u32(opt));
		break;
	case SUA_IEI_SRC_REF:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_SOURCE_LOCAL_REFERENCE, 3);
		msgb_put_u24be(msg, xua_msg_part_get_u32(opt));
		break;
	case SUA_IEI_DEST_ADDR:
		switch (type) {
		case SCCP_MSG_TYPE_CC:
		case SCCP_MSG_TYPE_CREF:
			/* The Destination of a CC message is the
			 * originator of the connection: Calling Party */
			msgb_put_u8(msg, SCCP_PNC_CALLING_PARTY_ADDRESS);
			break;
		default:
			msgb_put_u8(msg, SCCP_PNC_CALLED_PARTY_ADDRESS);
			break;
		}
		lenptr = msgb_put(msg, 1);
		rc = sua_addr_to_sccp(msg, opt);
		if (rc < 0)
			return rc;
		*lenptr = rc;
		break;
	case SUA_IEI_SRC_ADDR:
		switch (type) {
		case SCCP_MSG_TYPE_CC:
		case SCCP_MSG_TYPE_CREF:
			/* The Source of a CC message is the
			 * responder of the connection: Called Party */
			msgb_put_u8(msg, SCCP_PNC_CALLED_PARTY_ADDRESS);
			break;
		default:
			msgb_put_u8(msg, SCCP_PNC_CALLING_PARTY_ADDRESS);
			break;
		}
		lenptr = msgb_put(msg, 1);
		rc = sua_addr_to_sccp(msg, opt);
		if (rc < 0)
			return rc;
		*lenptr = rc;
		break;
	case SUA_IEI_PROTO_CLASS:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_PROTOCOL_CLASS, 1);
		msgb_put_u8(msg, xua_msg_part_get_u32(opt));
		break;
	case SUA_IEI_CREDIT:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_CREDIT, 1);
		msgb_put_u8(msg, xua_msg_part_get_u32(opt) & 0x7);
		break;
	case SUA_IEI_CAUSE:
		tmp32 = xua_msg_part_get_u32(opt);
		switch (tmp32 & SUA_CAUSE_T_MASK) {
		case SUA_CAUSE_T_RETURN:
			pnc = SCCP_PNC_RETURN_CAUSE;
			break;
		case SUA_CAUSE_T_REFUSAL:
			pnc = SCCP_PNC_REFUSAL_CAUSE;
			break;
		case SUA_CAUSE_T_RELEASE:
			pnc = SCCP_PNC_RELEASE_CAUSE;
			break;
		case SUA_CAUSE_T_RESET:
			pnc = SCCP_PNC_RESET_CAUSE;
			break;
		case SUA_CAUSE_T_ERROR:
			pnc = SCCP_PNC_ERROR_CAUSE;
			break;
		default:
			LOGP(DLSUA, LOGL_ERROR, "Unknown SUA Cause Class 0x%04x\n", tmp32);
			return -EINVAL;
		}
		msgb_put_sccp_opt_hdr(msg, pnc, 1);
		msgb_put_u8(msg, tmp32 & 0xff);
		break;
	case SUA_IEI_DATA:
		msgb_put_sccp_opt(msg, SCCP_PNC_DATA, opt->len, opt->dat);
		break;
	case SUA_IEI_S7_HOP_CTR:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_HOP_COUNTER, 1);
		msgb_put_u8(msg, xua_msg_part_get_u32(opt));
		break;
	case SUA_IEI_IMPORTANCE:
		msgb_put_sccp_opt_hdr(msg, SCCP_PNC_IMPORTANCE, 1);
		msgb_put_u8(msg, xua_msg_part_get_u32(opt) & 0x7);
		break;
	case SUA_IEI_ROUTE_CTX:
		break;
	case SUA_IEI_SEQ_CTRL:
		/* TODO */
		break;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unknown SUA IEI 0x%04x\n", opt->tag);
		return -1;
	}
	return 0;
}

/*! \brief convert SCCP optional part to list of SUA options
 *  \param[in] msg Message buffer holding SCCP message
 *  \param[in] ptr_opt address of relative pointer to optional part
 *  \param[in] ptr_opt_is_long whether ptr_opt is a long pointer (2 bytes, network order)
 *  \param xua caller-provided xUA message to which options are added
 *  \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_opt(const struct msgb *msg, const uint8_t *ptr_opt, bool ptr_opt_is_long, struct xua_msg *xua)
{
	const uint8_t *opt_start, *oneopt;
	uint16_t ptr_opt_value;

	/* some bounds checking */
	if (ptr_opt < msg->data)
		return NULL;
	if (ptr_opt > msg->tail - (ptr_opt_is_long ? 2 : 1))
		return NULL;

	if (ptr_opt_is_long)
		ptr_opt_value = osmo_load16le(ptr_opt);
	else
		ptr_opt_value = *ptr_opt;

	/* Q.713 section 2.3 "Coding of pointers": pointer value all zeros used
	  to indicate that no optional param is present. */
	if (ptr_opt_value == 0)
		return xua;

	if (ptr_opt_is_long) /* +1: Distance from MSB of pointer */
		opt_start = ptr_opt + 1 + ptr_opt_value;
	else
		opt_start = ptr_opt + ptr_opt_value;
	if (opt_start > msg->tail)
		return NULL;

	oneopt = opt_start;

	enum sccp_parameter_name_codes opt_type = 0; /* dummy value not used */
	while (oneopt < msg->tail) {
		uint8_t opt_len;
		uint16_t opt_len16;
		opt_type = oneopt[0];

		switch (opt_type) {
		case SCCP_PNC_END_OF_OPTIONAL:
			return xua;
		case SCCP_PNC_LONG_DATA:
			/* two byte length field */
			if (oneopt + 2 > msg->tail)
				goto malformed;
			opt_len16 = oneopt[1] << 8 | oneopt[2];
			if (oneopt + 3 + opt_len16 > msg->tail)
				goto malformed;
			xua_msg_add_sccp_opt(xua, opt_type, opt_len16, oneopt+3);
			oneopt += 3 + opt_len16;
			break;
		default:
			/* one byte length field */
			if (oneopt + 1 > msg->tail)
				goto malformed;

			opt_len = oneopt[1];
			if (oneopt + 2 + opt_len > msg->tail)
				goto malformed;
			xua_msg_add_sccp_opt(xua, opt_type, opt_len, oneopt+2);
			oneopt += 2 + opt_len;
		}
	}
	LOGP(DLSUA, LOGL_ERROR, "Parameter %s not found\n", osmo_sccp_pnc_name(SCCP_PNC_END_OF_OPTIONAL));
	return NULL;

malformed:
	LOGP(DLSUA, LOGL_ERROR, "Malformed parameter %s (%d)\n", osmo_sccp_pnc_name(opt_type), opt_type);
	return NULL;
}

#define MAX_IES		6
#define NUM_SCCP_MSGT	(SCCP_MSG_TYPE_LUDTS+1)

/* This table indicates which information elements are mandatory and not
 * optional in SCCP, per message type */
static const uint16_t sccp_mandatory[NUM_SCCP_MSGT][MAX_IES] = {
	/* Table 3/Q.713 */
	[SCCP_MSG_TYPE_CR] = {
		SUA_IEI_SRC_REF, SUA_IEI_PROTO_CLASS, SUA_IEI_DEST_ADDR , 0
	},
	/* Table 4/Q.713 */
	[SCCP_MSG_TYPE_CC] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, SUA_IEI_PROTO_CLASS, 0
	},
	/* Table 5/Q.713 */
	[SCCP_MSG_TYPE_CREF] = {
		SUA_IEI_DEST_REF, SUA_IEI_CAUSE, 0
	},
	/* Table 6/Q.713 */
	[SCCP_MSG_TYPE_RLSD] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, SUA_IEI_CAUSE, 0
	},
	/* Table 7/Q.713 */
	[SCCP_MSG_TYPE_RLC] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, 0
	},
	/* Table 8/Q.713 */
	[SCCP_MSG_TYPE_DT1] = {
		SUA_IEI_DEST_REF, SUA_IEI_SEGMENTATION, 0
	},
	/* Table 9/Q.713 */
	[SCCP_MSG_TYPE_DT2] = {
		SUA_IEI_DEST_REF, SUA_IEI_SEGMENTATION, 0
	},
	/* Table 10/Q.713 */
	[SCCP_MSG_TYPE_AK] = {
		SUA_IEI_DEST_REF, SUA_IEI_RX_SEQ_NR, 0
	},
	/* Table 11/Q.713 */
	[SCCP_MSG_TYPE_UDT] = {
		SUA_IEI_PROTO_CLASS, SUA_IEI_DEST_ADDR,
		SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
	/* Table 12/Q.713 */
	[SCCP_MSG_TYPE_UDTS] = {
		SUA_IEI_CAUSE, SUA_IEI_DEST_ADDR, SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
	/* Table 13/Q.713 */
	[SCCP_MSG_TYPE_ED] = {
		SUA_IEI_DEST_REF, 0
	},
	/* Table 14/Q.713 */
	[SCCP_MSG_TYPE_EA] = {
		SUA_IEI_DEST_REF, 0
	},
	/* Table 15/Q.713 */
	[SCCP_MSG_TYPE_RSR] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, SUA_IEI_CAUSE, 0
	},
	/* Table 16/Q.713 */
	[SCCP_MSG_TYPE_RSC] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, 0
	},
	/* Table 17/Q.713 */
	[SCCP_MSG_TYPE_ERR] = {
		SUA_IEI_DEST_REF, SUA_IEI_CAUSE, 0
	},
	/* Table 18/Q.713 */
	[SCCP_MSG_TYPE_IT] = {
		SUA_IEI_DEST_REF, SUA_IEI_SRC_REF, SUA_IEI_PROTO_CLASS,
		SUA_IEI_SEGMENTATION, SUA_IEI_CREDIT, 0
	},
	/* Table 19/Q.713 */
	[SCCP_MSG_TYPE_XUDT] = {
		SUA_IEI_PROTO_CLASS, SUA_IEI_S7_HOP_CTR,
		SUA_IEI_DEST_ADDR, SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
	/* Table 20/Q.713 */
	[SCCP_MSG_TYPE_XUDTS] = {
		SUA_IEI_CAUSE, SUA_IEI_S7_HOP_CTR, SUA_IEI_DEST_ADDR,
		SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
	/* Table 21/Q.713 */
	[SCCP_MSG_TYPE_LUDT] = {
		SUA_IEI_PROTO_CLASS, SUA_IEI_S7_HOP_CTR,
		SUA_IEI_DEST_ADDR, SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
	/* Table 22/Q.713 */
	[SCCP_MSG_TYPE_LUDTS] = {
		SUA_IEI_CAUSE, SUA_IEI_S7_HOP_CTR, SUA_IEI_DEST_ADDR,
		SUA_IEI_SRC_ADDR, SUA_IEI_DATA, 0
	},
};

/* This table indicates which information elements are optionally
 * permitted in the respective SCCP message type */
static const uint16_t sccp_optional[NUM_SCCP_MSGT][MAX_IES] = {
	/* Table 3/Q.713 */
	[SCCP_MSG_TYPE_CR] = {
		SUA_IEI_CREDIT, SUA_IEI_SRC_ADDR, SUA_IEI_DATA,
		SUA_IEI_S7_HOP_CTR, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 4/Q.713 */
	[SCCP_MSG_TYPE_CC] = {
		SUA_IEI_CREDIT, SUA_IEI_SRC_ADDR, SUA_IEI_DATA,
		SUA_IEI_IMPORTANCE, 0
	},
	/* Table 5/Q.713 */
	[SCCP_MSG_TYPE_CREF] = {
		SUA_IEI_SRC_ADDR, SUA_IEI_DATA, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 6/Q.713 */
	[SCCP_MSG_TYPE_RLSD] = {
		SUA_IEI_DATA, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 7/Q.713 */
	[SCCP_MSG_TYPE_RLC] = {
		0
	},
	/* Table 8/Q.713 */
	[SCCP_MSG_TYPE_DT1] = {
		0
	},
	/* Table 9/Q.713 */
	[SCCP_MSG_TYPE_DT2] = {
		0
	},
	/* Table 10/Q.713 */
	[SCCP_MSG_TYPE_AK] = {
		0
	},
	/* Table 11/Q.713 */
	[SCCP_MSG_TYPE_UDT] = {
		0
	},
	/* Table 12/Q.713 */
	[SCCP_MSG_TYPE_UDTS] = {
		0
	},
	/* Table 13/Q.713 */
	[SCCP_MSG_TYPE_ED] = {
		0
	},
	/* Table 14/Q.713 */
	[SCCP_MSG_TYPE_EA] = {
		0
	},
	/* Table 15/Q.713 */
	[SCCP_MSG_TYPE_RSR] = {
		0
	},
	/* Table 16/Q.713 */
	[SCCP_MSG_TYPE_RSC] = {
		0
	},
	/* Table 17/Q.713 */
	[SCCP_MSG_TYPE_ERR] = {
		0
	},
	/* Table 18/Q.713 */
	[SCCP_MSG_TYPE_IT] = {
		0
	},
	/* Table 19/Q.713 */
	[SCCP_MSG_TYPE_XUDT] = {
		SUA_IEI_SEGMENTATION, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 20/Q.713 */
	[SCCP_MSG_TYPE_XUDTS] = {
		SUA_IEI_SEGMENTATION, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 21/Q.713 */
	[SCCP_MSG_TYPE_LUDT] = {
		SUA_IEI_SEGMENTATION, SUA_IEI_IMPORTANCE, 0
	},
	/* Table 22/Q.713 */
	[SCCP_MSG_TYPE_LUDTS] = {
		SUA_IEI_SEGMENTATION, SUA_IEI_IMPORTANCE, 0
	},
};


static bool sccp_is_mandatory(enum sccp_message_types type, const struct xua_msg_part *part)
{
	unsigned int i;

	if (type >= ARRAY_SIZE(sccp_mandatory))
		return false;

	for (i = 0; i < MAX_IES; i++) {
		uint16_t val = sccp_mandatory[type][i];
		if (val == 0) {
			/* end of list, don't iterate further */
			return false;
		}
		if (val == part->tag) {
			/* found in list, it's mandatory */
			return true;
		}
	}
	/* not mandatory */
	return false;
}

static bool sccp_option_permitted(enum sccp_message_types type, const struct xua_msg_part *part)
{
	unsigned int i;

	if (type >= ARRAY_SIZE(sccp_optional))
		return false;

	for (i = 0; i < MAX_IES; i++) {
		uint16_t val = sccp_optional[type][i];
		if (val == 0) {
			/* end of list, don't iterate further */
			return false;
		}
		if (val == part->tag) {
			/* found in list, it's permitted */
			return true;
		}
	}
	/* not permitted */
	return false;
}

static int xua_ies_to_sccp_opts(struct msgb *msg, uint8_t *ptr_opt, bool ptr_opt_is_long,
				enum sccp_message_types type, const struct xua_msg *xua)
{
	const struct xua_msg_part *part;
	bool any_added = false;
	uint8_t *old_tail = msg->tail;

	llist_for_each_entry(part, &xua->headers, entry) {
		/* make sure we don't add a SCCP option for information
		 * that is already present in mandatory fixed or
		 * mandatory variable parts of the header */
		if (!sccp_is_mandatory(type, part) && sccp_option_permitted(type, part)) {
			sccp_msg_add_sua_opt(type, msg, part);
			any_added = true;
		}
	}
	if (any_added) {
		msgb_put_u8(msg, SCCP_PNC_END_OF_OPTIONAL);
		/* store relative pointer to start of optional part */
		if (ptr_opt_is_long) /* -1: Distance from MSB of pointer */
			osmo_store16le(old_tail - ptr_opt - 1, ptr_opt);
		else
			*ptr_opt = old_tail - ptr_opt;
	} else {
		/* If nothing was added, simply update the pointer to 0 to signal the optional part is omitted. */
		ptr_opt[0] = 0;
		if (ptr_opt_is_long)
			ptr_opt[1] = 0;
	}

	return 0;
}

/* store a 'local reference' as big-endian 24bit value at local_ref */
static int store_local_ref(struct sccp_source_reference *local_ref, const struct xua_msg *xua, uint16_t iei)
{
	uint32_t tmp32 = xua_msg_get_u32(xua, iei);
	if (tmp32 > 0x00fffffe) {
		LOGP(DLSUA, LOGL_ERROR, "SUA->SCCP: Local Reference value 0x%" PRIx32 " > 0x00fffffe\n", tmp32);
		return -1;
	}
	local_ref->octet1 = (tmp32 >> 16) & 0xff;
	local_ref->octet2 = (tmp32 >> 8) & 0xff;
	local_ref->octet3 = tmp32 & 0xff;
	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_cr(const struct msgb *msg, struct xua_msg *xua)
{
	struct sccp_connection_request *req = (struct sccp_connection_request *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, req->proto_class);
	xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(&req->source_local_reference));
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, &req->variable_called))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, &req->variable_called, false);
	/* Optional Part */
	return sccp_to_xua_opt(msg, &req->optional_start, false, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static int sua_to_sccp_cr(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_connection_request *req;
	int rc;
	req = (struct sccp_connection_request *) msgb_put(msg, sizeof(*req));

	/* Fixed Part */
	req->type = SCCP_MSG_TYPE_CR;
	req->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	rc = store_local_ref(&req->source_local_reference, xua, SUA_IEI_SRC_REF);
	if (rc < 0)
		return rc;
	/* Variable Part */
	sccp_add_var_addr(msg, &req->variable_called, false, xua, SUA_IEI_DEST_ADDR);

	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &req->optional_start, false, req->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_cc(const struct msgb *msg, struct xua_msg *xua)
{
	struct sccp_connection_confirm *cnf = (struct sccp_connection_confirm *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, cnf->proto_class);
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&cnf->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(&cnf->source_local_reference));
	/* Optional Part */
	return sccp_to_xua_opt(msg, &cnf->optional_start, false, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static int sua_to_sccp_cc(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_connection_confirm *cnf;
	int rc;
	cnf = (struct sccp_connection_confirm *) msgb_put(msg, sizeof(*cnf));

	/* Fixed Part */
	cnf->type = SCCP_MSG_TYPE_CC;
	cnf->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	rc = store_local_ref(&cnf->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	rc = store_local_ref(&cnf->source_local_reference, xua, SUA_IEI_SRC_REF);
	if (rc < 0)
		return rc;
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &cnf->optional_start, false, cnf->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_cref(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_connection_refused *ref = (const struct sccp_connection_refused *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&ref->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_REFUSAL | ref->cause);
	/* Optional Part */
	return sccp_to_xua_opt(msg, &ref->optional_start, false, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static int sua_to_sccp_cref(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_connection_refused *ref;
	int rc;
	ref = (struct sccp_connection_refused *) msgb_put(msg, sizeof(*ref));

	/* Fixed Part */
	ref->type = SCCP_MSG_TYPE_CREF;
	rc = store_local_ref(&ref->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	ref->cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &ref->optional_start, false, ref->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_rlsd(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_connection_released *rlsd = (const struct sccp_connection_released *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&rlsd->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(&rlsd->source_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RELEASE | rlsd->release_cause);
	/* Optional Part */
	return sccp_to_xua_opt(msg, &rlsd->optional_start, false, xua);
}

static int sua_to_sccp_rlsd(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_connection_released *rlsd;
	int rc;
	rlsd =(struct sccp_connection_released *) msgb_put(msg, sizeof(*rlsd));

	/* Fixed Part */
	rlsd->type = SCCP_MSG_TYPE_RLSD;
	rc = store_local_ref(&rlsd->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	rc = store_local_ref(&rlsd->source_local_reference, xua, SUA_IEI_SRC_REF);
	if (rc < 0)
		return rc;
	rlsd->release_cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;

	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &rlsd->optional_start, false, rlsd->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_rlc(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_connection_release_complete *rlc;
	rlc = (const struct sccp_connection_release_complete *) msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&rlc->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(&rlc->source_local_reference));
	return xua;
}

static int sua_to_sccp_rlc(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_connection_release_complete *rlc;
	int rc;
	rlc = (struct sccp_connection_release_complete *) msgb_put(msg, sizeof(*rlc));

	/* Fixed Part */
	rlc->type = SCCP_MSG_TYPE_RLC;
	rc = store_local_ref(&rlc->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	rc = store_local_ref(&rlc->source_local_reference, xua, SUA_IEI_SRC_REF);
	if (rc < 0)
		return rc;
	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_dt1(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_form1 *dt1 = (const struct sccp_data_form1 *) msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&dt1->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_SEGMENTATION, dt1->segmenting);
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, &dt1->variable_start))
		return NULL;
	sccp_data_to_sua_ptr(xua, SUA_IEI_DATA, &dt1->variable_start);
	return xua;
}

static int sua_to_sccp_dt1(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_form1 *dt1;
	int rc;
	dt1 = (struct sccp_data_form1 *) msgb_put(msg, sizeof(*dt1));

	/* Fixed Part */
	dt1->type = SCCP_MSG_TYPE_DT1;
	rc = store_local_ref(&dt1->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	dt1->segmenting = xua_msg_get_u32(xua, SUA_IEI_SEGMENTATION);
	/* Variable Part */
	sccp_add_variable_part(msg, &dt1->variable_start, xua, SUA_IEI_DATA);
	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_udt(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_unitdata *udt = (const struct sccp_data_unitdata *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, udt->proto_class);
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, &udt->variable_called))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, &udt->variable_called, false);
	if (!sccp_ptr_part_consistent(msg, &udt->variable_calling))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, &udt->variable_calling, false);
	if (!sccp_ptr_part_consistent(msg, &udt->variable_data))
		return NULL;
	sccp_data_to_sua_ptr(xua, SUA_IEI_DATA, &udt->variable_data);
	return xua;

}

static int sua_to_sccp_xudt(struct msgb *msg, const struct xua_msg *xua);
static int sua_to_sccp_ludt(struct msgb *msg, const struct xua_msg *xua);

static int sua_to_sccp_udt(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_unitdata *udt;

	/* Use LUDT if length exceeds 255 (single byte length field) */
	/* TODO: start using LUDT sooner if called/calling party contain GT or if
	 * segmentation and/or importance present, see Q.715 Section 8.3.2 */
	if (xua_msg_get_len(xua, SUA_IEI_DATA) > 255)
		return sua_to_sccp_ludt(msg, xua);

	/* Use XUDT if we have a hop counter on the SUA side */
	if (xua_msg_find_tag(xua, SUA_IEI_S7_HOP_CTR))
		return sua_to_sccp_xudt(msg, xua);

	udt = (struct sccp_data_unitdata *) msgb_put(msg, sizeof(*udt));

	/* Fixed Part */
	udt->type = SCCP_MSG_TYPE_UDT;
	udt->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	/* Variable Part */
	sccp_add_var_addr(msg, &udt->variable_called, false, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, &udt->variable_calling, false, xua, SUA_IEI_SRC_ADDR);
	sccp_add_variable_part(msg, &udt->variable_data, xua, SUA_IEI_DATA);
	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_xudt(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_ext_unitdata *xudt = (const struct sccp_data_ext_unitdata *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, xudt->proto_class);
	xua_msg_add_u32(xua, SUA_IEI_S7_HOP_CTR, xudt->hop_counter);
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, &xudt->variable_called))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, &xudt->variable_called, false);
	if (!sccp_ptr_part_consistent(msg, &xudt->variable_calling))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, &xudt->variable_calling, false);
	if (!sccp_ptr_part_consistent(msg, &xudt->variable_data))
		return NULL;
	sccp_data_to_sua_ptr(xua, SUA_IEI_DATA, &xudt->variable_data);
	/* Optional Part */
	return sccp_to_xua_opt(msg, &xudt->optional_start, false, xua);

}

static int sua_to_sccp_xudt(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_ext_unitdata *xudt;

	/* Use LUDT if length exceeds 255 (single byte length field) */
	/* TODO: start using LUDTS sooner if called/calling party contain GT or if
	 * segmentation and/or importance present, see Q.715 Section 8.3.2 */
	if (xua_msg_get_len(xua, SUA_IEI_DATA) > 255)
		return sua_to_sccp_ludt(msg, xua);

	xudt = (struct sccp_data_ext_unitdata *) msgb_put(msg, sizeof(*xudt));

	/* Fixed Part */
	xudt->type = SCCP_MSG_TYPE_XUDT;
	xudt->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	xudt->hop_counter = xua_msg_get_u32(xua, SUA_IEI_S7_HOP_CTR);
	/* Variable Part */
	sccp_add_var_addr(msg, &xudt->variable_called, false, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, &xudt->variable_calling, false, xua, SUA_IEI_SRC_ADDR);
	sccp_add_variable_part(msg, &xudt->variable_data, xua, SUA_IEI_DATA);
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &xudt->optional_start, false, xudt->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_ludt(struct msgb *msg, struct xua_msg *xua)
{
	struct sccp_data_long_unitdata *ludt = (struct sccp_data_long_unitdata *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, ludt->proto_class);
	xua_msg_add_u32(xua, SUA_IEI_S7_HOP_CTR, ludt->hop_counter);
	/* Variable Part */
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludt->variable_called, false))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, (uint8_t *)&ludt->variable_called, true);
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludt->variable_calling, false))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, (uint8_t *)&ludt->variable_calling, true);
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludt->variable_data, true))
		return NULL;
	sccp_longdata_to_sua_ptr(xua, SUA_IEI_DATA, (uint8_t *)&ludt->variable_data);
	/* Optional Part */
	return sccp_to_xua_opt(msg, (uint8_t *)&ludt->optional_start, true, xua);
}

static int sua_to_sccp_ludt(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_long_unitdata *ludt;
	ludt = (struct sccp_data_long_unitdata *) msgb_put(msg, sizeof(*ludt));

	/* Fixed Part */
	ludt->type = SCCP_MSG_TYPE_LUDT;
	ludt->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	ludt->hop_counter = xua_msg_get_u32(xua, SUA_IEI_S7_HOP_CTR);
	/* Variable Part */
	sccp_add_var_addr(msg, (uint8_t *)&ludt->variable_called, true, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, (uint8_t *)&ludt->variable_calling, true, xua, SUA_IEI_SRC_ADDR);
	sccp_add_long_variable_part(msg, (uint8_t *)&ludt->variable_data, xua, SUA_IEI_DATA);
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, (uint8_t *)&ludt->optional_start, true, ludt->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_udts(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_unitdata_service *udts;
	udts = (const struct sccp_data_unitdata_service *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RETURN | udts->return_cause);
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, &udts->variable_called))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, &udts->variable_called, false);
	if (!sccp_ptr_part_consistent(msg, &udts->variable_calling))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, &udts->variable_calling, false);
	if (!sccp_ptr_part_consistent(msg, &udts->variable_data))
		return NULL;
	sccp_data_to_sua_ptr(xua, SUA_IEI_DATA, &udts->variable_data);
	return xua;

}

static int sua_to_sccp_xudts(struct msgb *msg, const struct xua_msg *xua);
static int sua_to_sccp_ludts(struct msgb *msg, const struct xua_msg *xua);

static int sua_to_sccp_udts(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_unitdata_service *udts;

	/* Use LUDTS if length exceeds 255 (single byte length field) */
	/* TODO: start using LUDTS sooner if called/calling party contain GT,
	 * see Q.715 Section 8.3.2 */
	if (xua_msg_get_len(xua, SUA_IEI_DATA) > 255)
		return sua_to_sccp_ludts(msg, xua);

	/* Use XUDTS if we have a hop counter */
	if (xua_msg_find_tag(xua, SUA_IEI_S7_HOP_CTR))
		return sua_to_sccp_xudts(msg, xua);

	udts = (struct sccp_data_unitdata_service *) msgb_put(msg, sizeof(*udts));

	/* Fixed Part */
	udts->type = SCCP_MSG_TYPE_UDTS;
	udts->return_cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;
	/* Variable Part */
	sccp_add_var_addr(msg, &udts->variable_called, false, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, &udts->variable_calling, false, xua, SUA_IEI_SRC_ADDR);
	sccp_add_variable_part(msg, &udts->variable_data, xua, SUA_IEI_DATA);
	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_xudts(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_ext_unitdata_service *xudts;
	xudts = (const struct sccp_data_ext_unitdata_service *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RETURN | xudts->return_cause);
	xua_msg_add_u32(xua, SUA_IEI_S7_HOP_CTR, xudts->hop_counter);
	/* Variable Part */
	if (!sccp_ptr_part_consistent(msg, (uint8_t *)&xudts->variable_called))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, &xudts->variable_called, false);
	if (!sccp_ptr_part_consistent(msg, (uint8_t *)&xudts->variable_calling))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, &xudts->variable_calling, false);
	if (!sccp_ptr_part_consistent(msg, (uint8_t *)&xudts->variable_data))
		return NULL;
	sccp_data_to_sua_ptr(xua, SUA_IEI_DATA, &xudts->variable_data);
	/* Optional Part */
	return sccp_to_xua_opt(msg, &xudts->optional_start, false, xua);
}

static int sua_to_sccp_xudts(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_ext_unitdata_service *xudts;
	xudts = (struct sccp_data_ext_unitdata_service *) msgb_put(msg, sizeof(*xudts));

	/* Fixed Part */
	xudts->type = SCCP_MSG_TYPE_XUDTS;
	xudts->return_cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;
	xudts->hop_counter = xua_msg_get_u32(xua, SUA_IEI_S7_HOP_CTR);
	/* Variable Part */
	sccp_add_var_addr(msg, &xudts->variable_called, false, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, &xudts->variable_calling, false, xua, SUA_IEI_SRC_ADDR);
	sccp_add_variable_part(msg, &xudts->variable_data, xua, SUA_IEI_DATA);
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, &xudts->optional_start, false, xudts->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_ludts(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_long_unitdata_service *ludts;
	ludts = (const struct sccp_data_long_unitdata_service *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_RETURN | ludts->return_cause);
	xua_msg_add_u32(xua, SUA_IEI_S7_HOP_CTR, ludts->hop_counter);
	/* Variable Part */
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludts->variable_called, false))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_DEST_ADDR, (uint8_t *)&ludts->variable_called, true);
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludts->variable_calling, false))
		return NULL;
	sccp_addr_to_sua_ptr(xua, SUA_IEI_SRC_ADDR, (uint8_t *)&ludts->variable_calling, true);
	if (!sccp_longptr_part_consistent(msg, (uint8_t *)&ludts->variable_data, true))
		return NULL;
	sccp_longdata_to_sua_ptr(xua, SUA_IEI_DATA, (uint8_t *)&ludts->variable_data);
	/* Optional Part */
	return sccp_to_xua_opt(msg, (uint8_t *)&ludts->optional_start, true, xua);
}

static int sua_to_sccp_ludts(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_long_unitdata_service *ludts;
	ludts = (struct sccp_data_long_unitdata_service *) msgb_put(msg, sizeof(*ludts));

	/* Fixed Part */
	ludts->type = SCCP_MSG_TYPE_LUDTS;
	ludts->return_cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;
	ludts->hop_counter = xua_msg_get_u32(xua, SUA_IEI_S7_HOP_CTR);
	/* Variable Part */
	sccp_add_var_addr(msg, (uint8_t *)&ludts->variable_called, true, xua, SUA_IEI_DEST_ADDR);
	sccp_add_var_addr(msg, (uint8_t *)&ludts->variable_calling, true, xua, SUA_IEI_SRC_ADDR);
	sccp_add_long_variable_part(msg, (uint8_t *)&ludts->variable_data, xua, SUA_IEI_DATA);
	/* Optional Part */
	return xua_ies_to_sccp_opts(msg, (uint8_t *)&ludts->optional_start, true, ludts->type, xua);
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_it(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_data_it *it = (const struct sccp_data_it *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, it->proto_class);
	xua_msg_add_u32(xua, SUA_IEI_SRC_REF, load_24be(&it->source_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&it->destination_local_reference));
	if ((it->proto_class & 0xF) == 3) {
		//xua_msg_add_u32(xua, SUA_IEI_SEQUENCING, it->sequencing);
		xua_msg_add_u32(xua, SUA_IEI_CREDIT, it->credit);
	}
	return xua;
}

static int sua_to_sccp_it(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_data_it *it;
	int rc;
	it = (struct sccp_data_it *) msgb_put(msg, sizeof(*it));

	/* Fixed Part */
	it->type = SCCP_MSG_TYPE_IT;
	it->proto_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	rc = store_local_ref(&it->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	rc = store_local_ref(&it->source_local_reference, xua, SUA_IEI_SRC_REF);
	if (rc < 0)
		return rc;
	if ((it->proto_class & 0xF) == 3) {
		//it->sequencing
		it->credit = xua_msg_get_u32(xua, SUA_IEI_CREDIT);
	}

	return 0;
}

/*! \returns \ref xua in case of success, NULL on error (xua not freed!) */
static struct xua_msg *sccp_to_xua_err(const struct msgb *msg, struct xua_msg *xua)
{
	const struct sccp_proto_err *err = (const struct sccp_proto_err *)msg->l2h;

	/* Fixed Part */
	xua_msg_add_u32(xua, SUA_IEI_DEST_REF, load_24be(&err->destination_local_reference));
	xua_msg_add_u32(xua, SUA_IEI_CAUSE, SUA_CAUSE_T_ERROR | err->error_cause);
	return xua;
}

static int sua_to_sccp_err(struct msgb *msg, const struct xua_msg *xua)
{
	struct sccp_proto_err *err;
	int rc;
	err = (struct sccp_proto_err *) msgb_put(msg, sizeof(*err));

	/* Fixed Part */
	err->type = SCCP_MSG_TYPE_ERR;
	rc = store_local_ref(&err->destination_local_reference, xua, SUA_IEI_DEST_REF);
	if (rc < 0)
		return rc;
	err->error_cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE) & 0xff;
	return 0;
}

/*! \brief convert SCCP message to a SUA message
 *  \param[in] msg message buffer holding SCCP message at l2h
 *  \returns callee-allocated xUA message on success; NULL on error */
struct xua_msg *osmo_sccp_to_xua(struct msgb *msg)
{
	struct xua_msg *xua;

	if (msgb_l2len(msg) < 1) {
		LOGP(DLSUA, LOGL_ERROR, "Short SCCP Message, cannot transcode\n");
		return NULL;
	}

	xua = xua_msg_alloc();
	if (!xua)
		return NULL;

	switch (msg->l2h[0]) {
	case SCCP_MSG_TYPE_CR:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_CORE);
		if (!sccp_to_xua_cr(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_CC:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_COAK);
		if (!sccp_to_xua_cc(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_CREF:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_COREF);
		if (!sccp_to_xua_cref(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_RLSD:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_RELRE);
		if (!sccp_to_xua_rlsd(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_RLC:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_RELCO);
		if (!sccp_to_xua_rlc(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_DT1:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_CODT);
		if (!sccp_to_xua_dt1(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_UDT:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDT);
		if (!sccp_to_xua_udt(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_UDTS:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDR);
		if (!sccp_to_xua_udts(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_IT:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_COIT);
		if (!sccp_to_xua_it(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_ERR:
		xua->hdr = XUA_HDR(SUA_MSGC_CO, SUA_CO_COERR);
		if (!sccp_to_xua_err(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_XUDT:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDT);
		if (!sccp_to_xua_xudt(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_XUDTS:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDR);
		if (!sccp_to_xua_xudts(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_LUDT:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDT);
		if (!sccp_to_xua_ludt(msg, xua))
			goto malformed;
		return xua;
	case SCCP_MSG_TYPE_LUDTS:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDR);
		if (!sccp_to_xua_ludts(msg, xua))
			goto malformed;
		return xua;
	/* Unsupported Message Types */
	case SCCP_MSG_TYPE_DT2:
	case SCCP_MSG_TYPE_AK:
	case SCCP_MSG_TYPE_ED:
	case SCCP_MSG_TYPE_EA:
	case SCCP_MSG_TYPE_RSR:
	case SCCP_MSG_TYPE_RSC:
		LOGP(DLSUA, LOGL_ERROR, "Unsupported SCCP message %s\n",
			osmo_sccp_msg_type_name(msg->l2h[0]));
		xua_msg_free(xua);
		return NULL;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unsupported SCCP message type %u\n",
			msg->l2h[0]);
		xua_msg_free(xua);
		return NULL;
	}

	return NULL;

malformed:
	LOGP(DLSUA, LOGL_ERROR, "Malformed SCCP message %s\n",
	     osmo_sccp_msg_type_name(msg->l2h[0]));
	xua_msg_free(xua);
	return NULL;
}

/*! \brief convert parsed SUA message to SCCP message
 *  \param[in] xua parsed SUA message to be converted
 *  \returns callee-allocated msgb containing encoded SCCP message */
struct msgb *osmo_sua_to_sccp(struct xua_msg *xua)
{
	struct msgb *msg = sccp_msgb_alloc("SCCP from SUA");
	int rc;

	switch (xua->hdr.msg_class) {
	case SUA_MSGC_CL:
		switch (xua->hdr.msg_type) {
		case SUA_CL_CLDT:
			rc = sua_to_sccp_udt(msg, xua);
			break;
		case SUA_CL_CLDR:
			rc = sua_to_sccp_udts(msg, xua);
			break;
		default:
			LOGP(DLSUA, LOGL_ERROR, "Unsupported SUA message %s\n",
				xua_hdr_dump(xua, &xua_dialect_sua));
			goto out_err;
		}
		break;
	case SUA_MSGC_CO:
		switch (xua->hdr.msg_type) {
		case SUA_CO_CORE:
			rc = sua_to_sccp_cr(msg, xua);
			break;
		case SUA_CO_COAK:
			rc = sua_to_sccp_cc(msg, xua);
			break;
		case SUA_CO_COREF:
			rc = sua_to_sccp_cref(msg, xua);
			break;
		case SUA_CO_RELRE:
			rc = sua_to_sccp_rlsd(msg, xua);
			break;
		case SUA_CO_RELCO:
			rc = sua_to_sccp_rlc(msg, xua);
			break;
		case SUA_CO_CODT:
			rc = sua_to_sccp_dt1(msg, xua);
			break;
		case SUA_CO_COIT:
			rc = sua_to_sccp_it(msg, xua);
			break;
		case SUA_CO_COERR:
			rc = sua_to_sccp_err(msg, xua);
			break;
		default:
			LOGP(DLSUA, LOGL_ERROR, "Unsupported SUA message %s\n",
				xua_hdr_dump(xua, &xua_dialect_sua));
			goto out_err;
		}
		break;
	default:
		LOGP(DLSUA, LOGL_ERROR, "Unsupported SUA message class %s\n",
			xua_hdr_dump(xua, &xua_dialect_sua));
		goto out_err;
	}

	if (rc < 0)  {
		LOGP(DLSUA, LOGL_ERROR, "Malformed SUA message %s\n",
			xua_hdr_dump(xua, &xua_dialect_sua));
		goto out_err;
	}

	return msg;

out_err:
	msgb_free(msg);
	return NULL;
}