Introduce CRC and FSM for IuUP (user plane) as used in 3G RTP data

Only support for SMpSDU mode is introduced in this commit.

Not supported explicit list:
- Transparent mode
- ATM/AAL2 based Transport layer
- GTP-U based Transport Layer
- Iu Rate Control procedure
- Time Alignment procedure

APIs are provided to allocate the primitives properly inside the related
msgb. This way primitives can be placed in the headroom, leaving the
data part of the msgb for the IuUP payload, hence allowing re-use of the
msgb and 0 copy of IuUP payload when forwarding data over RNL<->TNL.
Since RNL and TNL primitives relu struct osmo_prim_header, which is not
packed, they cannot be set to packed, and hence proper memory alignment
in the msgb must be done to avoid misaligned accesses (Asan errors about
it otherwise).

Related: SYS#5516
Change-Id: Ibe356fa7b1abaca0091e368db8478e79c09c6cb0
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 18ad3ff..7907734 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -136,7 +136,8 @@
 #define DLBSSGP		-21	/*!< Osmocom BSSGP layer */
 #define DLNSDATA	-22	/*!< Osmocom NS layer data pdus */
 #define DLNSSIGNAL	-23	/*!< Osmocom NS layer signal pdus */
-#define OSMO_NUM_DLIB	23	/*!< Number of logging sub-systems in libraries */
+#define DLIUUP		-24	/*!< Osmocom IuUP layer */
+#define OSMO_NUM_DLIB	24	/*!< Number of logging sub-systems in libraries */
 
 /* Colors that can be used in log_info_cat.color */
 #define OSMO_LOGCOLOR_NORMAL NULL
