no reasonable commit message possible. tons of changes of the last 2 days

diff --git a/src/abis_rsl.c b/src/abis_rsl.c
index f503684..79f83ab 100644
--- a/src/abis_rsl.c
+++ b/src/abis_rsl.c
@@ -2,6 +2,7 @@
  * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
 
 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
+ *
  * All Rights Reserved
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,31 +22,34 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
 
-#include "gsm_data.h"
-#include "gsm_04_08.h"
-#include "abis_rsl.h"
-#include "debug.h"
-#include "tlv.h"
+#include <openbsc/gsm_data.h>
+#include <openbsc/gsm_04_08.h>
+#include <openbsc/abis_rsl.h>
+#include <openbsc/chan_alloc.h>
+#include <openbsc/debug.h>
+#include <openbsc/tlv.h>
 
-#define RSL_ALLOC_SIZE	1024
+#define RSL_ALLOC_SIZE		1024
+#define RSL_ALLOC_HEADROOM	128
 
 static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
 {
 	/* mask off the transparent bit ? */
 	msg_type &= 0xfe;
 
-	if (msg_type & 0xf0 == 0x00)
+	if ((msg_type & 0xf0) == 0x00)
 		return ABIS_RSL_MDISC_RLL;
-	if (msg_type & 0xf0 == 0x10) {
+	if ((msg_type & 0xf0) == 0x10) {
 		if (msg_type >= 0x19 && msg_type <= 0x22)
 			return ABIS_RSL_MDISC_TRX;
 		else
 			return ABIS_RSL_MDISC_COM_CHAN;
 	}
-	if (msg_type & 0xc == 0x00)
+	if ((msg_type & 0xc) == 0x00)
 		return ABIS_RSL_MDISC_DED_CHAN;
 	
 	return ABIS_RSL_MDISC_LOC;
@@ -97,6 +101,84 @@
 	return ret;
 }
 
+/* determine logical channel based on TRX and channel number IE */
+struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
+{
+	struct gsm_lchan *lchan;
+	u_int8_t ts_nr = chan_nr & 0x07;
+	u_int8_t cbits = chan_nr >> 3;
+	u_int8_t lch_idx;
+	struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
+
+	if (cbits == 0x01) {
+		lch_idx = 0;	/* TCH/F */	
+		if (ts->pchan != GSM_PCHAN_TCH_F)
+			fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
+				chan_nr, ts->pchan);
+	} else if ((cbits & 0x1e) == 0x02) {
+		lch_idx = cbits & 0x1;	/* TCH/H */
+		if (ts->pchan != GSM_PCHAN_TCH_H)
+			fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
+				chan_nr, ts->pchan);
+	} else if ((cbits & 0x1c) == 0x04) {
+		lch_idx = cbits & 0x3;	/* SDCCH/4 */
+		if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
+			fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
+				chan_nr, ts->pchan);
+	} else if ((cbits & 0x18) == 0x08) {
+		lch_idx = cbits & 0x7;	/* SDCCH/8 */
+		if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
+			fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
+				chan_nr, ts->pchan);
+	} else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
+		lch_idx = 0;
+		if (ts->pchan != GSM_PCHAN_CCCH &&
+		    ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
+			fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
+				chan_nr, ts->pchan);
+		/* FIXME: we should not return first sdcch4 !!! */
+	} else {
+		fprintf(stderr, "unknown chan_nr=0x%02x\n", chan_nr);
+		return NULL;
+	}
+
+	lchan = &ts->lchan[lch_idx];
+
+	return lchan;
+}
+
+u_int8_t lchan2chan_nr(struct gsm_lchan *lchan)
+{
+	struct gsm_bts_trx_ts *ts = lchan->ts;
+	u_int8_t cbits, chan_nr;
+
+	switch (ts->pchan) {
+	case GSM_PCHAN_TCH_F:
+		cbits = 0x01;
+		break;
+	case GSM_PCHAN_TCH_H:
+		cbits = 0x02;
+		cbits += lchan->nr;
+		break;
+	case GSM_PCHAN_CCCH_SDCCH4:
+		cbits = 0x04;
+		cbits += lchan->nr;
+		break;
+	case GSM_PCHAN_SDCCH8_SACCH8C:
+		cbits = 0x08;
+		cbits += lchan->nr;
+		break;
+	default:
+	case GSM_PCHAN_CCCH:
+		cbits = 0x10;
+		break;
+	}
+
+	chan_nr = (cbits << 3) | (ts->nr & 0x7);
+
+	return chan_nr;
+}
+
 /* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
 u_int64_t str_to_imsi(const char *imsi_str)
 {
@@ -130,12 +212,17 @@
 	return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
 }
 
+static struct msgb *rsl_msgb_alloc(void)
+{
+	return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM);
+}
+
 /* Send a BCCH_INFO message as per Chapter 8.5.1 */
 int rsl_bcch_info(struct gsm_bts *bts, u_int8_t type,
 		  const u_int8_t *data, int len)
 {
 	struct abis_rsl_dchan_hdr *dh;
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 
 	dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
 	init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
@@ -144,14 +231,16 @@
 	msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
 	msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
 
-	return abis_rsl_sendmsg(bts, msg);
+	msg->trx = bts->c0;
+
+	return abis_rsl_sendmsg(msg);
 }
 
 int rsl_sacch_filling(struct gsm_bts *bts, u_int8_t type, 
 		      const u_int8_t *data, int len)
 {
 	struct abis_rsl_common_hdr *ch;
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 
 	ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
 	ch->msg_discr = ABIS_RSL_MDISC_TRX;
@@ -160,7 +249,9 @@
 	msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
 	msgb_tlv_put(msg, RSL_IE_L3_INFO, len, data);
 
-	return abis_rsl_sendmsg(bts, msg);
+	msg->trx = bts->c0;
+
+	return abis_rsl_sendmsg(msg);
 }
 
 /* Chapter 8.4.1 */
