lapdm: Append RSL_IE_OSMO_ABS_FRAME_NUMBER to RSLms msgs towards upper layers

This makes it possible to track GSM time in the upper layers.
The existing RSL_IE_FRAME_NUMBER and RSL_IE_STARTNG_TIME cannot be used
there, since those are 16bit fields containing Relative FN values.

The IE needs to be added before the L3_INFO one, because user code
usually assumes the msgb->l3 pointing to L3_INFO value extends until the
end of the message, using msgb3_len(msg). Regarding having an extra IE
at the middle, it's not a big problem since the libosmocore version
submitting this commit to the upper layers is the same which will also
be parsing it through rsl_tlv_parse() later on by the app.

Related: OS#3626
Change-Id: Id62c18f49f270449067b25b7104eb8b47f1955ec
diff --git a/include/osmocom/gsm/rsl.h b/include/osmocom/gsm/rsl.h
index 285dbc2..b9d3282 100644
--- a/include/osmocom/gsm/rsl.h
+++ b/include/osmocom/gsm/rsl.h
@@ -51,6 +51,8 @@
 /* Push a RSL RLL header with L3_INFO IE */
 void rsl_rll_push_l3(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
 		     uint8_t link_id, int transparent);
+void rsl_rll_push_l3_with_fn(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
+		     uint8_t link_id, int transparent, uint32_t fn);
 
 /* Allocate msgb and fill with simple RSL RLL header */
 struct msgb *rsl_rll_simple(uint8_t msg_type, uint8_t chan_nr,
diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c
index e96e218..c259824 100644
--- a/src/gsm/lapdm.c
+++ b/src/gsm/lapdm.c
@@ -503,7 +503,7 @@
 	int transparent = rsl_is_transparent(msg_type);
 
 	/* Add the RSL + RLL header */
-	rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
+	rsl_rll_push_l3_with_fn(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent, mctx->fn);
 
 	/* send off the RSLms message to L3 */
 	return rslms_sendmsg(msg, mctx->dl->entity);
@@ -513,10 +513,14 @@
 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
 {
 	uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
+	uint32_t fn_be;
 
 	/* Add the RSL + RLL header */
 	msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
 
+	osmo_store32be(mctx->fn, &fn_be);
+	msgb_tlv_push(msg, RSL_IE_OSMO_ABS_FRAME_NUMBER, 4, (uint8_t *)&fn_be);
+
 	/* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
 	if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
 		msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
@@ -533,8 +537,12 @@
 {
 	struct msgb *msg;
 	int transparent = rsl_is_transparent(msg_type);
+	uint32_t fn_be;
 
 	msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
+	/* Add FN to the end to keep backward compat: */
+	osmo_store32be(mctx->fn, &fn_be);
+	msgb_tlv_put(msg, RSL_IE_OSMO_ABS_FRAME_NUMBER, 4, (uint8_t *)&fn_be);
 
 	/* send off the RSLms message to L3 */
 	return rslms_sendmsg(msg, mctx->dl->entity);
@@ -543,10 +551,15 @@
 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
 {
 	struct msgb *msg;
+	uint32_t fn_be;
 
 	LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
 	msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
 	msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
+	/* Add FN to the end to keep backward compat: */
+	osmo_store32be(mctx->fn, &fn_be);
+	msgb_tlv_put(msg, RSL_IE_OSMO_ABS_FRAME_NUMBER, 4, (uint8_t *)&fn_be);
+
 	return rslms_sendmsg(msg, mctx->dl->entity);
 }
 
diff --git a/src/gsm/rsl.c b/src/gsm/rsl.c
index 53eb983..e0708c1 100644
--- a/src/gsm/rsl.c
+++ b/src/gsm/rsl.c
@@ -561,6 +561,33 @@
 	rsl_rll_push_hdr(msg, msg_type, chan_nr, link_id, transparent);
 }
 
+/*! Wrap msgb in L3 Info IE and push a RSL RLL header
+ *  \param[in] msg Message Buffer to which L3 Header shall be appended
+ *  \param[in] msg_type RSL Message Type
+ *  \param[in] chan_hr RSL Channel Number
+ *  \param[in] link_id Link Identififer
+ *  \param[in] transparent Transparent to BTS (1) or not (0)
+ *  \param[in] fn Frame Number
+ */
+void rsl_rll_push_l3_with_fn(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
+		     uint8_t link_id, int transparent, uint32_t fn)
+{
+	uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
+	uint32_t fn_be;
+
+	/* construct a RSLms RLL message (DATA INDICATION, UNIT DATA
+	 * INDICATION) and send it off via RSLms */
+
+	/* Push the L3 IE tag and length */
+	msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
+
+	osmo_store32be(fn, &fn_be);
+	msgb_tlv_push(msg, RSL_IE_OSMO_ABS_FRAME_NUMBER, 4, (uint8_t *)&fn_be);
+
+	/* Then push the RSL header */
+	rsl_rll_push_hdr(msg, msg_type, chan_nr, link_id, transparent);
+}
+
 /*! Create msgb with RSL RLL header
  *  \param[in] msg_type RSL Message Type
  *  \param[in] chan_nr RSL Channel Number
diff --git a/tests/lapd/lapd_test.c b/tests/lapd/lapd_test.c
index 2f2a7f4..bae73c2 100644
--- a/tests/lapd/lapd_test.c
+++ b/tests/lapd/lapd_test.c
@@ -359,7 +359,7 @@
 		struct abis_rsl_rll_hdr hdr;
 
 		printf("MS: Verifying incoming primitive.\n");
-		OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr));
+		OSMO_ASSERT(msg->len >= sizeof(struct abis_rsl_rll_hdr));
 
 		/* verify the header */
 		memset(&hdr, 0, sizeof(hdr));
diff --git a/tests/lapd/lapd_test.ok b/tests/lapd/lapd_test.ok
index 065886c..ff9df5b 100644
--- a/tests/lapd/lapd_test.ok
+++ b/tests/lapd/lapd_test.ok
@@ -1,28 +1,28 @@
 I do some very simple LAPDm test.
 Establishing link.
 ms_to_bts_l1_cb: MS(us) -> BTS prim message
-bts_to_ms_tx_cb: MS->BTS(us) message 25
+bts_to_ms_tx_cb: MS->BTS(us) message 31
 BTS: Verifying CM request.
 
 Confirming
 lapdm_phsap_dequeue_prim(): got rc 0: Success
 Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x00
 Message: [L2]> 01 73 41 [L3]> 05 24 31 03 50 18 93 08 29 47 80 00 00 00 00 80 2b 2b 2b 2b 
-ms_to_bts_tx_cb: BTS->MS(us) message 6
+ms_to_bts_tx_cb: BTS->MS(us) message 12
 MS: Verifying incoming primitive.
 
 Sending back to MS
 lapdm_phsap_dequeue_prim(): got rc 0: Success
 Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x00
 Message: [L2]> 03 00 0d [L3]> 05 04 0d 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
-ms_to_bts_tx_cb: BTS->MS(us) message 12
+ms_to_bts_tx_cb: BTS->MS(us) message 18
 MS: Verifying incoming MM message: 3
 ms_to_bts_l1_cb: MS(us) -> BTS prim message
 lapdm_phsap_dequeue_prim(): got rc -19: No such device
 
 Sending back to BTS
 ms_to_bts_l1_cb: MS(us) -> BTS prim message
-bts_to_ms_tx_cb: MS->BTS(us) message 14
+bts_to_ms_tx_cb: MS->BTS(us) message 20
 BTS: Verifying dummy message.
 lapdm_phsap_dequeue_prim(): got rc 0: Success
 MSGB: L3 is undefined
@@ -32,7 +32,7 @@
 lapdm_phsap_dequeue_prim(): got rc -19: No such device
 I test RF channel release of an unestablished channel.
 I test contention resultion by having two mobiles collide and first mobile repeating SABM.
-bts_to_ms_tx_cb: MS->BTS(us) message 25
+bts_to_ms_tx_cb: MS->BTS(us) message 31
 BTS: Verifying CM request.
 lapdm_phsap_dequeue_prim(): got rc 0: Success
 Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x00
@@ -60,7 +60,7 @@
 I test if desync problems exist in LAPDm
 
 Establishing SAPI=0
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 25
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 31
 
 Dumping queue:
 00 00 17 [L2]> 01 73 41 [L3]> 05 24 31 03 50 18 93 08 29 47 80 00 00 00 00 80 
@@ -70,7 +70,7 @@
 Message: [L2]> 01 73 41 [L3]> 05 24 31 03 50 18 93 08 29 47 80 00 00 00 00 80 2b 2b 2b 2b 
 
 Sending Classmark Change
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 27
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 33
 
 Dumping queue:
 00 00 17 [L2]> 01 21 01 
@@ -87,7 +87,7 @@
 
 
 Sending GPRS Suspend Request
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 22
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 28
 
 Dumping queue:
 00 00 17 [L2]> 03 40 0d [L3]> 06 35 01 
@@ -97,7 +97,7 @@
 Message: [L2]> 03 40 0d [L3]> 06 35 01 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
 
 Sending Cipher Mode Complete
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 11
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 17
 
 Dumping queue:
 00 00 17 [L2]> 01 61 01 
@@ -108,7 +108,7 @@
 Message: [L2]> 01 61 01 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
 
 Establishing SAPI=3
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 6
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 12
 
 Dumping queue:
 
@@ -128,7 +128,7 @@
 === I test SAPI0/SAPI3 prioritization ===
 
 MS is establishing a SAPI=0 link
-bts_to_ms_dummy_tx_cb: MS->BTS(us) message 22
+bts_to_ms_dummy_tx_cb: MS->BTS(us) message 28
 BTS is establishing a SAPI=3 link
 lapdm_phsap_dequeue_prim(): got rc 0: Success
 Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x00