diff --git a/include/osmocom/gsm/iuup.h b/include/osmocom/gsm/iuup.h
new file mode 100644
index 0000000..b1651c5
--- /dev/null
+++ b/include/osmocom/gsm/iuup.h
@@ -0,0 +1,122 @@
+#pragma once
+
+#include <stdint.h>
+
+#include <osmocom/core/prim.h>
+#include <osmocom/gsm/protocol/gsm_25_415.h>
+
+/***********************************************************************
+ * Primitives towards the lower layers (typically RTP transport)
+ ***********************************************************************/
+enum osmo_iuup_tnl_prim_type {
+	OSMO_IUUP_TNL_UNITDATA,
+};
+
+struct osmo_iuup_tnl_prim {
+	struct osmo_prim_hdr oph;
+};
+
+/***********************************************************************
+ * Primitives towards the upper layers at the RNL SAP
+ ***********************************************************************/
+
+/* 3GPP TS 25.415 Section 7.2.1 */
+enum osmo_iuup_rnl_prim_type {
+	OSMO_IUUP_RNL_CONFIG,
+	OSMO_IUUP_RNL_DATA,
+	OSMO_IUUP_RNL_STATUS,
+	OSMO_IUUP_RNL_UNIT_DATA,
+};
+
+/* TS 25.413 9.2.1.3*/
+#define IUUP_MAX_SUBFLOWS 7
+#define IUUP_MAX_RFCIS 64
+
+#define IUUP_TIMER_INIT_T_DEFAULT 1000
+#define IUUP_TIMER_TA_T_DEFAULT 500
+#define IUUP_TIMER_RC_T_DEFAULT 500
+#define IUUP_TIMER_INIT_N_DEFAULT 3
+#define IUUP_TIMER_TA_N_DEFAULT 1
+#define IUUP_TIMER_RC_N_DEFAULT 1
+struct osmo_iuup_rnl_config_timer {
+	uint32_t t_ms;	/* time in ms */
+	uint32_t n_max;	/* max number of repetitions */
+};
+struct osmo_iuup_rnl_config {
+	/* transparent (true) or SMpSDU (false): */
+	bool transparent;
+
+	/* should we actively transmit INIT in SmpSDU mode? */
+	bool active;
+
+	/* Currently Version 0 or 1: */
+	uint8_t data_pdu_type;
+
+	/* Supported mode versions */
+	uint16_t supported_versions_mask;
+	uint8_t num_rfci;
+	uint8_t num_subflows;
+	uint16_t subflow_sizes[IUUP_MAX_RFCIS][IUUP_MAX_SUBFLOWS];
+	bool IPTIs_present;
+	uint8_t IPTIs[IUUP_MAX_RFCIS]; /* values range 0-15, 4 bits */
+
+	/* TODO: Indication of delivery of erroneous SDUs*/
+	struct osmo_iuup_rnl_config_timer t_init;
+	struct osmo_iuup_rnl_config_timer t_ta;
+	struct osmo_iuup_rnl_config_timer t_rc;
+};
+
+struct osmo_iuup_rnl_data {
+	uint8_t rfci;
+	uint8_t frame_nr;
+	uint8_t fqc;
+};
+
+struct osmo_iuup_rnl_status {
+	enum iuup_procedure procedure;
+	union {
+		struct {
+			enum iuup_error_cause cause;
+			enum iuup_error_distance distance;
+		} error_event;
+		struct {
+			uint16_t supported_versions_mask;
+			uint8_t num_subflows;
+			uint8_t num_rfci;
+			uint16_t subflow_sizes[IUUP_MAX_RFCIS][IUUP_MAX_SUBFLOWS];
+			bool IPTIs_present;
+			uint8_t IPTIs[IUUP_MAX_RFCIS]; /* values range 0-15, 4 bits */
+		} initialization;
+		struct {
+		} rate_control;
+		struct {
+		} time_alignment;
+	} u;
+};
+
+/* SAP on the upper side of IuUP, towards the user */
+struct osmo_iuup_rnl_prim {
+	struct osmo_prim_hdr oph;
+	union {
+		struct osmo_iuup_rnl_config config;
+		struct osmo_iuup_rnl_data data;
+		struct osmo_iuup_rnl_status status;
+		//struct osmo_iuup_rnl_unitdata unitdata;
+	} u;
+};
+
+struct osmo_iuup_instance;
+struct osmo_iuup_instance *osmo_iuup_instance_alloc(void *ctx, const char *id);
+void osmo_iuup_instance_free(struct osmo_iuup_instance *iui);
+
+void osmo_iuup_instance_set_user_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv);
+void osmo_iuup_instance_set_transport_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv);
+int osmo_iuup_tnl_prim_up(struct osmo_iuup_instance *iui, struct osmo_iuup_tnl_prim *itp);
+int osmo_iuup_rnl_prim_down(struct osmo_iuup_instance *inst, struct osmo_iuup_rnl_prim *irp);
+
+
+int osmo_iuup_compute_header_crc(const uint8_t *iuup_pdu, unsigned int pdu_len);
+int osmo_iuup_compute_payload_crc(const uint8_t *iuup_pdu, unsigned int pdu_len);
+
+struct osmo_iuup_rnl_prim *osmo_iuup_rnl_prim_alloc(void *ctx, unsigned int primitive, unsigned int operation, unsigned int size);
+struct osmo_iuup_tnl_prim *osmo_iuup_tnl_prim_alloc(void *ctx, unsigned int primitive, unsigned int operation, unsigned int size);
diff --git a/include/osmocom/gsm/prim.h b/include/osmocom/gsm/prim.h
index 045e353..73cd7f7 100644
--- a/include/osmocom/gsm/prim.h
+++ b/include/osmocom/gsm/prim.h
@@ -18,4 +18,7 @@
 	SAP_NS,
 
 	SAP_BSSGP_RIM,
+
+	SAP_IUUP_TNL,
+	SAP_IUUP_RNL,
 };