@@ -172,7 +263,7 @@
 		      u_int8_t ta)
 {
 	struct abis_rsl_dchan_hdr *dh;
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 	u_int8_t encr_info = 0x01;
 
 	dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
@@ -192,7 +283,7 @@
 	msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
 	msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
 
-	return abis_rsl_sendmsg(bts, msg);
+	return abis_rsl_sendmsg(msg);
 }
 
 #define TSC	7
@@ -243,23 +334,26 @@
 	return rsl_chan_activate(ts->trx->bts, chan_nr, 0x00, &cm, &ci, 0x01, 0x0f, 0x00);
 }
 
-int rsl_chan_release(struct gsm_bts_trx_ts *ts, u_int8_t chan_nr)
+int rsl_chan_release(struct gsm_lchan *lchan)
 {
 	struct abis_rsl_dchan_hdr *dh;
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 
 	dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
 	init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
-	dh->chan_nr = chan_nr;
+	dh->chan_nr = lchan2chan_nr(lchan);
 
-	return abis_rsl_sendmsg(ts->trx->bts, msg);
+	msg->lchan = lchan;
+	msg->trx = lchan->ts->trx;
+
+	return abis_rsl_sendmsg(msg);
 }
 
 int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
 		   u_int8_t *ms_ident, u_int8_t chan_needed)
 {
 	struct abis_rsl_dchan_hdr *dh;
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 
 	dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
 	init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
@@ -269,7 +363,9 @@
 	msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len, ms_ident);
 	msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
 
-	return abis_rsl_sendmsg(bts, msg);
+	msg->trx = bts->c0;
+
+	return abis_rsl_sendmsg(msg);
 }
 
 int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
@@ -299,7 +395,7 @@
 
 int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
 {
-	struct msgb *msg = msgb_alloc(RSL_ALLOC_SIZE);
+	struct msgb *msg = rsl_msgb_alloc();
 	struct abis_rsl_dchan_hdr *dh;
 
 	dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
@@ -310,34 +406,57 @@
 
 	msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
 
-	return abis_rsl_sendmsg(bts, msg);
+	msg->trx = bts->c0;
+
+	return abis_rsl_sendmsg(msg);
 }
 
+/* Send "DATA REQUEST" message with given L3 Info payload */
 /* Chapter 8.3.1 */
