blob: 0a86e7990e55ff7d71c5fd7c2cc5e81675da0444 [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
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +080045#include <osmocore/talloc.h>
46
Harald Welte59b04682009-06-10 05:40:52 +080047#define RSL_ALLOC_SIZE 1024
48#define RSL_ALLOC_HEADROOM 128
49
50#define MAX(a, b) (a) >= (b) ? (a) : (b)
51
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +080052static int rsl_send_imm_assignment(struct gsm_lchan *lchan);
53
Harald Welte59b04682009-06-10 05:40:52 +080054static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
55{
56 /* mask off the transparent bit ? */
57 msg_type &= 0xfe;
58
59 if ((msg_type & 0xf0) == 0x00)
60 return ABIS_RSL_MDISC_RLL;
61 if ((msg_type & 0xf0) == 0x10) {
62 if (msg_type >= 0x19 && msg_type <= 0x22)
63 return ABIS_RSL_MDISC_TRX;
64 else
65 return ABIS_RSL_MDISC_COM_CHAN;
66 }
67 if ((msg_type & 0xe0) == 0x20)
68 return ABIS_RSL_MDISC_DED_CHAN;
69
70 return ABIS_RSL_MDISC_LOC;
71}
72
73static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
74 u_int8_t msg_type)
75{
76 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
77 dh->c.msg_type = msg_type;
78 dh->ie_chan = RSL_IE_CHAN_NR;
79}
80
Harald Welte59b04682009-06-10 05:40:52 +080081/* determine logical channel based on TRX and channel number IE */
82struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
83{
84 struct gsm_lchan *lchan;
85 u_int8_t ts_nr = chan_nr & 0x07;
86 u_int8_t cbits = chan_nr >> 3;
87 u_int8_t lch_idx;
88 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
89
90 if (cbits == 0x01) {
91 lch_idx = 0; /* TCH/F */
Harald Welte37884ed2009-10-24 10:25:50 +020092 if (ts->pchan != GSM_PCHAN_TCH_F &&
93 ts->pchan != GSM_PCHAN_PDCH &&
94 ts->pchan != GSM_PCHAN_TCH_F_PDCH)
Harald Weltecf2ec4a2009-12-17 23:10:46 +010095 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +080096 chan_nr, ts->pchan);
97 } else if ((cbits & 0x1e) == 0x02) {
98 lch_idx = cbits & 0x1; /* TCH/H */
99 if (ts->pchan != GSM_PCHAN_TCH_H)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100100 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800101 chan_nr, ts->pchan);
102 } else if ((cbits & 0x1c) == 0x04) {
103 lch_idx = cbits & 0x3; /* SDCCH/4 */
104 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100105 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800106 chan_nr, ts->pchan);
107 } else if ((cbits & 0x18) == 0x08) {
108 lch_idx = cbits & 0x7; /* SDCCH/8 */
109 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100110 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800111 chan_nr, ts->pchan);
112 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
113 lch_idx = 0;
114 if (ts->pchan != GSM_PCHAN_CCCH &&
115 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100116 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800117 chan_nr, ts->pchan);
118 /* FIXME: we should not return first sdcch4 !!! */
119 } else {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100120 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +0800121 return NULL;
122 }
123
124 lchan = &ts->lchan[lch_idx];
Harald Welte51d2a592010-03-26 21:28:59 +0800125 log_set_context(BSC_CTX_LCHAN, lchan);
Holger Hans Peter Freyther1a95fa82010-06-28 15:47:12 +0800126 if (lchan->conn)
127 log_set_context(BSC_CTX_SUBSCR, lchan->conn->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800128
129 return lchan;
130}
131
Holger Hans Peter Freyther942ff172009-10-22 11:47:45 +0200132/* See Table 10.5.25 of GSM04.08 */
Harald Welteb5586242009-12-09 19:18:32 +0100133u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800134{
135 struct gsm_bts_trx_ts *ts = lchan->ts;
136 u_int8_t cbits, chan_nr;
137
138 switch (ts->pchan) {
139 case GSM_PCHAN_TCH_F:
Harald Welte37884ed2009-10-24 10:25:50 +0200140 case GSM_PCHAN_PDCH:
141 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte59b04682009-06-10 05:40:52 +0800142 cbits = 0x01;
143 break;
144 case GSM_PCHAN_TCH_H:
145 cbits = 0x02;
146 cbits += lchan->nr;
147 break;
148 case GSM_PCHAN_CCCH_SDCCH4:
149 cbits = 0x04;
150 cbits += lchan->nr;
151 break;
152 case GSM_PCHAN_SDCCH8_SACCH8C:
153 cbits = 0x08;
154 cbits += lchan->nr;
155 break;
156 default:
157 case GSM_PCHAN_CCCH:
158 cbits = 0x10;
159 break;
160 }
161
162 chan_nr = (cbits << 3) | (ts->nr & 0x7);
163
164 return chan_nr;
165}
166
167/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
168u_int64_t str_to_imsi(const char *imsi_str)
169{
170 u_int64_t ret;
171
172 ret = strtoull(imsi_str, NULL, 10);
173
174 return ret;
175}
176
177/* Table 5 Clause 7 TS 05.02 */
178unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
179{
180 if (!bs_ccch_sdcch_comb)
181 return 9 - bs_ag_blks_res;
182 else
183 return 3 - bs_ag_blks_res;
184}
185
186/* Chapter 6.5.2 of TS 05.02 */
187unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
188 unsigned int n_pag_blocks)
189{
190 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
191}
192
193/* Chapter 6.5.2 of TS 05.02 */
194unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
195 int n_pag_blocks)
196{
197 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
198}
199
200static struct msgb *rsl_msgb_alloc(void)
201{
Harald Welte9cfc9352009-06-26 19:39:35 +0200202 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
203 "RSL");
Harald Welte59b04682009-06-10 05:40:52 +0800204}
205
206#define MACBLOCK_SIZE 23
207static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
208{
209 memcpy(out, in, len);
210
211 if (len < MACBLOCK_SIZE)
212 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
213}
214
Harald Welted2dd9de2009-08-30 15:37:11 +0900215/* Chapter 9.3.7: Encryption Information */
216static int build_encr_info(u_int8_t *out, struct gsm_lchan *lchan)
217{
218 *out++ = lchan->encr.alg_id & 0xff;
219 if (lchan->encr.key_len)
220 memcpy(out, lchan->encr.key, lchan->encr.key_len);
221 return lchan->encr.key_len + 1;
222}
223
Harald Weltede4477a2009-12-24 12:20:20 +0100224static void print_rsl_cause(int lvl, const u_int8_t *cause_v, u_int8_t cause_len)
Harald Weltef1a168d2009-07-28 17:58:09 +0200225{
Harald Welte59b04682009-06-10 05:40:52 +0800226 int i;
227
Harald Weltede4477a2009-12-24 12:20:20 +0100228 LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ",
Harald Weltef1a168d2009-07-28 17:58:09 +0200229 cause_v[0], rsl_err_name(cause_v[0]));
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200230 for (i = 1; i < cause_len-1; i++)
Harald Weltede4477a2009-12-24 12:20:20 +0100231 LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
Harald Welte59b04682009-06-10 05:40:52 +0800232}
233
234/* Send a BCCH_INFO message as per Chapter 8.5.1 */
235int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
236 const u_int8_t *data, int len)
237{
238 struct abis_rsl_dchan_hdr *dh;
239 struct msgb *msg = rsl_msgb_alloc();
240
241 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
242 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
243 dh->chan_nr = RSL_CHAN_BCCH;
244
245 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
246 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
247
248 msg->trx = trx;
249
250 return abis_rsl_sendmsg(msg);
251}
252
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200253int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte59b04682009-06-10 05:40:52 +0800254 const u_int8_t *data, int len)
255{
256 struct abis_rsl_common_hdr *ch;
257 struct msgb *msg = rsl_msgb_alloc();
258
259 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
260 ch->msg_discr = ABIS_RSL_MDISC_TRX;
261 ch->msg_type = RSL_MT_SACCH_FILL;
262
263 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
264 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
265
266 msg->trx = trx;
267
268 return abis_rsl_sendmsg(msg);
269}
270
Harald Welte91afe4c2009-06-20 18:15:19 +0200271int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
272{
273 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200274 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200275 u_int8_t chan_nr = lchan2chan_nr(lchan);
276
277 db = abs(db);
278 if (db > 30)
279 return -EINVAL;
280
Harald Welteed831842009-06-27 03:09:08 +0200281 msg = rsl_msgb_alloc();
282
Harald Welte91afe4c2009-06-20 18:15:19 +0200283 lchan->bs_power = db/2;
284 if (fpc)
285 lchan->bs_power |= 0x10;
286
287 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
288 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
289 dh->chan_nr = chan_nr;
290
291 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
292
293 msg->trx = lchan->ts->trx;
294
295 return abis_rsl_sendmsg(msg);
296}
297
Harald Welte91afe4c2009-06-20 18:15:19 +0200298int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
299{
300 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200301 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200302 u_int8_t chan_nr = lchan2chan_nr(lchan);
303 int ctl_lvl;
304
Harald Weltec4dcda02009-08-09 14:45:18 +0200305 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Welte91afe4c2009-06-20 18:15:19 +0200306 if (ctl_lvl < 0)
307 return ctl_lvl;
308
Harald Welteed831842009-06-27 03:09:08 +0200309 msg = rsl_msgb_alloc();
310
Harald Welte91afe4c2009-06-20 18:15:19 +0200311 lchan->ms_power = ctl_lvl;
312
313 if (fpc)
314 lchan->ms_power |= 0x20;
315
316 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
317 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
318 dh->chan_nr = chan_nr;
319
320 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
321
322 msg->trx = lchan->ts->trx;
323
324 return abis_rsl_sendmsg(msg);
325}
326
Harald Welte39274f42009-07-29 15:41:29 +0200327static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
328 struct gsm_lchan *lchan)
329{
330 memset(cm, 0, sizeof(cm));
331
332 /* FIXME: what to do with data calls ? */
333 cm->dtx_dtu = 0x00;
334
335 /* set TCH Speech/Data */
336 cm->spd_ind = lchan->rsl_cmode;
337
Harald Welte951e3512009-11-27 08:55:16 +0100338 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
339 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100340 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte951e3512009-11-27 08:55:16 +0100341 "but tch_mode != signalling\n");
342
Harald Welte39274f42009-07-29 15:41:29 +0200343 switch (lchan->type) {
344 case GSM_LCHAN_SDCCH:
345 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
346 break;
347 case GSM_LCHAN_TCH_F:
348 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
349 break;
350 case GSM_LCHAN_TCH_H:
351 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
352 break;
353 case GSM_LCHAN_NONE:
354 case GSM_LCHAN_UNKNOWN:
355 default:
356 return -EINVAL;
357 }
358
359 switch (lchan->tch_mode) {
360 case GSM48_CMODE_SIGN:
361 cm->chan_rate = 0;
362 break;
363 case GSM48_CMODE_SPEECH_V1:
364 cm->chan_rate = RSL_CMOD_SP_GSM1;
365 break;
366 case GSM48_CMODE_SPEECH_EFR:
367 cm->chan_rate = RSL_CMOD_SP_GSM2;
368 break;
369 case GSM48_CMODE_SPEECH_AMR:
370 cm->chan_rate = RSL_CMOD_SP_GSM3;
371 break;
372 case GSM48_CMODE_DATA_14k5:
373 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
374 break;
375 case GSM48_CMODE_DATA_12k0:
376 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
377 break;
378 case GSM48_CMODE_DATA_6k0:
379 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
380 break;
381 default:
382 return -EINVAL;
383 }
384
385 return 0;
386}
387
Harald Welte59b04682009-06-10 05:40:52 +0800388/* Chapter 8.4.1 */
389#if 0
390int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
391 u_int8_t act_type,
392 struct rsl_ie_chan_mode *chan_mode,
393 struct rsl_ie_chan_ident *chan_ident,
394 u_int8_t bs_power, u_int8_t ms_power,
395 u_int8_t ta)
396{
397 struct abis_rsl_dchan_hdr *dh;
398 struct msgb *msg = rsl_msgb_alloc();
399
400 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
401 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
402 dh->chan_nr = chan_nr;
403
404 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
405 /* For compatibility with Phase 1 */
406 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
407 (u_int8_t *) chan_mode);
408 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
409 (u_int8_t *) chan_ident);
410#if 0
411 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
412 (u_int8_t *) &encr_info);
413#endif
414 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
415 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
416 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
417
418 msg->trx = trx;
419
420 return abis_rsl_sendmsg(msg);
421}
422#endif
423
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200424int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welteb90d7bd2009-12-17 00:31:10 +0100425 u_int8_t ta, u_int8_t ho_ref)
Harald Welte59b04682009-06-10 05:40:52 +0800426{
427 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200428 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200429 int rc;
Harald Weltedea24e92010-06-29 17:53:45 +0200430 uint8_t *len;
Harald Welte59b04682009-06-10 05:40:52 +0800431
432 u_int8_t chan_nr = lchan2chan_nr(lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800433 struct rsl_ie_chan_mode cm;
laforgef723cf02010-06-20 21:38:19 +0200434 struct gsm48_chan_desc cd;
Harald Welte59b04682009-06-10 05:40:52 +0800435
Harald Welte39274f42009-07-29 15:41:29 +0200436 rc = channel_mode_from_lchan(&cm, lchan);
437 if (rc < 0)
438 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800439
Holger Hans Peter Freyther11b01402010-06-30 11:56:43 +0800440 memset(&cd, 0, sizeof(cd));
laforgef723cf02010-06-20 21:38:19 +0200441 gsm48_lchan2chan_desc(&cd, lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800442
Harald Welteed831842009-06-27 03:09:08 +0200443 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800444 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
445 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
446 dh->chan_nr = chan_nr;
447
448 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte59b04682009-06-10 05:40:52 +0800449 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
450 (u_int8_t *) &cm);
Holger Hans Peter Freyther11b01402010-06-30 11:56:43 +0800451
452 /*
453 * The Channel Identification is needed for Phase1 phones
454 * and it contains the GSM48 Channel Description and the
455 * Mobile Allocation. The GSM 08.58 asks for the Mobile
456 * Allocation to have a length of zero. We are using the
457 * msgb_l3len to calculate the length of both messages.
458 */
laforgef723cf02010-06-20 21:38:19 +0200459 msgb_v_put(msg, RSL_IE_CHAN_IDENT);
Harald Weltedea24e92010-06-29 17:53:45 +0200460 len = msgb_put(msg, 1);
Holger Hans Peter Freyther11b01402010-06-30 11:56:43 +0800461 msgb_tlv_put(msg, GSM48_IE_CHANDESC_2, sizeof(cd), (const uint8_t *) &cd);
462 msgb_tlv_put(msg, GSM48_IE_MA_AFTER, 0, NULL);
463
464 /* update the calculated size */
465 msg->l3h = len + 1;
466 *len = msgb_l3len(msg);
467
Harald Welted2dd9de2009-08-30 15:37:11 +0900468 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
469 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
470 rc = build_encr_info(encr_info, lchan);
471 if (rc > 0)
472 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
473 }
474
Harald Welteb90d7bd2009-12-17 00:31:10 +0100475 switch (act_type) {
476 case RSL_ACT_INTER_ASYNC:
477 case RSL_ACT_INTER_SYNC:
478 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
479 break;
480 default:
481 break;
482 }
483
Harald Welte59b04682009-06-10 05:40:52 +0800484 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
485 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
486 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
487
Holger Hans Peter Freyther6fe8ab92010-01-28 04:45:05 +0100488 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
489 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
490 (u_int8_t *) &lchan->mr_conf);
491
Harald Welte59b04682009-06-10 05:40:52 +0800492 msg->trx = lchan->ts->trx;
493
494 return abis_rsl_sendmsg(msg);
495}
496
Harald Welte8e770492009-07-29 11:38:15 +0200497/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welte59b04682009-06-10 05:40:52 +0800498int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
499{
500 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200501 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200502 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800503
504 u_int8_t chan_nr = lchan2chan_nr(lchan);
505 struct rsl_ie_chan_mode cm;
506
Harald Welte39274f42009-07-29 15:41:29 +0200507 rc = channel_mode_from_lchan(&cm, lchan);
508 if (rc < 0)
509 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800510
Harald Welteed831842009-06-27 03:09:08 +0200511 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800512 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
513 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
514 dh->chan_nr = chan_nr;
515
516 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
517 (u_int8_t *) &cm);
Harald Welted2dd9de2009-08-30 15:37:11 +0900518
519 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
520 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
521 rc = build_encr_info(encr_info, lchan);
522 if (rc > 0)
523 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
524 }
525
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +0100526 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
527 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
528 (u_int8_t *) &lchan->mr_conf);
529 }
530
Harald Welted2dd9de2009-08-30 15:37:11 +0900531 msg->trx = lchan->ts->trx;
532
533 return abis_rsl_sendmsg(msg);
534}
535
536/* Chapter 8.4.6: Send the encryption command with given L3 info */
537int rsl_encryption_cmd(struct msgb *msg)
538{
539 struct abis_rsl_dchan_hdr *dh;
540 struct gsm_lchan *lchan = msg->lchan;
541 u_int8_t chan_nr = lchan2chan_nr(lchan);
542 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut01f1caf2009-09-27 11:13:18 +0200543 u_int8_t l3_len = msg->len;
Harald Welted2dd9de2009-08-30 15:37:11 +0900544 int rc;
545
546 /* First push the L3 IE tag and length */
547 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
548
549 /* then the link identifier (SAPI0, main sign link) */
550 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
551
552 /* then encryption information */
553 rc = build_encr_info(encr_info, lchan);
554 if (rc <= 0)
555 return rc;
556 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
557
558 /* and finally the DCHAN header */
559 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
560 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
561 dh->chan_nr = chan_nr;
Harald Welte59b04682009-06-10 05:40:52 +0800562
563 msg->trx = lchan->ts->trx;
564
565 return abis_rsl_sendmsg(msg);
566}
567
Harald Welte85a163c2009-08-10 11:43:22 +0200568/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteafe3c232009-07-19 18:36:49 +0200569int rsl_deact_sacch(struct gsm_lchan *lchan)
570{
571 struct abis_rsl_dchan_hdr *dh;
572 struct msgb *msg = rsl_msgb_alloc();
573
574 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
575 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
576 dh->chan_nr = lchan2chan_nr(lchan);
577
578 msg->lchan = lchan;
579 msg->trx = lchan->ts->trx;
580
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100581 DEBUGP(DRSL, "%s DEACTivate SACCH CMD\n", gsm_lchan_name(lchan));
Harald Welteafe3c232009-07-19 18:36:49 +0200582
583 return abis_rsl_sendmsg(msg);
584}
585
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800586static void error_timeout_cb(void *data)
587{
588 struct gsm_lchan *lchan = data;
589 if (lchan->state != LCHAN_S_REL_ERR) {
590 LOGP(DRSL, LOGL_ERROR, "%s error timeout but not in error state: %d\n",
591 gsm_lchan_name(lchan), lchan->state);
592 return;
593 }
594
595 /* go back to the none state */
596 LOGP(DRSL, LOGL_NOTICE, "%s is back in operation.\n", gsm_lchan_name(lchan));
Holger Hans Peter Freyther456fb9d2010-06-08 11:53:33 +0800597 rsl_lchan_set_state(lchan, LCHAN_S_NONE);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800598}
599
Harald Welte85a163c2009-08-10 11:43:22 +0200600/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800601static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error)
Harald Welte59b04682009-06-10 05:40:52 +0800602{
603 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800604 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800605
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800606 if (lchan->state == LCHAN_S_REL_ERR) {
607 LOGP(DRSL, LOGL_NOTICE, "%s is in error state not sending release.\n",
608 gsm_lchan_name(lchan));
609 return -1;
610 }
611
612 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800613 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
614 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
615 dh->chan_nr = lchan2chan_nr(lchan);
616
617 msg->lchan = lchan;
618 msg->trx = lchan->ts->trx;
619
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800620 DEBUGP(DRSL, "%s RF Channel Release CMD due error %d\n", gsm_lchan_name(lchan), error);
621
622 if (error) {
623 /*
624 * the nanoBTS sends RLL release indications after the channel release. This can
625 * be a problem when we have reassigned the channel to someone else and then can
626 * not figure out who used this channel.
627 */
Holger Hans Peter Freyther456fb9d2010-06-08 11:53:33 +0800628 rsl_lchan_set_state(lchan, LCHAN_S_REL_ERR);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800629 lchan->error_timer.data = lchan;
630 lchan->error_timer.cb = error_timeout_cb;
631 bsc_schedule_timer(&lchan->error_timer,
632 msg->trx->bts->network->T3111 + 2, 0);
633 }
Harald Welte59b04682009-06-10 05:40:52 +0800634
Harald Welte85a163c2009-08-10 11:43:22 +0200635 /* BTS will respond by RF CHAN REL ACK */
Harald Welte59b04682009-06-10 05:40:52 +0800636 return abis_rsl_sendmsg(msg);
637}
638
639int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
640 u_int8_t *ms_ident, u_int8_t chan_needed)
641{
642 struct abis_rsl_dchan_hdr *dh;
643 struct msgb *msg = rsl_msgb_alloc();
644
645 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
646 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
647 dh->chan_nr = RSL_CHAN_PCH_AGCH;
648
649 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
650 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
651 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
652
653 msg->trx = bts->c0;
654
655 return abis_rsl_sendmsg(msg);
656}
657
658int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
659 struct gsm_subscriber *subscr)
660{
661#if 0
662 u_int8_t mi[128];
663 unsigned int mi_len;
664 u_int8_t paging_group;
665#endif
666
667 return -1;
668}
669
670int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
671{
672 int i, len = strlen(str_in);
673
674 for (i = 0; i < len; i++) {
675 int num = str_in[i] - 0x30;
676 if (num < 0 || num > 9)
677 return -1;
678 if (i % 2 == 0)
679 bcd_out[i/2] = num;
680 else
681 bcd_out[i/2] |= (num << 4);
682 }
683
684 return 0;
685}
686
687/* Chapter 8.5.6 */
688int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
689{
690 struct msgb *msg = rsl_msgb_alloc();
691 struct abis_rsl_dchan_hdr *dh;
692 u_int8_t buf[MACBLOCK_SIZE];
693
694 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
695 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
696 dh->chan_nr = RSL_CHAN_PCH_AGCH;
697
698 switch (bts->type) {
699 case GSM_BTS_TYPE_BS11:
700 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
701 break;
702 default:
703 /* If phase 2, construct a FULL_IMM_ASS_INFO */
704 pad_macblock(buf, val, len);
705 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
706 break;
707 }
708
709 msg->trx = bts->c0;
710
711 return abis_rsl_sendmsg(msg);
712}
713
Harald Welte4684e632009-08-10 09:51:40 +0200714/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte12090752009-08-10 10:07:33 +0200715int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte4684e632009-08-10 09:51:40 +0200716{
717 struct msgb *msg = rsl_msgb_alloc();
718 struct abis_rsl_dchan_hdr *dh;
719
720 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
721 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte874a5b42009-08-10 11:26:14 +0200722 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte4684e632009-08-10 09:51:40 +0200723 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte12090752009-08-10 10:07:33 +0200724 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte4684e632009-08-10 09:51:40 +0200725
Harald Weltede4477a2009-12-24 12:20:20 +0100726 DEBUGP(DRSL, "%s TX Siemens MRPCI 0x%02x\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100727 gsm_lchan_name(lchan), *(u_int8_t *)mrpci);
Harald Welte874a5b42009-08-10 11:26:14 +0200728
729 msg->trx = lchan->ts->trx;
730
Harald Welte4684e632009-08-10 09:51:40 +0200731 return abis_rsl_sendmsg(msg);
732}
733
734
Harald Welte59b04682009-06-10 05:40:52 +0800735/* Send "DATA REQUEST" message with given L3 Info payload */
736/* Chapter 8.3.1 */
737int rsl_data_request(struct msgb *msg, u_int8_t link_id)
738{
Harald Welte59b04682009-06-10 05:40:52 +0800739 if (msg->lchan == NULL) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100740 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte59b04682009-06-10 05:40:52 +0800741 return -EINVAL;
742 }
743
Harald Weltea22d36b2010-03-04 10:33:10 +0100744 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, lchan2chan_nr(msg->lchan),
745 link_id, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800746
747 msg->trx = msg->lchan->ts->trx;
748
749 return abis_rsl_sendmsg(msg);
750}
751
Harald Welteed9a5ab2009-08-09 13:47:35 +0200752/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
753/* Chapter 8.3.1 */
754int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
755{
Harald Weltea22d36b2010-03-04 10:33:10 +0100756 struct msgb *msg;
Harald Welteed9a5ab2009-08-09 13:47:35 +0200757
Harald Weltea22d36b2010-03-04 10:33:10 +0100758 msg = rsl_rll_simple(RSL_MT_EST_REQ, lchan2chan_nr(lchan),
759 link_id, 0);
Harald Welteed9a5ab2009-08-09 13:47:35 +0200760 msg->trx = lchan->ts->trx;
761
762 return abis_rsl_sendmsg(msg);
763}
764
Harald Welte0f2e3c12009-08-08 13:15:07 +0200765/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
766 This is what higher layers should call. The BTS then responds with
767 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
768 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
769 lchan_free() */
Holger Hans Peter Freytherbcea9a72010-06-08 11:57:45 +0800770int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id, u_int8_t reason)
Harald Welte0f2e3c12009-08-08 13:15:07 +0200771{
Harald Welte0f2e3c12009-08-08 13:15:07 +0200772
Harald Weltea22d36b2010-03-04 10:33:10 +0100773 struct msgb *msg;
774
775 msg = rsl_rll_simple(RSL_MT_REL_REQ, lchan2chan_nr(lchan),
776 link_id, 0);
Holger Hans Peter Freytherbcea9a72010-06-08 11:57:45 +0800777 /* 0 is normal release, 1 is local end */
778 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, reason);
Harald Welte0f2e3c12009-08-08 13:15:07 +0200779
Harald Weltec88a4432009-12-29 10:44:17 +0100780 /* FIXME: start some timer in case we don't receive a REL ACK ? */
781
Harald Welte0f2e3c12009-08-08 13:15:07 +0200782 msg->trx = lchan->ts->trx;
783
784 return abis_rsl_sendmsg(msg);
785}
786
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200787int rsl_lchan_set_state(struct gsm_lchan *lchan, int state)
788{
789 lchan->state = state;
790 return 0;
791}
792
Harald Welte59b04682009-06-10 05:40:52 +0800793/* Chapter 8.4.2: Channel Activate Acknowledge */
794static int rsl_rx_chan_act_ack(struct msgb *msg)
795{
796 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
797
798 /* BTS has confirmed channel activation, we now need
799 * to assign the activated channel to the MS */
800 if (rslh->ie_chan != RSL_IE_CHAN_NR)
801 return -EINVAL;
Harald Welte6720a432009-11-29 22:45:52 +0100802
Harald Weltec88a4432009-12-29 10:44:17 +0100803 if (msg->lchan->state != LCHAN_S_ACT_REQ)
Harald Welteab2534c2009-12-29 10:52:38 +0100804 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
805 gsm_lchan_name(msg->lchan),
806 gsm_lchans_name(msg->lchan->state));
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200807 rsl_lchan_set_state(msg->lchan, LCHAN_S_ACTIVE);
Harald Welte4baa9c52009-12-21 13:27:11 +0100808
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +0800809 if (msg->lchan->rqd_ref) {
810 rsl_send_imm_assignment(msg->lchan);
811 talloc_free(msg->lchan->rqd_ref);
812 msg->lchan->rqd_ref = NULL;
813 msg->lchan->rqd_ta = 0;
814 }
815
Harald Welte6720a432009-11-29 22:45:52 +0100816 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
817
Harald Welte59b04682009-06-10 05:40:52 +0800818 return 0;
819}
820
821/* Chapter 8.4.3: Channel Activate NACK */
822static int rsl_rx_chan_act_nack(struct msgb *msg)
823{
824 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
825 struct tlv_parsed tp;
826
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100827 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100828 gsm_lchan_name(msg->lchan));
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100829
Harald Welte59b04682009-06-10 05:40:52 +0800830 /* BTS has rejected channel activation ?!? */
831 if (dh->ie_chan != RSL_IE_CHAN_NR)
832 return -EINVAL;
833
834 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100835 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
836 const u_int8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
837 print_rsl_cause(LOGL_ERROR, cause,
Harald Weltef1a168d2009-07-28 17:58:09 +0200838 TLVP_LEN(&tp, RSL_IE_CAUSE));
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100839 if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC)
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200840 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Harald Welte (local)c3be50c2009-12-27 18:12:29 +0100841 } else
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200842 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +0200843
Harald Welte (local)ed6d7622009-12-27 11:48:11 +0100844 LOGPC(DRSL, LOGL_ERROR, "\n");
845
Harald Welte6720a432009-11-29 22:45:52 +0100846 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
847
Harald Weltecddb9802009-08-09 19:50:08 +0200848 lchan_free(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800849 return 0;
850}
851
852/* Chapter 8.4.4: Connection Failure Indication */
853static int rsl_rx_conn_fail(struct msgb *msg)
854{
855 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
856 struct tlv_parsed tp;
857
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100858 /* FIXME: print which channel */
Harald Welte (local)4bd76642009-12-26 22:33:09 +0100859 LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL: RELEASING ",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +0100860 gsm_lchan_name(msg->lchan));
Harald Welte59b04682009-06-10 05:40:52 +0800861
862 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
863
Harald Weltef1a168d2009-07-28 17:58:09 +0200864 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +0100865 print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +0200866 TLVP_LEN(&tp, RSL_IE_CAUSE));
867
Harald Welte (local)4bd76642009-12-26 22:33:09 +0100868 LOGPC(DRSL, LOGL_NOTICE, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800869 /* FIXME: only free it after channel release ACK */
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +0200870 counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rf_fail);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +0800871 return rsl_rf_chan_release(msg->lchan, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800872}
873
Harald Weltec20bd1d2009-11-29 19:07:28 +0100874static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
875 const char *prefix)
876{
Harald Welte0e4fa782009-12-16 16:52:07 +0100877 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
878 prefix, rxlev2dbm(mru->full.rx_lev),
879 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Weltec20bd1d2009-11-29 19:07:28 +0100880 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
881 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
882}
883
884static void print_meas_rep(struct gsm_meas_rep *mr)
885{
Harald Welte0e4fa782009-12-16 16:52:07 +0100886 int i;
887
Harald Weltec20bd1d2009-11-29 19:07:28 +0100888 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
889
890 if (mr->flags & MEAS_REP_F_DL_DTX)
891 DEBUGPC(DMEAS, "DTXd ");
892
893 print_meas_rep_uni(&mr->ul, "ul");
894 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
895 if (mr->flags & MEAS_REP_F_MS_TO)
896 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
897
898 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte0e4fa782009-12-16 16:52:07 +0100899 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100900 DEBUGPC(DMEAS, "L1_FPC=%u ",
901 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
902 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
903 }
904
905 if (mr->flags & MEAS_REP_F_UL_DTX)
906 DEBUGPC(DMEAS, "DTXu ");
907 if (mr->flags & MEAS_REP_F_BA1)
908 DEBUGPC(DMEAS, "BA1 ");
909 if (!(mr->flags & MEAS_REP_F_DL_VALID))
910 DEBUGPC(DMEAS, "NOT VALID ");
911 else
912 print_meas_rep_uni(&mr->dl, "dl");
913
914 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte0b833f82009-12-19 18:33:05 +0100915 if (mr->num_cell == 7)
916 return;
Harald Welte0e4fa782009-12-16 16:52:07 +0100917 for (i = 0; i < mr->num_cell; i++) {
918 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
Harald Welte350c2d32009-12-25 23:02:22 +0100919 DEBUGP(DMEAS, "IDX=%u ARFCN=%u BSIC=%u => %d dBm\n",
920 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
Harald Welte0e4fa782009-12-16 16:52:07 +0100921 }
Harald Weltec20bd1d2009-11-29 19:07:28 +0100922}
923
Harald Welte59b04682009-06-10 05:40:52 +0800924static int rsl_rx_meas_res(struct msgb *msg)
925{
926 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
927 struct tlv_parsed tp;
Harald Weltef9476812009-12-15 21:36:05 +0100928 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100929 u_int8_t len;
930 const u_int8_t *val;
931 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800932
Harald Welte4baa9c52009-12-21 13:27:11 +0100933 /* check if this channel is actually active */
934 /* FIXME: maybe this check should be way more generic/centralized */
Harald Weltec88a4432009-12-29 10:44:17 +0100935 if (msg->lchan->state != LCHAN_S_ACTIVE) {
936 LOGP(DRSL, LOGL_NOTICE, "%s: MEAS RES for inactive channel\n",
937 gsm_lchan_name(msg->lchan));
Harald Welte4baa9c52009-12-21 13:27:11 +0100938 return 0;
Harald Weltec88a4432009-12-29 10:44:17 +0100939 }
Harald Welte4baa9c52009-12-21 13:27:11 +0100940
Harald Weltef9476812009-12-15 21:36:05 +0100941 memset(mr, 0, sizeof(*mr));
Harald Welteaa0efa12009-12-16 23:29:34 +0100942 mr->lchan = msg->lchan;
Harald Welte4efcc542009-11-30 19:16:47 +0100943
Harald Welte59b04682009-06-10 05:40:52 +0800944 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
945
Harald Weltec20bd1d2009-11-29 19:07:28 +0100946 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
947 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
948 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
949 return -EIO;
950
951 /* Mandatory Parts */
Harald Weltef9476812009-12-15 21:36:05 +0100952 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100953
954 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
955 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
956 if (len >= 3) {
957 if (val[0] & 0x40)
Harald Weltef9476812009-12-15 21:36:05 +0100958 mr->flags |= MEAS_REP_F_DL_DTX;
959 mr->ul.full.rx_lev = val[0] & 0x3f;
960 mr->ul.sub.rx_lev = val[1] & 0x3f;
961 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
962 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte59b04682009-06-10 05:40:52 +0800963 }
Harald Weltec20bd1d2009-11-29 19:07:28 +0100964
Harald Weltef9476812009-12-15 21:36:05 +0100965 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100966
967 /* Optional Parts */
Harald Welte59b04682009-06-10 05:40:52 +0800968 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Weltef9476812009-12-15 21:36:05 +0100969 mr->ms_timing_offset =
Harald Weltec20bd1d2009-11-29 19:07:28 +0100970 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
971
Harald Weltea1467eb2009-06-20 18:44:35 +0200972 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Weltec20bd1d2009-11-29 19:07:28 +0100973 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Weltef9476812009-12-15 21:36:05 +0100974 mr->flags |= MEAS_REP_F_MS_L1;
975 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100976 if (val[0] & 0x04)
Harald Weltef9476812009-12-15 21:36:05 +0100977 mr->flags |= MEAS_REP_F_FPC;
978 mr->ms_l1.ta = val[1];
Harald Weltea1467eb2009-06-20 18:44:35 +0200979 }
Harald Welte59b04682009-06-10 05:40:52 +0800980 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freyther6d0c8b42009-10-22 15:43:55 +0200981 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Weltef9476812009-12-15 21:36:05 +0100982 rc = gsm48_parse_meas_rep(mr, msg);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100983 if (rc < 0)
984 return rc;
985 }
986
Harald Weltef9476812009-12-15 21:36:05 +0100987 print_meas_rep(mr);
Harald Welte59b04682009-06-10 05:40:52 +0800988
Harald Weltef9476812009-12-15 21:36:05 +0100989 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Welte4efcc542009-11-30 19:16:47 +0100990
Harald Welte59b04682009-06-10 05:40:52 +0800991 return 0;
992}
993
Harald Welte6720a432009-11-29 22:45:52 +0100994/* Chapter 8.4.7 */
995static int rsl_rx_hando_det(struct msgb *msg)
996{
997 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
998 struct tlv_parsed tp;
999
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001000 DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
Harald Welte6720a432009-11-29 22:45:52 +01001001
1002 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1003
1004 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
1005 DEBUGPC(DRSL, "access delay = %u\n",
1006 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
1007 else
1008 DEBUGPC(DRSL, "\n");
1009
1010 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
1011
1012 return 0;
1013}
1014
Harald Welte59b04682009-06-10 05:40:52 +08001015static int abis_rsl_rx_dchan(struct msgb *msg)
1016{
1017 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1018 int rc = 0;
1019 char *ts_name;
1020
1021 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001022 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001023
Harald Welte59b04682009-06-10 05:40:52 +08001024 switch (rslh->c.msg_type) {
1025 case RSL_MT_CHAN_ACTIV_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001026 DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001027 rc = rsl_rx_chan_act_ack(msg);
1028 break;
1029 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001030 rc = rsl_rx_chan_act_nack(msg);
1031 break;
1032 case RSL_MT_CONN_FAIL:
1033 rc = rsl_rx_conn_fail(msg);
1034 break;
1035 case RSL_MT_MEAS_RES:
1036 rc = rsl_rx_meas_res(msg);
1037 break;
Harald Welte6720a432009-11-29 22:45:52 +01001038 case RSL_MT_HANDO_DET:
1039 rc = rsl_rx_hando_det(msg);
1040 break;
Harald Welte59b04682009-06-10 05:40:52 +08001041 case RSL_MT_RF_CHAN_REL_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001042 DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", ts_name);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001043 if (msg->lchan->state != LCHAN_S_REL_REQ && msg->lchan->state != LCHAN_S_REL_ERR)
Harald Welteab2534c2009-12-29 10:52:38 +01001044 LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
1045 gsm_lchan_name(msg->lchan),
1046 gsm_lchans_name(msg->lchan->state));
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001047 bsc_del_timer(&msg->lchan->T3111);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001048 /* we have an error timer pending to release that */
1049 if (msg->lchan->state != LCHAN_S_REL_ERR)
1050 rsl_lchan_set_state(msg->lchan, LCHAN_S_NONE);
Harald Welte59b04682009-06-10 05:40:52 +08001051 lchan_free(msg->lchan);
1052 break;
1053 case RSL_MT_MODE_MODIFY_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001054 DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001055 break;
1056 case RSL_MT_MODE_MODIFY_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001057 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001058 break;
Harald Welteaed946e2009-10-24 10:29:22 +02001059 case RSL_MT_IPAC_PDCH_ACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001060 DEBUGPC(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
Harald Welte2b361522010-03-28 14:42:09 +08001061 msg->lchan->ts->flags |= TS_F_PDCH_MODE;
Harald Welteaed946e2009-10-24 10:29:22 +02001062 break;
1063 case RSL_MT_IPAC_PDCH_ACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001064 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001065 break;
1066 case RSL_MT_IPAC_PDCH_DEACT_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001067 DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
Harald Welte2b361522010-03-28 14:42:09 +08001068 msg->lchan->ts->flags &= ~TS_F_PDCH_MODE;
Harald Welteaed946e2009-10-24 10:29:22 +02001069 break;
1070 case RSL_MT_IPAC_PDCH_DEACT_NACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001071 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
Harald Welteaed946e2009-10-24 10:29:22 +02001072 break;
Harald Welte59b04682009-06-10 05:40:52 +08001073 case RSL_MT_PHY_CONTEXT_CONF:
1074 case RSL_MT_PREPROC_MEAS_RES:
1075 case RSL_MT_TALKER_DET:
1076 case RSL_MT_LISTENER_DET:
1077 case RSL_MT_REMOTE_CODEC_CONF_REP:
1078 case RSL_MT_MR_CODEC_MOD_ACK:
1079 case RSL_MT_MR_CODEC_MOD_NACK:
1080 case RSL_MT_MR_CODEC_MOD_PER:
Harald Weltede4477a2009-12-24 12:20:20 +01001081 LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
1082 "msg 0x%02x\n", ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001083 break;
1084 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001085 LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
1086 ts_name, rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001087 return -EINVAL;
1088 }
1089
1090 return rc;
1091}
1092
1093static int rsl_rx_error_rep(struct msgb *msg)
1094{
1095 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Weltef1a168d2009-07-28 17:58:09 +02001096 struct tlv_parsed tp;
Harald Welte59b04682009-06-10 05:40:52 +08001097
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001098 LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(msg->trx));
Harald Weltef1a168d2009-07-28 17:58:09 +02001099
1100 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1101
1102 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001103 print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001104 TLVP_LEN(&tp, RSL_IE_CAUSE));
1105
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001106 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001107
1108 return 0;
1109}
1110
1111static int abis_rsl_rx_trx(struct msgb *msg)
1112{
1113 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1114 int rc = 0;
1115
1116 switch (rslh->msg_type) {
1117 case RSL_MT_ERROR_REPORT:
1118 rc = rsl_rx_error_rep(msg);
1119 break;
1120 case RSL_MT_RF_RES_IND:
1121 /* interference on idle channels of TRX */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001122 //DEBUGP(DRSL, "%s RF Resource Indication\n", gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001123 break;
1124 case RSL_MT_OVERLOAD:
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001125 /* indicate CCCH / ACCH / processor overload */
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001126 LOGP(DRSL, LOGL_ERROR, "%s CCCH/ACCH/CPU Overload\n",
1127 gsm_trx_name(msg->trx));
Harald Welte59b04682009-06-10 05:40:52 +08001128 break;
1129 default:
Harald Welte (local)ab788cf2009-12-28 23:14:22 +01001130 LOGP(DRSL, LOGL_NOTICE, "%s Unknown Abis RSL TRX message "
1131 "type 0x%02x\n", gsm_trx_name(msg->trx), rslh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001132 return -EINVAL;
1133 }
1134 return rc;
1135}
1136
Harald Welte427dbc42009-08-10 00:26:10 +02001137/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1138static void t3101_expired(void *data)
1139{
1140 struct gsm_lchan *lchan = data;
1141
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001142 rsl_rf_chan_release(lchan, 1);
Harald Welte427dbc42009-08-10 00:26:10 +02001143}
1144
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001145/* If T3111 expires, we will send the RF Channel Request */
1146static void t3111_expired(void *data)
1147{
1148 struct gsm_lchan *lchan = data;
1149
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001150 rsl_rf_chan_release(lchan, 0);
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001151}
1152
laforge50312e82010-06-21 12:08:52 +02001153#define GSM48_LEN2PLEN(a) (((a) << 2) | 1)
1154
Harald Welte59b04682009-06-10 05:40:52 +08001155/* MS has requested a channel on the RACH */
1156static int rsl_rx_chan_rqd(struct msgb *msg)
1157{
1158 struct gsm_bts *bts = msg->trx->bts;
1159 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1160 struct gsm48_req_ref *rqd_ref;
Harald Welte59b04682009-06-10 05:40:52 +08001161 enum gsm_chan_t lctype;
1162 enum gsm_chreq_reason_t chreq_reason;
1163 struct gsm_lchan *lchan;
1164 u_int8_t rqd_ta;
Harald Welte59b04682009-06-10 05:40:52 +08001165
1166 u_int16_t arfcn;
1167 u_int8_t ts_number, subch;
1168
1169 /* parse request reference to be used in immediate assign */
1170 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1171 return -EINVAL;
1172
1173 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1174
1175 /* parse access delay and use as TA */
1176 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1177 return -EINVAL;
1178 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1179
1180 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1181 * request reference RA */
Holger Hans Peter Freyther96c89822009-11-16 17:12:38 +01001182 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1183 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte59b04682009-06-10 05:40:52 +08001184
Harald Weltebdbb7442009-12-22 19:07:32 +01001185 counter_inc(bts->network->stats.chreq.total);
Harald Welte3edc5a92009-12-22 00:41:05 +01001186
Harald Welte59b04682009-06-10 05:40:52 +08001187 /* check availability / allocate channel */
1188 lchan = lchan_alloc(bts, lctype);
1189 if (!lchan) {
Harald Welte (local)e0bb5fa2009-12-27 13:48:09 +01001190 LOGP(DRSL, LOGL_NOTICE, "BTS %d CHAN RQD: no resources for %s 0x%x\n",
Harald Welte (local)02204d02009-12-27 18:05:25 +01001191 msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
Harald Weltebdbb7442009-12-22 19:07:32 +01001192 counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte59b04682009-06-10 05:40:52 +08001193 /* FIXME: send some kind of reject ?!? */
1194 return -ENOMEM;
1195 }
1196
Harald Weltec88a4432009-12-29 10:44:17 +01001197 if (lchan->state != LCHAN_S_NONE)
1198 LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
Harald Welteab2534c2009-12-29 10:52:38 +01001199 "in state %s\n", gsm_lchan_name(lchan),
1200 gsm_lchans_name(lchan->state));
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +02001201 rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
Harald Welte (local)c3be50c2009-12-27 18:12:29 +01001202
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +08001203 /* save the RACH data as we need it after the CHAN ACT ACK */
1204 lchan->rqd_ref = talloc_zero(bts, struct gsm48_req_ref);
1205 if (!lchan->rqd_ref) {
1206 LOGP(DRSL, LOGL_ERROR, "Failed to allocate gsm48_req_ref.\n");
1207 lchan_free(lchan);
1208 return -ENOMEM;
1209 }
1210
1211 memcpy(lchan->rqd_ref, rqd_ref, sizeof(*rqd_ref));
1212 lchan->rqd_ta = rqd_ta;
1213
Harald Welte59b04682009-06-10 05:40:52 +08001214 ts_number = lchan->ts->nr;
1215 arfcn = lchan->ts->trx->arfcn;
1216 subch = lchan->nr;
1217
Harald Welted2dd9de2009-08-30 15:37:11 +09001218 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)cbd46102009-08-13 10:14:26 +02001219 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte9a229e12009-08-10 00:45:40 +02001220 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte39274f42009-07-29 15:41:29 +02001221 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte77234e12009-08-28 23:28:28 +09001222 lchan->tch_mode = GSM48_CMODE_SIGN;
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +08001223
1224 /* FIXME: Start another timer or assume the BTS sends a ACK/NACK? */
Harald Welteb90d7bd2009-12-17 00:31:10 +01001225 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001226
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +08001227 DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
1228 "r=%s ra=0x%02x\n", gsm_lchan_name(lchan), arfcn, subch,
1229 gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
1230 rqd_ref->ra);
1231 return 0;
1232}
1233
1234static int rsl_send_imm_assignment(struct gsm_lchan *lchan)
1235{
1236 struct gsm_bts *bts = lchan->ts->trx->bts;
1237 u_int8_t buf[GSM_MACBLOCK_LEN];
1238 struct gsm48_imm_ass *ia = (struct gsm48_imm_ass *) buf;
1239
Harald Welte59b04682009-06-10 05:40:52 +08001240 /* create IMMEDIATE ASSIGN 04.08 messge */
laforgee06d5982010-06-20 15:18:46 +02001241 memset(ia, 0, sizeof(*ia));
laforge50312e82010-06-21 12:08:52 +02001242 /* we set ia->l2_plen once we know the length of the MA below */
laforgee06d5982010-06-20 15:18:46 +02001243 ia->proto_discr = GSM48_PDISC_RR;
1244 ia->msg_type = GSM48_MT_RR_IMM_ASS;
1245 ia->page_mode = GSM48_PM_SAME;
1246 gsm48_lchan2chan_desc(&ia->chan_desc, lchan);
Harald Weltea42a93f2010-06-14 22:26:10 +02001247
Harald Welte59b04682009-06-10 05:40:52 +08001248 /* use request reference extracted from CHAN_RQD */
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +08001249 memcpy(&ia->req_ref, lchan->rqd_ref, sizeof(ia->req_ref));
1250 ia->timing_advance = lchan->rqd_ta;
Harald Weltea42a93f2010-06-14 22:26:10 +02001251 if (!lchan->ts->hopping.enabled) {
laforgee06d5982010-06-20 15:18:46 +02001252 ia->mob_alloc_len = 0;
Harald Weltea42a93f2010-06-14 22:26:10 +02001253 } else {
laforgee06d5982010-06-20 15:18:46 +02001254 ia->mob_alloc_len = lchan->ts->hopping.ma_len;
1255 memcpy(ia->mob_alloc, lchan->ts->hopping.ma_data, ia->mob_alloc_len);
Harald Weltea42a93f2010-06-14 22:26:10 +02001256 }
Harald Welte07f32182010-06-28 18:41:27 +02001257 /* we need to subtract 1 byte from sizeof(*ia) since ia includes the l2_plen field */
1258 ia->l2_plen = GSM48_LEN2PLEN((sizeof(*ia)-1) + ia->mob_alloc_len);
Harald Welte59b04682009-06-10 05:40:52 +08001259
Harald Welte427dbc42009-08-10 00:26:10 +02001260 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1261 lchan->T3101.cb = t3101_expired;
1262 lchan->T3101.data = lchan;
Holger Hans Peter Freyther26ba2e72009-11-21 21:18:38 +01001263 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001264
1265 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
Holger Hans Peter Freytherc08f6f02010-06-22 12:11:59 +08001266 return rsl_imm_assign_cmd(bts, sizeof(*ia)+ia->mob_alloc_len, (u_int8_t *) ia);
Harald Welte59b04682009-06-10 05:40:52 +08001267}
1268
1269/* MS has requested a channel on the RACH */
1270static int rsl_rx_ccch_load(struct msgb *msg)
1271{
1272 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1273 u_int16_t pg_buf_space;
1274 u_int16_t rach_slot_count = -1;
1275 u_int16_t rach_busy_count = -1;
1276 u_int16_t rach_access_count = -1;
1277
1278 switch (rslh->data[0]) {
1279 case RSL_IE_PAGING_LOAD:
1280 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
Harald Welte008a4922010-04-19 10:24:07 +02001281 if (is_ipaccess_bts(msg->trx->bts) && pg_buf_space == 0xffff) {
1282 /* paging load below configured threshold, use 50 as default */
1283 pg_buf_space = 50;
1284 }
Harald Welte59b04682009-06-10 05:40:52 +08001285 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
1286 break;
1287 case RSL_IE_RACH_LOAD:
1288 if (msg->data_len >= 7) {
1289 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1290 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1291 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1292 }
1293 break;
1294 default:
1295 break;
1296 }
1297
1298 return 0;
1299}
1300
1301static int abis_rsl_rx_cchan(struct msgb *msg)
1302{
1303 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1304 int rc = 0;
1305
1306 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1307
1308 switch (rslh->c.msg_type) {
1309 case RSL_MT_CHAN_RQD:
1310 /* MS has requested a channel on the RACH */
1311 rc = rsl_rx_chan_rqd(msg);
1312 break;
1313 case RSL_MT_CCCH_LOAD_IND:
1314 /* current load on the CCCH */
1315 rc = rsl_rx_ccch_load(msg);
1316 break;
1317 case RSL_MT_DELETE_IND:
1318 /* CCCH overloaded, IMM_ASSIGN was dropped */
1319 case RSL_MT_CBCH_LOAD_IND:
1320 /* current load on the CBCH */
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001321 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1322 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001323 break;
1324 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001325 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1326 "0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001327 return -EINVAL;
1328 }
1329
1330 return rc;
1331}
1332
1333static int rsl_rx_rll_err_ind(struct msgb *msg)
1334{
1335 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1336 u_int8_t *rlm_cause = rllh->data;
1337
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001338 LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s\n",
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001339 gsm_lchan_name(msg->lchan),
Harald Welteb30935e2010-03-25 12:13:02 +08001340 rsl_rlm_cause_name(rlm_cause[1]));
Harald Welteed9a5ab2009-08-09 13:47:35 +02001341
1342 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte (local)bd76cce2009-12-26 23:55:00 +01001343
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +02001344 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED) {
1345 counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rll_err);
Holger Hans Peter Freyther10ea12f2010-05-31 21:38:24 +08001346 return rsl_rf_chan_release(msg->lchan, 1);
Holger Hans Peter Freyther27942e92010-04-17 06:48:29 +02001347 }
Harald Welte692f5852009-07-04 09:40:05 +02001348
Harald Welte59b04682009-06-10 05:40:52 +08001349 return 0;
1350}
1351
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001352static void rsl_handle_release(struct gsm_lchan *lchan)
1353{
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001354 struct gsm_bts *bts;
Holger Hans Peter Freytherd26cbc82010-04-08 22:47:44 +02001355 if (lchan->state != LCHAN_S_REL_REQ)
1356 LOGP(DRSL, LOGL_ERROR, "RF release on %s but state %s\n",
1357 gsm_lchan_name(lchan),
1358 gsm_lchans_name(lchan->state));
1359
1360
Holger Hans Peter Freyther4a00c062010-05-31 21:33:15 +08001361 /* wait a bit to send the RF Channel Release */
1362 lchan->T3111.cb = t3111_expired;
1363 lchan->T3111.data = lchan;
1364 bts = lchan->ts->trx->bts;
1365 bsc_schedule_timer(&lchan->T3111, bts->network->T3111, 0);
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001366}
1367
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001368/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
Harald Welte59b04682009-06-10 05:40:52 +08001369 0x02, 0x06,
1370 0x01, 0x20,
1371 0x02, 0x00,
1372 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1373
1374static int abis_rsl_rx_rll(struct msgb *msg)
1375{
1376 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1377 int rc = 0;
1378 char *ts_name;
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001379 u_int8_t sapi = rllh->link_id & 7;
Harald Welte59b04682009-06-10 05:40:52 +08001380
1381 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001382 ts_name = gsm_lchan_name(msg->lchan);
Harald Weltede4477a2009-12-24 12:20:20 +01001383 DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
Harald Welte59b04682009-06-10 05:40:52 +08001384
1385 switch (rllh->c.msg_type) {
1386 case RSL_MT_DATA_IND:
1387 DEBUGPC(DRLL, "DATA INDICATION\n");
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001388 if (msgb_l2len(msg) >
Harald Welte59b04682009-06-10 05:40:52 +08001389 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1390 rllh->data[0] == RSL_IE_L3_INFO) {
1391 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001392 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001393 }
1394 break;
1395 case RSL_MT_EST_IND:
1396 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welte427dbc42009-08-10 00:26:10 +02001397 /* lchan is established, stop T3101 */
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001398 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welte427dbc42009-08-10 00:26:10 +02001399 bsc_del_timer(&msg->lchan->T3101);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001400 if (msgb_l2len(msg) >
Harald Welte59b04682009-06-10 05:40:52 +08001401 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1402 rllh->data[0] == RSL_IE_L3_INFO) {
1403 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001404 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001405 }
1406 break;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001407 case RSL_MT_EST_CONF:
Harald Welte61402172009-08-09 14:13:58 +02001408 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001409 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001410 rll_indication(msg->lchan, rllh->link_id,
1411 BSC_RLLR_IND_EST_CONF);
1412 break;
Harald Welte59b04682009-06-10 05:40:52 +08001413 case RSL_MT_REL_IND:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001414 /* BTS informs us of having received DISC from MS */
Harald Welteb6601442009-08-04 02:50:21 +02001415 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001416 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001417 rll_indication(msg->lchan, rllh->link_id,
1418 BSC_RLLR_IND_REL_IND);
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001419 rsl_handle_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001420 break;
1421 case RSL_MT_REL_CONF:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001422 /* BTS informs us of having received UA from MS,
1423 * in response to DISC that we've sent earlier */
Harald Welteb6601442009-08-04 02:50:21 +02001424 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001425 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Holger Hans Peter Freyther65f08522010-04-08 22:39:34 +02001426 rsl_handle_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001427 break;
1428 case RSL_MT_ERROR_IND:
Harald Welte59b04682009-06-10 05:40:52 +08001429 rc = rsl_rx_rll_err_ind(msg);
1430 break;
1431 case RSL_MT_UNIT_DATA_IND:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001432 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1433 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001434 break;
1435 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001436 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1437 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001438 }
Harald Welte59b04682009-06-10 05:40:52 +08001439 return rc;
1440}
1441
Harald Welteb284b472009-12-02 01:58:23 +05301442static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Welte98d79f92009-07-28 18:11:56 +02001443{
Harald Welteb284b472009-12-02 01:58:23 +05301444 switch (lchan->tch_mode) {
Harald Welte98d79f92009-07-28 18:11:56 +02001445 case GSM48_CMODE_SPEECH_V1:
Harald Welteb284b472009-12-02 01:58:23 +05301446 switch (lchan->type) {
1447 case GSM_LCHAN_TCH_F:
1448 return 0x00;
1449 case GSM_LCHAN_TCH_H:
1450 return 0x03;
1451 default:
1452 break;
1453 }
Harald Welte98d79f92009-07-28 18:11:56 +02001454 case GSM48_CMODE_SPEECH_EFR:
Harald Welteb284b472009-12-02 01:58:23 +05301455 switch (lchan->type) {
1456 case GSM_LCHAN_TCH_F:
1457 return 0x01;
1458 /* there's no half-rate EFR */
1459 default:
1460 break;
1461 }
Harald Welte98d79f92009-07-28 18:11:56 +02001462 case GSM48_CMODE_SPEECH_AMR:
Harald Welteb284b472009-12-02 01:58:23 +05301463 switch (lchan->type) {
1464 case GSM_LCHAN_TCH_F:
1465 return 0x02;
1466 case GSM_LCHAN_TCH_H:
1467 return 0x05;
1468 default:
1469 break;
1470 }
1471 default:
1472 break;
Harald Welte98d79f92009-07-28 18:11:56 +02001473 }
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001474 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welteb284b472009-12-02 01:58:23 +05301475 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001476 return 0;
Harald Welte98d79f92009-07-28 18:11:56 +02001477}
1478
Sylvain Munaut1338a552009-12-20 22:06:40 +01001479static u_int8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
1480{
1481 switch (lchan->tch_mode) {
1482 case GSM48_CMODE_SPEECH_V1:
1483 switch (lchan->type) {
1484 case GSM_LCHAN_TCH_F:
1485 return RTP_PT_GSM_FULL;
1486 case GSM_LCHAN_TCH_H:
1487 return RTP_PT_GSM_HALF;
1488 default:
1489 break;
1490 }
1491 case GSM48_CMODE_SPEECH_EFR:
1492 switch (lchan->type) {
1493 case GSM_LCHAN_TCH_F:
1494 return RTP_PT_GSM_EFR;
1495 /* there's no half-rate EFR */
1496 default:
1497 break;
1498 }
1499 case GSM48_CMODE_SPEECH_AMR:
1500 switch (lchan->type) {
1501 case GSM_LCHAN_TCH_F:
1502 return RTP_PT_AMR_FULL;
1503 case GSM_LCHAN_TCH_H:
1504 return RTP_PT_AMR_HALF;
1505 default:
1506 break;
1507 }
1508 default:
1509 break;
1510 }
1511 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access rtp payload type for "
1512 "tch_mode == 0x%02x\n & lchan_type == %d",
1513 lchan->tch_mode, lchan->type);
1514 return 0;
1515}
1516
Harald Welte59b04682009-06-10 05:40:52 +08001517/* ip.access specific RSL extensions */
Harald Weltebffa4992009-12-19 16:42:06 +01001518static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
1519{
1520 struct in_addr ip;
1521 u_int16_t port, conn_id;
1522
1523 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
1524 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_IP));
1525 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
1526 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1527 }
1528
1529 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
1530 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_PORT));
1531 port = ntohs(port);
1532 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
1533 lchan->abis_ip.bound_port = port;
1534 }
1535
1536 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
1537 conn_id = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_CONN_ID));
1538 conn_id = ntohs(conn_id);
1539 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
1540 lchan->abis_ip.conn_id = conn_id;
1541 }
1542
1543 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
1544 lchan->abis_ip.rtp_payload2 =
1545 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1546 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
1547 lchan->abis_ip.rtp_payload2);
1548 }
1549
1550 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
1551 lchan->abis_ip.speech_mode =
1552 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
1553 DEBUGPC(DRSL, "speech_mode=0x%02x ",
1554 lchan->abis_ip.speech_mode);
1555 }
1556
1557 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
1558 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_IP));
1559 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
1560 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
1561 }
1562
1563 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
1564 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_PORT));
1565 port = ntohs(port);
1566 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
1567 lchan->abis_ip.connect_port = port;
1568 }
1569}
1570
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001571int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +08001572{
1573 struct msgb *msg = rsl_msgb_alloc();
1574 struct abis_rsl_dchan_hdr *dh;
1575
1576 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001577 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte59b04682009-06-10 05:40:52 +08001578 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1579 dh->chan_nr = lchan2chan_nr(lchan);
1580
Harald Welte98d79f92009-07-28 18:11:56 +02001581 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001582 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001583 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Weltebffa4992009-12-19 16:42:06 +01001584 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001585 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001586
Sylvain Munaut1338a552009-12-20 22:06:40 +01001587 DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x RTP_PAYLOAD=%d\n",
1588 gsm_lchan_name(lchan), lchan->abis_ip.speech_mode,
1589 lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001590
Harald Welte59b04682009-06-10 05:40:52 +08001591 msg->trx = lchan->ts->trx;
1592
1593 return abis_rsl_sendmsg(msg);
1594}
1595
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001596int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Weltebffa4992009-12-19 16:42:06 +01001597 u_int8_t rtp_payload2)
Harald Welte59b04682009-06-10 05:40:52 +08001598{
1599 struct msgb *msg = rsl_msgb_alloc();
1600 struct abis_rsl_dchan_hdr *dh;
Harald Weltebffa4992009-12-19 16:42:06 +01001601 u_int32_t *att_ip;
Harald Welte98d79f92009-07-28 18:11:56 +02001602 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +08001603
1604 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001605 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte59b04682009-06-10 05:40:52 +08001606 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1607 dh->chan_nr = lchan2chan_nr(lchan);
1608
Harald Weltebffa4992009-12-19 16:42:06 +01001609 /* we need to store these now as MDCX_ACK does not return them :( */
1610 lchan->abis_ip.rtp_payload2 = rtp_payload2;
1611 lchan->abis_ip.connect_port = port;
1612 lchan->abis_ip.connect_ip = ip;
1613
Harald Weltefb4a9e92009-07-29 12:12:18 +02001614 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001615 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001616 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Weltefb4a9e92009-07-29 12:12:18 +02001617
Harald Welte98d79f92009-07-28 18:11:56 +02001618 ia.s_addr = htonl(ip);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001619 DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD=%d RTP_PAYLOAD2=%d "
1620 "CONN_ID=%d speech_mode=0x%02x\n", gsm_lchan_name(lchan),
1621 inet_ntoa(ia), port, lchan->abis_ip.rtp_payload, rtp_payload2,
1622 lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001623
Harald Weltebffa4992009-12-19 16:42:06 +01001624 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
1625 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
1626 att_ip = (u_int32_t *) msgb_put(msg, sizeof(ip));
1627 *att_ip = ia.s_addr;
1628 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
1629 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munaut1338a552009-12-20 22:06:40 +01001630 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Welte98d79f92009-07-28 18:11:56 +02001631 if (rtp_payload2)
1632 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1633
Harald Welte59b04682009-06-10 05:40:52 +08001634 msg->trx = lchan->ts->trx;
1635
1636 return abis_rsl_sendmsg(msg);
1637}
1638
Harald Welte9947d9f2009-12-20 16:51:09 +01001639/* tell BTS to connect RTP stream to our local RTP socket */
1640int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
1641{
1642 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
1643 int rc;
1644
1645 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1646 ntohs(rs->rtp.sin_local.sin_port),
1647 /* FIXME: use RTP payload of bound socket, not BTS*/
1648 lchan->abis_ip.rtp_payload2);
1649
1650 return rc;
1651}
1652
Harald Welte2b361522010-03-28 14:42:09 +08001653int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan, int act)
Harald Welteaed946e2009-10-24 10:29:22 +02001654{
1655 struct msgb *msg = rsl_msgb_alloc();
1656 struct abis_rsl_dchan_hdr *dh;
Harald Welte2b361522010-03-28 14:42:09 +08001657 u_int8_t msg_type;
1658
1659 if (act)
1660 msg_type = RSL_MT_IPAC_PDCH_ACT;
1661 else
1662 msg_type = RSL_MT_IPAC_PDCH_DEACT;
Harald Welteaed946e2009-10-24 10:29:22 +02001663
1664 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Harald Welte2b361522010-03-28 14:42:09 +08001665 init_dchan_hdr(dh, msg_type);
Harald Welteaed946e2009-10-24 10:29:22 +02001666 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1667 dh->chan_nr = lchan2chan_nr(lchan);
1668
Harald Welte2b361522010-03-28 14:42:09 +08001669 DEBUGP(DRSL, "%s IPAC_PDCH_%sACT\n", gsm_lchan_name(lchan),
1670 act ? "" : "DE");
Harald Welteaed946e2009-10-24 10:29:22 +02001671
1672 msg->trx = lchan->ts->trx;
1673
1674 return abis_rsl_sendmsg(msg);
1675}
1676
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001677static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001678{
1679 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1680 struct tlv_parsed tv;
Harald Welte87504212009-12-02 01:56:49 +05301681 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001682
1683 /* the BTS has acknowledged a local bind, it now tells us the IP
1684 * address and port number to which it has bound the given logical
1685 * channel */
1686
1687 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1688 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1689 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welteb9498952009-07-12 09:45:05 +02001690 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001691 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte59b04682009-06-10 05:40:52 +08001692 return -EINVAL;
1693 }
Harald Welte50517742009-12-20 15:42:44 +01001694
Harald Weltebffa4992009-12-19 16:42:06 +01001695 ipac_parse_rtp(lchan, &tv);
Harald Welte50517742009-12-20 15:42:44 +01001696
1697 /* in case we don't use direct BTS-to-BTS RTP */
1698 if (!ipacc_rtp_direct) {
1699 int rc;
1700 /* the BTS has successfully bound a TCH to a local ip/port,
1701 * which means we can connect our UDP socket to it */
1702 if (lchan->abis_ip.rtp_socket) {
1703 rtp_socket_free(lchan->abis_ip.rtp_socket);
1704 lchan->abis_ip.rtp_socket = NULL;
1705 }
1706
1707 lchan->abis_ip.rtp_socket = rtp_socket_create();
1708 if (!lchan->abis_ip.rtp_socket)
1709 goto out_err;
1710
1711 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1712 lchan->abis_ip.bound_ip,
1713 lchan->abis_ip.bound_port);
1714 if (rc < 0)
1715 goto out_err;
1716 }
1717
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001718 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001719
1720 return 0;
Harald Welte50517742009-12-20 15:42:44 +01001721out_err:
1722 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +08001723}
1724
Harald Weltebffa4992009-12-19 16:42:06 +01001725static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
1726{
1727 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1728 struct tlv_parsed tv;
1729 struct gsm_lchan *lchan = msg->lchan;
1730
1731 /* the BTS has acknowledged a remote connect request and
1732 * it now tells us the IP address and port number to which it has
1733 * connected the given logical channel */
1734
1735 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1736 ipac_parse_rtp(lchan, &tv);
1737 dispatch_signal(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
1738
1739 return 0;
1740}
1741
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001742static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001743{
1744 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1745 struct tlv_parsed tv;
Harald Welte50517742009-12-20 15:42:44 +01001746 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001747
1748 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte59b04682009-06-10 05:40:52 +08001749
Harald Weltef1a168d2009-07-28 17:58:09 +02001750 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
Harald Weltede4477a2009-12-24 12:20:20 +01001751 print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
Harald Weltef1a168d2009-07-28 17:58:09 +02001752 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte59b04682009-06-10 05:40:52 +08001753
Harald Welte50517742009-12-20 15:42:44 +01001754 /* the BTS tells us a RTP stream has been disconnected */
1755 if (lchan->abis_ip.rtp_socket) {
1756 rtp_socket_free(lchan->abis_ip.rtp_socket);
1757 lchan->abis_ip.rtp_socket = NULL;
1758 }
1759
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001760 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welteba4e58d2009-07-28 18:02:05 +02001761
Harald Welte59b04682009-06-10 05:40:52 +08001762 return 0;
1763}
1764
1765static int abis_rsl_rx_ipacc(struct msgb *msg)
1766{
1767 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltede4477a2009-12-24 12:20:20 +01001768 char *ts_name;
Harald Welte59b04682009-06-10 05:40:52 +08001769 int rc = 0;
1770
1771 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)c4e9c9c2009-12-27 18:16:36 +01001772 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001773
1774 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001775 case RSL_MT_IPAC_CRCX_ACK:
Harald Weltede4477a2009-12-24 12:20:20 +01001776 DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001777 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001778 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001779 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001780 /* somehow the BTS was unable to bind the lchan to its local
1781 * port?!? */
Harald Weltede4477a2009-12-24 12:20:20 +01001782 LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001783 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001784 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte59b04682009-06-10 05:40:52 +08001785 /* the BTS tells us that a connect operation was successful */
Harald Weltede4477a2009-12-24 12:20:20 +01001786 DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
Harald Weltebffa4992009-12-19 16:42:06 +01001787 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001788 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001789 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001790 /* somehow the BTS was unable to connect the lchan to a remote
1791 * port */
Harald Weltede4477a2009-12-24 12:20:20 +01001792 LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
Harald Welte59b04682009-06-10 05:40:52 +08001793 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001794 case RSL_MT_IPAC_DLCX_IND:
Harald Weltede4477a2009-12-24 12:20:20 +01001795 DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001796 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001797 break;
1798 default:
Harald Weltede4477a2009-12-24 12:20:20 +01001799 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n",
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001800 rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001801 break;
1802 }
1803 DEBUGPC(DRSL, "\n");
1804
1805 return rc;
1806}
1807
1808
1809/* Entry-point where L2 RSL from BTS enters */
1810int abis_rsl_rcvmsg(struct msgb *msg)
1811{
Holger Hans Peter Freytherc7d94092009-11-20 15:14:01 +01001812 struct abis_rsl_common_hdr *rslh;
Harald Welte59b04682009-06-10 05:40:52 +08001813 int rc = 0;
1814
Holger Hans Peter Freytherc7d94092009-11-20 15:14:01 +01001815 if (!msg) {
1816 DEBUGP(DRSL, "Empty RSL msg?..\n");
1817 return -1;
1818 }
1819
1820 if (msgb_l2len(msg) < sizeof(*rslh)) {
1821 DEBUGP(DRSL, "Truncated RSL message with l2len: %u\n", msgb_l2len(msg));
1822 return -1;
1823 }
1824
1825 rslh = msgb_l2(msg);
1826
Harald Welte59b04682009-06-10 05:40:52 +08001827 switch (rslh->msg_discr & 0xfe) {
1828 case ABIS_RSL_MDISC_RLL:
1829 rc = abis_rsl_rx_rll(msg);
1830 break;
1831 case ABIS_RSL_MDISC_DED_CHAN:
1832 rc = abis_rsl_rx_dchan(msg);
1833 break;
1834 case ABIS_RSL_MDISC_COM_CHAN:
1835 rc = abis_rsl_rx_cchan(msg);
1836 break;
1837 case ABIS_RSL_MDISC_TRX:
1838 rc = abis_rsl_rx_trx(msg);
1839 break;
1840 case ABIS_RSL_MDISC_LOC:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001841 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001842 rslh->msg_discr);
1843 break;
1844 case ABIS_RSL_MDISC_IPACCESS:
1845 rc = abis_rsl_rx_ipacc(msg);
1846 break;
1847 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001848 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1849 "0x%02x\n", rslh->msg_discr);
Harald Welte59b04682009-06-10 05:40:52 +08001850 return -EINVAL;
1851 }
1852 msgb_free(msg);
1853 return rc;
1854}
1855
Harald Welte59b04682009-06-10 05:40:52 +08001856/* From Table 10.5.33 of GSM 04.08 */
1857int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1858{
Harald Weltea54a2bb2009-12-01 18:04:30 +05301859 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1860 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1861 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001862 } else {
Harald Weltea54a2bb2009-12-01 18:04:30 +05301863 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1864 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001865 }
1866}