diff --git a/include/osmocom/gsm/protocol/gsm_25_415.h b/include/osmocom/gsm/protocol/gsm_25_415.h
new file mode 100644
index 0000000..5d90fd3
--- /dev/null
+++ b/include/osmocom/gsm/protocol/gsm_25_415.h
@@ -0,0 +1,222 @@
+#pragma once
+/* Iu User Plane (IuUP) Definitions as per 3GPP TS 25.415 */
+/* (C) 2017 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <stdint.h>
+#include <osmocom/core/endian.h>
+
+/* 3GPP TS 25.415 Section 6.6.2.1 */
+struct iuup_pdutype0_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+	/* control part */
+	uint8_t frame_nr:4,
+		pdu_type:4;
+	uint8_t rfci:6,
+		fqc:2;
+	/* checksum part */
+	uint8_t payload_crc_hi:2, header_crc:6;
+	uint8_t payload_crc_lo;
+
+	/* payload part */
+	uint8_t payload[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint8_t pdu_type:4, frame_nr:4;
+	uint8_t fqc:2, rfci:6;
+	uint8_t header_crc:6, payload_crc_hi:2;
+	uint8_t payload_crc_lo;
+	uint8_t payload[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.2 */
+struct iuup_pdutype1_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+	/* control part */
+	uint8_t frame_nr:4,
+		pdu_type:4;
+	uint8_t rfci:6,
+		fqc:2;
+	/* checksum part */
+	uint8_t spare:2,
+		header_crc:6;
+	/* payload part */
+	uint8_t payload[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint8_t pdu_type:4, frame_nr:4;
+	uint8_t fqc:2, rfci:6;
+	uint8_t header_crc:6, spare:2;
+	uint8_t payload[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.3 */
+struct iuup_pdutype14_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+	/* control part */
+	uint8_t frame_nr:2,
+		ack_nack:2,
+		pdu_type:4;
+	uint8_t proc_ind:4,
+		mode_version:4;
+	/* checksum part */
+	uint8_t payload_crc_hi:2, header_crc:6;
+	uint8_t payload_crc_lo;
+	/* payload part */
+	uint8_t payload[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint8_t pdu_type:4, ack_nack:2, frame_nr:2;
+	uint8_t mode_version:4, proc_ind:4;
+	uint8_t header_crc:6, payload_crc_hi:2;
+	uint8_t payload_crc_lo;
+	uint8_t payload[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.3.4.1 */
+struct iuup_ctrl_init_rfci_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+	uint8_t rfci:6,
+		li:1,
+		lri:1;
+	uint8_t subflow_length[0]; /* 1 or 2 bytes depending on li */
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint8_t lri:1, li:1, rfci:6;
+	uint8_t subflow_length[0];
+#endif
+} __attribute__((packed));
+struct iuup_ctrl_init_hdr {
+#if OSMO_IS_LITTLE_ENDIAN
+	uint8_t chain_ind:1,
+		num_subflows_per_rfci:3,
+		ti:1,
+		spare:3;
+	uint8_t rfci_data[0]; /* struct iuup_ctrl_init_rfci_hdr* */
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint8_t spare:3, ti:1, num_subflows_per_rfci:3, chain_ind:1;
+	uint8_t rfci_data[0]; /* struct iuup_ctrl_init_rfci_hdr* */
+;
+#endif
+} __attribute__((packed));
+struct iuup_ctrl_init_tail {
+#if OSMO_IS_LITTLE_ENDIAN
+	uint16_t versions_supported;
+	uint8_t spare:4,
+		data_pdu_type:4;
+	uint8_t spare_extension[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	uint16_t versions_supported;
+	uint8_t data_pdu_type:4, spare:4;
+	uint8_t spare_extension[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.3.4.4 */
+struct iuup_ctrl_error_event {
+#if OSMO_IS_LITTLE_ENDIAN
+	struct iuup_pdutype14_hdr hdr;
+	uint8_t error_cause:6,
+		error_distance:2;
+	uint8_t spare_extension[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	struct iuup_pdutype14_hdr hdr;
+	uint8_t error_distance:2, error_cause:6;
+	uint8_t spare_extension[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.3.2 */
+struct iuup_ctrl_ack {
+	struct iuup_pdutype14_hdr hdr;
+	uint8_t spare_extension[0];
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2.3.3 */
+struct iuup_ctrl_nack {
+#if OSMO_IS_LITTLE_ENDIAN
+	struct iuup_pdutype14_hdr hdr;
+	uint8_t spare:2,
+		error_cause:6;
+	uint8_t spare_extension[0];
+#elif OSMO_IS_BIG_ENDIAN
+/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
+	struct iuup_pdutype14_hdr hdr;
+	uint8_t error_cause:6, spare:2;
+	uint8_t spare_extension[0];
+#endif
+} __attribute__((packed));
+
+/* 3GPP TS 25.415 Section 6.6.2 + 6.6.3.1 */
+enum iuup_pdu_type {
+	IUUP_PDU_T_DATA_CRC	= 0,
+	IUUP_PDU_T_DATA_NOCRC	= 1,
+	IUUP_PDU_T_CONTROL	= 14,
+};
+
+/* 3GPP TS 25.415 Section 6.6.3.2 */
+enum iuup_ack_nack {
+	IUUP_AN_PROCEDURE	= 0,
+	IUUP_AN_ACK		= 1,
+	IUUP_AN_NACK		= 2,
+};
+
+/* 3GPP TS 25.415 Section 6.6.3.5 */
+enum iuup_fqc {
+	IUUP_FQC_FRAME_GOOD	= 0,
+	IUUP_FQC_FRAME_BAD	= 1,
+	IUUP_FQC_FRAME_BAD_RADIO = 2,
+};
+
+/* 3GPP TS 25.415 Section 6.6.3.7 */
+enum iuup_procedure {
+	IUUP_PROC_INIT		= 0,
+	IUUP_PROC_RATE_CTRL	= 1,
+	IUUP_PROC_TIME_ALIGN	= 2,
+	IUUP_PROC_ERR_EVENT	= 3,
+};
+
+
+/* 3GPP TS 25.415 Section 6.6.3.15 */
+enum iuup_error_distance {
+	IUUP_ERR_DIST_LOCAL		= 0,
+	IUUP_ERR_DIST_FIRST_FWD		= 1,
+	IUUP_ERR_DIST_SECOND_FWD	= 2,
+	IUUP_ERR_DIST_RESERVED		= 3,
+};
+
+
+/* 3GPP TS 25.415 Section 6.6.3.16 */
+enum iuup_error_cause {
+	IUUP_ERR_CAUSE_CRC_ERR_HDR	= 0,
+	IUUP_ERR_CAUSE_CRC_ERR_DATA	= 1,
+	IUUP_ERR_CAUSE_UNEXPECTED_FN	= 2,
+	IUUP_ERR_CAUSE_FRAME_LOSS	= 3,
+	IUUP_ERR_CAUSE_UNKNOWN_PDUTYPE	= 4,
+	IUUP_ERR_CAUSE_UNKNOWN_PROC	= 5,
+	IUUP_ERR_CAUSE_UNKNNOWN_RES_VAL	= 6,
+	IUUP_ERR_CAUSE_UNKNNOWN_FIELD	= 7,
+	IUUP_ERR_CAUSE_FRAME_TOO_SHORT	= 8,
+	IUUP_ERR_CAUSE_MISSING_FIELDS	= 9,
+	IUUP_ERR_CAUSE_UNEXPECTED_PDU_T	= 16,
+	IUUP_ERR_CAUSE_UNEXPECTED_PROC	= 18,
+	IUUP_ERR_CAUSE_UNEXPECTED_RFCI	= 19,
+	IUUP_ERR_CAUSE_UNEXPECTED_VALUE	= 20,
+	IUUP_ERR_CAUSE_INIT_FAILURE	= 42,
+	IUUP_ERR_CAUSE_INIT_FAILURE_NET_TMR = 43,
+	IUUP_ERR_CAUSE_INIT_FAILURE_REP_NACK = 44,
+	IUUP_ERR_CAUSE_RATE_CTRL_FAILURE = 45,
+	IUUP_ERR_CAUSE_ERR_EVENT_FAIL	= 46,
+	IUUP_ERR_CAUSE_TIME_ALIGN_NOTSUPP = 47,
+	IUUP_ERR_CAUSE_REQ_TIME_ALIGN_NOTPOSS = 48,
+	IUUP_ERR_CAUSE_MODE_VERSION_NOT_SUPPORTED = 49,
+};