-int rsl_data_request(struct gsm_bts *bts, struct msgb *msg)
+int rsl_data_request(struct msgb *msg, u_int8_t link_id)
 {
-	/* FIXME: prepend RSL header to layer 3 message */
-	u_int8_t len = msg->len;
+	u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
 	struct abis_rsl_rll_hdr *rh;
 
-	msgb_tv_push(msg, RSL_IE_L3_INFO, len);
+	if (msg->lchan == NULL) {
+		fprintf(stderr, "cannot send DATA REQUEST to unknown lchan\n");
+		return -EINVAL;
+	}
 
+	/* First push the L3 IE tag and length */
+	msgb_tv_push(msg, RSL_IE_L3_INFO, l3_len);
+
+	/* Then push the RSL header */
 	rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
 	init_llm_hdr(rh, RSL_MT_DATA_REQ);
-	rh->chan_nr = RSL_CHAN_SDCCH4_ACCH;	/* FIXME: don't harcode */
+	rh->chan_nr = lchan2chan_nr(msg->lchan);
+	rh->link_id = link_id;
 
-	return abis_rsl_sendmsg(bts, msg);
+	msg->trx = msg->lchan->ts->trx;
+
+	return abis_rsl_sendmsg(msg);
 }
 
 static int abis_rsl_rx_dchan(struct msgb *msg)
 {
-	struct abis_rsl_common_hdr *rslh = msgb_l2(msg)	;
+	struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
+	int rc = 0;
 
-	switch (rslh->msg_type) {
+	msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
+
+	switch (rslh->c.msg_type) {
 	case RSL_MT_CHAN_ACTIV_ACK:
+		DEBUGP(DRSL, "rsl_rx_dchan: Channel Activate ACK\n");
+		break;
 	case RSL_MT_CHAN_ACTIV_NACK:
+		DEBUGP(DRSL, "rsl_rx_dchan: Channel Activate NACK\n");
+		break;
 	case RSL_MT_CONN_FAIL:
+		DEBUGP(DRSL, "rsl_rx_dchan: Connection Fail\n");
+		break;
 	case RSL_MT_MEAS_RES:
+		DEBUGP(DRSL, "rsl_rx_dchan: Measurement Result\n");
+		break;
 	case RSL_MT_MODE_MODIFY_ACK:
 	case RSL_MT_MODE_MODIFY_NACK:
 	case RSL_MT_PHY_CONTEXT_CONF:
@@ -350,18 +469,20 @@
 	case RSL_MT_MR_CODEC_MOD_NACK:
 	case RSL_MT_MR_CODEC_MOD_PER:
 		fprintf(stderr, "Unimplemented Abis RSL DChan msg 0x%02x\n",
-			rslh->msg_type);
+			rslh->c.msg_type);
 		break;
 	default:
 		fprintf(stderr, "unknown Abis RSL DChan msg 0x%02x\n",
-			rslh->msg_type);
+			rslh->c.msg_type);
 		return -EINVAL;
 	}
+	return rc;
 }
 
 static int abis_rsl_rx_trx(struct msgb *msg)
 {
 	struct abis_rsl_common_hdr *rslh = msgb_l2(msg)	;
+	int rc = 0;
 
 	switch (rslh->msg_type) {
 	case RSL_MT_RF_RES_IND:
@@ -377,25 +498,58 @@
 			rslh->msg_type);
 		return -EINVAL;
 	}
-
+	return rc;
 }
 
+/* MS has requested a channel on the RACH */
 static int rsl_rx_chan_rqd(struct msgb *msg)
 {
 	struct gsm_bts *bts = msg->bts_link->bts;
+	struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
+	struct gsm48_req_ref *rqd_ref;
 	struct gsm48_imm_ass ia;
+	enum gsm_chan_t lctype;
+	struct gsm_lchan *lchan;
+	u_int8_t rqd_ta;
+
 	u_int16_t arfcn;
 	u_int8_t ts_number, subch;
 
-	/* MS has requested a channel on the RACH */
-	/* parse channel number, request reference, access delay */
-	/* FIXME: check permission/availability */
-	ts_number = 0;
-	arfcn = HARDCODED_ARFCN;
-	subch = 0;
+	/* parse request reference to be used in immediate assign */
+	if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
+		return -EINVAL;
+
+	rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
+
+	/* parse access delay and use as TA */
+	if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
+		return -EINVAL;
+	rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
+
+	/* determine channel type (SDCCH/TCH_F/TCH_H) based on
+	 * request reference RA */
+	lctype = get_ctype_by_chreq(bts, rqd_ref->ra);
+
+	/* check availability / allocate channel */
+	lchan = lchan_alloc(bts, lctype);
+	if (!lchan) {
+		fprintf(stderr, "CHAN RQD: no resources\n");
+		/* FIXME: send some kind of reject ?!? */
+		return -ENOMEM;
+	}
+
+	ts_number = lchan->ts->nr;
+	arfcn = lchan->ts->trx->arfcn;
+	subch = lchan->nr;
 	
+	DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %u\n",
+		arfcn, ts_number, subch, lchan->type);
+
 	/* send CHANNEL ACTIVATION on RSL to BTS */
-	rsl_chan_activate_sdcch(&bts->trx[0].ts[ts_number]);
+	if (lchan->ts->pchan == GSM_PCHAN_SDCCH8_SACCH8C)
+		rsl_chan_activate_sdcch(lchan->ts);
+	else
+		rsl_chan_activate_tch_f(lchan->ts);
 
 	/* create IMMEDIATE ASSIGN 04.08 messge */
 	memset(&ia, 0, sizeof(ia));
