blob: 15fe488a30093975b76562eea842eac0242bde15 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Radio Signalling Link messages on the A-bis interface
2 * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
Harald Welte8f5e2392009-02-03 12:57:37 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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>
Harald Welte8470bf22008-12-25 23:28:35 +000025#include <stdlib.h>
Harald Welte52b1f982008-12-23 20:25:15 +000026#include <errno.h>
27#include <sys/types.h>
Harald Welte75099262009-02-16 21:12:08 +000028#include <netinet/in.h>
Harald Welte167df882009-02-17 14:35:45 +000029#include <arpa/inet.h>
Harald Welte52b1f982008-12-23 20:25:15 +000030
Harald Welte8470bf22008-12-25 23:28:35 +000031#include <openbsc/gsm_data.h>
32#include <openbsc/gsm_04_08.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010033#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/abis_rsl.h>
35#include <openbsc/chan_alloc.h>
Harald Welteedcc5272009-08-09 13:47:35 +020036#include <openbsc/bsc_rll.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010038#include <osmocore/tlv.h>
Holger Freyther392209c2009-02-10 00:06:19 +000039#include <openbsc/paging.h>
Harald Welte167df882009-02-17 14:35:45 +000040#include <openbsc/signal.h>
Harald Welte3c7dc6e2009-11-29 19:07:28 +010041#include <openbsc/meas_rep.h>
Harald Welte17f5bf62009-12-20 15:42:44 +010042#include <openbsc/rtp_proxy.h>
Harald Welte2e411c72010-03-01 21:59:06 +010043#include <osmocore/rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000044
Harald Welte8470bf22008-12-25 23:28:35 +000045#define RSL_ALLOC_SIZE 1024
46#define RSL_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000047
Holger Freyther3b72a892009-02-04 00:31:39 +000048#define MAX(a, b) (a) >= (b) ? (a) : (b)
49
Harald Welte52b1f982008-12-23 20:25:15 +000050static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
51{
52 /* mask off the transparent bit ? */
53 msg_type &= 0xfe;
54
Harald Welte8470bf22008-12-25 23:28:35 +000055 if ((msg_type & 0xf0) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +000056 return ABIS_RSL_MDISC_RLL;
Harald Welte8470bf22008-12-25 23:28:35 +000057 if ((msg_type & 0xf0) == 0x10) {
Harald Welte52b1f982008-12-23 20:25:15 +000058 if (msg_type >= 0x19 && msg_type <= 0x22)
59 return ABIS_RSL_MDISC_TRX;
60 else
61 return ABIS_RSL_MDISC_COM_CHAN;
62 }
Harald Welte2d5b6382008-12-27 19:46:06 +000063 if ((msg_type & 0xe0) == 0x20)
Harald Welte52b1f982008-12-23 20:25:15 +000064 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 Welte8470bf22008-12-25 23:28:35 +000077/* 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 Weltea1499d02009-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 Welteb1d4c8e2009-12-17 23:10:46 +010091 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +000092 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 Welteb1d4c8e2009-12-17 23:10:46 +010096 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +000097 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 Welteb1d4c8e2009-12-17 23:10:46 +0100101 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000102 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 Welteb1d4c8e2009-12-17 23:10:46 +0100106 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000107 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 Welteb1d4c8e2009-12-17 23:10:46 +0100112 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000113 chan_nr, ts->pchan);
114 /* FIXME: we should not return first sdcch4 !!! */
115 } else {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100116 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000117 return NULL;
118 }
119
120 lchan = &ts->lchan[lch_idx];
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100121 debug_set_context(BSC_CTX_LCHAN, lchan);
122 debug_set_context(BSC_CTX_SUBSCR, lchan->subscr);
Harald Welte8470bf22008-12-25 23:28:35 +0000123
124 return lchan;
125}
126
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200127/* See Table 10.5.25 of GSM04.08 */
Harald Welte24766092009-12-09 19:18:32 +0100128u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte8470bf22008-12-25 23:28:35 +0000129{
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 Weltea1499d02009-10-24 10:25:50 +0200135 case GSM_PCHAN_PDCH:
136 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte8470bf22008-12-25 23:28:35 +0000137 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
Harald Welte52b1f982008-12-23 20:25:15 +0000162/* 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
Harald Welte8470bf22008-12-25 23:28:35 +0000195static struct msgb *rsl_msgb_alloc(void)
196{
Harald Welte966636f2009-06-26 19:39:35 +0200197 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
198 "RSL");
Harald Welte8470bf22008-12-25 23:28:35 +0000199}
200
Harald Welte362322e2009-02-15 14:36:38 +0000201#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 Welte08d91a52009-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 Welte5b8ed432009-12-24 12:20:20 +0100219static void print_rsl_cause(int lvl, const u_int8_t *cause_v, u_int8_t cause_len)
Harald Welte8830e072009-07-28 17:58:09 +0200220{
Harald Welte7f93cea2009-02-23 00:02:59 +0000221 int i;
222
Harald Welte5b8ed432009-12-24 12:20:20 +0100223 LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ",
Harald Welte8830e072009-07-28 17:58:09 +0200224 cause_v[0], rsl_err_name(cause_v[0]));
225 for (i = 1; i < cause_len-1; i++)
Harald Welte5b8ed432009-12-24 12:20:20 +0100226 LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
Harald Welte7f93cea2009-02-23 00:02:59 +0000227}
228
Harald Welte52b1f982008-12-23 20:25:15 +0000229/* Send a BCCH_INFO message as per Chapter 8.5.1 */
Harald Weltee79769b2009-02-07 00:48:17 +0000230int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000231 const u_int8_t *data, int len)
232{
233 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000234 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000235
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
Harald Weltee79769b2009-02-07 00:48:17 +0000243 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000244
245 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000246}
247
Harald Weltee79769b2009-02-07 00:48:17 +0000248int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000249 const u_int8_t *data, int len)
250{
251 struct abis_rsl_common_hdr *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000252 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000253
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);
Harald Welte702d8702008-12-26 20:25:35 +0000259 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
Harald Welte52b1f982008-12-23 20:25:15 +0000260
Harald Weltee79769b2009-02-07 00:48:17 +0000261 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000262
263 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000264}
265
Harald Weltefcd24452009-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 Welteeab33352009-06-27 03:09:08 +0200269 struct msgb *msg;
Harald Weltefcd24452009-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 Welteeab33352009-06-27 03:09:08 +0200276 msg = rsl_msgb_alloc();
277
Harald Weltefcd24452009-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 Weltefcd24452009-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 Welteeab33352009-06-27 03:09:08 +0200296 struct msgb *msg;
Harald Weltefcd24452009-06-20 18:15:19 +0200297 u_int8_t chan_nr = lchan2chan_nr(lchan);
298 int ctl_lvl;
299
Harald Welte66b6a8d2009-08-09 14:45:18 +0200300 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Weltefcd24452009-06-20 18:15:19 +0200301 if (ctl_lvl < 0)
302 return ctl_lvl;
303
Harald Welteeab33352009-06-27 03:09:08 +0200304 msg = rsl_msgb_alloc();
305
Harald Weltefcd24452009-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 Welte9943c5b2009-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 Welte1a79d362009-11-27 08:55:16 +0100333 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
334 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100335 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte1a79d362009-11-27 08:55:16 +0100336 "but tch_mode != signalling\n");
337
Harald Welte9943c5b2009-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 Welte52b1f982008-12-23 20:25:15 +0000383/* Chapter 8.4.1 */
Harald Welteddab3c72009-02-28 13:19:15 +0000384#if 0
Harald Weltee79769b2009-02-07 00:48:17 +0000385int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
Harald Welte52b1f982008-12-23 20:25:15 +0000386 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;
Harald Welte8470bf22008-12-25 23:28:35 +0000393 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000394
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,
Harald Welte702d8702008-12-26 20:25:35 +0000404 (u_int8_t *) chan_ident);
Harald Welte702d8702008-12-26 20:25:35 +0000405#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000406 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
407 (u_int8_t *) &encr_info);
Harald Welte702d8702008-12-26 20:25:35 +0000408#endif
Harald Welted4c9bf32009-02-15 16:56:18 +0000409 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
Harald Welte52b1f982008-12-23 20:25:15 +0000410 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
411 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
412
Harald Weltee79769b2009-02-07 00:48:17 +0000413 msg->trx = trx;
414
Harald Welte8470bf22008-12-25 23:28:35 +0000415 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000416}
Harald Welteddab3c72009-02-28 13:19:15 +0000417#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000418
Harald Welte8f5e2392009-02-03 12:57:37 +0000419int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welte8d77b952009-12-17 00:31:10 +0100420 u_int8_t ta, u_int8_t ho_ref)
Harald Welte4b634542008-12-27 01:55:51 +0000421{
422 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200423 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200424 int rc;
Harald Welte4b634542008-12-27 01:55:51 +0000425
426 u_int8_t chan_nr = lchan2chan_nr(lchan);
427 u_int16_t arfcn = lchan->ts->trx->arfcn;
428 struct rsl_ie_chan_mode cm;
429 struct rsl_ie_chan_ident ci;
430
Harald Welte9943c5b2009-07-29 15:41:29 +0200431 rc = channel_mode_from_lchan(&cm, lchan);
432 if (rc < 0)
433 return rc;
Harald Welte4b634542008-12-27 01:55:51 +0000434
Harald Welte02b0e092009-02-28 13:11:07 +0000435 memset(&ci, 0, sizeof(ci));
Harald Welte4b634542008-12-27 01:55:51 +0000436 ci.chan_desc.iei = 0x64;
437 ci.chan_desc.chan_nr = chan_nr;
Harald Welte02b0e092009-02-28 13:11:07 +0000438 ci.chan_desc.oct3 = (lchan->ts->trx->bts->tsc << 5) | ((arfcn & 0x3ff) >> 8);
Harald Welte4b634542008-12-27 01:55:51 +0000439 ci.chan_desc.oct4 = arfcn & 0xff;
440
Harald Welteeab33352009-06-27 03:09:08 +0200441 msg = rsl_msgb_alloc();
Harald Welte4b634542008-12-27 01:55:51 +0000442 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
443 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
444 dh->chan_nr = chan_nr;
445
446 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte4b634542008-12-27 01:55:51 +0000447 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
448 (u_int8_t *) &cm);
Harald Welte8d77b952009-12-17 00:31:10 +0100449 /* For compatibility with Phase 1 */
Harald Welte4b634542008-12-27 01:55:51 +0000450 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
451 (u_int8_t *) &ci);
Harald Welte08d91a52009-08-30 15:37:11 +0900452
453 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
454 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
455 rc = build_encr_info(encr_info, lchan);
456 if (rc > 0)
457 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
458 }
459
Harald Welte8d77b952009-12-17 00:31:10 +0100460 switch (act_type) {
461 case RSL_ACT_INTER_ASYNC:
462 case RSL_ACT_INTER_SYNC:
463 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
464 break;
465 default:
466 break;
467 }
468
Harald Welted4c9bf32009-02-15 16:56:18 +0000469 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
470 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
Harald Welte4b634542008-12-27 01:55:51 +0000471 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
472
Holger Hans Peter Freyther93b6c652010-01-28 04:45:05 +0100473 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
474 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
475 (u_int8_t *) &lchan->mr_conf);
476
Harald Weltee79769b2009-02-07 00:48:17 +0000477 msg->trx = lchan->ts->trx;
478
Harald Welte4b634542008-12-27 01:55:51 +0000479 return abis_rsl_sendmsg(msg);
480}
481
Harald Welte470abb72009-07-29 11:38:15 +0200482/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welteda783762009-02-18 03:29:53 +0000483int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
484{
485 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200486 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200487 int rc;
Harald Welteda783762009-02-18 03:29:53 +0000488
489 u_int8_t chan_nr = lchan2chan_nr(lchan);
490 struct rsl_ie_chan_mode cm;
491
Harald Welte9943c5b2009-07-29 15:41:29 +0200492 rc = channel_mode_from_lchan(&cm, lchan);
493 if (rc < 0)
494 return rc;
Harald Welteda783762009-02-18 03:29:53 +0000495
Harald Welteeab33352009-06-27 03:09:08 +0200496 msg = rsl_msgb_alloc();
Harald Welteda783762009-02-18 03:29:53 +0000497 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
498 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
499 dh->chan_nr = chan_nr;
500
501 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
502 (u_int8_t *) &cm);
Harald Welte08d91a52009-08-30 15:37:11 +0900503
504 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
505 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
506 rc = build_encr_info(encr_info, lchan);
507 if (rc > 0)
508 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
509 }
510
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100511 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
512 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
513 (u_int8_t *) &lchan->mr_conf);
514 }
515
Harald Welte08d91a52009-08-30 15:37:11 +0900516 msg->trx = lchan->ts->trx;
517
518 return abis_rsl_sendmsg(msg);
519}
520
521/* Chapter 8.4.6: Send the encryption command with given L3 info */
522int rsl_encryption_cmd(struct msgb *msg)
523{
524 struct abis_rsl_dchan_hdr *dh;
525 struct gsm_lchan *lchan = msg->lchan;
526 u_int8_t chan_nr = lchan2chan_nr(lchan);
527 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut82aa6842009-09-27 11:13:18 +0200528 u_int8_t l3_len = msg->len;
Harald Welte08d91a52009-08-30 15:37:11 +0900529 int rc;
530
531 /* First push the L3 IE tag and length */
532 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
533
534 /* then the link identifier (SAPI0, main sign link) */
535 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
536
537 /* then encryption information */
538 rc = build_encr_info(encr_info, lchan);
539 if (rc <= 0)
540 return rc;
541 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
542
543 /* and finally the DCHAN header */
544 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
545 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
546 dh->chan_nr = chan_nr;
Harald Welteda783762009-02-18 03:29:53 +0000547
548 msg->trx = lchan->ts->trx;
549
550 return abis_rsl_sendmsg(msg);
551}
552
Harald Welte115d1032009-08-10 11:43:22 +0200553/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteae0f2362009-07-19 18:36:49 +0200554int rsl_deact_sacch(struct gsm_lchan *lchan)
555{
556 struct abis_rsl_dchan_hdr *dh;
557 struct msgb *msg = rsl_msgb_alloc();
558
559 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
560 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
561 dh->chan_nr = lchan2chan_nr(lchan);
562
563 msg->lchan = lchan;
564 msg->trx = lchan->ts->trx;
565
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100566 DEBUGP(DRSL, "%s DEACTivate SACCH CMD\n", gsm_lchan_name(lchan));
Harald Welteae0f2362009-07-19 18:36:49 +0200567
568 return abis_rsl_sendmsg(msg);
569}
570
Harald Welte115d1032009-08-10 11:43:22 +0200571/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
572int rsl_rf_chan_release(struct gsm_lchan *lchan)
Harald Welte52b1f982008-12-23 20:25:15 +0000573{
574 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000575 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000576
577 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
578 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
Harald Welte8470bf22008-12-25 23:28:35 +0000579 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000580
Harald Welte8470bf22008-12-25 23:28:35 +0000581 msg->lchan = lchan;
582 msg->trx = lchan->ts->trx;
583
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100584 DEBUGP(DRSL, "%s RF Channel Release CMD\n", gsm_lchan_name(lchan));
Harald Welte2d5b6382008-12-27 19:46:06 +0000585
Harald Welte115d1032009-08-10 11:43:22 +0200586 /* BTS will respond by RF CHAN REL ACK */
Harald Welte8470bf22008-12-25 23:28:35 +0000587 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000588}
589
590int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
591 u_int8_t *ms_ident, u_int8_t chan_needed)
592{
593 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000594 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000595
596 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
597 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
598 dh->chan_nr = RSL_CHAN_PCH_AGCH;
599
600 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
Harald Welte255539c2008-12-28 02:26:27 +0000601 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
Harald Welte52b1f982008-12-23 20:25:15 +0000602 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
603
Harald Welte8470bf22008-12-25 23:28:35 +0000604 msg->trx = bts->c0;
605
606 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000607}
608
Holger Freyther7448a532009-01-04 20:18:23 +0000609int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
610 struct gsm_subscriber *subscr)
611{
Holger Freytherca362a62009-01-04 21:05:01 +0000612#if 0
Holger Freyther7448a532009-01-04 20:18:23 +0000613 u_int8_t mi[128];
614 unsigned int mi_len;
615 u_int8_t paging_group;
Holger Freytherca362a62009-01-04 21:05:01 +0000616#endif
Holger Freyther7448a532009-01-04 20:18:23 +0000617
618 return -1;
619}
620
Harald Welte52b1f982008-12-23 20:25:15 +0000621int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
622{
623 int i, len = strlen(str_in);
624
625 for (i = 0; i < len; i++) {
626 int num = str_in[i] - 0x30;
627 if (num < 0 || num > 9)
628 return -1;
629 if (i % 2 == 0)
630 bcd_out[i/2] = num;
631 else
632 bcd_out[i/2] |= (num << 4);
633 }
634
635 return 0;
636}
637
Harald Welte702d8702008-12-26 20:25:35 +0000638/* Chapter 8.5.6 */
Harald Welte52b1f982008-12-23 20:25:15 +0000639int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
640{
Harald Welte8470bf22008-12-25 23:28:35 +0000641 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000642 struct abis_rsl_dchan_hdr *dh;
Harald Welte362322e2009-02-15 14:36:38 +0000643 u_int8_t buf[MACBLOCK_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000644
645 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
646 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
647 dh->chan_nr = RSL_CHAN_PCH_AGCH;
648
Harald Welte362322e2009-02-15 14:36:38 +0000649 switch (bts->type) {
650 case GSM_BTS_TYPE_BS11:
651 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
652 break;
653 default:
654 /* If phase 2, construct a FULL_IMM_ASS_INFO */
655 pad_macblock(buf, val, len);
656 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
657 break;
658 }
Harald Welte52b1f982008-12-23 20:25:15 +0000659
Harald Welte8470bf22008-12-25 23:28:35 +0000660 msg->trx = bts->c0;
661
662 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000663}
664
Harald Welte67fa91b2009-08-10 09:51:40 +0200665/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte31c48932009-08-10 10:07:33 +0200666int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte67fa91b2009-08-10 09:51:40 +0200667{
668 struct msgb *msg = rsl_msgb_alloc();
669 struct abis_rsl_dchan_hdr *dh;
670
671 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
672 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte3c456d02009-08-10 11:26:14 +0200673 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte67fa91b2009-08-10 09:51:40 +0200674 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte31c48932009-08-10 10:07:33 +0200675 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte67fa91b2009-08-10 09:51:40 +0200676
Harald Welte5b8ed432009-12-24 12:20:20 +0100677 DEBUGP(DRSL, "%s TX Siemens MRPCI 0x%02x\n",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100678 gsm_lchan_name(lchan), *(u_int8_t *)mrpci);
Harald Welte3c456d02009-08-10 11:26:14 +0200679
680 msg->trx = lchan->ts->trx;
681
Harald Welte67fa91b2009-08-10 09:51:40 +0200682 return abis_rsl_sendmsg(msg);
683}
684
685
Harald Welte8470bf22008-12-25 23:28:35 +0000686/* Send "DATA REQUEST" message with given L3 Info payload */
Harald Welte52b1f982008-12-23 20:25:15 +0000687/* Chapter 8.3.1 */
Harald Welte8470bf22008-12-25 23:28:35 +0000688int rsl_data_request(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +0000689{
Harald Welte8470bf22008-12-25 23:28:35 +0000690 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000691 struct abis_rsl_rll_hdr *rh;
692
Harald Welte8470bf22008-12-25 23:28:35 +0000693 if (msg->lchan == NULL) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100694 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000695 return -EINVAL;
696 }
Harald Welte52b1f982008-12-23 20:25:15 +0000697
Harald Welte8470bf22008-12-25 23:28:35 +0000698 /* First push the L3 IE tag and length */
Harald Welte4b634542008-12-27 01:55:51 +0000699 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welte8470bf22008-12-25 23:28:35 +0000700
701 /* Then push the RSL header */
Harald Welte52b1f982008-12-23 20:25:15 +0000702 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
Harald Welte2e411c72010-03-01 21:59:06 +0100703 rsl_init_rll_hdr(rh, RSL_MT_DATA_REQ);
Harald Welte4a543e82009-02-28 13:17:55 +0000704 rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Harald Welte8470bf22008-12-25 23:28:35 +0000705 rh->chan_nr = lchan2chan_nr(msg->lchan);
706 rh->link_id = link_id;
Harald Welte52b1f982008-12-23 20:25:15 +0000707
Harald Welte8470bf22008-12-25 23:28:35 +0000708 msg->trx = msg->lchan->ts->trx;
709
710 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000711}
712
Harald Welteedcc5272009-08-09 13:47:35 +0200713/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
714/* Chapter 8.3.1 */
715int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
716{
717 struct msgb *msg = rsl_msgb_alloc();
718 struct abis_rsl_rll_hdr *rh;
719
720 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
Harald Welte2e411c72010-03-01 21:59:06 +0100721 rsl_init_rll_hdr(rh, RSL_MT_EST_REQ);
Harald Welteedcc5272009-08-09 13:47:35 +0200722 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
723 rh->chan_nr = lchan2chan_nr(lchan);
724 rh->link_id = link_id;
725
726 msg->trx = lchan->ts->trx;
727
728 return abis_rsl_sendmsg(msg);
729}
730
Harald Welted2dc1de2009-08-08 13:15:07 +0200731/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
732 This is what higher layers should call. The BTS then responds with
733 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
734 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
735 lchan_free() */
736int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id)
737{
738 struct msgb *msg = rsl_msgb_alloc();
739 struct abis_rsl_rll_hdr *rh;
740
741 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
Harald Welte2e411c72010-03-01 21:59:06 +0100742 rsl_init_rll_hdr(rh, RSL_MT_REL_REQ);
Harald Welted2dc1de2009-08-08 13:15:07 +0200743 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
744 rh->chan_nr = lchan2chan_nr(lchan);
745 rh->link_id = link_id;
Harald Welte6c3d2ed2009-08-10 00:19:36 +0200746 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, 0); /* normal release */
Harald Welted2dc1de2009-08-08 13:15:07 +0200747
Harald Welte8e93b792009-12-29 10:44:17 +0100748 lchan->state = LCHAN_S_REL_REQ;
749 /* FIXME: start some timer in case we don't receive a REL ACK ? */
750
Harald Welted2dc1de2009-08-08 13:15:07 +0200751 msg->trx = lchan->ts->trx;
752
753 return abis_rsl_sendmsg(msg);
754}
755
Harald Welte702d8702008-12-26 20:25:35 +0000756/* Chapter 8.4.2: Channel Activate Acknowledge */
757static int rsl_rx_chan_act_ack(struct msgb *msg)
758{
759 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
760
761 /* BTS has confirmed channel activation, we now need
762 * to assign the activated channel to the MS */
Harald Welte4b634542008-12-27 01:55:51 +0000763 if (rslh->ie_chan != RSL_IE_CHAN_NR)
764 return -EINVAL;
Harald Welted011e8b2009-11-29 22:45:52 +0100765
Harald Welte8e93b792009-12-29 10:44:17 +0100766 if (msg->lchan->state != LCHAN_S_ACT_REQ)
Harald Welte1887f9d2009-12-29 10:52:38 +0100767 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
768 gsm_lchan_name(msg->lchan),
769 gsm_lchans_name(msg->lchan->state));
Harald Welteb8bfc562009-12-21 13:27:11 +0100770 msg->lchan->state = LCHAN_S_ACTIVE;
771
Harald Welted011e8b2009-11-29 22:45:52 +0100772 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
773
Harald Welte4b634542008-12-27 01:55:51 +0000774 return 0;
775}
Harald Welte702d8702008-12-26 20:25:35 +0000776
Harald Welte4b634542008-12-27 01:55:51 +0000777/* Chapter 8.4.3: Channel Activate NACK */
778static int rsl_rx_chan_act_nack(struct msgb *msg)
779{
Harald Welte6dab0552009-05-01 17:21:37 +0000780 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
781 struct tlv_parsed tp;
Harald Welte4b634542008-12-27 01:55:51 +0000782
Harald Welte (local)91b603d2009-12-27 11:48:11 +0100783 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100784 gsm_lchan_name(msg->lchan));
Harald Welte (local)91b603d2009-12-27 11:48:11 +0100785
Harald Welte6dab0552009-05-01 17:21:37 +0000786 /* BTS has rejected channel activation ?!? */
787 if (dh->ie_chan != RSL_IE_CHAN_NR)
Harald Welte4b634542008-12-27 01:55:51 +0000788 return -EINVAL;
Harald Welte6dab0552009-05-01 17:21:37 +0000789
790 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte (local)3e460312009-12-27 18:12:29 +0100791 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
792 const u_int8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
793 print_rsl_cause(LOGL_ERROR, cause,
Harald Welte8830e072009-07-28 17:58:09 +0200794 TLVP_LEN(&tp, RSL_IE_CAUSE));
Harald Welte (local)3e460312009-12-27 18:12:29 +0100795 if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC)
796 msg->lchan->state = LCHAN_S_NONE;
797 } else
798 msg->lchan->state = LCHAN_S_NONE;
799
Harald Welte (local)91b603d2009-12-27 11:48:11 +0100800 LOGPC(DRSL, LOGL_ERROR, "\n");
801
Harald Welted011e8b2009-11-29 22:45:52 +0100802 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
803
Harald Welte3073a9f2009-08-09 19:50:08 +0200804 lchan_free(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000805 return 0;
Harald Welte702d8702008-12-26 20:25:35 +0000806}
807
Harald Welte7f93cea2009-02-23 00:02:59 +0000808/* Chapter 8.4.4: Connection Failure Indication */
809static int rsl_rx_conn_fail(struct msgb *msg)
810{
811 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
812 struct tlv_parsed tp;
813
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100814 /* FIXME: print which channel */
Harald Welte (local)fc057502009-12-26 22:33:09 +0100815 LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL: RELEASING ",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100816 gsm_lchan_name(msg->lchan));
Harald Welte7f93cea2009-02-23 00:02:59 +0000817
818 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
819
Harald Welte8830e072009-07-28 17:58:09 +0200820 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +0100821 print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +0200822 TLVP_LEN(&tp, RSL_IE_CAUSE));
823
Harald Welte (local)fc057502009-12-26 22:33:09 +0100824 LOGPC(DRSL, LOGL_NOTICE, "\n");
Harald Welte7f93cea2009-02-23 00:02:59 +0000825 /* FIXME: only free it after channel release ACK */
Harald Welte115d1032009-08-10 11:43:22 +0200826 return rsl_rf_chan_release(msg->lchan);
Harald Welte7f93cea2009-02-23 00:02:59 +0000827}
828
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100829static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
830 const char *prefix)
831{
Harald Welte6739dfb2009-12-16 16:52:07 +0100832 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
833 prefix, rxlev2dbm(mru->full.rx_lev),
834 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100835 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
836 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
837}
838
839static void print_meas_rep(struct gsm_meas_rep *mr)
840{
Harald Welte6739dfb2009-12-16 16:52:07 +0100841 int i;
842
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100843 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
844
845 if (mr->flags & MEAS_REP_F_DL_DTX)
846 DEBUGPC(DMEAS, "DTXd ");
847
848 print_meas_rep_uni(&mr->ul, "ul");
849 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
850 if (mr->flags & MEAS_REP_F_MS_TO)
851 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
852
853 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte6739dfb2009-12-16 16:52:07 +0100854 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100855 DEBUGPC(DMEAS, "L1_FPC=%u ",
856 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
857 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
858 }
859
860 if (mr->flags & MEAS_REP_F_UL_DTX)
861 DEBUGPC(DMEAS, "DTXu ");
862 if (mr->flags & MEAS_REP_F_BA1)
863 DEBUGPC(DMEAS, "BA1 ");
864 if (!(mr->flags & MEAS_REP_F_DL_VALID))
865 DEBUGPC(DMEAS, "NOT VALID ");
866 else
867 print_meas_rep_uni(&mr->dl, "dl");
868
869 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte479015b2009-12-19 18:33:05 +0100870 if (mr->num_cell == 7)
871 return;
Harald Welte6739dfb2009-12-16 16:52:07 +0100872 for (i = 0; i < mr->num_cell; i++) {
873 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
Harald Welte303e5e02009-12-25 23:02:22 +0100874 DEBUGP(DMEAS, "IDX=%u ARFCN=%u BSIC=%u => %d dBm\n",
875 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
Harald Welte6739dfb2009-12-16 16:52:07 +0100876 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100877}
878
Harald Welte440fed02009-05-01 18:43:47 +0000879static int rsl_rx_meas_res(struct msgb *msg)
880{
881 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
882 struct tlv_parsed tp;
Harald Welted12b0fd2009-12-15 21:36:05 +0100883 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100884 u_int8_t len;
885 const u_int8_t *val;
886 int rc;
Harald Welte440fed02009-05-01 18:43:47 +0000887
Harald Welteb8bfc562009-12-21 13:27:11 +0100888 /* check if this channel is actually active */
889 /* FIXME: maybe this check should be way more generic/centralized */
Harald Welte8e93b792009-12-29 10:44:17 +0100890 if (msg->lchan->state != LCHAN_S_ACTIVE) {
891 LOGP(DRSL, LOGL_NOTICE, "%s: MEAS RES for inactive channel\n",
892 gsm_lchan_name(msg->lchan));
Harald Welteb8bfc562009-12-21 13:27:11 +0100893 return 0;
Harald Welte8e93b792009-12-29 10:44:17 +0100894 }
Harald Welteb8bfc562009-12-21 13:27:11 +0100895
Harald Welted12b0fd2009-12-15 21:36:05 +0100896 memset(mr, 0, sizeof(*mr));
Harald Welte33e65972009-12-16 23:29:34 +0100897 mr->lchan = msg->lchan;
Harald Weltedbb1d882009-11-30 19:16:47 +0100898
Harald Welte440fed02009-05-01 18:43:47 +0000899 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
900
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100901 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
902 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
903 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
904 return -EIO;
905
906 /* Mandatory Parts */
Harald Welted12b0fd2009-12-15 21:36:05 +0100907 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100908
909 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
910 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
911 if (len >= 3) {
912 if (val[0] & 0x40)
Harald Welted12b0fd2009-12-15 21:36:05 +0100913 mr->flags |= MEAS_REP_F_DL_DTX;
914 mr->ul.full.rx_lev = val[0] & 0x3f;
915 mr->ul.sub.rx_lev = val[1] & 0x3f;
916 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
917 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte440fed02009-05-01 18:43:47 +0000918 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100919
Harald Welted12b0fd2009-12-15 21:36:05 +0100920 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100921
922 /* Optional Parts */
Harald Welte440fed02009-05-01 18:43:47 +0000923 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Welted12b0fd2009-12-15 21:36:05 +0100924 mr->ms_timing_offset =
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100925 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
926
Harald Weltefe9af262009-06-20 18:44:35 +0200927 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100928 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +0100929 mr->flags |= MEAS_REP_F_MS_L1;
930 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100931 if (val[0] & 0x04)
Harald Welted12b0fd2009-12-15 21:36:05 +0100932 mr->flags |= MEAS_REP_F_FPC;
933 mr->ms_l1.ta = val[1];
Harald Weltefe9af262009-06-20 18:44:35 +0200934 }
Harald Weltef7c43522009-06-09 20:24:21 +0000935 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freytherddd918f2009-10-22 15:43:55 +0200936 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +0100937 rc = gsm48_parse_meas_rep(mr, msg);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100938 if (rc < 0)
939 return rc;
940 }
941
Harald Welted12b0fd2009-12-15 21:36:05 +0100942 print_meas_rep(mr);
Harald Welte60d68f12009-06-05 20:07:43 +0000943
Harald Welted12b0fd2009-12-15 21:36:05 +0100944 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Weltedbb1d882009-11-30 19:16:47 +0100945
Harald Welte75d34a82009-05-23 06:11:13 +0000946 return 0;
Harald Welte440fed02009-05-01 18:43:47 +0000947}
948
Harald Welted011e8b2009-11-29 22:45:52 +0100949/* Chapter 8.4.7 */
950static int rsl_rx_hando_det(struct msgb *msg)
951{
952 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
953 struct tlv_parsed tp;
954
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100955 DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
Harald Welted011e8b2009-11-29 22:45:52 +0100956
957 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
958
959 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
960 DEBUGPC(DRSL, "access delay = %u\n",
961 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
962 else
963 DEBUGPC(DRSL, "\n");
964
965 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
966
967 return 0;
968}
969
Harald Welte52b1f982008-12-23 20:25:15 +0000970static int abis_rsl_rx_dchan(struct msgb *msg)
971{
Harald Welte8470bf22008-12-25 23:28:35 +0000972 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
973 int rc = 0;
Harald Weltef325eb42009-02-19 17:07:39 +0000974 char *ts_name;
Harald Welte52b1f982008-12-23 20:25:15 +0000975
Harald Welte8470bf22008-12-25 23:28:35 +0000976 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100977 ts_name = gsm_lchan_name(msg->lchan);
Harald Weltef325eb42009-02-19 17:07:39 +0000978
Harald Welte8470bf22008-12-25 23:28:35 +0000979 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000980 case RSL_MT_CHAN_ACTIV_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +0100981 DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
Harald Welte4b634542008-12-27 01:55:51 +0000982 rc = rsl_rx_chan_act_ack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000983 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000984 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte4b634542008-12-27 01:55:51 +0000985 rc = rsl_rx_chan_act_nack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000986 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000987 case RSL_MT_CONN_FAIL:
Harald Welte7f93cea2009-02-23 00:02:59 +0000988 rc = rsl_rx_conn_fail(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000989 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000990 case RSL_MT_MEAS_RES:
Harald Welte440fed02009-05-01 18:43:47 +0000991 rc = rsl_rx_meas_res(msg);
Harald Welte2d5b6382008-12-27 19:46:06 +0000992 break;
Harald Welted011e8b2009-11-29 22:45:52 +0100993 case RSL_MT_HANDO_DET:
994 rc = rsl_rx_hando_det(msg);
995 break;
Harald Welte2d5b6382008-12-27 19:46:06 +0000996 case RSL_MT_RF_CHAN_REL_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +0100997 DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", ts_name);
Harald Welte8e93b792009-12-29 10:44:17 +0100998 if (msg->lchan->state != LCHAN_S_REL_REQ)
Harald Welte1887f9d2009-12-29 10:52:38 +0100999 LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
1000 gsm_lchan_name(msg->lchan),
1001 gsm_lchans_name(msg->lchan->state));
Harald Welteb8bfc562009-12-21 13:27:11 +01001002 msg->lchan->state = LCHAN_S_NONE;
Harald Welte2d5b6382008-12-27 19:46:06 +00001003 lchan_free(msg->lchan);
Harald Welte8470bf22008-12-25 23:28:35 +00001004 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001005 case RSL_MT_MODE_MODIFY_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001006 DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
Harald Welteda783762009-02-18 03:29:53 +00001007 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001008 case RSL_MT_MODE_MODIFY_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001009 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
Harald Welteda783762009-02-18 03:29:53 +00001010 break;
Harald Welte9c880c92009-10-24 10:29:22 +02001011 case RSL_MT_IPAC_PDCH_ACT_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001012 DEBUGPC(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001013 break;
1014 case RSL_MT_IPAC_PDCH_ACT_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001015 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001016 break;
1017 case RSL_MT_IPAC_PDCH_DEACT_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001018 DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001019 break;
1020 case RSL_MT_IPAC_PDCH_DEACT_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001021 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001022 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001023 case RSL_MT_PHY_CONTEXT_CONF:
1024 case RSL_MT_PREPROC_MEAS_RES:
Harald Welte52b1f982008-12-23 20:25:15 +00001025 case RSL_MT_TALKER_DET:
1026 case RSL_MT_LISTENER_DET:
1027 case RSL_MT_REMOTE_CODEC_CONF_REP:
1028 case RSL_MT_MR_CODEC_MOD_ACK:
1029 case RSL_MT_MR_CODEC_MOD_NACK:
1030 case RSL_MT_MR_CODEC_MOD_PER:
Harald Welte5b8ed432009-12-24 12:20:20 +01001031 LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
1032 "msg 0x%02x\n", ts_name, rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001033 break;
1034 default:
Harald Welte5b8ed432009-12-24 12:20:20 +01001035 LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
1036 ts_name, rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001037 return -EINVAL;
1038 }
Harald Weltef325eb42009-02-19 17:07:39 +00001039
Harald Welte8470bf22008-12-25 23:28:35 +00001040 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001041}
1042
Harald Welte702d8702008-12-26 20:25:35 +00001043static int rsl_rx_error_rep(struct msgb *msg)
1044{
1045 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8830e072009-07-28 17:58:09 +02001046 struct tlv_parsed tp;
Harald Welte702d8702008-12-26 20:25:35 +00001047
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001048 LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(msg->trx));
Harald Welte8830e072009-07-28 17:58:09 +02001049
1050 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1051
1052 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +01001053 print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +02001054 TLVP_LEN(&tp, RSL_IE_CAUSE));
1055
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001056 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte702d8702008-12-26 20:25:35 +00001057
1058 return 0;
1059}
1060
Harald Welte52b1f982008-12-23 20:25:15 +00001061static int abis_rsl_rx_trx(struct msgb *msg)
1062{
Harald Welte702d8702008-12-26 20:25:35 +00001063 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001064 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001065
1066 switch (rslh->msg_type) {
Harald Welte702d8702008-12-26 20:25:35 +00001067 case RSL_MT_ERROR_REPORT:
1068 rc = rsl_rx_error_rep(msg);
1069 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001070 case RSL_MT_RF_RES_IND:
1071 /* interference on idle channels of TRX */
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001072 //DEBUGP(DRSL, "%s RF Resource Indication\n", gsm_trx_name(msg->trx));
Harald Welte8f5e2392009-02-03 12:57:37 +00001073 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001074 case RSL_MT_OVERLOAD:
1075 /* indicate CCCH / ACCH / processor overload */
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001076 LOGP(DRSL, LOGL_ERROR, "%s CCCH/ACCH/CPU Overload\n",
1077 gsm_trx_name(msg->trx));
Harald Welte52b1f982008-12-23 20:25:15 +00001078 break;
1079 default:
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001080 LOGP(DRSL, LOGL_NOTICE, "%s Unknown Abis RSL TRX message "
1081 "type 0x%02x\n", gsm_trx_name(msg->trx), rslh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001082 return -EINVAL;
1083 }
Harald Welte8470bf22008-12-25 23:28:35 +00001084 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001085}
1086
Harald Welteb7e81162009-08-10 00:26:10 +02001087/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1088static void t3101_expired(void *data)
1089{
1090 struct gsm_lchan *lchan = data;
1091
Harald Welte115d1032009-08-10 11:43:22 +02001092 rsl_rf_chan_release(lchan);
Harald Welteb7e81162009-08-10 00:26:10 +02001093}
1094
Harald Welte8470bf22008-12-25 23:28:35 +00001095/* MS has requested a channel on the RACH */
Harald Welte52b1f982008-12-23 20:25:15 +00001096static int rsl_rx_chan_rqd(struct msgb *msg)
1097{
Harald Welte702d8702008-12-26 20:25:35 +00001098 struct gsm_bts *bts = msg->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001099 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1100 struct gsm48_req_ref *rqd_ref;
Harald Welte52b1f982008-12-23 20:25:15 +00001101 struct gsm48_imm_ass ia;
Harald Welte8470bf22008-12-25 23:28:35 +00001102 enum gsm_chan_t lctype;
Harald Welte2cbe0922008-12-29 04:09:31 +00001103 enum gsm_chreq_reason_t chreq_reason;
Harald Welte8470bf22008-12-25 23:28:35 +00001104 struct gsm_lchan *lchan;
1105 u_int8_t rqd_ta;
Holger Freyther3186bf22008-12-29 06:23:49 +00001106 int ret;
Harald Welte8470bf22008-12-25 23:28:35 +00001107
Harald Welte52b1f982008-12-23 20:25:15 +00001108 u_int16_t arfcn;
1109 u_int8_t ts_number, subch;
1110
Harald Welte8470bf22008-12-25 23:28:35 +00001111 /* parse request reference to be used in immediate assign */
1112 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1113 return -EINVAL;
1114
1115 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1116
1117 /* parse access delay and use as TA */
1118 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1119 return -EINVAL;
1120 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1121
1122 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1123 * request reference RA */
Holger Hans Peter Freytherf7d752f2009-11-16 17:12:38 +01001124 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1125 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte2cbe0922008-12-29 04:09:31 +00001126
Harald Welteffa55a42009-12-22 19:07:32 +01001127 counter_inc(bts->network->stats.chreq.total);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001128
Harald Welte8470bf22008-12-25 23:28:35 +00001129 /* check availability / allocate channel */
1130 lchan = lchan_alloc(bts, lctype);
1131 if (!lchan) {
Harald Welte (local)2f5df852009-12-27 13:48:09 +01001132 LOGP(DRSL, LOGL_NOTICE, "BTS %d CHAN RQD: no resources for %s 0x%x\n",
Harald Welte (local)ccd88452009-12-27 18:05:25 +01001133 msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
Harald Welteffa55a42009-12-22 19:07:32 +01001134 counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte8470bf22008-12-25 23:28:35 +00001135 /* FIXME: send some kind of reject ?!? */
1136 return -ENOMEM;
1137 }
1138
Harald Welte8e93b792009-12-29 10:44:17 +01001139 if (lchan->state != LCHAN_S_NONE)
1140 LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
Harald Welte1887f9d2009-12-29 10:52:38 +01001141 "in state %s\n", gsm_lchan_name(lchan),
1142 gsm_lchans_name(lchan->state));
Harald Welte (local)3e460312009-12-27 18:12:29 +01001143 lchan->state = LCHAN_S_ACT_REQ;
1144
Harald Welte8470bf22008-12-25 23:28:35 +00001145 ts_number = lchan->ts->nr;
1146 arfcn = lchan->ts->trx->arfcn;
1147 subch = lchan->nr;
Harald Welte52b1f982008-12-23 20:25:15 +00001148
Harald Welte08d91a52009-08-30 15:37:11 +09001149 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)0e451d02009-08-13 10:14:26 +02001150 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte0b2124b2009-08-10 00:45:40 +02001151 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte9943c5b2009-07-29 15:41:29 +02001152 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte196d0522009-08-28 23:28:28 +09001153 lchan->tch_mode = GSM48_CMODE_SIGN;
Harald Welte8d77b952009-12-17 00:31:10 +01001154 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte52b1f982008-12-23 20:25:15 +00001155
1156 /* create IMMEDIATE ASSIGN 04.08 messge */
1157 memset(&ia, 0, sizeof(ia));
1158 ia.l2_plen = 0x2d;
1159 ia.proto_discr = GSM48_PDISC_RR;
1160 ia.msg_type = GSM48_MT_RR_IMM_ASS;
Harald Welte2d5b6382008-12-27 19:46:06 +00001161 ia.page_mode = GSM48_PM_SAME;
Harald Welte4b634542008-12-27 01:55:51 +00001162 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +00001163 ia.chan_desc.h0.h = 0;
1164 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
1165 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
Harald Welte814c4b72009-07-21 20:55:56 +02001166 ia.chan_desc.h0.tsc = bts->tsc;
Harald Welte8470bf22008-12-25 23:28:35 +00001167 /* use request reference extracted from CHAN_RQD */
1168 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
1169 ia.timing_advance = rqd_ta;
Harald Welte52b1f982008-12-23 20:25:15 +00001170 ia.mob_alloc_len = 0;
1171
Harald Welte5b8ed432009-12-24 12:20:20 +01001172 DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001173 "r=%s ra=0x%02x\n", gsm_lchan_name(lchan), arfcn, subch,
Harald Welte (local)ccd88452009-12-27 18:05:25 +01001174 gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
Harald Welte4a543e82009-02-28 13:17:55 +00001175 rqd_ref->ra);
Harald Welte75a983f2008-12-27 21:34:06 +00001176
Harald Welteb7e81162009-08-10 00:26:10 +02001177 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1178 lchan->T3101.cb = t3101_expired;
1179 lchan->T3101.data = lchan;
Holger Hans Peter Freytherc4d88ad2009-11-21 21:18:38 +01001180 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Holger Freyther3186bf22008-12-29 06:23:49 +00001181
Harald Welte52b1f982008-12-23 20:25:15 +00001182 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
Holger Freyther3186bf22008-12-29 06:23:49 +00001183 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
1184
Harald Welte817f3c82008-12-30 14:57:59 +00001185 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001186}
1187
Harald Welteea280442009-02-02 22:29:56 +00001188/* MS has requested a channel on the RACH */
1189static int rsl_rx_ccch_load(struct msgb *msg)
1190{
1191 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1192 u_int16_t pg_buf_space;
Holger Freyther8c563cf2009-02-03 20:08:51 +00001193 u_int16_t rach_slot_count = -1;
1194 u_int16_t rach_busy_count = -1;
1195 u_int16_t rach_access_count = -1;
Harald Welteea280442009-02-02 22:29:56 +00001196
1197 switch (rslh->data[0]) {
1198 case RSL_IE_PAGING_LOAD:
1199 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
Holger Freyther392209c2009-02-10 00:06:19 +00001200 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
Harald Welteea280442009-02-02 22:29:56 +00001201 break;
1202 case RSL_IE_RACH_LOAD:
Holger Freyther8c563cf2009-02-03 20:08:51 +00001203 if (msg->data_len >= 7) {
1204 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1205 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1206 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1207 }
Harald Welteea280442009-02-02 22:29:56 +00001208 break;
1209 default:
1210 break;
1211 }
1212
1213 return 0;
1214}
1215
Harald Welte52b1f982008-12-23 20:25:15 +00001216static int abis_rsl_rx_cchan(struct msgb *msg)
1217{
Harald Welteea280442009-02-02 22:29:56 +00001218 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001219 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001220
Harald Welte8470bf22008-12-25 23:28:35 +00001221 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1222
1223 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001224 case RSL_MT_CHAN_RQD:
1225 /* MS has requested a channel on the RACH */
1226 rc = rsl_rx_chan_rqd(msg);
1227 break;
Harald Welteea280442009-02-02 22:29:56 +00001228 case RSL_MT_CCCH_LOAD_IND:
1229 /* current load on the CCCH */
1230 rc = rsl_rx_ccch_load(msg);
1231 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001232 case RSL_MT_DELETE_IND:
1233 /* CCCH overloaded, IMM_ASSIGN was dropped */
1234 case RSL_MT_CBCH_LOAD_IND:
1235 /* current load on the CBCH */
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001236 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1237 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001238 break;
1239 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001240 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1241 "0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001242 return -EINVAL;
1243 }
Harald Welte8470bf22008-12-25 23:28:35 +00001244
1245 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001246}
1247
Harald Welte4b634542008-12-27 01:55:51 +00001248static int rsl_rx_rll_err_ind(struct msgb *msg)
1249{
1250 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1251 u_int8_t *rlm_cause = rllh->data;
1252
Harald Welte (local)9538efc2009-12-26 23:55:00 +01001253 LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s\n",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001254 gsm_lchan_name(msg->lchan),
Harald Welte2e411c72010-03-01 21:59:06 +01001255 get_value_string(rsl_rlm_cause_strs, rlm_cause[1]));
Harald Welteedcc5272009-08-09 13:47:35 +02001256
1257 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte (local)9538efc2009-12-26 23:55:00 +01001258
Harald Welte81543bc2009-07-04 09:40:05 +02001259 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED)
Harald Welte115d1032009-08-10 11:43:22 +02001260 return rsl_rf_chan_release(msg->lchan);
Harald Welte81543bc2009-07-04 09:40:05 +02001261
Harald Welte4b634542008-12-27 01:55:51 +00001262 return 0;
1263}
Harald Weltef325eb42009-02-19 17:07:39 +00001264
Harald Welte52b1f982008-12-23 20:25:15 +00001265/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
1266 0x02, 0x06,
1267 0x01, 0x20,
1268 0x02, 0x00,
1269 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1270
1271static int abis_rsl_rx_rll(struct msgb *msg)
1272{
1273 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltef325eb42009-02-19 17:07:39 +00001274 int rc = 0;
1275 char *ts_name;
Harald Welte (local)daef6062009-08-14 11:41:12 +02001276 u_int8_t sapi = rllh->link_id & 7;
Harald Welte8470bf22008-12-25 23:28:35 +00001277
1278 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001279 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte5b8ed432009-12-24 12:20:20 +01001280 DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
Harald Welte52b1f982008-12-23 20:25:15 +00001281
1282 switch (rllh->c.msg_type) {
1283 case RSL_MT_DATA_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001284 DEBUGPC(DRLL, "DATA INDICATION\n");
Harald Welte4a543e82009-02-28 13:17:55 +00001285 if (msgb_l2len(msg) >
1286 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1287 rllh->data[0] == RSL_IE_L3_INFO) {
1288 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001289 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001290 }
Harald Welte52b1f982008-12-23 20:25:15 +00001291 break;
1292 case RSL_MT_EST_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001293 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welteb7e81162009-08-10 00:26:10 +02001294 /* lchan is established, stop T3101 */
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001295 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welteb7e81162009-08-10 00:26:10 +02001296 bsc_del_timer(&msg->lchan->T3101);
Harald Welte4a543e82009-02-28 13:17:55 +00001297 if (msgb_l2len(msg) >
1298 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1299 rllh->data[0] == RSL_IE_L3_INFO) {
1300 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001301 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001302 }
Harald Welte52b1f982008-12-23 20:25:15 +00001303 break;
Harald Welteedcc5272009-08-09 13:47:35 +02001304 case RSL_MT_EST_CONF:
Harald Welte1c409272009-08-09 14:13:58 +02001305 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001306 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteedcc5272009-08-09 13:47:35 +02001307 rll_indication(msg->lchan, rllh->link_id,
1308 BSC_RLLR_IND_EST_CONF);
1309 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001310 case RSL_MT_REL_IND:
Harald Welted2dc1de2009-08-08 13:15:07 +02001311 /* BTS informs us of having received DISC from MS */
Harald Welte602f2b82009-08-04 02:50:21 +02001312 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001313 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteedcc5272009-08-09 13:47:35 +02001314 rll_indication(msg->lchan, rllh->link_id,
1315 BSC_RLLR_IND_REL_IND);
Harald Welted2dc1de2009-08-08 13:15:07 +02001316 /* we can now releae the channel on the BTS/Abis side */
Harald Welte115d1032009-08-10 11:43:22 +02001317 /* FIXME: officially we need to start T3111 and wait for
1318 * some grace period */
1319 rsl_rf_chan_release(msg->lchan);
Harald Welte2d5b6382008-12-27 19:46:06 +00001320 break;
1321 case RSL_MT_REL_CONF:
Harald Welted2dc1de2009-08-08 13:15:07 +02001322 /* BTS informs us of having received UA from MS,
1323 * in response to DISC that we've sent earlier */
Harald Welte602f2b82009-08-04 02:50:21 +02001324 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001325 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welted2dc1de2009-08-08 13:15:07 +02001326 /* we can now releae the channel on the BTS/Abis side */
Harald Welte115d1032009-08-10 11:43:22 +02001327 /* FIXME: officially we need to start T3111 and wait for
1328 * some grace period */
1329 rsl_rf_chan_release(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +00001330 break;
1331 case RSL_MT_ERROR_IND:
1332 rc = rsl_rx_rll_err_ind(msg);
1333 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001334 case RSL_MT_UNIT_DATA_IND:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001335 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1336 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001337 break;
1338 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001339 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1340 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001341 }
Harald Welte8470bf22008-12-25 23:28:35 +00001342 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001343}
1344
Harald Welte0603c9d2009-12-02 01:58:23 +05301345static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Weltef4e79f22009-07-28 18:11:56 +02001346{
Harald Welte0603c9d2009-12-02 01:58:23 +05301347 switch (lchan->tch_mode) {
Harald Weltef4e79f22009-07-28 18:11:56 +02001348 case GSM48_CMODE_SPEECH_V1:
Harald Welte0603c9d2009-12-02 01:58:23 +05301349 switch (lchan->type) {
1350 case GSM_LCHAN_TCH_F:
1351 return 0x00;
1352 case GSM_LCHAN_TCH_H:
1353 return 0x03;
1354 default:
1355 break;
1356 }
Harald Weltef4e79f22009-07-28 18:11:56 +02001357 case GSM48_CMODE_SPEECH_EFR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301358 switch (lchan->type) {
1359 case GSM_LCHAN_TCH_F:
1360 return 0x01;
1361 /* there's no half-rate EFR */
1362 default:
1363 break;
1364 }
Harald Weltef4e79f22009-07-28 18:11:56 +02001365 case GSM48_CMODE_SPEECH_AMR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301366 switch (lchan->type) {
1367 case GSM_LCHAN_TCH_F:
1368 return 0x02;
1369 case GSM_LCHAN_TCH_H:
1370 return 0x05;
1371 default:
1372 break;
1373 }
1374 default:
1375 break;
Harald Weltef4e79f22009-07-28 18:11:56 +02001376 }
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001377 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welte0603c9d2009-12-02 01:58:23 +05301378 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001379 return 0;
Harald Weltef4e79f22009-07-28 18:11:56 +02001380}
1381
Harald Welte75099262009-02-16 21:12:08 +00001382/* ip.access specific RSL extensions */
Harald Welte5e3d91b2009-12-19 16:42:06 +01001383static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
1384{
1385 struct in_addr ip;
1386 u_int16_t port, conn_id;
1387
1388 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
1389 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_IP));
1390 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
1391 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1392 }
1393
1394 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
1395 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_PORT));
1396 port = ntohs(port);
1397 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
1398 lchan->abis_ip.bound_port = port;
1399 }
1400
1401 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
1402 conn_id = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_CONN_ID));
1403 conn_id = ntohs(conn_id);
1404 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
1405 lchan->abis_ip.conn_id = conn_id;
1406 }
1407
1408 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
1409 lchan->abis_ip.rtp_payload2 =
1410 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1411 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
1412 lchan->abis_ip.rtp_payload2);
1413 }
1414
1415 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
1416 lchan->abis_ip.speech_mode =
1417 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
1418 DEBUGPC(DRSL, "speech_mode=0x%02x ",
1419 lchan->abis_ip.speech_mode);
1420 }
1421
1422 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
1423 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_IP));
1424 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
1425 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
1426 }
1427
1428 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
1429 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_PORT));
1430 port = ntohs(port);
1431 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
1432 lchan->abis_ip.connect_port = port;
1433 }
1434}
1435
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001436int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte75099262009-02-16 21:12:08 +00001437{
1438 struct msgb *msg = rsl_msgb_alloc();
1439 struct abis_rsl_dchan_hdr *dh;
1440
1441 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001442 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte75099262009-02-16 21:12:08 +00001443 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1444 dh->chan_nr = lchan2chan_nr(lchan);
1445
Harald Weltef4e79f22009-07-28 18:11:56 +02001446 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Welte5e3d91b2009-12-19 16:42:06 +01001447 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
1448 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001449
Harald Welte5b8ed432009-12-24 12:20:20 +01001450 DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x\n",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001451 gsm_lchan_name(lchan), lchan->abis_ip.speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001452
Harald Welte75099262009-02-16 21:12:08 +00001453 msg->trx = lchan->ts->trx;
1454
1455 return abis_rsl_sendmsg(msg);
1456}
1457
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001458int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Welte5e3d91b2009-12-19 16:42:06 +01001459 u_int8_t rtp_payload2)
Harald Welte75099262009-02-16 21:12:08 +00001460{
1461 struct msgb *msg = rsl_msgb_alloc();
1462 struct abis_rsl_dchan_hdr *dh;
Harald Welte5e3d91b2009-12-19 16:42:06 +01001463 u_int32_t *att_ip;
Harald Weltef4e79f22009-07-28 18:11:56 +02001464 struct in_addr ia;
Harald Welte75099262009-02-16 21:12:08 +00001465
1466 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001467 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte75099262009-02-16 21:12:08 +00001468 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1469 dh->chan_nr = lchan2chan_nr(lchan);
1470
Harald Welte5e3d91b2009-12-19 16:42:06 +01001471 /* we need to store these now as MDCX_ACK does not return them :( */
1472 lchan->abis_ip.rtp_payload2 = rtp_payload2;
1473 lchan->abis_ip.connect_port = port;
1474 lchan->abis_ip.connect_ip = ip;
1475
Harald Welte58ca5b72009-07-29 12:12:18 +02001476 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Welte5e3d91b2009-12-19 16:42:06 +01001477 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Harald Welte58ca5b72009-07-29 12:12:18 +02001478
Harald Weltef4e79f22009-07-28 18:11:56 +02001479 ia.s_addr = htonl(ip);
Harald Welte5b8ed432009-12-24 12:20:20 +01001480 DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD2=%d CONN_ID=%d "
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001481 "speech_mode=0x%02x\n", gsm_lchan_name(lchan), inet_ntoa(ia), port,
Harald Welte5e3d91b2009-12-19 16:42:06 +01001482 rtp_payload2, lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001483
Harald Welte5e3d91b2009-12-19 16:42:06 +01001484 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
1485 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
1486 att_ip = (u_int32_t *) msgb_put(msg, sizeof(ip));
1487 *att_ip = ia.s_addr;
1488 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
1489 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001490 if (rtp_payload2)
1491 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1492
Harald Welte75099262009-02-16 21:12:08 +00001493 msg->trx = lchan->ts->trx;
1494
1495 return abis_rsl_sendmsg(msg);
1496}
1497
Harald Weltea72273e2009-12-20 16:51:09 +01001498/* tell BTS to connect RTP stream to our local RTP socket */
1499int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
1500{
1501 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
1502 int rc;
1503
1504 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1505 ntohs(rs->rtp.sin_local.sin_port),
1506 /* FIXME: use RTP payload of bound socket, not BTS*/
1507 lchan->abis_ip.rtp_payload2);
1508
1509 return rc;
1510}
1511
Harald Welte9c880c92009-10-24 10:29:22 +02001512int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan)
1513{
1514 struct msgb *msg = rsl_msgb_alloc();
1515 struct abis_rsl_dchan_hdr *dh;
1516
1517 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1518 init_dchan_hdr(dh, RSL_MT_IPAC_PDCH_ACT);
1519 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1520 dh->chan_nr = lchan2chan_nr(lchan);
1521
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001522 DEBUGP(DRSL, "%s IPAC_PDCH_ACT\n", gsm_lchan_name(lchan));
Harald Welte9c880c92009-10-24 10:29:22 +02001523
1524 msg->trx = lchan->ts->trx;
1525
1526 return abis_rsl_sendmsg(msg);
1527}
1528
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001529static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00001530{
1531 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1532 struct tlv_parsed tv;
Harald Welte2c828992009-12-02 01:56:49 +05301533 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75099262009-02-16 21:12:08 +00001534
1535 /* the BTS has acknowledged a local bind, it now tells us the IP
1536 * address and port number to which it has bound the given logical
1537 * channel */
1538
1539 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1540 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1541 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welte86c162d2009-07-12 09:45:05 +02001542 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001543 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte75099262009-02-16 21:12:08 +00001544 return -EINVAL;
1545 }
Harald Welte17f5bf62009-12-20 15:42:44 +01001546
Harald Welte5e3d91b2009-12-19 16:42:06 +01001547 ipac_parse_rtp(lchan, &tv);
Harald Welte17f5bf62009-12-20 15:42:44 +01001548
1549 /* in case we don't use direct BTS-to-BTS RTP */
1550 if (!ipacc_rtp_direct) {
1551 int rc;
1552 /* the BTS has successfully bound a TCH to a local ip/port,
1553 * which means we can connect our UDP socket to it */
1554 if (lchan->abis_ip.rtp_socket) {
1555 rtp_socket_free(lchan->abis_ip.rtp_socket);
1556 lchan->abis_ip.rtp_socket = NULL;
1557 }
1558
1559 lchan->abis_ip.rtp_socket = rtp_socket_create();
1560 if (!lchan->abis_ip.rtp_socket)
1561 goto out_err;
1562
1563 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1564 lchan->abis_ip.bound_ip,
1565 lchan->abis_ip.bound_port);
1566 if (rc < 0)
1567 goto out_err;
1568 }
1569
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001570 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte167df882009-02-17 14:35:45 +00001571
Harald Welte75099262009-02-16 21:12:08 +00001572 return 0;
Harald Welte17f5bf62009-12-20 15:42:44 +01001573out_err:
1574 return -EIO;
Harald Welte75099262009-02-16 21:12:08 +00001575}
1576
Harald Welte5e3d91b2009-12-19 16:42:06 +01001577static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
1578{
1579 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1580 struct tlv_parsed tv;
1581 struct gsm_lchan *lchan = msg->lchan;
1582
1583 /* the BTS has acknowledged a remote connect request and
1584 * it now tells us the IP address and port number to which it has
1585 * connected the given logical channel */
1586
1587 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1588 ipac_parse_rtp(lchan, &tv);
1589 dispatch_signal(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
1590
1591 return 0;
1592}
1593
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001594static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00001595{
1596 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1597 struct tlv_parsed tv;
Harald Welte17f5bf62009-12-20 15:42:44 +01001598 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75099262009-02-16 21:12:08 +00001599
1600 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte75099262009-02-16 21:12:08 +00001601
Harald Welte8830e072009-07-28 17:58:09 +02001602 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +01001603 print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +02001604 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte75099262009-02-16 21:12:08 +00001605
Harald Welte17f5bf62009-12-20 15:42:44 +01001606 /* the BTS tells us a RTP stream has been disconnected */
1607 if (lchan->abis_ip.rtp_socket) {
1608 rtp_socket_free(lchan->abis_ip.rtp_socket);
1609 lchan->abis_ip.rtp_socket = NULL;
1610 }
1611
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001612 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welte888b1142009-07-28 18:02:05 +02001613
Harald Welte75099262009-02-16 21:12:08 +00001614 return 0;
1615}
1616
1617static int abis_rsl_rx_ipacc(struct msgb *msg)
1618{
1619 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte5b8ed432009-12-24 12:20:20 +01001620 char *ts_name;
Harald Welte75099262009-02-16 21:12:08 +00001621 int rc = 0;
1622
1623 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001624 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte75099262009-02-16 21:12:08 +00001625
1626 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001627 case RSL_MT_IPAC_CRCX_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001628 DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001629 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte75099262009-02-16 21:12:08 +00001630 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001631 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00001632 /* somehow the BTS was unable to bind the lchan to its local
1633 * port?!? */
Harald Welte5b8ed432009-12-24 12:20:20 +01001634 LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
Harald Welte75099262009-02-16 21:12:08 +00001635 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001636 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte75099262009-02-16 21:12:08 +00001637 /* the BTS tells us that a connect operation was successful */
Harald Welte5b8ed432009-12-24 12:20:20 +01001638 DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
Harald Welte5e3d91b2009-12-19 16:42:06 +01001639 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte75099262009-02-16 21:12:08 +00001640 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001641 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00001642 /* somehow the BTS was unable to connect the lchan to a remote
1643 * port */
Harald Welte5b8ed432009-12-24 12:20:20 +01001644 LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
Harald Welte75099262009-02-16 21:12:08 +00001645 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001646 case RSL_MT_IPAC_DLCX_IND:
Harald Welte5b8ed432009-12-24 12:20:20 +01001647 DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001648 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte75099262009-02-16 21:12:08 +00001649 break;
1650 default:
Harald Welte5b8ed432009-12-24 12:20:20 +01001651 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n",
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001652 rllh->c.msg_type);
Harald Welte75099262009-02-16 21:12:08 +00001653 break;
1654 }
Harald Welte6dab0552009-05-01 17:21:37 +00001655 DEBUGPC(DRSL, "\n");
Harald Welte75099262009-02-16 21:12:08 +00001656
1657 return rc;
1658}
1659
1660
Harald Welte52b1f982008-12-23 20:25:15 +00001661/* Entry-point where L2 RSL from BTS enters */
Harald Welte8470bf22008-12-25 23:28:35 +00001662int abis_rsl_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001663{
1664 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
Harald Welte8f5e2392009-02-03 12:57:37 +00001665 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001666
1667 switch (rslh->msg_discr & 0xfe) {
1668 case ABIS_RSL_MDISC_RLL:
1669 rc = abis_rsl_rx_rll(msg);
1670 break;
1671 case ABIS_RSL_MDISC_DED_CHAN:
1672 rc = abis_rsl_rx_dchan(msg);
1673 break;
1674 case ABIS_RSL_MDISC_COM_CHAN:
Harald Welte52b1f982008-12-23 20:25:15 +00001675 rc = abis_rsl_rx_cchan(msg);
1676 break;
Harald Welte8470bf22008-12-25 23:28:35 +00001677 case ABIS_RSL_MDISC_TRX:
1678 rc = abis_rsl_rx_trx(msg);
1679 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001680 case ABIS_RSL_MDISC_LOC:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001681 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte8f5e2392009-02-03 12:57:37 +00001682 rslh->msg_discr);
1683 break;
Harald Welte75099262009-02-16 21:12:08 +00001684 case ABIS_RSL_MDISC_IPACCESS:
1685 rc = abis_rsl_rx_ipacc(msg);
1686 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001687 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001688 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1689 "0x%02x\n", rslh->msg_discr);
Harald Welte52b1f982008-12-23 20:25:15 +00001690 return -EINVAL;
1691 }
Harald Welte4f4a3902008-12-26 00:04:49 +00001692 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001693 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001694}
Holger Freyther3b72a892009-02-04 00:31:39 +00001695
Holger Freyther3b72a892009-02-04 00:31:39 +00001696/* From Table 10.5.33 of GSM 04.08 */
1697int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1698{
Harald Weltea43f7892009-12-01 18:04:30 +05301699 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1700 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1701 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001702 } else {
Harald Weltea43f7892009-12-01 18:04:30 +05301703 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1704 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001705 }
1706}