blob: 24bc9ca7909e1b099d48daa12322a0839e51a645 [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Radio Signalling Link messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
Harald Weltea22d36b2010-03-04 10:33:10 +01004/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte59b04682009-06-10 05:40:52 +08005 *
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 Weltef4625b12010-02-20 16:24:02 +010033#include <osmocore/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>
Harald Weltef4625b12010-02-20 16:24:02 +010038#include <osmocore/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080039#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 Welte1fa8bac2010-03-01 21:59:06 +010043#include <osmocore/rsl.h>
Harald Welte59b04682009-06-10 05:40:52 +080044
45#define RSL_ALLOC_SIZE 1024
46#define RSL_ALLOC_HEADROOM 128
47
48#define MAX(a, b) (a) >= (b) ? (a) : (b)
49
Harald Welte59b04682009-06-10 05:40:52 +080050static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
51{
52 /* mask off the transparent bit ? */
53 msg_type &= 0xfe;
54
55 if ((msg_type & 0xf0) == 0x00)
56 return ABIS_RSL_MDISC_RLL;
57 if ((msg_type & 0xf0) == 0x10) {
58 if (msg_type >= 0x19 && msg_type <= 0x22)
59 return ABIS_RSL_MDISC_TRX;
60 else
61 return ABIS_RSL_MDISC_COM_CHAN;
62 }
63 if ((msg_type & 0xe0) == 0x20)
64 return ABIS_RSL_MDISC_DED_CHAN;
65
66 return ABIS_RSL_MDISC_LOC;
67}
68
69static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
70 u_int8_t msg_type)
71{
72 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
73 dh->c.msg_type = msg_type;
74 dh->ie_chan = RSL_IE_CHAN_NR;
75}
76
Harald Welte59b04682009-06-10 05:40:52 +080077/* determine logical channel based on TRX and channel number IE */
78struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
79{
80 struct gsm_lchan *lchan;
81 u_int8_t ts_nr = chan_nr & 0x07;
82 u_int8_t cbits = chan_nr >> 3;
83 u_int8_t lch_idx;
84 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
85
86 if (cbits == 0x01) {
87 lch_idx = 0; /* TCH/F */
Harald Welte37884ed2009-10-24 10:25:50 +020088 if (ts->pchan != GSM_PCHAN_TCH_F &&
89 ts->pchan != GSM_PCHAN_PDCH &&
90 ts->pchan != GSM_PCHAN_TCH_F_PDCH)
Harald Weltecf2ec4a2009-12-17 23:10:46 +010091 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +080092 chan_nr, ts->pchan);
93 } else if ((cbits & 0x1e) == 0x02) {
94 lch_idx = cbits & 0x1; /* TCH/H */
95 if (ts->pchan != GSM_PCHAN_TCH_H)
Harald Weltecf2ec4a2009-12-17 23:10:46 +010096 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +080097 chan_nr, ts->pchan);
98 } else if ((cbits & 0x1c) == 0x04) {
99 lch_idx = cbits & 0x3; /* SDCCH/4 */
100 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100101 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800102 chan_nr, ts->pchan);
103 } else if ((cbits & 0x18) == 0x08) {
104 lch_idx = cbits & 0x7; /* SDCCH/8 */
105 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100106 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800107 chan_nr, ts->pchan);
108 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
109 lch_idx = 0;
110 if (ts->pchan != GSM_PCHAN_CCCH &&
111 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100112 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800113 chan_nr, ts->pchan);
114 /* FIXME: we should not return first sdcch4 !!! */
115 } else {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100116 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +0800117 return NULL;
118 }
119
120 lchan = &ts->lchan[lch_idx];
Harald Welte51d2a592010-03-26 21:28:59 +0800121 log_set_context(BSC_CTX_LCHAN, lchan);
122 log_set_context(BSC_CTX_SUBSCR, lchan->conn.subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800123
124 return lchan;
125}
126
Holger Hans Peter Freyther942ff172009-10-22 11:47:45 +0200127/* See Table 10.5.25 of GSM04.08 */
Harald Welteb5586242009-12-09 19:18:32 +0100128u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800129{
130 struct gsm_bts_trx_ts *ts = lchan->ts;
131 u_int8_t cbits, chan_nr;
132
133 switch (ts->pchan) {
134 case GSM_PCHAN_TCH_F:
Harald Welte37884ed2009-10-24 10:25:50 +0200135 case GSM_PCHAN_PDCH:
136 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte59b04682009-06-10 05:40:52 +0800137 cbits = 0x01;
138 break;
139 case GSM_PCHAN_TCH_H:
140 cbits = 0x02;
141 cbits += lchan->nr;
142 break;
143 case GSM_PCHAN_CCCH_SDCCH4:
144 cbits = 0x04;
145 cbits += lchan->nr;
146 break;
147 case GSM_PCHAN_SDCCH8_SACCH8C:
148 cbits = 0x08;
149 cbits += lchan->nr;
150 break;
151 default:
152 case GSM_PCHAN_CCCH:
153 cbits = 0x10;
154 break;
155 }
156
157 chan_nr = (cbits << 3) | (ts->nr & 0x7);
158
159 return chan_nr;
160}
161
162/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
163u_int64_t str_to_imsi(const char *imsi_str)
164{
165 u_int64_t ret;
166
167 ret = strtoull(imsi_str, NULL, 10);
168
169 return ret;
170}
171
172/* Table 5 Clause 7 TS 05.02 */
173unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
174{
175 if (!bs_ccch_sdcch_comb)
176 return 9 - bs_ag_blks_res;
177 else
178 return 3 - bs_ag_blks_res;
179}
180
181/* Chapter 6.5.2 of TS 05.02 */
182unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
183 unsigned int n_pag_blocks)
184{
185 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
186}
187
188/* Chapter 6.5.2 of TS 05.02 */
189unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
190 int n_pag_blocks)
191{
192 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
193}
194
195static struct msgb *rsl_msgb_alloc(void)
196{
Harald Welte9cfc9352009-06-26 19:39:35 +0200197 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
198 "RSL");
Harald Welte59b04682009-06-10 05:40:52 +0800199}
200
201#define MACBLOCK_SIZE 23
202static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
203{
204 memcpy(out, in, len);
205
206 if (len < MACBLOCK_SIZE)
207 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
208}
209
Harald Welted2dd9de2009-08-30 15:37:11 +0900210/* Chapter 9.3.7: Encryption Information */
211static int build_encr_info(u_int8_t *out, struct gsm_lchan *lchan)
212{
213 *out++ = lchan->encr.alg_id & 0xff;
214 if (lchan->encr.key_len)
215 memcpy(out, lchan->encr.key, lchan->encr.key_len);
216 return lchan->encr.key_len + 1;
217}
218
Harald Weltede4477a2009-12-24 12:20:20 +0100219static void print_rsl_cause(int lvl, const u_int8_t *cause_v, u_int8_t cause_len)
Harald Weltef1a168d2009-07-28 17:58:09 +0200220{
Harald Welte59b04682009-06-10 05:40:52 +0800221 int i;
222
Harald Weltede4477a2009-12-24 12:20:20 +0100223 LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ",
Harald Weltef1a168d2009-07-28 17:58:09 +0200224 cause_v[0], rsl_err_name(cause_v[0]));
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200225 for (i = 1; i < cause_len-1; i++)
Harald Weltede4477a2009-12-24 12:20:20 +0100226 LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
Harald Welte59b04682009-06-10 05:40:52 +0800227}
228
229/* Send a BCCH_INFO message as per Chapter 8.5.1 */
230int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
231 const u_int8_t *data, int len)
232{
233 struct abis_rsl_dchan_hdr *dh;
234 struct msgb *msg = rsl_msgb_alloc();
235
236 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
237 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
238 dh->chan_nr = RSL_CHAN_BCCH;
239
240 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
241 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
242
243 msg->trx = trx;
244
245 return abis_rsl_sendmsg(msg);
246}
247
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200248int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte59b04682009-06-10 05:40:52 +0800249 const u_int8_t *data, int len)
250{
251 struct abis_rsl_common_hdr *ch;
252 struct msgb *msg = rsl_msgb_alloc();
253
254 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
255 ch->msg_discr = ABIS_RSL_MDISC_TRX;
256 ch->msg_type = RSL_MT_SACCH_FILL;
257
258 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
259 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
260
261 msg->trx = trx;
262
263 return abis_rsl_sendmsg(msg);
264}
265
Harald Welte91afe4c2009-06-20 18:15:19 +0200266int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
267{
268 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200269 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200270 u_int8_t chan_nr = lchan2chan_nr(lchan);
271
272 db = abs(db);
273 if (db > 30)
274 return -EINVAL;
275
Harald Welteed831842009-06-27 03:09:08 +0200276 msg = rsl_msgb_alloc();
277
Harald Welte91afe4c2009-06-20 18:15:19 +0200278 lchan->bs_power = db/2;
279 if (fpc)
280 lchan->bs_power |= 0x10;
281
282 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
283 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
284 dh->chan_nr = chan_nr;
285
286 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
287
288 msg->trx = lchan->ts->trx;
289
290 return abis_rsl_sendmsg(msg);
291}
292
Harald Welte91afe4c2009-06-20 18:15:19 +0200293int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
294{
295 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200296 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200297 u_int8_t chan_nr = lchan2chan_nr(lchan);
298 int ctl_lvl;
299
Harald Weltec4dcda02009-08-09 14:45:18 +0200300 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Welte91afe4c2009-06-20 18:15:19 +0200301 if (ctl_lvl < 0)
302 return ctl_lvl;
303
Harald Welteed831842009-06-27 03:09:08 +0200304 msg = rsl_msgb_alloc();
305
Harald Welte91afe4c2009-06-20 18:15:19 +0200306 lchan->ms_power = ctl_lvl;
307
308 if (fpc)
309 lchan->ms_power |= 0x20;
310
311 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
312 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
313 dh->chan_nr = chan_nr;
314
315 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
316
317 msg->trx = lchan->ts->trx;
318
319 return abis_rsl_sendmsg(msg);
320}
321
Harald Welte39274f42009-07-29 15:41:29 +0200322static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
323 struct gsm_lchan *lchan)
324{
325 memset(cm, 0, sizeof(cm));
326
327 /* FIXME: what to do with data calls ? */
328 cm->dtx_dtu = 0x00;
329
330 /* set TCH Speech/Data */
331 cm->spd_ind = lchan->rsl_cmode;
332
Harald Welte951e3512009-11-27 08:55:16 +0100333 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
334 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100335 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte951e3512009-11-27 08:55:16 +0100336 "but tch_mode != signalling\n");
337
Harald Welte39274f42009-07-29 15:41:29 +0200338 switch (lchan->type) {
339 case GSM_LCHAN_SDCCH:
340 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
341 break;
342 case GSM_LCHAN_TCH_F:
343 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
344 break;
345 case GSM_LCHAN_TCH_H:
346 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
347 break;
348 case GSM_LCHAN_NONE:
349 case GSM_LCHAN_UNKNOWN:
350 default:
351 return -EINVAL;
352 }
353
354 switch (lchan->tch_mode) {
355 case GSM48_CMODE_SIGN:
356 cm->chan_rate = 0;
357 break;
358 case GSM48_CMODE_SPEECH_V1:
359 cm->chan_rate = RSL_CMOD_SP_GSM1;
360 break;
361 case GSM48_CMODE_SPEECH_EFR:
362 cm->chan_rate = RSL_CMOD_SP_GSM2;
363 break;
364 case GSM48_CMODE_SPEECH_AMR:
365 cm->chan_rate = RSL_CMOD_SP_GSM3;
366 break;
367 case GSM48_CMODE_DATA_14k5:
368 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
369 break;
370 case GSM48_CMODE_DATA_12k0:
371 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
372 break;
373 case GSM48_CMODE_DATA_6k0:
374 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
375 break;
376 default:
377 return -EINVAL;
378 }
379
380 return 0;
381}
382
Harald Welte59b04682009-06-10 05:40:52 +0800383/* Chapter 8.4.1 */
384#if 0
385int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
386 u_int8_t act_type,
387 struct rsl_ie_chan_mode *chan_mode,
388 struct rsl_ie_chan_ident *chan_ident,
389 u_int8_t bs_power, u_int8_t ms_power,
390 u_int8_t ta)
391{
392 struct abis_rsl_dchan_hdr *dh;
393 struct msgb *msg = rsl_msgb_alloc();
394
395 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
396 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
397 dh->chan_nr = chan_nr;
398
399 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
400 /* For compatibility with Phase 1 */
401 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
402 (u_int8_t *) chan_mode);
403 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
404 (u_int8_t *) chan_ident);
405#if 0
406 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
407 (u_int8_t *) &encr_info);
408#endif
409 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
410 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
411 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
412
413 msg->trx = trx;
414
415 return abis_rsl_sendmsg(msg);
416}
417#endif
418
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200419int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welteb90d7bd2009-12-17 00:31:10 +0100420 u_int8_t ta, u_int8_t ho_ref)
Harald Welte59b04682009-06-10 05:40:52 +0800421{
422 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200423 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200424 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800425
426 u_int8_t chan_nr = lchan2chan_nr(lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800427 struct rsl_ie_chan_mode cm;
laforgef723cf02010-06-20 21:38:19 +0200428 struct gsm48_chan_desc cd;
429 uint8_t *msgb_cd;
Harald Welte59b04682009-06-10 05:40:52 +0800430
Harald Welte39274f42009-07-29 15:41:29 +0200431 rc = channel_mode_from_lchan(&cm, lchan);
432 if (rc < 0)
433 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800434
laforgef723cf02010-06-20 21:38:19 +0200435 gsm48_lchan2chan_desc(&cd, lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800436
Harald Welteed831842009-06-27 03:09:08 +0200437 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800438 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
439 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
440 dh->chan_nr = chan_nr;
441
442 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte59b04682009-06-10 05:40:52 +0800443 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
444 (u_int8_t *) &cm);
Harald Welteb90d7bd2009-12-17 00:31:10 +0100445 /* For compatibility with Phase 1 */
laforgef723cf02010-06-20 21:38:19 +0200446 /* RSL IE TAG */
447 msgb_v_put(msg, RSL_IE_CHAN_IDENT);
448 /* RSL IE LEN: 04.08_cd_tag + CD + 04.08_ma_tag + 04.08_ma_len + ma_len */
449 msgb_v_put(msg, 1 + sizeof(struct gsm48_chan_desc) + 2 + lchan->ts->hopping.ma_len);
450 /* GSM 04.08 Chan Desc 2 (TAG + fixed 3 byte length V) */
451 msgb_v_put(msg, GSM48_IE_CHANDESC_2);
452 msgb_cd = msgb_put(msg, sizeof(cd));
453 memcpy(msgb_cd, &cd, sizeof(cd));
454 /* GSM 04.08 Mobile Allocation: TLV */
455 msgb_tlv_put(msg, GSM48_IE_MA_AFTER, lchan->ts->hopping.ma_len,
456 lchan->ts->hopping.ma_data);
457
Harald Welted2dd9de2009-08-30 15:37:11 +0900458 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
459 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
460 rc = build_encr_info(encr_info, lchan);
461 if (rc > 0)
462 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
463 }
464
Harald Welteb90d7bd2009-12-17 00:31:10 +0100465 switch (act_type) {
466 case RSL_ACT_INTER_ASYNC:
467 case RSL_ACT_INTER_SYNC:
468 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
469 break;
470 default:
471 break;
472 }
473
Harald Welte59b04682009-06-10 05:40:52 +0800474 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
475 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
476 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
477
Holger Hans Peter Freyther6fe8ab92010-01-28 04:45:05 +0100478 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
479 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
480 (u_int8_t *) &lchan->mr_conf);
481
Harald Welte59b04682009-06-10 05:40:52 +0800482 msg->trx = lchan->ts->trx;
483
484 return abis_rsl_sendmsg(msg);
485}
486
Harald Welte8e770492009-07-29 11:38:15 +0200487/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welte59b04682009-06-10 05:40:52 +0800488int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
489{
490 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200491 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200492 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800493
494 u_int8_t chan_nr = lchan2chan_nr(lchan);
495 struct rsl_ie_chan_mode cm;
496
Harald Welte39274f42009-07-29 15:41:29 +0200497 rc = channel_mode_from_lchan(&cm, lchan);
498 if (rc < 0)
499 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800500
Harald Welteed831842009-06-27 03:09:08 +0200501 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800502 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
503 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
504 dh->chan_nr = chan_nr;
505
506 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
507 (u_int8_t *) &cm);
Harald Welted2dd9de2009-08-30 15:37:11 +0900508
509 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
510 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
511 rc = build_encr_info(encr_info, lchan);
512 if (rc > 0)
513 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
514 }
515
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +0100516 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
517 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
518 (u_int8_t *) &lchan->mr_conf);
519 }
520
Harald Welted2dd9de2009-08-30 15:37:11 +0900521 msg->trx = lchan->ts->trx;
522
523 return abis_rsl_sendmsg(msg);
524}
525
526/* Chapter 8.4.6: Send the encryption command with given L3 info */
527int rsl_encryption_cmd(struct msgb *msg)
528{
529 struct abis_rsl_dchan_hdr *dh;
530 struct gsm_lchan *lchan = msg->lchan;
531 u_int8_t chan_nr = lchan2chan_nr(lchan);
532 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut01f1caf2009-09-27 11:13:18 +0200533 u_int8_t l3_len = msg->len;
Harald Welted2dd9de2009-08-30 15:37:11 +0900534 int rc;
535
536 /* First push the L3 IE tag and length */
537 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
538
539 /* then the link identifier (SAPI0, main sign link) */
540 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
541
542 /* then encryption information */
543 rc = build_encr_info(encr_info, lchan);
544 if (rc <= 0)
545 return rc;
546 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
547
548 /* and finally the DCHAN header */
549 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
550 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
551 dh->chan_nr = chan_nr;
Harald Welte59b04682009-06-10 05:40:52 +0800552
553 msg->trx = lchan->ts->trx;
554
555 return abis_rsl_sendmsg(msg);
556}
557
Harald Welte85a163c2009-08-10 11:43:22 +0200558/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteafe3c232009-07-19 18:36:49 +0200559int rsl_deact_sacch(struct gsm_lchan *lchan)
560{
561 struct abis_rsl_dchan_hdr *dh;
562 struct msgb *msg = rsl_msgb_alloc();
563
564 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
565 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
566 dh->chan_nr = lchan2chan_nr(lchan);
567
568 msg->lchan = lchan;
569 msg->trx = lchan->ts->trx;
570
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100571 DEBUGP(DRSL, "%s DEACTivate SACCH CMD\n", gsm_lchan_name(lchan));
Harald Welteafe3c232009-07-19 18:36:49 +0200572
573 return abis_rsl_sendmsg(msg);
574}
575
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800576static void error_timeout_cb(void *data)
577{
578 struct gsm_lchan *lchan = data;
579 if (lchan->state != LCHAN_S_REL_ERR) {
580 LOGP(DRSL, LOGL_ERROR, "%s error timeout but not in error state: %d\n",
581 gsm_lchan_name(lchan), lchan->state);
582 return;
583 }
584
585 /* go back to the none state */
586 LOGP(DRSL, LOGL_NOTICE, "%s is back in operation.\n", gsm_lchan_name(lchan));
Holger Hans Peter Freyther456fb9d2010-06-08 11:53:33 +0800587 rsl_lchan_set_state(lchan, LCHAN_S_NONE);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800588}
589
Harald Welte85a163c2009-08-10 11:43:22 +0200590/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800591static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error)
Harald Welte59b04682009-06-10 05:40:52 +0800592{
593 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800594 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800595
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800596 if (lchan->state == LCHAN_S_REL_ERR) {
597 LOGP(DRSL, LOGL_NOTICE, "%s is in error state not sending release.\n",
598 gsm_lchan_name(lchan));
599 return -1;
600 }
601
602 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800603 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
604 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
605 dh->chan_nr = lchan2chan_nr(lchan);
606
607 msg->lchan = lchan;
608 msg->trx = lchan->ts->trx;
609
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800610 DEBUGP(DRSL, "%s RF Channel Release CMD due error %d\n", gsm_lchan_name(lchan), error);
611
612 if (error) {
613 /*
614 * the nanoBTS sends RLL release indications after the channel release. This can
615 * be a problem when we have reassigned the channel to someone else and then can
616 * not figure out who used this channel.
617 */
Holger Hans Peter Freyther456fb9d2010-06-08 11:53:33 +0800618 rsl_lchan_set_state(lchan, LCHAN_S_REL_ERR);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800619 lchan->error_timer.data = lchan;
620 lchan->error_timer.cb = error_timeout_cb;
621 bsc_schedule_timer(&lchan->error_timer,
622 msg->trx->bts->network->T3111 + 2, 0);
623 }
Harald Welte59b04682009-06-10 05:40:52 +0800624
Harald Welte85a163c2009-08-10 11:43:22 +0200625 /* BTS will respond by RF CHAN REL ACK */
Harald Welte59b04682009-06-10 05:40:52 +0800626 return abis_rsl_sendmsg(msg);
627}
628
629int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
630 u_int8_t *ms_ident, u_int8_t chan_needed)
631{
632 struct abis_rsl_dchan_hdr *dh;
633 struct msgb *msg = rsl_msgb_alloc();
634
635 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
636 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
637 dh->chan_nr = RSL_CHAN_PCH_AGCH;
638
639 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
640 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
641 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
642
643 msg->trx = bts->c0;
644
645 return abis_rsl_sendmsg(msg);
646}
647
648int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
649 struct gsm_subscriber *subscr)
650{
651#if 0
652 u_int8_t mi[128];
653 unsigned int mi_len;
654 u_int8_t paging_group;
655#endif
656
657 return -1;
658}
659
660int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
661{
662 int i, len = strlen(str_in);
663
664 for (i = 0; i < len; i++) {
665 int num = str_in[i] - 0x30;
666 if (num < 0 || num > 9)
667 return -1;
668 if (i % 2 == 0)
669 bcd_out[i/2] = num;
670 else
671 bcd_out[i/2] |= (num << 4);
672 }
673
674 return 0;
675}
676
677/* Chapter 8.5.6 */
678int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
679{
680 struct msgb *msg = rsl_msgb_alloc();
681 struct abis_rsl_dchan_hdr *dh;
682 u_int8_t buf[MACBLOCK_SIZE];
683
684 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
685 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
686 dh->chan_nr = RSL_CHAN_PCH_AGCH;
687
688 switch (bts->type) {
689 case GSM_BTS_TYPE_BS11:
690 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
691 break;
692 default:
693 /* If phase 2, construct a FULL_IMM_ASS_INFO */
694 pad_macblock(buf, val, len);
695 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
696 break;
697 }
698
699 msg->trx = bts->c0;
700
701 return abis_rsl_sendmsg(msg);
702}
703
Harald Welte4684e632009-08-10 09:51:40 +0200704/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte12090752009-08-10 10:07:33 +0200705int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte4684e632009-08-10 09:51:40 +0200706{
707 struct msgb *msg = rsl_msgb_alloc();
708 struct abis_rsl_dchan_hdr *dh;
709
710 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
711 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte874a5b42009-08-10 11:26:14 +0200712 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte4684e632009-08-10 09:51:40 +0200713 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte12090752009-08-10 10:07:33 +0200714 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte4684e632009-08-10 09:51:40 +0200715
Harald Weltede4477a2009-12-24 12:20:20 +0100716 DEBUGP(DRSL, "%s TX Siemens MRPCI 0x%02x\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100717 gsm_lchan_name(lchan), *(u_int8_t *)mrpci);
Harald Welte874a5b42009-08-10 11:26:14 +0200718
719 msg->trx = lchan->ts->trx;
720
Harald Welte4684e632009-08-10 09:51:40 +0200721 return abis_rsl_sendmsg(msg);
722}
723
724
Harald Welte59b04682009-06-10 05:40:52 +0800725/* Send "DATA REQUEST" message with given L3 Info payload */
726/* Chapter 8.3.1 */
727int rsl_data_request(struct msgb *msg, u_int8_t link_id)
728{
Harald Welte59b04682009-06-10 05:40:52 +0800729 if (msg->lchan == NULL) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100730 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte59b04682009-06-10 05:40:52 +0800731 return -EINVAL;
732 }
733
Harald Weltea22d36b2010-03-04 10:33:10 +0100734 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, lchan2chan_nr(msg->lchan),
735 link_id, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800736
737 msg->trx = msg->lchan->ts->trx;
738
739 return abis_rsl_sendmsg(msg);
740}
741
Harald Welteed9a5ab2009-08-09 13:47:35 +0200742/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
743/* Chapter 8.3.1 */
744int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
745{
Harald Weltea22d36b2010-03-04 10:33:10 +0100746 struct msgb *msg;
Harald Welteed9a5ab2009-08-09 13:47:35 +0200747
Harald Weltea22d36b2010-03-04 10:33:10 +0100748 msg = rsl_rll_simple(RSL_MT_EST_REQ, lchan2chan_nr(lchan),
749 link_id, 0);
Harald Welteed9a5ab2009-08-09 13:47:35 +0200750 msg->trx = lchan->ts->trx;
751
752 return abis_rsl_sendmsg(msg);
753}
754
Harald Welte0f2e3c12009-08-08 13:15:07 +0200755/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
756 This is what higher layers should call. The BTS then responds with
757 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
758 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
759 lchan_free() */
Holger Hans Peter Freytherbcea9a72010-06-08 11:57:45 +0800760int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id, u_int8_t reason)
Harald Welte0f2e3c12009-08-08 13:15:07 +0200761{
Harald Welte0f2e3c12009-08-08 13:15:07 +0200762
Harald Weltea22d36b2010-03-04 10:33:10 +0100763 struct msgb *msg;
764
765 msg = rsl_rll_simple(RSL_MT_REL_REQ, lchan2chan_nr(lchan),
766 link_id, 0);
Holger Hans Peter Freytherbcea9a72010-06-08 11:57:45 +0800767 /* 0 is normal release, 1 is local end */
768 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, reason);
Harald Welte0f2e3c12009-08-08 13:15:07 +0200769
Harald Weltec88a4432009-12-29 10:44:17 +0100770 /* FIXME: start some timer in case we don't receive a REL ACK ? */
771
Harald Welte0f2e3c12009-08-08 13:15:07 +0200772 msg->trx = lchan->ts->trx;
773
774 return abis_rsl_sendmsg(msg);
775}
776
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200777int rsl_lchan_set_state(struct gsm_lchan *lchan, int state)
778{
779 lchan->state = state;
780 return 0;
781}
782
Harald Welte59b04682009-06-10 05:40:52 +0800783/* Chapter 8.4.2: Channel Activate Acknowledge */
784static int rsl_rx_chan_act_ack(struct msgb *msg)
785{
786 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
787
788 /* BTS has confirmed channel activation, we now need
789 * to assign the activated channel to the MS */
790 if (rslh->ie_chan != RSL_IE_CHAN_NR)
791 return -EINVAL;
Harald Welte6720a432009-11-29 22:45:52 +0100792
Harald Weltec88a4432009-12-29 10:44:17 +0100793 if (msg->lchan->state != LCHAN_S_ACT_REQ)
Harald Welteab2534c2009-12-29 10:52:38 +0100794 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
795 gsm_lchan_name(msg->lchan),
796 gsm_lchans_name(msg->lchan->state));
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200797 rsl_lchan_set_state(msg->lchan, LCHAN_S_ACTIVE);
Harald Welte4baa9c52009-12-21 13:27:11 +0100798
Harald Welte6720a432009-11-29 22:45:52 +0100799 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
800
Harald Welte59b04682009-06-10 05:40:52 +0800801 return 0;
802}
803
804/* Chapter 8.4.3: Channel Activate NACK */
805static int rsl_rx_chan_act_nack(struct msgb *msg)
806{
807 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
808 struct tlv_parsed tp;
809
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100810 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100811 gsm_lchan_name(msg->lchan));
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100812
Harald Welte59b04682009-06-10 05:40:52 +0800813 /* BTS has rejected channel activation ?!? */
814 if (dh->ie_chan != RSL_IE_CHAN_NR)
815 return -EINVAL;
816
817 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100818 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
819 const u_int8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
820 print_rsl_cause(LOGL_ERROR, cause,
Harald Weltef1a168d2009-07-28 17:58:09 +0200821 TLVP_LEN(&tp, RSL_IE_CAUSE));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100822 if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC)
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200823 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100824 } else
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200825 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200826
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100827 LOGPC(DRSL, LOGL_ERROR, "\n");
828
Harald Welte6720a432009-11-29 22:45:52 +0100829 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
830
Harald Weltecddb9802009-08-09 19:50:08 +0200831 lchan_free(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800832 return 0;
833}
834
835/* Chapter 8.4.4: Connection Failure Indication */
836static int rsl_rx_conn_fail(struct msgb *msg)
837{
838 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
839 struct tlv_parsed tp;
840
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100841 /* FIXME: print which channel */
Harald Welte (local)4bd76642009-12-26 22:33:09 +0100842 LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL: RELEASING ",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100843 gsm_lchan_name(msg->lchan));
Harald Welte59b04682009-06-10 05:40:52 +0800844
845 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
846
Harald Weltef1a168d2009-07-28 17:58:09 +0200847 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +0100848 print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +0200849 TLVP_LEN(&tp, RSL_IE_CAUSE));
850
Harald Welte (local)4bd76642009-12-26 22:33:09 +0100851 LOGPC(DRSL, LOGL_NOTICE, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800852 /* FIXME: only free it after channel release ACK */
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +0200853 counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rf_fail);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800854 return rsl_rf_chan_release(msg->lchan, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800855}
856
Harald Weltec20bd1d2009-11-29 19:07:28 +0100857static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
858 const char *prefix)
859{
Harald Welte0e4fa782009-12-16 16:52:07 +0100860 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
861 prefix, rxlev2dbm(mru->full.rx_lev),
862 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Weltec20bd1d2009-11-29 19:07:28 +0100863 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
864 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
865}
866
867static void print_meas_rep(struct gsm_meas_rep *mr)
868{
Harald Welte0e4fa782009-12-16 16:52:07 +0100869 int i;
870
Harald Weltec20bd1d2009-11-29 19:07:28 +0100871 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
872
873 if (mr->flags & MEAS_REP_F_DL_DTX)
874 DEBUGPC(DMEAS, "DTXd ");
875
876 print_meas_rep_uni(&mr->ul, "ul");
877 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
878 if (mr->flags & MEAS_REP_F_MS_TO)
879 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
880
881 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte0e4fa782009-12-16 16:52:07 +0100882 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100883 DEBUGPC(DMEAS, "L1_FPC=%u ",
884 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
885 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
886 }
887
888 if (mr->flags & MEAS_REP_F_UL_DTX)
889 DEBUGPC(DMEAS, "DTXu ");
890 if (mr->flags & MEAS_REP_F_BA1)
891 DEBUGPC(DMEAS, "BA1 ");
892 if (!(mr->flags & MEAS_REP_F_DL_VALID))
893 DEBUGPC(DMEAS, "NOT VALID ");
894 else
895 print_meas_rep_uni(&mr->dl, "dl");
896
897 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte0b833f82009-12-19 18:33:05 +0100898 if (mr->num_cell == 7)
899 return;
Harald Welte0e4fa782009-12-16 16:52:07 +0100900 for (i = 0; i < mr->num_cell; i++) {
901 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
Harald Welte350c2d32009-12-25 23:02:22 +0100902 DEBUGP(DMEAS, "IDX=%u ARFCN=%u BSIC=%u => %d dBm\n",
903 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
Harald Welte0e4fa782009-12-16 16:52:07 +0100904 }
Harald Weltec20bd1d2009-11-29 19:07:28 +0100905}
906
Harald Welte59b04682009-06-10 05:40:52 +0800907static int rsl_rx_meas_res(struct msgb *msg)
908{
909 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
910 struct tlv_parsed tp;
Harald Weltef9476812009-12-15 21:36:05 +0100911 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100912 u_int8_t len;
913 const u_int8_t *val;
914 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800915
Harald Welte4baa9c52009-12-21 13:27:11 +0100916 /* check if this channel is actually active */
917 /* FIXME: maybe this check should be way more generic/centralized */
Harald Weltec88a4432009-12-29 10:44:17 +0100918 if (msg->lchan->state != LCHAN_S_ACTIVE) {
919 LOGP(DRSL, LOGL_NOTICE, "%s: MEAS RES for inactive channel\n",
920 gsm_lchan_name(msg->lchan));
Harald Welte4baa9c52009-12-21 13:27:11 +0100921 return 0;
Harald Weltec88a4432009-12-29 10:44:17 +0100922 }
Harald Welte4baa9c52009-12-21 13:27:11 +0100923
Harald Weltef9476812009-12-15 21:36:05 +0100924 memset(mr, 0, sizeof(*mr));
Harald Welteaa0efa12009-12-16 23:29:34 +0100925 mr->lchan = msg->lchan;
Harald Welte4efcc542009-11-30 19:16:47 +0100926
Harald Welte59b04682009-06-10 05:40:52 +0800927 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
928
Harald Weltec20bd1d2009-11-29 19:07:28 +0100929 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
930 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
931 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
932 return -EIO;
933
934 /* Mandatory Parts */
Harald Weltef9476812009-12-15 21:36:05 +0100935 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100936
937 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
938 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
939 if (len >= 3) {
940 if (val[0] & 0x40)
Harald Weltef9476812009-12-15 21:36:05 +0100941 mr->flags |= MEAS_REP_F_DL_DTX;
942 mr->ul.full.rx_lev = val[0] & 0x3f;
943 mr->ul.sub.rx_lev = val[1] & 0x3f;
944 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
945 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte59b04682009-06-10 05:40:52 +0800946 }
Harald Weltec20bd1d2009-11-29 19:07:28 +0100947
Harald Weltef9476812009-12-15 21:36:05 +0100948 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100949
950 /* Optional Parts */
Harald Welte59b04682009-06-10 05:40:52 +0800951 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Weltef9476812009-12-15 21:36:05 +0100952 mr->ms_timing_offset =
Harald Weltec20bd1d2009-11-29 19:07:28 +0100953 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
954
Harald Weltea1467eb2009-06-20 18:44:35 +0200955 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Weltec20bd1d2009-11-29 19:07:28 +0100956 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Weltef9476812009-12-15 21:36:05 +0100957 mr->flags |= MEAS_REP_F_MS_L1;
958 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100959 if (val[0] & 0x04)
Harald Weltef9476812009-12-15 21:36:05 +0100960 mr->flags |= MEAS_REP_F_FPC;
961 mr->ms_l1.ta = val[1];
Harald Weltea1467eb2009-06-20 18:44:35 +0200962 }
Harald Welte59b04682009-06-10 05:40:52 +0800963 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freyther6d0c8b42009-10-22 15:43:55 +0200964 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Weltef9476812009-12-15 21:36:05 +0100965 rc = gsm48_parse_meas_rep(mr, msg);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100966 if (rc < 0)
967 return rc;
968 }
969
Harald Weltef9476812009-12-15 21:36:05 +0100970 print_meas_rep(mr);
Harald Welte59b04682009-06-10 05:40:52 +0800971
Harald Weltef9476812009-12-15 21:36:05 +0100972 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Welte4efcc542009-11-30 19:16:47 +0100973
Harald Welte59b04682009-06-10 05:40:52 +0800974 return 0;
975}
976
Harald Welte6720a432009-11-29 22:45:52 +0100977/* Chapter 8.4.7 */
978static int rsl_rx_hando_det(struct msgb *msg)
979{
980 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
981 struct tlv_parsed tp;
982
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100983 DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
Harald Welte6720a432009-11-29 22:45:52 +0100984
985 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
986
987 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
988 DEBUGPC(DRSL, "access delay = %u\n",
989 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
990 else
991 DEBUGPC(DRSL, "\n");
992
993 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
994
995 return 0;
996}
997
Harald Welte59b04682009-06-10 05:40:52 +0800998static int abis_rsl_rx_dchan(struct msgb *msg)
999{
1000 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1001 int rc = 0;
1002 char *ts_name;
1003
1004 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001005 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001006
Harald Welte59b04682009-06-10 05:40:52 +08001007 switch (rslh->c.msg_type) {
1008 case RSL_MT_CHAN_ACTIV_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001009 DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001010 rc = rsl_rx_chan_act_ack(msg);
1011 break;
1012 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001013 rc = rsl_rx_chan_act_nack(msg);
1014 break;
1015 case RSL_MT_CONN_FAIL:
1016 rc = rsl_rx_conn_fail(msg);
1017 break;
1018 case RSL_MT_MEAS_RES:
1019 rc = rsl_rx_meas_res(msg);
1020 break;
Harald Welte6720a432009-11-29 22:45:52 +01001021 case RSL_MT_HANDO_DET:
1022 rc = rsl_rx_hando_det(msg);
1023 break;
Harald Welte59b04682009-06-10 05:40:52 +08001024 case RSL_MT_RF_CHAN_REL_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001025 DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", ts_name);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001026 if (msg->lchan->state != LCHAN_S_REL_REQ && msg->lchan->state != LCHAN_S_REL_ERR)
Harald Welteab2534c2009-12-29 10:52:38 +01001027 LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
1028 gsm_lchan_name(msg->lchan),
1029 gsm_lchans_name(msg->lchan->state));
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001030 bsc_del_timer(&msg->lchan->T3111);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001031 /* we have an error timer pending to release that */
1032 if (msg->lchan->state != LCHAN_S_REL_ERR)
1033 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Harald Welte59b04682009-06-10 05:40:52 +08001034 lchan_free(msg->lchan);
1035 break;
1036 case RSL_MT_MODE_MODIFY_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001037 DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001038 break;
1039 case RSL_MT_MODE_MODIFY_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001040 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001041 break;
Harald Welteaed946e2009-10-24 10:29:22 +02001042 case RSL_MT_IPAC_PDCH_ACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001043 DEBUGPC(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
Harald Welte2b361522010-03-28 14:42:09 +08001044 msg->lchan->ts->flags |= TS_F_PDCH_MODE;
Harald Welteaed946e2009-10-24 10:29:22 +02001045 break;
1046 case RSL_MT_IPAC_PDCH_ACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001047 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001048 break;
1049 case RSL_MT_IPAC_PDCH_DEACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001050 DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
Harald Welte2b361522010-03-28 14:42:09 +08001051 msg->lchan->ts->flags &= ~TS_F_PDCH_MODE;
Harald Welteaed946e2009-10-24 10:29:22 +02001052 break;
1053 case RSL_MT_IPAC_PDCH_DEACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001054 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001055 break;
Harald Welte59b04682009-06-10 05:40:52 +08001056 case RSL_MT_PHY_CONTEXT_CONF:
1057 case RSL_MT_PREPROC_MEAS_RES:
1058 case RSL_MT_TALKER_DET:
1059 case RSL_MT_LISTENER_DET:
1060 case RSL_MT_REMOTE_CODEC_CONF_REP:
1061 case RSL_MT_MR_CODEC_MOD_ACK:
1062 case RSL_MT_MR_CODEC_MOD_NACK:
1063 case RSL_MT_MR_CODEC_MOD_PER:
Harald Weltede4477a2009-12-24 12:20:20 +01001064 LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
1065 "msg 0x%02x\n", ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001066 break;
1067 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001068 LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
1069 ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001070 return -EINVAL;
1071 }
1072
1073 return rc;
1074}
1075
1076static int rsl_rx_error_rep(struct msgb *msg)
1077{
1078 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Weltef1a168d2009-07-28 17:58:09 +02001079 struct tlv_parsed tp;
Harald Welte59b04682009-06-10 05:40:52 +08001080
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001081 LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(msg->trx));
Harald Weltef1a168d2009-07-28 17:58:09 +02001082
1083 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1084
1085 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001086 print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001087 TLVP_LEN(&tp, RSL_IE_CAUSE));
1088
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001089 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001090
1091 return 0;
1092}
1093
1094static int abis_rsl_rx_trx(struct msgb *msg)
1095{
1096 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1097 int rc = 0;
1098
1099 switch (rslh->msg_type) {
1100 case RSL_MT_ERROR_REPORT:
1101 rc = rsl_rx_error_rep(msg);
1102 break;
1103 case RSL_MT_RF_RES_IND:
1104 /* interference on idle channels of TRX */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001105 //DEBUGP(DRSL, "%s RF Resource Indication\n", gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001106 break;
1107 case RSL_MT_OVERLOAD:
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001108 /* indicate CCCH / ACCH / processor overload */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001109 LOGP(DRSL, LOGL_ERROR, "%s CCCH/ACCH/CPU Overload\n",
1110 gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001111 break;
1112 default:
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001113 LOGP(DRSL, LOGL_NOTICE, "%s Unknown Abis RSL TRX message "
1114 "type 0x%02x\n", gsm_trx_name(msg->trx), rslh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001115 return -EINVAL;
1116 }
1117 return rc;
1118}
1119
Harald Welte427dbc42009-08-10 00:26:10 +02001120/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1121static void t3101_expired(void *data)
1122{
1123 struct gsm_lchan *lchan = data;
1124
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001125 rsl_rf_chan_release(lchan, 1);
Harald Welte427dbc42009-08-10 00:26:10 +02001126}
1127
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001128/* If T3111 expires, we will send the RF Channel Request */
1129static void t3111_expired(void *data)
1130{
1131 struct gsm_lchan *lchan = data;
1132
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001133 rsl_rf_chan_release(lchan, 0);
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001134}
1135
Harald Welte59b04682009-06-10 05:40:52 +08001136/* MS has requested a channel on the RACH */
1137static int rsl_rx_chan_rqd(struct msgb *msg)
1138{
1139 struct gsm_bts *bts = msg->trx->bts;
1140 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
laforgee06d5982010-06-20 15:18:46 +02001141 u_int8_t buf[GSM_MACBLOCK_LEN];
1142 struct gsm48_imm_ass *ia = (struct gsm48_imm_ass *) buf;
Harald Welte59b04682009-06-10 05:40:52 +08001143 struct gsm48_req_ref *rqd_ref;
Harald Welte59b04682009-06-10 05:40:52 +08001144 enum gsm_chan_t lctype;
1145 enum gsm_chreq_reason_t chreq_reason;
1146 struct gsm_lchan *lchan;
1147 u_int8_t rqd_ta;
1148 int ret;
1149
1150 u_int16_t arfcn;
1151 u_int8_t ts_number, subch;
1152
1153 /* parse request reference to be used in immediate assign */
1154 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1155 return -EINVAL;
1156
1157 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1158
1159 /* parse access delay and use as TA */
1160 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1161 return -EINVAL;
1162 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1163
1164 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1165 * request reference RA */
Holger Hans Peter Freyther96c89822009-11-16 17:12:38 +01001166 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1167 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte59b04682009-06-10 05:40:52 +08001168
Harald Weltebdbb7442009-12-22 19:07:32 +01001169 counter_inc(bts->network->stats.chreq.total);
Harald Welte3edc5a92009-12-22 00:41:05 +01001170
Harald Welte59b04682009-06-10 05:40:52 +08001171 /* check availability / allocate channel */
1172 lchan = lchan_alloc(bts, lctype);
1173 if (!lchan) {
Harald Welte (local)e0bb5fa2009-12-27 13:48:09 +01001174 LOGP(DRSL, LOGL_NOTICE, "BTS %d CHAN RQD: no resources for %s 0x%x\n",
Harald Welte (local)02204d02009-12-27 18:05:25 +01001175 msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
Harald Weltebdbb7442009-12-22 19:07:32 +01001176 counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte59b04682009-06-10 05:40:52 +08001177 /* FIXME: send some kind of reject ?!? */
1178 return -ENOMEM;
1179 }
1180
Harald Weltec88a4432009-12-29 10:44:17 +01001181 if (lchan->state != LCHAN_S_NONE)
1182 LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
Harald Welteab2534c2009-12-29 10:52:38 +01001183 "in state %s\n", gsm_lchan_name(lchan),
1184 gsm_lchans_name(lchan->state));
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +02001185 rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
Harald Welte (local)c3be50c2009-12-27 18:12:29 +01001186
Harald Welte59b04682009-06-10 05:40:52 +08001187 ts_number = lchan->ts->nr;
1188 arfcn = lchan->ts->trx->arfcn;
1189 subch = lchan->nr;
1190
Harald Welted2dd9de2009-08-30 15:37:11 +09001191 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)cbd46102009-08-13 10:14:26 +02001192 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte9a229e12009-08-10 00:45:40 +02001193 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte39274f42009-07-29 15:41:29 +02001194 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte77234e12009-08-28 23:28:28 +09001195 lchan->tch_mode = GSM48_CMODE_SIGN;
Harald Welteb90d7bd2009-12-17 00:31:10 +01001196 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001197
1198 /* create IMMEDIATE ASSIGN 04.08 messge */
laforgee06d5982010-06-20 15:18:46 +02001199 memset(ia, 0, sizeof(*ia));
1200 ia->l2_plen = 0x2d;
1201 ia->proto_discr = GSM48_PDISC_RR;
1202 ia->msg_type = GSM48_MT_RR_IMM_ASS;
1203 ia->page_mode = GSM48_PM_SAME;
1204 gsm48_lchan2chan_desc(&ia->chan_desc, lchan);
Harald Weltea42a93f2010-06-14 22:26:10 +02001205
Harald Welte59b04682009-06-10 05:40:52 +08001206 /* use request reference extracted from CHAN_RQD */
laforgee06d5982010-06-20 15:18:46 +02001207 memcpy(&ia->req_ref, rqd_ref, sizeof(ia->req_ref));
1208 ia->timing_advance = rqd_ta;
Harald Weltea42a93f2010-06-14 22:26:10 +02001209 if (!lchan->ts->hopping.enabled) {
laforgee06d5982010-06-20 15:18:46 +02001210 ia->mob_alloc_len = 0;
Harald Weltea42a93f2010-06-14 22:26:10 +02001211 } else {
laforgee06d5982010-06-20 15:18:46 +02001212 ia->mob_alloc_len = lchan->ts->hopping.ma_len;
1213 memcpy(ia->mob_alloc, lchan->ts->hopping.ma_data, ia->mob_alloc_len);
Harald Weltea42a93f2010-06-14 22:26:10 +02001214 }
Harald Welte59b04682009-06-10 05:40:52 +08001215
Harald Weltede4477a2009-12-24 12:20:20 +01001216 DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001217 "r=%s ra=0x%02x\n", gsm_lchan_name(lchan), arfcn, subch,
Harald Welte (local)02204d02009-12-27 18:05:25 +01001218 gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
Harald Welte59b04682009-06-10 05:40:52 +08001219 rqd_ref->ra);
1220
Harald Welte427dbc42009-08-10 00:26:10 +02001221 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1222 lchan->T3101.cb = t3101_expired;
1223 lchan->T3101.data = lchan;
Holger Hans Peter Freyther26ba2e72009-11-21 21:18:38 +01001224 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001225
1226 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
laforgee06d5982010-06-20 15:18:46 +02001227 ret = rsl_imm_assign_cmd(bts, sizeof(*ia)+ia->mob_alloc_len, (u_int8_t *) ia);
Harald Welte59b04682009-06-10 05:40:52 +08001228
1229 return ret;
1230}
1231
1232/* MS has requested a channel on the RACH */
1233static int rsl_rx_ccch_load(struct msgb *msg)
1234{
1235 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1236 u_int16_t pg_buf_space;
1237 u_int16_t rach_slot_count = -1;
1238 u_int16_t rach_busy_count = -1;
1239 u_int16_t rach_access_count = -1;
1240
1241 switch (rslh->data[0]) {
1242 case RSL_IE_PAGING_LOAD:
1243 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
Harald Welte008a4922010-04-19 10:24:07 +02001244 if (is_ipaccess_bts(msg->trx->bts) && pg_buf_space == 0xffff) {
1245 /* paging load below configured threshold, use 50 as default */
1246 pg_buf_space = 50;
1247 }
Harald Welte59b04682009-06-10 05:40:52 +08001248 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
1249 break;
1250 case RSL_IE_RACH_LOAD:
1251 if (msg->data_len >= 7) {
1252 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1253 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1254 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1255 }
1256 break;
1257 default:
1258 break;
1259 }
1260
1261 return 0;
1262}
1263
1264static int abis_rsl_rx_cchan(struct msgb *msg)
1265{
1266 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1267 int rc = 0;
1268
1269 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1270
1271 switch (rslh->c.msg_type) {
1272 case RSL_MT_CHAN_RQD:
1273 /* MS has requested a channel on the RACH */
1274 rc = rsl_rx_chan_rqd(msg);
1275 break;
1276 case RSL_MT_CCCH_LOAD_IND:
1277 /* current load on the CCCH */
1278 rc = rsl_rx_ccch_load(msg);
1279 break;
1280 case RSL_MT_DELETE_IND:
1281 /* CCCH overloaded, IMM_ASSIGN was dropped */
1282 case RSL_MT_CBCH_LOAD_IND:
1283 /* current load on the CBCH */
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001284 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1285 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001286 break;
1287 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001288 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1289 "0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001290 return -EINVAL;
1291 }
1292
1293 return rc;
1294}
1295
1296static int rsl_rx_rll_err_ind(struct msgb *msg)
1297{
1298 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1299 u_int8_t *rlm_cause = rllh->data;
1300
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001301 LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001302 gsm_lchan_name(msg->lchan),
Harald Welteb30935e2010-03-25 12:13:02 +08001303 rsl_rlm_cause_name(rlm_cause[1]));
Harald Welteed9a5ab2009-08-09 13:47:35 +02001304
1305 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001306
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +02001307 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED) {
1308 counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rll_err);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001309 return rsl_rf_chan_release(msg->lchan, 1);
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +02001310 }
Harald Welte692f5852009-07-04 09:40:05 +02001311
Harald Welte59b04682009-06-10 05:40:52 +08001312 return 0;
1313}
1314
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001315static void rsl_handle_release(struct gsm_lchan *lchan)
1316{
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001317 struct gsm_bts *bts;
Holger Hans Peter Freytherd26cbc82010-04-08 22:47:44 +02001318 if (lchan->state != LCHAN_S_REL_REQ)
1319 LOGP(DRSL, LOGL_ERROR, "RF release on %s but state %s\n",
1320 gsm_lchan_name(lchan),
1321 gsm_lchans_name(lchan->state));
1322
1323
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001324 /* wait a bit to send the RF Channel Release */
1325 lchan->T3111.cb = t3111_expired;
1326 lchan->T3111.data = lchan;
1327 bts = lchan->ts->trx->bts;
1328 bsc_schedule_timer(&lchan->T3111, bts->network->T3111, 0);
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001329}
1330
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001331/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
Harald Welte59b04682009-06-10 05:40:52 +08001332 0x02, 0x06,
1333 0x01, 0x20,
1334 0x02, 0x00,
1335 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1336
1337static int abis_rsl_rx_rll(struct msgb *msg)
1338{
1339 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1340 int rc = 0;
1341 char *ts_name;
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001342 u_int8_t sapi = rllh->link_id & 7;
Harald Welte59b04682009-06-10 05:40:52 +08001343
1344 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001345 ts_name = gsm_lchan_name(msg->lchan);
Harald Weltede4477a2009-12-24 12:20:20 +01001346 DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
Harald Welte59b04682009-06-10 05:40:52 +08001347
1348 switch (rllh->c.msg_type) {
1349 case RSL_MT_DATA_IND:
1350 DEBUGPC(DRLL, "DATA INDICATION\n");
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001351 if (msgb_l2len(msg) >
Harald Welte59b04682009-06-10 05:40:52 +08001352 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1353 rllh->data[0] == RSL_IE_L3_INFO) {
1354 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001355 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001356 }
1357 break;
1358 case RSL_MT_EST_IND:
1359 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welte427dbc42009-08-10 00:26:10 +02001360 /* lchan is established, stop T3101 */
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001361 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welte427dbc42009-08-10 00:26:10 +02001362 bsc_del_timer(&msg->lchan->T3101);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001363 if (msgb_l2len(msg) >
Harald Welte59b04682009-06-10 05:40:52 +08001364 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1365 rllh->data[0] == RSL_IE_L3_INFO) {
1366 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001367 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001368 }
1369 break;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001370 case RSL_MT_EST_CONF:
Harald Welte61402172009-08-09 14:13:58 +02001371 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001372 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001373 rll_indication(msg->lchan, rllh->link_id,
1374 BSC_RLLR_IND_EST_CONF);
1375 break;
Harald Welte59b04682009-06-10 05:40:52 +08001376 case RSL_MT_REL_IND:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001377 /* BTS informs us of having received DISC from MS */
Harald Welteb6601442009-08-04 02:50:21 +02001378 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001379 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001380 rll_indication(msg->lchan, rllh->link_id,
1381 BSC_RLLR_IND_REL_IND);
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001382 rsl_handle_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001383 break;
1384 case RSL_MT_REL_CONF:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001385 /* BTS informs us of having received UA from MS,
1386 * in response to DISC that we've sent earlier */
Harald Welteb6601442009-08-04 02:50:21 +02001387 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001388 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001389 rsl_handle_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001390 break;
1391 case RSL_MT_ERROR_IND:
Harald Welte59b04682009-06-10 05:40:52 +08001392 rc = rsl_rx_rll_err_ind(msg);
1393 break;
1394 case RSL_MT_UNIT_DATA_IND:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001395 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1396 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001397 break;
1398 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001399 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1400 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001401 }
Harald Welte59b04682009-06-10 05:40:52 +08001402 return rc;
1403}
1404
Harald Welteb284b472009-12-02 01:58:23 +05301405static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Welte98d79f92009-07-28 18:11:56 +02001406{
Harald Welteb284b472009-12-02 01:58:23 +05301407 switch (lchan->tch_mode) {
Harald Welte98d79f92009-07-28 18:11:56 +02001408 case GSM48_CMODE_SPEECH_V1:
Harald Welteb284b472009-12-02 01:58:23 +05301409 switch (lchan->type) {
1410 case GSM_LCHAN_TCH_F:
1411 return 0x00;
1412 case GSM_LCHAN_TCH_H:
1413 return 0x03;
1414 default:
1415 break;
1416 }
Harald Welte98d79f92009-07-28 18:11:56 +02001417 case GSM48_CMODE_SPEECH_EFR:
Harald Welteb284b472009-12-02 01:58:23 +05301418 switch (lchan->type) {
1419 case GSM_LCHAN_TCH_F:
1420 return 0x01;
1421 /* there's no half-rate EFR */
1422 default:
1423 break;
1424 }
Harald Welte98d79f92009-07-28 18:11:56 +02001425 case GSM48_CMODE_SPEECH_AMR:
Harald Welteb284b472009-12-02 01:58:23 +05301426 switch (lchan->type) {
1427 case GSM_LCHAN_TCH_F:
1428 return 0x02;
1429 case GSM_LCHAN_TCH_H:
1430 return 0x05;
1431 default:
1432 break;
1433 }
1434 default:
1435 break;
Harald Welte98d79f92009-07-28 18:11:56 +02001436 }
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001437 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welteb284b472009-12-02 01:58:23 +05301438 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001439 return 0;
Harald Welte98d79f92009-07-28 18:11:56 +02001440}
1441
Sylvain Munaut1338a552009-12-20 22:06:40 +01001442static u_int8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
1443{
1444 switch (lchan->tch_mode) {
1445 case GSM48_CMODE_SPEECH_V1:
1446 switch (lchan->type) {
1447 case GSM_LCHAN_TCH_F:
1448 return RTP_PT_GSM_FULL;
1449 case GSM_LCHAN_TCH_H:
1450 return RTP_PT_GSM_HALF;
1451 default:
1452 break;
1453 }
1454 case GSM48_CMODE_SPEECH_EFR:
1455 switch (lchan->type) {
1456 case GSM_LCHAN_TCH_F:
1457 return RTP_PT_GSM_EFR;
1458 /* there's no half-rate EFR */
1459 default:
1460 break;
1461 }
1462 case GSM48_CMODE_SPEECH_AMR:
1463 switch (lchan->type) {
1464 case GSM_LCHAN_TCH_F:
1465 return RTP_PT_AMR_FULL;
1466 case GSM_LCHAN_TCH_H:
1467 return RTP_PT_AMR_HALF;
1468 default:
1469 break;
1470 }
1471 default:
1472 break;
1473 }
1474 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access rtp payload type for "
1475 "tch_mode == 0x%02x\n & lchan_type == %d",
1476 lchan->tch_mode, lchan->type);
1477 return 0;
1478}
1479
Harald Welte59b04682009-06-10 05:40:52 +08001480/* ip.access specific RSL extensions */
Harald Weltebffa4992009-12-19 16:42:06 +01001481static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
1482{
1483 struct in_addr ip;
1484 u_int16_t port, conn_id;
1485
1486 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
1487 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_IP));
1488 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
1489 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1490 }
1491
1492 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
1493 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_PORT));
1494 port = ntohs(port);
1495 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
1496 lchan->abis_ip.bound_port = port;
1497 }
1498
1499 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
1500 conn_id = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_CONN_ID));
1501 conn_id = ntohs(conn_id);
1502 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
1503 lchan->abis_ip.conn_id = conn_id;
1504 }
1505
1506 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
1507 lchan->abis_ip.rtp_payload2 =
1508 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1509 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
1510 lchan->abis_ip.rtp_payload2);
1511 }
1512
1513 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
1514 lchan->abis_ip.speech_mode =
1515 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
1516 DEBUGPC(DRSL, "speech_mode=0x%02x ",
1517 lchan->abis_ip.speech_mode);
1518 }
1519
1520 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
1521 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_IP));
1522 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
1523 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
1524 }
1525
1526 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
1527 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_PORT));
1528 port = ntohs(port);
1529 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
1530 lchan->abis_ip.connect_port = port;
1531 }
1532}
1533
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001534int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +08001535{
1536 struct msgb *msg = rsl_msgb_alloc();
1537 struct abis_rsl_dchan_hdr *dh;
1538
1539 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001540 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte59b04682009-06-10 05:40:52 +08001541 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1542 dh->chan_nr = lchan2chan_nr(lchan);
1543
Harald Welte98d79f92009-07-28 18:11:56 +02001544 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001545 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001546 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Weltebffa4992009-12-19 16:42:06 +01001547 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001548 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001549
Sylvain Munaut1338a552009-12-20 22:06:40 +01001550 DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x RTP_PAYLOAD=%d\n",
1551 gsm_lchan_name(lchan), lchan->abis_ip.speech_mode,
1552 lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001553
Harald Welte59b04682009-06-10 05:40:52 +08001554 msg->trx = lchan->ts->trx;
1555
1556 return abis_rsl_sendmsg(msg);
1557}
1558
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001559int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Weltebffa4992009-12-19 16:42:06 +01001560 u_int8_t rtp_payload2)
Harald Welte59b04682009-06-10 05:40:52 +08001561{
1562 struct msgb *msg = rsl_msgb_alloc();
1563 struct abis_rsl_dchan_hdr *dh;
Harald Weltebffa4992009-12-19 16:42:06 +01001564 u_int32_t *att_ip;
Harald Welte98d79f92009-07-28 18:11:56 +02001565 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +08001566
1567 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001568 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte59b04682009-06-10 05:40:52 +08001569 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1570 dh->chan_nr = lchan2chan_nr(lchan);
1571
Harald Weltebffa4992009-12-19 16:42:06 +01001572 /* we need to store these now as MDCX_ACK does not return them :( */
1573 lchan->abis_ip.rtp_payload2 = rtp_payload2;
1574 lchan->abis_ip.connect_port = port;
1575 lchan->abis_ip.connect_ip = ip;
1576
Harald Weltefb4a9e92009-07-29 12:12:18 +02001577 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001578 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001579 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Weltefb4a9e92009-07-29 12:12:18 +02001580
Harald Welte98d79f92009-07-28 18:11:56 +02001581 ia.s_addr = htonl(ip);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001582 DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD=%d RTP_PAYLOAD2=%d "
1583 "CONN_ID=%d speech_mode=0x%02x\n", gsm_lchan_name(lchan),
1584 inet_ntoa(ia), port, lchan->abis_ip.rtp_payload, rtp_payload2,
1585 lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001586
Harald Weltebffa4992009-12-19 16:42:06 +01001587 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
1588 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
1589 att_ip = (u_int32_t *) msgb_put(msg, sizeof(ip));
1590 *att_ip = ia.s_addr;
1591 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
1592 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001593 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001594 if (rtp_payload2)
1595 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1596
Harald Welte59b04682009-06-10 05:40:52 +08001597 msg->trx = lchan->ts->trx;
1598
1599 return abis_rsl_sendmsg(msg);
1600}
1601
Harald Welte9947d9f2009-12-20 16:51:09 +01001602/* tell BTS to connect RTP stream to our local RTP socket */
1603int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
1604{
1605 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
1606 int rc;
1607
1608 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1609 ntohs(rs->rtp.sin_local.sin_port),
1610 /* FIXME: use RTP payload of bound socket, not BTS*/
1611 lchan->abis_ip.rtp_payload2);
1612
1613 return rc;
1614}
1615
Harald Welte2b361522010-03-28 14:42:09 +08001616int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan, int act)
Harald Welteaed946e2009-10-24 10:29:22 +02001617{
1618 struct msgb *msg = rsl_msgb_alloc();
1619 struct abis_rsl_dchan_hdr *dh;
Harald Welte2b361522010-03-28 14:42:09 +08001620 u_int8_t msg_type;
1621
1622 if (act)
1623 msg_type = RSL_MT_IPAC_PDCH_ACT;
1624 else
1625 msg_type = RSL_MT_IPAC_PDCH_DEACT;
Harald Welteaed946e2009-10-24 10:29:22 +02001626
1627 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Harald Welte2b361522010-03-28 14:42:09 +08001628 init_dchan_hdr(dh, msg_type);
Harald Welteaed946e2009-10-24 10:29:22 +02001629 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1630 dh->chan_nr = lchan2chan_nr(lchan);
1631
Harald Welte2b361522010-03-28 14:42:09 +08001632 DEBUGP(DRSL, "%s IPAC_PDCH_%sACT\n", gsm_lchan_name(lchan),
1633 act ? "" : "DE");
Harald Welteaed946e2009-10-24 10:29:22 +02001634
1635 msg->trx = lchan->ts->trx;
1636
1637 return abis_rsl_sendmsg(msg);
1638}
1639
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001640static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001641{
1642 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1643 struct tlv_parsed tv;
Harald Welte87504212009-12-02 01:56:49 +05301644 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001645
1646 /* the BTS has acknowledged a local bind, it now tells us the IP
1647 * address and port number to which it has bound the given logical
1648 * channel */
1649
1650 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1651 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1652 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welteb9498952009-07-12 09:45:05 +02001653 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001654 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte59b04682009-06-10 05:40:52 +08001655 return -EINVAL;
1656 }
Harald Welte50517742009-12-20 15:42:44 +01001657
Harald Weltebffa4992009-12-19 16:42:06 +01001658 ipac_parse_rtp(lchan, &tv);
Harald Welte50517742009-12-20 15:42:44 +01001659
1660 /* in case we don't use direct BTS-to-BTS RTP */
1661 if (!ipacc_rtp_direct) {
1662 int rc;
1663 /* the BTS has successfully bound a TCH to a local ip/port,
1664 * which means we can connect our UDP socket to it */
1665 if (lchan->abis_ip.rtp_socket) {
1666 rtp_socket_free(lchan->abis_ip.rtp_socket);
1667 lchan->abis_ip.rtp_socket = NULL;
1668 }
1669
1670 lchan->abis_ip.rtp_socket = rtp_socket_create();
1671 if (!lchan->abis_ip.rtp_socket)
1672 goto out_err;
1673
1674 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1675 lchan->abis_ip.bound_ip,
1676 lchan->abis_ip.bound_port);
1677 if (rc < 0)
1678 goto out_err;
1679 }
1680
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001681 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001682
1683 return 0;
Harald Welte50517742009-12-20 15:42:44 +01001684out_err:
1685 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +08001686}
1687
Harald Weltebffa4992009-12-19 16:42:06 +01001688static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
1689{
1690 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1691 struct tlv_parsed tv;
1692 struct gsm_lchan *lchan = msg->lchan;
1693
1694 /* the BTS has acknowledged a remote connect request and
1695 * it now tells us the IP address and port number to which it has
1696 * connected the given logical channel */
1697
1698 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1699 ipac_parse_rtp(lchan, &tv);
1700 dispatch_signal(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
1701
1702 return 0;
1703}
1704
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001705static int abis_rsl_rx_ipacc_dlcx_ind(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 Welte50517742009-12-20 15:42:44 +01001709 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001710
1711 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte59b04682009-06-10 05:40:52 +08001712
Harald Weltef1a168d2009-07-28 17:58:09 +02001713 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001714 print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001715 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte59b04682009-06-10 05:40:52 +08001716
Harald Welte50517742009-12-20 15:42:44 +01001717 /* the BTS tells us a RTP stream has been disconnected */
1718 if (lchan->abis_ip.rtp_socket) {
1719 rtp_socket_free(lchan->abis_ip.rtp_socket);
1720 lchan->abis_ip.rtp_socket = NULL;
1721 }
1722
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001723 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welteba4e58d2009-07-28 18:02:05 +02001724
Harald Welte59b04682009-06-10 05:40:52 +08001725 return 0;
1726}
1727
1728static int abis_rsl_rx_ipacc(struct msgb *msg)
1729{
1730 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltede4477a2009-12-24 12:20:20 +01001731 char *ts_name;
Harald Welte59b04682009-06-10 05:40:52 +08001732 int rc = 0;
1733
1734 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001735 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001736
1737 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001738 case RSL_MT_IPAC_CRCX_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001739 DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001740 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001741 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001742 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001743 /* somehow the BTS was unable to bind the lchan to its local
1744 * port?!? */
Harald Weltede4477a2009-12-24 12:20:20 +01001745 LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001746 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001747 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte59b04682009-06-10 05:40:52 +08001748 /* the BTS tells us that a connect operation was successful */
Harald Weltede4477a2009-12-24 12:20:20 +01001749 DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
Harald Weltebffa4992009-12-19 16:42:06 +01001750 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001751 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001752 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001753 /* somehow the BTS was unable to connect the lchan to a remote
1754 * port */
Harald Weltede4477a2009-12-24 12:20:20 +01001755 LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001756 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001757 case RSL_MT_IPAC_DLCX_IND:
Harald Weltede4477a2009-12-24 12:20:20 +01001758 DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001759 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001760 break;
1761 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001762 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n",
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001763 rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001764 break;
1765 }
1766 DEBUGPC(DRSL, "\n");
1767
1768 return rc;
1769}
1770
1771
1772/* Entry-point where L2 RSL from BTS enters */
1773int abis_rsl_rcvmsg(struct msgb *msg)
1774{
Holger Hans Peter Freytherc7d94092009-11-20 15:14:01 +01001775 struct abis_rsl_common_hdr *rslh;
Harald Welte59b04682009-06-10 05:40:52 +08001776 int rc = 0;
1777
Holger Hans Peter Freytherc7d94092009-11-20 15:14:01 +01001778 if (!msg) {
1779 DEBUGP(DRSL, "Empty RSL msg?..\n");
1780 return -1;
1781 }
1782
1783 if (msgb_l2len(msg) < sizeof(*rslh)) {
1784 DEBUGP(DRSL, "Truncated RSL message with l2len: %u\n", msgb_l2len(msg));
1785 return -1;
1786 }
1787
1788 rslh = msgb_l2(msg);
1789
Harald Welte59b04682009-06-10 05:40:52 +08001790 switch (rslh->msg_discr & 0xfe) {
1791 case ABIS_RSL_MDISC_RLL:
1792 rc = abis_rsl_rx_rll(msg);
1793 break;
1794 case ABIS_RSL_MDISC_DED_CHAN:
1795 rc = abis_rsl_rx_dchan(msg);
1796 break;
1797 case ABIS_RSL_MDISC_COM_CHAN:
1798 rc = abis_rsl_rx_cchan(msg);
1799 break;
1800 case ABIS_RSL_MDISC_TRX:
1801 rc = abis_rsl_rx_trx(msg);
1802 break;
1803 case ABIS_RSL_MDISC_LOC:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001804 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001805 rslh->msg_discr);
1806 break;
1807 case ABIS_RSL_MDISC_IPACCESS:
1808 rc = abis_rsl_rx_ipacc(msg);
1809 break;
1810 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001811 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1812 "0x%02x\n", rslh->msg_discr);
Harald Welte59b04682009-06-10 05:40:52 +08001813 return -EINVAL;
1814 }
1815 msgb_free(msg);
1816 return rc;
1817}
1818
Harald Welte59b04682009-06-10 05:40:52 +08001819/* From Table 10.5.33 of GSM 04.08 */
1820int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1821{
Harald Weltea54a2bb2009-12-01 18:04:30 +05301822 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1823 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1824 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001825 } else {
Harald Weltea54a2bb2009-12-01 18:04:30 +05301826 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1827 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001828 }
1829}