blob: c4bb4bd1164a1d7f5cb50a63a24789b35299c03e [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Radio Signalling Link messages on the A-bis interface
2 * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
Harald Welte8f5e2392009-02-03 12:57:37 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
Harald Welte8470bf22008-12-25 23:28:35 +000025#include <stdlib.h>
Harald Welte52b1f982008-12-23 20:25:15 +000026#include <errno.h>
27#include <sys/types.h>
Harald Welte75099262009-02-16 21:12:08 +000028#include <netinet/in.h>
Harald Welte167df882009-02-17 14:35:45 +000029#include <arpa/inet.h>
Harald Welte52b1f982008-12-23 20:25:15 +000030
Harald Welte8470bf22008-12-25 23:28:35 +000031#include <openbsc/gsm_data.h>
32#include <openbsc/gsm_04_08.h>
Harald Welte66b6a8d2009-08-09 14:45:18 +020033#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/abis_rsl.h>
35#include <openbsc/chan_alloc.h>
Harald Welteedcc5272009-08-09 13:47:35 +020036#include <openbsc/bsc_rll.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/tlv.h>
Holger Freyther392209c2009-02-10 00:06:19 +000039#include <openbsc/paging.h>
Harald Welte167df882009-02-17 14:35:45 +000040#include <openbsc/signal.h>
Harald Welte3c7dc6e2009-11-29 19:07:28 +010041#include <openbsc/meas_rep.h>
Harald Welte52b1f982008-12-23 20:25:15 +000042
Harald Welte8470bf22008-12-25 23:28:35 +000043#define RSL_ALLOC_SIZE 1024
44#define RSL_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000045
Holger Freyther3b72a892009-02-04 00:31:39 +000046#define MAX(a, b) (a) >= (b) ? (a) : (b)
47
Harald Welte75099262009-02-16 21:12:08 +000048static const struct tlv_definition rsl_att_tlvdef = {
49 .def = {
50 [RSL_IE_CHAN_NR] = { TLV_TYPE_TV },
51 [RSL_IE_LINK_IDENT] = { TLV_TYPE_TV },
52 [RSL_IE_ACT_TYPE] = { TLV_TYPE_TV },
53 [RSL_IE_BS_POWER] = { TLV_TYPE_TV },
54 [RSL_IE_CHAN_IDENT] = { TLV_TYPE_TLV },
55 [RSL_IE_CHAN_MODE] = { TLV_TYPE_TLV },
56 [RSL_IE_ENCR_INFO] = { TLV_TYPE_TLV },
57 [RSL_IE_FRAME_NUMBER] = { TLV_TYPE_FIXED, 2 },
58 [RSL_IE_HANDO_REF] = { TLV_TYPE_TV },
59 [RSL_IE_L1_INFO] = { TLV_TYPE_FIXED, 2 },
60 [RSL_IE_L3_INFO] = { TLV_TYPE_TL16V },
61 [RSL_IE_MS_IDENTITY] = { TLV_TYPE_TLV },
62 [RSL_IE_MS_POWER] = { TLV_TYPE_TV },
63 [RSL_IE_PAGING_GROUP] = { TLV_TYPE_TV },
64 [RSL_IE_PAGING_LOAD] = { TLV_TYPE_FIXED, 2 },
65 [RSL_IE_PYHS_CONTEXT] = { TLV_TYPE_TLV },
66 [RSL_IE_ACCESS_DELAY] = { TLV_TYPE_TV },
67 [RSL_IE_RACH_LOAD] = { TLV_TYPE_TLV },
68 [RSL_IE_REQ_REFERENCE] = { TLV_TYPE_FIXED, 3 },
69 [RSL_IE_RELEASE_MODE] = { TLV_TYPE_TV },
70 [RSL_IE_RESOURCE_INFO] = { TLV_TYPE_TLV },
71 [RSL_IE_RLM_CAUSE] = { TLV_TYPE_TLV },
72 [RSL_IE_STARTNG_TIME] = { TLV_TYPE_FIXED, 2 },
73 [RSL_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
74 [RSL_IE_UPLINK_MEAS] = { TLV_TYPE_TLV },
75 [RSL_IE_CAUSE] = { TLV_TYPE_TLV },
76 [RSL_IE_MEAS_RES_NR] = { TLV_TYPE_TV },
77 [RSL_IE_MSG_ID] = { TLV_TYPE_TV },
78 [RSL_IE_SYSINFO_TYPE] = { TLV_TYPE_TV },
79 [RSL_IE_MS_POWER_PARAM] = { TLV_TYPE_TLV },
80 [RSL_IE_BS_POWER_PARAM] = { TLV_TYPE_TLV },
81 [RSL_IE_PREPROC_PARAM] = { TLV_TYPE_TLV },
82 [RSL_IE_PREPROC_MEAS] = { TLV_TYPE_TLV },
83 [RSL_IE_IMM_ASS_INFO] = { TLV_TYPE_TLV },
84 [RSL_IE_SMSCB_INFO] = { TLV_TYPE_FIXED, 23 },
85 [RSL_IE_MS_TIMING_OFFSET] = { TLV_TYPE_TV },
86 [RSL_IE_ERR_MSG] = { TLV_TYPE_TLV },
87 [RSL_IE_FULL_BCCH_INFO] = { TLV_TYPE_TLV },
88 [RSL_IE_CHAN_NEEDED] = { TLV_TYPE_TV },
89 [RSL_IE_CB_CMD_TYPE] = { TLV_TYPE_TV },
90 [RSL_IE_SMSCB_MSG] = { TLV_TYPE_TLV },
91 [RSL_IE_FULL_IMM_ASS_INFO] = { TLV_TYPE_TLV },
92 [RSL_IE_SACCH_INFO] = { TLV_TYPE_TLV },
93 [RSL_IE_CBCH_LOAD_INFO] = { TLV_TYPE_TV },
94 [RSL_IE_SMSCB_CHAN_INDICATOR] = { TLV_TYPE_TV },
95 [RSL_IE_GROUP_CALL_REF] = { TLV_TYPE_TLV },
96 [RSL_IE_CHAN_DESC] = { TLV_TYPE_TLV },
97 [RSL_IE_NCH_DRX_INFO] = { TLV_TYPE_TLV },
98 [RSL_IE_CMD_INDICATOR] = { TLV_TYPE_TLV },
99 [RSL_IE_EMLPP_PRIO] = { TLV_TYPE_TV },
100 [RSL_IE_UIC] = { TLV_TYPE_TLV },
101 [RSL_IE_MAIN_CHAN_REF] = { TLV_TYPE_TV },
102 [RSL_IE_MR_CONFIG] = { TLV_TYPE_TLV },
103 [RSL_IE_MR_CONTROL] = { TLV_TYPE_TV },
104 [RSL_IE_SUP_CODEC_TYPES] = { TLV_TYPE_TLV },
105 [RSL_IE_CODEC_CONFIG] = { TLV_TYPE_TLV },
106 [RSL_IE_RTD] = { TLV_TYPE_TV },
107 [RSL_IE_TFO_STATUS] = { TLV_TYPE_TV },
108 [RSL_IE_LLP_APDU] = { TLV_TYPE_TLV },
Harald Welte67fa91b2009-08-10 09:51:40 +0200109 [RSL_IE_SIEMENS_MRPCI] = { TLV_TYPE_TV },
Harald Welteab46d742009-07-12 09:56:39 +0200110 [RSL_IE_IPAC_PROXY_UDP] = { TLV_TYPE_FIXED, 2 },
111 [RSL_IE_IPAC_BSCMPL_TOUT] = { TLV_TYPE_TV },
Harald Welte75099262009-02-16 21:12:08 +0000112 [RSL_IE_IPAC_REMOTE_IP] = { TLV_TYPE_FIXED, 4 },
113 [RSL_IE_IPAC_REMOTE_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welteab46d742009-07-12 09:56:39 +0200114 [RSL_IE_IPAC_RTP_PAYLOAD] = { TLV_TYPE_TV },
Harald Welte75099262009-02-16 21:12:08 +0000115 [RSL_IE_IPAC_LOCAL_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welte86c162d2009-07-12 09:45:05 +0200116 [RSL_IE_IPAC_SPEECH_MODE] = { TLV_TYPE_TV },
Harald Welteab46d742009-07-12 09:56:39 +0200117 [RSL_IE_IPAC_LOCAL_IP] = { TLV_TYPE_FIXED, 4 },
Harald Welte86c162d2009-07-12 09:45:05 +0200118 [RSL_IE_IPAC_CONN_ID] = { TLV_TYPE_FIXED, 2 },
Harald Welteab46d742009-07-12 09:56:39 +0200119 [RSL_IE_IPAC_RTP_CSD_FMT] = { TLV_TYPE_TV },
120 [RSL_IE_IPAC_RTP_JIT_BUF] = { TLV_TYPE_FIXED, 2 },
121 [RSL_IE_IPAC_RTP_COMPR] = { TLV_TYPE_TV },
Harald Welte86c162d2009-07-12 09:45:05 +0200122 [RSL_IE_IPAC_RTP_PAYLOAD2] = { TLV_TYPE_TV },
Harald Welteab46d742009-07-12 09:56:39 +0200123 [RSL_IE_IPAC_RTP_MPLEX] = { TLV_TYPE_FIXED, 8 },
124 [RSL_IE_IPAC_RTP_MPLEX_ID] = { TLV_TYPE_TV },
Harald Welte75099262009-02-16 21:12:08 +0000125 },
126};
Harald Weltea4d49e92009-05-23 06:39:58 +0000127#define rsl_tlv_parse(dec, buf, len) \
128 tlv_parse(dec, &rsl_att_tlvdef, buf, len, 0, 0)
Harald Welte75099262009-02-16 21:12:08 +0000129
Harald Welte52b1f982008-12-23 20:25:15 +0000130static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
131{
132 /* mask off the transparent bit ? */
133 msg_type &= 0xfe;
134
Harald Welte8470bf22008-12-25 23:28:35 +0000135 if ((msg_type & 0xf0) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +0000136 return ABIS_RSL_MDISC_RLL;
Harald Welte8470bf22008-12-25 23:28:35 +0000137 if ((msg_type & 0xf0) == 0x10) {
Harald Welte52b1f982008-12-23 20:25:15 +0000138 if (msg_type >= 0x19 && msg_type <= 0x22)
139 return ABIS_RSL_MDISC_TRX;
140 else
141 return ABIS_RSL_MDISC_COM_CHAN;
142 }
Harald Welte2d5b6382008-12-27 19:46:06 +0000143 if ((msg_type & 0xe0) == 0x20)
Harald Welte52b1f982008-12-23 20:25:15 +0000144 return ABIS_RSL_MDISC_DED_CHAN;
145
146 return ABIS_RSL_MDISC_LOC;
147}
148
149static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
150 u_int8_t msg_type)
151{
152 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
153 dh->c.msg_type = msg_type;
154 dh->ie_chan = RSL_IE_CHAN_NR;
155}
156
157static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
158 u_int8_t msg_type)
159{
160 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
161 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
162 dh->c.msg_type = msg_type;
163 dh->ie_chan = RSL_IE_CHAN_NR;
164 dh->ie_link_id = RSL_IE_LINK_IDENT;
165}
166
167
168/* encode channel number as per Section 9.3.1 */
169u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
170{
171 u_int8_t ret;
172
173 ret = (timeslot & 0x07) | type;
174
175 switch (type) {
176 case RSL_CHAN_Lm_ACCHs:
177 subch &= 0x01;
178 break;
179 case RSL_CHAN_SDCCH4_ACCH:
180 subch &= 0x07;
181 break;
182 case RSL_CHAN_SDCCH8_ACCH:
183 subch &= 0x07;
184 break;
185 default:
186 /* no subchannels allowed */
187 subch = 0x00;
188 break;
189 }
190 ret |= (subch << 3);
191
192 return ret;
193}
194
Harald Welte8470bf22008-12-25 23:28:35 +0000195/* determine logical channel based on TRX and channel number IE */
196struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
197{
198 struct gsm_lchan *lchan;
199 u_int8_t ts_nr = chan_nr & 0x07;
200 u_int8_t cbits = chan_nr >> 3;
201 u_int8_t lch_idx;
202 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
203
204 if (cbits == 0x01) {
205 lch_idx = 0; /* TCH/F */
Harald Weltea1499d02009-10-24 10:25:50 +0200206 if (ts->pchan != GSM_PCHAN_TCH_F &&
207 ts->pchan != GSM_PCHAN_PDCH &&
208 ts->pchan != GSM_PCHAN_TCH_F_PDCH)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100209 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000210 chan_nr, ts->pchan);
211 } else if ((cbits & 0x1e) == 0x02) {
212 lch_idx = cbits & 0x1; /* TCH/H */
213 if (ts->pchan != GSM_PCHAN_TCH_H)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100214 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000215 chan_nr, ts->pchan);
216 } else if ((cbits & 0x1c) == 0x04) {
217 lch_idx = cbits & 0x3; /* SDCCH/4 */
218 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100219 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000220 chan_nr, ts->pchan);
221 } else if ((cbits & 0x18) == 0x08) {
222 lch_idx = cbits & 0x7; /* SDCCH/8 */
223 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100224 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000225 chan_nr, ts->pchan);
226 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
227 lch_idx = 0;
228 if (ts->pchan != GSM_PCHAN_CCCH &&
229 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100230 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000231 chan_nr, ts->pchan);
232 /* FIXME: we should not return first sdcch4 !!! */
233 } else {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100234 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000235 return NULL;
236 }
237
238 lchan = &ts->lchan[lch_idx];
239
240 return lchan;
241}
242
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200243/* See Table 10.5.25 of GSM04.08 */
Harald Welte24766092009-12-09 19:18:32 +0100244u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte8470bf22008-12-25 23:28:35 +0000245{
246 struct gsm_bts_trx_ts *ts = lchan->ts;
247 u_int8_t cbits, chan_nr;
248
249 switch (ts->pchan) {
250 case GSM_PCHAN_TCH_F:
Harald Weltea1499d02009-10-24 10:25:50 +0200251 case GSM_PCHAN_PDCH:
252 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte8470bf22008-12-25 23:28:35 +0000253 cbits = 0x01;
254 break;
255 case GSM_PCHAN_TCH_H:
256 cbits = 0x02;
257 cbits += lchan->nr;
258 break;
259 case GSM_PCHAN_CCCH_SDCCH4:
260 cbits = 0x04;
261 cbits += lchan->nr;
262 break;
263 case GSM_PCHAN_SDCCH8_SACCH8C:
264 cbits = 0x08;
265 cbits += lchan->nr;
266 break;
267 default:
268 case GSM_PCHAN_CCCH:
269 cbits = 0x10;
270 break;
271 }
272
273 chan_nr = (cbits << 3) | (ts->nr & 0x7);
274
275 return chan_nr;
276}
277
Harald Welte52b1f982008-12-23 20:25:15 +0000278/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
279u_int64_t str_to_imsi(const char *imsi_str)
280{
281 u_int64_t ret;
282
283 ret = strtoull(imsi_str, NULL, 10);
284
285 return ret;
286}
287
288/* Table 5 Clause 7 TS 05.02 */
289unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
290{
291 if (!bs_ccch_sdcch_comb)
292 return 9 - bs_ag_blks_res;
293 else
294 return 3 - bs_ag_blks_res;
295}
296
297/* Chapter 6.5.2 of TS 05.02 */
298unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
299 unsigned int n_pag_blocks)
300{
301 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
302}
303
304/* Chapter 6.5.2 of TS 05.02 */
305unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
306 int n_pag_blocks)
307{
308 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
309}
310
Harald Welte8470bf22008-12-25 23:28:35 +0000311static struct msgb *rsl_msgb_alloc(void)
312{
Harald Welte966636f2009-06-26 19:39:35 +0200313 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
314 "RSL");
Harald Welte8470bf22008-12-25 23:28:35 +0000315}
316
Harald Welte362322e2009-02-15 14:36:38 +0000317#define MACBLOCK_SIZE 23
318static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
319{
320 memcpy(out, in, len);
321
322 if (len < MACBLOCK_SIZE)
323 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
324}
325
Harald Welte08d91a52009-08-30 15:37:11 +0900326/* Chapter 9.3.7: Encryption Information */
327static int build_encr_info(u_int8_t *out, struct gsm_lchan *lchan)
328{
329 *out++ = lchan->encr.alg_id & 0xff;
330 if (lchan->encr.key_len)
331 memcpy(out, lchan->encr.key, lchan->encr.key_len);
332 return lchan->encr.key_len + 1;
333}
334
335
Harald Welte8830e072009-07-28 17:58:09 +0200336static const char *rsl_err_vals[0xff] = {
337 [RSL_ERR_RADIO_IF_FAIL] = "Radio Interface Failure",
338 [RSL_ERR_RADIO_LINK_FAIL] = "Radio Link Failure",
339 [RSL_ERR_HANDOVER_ACC_FAIL] = "Handover Access Failure",
340 [RSL_ERR_TALKER_ACC_FAIL] = "Talker Access Failure",
341 [RSL_ERR_OM_INTERVENTION] = "O&M Intervention",
342 [RSL_ERR_NORMAL_UNSPEC] = "Normal event, unspecified",
Harald Welte2da86292009-08-04 02:31:05 +0200343 [RSL_ERR_T_MSRFPCI_EXP] = "Siemens: T_MSRFPCI Expired",
Harald Welte8830e072009-07-28 17:58:09 +0200344 [RSL_ERR_EQUIPMENT_FAIL] = "Equipment Failure",
345 [RSL_ERR_RR_UNAVAIL] = "Radio Resource not available",
346 [RSL_ERR_TERR_CH_FAIL] = "Terrestrial Channel Failure",
347 [RSL_ERR_CCCH_OVERLOAD] = "CCCH Overload",
348 [RSL_ERR_ACCH_OVERLOAD] = "ACCH Overload",
349 [RSL_ERR_PROCESSOR_OVERLOAD] = "Processor Overload",
350 [RSL_ERR_RES_UNAVAIL] = "Resource not available, unspecified",
351 [RSL_ERR_TRANSC_UNAVAIL] = "Transcoding not available",
352 [RSL_ERR_SERV_OPT_UNAVAIL] = "Service or Option not available",
353 [RSL_ERR_ENCR_UNIMPL] = "Encryption algorithm not implemented",
354 [RSL_ERR_SERV_OPT_UNIMPL] = "Service or Option not implemented",
355 [RSL_ERR_RCH_ALR_ACTV_ALLOC] = "Radio channel already activated",
356 [RSL_ERR_INVALID_MESSAGE] = "Invalid Message, unspecified",
357 [RSL_ERR_MSG_DISCR] = "Message Discriminator Error",
358 [RSL_ERR_MSG_TYPE] = "Message Type Error",
359 [RSL_ERR_MSG_SEQ] = "Message Sequence Error",
360 [RSL_ERR_IE_ERROR] = "General IE error",
361 [RSL_ERR_MAND_IE_ERROR] = "Mandatory IE error",
362 [RSL_ERR_OPT_IE_ERROR] = "Optional IE error",
363 [RSL_ERR_IE_NONEXIST] = "IE non-existent",
364 [RSL_ERR_IE_LENGTH] = "IE length error",
365 [RSL_ERR_IE_CONTENT] = "IE content error",
366 [RSL_ERR_PROTO] = "Protocol error, unspecified",
367 [RSL_ERR_INTERWORKING] = "Interworking error, unspecified",
368};
369
370static const char *rsl_err_name(u_int8_t err)
Harald Welte7f93cea2009-02-23 00:02:59 +0000371{
Harald Welte8830e072009-07-28 17:58:09 +0200372 if (rsl_err_vals[err])
373 return rsl_err_vals[err];
374 else
375 return "unknown";
376}
377
378static void print_rsl_cause(const u_int8_t *cause_v, u_int8_t cause_len)
379{
Harald Welte7f93cea2009-02-23 00:02:59 +0000380 int i;
381
Harald Welte8830e072009-07-28 17:58:09 +0200382 DEBUGPC(DRSL, "CAUSE=0x%02x(%s) ",
383 cause_v[0], rsl_err_name(cause_v[0]));
384 for (i = 1; i < cause_len-1; i++)
385 DEBUGPC(DRSL, "%02x ", cause_v[i]);
Harald Welte7f93cea2009-02-23 00:02:59 +0000386}
387
Harald Welte52b1f982008-12-23 20:25:15 +0000388/* Send a BCCH_INFO message as per Chapter 8.5.1 */
Harald Weltee79769b2009-02-07 00:48:17 +0000389int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000390 const u_int8_t *data, int len)
391{
392 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000393 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000394
395 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
396 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
397 dh->chan_nr = RSL_CHAN_BCCH;
398
399 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
400 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
401
Harald Weltee79769b2009-02-07 00:48:17 +0000402 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000403
404 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000405}
406
Harald Weltee79769b2009-02-07 00:48:17 +0000407int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000408 const u_int8_t *data, int len)
409{
410 struct abis_rsl_common_hdr *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000411 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000412
413 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
414 ch->msg_discr = ABIS_RSL_MDISC_TRX;
415 ch->msg_type = RSL_MT_SACCH_FILL;
416
417 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
Harald Welte702d8702008-12-26 20:25:35 +0000418 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
Harald Welte52b1f982008-12-23 20:25:15 +0000419
Harald Weltee79769b2009-02-07 00:48:17 +0000420 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000421
422 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000423}
424
Harald Weltefcd24452009-06-20 18:15:19 +0200425int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
426{
427 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200428 struct msgb *msg;
Harald Weltefcd24452009-06-20 18:15:19 +0200429 u_int8_t chan_nr = lchan2chan_nr(lchan);
430
431 db = abs(db);
432 if (db > 30)
433 return -EINVAL;
434
Harald Welteeab33352009-06-27 03:09:08 +0200435 msg = rsl_msgb_alloc();
436
Harald Weltefcd24452009-06-20 18:15:19 +0200437 lchan->bs_power = db/2;
438 if (fpc)
439 lchan->bs_power |= 0x10;
440
441 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
442 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
443 dh->chan_nr = chan_nr;
444
445 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
446
447 msg->trx = lchan->ts->trx;
448
449 return abis_rsl_sendmsg(msg);
450}
451
Harald Weltefcd24452009-06-20 18:15:19 +0200452int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
453{
454 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200455 struct msgb *msg;
Harald Weltefcd24452009-06-20 18:15:19 +0200456 u_int8_t chan_nr = lchan2chan_nr(lchan);
457 int ctl_lvl;
458
Harald Welte66b6a8d2009-08-09 14:45:18 +0200459 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Weltefcd24452009-06-20 18:15:19 +0200460 if (ctl_lvl < 0)
461 return ctl_lvl;
462
Harald Welteeab33352009-06-27 03:09:08 +0200463 msg = rsl_msgb_alloc();
464
Harald Weltefcd24452009-06-20 18:15:19 +0200465 lchan->ms_power = ctl_lvl;
466
467 if (fpc)
468 lchan->ms_power |= 0x20;
469
470 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
471 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
472 dh->chan_nr = chan_nr;
473
474 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
475
476 msg->trx = lchan->ts->trx;
477
478 return abis_rsl_sendmsg(msg);
479}
480
Harald Welte9943c5b2009-07-29 15:41:29 +0200481static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
482 struct gsm_lchan *lchan)
483{
484 memset(cm, 0, sizeof(cm));
485
486 /* FIXME: what to do with data calls ? */
487 cm->dtx_dtu = 0x00;
488
489 /* set TCH Speech/Data */
490 cm->spd_ind = lchan->rsl_cmode;
491
Harald Welte1a79d362009-11-27 08:55:16 +0100492 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
493 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100494 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte1a79d362009-11-27 08:55:16 +0100495 "but tch_mode != signalling\n");
496
Harald Welte9943c5b2009-07-29 15:41:29 +0200497 switch (lchan->type) {
498 case GSM_LCHAN_SDCCH:
499 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
500 break;
501 case GSM_LCHAN_TCH_F:
502 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
503 break;
504 case GSM_LCHAN_TCH_H:
505 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
506 break;
507 case GSM_LCHAN_NONE:
508 case GSM_LCHAN_UNKNOWN:
509 default:
510 return -EINVAL;
511 }
512
513 switch (lchan->tch_mode) {
514 case GSM48_CMODE_SIGN:
515 cm->chan_rate = 0;
516 break;
517 case GSM48_CMODE_SPEECH_V1:
518 cm->chan_rate = RSL_CMOD_SP_GSM1;
519 break;
520 case GSM48_CMODE_SPEECH_EFR:
521 cm->chan_rate = RSL_CMOD_SP_GSM2;
522 break;
523 case GSM48_CMODE_SPEECH_AMR:
524 cm->chan_rate = RSL_CMOD_SP_GSM3;
525 break;
526 case GSM48_CMODE_DATA_14k5:
527 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
528 break;
529 case GSM48_CMODE_DATA_12k0:
530 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
531 break;
532 case GSM48_CMODE_DATA_6k0:
533 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
534 break;
535 default:
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
Harald Welte52b1f982008-12-23 20:25:15 +0000542/* Chapter 8.4.1 */
Harald Welteddab3c72009-02-28 13:19:15 +0000543#if 0
Harald Weltee79769b2009-02-07 00:48:17 +0000544int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
Harald Welte52b1f982008-12-23 20:25:15 +0000545 u_int8_t act_type,
546 struct rsl_ie_chan_mode *chan_mode,
547 struct rsl_ie_chan_ident *chan_ident,
548 u_int8_t bs_power, u_int8_t ms_power,
549 u_int8_t ta)
550{
551 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000552 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000553
554 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
555 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
556 dh->chan_nr = chan_nr;
557
558 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
559 /* For compatibility with Phase 1 */
560 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
561 (u_int8_t *) chan_mode);
562 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
Harald Welte702d8702008-12-26 20:25:35 +0000563 (u_int8_t *) chan_ident);
Harald Welte702d8702008-12-26 20:25:35 +0000564#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000565 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
566 (u_int8_t *) &encr_info);
Harald Welte702d8702008-12-26 20:25:35 +0000567#endif
Harald Welted4c9bf32009-02-15 16:56:18 +0000568 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
Harald Welte52b1f982008-12-23 20:25:15 +0000569 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
570 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
571
Harald Weltee79769b2009-02-07 00:48:17 +0000572 msg->trx = trx;
573
Harald Welte8470bf22008-12-25 23:28:35 +0000574 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000575}
Harald Welteddab3c72009-02-28 13:19:15 +0000576#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000577
Harald Welte8f5e2392009-02-03 12:57:37 +0000578int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welte8d77b952009-12-17 00:31:10 +0100579 u_int8_t ta, u_int8_t ho_ref)
Harald Welte4b634542008-12-27 01:55:51 +0000580{
581 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200582 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200583 int rc;
Harald Welte4b634542008-12-27 01:55:51 +0000584
585 u_int8_t chan_nr = lchan2chan_nr(lchan);
586 u_int16_t arfcn = lchan->ts->trx->arfcn;
587 struct rsl_ie_chan_mode cm;
588 struct rsl_ie_chan_ident ci;
589
Harald Welte9943c5b2009-07-29 15:41:29 +0200590 rc = channel_mode_from_lchan(&cm, lchan);
591 if (rc < 0)
592 return rc;
Harald Welte4b634542008-12-27 01:55:51 +0000593
Harald Welte02b0e092009-02-28 13:11:07 +0000594 memset(&ci, 0, sizeof(ci));
Harald Welte4b634542008-12-27 01:55:51 +0000595 ci.chan_desc.iei = 0x64;
596 ci.chan_desc.chan_nr = chan_nr;
Harald Welte02b0e092009-02-28 13:11:07 +0000597 ci.chan_desc.oct3 = (lchan->ts->trx->bts->tsc << 5) | ((arfcn & 0x3ff) >> 8);
Harald Welte4b634542008-12-27 01:55:51 +0000598 ci.chan_desc.oct4 = arfcn & 0xff;
599
Harald Welteeab33352009-06-27 03:09:08 +0200600 msg = rsl_msgb_alloc();
Harald Welte4b634542008-12-27 01:55:51 +0000601 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
602 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
603 dh->chan_nr = chan_nr;
604
605 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte4b634542008-12-27 01:55:51 +0000606 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
607 (u_int8_t *) &cm);
Harald Welte8d77b952009-12-17 00:31:10 +0100608 /* For compatibility with Phase 1 */
Harald Welte4b634542008-12-27 01:55:51 +0000609 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
610 (u_int8_t *) &ci);
Harald Welte08d91a52009-08-30 15:37:11 +0900611
612 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
613 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
614 rc = build_encr_info(encr_info, lchan);
615 if (rc > 0)
616 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
617 }
618
Harald Welte8d77b952009-12-17 00:31:10 +0100619 switch (act_type) {
620 case RSL_ACT_INTER_ASYNC:
621 case RSL_ACT_INTER_SYNC:
622 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
623 break;
624 default:
625 break;
626 }
627
Harald Welted4c9bf32009-02-15 16:56:18 +0000628 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
629 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
Harald Welte4b634542008-12-27 01:55:51 +0000630 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
631
Harald Weltee79769b2009-02-07 00:48:17 +0000632 msg->trx = lchan->ts->trx;
633
Harald Welte4b634542008-12-27 01:55:51 +0000634 return abis_rsl_sendmsg(msg);
635}
636
Harald Welte470abb72009-07-29 11:38:15 +0200637/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welteda783762009-02-18 03:29:53 +0000638int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
639{
640 struct abis_rsl_dchan_hdr *dh;
Harald Welteeab33352009-06-27 03:09:08 +0200641 struct msgb *msg;
Harald Welte9943c5b2009-07-29 15:41:29 +0200642 int rc;
Harald Welteda783762009-02-18 03:29:53 +0000643
644 u_int8_t chan_nr = lchan2chan_nr(lchan);
645 struct rsl_ie_chan_mode cm;
646
Harald Welte9943c5b2009-07-29 15:41:29 +0200647 rc = channel_mode_from_lchan(&cm, lchan);
648 if (rc < 0)
649 return rc;
Harald Welteda783762009-02-18 03:29:53 +0000650
Harald Welteeab33352009-06-27 03:09:08 +0200651 msg = rsl_msgb_alloc();
Harald Welteda783762009-02-18 03:29:53 +0000652 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
653 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
654 dh->chan_nr = chan_nr;
655
656 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
657 (u_int8_t *) &cm);
Harald Welte08d91a52009-08-30 15:37:11 +0900658
659 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
660 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
661 rc = build_encr_info(encr_info, lchan);
662 if (rc > 0)
663 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
664 }
665
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100666 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
667 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
668 (u_int8_t *) &lchan->mr_conf);
669 }
670
Harald Welte08d91a52009-08-30 15:37:11 +0900671 msg->trx = lchan->ts->trx;
672
673 return abis_rsl_sendmsg(msg);
674}
675
676/* Chapter 8.4.6: Send the encryption command with given L3 info */
677int rsl_encryption_cmd(struct msgb *msg)
678{
679 struct abis_rsl_dchan_hdr *dh;
680 struct gsm_lchan *lchan = msg->lchan;
681 u_int8_t chan_nr = lchan2chan_nr(lchan);
682 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut82aa6842009-09-27 11:13:18 +0200683 u_int8_t l3_len = msg->len;
Harald Welte08d91a52009-08-30 15:37:11 +0900684 int rc;
685
686 /* First push the L3 IE tag and length */
687 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
688
689 /* then the link identifier (SAPI0, main sign link) */
690 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
691
692 /* then encryption information */
693 rc = build_encr_info(encr_info, lchan);
694 if (rc <= 0)
695 return rc;
696 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
697
698 /* and finally the DCHAN header */
699 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
700 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
701 dh->chan_nr = chan_nr;
Harald Welteda783762009-02-18 03:29:53 +0000702
703 msg->trx = lchan->ts->trx;
704
705 return abis_rsl_sendmsg(msg);
706}
707
Harald Welte115d1032009-08-10 11:43:22 +0200708/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteae0f2362009-07-19 18:36:49 +0200709int rsl_deact_sacch(struct gsm_lchan *lchan)
710{
711 struct abis_rsl_dchan_hdr *dh;
712 struct msgb *msg = rsl_msgb_alloc();
713
714 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
715 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
716 dh->chan_nr = lchan2chan_nr(lchan);
717
718 msg->lchan = lchan;
719 msg->trx = lchan->ts->trx;
720
721 DEBUGP(DRSL, "DEACTivate SACCH CMD channel=%s chan_nr=0x%02x\n",
722 gsm_ts_name(lchan->ts), dh->chan_nr);
723
724 return abis_rsl_sendmsg(msg);
725}
726
Harald Welte115d1032009-08-10 11:43:22 +0200727/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
728int rsl_rf_chan_release(struct gsm_lchan *lchan)
Harald Welte52b1f982008-12-23 20:25:15 +0000729{
730 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000731 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000732
733 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
734 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
Harald Welte8470bf22008-12-25 23:28:35 +0000735 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000736
Harald Welte8470bf22008-12-25 23:28:35 +0000737 msg->lchan = lchan;
738 msg->trx = lchan->ts->trx;
739
Harald Welte115d1032009-08-10 11:43:22 +0200740 DEBUGP(DRSL, "RF Channel Release CMD channel=%s chan_nr=0x%02x\n",
Harald Weltef325eb42009-02-19 17:07:39 +0000741 gsm_ts_name(lchan->ts), dh->chan_nr);
Harald Welte2d5b6382008-12-27 19:46:06 +0000742
Harald Welte115d1032009-08-10 11:43:22 +0200743 /* BTS will respond by RF CHAN REL ACK */
Harald Welte8470bf22008-12-25 23:28:35 +0000744 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000745}
746
747int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
748 u_int8_t *ms_ident, u_int8_t chan_needed)
749{
750 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000751 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000752
753 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
754 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
755 dh->chan_nr = RSL_CHAN_PCH_AGCH;
756
757 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
Harald Welte255539c2008-12-28 02:26:27 +0000758 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
Harald Welte52b1f982008-12-23 20:25:15 +0000759 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
760
Harald Welte8470bf22008-12-25 23:28:35 +0000761 msg->trx = bts->c0;
762
763 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000764}
765
Holger Freyther7448a532009-01-04 20:18:23 +0000766int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
767 struct gsm_subscriber *subscr)
768{
Holger Freytherca362a62009-01-04 21:05:01 +0000769#if 0
Holger Freyther7448a532009-01-04 20:18:23 +0000770 u_int8_t mi[128];
771 unsigned int mi_len;
772 u_int8_t paging_group;
Holger Freytherca362a62009-01-04 21:05:01 +0000773#endif
Holger Freyther7448a532009-01-04 20:18:23 +0000774
775 return -1;
776}
777
Harald Welte52b1f982008-12-23 20:25:15 +0000778int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
779{
780 int i, len = strlen(str_in);
781
782 for (i = 0; i < len; i++) {
783 int num = str_in[i] - 0x30;
784 if (num < 0 || num > 9)
785 return -1;
786 if (i % 2 == 0)
787 bcd_out[i/2] = num;
788 else
789 bcd_out[i/2] |= (num << 4);
790 }
791
792 return 0;
793}
794
Harald Welte702d8702008-12-26 20:25:35 +0000795/* Chapter 8.5.6 */
Harald Welte52b1f982008-12-23 20:25:15 +0000796int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
797{
Harald Welte8470bf22008-12-25 23:28:35 +0000798 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000799 struct abis_rsl_dchan_hdr *dh;
Harald Welte362322e2009-02-15 14:36:38 +0000800 u_int8_t buf[MACBLOCK_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000801
802 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
803 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
804 dh->chan_nr = RSL_CHAN_PCH_AGCH;
805
Harald Welte362322e2009-02-15 14:36:38 +0000806 switch (bts->type) {
807 case GSM_BTS_TYPE_BS11:
808 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
809 break;
810 default:
811 /* If phase 2, construct a FULL_IMM_ASS_INFO */
812 pad_macblock(buf, val, len);
813 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
814 break;
815 }
Harald Welte52b1f982008-12-23 20:25:15 +0000816
Harald Welte8470bf22008-12-25 23:28:35 +0000817 msg->trx = bts->c0;
818
819 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000820}
821
Harald Welte67fa91b2009-08-10 09:51:40 +0200822/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte31c48932009-08-10 10:07:33 +0200823int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte67fa91b2009-08-10 09:51:40 +0200824{
825 struct msgb *msg = rsl_msgb_alloc();
826 struct abis_rsl_dchan_hdr *dh;
827
828 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
829 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte3c456d02009-08-10 11:26:14 +0200830 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte67fa91b2009-08-10 09:51:40 +0200831 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte31c48932009-08-10 10:07:33 +0200832 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte67fa91b2009-08-10 09:51:40 +0200833
Harald Welte3c456d02009-08-10 11:26:14 +0200834 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x TX Siemens MRPCI 0x%02x\n",
835 gsm_ts_name(lchan->ts), dh->chan_nr, *(u_int8_t *)mrpci);
836
837 msg->trx = lchan->ts->trx;
838
Harald Welte67fa91b2009-08-10 09:51:40 +0200839 return abis_rsl_sendmsg(msg);
840}
841
842
Harald Welte8470bf22008-12-25 23:28:35 +0000843/* Send "DATA REQUEST" message with given L3 Info payload */
Harald Welte52b1f982008-12-23 20:25:15 +0000844/* Chapter 8.3.1 */
Harald Welte8470bf22008-12-25 23:28:35 +0000845int rsl_data_request(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +0000846{
Harald Welte8470bf22008-12-25 23:28:35 +0000847 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000848 struct abis_rsl_rll_hdr *rh;
849
Harald Welte8470bf22008-12-25 23:28:35 +0000850 if (msg->lchan == NULL) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100851 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000852 return -EINVAL;
853 }
Harald Welte52b1f982008-12-23 20:25:15 +0000854
Harald Welte8470bf22008-12-25 23:28:35 +0000855 /* First push the L3 IE tag and length */
Harald Welte4b634542008-12-27 01:55:51 +0000856 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welte8470bf22008-12-25 23:28:35 +0000857
858 /* Then push the RSL header */
Harald Welte52b1f982008-12-23 20:25:15 +0000859 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
860 init_llm_hdr(rh, RSL_MT_DATA_REQ);
Harald Welte4a543e82009-02-28 13:17:55 +0000861 rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Harald Welte8470bf22008-12-25 23:28:35 +0000862 rh->chan_nr = lchan2chan_nr(msg->lchan);
863 rh->link_id = link_id;
Harald Welte52b1f982008-12-23 20:25:15 +0000864
Harald Welte8470bf22008-12-25 23:28:35 +0000865 msg->trx = msg->lchan->ts->trx;
866
867 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000868}
869
Harald Welteedcc5272009-08-09 13:47:35 +0200870/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
871/* Chapter 8.3.1 */
872int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
873{
874 struct msgb *msg = rsl_msgb_alloc();
875 struct abis_rsl_rll_hdr *rh;
876
877 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
Harald Welte1c409272009-08-09 14:13:58 +0200878 init_llm_hdr(rh, RSL_MT_EST_REQ);
Harald Welteedcc5272009-08-09 13:47:35 +0200879 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
880 rh->chan_nr = lchan2chan_nr(lchan);
881 rh->link_id = link_id;
882
883 msg->trx = lchan->ts->trx;
884
885 return abis_rsl_sendmsg(msg);
886}
887
Harald Welted2dc1de2009-08-08 13:15:07 +0200888/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
889 This is what higher layers should call. The BTS then responds with
890 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
891 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
892 lchan_free() */
893int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id)
894{
895 struct msgb *msg = rsl_msgb_alloc();
896 struct abis_rsl_rll_hdr *rh;
897
898 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
899 init_llm_hdr(rh, RSL_MT_REL_REQ);
900 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
901 rh->chan_nr = lchan2chan_nr(lchan);
902 rh->link_id = link_id;
Harald Welte6c3d2ed2009-08-10 00:19:36 +0200903 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, 0); /* normal release */
Harald Welted2dc1de2009-08-08 13:15:07 +0200904
905 msg->trx = lchan->ts->trx;
906
907 return abis_rsl_sendmsg(msg);
908}
909
Harald Welte702d8702008-12-26 20:25:35 +0000910/* Chapter 8.4.2: Channel Activate Acknowledge */
911static int rsl_rx_chan_act_ack(struct msgb *msg)
912{
913 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
914
915 /* BTS has confirmed channel activation, we now need
916 * to assign the activated channel to the MS */
Harald Welte4b634542008-12-27 01:55:51 +0000917 if (rslh->ie_chan != RSL_IE_CHAN_NR)
918 return -EINVAL;
Harald Welted011e8b2009-11-29 22:45:52 +0100919
920 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
921
Harald Welte4b634542008-12-27 01:55:51 +0000922 return 0;
923}
Harald Welte702d8702008-12-26 20:25:35 +0000924
Harald Welte4b634542008-12-27 01:55:51 +0000925/* Chapter 8.4.3: Channel Activate NACK */
926static int rsl_rx_chan_act_nack(struct msgb *msg)
927{
Harald Welte6dab0552009-05-01 17:21:37 +0000928 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
929 struct tlv_parsed tp;
Harald Welte4b634542008-12-27 01:55:51 +0000930
Harald Welte6dab0552009-05-01 17:21:37 +0000931 /* BTS has rejected channel activation ?!? */
932 if (dh->ie_chan != RSL_IE_CHAN_NR)
Harald Welte4b634542008-12-27 01:55:51 +0000933 return -EINVAL;
Harald Welte6dab0552009-05-01 17:21:37 +0000934
935 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
936 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Welte8830e072009-07-28 17:58:09 +0200937 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
938 TLVP_LEN(&tp, RSL_IE_CAUSE));
939
Harald Welted011e8b2009-11-29 22:45:52 +0100940 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
941
Harald Welte3073a9f2009-08-09 19:50:08 +0200942 lchan_free(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000943 return 0;
Harald Welte702d8702008-12-26 20:25:35 +0000944}
945
Harald Welte7f93cea2009-02-23 00:02:59 +0000946/* Chapter 8.4.4: Connection Failure Indication */
947static int rsl_rx_conn_fail(struct msgb *msg)
948{
949 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
950 struct tlv_parsed tp;
951
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100952 /* FIXME: print which channel */
953 LOGP(DRSL, LOGL_NOTICE, "CONNECTION FAIL: RELEASING\n");
Harald Welte7f93cea2009-02-23 00:02:59 +0000954
955 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
956
Harald Welte8830e072009-07-28 17:58:09 +0200957 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
958 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
959 TLVP_LEN(&tp, RSL_IE_CAUSE));
960
Harald Welte7f93cea2009-02-23 00:02:59 +0000961 /* FIXME: only free it after channel release ACK */
Harald Welte115d1032009-08-10 11:43:22 +0200962 return rsl_rf_chan_release(msg->lchan);
Harald Welte7f93cea2009-02-23 00:02:59 +0000963}
964
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100965static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
966 const char *prefix)
967{
Harald Welte6739dfb2009-12-16 16:52:07 +0100968 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
969 prefix, rxlev2dbm(mru->full.rx_lev),
970 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100971 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
972 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
973}
974
975static void print_meas_rep(struct gsm_meas_rep *mr)
976{
Harald Welte6739dfb2009-12-16 16:52:07 +0100977 int i;
978
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100979 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
980
981 if (mr->flags & MEAS_REP_F_DL_DTX)
982 DEBUGPC(DMEAS, "DTXd ");
983
984 print_meas_rep_uni(&mr->ul, "ul");
985 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
986 if (mr->flags & MEAS_REP_F_MS_TO)
987 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
988
989 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte6739dfb2009-12-16 16:52:07 +0100990 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100991 DEBUGPC(DMEAS, "L1_FPC=%u ",
992 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
993 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
994 }
995
996 if (mr->flags & MEAS_REP_F_UL_DTX)
997 DEBUGPC(DMEAS, "DTXu ");
998 if (mr->flags & MEAS_REP_F_BA1)
999 DEBUGPC(DMEAS, "BA1 ");
1000 if (!(mr->flags & MEAS_REP_F_DL_VALID))
1001 DEBUGPC(DMEAS, "NOT VALID ");
1002 else
1003 print_meas_rep_uni(&mr->dl, "dl");
1004
1005 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte6739dfb2009-12-16 16:52:07 +01001006 for (i = 0; i < mr->num_cell; i++) {
1007 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
1008 DEBUGP(DMEAS, "ARFCN=%u BSIC=%u => %d dBm\n", mrc->arfcn, mrc->bsic,
1009 rxlev2dbm(mrc->rxlev));
1010 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001011}
1012
Harald Welte440fed02009-05-01 18:43:47 +00001013static int rsl_rx_meas_res(struct msgb *msg)
1014{
1015 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1016 struct tlv_parsed tp;
Harald Welted12b0fd2009-12-15 21:36:05 +01001017 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001018 u_int8_t len;
1019 const u_int8_t *val;
1020 int rc;
Harald Welte440fed02009-05-01 18:43:47 +00001021
Harald Welted12b0fd2009-12-15 21:36:05 +01001022 memset(mr, 0, sizeof(*mr));
Harald Welte33e65972009-12-16 23:29:34 +01001023 mr->lchan = msg->lchan;
Harald Weltedbb1d882009-11-30 19:16:47 +01001024
Harald Welte440fed02009-05-01 18:43:47 +00001025 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1026
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001027 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
1028 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
1029 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
1030 return -EIO;
1031
1032 /* Mandatory Parts */
Harald Welted12b0fd2009-12-15 21:36:05 +01001033 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001034
1035 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
1036 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
1037 if (len >= 3) {
1038 if (val[0] & 0x40)
Harald Welted12b0fd2009-12-15 21:36:05 +01001039 mr->flags |= MEAS_REP_F_DL_DTX;
1040 mr->ul.full.rx_lev = val[0] & 0x3f;
1041 mr->ul.sub.rx_lev = val[1] & 0x3f;
1042 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
1043 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte440fed02009-05-01 18:43:47 +00001044 }
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001045
Harald Welted12b0fd2009-12-15 21:36:05 +01001046 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001047
1048 /* Optional Parts */
Harald Welte440fed02009-05-01 18:43:47 +00001049 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Welted12b0fd2009-12-15 21:36:05 +01001050 mr->ms_timing_offset =
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001051 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
1052
Harald Weltefe9af262009-06-20 18:44:35 +02001053 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001054 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +01001055 mr->flags |= MEAS_REP_F_MS_L1;
1056 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001057 if (val[0] & 0x04)
Harald Welted12b0fd2009-12-15 21:36:05 +01001058 mr->flags |= MEAS_REP_F_FPC;
1059 mr->ms_l1.ta = val[1];
Harald Weltefe9af262009-06-20 18:44:35 +02001060 }
Harald Weltef7c43522009-06-09 20:24:21 +00001061 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freytherddd918f2009-10-22 15:43:55 +02001062 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Welted12b0fd2009-12-15 21:36:05 +01001063 rc = gsm48_parse_meas_rep(mr, msg);
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001064 if (rc < 0)
1065 return rc;
1066 }
1067
Harald Welted12b0fd2009-12-15 21:36:05 +01001068 print_meas_rep(mr);
Harald Welte60d68f12009-06-05 20:07:43 +00001069
Harald Welted12b0fd2009-12-15 21:36:05 +01001070 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Weltedbb1d882009-11-30 19:16:47 +01001071
Harald Welte75d34a82009-05-23 06:11:13 +00001072 return 0;
Harald Welte440fed02009-05-01 18:43:47 +00001073}
1074
Harald Welted011e8b2009-11-29 22:45:52 +01001075/* Chapter 8.4.7 */
1076static int rsl_rx_hando_det(struct msgb *msg)
1077{
1078 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1079 struct tlv_parsed tp;
1080
1081 DEBUGP(DRSL, "HANDOVER DETECT ");
1082
1083 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1084
1085 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
1086 DEBUGPC(DRSL, "access delay = %u\n",
1087 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
1088 else
1089 DEBUGPC(DRSL, "\n");
1090
1091 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
1092
1093 return 0;
1094}
1095
Harald Welte52b1f982008-12-23 20:25:15 +00001096static int abis_rsl_rx_dchan(struct msgb *msg)
1097{
Harald Welte8470bf22008-12-25 23:28:35 +00001098 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1099 int rc = 0;
Harald Weltef325eb42009-02-19 17:07:39 +00001100 char *ts_name;
Harald Welte52b1f982008-12-23 20:25:15 +00001101
Harald Welte8470bf22008-12-25 23:28:35 +00001102 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
Harald Weltef325eb42009-02-19 17:07:39 +00001103 ts_name = gsm_ts_name(msg->lchan->ts);
1104
Harald Welte10d0e672009-06-27 02:53:10 +02001105 if (rslh->c.msg_type != RSL_MT_MEAS_RES)
1106 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ", ts_name, rslh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +00001107
1108 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001109 case RSL_MT_CHAN_ACTIV_ACK:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001110 DEBUGPC(DRSL, "CHANNEL ACTIVATE ACK\n");
Harald Welte4b634542008-12-27 01:55:51 +00001111 rc = rsl_rx_chan_act_ack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001112 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001113 case RSL_MT_CHAN_ACTIV_NACK:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001114 DEBUGPC(DRSL, "CHANNEL ACTIVATE NACK\n");
Harald Welte4b634542008-12-27 01:55:51 +00001115 rc = rsl_rx_chan_act_nack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001116 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001117 case RSL_MT_CONN_FAIL:
Harald Welte7f93cea2009-02-23 00:02:59 +00001118 rc = rsl_rx_conn_fail(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001119 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001120 case RSL_MT_MEAS_RES:
Harald Welte440fed02009-05-01 18:43:47 +00001121 rc = rsl_rx_meas_res(msg);
Harald Welte2d5b6382008-12-27 19:46:06 +00001122 break;
Harald Welted011e8b2009-11-29 22:45:52 +01001123 case RSL_MT_HANDO_DET:
1124 rc = rsl_rx_hando_det(msg);
1125 break;
Harald Welte2d5b6382008-12-27 19:46:06 +00001126 case RSL_MT_RF_CHAN_REL_ACK:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001127 DEBUGPC(DRSL, "RF CHANNEL RELEASE ACK\n");
Harald Welte2d5b6382008-12-27 19:46:06 +00001128 lchan_free(msg->lchan);
Harald Welte8470bf22008-12-25 23:28:35 +00001129 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001130 case RSL_MT_MODE_MODIFY_ACK:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001131 DEBUGPC(DRSL, "CHANNEL MODE MODIFY ACK\n");
Harald Welteda783762009-02-18 03:29:53 +00001132 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001133 case RSL_MT_MODE_MODIFY_NACK:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001134 DEBUGPC(DRSL, "CHANNEL MODE MODIFY NACK\n");
Harald Welteda783762009-02-18 03:29:53 +00001135 break;
Harald Welte9c880c92009-10-24 10:29:22 +02001136 case RSL_MT_IPAC_PDCH_ACT_ACK:
1137 DEBUGPC(DRSL, "IPAC PDCH ACT ACK\n");
1138 break;
1139 case RSL_MT_IPAC_PDCH_ACT_NACK:
1140 DEBUGPC(DRSL, "IPAC PDCH ACT NACK\n");
1141 break;
1142 case RSL_MT_IPAC_PDCH_DEACT_ACK:
1143 DEBUGPC(DRSL, "IPAC PDCH DEACT ACK\n");
1144 break;
1145 case RSL_MT_IPAC_PDCH_DEACT_NACK:
1146 DEBUGPC(DRSL, "IPAC PDCH DEACT NACK\n");
1147 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001148 case RSL_MT_PHY_CONTEXT_CONF:
1149 case RSL_MT_PREPROC_MEAS_RES:
Harald Welte52b1f982008-12-23 20:25:15 +00001150 case RSL_MT_TALKER_DET:
1151 case RSL_MT_LISTENER_DET:
1152 case RSL_MT_REMOTE_CODEC_CONF_REP:
1153 case RSL_MT_MR_CODEC_MOD_ACK:
1154 case RSL_MT_MR_CODEC_MOD_NACK:
1155 case RSL_MT_MR_CODEC_MOD_PER:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001156 DEBUGPC(DRSL, "Unimplemented Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +00001157 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001158 break;
1159 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001160 DEBUGPC(DRSL, "unknown Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +00001161 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001162 return -EINVAL;
1163 }
Harald Weltef325eb42009-02-19 17:07:39 +00001164
Harald Welte8470bf22008-12-25 23:28:35 +00001165 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001166}
1167
Harald Welte702d8702008-12-26 20:25:35 +00001168static int rsl_rx_error_rep(struct msgb *msg)
1169{
1170 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8830e072009-07-28 17:58:09 +02001171 struct tlv_parsed tp;
Harald Welte702d8702008-12-26 20:25:35 +00001172
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001173 LOGP(DRSL, LOGL_ERROR, "ERROR REPORT ");
Harald Welte8830e072009-07-28 17:58:09 +02001174
1175 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1176
1177 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
1178 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
1179 TLVP_LEN(&tp, RSL_IE_CAUSE));
1180
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001181 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte702d8702008-12-26 20:25:35 +00001182
1183 return 0;
1184}
1185
Harald Welte52b1f982008-12-23 20:25:15 +00001186static int abis_rsl_rx_trx(struct msgb *msg)
1187{
Harald Welte702d8702008-12-26 20:25:35 +00001188 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001189 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001190
1191 switch (rslh->msg_type) {
Harald Welte702d8702008-12-26 20:25:35 +00001192 case RSL_MT_ERROR_REPORT:
1193 rc = rsl_rx_error_rep(msg);
1194 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001195 case RSL_MT_RF_RES_IND:
1196 /* interference on idle channels of TRX */
Harald Welte3cf7c3f2009-05-01 18:28:00 +00001197 //DEBUGP(DRSL, "TRX: RF Interference Indication\n");
Harald Welte8f5e2392009-02-03 12:57:37 +00001198 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001199 case RSL_MT_OVERLOAD:
1200 /* indicate CCCH / ACCH / processor overload */
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001201 LOGP(DRSL, LOGL_ERROR, "TRX: CCCH/ACCH/CPU Overload\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001202 break;
1203 default:
Harald Weltef325eb42009-02-19 17:07:39 +00001204 DEBUGP(DRSL, "Unknown Abis RSL TRX message type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001205 rslh->msg_type);
1206 return -EINVAL;
1207 }
Harald Welte8470bf22008-12-25 23:28:35 +00001208 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001209}
1210
Harald Welteb7e81162009-08-10 00:26:10 +02001211/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1212static void t3101_expired(void *data)
1213{
1214 struct gsm_lchan *lchan = data;
1215
Harald Welte115d1032009-08-10 11:43:22 +02001216 rsl_rf_chan_release(lchan);
Harald Welteb7e81162009-08-10 00:26:10 +02001217}
1218
Harald Welte8470bf22008-12-25 23:28:35 +00001219/* MS has requested a channel on the RACH */
Harald Welte52b1f982008-12-23 20:25:15 +00001220static int rsl_rx_chan_rqd(struct msgb *msg)
1221{
Harald Welte702d8702008-12-26 20:25:35 +00001222 struct gsm_bts *bts = msg->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001223 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1224 struct gsm48_req_ref *rqd_ref;
Harald Welte52b1f982008-12-23 20:25:15 +00001225 struct gsm48_imm_ass ia;
Harald Welte8470bf22008-12-25 23:28:35 +00001226 enum gsm_chan_t lctype;
Harald Welte2cbe0922008-12-29 04:09:31 +00001227 enum gsm_chreq_reason_t chreq_reason;
Harald Welte8470bf22008-12-25 23:28:35 +00001228 struct gsm_lchan *lchan;
1229 u_int8_t rqd_ta;
Holger Freyther3186bf22008-12-29 06:23:49 +00001230 int ret;
Harald Welte8470bf22008-12-25 23:28:35 +00001231
Harald Welte52b1f982008-12-23 20:25:15 +00001232 u_int16_t arfcn;
1233 u_int8_t ts_number, subch;
1234
Harald Welte8470bf22008-12-25 23:28:35 +00001235 /* parse request reference to be used in immediate assign */
1236 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1237 return -EINVAL;
1238
1239 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1240
1241 /* parse access delay and use as TA */
1242 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1243 return -EINVAL;
1244 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1245
1246 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1247 * request reference RA */
Holger Hans Peter Freytherf7d752f2009-11-16 17:12:38 +01001248 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1249 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte2cbe0922008-12-29 04:09:31 +00001250
Harald Welte8470bf22008-12-25 23:28:35 +00001251 /* check availability / allocate channel */
1252 lchan = lchan_alloc(bts, lctype);
1253 if (!lchan) {
Harald Welte9e2748e2009-12-12 20:53:23 +01001254 DEBUGP(DRSL, "CHAN RQD: no resources for %s 0x%x\n",
1255 gsm_lchan_name(lctype), rqd_ref->ra);
Harald Welte8470bf22008-12-25 23:28:35 +00001256 /* FIXME: send some kind of reject ?!? */
1257 return -ENOMEM;
1258 }
1259
1260 ts_number = lchan->ts->nr;
1261 arfcn = lchan->ts->trx->arfcn;
1262 subch = lchan->nr;
Harald Welte52b1f982008-12-23 20:25:15 +00001263
Harald Welte08d91a52009-08-30 15:37:11 +09001264 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)0e451d02009-08-13 10:14:26 +02001265 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte0b2124b2009-08-10 00:45:40 +02001266 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte9943c5b2009-07-29 15:41:29 +02001267 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte196d0522009-08-28 23:28:28 +09001268 lchan->tch_mode = GSM48_CMODE_SIGN;
Harald Welte8d77b952009-12-17 00:31:10 +01001269 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte52b1f982008-12-23 20:25:15 +00001270
1271 /* create IMMEDIATE ASSIGN 04.08 messge */
1272 memset(&ia, 0, sizeof(ia));
1273 ia.l2_plen = 0x2d;
1274 ia.proto_discr = GSM48_PDISC_RR;
1275 ia.msg_type = GSM48_MT_RR_IMM_ASS;
Harald Welte2d5b6382008-12-27 19:46:06 +00001276 ia.page_mode = GSM48_PM_SAME;
Harald Welte4b634542008-12-27 01:55:51 +00001277 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +00001278 ia.chan_desc.h0.h = 0;
1279 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
1280 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
Harald Welte814c4b72009-07-21 20:55:56 +02001281 ia.chan_desc.h0.tsc = bts->tsc;
Harald Welte8470bf22008-12-25 23:28:35 +00001282 /* use request reference extracted from CHAN_RQD */
1283 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
1284 ia.timing_advance = rqd_ta;
Harald Welte52b1f982008-12-23 20:25:15 +00001285 ia.mob_alloc_len = 0;
1286
Harald Welte8f5e2392009-02-03 12:57:37 +00001287 DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %s "
Holger Freyther79f4ae62009-06-02 03:25:04 +00001288 "chan_nr=0x%02x r=%s ra=0x%02x\n",
Harald Welteca64da92009-01-04 16:54:12 +00001289 arfcn, ts_number, subch, gsm_lchan_name(lchan->type),
Harald Welte4a543e82009-02-28 13:17:55 +00001290 ia.chan_desc.chan_nr, gsm_chreq_name(chreq_reason),
1291 rqd_ref->ra);
Harald Welte75a983f2008-12-27 21:34:06 +00001292
Harald Welteb7e81162009-08-10 00:26:10 +02001293 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1294 lchan->T3101.cb = t3101_expired;
1295 lchan->T3101.data = lchan;
Holger Hans Peter Freytherc4d88ad2009-11-21 21:18:38 +01001296 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Holger Freyther3186bf22008-12-29 06:23:49 +00001297
Harald Welte52b1f982008-12-23 20:25:15 +00001298 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
Holger Freyther3186bf22008-12-29 06:23:49 +00001299 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
1300
Harald Welte817f3c82008-12-30 14:57:59 +00001301 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001302}
1303
Harald Welteea280442009-02-02 22:29:56 +00001304/* MS has requested a channel on the RACH */
1305static int rsl_rx_ccch_load(struct msgb *msg)
1306{
1307 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1308 u_int16_t pg_buf_space;
Holger Freyther8c563cf2009-02-03 20:08:51 +00001309 u_int16_t rach_slot_count = -1;
1310 u_int16_t rach_busy_count = -1;
1311 u_int16_t rach_access_count = -1;
Harald Welteea280442009-02-02 22:29:56 +00001312
1313 switch (rslh->data[0]) {
1314 case RSL_IE_PAGING_LOAD:
1315 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
Holger Freyther392209c2009-02-10 00:06:19 +00001316 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
Harald Welteea280442009-02-02 22:29:56 +00001317 break;
1318 case RSL_IE_RACH_LOAD:
Holger Freyther8c563cf2009-02-03 20:08:51 +00001319 if (msg->data_len >= 7) {
1320 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1321 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1322 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1323 }
Harald Welteea280442009-02-02 22:29:56 +00001324 break;
1325 default:
1326 break;
1327 }
1328
1329 return 0;
1330}
1331
Harald Welte52b1f982008-12-23 20:25:15 +00001332static int abis_rsl_rx_cchan(struct msgb *msg)
1333{
Harald Welteea280442009-02-02 22:29:56 +00001334 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001335 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001336
Harald Welte8470bf22008-12-25 23:28:35 +00001337 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1338
1339 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001340 case RSL_MT_CHAN_RQD:
1341 /* MS has requested a channel on the RACH */
1342 rc = rsl_rx_chan_rqd(msg);
1343 break;
Harald Welteea280442009-02-02 22:29:56 +00001344 case RSL_MT_CCCH_LOAD_IND:
1345 /* current load on the CCCH */
1346 rc = rsl_rx_ccch_load(msg);
1347 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001348 case RSL_MT_DELETE_IND:
1349 /* CCCH overloaded, IMM_ASSIGN was dropped */
1350 case RSL_MT_CBCH_LOAD_IND:
1351 /* current load on the CBCH */
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001352 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1353 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001354 break;
1355 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001356 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1357 "0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001358 return -EINVAL;
1359 }
Harald Welte8470bf22008-12-25 23:28:35 +00001360
1361 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001362}
1363
Harald Welte4b634542008-12-27 01:55:51 +00001364static int rsl_rx_rll_err_ind(struct msgb *msg)
1365{
1366 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1367 u_int8_t *rlm_cause = rllh->data;
1368
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001369 LOGP(DRLL, LOGL_ERROR, "ERROR INDICATION cause=0x%02x\n", rlm_cause[1]);
Harald Welteedcc5272009-08-09 13:47:35 +02001370
1371 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte4b634542008-12-27 01:55:51 +00001372
Harald Welte81543bc2009-07-04 09:40:05 +02001373 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED)
Harald Welte115d1032009-08-10 11:43:22 +02001374 return rsl_rf_chan_release(msg->lchan);
Harald Welte81543bc2009-07-04 09:40:05 +02001375
Harald Welte4b634542008-12-27 01:55:51 +00001376 return 0;
1377}
Harald Weltef325eb42009-02-19 17:07:39 +00001378
Harald Welte52b1f982008-12-23 20:25:15 +00001379/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
1380 0x02, 0x06,
1381 0x01, 0x20,
1382 0x02, 0x00,
1383 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1384
1385static int abis_rsl_rx_rll(struct msgb *msg)
1386{
1387 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Weltef325eb42009-02-19 17:07:39 +00001388 int rc = 0;
1389 char *ts_name;
Harald Welte (local)daef6062009-08-14 11:41:12 +02001390 u_int8_t sapi = rllh->link_id & 7;
Harald Welte8470bf22008-12-25 23:28:35 +00001391
1392 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Weltef325eb42009-02-19 17:07:39 +00001393 ts_name = gsm_ts_name(msg->lchan->ts);
Harald Welte (local)daef6062009-08-14 11:41:12 +02001394 DEBUGP(DRLL, "channel=%s chan_nr=0x%02x sapi=%u ", ts_name,
1395 rllh->chan_nr, sapi);
Harald Welte52b1f982008-12-23 20:25:15 +00001396
1397 switch (rllh->c.msg_type) {
1398 case RSL_MT_DATA_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001399 DEBUGPC(DRLL, "DATA INDICATION\n");
Harald Welte4a543e82009-02-28 13:17:55 +00001400 if (msgb_l2len(msg) >
1401 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1402 rllh->data[0] == RSL_IE_L3_INFO) {
1403 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001404 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001405 }
Harald Welte52b1f982008-12-23 20:25:15 +00001406 break;
1407 case RSL_MT_EST_IND:
Harald Weltef325eb42009-02-19 17:07:39 +00001408 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welteb7e81162009-08-10 00:26:10 +02001409 /* lchan is established, stop T3101 */
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001410 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welteb7e81162009-08-10 00:26:10 +02001411 bsc_del_timer(&msg->lchan->T3101);
Harald Welte4a543e82009-02-28 13:17:55 +00001412 if (msgb_l2len(msg) >
1413 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1414 rllh->data[0] == RSL_IE_L3_INFO) {
1415 msg->l3h = &rllh->data[3];
Harald Welte (local)daef6062009-08-14 11:41:12 +02001416 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte4a543e82009-02-28 13:17:55 +00001417 }
Harald Welte52b1f982008-12-23 20:25:15 +00001418 break;
Harald Welteedcc5272009-08-09 13:47:35 +02001419 case RSL_MT_EST_CONF:
Harald Welte1c409272009-08-09 14:13:58 +02001420 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001421 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteedcc5272009-08-09 13:47:35 +02001422 rll_indication(msg->lchan, rllh->link_id,
1423 BSC_RLLR_IND_EST_CONF);
1424 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001425 case RSL_MT_REL_IND:
Harald Welted2dc1de2009-08-08 13:15:07 +02001426 /* BTS informs us of having received DISC from MS */
Harald Welte602f2b82009-08-04 02:50:21 +02001427 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001428 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteedcc5272009-08-09 13:47:35 +02001429 rll_indication(msg->lchan, rllh->link_id,
1430 BSC_RLLR_IND_REL_IND);
Harald Welted2dc1de2009-08-08 13:15:07 +02001431 /* we can now releae the channel on the BTS/Abis side */
Harald Welte115d1032009-08-10 11:43:22 +02001432 /* FIXME: officially we need to start T3111 and wait for
1433 * some grace period */
1434 rsl_rf_chan_release(msg->lchan);
Harald Welte2d5b6382008-12-27 19:46:06 +00001435 break;
1436 case RSL_MT_REL_CONF:
Harald Welted2dc1de2009-08-08 13:15:07 +02001437 /* BTS informs us of having received UA from MS,
1438 * in response to DISC that we've sent earlier */
Harald Welte602f2b82009-08-04 02:50:21 +02001439 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +01001440 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welted2dc1de2009-08-08 13:15:07 +02001441 /* we can now releae the channel on the BTS/Abis side */
Harald Welte115d1032009-08-10 11:43:22 +02001442 /* FIXME: officially we need to start T3111 and wait for
1443 * some grace period */
1444 rsl_rf_chan_release(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +00001445 break;
1446 case RSL_MT_ERROR_IND:
1447 rc = rsl_rx_rll_err_ind(msg);
1448 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001449 case RSL_MT_UNIT_DATA_IND:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001450 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1451 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001452 break;
1453 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001454 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1455 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001456 }
Harald Welte8470bf22008-12-25 23:28:35 +00001457 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001458}
1459
Harald Welte0603c9d2009-12-02 01:58:23 +05301460static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Weltef4e79f22009-07-28 18:11:56 +02001461{
Harald Welte0603c9d2009-12-02 01:58:23 +05301462 switch (lchan->tch_mode) {
Harald Weltef4e79f22009-07-28 18:11:56 +02001463 case GSM48_CMODE_SPEECH_V1:
Harald Welte0603c9d2009-12-02 01:58:23 +05301464 switch (lchan->type) {
1465 case GSM_LCHAN_TCH_F:
1466 return 0x00;
1467 case GSM_LCHAN_TCH_H:
1468 return 0x03;
1469 default:
1470 break;
1471 }
Harald Weltef4e79f22009-07-28 18:11:56 +02001472 case GSM48_CMODE_SPEECH_EFR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301473 switch (lchan->type) {
1474 case GSM_LCHAN_TCH_F:
1475 return 0x01;
1476 /* there's no half-rate EFR */
1477 default:
1478 break;
1479 }
Harald Weltef4e79f22009-07-28 18:11:56 +02001480 case GSM48_CMODE_SPEECH_AMR:
Harald Welte0603c9d2009-12-02 01:58:23 +05301481 switch (lchan->type) {
1482 case GSM_LCHAN_TCH_F:
1483 return 0x02;
1484 case GSM_LCHAN_TCH_H:
1485 return 0x05;
1486 default:
1487 break;
1488 }
1489 default:
1490 break;
Harald Weltef4e79f22009-07-28 18:11:56 +02001491 }
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001492 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welte0603c9d2009-12-02 01:58:23 +05301493 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001494 return 0;
Harald Weltef4e79f22009-07-28 18:11:56 +02001495}
1496
Harald Welte75099262009-02-16 21:12:08 +00001497/* ip.access specific RSL extensions */
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001498int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte75099262009-02-16 21:12:08 +00001499{
1500 struct msgb *msg = rsl_msgb_alloc();
1501 struct abis_rsl_dchan_hdr *dh;
Harald Welte58ca5b72009-07-29 12:12:18 +02001502 u_int8_t speech_mode;
Harald Welte75099262009-02-16 21:12:08 +00001503
1504 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001505 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte75099262009-02-16 21:12:08 +00001506 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1507 dh->chan_nr = lchan2chan_nr(lchan);
1508
Harald Weltef4e79f22009-07-28 18:11:56 +02001509 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Welte0603c9d2009-12-02 01:58:23 +05301510 speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
Harald Welte58ca5b72009-07-29 12:12:18 +02001511 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001512
Harald Welte58ca5b72009-07-29 12:12:18 +02001513 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_BIND "
1514 "speech_mode=0x%02x\n", gsm_ts_name(lchan->ts),
1515 dh->chan_nr, speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001516
Harald Welte75099262009-02-16 21:12:08 +00001517 msg->trx = lchan->ts->trx;
1518
1519 return abis_rsl_sendmsg(msg);
1520}
1521
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001522int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Welte20855542009-07-12 09:50:35 +02001523 u_int16_t conn_id, u_int8_t rtp_payload2)
Harald Welte75099262009-02-16 21:12:08 +00001524{
1525 struct msgb *msg = rsl_msgb_alloc();
1526 struct abis_rsl_dchan_hdr *dh;
1527 u_int8_t *att_f8, *att_ip, *att_port;
Harald Welte58ca5b72009-07-29 12:12:18 +02001528 u_int8_t speech_mode;
Harald Weltef4e79f22009-07-28 18:11:56 +02001529 struct in_addr ia;
Harald Welte75099262009-02-16 21:12:08 +00001530
1531 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001532 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte75099262009-02-16 21:12:08 +00001533 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1534 dh->chan_nr = lchan2chan_nr(lchan);
1535
Harald Welte58ca5b72009-07-29 12:12:18 +02001536 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Welte0603c9d2009-12-02 01:58:23 +05301537 speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Harald Welte58ca5b72009-07-29 12:12:18 +02001538
Harald Weltef4e79f22009-07-28 18:11:56 +02001539 ia.s_addr = htonl(ip);
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001540 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_MDCX "
Harald Welte58ca5b72009-07-29 12:12:18 +02001541 "IP=%s PORT=%d RTP_PAYLOAD2=%d CONN_ID=%d speech_mode=0x%02x\n",
Harald Weltef4e79f22009-07-28 18:11:56 +02001542 gsm_ts_name(lchan->ts), dh->chan_nr,
Harald Welte58ca5b72009-07-29 12:12:18 +02001543 inet_ntoa(ia), port, rtp_payload2, conn_id, speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001544
Harald Welte20855542009-07-12 09:50:35 +02001545 att_f8 = msgb_put(msg, sizeof(conn_id)+1);
Harald Welte86c162d2009-07-12 09:45:05 +02001546 att_f8[0] = RSL_IE_IPAC_CONN_ID;
Harald Welte20855542009-07-12 09:50:35 +02001547 att_f8[1] = conn_id >> 8;
1548 att_f8[2] = conn_id & 0xff;
Harald Welte75099262009-02-16 21:12:08 +00001549
1550 att_ip = msgb_put(msg, sizeof(ip)+1);
1551 att_ip[0] = RSL_IE_IPAC_REMOTE_IP;
1552 att_ip[1] = ip >> 24;
1553 att_ip[2] = ip >> 16;
1554 att_ip[3] = ip >> 8;
1555 att_ip[4] = ip & 0xff;
Harald Welteda783762009-02-18 03:29:53 +00001556 //att_ip[4] = 11;
Harald Welte75099262009-02-16 21:12:08 +00001557
1558 att_port = msgb_put(msg, sizeof(port)+1);
1559 att_port[0] = RSL_IE_IPAC_REMOTE_PORT;
1560 att_port[1] = port >> 8;
1561 att_port[2] = port & 0xff;
1562
Harald Welte58ca5b72009-07-29 12:12:18 +02001563 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, speech_mode);
Harald Weltef4e79f22009-07-28 18:11:56 +02001564 if (rtp_payload2)
1565 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1566
Harald Welte75099262009-02-16 21:12:08 +00001567 msg->trx = lchan->ts->trx;
1568
1569 return abis_rsl_sendmsg(msg);
1570}
1571
Harald Welte9c880c92009-10-24 10:29:22 +02001572int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan)
1573{
1574 struct msgb *msg = rsl_msgb_alloc();
1575 struct abis_rsl_dchan_hdr *dh;
1576
1577 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1578 init_dchan_hdr(dh, RSL_MT_IPAC_PDCH_ACT);
1579 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1580 dh->chan_nr = lchan2chan_nr(lchan);
1581
1582 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_PDCH_ACT\n",
1583 gsm_ts_name(lchan->ts), dh->chan_nr);
1584
1585 msg->trx = lchan->ts->trx;
1586
1587 return abis_rsl_sendmsg(msg);
1588}
1589
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001590static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00001591{
1592 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1593 struct tlv_parsed tv;
Harald Welte2c828992009-12-02 01:56:49 +05301594 struct gsm_lchan *lchan = msg->lchan;
Harald Welte167df882009-02-17 14:35:45 +00001595 struct in_addr ip;
Harald Welte75099262009-02-16 21:12:08 +00001596 u_int16_t port, attr_f8;
1597
1598 /* the BTS has acknowledged a local bind, it now tells us the IP
1599 * address and port number to which it has bound the given logical
1600 * channel */
1601
1602 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1603 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1604 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welte86c162d2009-07-12 09:45:05 +02001605 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001606 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte75099262009-02-16 21:12:08 +00001607 return -EINVAL;
1608 }
Harald Welte167df882009-02-17 14:35:45 +00001609 ip.s_addr = *((u_int32_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_IP));
Harald Welte75099262009-02-16 21:12:08 +00001610 port = *((u_int16_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_PORT));
1611 attr_f8 = *((u_int16_t *) TLVP_VAL(&tv, 0xf8));
1612
Harald Weltef4e79f22009-07-28 18:11:56 +02001613 DEBUGPC(DRSL, "IP=%s PORT=%d CONN_ID=%d ",
1614 inet_ntoa(ip), ntohs(port), ntohs(attr_f8));
1615
1616 if (TLVP_PRESENT(&tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
Harald Welte2c828992009-12-02 01:56:49 +05301617 lchan->abis_ip.rtp_payload2 =
Harald Weltef4e79f22009-07-28 18:11:56 +02001618 *TLVP_VAL(&tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1619 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
Harald Welte2c828992009-12-02 01:56:49 +05301620 lchan->abis_ip.rtp_payload2);
Harald Weltef4e79f22009-07-28 18:11:56 +02001621 }
Harald Welte167df882009-02-17 14:35:45 +00001622
Harald Welte75099262009-02-16 21:12:08 +00001623 /* update our local information about this TS */
Harald Welte2c828992009-12-02 01:56:49 +05301624 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1625 lchan->abis_ip.bound_port = ntohs(port);
1626 lchan->abis_ip.conn_id = ntohs(attr_f8);
Harald Welte75099262009-02-16 21:12:08 +00001627
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001628 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte167df882009-02-17 14:35:45 +00001629
Harald Welte75099262009-02-16 21:12:08 +00001630 return 0;
1631}
1632
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001633static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte75099262009-02-16 21:12:08 +00001634{
1635 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1636 struct tlv_parsed tv;
1637
1638 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte75099262009-02-16 21:12:08 +00001639
Harald Welte8830e072009-07-28 17:58:09 +02001640 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
1641 print_rsl_cause(TLVP_VAL(&tv, RSL_IE_CAUSE),
1642 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte75099262009-02-16 21:12:08 +00001643
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001644 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welte888b1142009-07-28 18:02:05 +02001645
Harald Welte75099262009-02-16 21:12:08 +00001646 return 0;
1647}
1648
1649static int abis_rsl_rx_ipacc(struct msgb *msg)
1650{
1651 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1652 int rc = 0;
1653
1654 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Weltef325eb42009-02-19 17:07:39 +00001655 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ",
1656 gsm_ts_name(msg->lchan->ts), rllh->chan_nr);
Harald Welte75099262009-02-16 21:12:08 +00001657
1658 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001659 case RSL_MT_IPAC_CRCX_ACK:
1660 DEBUGPC(DRSL, "IPAC_CRCX_ACK ");
1661 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte75099262009-02-16 21:12:08 +00001662 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001663 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00001664 /* somehow the BTS was unable to bind the lchan to its local
1665 * port?!? */
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001666 DEBUGPC(DRSL, "IPAC_CRCX_NACK ");
Harald Welte75099262009-02-16 21:12:08 +00001667 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001668 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte75099262009-02-16 21:12:08 +00001669 /* the BTS tells us that a connect operation was successful */
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001670 DEBUGPC(DRSL, "IPAC_MDCX_ACK ");
Harald Welte75099262009-02-16 21:12:08 +00001671 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001672 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte75099262009-02-16 21:12:08 +00001673 /* somehow the BTS was unable to connect the lchan to a remote
1674 * port */
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001675 DEBUGPC(DRSL, "IPAC_MDCX_NACK ");
Harald Welte75099262009-02-16 21:12:08 +00001676 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001677 case RSL_MT_IPAC_DLCX_IND:
1678 DEBUGPC(DRSL, "IPAC_DLCX_IND ");
1679 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte75099262009-02-16 21:12:08 +00001680 break;
1681 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001682 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x",
1683 rllh->c.msg_type);
Harald Welte75099262009-02-16 21:12:08 +00001684 break;
1685 }
Harald Welte6dab0552009-05-01 17:21:37 +00001686 DEBUGPC(DRSL, "\n");
Harald Welte75099262009-02-16 21:12:08 +00001687
1688 return rc;
1689}
1690
1691
Harald Welte52b1f982008-12-23 20:25:15 +00001692/* Entry-point where L2 RSL from BTS enters */
Harald Welte8470bf22008-12-25 23:28:35 +00001693int abis_rsl_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001694{
1695 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
Harald Welte8f5e2392009-02-03 12:57:37 +00001696 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001697
1698 switch (rslh->msg_discr & 0xfe) {
1699 case ABIS_RSL_MDISC_RLL:
1700 rc = abis_rsl_rx_rll(msg);
1701 break;
1702 case ABIS_RSL_MDISC_DED_CHAN:
1703 rc = abis_rsl_rx_dchan(msg);
1704 break;
1705 case ABIS_RSL_MDISC_COM_CHAN:
Harald Welte52b1f982008-12-23 20:25:15 +00001706 rc = abis_rsl_rx_cchan(msg);
1707 break;
Harald Welte8470bf22008-12-25 23:28:35 +00001708 case ABIS_RSL_MDISC_TRX:
1709 rc = abis_rsl_rx_trx(msg);
1710 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001711 case ABIS_RSL_MDISC_LOC:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001712 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte8f5e2392009-02-03 12:57:37 +00001713 rslh->msg_discr);
1714 break;
Harald Welte75099262009-02-16 21:12:08 +00001715 case ABIS_RSL_MDISC_IPACCESS:
1716 rc = abis_rsl_rx_ipacc(msg);
1717 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001718 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +01001719 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1720 "0x%02x\n", rslh->msg_discr);
Harald Welte52b1f982008-12-23 20:25:15 +00001721 return -EINVAL;
1722 }
Harald Welte4f4a3902008-12-26 00:04:49 +00001723 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001724 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001725}
Holger Freyther3b72a892009-02-04 00:31:39 +00001726
1727
Holger Hans Peter Freyther3d571832009-07-09 20:43:16 +02001728/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
Holger Freyther3b72a892009-02-04 00:31:39 +00001729int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
1730{
1731 switch (ccch_conf) {
1732 case RSL_BCCH_CCCH_CONF_1_NC:
1733 return 1;
1734 case RSL_BCCH_CCCH_CONF_1_C:
1735 return 1;
1736 case RSL_BCCH_CCCH_CONF_2_NC:
1737 return 2;
1738 case RSL_BCCH_CCCH_CONF_3_NC:
1739 return 3;
1740 case RSL_BCCH_CCCH_CONF_4_NC:
1741 return 4;
1742 default:
1743 return -1;
1744 }
1745}
1746
Holger Hans Peter Freyther3d571832009-07-09 20:43:16 +02001747/* Section 3.3.2.3 TS 05.02 */
Holger Freyther3b72a892009-02-04 00:31:39 +00001748int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
1749{
1750 switch (ccch_conf) {
1751 case RSL_BCCH_CCCH_CONF_1_NC:
1752 return 0;
1753 case RSL_BCCH_CCCH_CONF_1_C:
1754 return 1;
1755 case RSL_BCCH_CCCH_CONF_2_NC:
1756 return 0;
1757 case RSL_BCCH_CCCH_CONF_3_NC:
1758 return 0;
1759 case RSL_BCCH_CCCH_CONF_4_NC:
1760 return 0;
1761 default:
1762 return -1;
1763 }
1764}
1765
1766/* From Table 10.5.33 of GSM 04.08 */
1767int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1768{
Harald Weltea43f7892009-12-01 18:04:30 +05301769 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1770 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1771 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001772 } else {
Harald Weltea43f7892009-12-01 18:04:30 +05301773 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1774 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001775 }
1776}