respect the link_id, not only the SAPI from SMS code

SMS related messages are all sent over SAPI=3.  But in addition
to that, we also need to send it over the correct link identifier,
i.e. SACCH or main signalling channel
diff --git a/openbsc/include/openbsc/gsm_04_08.h b/openbsc/include/openbsc/gsm_04_08.h
index 75d93fb..1ca1ccd 100644
--- a/openbsc/include/openbsc/gsm_04_08.h
+++ b/openbsc/include/openbsc/gsm_04_08.h
@@ -691,7 +691,7 @@
 void gsm0408_allow_everyone(int allow);
 void gsm0408_set_reject_cause(int cause);
 
-int gsm0408_rcvmsg(struct msgb *msg);
+int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id);
 void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, 
 		u_int16_t mnc, u_int16_t lac);
 enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra);
diff --git a/openbsc/include/openbsc/gsm_04_11.h b/openbsc/include/openbsc/gsm_04_11.h
index dd6a263..bfa90b5 100644
--- a/openbsc/include/openbsc/gsm_04_11.h
+++ b/openbsc/include/openbsc/gsm_04_11.h
@@ -203,7 +203,7 @@
 
 struct msgb;
 
-int gsm0411_rcv_sms(struct msgb *msg);
+int gsm0411_rcv_sms(struct msgb *msg, u_int8_t link_id);
 
 int gsm411_send_sms_lchan(struct gsm_lchan *lchan, struct gsm_sms *sms);
 
diff --git a/openbsc/include/openbsc/transaction.h b/openbsc/include/openbsc/transaction.h
index 466e88a..961a649 100644
--- a/openbsc/include/openbsc/transaction.h
+++ b/openbsc/include/openbsc/transaction.h
@@ -39,6 +39,7 @@
 			struct gsm_mncc msg;	/* stores setup/disconnect/release message */
 		} cc;
 		struct {
+			u_int8_t link_id;	/* RSL Link ID to be used for this trans */
 			int is_mt;	/* is this a MO (0) or MT (1) transfer */
 			enum gsm411_cp_state cp_state;
 			struct timer_list cp_timer;
diff --git a/openbsc/src/abis_rsl.c b/openbsc/src/abis_rsl.c
index d54f2fd..8cec2b6 100644
--- a/openbsc/src/abis_rsl.c
+++ b/openbsc/src/abis_rsl.c
@@ -923,7 +923,7 @@
 	if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
 		DEBUGPC(DMEAS, "L3\n");
 		msg->l3h = TLVP_VAL(&tp, RSL_IE_L3_INFO);
-		return gsm0408_rcvmsg(msg);
+		return gsm0408_rcvmsg(msg, 0);
 	} else
 		DEBUGPC(DMEAS, "\n");
 
@@ -1206,10 +1206,12 @@
 	struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
 	int rc = 0;
 	char *ts_name;
+	u_int8_t sapi = rllh->link_id & 7;
 
 	msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
 	ts_name = gsm_ts_name(msg->lchan->ts);
-	DEBUGP(DRLL, "channel=%s chan_nr=0x%02x ", ts_name, rllh->chan_nr);
+	DEBUGP(DRLL, "channel=%s chan_nr=0x%02x sapi=%u ", ts_name,
+		rllh->chan_nr, sapi);
 	
 	switch (rllh->c.msg_type) {
 	case RSL_MT_DATA_IND:
@@ -1218,7 +1220,7 @@
 		    sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
 		    rllh->data[0] == RSL_IE_L3_INFO) {
 			msg->l3h = &rllh->data[3];
-			return gsm0408_rcvmsg(msg);
+			return gsm0408_rcvmsg(msg, rllh->link_id);
 		}
 		break;
 	case RSL_MT_EST_IND:
@@ -1229,7 +1231,7 @@
 		    sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
 		    rllh->data[0] == RSL_IE_L3_INFO) {
 			msg->l3h = &rllh->data[3];
-			return gsm0408_rcvmsg(msg);
+			return gsm0408_rcvmsg(msg, rllh->link_id);
 		}
 		break;
 	case RSL_MT_EST_CONF:
