blob: d669ba06d8c506e50fd541b8244a5f5deeda9ff8 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/gsm_04_08.h>
Harald Weltec4dcda02009-08-09 14:45:18 +020033#include <openbsc/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/abis_rsl.h>
35#include <openbsc/chan_alloc.h>
Harald Welteed9a5ab2009-08-09 13:47:35 +020036#include <openbsc/bsc_rll.h>
Harald Welte59b04682009-06-10 05:40:52 +080037#include <openbsc/debug.h>
38#include <openbsc/tlv.h>
39#include <openbsc/paging.h>
40#include <openbsc/signal.h>
Harald Weltec20bd1d2009-11-29 19:07:28 +010041#include <openbsc/meas_rep.h>
Harald Welte50517742009-12-20 15:42:44 +010042#include <openbsc/rtp_proxy.h>
Harald Welte59b04682009-06-10 05:40:52 +080043
44#define RSL_ALLOC_SIZE 1024
45#define RSL_ALLOC_HEADROOM 128
46
47#define MAX(a, b) (a) >= (b) ? (a) : (b)
48
49static const struct tlv_definition rsl_att_tlvdef = {
50 .def = {
51 [RSL_IE_CHAN_NR] = { TLV_TYPE_TV },
52 [RSL_IE_LINK_IDENT] = { TLV_TYPE_TV },
53 [RSL_IE_ACT_TYPE] = { TLV_TYPE_TV },
54 [RSL_IE_BS_POWER] = { TLV_TYPE_TV },
55 [RSL_IE_CHAN_IDENT] = { TLV_TYPE_TLV },
56 [RSL_IE_CHAN_MODE] = { TLV_TYPE_TLV },
57 [RSL_IE_ENCR_INFO] = { TLV_TYPE_TLV },
58 [RSL_IE_FRAME_NUMBER] = { TLV_TYPE_FIXED, 2 },
59 [RSL_IE_HANDO_REF] = { TLV_TYPE_TV },
60 [RSL_IE_L1_INFO] = { TLV_TYPE_FIXED, 2 },
61 [RSL_IE_L3_INFO] = { TLV_TYPE_TL16V },
62 [RSL_IE_MS_IDENTITY] = { TLV_TYPE_TLV },
63 [RSL_IE_MS_POWER] = { TLV_TYPE_TV },
64 [RSL_IE_PAGING_GROUP] = { TLV_TYPE_TV },
65 [RSL_IE_PAGING_LOAD] = { TLV_TYPE_FIXED, 2 },
66 [RSL_IE_PYHS_CONTEXT] = { TLV_TYPE_TLV },
67 [RSL_IE_ACCESS_DELAY] = { TLV_TYPE_TV },
68 [RSL_IE_RACH_LOAD] = { TLV_TYPE_TLV },
69 [RSL_IE_REQ_REFERENCE] = { TLV_TYPE_FIXED, 3 },
70 [RSL_IE_RELEASE_MODE] = { TLV_TYPE_TV },
71 [RSL_IE_RESOURCE_INFO] = { TLV_TYPE_TLV },
72 [RSL_IE_RLM_CAUSE] = { TLV_TYPE_TLV },
73 [RSL_IE_STARTNG_TIME] = { TLV_TYPE_FIXED, 2 },
74 [RSL_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
75 [RSL_IE_UPLINK_MEAS] = { TLV_TYPE_TLV },
76 [RSL_IE_CAUSE] = { TLV_TYPE_TLV },
77 [RSL_IE_MEAS_RES_NR] = { TLV_TYPE_TV },
78 [RSL_IE_MSG_ID] = { TLV_TYPE_TV },
79 [RSL_IE_SYSINFO_TYPE] = { TLV_TYPE_TV },
80 [RSL_IE_MS_POWER_PARAM] = { TLV_TYPE_TLV },
81 [RSL_IE_BS_POWER_PARAM] = { TLV_TYPE_TLV },
82 [RSL_IE_PREPROC_PARAM] = { TLV_TYPE_TLV },
83 [RSL_IE_PREPROC_MEAS] = { TLV_TYPE_TLV },
84 [RSL_IE_IMM_ASS_INFO] = { TLV_TYPE_TLV },
85 [RSL_IE_SMSCB_INFO] = { TLV_TYPE_FIXED, 23 },
86 [RSL_IE_MS_TIMING_OFFSET] = { TLV_TYPE_TV },
87 [RSL_IE_ERR_MSG] = { TLV_TYPE_TLV },
88 [RSL_IE_FULL_BCCH_INFO] = { TLV_TYPE_TLV },
89 [RSL_IE_CHAN_NEEDED] = { TLV_TYPE_TV },
90 [RSL_IE_CB_CMD_TYPE] = { TLV_TYPE_TV },
91 [RSL_IE_SMSCB_MSG] = { TLV_TYPE_TLV },
92 [RSL_IE_FULL_IMM_ASS_INFO] = { TLV_TYPE_TLV },
93 [RSL_IE_SACCH_INFO] = { TLV_TYPE_TLV },
94 [RSL_IE_CBCH_LOAD_INFO] = { TLV_TYPE_TV },
95 [RSL_IE_SMSCB_CHAN_INDICATOR] = { TLV_TYPE_TV },
96 [RSL_IE_GROUP_CALL_REF] = { TLV_TYPE_TLV },
97 [RSL_IE_CHAN_DESC] = { TLV_TYPE_TLV },
98 [RSL_IE_NCH_DRX_INFO] = { TLV_TYPE_TLV },
99 [RSL_IE_CMD_INDICATOR] = { TLV_TYPE_TLV },
100 [RSL_IE_EMLPP_PRIO] = { TLV_TYPE_TV },
101 [RSL_IE_UIC] = { TLV_TYPE_TLV },
102 [RSL_IE_MAIN_CHAN_REF] = { TLV_TYPE_TV },
103 [RSL_IE_MR_CONFIG] = { TLV_TYPE_TLV },
104 [RSL_IE_MR_CONTROL] = { TLV_TYPE_TV },
105 [RSL_IE_SUP_CODEC_TYPES] = { TLV_TYPE_TLV },
106 [RSL_IE_CODEC_CONFIG] = { TLV_TYPE_TLV },
107 [RSL_IE_RTD] = { TLV_TYPE_TV },
108 [RSL_IE_TFO_STATUS] = { TLV_TYPE_TV },
109 [RSL_IE_LLP_APDU] = { TLV_TYPE_TLV },
Harald Welte4684e632009-08-10 09:51:40 +0200110 [RSL_IE_SIEMENS_MRPCI] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200111 [RSL_IE_IPAC_PROXY_UDP] = { TLV_TYPE_FIXED, 2 },
112 [RSL_IE_IPAC_BSCMPL_TOUT] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800113 [RSL_IE_IPAC_REMOTE_IP] = { TLV_TYPE_FIXED, 4 },
114 [RSL_IE_IPAC_REMOTE_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200115 [RSL_IE_IPAC_RTP_PAYLOAD] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800116 [RSL_IE_IPAC_LOCAL_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welteb9498952009-07-12 09:45:05 +0200117 [RSL_IE_IPAC_SPEECH_MODE] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200118 [RSL_IE_IPAC_LOCAL_IP] = { TLV_TYPE_FIXED, 4 },
Harald Welteb9498952009-07-12 09:45:05 +0200119 [RSL_IE_IPAC_CONN_ID] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200120 [RSL_IE_IPAC_RTP_CSD_FMT] = { TLV_TYPE_TV },
121 [RSL_IE_IPAC_RTP_JIT_BUF] = { TLV_TYPE_FIXED, 2 },
122 [RSL_IE_IPAC_RTP_COMPR] = { TLV_TYPE_TV },
Harald Welteb9498952009-07-12 09:45:05 +0200123 [RSL_IE_IPAC_RTP_PAYLOAD2] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200124 [RSL_IE_IPAC_RTP_MPLEX] = { TLV_TYPE_FIXED, 8 },
125 [RSL_IE_IPAC_RTP_MPLEX_ID] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800126 },
127};
128#define rsl_tlv_parse(dec, buf, len) \
129 tlv_parse(dec, &rsl_att_tlvdef, buf, len, 0, 0)
130
131static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
132{
133 /* mask off the transparent bit ? */
134 msg_type &= 0xfe;
135
136 if ((msg_type & 0xf0) == 0x00)
137 return ABIS_RSL_MDISC_RLL;
138 if ((msg_type & 0xf0) == 0x10) {
139 if (msg_type >= 0x19 && msg_type <= 0x22)
140 return ABIS_RSL_MDISC_TRX;
141 else
142 return ABIS_RSL_MDISC_COM_CHAN;
143 }
144 if ((msg_type & 0xe0) == 0x20)
145 return ABIS_RSL_MDISC_DED_CHAN;
146
147 return ABIS_RSL_MDISC_LOC;
148}
149
150static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
151 u_int8_t msg_type)
152{
153 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
154 dh->c.msg_type = msg_type;
155 dh->ie_chan = RSL_IE_CHAN_NR;
156}
157
158static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
159 u_int8_t msg_type)
160{
161 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
162 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
163 dh->c.msg_type = msg_type;
164 dh->ie_chan = RSL_IE_CHAN_NR;
165 dh->ie_link_id = RSL_IE_LINK_IDENT;
166}
167
168
169/* encode channel number as per Section 9.3.1 */
170u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
171{
172 u_int8_t ret;
173
174 ret = (timeslot & 0x07) | type;
175
176 switch (type) {
177 case RSL_CHAN_Lm_ACCHs:
178 subch &= 0x01;
179 break;
180 case RSL_CHAN_SDCCH4_ACCH:
181 subch &= 0x07;
182 break;
183 case RSL_CHAN_SDCCH8_ACCH:
184 subch &= 0x07;
185 break;
186 default:
187 /* no subchannels allowed */
188 subch = 0x00;
189 break;
190 }
191 ret |= (subch << 3);
192
193 return ret;
194}
195
196/* determine logical channel based on TRX and channel number IE */
197struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
198{
199 struct gsm_lchan *lchan;
200 u_int8_t ts_nr = chan_nr & 0x07;
201 u_int8_t cbits = chan_nr >> 3;
202 u_int8_t lch_idx;
203 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
204
205 if (cbits == 0x01) {
206 lch_idx = 0; /* TCH/F */
Harald Welte37884ed2009-10-24 10:25:50 +0200207 if (ts->pchan != GSM_PCHAN_TCH_F &&
208 ts->pchan != GSM_PCHAN_PDCH &&
209 ts->pchan != GSM_PCHAN_TCH_F_PDCH)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100210 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800211 chan_nr, ts->pchan);
212 } else if ((cbits & 0x1e) == 0x02) {
213 lch_idx = cbits & 0x1; /* TCH/H */
214 if (ts->pchan != GSM_PCHAN_TCH_H)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100215 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800216 chan_nr, ts->pchan);
217 } else if ((cbits & 0x1c) == 0x04) {
218 lch_idx = cbits & 0x3; /* SDCCH/4 */
219 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100220 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800221 chan_nr, ts->pchan);
222 } else if ((cbits & 0x18) == 0x08) {
223 lch_idx = cbits & 0x7; /* SDCCH/8 */
224 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100225 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800226 chan_nr, ts->pchan);
227 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
228 lch_idx = 0;
229 if (ts->pchan != GSM_PCHAN_CCCH &&
230 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100231 LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800232 chan_nr, ts->pchan);
233 /* FIXME: we should not return first sdcch4 !!! */
234 } else {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100235 LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +0800236 return NULL;
237 }
238
239 lchan = &ts->lchan[lch_idx];
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100240 debug_set_context(BSC_CTX_LCHAN, lchan);
241 debug_set_context(BSC_CTX_SUBSCR, lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800242
243 return lchan;
244}
245
Holger Hans Peter Freyther942ff172009-10-22 11:47:45 +0200246/* See Table 10.5.25 of GSM04.08 */
Harald Welteb5586242009-12-09 19:18:32 +0100247u_int8_t lchan2chan_nr(const struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800248{
249 struct gsm_bts_trx_ts *ts = lchan->ts;
250 u_int8_t cbits, chan_nr;
251
252 switch (ts->pchan) {
253 case GSM_PCHAN_TCH_F:
Harald Welte37884ed2009-10-24 10:25:50 +0200254 case GSM_PCHAN_PDCH:
255 case GSM_PCHAN_TCH_F_PDCH:
Harald Welte59b04682009-06-10 05:40:52 +0800256 cbits = 0x01;
257 break;
258 case GSM_PCHAN_TCH_H:
259 cbits = 0x02;
260 cbits += lchan->nr;
261 break;
262 case GSM_PCHAN_CCCH_SDCCH4:
263 cbits = 0x04;
264 cbits += lchan->nr;
265 break;
266 case GSM_PCHAN_SDCCH8_SACCH8C:
267 cbits = 0x08;
268 cbits += lchan->nr;
269 break;
270 default:
271 case GSM_PCHAN_CCCH:
272 cbits = 0x10;
273 break;
274 }
275
276 chan_nr = (cbits << 3) | (ts->nr & 0x7);
277
278 return chan_nr;
279}
280
281/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
282u_int64_t str_to_imsi(const char *imsi_str)
283{
284 u_int64_t ret;
285
286 ret = strtoull(imsi_str, NULL, 10);
287
288 return ret;
289}
290
291/* Table 5 Clause 7 TS 05.02 */
292unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
293{
294 if (!bs_ccch_sdcch_comb)
295 return 9 - bs_ag_blks_res;
296 else
297 return 3 - bs_ag_blks_res;
298}
299
300/* Chapter 6.5.2 of TS 05.02 */
301unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
302 unsigned int n_pag_blocks)
303{
304 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
305}
306
307/* Chapter 6.5.2 of TS 05.02 */
308unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
309 int n_pag_blocks)
310{
311 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
312}
313
314static struct msgb *rsl_msgb_alloc(void)
315{
Harald Welte9cfc9352009-06-26 19:39:35 +0200316 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
317 "RSL");
Harald Welte59b04682009-06-10 05:40:52 +0800318}
319
320#define MACBLOCK_SIZE 23
321static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
322{
323 memcpy(out, in, len);
324
325 if (len < MACBLOCK_SIZE)
326 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
327}
328
Harald Welted2dd9de2009-08-30 15:37:11 +0900329/* Chapter 9.3.7: Encryption Information */
330static int build_encr_info(u_int8_t *out, struct gsm_lchan *lchan)
331{
332 *out++ = lchan->encr.alg_id & 0xff;
333 if (lchan->encr.key_len)
334 memcpy(out, lchan->encr.key, lchan->encr.key_len);
335 return lchan->encr.key_len + 1;
336}
337
338
Harald Weltef1a168d2009-07-28 17:58:09 +0200339static const char *rsl_err_vals[0xff] = {
340 [RSL_ERR_RADIO_IF_FAIL] = "Radio Interface Failure",
341 [RSL_ERR_RADIO_LINK_FAIL] = "Radio Link Failure",
342 [RSL_ERR_HANDOVER_ACC_FAIL] = "Handover Access Failure",
343 [RSL_ERR_TALKER_ACC_FAIL] = "Talker Access Failure",
344 [RSL_ERR_OM_INTERVENTION] = "O&M Intervention",
345 [RSL_ERR_NORMAL_UNSPEC] = "Normal event, unspecified",
Harald Welteb1717e92009-08-04 02:31:05 +0200346 [RSL_ERR_T_MSRFPCI_EXP] = "Siemens: T_MSRFPCI Expired",
Harald Weltef1a168d2009-07-28 17:58:09 +0200347 [RSL_ERR_EQUIPMENT_FAIL] = "Equipment Failure",
348 [RSL_ERR_RR_UNAVAIL] = "Radio Resource not available",
349 [RSL_ERR_TERR_CH_FAIL] = "Terrestrial Channel Failure",
350 [RSL_ERR_CCCH_OVERLOAD] = "CCCH Overload",
351 [RSL_ERR_ACCH_OVERLOAD] = "ACCH Overload",
352 [RSL_ERR_PROCESSOR_OVERLOAD] = "Processor Overload",
353 [RSL_ERR_RES_UNAVAIL] = "Resource not available, unspecified",
354 [RSL_ERR_TRANSC_UNAVAIL] = "Transcoding not available",
355 [RSL_ERR_SERV_OPT_UNAVAIL] = "Service or Option not available",
356 [RSL_ERR_ENCR_UNIMPL] = "Encryption algorithm not implemented",
357 [RSL_ERR_SERV_OPT_UNIMPL] = "Service or Option not implemented",
358 [RSL_ERR_RCH_ALR_ACTV_ALLOC] = "Radio channel already activated",
359 [RSL_ERR_INVALID_MESSAGE] = "Invalid Message, unspecified",
360 [RSL_ERR_MSG_DISCR] = "Message Discriminator Error",
361 [RSL_ERR_MSG_TYPE] = "Message Type Error",
362 [RSL_ERR_MSG_SEQ] = "Message Sequence Error",
363 [RSL_ERR_IE_ERROR] = "General IE error",
364 [RSL_ERR_MAND_IE_ERROR] = "Mandatory IE error",
365 [RSL_ERR_OPT_IE_ERROR] = "Optional IE error",
366 [RSL_ERR_IE_NONEXIST] = "IE non-existent",
367 [RSL_ERR_IE_LENGTH] = "IE length error",
368 [RSL_ERR_IE_CONTENT] = "IE content error",
369 [RSL_ERR_PROTO] = "Protocol error, unspecified",
370 [RSL_ERR_INTERWORKING] = "Interworking error, unspecified",
371};
372
373static const char *rsl_err_name(u_int8_t err)
Harald Welte59b04682009-06-10 05:40:52 +0800374{
Harald Weltef1a168d2009-07-28 17:58:09 +0200375 if (rsl_err_vals[err])
376 return rsl_err_vals[err];
377 else
378 return "unknown";
379}
380
381static void print_rsl_cause(const u_int8_t *cause_v, u_int8_t cause_len)
382{
Harald Welte59b04682009-06-10 05:40:52 +0800383 int i;
384
Harald Weltef1a168d2009-07-28 17:58:09 +0200385 DEBUGPC(DRSL, "CAUSE=0x%02x(%s) ",
386 cause_v[0], rsl_err_name(cause_v[0]));
387 for (i = 1; i < cause_len-1; i++)
388 DEBUGPC(DRSL, "%02x ", cause_v[i]);
Harald Welte59b04682009-06-10 05:40:52 +0800389}
390
391/* Send a BCCH_INFO message as per Chapter 8.5.1 */
392int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
393 const u_int8_t *data, int len)
394{
395 struct abis_rsl_dchan_hdr *dh;
396 struct msgb *msg = rsl_msgb_alloc();
397
398 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
399 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
400 dh->chan_nr = RSL_CHAN_BCCH;
401
402 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
403 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
404
405 msg->trx = trx;
406
407 return abis_rsl_sendmsg(msg);
408}
409
410int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
411 const u_int8_t *data, int len)
412{
413 struct abis_rsl_common_hdr *ch;
414 struct msgb *msg = rsl_msgb_alloc();
415
416 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
417 ch->msg_discr = ABIS_RSL_MDISC_TRX;
418 ch->msg_type = RSL_MT_SACCH_FILL;
419
420 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
421 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
422
423 msg->trx = trx;
424
425 return abis_rsl_sendmsg(msg);
426}
427
Harald Welte91afe4c2009-06-20 18:15:19 +0200428int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
429{
430 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200431 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200432 u_int8_t chan_nr = lchan2chan_nr(lchan);
433
434 db = abs(db);
435 if (db > 30)
436 return -EINVAL;
437
Harald Welteed831842009-06-27 03:09:08 +0200438 msg = rsl_msgb_alloc();
439
Harald Welte91afe4c2009-06-20 18:15:19 +0200440 lchan->bs_power = db/2;
441 if (fpc)
442 lchan->bs_power |= 0x10;
443
444 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
445 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
446 dh->chan_nr = chan_nr;
447
448 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
449
450 msg->trx = lchan->ts->trx;
451
452 return abis_rsl_sendmsg(msg);
453}
454
Harald Welte91afe4c2009-06-20 18:15:19 +0200455int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
456{
457 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200458 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200459 u_int8_t chan_nr = lchan2chan_nr(lchan);
460 int ctl_lvl;
461
Harald Weltec4dcda02009-08-09 14:45:18 +0200462 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts->band, dbm);
Harald Welte91afe4c2009-06-20 18:15:19 +0200463 if (ctl_lvl < 0)
464 return ctl_lvl;
465
Harald Welteed831842009-06-27 03:09:08 +0200466 msg = rsl_msgb_alloc();
467
Harald Welte91afe4c2009-06-20 18:15:19 +0200468 lchan->ms_power = ctl_lvl;
469
470 if (fpc)
471 lchan->ms_power |= 0x20;
472
473 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
474 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
475 dh->chan_nr = chan_nr;
476
477 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
478
479 msg->trx = lchan->ts->trx;
480
481 return abis_rsl_sendmsg(msg);
482}
483
Harald Welte39274f42009-07-29 15:41:29 +0200484static int channel_mode_from_lchan(struct rsl_ie_chan_mode *cm,
485 struct gsm_lchan *lchan)
486{
487 memset(cm, 0, sizeof(cm));
488
489 /* FIXME: what to do with data calls ? */
490 cm->dtx_dtu = 0x00;
491
492 /* set TCH Speech/Data */
493 cm->spd_ind = lchan->rsl_cmode;
494
Harald Welte951e3512009-11-27 08:55:16 +0100495 if (lchan->rsl_cmode == RSL_CMOD_SPD_SIGN &&
496 lchan->tch_mode != GSM48_CMODE_SIGN)
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100497 LOGP(DRSL, LOGL_ERROR, "unsupported: rsl_mode == signalling, "
Harald Welte951e3512009-11-27 08:55:16 +0100498 "but tch_mode != signalling\n");
499
Harald Welte39274f42009-07-29 15:41:29 +0200500 switch (lchan->type) {
501 case GSM_LCHAN_SDCCH:
502 cm->chan_rt = RSL_CMOD_CRT_SDCCH;
503 break;
504 case GSM_LCHAN_TCH_F:
505 cm->chan_rt = RSL_CMOD_CRT_TCH_Bm;
506 break;
507 case GSM_LCHAN_TCH_H:
508 cm->chan_rt = RSL_CMOD_CRT_TCH_Lm;
509 break;
510 case GSM_LCHAN_NONE:
511 case GSM_LCHAN_UNKNOWN:
512 default:
513 return -EINVAL;
514 }
515
516 switch (lchan->tch_mode) {
517 case GSM48_CMODE_SIGN:
518 cm->chan_rate = 0;
519 break;
520 case GSM48_CMODE_SPEECH_V1:
521 cm->chan_rate = RSL_CMOD_SP_GSM1;
522 break;
523 case GSM48_CMODE_SPEECH_EFR:
524 cm->chan_rate = RSL_CMOD_SP_GSM2;
525 break;
526 case GSM48_CMODE_SPEECH_AMR:
527 cm->chan_rate = RSL_CMOD_SP_GSM3;
528 break;
529 case GSM48_CMODE_DATA_14k5:
530 cm->chan_rate = RSL_CMOD_SP_NT_14k5;
531 break;
532 case GSM48_CMODE_DATA_12k0:
533 cm->chan_rate = RSL_CMOD_SP_NT_12k0;
534 break;
535 case GSM48_CMODE_DATA_6k0:
536 cm->chan_rate = RSL_CMOD_SP_NT_6k0;
537 break;
538 default:
539 return -EINVAL;
540 }
541
542 return 0;
543}
544
Harald Welte59b04682009-06-10 05:40:52 +0800545/* Chapter 8.4.1 */
546#if 0
547int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
548 u_int8_t act_type,
549 struct rsl_ie_chan_mode *chan_mode,
550 struct rsl_ie_chan_ident *chan_ident,
551 u_int8_t bs_power, u_int8_t ms_power,
552 u_int8_t ta)
553{
554 struct abis_rsl_dchan_hdr *dh;
555 struct msgb *msg = rsl_msgb_alloc();
556
557 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
558 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
559 dh->chan_nr = chan_nr;
560
561 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
562 /* For compatibility with Phase 1 */
563 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
564 (u_int8_t *) chan_mode);
565 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
566 (u_int8_t *) chan_ident);
567#if 0
568 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
569 (u_int8_t *) &encr_info);
570#endif
571 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
572 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
573 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
574
575 msg->trx = trx;
576
577 return abis_rsl_sendmsg(msg);
578}
579#endif
580
581int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
Harald Welteb90d7bd2009-12-17 00:31:10 +0100582 u_int8_t ta, u_int8_t ho_ref)
Harald Welte59b04682009-06-10 05:40:52 +0800583{
584 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200585 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200586 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800587
588 u_int8_t chan_nr = lchan2chan_nr(lchan);
589 u_int16_t arfcn = lchan->ts->trx->arfcn;
590 struct rsl_ie_chan_mode cm;
591 struct rsl_ie_chan_ident ci;
592
Harald Welte39274f42009-07-29 15:41:29 +0200593 rc = channel_mode_from_lchan(&cm, lchan);
594 if (rc < 0)
595 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800596
597 memset(&ci, 0, sizeof(ci));
598 ci.chan_desc.iei = 0x64;
599 ci.chan_desc.chan_nr = chan_nr;
600 ci.chan_desc.oct3 = (lchan->ts->trx->bts->tsc << 5) | ((arfcn & 0x3ff) >> 8);
601 ci.chan_desc.oct4 = arfcn & 0xff;
602
Harald Welteed831842009-06-27 03:09:08 +0200603 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800604 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
605 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
606 dh->chan_nr = chan_nr;
607
608 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
Harald Welte59b04682009-06-10 05:40:52 +0800609 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
610 (u_int8_t *) &cm);
Harald Welteb90d7bd2009-12-17 00:31:10 +0100611 /* For compatibility with Phase 1 */
Harald Welte59b04682009-06-10 05:40:52 +0800612 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
613 (u_int8_t *) &ci);
Harald Welted2dd9de2009-08-30 15:37:11 +0900614
615 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
616 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
617 rc = build_encr_info(encr_info, lchan);
618 if (rc > 0)
619 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
620 }
621
Harald Welteb90d7bd2009-12-17 00:31:10 +0100622 switch (act_type) {
623 case RSL_ACT_INTER_ASYNC:
624 case RSL_ACT_INTER_SYNC:
625 msgb_tv_put(msg, RSL_IE_HANDO_REF, ho_ref);
626 break;
627 default:
628 break;
629 }
630
Harald Welte59b04682009-06-10 05:40:52 +0800631 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
632 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
633 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
634
635 msg->trx = lchan->ts->trx;
636
637 return abis_rsl_sendmsg(msg);
638}
639
Harald Welte8e770492009-07-29 11:38:15 +0200640/* Chapter 8.4.9: Modify channel mode on BTS side */
Harald Welte59b04682009-06-10 05:40:52 +0800641int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
642{
643 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200644 struct msgb *msg;
Harald Welte39274f42009-07-29 15:41:29 +0200645 int rc;
Harald Welte59b04682009-06-10 05:40:52 +0800646
647 u_int8_t chan_nr = lchan2chan_nr(lchan);
648 struct rsl_ie_chan_mode cm;
649
Harald Welte39274f42009-07-29 15:41:29 +0200650 rc = channel_mode_from_lchan(&cm, lchan);
651 if (rc < 0)
652 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800653
Harald Welteed831842009-06-27 03:09:08 +0200654 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800655 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
656 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
657 dh->chan_nr = chan_nr;
658
659 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
660 (u_int8_t *) &cm);
Harald Welted2dd9de2009-08-30 15:37:11 +0900661
662 if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
663 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
664 rc = build_encr_info(encr_info, lchan);
665 if (rc > 0)
666 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, rc, encr_info);
667 }
668
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +0100669 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
670 msgb_tlv_put(msg, RSL_IE_MR_CONFIG, sizeof(lchan->mr_conf),
671 (u_int8_t *) &lchan->mr_conf);
672 }
673
Harald Welted2dd9de2009-08-30 15:37:11 +0900674 msg->trx = lchan->ts->trx;
675
676 return abis_rsl_sendmsg(msg);
677}
678
679/* Chapter 8.4.6: Send the encryption command with given L3 info */
680int rsl_encryption_cmd(struct msgb *msg)
681{
682 struct abis_rsl_dchan_hdr *dh;
683 struct gsm_lchan *lchan = msg->lchan;
684 u_int8_t chan_nr = lchan2chan_nr(lchan);
685 u_int8_t encr_info[MAX_A5_KEY_LEN+2];
Sylvain Munaut01f1caf2009-09-27 11:13:18 +0200686 u_int8_t l3_len = msg->len;
Harald Welted2dd9de2009-08-30 15:37:11 +0900687 int rc;
688
689 /* First push the L3 IE tag and length */
690 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
691
692 /* then the link identifier (SAPI0, main sign link) */
693 msgb_tv_push(msg, RSL_IE_LINK_IDENT, 0);
694
695 /* then encryption information */
696 rc = build_encr_info(encr_info, lchan);
697 if (rc <= 0)
698 return rc;
699 msgb_tlv_push(msg, RSL_IE_ENCR_INFO, rc, encr_info);
700
701 /* and finally the DCHAN header */
702 dh = (struct abis_rsl_dchan_hdr *) msgb_push(msg, sizeof(*dh));
703 init_dchan_hdr(dh, RSL_MT_ENCR_CMD);
704 dh->chan_nr = chan_nr;
Harald Welte59b04682009-06-10 05:40:52 +0800705
706 msg->trx = lchan->ts->trx;
707
708 return abis_rsl_sendmsg(msg);
709}
710
Harald Welte85a163c2009-08-10 11:43:22 +0200711/* Chapter 8.4.5 / 4.6: Deactivate the SACCH after 04.08 RR CHAN RELEASE */
Harald Welteafe3c232009-07-19 18:36:49 +0200712int rsl_deact_sacch(struct gsm_lchan *lchan)
713{
714 struct abis_rsl_dchan_hdr *dh;
715 struct msgb *msg = rsl_msgb_alloc();
716
717 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
718 init_dchan_hdr(dh, RSL_MT_DEACTIVATE_SACCH);
719 dh->chan_nr = lchan2chan_nr(lchan);
720
721 msg->lchan = lchan;
722 msg->trx = lchan->ts->trx;
723
724 DEBUGP(DRSL, "DEACTivate SACCH CMD channel=%s chan_nr=0x%02x\n",
725 gsm_ts_name(lchan->ts), dh->chan_nr);
726
727 return abis_rsl_sendmsg(msg);
728}
729
Harald Welte85a163c2009-08-10 11:43:22 +0200730/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
731int rsl_rf_chan_release(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800732{
733 struct abis_rsl_dchan_hdr *dh;
734 struct msgb *msg = rsl_msgb_alloc();
735
736 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
737 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
738 dh->chan_nr = lchan2chan_nr(lchan);
739
740 msg->lchan = lchan;
741 msg->trx = lchan->ts->trx;
742
Harald Welte85a163c2009-08-10 11:43:22 +0200743 DEBUGP(DRSL, "RF Channel Release CMD channel=%s chan_nr=0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +0800744 gsm_ts_name(lchan->ts), dh->chan_nr);
745
Harald Welte85a163c2009-08-10 11:43:22 +0200746 /* BTS will respond by RF CHAN REL ACK */
Harald Welte59b04682009-06-10 05:40:52 +0800747 return abis_rsl_sendmsg(msg);
748}
749
750int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
751 u_int8_t *ms_ident, u_int8_t chan_needed)
752{
753 struct abis_rsl_dchan_hdr *dh;
754 struct msgb *msg = rsl_msgb_alloc();
755
756 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
757 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
758 dh->chan_nr = RSL_CHAN_PCH_AGCH;
759
760 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
761 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
762 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
763
764 msg->trx = bts->c0;
765
766 return abis_rsl_sendmsg(msg);
767}
768
769int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
770 struct gsm_subscriber *subscr)
771{
772#if 0
773 u_int8_t mi[128];
774 unsigned int mi_len;
775 u_int8_t paging_group;
776#endif
777
778 return -1;
779}
780
781int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
782{
783 int i, len = strlen(str_in);
784
785 for (i = 0; i < len; i++) {
786 int num = str_in[i] - 0x30;
787 if (num < 0 || num > 9)
788 return -1;
789 if (i % 2 == 0)
790 bcd_out[i/2] = num;
791 else
792 bcd_out[i/2] |= (num << 4);
793 }
794
795 return 0;
796}
797
798/* Chapter 8.5.6 */
799int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
800{
801 struct msgb *msg = rsl_msgb_alloc();
802 struct abis_rsl_dchan_hdr *dh;
803 u_int8_t buf[MACBLOCK_SIZE];
804
805 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
806 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
807 dh->chan_nr = RSL_CHAN_PCH_AGCH;
808
809 switch (bts->type) {
810 case GSM_BTS_TYPE_BS11:
811 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
812 break;
813 default:
814 /* If phase 2, construct a FULL_IMM_ASS_INFO */
815 pad_macblock(buf, val, len);
816 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
817 break;
818 }
819
820 msg->trx = bts->c0;
821
822 return abis_rsl_sendmsg(msg);
823}
824
Harald Welte4684e632009-08-10 09:51:40 +0200825/* Send Siemens specific MS RF Power Capability Indication */
Harald Welte12090752009-08-10 10:07:33 +0200826int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci)
Harald Welte4684e632009-08-10 09:51:40 +0200827{
828 struct msgb *msg = rsl_msgb_alloc();
829 struct abis_rsl_dchan_hdr *dh;
830
831 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
832 init_dchan_hdr(dh, RSL_MT_SIEMENS_MRPCI);
Harald Welte874a5b42009-08-10 11:26:14 +0200833 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
Harald Welte4684e632009-08-10 09:51:40 +0200834 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte12090752009-08-10 10:07:33 +0200835 msgb_tv_put(msg, RSL_IE_SIEMENS_MRPCI, *(u_int8_t *)mrpci);
Harald Welte4684e632009-08-10 09:51:40 +0200836
Harald Welte874a5b42009-08-10 11:26:14 +0200837 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x TX Siemens MRPCI 0x%02x\n",
838 gsm_ts_name(lchan->ts), dh->chan_nr, *(u_int8_t *)mrpci);
839
840 msg->trx = lchan->ts->trx;
841
Harald Welte4684e632009-08-10 09:51:40 +0200842 return abis_rsl_sendmsg(msg);
843}
844
845
Harald Welte59b04682009-06-10 05:40:52 +0800846/* Send "DATA REQUEST" message with given L3 Info payload */
847/* Chapter 8.3.1 */
848int rsl_data_request(struct msgb *msg, u_int8_t link_id)
849{
850 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
851 struct abis_rsl_rll_hdr *rh;
852
853 if (msg->lchan == NULL) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100854 LOGP(DRSL, LOGL_ERROR, "cannot send DATA REQUEST to unknown lchan\n");
Harald Welte59b04682009-06-10 05:40:52 +0800855 return -EINVAL;
856 }
857
858 /* First push the L3 IE tag and length */
859 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
860
861 /* Then push the RSL header */
862 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
863 init_llm_hdr(rh, RSL_MT_DATA_REQ);
864 rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
865 rh->chan_nr = lchan2chan_nr(msg->lchan);
866 rh->link_id = link_id;
867
868 msg->trx = msg->lchan->ts->trx;
869
870 return abis_rsl_sendmsg(msg);
871}
872
Harald Welteed9a5ab2009-08-09 13:47:35 +0200873/* Send "ESTABLISH REQUEST" message with given L3 Info payload */
874/* Chapter 8.3.1 */
875int rsl_establish_request(struct gsm_lchan *lchan, u_int8_t link_id)
876{
877 struct msgb *msg = rsl_msgb_alloc();
878 struct abis_rsl_rll_hdr *rh;
879
880 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
Harald Welte61402172009-08-09 14:13:58 +0200881 init_llm_hdr(rh, RSL_MT_EST_REQ);
Harald Welteed9a5ab2009-08-09 13:47:35 +0200882 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
883 rh->chan_nr = lchan2chan_nr(lchan);
884 rh->link_id = link_id;
885
886 msg->trx = lchan->ts->trx;
887
888 return abis_rsl_sendmsg(msg);
889}
890
Harald Welte0f2e3c12009-08-08 13:15:07 +0200891/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
892 This is what higher layers should call. The BTS then responds with
893 RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
894 which in turn is acknowledged by RSL CHANNEL RELEASE ACK, which calls
895 lchan_free() */
896int rsl_release_request(struct gsm_lchan *lchan, u_int8_t link_id)
897{
898 struct msgb *msg = rsl_msgb_alloc();
899 struct abis_rsl_rll_hdr *rh;
900
901 rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
902 init_llm_hdr(rh, RSL_MT_REL_REQ);
903 //rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
904 rh->chan_nr = lchan2chan_nr(lchan);
905 rh->link_id = link_id;
Harald Weltead738562009-08-10 00:19:36 +0200906 msgb_tv_put(msg, RSL_IE_RELEASE_MODE, 0); /* normal release */
Harald Welte0f2e3c12009-08-08 13:15:07 +0200907
908 msg->trx = lchan->ts->trx;
909
910 return abis_rsl_sendmsg(msg);
911}
912
Harald Welte59b04682009-06-10 05:40:52 +0800913/* Chapter 8.4.2: Channel Activate Acknowledge */
914static int rsl_rx_chan_act_ack(struct msgb *msg)
915{
916 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
917
918 /* BTS has confirmed channel activation, we now need
919 * to assign the activated channel to the MS */
920 if (rslh->ie_chan != RSL_IE_CHAN_NR)
921 return -EINVAL;
Harald Welte6720a432009-11-29 22:45:52 +0100922
Harald Welte4baa9c52009-12-21 13:27:11 +0100923 msg->lchan->state = LCHAN_S_ACTIVE;
924
Harald Welte6720a432009-11-29 22:45:52 +0100925 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_ACK, msg->lchan);
926
Harald Welte59b04682009-06-10 05:40:52 +0800927 return 0;
928}
929
930/* Chapter 8.4.3: Channel Activate NACK */
931static int rsl_rx_chan_act_nack(struct msgb *msg)
932{
933 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
934 struct tlv_parsed tp;
935
936 /* BTS has rejected channel activation ?!? */
937 if (dh->ie_chan != RSL_IE_CHAN_NR)
938 return -EINVAL;
939
940 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
941 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
Harald Weltef1a168d2009-07-28 17:58:09 +0200942 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
943 TLVP_LEN(&tp, RSL_IE_CAUSE));
944
Harald Welte4baa9c52009-12-21 13:27:11 +0100945 msg->lchan->state = LCHAN_S_NONE;
946
Harald Welte6720a432009-11-29 22:45:52 +0100947 dispatch_signal(SS_LCHAN, S_LCHAN_ACTIVATE_NACK, msg->lchan);
948
Harald Weltecddb9802009-08-09 19:50:08 +0200949 lchan_free(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800950 return 0;
951}
952
953/* Chapter 8.4.4: Connection Failure Indication */
954static int rsl_rx_conn_fail(struct msgb *msg)
955{
956 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
957 struct tlv_parsed tp;
958
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100959 /* FIXME: print which channel */
960 LOGP(DRSL, LOGL_NOTICE, "CONNECTION FAIL: RELEASING\n");
Harald Welte59b04682009-06-10 05:40:52 +0800961
962 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
963
Harald Weltef1a168d2009-07-28 17:58:09 +0200964 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
965 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
966 TLVP_LEN(&tp, RSL_IE_CAUSE));
967
Harald Welte59b04682009-06-10 05:40:52 +0800968 /* FIXME: only free it after channel release ACK */
Harald Welte85a163c2009-08-10 11:43:22 +0200969 return rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800970}
971
Harald Weltec20bd1d2009-11-29 19:07:28 +0100972static void print_meas_rep_uni(struct gsm_meas_rep_unidir *mru,
973 const char *prefix)
974{
Harald Welte0e4fa782009-12-16 16:52:07 +0100975 DEBUGPC(DMEAS, "RXL-FULL-%s=%3ddBm RXL-SUB-%s=%3ddBm ",
976 prefix, rxlev2dbm(mru->full.rx_lev),
977 prefix, rxlev2dbm(mru->sub.rx_lev));
Harald Weltec20bd1d2009-11-29 19:07:28 +0100978 DEBUGPC(DMEAS, "RXQ-FULL-%s=%d RXQ-SUB-%s=%d ",
979 prefix, mru->full.rx_qual, prefix, mru->sub.rx_qual);
980}
981
982static void print_meas_rep(struct gsm_meas_rep *mr)
983{
Harald Welte0e4fa782009-12-16 16:52:07 +0100984 int i;
985
Harald Weltec20bd1d2009-11-29 19:07:28 +0100986 DEBUGP(DMEAS, "MEASUREMENT RESULT NR=%d ", mr->nr);
987
988 if (mr->flags & MEAS_REP_F_DL_DTX)
989 DEBUGPC(DMEAS, "DTXd ");
990
991 print_meas_rep_uni(&mr->ul, "ul");
992 DEBUGPC(DMEAS, "BS_POWER=%d ", mr->bs_power);
993 if (mr->flags & MEAS_REP_F_MS_TO)
994 DEBUGPC(DMEAS, "MS_TO=%d ", mr->ms_timing_offset);
995
996 if (mr->flags & MEAS_REP_F_MS_L1) {
Harald Welte0e4fa782009-12-16 16:52:07 +0100997 DEBUGPC(DMEAS, "L1_MS_PWR=%3ddBm ", mr->ms_l1.pwr);
Harald Weltec20bd1d2009-11-29 19:07:28 +0100998 DEBUGPC(DMEAS, "L1_FPC=%u ",
999 mr->flags & MEAS_REP_F_FPC ? 1 : 0);
1000 DEBUGPC(DMEAS, "L1_TA=%u ", mr->ms_l1.ta);
1001 }
1002
1003 if (mr->flags & MEAS_REP_F_UL_DTX)
1004 DEBUGPC(DMEAS, "DTXu ");
1005 if (mr->flags & MEAS_REP_F_BA1)
1006 DEBUGPC(DMEAS, "BA1 ");
1007 if (!(mr->flags & MEAS_REP_F_DL_VALID))
1008 DEBUGPC(DMEAS, "NOT VALID ");
1009 else
1010 print_meas_rep_uni(&mr->dl, "dl");
1011
1012 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", mr->num_cell);
Harald Welte0b833f82009-12-19 18:33:05 +01001013 if (mr->num_cell == 7)
1014 return;
Harald Welte0e4fa782009-12-16 16:52:07 +01001015 for (i = 0; i < mr->num_cell; i++) {
1016 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
1017 DEBUGP(DMEAS, "ARFCN=%u BSIC=%u => %d dBm\n", mrc->arfcn, mrc->bsic,
1018 rxlev2dbm(mrc->rxlev));
1019 }
Harald Weltec20bd1d2009-11-29 19:07:28 +01001020}
1021
Harald Welte59b04682009-06-10 05:40:52 +08001022static int rsl_rx_meas_res(struct msgb *msg)
1023{
1024 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1025 struct tlv_parsed tp;
Harald Weltef9476812009-12-15 21:36:05 +01001026 struct gsm_meas_rep *mr = lchan_next_meas_rep(msg->lchan);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001027 u_int8_t len;
1028 const u_int8_t *val;
1029 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001030
Harald Welte4baa9c52009-12-21 13:27:11 +01001031 /* check if this channel is actually active */
1032 /* FIXME: maybe this check should be way more generic/centralized */
1033 if (msg->lchan->state != LCHAN_S_ACTIVE)
1034 return 0;
1035
Harald Weltef9476812009-12-15 21:36:05 +01001036 memset(mr, 0, sizeof(*mr));
Harald Welteaa0efa12009-12-16 23:29:34 +01001037 mr->lchan = msg->lchan;
Harald Welte4efcc542009-11-30 19:16:47 +01001038
Harald Welte59b04682009-06-10 05:40:52 +08001039 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1040
Harald Weltec20bd1d2009-11-29 19:07:28 +01001041 if (!TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR) ||
1042 !TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS) ||
1043 !TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
1044 return -EIO;
1045
1046 /* Mandatory Parts */
Harald Weltef9476812009-12-15 21:36:05 +01001047 mr->nr = *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001048
1049 len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
1050 val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
1051 if (len >= 3) {
1052 if (val[0] & 0x40)
Harald Weltef9476812009-12-15 21:36:05 +01001053 mr->flags |= MEAS_REP_F_DL_DTX;
1054 mr->ul.full.rx_lev = val[0] & 0x3f;
1055 mr->ul.sub.rx_lev = val[1] & 0x3f;
1056 mr->ul.full.rx_qual = val[2]>>3 & 0x7;
1057 mr->ul.sub.rx_qual = val[2] & 0x7;
Harald Welte59b04682009-06-10 05:40:52 +08001058 }
Harald Weltec20bd1d2009-11-29 19:07:28 +01001059
Harald Weltef9476812009-12-15 21:36:05 +01001060 mr->bs_power = *TLVP_VAL(&tp, RSL_IE_BS_POWER);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001061
1062 /* Optional Parts */
Harald Welte59b04682009-06-10 05:40:52 +08001063 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Weltef9476812009-12-15 21:36:05 +01001064 mr->ms_timing_offset =
Harald Weltec20bd1d2009-11-29 19:07:28 +01001065 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET);
1066
Harald Weltea1467eb2009-06-20 18:44:35 +02001067 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Weltec20bd1d2009-11-29 19:07:28 +01001068 val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Weltef9476812009-12-15 21:36:05 +01001069 mr->flags |= MEAS_REP_F_MS_L1;
1070 mr->ms_l1.pwr = ms_pwr_dbm(msg->trx->bts->band, val[0] >> 3);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001071 if (val[0] & 0x04)
Harald Weltef9476812009-12-15 21:36:05 +01001072 mr->flags |= MEAS_REP_F_FPC;
1073 mr->ms_l1.ta = val[1];
Harald Weltea1467eb2009-06-20 18:44:35 +02001074 }
Harald Welte59b04682009-06-10 05:40:52 +08001075 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Holger Hans Peter Freyther6d0c8b42009-10-22 15:43:55 +02001076 msg->l3h = (u_int8_t *) TLVP_VAL(&tp, RSL_IE_L3_INFO);
Harald Weltef9476812009-12-15 21:36:05 +01001077 rc = gsm48_parse_meas_rep(mr, msg);
Harald Weltec20bd1d2009-11-29 19:07:28 +01001078 if (rc < 0)
1079 return rc;
1080 }
1081
Harald Weltef9476812009-12-15 21:36:05 +01001082 print_meas_rep(mr);
Harald Welte59b04682009-06-10 05:40:52 +08001083
Harald Weltef9476812009-12-15 21:36:05 +01001084 dispatch_signal(SS_LCHAN, S_LCHAN_MEAS_REP, mr);
Harald Welte4efcc542009-11-30 19:16:47 +01001085
Harald Welte59b04682009-06-10 05:40:52 +08001086 return 0;
1087}
1088
Harald Welte6720a432009-11-29 22:45:52 +01001089/* Chapter 8.4.7 */
1090static int rsl_rx_hando_det(struct msgb *msg)
1091{
1092 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1093 struct tlv_parsed tp;
1094
1095 DEBUGP(DRSL, "HANDOVER DETECT ");
1096
1097 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
1098
1099 if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
1100 DEBUGPC(DRSL, "access delay = %u\n",
1101 *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
1102 else
1103 DEBUGPC(DRSL, "\n");
1104
1105 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_DETECT, msg->lchan);
1106
1107 return 0;
1108}
1109
Harald Welte59b04682009-06-10 05:40:52 +08001110static int abis_rsl_rx_dchan(struct msgb *msg)
1111{
1112 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1113 int rc = 0;
1114 char *ts_name;
1115
1116 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1117 ts_name = gsm_ts_name(msg->lchan->ts);
1118
Harald Welte02993682009-06-27 02:53:10 +02001119 if (rslh->c.msg_type != RSL_MT_MEAS_RES)
1120 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ", ts_name, rslh->chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +08001121
1122 switch (rslh->c.msg_type) {
1123 case RSL_MT_CHAN_ACTIV_ACK:
1124 DEBUGPC(DRSL, "CHANNEL ACTIVATE ACK\n");
1125 rc = rsl_rx_chan_act_ack(msg);
1126 break;
1127 case RSL_MT_CHAN_ACTIV_NACK:
1128 DEBUGPC(DRSL, "CHANNEL ACTIVATE NACK\n");
1129 rc = rsl_rx_chan_act_nack(msg);
1130 break;
1131 case RSL_MT_CONN_FAIL:
1132 rc = rsl_rx_conn_fail(msg);
1133 break;
1134 case RSL_MT_MEAS_RES:
1135 rc = rsl_rx_meas_res(msg);
1136 break;
Harald Welte6720a432009-11-29 22:45:52 +01001137 case RSL_MT_HANDO_DET:
1138 rc = rsl_rx_hando_det(msg);
1139 break;
Harald Welte59b04682009-06-10 05:40:52 +08001140 case RSL_MT_RF_CHAN_REL_ACK:
1141 DEBUGPC(DRSL, "RF CHANNEL RELEASE ACK\n");
Harald Welte4baa9c52009-12-21 13:27:11 +01001142 msg->lchan->state = LCHAN_S_NONE;
Harald Welte59b04682009-06-10 05:40:52 +08001143 lchan_free(msg->lchan);
1144 break;
1145 case RSL_MT_MODE_MODIFY_ACK:
1146 DEBUGPC(DRSL, "CHANNEL MODE MODIFY ACK\n");
1147 break;
1148 case RSL_MT_MODE_MODIFY_NACK:
1149 DEBUGPC(DRSL, "CHANNEL MODE MODIFY NACK\n");
1150 break;
Harald Welteaed946e2009-10-24 10:29:22 +02001151 case RSL_MT_IPAC_PDCH_ACT_ACK:
1152 DEBUGPC(DRSL, "IPAC PDCH ACT ACK\n");
1153 break;
1154 case RSL_MT_IPAC_PDCH_ACT_NACK:
1155 DEBUGPC(DRSL, "IPAC PDCH ACT NACK\n");
1156 break;
1157 case RSL_MT_IPAC_PDCH_DEACT_ACK:
1158 DEBUGPC(DRSL, "IPAC PDCH DEACT ACK\n");
1159 break;
1160 case RSL_MT_IPAC_PDCH_DEACT_NACK:
1161 DEBUGPC(DRSL, "IPAC PDCH DEACT NACK\n");
1162 break;
Harald Welte59b04682009-06-10 05:40:52 +08001163 case RSL_MT_PHY_CONTEXT_CONF:
1164 case RSL_MT_PREPROC_MEAS_RES:
1165 case RSL_MT_TALKER_DET:
1166 case RSL_MT_LISTENER_DET:
1167 case RSL_MT_REMOTE_CODEC_CONF_REP:
1168 case RSL_MT_MR_CODEC_MOD_ACK:
1169 case RSL_MT_MR_CODEC_MOD_NACK:
1170 case RSL_MT_MR_CODEC_MOD_PER:
1171 DEBUGPC(DRSL, "Unimplemented Abis RSL DChan msg 0x%02x\n",
1172 rslh->c.msg_type);
1173 break;
1174 default:
1175 DEBUGPC(DRSL, "unknown Abis RSL DChan msg 0x%02x\n",
1176 rslh->c.msg_type);
1177 return -EINVAL;
1178 }
1179
1180 return rc;
1181}
1182
1183static int rsl_rx_error_rep(struct msgb *msg)
1184{
1185 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Weltef1a168d2009-07-28 17:58:09 +02001186 struct tlv_parsed tp;
Harald Welte59b04682009-06-10 05:40:52 +08001187
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001188 LOGP(DRSL, LOGL_ERROR, "ERROR REPORT ");
Harald Weltef1a168d2009-07-28 17:58:09 +02001189
1190 rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh));
1191
1192 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
1193 print_rsl_cause(TLVP_VAL(&tp, RSL_IE_CAUSE),
1194 TLVP_LEN(&tp, RSL_IE_CAUSE));
1195
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001196 LOGPC(DRSL, LOGL_ERROR, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001197
1198 return 0;
1199}
1200
1201static int abis_rsl_rx_trx(struct msgb *msg)
1202{
1203 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1204 int rc = 0;
1205
1206 switch (rslh->msg_type) {
1207 case RSL_MT_ERROR_REPORT:
1208 rc = rsl_rx_error_rep(msg);
1209 break;
1210 case RSL_MT_RF_RES_IND:
1211 /* interference on idle channels of TRX */
1212 //DEBUGP(DRSL, "TRX: RF Interference Indication\n");
1213 break;
1214 case RSL_MT_OVERLOAD:
1215 /* indicate CCCH / ACCH / processor overload */
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001216 LOGP(DRSL, LOGL_ERROR, "TRX: CCCH/ACCH/CPU Overload\n");
Harald Welte59b04682009-06-10 05:40:52 +08001217 break;
1218 default:
1219 DEBUGP(DRSL, "Unknown Abis RSL TRX message type 0x%02x\n",
1220 rslh->msg_type);
1221 return -EINVAL;
1222 }
1223 return rc;
1224}
1225
Harald Welte427dbc42009-08-10 00:26:10 +02001226/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
1227static void t3101_expired(void *data)
1228{
1229 struct gsm_lchan *lchan = data;
1230
Harald Welte85a163c2009-08-10 11:43:22 +02001231 rsl_rf_chan_release(lchan);
Harald Welte427dbc42009-08-10 00:26:10 +02001232}
1233
Harald Welte59b04682009-06-10 05:40:52 +08001234/* MS has requested a channel on the RACH */
1235static int rsl_rx_chan_rqd(struct msgb *msg)
1236{
1237 struct gsm_bts *bts = msg->trx->bts;
1238 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
1239 struct gsm48_req_ref *rqd_ref;
1240 struct gsm48_imm_ass ia;
1241 enum gsm_chan_t lctype;
1242 enum gsm_chreq_reason_t chreq_reason;
1243 struct gsm_lchan *lchan;
1244 u_int8_t rqd_ta;
1245 int ret;
1246
1247 u_int16_t arfcn;
1248 u_int8_t ts_number, subch;
1249
1250 /* parse request reference to be used in immediate assign */
1251 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
1252 return -EINVAL;
1253
1254 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
1255
1256 /* parse access delay and use as TA */
1257 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
1258 return -EINVAL;
1259 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
1260
1261 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
1262 * request reference RA */
Holger Hans Peter Freyther96c89822009-11-16 17:12:38 +01001263 lctype = get_ctype_by_chreq(bts, rqd_ref->ra, bts->network->neci);
1264 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra, bts->network->neci);
Harald Welte59b04682009-06-10 05:40:52 +08001265
Harald Weltebdbb7442009-12-22 19:07:32 +01001266 counter_inc(bts->network->stats.chreq.total);
Harald Welte3edc5a92009-12-22 00:41:05 +01001267
Harald Welte59b04682009-06-10 05:40:52 +08001268 /* check availability / allocate channel */
1269 lchan = lchan_alloc(bts, lctype);
1270 if (!lchan) {
Harald Welte059b6a82009-12-12 20:53:23 +01001271 DEBUGP(DRSL, "CHAN RQD: no resources for %s 0x%x\n",
1272 gsm_lchan_name(lctype), rqd_ref->ra);
Harald Weltebdbb7442009-12-22 19:07:32 +01001273 counter_inc(bts->network->stats.chreq.no_channel);
Harald Welte59b04682009-06-10 05:40:52 +08001274 /* FIXME: send some kind of reject ?!? */
1275 return -ENOMEM;
1276 }
1277
1278 ts_number = lchan->ts->nr;
1279 arfcn = lchan->ts->trx->arfcn;
1280 subch = lchan->nr;
1281
Harald Welted2dd9de2009-08-30 15:37:11 +09001282 lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
Harald Welte (local)cbd46102009-08-13 10:14:26 +02001283 lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
Harald Welte9a229e12009-08-10 00:45:40 +02001284 lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
Harald Welte39274f42009-07-29 15:41:29 +02001285 lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
Harald Welte77234e12009-08-28 23:28:28 +09001286 lchan->tch_mode = GSM48_CMODE_SIGN;
Harald Welteb90d7bd2009-12-17 00:31:10 +01001287 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001288
1289 /* create IMMEDIATE ASSIGN 04.08 messge */
1290 memset(&ia, 0, sizeof(ia));
1291 ia.l2_plen = 0x2d;
1292 ia.proto_discr = GSM48_PDISC_RR;
1293 ia.msg_type = GSM48_MT_RR_IMM_ASS;
1294 ia.page_mode = GSM48_PM_SAME;
1295 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
1296 ia.chan_desc.h0.h = 0;
1297 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
1298 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
Harald Welte63d23c82009-07-21 20:55:56 +02001299 ia.chan_desc.h0.tsc = bts->tsc;
Harald Welte59b04682009-06-10 05:40:52 +08001300 /* use request reference extracted from CHAN_RQD */
1301 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
1302 ia.timing_advance = rqd_ta;
1303 ia.mob_alloc_len = 0;
1304
1305 DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %s "
1306 "chan_nr=0x%02x r=%s ra=0x%02x\n",
1307 arfcn, ts_number, subch, gsm_lchan_name(lchan->type),
1308 ia.chan_desc.chan_nr, gsm_chreq_name(chreq_reason),
1309 rqd_ref->ra);
1310
Harald Welte427dbc42009-08-10 00:26:10 +02001311 /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1312 lchan->T3101.cb = t3101_expired;
1313 lchan->T3101.data = lchan;
Holger Hans Peter Freyther26ba2e72009-11-21 21:18:38 +01001314 bsc_schedule_timer(&lchan->T3101, bts->network->T3101, 0);
Harald Welte59b04682009-06-10 05:40:52 +08001315
1316 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
1317 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
1318
1319 return ret;
1320}
1321
1322/* MS has requested a channel on the RACH */
1323static int rsl_rx_ccch_load(struct msgb *msg)
1324{
1325 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1326 u_int16_t pg_buf_space;
1327 u_int16_t rach_slot_count = -1;
1328 u_int16_t rach_busy_count = -1;
1329 u_int16_t rach_access_count = -1;
1330
1331 switch (rslh->data[0]) {
1332 case RSL_IE_PAGING_LOAD:
1333 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
1334 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
1335 break;
1336 case RSL_IE_RACH_LOAD:
1337 if (msg->data_len >= 7) {
1338 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1339 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1340 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1341 }
1342 break;
1343 default:
1344 break;
1345 }
1346
1347 return 0;
1348}
1349
1350static int abis_rsl_rx_cchan(struct msgb *msg)
1351{
1352 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1353 int rc = 0;
1354
1355 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1356
1357 switch (rslh->c.msg_type) {
1358 case RSL_MT_CHAN_RQD:
1359 /* MS has requested a channel on the RACH */
1360 rc = rsl_rx_chan_rqd(msg);
1361 break;
1362 case RSL_MT_CCCH_LOAD_IND:
1363 /* current load on the CCCH */
1364 rc = rsl_rx_ccch_load(msg);
1365 break;
1366 case RSL_MT_DELETE_IND:
1367 /* CCCH overloaded, IMM_ASSIGN was dropped */
1368 case RSL_MT_CBCH_LOAD_IND:
1369 /* current load on the CBCH */
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001370 LOGP(DRSL, LOGL_NOTICE, "Unimplemented Abis RSL TRX message "
1371 "type 0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001372 break;
1373 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001374 LOGP(DRSL, LOGL_NOTICE, "Unknown Abis RSL TRX message type "
1375 "0x%02x\n", rslh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001376 return -EINVAL;
1377 }
1378
1379 return rc;
1380}
1381
1382static int rsl_rx_rll_err_ind(struct msgb *msg)
1383{
1384 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1385 u_int8_t *rlm_cause = rllh->data;
1386
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001387 LOGP(DRLL, LOGL_ERROR, "ERROR INDICATION cause=0x%02x\n", rlm_cause[1]);
Harald Welteed9a5ab2009-08-09 13:47:35 +02001388
1389 rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
Harald Welte59b04682009-06-10 05:40:52 +08001390
Harald Welte692f5852009-07-04 09:40:05 +02001391 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED)
Harald Welte85a163c2009-08-10 11:43:22 +02001392 return rsl_rf_chan_release(msg->lchan);
Harald Welte692f5852009-07-04 09:40:05 +02001393
Harald Welte59b04682009-06-10 05:40:52 +08001394 return 0;
1395}
1396
1397/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
1398 0x02, 0x06,
1399 0x01, 0x20,
1400 0x02, 0x00,
1401 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1402
1403static int abis_rsl_rx_rll(struct msgb *msg)
1404{
1405 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1406 int rc = 0;
1407 char *ts_name;
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001408 u_int8_t sapi = rllh->link_id & 7;
Harald Welte59b04682009-06-10 05:40:52 +08001409
1410 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
1411 ts_name = gsm_ts_name(msg->lchan->ts);
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001412 DEBUGP(DRLL, "channel=%s chan_nr=0x%02x sapi=%u ", ts_name,
1413 rllh->chan_nr, sapi);
Harald Welte59b04682009-06-10 05:40:52 +08001414
1415 switch (rllh->c.msg_type) {
1416 case RSL_MT_DATA_IND:
1417 DEBUGPC(DRLL, "DATA INDICATION\n");
1418 if (msgb_l2len(msg) >
1419 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1420 rllh->data[0] == RSL_IE_L3_INFO) {
1421 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001422 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001423 }
1424 break;
1425 case RSL_MT_EST_IND:
1426 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
Harald Welte427dbc42009-08-10 00:26:10 +02001427 /* lchan is established, stop T3101 */
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001428 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
Harald Welte427dbc42009-08-10 00:26:10 +02001429 bsc_del_timer(&msg->lchan->T3101);
Harald Welte59b04682009-06-10 05:40:52 +08001430 if (msgb_l2len(msg) >
1431 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1432 rllh->data[0] == RSL_IE_L3_INFO) {
1433 msg->l3h = &rllh->data[3];
Harald Welte (local)64994ce2009-08-14 11:41:12 +02001434 return gsm0408_rcvmsg(msg, rllh->link_id);
Harald Welte59b04682009-06-10 05:40:52 +08001435 }
1436 break;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001437 case RSL_MT_EST_CONF:
Harald Welte61402172009-08-09 14:13:58 +02001438 DEBUGPC(DRLL, "ESTABLISH CONFIRM\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001439 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001440 rll_indication(msg->lchan, rllh->link_id,
1441 BSC_RLLR_IND_EST_CONF);
1442 break;
Harald Welte59b04682009-06-10 05:40:52 +08001443 case RSL_MT_REL_IND:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001444 /* BTS informs us of having received DISC from MS */
Harald Welteb6601442009-08-04 02:50:21 +02001445 DEBUGPC(DRLL, "RELEASE INDICATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001446 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welteed9a5ab2009-08-09 13:47:35 +02001447 rll_indication(msg->lchan, rllh->link_id,
1448 BSC_RLLR_IND_REL_IND);
Harald Welte0f2e3c12009-08-08 13:15:07 +02001449 /* we can now releae the channel on the BTS/Abis side */
Harald Welte85a163c2009-08-10 11:43:22 +02001450 /* FIXME: officially we need to start T3111 and wait for
1451 * some grace period */
1452 rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001453 break;
1454 case RSL_MT_REL_CONF:
Harald Welte0f2e3c12009-08-08 13:15:07 +02001455 /* BTS informs us of having received UA from MS,
1456 * in response to DISC that we've sent earlier */
Harald Welteb6601442009-08-04 02:50:21 +02001457 DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +01001458 msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
Harald Welte0f2e3c12009-08-08 13:15:07 +02001459 /* we can now releae the channel on the BTS/Abis side */
Harald Welte85a163c2009-08-10 11:43:22 +02001460 /* FIXME: officially we need to start T3111 and wait for
1461 * some grace period */
1462 rsl_rf_chan_release(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001463 break;
1464 case RSL_MT_ERROR_IND:
Harald Welte59b04682009-06-10 05:40:52 +08001465 rc = rsl_rx_rll_err_ind(msg);
1466 break;
1467 case RSL_MT_UNIT_DATA_IND:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001468 LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message "
1469 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001470 break;
1471 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001472 LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message "
1473 "type 0x%02x\n", rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001474 }
Harald Welte59b04682009-06-10 05:40:52 +08001475 return rc;
1476}
1477
Harald Welteb284b472009-12-02 01:58:23 +05301478static u_int8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
Harald Welte98d79f92009-07-28 18:11:56 +02001479{
Harald Welteb284b472009-12-02 01:58:23 +05301480 switch (lchan->tch_mode) {
Harald Welte98d79f92009-07-28 18:11:56 +02001481 case GSM48_CMODE_SPEECH_V1:
Harald Welteb284b472009-12-02 01:58:23 +05301482 switch (lchan->type) {
1483 case GSM_LCHAN_TCH_F:
1484 return 0x00;
1485 case GSM_LCHAN_TCH_H:
1486 return 0x03;
1487 default:
1488 break;
1489 }
Harald Welte98d79f92009-07-28 18:11:56 +02001490 case GSM48_CMODE_SPEECH_EFR:
Harald Welteb284b472009-12-02 01:58:23 +05301491 switch (lchan->type) {
1492 case GSM_LCHAN_TCH_F:
1493 return 0x01;
1494 /* there's no half-rate EFR */
1495 default:
1496 break;
1497 }
Harald Welte98d79f92009-07-28 18:11:56 +02001498 case GSM48_CMODE_SPEECH_AMR:
Harald Welteb284b472009-12-02 01:58:23 +05301499 switch (lchan->type) {
1500 case GSM_LCHAN_TCH_F:
1501 return 0x02;
1502 case GSM_LCHAN_TCH_H:
1503 return 0x05;
1504 default:
1505 break;
1506 }
1507 default:
1508 break;
Harald Welte98d79f92009-07-28 18:11:56 +02001509 }
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001510 LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
Harald Welteb284b472009-12-02 01:58:23 +05301511 "tch_mode == 0x%02x\n", lchan->tch_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001512 return 0;
Harald Welte98d79f92009-07-28 18:11:56 +02001513}
1514
Harald Welte59b04682009-06-10 05:40:52 +08001515/* ip.access specific RSL extensions */
Harald Weltebffa4992009-12-19 16:42:06 +01001516static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
1517{
1518 struct in_addr ip;
1519 u_int16_t port, conn_id;
1520
1521 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
1522 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_IP));
1523 DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
1524 lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
1525 }
1526
1527 if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
1528 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_LOCAL_PORT));
1529 port = ntohs(port);
1530 DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
1531 lchan->abis_ip.bound_port = port;
1532 }
1533
1534 if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
1535 conn_id = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_CONN_ID));
1536 conn_id = ntohs(conn_id);
1537 DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
1538 lchan->abis_ip.conn_id = conn_id;
1539 }
1540
1541 if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
1542 lchan->abis_ip.rtp_payload2 =
1543 *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
1544 DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
1545 lchan->abis_ip.rtp_payload2);
1546 }
1547
1548 if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
1549 lchan->abis_ip.speech_mode =
1550 *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
1551 DEBUGPC(DRSL, "speech_mode=0x%02x ",
1552 lchan->abis_ip.speech_mode);
1553 }
1554
1555 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
1556 ip.s_addr = *((u_int32_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_IP));
1557 DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
1558 lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
1559 }
1560
1561 if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
1562 port = *((u_int16_t *) TLVP_VAL(tv, RSL_IE_IPAC_REMOTE_PORT));
1563 port = ntohs(port);
1564 DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
1565 lchan->abis_ip.connect_port = port;
1566 }
1567}
1568
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001569int rsl_ipacc_crcx(struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +08001570{
1571 struct msgb *msg = rsl_msgb_alloc();
1572 struct abis_rsl_dchan_hdr *dh;
1573
1574 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001575 init_dchan_hdr(dh, RSL_MT_IPAC_CRCX);
Harald Welte59b04682009-06-10 05:40:52 +08001576 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1577 dh->chan_nr = lchan2chan_nr(lchan);
1578
Harald Welte98d79f92009-07-28 18:11:56 +02001579 /* 0x1- == receive-only, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001580 lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
1581 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001582
Harald Weltefb4a9e92009-07-29 12:12:18 +02001583 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_BIND "
1584 "speech_mode=0x%02x\n", gsm_ts_name(lchan->ts),
Harald Weltebffa4992009-12-19 16:42:06 +01001585 dh->chan_nr, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001586
Harald Welte59b04682009-06-10 05:40:52 +08001587 msg->trx = lchan->ts->trx;
1588
1589 return abis_rsl_sendmsg(msg);
1590}
1591
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001592int rsl_ipacc_mdcx(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
Harald Weltebffa4992009-12-19 16:42:06 +01001593 u_int8_t rtp_payload2)
Harald Welte59b04682009-06-10 05:40:52 +08001594{
1595 struct msgb *msg = rsl_msgb_alloc();
1596 struct abis_rsl_dchan_hdr *dh;
Harald Weltebffa4992009-12-19 16:42:06 +01001597 u_int32_t *att_ip;
Harald Welte98d79f92009-07-28 18:11:56 +02001598 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +08001599
1600 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001601 init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
Harald Welte59b04682009-06-10 05:40:52 +08001602 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1603 dh->chan_nr = lchan2chan_nr(lchan);
1604
Harald Weltebffa4992009-12-19 16:42:06 +01001605 /* we need to store these now as MDCX_ACK does not return them :( */
1606 lchan->abis_ip.rtp_payload2 = rtp_payload2;
1607 lchan->abis_ip.connect_port = port;
1608 lchan->abis_ip.connect_ip = ip;
1609
Harald Weltefb4a9e92009-07-29 12:12:18 +02001610 /* 0x0- == both directions, 0x-1 == EFR codec */
Harald Weltebffa4992009-12-19 16:42:06 +01001611 lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
Harald Weltefb4a9e92009-07-29 12:12:18 +02001612
Harald Welte98d79f92009-07-28 18:11:56 +02001613 ia.s_addr = htonl(ip);
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001614 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_MDCX "
Harald Weltefb4a9e92009-07-29 12:12:18 +02001615 "IP=%s PORT=%d RTP_PAYLOAD2=%d CONN_ID=%d speech_mode=0x%02x\n",
Harald Weltebffa4992009-12-19 16:42:06 +01001616 gsm_ts_name(lchan->ts), dh->chan_nr, inet_ntoa(ia), port,
1617 rtp_payload2, lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001618
Harald Weltebffa4992009-12-19 16:42:06 +01001619 msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
1620 msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
1621 att_ip = (u_int32_t *) msgb_put(msg, sizeof(ip));
1622 *att_ip = ia.s_addr;
1623 msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
1624 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
Harald Welte98d79f92009-07-28 18:11:56 +02001625 if (rtp_payload2)
1626 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
1627
Harald Welte59b04682009-06-10 05:40:52 +08001628 msg->trx = lchan->ts->trx;
1629
1630 return abis_rsl_sendmsg(msg);
1631}
1632
Harald Welte9947d9f2009-12-20 16:51:09 +01001633/* tell BTS to connect RTP stream to our local RTP socket */
1634int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan)
1635{
1636 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
1637 int rc;
1638
1639 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1640 ntohs(rs->rtp.sin_local.sin_port),
1641 /* FIXME: use RTP payload of bound socket, not BTS*/
1642 lchan->abis_ip.rtp_payload2);
1643
1644 return rc;
1645}
1646
Harald Welteaed946e2009-10-24 10:29:22 +02001647int rsl_ipacc_pdch_activate(struct gsm_lchan *lchan)
1648{
1649 struct msgb *msg = rsl_msgb_alloc();
1650 struct abis_rsl_dchan_hdr *dh;
1651
1652 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1653 init_dchan_hdr(dh, RSL_MT_IPAC_PDCH_ACT);
1654 dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
1655 dh->chan_nr = lchan2chan_nr(lchan);
1656
1657 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x IPAC_PDCH_ACT\n",
1658 gsm_ts_name(lchan->ts), dh->chan_nr);
1659
1660 msg->trx = lchan->ts->trx;
1661
1662 return abis_rsl_sendmsg(msg);
1663}
1664
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001665static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001666{
1667 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1668 struct tlv_parsed tv;
Harald Welte87504212009-12-02 01:56:49 +05301669 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001670
1671 /* the BTS has acknowledged a local bind, it now tells us the IP
1672 * address and port number to which it has bound the given logical
1673 * channel */
1674
1675 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1676 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1677 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welteb9498952009-07-12 09:45:05 +02001678 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001679 LOGP(DRSL, LOGL_NOTICE, "mandatory IE missing");
Harald Welte59b04682009-06-10 05:40:52 +08001680 return -EINVAL;
1681 }
Harald Welte50517742009-12-20 15:42:44 +01001682
Harald Weltebffa4992009-12-19 16:42:06 +01001683 ipac_parse_rtp(lchan, &tv);
Harald Welte50517742009-12-20 15:42:44 +01001684
1685 /* in case we don't use direct BTS-to-BTS RTP */
1686 if (!ipacc_rtp_direct) {
1687 int rc;
1688 /* the BTS has successfully bound a TCH to a local ip/port,
1689 * which means we can connect our UDP socket to it */
1690 if (lchan->abis_ip.rtp_socket) {
1691 rtp_socket_free(lchan->abis_ip.rtp_socket);
1692 lchan->abis_ip.rtp_socket = NULL;
1693 }
1694
1695 lchan->abis_ip.rtp_socket = rtp_socket_create();
1696 if (!lchan->abis_ip.rtp_socket)
1697 goto out_err;
1698
1699 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1700 lchan->abis_ip.bound_ip,
1701 lchan->abis_ip.bound_port);
1702 if (rc < 0)
1703 goto out_err;
1704 }
1705
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001706 dispatch_signal(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001707
1708 return 0;
Harald Welte50517742009-12-20 15:42:44 +01001709out_err:
1710 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +08001711}
1712
Harald Weltebffa4992009-12-19 16:42:06 +01001713static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
1714{
1715 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1716 struct tlv_parsed tv;
1717 struct gsm_lchan *lchan = msg->lchan;
1718
1719 /* the BTS has acknowledged a remote connect request and
1720 * it now tells us the IP address and port number to which it has
1721 * connected the given logical channel */
1722
1723 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1724 ipac_parse_rtp(lchan, &tv);
1725 dispatch_signal(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
1726
1727 return 0;
1728}
1729
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001730static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001731{
1732 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1733 struct tlv_parsed tv;
Harald Welte50517742009-12-20 15:42:44 +01001734 struct gsm_lchan *lchan = msg->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001735
1736 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
Harald Welte59b04682009-06-10 05:40:52 +08001737
Harald Weltef1a168d2009-07-28 17:58:09 +02001738 if (TLVP_PRESENT(&tv, RSL_IE_CAUSE))
1739 print_rsl_cause(TLVP_VAL(&tv, RSL_IE_CAUSE),
1740 TLVP_LEN(&tv, RSL_IE_CAUSE));
Harald Welte59b04682009-06-10 05:40:52 +08001741
Harald Welte50517742009-12-20 15:42:44 +01001742 /* the BTS tells us a RTP stream has been disconnected */
1743 if (lchan->abis_ip.rtp_socket) {
1744 rtp_socket_free(lchan->abis_ip.rtp_socket);
1745 lchan->abis_ip.rtp_socket = NULL;
1746 }
1747
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001748 dispatch_signal(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
Harald Welteba4e58d2009-07-28 18:02:05 +02001749
Harald Welte59b04682009-06-10 05:40:52 +08001750 return 0;
1751}
1752
1753static int abis_rsl_rx_ipacc(struct msgb *msg)
1754{
1755 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1756 int rc = 0;
1757
1758 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
1759 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ",
1760 gsm_ts_name(msg->lchan->ts), rllh->chan_nr);
1761
1762 switch (rllh->c.msg_type) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001763 case RSL_MT_IPAC_CRCX_ACK:
1764 DEBUGPC(DRSL, "IPAC_CRCX_ACK ");
1765 rc = abis_rsl_rx_ipacc_crcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001766 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001767 case RSL_MT_IPAC_CRCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001768 /* somehow the BTS was unable to bind the lchan to its local
1769 * port?!? */
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001770 DEBUGPC(DRSL, "IPAC_CRCX_NACK ");
Harald Welte59b04682009-06-10 05:40:52 +08001771 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001772 case RSL_MT_IPAC_MDCX_ACK:
Harald Welte59b04682009-06-10 05:40:52 +08001773 /* the BTS tells us that a connect operation was successful */
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001774 DEBUGPC(DRSL, "IPAC_MDCX_ACK ");
Harald Weltebffa4992009-12-19 16:42:06 +01001775 rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001776 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001777 case RSL_MT_IPAC_MDCX_NACK:
Harald Welte59b04682009-06-10 05:40:52 +08001778 /* somehow the BTS was unable to connect the lchan to a remote
1779 * port */
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001780 DEBUGPC(DRSL, "IPAC_MDCX_NACK ");
Harald Welte59b04682009-06-10 05:40:52 +08001781 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001782 case RSL_MT_IPAC_DLCX_IND:
1783 DEBUGPC(DRSL, "IPAC_DLCX_IND ");
1784 rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001785 break;
1786 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001787 LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x",
1788 rllh->c.msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001789 break;
1790 }
1791 DEBUGPC(DRSL, "\n");
1792
1793 return rc;
1794}
1795
1796
1797/* Entry-point where L2 RSL from BTS enters */
1798int abis_rsl_rcvmsg(struct msgb *msg)
1799{
1800 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
1801 int rc = 0;
1802
1803 switch (rslh->msg_discr & 0xfe) {
1804 case ABIS_RSL_MDISC_RLL:
1805 rc = abis_rsl_rx_rll(msg);
1806 break;
1807 case ABIS_RSL_MDISC_DED_CHAN:
1808 rc = abis_rsl_rx_dchan(msg);
1809 break;
1810 case ABIS_RSL_MDISC_COM_CHAN:
1811 rc = abis_rsl_rx_cchan(msg);
1812 break;
1813 case ABIS_RSL_MDISC_TRX:
1814 rc = abis_rsl_rx_trx(msg);
1815 break;
1816 case ABIS_RSL_MDISC_LOC:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001817 LOGP(DRSL, LOGL_NOTICE, "unimplemented RSL msg disc 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001818 rslh->msg_discr);
1819 break;
1820 case ABIS_RSL_MDISC_IPACCESS:
1821 rc = abis_rsl_rx_ipacc(msg);
1822 break;
1823 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +01001824 LOGP(DRSL, LOGL_NOTICE, "unknown RSL message discriminator "
1825 "0x%02x\n", rslh->msg_discr);
Harald Welte59b04682009-06-10 05:40:52 +08001826 return -EINVAL;
1827 }
1828 msgb_free(msg);
1829 return rc;
1830}
1831
1832
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001833/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
Harald Welte59b04682009-06-10 05:40:52 +08001834int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
1835{
1836 switch (ccch_conf) {
1837 case RSL_BCCH_CCCH_CONF_1_NC:
1838 return 1;
1839 case RSL_BCCH_CCCH_CONF_1_C:
1840 return 1;
1841 case RSL_BCCH_CCCH_CONF_2_NC:
1842 return 2;
1843 case RSL_BCCH_CCCH_CONF_3_NC:
1844 return 3;
1845 case RSL_BCCH_CCCH_CONF_4_NC:
1846 return 4;
1847 default:
1848 return -1;
1849 }
1850}
1851
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001852/* Section 3.3.2.3 TS 05.02 */
Harald Welte59b04682009-06-10 05:40:52 +08001853int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
1854{
1855 switch (ccch_conf) {
1856 case RSL_BCCH_CCCH_CONF_1_NC:
1857 return 0;
1858 case RSL_BCCH_CCCH_CONF_1_C:
1859 return 1;
1860 case RSL_BCCH_CCCH_CONF_2_NC:
1861 return 0;
1862 case RSL_BCCH_CCCH_CONF_3_NC:
1863 return 0;
1864 case RSL_BCCH_CCCH_CONF_4_NC:
1865 return 0;
1866 default:
1867 return -1;
1868 }
1869}
1870
1871/* From Table 10.5.33 of GSM 04.08 */
1872int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1873{
Harald Weltea54a2bb2009-12-01 18:04:30 +05301874 if (bts->si_common.chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1875 return MAX(1, (3 - bts->si_common.chan_desc.bs_ag_blks_res))
1876 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001877 } else {
Harald Weltea54a2bb2009-12-01 18:04:30 +05301878 return (9 - bts->si_common.chan_desc.bs_ag_blks_res)
1879 * (bts->si_common.chan_desc.bs_pa_mfrms + 2);
Harald Welte59b04682009-06-10 05:40:52 +08001880 }
1881}