blob: 11a4355a7d706c566b15d2491c4a4258027a67ac [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* GSM Radio Signalling Link messages on the A-bis interface
2 * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/gsm_04_08.h>
Harald Weltec4dcda02009-08-09 14:45:18 +020033#include <openbsc/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/abis_rsl.h>
35#include <openbsc/chan_alloc.h>
Harald Welteed9a5ab2009-08-09 13:47:35 +020036#include <openbsc/bsc_rll.h>
Harald Welte59b04682009-06-10 05:40:52 +080037#include <openbsc/debug.h>
38#include <openbsc/tlv.h>
39#include <openbsc/paging.h>
40#include <openbsc/signal.h>
Harald Weltec20bd1d2009-11-29 19:07:28 +010041#include <openbsc/meas_rep.h>
Harald Welte50517742009-12-20 15:42:44 +010042#include <openbsc/rtp_proxy.h>
Harald Welte59b04682009-06-10 05:40:52 +080043
44#define RSL_ALLOC_SIZE 1024
45#define RSL_ALLOC_HEADROOM 128
46
47#define MAX(a, b) (a) >= (b) ? (a) : (b)
48
49static const struct tlv_definition rsl_att_tlvdef = {
50 .def = {
51 [RSL_IE_CHAN_NR] = { TLV_TYPE_TV },
52 [RSL_IE_LINK_IDENT] = { TLV_TYPE_TV },
53 [RSL_IE_ACT_TYPE] = { TLV_TYPE_TV },
54 [RSL_IE_BS_POWER] = { TLV_TYPE_TV },
55 [RSL_IE_CHAN_IDENT] = { TLV_TYPE_TLV },
56 [RSL_IE_CHAN_MODE] = { TLV_TYPE_TLV },
57 [RSL_IE_ENCR_INFO] = { TLV_TYPE_TLV },
58 [RSL_IE_FRAME_NUMBER] = { TLV_TYPE_FIXED, 2 },
59 [RSL_IE_HANDO_REF] = { TLV_TYPE_TV },
60 [RSL_IE_L1_INFO] = { TLV_TYPE_FIXED, 2 },
61 [RSL_IE_L3_INFO] = { TLV_TYPE_TL16V },
62 [RSL_IE_MS_IDENTITY] = { TLV_TYPE_TLV },
63 [RSL_IE_MS_POWER] = { TLV_TYPE_TV },
64 [RSL_IE_PAGING_GROUP] = { TLV_TYPE_TV },
65 [RSL_IE_PAGING_LOAD] = { TLV_TYPE_FIXED, 2 },
66 [RSL_IE_PYHS_CONTEXT] = { TLV_TYPE_TLV },
67 [RSL_IE_ACCESS_DELAY] = { TLV_TYPE_TV },
68 [RSL_IE_RACH_LOAD] = { TLV_TYPE_TLV },
69 [RSL_IE_REQ_REFERENCE] = { TLV_TYPE_FIXED, 3 },
70 [RSL_IE_RELEASE_MODE] = { TLV_TYPE_TV },
71 [RSL_IE_RESOURCE_INFO] = { TLV_TYPE_TLV },
72 [RSL_IE_RLM_CAUSE] = { TLV_TYPE_TLV },
73 [RSL_IE_STARTNG_TIME] = { TLV_TYPE_FIXED, 2 },
74 [RSL_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
75 [RSL_IE_UPLINK_MEAS] = { TLV_TYPE_TLV },
76 [RSL_IE_CAUSE] = { TLV_TYPE_TLV },
77 [RSL_IE_MEAS_RES_NR] = { TLV_TYPE_TV },
78 [RSL_IE_MSG_ID] = { TLV_TYPE_TV },
79 [RSL_IE_SYSINFO_TYPE] = { TLV_TYPE_TV },
80 [RSL_IE_MS_POWER_PARAM] = { TLV_TYPE_TLV },
81 [RSL_IE_BS_POWER_PARAM] = { TLV_TYPE_TLV },
82 [RSL_IE_PREPROC_PARAM] = { TLV_TYPE_TLV },
83 [RSL_IE_PREPROC_MEAS] = { TLV_TYPE_TLV },
84 [RSL_IE_IMM_ASS_INFO] = { TLV_TYPE_TLV },
85 [RSL_IE_SMSCB_INFO] = { TLV_TYPE_FIXED, 23 },
86 [RSL_IE_MS_TIMING_OFFSET] = { TLV_TYPE_TV },
87 [RSL_IE_ERR_MSG] = { TLV_TYPE_TLV },
88 [RSL_IE_FULL_BCCH_INFO] = { TLV_TYPE_TLV },
89 [RSL_IE_CHAN_NEEDED] = { TLV_TYPE_TV },
90 [RSL_IE_CB_CMD_TYPE] = { TLV_TYPE_TV },
91 [RSL_IE_SMSCB_MSG] = { TLV_TYPE_TLV },
92 [RSL_IE_FULL_IMM_ASS_INFO] = { TLV_TYPE_TLV },
93 [RSL_IE_SACCH_INFO] = { TLV_TYPE_TLV },
94 [RSL_IE_CBCH_LOAD_INFO] = { TLV_TYPE_TV },
95 [RSL_IE_SMSCB_CHAN_INDICATOR] = { TLV_TYPE_TV },
96 [RSL_IE_GROUP_CALL_REF] = { TLV_TYPE_TLV },
97 [RSL_IE_CHAN_DESC] = { TLV_TYPE_TLV },
98 [RSL_IE_NCH_DRX_INFO] = { TLV_TYPE_TLV },
99 [RSL_IE_CMD_INDICATOR] = { TLV_TYPE_TLV },
100 [RSL_IE_EMLPP_PRIO] = { TLV_TYPE_TV },
101 [RSL_IE_UIC] = { TLV_TYPE_TLV },
102 [RSL_IE_MAIN_CHAN_REF] = { TLV_TYPE_TV },
103 [RSL_IE_MR_CONFIG] = { TLV_TYPE_TLV },
104 [RSL_IE_MR_CONTROL] = { TLV_TYPE_TV },
105 [RSL_IE_SUP_CODEC_TYPES] = { TLV_TYPE_TLV },
106 [RSL_IE_CODEC_CONFIG] = { TLV_TYPE_TLV },
107 [RSL_IE_RTD] = { TLV_TYPE_TV },
108 [RSL_IE_TFO_STATUS] = { TLV_TYPE_TV },
109 [RSL_IE_LLP_APDU] = { TLV_TYPE_TLV },
Harald Welte4684e632009-08-10 09:51:40 +0200110 [RSL_IE_SIEMENS_MRPCI] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200111 [RSL_IE_IPAC_PROXY_UDP] = { TLV_TYPE_FIXED, 2 },
112 [RSL_IE_IPAC_BSCMPL_TOUT] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800113 [RSL_IE_IPAC_REMOTE_IP] = { TLV_TYPE_FIXED, 4 },
114 [RSL_IE_IPAC_REMOTE_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200115 [RSL_IE_IPAC_RTP_PAYLOAD] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800116 [RSL_IE_IPAC_LOCAL_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welteb9498952009-07-12 09:45:05 +0200117 [RSL_IE_IPAC_SPEECH_MODE] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200118 [RSL_IE_IPAC_LOCAL_IP] = { TLV_TYPE_FIXED, 4 },
Harald Welteb9498952009-07-12 09:45:05 +0200119 [RSL_IE_IPAC_CONN_ID] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200120 [RSL_IE_IPAC_RTP_CSD_FMT] = { TLV_TYPE_TV },
121 [RSL_IE_IPAC_RTP_JIT_BUF] = { TLV_TYPE_FIXED, 2 },
122 [RSL_IE_IPAC_RTP_COMPR] = { TLV_TYPE_TV },
Harald Welteb9498952009-07-12 09:45:05 +0200123 [RSL_IE_IPAC_RTP_PAYLOAD2] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200124 [RSL_IE_IPAC_RTP_MPLEX] = { TLV_TYPE_FIXED, 8 },
125 [RSL_IE_IPAC_RTP_MPLEX_ID] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800126 },
127};
128#define rsl_tlv_parse(dec, buf, len) \
129 tlv_parse(dec, &rsl_att_tlvdef, buf, len, 0, 0)
130
131static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
132{
133 /* mask off the transparent bit ? */
134 msg_type &= 0xfe;
135
136 if ((msg_type & 0xf0) == 0x00)
137 return ABIS_RSL_MDISC_RLL;
138 if ((msg_type & 0xf0) == 0x10) {
139 if (msg_type >= 0x19 && msg_type <= 0x22)
140 return ABIS_RSL_MDISC_TRX;
141 else
142 return ABIS_RSL_MDISC_COM_CHAN;
143 }
144 if ((msg_type & 0xe0) == 0x20)
145 return ABIS_RSL_MDISC_DED_CHAN;
146
147 return ABIS_RSL_MDISC_LOC;
148}
149
150static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
151 u_int8_t msg_type)
152{
153 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
154 dh->c.msg_type = msg_type;
155 dh->ie_chan = RSL_IE_CHAN_NR;
156}
157
158static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
159 u_int8_t msg_type)
160{
161 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
162 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
163 dh->c.msg_type = msg_type;
164 dh->ie_chan = RSL_IE_CHAN_NR;
165 dh->ie_link_id = RSL_IE_LINK_IDENT;
166}
167
168
169/* encode channel number as per Section 9.3.1 */
170u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
171{
172 u_int8_t ret;
173
174 ret = (timeslot & 0x07) | type;
175
176 switch (type) {
177 case RSL_CHAN_Lm_ACCHs:
178 subch &= 0x01;
179 break;
180 case RSL_CHAN_SDCCH4_ACCH:
181 subch &= 0x07;
182 break;
183 case RSL_CHAN_SDCCH8_ACCH:
184 subch &= 0x07;
185 break;
186 default:
187 /* no subchannels allowed */
188 subch = 0x00;
189 break;
190 }
191 ret |= (subch << 3);
192
193 return ret;
194}
195
196/* determine logical channel based on TRX and channel number IE */
197struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
198{
199 struct gsm_lchan *lchan;
200 u_int8_t ts_nr = chan_nr & 0x07;
201 u_int8_t cbits = chan_nr >> 3;
202 u_int8_t lch_idx;
203 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
204
205 if (cbits == 0x01) {
206 lch_idx = 0; /* TCH/F */
Harald Welte37884ed2009-10-24 10:25:50 +0200207 if (ts->pchan != GSM_PCHAN_TCH_F &&
208 ts->pchan != GSM_PCHAN_PDCH &&
209 ts->pchan != GSM_PCHAN_TCH_F_PDCH)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100210 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800211 chan_nr, ts->pchan);
212 } else if ((cbits & 0x1e) == 0x02) {
213 lch_idx = cbits & 0x1; /* TCH/H */
214 if (ts->pchan != GSM_PCHAN_TCH_H)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100215 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800216 chan_nr, ts->pchan);
217 } else if ((cbits & 0x1c) == 0x04) {
218 lch_idx = cbits & 0x3; /* SDCCH/4 */
219 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100220 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800221 chan_nr, ts->pchan);
222 } else if ((cbits & 0x18) == 0x08) {
223 lch_idx = cbits & 0x7; /* SDCCH/8 */
224 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100225 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800226 chan_nr, ts->pchan);
227 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
228 lch_idx = 0;
229 if (ts->pchan != GSM_PCHAN_CCCH &&
230 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100231 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800232 chan_nr, ts->pchan);
233 /* FIXME: we should not return first sdcch4 !!! */
234 } else {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100235 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +0800236 return NULL;
237 }
238
239 lchan = &ts->lchan[lch_idx];
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100240 debug_set_context(BSC_CTX_LCHAN, lchan);
241 debug_set_context(BSC_CTX_SUBSCR, lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800242
243 return lchan;
244}
245
Holger Hans Peter Freyther942ff172009-10-22 11:47:45 +0200246/* See Table 10.5.25 of GSM04.08 */
Harald Welteb5586242009-12-09 19:18:32 +0100247u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800248{
249 struct gsm_bts_trx_ts *ts = lchan->ts;
250 u_int8_t cbits, chan_nr;
251
252 switch (ts->pchan) {
253 case GSM_PCHAN_TCH_F:
Harald Welte37884ed2009-10-24 10:25:50 +0200254 case GSM_PCHAN_PDCH:
255 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte59b04682009-06-10 05:40:52 +0800256 cbits = 0x01;
257 break;
258 case GSM_PCHAN_TCH_H:
259 cbits = 0x02;
260 cbits += lchan->nr;
261 break;
262 case GSM_PCHAN_CCCH_SDCCH4:
263 cbits = 0x04;
264 cbits += lchan->nr;
265 break;
266 case GSM_PCHAN_SDCCH8_SACCH8C:
267 cbits = 0x08;
268 cbits += lchan->nr;
269 break;
270 default:
271 case GSM_PCHAN_CCCH:
272 cbits = 0x10;
273 break;
274 }
275
276 chan_nr = (cbits << 3) | (ts->nr & 0x7);
277
278 return chan_nr;
279}
280
281/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
282u_int64_t str_to_imsi(const char *imsi_str)
283{
284 u_int64_t ret;
285
286 ret = strtoull(imsi_str, NULL, 10);
287
288 return ret;
289}
290
291/* Table 5 Clause 7 TS 05.02 */
292unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
293{
294 if (!bs_ccch_sdcch_comb)
295 return 9 - bs_ag_blks_res;
296 else
297 return 3 - bs_ag_blks_res;
298}
299
300/* Chapter 6.5.2 of TS 05.02 */
301unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
302 unsigned int n_pag_blocks)
303{
304 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
305}
306
307/* Chapter 6.5.2 of TS 05.02 */
308unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
309 int n_pag_blocks)
310{
311 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
312}
313
314static struct msgb *rsl_msgb_alloc(void)
315{
Harald Welte9cfc9352009-06-26 19:39:35 +0200316 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
317 "RSL");
Harald Welte59b04682009-06-10 05:40:52 +0800318}
319
320#define MACBLOCK_SIZE 23
321static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
322{
323 memcpy(out, in, len);
324
325 if (len < MACBLOCK_SIZE)
326 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
327}
328
Harald Welted2dd9de2009-08-30 15:37:11 +0900329/* Chapter 9.3.7: Encryption Information */
330static int build_encr_info(u_int8_t *out, struct gsm_lchan *lchan)
331{
332 *out++ = lchan->encr.alg_id & 0xff;
333 if (lchan->encr.key_len)
334 memcpy(out, lchan->encr.key, lchan->encr.key_len);
335 return lchan->encr.key_len + 1;
336}
337
338
Harald Weltef1a168d2009-07-28 17:58:09 +0200339static const char *rsl_err_vals[0xff] = {
340 [RSL_ERR_RADIO_IF_FAIL] = "Radio Interface Failure",
341 [RSL_ERR_RADIO_LINK_FAIL] = "Radio Link Failure",
342 [RSL_ERR_HANDOVER_ACC_FAIL] = "Handover Access Failure",
343 [RSL_ERR_TALKER_ACC_FAIL] = "Talker Access Failure",
344 [RSL_ERR_OM_INTERVENTION] = "O&M Intervention",
345 [RSL_ERR_NORMAL_UNSPEC] = "Normal event, unspecified",
Harald Welteb1717e92009-08-04 02:31:05 +0200346 [RSL_ERR_T_MSRFPCI_EXP] = "Siemens: T_MSRFPCI Expired",
Harald Weltef1a168d2009-07-28 17:58:09 +0200347 [RSL_ERR_EQUIPMENT_FAIL] = "Equipment Failure",
348 [RSL_ERR_RR_UNAVAIL] = "Radio Resource not available",
349 [RSL_ERR_TERR_CH_FAIL] = "Terrestrial Channel Failure",
350 [RSL_ERR_CCCH_OVERLOAD] = "CCCH Overload",
351 [RSL_ERR_ACCH_OVERLOAD] = "ACCH Overload",
352 [RSL_ERR_PROCESSOR_OVERLOAD] = "Processor Overload",
353 [RSL_ERR_RES_UNAVAIL] = "Resource not available, unspecified",
354 [RSL_ERR_TRANSC_UNAVAIL] = "Transcoding not available",
355 [RSL_ERR_SERV_OPT_UNAVAIL] = "Service or Option not available",
356 [RSL_ERR_ENCR_UNIMPL] = "Encryption algorithm not implemented",
357 [RSL_ERR_SERV_OPT_UNIMPL] = "Service or Option not implemented",
358 [RSL_ERR_RCH_ALR_ACTV_ALLOC] = "Radio channel already activated",
359 [RSL_ERR_INVALID_MESSAGE] = "Invalid Message, unspecified",
360 [RSL_ERR_MSG_DISCR] = "Message Discriminator Error",
361 [RSL_ERR_MSG_TYPE] = "Message Type Error",
362 [RSL_ERR_MSG_SEQ] = "Message Sequence Error",
363 [RSL_ERR_IE_ERROR] = "General IE error",
364 [RSL_ERR_MAND_IE_ERROR] = "Mandatory IE error",
365 [RSL_ERR_OPT_IE_ERROR] = "Optional IE error",
366 [RSL_ERR_IE_NONEXIST] = "IE non-existent",
367 [RSL_ERR_IE_LENGTH] = "IE length error",
368 [RSL_ERR_IE_CONTENT] = "IE content error",
369 [RSL_ERR_PROTO] = "Protocol error, unspecified",
370 [RSL_ERR_INTERWORKING] = "Interworking error, unspecified",
371};
372
Harald Welte (local)bd76cce2009-12-26 23:55:00 +0100373static const struct value_string rlm_cause_strs[] = {
374 { RLL_CAUSE_T200_EXPIRED, "Timer T200 expired (N200+1) times" },
375 { RLL_CAUSE_REEST_REQ, "Re-establishment request" },
376 { RLL_CAUSE_UNSOL_UA_RESP, "Unsolicited UA response" },
377 { RLL_CAUSE_UNSOL_DM_RESP, "Unsolicited DM response" },
378 { RLL_CAUSE_UNSOL_DM_RESP_MF, "Unsolicited DM response, multiple frame" },
379 { RLL_CAUSE_UNSOL_SPRV_RESP, "Unsolicited supervisory response" },
380 { RLL_CAUSE_SEQ_ERR, "Sequence Error" },
381 { RLL_CAUSE_UFRM_INC_PARAM, "U-Frame with incorrect parameters" },
382 { RLL_CAUSE_SFRM_INC_PARAM, "S-Frame with incorrect parameters" },
383 { RLL_CAUSE_IFRM_INC_MBITS, "I-Frame with incorrect use of M bit" },
384 { RLL_CAUSE_IFRM_INC_LEN, "I-Frame with incorrect length" },
385 { RLL_CAUSE_FRM_UNIMPL, "Fraeme not implemented" },
386 { RLL_CAUSE_SABM_MF, "SABM command, multiple frame established state" },
387 { RLL_CAUSE_SABM_INFO_NOTALL, "SABM frame with information not allowed in this state" },
388 { 0, NULL },
389};
390
Harald Weltef1a168d2009-07-28 17:58:09 +0200391static const char *rsl_err_name(u_int8_t err)
Harald Welte59b04682009-06-10 05:40:52 +0800392{
Harald Weltef1a168d2009-07-28 17:58:09 +0200393 if (rsl_err_vals[err])
394 return rsl_err_vals[err];
395 else
396 return "unknown";
397}
398
Harald Weltede4477a2009-12-24 12:20:20 +0100399static void print_rsl_cause(int lvl, const u_int8_t *cause_v, u_int8_t cause_len)
Harald Weltef1a168d2009-07-28 17:58:09 +0200400{
Harald Welte59b04682009-06-10 05:40:52 +0800401 int i;
402
Harald Weltede4477a2009-12-24 12:20:20 +0100403 LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ",
Harald Weltef1a168d2009-07-28 17:58:09 +0200404 cause_v[0], rsl_err_name(cause_v[0]));
405 for (i = 1; i < cause_len-1; i++)
Harald Weltede4477a2009-12-24 12:20:20 +0100406 LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
Harald Welte59b04682009-06-10 05:40:52 +0800407}
408
409/* Send a BCCH_INFO message as per Chapter 8.5.1 */
410int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
411 const u_int8_t *data, int len)
412{
413 struct abis_rsl_dchan_hdr *dh;
414 struct msgb *msg = rsl_msgb_alloc();
415
416 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
417 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
418 dh->chan_nr = RSL_CHAN_BCCH;
419
420 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
421 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
422
423 msg->trx = trx;
424
425 return abis_rsl_sendmsg(msg);
426}
427
428int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
429 const u_int8_t *data, int len)
430{
431 struct abis_rsl_common_hdr *ch;
432 struct msgb *msg = rsl_msgb_alloc();
433
434 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
435 ch->msg_discr = ABIS_RSL_MDISC_TRX;
436 ch->msg_type = RSL_MT_SACCH_FILL;
437
438 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
439 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
440
441 msg->trx = trx;
442
443 return abis_rsl_sendmsg(msg);
444}
445
Harald Welte91afe4c2009-06-20 18:15:19 +0200446int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
447{
448 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200449 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200450 u_int8_t chan_nr = lchan2chan_nr(lchan);
451
452 db = abs(db);
453 if (db > 30)
454 return -EINVAL;
455
Harald Welteed831842009-06-27 03:09:08 +0200456 msg = rsl_msgb_alloc();
457
Harald Welte91afe4c2009-06-20 18:15:19 +0200458 lchan->bs_power = db/2;
459 if (fpc)
460 lchan->bs_power |= 0x10;
461
462 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
463 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
464 dh->chan_nr = chan_nr;
465
466 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
467
468 msg->trx = lchan->ts->trx;
469
470 return abis_rsl_sendmsg(msg);
471}
472
Harald Welte91afe4c2009-06-20 18:15:19 +0200473int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
474{
475 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200476 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200477 u_int8_t chan_nr = lchan2chan_nr(lchan);
478 int ctl_lvl;
479
Harald Weltec4dcda02009-08-09 14:45:18 +0200480 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Welte91afe4c2009-06-20 18:15:19 +0200481 if (ctl_lvl < 0)
482 return ctl_lvl;
483
Harald Welteed831842009-06-27 03:09:08 +0200484 msg = rsl_msgb_alloc();
485
Harald Welte91afe4c2009-06-20 18:15:19 +0200486 lchan->ms_power = ctl_lvl;
487
488 if (fpc)
489 lchan->ms_power |= 0x20;
490
491 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
492 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
493 dh->chan_nr = chan_nr;
494
495 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
496
497 msg->trx = lchan->ts->trx;
498
499 return abis_rsl_sendmsg(msg);
500}
501
Harald Welte39274f42009-07-29 15:41:29 +0200502static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
503 struct gsm_lchan *lchan)
504{
505 memset(cm, 0, sizeof(cm));
506
507 /* FIXME: what to do with data calls ? */
508 cm->dtx_dtu = 0x00;
509
510 /* set TCH Speech/Data */
511 cm->spd_ind = lchan->rsl_cmode;
512
Harald Welte951e3512009-11-27 08:55:16 +0100513 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
514 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100515 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte951e3512009-11-27 08:55:16 +0100516 "but tch_mode != signalling\n");
517
Harald Welte39274f42009-07-29 15:41:29 +0200518 switch (lchan->type) {
519 case GSM_LCHAN_SDCCH:
520 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
521 break;
522 case GSM_LCHAN_TCH_F:
523 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
524 break;
525 case GSM_LCHAN_TCH_H:
526 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
527 break;
528 case GSM_LCHAN_NONE:
529 case GSM_LCHAN_UNKNOWN:
530 default:
531 return -EINVAL;
532 }
533
534 switch (lchan->tch_mode) {
535 case GSM48_CMODE_SIGN:
536 cm->chan_rate = 0;
537 break;
538 case GSM48_CMODE_SPEECH_V1:
539 cm->chan_rate = RSL_CMOD_SP_GSM1;
540 break;
541 case GSM48_CMODE_SPEECH_EFR:
542 cm->chan_rate = RSL_CMOD_SP_GSM2;
543 break;
544 case GSM48_CMODE_SPEECH_AMR:
545 cm->chan_rate = RSL_CMOD_SP_GSM3;
546 break;
547 case GSM48_CMODE_DATA_14k5:
548 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
549 break;
550 case GSM48_CMODE_DATA_12k0:
551 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
552 break;
553 case GSM48_CMODE_DATA_6k0:
554 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
555 break;
556 default:
557 return -EINVAL;
558 }
559
560 return 0;
561}
562
Harald Welte59b04682009-06-10 05:40:52 +0800563/* Chapter 8.4.1 */
564#if 0
565int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
566 u_int8_t act_type,
567 struct rsl_ie_chan_mode *chan_mode,
568 struct rsl_ie_chan_ident *chan_ident,
569 u_int8_t bs_power, u_int8_t ms_power,
570 u_int8_t ta)
571{
572 struct abis_rsl_dchan_hdr *dh;
573 struct msgb *msg = rsl_msgb_alloc();
574
575 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
576 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
577 dh->chan_nr = chan_nr;
578
579 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
580 /* For compatibility with Phase 1 */
581 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
582 (u_int8_t *) chan_mode);
583 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
584 (u_int8_t *) chan_ident);
585#if 0
586 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
587 (u_int8_t *) &encr_info);
588#endif
589 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
590 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
591 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
592
593 msg->trx = trx;
594
595 return abis_rsl_sendmsg(msg);
596}
597#endif
598
599int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welteb90d7bd2009-12-17 00:31:10 +0100600 u_int8_t ta, u_int8_t ho_ref)
Harald Welte59b04682009-06-10 05:40:52 +0800601{
602 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200603 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200604 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800605
606 u_int8_t chan_nr = lchan2chan_nr(lchan);
607 u_int16_t arfcn = lchan->ts->trx->arfcn;
608 struct rsl_ie_chan_mode cm;
609 struct rsl_ie_chan_ident ci;
610
Harald Welte39274f42009-07-29 15:41:29 +0200611 rc = channel_mode_from_lchan(&cm, lchan);
612 if (rc < 0)
613 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800614
615 memset(&ci, 0, sizeof(ci));
616 ci.chan_desc.iei = 0x64;
617 ci.chan_desc.chan_nr = chan_nr;
618 ci.chan_desc.oct3 = (lchan->ts->trx->bts->tsc << 5) | ((arfcn & 0x3ff) >> 8);
619 ci.chan_desc.oct4 = arfcn & 0xff;
620
Harald Welteed831842009-06-27 03:09:08 +0200621 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800622 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
623 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
624 dh->chan_nr = chan_nr;
625
626 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte59b04682009-06-10 05:40:52 +0800627 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
628 (u_int8_t *) &cm);
Harald Welteb90d7bd2009-12-17 00:31:10 +0100629 /* For compatibility with Phase 1 */
Harald Welte59b04682009-06-10 05:40:52 +0800630 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
631 (u_int8_t *) &ci);
Harald Welted2dd9de2009-08-30 15:37:11 +0900632
633 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
634 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
635 rc = build_encr_info(encr_info, lchan);
636 if (rc > 0)
637 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
638 }
639
Harald Welteb90d7bd2009-12-17 00:31:10 +0100640 switch (act_type) {
641 case RSL_ACT_INTER_ASYNC:
642 case RSL_ACT_INTER_SYNC:
643 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
644 break;
645 default:
646 break;
647 }
648
Harald Welte59b04682009-06-10 05:40:52 +0800649 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
650 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
651 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
652
653 msg->trx = lchan->ts->trx;
654
655 return abis_rsl_sendmsg(msg);
656}
657
Harald Welte8e770492009-07-29 11:38:15 +0200658/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welte59b04682009-06-10 05:40:52 +0800659int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
660{
661 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200662 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200663 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800664
665 u_int8_t chan_nr = lchan2chan_nr(lchan);
666 struct rsl_ie_chan_mode cm;
667
Harald Welte39274f42009-07-29 15:41:29 +0200668 rc = channel_mode_from_lchan(&cm, lchan);
669 if (rc < 0)
670 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800671
Harald Welteed831842009-06-27 03:09:08 +0200672 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800673 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
674 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
675 dh->chan_nr = chan_nr;
676
677 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
678 (u_int8_t *) &cm);
Harald Welted2dd9de2009-08-30 15:37:11 +0900679
680 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
681 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
682 rc = build_encr_info(encr_info, lchan);
683 if (rc > 0)
684 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
685 }
686
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +0100687 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
688 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
689 (u_int8_t *) &lchan->mr_conf);
690 }
691
Harald Welted2dd9de2009-08-30 15:37:11 +0900692 msg->trx = lchan->ts->trx;
693
694 return abis_rsl_sendmsg(msg);
695}
696
697/* Chapter 8.4.6: Send the encryption command with given L3 info */
698int rsl_encryption_cmd(struct msgb *msg)
699{
700 struct abis_rsl_dchan_hdr *dh;
701 struct gsm_lchan *lchan = msg->lchan;
702 u_int8_t chan_nr = lchan2chan_nr(lchan);
703 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut01f1caf2009-09-27 11:13:18 +0200704 u_int8_t l3_len = msg->len;
Harald Welted2dd9de2009-08-30 15:37:11 +0900705 int rc;
706
707 /* First push the L3 IE tag and length */
708 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
709
710 /* then the link identifier (SAPI0, main sign link) */
711 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
712
713 /* then encryption information */
714 rc = build_encr_info(encr_info, lchan);
715 if (rc <= 0)
716 return rc;
717 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
718
719 /* and finally the DCHAN header */
720 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
721 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
722 dh->chan_nr = chan_nr;
Harald Welte59b04682009-06-10 05:40:52 +0800723
724 msg->trx = lchan->ts->trx;
725
726 return abis_rsl_sendmsg(msg);
727}
728
Harald Welte85a163c2009-08-10 11:43:22 +0200729/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteafe3c232009-07-19 18:36:49 +0200730int rsl_deact_sacch(struct gsm_lchan *lchan)
731{
732 struct abis_rsl_dchan_hdr *dh;
733 struct msgb *msg = rsl_msgb_alloc();
734
735 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
736 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
737 dh->chan_nr = lchan2chan_nr(lchan);
738
739 msg->lchan = lchan;
740 msg->trx = lchan->ts->trx;
741
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100742 DEBUGP(DRSL, "%s DEACTivate SACCH CMD\n", gsm_lchan_name(lchan));
Harald Welteafe3c232009-07-19 18:36:49 +0200743
744 return abis_rsl_sendmsg(msg);
745}
746
Harald Welte85a163c2009-08-10 11:43:22 +0200747/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
748int rsl_rf_chan_release(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800749{
750 struct abis_rsl_dchan_hdr *dh;
751 struct msgb *msg = rsl_msgb_alloc();
752
753 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
754 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
755 dh->chan_nr = lchan2chan_nr(lchan);
756
757 msg->lchan = lchan;
758 msg->trx = lchan->ts->trx;
759
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100760 DEBUGP(DRSL, "%s RF Channel Release CMD\n", gsm_lchan_name(lchan));
Harald Welte59b04682009-06-10 05:40:52 +0800761
Harald Welte85a163c2009-08-10 11:43:22 +0200762 /* BTS will respond by RF CHAN REL ACK */
Harald Welte59b04682009-06-10 05:40:52 +0800763 return abis_rsl_sendmsg(msg);
764}
765
766int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
767 u_int8_t *ms_ident, u_int8_t chan_needed)
768{
769 struct abis_rsl_dchan_hdr *dh;
770 struct msgb *msg = rsl_msgb_alloc();
771
772 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
773 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
774 dh->chan_nr = RSL_CHAN_PCH_AGCH;
775
776 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
777 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
778 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
779
780 msg->trx = bts->c0;
781
782 return abis_rsl_sendmsg(msg);
783}
784
785int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
786 struct gsm_subscriber *subscr)
787{
788#if 0
789 u_int8_t mi[128];
790 unsigned int mi_len;
791 u_int8_t paging_group;
792#endif
793
794 return -1;
795}
796
797int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
798{
799 int i, len = strlen(str_in);
800
801 for (i = 0; i < len; i++) {
802 int num = str_in[i] - 0x30;
803 if (num < 0 || num > 9)
804 return -1;
805 if (i % 2 == 0)
806 bcd_out[i/2] = num;
807 else
808 bcd_out[i/2] |= (num << 4);
809 }
810
811 return 0;
812}
813
814/* Chapter 8.5.6 */
815int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
816{
817 struct msgb *msg = rsl_msgb_alloc();
818 struct abis_rsl_dchan_hdr *dh;
819 u_int8_t buf[MACBLOCK_SIZE];
820
821 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
822 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
823 dh->chan_nr = RSL_CHAN_PCH_AGCH;
824
825 switch (bts->type) {
826 case GSM_BTS_TYPE_BS11:
827 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
828 break;
829 default:
830 /* If phase 2, construct a FULL_IMM_ASS_INFO */
831 pad_macblock(buf, val, len);
832 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
833 break;
834 }
835
836 msg->trx = bts->c0;
837
838 return abis_rsl_sendmsg(msg);
839}
840
Harald Welte4684e632009-08-10 09:51:40 +0200841/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte12090752009-08-10 10:07:33 +0200842int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte4684e632009-08-10 09:51:40 +0200843{
844 struct msgb *msg = rsl_msgb_alloc();
845 struct abis_rsl_dchan_hdr *dh;
846
847 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
848 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte874a5b42009-08-10 11:26:14 +0200849 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte4684e632009-08-10 09:51:40 +0200850 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte12090752009-08-10 10:07:33 +0200851 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte4684e632009-08-10 09:51:40 +0200852
Harald Weltede4477a2009-12-24 12:20:20 +0100853 DEBUGP(DRSL, "%s TX Siemens MRPCI 0x%02x\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100854 gsm_lchan_name(lchan), *(u_int8_t *)mrpci);
Harald Welte874a5b42009-08-10 11:26:14 +0200855
856 msg->trx = lchan->ts->trx;
857
Harald Welte4684e632009-08-10 09:51:40 +0200858 return abis_rsl_sendmsg(msg);
859}
860
861
Harald Welte59b04682009-06-10 05:40:52 +0800862/* Send "DATA REQUEST" message with given L3 Info payload */
863/* Chapter 8.3.1 */
864int rsl_data_request(struct msgb *msg, u_int8_t link_id)
865{
866 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
867 struct abis_rsl_rll_hdr *rh;
868
869 if (msg->lchan == NULL) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100870 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte59b04682009-06-10 05:40:52 +0800871 return -EINVAL;
872 }
873
874 /* First push the L3 IE tag and length */
875 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
876
877 /* Then push the RSL header */
878 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
879 init_llm_hdr(rh, RSL_MT_DATA_REQ);
880 rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
881 rh->chan_nr = lchan2chan_nr(msg->lchan);
882 rh->link_id = link_id;
883
884 msg->trx = msg->lchan->ts->trx;
885
886 return abis_rsl_sendmsg(msg);
887}
888
Harald Welteed9a5ab2009-08-09 13:47:35 +0200889/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
890/* Chapter 8.3.1 */
891int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
892{
893 struct msgb *msg = rsl_msgb_alloc();
894 struct abis_rsl_rll_hdr *rh;
895
896 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
Harald Welte61402172009-08-09 14:13:58 +0200897 init_llm_hdr(rh, RSL_MT_EST_REQ);
Harald Welteed9a5ab2009-08-09 13:47:35 +0200898 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
899 rh->chan_nr = lchan2chan_nr(lchan);
900 rh->link_id = link_id;
901
902 msg->trx = lchan->ts->trx;
903
904 return abis_rsl_sendmsg(msg);
905}
906
Harald Welte0f2e3c12009-08-08 13:15:07 +0200907/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
908 This is what higher layers should call. The BTS then responds with
909 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
910 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
911 lchan_free() */
912int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id)
913{
914 struct msgb *msg = rsl_msgb_alloc();
915 struct abis_rsl_rll_hdr *rh;
916
917 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
918 init_llm_hdr(rh, RSL_MT_REL_REQ);
919 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
920 rh->chan_nr = lchan2chan_nr(lchan);
921 rh->link_id = link_id;
Harald Weltead738562009-08-10 00:19:36 +0200922 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, 0); /* normal release */
Harald Welte0f2e3c12009-08-08 13:15:07 +0200923
Harald Weltec88a4432009-12-29 10:44:17 +0100924 lchan->state = LCHAN_S_REL_REQ;
925 /* FIXME: start some timer in case we don't receive a REL ACK ? */
926
Harald Welte0f2e3c12009-08-08 13:15:07 +0200927 msg->trx = lchan->ts->trx;
928
929 return abis_rsl_sendmsg(msg);
930}
931
Harald Welte59b04682009-06-10 05:40:52 +0800932/* Chapter 8.4.2: Channel Activate Acknowledge */
933static int rsl_rx_chan_act_ack(struct msgb *msg)
934{
935 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
936
937 /* BTS has confirmed channel activation, we now need
938 * to assign the activated channel to the MS */
939 if (rslh->ie_chan != RSL_IE_CHAN_NR)
940 return -EINVAL;
Harald Welte6720a432009-11-29 22:45:52 +0100941
Harald Weltec88a4432009-12-29 10:44:17 +0100942 if (msg->lchan->state != LCHAN_S_ACT_REQ)
Harald Welteab2534c2009-12-29 10:52:38 +0100943 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
944 gsm_lchan_name(msg->lchan),
945 gsm_lchans_name(msg->lchan->state));
Harald Welte4baa9c52009-12-21 13:27:11 +0100946 msg->lchan->state = LCHAN_S_ACTIVE;
947
Harald Welte6720a432009-11-29 22:45:52 +0100948 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
949
Harald Welte59b04682009-06-10 05:40:52 +0800950 return 0;
951}
952
953/* Chapter 8.4.3: Channel Activate NACK */
954static int rsl_rx_chan_act_nack(struct msgb *msg)
955{
956 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
957 struct tlv_parsed tp;
958
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100959 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100960 gsm_lchan_name(msg->lchan));
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100961
Harald Welte59b04682009-06-10 05:40:52 +0800962 /* BTS has rejected channel activation ?!? */
963 if (dh->ie_chan != RSL_IE_CHAN_NR)
964 return -EINVAL;
965
966 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100967 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
968 const u_int8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
969 print_rsl_cause(LOGL_ERROR, cause,
Harald Weltef1a168d2009-07-28 17:58:09 +0200970 TLVP_LEN(&tp, RSL_IE_CAUSE));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100971 if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC)
972 msg->lchan->state = LCHAN_S_NONE;
973 } else
974 msg->lchan->state = LCHAN_S_NONE;
975
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100976 LOGPC(DRSL, LOGL_ERROR, "\n");
977
Harald Welte6720a432009-11-29 22:45:52 +0100978 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
979
Harald Weltecddb9802009-08-09 19:50:08 +0200980 lchan_free(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800981 return 0;
982}
983
984/* Chapter 8.4.4: Connection Failure Indication */
985static int rsl_rx_conn_fail(struct msgb *msg)
986{
987 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
988 struct tlv_parsed tp;
989
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100990 /* FIXME: print which channel */
Harald Welte (local)4bd76642009-12-26 22:33:09 +0100991 LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL: RELEASING ",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100992 gsm_lchan_name(msg->lchan));
Harald Welte59b04682009-06-10 05:40:52 +0800993
994 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
995
Harald Weltef1a168d2009-07-28 17:58:09 +0200996 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +0100997 print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +0200998 TLVP_LEN(&tp, RSL_IE_CAUSE));
999
Harald Welte (local)4bd76642009-12-26 22:33:09 +01001000 LOGPC(DRSL, LOGL_NOTICE, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001001 /* FIXME: only free it after channel release ACK */
Harald Welte85a163c2009-08-10 11:43:22 +02001002 return rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001003}
1004
Harald Weltec20bd1d2009-11-29 19:07:28 +01001005static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
1006 const char *prefix)
1007{
Harald Welte0e4fa782009-12-16 16:52:07 +01001008 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
1009 prefix, rxlev2dbm(mru->full.rx_lev),
1010 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Weltec20bd1d2009-11-29 19:07:28 +01001011 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
1012 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
1013}
1014
1015static void print_meas_rep(struct gsm_meas_rep *mr)
1016{
Harald Welte0e4fa782009-12-16 16:52:07 +01001017 int i;
1018
Harald Weltec20bd1d2009-11-29 19:07:28 +01001019 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
1020
1021 if (mr->flags & MEAS_REP_F_DL_DTX)
1022 DEBUGPC(DMEAS, "DTXd ");
1023
1024 print_meas_rep_uni(&mr->ul, "ul");
1025 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
1026 if (mr->flags & MEAS_REP_F_MS_TO)
1027 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
1028
1029 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte0e4fa782009-12-16 16:52:07 +01001030 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001031 DEBUGPC(DMEAS, "L1_FPC=%u ",
1032 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
1033 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
1034 }
1035
1036 if (mr->flags & MEAS_REP_F_UL_DTX)
1037 DEBUGPC(DMEAS, "DTXu ");
1038 if (mr->flags & MEAS_REP_F_BA1)
1039 DEBUGPC(DMEAS, "BA1 ");
1040 if (!(mr->flags & MEAS_REP_F_DL_VALID))
1041 DEBUGPC(DMEAS, "NOT VALID ");
1042 else
1043 print_meas_rep_uni(&mr->dl, "dl");
1044
1045 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte0b833f82009-12-19 18:33:05 +01001046 if (mr->num_cell == 7)
1047 return;
Harald Welte0e4fa782009-12-16 16:52:07 +01001048 for (i = 0; i < mr->num_cell; i++) {
1049 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
Harald Welte350c2d32009-12-25 23:02:22 +01001050 DEBUGP(DMEAS, "IDX=%u ARFCN=%u BSIC=%u => %d dBm\n",
1051 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
Harald Welte0e4fa782009-12-16 16:52:07 +01001052 }
Harald Weltec20bd1d2009-11-29 19:07:28 +01001053}
1054
Harald Welte59b04682009-06-10 05:40:52 +08001055static int rsl_rx_meas_res(struct msgb *msg)
1056{
1057 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1058 struct tlv_parsed tp;
Harald Weltef9476812009-12-15 21:36:05 +01001059 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001060 u_int8_t len;
1061 const u_int8_t *val;
1062 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001063
Harald Welte4baa9c52009-12-21 13:27:11 +01001064 /* check if this channel is actually active */
1065 /* FIXME: maybe this check should be way more generic/centralized */
Harald Weltec88a4432009-12-29 10:44:17 +01001066 if (msg->lchan->state != LCHAN_S_ACTIVE) {
1067 LOGP(DRSL, LOGL_NOTICE, "%s: MEAS RES for inactive channel\n",
1068 gsm_lchan_name(msg->lchan));
Harald Welte4baa9c52009-12-21 13:27:11 +01001069 return 0;
Harald Weltec88a4432009-12-29 10:44:17 +01001070 }
Harald Welte4baa9c52009-12-21 13:27:11 +01001071
Harald Weltef9476812009-12-15 21:36:05 +01001072 memset(mr, 0, sizeof(*mr));
Harald Welteaa0efa12009-12-16 23:29:34 +01001073 mr->lchan = msg->lchan;
Harald Welte4efcc542009-11-30 19:16:47 +01001074
Harald Welte59b04682009-06-10 05:40:52 +08001075 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1076
Harald Weltec20bd1d2009-11-29 19:07:28 +01001077 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
1078 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
1079 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
1080 return -EIO;
1081
1082 /* Mandatory Parts */
Harald Weltef9476812009-12-15 21:36:05 +01001083 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001084
1085 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
1086 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
1087 if (len >= 3) {
1088 if (val[0] & 0x40)
Harald Weltef9476812009-12-15 21:36:05 +01001089 mr->flags |= MEAS_REP_F_DL_DTX;
1090 mr->ul.full.rx_lev = val[0] & 0x3f;
1091 mr->ul.sub.rx_lev = val[1] & 0x3f;
1092 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
1093 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte59b04682009-06-10 05:40:52 +08001094 }
Harald Weltec20bd1d2009-11-29 19:07:28 +01001095
Harald Weltef9476812009-12-15 21:36:05 +01001096 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001097
1098 /* Optional Parts */
Harald Welte59b04682009-06-10 05:40:52 +08001099 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Weltef9476812009-12-15 21:36:05 +01001100 mr->ms_timing_offset =
Harald Weltec20bd1d2009-11-29 19:07:28 +01001101 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
1102
Harald Weltea1467eb2009-06-20 18:44:35 +02001103 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Weltec20bd1d2009-11-29 19:07:28 +01001104 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Weltef9476812009-12-15 21:36:05 +01001105 mr->flags |= MEAS_REP_F_MS_L1;
1106 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001107 if (val[0] & 0x04)
Harald Weltef9476812009-12-15 21:36:05 +01001108 mr->flags |= MEAS_REP_F_FPC;
1109 mr->ms_l1.ta = val[1];
Harald Weltea1467eb2009-06-20 18:44:35 +02001110 }
Harald Welte59b04682009-06-10 05:40:52 +08001111 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freyther6d0c8b42009-10-22 15:43:55 +02001112 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Weltef9476812009-12-15 21:36:05 +01001113 rc = gsm48_parse_meas_rep(mr, msg);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001114 if (rc < 0)
1115 return rc;
1116 }
1117
Harald Weltef9476812009-12-15 21:36:05 +01001118 print_meas_rep(mr);
Harald Welte59b04682009-06-10 05:40:52 +08001119
Harald Weltef9476812009-12-15 21:36:05 +01001120 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Welte4efcc542009-11-30 19:16:47 +01001121
Harald Welte59b04682009-06-10 05:40:52 +08001122 return 0;
1123}
1124
Harald Welte6720a432009-11-29 22:45:52 +01001125/* Chapter 8.4.7 */
1126static int rsl_rx_hando_det(struct msgb *msg)
1127{
1128 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1129 struct tlv_parsed tp;
1130
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001131 DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
Harald Welte6720a432009-11-29 22:45:52 +01001132
1133 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1134
1135 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
1136 DEBUGPC(DRSL, "access delay = %u\n",
1137 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
1138 else
1139 DEBUGPC(DRSL, "\n");
1140
1141 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
1142
1143 return 0;
1144}
1145
Harald Welte59b04682009-06-10 05:40:52 +08001146static int abis_rsl_rx_dchan(struct msgb *msg)
1147{
1148 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1149 int rc = 0;
1150 char *ts_name;
1151
1152 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001153 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001154
Harald Welte59b04682009-06-10 05:40:52 +08001155 switch (rslh->c.msg_type) {
1156 case RSL_MT_CHAN_ACTIV_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001157 DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001158 rc = rsl_rx_chan_act_ack(msg);
1159 break;
1160 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001161 rc = rsl_rx_chan_act_nack(msg);
1162 break;
1163 case RSL_MT_CONN_FAIL:
1164 rc = rsl_rx_conn_fail(msg);
1165 break;
1166 case RSL_MT_MEAS_RES:
1167 rc = rsl_rx_meas_res(msg);
1168 break;
Harald Welte6720a432009-11-29 22:45:52 +01001169 case RSL_MT_HANDO_DET:
1170 rc = rsl_rx_hando_det(msg);
1171 break;
Harald Welte59b04682009-06-10 05:40:52 +08001172 case RSL_MT_RF_CHAN_REL_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001173 DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", ts_name);
Harald Weltec88a4432009-12-29 10:44:17 +01001174 if (msg->lchan->state != LCHAN_S_REL_REQ)
Harald Welteab2534c2009-12-29 10:52:38 +01001175 LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
1176 gsm_lchan_name(msg->lchan),
1177 gsm_lchans_name(msg->lchan->state));
Harald Welte4baa9c52009-12-21 13:27:11 +01001178 msg->lchan->state = LCHAN_S_NONE;
Harald Welte59b04682009-06-10 05:40:52 +08001179 lchan_free(msg->lchan);
1180 break;
1181 case RSL_MT_MODE_MODIFY_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001182 DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001183 break;
1184 case RSL_MT_MODE_MODIFY_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001185 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001186 break;
Harald Welteaed946e2009-10-24 10:29:22 +02001187 case RSL_MT_IPAC_PDCH_ACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001188 DEBUGPC(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001189 break;
1190 case RSL_MT_IPAC_PDCH_ACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001191 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001192 break;
1193 case RSL_MT_IPAC_PDCH_DEACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001194 DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001195 break;
1196 case RSL_MT_IPAC_PDCH_DEACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001197 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001198 break;
Harald Welte59b04682009-06-10 05:40:52 +08001199 case RSL_MT_PHY_CONTEXT_CONF:
1200 case RSL_MT_PREPROC_MEAS_RES:
1201 case RSL_MT_TALKER_DET:
1202 case RSL_MT_LISTENER_DET:
1203 case RSL_MT_REMOTE_CODEC_CONF_REP:
1204 case RSL_MT_MR_CODEC_MOD_ACK:
1205 case RSL_MT_MR_CODEC_MOD_NACK:
1206 case RSL_MT_MR_CODEC_MOD_PER:
Harald Weltede4477a2009-12-24 12:20:20 +01001207 LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
1208 "msg 0x%02x\n", ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001209 break;
1210 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001211 LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
1212 ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001213 return -EINVAL;
1214 }
1215
1216 return rc;
1217}
1218
1219static int rsl_rx_error_rep(struct msgb *msg)
1220{
1221 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Weltef1a168d2009-07-28 17:58:09 +02001222 struct tlv_parsed tp;
Harald Welte59b04682009-06-10 05:40:52 +08001223
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001224 LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(msg->trx));
Harald Weltef1a168d2009-07-28 17:58:09 +02001225
1226 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1227
1228 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001229 print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001230 TLVP_LEN(&tp, RSL_IE_CAUSE));
1231
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001232 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001233
1234 return 0;
1235}
1236
1237static int abis_rsl_rx_trx(struct msgb *msg)
1238{
1239 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1240 int rc = 0;
1241
1242 switch (rslh->msg_type) {
1243 case RSL_MT_ERROR_REPORT:
1244 rc = rsl_rx_error_rep(msg);
1245 break;
1246 case RSL_MT_RF_RES_IND:
1247 /* interference on idle channels of TRX */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001248 //DEBUGP(DRSL, "%s RF Resource Indication\n", gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001249 break;
1250 case RSL_MT_OVERLOAD:
1251 /* indicate CCCH / ACCH / processor overload */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001252 LOGP(DRSL, LOGL_ERROR, "%s CCCH/ACCH/CPU Overload\n",
1253 gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001254 break;
1255 default:
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001256 LOGP(DRSL, LOGL_NOTICE, "%s Unknown Abis RSL TRX message "
1257 "type 0x%02x\n", gsm_trx_name(msg->trx), rslh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001258 return -EINVAL;
1259 }
1260 return rc;
1261}
1262
Harald Welte427dbc42009-08-10 00:26:10 +02001263/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1264static void t3101_expired(void *data)
1265{
1266 struct gsm_lchan *lchan = data;
1267
Harald Welte85a163c2009-08-10 11:43:22 +02001268 rsl_rf_chan_release(lchan);
Harald Welte427dbc42009-08-10 00:26:10 +02001269}
1270
Harald Welte59b04682009-06-10 05:40:52 +08001271/* MS has requested a channel on the RACH */
1272static int rsl_rx_chan_rqd(struct msgb *msg)
1273{
1274 struct gsm_bts *bts = msg->trx->bts;
1275 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1276 struct gsm48_req_ref *rqd_ref;
1277 struct gsm48_imm_ass ia;
1278 enum gsm_chan_t lctype;
1279 enum gsm_chreq_reason_t chreq_reason;
1280 struct gsm_lchan *lchan;
1281 u_int8_t rqd_ta;
1282 int ret;
1283
1284 u_int16_t arfcn;
1285 u_int8_t ts_number, subch;
1286
1287 /* parse request reference to be used in immediate assign */
1288 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1289 return -EINVAL;
1290
1291 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1292
1293 /* parse access delay and use as TA */
1294 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1295 return -EINVAL;
1296 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1297
1298 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1299 * request reference RA */
Holger Hans Peter Freyther96c89822009-11-16 17:12:38 +01001300 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1301 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte59b04682009-06-10 05:40:52 +08001302
Harald Weltebdbb7442009-12-22 19:07:32 +01001303 counter_inc(bts->network->stats.chreq.total);
Harald Welte3edc5a92009-12-22 00:41:05 +01001304
Harald Welte59b04682009-06-10 05:40:52 +08001305 /* check availability / allocate channel */
1306 lchan = lchan_alloc(bts, lctype);
1307 if (!lchan) {
Harald Welte (local)e0bb5fa2009-12-27 13:48:09 +01001308 LOGP(DRSL, LOGL_NOTICE, "BTS %d CHAN RQD: no resources for %s 0x%x\n",
Harald Welte (local)02204d02009-12-27 18:05:25 +01001309 msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
Harald Weltebdbb7442009-12-22 19:07:32 +01001310 counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte59b04682009-06-10 05:40:52 +08001311 /* FIXME: send some kind of reject ?!? */
1312 return -ENOMEM;
1313 }
1314
Harald Weltec88a4432009-12-29 10:44:17 +01001315 if (lchan->state != LCHAN_S_NONE)
1316 LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
Harald Welteab2534c2009-12-29 10:52:38 +01001317 "in state %s\n", gsm_lchan_name(lchan),
1318 gsm_lchans_name(lchan->state));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +01001319 lchan->state = LCHAN_S_ACT_REQ;
1320
Harald Welte59b04682009-06-10 05:40:52 +08001321 ts_number = lchan->ts->nr;
1322 arfcn = lchan->ts->trx->arfcn;
1323 subch = lchan->nr;
1324
Harald Welted2dd9de2009-08-30 15:37:11 +09001325 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)cbd46102009-08-13 10:14:26 +02001326 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte9a229e12009-08-10 00:45:40 +02001327 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte39274f42009-07-29 15:41:29 +02001328 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte77234e12009-08-28 23:28:28 +09001329 lchan->tch_mode = GSM48_CMODE_SIGN;
Harald Welteb90d7bd2009-12-17 00:31:10 +01001330 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001331
1332 /* create IMMEDIATE ASSIGN 04.08 messge */
1333 memset(&ia, 0, sizeof(ia));
1334 ia.l2_plen = 0x2d;
1335 ia.proto_discr = GSM48_PDISC_RR;
1336 ia.msg_type = GSM48_MT_RR_IMM_ASS;
1337 ia.page_mode = GSM48_PM_SAME;
1338 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
1339 ia.chan_desc.h0.h = 0;
1340 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
1341 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
Harald Welte63d23c82009-07-21 20:55:56 +02001342 ia.chan_desc.h0.tsc = bts->tsc;
Harald Welte59b04682009-06-10 05:40:52 +08001343 /* use request reference extracted from CHAN_RQD */
1344 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
1345 ia.timing_advance = rqd_ta;
1346 ia.mob_alloc_len = 0;
1347
Harald Weltede4477a2009-12-24 12:20:20 +01001348 DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001349 "r=%s ra=0x%02x\n", gsm_lchan_name(lchan), arfcn, subch,
Harald Welte (local)02204d02009-12-27 18:05:25 +01001350 gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
Harald Welte59b04682009-06-10 05:40:52 +08001351 rqd_ref->ra);
1352
Harald Welte427dbc42009-08-10 00:26:10 +02001353 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1354 lchan->T3101.cb = t3101_expired;
1355 lchan->T3101.data = lchan;
Holger Hans Peter Freyther26ba2e72009-11-21 21:18:38 +01001356 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001357
1358 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
1359 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
1360
1361 return ret;
1362}
1363
1364/* MS has requested a channel on the RACH */
1365static int rsl_rx_ccch_load(struct msgb *msg)
1366{
1367 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1368 u_int16_t pg_buf_space;
1369 u_int16_t rach_slot_count = -1;
1370 u_int16_t rach_busy_count = -1;
1371 u_int16_t rach_access_count = -1;
1372
1373 switch (rslh->data[0]) {
1374 case RSL_IE_PAGING_LOAD:
1375 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
1376 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
1377 break;
1378 case RSL_IE_RACH_LOAD:
1379 if (msg->data_len >= 7) {
1380 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1381 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1382 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1383 }
1384 break;
1385 default:
1386 break;
1387 }
1388
1389 return 0;
1390}
1391
1392static int abis_rsl_rx_cchan(struct msgb *msg)
1393{
1394 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1395 int rc = 0;
1396
1397 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1398
1399 switch (rslh->c.msg_type) {
1400 case RSL_MT_CHAN_RQD:
1401 /* MS has requested a channel on the RACH */
1402 rc = rsl_rx_chan_rqd(msg);
1403 break;
1404 case RSL_MT_CCCH_LOAD_IND:
1405 /* current load on the CCCH */
1406 rc = rsl_rx_ccch_load(msg);
1407 break;
1408 case RSL_MT_DELETE_IND:
1409 /* CCCH overloaded, IMM_ASSIGN was dropped */
1410 case RSL_MT_CBCH_LOAD_IND:
1411 /* current load on the CBCH */
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001412 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1413 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001414 break;
1415 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001416 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1417 "0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001418 return -EINVAL;
1419 }
1420
1421 return rc;
1422}
1423
1424static int rsl_rx_rll_err_ind(struct msgb *msg)
1425{
1426 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1427 u_int8_t *rlm_cause = rllh->data;
1428
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001429 LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001430 gsm_lchan_name(msg->lchan),
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001431 get_value_string(rlm_cause_strs, rlm_cause[1]));
Harald Welteed9a5ab2009-08-09 13:47:35 +02001432
1433 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001434
Harald Welte692f5852009-07-04 09:40:05 +02001435 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED)
Harald Welte85a163c2009-08-10 11:43:22 +02001436 return rsl_rf_chan_release(msg->lchan);
Harald Welte692f5852009-07-04 09:40:05 +02001437
Harald Welte59b04682009-06-10 05:40:52 +08001438 return 0;
1439}
1440
1441/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
1442 0x02, 0x06,
1443 0x01, 0x20,
1444 0x02, 0x00,
1445 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1446
1447static int abis_rsl_rx_rll(struct msgb *msg)
1448{
1449 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1450 int rc = 0;
1451 char *ts_name;
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001452 u_int8_t sapi = rllh->link_id & 7;
Harald Welte59b04682009-06-10 05:40:52 +08001453
1454 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001455 ts_name = gsm_lchan_name(msg->lchan);
Harald Weltede4477a2009-12-24 12:20:20 +01001456 DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
Harald Welte59b04682009-06-10 05:40:52 +08001457
1458 switch (rllh->c.msg_type) {
1459 case RSL_MT_DATA_IND:
1460 DEBUGPC(DRLL, "DATA INDICATION\n");
1461 if (msgb_l2len(msg) >
1462 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1463 rllh->data[0] == RSL_IE_L3_INFO) {
1464 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001465 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001466 }
1467 break;
1468 case RSL_MT_EST_IND:
1469 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welte427dbc42009-08-10 00:26:10 +02001470 /* lchan is established, stop T3101 */
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001471 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welte427dbc42009-08-10 00:26:10 +02001472 bsc_del_timer(&msg->lchan->T3101);
Harald Welte59b04682009-06-10 05:40:52 +08001473 if (msgb_l2len(msg) >
1474 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1475 rllh->data[0] == RSL_IE_L3_INFO) {
1476 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001477 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001478 }
1479 break;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001480 case RSL_MT_EST_CONF:
Harald Welte61402172009-08-09 14:13:58 +02001481 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001482 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001483 rll_indication(msg->lchan, rllh->link_id,
1484 BSC_RLLR_IND_EST_CONF);
1485 break;
Harald Welte59b04682009-06-10 05:40:52 +08001486 case RSL_MT_REL_IND:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001487 /* BTS informs us of having received DISC from MS */
Harald Welteb6601442009-08-04 02:50:21 +02001488 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001489 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001490 rll_indication(msg->lchan, rllh->link_id,
1491 BSC_RLLR_IND_REL_IND);
Harald Welte0f2e3c12009-08-08 13:15:07 +02001492 /* we can now releae the channel on the BTS/Abis side */
Harald Welte85a163c2009-08-10 11:43:22 +02001493 /* FIXME: officially we need to start T3111 and wait for
1494 * some grace period */
1495 rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001496 break;
1497 case RSL_MT_REL_CONF:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001498 /* BTS informs us of having received UA from MS,
1499 * in response to DISC that we've sent earlier */
Harald Welteb6601442009-08-04 02:50:21 +02001500 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001501 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welte0f2e3c12009-08-08 13:15:07 +02001502 /* we can now releae the channel on the BTS/Abis side */
Harald Welte85a163c2009-08-10 11:43:22 +02001503 /* FIXME: officially we need to start T3111 and wait for
1504 * some grace period */
1505 rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001506 break;
1507 case RSL_MT_ERROR_IND:
Harald Welte59b04682009-06-10 05:40:52 +08001508 rc = rsl_rx_rll_err_ind(msg);
1509 break;
1510 case RSL_MT_UNIT_DATA_IND:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001511 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1512 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001513 break;
1514 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001515 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1516 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001517 }
Harald Welte59b04682009-06-10 05:40:52 +08001518 return rc;
1519}
1520
Harald Welteb284b472009-12-02 01:58:23 +05301521static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Welte98d79f92009-07-28 18:11:56 +02001522{
Harald Welteb284b472009-12-02 01:58:23 +05301523 switch (lchan->tch_mode) {
Harald Welte98d79f92009-07-28 18:11:56 +02001524 case GSM48_CMODE_SPEECH_V1:
Harald Welteb284b472009-12-02 01:58:23 +05301525 switch (lchan->type) {
1526 case GSM_LCHAN_TCH_F:
1527 return 0x00;
1528 case GSM_LCHAN_TCH_H:
1529 return 0x03;
1530 default:
1531 break;
1532 }
Harald Welte98d79f92009-07-28 18:11:56 +02001533 case GSM48_CMODE_SPEECH_EFR:
Harald Welteb284b472009-12-02 01:58:23 +05301534 switch (lchan->type) {
1535 case GSM_LCHAN_TCH_F:
1536 return 0x01;
1537 /* there's no half-rate EFR */
1538 default:
1539 break;
1540 }
Harald Welte98d79f92009-07-28 18:11:56 +02001541 case GSM48_CMODE_SPEECH_AMR:
Harald Welteb284b472009-12-02 01:58:23 +05301542 switch (lchan->type) {
1543 case GSM_LCHAN_TCH_F:
1544 return 0x02;
1545 case GSM_LCHAN_TCH_H:
1546 return 0x05;
1547 default:
1548 break;
1549 }
1550 default:
1551 break;
Harald Welte98d79f92009-07-28 18:11:56 +02001552 }
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001553 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welteb284b472009-12-02 01:58:23 +05301554 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001555 return 0;
Harald Welte98d79f92009-07-28 18:11:56 +02001556}
1557
Harald Welte59b04682009-06-10 05:40:52 +08001558/* ip.access specific RSL extensions */
Harald Weltebffa4992009-12-19 16:42:06 +01001559static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
1560{
1561 struct in_addr ip;
1562 u_int16_t port, conn_id;
1563
1564 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
1565 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_IP));
1566 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
1567 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1568 }
1569
1570 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
1571 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_PORT));
1572 port = ntohs(port);
1573 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
1574 lchan->abis_ip.bound_port = port;
1575 }
1576
1577 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
1578 conn_id = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_CONN_ID));
1579 conn_id = ntohs(conn_id);
1580 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
1581 lchan->abis_ip.conn_id = conn_id;
1582 }
1583
1584 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
1585 lchan->abis_ip.rtp_payload2 =
1586 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1587 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
1588 lchan->abis_ip.rtp_payload2);
1589 }
1590
1591 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
1592 lchan->abis_ip.speech_mode =
1593 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
1594 DEBUGPC(DRSL, "speech_mode=0x%02x ",
1595 lchan->abis_ip.speech_mode);
1596 }
1597
1598 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
1599 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_IP));
1600 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
1601 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
1602 }
1603
1604 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
1605 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_PORT));
1606 port = ntohs(port);
1607 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
1608 lchan->abis_ip.connect_port = port;
1609 }
1610}
1611
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001612int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +08001613{
1614 struct msgb *msg = rsl_msgb_alloc();
1615 struct abis_rsl_dchan_hdr *dh;
1616
1617 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001618 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte59b04682009-06-10 05:40:52 +08001619 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1620 dh->chan_nr = lchan2chan_nr(lchan);
1621
Harald Welte98d79f92009-07-28 18:11:56 +02001622 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001623 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
1624 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001625
Harald Weltede4477a2009-12-24 12:20:20 +01001626 DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001627 gsm_lchan_name(lchan), lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001628
Harald Welte59b04682009-06-10 05:40:52 +08001629 msg->trx = lchan->ts->trx;
1630
1631 return abis_rsl_sendmsg(msg);
1632}
1633
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001634int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Weltebffa4992009-12-19 16:42:06 +01001635 u_int8_t rtp_payload2)
Harald Welte59b04682009-06-10 05:40:52 +08001636{
1637 struct msgb *msg = rsl_msgb_alloc();
1638 struct abis_rsl_dchan_hdr *dh;
Harald Weltebffa4992009-12-19 16:42:06 +01001639 u_int32_t *att_ip;
Harald Welte98d79f92009-07-28 18:11:56 +02001640 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +08001641
1642 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001643 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte59b04682009-06-10 05:40:52 +08001644 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1645 dh->chan_nr = lchan2chan_nr(lchan);
1646
Harald Weltebffa4992009-12-19 16:42:06 +01001647 /* we need to store these now as MDCX_ACK does not return them :( */
1648 lchan->abis_ip.rtp_payload2 = rtp_payload2;
1649 lchan->abis_ip.connect_port = port;
1650 lchan->abis_ip.connect_ip = ip;
1651
Harald Weltefb4a9e92009-07-29 12:12:18 +02001652 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001653 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Harald Weltefb4a9e92009-07-29 12:12:18 +02001654
Harald Welte98d79f92009-07-28 18:11:56 +02001655 ia.s_addr = htonl(ip);
Harald Weltede4477a2009-12-24 12:20:20 +01001656 DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD2=%d CONN_ID=%d "
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001657 "speech_mode=0x%02x\n", gsm_lchan_name(lchan), inet_ntoa(ia), port,
Harald Weltebffa4992009-12-19 16:42:06 +01001658 rtp_payload2, lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001659
Harald Weltebffa4992009-12-19 16:42:06 +01001660 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
1661 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
1662 att_ip = (u_int32_t *) msgb_put(msg, sizeof(ip));
1663 *att_ip = ia.s_addr;
1664 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
1665 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001666 if (rtp_payload2)
1667 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1668
Harald Welte59b04682009-06-10 05:40:52 +08001669 msg->trx = lchan->ts->trx;
1670
1671 return abis_rsl_sendmsg(msg);
1672}
1673
Harald Welte9947d9f2009-12-20 16:51:09 +01001674/* tell BTS to connect RTP stream to our local RTP socket */
1675int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
1676{
1677 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
1678 int rc;
1679
1680 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1681 ntohs(rs->rtp.sin_local.sin_port),
1682 /* FIXME: use RTP payload of bound socket, not BTS*/
1683 lchan->abis_ip.rtp_payload2);
1684
1685 return rc;
1686}
1687
Harald Welteaed946e2009-10-24 10:29:22 +02001688int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan)
1689{
1690 struct msgb *msg = rsl_msgb_alloc();
1691 struct abis_rsl_dchan_hdr *dh;
1692
1693 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1694 init_dchan_hdr(dh, RSL_MT_IPAC_PDCH_ACT);
1695 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1696 dh->chan_nr = lchan2chan_nr(lchan);
1697
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001698 DEBUGP(DRSL, "%s IPAC_PDCH_ACT\n", gsm_lchan_name(lchan));
Harald Welteaed946e2009-10-24 10:29:22 +02001699
1700 msg->trx = lchan->ts->trx;
1701
1702 return abis_rsl_sendmsg(msg);
1703}
1704
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001705static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001706{
1707 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1708 struct tlv_parsed tv;
Harald Welte87504212009-12-02 01:56:49 +05301709 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001710
1711 /* the BTS has acknowledged a local bind, it now tells us the IP
1712 * address and port number to which it has bound the given logical
1713 * channel */
1714
1715 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1716 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1717 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welteb9498952009-07-12 09:45:05 +02001718 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001719 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte59b04682009-06-10 05:40:52 +08001720 return -EINVAL;
1721 }
Harald Welte50517742009-12-20 15:42:44 +01001722
Harald Weltebffa4992009-12-19 16:42:06 +01001723 ipac_parse_rtp(lchan, &tv);
Harald Welte50517742009-12-20 15:42:44 +01001724
1725 /* in case we don't use direct BTS-to-BTS RTP */
1726 if (!ipacc_rtp_direct) {
1727 int rc;
1728 /* the BTS has successfully bound a TCH to a local ip/port,
1729 * which means we can connect our UDP socket to it */
1730 if (lchan->abis_ip.rtp_socket) {
1731 rtp_socket_free(lchan->abis_ip.rtp_socket);
1732 lchan->abis_ip.rtp_socket = NULL;
1733 }
1734
1735 lchan->abis_ip.rtp_socket = rtp_socket_create();
1736 if (!lchan->abis_ip.rtp_socket)
1737 goto out_err;
1738
1739 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1740 lchan->abis_ip.bound_ip,
1741 lchan->abis_ip.bound_port);
1742 if (rc < 0)
1743 goto out_err;
1744 }
1745
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001746 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001747
1748 return 0;
Harald Welte50517742009-12-20 15:42:44 +01001749out_err:
1750 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +08001751}
1752
Harald Weltebffa4992009-12-19 16:42:06 +01001753static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
1754{
1755 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1756 struct tlv_parsed tv;
1757 struct gsm_lchan *lchan = msg->lchan;
1758
1759 /* the BTS has acknowledged a remote connect request and
1760 * it now tells us the IP address and port number to which it has
1761 * connected the given logical channel */
1762
1763 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1764 ipac_parse_rtp(lchan, &tv);
1765 dispatch_signal(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
1766
1767 return 0;
1768}
1769
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001770static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001771{
1772 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1773 struct tlv_parsed tv;
Harald Welte50517742009-12-20 15:42:44 +01001774 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001775
1776 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte59b04682009-06-10 05:40:52 +08001777
Harald Weltef1a168d2009-07-28 17:58:09 +02001778 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001779 print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001780 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte59b04682009-06-10 05:40:52 +08001781
Harald Welte50517742009-12-20 15:42:44 +01001782 /* the BTS tells us a RTP stream has been disconnected */
1783 if (lchan->abis_ip.rtp_socket) {
1784 rtp_socket_free(lchan->abis_ip.rtp_socket);
1785 lchan->abis_ip.rtp_socket = NULL;
1786 }
1787
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001788 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welteba4e58d2009-07-28 18:02:05 +02001789
Harald Welte59b04682009-06-10 05:40:52 +08001790 return 0;
1791}
1792
1793static int abis_rsl_rx_ipacc(struct msgb *msg)
1794{
1795 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltede4477a2009-12-24 12:20:20 +01001796 char *ts_name;
Harald Welte59b04682009-06-10 05:40:52 +08001797 int rc = 0;
1798
1799 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001800 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001801
1802 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001803 case RSL_MT_IPAC_CRCX_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001804 DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001805 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001806 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001807 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001808 /* somehow the BTS was unable to bind the lchan to its local
1809 * port?!? */
Harald Weltede4477a2009-12-24 12:20:20 +01001810 LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001811 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001812 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte59b04682009-06-10 05:40:52 +08001813 /* the BTS tells us that a connect operation was successful */
Harald Weltede4477a2009-12-24 12:20:20 +01001814 DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
Harald Weltebffa4992009-12-19 16:42:06 +01001815 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001816 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001817 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001818 /* somehow the BTS was unable to connect the lchan to a remote
1819 * port */
Harald Weltede4477a2009-12-24 12:20:20 +01001820 LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001821 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001822 case RSL_MT_IPAC_DLCX_IND:
Harald Weltede4477a2009-12-24 12:20:20 +01001823 DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001824 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001825 break;
1826 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001827 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n",
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001828 rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001829 break;
1830 }
1831 DEBUGPC(DRSL, "\n");
1832
1833 return rc;
1834}
1835
1836
1837/* Entry-point where L2 RSL from BTS enters */
1838int abis_rsl_rcvmsg(struct msgb *msg)
1839{
1840 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
1841 int rc = 0;
1842
1843 switch (rslh->msg_discr & 0xfe) {
1844 case ABIS_RSL_MDISC_RLL:
1845 rc = abis_rsl_rx_rll(msg);
1846 break;
1847 case ABIS_RSL_MDISC_DED_CHAN:
1848 rc = abis_rsl_rx_dchan(msg);
1849 break;
1850 case ABIS_RSL_MDISC_COM_CHAN:
1851 rc = abis_rsl_rx_cchan(msg);
1852 break;
1853 case ABIS_RSL_MDISC_TRX:
1854 rc = abis_rsl_rx_trx(msg);
1855 break;
1856 case ABIS_RSL_MDISC_LOC:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001857 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001858 rslh->msg_discr);
1859 break;
1860 case ABIS_RSL_MDISC_IPACCESS:
1861 rc = abis_rsl_rx_ipacc(msg);
1862 break;
1863 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001864 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1865 "0x%02x\n", rslh->msg_discr);
Harald Welte59b04682009-06-10 05:40:52 +08001866 return -EINVAL;
1867 }
1868 msgb_free(msg);
1869 return rc;
1870}
1871
1872
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001873/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
Harald Welte59b04682009-06-10 05:40:52 +08001874int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
1875{
1876 switch (ccch_conf) {
1877 case RSL_BCCH_CCCH_CONF_1_NC:
1878 return 1;
1879 case RSL_BCCH_CCCH_CONF_1_C:
1880 return 1;
1881 case RSL_BCCH_CCCH_CONF_2_NC:
1882 return 2;
1883 case RSL_BCCH_CCCH_CONF_3_NC:
1884 return 3;
1885 case RSL_BCCH_CCCH_CONF_4_NC:
1886 return 4;
1887 default:
1888 return -1;
1889 }
1890}
1891
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001892/* Section 3.3.2.3 TS 05.02 */
Harald Welte59b04682009-06-10 05:40:52 +08001893int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
1894{
1895 switch (ccch_conf) {
1896 case RSL_BCCH_CCCH_CONF_1_NC:
1897 return 0;
1898 case RSL_BCCH_CCCH_CONF_1_C:
1899 return 1;
1900 case RSL_BCCH_CCCH_CONF_2_NC:
1901 return 0;
1902 case RSL_BCCH_CCCH_CONF_3_NC:
1903 return 0;
1904 case RSL_BCCH_CCCH_CONF_4_NC:
1905 return 0;
1906 default:
1907 return -1;
1908 }
1909}
1910
1911/* From Table 10.5.33 of GSM 04.08 */
1912int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1913{
Harald Weltea54a2bb2009-12-01 18:04:30 +05301914 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1915 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1916 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001917 } else {
Harald Weltea54a2bb2009-12-01 18:04:30 +05301918 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1919 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001920 }
1921}