diff --git a/openbsc/src/gsm_04_08.c b/openbsc/src/gsm_04_08.c
index 3e6c149..dc2bd19 100644
--- a/openbsc/src/gsm_04_08.c
+++ b/openbsc/src/gsm_04_08.c
@@ -3745,7 +3745,7 @@
 }
 
 /* here we pass in a msgb from the RSL->RLL.  We expect the l3 pointer to be set */
-int gsm0408_rcvmsg(struct msgb *msg)
+int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
 {
 	struct gsm48_hdr *gh = msgb_l3(msg);
 	u_int8_t pdisc = gh->proto_discr & 0x0f;
@@ -3762,7 +3762,7 @@
 		rc = gsm0408_rcv_rr(msg);
 		break;
 	case GSM48_PDISC_SMS:
-		rc = gsm0411_rcv_sms(msg);
+		rc = gsm0411_rcv_sms(msg, link_id);
 		break;
 	case GSM48_PDISC_MM_GPRS:
 	case GSM48_PDISC_SM_GPRS:
diff --git a/openbsc/src/gsm_04_11.c b/openbsc/src/gsm_04_11.c
index bebd07d..9580b07 100644
--- a/openbsc/src/gsm_04_11.c
+++ b/openbsc/src/gsm_04_11.c
@@ -139,7 +139,7 @@
 				   "GSM 04.11");
 }
 
-static int gsm411_sendmsg(struct msgb *msg)
+static int gsm411_sendmsg(struct msgb *msg, u_int8_t link_id)
 {
 	if (msg->lchan)
 		msg->trx = msg->lchan->ts->trx;
@@ -148,7 +148,7 @@
 
 	DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len));
 
-	return rsl_data_request(msg, UM_SAPI_SMS);
+	return rsl_data_request(msg, link_id);
 }
 
 /* SMC TC1* is expired */
@@ -197,7 +197,7 @@
 
 	DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id);
 
-	return gsm411_sendmsg(msg);
+	return gsm411_sendmsg(msg, trans->sms.link_id);
 }
 
 /* Prefix msg with a RP-DATA header and send as CP-DATA */
@@ -645,7 +645,7 @@
 	if (sms)
 		gsm411_send_sms_lchan(msg->lchan, sms);
 	else
-		rsl_release_request(msg->lchan, UM_SAPI_SMS);
+		rsl_release_request(msg->lchan, trans->sms.link_id);
 
 	return 0;
 }
@@ -710,7 +710,7 @@
 	if (sms)
 		gsm411_send_sms_lchan(msg->lchan, sms);
 	else
-		rsl_release_request(msg->lchan, UM_SAPI_SMS);
+		rsl_release_request(msg->lchan, trans->sms.link_id);
 
 	return rc;
 }
@@ -784,7 +784,7 @@
 }
 
 /* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */
-int gsm0411_rcv_sms(struct msgb *msg)
+int gsm0411_rcv_sms(struct msgb *msg, u_int8_t link_id)
 {
 	struct gsm48_hdr *gh = msgb_l3(msg);
 	u_int8_t msg_type = gh->msg_type;
@@ -812,6 +812,7 @@
 		trans->sms.cp_state = GSM411_CPS_IDLE;
 		trans->sms.rp_state = GSM411_RPS_IDLE;
 		trans->sms.is_mt = 0;
+		trans->sms.link_id = link_id;
 
 		trans->lchan = lchan;
 		use_lchan(lchan);
@@ -826,12 +827,17 @@
 		 * CP-DATA, including sending of the assoc. CP-ACK */
 		trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
 
+		/* SMC instance acknowledges the CP-DATA frame */
+		gsm411_tx_cp_ack(trans);
+		
 		rc = gsm411_rx_cp_data(msg, gh, trans);
+#if 0
 		/* Send CP-ACK or CP-ERORR in response */
 		if (rc < 0) {
 			rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
 		} else
 			rc = gsm411_tx_cp_ack(trans);
+#endif
 		break;
 	case GSM411_MT_CP_ACK:
 		/* previous CP-DATA in this transaction was confirmed */
@@ -908,6 +914,7 @@
 	trans->sms.rp_state = GSM411_RPS_IDLE;
 	trans->sms.is_mt = 1;
 	trans->sms.sms = sms;
+	trans->sms.link_id = UM_SAPI_SMS;	/* FIXME: main or SACCH ? */
 
 	trans->lchan = lchan;
 	use_lchan(lchan);