blob: 8af3b9bd426968ea5a07235e86afd2b4e261c232 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Radio Signalling Link messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
Harald Welte3c9c5f92010-03-04 10:33:10 +01004/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther85825352011-12-27 22:24:17 +01005 * (C) 2012 by Holger Hans Peter Freyther
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
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>
Harald Welte75099262009-02-16 21:12:08 +000027#include <netinet/in.h>
Harald Welte167df882009-02-17 14:35:45 +000028#include <arpa/inet.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/gsm_data.h>
31#include <openbsc/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/gsm/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000033#include <openbsc/abis_rsl.h>
34#include <openbsc/chan_alloc.h>
Harald Welteedcc5272009-08-09 13:47:35 +020035#include <openbsc/bsc_rll.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/gsm/tlv.h>
Maxc08ee712016-05-11 12:45:13 +020038#include <osmocom/gsm/protocol/gsm_04_08.h>
39#include <osmocom/gsm/protocol/gsm_08_58.h>
Holger Freyther392209c2009-02-10 00:06:19 +000040#include <openbsc/paging.h>
Harald Welte167df882009-02-17 14:35:45 +000041#include <openbsc/signal.h>
Harald Welte3c7dc6e2009-11-29 19:07:28 +010042#include <openbsc/meas_rep.h>
Harald Welte17f5bf62009-12-20 15:42:44 +010043#include <openbsc/rtp_proxy.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020044#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010045#include <osmocom/gsm/rsl.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010046#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080047
Harald Welte8470bf22008-12-25 23:28:35 +000048#define RSL_ALLOC_SIZE 1024
49#define RSL_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000050
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +010051enum sacch_deact {
52 SACCH_NONE,
53 SACCH_DEACTIVATE,
54};
55
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080056static int rsl_send_imm_assignment(struct gsm_lchan *lchan);
Holger Hans Peter Freytherf0405062014-04-06 12:21:05 +020057static void error_timeout_cb(void *data);
Neels Hofmeyrb91e6002016-07-23 19:45:15 +020058static int dyn_ts_switchover_start(struct gsm_lchan *lchan,
59 enum gsm_phys_chan_config to_pchan);
60static int dyn_ts_switchover_continue(struct gsm_lchan *lchan);
61static int dyn_ts_switchover_failed(struct gsm_lchan *lchan, int rc);
62static void dyn_ts_switchover_complete(struct gsm_lchan *lchan);
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080063
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +010064static void send_lchan_signal(int sig_no, struct gsm_lchan *lchan,
65 struct gsm_meas_rep *resp)
66{
67 struct lchan_signal_data sig;
68 sig.lchan = lchan;
69 sig.mr = resp;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +020070 osmo_signal_dispatch(SS_LCHAN, sig_no, &sig);
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +010071}
72
Holger Hans Peter Freyther93599a22012-12-06 19:09:58 +010073static void do_lchan_free(struct gsm_lchan *lchan)
74{
Holger Hans Peter Freytherf0405062014-04-06 12:21:05 +020075 /* We start the error timer to make the channel available again */
76 if (lchan->state == LCHAN_S_REL_ERR) {
77 lchan->error_timer.data = lchan;
78 lchan->error_timer.cb = error_timeout_cb;
79 osmo_timer_schedule(&lchan->error_timer,
80 lchan->ts->trx->bts->network->T3111 + 2, 0);
81 } else {
Holger Hans Peter Freyther93599a22012-12-06 19:09:58 +010082 rsl_lchan_set_state(lchan, LCHAN_S_NONE);
Holger Hans Peter Freytherf0405062014-04-06 12:21:05 +020083 }
Holger Hans Peter Freyther93599a22012-12-06 19:09:58 +010084 lchan_free(lchan);
85}
86
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020087static uint8_t mdisc_by_msgtype(uint8_t msg_type)
Harald Welte52b1f982008-12-23 20:25:15 +000088{
89 /* mask off the transparent bit ? */
90 msg_type &= 0xfe;
91
Harald Welte8470bf22008-12-25 23:28:35 +000092 if ((msg_type & 0xf0) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +000093 return ABIS_RSL_MDISC_RLL;
Harald Welte8470bf22008-12-25 23:28:35 +000094 if ((msg_type & 0xf0) == 0x10) {
Harald Welte52b1f982008-12-23 20:25:15 +000095 if (msg_type >= 0x19 && msg_type <= 0x22)
96 return ABIS_RSL_MDISC_TRX;
97 else
98 return ABIS_RSL_MDISC_COM_CHAN;
99 }
Harald Welte2d5b6382008-12-27 19:46:06 +0000100 if ((msg_type & 0xe0) == 0x20)
Harald Welte52b1f982008-12-23 20:25:15 +0000101 return ABIS_RSL_MDISC_DED_CHAN;
102
103 return ABIS_RSL_MDISC_LOC;
104}
105
106static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107 uint8_t msg_type)
Harald Welte52b1f982008-12-23 20:25:15 +0000108{
109 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
110 dh->c.msg_type = msg_type;
111 dh->ie_chan = RSL_IE_CHAN_NR;
112}
113
Neels Hofmeyr74585722016-07-23 17:38:22 +0200114/* call rsl_lchan_lookup and set the log context */
115static struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr)
Harald Welte8470bf22008-12-25 23:28:35 +0000116{
Neels Hofmeyr74585722016-07-23 17:38:22 +0200117 int rc;
118 struct gsm_lchan *lchan = rsl_lchan_lookup(trx, chan_nr, &rc);
Harald Welte8470bf22008-12-25 23:28:35 +0000119
Neels Hofmeyr74585722016-07-23 17:38:22 +0200120 if (!lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100121 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000122 return NULL;
123 }
124
Neels Hofmeyr74585722016-07-23 17:38:22 +0200125 if (rc < 0)
126 LOGP(DRSL, LOGL_ERROR, "%s mismatching chan_nr=0x%02x\n",
127 gsm_ts_and_pchan_name(lchan->ts), chan_nr);
128
Harald Weltedc5062b2010-03-26 21:28:59 +0800129 log_set_context(BSC_CTX_LCHAN, lchan);
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800130 if (lchan->conn)
131 log_set_context(BSC_CTX_SUBSCR, lchan->conn->subscr);
Harald Welte8470bf22008-12-25 23:28:35 +0000132
133 return lchan;
134}
135
Harald Welte52b1f982008-12-23 20:25:15 +0000136/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200137uint64_t str_to_imsi(const char *imsi_str)
Harald Welte52b1f982008-12-23 20:25:15 +0000138{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200139 uint64_t ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000140
141 ret = strtoull(imsi_str, NULL, 10);
142
143 return ret;
144}
145
Harald Welte8470bf22008-12-25 23:28:35 +0000146static struct msgb *rsl_msgb_alloc(void)
147{
Harald Welte966636f2009-06-26 19:39:35 +0200148 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
149 "RSL");
Harald Welte8470bf22008-12-25 23:28:35 +0000150}
151
Harald Welte362322e2009-02-15 14:36:38 +0000152#define MACBLOCK_SIZE 23
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200153static void pad_macblock(uint8_t *out, const uint8_t *in, int len)
Harald Welte362322e2009-02-15 14:36:38 +0000154{
155 memcpy(out, in, len);
156
157 if (len < MACBLOCK_SIZE)
158 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
159}
160
Harald Welte08d91a52009-08-30 15:37:11 +0900161/* Chapter 9.3.7: Encryption Information */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200162static int build_encr_info(uint8_t *out, struct gsm_lchan *lchan)
Harald Welte08d91a52009-08-30 15:37:11 +0900163{
164 *out++ = lchan->encr.alg_id & 0xff;
165 if (lchan->encr.key_len)
166 memcpy(out, lchan->encr.key, lchan->encr.key_len);
167 return lchan->encr.key_len + 1;
168}
169
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200170static void print_rsl_cause(int lvl, const uint8_t *cause_v, uint8_t cause_len)
Harald Welte8830e072009-07-28 17:58:09 +0200171{
Harald Welte7f93cea2009-02-23 00:02:59 +0000172 int i;
173
Harald Welte5b8ed432009-12-24 12:20:20 +0100174 LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ",
Harald Welte8830e072009-07-28 17:58:09 +0200175 cause_v[0], rsl_err_name(cause_v[0]));
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200176 for (i = 1; i < cause_len-1; i++)
Harald Welte5b8ed432009-12-24 12:20:20 +0100177 LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
Harald Welte7f93cea2009-02-23 00:02:59 +0000178}
179
Harald Weltee8bd9e82011-08-10 23:26:33 +0200180static void lchan_act_tmr_cb(void *data)
181{
182 struct gsm_lchan *lchan = data;
183
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +0200184 LOGP(DRSL, LOGL_ERROR,
185 "%s Timeout during activation. Marked as broken.\n",
Harald Weltee8bd9e82011-08-10 23:26:33 +0200186 gsm_lchan_name(lchan));
187
Holger Hans Peter Freyther454140e2014-12-28 12:08:28 +0100188 rsl_lchan_mark_broken(lchan, "activation timeout");
Daniel Willmann513da172011-08-11 04:44:12 +0200189 lchan_free(lchan);
Harald Weltee8bd9e82011-08-10 23:26:33 +0200190}
191
192static void lchan_deact_tmr_cb(void *data)
193{
194 struct gsm_lchan *lchan = data;
195
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +0200196 LOGP(DRSL, LOGL_ERROR,
197 "%s Timeout during deactivation! Marked as broken.\n",
Harald Weltee8bd9e82011-08-10 23:26:33 +0200198 gsm_lchan_name(lchan));
199
Holger Hans Peter Freyther454140e2014-12-28 12:08:28 +0100200 rsl_lchan_mark_broken(lchan, "de-activation timeout");
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +0200201 lchan_free(lchan);
Harald Weltee8bd9e82011-08-10 23:26:33 +0200202}
203
204
Harald Welte52b1f982008-12-23 20:25:15 +0000205/* Send a BCCH_INFO message as per Chapter 8.5.1 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200206int rsl_bcch_info(struct gsm_bts_trx *trx, uint8_t type,
207 const uint8_t *data, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000208{
209 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000210 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000211
212 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
213 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
214 dh->chan_nr = RSL_CHAN_BCCH;
215
216 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
217 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
218
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200219 msg->dst = trx->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +0000220
221 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000222}
223
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200224int rsl_sacch_filling(struct gsm_bts_trx *trx, uint8_t type,
225 const uint8_t *data, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000226{
227 struct abis_rsl_common_hdr *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000228 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000229
230 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
231 ch->msg_discr = ABIS_RSL_MDISC_TRX;
232 ch->msg_type = RSL_MT_SACCH_FILL;
233
234 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
Harald Welte702d8702008-12-26 20:25:35 +0000235 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
Harald Welte52b1f982008-12-23 20:25:15 +0000236
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200237 msg->dst = trx->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +0000238
239 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000240}
241
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200242int rsl_sacch_info_modify(struct gsm_lchan *lchan, uint8_t type,
243 const uint8_t *data, int len)
Harald Welte7a69cf02011-01-13 23:16:03 +0100244{
245 struct abis_rsl_dchan_hdr *dh;
246 struct msgb *msg = rsl_msgb_alloc();
Harald Weltef6093a42011-06-25 10:02:33 +0200247 uint8_t chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welte7a69cf02011-01-13 23:16:03 +0100248
249 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
250 init_dchan_hdr(dh, RSL_MT_SACCH_INFO_MODIFY);
251 dh->chan_nr = chan_nr;
252
253 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
254 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
255
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200256 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte7a69cf02011-01-13 23:16:03 +0100257
258 return abis_rsl_sendmsg(msg);
259}
260
Harald Weltefcd24452009-06-20 18:15:19 +0200261int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
262{
263 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200264 struct msgb *msg;
Harald Weltef6093a42011-06-25 10:02:33 +0200265 uint8_t chan_nr = gsm_lchan2chan_nr(lchan);
Harald Weltefcd24452009-06-20 18:15:19 +0200266
267 db = abs(db);
268 if (db > 30)
269 return -EINVAL;
270
Harald Welteeab33352009-06-27 03:09:08 +0200271 msg = rsl_msgb_alloc();
272
Harald Weltefcd24452009-06-20 18:15:19 +0200273 lchan->bs_power = db/2;
274 if (fpc)
275 lchan->bs_power |= 0x10;
276
277 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
278 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
279 dh->chan_nr = chan_nr;
280
281 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
282
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200283 msg->dst = lchan->ts->trx->rsl_link;
Harald Weltefcd24452009-06-20 18:15:19 +0200284
285 return abis_rsl_sendmsg(msg);
286}
287
Harald Weltefcd24452009-06-20 18:15:19 +0200288int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
289{
290 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200291 struct msgb *msg;
Harald Weltef6093a42011-06-25 10:02:33 +0200292 uint8_t chan_nr = gsm_lchan2chan_nr(lchan);
Harald Weltefcd24452009-06-20 18:15:19 +0200293 int ctl_lvl;
294
Harald Welte66b6a8d2009-08-09 14:45:18 +0200295 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Weltefcd24452009-06-20 18:15:19 +0200296 if (ctl_lvl < 0)
297 return ctl_lvl;
298
Harald Welteeab33352009-06-27 03:09:08 +0200299 msg = rsl_msgb_alloc();
300
Harald Weltefcd24452009-06-20 18:15:19 +0200301 lchan->ms_power = ctl_lvl;
302
303 if (fpc)
304 lchan->ms_power |= 0x20;
305
306 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
307 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
308 dh->chan_nr = chan_nr;
309
310 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
311
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200312 msg->dst = lchan->ts->trx->rsl_link;
Harald Weltefcd24452009-06-20 18:15:19 +0200313
314 return abis_rsl_sendmsg(msg);
315}
316
Harald Welte9943c5b2009-07-29 15:41:29 +0200317static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
318 struct gsm_lchan *lchan)
319{
Holger Hans Peter Freythere1145cf2013-03-09 17:50:10 +0100320 memset(cm, 0, sizeof(*cm));
Harald Welte9943c5b2009-07-29 15:41:29 +0200321
322 /* FIXME: what to do with data calls ? */
Maxc08ee712016-05-11 12:45:13 +0200323 cm->dtx_dtu = 0;
324 if (lchan->ts->trx->bts->dtxu != GSM48_DTX_SHALL_NOT_BE_USED)
325 cm->dtx_dtu |= RSL_CMOD_DTXu;
326 if (lchan->ts->trx->bts->dtxd)
327 cm->dtx_dtu |= RSL_CMOD_DTXd;
Harald Welte9943c5b2009-07-29 15:41:29 +0200328
329 /* set TCH Speech/Data */
330 cm->spd_ind = lchan->rsl_cmode;
331
Harald Welte1a79d362009-11-27 08:55:16 +0100332 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
333 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100334 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte1a79d362009-11-27 08:55:16 +0100335 "but tch_mode != signalling\n");
336
Harald Welte9943c5b2009-07-29 15:41:29 +0200337 switch (lchan->type) {
338 case GSM_LCHAN_SDCCH:
339 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
340 break;
341 case GSM_LCHAN_TCH_F:
342 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
343 break;
344 case GSM_LCHAN_TCH_H:
345 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
346 break;
347 case GSM_LCHAN_NONE:
348 case GSM_LCHAN_UNKNOWN:
349 default:
Neels Hofmeyrbbbcfe52016-07-18 23:47:24 +0200350 LOGP(DRSL, LOGL_ERROR,
351 "unsupported activation lchan->type %u %s\n",
352 lchan->type, gsm_lchant_name(lchan->type));
Harald Welte9943c5b2009-07-29 15:41:29 +0200353 return -EINVAL;
354 }
355
356 switch (lchan->tch_mode) {
357 case GSM48_CMODE_SIGN:
358 cm->chan_rate = 0;
359 break;
360 case GSM48_CMODE_SPEECH_V1:
361 cm->chan_rate = RSL_CMOD_SP_GSM1;
362 break;
363 case GSM48_CMODE_SPEECH_EFR:
364 cm->chan_rate = RSL_CMOD_SP_GSM2;
365 break;
366 case GSM48_CMODE_SPEECH_AMR:
367 cm->chan_rate = RSL_CMOD_SP_GSM3;
368 break;
369 case GSM48_CMODE_DATA_14k5:
Harald Welte9943c5b2009-07-29 15:41:29 +0200370 case GSM48_CMODE_DATA_12k0:
Harald Welte9943c5b2009-07-29 15:41:29 +0200371 case GSM48_CMODE_DATA_6k0:
Harald Weltee4227982012-08-24 15:33:56 +0200372 switch (lchan->csd_mode) {
373 case LCHAN_CSD_M_NT:
374 /* non-transparent CSD with RLP */
375 switch (lchan->tch_mode) {
376 case GSM48_CMODE_DATA_14k5:
377 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
378 break;
379 case GSM48_CMODE_DATA_12k0:
380 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
381 break;
382 case GSM48_CMODE_DATA_6k0:
383 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
384 break;
385 default:
Neels Hofmeyrbbbcfe52016-07-18 23:47:24 +0200386 LOGP(DRSL, LOGL_ERROR,
387 "unsupported lchan->tch_mode %u\n",
388 lchan->tch_mode);
Harald Weltee4227982012-08-24 15:33:56 +0200389 return -EINVAL;
390 }
391 break;
392 /* transparent data services below */
393 case LCHAN_CSD_M_T_1200_75:
394 cm->chan_rate = RSL_CMOD_CSD_T_1200_75;
395 break;
396 case LCHAN_CSD_M_T_600:
397 cm->chan_rate = RSL_CMOD_CSD_T_600;
398 break;
399 case LCHAN_CSD_M_T_1200:
400 cm->chan_rate = RSL_CMOD_CSD_T_1200;
401 break;
402 case LCHAN_CSD_M_T_2400:
403 cm->chan_rate = RSL_CMOD_CSD_T_2400;
404 break;
405 case LCHAN_CSD_M_T_9600:
406 cm->chan_rate = RSL_CMOD_CSD_T_9600;
407 break;
408 case LCHAN_CSD_M_T_14400:
409 cm->chan_rate = RSL_CMOD_CSD_T_14400;
410 break;
411 case LCHAN_CSD_M_T_29000:
412 cm->chan_rate = RSL_CMOD_CSD_T_29000;
413 break;
414 case LCHAN_CSD_M_T_32000:
415 cm->chan_rate = RSL_CMOD_CSD_T_32000;
416 break;
417 default:
Neels Hofmeyrbbbcfe52016-07-18 23:47:24 +0200418 LOGP(DRSL, LOGL_ERROR,
419 "unsupported lchan->csd_mode %u\n",
420 lchan->csd_mode);
Harald Weltee4227982012-08-24 15:33:56 +0200421 return -EINVAL;
422 }
Harald Welte9943c5b2009-07-29 15:41:29 +0200423 default:
Neels Hofmeyrbbbcfe52016-07-18 23:47:24 +0200424 LOGP(DRSL, LOGL_ERROR,
425 "unsupported lchan->tch_mode %u\n",
426 lchan->tch_mode);
Harald Welte9943c5b2009-07-29 15:41:29 +0200427 return -EINVAL;
428 }
429
430 return 0;
431}
432
Holger Hans Peter Freyther11cb7a92015-08-20 19:32:46 +0200433static void mr_config_for_bts(struct gsm_lchan *lchan, struct msgb *msg)
434{
435 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
436 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, lchan->mr_bts_lv[0],
437 lchan->mr_bts_lv + 1);
438}
439
Neels Hofmeyrb91e6002016-07-23 19:45:15 +0200440static enum gsm_phys_chan_config pchan_for_lchant(enum gsm_chan_t type)
441{
442 switch (type) {
443 case GSM_LCHAN_TCH_F:
444 return GSM_PCHAN_TCH_F;
445 case GSM_LCHAN_TCH_H:
446 return GSM_PCHAN_TCH_H;
447 case GSM_LCHAN_NONE:
448 case GSM_LCHAN_PDTCH:
449 /* TODO: so far lchan->type is NONE in PDCH mode. PDTCH is only
450 * used in osmo-bts. Maybe set PDTCH and drop the NONE case
451 * here. */
452 return GSM_PCHAN_PDCH;
453 default:
454 return GSM_PCHAN_UNKNOWN;
455 }
456}
457
458/*! Tx simplified channel activation message for non-standard PDCH type. */
459static int rsl_chan_activate_lchan_as_pdch(struct gsm_lchan *lchan)
460{
461 struct msgb *msg;
462 struct abis_rsl_dchan_hdr *dh;
463
464 /* This might be called after release of the second lchan of a TCH/H,
465 * but PDCH activation should always happen on the first lchan. So
466 * switch to lchan->nr == 0. */
467 lchan = lchan->ts->lchan;
468
469 rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
470
471 msg = rsl_msgb_alloc();
472 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
473 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
474 dh->chan_nr = gsm_lchan_as_pchan2chan_nr(lchan, GSM_PCHAN_PDCH);
475
476 msgb_tv_put(msg, RSL_IE_ACT_TYPE, RSL_ACT_OSMO_PDCH);
477
478 msg->dst = lchan->ts->trx->rsl_link;
479
480 return abis_rsl_sendmsg(msg);
481}
482
Harald Welte52b1f982008-12-23 20:25:15 +0000483/* Chapter 8.4.1 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200484int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
Andreas Eversberg723a7512013-10-11 12:55:35 +0200485 uint8_t ho_ref)
Harald Welte4b634542008-12-27 01:55:51 +0000486{
487 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200488 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200489 int rc;
Harald Welte93d50e62010-06-29 17:53:45 +0200490 uint8_t *len;
Andreas Eversberg723a7512013-10-11 12:55:35 +0200491 uint8_t ta;
Harald Welte4b634542008-12-27 01:55:51 +0000492
Harald Welte4b634542008-12-27 01:55:51 +0000493 struct rsl_ie_chan_mode cm;
laforge694a5cf2010-06-20 21:38:19 +0200494 struct gsm48_chan_desc cd;
Harald Welte4b634542008-12-27 01:55:51 +0000495
Neels Hofmeyrc6926d02016-07-14 02:51:13 +0200496 /* If a TCH_F/PDCH TS is in PDCH mode, deactivate PDCH first. */
Andreas Eversberg9df268e2013-10-11 13:32:30 +0200497 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH
Neels Hofmeyr2ebacce2016-06-14 14:08:35 +0200498 && (lchan->ts->flags & TS_F_PDCH_ACTIVE)) {
Andreas Eversberg9df268e2013-10-11 13:32:30 +0200499 /* store activation type and handover reference */
Neels Hofmeyr6e999b72016-07-23 21:00:51 +0200500 lchan->dyn.act_type = act_type;
501 lchan->dyn.ho_ref = ho_ref;
Andreas Eversberg9df268e2013-10-11 13:32:30 +0200502 return rsl_ipacc_pdch_activate(lchan->ts, 0);
503 }
504
Neels Hofmeyrb91e6002016-07-23 19:45:15 +0200505 /*
506 * If necessary, release PDCH on dynamic TS. Note that sending a
507 * release here is only necessary when in PDCH mode; for TCH types, an
508 * RSL RF Chan Release is initiated by the BTS when a voice call ends,
509 * so when we reach this, it will already be released. If a dyn TS is
510 * in PDCH mode, it is still active and we need to initiate a release
511 * from the BSC side here.
512 *
513 * If pchan_is != pchan_want, the PDCH has already been taken down and
514 * the switchover now needs to enable the TCH lchan.
515 *
516 * To switch a dyn TS between TCH/H and TCH/F, it is sufficient to send
517 * a chan activ with the new lchan type, because it will already be
518 * released.
519 */
520 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
521 && lchan->ts->dyn.pchan_is == GSM_PCHAN_PDCH
522 && lchan->ts->dyn.pchan_is == lchan->ts->dyn.pchan_want) {
523 enum gsm_phys_chan_config pchan_want;
524 pchan_want = pchan_for_lchant(lchan->type);
525 if (lchan->ts->dyn.pchan_is != pchan_want) {
526 lchan->dyn.act_type = act_type,
527 lchan->dyn.ho_ref = ho_ref;
528 lchan->dyn.rqd_ref = lchan->rqd_ref;
529 lchan->dyn.rqd_ta = lchan->rqd_ta;
530 lchan->rqd_ref = NULL;
531 lchan->rqd_ta = 0;
532 DEBUGP(DRSL, "%s saved rqd_ref=%p ta=%u\n",
533 gsm_lchan_name(lchan), lchan->rqd_ref,
534 lchan->rqd_ta);
535 return dyn_ts_switchover_start(lchan, pchan_want);
536 }
537 }
538
539 DEBUGP(DRSL, "%s Tx RSL Channel Activate with act_type=%s\n",
540 gsm_ts_and_pchan_name(lchan->ts),
541 rsl_act_type_name(act_type));
542
543 if (act_type == RSL_ACT_OSMO_PDCH) {
544 if (lchan->ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH) {
545 LOGP(DRSL, LOGL_ERROR,
546 "%s PDCH channel activation only allowed on %s\n",
547 gsm_ts_and_pchan_name(lchan->ts),
548 gsm_pchan_name(GSM_PCHAN_TCH_F_TCH_H_PDCH));
549 return -EINVAL;
550 }
551 return rsl_chan_activate_lchan_as_pdch(lchan);
552 }
553
554 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
555 && lchan->ts->dyn.pchan_want == GSM_PCHAN_PDCH) {
556 LOGP(DRSL, LOGL_ERROR,
557 "%s Expected PDCH activation kind\n",
558 gsm_ts_and_pchan_name(lchan->ts));
559 return -EINVAL;
560 }
561
Neels Hofmeyrcf793382016-07-23 19:49:58 +0200562 rc = channel_mode_from_lchan(&cm, lchan);
563 if (rc < 0) {
564 LOGP(DRSL, LOGL_ERROR,
565 "%s Cannot find channel mode from lchan type\n",
566 gsm_ts_and_pchan_name(lchan->ts));
567 return rc;
568 }
569
Neels Hofmeyr832afa32016-06-14 13:12:00 +0200570 rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
571
Andreas Eversberg723a7512013-10-11 12:55:35 +0200572 ta = lchan->rqd_ta;
573
574 /* BS11 requires TA shifted by 2 bits */
575 if (lchan->ts->trx->bts->type == GSM_BTS_TYPE_BS11)
576 ta <<= 2;
577
Holger Hans Peter Freythere38bd6c2010-06-30 11:56:43 +0800578 memset(&cd, 0, sizeof(cd));
laforge694a5cf2010-06-20 21:38:19 +0200579 gsm48_lchan2chan_desc(&cd, lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000580
Harald Welteeab33352009-06-27 03:09:08 +0200581 msg = rsl_msgb_alloc();
Harald Welte4b634542008-12-27 01:55:51 +0000582 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
583 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
Neels Hofmeyr7af652c2016-07-23 19:51:09 +0200584
585 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH)
586 dh->chan_nr = gsm_lchan_as_pchan2chan_nr(
587 lchan, lchan->ts->dyn.pchan_want);
588 else
589 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000590
591 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte4b634542008-12-27 01:55:51 +0000592 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200593 (uint8_t *) &cm);
Holger Hans Peter Freythere38bd6c2010-06-30 11:56:43 +0800594
595 /*
596 * The Channel Identification is needed for Phase1 phones
597 * and it contains the GSM48 Channel Description and the
598 * Mobile Allocation. The GSM 08.58 asks for the Mobile
599 * Allocation to have a length of zero. We are using the
600 * msgb_l3len to calculate the length of both messages.
601 */
laforge694a5cf2010-06-20 21:38:19 +0200602 msgb_v_put(msg, RSL_IE_CHAN_IDENT);
Harald Welte93d50e62010-06-29 17:53:45 +0200603 len = msgb_put(msg, 1);
Dieter Spaareabb6e32011-07-27 23:40:33 +0200604 msgb_tv_fixed_put(msg, GSM48_IE_CHANDESC_2, sizeof(cd), (const uint8_t *) &cd);
Holger Hans Peter Freyther0379c6d2010-06-30 12:06:20 +0800605
606 if (lchan->ts->hopping.enabled)
607 msgb_tlv_put(msg, GSM48_IE_MA_AFTER, lchan->ts->hopping.ma_len,
608 lchan->ts->hopping.ma_data);
609 else
610 msgb_tlv_put(msg, GSM48_IE_MA_AFTER, 0, NULL);
Holger Hans Peter Freythere38bd6c2010-06-30 11:56:43 +0800611
612 /* update the calculated size */
613 msg->l3h = len + 1;
614 *len = msgb_l3len(msg);
615
Harald Welte08d91a52009-08-30 15:37:11 +0900616 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200617 uint8_t encr_info[MAX_A5_KEY_LEN+2];
Harald Welte08d91a52009-08-30 15:37:11 +0900618 rc = build_encr_info(encr_info, lchan);
619 if (rc > 0)
620 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
621 }
622
Harald Welte8d77b952009-12-17 00:31:10 +0100623 switch (act_type) {
624 case RSL_ACT_INTER_ASYNC:
625 case RSL_ACT_INTER_SYNC:
626 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
627 break;
628 default:
629 break;
630 }
631
Harald Welted4c9bf32009-02-15 16:56:18 +0000632 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
633 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
Harald Welte4b634542008-12-27 01:55:51 +0000634 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
Holger Hans Peter Freyther11cb7a92015-08-20 19:32:46 +0200635 mr_config_for_bts(lchan, msg);
Holger Hans Peter Freyther93b6c652010-01-28 04:45:05 +0100636
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200637 msg->dst = lchan->ts->trx->rsl_link;
Harald Weltee79769b2009-02-07 00:48:17 +0000638
Harald Welte4b634542008-12-27 01:55:51 +0000639 return abis_rsl_sendmsg(msg);
640}
641
Harald Welte470abb72009-07-29 11:38:15 +0200642/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welteda783762009-02-18 03:29:53 +0000643int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
644{
645 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200646 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200647 int rc;
Harald Welteda783762009-02-18 03:29:53 +0000648
Harald Weltef6093a42011-06-25 10:02:33 +0200649 uint8_t chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welteda783762009-02-18 03:29:53 +0000650 struct rsl_ie_chan_mode cm;
651
Harald Welte9943c5b2009-07-29 15:41:29 +0200652 rc = channel_mode_from_lchan(&cm, lchan);
653 if (rc < 0)
654 return rc;
Harald Welteda783762009-02-18 03:29:53 +0000655
Harald Welteeab33352009-06-27 03:09:08 +0200656 msg = rsl_msgb_alloc();
Harald Welteda783762009-02-18 03:29:53 +0000657 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
658 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
659 dh->chan_nr = chan_nr;
660
661 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200662 (uint8_t *) &cm);
Harald Welte08d91a52009-08-30 15:37:11 +0900663
664 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200665 uint8_t encr_info[MAX_A5_KEY_LEN+2];
Harald Welte08d91a52009-08-30 15:37:11 +0900666 rc = build_encr_info(encr_info, lchan);
667 if (rc > 0)
668 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
669 }
670
Holger Hans Peter Freyther11cb7a92015-08-20 19:32:46 +0200671 mr_config_for_bts(lchan, msg);
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100672
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200673 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte08d91a52009-08-30 15:37:11 +0900674
675 return abis_rsl_sendmsg(msg);
676}
677
678/* Chapter 8.4.6: Send the encryption command with given L3 info */
679int rsl_encryption_cmd(struct msgb *msg)
680{
681 struct abis_rsl_dchan_hdr *dh;
682 struct gsm_lchan *lchan = msg->lchan;
Harald Weltef6093a42011-06-25 10:02:33 +0200683 uint8_t chan_nr = gsm_lchan2chan_nr(lchan);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200684 uint8_t encr_info[MAX_A5_KEY_LEN+2];
685 uint8_t l3_len = msg->len;
Harald Welte08d91a52009-08-30 15:37:11 +0900686 int rc;
687
688 /* First push the L3 IE tag and length */
689 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
690
691 /* then the link identifier (SAPI0, main sign link) */
692 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
693
694 /* then encryption information */
695 rc = build_encr_info(encr_info, lchan);
696 if (rc <= 0)
697 return rc;
698 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
699
700 /* and finally the DCHAN header */
701 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
702 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
703 dh->chan_nr = chan_nr;
Harald Welteda783762009-02-18 03:29:53 +0000704
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200705 msg->dst = lchan->ts->trx->rsl_link;
Harald Welteda783762009-02-18 03:29:53 +0000706
707 return abis_rsl_sendmsg(msg);
708}
709
Harald Welte115d1032009-08-10 11:43:22 +0200710/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteae0f2362009-07-19 18:36:49 +0200711int rsl_deact_sacch(struct gsm_lchan *lchan)
712{
713 struct abis_rsl_dchan_hdr *dh;
714 struct msgb *msg = rsl_msgb_alloc();
715
716 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
717 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
Harald Weltef6093a42011-06-25 10:02:33 +0200718 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welteae0f2362009-07-19 18:36:49 +0200719
720 msg->lchan = lchan;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200721 msg->dst = lchan->ts->trx->rsl_link;
Harald Welteae0f2362009-07-19 18:36:49 +0200722
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100723 DEBUGP(DRSL, "%s DEACTivate SACCH CMD\n", gsm_lchan_name(lchan));
Harald Welteae0f2362009-07-19 18:36:49 +0200724
725 return abis_rsl_sendmsg(msg);
726}
727
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800728static void error_timeout_cb(void *data)
729{
730 struct gsm_lchan *lchan = data;
731 if (lchan->state != LCHAN_S_REL_ERR) {
732 LOGP(DRSL, LOGL_ERROR, "%s error timeout but not in error state: %d\n",
733 gsm_lchan_name(lchan), lchan->state);
734 return;
735 }
736
737 /* go back to the none state */
Harald Weltea9e420e2012-11-13 04:26:22 +0100738 LOGP(DRSL, LOGL_INFO, "%s is back in operation.\n", gsm_lchan_name(lchan));
Holger Hans Peter Freyther44752d92010-06-08 11:53:33 +0800739 rsl_lchan_set_state(lchan, LCHAN_S_NONE);
Neels Hofmeyr82c8f752016-06-21 20:55:14 +0200740
Neels Hofmeyr9ddd8e62016-07-06 14:39:04 +0200741 /* Put PDCH channel back into PDCH mode, if GPRS is enabled */
742 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH
743 && lchan->ts->trx->bts->gprs.mode != BTS_GPRS_NONE)
Neels Hofmeyr82c8f752016-06-21 20:55:14 +0200744 rsl_ipacc_pdch_activate(lchan->ts, 1);
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800745}
746
Harald Weltefd355a32011-03-04 13:41:31 +0100747static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan);
748
Harald Welte115d1032009-08-10 11:43:22 +0200749/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +0100750static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error,
751 enum sacch_deact deact_sacch)
Harald Welte52b1f982008-12-23 20:25:15 +0000752{
753 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800754 struct msgb *msg;
Harald Weltefd355a32011-03-04 13:41:31 +0100755 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000756
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +0100757 /* Stop timers that should lead to a channel release */
758 osmo_timer_del(&lchan->T3109);
759
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800760 if (lchan->state == LCHAN_S_REL_ERR) {
761 LOGP(DRSL, LOGL_NOTICE, "%s is in error state not sending release.\n",
762 gsm_lchan_name(lchan));
763 return -1;
764 }
765
766 msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000767 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
768 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
Harald Weltef6093a42011-06-25 10:02:33 +0200769 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000770
Harald Welte8470bf22008-12-25 23:28:35 +0000771 msg->lchan = lchan;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200772 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +0000773
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800774 DEBUGP(DRSL, "%s RF Channel Release CMD due error %d\n", gsm_lchan_name(lchan), error);
775
776 if (error) {
Holger Hans Peter Freyther9d50a272011-12-28 12:11:40 +0100777 /*
778 * FIXME: GSM 04.08 gives us two options for the abnormal
779 * chanel release. This can be either like in the non-existent
780 * sub-lcuase 3.5.1 or for the main signalling link deactivate
781 * the SACCH, start timer T3109 and consider the channel as
782 * released.
783 *
784 * This code is doing the later for all raido links and not
785 * only the main link. Right now all SAPIs are released on the
786 * local end, the SACCH will be de-activated and right now the
787 * T3111 will be started. First T3109 should be started and then
788 * the T3111.
789 *
790 * TODO: Move this out of the function.
791 */
792
793 /*
794 * sacch de-activate and "local end release"
795 */
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +0100796 if (deact_sacch == SACCH_DEACTIVATE)
797 rsl_deact_sacch(lchan);
Holger Hans Peter Freyther9d50a272011-12-28 12:11:40 +0100798 rsl_release_sapis_from(lchan, 0, RSL_REL_LOCAL_END);
799
800 /*
801 * TODO: start T3109 now.
802 */
Holger Hans Peter Freyther44752d92010-06-08 11:53:33 +0800803 rsl_lchan_set_state(lchan, LCHAN_S_REL_ERR);
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +0800804 }
Harald Welte2d5b6382008-12-27 19:46:06 +0000805
Harald Weltee8bd9e82011-08-10 23:26:33 +0200806 /* Start another timer or assume the BTS sends a ACK/NACK? */
807 lchan->act_timer.cb = lchan_deact_tmr_cb;
808 lchan->act_timer.data = lchan;
809 osmo_timer_schedule(&lchan->act_timer, 4, 0);
810
Harald Weltefd355a32011-03-04 13:41:31 +0100811 rc = abis_rsl_sendmsg(msg);
812
Harald Welte115d1032009-08-10 11:43:22 +0200813 /* BTS will respond by RF CHAN REL ACK */
Harald Weltefd355a32011-03-04 13:41:31 +0100814 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000815}
816
Holger Hans Peter Freyther0e4e73a2014-04-19 17:38:33 +0200817/*
818 * Special handling for channel releases in the error case.
819 */
820static int rsl_rf_chan_release_err(struct gsm_lchan *lchan)
821{
822 if (lchan->state != LCHAN_S_ACTIVE)
823 return 0;
824 return rsl_rf_chan_release(lchan, 1, SACCH_DEACTIVATE);
825}
826
Harald Welte64bb7542011-01-14 14:16:16 +0100827static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan)
828{
Neels Hofmeyrb91e6002016-07-23 19:45:15 +0200829 int ss;
Neels Hofmeyr40074682016-07-23 20:01:49 +0200830 struct gsm_bts_trx_ts *ts = lchan->ts;
Harald Welte64bb7542011-01-14 14:16:16 +0100831
832 DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", gsm_lchan_name(lchan));
833
Holger Hans Peter Freyther93599a22012-12-06 19:09:58 +0100834 /* Stop all pending timers */
Harald Weltee8bd9e82011-08-10 23:26:33 +0200835 osmo_timer_del(&lchan->act_timer);
Holger Hans Peter Freyther93599a22012-12-06 19:09:58 +0100836 osmo_timer_del(&lchan->T3111);
Harald Weltee8bd9e82011-08-10 23:26:33 +0200837
Holger Hans Peter Freyther6f6cbf72015-04-04 19:35:22 +0200838 /*
839 * The BTS didn't respond within the timeout to our channel
840 * release request and we have marked the channel as broken.
841 * Now we do receive an ACK and let's be conservative. If it
842 * is a sysmoBTS we know that only one RF Channel Release ACK
843 * will be sent. So let's "repair" the channel.
844 */
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +0200845 if (lchan->state == LCHAN_S_BROKEN) {
Neels Hofmeyr40074682016-07-23 20:01:49 +0200846 int do_free = is_sysmobts_v2(ts->trx->bts);
Holger Hans Peter Freyther6f6cbf72015-04-04 19:35:22 +0200847 LOGP(DRSL, LOGL_NOTICE,
848 "%s CHAN REL ACK for broken channel. %s.\n",
849 gsm_lchan_name(lchan),
850 do_free ? "Releasing it" : "Keeping it broken");
851 if (do_free)
852 do_lchan_free(lchan);
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +0200853 return 0;
854 }
855
Harald Welte64bb7542011-01-14 14:16:16 +0100856 if (lchan->state != LCHAN_S_REL_REQ && lchan->state != LCHAN_S_REL_ERR)
857 LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
858 gsm_lchan_name(lchan),
859 gsm_lchans_name(lchan->state));
Andreas Eversberg9df268e2013-10-11 13:32:30 +0200860
Neels Hofmeyr832afa32016-06-14 13:12:00 +0200861 do_lchan_free(lchan);
862
Neels Hofmeyr3f221222016-06-23 22:44:20 +0200863 /*
Neels Hofmeyrb91e6002016-07-23 19:45:15 +0200864 * Check Osmocom RSL CHAN ACT style dynamic TCH/F_TCH/H_PDCH TS for pending
865 * transitions in these cases:
866 *
867 * a) after PDCH was released due to switchover request, activate TCH.
868 * BSC initiated this switchover, so dyn.pchan_is != pchan_want and
869 * lchan->type has been set to the desired GSM_LCHAN_*.
870 *
871 * b) Voice call ended and a TCH is released. If the TS is now unused,
872 * switch to PDCH. Here still dyn.pchan_is == dyn.pchan_want because
873 * we're only just notified and may decide to switch to PDCH now.
874 */
875 if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
876 DEBUGP(DRSL, "%s Rx RSL Channel Release ack for lchan %u\n",
877 gsm_ts_and_pchan_name(ts), lchan->nr);
878
879 /* (a) */
880 if (ts->dyn.pchan_is != ts->dyn.pchan_want)
881 return dyn_ts_switchover_continue(lchan);
882
883 /* (b) */
884 if (ts->dyn.pchan_is != GSM_PCHAN_PDCH
885 && ts->trx->bts->gprs.mode != BTS_GPRS_NONE) {
886 for (ss = 0; ss < ts_subslots(ts); ss++) {
887 struct gsm_lchan *lc = &ts->lchan[ss];
888 if (lc->state != LCHAN_S_NONE) {
889 DEBUGP(DRSL, "%s lchan %u still in use\n",
890 gsm_ts_and_pchan_name(ts),
891 lc->nr);
892 /* An lchan is still used. */
893 return 0;
894 }
895 }
896 /* All channels are released, go to PDCH mode. */
897 DEBUGP(DRSL, "%s back to PDCH\n",
898 gsm_ts_and_pchan_name(ts));
899 return dyn_ts_switchover_start(lchan, GSM_PCHAN_PDCH);
900 }
901 }
902
903 /*
Neels Hofmeyr3f221222016-06-23 22:44:20 +0200904 * Put a dynamic TCH/F_PDCH channel back to PDCH mode iff it was
905 * released successfully. If in error, the PDCH ACT will follow after
906 * T3111 in error_timeout_cb().
907 *
908 * Any state other than LCHAN_S_REL_ERR became LCHAN_S_NONE after above
909 * do_lchan_free(). Assert this, because that's what ensures a PDCH ACT
Neels Hofmeyrc6926d02016-07-14 02:51:13 +0200910 * on a TCH/F_PDCH TS in all cases.
Neels Hofmeyr9ddd8e62016-07-06 14:39:04 +0200911 *
912 * If GPRS is disabled, always skip the PDCH ACT.
Neels Hofmeyr3f221222016-06-23 22:44:20 +0200913 */
914 OSMO_ASSERT(lchan->state == LCHAN_S_NONE
915 || lchan->state == LCHAN_S_REL_ERR);
Neels Hofmeyr40074682016-07-23 20:01:49 +0200916 if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE)
Neels Hofmeyr9ddd8e62016-07-06 14:39:04 +0200917 return 0;
Neels Hofmeyr40074682016-07-23 20:01:49 +0200918 if (ts->pchan == GSM_PCHAN_TCH_F_PDCH
Neels Hofmeyr82c8f752016-06-21 20:55:14 +0200919 && lchan->state == LCHAN_S_NONE)
Neels Hofmeyr40074682016-07-23 20:01:49 +0200920 return rsl_ipacc_pdch_activate(ts, 1);
Harald Welte64bb7542011-01-14 14:16:16 +0100921 return 0;
922}
923
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200924int rsl_paging_cmd(struct gsm_bts *bts, uint8_t paging_group, uint8_t len,
925 uint8_t *ms_ident, uint8_t chan_needed)
Harald Welte52b1f982008-12-23 20:25:15 +0000926{
927 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000928 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000929
930 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
931 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
932 dh->chan_nr = RSL_CHAN_PCH_AGCH;
933
934 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
Harald Welte255539c2008-12-28 02:26:27 +0000935 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
Harald Welte52b1f982008-12-23 20:25:15 +0000936 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
937
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200938 msg->dst = bts->c0->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +0000939
940 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000941}
942
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200943int imsi_str2bcd(uint8_t *bcd_out, const char *str_in)
Harald Welte52b1f982008-12-23 20:25:15 +0000944{
945 int i, len = strlen(str_in);
946
947 for (i = 0; i < len; i++) {
948 int num = str_in[i] - 0x30;
949 if (num < 0 || num > 9)
950 return -1;
951 if (i % 2 == 0)
952 bcd_out[i/2] = num;
953 else
954 bcd_out[i/2] |= (num << 4);
955 }
956
957 return 0;
958}
959
Harald Welte702d8702008-12-26 20:25:35 +0000960/* Chapter 8.5.6 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200961int rsl_imm_assign_cmd(struct gsm_bts *bts, uint8_t len, uint8_t *val)
Harald Welte52b1f982008-12-23 20:25:15 +0000962{
Harald Welte8470bf22008-12-25 23:28:35 +0000963 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000964 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200965 uint8_t buf[MACBLOCK_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000966
967 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
968 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
969 dh->chan_nr = RSL_CHAN_PCH_AGCH;
970
Harald Welte362322e2009-02-15 14:36:38 +0000971 switch (bts->type) {
972 case GSM_BTS_TYPE_BS11:
973 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
974 break;
975 default:
976 /* If phase 2, construct a FULL_IMM_ASS_INFO */
977 pad_macblock(buf, val, len);
978 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
979 break;
980 }
Harald Welte52b1f982008-12-23 20:25:15 +0000981
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200982 msg->dst = bts->c0->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +0000983
984 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000985}
986
Harald Welte67fa91b2009-08-10 09:51:40 +0200987/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte31c48932009-08-10 10:07:33 +0200988int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte67fa91b2009-08-10 09:51:40 +0200989{
990 struct msgb *msg = rsl_msgb_alloc();
991 struct abis_rsl_dchan_hdr *dh;
992
993 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
994 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte3c456d02009-08-10 11:26:14 +0200995 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Weltef6093a42011-06-25 10:02:33 +0200996 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200997 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(uint8_t *)mrpci);
Harald Welte67fa91b2009-08-10 09:51:40 +0200998
Harald Welte5b8ed432009-12-24 12:20:20 +0100999 DEBUGP(DRSL, "%s TX Siemens MRPCI 0x%02x\n",
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001000 gsm_lchan_name(lchan), *(uint8_t *)mrpci);
Harald Welte3c456d02009-08-10 11:26:14 +02001001
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001002 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte3c456d02009-08-10 11:26:14 +02001003
Harald Welte67fa91b2009-08-10 09:51:40 +02001004 return abis_rsl_sendmsg(msg);
1005}
1006
1007
Harald Welte8470bf22008-12-25 23:28:35 +00001008/* Send "DATA REQUEST" message with given L3 Info payload */
Harald Welte52b1f982008-12-23 20:25:15 +00001009/* Chapter 8.3.1 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001010int rsl_data_request(struct msgb *msg, uint8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00001011{
Harald Welte8470bf22008-12-25 23:28:35 +00001012 if (msg->lchan == NULL) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001013 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte8470bf22008-12-25 23:28:35 +00001014 return -EINVAL;
1015 }
Harald Welte52b1f982008-12-23 20:25:15 +00001016
Harald Weltef6093a42011-06-25 10:02:33 +02001017 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, gsm_lchan2chan_nr(msg->lchan),
Harald Welte3c9c5f92010-03-04 10:33:10 +01001018 link_id, 1);
Harald Welte52b1f982008-12-23 20:25:15 +00001019
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001020 msg->dst = msg->lchan->ts->trx->rsl_link;
Harald Welte8470bf22008-12-25 23:28:35 +00001021
1022 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001023}
1024
Harald Welteedcc5272009-08-09 13:47:35 +02001025/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
1026/* Chapter 8.3.1 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001027int rsl_establish_request(struct gsm_lchan *lchan, uint8_t link_id)
Harald Welteedcc5272009-08-09 13:47:35 +02001028{
Harald Welte3c9c5f92010-03-04 10:33:10 +01001029 struct msgb *msg;
Harald Welteedcc5272009-08-09 13:47:35 +02001030
Harald Weltef6093a42011-06-25 10:02:33 +02001031 msg = rsl_rll_simple(RSL_MT_EST_REQ, gsm_lchan2chan_nr(lchan),
Harald Welte3c9c5f92010-03-04 10:33:10 +01001032 link_id, 0);
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001033 msg->dst = lchan->ts->trx->rsl_link;
Harald Welteedcc5272009-08-09 13:47:35 +02001034
Harald Weltefda74ee2012-04-26 19:42:19 +02001035 DEBUGP(DRLL, "%s RSL RLL ESTABLISH REQ (link_id=0x%02x)\n",
1036 gsm_lchan_name(lchan), link_id);
1037
Harald Welteedcc5272009-08-09 13:47:35 +02001038 return abis_rsl_sendmsg(msg);
1039}
1040
Andreas Eversberg7d8fa342013-12-05 13:25:06 +01001041static void rsl_handle_release(struct gsm_lchan *lchan);
1042
1043/* Special work handler to handle missing RSL_MT_REL_CONF message from
1044 * Nokia InSite BTS */
1045static void lchan_rel_work_cb(void *data)
1046{
1047 struct gsm_lchan *lchan = data;
1048 int sapi;
1049
1050 for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
1051 if (lchan->sapis[sapi] == LCHAN_SAPI_REL)
1052 lchan->sapis[sapi] = LCHAN_SAPI_UNUSED;
1053 }
1054 rsl_handle_release(lchan);
1055}
1056
Harald Welted2dc1de2009-08-08 13:15:07 +02001057/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
1058 This is what higher layers should call. The BTS then responds with
1059 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
1060 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
1061 lchan_free() */
Holger Hans Peter Freyther5ca825e2012-12-06 12:01:38 +01001062int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id,
1063 enum rsl_rel_mode release_mode)
Harald Welted2dc1de2009-08-08 13:15:07 +02001064{
Harald Welted2dc1de2009-08-08 13:15:07 +02001065
Harald Welte3c9c5f92010-03-04 10:33:10 +01001066 struct msgb *msg;
1067
Harald Weltef6093a42011-06-25 10:02:33 +02001068 msg = rsl_rll_simple(RSL_MT_REL_REQ, gsm_lchan2chan_nr(lchan),
Harald Welte3c9c5f92010-03-04 10:33:10 +01001069 link_id, 0);
Holger Hans Peter Freyther4f5848d2010-06-08 11:57:45 +08001070 /* 0 is normal release, 1 is local end */
Holger Hans Peter Freyther5ca825e2012-12-06 12:01:38 +01001071 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, release_mode);
Harald Welted2dc1de2009-08-08 13:15:07 +02001072
Harald Welte8e93b792009-12-29 10:44:17 +01001073 /* FIXME: start some timer in case we don't receive a REL ACK ? */
1074
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001075 msg->dst = lchan->ts->trx->rsl_link;
Harald Welted2dc1de2009-08-08 13:15:07 +02001076
Harald Weltefda74ee2012-04-26 19:42:19 +02001077 DEBUGP(DRLL, "%s RSL RLL RELEASE REQ (link_id=0x%02x, reason=%u)\n",
Holger Hans Peter Freyther5ca825e2012-12-06 12:01:38 +01001078 gsm_lchan_name(lchan), link_id, release_mode);
Harald Weltefda74ee2012-04-26 19:42:19 +02001079
Andreas Eversberg7d8fa342013-12-05 13:25:06 +01001080 abis_rsl_sendmsg(msg);
1081
1082 /* Do not wait for Nokia BTS to send the confirm. */
1083 if (is_nokia_bts(lchan->ts->trx->bts)
1084 && lchan->ts->trx->bts->nokia.no_loc_rel_cnf
1085 && release_mode == RSL_REL_LOCAL_END) {
1086 DEBUGP(DRLL, "Scheduling release, becasuse Nokia InSite BTS does not send a RELease CONFirm.\n");
1087 lchan->sapis[link_id & 0x7] = LCHAN_SAPI_REL;
1088 lchan->rel_work.cb = lchan_rel_work_cb;
1089 lchan->rel_work.data = lchan;
1090 osmo_timer_schedule(&lchan->rel_work, 0, 0);
1091 }
1092
1093 return 0;
Harald Welted2dc1de2009-08-08 13:15:07 +02001094}
1095
Holger Hans Peter Freyther454140e2014-12-28 12:08:28 +01001096int rsl_lchan_mark_broken(struct gsm_lchan *lchan, const char *reason)
1097{
1098 lchan->state = LCHAN_S_BROKEN;
1099 lchan->broken_reason = reason;
1100 return 0;
1101}
1102
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +02001103int rsl_lchan_set_state(struct gsm_lchan *lchan, int state)
1104{
Neels Hofmeyrb0cc6422016-06-21 21:34:46 +02001105 DEBUGP(DRSL, "%s state %s -> %s\n",
1106 gsm_lchan_name(lchan), gsm_lchans_name(lchan->state),
1107 gsm_lchans_name(state));
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +02001108 lchan->state = state;
1109 return 0;
1110}
1111
Harald Welte702d8702008-12-26 20:25:35 +00001112/* Chapter 8.4.2: Channel Activate Acknowledge */
1113static int rsl_rx_chan_act_ack(struct msgb *msg)
1114{
1115 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001116 struct gsm_lchan *lchan = msg->lchan;
Harald Welte702d8702008-12-26 20:25:35 +00001117
1118 /* BTS has confirmed channel activation, we now need
1119 * to assign the activated channel to the MS */
Harald Welte4b634542008-12-27 01:55:51 +00001120 if (rslh->ie_chan != RSL_IE_CHAN_NR)
1121 return -EINVAL;
Harald Welted011e8b2009-11-29 22:45:52 +01001122
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001123 osmo_timer_del(&lchan->act_timer);
Harald Weltee8bd9e82011-08-10 23:26:33 +02001124
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001125 if (lchan->state == LCHAN_S_BROKEN) {
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +02001126 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK for broken channel.\n",
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001127 gsm_lchan_name(lchan));
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +02001128 return 0;
1129 }
1130
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001131 if (lchan->state != LCHAN_S_ACT_REQ)
Harald Welte1887f9d2009-12-29 10:52:38 +01001132 LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001133 gsm_lchan_name(lchan),
1134 gsm_lchans_name(lchan->state));
1135 rsl_lchan_set_state(lchan, LCHAN_S_ACTIVE);
Harald Welteb8bfc562009-12-21 13:27:11 +01001136
Neels Hofmeyrb91e6002016-07-23 19:45:15 +02001137 if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH)
1138 dyn_ts_switchover_complete(lchan);
1139
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001140 if (lchan->rqd_ref) {
1141 rsl_send_imm_assignment(lchan);
1142 talloc_free(lchan->rqd_ref);
1143 lchan->rqd_ref = NULL;
1144 lchan->rqd_ta = 0;
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001145 }
1146
Neels Hofmeyrf29dd5f2016-07-14 16:16:33 +02001147 send_lchan_signal(S_LCHAN_ACTIVATE_ACK, lchan, NULL);
Harald Welted011e8b2009-11-29 22:45:52 +01001148
Harald Welte4b634542008-12-27 01:55:51 +00001149 return 0;
1150}
Harald Welte702d8702008-12-26 20:25:35 +00001151
Harald Welte4b634542008-12-27 01:55:51 +00001152/* Chapter 8.4.3: Channel Activate NACK */
1153static int rsl_rx_chan_act_nack(struct msgb *msg)
1154{
Harald Welte6dab0552009-05-01 17:21:37 +00001155 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1156 struct tlv_parsed tp;
Harald Welte4b634542008-12-27 01:55:51 +00001157
Harald Weltee8bd9e82011-08-10 23:26:33 +02001158 osmo_timer_del(&msg->lchan->act_timer);
1159
Holger Hans Peter Freyther21776242013-05-01 18:44:04 +02001160 if (msg->lchan->state == LCHAN_S_BROKEN) {
1161 LOGP(DRSL, LOGL_ERROR,
1162 "%s CHANNEL ACTIVATE NACK for broken channel.\n",
1163 gsm_lchan_name(msg->lchan));
1164 return -1;
1165 }
1166
Daniel Willmann6fc6a122011-08-11 04:54:23 +02001167 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK ",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001168 gsm_lchan_name(msg->lchan));
Harald Welte (local)91b603d2009-12-27 11:48:11 +01001169
Harald Welte6dab0552009-05-01 17:21:37 +00001170 /* BTS has rejected channel activation ?!? */
1171 if (dh->ie_chan != RSL_IE_CHAN_NR)
Harald Welte4b634542008-12-27 01:55:51 +00001172 return -EINVAL;
Harald Welte6dab0552009-05-01 17:21:37 +00001173
1174 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte (local)3e460312009-12-27 18:12:29 +01001175 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001176 const uint8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
Harald Welte (local)3e460312009-12-27 18:12:29 +01001177 print_rsl_cause(LOGL_ERROR, cause,
Harald Welte8830e072009-07-28 17:58:09 +02001178 TLVP_LEN(&tp, RSL_IE_CAUSE));
Holger Hans Peter Freyther638da512012-12-06 19:25:06 +01001179 msg->lchan->error_cause = *cause;
Holger Hans Peter Freyther454140e2014-12-28 12:08:28 +01001180 if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC) {
1181 rsl_lchan_mark_broken(msg->lchan, "NACK on activation");
1182 } else
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +01001183 rsl_rf_chan_release(msg->lchan, 1, SACCH_DEACTIVATE);
Daniel Willmann7ddc3182011-08-11 04:47:11 +02001184
Holger Hans Peter Freyther454140e2014-12-28 12:08:28 +01001185 } else {
1186 rsl_lchan_mark_broken(msg->lchan, "NACK on activation no IE");
1187 }
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001188
Harald Welte (local)91b603d2009-12-27 11:48:11 +01001189 LOGPC(DRSL, LOGL_ERROR, "\n");
1190
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001191 send_lchan_signal(S_LCHAN_ACTIVATE_NACK, msg->lchan, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001192 return 0;
Harald Welte702d8702008-12-26 20:25:35 +00001193}
1194
Harald Welte7f93cea2009-02-23 00:02:59 +00001195/* Chapter 8.4.4: Connection Failure Indication */
1196static int rsl_rx_conn_fail(struct msgb *msg)
1197{
1198 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1199 struct tlv_parsed tp;
1200
Holger Hans Peter Freyther1e93b792014-04-19 16:45:36 +02001201 LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL: RELEASING state %s ",
1202 gsm_lchan_name(msg->lchan),
1203 gsm_lchans_name(msg->lchan->state));
1204
Harald Welte7f93cea2009-02-23 00:02:59 +00001205 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1206
Harald Welte8830e072009-07-28 17:58:09 +02001207 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +01001208 print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +02001209 TLVP_LEN(&tp, RSL_IE_CAUSE));
1210
Harald Welte (local)fc057502009-12-26 22:33:09 +01001211 LOGPC(DRSL, LOGL_NOTICE, "\n");
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001212 osmo_counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rf_fail);
Holger Hans Peter Freyther0e4e73a2014-04-19 17:38:33 +02001213 return rsl_rf_chan_release_err(msg->lchan);
Harald Welte7f93cea2009-02-23 00:02:59 +00001214}
1215
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001216static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
1217 const char *prefix)
1218{
Harald Welte6739dfb2009-12-16 16:52:07 +01001219 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
1220 prefix, rxlev2dbm(mru->full.rx_lev),
1221 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001222 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
1223 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
1224}
1225
Harald Welte0c1bd612012-07-02 17:12:08 +02001226static void print_meas_rep(struct gsm_lchan *lchan, struct gsm_meas_rep *mr)
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001227{
Harald Welte6739dfb2009-12-16 16:52:07 +01001228 int i;
Harald Welte0c1bd612012-07-02 17:12:08 +02001229 char *name = "";
Harald Welte6739dfb2009-12-16 16:52:07 +01001230
Harald Welte7184bd02015-12-28 14:04:36 +01001231 if (lchan && lchan->conn)
Harald Welte0c1bd612012-07-02 17:12:08 +02001232 name = subscr_name(lchan->conn->subscr);
1233
1234 DEBUGP(DMEAS, "[%s] MEASUREMENT RESULT NR=%d ", name, mr->nr);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001235
1236 if (mr->flags & MEAS_REP_F_DL_DTX)
1237 DEBUGPC(DMEAS, "DTXd ");
1238
1239 print_meas_rep_uni(&mr->ul, "ul");
1240 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
1241 if (mr->flags & MEAS_REP_F_MS_TO)
1242 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
1243
1244 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte6739dfb2009-12-16 16:52:07 +01001245 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001246 DEBUGPC(DMEAS, "L1_FPC=%u ",
1247 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
1248 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
1249 }
1250
1251 if (mr->flags & MEAS_REP_F_UL_DTX)
1252 DEBUGPC(DMEAS, "DTXu ");
1253 if (mr->flags & MEAS_REP_F_BA1)
1254 DEBUGPC(DMEAS, "BA1 ");
1255 if (!(mr->flags & MEAS_REP_F_DL_VALID))
1256 DEBUGPC(DMEAS, "NOT VALID ");
1257 else
1258 print_meas_rep_uni(&mr->dl, "dl");
1259
1260 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte479015b2009-12-19 18:33:05 +01001261 if (mr->num_cell == 7)
1262 return;
Harald Welte6739dfb2009-12-16 16:52:07 +01001263 for (i = 0; i < mr->num_cell; i++) {
1264 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
Harald Welte303e5e02009-12-25 23:02:22 +01001265 DEBUGP(DMEAS, "IDX=%u ARFCN=%u BSIC=%u => %d dBm\n",
1266 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
Harald Welte6739dfb2009-12-16 16:52:07 +01001267 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001268}
1269
Harald Welte440fed02009-05-01 18:43:47 +00001270static int rsl_rx_meas_res(struct msgb *msg)
1271{
1272 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1273 struct tlv_parsed tp;
Harald Welted12b0fd2009-12-15 21:36:05 +01001274 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001275 uint8_t len;
1276 const uint8_t *val;
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001277 int rc;
Harald Welte440fed02009-05-01 18:43:47 +00001278
Harald Welteb8bfc562009-12-21 13:27:11 +01001279 /* check if this channel is actually active */
1280 /* FIXME: maybe this check should be way more generic/centralized */
Harald Welte8e93b792009-12-29 10:44:17 +01001281 if (msg->lchan->state != LCHAN_S_ACTIVE) {
Holger Hans Peter Freytherc44db4a2010-07-29 14:50:57 +08001282 LOGP(DRSL, LOGL_DEBUG, "%s: MEAS RES for inactive channel\n",
Harald Welte8e93b792009-12-29 10:44:17 +01001283 gsm_lchan_name(msg->lchan));
Harald Welteb8bfc562009-12-21 13:27:11 +01001284 return 0;
Harald Welte8e93b792009-12-29 10:44:17 +01001285 }
Harald Welteb8bfc562009-12-21 13:27:11 +01001286
Harald Welted12b0fd2009-12-15 21:36:05 +01001287 memset(mr, 0, sizeof(*mr));
Harald Welte33e65972009-12-16 23:29:34 +01001288 mr->lchan = msg->lchan;
Harald Weltedbb1d882009-11-30 19:16:47 +01001289
Harald Welte440fed02009-05-01 18:43:47 +00001290 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1291
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001292 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
1293 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
1294 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
1295 return -EIO;
1296
1297 /* Mandatory Parts */
Harald Welted12b0fd2009-12-15 21:36:05 +01001298 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001299
1300 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
1301 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
1302 if (len >= 3) {
1303 if (val[0] & 0x40)
Harald Welted12b0fd2009-12-15 21:36:05 +01001304 mr->flags |= MEAS_REP_F_DL_DTX;
1305 mr->ul.full.rx_lev = val[0] & 0x3f;
1306 mr->ul.sub.rx_lev = val[1] & 0x3f;
1307 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
1308 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte440fed02009-05-01 18:43:47 +00001309 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001310
Harald Welted12b0fd2009-12-15 21:36:05 +01001311 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001312
1313 /* Optional Parts */
Harald Welte440fed02009-05-01 18:43:47 +00001314 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Welted12b0fd2009-12-15 21:36:05 +01001315 mr->ms_timing_offset =
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001316 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
1317
Harald Weltefe9af262009-06-20 18:44:35 +02001318 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001319 struct e1inp_sign_link *sign_link = msg->dst;
1320
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001321 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +01001322 mr->flags |= MEAS_REP_F_MS_L1;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001323 mr->ms_l1.pwr = ms_pwr_dbm(sign_link->trx->bts->band, val[0] >> 3);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001324 if (val[0] & 0x04)
Harald Welted12b0fd2009-12-15 21:36:05 +01001325 mr->flags |= MEAS_REP_F_FPC;
1326 mr->ms_l1.ta = val[1];
Andreas Eversberg3365cd12011-12-24 11:49:05 +01001327 /* BS11 and Nokia reports TA shifted by 2 bits */
1328 if (msg->lchan->ts->trx->bts->type == GSM_BTS_TYPE_BS11
1329 || msg->lchan->ts->trx->bts->type == GSM_BTS_TYPE_NOKIA_SITE)
Andreas Eversberg2957de92011-12-16 17:45:37 +01001330 mr->ms_l1.ta >>= 2;
Harald Weltefe9af262009-06-20 18:44:35 +02001331 }
Harald Weltef7c43522009-06-09 20:24:21 +00001332 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001333 msg->l3h = (uint8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +01001334 rc = gsm48_parse_meas_rep(mr, msg);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001335 if (rc < 0)
1336 return rc;
1337 }
1338
Harald Welte0c1bd612012-07-02 17:12:08 +02001339 print_meas_rep(msg->lchan, mr);
Harald Welte60d68f12009-06-05 20:07:43 +00001340
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001341 send_lchan_signal(S_LCHAN_MEAS_REP, msg->lchan, mr);
Harald Weltedbb1d882009-11-30 19:16:47 +01001342
Harald Welte75d34a82009-05-23 06:11:13 +00001343 return 0;
Harald Welte440fed02009-05-01 18:43:47 +00001344}
1345
Harald Welted011e8b2009-11-29 22:45:52 +01001346/* Chapter 8.4.7 */
1347static int rsl_rx_hando_det(struct msgb *msg)
1348{
1349 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1350 struct tlv_parsed tp;
1351
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001352 DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
Harald Welted011e8b2009-11-29 22:45:52 +01001353
1354 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1355
1356 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
1357 DEBUGPC(DRSL, "access delay = %u\n",
1358 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
1359 else
1360 DEBUGPC(DRSL, "\n");
1361
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001362 send_lchan_signal(S_LCHAN_HANDOVER_DETECT, msg->lchan, NULL);
Harald Welted011e8b2009-11-29 22:45:52 +01001363
1364 return 0;
1365}
1366
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001367static bool lchan_may_change_pdch(struct gsm_lchan *lchan, bool pdch_act)
1368{
1369 struct gsm_bts_trx_ts *ts;
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001370
1371 OSMO_ASSERT(lchan);
1372
1373 ts = lchan->ts;
1374 OSMO_ASSERT(ts);
1375 OSMO_ASSERT(ts->trx);
1376 OSMO_ASSERT(ts->trx->bts);
1377
1378 if (lchan->ts->pchan != GSM_PCHAN_TCH_F_PDCH) {
Neels Hofmeyrdd49bee2016-06-21 20:53:27 +02001379 LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK"
1380 " for channel that is no TCH/F_PDCH\n",
1381 gsm_lchan_name(lchan),
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001382 gsm_pchan_name(ts->pchan),
1383 pdch_act? "ACT" : "DEACT");
1384 return false;
1385 }
1386
Neels Hofmeyr832afa32016-06-14 13:12:00 +02001387 if (lchan->state != LCHAN_S_NONE) {
Neels Hofmeyrdd49bee2016-06-21 20:53:27 +02001388 LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK"
1389 " in unexpected state: %s\n",
1390 gsm_lchan_name(lchan),
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001391 gsm_pchan_name(ts->pchan),
1392 pdch_act? "ACT" : "DEACT",
1393 gsm_lchans_name(lchan->state));
1394 return false;
1395 }
1396 return true;
1397}
1398
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001399static int rsl_rx_pdch_act_ack(struct msgb *msg)
1400{
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001401 if (!lchan_may_change_pdch(msg->lchan, true))
1402 return -EINVAL;
1403
Neels Hofmeyr2ebacce2016-06-14 14:08:35 +02001404 msg->lchan->ts->flags |= TS_F_PDCH_ACTIVE;
Neels Hofmeyr32019882016-06-15 15:32:29 +02001405 msg->lchan->ts->flags &= ~TS_F_PDCH_ACT_PENDING;
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001406
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001407 return 0;
1408}
1409
1410static int rsl_rx_pdch_deact_ack(struct msgb *msg)
1411{
Neels Hofmeyr285df2e2016-06-13 12:28:21 +02001412 if (!lchan_may_change_pdch(msg->lchan, false))
1413 return -EINVAL;
1414
Neels Hofmeyr2ebacce2016-06-14 14:08:35 +02001415 msg->lchan->ts->flags &= ~TS_F_PDCH_ACTIVE;
Neels Hofmeyr32019882016-06-15 15:32:29 +02001416 msg->lchan->ts->flags &= ~TS_F_PDCH_DEACT_PENDING;
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001417
Neels Hofmeyr6e999b72016-07-23 21:00:51 +02001418 rsl_chan_activate_lchan(msg->lchan, msg->lchan->dyn.act_type,
1419 msg->lchan->dyn.ho_ref);
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001420
1421 return 0;
1422}
1423
Harald Welte52b1f982008-12-23 20:25:15 +00001424static int abis_rsl_rx_dchan(struct msgb *msg)
1425{
Harald Welte8470bf22008-12-25 23:28:35 +00001426 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1427 int rc = 0;
Harald Weltef325eb42009-02-19 17:07:39 +00001428 char *ts_name;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001429 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte52b1f982008-12-23 20:25:15 +00001430
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001431 msg->lchan = lchan_lookup(sign_link->trx, rslh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001432 ts_name = gsm_lchan_name(msg->lchan);
Harald Weltef325eb42009-02-19 17:07:39 +00001433
Harald Welte8470bf22008-12-25 23:28:35 +00001434 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001435 case RSL_MT_CHAN_ACTIV_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001436 DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
Harald Welte4b634542008-12-27 01:55:51 +00001437 rc = rsl_rx_chan_act_ack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001438 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001439 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte4b634542008-12-27 01:55:51 +00001440 rc = rsl_rx_chan_act_nack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001441 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001442 case RSL_MT_CONN_FAIL:
Harald Welte7f93cea2009-02-23 00:02:59 +00001443 rc = rsl_rx_conn_fail(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001444 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001445 case RSL_MT_MEAS_RES:
Harald Welte440fed02009-05-01 18:43:47 +00001446 rc = rsl_rx_meas_res(msg);
Harald Welte2d5b6382008-12-27 19:46:06 +00001447 break;
Harald Welted011e8b2009-11-29 22:45:52 +01001448 case RSL_MT_HANDO_DET:
1449 rc = rsl_rx_hando_det(msg);
1450 break;
Harald Welte2d5b6382008-12-27 19:46:06 +00001451 case RSL_MT_RF_CHAN_REL_ACK:
Harald Welte64bb7542011-01-14 14:16:16 +01001452 rc = rsl_rx_rf_chan_rel_ack(msg->lchan);
Harald Welte8470bf22008-12-25 23:28:35 +00001453 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001454 case RSL_MT_MODE_MODIFY_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001455 DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
Harald Welteda783762009-02-18 03:29:53 +00001456 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001457 case RSL_MT_MODE_MODIFY_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001458 LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
Harald Welteda783762009-02-18 03:29:53 +00001459 break;
Harald Welte9c880c92009-10-24 10:29:22 +02001460 case RSL_MT_IPAC_PDCH_ACT_ACK:
Neels Hofmeyr9f5d2312016-05-31 17:51:41 +02001461 DEBUGP(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001462 rc = rsl_rx_pdch_act_ack(msg);
Harald Welte9c880c92009-10-24 10:29:22 +02001463 break;
1464 case RSL_MT_IPAC_PDCH_ACT_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001465 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001466 break;
1467 case RSL_MT_IPAC_PDCH_DEACT_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001468 DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
Andreas Eversberg9df268e2013-10-11 13:32:30 +02001469 rc = rsl_rx_pdch_deact_ack(msg);
Harald Welte9c880c92009-10-24 10:29:22 +02001470 break;
1471 case RSL_MT_IPAC_PDCH_DEACT_NACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01001472 LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
Harald Welte9c880c92009-10-24 10:29:22 +02001473 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001474 case RSL_MT_PHY_CONTEXT_CONF:
1475 case RSL_MT_PREPROC_MEAS_RES:
Harald Welte52b1f982008-12-23 20:25:15 +00001476 case RSL_MT_TALKER_DET:
1477 case RSL_MT_LISTENER_DET:
1478 case RSL_MT_REMOTE_CODEC_CONF_REP:
1479 case RSL_MT_MR_CODEC_MOD_ACK:
1480 case RSL_MT_MR_CODEC_MOD_NACK:
1481 case RSL_MT_MR_CODEC_MOD_PER:
Harald Welte5b8ed432009-12-24 12:20:20 +01001482 LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
1483 "msg 0x%02x\n", ts_name, rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001484 break;
1485 default:
Harald Welte5b8ed432009-12-24 12:20:20 +01001486 LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
1487 ts_name, rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001488 return -EINVAL;
1489 }
Harald Weltef325eb42009-02-19 17:07:39 +00001490
Harald Welte8470bf22008-12-25 23:28:35 +00001491 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001492}
1493
Harald Welte702d8702008-12-26 20:25:35 +00001494static int rsl_rx_error_rep(struct msgb *msg)
1495{
1496 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8830e072009-07-28 17:58:09 +02001497 struct tlv_parsed tp;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001498 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte702d8702008-12-26 20:25:35 +00001499
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001500 LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(sign_link->trx));
Harald Welte8830e072009-07-28 17:58:09 +02001501
1502 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1503
1504 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +01001505 print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +02001506 TLVP_LEN(&tp, RSL_IE_CAUSE));
1507
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001508 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte702d8702008-12-26 20:25:35 +00001509
1510 return 0;
1511}
1512
Harald Welte52b1f982008-12-23 20:25:15 +00001513static int abis_rsl_rx_trx(struct msgb *msg)
1514{
Harald Welte702d8702008-12-26 20:25:35 +00001515 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001516 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte8470bf22008-12-25 23:28:35 +00001517 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001518
1519 switch (rslh->msg_type) {
Harald Welte702d8702008-12-26 20:25:35 +00001520 case RSL_MT_ERROR_REPORT:
1521 rc = rsl_rx_error_rep(msg);
1522 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001523 case RSL_MT_RF_RES_IND:
1524 /* interference on idle channels of TRX */
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001525 //DEBUGP(DRSL, "%s RF Resource Indication\n", gsm_trx_name(sign_link->trx));
Harald Welte8f5e2392009-02-03 12:57:37 +00001526 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001527 case RSL_MT_OVERLOAD:
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001528 /* indicate CCCH / ACCH / processor overload */
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001529 LOGP(DRSL, LOGL_ERROR, "%s CCCH/ACCH/CPU Overload\n",
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001530 gsm_trx_name(sign_link->trx));
Harald Welte52b1f982008-12-23 20:25:15 +00001531 break;
Dieter Spaar16646022011-07-28 00:01:50 +02001532 case 0x42: /* Nokia specific: SI End ACK */
1533 LOGP(DRSL, LOGL_INFO, "Nokia SI End ACK\n");
1534 break;
1535 case 0x43: /* Nokia specific: SI End NACK */
1536 LOGP(DRSL, LOGL_INFO, "Nokia SI End NACK\n");
1537 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001538 default:
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +01001539 LOGP(DRSL, LOGL_NOTICE, "%s Unknown Abis RSL TRX message "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001540 "type 0x%02x\n", gsm_trx_name(sign_link->trx), rslh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001541 return -EINVAL;
1542 }
Harald Welte8470bf22008-12-25 23:28:35 +00001543 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001544}
1545
Harald Welteb7e81162009-08-10 00:26:10 +02001546/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1547static void t3101_expired(void *data)
1548{
1549 struct gsm_lchan *lchan = data;
1550
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +01001551 rsl_rf_chan_release(lchan, 1, SACCH_DEACTIVATE);
Harald Welteb7e81162009-08-10 00:26:10 +02001552}
1553
Holger Hans Peter Freytherf30c0dc2010-05-31 21:33:15 +08001554/* If T3111 expires, we will send the RF Channel Request */
1555static void t3111_expired(void *data)
1556{
1557 struct gsm_lchan *lchan = data;
1558
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +01001559 rsl_rf_chan_release(lchan, 0, SACCH_NONE);
1560}
1561
1562/* If T3109 expires the MS has not send a UA/UM do the error release */
1563static void t3109_expired(void *data)
1564{
1565 struct gsm_lchan *lchan = data;
1566
1567 LOGP(DRSL, LOGL_ERROR,
1568 "%s SACCH deactivation timeout.\n", gsm_lchan_name(lchan));
1569 rsl_rf_chan_release(lchan, 1, SACCH_NONE);
Holger Hans Peter Freytherf30c0dc2010-05-31 21:33:15 +08001570}
1571
Harald Welte2862dca2010-12-23 14:39:29 +01001572/* Format an IMM ASS REJ according to 04.08 Chapter 9.1.20 */
1573static int rsl_send_imm_ass_rej(struct gsm_bts *bts,
1574 unsigned int num_req_refs,
1575 struct gsm48_req_ref *rqd_refs,
1576 uint8_t wait_ind)
1577{
1578 uint8_t buf[GSM_MACBLOCK_LEN];
1579 struct gsm48_imm_ass_rej *iar = (struct gsm48_imm_ass_rej *)buf;
1580
1581 /* create IMMEDIATE ASSIGN REJECT 04.08 message */
1582 memset(iar, 0, sizeof(*iar));
1583 iar->proto_discr = GSM48_PDISC_RR;
Andreas Eversberg75e13a42013-02-07 11:51:16 +01001584 iar->msg_type = GSM48_MT_RR_IMM_ASS_REJ;
Harald Welte2862dca2010-12-23 14:39:29 +01001585 iar->page_mode = GSM48_PM_SAME;
1586
1587 memcpy(&iar->req_ref1, &rqd_refs[0], sizeof(iar->req_ref1));
1588 iar->wait_ind1 = wait_ind;
1589
1590 if (num_req_refs >= 2)
1591 memcpy(&iar->req_ref2, &rqd_refs[1], sizeof(iar->req_ref2));
1592 else
1593 memcpy(&iar->req_ref2, &rqd_refs[0], sizeof(iar->req_ref2));
1594 iar->wait_ind2 = wait_ind;
1595
1596 if (num_req_refs >= 3)
1597 memcpy(&iar->req_ref3, &rqd_refs[2], sizeof(iar->req_ref3));
1598 else
1599 memcpy(&iar->req_ref3, &rqd_refs[0], sizeof(iar->req_ref3));
1600 iar->wait_ind3 = wait_ind;
1601
1602 if (num_req_refs >= 4)
1603 memcpy(&iar->req_ref4, &rqd_refs[3], sizeof(iar->req_ref4));
1604 else
1605 memcpy(&iar->req_ref4, &rqd_refs[0], sizeof(iar->req_ref4));
1606 iar->wait_ind4 = wait_ind;
1607
Andreas Eversberg75e13a42013-02-07 11:51:16 +01001608 /* we need to subtract 1 byte from sizeof(*iar) since ia includes the l2_plen field */
1609 iar->l2_plen = GSM48_LEN2PLEN((sizeof(*iar)-1));
1610
1611 return rsl_imm_assign_cmd(bts, sizeof(*iar), (uint8_t *) iar);
Harald Welte2862dca2010-12-23 14:39:29 +01001612}
1613
Harald Welte8470bf22008-12-25 23:28:35 +00001614/* MS has requested a channel on the RACH */
Harald Welte52b1f982008-12-23 20:25:15 +00001615static int rsl_rx_chan_rqd(struct msgb *msg)
1616{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001617 struct e1inp_sign_link *sign_link = msg->dst;
1618 struct gsm_bts *bts = sign_link->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001619 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1620 struct gsm48_req_ref *rqd_ref;
Harald Welte8470bf22008-12-25 23:28:35 +00001621 enum gsm_chan_t lctype;
Harald Welte2cbe0922008-12-29 04:09:31 +00001622 enum gsm_chreq_reason_t chreq_reason;
Harald Welte8470bf22008-12-25 23:28:35 +00001623 struct gsm_lchan *lchan;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001624 uint8_t rqd_ta;
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +08001625 int is_lu;
Harald Welte8470bf22008-12-25 23:28:35 +00001626
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001627 uint16_t arfcn;
Holger Hans Peter Freytherc6d0a172012-02-03 20:10:13 +01001628 uint8_t subch;
Harald Welte52b1f982008-12-23 20:25:15 +00001629
Harald Welte8470bf22008-12-25 23:28:35 +00001630 /* parse request reference to be used in immediate assign */
1631 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1632 return -EINVAL;
1633
1634 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1635
1636 /* parse access delay and use as TA */
1637 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1638 return -EINVAL;
1639 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1640
1641 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1642 * request reference RA */
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +08001643 lctype = get_ctype_by_chreq(bts->network, rqd_ref->ra);
1644 chreq_reason = get_reason_by_chreq(rqd_ref->ra, bts->network->neci);
Harald Welte2cbe0922008-12-29 04:09:31 +00001645
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001646 osmo_counter_inc(bts->network->stats.chreq.total);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001647
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +08001648 /*
1649 * We want LOCATION UPDATES to succeed and will assign a TCH
1650 * if we have no SDCCH available.
1651 */
1652 is_lu = !!(chreq_reason == GSM_CHREQ_REASON_LOCATION_UPD);
1653
Harald Welte8470bf22008-12-25 23:28:35 +00001654 /* check availability / allocate channel */
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +08001655 lchan = lchan_alloc(bts, lctype, is_lu);
Harald Welte8470bf22008-12-25 23:28:35 +00001656 if (!lchan) {
Harald Welte (local)2f5df852009-12-27 13:48:09 +01001657 LOGP(DRSL, LOGL_NOTICE, "BTS %d CHAN RQD: no resources for %s 0x%x\n",
Harald Welte (local)ccd88452009-12-27 18:05:25 +01001658 msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001659 osmo_counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte2862dca2010-12-23 14:39:29 +01001660 /* FIXME gather multiple CHAN RQD and reject up to 4 at the same time */
1661 if (bts->network->T3122)
1662 rsl_send_imm_ass_rej(bts, 1, rqd_ref, bts->network->T3122 & 0xff);
Harald Welte1dcc2602012-11-13 04:46:03 +01001663 return 0;
Harald Welte8470bf22008-12-25 23:28:35 +00001664 }
1665
Neels Hofmeyrb91e6002016-07-23 19:45:15 +02001666 /*
1667 * Expecting lchan state to be NONE, except for dyn TS in PDCH mode.
1668 * Those are expected to be ACTIVE: the PDCH release will be sent from
1669 * rsl_chan_activate_lchan() below.
1670 */
1671 if (lchan->state != LCHAN_S_NONE
1672 && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
1673 && lchan->ts->dyn.pchan_is == GSM_PCHAN_PDCH
1674 && lchan->state == LCHAN_S_ACTIVE))
Harald Welte8e93b792009-12-29 10:44:17 +01001675 LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
Harald Welte1887f9d2009-12-29 10:52:38 +01001676 "in state %s\n", gsm_lchan_name(lchan),
1677 gsm_lchans_name(lchan->state));
Harald Welte (local)3e460312009-12-27 18:12:29 +01001678
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001679 /* save the RACH data as we need it after the CHAN ACT ACK */
1680 lchan->rqd_ref = talloc_zero(bts, struct gsm48_req_ref);
1681 if (!lchan->rqd_ref) {
1682 LOGP(DRSL, LOGL_ERROR, "Failed to allocate gsm48_req_ref.\n");
1683 lchan_free(lchan);
1684 return -ENOMEM;
1685 }
1686
1687 memcpy(lchan->rqd_ref, rqd_ref, sizeof(*rqd_ref));
1688 lchan->rqd_ta = rqd_ta;
1689
Harald Welte8470bf22008-12-25 23:28:35 +00001690 arfcn = lchan->ts->trx->arfcn;
1691 subch = lchan->nr;
Harald Welte52b1f982008-12-23 20:25:15 +00001692
Harald Welte08d91a52009-08-30 15:37:11 +09001693 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)0e451d02009-08-13 10:14:26 +02001694 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte0b2124b2009-08-10 00:45:40 +02001695 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte9943c5b2009-07-29 15:41:29 +02001696 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte196d0522009-08-28 23:28:28 +09001697 lchan->tch_mode = GSM48_CMODE_SIGN;
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001698
Harald Weltee8bd9e82011-08-10 23:26:33 +02001699 /* Start another timer or assume the BTS sends a ACK/NACK? */
1700 lchan->act_timer.cb = lchan_act_tmr_cb;
1701 lchan->act_timer.data = lchan;
1702 osmo_timer_schedule(&lchan->act_timer, 4, 0);
1703
Andreas Eversberg2957de92011-12-16 17:45:37 +01001704 DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
1705 "r=%s ra=0x%02x ta=%d\n", gsm_lchan_name(lchan), arfcn, subch,
1706 gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
1707 rqd_ref->ra, rqd_ta);
1708
Neels Hofmeyr81516482016-07-15 01:26:03 +02001709 rsl_chan_activate_lchan(lchan, RSL_ACT_INTRA_IMM_ASS, 0);
Harald Welte52b1f982008-12-23 20:25:15 +00001710
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001711 return 0;
1712}
1713
1714static int rsl_send_imm_assignment(struct gsm_lchan *lchan)
1715{
1716 struct gsm_bts *bts = lchan->ts->trx->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001717 uint8_t buf[GSM_MACBLOCK_LEN];
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001718 struct gsm48_imm_ass *ia = (struct gsm48_imm_ass *) buf;
1719
Harald Welte52b1f982008-12-23 20:25:15 +00001720 /* create IMMEDIATE ASSIGN 04.08 messge */
laforge09108bf2010-06-20 15:18:46 +02001721 memset(ia, 0, sizeof(*ia));
laforgecfa4a012010-06-21 12:08:52 +02001722 /* we set ia->l2_plen once we know the length of the MA below */
laforge09108bf2010-06-20 15:18:46 +02001723 ia->proto_discr = GSM48_PDISC_RR;
1724 ia->msg_type = GSM48_MT_RR_IMM_ASS;
1725 ia->page_mode = GSM48_PM_SAME;
1726 gsm48_lchan2chan_desc(&ia->chan_desc, lchan);
Harald Weltea39b0f22010-06-14 22:26:10 +02001727
Harald Welte8470bf22008-12-25 23:28:35 +00001728 /* use request reference extracted from CHAN_RQD */
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +08001729 memcpy(&ia->req_ref, lchan->rqd_ref, sizeof(ia->req_ref));
1730 ia->timing_advance = lchan->rqd_ta;
Harald Weltea39b0f22010-06-14 22:26:10 +02001731 if (!lchan->ts->hopping.enabled) {
laforge09108bf2010-06-20 15:18:46 +02001732 ia->mob_alloc_len = 0;
Harald Weltea39b0f22010-06-14 22:26:10 +02001733 } else {
laforge09108bf2010-06-20 15:18:46 +02001734 ia->mob_alloc_len = lchan->ts->hopping.ma_len;
1735 memcpy(ia->mob_alloc, lchan->ts->hopping.ma_data, ia->mob_alloc_len);
Harald Weltea39b0f22010-06-14 22:26:10 +02001736 }
Harald Weltea1d39a22010-06-28 18:41:27 +02001737 /* we need to subtract 1 byte from sizeof(*ia) since ia includes the l2_plen field */
1738 ia->l2_plen = GSM48_LEN2PLEN((sizeof(*ia)-1) + ia->mob_alloc_len);
Harald Welte52b1f982008-12-23 20:25:15 +00001739
Harald Welteb7e81162009-08-10 00:26:10 +02001740 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1741 lchan->T3101.cb = t3101_expired;
1742 lchan->T3101.data = lchan;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001743 osmo_timer_schedule(&lchan->T3101, bts->network->T3101, 0);
Holger Freyther3186bf22008-12-29 06:23:49 +00001744
Harald Welte52b1f982008-12-23 20:25:15 +00001745 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001746 return rsl_imm_assign_cmd(bts, sizeof(*ia)+ia->mob_alloc_len, (uint8_t *) ia);
Harald Welte52b1f982008-12-23 20:25:15 +00001747}
1748
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001749/* current load on the CCCH */
Harald Welteea280442009-02-02 22:29:56 +00001750static int rsl_rx_ccch_load(struct msgb *msg)
1751{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001752 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welteea280442009-02-02 22:29:56 +00001753 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001754 struct ccch_signal_data sd;
1755
1756 sd.bts = sign_link->trx->bts;
1757 sd.rach_slot_count = -1;
1758 sd.rach_busy_count = -1;
1759 sd.rach_access_count = -1;
Harald Welteea280442009-02-02 22:29:56 +00001760
1761 switch (rslh->data[0]) {
1762 case RSL_IE_PAGING_LOAD:
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001763 sd.pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
1764 if (is_ipaccess_bts(sign_link->trx->bts) && sd.pg_buf_space == 0xffff) {
Harald Welte38e9c822010-04-19 10:24:07 +02001765 /* paging load below configured threshold, use 50 as default */
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001766 sd.pg_buf_space = 50;
Harald Welte38e9c822010-04-19 10:24:07 +02001767 }
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001768 paging_update_buffer_space(sign_link->trx->bts, sd.pg_buf_space);
1769 osmo_signal_dispatch(SS_CCCH, S_CCCH_PAGING_LOAD, &sd);
Harald Welteea280442009-02-02 22:29:56 +00001770 break;
1771 case RSL_IE_RACH_LOAD:
Holger Freyther8c563cf2009-02-03 20:08:51 +00001772 if (msg->data_len >= 7) {
Holger Hans Peter Freyther54fa2c72012-02-03 20:26:25 +01001773 sd.rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1774 sd.rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1775 sd.rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1776 osmo_signal_dispatch(SS_CCCH, S_CCCH_RACH_LOAD, &sd);
Holger Freyther8c563cf2009-02-03 20:08:51 +00001777 }
Harald Welteea280442009-02-02 22:29:56 +00001778 break;
1779 default:
1780 break;
1781 }
1782
1783 return 0;
1784}
1785
Harald Welte52b1f982008-12-23 20:25:15 +00001786static int abis_rsl_rx_cchan(struct msgb *msg)
1787{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001788 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welteea280442009-02-02 22:29:56 +00001789 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001790 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001791
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001792 msg->lchan = lchan_lookup(sign_link->trx, rslh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +00001793
1794 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001795 case RSL_MT_CHAN_RQD:
1796 /* MS has requested a channel on the RACH */
1797 rc = rsl_rx_chan_rqd(msg);
1798 break;
Harald Welteea280442009-02-02 22:29:56 +00001799 case RSL_MT_CCCH_LOAD_IND:
1800 /* current load on the CCCH */
1801 rc = rsl_rx_ccch_load(msg);
1802 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001803 case RSL_MT_DELETE_IND:
1804 /* CCCH overloaded, IMM_ASSIGN was dropped */
1805 case RSL_MT_CBCH_LOAD_IND:
1806 /* current load on the CBCH */
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001807 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1808 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001809 break;
1810 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001811 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1812 "0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001813 return -EINVAL;
1814 }
Harald Welte8470bf22008-12-25 23:28:35 +00001815
1816 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001817}
1818
Harald Welte4b634542008-12-27 01:55:51 +00001819static int rsl_rx_rll_err_ind(struct msgb *msg)
1820{
Holger Hans Peter Freyther164ee302013-01-16 21:07:43 +01001821 struct tlv_parsed tp;
Harald Welte4b634542008-12-27 01:55:51 +00001822 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Holger Hans Peter Freyther164ee302013-01-16 21:07:43 +01001823 uint8_t rlm_cause;
Harald Welte4b634542008-12-27 01:55:51 +00001824
Holger Hans Peter Freyther164ee302013-01-16 21:07:43 +01001825 rsl_tlv_parse(&tp, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
1826 if (!TLVP_PRESENT(&tp, RSL_IE_RLM_CAUSE)) {
1827 LOGP(DRLL, LOGL_ERROR,
1828 "%s ERROR INDICATION without mandantory cause.\n",
1829 gsm_lchan_name(msg->lchan));
1830 return -1;
1831 }
1832
1833 rlm_cause = *TLVP_VAL(&tp, RSL_IE_RLM_CAUSE);
Holger Hans Peter Freyther1e93b792014-04-19 16:45:36 +02001834 LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s in state=%s\n",
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001835 gsm_lchan_name(msg->lchan),
Holger Hans Peter Freyther1e93b792014-04-19 16:45:36 +02001836 rsl_rlm_cause_name(rlm_cause),
1837 gsm_lchans_name(msg->lchan->state));
1838
Harald Welteedcc5272009-08-09 13:47:35 +02001839 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte (local)9538efc2009-12-26 23:55:00 +01001840
Holger Hans Peter Freyther164ee302013-01-16 21:07:43 +01001841 if (rlm_cause == RLL_CAUSE_T200_EXPIRED) {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001842 osmo_counter_inc(msg->lchan->ts->trx->bts->network->stats.chan.rll_err);
Holger Hans Peter Freyther0e4e73a2014-04-19 17:38:33 +02001843 return rsl_rf_chan_release_err(msg->lchan);
Holger Hans Peter Freyther3ba36d52010-04-17 06:48:29 +02001844 }
Harald Welte81543bc2009-07-04 09:40:05 +02001845
Harald Welte4b634542008-12-27 01:55:51 +00001846 return 0;
1847}
Harald Weltef325eb42009-02-19 17:07:39 +00001848
Holger Hans Peter Freytherdbc5fae2010-04-08 22:39:34 +02001849static void rsl_handle_release(struct gsm_lchan *lchan)
1850{
Holger Hans Peter Freyther4b85a322010-07-29 17:09:36 +08001851 int sapi;
Holger Hans Peter Freytherf30c0dc2010-05-31 21:33:15 +08001852 struct gsm_bts *bts;
Holger Hans Peter Freyther4b85a322010-07-29 17:09:36 +08001853
Holger Hans Peter Freyther9d50a272011-12-28 12:11:40 +01001854 /*
1855 * Maybe only one link/SAPI was releasd or the error handling
1856 * was activated. Just return now and let the other code handle
1857 * it.
1858 */
Holger Hans Peter Freytherd7fd3062010-04-08 22:47:44 +02001859 if (lchan->state != LCHAN_S_REL_REQ)
Holger Hans Peter Freyther4b85a322010-07-29 17:09:36 +08001860 return;
1861
1862 for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
1863 if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED)
1864 continue;
Harald Welte3a3c2772010-12-24 12:51:07 +01001865 LOGP(DRSL, LOGL_DEBUG, "%s waiting for SAPI=%d to be released.\n",
Holger Hans Peter Freyther4b85a322010-07-29 17:09:36 +08001866 gsm_lchan_name(lchan), sapi);
1867 return;
1868 }
1869
Holger Hans Peter Freytherd7fd3062010-04-08 22:47:44 +02001870
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +01001871 /* Stop T3109 and wait for T3111 before re-using the channel */
1872 osmo_timer_del(&lchan->T3109);
Holger Hans Peter Freytherf30c0dc2010-05-31 21:33:15 +08001873 lchan->T3111.cb = t3111_expired;
1874 lchan->T3111.data = lchan;
1875 bts = lchan->ts->trx->bts;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001876 osmo_timer_schedule(&lchan->T3111, bts->network->T3111, 0);
Holger Hans Peter Freytherdbc5fae2010-04-08 22:39:34 +02001877}
1878
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001879/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
Harald Welte52b1f982008-12-23 20:25:15 +00001880 0x02, 0x06,
1881 0x01, 0x20,
1882 0x02, 0x00,
1883 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1884
1885static int abis_rsl_rx_rll(struct msgb *msg)
1886{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001887 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte52b1f982008-12-23 20:25:15 +00001888 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltef325eb42009-02-19 17:07:39 +00001889 int rc = 0;
1890 char *ts_name;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001891 uint8_t sapi = rllh->link_id & 7;
Harald Welte8470bf22008-12-25 23:28:35 +00001892
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02001893 msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01001894 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte5b8ed432009-12-24 12:20:20 +01001895 DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
Harald Welte52b1f982008-12-23 20:25:15 +00001896
1897 switch (rllh->c.msg_type) {
1898 case RSL_MT_DATA_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001899 DEBUGPC(DRLL, "DATA INDICATION\n");
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001900 if (msgb_l2len(msg) >
Harald Welte4a543e82009-02-28 13:17:55 +00001901 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1902 rllh->data[0] == RSL_IE_L3_INFO) {
1903 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001904 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001905 }
Harald Welte52b1f982008-12-23 20:25:15 +00001906 break;
1907 case RSL_MT_EST_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001908 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welteb7e81162009-08-10 00:26:10 +02001909 /* lchan is established, stop T3101 */
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001910 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001911 osmo_timer_del(&msg->lchan->T3101);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001912 if (msgb_l2len(msg) >
Harald Welte4a543e82009-02-28 13:17:55 +00001913 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1914 rllh->data[0] == RSL_IE_L3_INFO) {
1915 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001916 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001917 }
Harald Welte52b1f982008-12-23 20:25:15 +00001918 break;
Harald Welteedcc5272009-08-09 13:47:35 +02001919 case RSL_MT_EST_CONF:
Harald Welte1c409272009-08-09 14:13:58 +02001920 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001921 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteedcc5272009-08-09 13:47:35 +02001922 rll_indication(msg->lchan, rllh->link_id,
1923 BSC_RLLR_IND_EST_CONF);
1924 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001925 case RSL_MT_REL_IND:
Harald Welted2dc1de2009-08-08 13:15:07 +02001926 /* BTS informs us of having received DISC from MS */
Harald Welte602f2b82009-08-04 02:50:21 +02001927 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001928 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteedcc5272009-08-09 13:47:35 +02001929 rll_indication(msg->lchan, rllh->link_id,
1930 BSC_RLLR_IND_REL_IND);
Holger Hans Peter Freytherdbc5fae2010-04-08 22:39:34 +02001931 rsl_handle_release(msg->lchan);
Harald Welte2d5b6382008-12-27 19:46:06 +00001932 break;
1933 case RSL_MT_REL_CONF:
Harald Welted2dc1de2009-08-08 13:15:07 +02001934 /* BTS informs us of having received UA from MS,
1935 * in response to DISC that we've sent earlier */
Harald Welte602f2b82009-08-04 02:50:21 +02001936 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001937 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Holger Hans Peter Freytherdbc5fae2010-04-08 22:39:34 +02001938 rsl_handle_release(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +00001939 break;
1940 case RSL_MT_ERROR_IND:
Neels Hofmeyre3dc4982016-07-26 19:39:43 +02001941 DEBUGPC(DRLL, "ERROR INDICATION\n");
Harald Welte4b634542008-12-27 01:55:51 +00001942 rc = rsl_rx_rll_err_ind(msg);
1943 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001944 case RSL_MT_UNIT_DATA_IND:
Neels Hofmeyre3dc4982016-07-26 19:39:43 +02001945 DEBUGPC(DRLL, "UNIT DATA INDICATION\n");
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001946 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1947 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001948 break;
1949 default:
Neels Hofmeyre3dc4982016-07-26 19:39:43 +02001950 DEBUGPC(DRLL, "UNKNOWN\n");
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001951 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1952 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001953 }
Harald Welte8470bf22008-12-25 23:28:35 +00001954 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001955}
1956
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001957static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Weltef4e79f22009-07-28 18:11:56 +02001958{
Harald Welte0603c9d2009-12-02 01:58:23 +05301959 switch (lchan->tch_mode) {
Harald Weltef4e79f22009-07-28 18:11:56 +02001960 case GSM48_CMODE_SPEECH_V1:
Harald Welte0603c9d2009-12-02 01:58:23 +05301961 switch (lchan->type) {
1962 case GSM_LCHAN_TCH_F:
1963 return 0x00;
1964 case GSM_LCHAN_TCH_H:
1965 return 0x03;
1966 default:
1967 break;
1968 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02001969 break;
Harald Weltef4e79f22009-07-28 18:11:56 +02001970 case GSM48_CMODE_SPEECH_EFR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301971 switch (lchan->type) {
1972 case GSM_LCHAN_TCH_F:
1973 return 0x01;
1974 /* there's no half-rate EFR */
1975 default:
1976 break;
1977 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02001978 break;
Harald Weltef4e79f22009-07-28 18:11:56 +02001979 case GSM48_CMODE_SPEECH_AMR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301980 switch (lchan->type) {
1981 case GSM_LCHAN_TCH_F:
1982 return 0x02;
1983 case GSM_LCHAN_TCH_H:
1984 return 0x05;
1985 default:
1986 break;
1987 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02001988 break;
Harald Welte0603c9d2009-12-02 01:58:23 +05301989 default:
1990 break;
Harald Weltef4e79f22009-07-28 18:11:56 +02001991 }
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001992 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welte0603c9d2009-12-02 01:58:23 +05301993 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001994 return 0;
Harald Weltef4e79f22009-07-28 18:11:56 +02001995}
1996
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001997static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
Sylvain Munautb54dda42009-12-20 22:06:40 +01001998{
1999 switch (lchan->tch_mode) {
2000 case GSM48_CMODE_SPEECH_V1:
2001 switch (lchan->type) {
2002 case GSM_LCHAN_TCH_F:
2003 return RTP_PT_GSM_FULL;
2004 case GSM_LCHAN_TCH_H:
2005 return RTP_PT_GSM_HALF;
2006 default:
2007 break;
2008 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02002009 break;
Sylvain Munautb54dda42009-12-20 22:06:40 +01002010 case GSM48_CMODE_SPEECH_EFR:
2011 switch (lchan->type) {
2012 case GSM_LCHAN_TCH_F:
2013 return RTP_PT_GSM_EFR;
2014 /* there's no half-rate EFR */
2015 default:
2016 break;
2017 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02002018 break;
Sylvain Munautb54dda42009-12-20 22:06:40 +01002019 case GSM48_CMODE_SPEECH_AMR:
2020 switch (lchan->type) {
2021 case GSM_LCHAN_TCH_F:
Sylvain Munautb54dda42009-12-20 22:06:40 +01002022 case GSM_LCHAN_TCH_H:
Holger Hans Peter Freythered999b22011-07-21 10:24:46 +02002023 return RTP_PT_AMR;
Sylvain Munautb54dda42009-12-20 22:06:40 +01002024 default:
2025 break;
2026 }
Holger Hans Peter Freyther47665242014-04-04 12:14:55 +02002027 break;
Sylvain Munautb54dda42009-12-20 22:06:40 +01002028 default:
2029 break;
2030 }
2031 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access rtp payload type for "
2032 "tch_mode == 0x%02x\n & lchan_type == %d",
2033 lchan->tch_mode, lchan->type);
2034 return 0;
2035}
2036
Harald Welte75099262009-02-16 21:12:08 +00002037/* ip.access specific RSL extensions */
Harald Welte5e3d91b2009-12-19 16:42:06 +01002038static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
2039{
2040 struct in_addr ip;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002041 uint16_t port, conn_id;
Harald Welte5e3d91b2009-12-19 16:42:06 +01002042
2043 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
Daniel Willmann8a485f02014-06-27 17:05:47 +02002044 ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_LOCAL_IP);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002045 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
2046 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
2047 }
2048
2049 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
Daniel Willmann8a485f02014-06-27 17:05:47 +02002050 port = tlvp_val16_unal(tv, RSL_IE_IPAC_LOCAL_PORT);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002051 port = ntohs(port);
2052 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
2053 lchan->abis_ip.bound_port = port;
2054 }
2055
2056 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
Daniel Willmann8a485f02014-06-27 17:05:47 +02002057 conn_id = tlvp_val16_unal(tv, RSL_IE_IPAC_CONN_ID);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002058 conn_id = ntohs(conn_id);
2059 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
2060 lchan->abis_ip.conn_id = conn_id;
2061 }
2062
2063 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
2064 lchan->abis_ip.rtp_payload2 =
2065 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
2066 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
2067 lchan->abis_ip.rtp_payload2);
2068 }
2069
2070 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
2071 lchan->abis_ip.speech_mode =
2072 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
2073 DEBUGPC(DRSL, "speech_mode=0x%02x ",
2074 lchan->abis_ip.speech_mode);
2075 }
2076
2077 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
Daniel Willmann8a485f02014-06-27 17:05:47 +02002078 ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_REMOTE_IP);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002079 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
2080 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
2081 }
2082
2083 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
Daniel Willmann8a485f02014-06-27 17:05:47 +02002084 port = tlvp_val16_unal(tv, RSL_IE_IPAC_REMOTE_PORT);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002085 port = ntohs(port);
2086 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
2087 lchan->abis_ip.connect_port = port;
2088 }
2089}
2090
Harald Welte647db842013-02-03 12:06:58 +01002091/*! \brief Issue IPA RSL CRCX to configure RTP on BTS side
2092 * \param[in] lchan Logical Channel for which we issue CRCX
2093 */
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002094int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte75099262009-02-16 21:12:08 +00002095{
2096 struct msgb *msg = rsl_msgb_alloc();
2097 struct abis_rsl_dchan_hdr *dh;
2098
2099 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002100 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte75099262009-02-16 21:12:08 +00002101 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
Harald Weltef6093a42011-06-25 10:02:33 +02002102 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welte75099262009-02-16 21:12:08 +00002103
Harald Weltef4e79f22009-07-28 18:11:56 +02002104 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Welte5e3d91b2009-12-19 16:42:06 +01002105 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
Sylvain Munautb54dda42009-12-20 22:06:40 +01002106 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002107 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munautb54dda42009-12-20 22:06:40 +01002108 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Weltef4e79f22009-07-28 18:11:56 +02002109
Sylvain Munautb54dda42009-12-20 22:06:40 +01002110 DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x RTP_PAYLOAD=%d\n",
2111 gsm_lchan_name(lchan), lchan->abis_ip.speech_mode,
2112 lchan->abis_ip.rtp_payload);
Harald Weltef4e79f22009-07-28 18:11:56 +02002113
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002114 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte75099262009-02-16 21:12:08 +00002115
2116 return abis_rsl_sendmsg(msg);
2117}
2118
Harald Welte647db842013-02-03 12:06:58 +01002119/*! \brief Issue IPA RSL MDCX to configure MGW-side of RTP
2120 * \param[in] lchan Logical Channel for which we issue MDCX
2121 * \param[in] ip Remote (MGW) IP address for RTP
2122 * \param[in] port Remote (MGW) UDP port number for RTP
2123 * \param[in] rtp_payload2 Contents of RTP PAYLOAD 2 IE
2124 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002125int rsl_ipacc_mdcx(struct gsm_lchan *lchan, uint32_t ip, uint16_t port,
2126 uint8_t rtp_payload2)
Harald Welte75099262009-02-16 21:12:08 +00002127{
2128 struct msgb *msg = rsl_msgb_alloc();
2129 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002130 uint32_t *att_ip;
Harald Weltef4e79f22009-07-28 18:11:56 +02002131 struct in_addr ia;
Harald Welte75099262009-02-16 21:12:08 +00002132
2133 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002134 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte75099262009-02-16 21:12:08 +00002135 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
Harald Weltef6093a42011-06-25 10:02:33 +02002136 dh->chan_nr = gsm_lchan2chan_nr(lchan);
Harald Welte75099262009-02-16 21:12:08 +00002137
Harald Welte5e3d91b2009-12-19 16:42:06 +01002138 /* we need to store these now as MDCX_ACK does not return them :( */
2139 lchan->abis_ip.rtp_payload2 = rtp_payload2;
2140 lchan->abis_ip.connect_port = port;
2141 lchan->abis_ip.connect_ip = ip;
2142
Harald Welte58ca5b72009-07-29 12:12:18 +02002143 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Welte5e3d91b2009-12-19 16:42:06 +01002144 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Sylvain Munautb54dda42009-12-20 22:06:40 +01002145 lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
Harald Welte58ca5b72009-07-29 12:12:18 +02002146
Harald Weltef4e79f22009-07-28 18:11:56 +02002147 ia.s_addr = htonl(ip);
Sylvain Munautb54dda42009-12-20 22:06:40 +01002148 DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD=%d RTP_PAYLOAD2=%d "
2149 "CONN_ID=%d speech_mode=0x%02x\n", gsm_lchan_name(lchan),
2150 inet_ntoa(ia), port, lchan->abis_ip.rtp_payload, rtp_payload2,
2151 lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02002152
Harald Welte5e3d91b2009-12-19 16:42:06 +01002153 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
2154 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002155 att_ip = (uint32_t *) msgb_put(msg, sizeof(ip));
Harald Welte5e3d91b2009-12-19 16:42:06 +01002156 *att_ip = ia.s_addr;
2157 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
2158 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Sylvain Munautb54dda42009-12-20 22:06:40 +01002159 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
Harald Weltef4e79f22009-07-28 18:11:56 +02002160 if (rtp_payload2)
2161 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002162
2163 msg->dst = lchan->ts->trx->rsl_link;
Harald Welte75099262009-02-16 21:12:08 +00002164
2165 return abis_rsl_sendmsg(msg);
2166}
2167
Harald Weltea72273e2009-12-20 16:51:09 +01002168/* tell BTS to connect RTP stream to our local RTP socket */
2169int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
2170{
2171 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
2172 int rc;
2173
2174 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
2175 ntohs(rs->rtp.sin_local.sin_port),
2176 /* FIXME: use RTP payload of bound socket, not BTS*/
2177 lchan->abis_ip.rtp_payload2);
2178
2179 return rc;
2180}
2181
Harald Welte53cd7ac2010-12-23 12:59:52 +01002182int rsl_ipacc_pdch_activate(struct gsm_bts_trx_ts *ts, int act)
Harald Welte9c880c92009-10-24 10:29:22 +02002183{
2184 struct msgb *msg = rsl_msgb_alloc();
2185 struct abis_rsl_dchan_hdr *dh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002186 uint8_t msg_type;
Harald Welte4563eab2010-03-28 14:42:09 +08002187
Neels Hofmeyr32019882016-06-15 15:32:29 +02002188 if (ts->flags & TS_F_PDCH_PENDING_MASK) {
2189 LOGP(DRSL, LOGL_ERROR,
2190 "%s PDCH %s requested, but a PDCH%s%s is still pending\n",
2191 gsm_ts_name(ts),
2192 act ? "ACT" : "DEACT",
2193 ts->flags & TS_F_PDCH_ACT_PENDING? " ACT" : "",
2194 ts->flags & TS_F_PDCH_DEACT_PENDING? " DEACT" : "");
2195 return -EINVAL;
2196 }
2197
2198 if (act){
Neels Hofmeyr9ddd8e62016-07-06 14:39:04 +02002199 /* Callers should heed the GPRS mode. */
2200 OSMO_ASSERT(ts->trx->bts->gprs.mode != BTS_GPRS_NONE);
Harald Welte4563eab2010-03-28 14:42:09 +08002201 msg_type = RSL_MT_IPAC_PDCH_ACT;
Neels Hofmeyr32019882016-06-15 15:32:29 +02002202 ts->flags |= TS_F_PDCH_ACT_PENDING;
2203 } else {
Harald Welte4563eab2010-03-28 14:42:09 +08002204 msg_type = RSL_MT_IPAC_PDCH_DEACT;
Neels Hofmeyr32019882016-06-15 15:32:29 +02002205 ts->flags |= TS_F_PDCH_DEACT_PENDING;
2206 }
2207 /* TODO add timeout to cancel PDCH DE/ACT */
Harald Welte9c880c92009-10-24 10:29:22 +02002208
2209 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Harald Welte4563eab2010-03-28 14:42:09 +08002210 init_dchan_hdr(dh, msg_type);
Harald Welte9c880c92009-10-24 10:29:22 +02002211 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Neels Hofmeyrd3841102016-07-18 23:43:44 +02002212 dh->chan_nr = gsm_pchan2chan_nr(GSM_PCHAN_PDCH, ts->nr, 0);
Harald Welte9c880c92009-10-24 10:29:22 +02002213
Neels Hofmeyr9f5d2312016-05-31 17:51:41 +02002214 DEBUGP(DRSL, "%s IPAC PDCH %sACT\n", gsm_ts_name(ts),
Harald Welte4563eab2010-03-28 14:42:09 +08002215 act ? "" : "DE");
Harald Welte9c880c92009-10-24 10:29:22 +02002216
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002217 msg->dst = ts->trx->rsl_link;
Harald Welte9c880c92009-10-24 10:29:22 +02002218
2219 return abis_rsl_sendmsg(msg);
2220}
2221
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002222static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00002223{
2224 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
2225 struct tlv_parsed tv;
Harald Welte2c828992009-12-02 01:56:49 +05302226 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75099262009-02-16 21:12:08 +00002227
2228 /* the BTS has acknowledged a local bind, it now tells us the IP
2229 * address and port number to which it has bound the given logical
2230 * channel */
2231
2232 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
2233 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
2234 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welte86c162d2009-07-12 09:45:05 +02002235 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +01002236 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte75099262009-02-16 21:12:08 +00002237 return -EINVAL;
2238 }
Harald Welte17f5bf62009-12-20 15:42:44 +01002239
Harald Welte5e3d91b2009-12-19 16:42:06 +01002240 ipac_parse_rtp(lchan, &tv);
Harald Welte17f5bf62009-12-20 15:42:44 +01002241
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02002242 osmo_signal_dispatch(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte167df882009-02-17 14:35:45 +00002243
Harald Welte75099262009-02-16 21:12:08 +00002244 return 0;
2245}
2246
Harald Welte5e3d91b2009-12-19 16:42:06 +01002247static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
2248{
2249 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
2250 struct tlv_parsed tv;
2251 struct gsm_lchan *lchan = msg->lchan;
2252
2253 /* the BTS has acknowledged a remote connect request and
2254 * it now tells us the IP address and port number to which it has
2255 * connected the given logical channel */
2256
2257 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
2258 ipac_parse_rtp(lchan, &tv);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02002259 osmo_signal_dispatch(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002260
2261 return 0;
2262}
2263
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002264static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00002265{
2266 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
2267 struct tlv_parsed tv;
2268
2269 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte75099262009-02-16 21:12:08 +00002270
Harald Welte8830e072009-07-28 17:58:09 +02002271 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
Harald Welte5b8ed432009-12-24 12:20:20 +01002272 print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
Harald Welte8830e072009-07-28 17:58:09 +02002273 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte75099262009-02-16 21:12:08 +00002274
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02002275 osmo_signal_dispatch(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welte888b1142009-07-28 18:02:05 +02002276
Harald Welte75099262009-02-16 21:12:08 +00002277 return 0;
2278}
2279
2280static int abis_rsl_rx_ipacc(struct msgb *msg)
2281{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002282 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte75099262009-02-16 21:12:08 +00002283 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte5b8ed432009-12-24 12:20:20 +01002284 char *ts_name;
Harald Welte75099262009-02-16 21:12:08 +00002285 int rc = 0;
2286
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002287 msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr);
Harald Welte (local)19ef62a2009-12-27 18:16:36 +01002288 ts_name = gsm_lchan_name(msg->lchan);
Harald Welte75099262009-02-16 21:12:08 +00002289
2290 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002291 case RSL_MT_IPAC_CRCX_ACK:
Harald Welte5b8ed432009-12-24 12:20:20 +01002292 DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002293 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte75099262009-02-16 21:12:08 +00002294 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002295 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00002296 /* somehow the BTS was unable to bind the lchan to its local
2297 * port?!? */
Harald Welte5b8ed432009-12-24 12:20:20 +01002298 LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
Harald Welte75099262009-02-16 21:12:08 +00002299 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002300 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte75099262009-02-16 21:12:08 +00002301 /* the BTS tells us that a connect operation was successful */
Harald Welte5b8ed432009-12-24 12:20:20 +01002302 DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
Harald Welte5e3d91b2009-12-19 16:42:06 +01002303 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte75099262009-02-16 21:12:08 +00002304 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002305 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00002306 /* somehow the BTS was unable to connect the lchan to a remote
2307 * port */
Harald Welte5b8ed432009-12-24 12:20:20 +01002308 LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
Harald Welte75099262009-02-16 21:12:08 +00002309 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002310 case RSL_MT_IPAC_DLCX_IND:
Harald Welte5b8ed432009-12-24 12:20:20 +01002311 DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01002312 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte75099262009-02-16 21:12:08 +00002313 break;
2314 default:
Harald Welte5b8ed432009-12-24 12:20:20 +01002315 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n",
Harald Welteb1d4c8e2009-12-17 23:10:46 +01002316 rllh->c.msg_type);
Harald Welte75099262009-02-16 21:12:08 +00002317 break;
2318 }
Harald Welte6dab0552009-05-01 17:21:37 +00002319 DEBUGPC(DRSL, "\n");
Harald Welte75099262009-02-16 21:12:08 +00002320
2321 return rc;
2322}
2323
Neels Hofmeyrb91e6002016-07-23 19:45:15 +02002324static int dyn_ts_switchover_start(struct gsm_lchan *lchan,
2325 enum gsm_phys_chan_config to_pchan)
2326{
2327 int ss;
2328 struct gsm_bts_trx_ts *ts = lchan->ts;
2329 int rc = -EIO;
2330
2331 OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
2332 DEBUGP(DRSL, "%s starting switchover to %s\n",
2333 gsm_ts_and_pchan_name(ts), gsm_pchan_name(to_pchan));
2334
2335 if (ts->dyn.pchan_is != ts->dyn.pchan_want) {
2336 LOGP(DRSL, LOGL_ERROR,
2337 "%s: Attempt to switch dynamic channel to %s,"
2338 " but is already in switchover.\n",
2339 gsm_ts_and_pchan_name(ts),
2340 gsm_pchan_name(to_pchan));
2341 return ts->dyn.pchan_want == to_pchan? 0 : -EAGAIN;
2342 }
2343
2344 if (ts->dyn.pchan_is == to_pchan) {
2345 LOGP(DRSL, LOGL_INFO,
2346 "%s %s Already is in %s mode, skipping switchover.\n",
2347 gsm_ts_name(ts), gsm_pchan_name(ts->pchan),
2348 gsm_pchan_name(to_pchan));
2349 dyn_ts_switchover_complete(lchan);
2350 return 0;
2351 }
2352
2353 /* Paranoia: let's make sure all is indeed released. */
2354 for (ss = 0; ss < ts_subslots(lchan->ts); ss++) {
2355 struct gsm_lchan *lc = &ts->lchan[ss];
2356 if (lc->state != LCHAN_S_NONE) {
2357 LOGP(DRSL, LOGL_ERROR,
2358 "%s Attempt to switch dynamic channel to %s,"
2359 " but is not fully released.\n",
2360 gsm_ts_and_pchan_name(ts),
2361 gsm_pchan_name(to_pchan));
2362 return -EAGAIN;
2363 }
2364 }
2365
2366 /* Record that we're busy switching. */
2367 ts->dyn.pchan_want = to_pchan;
2368
2369 /*
2370 * To switch from PDCH, we need to initiate the release from the BSC
2371 * side. dyn_ts_switchover_continue() will be called from
2372 * rsl_rx_rf_chan_rel_ack().
2373 */
2374 if (ts->dyn.pchan_is == GSM_PCHAN_PDCH) {
2375 rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ);
2376 rc = rsl_rf_chan_release(lchan, 0, SACCH_NONE);
2377 if (rc) {
2378 LOGP(DRSL, LOGL_ERROR,
2379 "%s RSL RF Chan Release failed\n",
2380 gsm_ts_and_pchan_name(ts));
2381 return dyn_ts_switchover_failed(lchan, rc);
2382 }
2383 return 0;
2384 }
2385
2386 /*
2387 * To switch from TCH/F and TCH/H pchans, this has been called from
2388 * rsl_rx_rf_chan_rel_ack(), i.e. release is complete. Go ahead and
2389 * activate as new type. This will always be PDCH.
2390 */
2391 return dyn_ts_switchover_continue(lchan);
2392}
2393
2394static int dyn_ts_switchover_continue(struct gsm_lchan *lchan)
2395{
2396 int rc;
2397 uint8_t act_type;
2398 uint8_t ho_ref;
2399 struct gsm_bts_trx_ts *ts = lchan->ts;
2400
2401 OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
2402 DEBUGP(DRSL, "%s switchover: release complete,"
2403 " activating new pchan type\n",
2404 gsm_ts_and_pchan_name(ts));
2405
2406 if (ts->dyn.pchan_is == ts->dyn.pchan_want) {
2407 LOGP(DRSL, LOGL_ERROR,
2408 "%s Requested to switchover dynamic channel to the"
2409 " same type it is already in.\n",
2410 gsm_ts_and_pchan_name(ts));
2411 return 0;
2412 }
2413
2414 /*
2415 * For TCH/x, the lchan->type has been set in lchan_alloc(), but it may
2416 * have been lost during channel release due to dynamic switchover.
2417 *
2418 * For PDCH, the lchan->type will actually remain NONE.
2419 * TODO: set GSM_LCHAN_PDTCH?
2420 */
2421 switch (ts->dyn.pchan_want) {
2422 case GSM_PCHAN_TCH_F:
2423 lchan->type = GSM_LCHAN_TCH_F;
2424 break;
2425 case GSM_PCHAN_TCH_H:
2426 lchan->type = GSM_LCHAN_TCH_H;
2427 break;
2428 case GSM_PCHAN_PDCH:
2429 lchan->type = GSM_LCHAN_NONE;
2430 break;
2431 default:
2432 LOGP(DRSL, LOGL_ERROR,
2433 "%s Invalid target pchan for dynamic TS\n",
2434 gsm_ts_and_pchan_name(ts));
2435 }
2436
2437 act_type = (ts->dyn.pchan_want == GSM_PCHAN_PDCH)
2438 ? RSL_ACT_OSMO_PDCH
2439 : lchan->dyn.act_type;
2440 ho_ref = (ts->dyn.pchan_want == GSM_PCHAN_PDCH)
2441 ? 0
2442 : lchan->dyn.ho_ref;
2443
2444 /* Fetch the rqd_ref back from before switchover started. */
2445 if (lchan->rqd_ref) {
2446 LOGP(DRSL, LOGL_ERROR,
2447 "%s During dyn TS switchover, expecting no"
2448 " Request Reference to be pending. Discarding!\n",
2449 gsm_lchan_name(lchan));
2450 talloc_free(lchan->rqd_ref);
2451 lchan->rqd_ref = NULL;
2452 }
2453 lchan->rqd_ref = lchan->dyn.rqd_ref;
2454 lchan->rqd_ta = lchan->dyn.rqd_ta;
2455 lchan->dyn.rqd_ref = NULL;
2456 lchan->dyn.rqd_ta = 0;
2457
2458 rc = rsl_chan_activate_lchan(lchan, act_type, ho_ref);
2459 if (rc) {
2460 LOGP(DRSL, LOGL_ERROR,
2461 "%s RSL Chan Activate failed\n",
2462 gsm_ts_and_pchan_name(ts));
2463 return dyn_ts_switchover_failed(lchan, rc);
2464 }
2465 return 0;
2466}
2467
2468static int dyn_ts_switchover_failed(struct gsm_lchan *lchan, int rc)
2469{
2470 struct gsm_bts_trx_ts *ts = lchan->ts;
2471 ts->dyn.pchan_want = ts->dyn.pchan_is;
2472 LOGP(DRSL, LOGL_ERROR, "%s Error %d during dynamic channel switchover."
2473 " Going back to previous pchan.\n", gsm_ts_and_pchan_name(ts),
2474 rc);
2475 return rc;
2476}
2477
2478static void dyn_ts_switchover_complete(struct gsm_lchan *lchan)
2479{
2480 enum gsm_phys_chan_config pchan_act;
2481 enum gsm_phys_chan_config pchan_was;
2482 struct gsm_bts_trx_ts *ts = lchan->ts;
2483
2484 OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
2485
2486 pchan_act = pchan_for_lchant(lchan->type);
2487 /*
2488 * Paranoia: do the types match?
2489 * In case of errors: we've received an act ack already, so what to do
2490 * about it? Logging the error should suffice for now.
2491 */
2492 if (pchan_act != ts->dyn.pchan_want)
2493 LOGP(DRSL, LOGL_ERROR,
2494 "%s Requested transition does not match lchan type %s\n",
2495 gsm_ts_and_pchan_name(ts),
2496 gsm_lchant_name(ts->lchan[0].type));
2497
2498 pchan_was = ts->dyn.pchan_is;
2499 ts->dyn.pchan_is = ts->dyn.pchan_want = pchan_act;
2500
2501 LOGP(DRSL, LOGL_INFO, "%s switchover from %s complete.\n",
2502 gsm_ts_and_pchan_name(ts), gsm_pchan_name(pchan_was));
2503}
Harald Welte75099262009-02-16 21:12:08 +00002504
Harald Welte52b1f982008-12-23 20:25:15 +00002505/* Entry-point where L2 RSL from BTS enters */
Harald Welte8470bf22008-12-25 23:28:35 +00002506int abis_rsl_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00002507{
Holger Hans Peter Freyther19bab732009-11-20 15:14:01 +01002508 struct abis_rsl_common_hdr *rslh;
Harald Welte8f5e2392009-02-03 12:57:37 +00002509 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00002510
Holger Hans Peter Freyther19bab732009-11-20 15:14:01 +01002511 if (!msg) {
2512 DEBUGP(DRSL, "Empty RSL msg?..\n");
2513 return -1;
2514 }
2515
2516 if (msgb_l2len(msg) < sizeof(*rslh)) {
2517 DEBUGP(DRSL, "Truncated RSL message with l2len: %u\n", msgb_l2len(msg));
Harald Weltef25b55e2012-05-31 20:22:34 +02002518 msgb_free(msg);
Holger Hans Peter Freyther19bab732009-11-20 15:14:01 +01002519 return -1;
2520 }
2521
2522 rslh = msgb_l2(msg);
2523
Harald Welte52b1f982008-12-23 20:25:15 +00002524 switch (rslh->msg_discr & 0xfe) {
2525 case ABIS_RSL_MDISC_RLL:
2526 rc = abis_rsl_rx_rll(msg);
2527 break;
2528 case ABIS_RSL_MDISC_DED_CHAN:
2529 rc = abis_rsl_rx_dchan(msg);
2530 break;
2531 case ABIS_RSL_MDISC_COM_CHAN:
Harald Welte52b1f982008-12-23 20:25:15 +00002532 rc = abis_rsl_rx_cchan(msg);
2533 break;
Harald Welte8470bf22008-12-25 23:28:35 +00002534 case ABIS_RSL_MDISC_TRX:
2535 rc = abis_rsl_rx_trx(msg);
2536 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002537 case ABIS_RSL_MDISC_LOC:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01002538 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte8f5e2392009-02-03 12:57:37 +00002539 rslh->msg_discr);
2540 break;
Harald Welte75099262009-02-16 21:12:08 +00002541 case ABIS_RSL_MDISC_IPACCESS:
2542 rc = abis_rsl_rx_ipacc(msg);
2543 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002544 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01002545 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
2546 "0x%02x\n", rslh->msg_discr);
Harald Weltef25b55e2012-05-31 20:22:34 +02002547 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00002548 }
Harald Welte4f4a3902008-12-26 00:04:49 +00002549 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00002550 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00002551}
Holger Freyther3b72a892009-02-04 00:31:39 +00002552
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002553int rsl_sms_cb_command(struct gsm_bts *bts, uint8_t chan_number,
Harald Welte30f1f372014-12-28 15:00:45 +01002554 struct rsl_ie_cb_cmd_type cb_command,
2555 const uint8_t *data, int len)
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002556{
2557 struct abis_rsl_dchan_hdr *dh;
2558 struct msgb *cb_cmd;
2559
2560 cb_cmd = rsl_msgb_alloc();
2561 if (!cb_cmd)
2562 return -1;
2563
Harald Welte30f1f372014-12-28 15:00:45 +01002564 dh = (struct abis_rsl_dchan_hdr *) msgb_put(cb_cmd, sizeof(*dh));
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002565 init_dchan_hdr(dh, RSL_MT_SMS_BC_CMD);
Harald Welte30f1f372014-12-28 15:00:45 +01002566 dh->c.msg_discr = ABIS_RSL_MDISC_COM_CHAN;
2567 dh->chan_nr = chan_number; /* TODO: check the chan config */
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002568
Harald Welte30f1f372014-12-28 15:00:45 +01002569 msgb_tv_put(cb_cmd, RSL_IE_CB_CMD_TYPE, *(uint8_t*)&cb_command);
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002570 msgb_tlv_put(cb_cmd, RSL_IE_SMSCB_MSG, len, data);
2571
Harald Welte30f1f372014-12-28 15:00:45 +01002572 cb_cmd->dst = bts->c0->rsl_link;
Holger Hans Peter Freyther8cb4a0f2010-07-21 15:54:32 +08002573
2574 return abis_rsl_sendmsg(cb_cmd);
2575}
Dieter Spaar16646022011-07-28 00:01:50 +02002576
2577int rsl_nokia_si_begin(struct gsm_bts_trx *trx)
2578{
2579 struct abis_rsl_common_hdr *ch;
2580 struct msgb *msg = rsl_msgb_alloc();
2581
2582 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
2583 ch->msg_discr = ABIS_RSL_MDISC_TRX;
2584 ch->msg_type = 0x40; /* Nokia SI Begin */
2585
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002586 msg->dst = trx->rsl_link;
Dieter Spaar16646022011-07-28 00:01:50 +02002587
2588 return abis_rsl_sendmsg(msg);
2589}
2590
2591int rsl_nokia_si_end(struct gsm_bts_trx *trx)
2592{
2593 struct abis_rsl_common_hdr *ch;
2594 struct msgb *msg = rsl_msgb_alloc();
2595
2596 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
2597 ch->msg_discr = ABIS_RSL_MDISC_TRX;
2598 ch->msg_type = 0x41; /* Nokia SI End */
2599
2600 msgb_tv_put(msg, 0xFD, 0x00); /* Nokia Pagemode Info, No paging reorganisation required */
2601
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002602 msg->dst = trx->rsl_link;
Dieter Spaar16646022011-07-28 00:01:50 +02002603
2604 return abis_rsl_sendmsg(msg);
2605}
2606
2607int rsl_bs_power_control(struct gsm_bts_trx *trx, uint8_t channel, uint8_t reduction)
2608{
2609 struct abis_rsl_common_hdr *ch;
2610 struct msgb *msg = rsl_msgb_alloc();
2611
2612 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
2613 ch->msg_discr = ABIS_RSL_MDISC_DED_CHAN;
2614 ch->msg_type = RSL_MT_BS_POWER_CONTROL;
2615
2616 msgb_tv_put(msg, RSL_IE_CHAN_NR, channel);
2617 msgb_tv_put(msg, RSL_IE_BS_POWER, reduction); /* reduction in 2dB steps */
2618
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +02002619 msg->dst = trx->rsl_link;
Dieter Spaar16646022011-07-28 00:01:50 +02002620
2621 return abis_rsl_sendmsg(msg);
2622}
Holger Hans Peter Freyther85825352011-12-27 22:24:17 +01002623
2624/**
2625 * Release all allocated SAPIs starting from @param start and
2626 * release them with the given release mode. Once the release
2627 * confirmation arrives it will be attempted to release the
2628 * the RF channel.
2629 */
2630int rsl_release_sapis_from(struct gsm_lchan *lchan, int start,
2631 enum rsl_rel_mode release_mode)
2632{
2633 int no_sapi = 1;
2634 int sapi;
2635
2636 for (sapi = start; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
2637 uint8_t link_id;
2638 if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED)
2639 continue;
2640
2641 link_id = sapi;
2642 if (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H)
2643 link_id |= 0x40;
2644 rsl_release_request(lchan, link_id, release_mode);
2645 no_sapi = 0;
2646 }
2647
2648 return no_sapi;
2649}
Holger Hans Peter Freytherb3489392011-12-28 16:21:05 +01002650
2651int rsl_start_t3109(struct gsm_lchan *lchan)
2652{
2653 struct gsm_bts *bts = lchan->ts->trx->bts;
2654
2655 /* Disabled, mostly legacy code */
2656 if (bts->network->T3109 == 0)
2657 return -1;
2658
2659 lchan->T3109.cb = t3109_expired;
2660 lchan->T3109.data = lchan;
2661 osmo_timer_schedule(&lchan->T3109, bts->network->T3109, 0);
2662 return 0;
2663}
Holger Hans Peter Freyther006e3d82012-12-25 23:45:14 +01002664
2665/**
2666 * \brief directly RF Channel Release the lchan
2667 *
2668 * When no SAPI was allocated, directly release the logical channel. This
2669 * should only be called from chan_alloc.c on channel release handling. In
2670 * case no SAPI was established the RF Channel can be directly released,
2671 */
2672int rsl_direct_rf_release(struct gsm_lchan *lchan)
2673{
2674 int i;
2675 for (i = 0; i < ARRAY_SIZE(lchan->sapis); ++i) {
2676 if (lchan->sapis[i] != LCHAN_SAPI_UNUSED) {
2677 LOGP(DRSL, LOGL_ERROR, "%s SAPI(%d) still allocated.\n",
2678 gsm_lchan_name(lchan), i);
2679 return -1;
2680 }
2681 }
2682
2683 /* Now release it */
2684 return rsl_rf_chan_release(lchan, 0, SACCH_NONE);
2685}