@@ -403,18 +557,14 @@
 	ia.proto_discr = GSM48_PDISC_RR;
 	ia.msg_type = GSM48_MT_RR_IMM_ASS;
 	ia.page_mode = GSM48_PM_NORMAL;
-	ia.chan_desc.chan_nr = rsl_enc_chan_nr(RSL_CHAN_SDCCH4_ACCH, subch, ts_number);
+	ia.chan_desc.chan_nr = rsl_enc_chan_nr(lchan->ts->pchan, subch, ts_number);
 	ia.chan_desc.h0.h = 0;
 	ia.chan_desc.h0.arfcn_high = arfcn >> 8;
 	ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
 	ia.chan_desc.h0.tsc = 7;
-	/* FIXME: use real request reference extracted from CHAN_RQD */
-	ia.req_ref.ra = 0x80 | 0x1e;
-	ia.req_ref.t2 = 0x0c;
-	ia.req_ref.t1_ = 0x12;
-	ia.req_ref.t3_low = 0x19 & 3;
-	ia.req_ref.t3_high = 0x19 >> 3;
-	ia.timing_advance = 0;
+	/* use request reference extracted from CHAN_RQD */
+	memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
+	ia.timing_advance = rqd_ta;
 	ia.mob_alloc_len = 0;
 
 	/* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
@@ -423,10 +573,12 @@
 
 static int abis_rsl_rx_cchan(struct msgb *msg)
 {
-	struct abis_rsl_common_hdr *rslh = msgb_l2(msg)	;
-	int rc;
+	struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg)	;
+	int rc = 0;
 
-	switch (rslh->msg_type) {
+	msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
+
+	switch (rslh->c.msg_type) {
 	case RSL_MT_CHAN_RQD:
 		/* MS has requested a channel on the RACH */
 		rc = rsl_rx_chan_rqd(msg);
@@ -438,13 +590,15 @@
 	case RSL_MT_CCCH_LOAD_IND:
 		/* current load on the CCCH */
 		fprintf(stderr, "Unimplemented Abis RSL TRX message type 0x%02x\n",
-			rslh->msg_type);
+			rslh->c.msg_type);
 		break;
 	default:
 		fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
-			rslh->msg_type);
+			rslh->c.msg_type);
 		return -EINVAL;
 	}
+
+	return rc;
 }
 
 /*	ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST 
@@ -457,16 +611,20 @@
 {
 	struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
 	int rc;
+
+	msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
 	
 	switch (rllh->c.msg_type) {
 	case RSL_MT_DATA_IND:
 		DEBUGP(DRLL, "DATA INDICATION\n");
-		/* FIXME: parse L3 info element */
+		/* FIXME: Verify L3 info element */
+		msg->l3_off = &rllh->data[2] - msg->data;
 		rc = gsm0408_rcvmsg(msg);
 		break;
 	case RSL_MT_EST_IND:
 		DEBUGP(DRLL, "ESTABLISH INDICATION\n");
-		/* FIXME: parse L3 info element */
+		/* FIXME: Verify L3 info element */
+		msg->l3_off = &rllh->data[2] - msg->data;
 		rc = gsm0408_rcvmsg(msg);
 		break;
 	case RSL_MT_ERROR_IND:
@@ -479,10 +637,11 @@
 		fprintf(stderr, "unknown Abis RLL message type 0x%02x\n",
 			rllh->c.msg_type);
 	}
+	return rc;
 }
 
 /* Entry-point where L2 RSL from BTS enters */
-int abis_rsl_rx(struct msgb *msg)
+int abis_rsl_rcvmsg(struct msgb *msg)
 {
 	struct abis_rsl_common_hdr *rslh = msgb_l2(msg)	;
 	unsigned int l2_len = (void *)msg->tail - msgb_l2(msg);
@@ -496,13 +655,16 @@
 		rc = abis_rsl_rx_dchan(msg);
 		break;
 	case ABIS_RSL_MDISC_COM_CHAN:
-	case ABIS_RSL_MDISC_TRX:
 		rc = abis_rsl_rx_cchan(msg);
 		break;
+	case ABIS_RSL_MDISC_TRX:
+		rc = abis_rsl_rx_trx(msg);
+		break;
 	case ABIS_RSL_MDISC_LOC:
 	default:
 		fprintf(stderr, "unknown RSL message discriminator 0x%02x\n",
 			rslh->msg_discr);
 		return -EINVAL;
 	}
+	return rc;
 }