blob: 90d43608d16f488ef841a18651facd22784b274f [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 Welte52b1f982008-12-23 20:25:15 +000029
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/gsm_data.h>
31#include <openbsc/gsm_04_08.h>
32#include <openbsc/abis_rsl.h>
33#include <openbsc/chan_alloc.h>
34#include <openbsc/debug.h>
35#include <openbsc/tlv.h>
Holger Freyther392209c2009-02-10 00:06:19 +000036#include <openbsc/paging.h>
Harald Welte52b1f982008-12-23 20:25:15 +000037
Harald Welte8470bf22008-12-25 23:28:35 +000038#define RSL_ALLOC_SIZE 1024
39#define RSL_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000040
Holger Freyther3b72a892009-02-04 00:31:39 +000041#define MAX(a, b) (a) >= (b) ? (a) : (b)
42
Harald Welte75099262009-02-16 21:12:08 +000043static const struct tlv_definition rsl_att_tlvdef = {
44 .def = {
45 [RSL_IE_CHAN_NR] = { TLV_TYPE_TV },
46 [RSL_IE_LINK_IDENT] = { TLV_TYPE_TV },
47 [RSL_IE_ACT_TYPE] = { TLV_TYPE_TV },
48 [RSL_IE_BS_POWER] = { TLV_TYPE_TV },
49 [RSL_IE_CHAN_IDENT] = { TLV_TYPE_TLV },
50 [RSL_IE_CHAN_MODE] = { TLV_TYPE_TLV },
51 [RSL_IE_ENCR_INFO] = { TLV_TYPE_TLV },
52 [RSL_IE_FRAME_NUMBER] = { TLV_TYPE_FIXED, 2 },
53 [RSL_IE_HANDO_REF] = { TLV_TYPE_TV },
54 [RSL_IE_L1_INFO] = { TLV_TYPE_FIXED, 2 },
55 [RSL_IE_L3_INFO] = { TLV_TYPE_TL16V },
56 [RSL_IE_MS_IDENTITY] = { TLV_TYPE_TLV },
57 [RSL_IE_MS_POWER] = { TLV_TYPE_TV },
58 [RSL_IE_PAGING_GROUP] = { TLV_TYPE_TV },
59 [RSL_IE_PAGING_LOAD] = { TLV_TYPE_FIXED, 2 },
60 [RSL_IE_PYHS_CONTEXT] = { TLV_TYPE_TLV },
61 [RSL_IE_ACCESS_DELAY] = { TLV_TYPE_TV },
62 [RSL_IE_RACH_LOAD] = { TLV_TYPE_TLV },
63 [RSL_IE_REQ_REFERENCE] = { TLV_TYPE_FIXED, 3 },
64 [RSL_IE_RELEASE_MODE] = { TLV_TYPE_TV },
65 [RSL_IE_RESOURCE_INFO] = { TLV_TYPE_TLV },
66 [RSL_IE_RLM_CAUSE] = { TLV_TYPE_TLV },
67 [RSL_IE_STARTNG_TIME] = { TLV_TYPE_FIXED, 2 },
68 [RSL_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
69 [RSL_IE_UPLINK_MEAS] = { TLV_TYPE_TLV },
70 [RSL_IE_CAUSE] = { TLV_TYPE_TLV },
71 [RSL_IE_MEAS_RES_NR] = { TLV_TYPE_TV },
72 [RSL_IE_MSG_ID] = { TLV_TYPE_TV },
73 [RSL_IE_SYSINFO_TYPE] = { TLV_TYPE_TV },
74 [RSL_IE_MS_POWER_PARAM] = { TLV_TYPE_TLV },
75 [RSL_IE_BS_POWER_PARAM] = { TLV_TYPE_TLV },
76 [RSL_IE_PREPROC_PARAM] = { TLV_TYPE_TLV },
77 [RSL_IE_PREPROC_MEAS] = { TLV_TYPE_TLV },
78 [RSL_IE_IMM_ASS_INFO] = { TLV_TYPE_TLV },
79 [RSL_IE_SMSCB_INFO] = { TLV_TYPE_FIXED, 23 },
80 [RSL_IE_MS_TIMING_OFFSET] = { TLV_TYPE_TV },
81 [RSL_IE_ERR_MSG] = { TLV_TYPE_TLV },
82 [RSL_IE_FULL_BCCH_INFO] = { TLV_TYPE_TLV },
83 [RSL_IE_CHAN_NEEDED] = { TLV_TYPE_TV },
84 [RSL_IE_CB_CMD_TYPE] = { TLV_TYPE_TV },
85 [RSL_IE_SMSCB_MSG] = { TLV_TYPE_TLV },
86 [RSL_IE_FULL_IMM_ASS_INFO] = { TLV_TYPE_TLV },
87 [RSL_IE_SACCH_INFO] = { TLV_TYPE_TLV },
88 [RSL_IE_CBCH_LOAD_INFO] = { TLV_TYPE_TV },
89 [RSL_IE_SMSCB_CHAN_INDICATOR] = { TLV_TYPE_TV },
90 [RSL_IE_GROUP_CALL_REF] = { TLV_TYPE_TLV },
91 [RSL_IE_CHAN_DESC] = { TLV_TYPE_TLV },
92 [RSL_IE_NCH_DRX_INFO] = { TLV_TYPE_TLV },
93 [RSL_IE_CMD_INDICATOR] = { TLV_TYPE_TLV },
94 [RSL_IE_EMLPP_PRIO] = { TLV_TYPE_TV },
95 [RSL_IE_UIC] = { TLV_TYPE_TLV },
96 [RSL_IE_MAIN_CHAN_REF] = { TLV_TYPE_TV },
97 [RSL_IE_MR_CONFIG] = { TLV_TYPE_TLV },
98 [RSL_IE_MR_CONTROL] = { TLV_TYPE_TV },
99 [RSL_IE_SUP_CODEC_TYPES] = { TLV_TYPE_TLV },
100 [RSL_IE_CODEC_CONFIG] = { TLV_TYPE_TLV },
101 [RSL_IE_RTD] = { TLV_TYPE_TV },
102 [RSL_IE_TFO_STATUS] = { TLV_TYPE_TV },
103 [RSL_IE_LLP_APDU] = { TLV_TYPE_TLV },
104 [RSL_IE_IPAC_REMOTE_IP] = { TLV_TYPE_FIXED, 4 },
105 [RSL_IE_IPAC_REMOTE_PORT] = { TLV_TYPE_FIXED, 2 },
106 [RSL_IE_IPAC_LOCAL_IP] = { TLV_TYPE_FIXED, 4 },
107 [RSL_IE_IPAC_LOCAL_PORT] = { TLV_TYPE_FIXED, 2 },
108 [0xf4] = { TLV_TYPE_TV },
109 [0xf8] = { TLV_TYPE_FIXED, 2 },
110 [0xfc] = { TLV_TYPE_TV },
111 },
112};
113#define rsl_tlv_parse(dec, buf, len) tlv_parse(dec, &rsl_att_tlvdef, buf, len)
114
Harald Welte52b1f982008-12-23 20:25:15 +0000115static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
116{
117 /* mask off the transparent bit ? */
118 msg_type &= 0xfe;
119
Harald Welte8470bf22008-12-25 23:28:35 +0000120 if ((msg_type & 0xf0) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +0000121 return ABIS_RSL_MDISC_RLL;
Harald Welte8470bf22008-12-25 23:28:35 +0000122 if ((msg_type & 0xf0) == 0x10) {
Harald Welte52b1f982008-12-23 20:25:15 +0000123 if (msg_type >= 0x19 && msg_type <= 0x22)
124 return ABIS_RSL_MDISC_TRX;
125 else
126 return ABIS_RSL_MDISC_COM_CHAN;
127 }
Harald Welte2d5b6382008-12-27 19:46:06 +0000128 if ((msg_type & 0xe0) == 0x20)
Harald Welte52b1f982008-12-23 20:25:15 +0000129 return ABIS_RSL_MDISC_DED_CHAN;
130
131 return ABIS_RSL_MDISC_LOC;
132}
133
134static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
135 u_int8_t msg_type)
136{
137 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
138 dh->c.msg_type = msg_type;
139 dh->ie_chan = RSL_IE_CHAN_NR;
140}
141
142static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
143 u_int8_t msg_type)
144{
145 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
146 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
147 dh->c.msg_type = msg_type;
148 dh->ie_chan = RSL_IE_CHAN_NR;
149 dh->ie_link_id = RSL_IE_LINK_IDENT;
150}
151
152
153/* encode channel number as per Section 9.3.1 */
154u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
155{
156 u_int8_t ret;
157
158 ret = (timeslot & 0x07) | type;
159
160 switch (type) {
161 case RSL_CHAN_Lm_ACCHs:
162 subch &= 0x01;
163 break;
164 case RSL_CHAN_SDCCH4_ACCH:
165 subch &= 0x07;
166 break;
167 case RSL_CHAN_SDCCH8_ACCH:
168 subch &= 0x07;
169 break;
170 default:
171 /* no subchannels allowed */
172 subch = 0x00;
173 break;
174 }
175 ret |= (subch << 3);
176
177 return ret;
178}
179
Harald Welte8470bf22008-12-25 23:28:35 +0000180/* determine logical channel based on TRX and channel number IE */
181struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
182{
183 struct gsm_lchan *lchan;
184 u_int8_t ts_nr = chan_nr & 0x07;
185 u_int8_t cbits = chan_nr >> 3;
186 u_int8_t lch_idx;
187 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
188
189 if (cbits == 0x01) {
190 lch_idx = 0; /* TCH/F */
191 if (ts->pchan != GSM_PCHAN_TCH_F)
192 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
193 chan_nr, ts->pchan);
194 } else if ((cbits & 0x1e) == 0x02) {
195 lch_idx = cbits & 0x1; /* TCH/H */
196 if (ts->pchan != GSM_PCHAN_TCH_H)
197 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
198 chan_nr, ts->pchan);
199 } else if ((cbits & 0x1c) == 0x04) {
200 lch_idx = cbits & 0x3; /* SDCCH/4 */
201 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
202 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
203 chan_nr, ts->pchan);
204 } else if ((cbits & 0x18) == 0x08) {
205 lch_idx = cbits & 0x7; /* SDCCH/8 */
206 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
207 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
208 chan_nr, ts->pchan);
209 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
210 lch_idx = 0;
211 if (ts->pchan != GSM_PCHAN_CCCH &&
212 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
213 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
214 chan_nr, ts->pchan);
215 /* FIXME: we should not return first sdcch4 !!! */
216 } else {
217 fprintf(stderr, "unknown chan_nr=0x%02x\n", chan_nr);
218 return NULL;
219 }
220
221 lchan = &ts->lchan[lch_idx];
222
223 return lchan;
224}
225
226u_int8_t lchan2chan_nr(struct gsm_lchan *lchan)
227{
228 struct gsm_bts_trx_ts *ts = lchan->ts;
229 u_int8_t cbits, chan_nr;
230
231 switch (ts->pchan) {
232 case GSM_PCHAN_TCH_F:
233 cbits = 0x01;
234 break;
235 case GSM_PCHAN_TCH_H:
236 cbits = 0x02;
237 cbits += lchan->nr;
238 break;
239 case GSM_PCHAN_CCCH_SDCCH4:
240 cbits = 0x04;
241 cbits += lchan->nr;
242 break;
243 case GSM_PCHAN_SDCCH8_SACCH8C:
244 cbits = 0x08;
245 cbits += lchan->nr;
246 break;
247 default:
248 case GSM_PCHAN_CCCH:
249 cbits = 0x10;
250 break;
251 }
252
253 chan_nr = (cbits << 3) | (ts->nr & 0x7);
254
255 return chan_nr;
256}
257
Harald Welte52b1f982008-12-23 20:25:15 +0000258/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
259u_int64_t str_to_imsi(const char *imsi_str)
260{
261 u_int64_t ret;
262
263 ret = strtoull(imsi_str, NULL, 10);
264
265 return ret;
266}
267
268/* Table 5 Clause 7 TS 05.02 */
269unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
270{
271 if (!bs_ccch_sdcch_comb)
272 return 9 - bs_ag_blks_res;
273 else
274 return 3 - bs_ag_blks_res;
275}
276
277/* Chapter 6.5.2 of TS 05.02 */
278unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
279 unsigned int n_pag_blocks)
280{
281 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
282}
283
284/* Chapter 6.5.2 of TS 05.02 */
285unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
286 int n_pag_blocks)
287{
288 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
289}
290
Harald Welte8470bf22008-12-25 23:28:35 +0000291static struct msgb *rsl_msgb_alloc(void)
292{
293 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM);
294}
295
Harald Welte362322e2009-02-15 14:36:38 +0000296#define MACBLOCK_SIZE 23
297static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
298{
299 memcpy(out, in, len);
300
301 if (len < MACBLOCK_SIZE)
302 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
303}
304
Harald Welte52b1f982008-12-23 20:25:15 +0000305/* Send a BCCH_INFO message as per Chapter 8.5.1 */
Harald Weltee79769b2009-02-07 00:48:17 +0000306int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000307 const u_int8_t *data, int len)
308{
309 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000310 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000311
312 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
313 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
314 dh->chan_nr = RSL_CHAN_BCCH;
315
316 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
317 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
318
Harald Weltee79769b2009-02-07 00:48:17 +0000319 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000320
321 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000322}
323
Harald Weltee79769b2009-02-07 00:48:17 +0000324int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
Harald Welte52b1f982008-12-23 20:25:15 +0000325 const u_int8_t *data, int len)
326{
327 struct abis_rsl_common_hdr *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000328 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000329
330 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
331 ch->msg_discr = ABIS_RSL_MDISC_TRX;
332 ch->msg_type = RSL_MT_SACCH_FILL;
333
334 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
Harald Welte702d8702008-12-26 20:25:35 +0000335 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
Harald Welte52b1f982008-12-23 20:25:15 +0000336
Harald Weltee79769b2009-02-07 00:48:17 +0000337 msg->trx = trx;
Harald Welte8470bf22008-12-25 23:28:35 +0000338
339 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000340}
341
342/* Chapter 8.4.1 */
Harald Weltee79769b2009-02-07 00:48:17 +0000343int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
Harald Welte52b1f982008-12-23 20:25:15 +0000344 u_int8_t act_type,
345 struct rsl_ie_chan_mode *chan_mode,
346 struct rsl_ie_chan_ident *chan_ident,
347 u_int8_t bs_power, u_int8_t ms_power,
348 u_int8_t ta)
349{
350 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000351 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000352
353 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
354 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
355 dh->chan_nr = chan_nr;
356
357 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
358 /* For compatibility with Phase 1 */
359 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
360 (u_int8_t *) chan_mode);
361 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
Harald Welte702d8702008-12-26 20:25:35 +0000362 (u_int8_t *) chan_ident);
Harald Welte702d8702008-12-26 20:25:35 +0000363#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000364 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
365 (u_int8_t *) &encr_info);
Harald Welte702d8702008-12-26 20:25:35 +0000366#endif
Harald Welted4c9bf32009-02-15 16:56:18 +0000367 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
Harald Welte52b1f982008-12-23 20:25:15 +0000368 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
369 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
370
Harald Weltee79769b2009-02-07 00:48:17 +0000371 msg->trx = trx;
372
Harald Welte8470bf22008-12-25 23:28:35 +0000373 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000374}
375
376#define TSC 7
377
Harald Welte8f5e2392009-02-03 12:57:37 +0000378int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
379 u_int8_t ta)
Harald Welte4b634542008-12-27 01:55:51 +0000380{
381 struct abis_rsl_dchan_hdr *dh;
382 struct msgb *msg = rsl_msgb_alloc();
Harald Welte4b634542008-12-27 01:55:51 +0000383
384 u_int8_t chan_nr = lchan2chan_nr(lchan);
385 u_int16_t arfcn = lchan->ts->trx->arfcn;
386 struct rsl_ie_chan_mode cm;
387 struct rsl_ie_chan_ident ci;
388
389 /* FIXME: what to do with data calls ? */
390 cm.dtx_dtu = 0x00;
391 switch (lchan->type) {
392 case GSM_LCHAN_SDCCH:
393 cm.spd_ind = RSL_CMOD_SPD_SIGN;
394 cm.chan_rt = RSL_CMOD_CRT_SDCCH;
395 cm.chan_rate = 0x00;
396 break;
397 case GSM_LCHAN_TCH_F:
398 cm.spd_ind = RSL_CMOD_SPD_SPEECH;
399 cm.chan_rt = RSL_CMOD_CRT_TCH_Bm;
400 cm.chan_rate = 0x11; /* speech coding alg version 2*/
401 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000402 case GSM_LCHAN_TCH_H:
Harald Welte8f5e2392009-02-03 12:57:37 +0000403 DEBUGP(DRSL, "Unimplemented TCH_H activation\n");
Holger Freytherca362a62009-01-04 21:05:01 +0000404 return -1;
405 case GSM_LCHAN_UNKNOWN:
406 case GSM_LCHAN_NONE:
407 return -1;
Harald Welte4b634542008-12-27 01:55:51 +0000408 }
409
410 ci.chan_desc.iei = 0x64;
411 ci.chan_desc.chan_nr = chan_nr;
412 ci.chan_desc.oct3 = (TSC << 5) | ((arfcn & 0x3ff) >> 8);
413 ci.chan_desc.oct4 = arfcn & 0xff;
414
415 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
416 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
417 dh->chan_nr = chan_nr;
418
419 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
420 /* For compatibility with Phase 1 */
421 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
422 (u_int8_t *) &cm);
423 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
424 (u_int8_t *) &ci);
Harald Welte4b634542008-12-27 01:55:51 +0000425#if 0
426 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
427 (u_int8_t *) &encr_info);
Harald Welte4b634542008-12-27 01:55:51 +0000428#endif
Harald Welted4c9bf32009-02-15 16:56:18 +0000429 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
430 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
Harald Welte4b634542008-12-27 01:55:51 +0000431 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
432
Harald Weltee79769b2009-02-07 00:48:17 +0000433 msg->trx = lchan->ts->trx;
434
Harald Welte4b634542008-12-27 01:55:51 +0000435 return abis_rsl_sendmsg(msg);
436}
437
Holger Freyther36cbeff2008-12-30 19:15:20 +0000438/* Chapter 9.1.7 of 04.08 */
Harald Welte8470bf22008-12-25 23:28:35 +0000439int rsl_chan_release(struct gsm_lchan *lchan)
Harald Welte52b1f982008-12-23 20:25:15 +0000440{
441 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000442 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000443
444 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
445 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
Harald Welte8470bf22008-12-25 23:28:35 +0000446 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000447
Harald Welte8470bf22008-12-25 23:28:35 +0000448 msg->lchan = lchan;
449 msg->trx = lchan->ts->trx;
450
Harald Welte2d5b6382008-12-27 19:46:06 +0000451 DEBUGP(DRSL, "Channel Release CMD, chan_nr=0x%02x\n", dh->chan_nr);
452
Harald Welte8470bf22008-12-25 23:28:35 +0000453 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000454}
455
456int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
457 u_int8_t *ms_ident, u_int8_t chan_needed)
458{
459 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000460 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000461
462 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
463 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
464 dh->chan_nr = RSL_CHAN_PCH_AGCH;
465
466 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
Harald Welte255539c2008-12-28 02:26:27 +0000467 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
Harald Welte52b1f982008-12-23 20:25:15 +0000468 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
469
Harald Welte8470bf22008-12-25 23:28:35 +0000470 msg->trx = bts->c0;
471
472 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000473}
474
Holger Freyther7448a532009-01-04 20:18:23 +0000475int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
476 struct gsm_subscriber *subscr)
477{
Holger Freytherca362a62009-01-04 21:05:01 +0000478#if 0
Holger Freyther7448a532009-01-04 20:18:23 +0000479 u_int8_t mi[128];
480 unsigned int mi_len;
481 u_int8_t paging_group;
Holger Freytherca362a62009-01-04 21:05:01 +0000482#endif
Holger Freyther7448a532009-01-04 20:18:23 +0000483
484 return -1;
485}
486
Harald Welte52b1f982008-12-23 20:25:15 +0000487int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
488{
489 int i, len = strlen(str_in);
490
491 for (i = 0; i < len; i++) {
492 int num = str_in[i] - 0x30;
493 if (num < 0 || num > 9)
494 return -1;
495 if (i % 2 == 0)
496 bcd_out[i/2] = num;
497 else
498 bcd_out[i/2] |= (num << 4);
499 }
500
501 return 0;
502}
503
Harald Welte702d8702008-12-26 20:25:35 +0000504/* Chapter 8.5.6 */
Harald Welte52b1f982008-12-23 20:25:15 +0000505int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
506{
Harald Welte8470bf22008-12-25 23:28:35 +0000507 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000508 struct abis_rsl_dchan_hdr *dh;
Harald Welte362322e2009-02-15 14:36:38 +0000509 u_int8_t buf[MACBLOCK_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000510
511 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
512 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
513 dh->chan_nr = RSL_CHAN_PCH_AGCH;
514
Harald Welte362322e2009-02-15 14:36:38 +0000515 switch (bts->type) {
516 case GSM_BTS_TYPE_BS11:
517 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
518 break;
519 default:
520 /* If phase 2, construct a FULL_IMM_ASS_INFO */
521 pad_macblock(buf, val, len);
522 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
523 break;
524 }
Harald Welte52b1f982008-12-23 20:25:15 +0000525
Harald Welte8470bf22008-12-25 23:28:35 +0000526 msg->trx = bts->c0;
527
528 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000529}
530
Harald Welte8470bf22008-12-25 23:28:35 +0000531/* Send "DATA REQUEST" message with given L3 Info payload */
Harald Welte52b1f982008-12-23 20:25:15 +0000532/* Chapter 8.3.1 */
Harald Welte8470bf22008-12-25 23:28:35 +0000533int rsl_data_request(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +0000534{
Harald Welte8470bf22008-12-25 23:28:35 +0000535 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000536 struct abis_rsl_rll_hdr *rh;
537
Harald Welte8470bf22008-12-25 23:28:35 +0000538 if (msg->lchan == NULL) {
539 fprintf(stderr, "cannot send DATA REQUEST to unknown lchan\n");
540 return -EINVAL;
541 }
Harald Welte52b1f982008-12-23 20:25:15 +0000542
Harald Welte8470bf22008-12-25 23:28:35 +0000543 /* First push the L3 IE tag and length */
Harald Welte4b634542008-12-27 01:55:51 +0000544 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welte8470bf22008-12-25 23:28:35 +0000545
546 /* Then push the RSL header */
Harald Welte52b1f982008-12-23 20:25:15 +0000547 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
548 init_llm_hdr(rh, RSL_MT_DATA_REQ);
Harald Welte8470bf22008-12-25 23:28:35 +0000549 rh->chan_nr = lchan2chan_nr(msg->lchan);
550 rh->link_id = link_id;
Harald Welte52b1f982008-12-23 20:25:15 +0000551
Harald Welte8470bf22008-12-25 23:28:35 +0000552 msg->trx = msg->lchan->ts->trx;
553
554 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000555}
556
Harald Welte702d8702008-12-26 20:25:35 +0000557/* Chapter 8.4.2: Channel Activate Acknowledge */
558static int rsl_rx_chan_act_ack(struct msgb *msg)
559{
560 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
561
562 /* BTS has confirmed channel activation, we now need
563 * to assign the activated channel to the MS */
Harald Welte4b634542008-12-27 01:55:51 +0000564 if (rslh->ie_chan != RSL_IE_CHAN_NR)
565 return -EINVAL;
566
567 DEBUGP(DRSL, "Channel Activate ACK Channel 0x%02x\n", rslh->chan_nr);
Harald Welte702d8702008-12-26 20:25:35 +0000568
Harald Welte4b634542008-12-27 01:55:51 +0000569 return 0;
570}
Harald Welte702d8702008-12-26 20:25:35 +0000571
Harald Welte4b634542008-12-27 01:55:51 +0000572/* Chapter 8.4.3: Channel Activate NACK */
573static int rsl_rx_chan_act_nack(struct msgb *msg)
574{
575 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
576
577 /* BTS has confirmed channel activation, we now need
578 * to assign the activated channel to the MS */
579 if (rslh->ie_chan != RSL_IE_CHAN_NR)
580 return -EINVAL;
581
582 DEBUGP(DRSL, "Channel Activate NACK Channel 0x%02x\n", rslh->chan_nr);
583
584 return 0;
Harald Welte702d8702008-12-26 20:25:35 +0000585}
586
Harald Welte52b1f982008-12-23 20:25:15 +0000587static int abis_rsl_rx_dchan(struct msgb *msg)
588{
Harald Welte8470bf22008-12-25 23:28:35 +0000589 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
590 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000591
Harald Welte8470bf22008-12-25 23:28:35 +0000592 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
593
594 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000595 case RSL_MT_CHAN_ACTIV_ACK:
Harald Welte4b634542008-12-27 01:55:51 +0000596 rc = rsl_rx_chan_act_ack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000597 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000598 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte4b634542008-12-27 01:55:51 +0000599 rc = rsl_rx_chan_act_nack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000600 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000601 case RSL_MT_CONN_FAIL:
Harald Welte2d5b6382008-12-27 19:46:06 +0000602 DEBUGP(DRSL, "Connection Fail, release channel\n");
Harald Welte14537e52008-12-27 10:29:08 +0000603 rc = rsl_chan_release(msg->lchan);
Harald Welte2d5b6382008-12-27 19:46:06 +0000604 /* only free it after channel release ACK */
Harald Welte8470bf22008-12-25 23:28:35 +0000605 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000606 case RSL_MT_MEAS_RES:
Harald Welte2d5b6382008-12-27 19:46:06 +0000607 DEBUGP(DRSL, "Measurement Result\n");
608 break;
609 case RSL_MT_RF_CHAN_REL_ACK:
Harald Welte8f5e2392009-02-03 12:57:37 +0000610 DEBUGP(DRSL, "RF CHANNEL RELEASE ACK chan_nr=0x%02x\n",
611 rslh->chan_nr);
Harald Welte2d5b6382008-12-27 19:46:06 +0000612 lchan_free(msg->lchan);
Harald Welte8470bf22008-12-25 23:28:35 +0000613 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000614 case RSL_MT_MODE_MODIFY_ACK:
615 case RSL_MT_MODE_MODIFY_NACK:
616 case RSL_MT_PHY_CONTEXT_CONF:
617 case RSL_MT_PREPROC_MEAS_RES:
Harald Welte52b1f982008-12-23 20:25:15 +0000618 case RSL_MT_TALKER_DET:
619 case RSL_MT_LISTENER_DET:
620 case RSL_MT_REMOTE_CODEC_CONF_REP:
621 case RSL_MT_MR_CODEC_MOD_ACK:
622 case RSL_MT_MR_CODEC_MOD_NACK:
623 case RSL_MT_MR_CODEC_MOD_PER:
624 fprintf(stderr, "Unimplemented Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000625 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000626 break;
627 default:
628 fprintf(stderr, "unknown Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000629 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000630 return -EINVAL;
631 }
Harald Welte8470bf22008-12-25 23:28:35 +0000632 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000633}
634
Harald Welte702d8702008-12-26 20:25:35 +0000635static int rsl_rx_error_rep(struct msgb *msg)
636{
637 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
638 u_int8_t cause_len;
639
640 if (rslh->data[0] != RSL_IE_CAUSE)
641 return -EINVAL;
642
643 cause_len = rslh->data[1];
Harald Welte4b634542008-12-27 01:55:51 +0000644 fprintf(stdout, "RSL ERROR REPORT, Cause ");
Harald Welte702d8702008-12-26 20:25:35 +0000645 hexdump(&rslh->data[2], cause_len);
646
647 return 0;
648}
649
Harald Welte52b1f982008-12-23 20:25:15 +0000650static int abis_rsl_rx_trx(struct msgb *msg)
651{
Harald Welte702d8702008-12-26 20:25:35 +0000652 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000653 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000654
655 switch (rslh->msg_type) {
Harald Welte702d8702008-12-26 20:25:35 +0000656 case RSL_MT_ERROR_REPORT:
657 rc = rsl_rx_error_rep(msg);
658 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000659 case RSL_MT_RF_RES_IND:
660 /* interference on idle channels of TRX */
Harald Welte8f5e2392009-02-03 12:57:37 +0000661 fprintf(stderr, "RSL TRX: RF Interference Indication\n");
662 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000663 case RSL_MT_OVERLOAD:
664 /* indicate CCCH / ACCH / processor overload */
Harald Welte8f5e2392009-02-03 12:57:37 +0000665 fprintf(stderr, "RSL TRX: CCCH/ACCH/CPU Overload\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000666 break;
667 default:
668 fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
669 rslh->msg_type);
670 return -EINVAL;
671 }
Harald Welte8470bf22008-12-25 23:28:35 +0000672 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000673}
674
Harald Welte8470bf22008-12-25 23:28:35 +0000675/* MS has requested a channel on the RACH */
Harald Welte52b1f982008-12-23 20:25:15 +0000676static int rsl_rx_chan_rqd(struct msgb *msg)
677{
Harald Welte702d8702008-12-26 20:25:35 +0000678 struct gsm_bts *bts = msg->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000679 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
680 struct gsm48_req_ref *rqd_ref;
Harald Welte52b1f982008-12-23 20:25:15 +0000681 struct gsm48_imm_ass ia;
Harald Welte8470bf22008-12-25 23:28:35 +0000682 enum gsm_chan_t lctype;
Harald Welte2cbe0922008-12-29 04:09:31 +0000683 enum gsm_chreq_reason_t chreq_reason;
Harald Welte8470bf22008-12-25 23:28:35 +0000684 struct gsm_lchan *lchan;
685 u_int8_t rqd_ta;
Holger Freyther3186bf22008-12-29 06:23:49 +0000686 int ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000687
Harald Welte52b1f982008-12-23 20:25:15 +0000688 u_int16_t arfcn;
689 u_int8_t ts_number, subch;
690
Harald Welte8470bf22008-12-25 23:28:35 +0000691 /* parse request reference to be used in immediate assign */
692 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
693 return -EINVAL;
694
695 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
696
697 /* parse access delay and use as TA */
698 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
699 return -EINVAL;
700 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
701
702 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
703 * request reference RA */
704 lctype = get_ctype_by_chreq(bts, rqd_ref->ra);
Harald Welte2cbe0922008-12-29 04:09:31 +0000705 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra);
706
Harald Welte8470bf22008-12-25 23:28:35 +0000707 /* check availability / allocate channel */
708 lchan = lchan_alloc(bts, lctype);
709 if (!lchan) {
710 fprintf(stderr, "CHAN RQD: no resources\n");
711 /* FIXME: send some kind of reject ?!? */
712 return -ENOMEM;
713 }
714
715 ts_number = lchan->ts->nr;
716 arfcn = lchan->ts->trx->arfcn;
717 subch = lchan->nr;
Harald Welte52b1f982008-12-23 20:25:15 +0000718
Harald Welted4c9bf32009-02-15 16:56:18 +0000719 lchan->ms_power = lchan->bs_power = 0x0f; /* 30dB reduction */
Harald Welte4b634542008-12-27 01:55:51 +0000720 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta);
Harald Welte52b1f982008-12-23 20:25:15 +0000721
722 /* create IMMEDIATE ASSIGN 04.08 messge */
723 memset(&ia, 0, sizeof(ia));
724 ia.l2_plen = 0x2d;
725 ia.proto_discr = GSM48_PDISC_RR;
726 ia.msg_type = GSM48_MT_RR_IMM_ASS;
Harald Welte2d5b6382008-12-27 19:46:06 +0000727 ia.page_mode = GSM48_PM_SAME;
Harald Welte4b634542008-12-27 01:55:51 +0000728 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000729 ia.chan_desc.h0.h = 0;
730 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
731 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
732 ia.chan_desc.h0.tsc = 7;
Harald Welte8470bf22008-12-25 23:28:35 +0000733 /* use request reference extracted from CHAN_RQD */
734 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
735 ia.timing_advance = rqd_ta;
Harald Welte52b1f982008-12-23 20:25:15 +0000736 ia.mob_alloc_len = 0;
737
Harald Welte8f5e2392009-02-03 12:57:37 +0000738 DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %s "
739 "chan_nr=0x%02x r=%s\n",
Harald Welteca64da92009-01-04 16:54:12 +0000740 arfcn, ts_number, subch, gsm_lchan_name(lchan->type),
741 ia.chan_desc.chan_nr, gsm_chreq_name(chreq_reason));
Harald Welte75a983f2008-12-27 21:34:06 +0000742
Holger Freyther3186bf22008-12-29 06:23:49 +0000743
Harald Welte52b1f982008-12-23 20:25:15 +0000744 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
Holger Freyther3186bf22008-12-29 06:23:49 +0000745 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
746
Harald Welte817f3c82008-12-30 14:57:59 +0000747 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000748}
749
Harald Welteea280442009-02-02 22:29:56 +0000750/* MS has requested a channel on the RACH */
751static int rsl_rx_ccch_load(struct msgb *msg)
752{
753 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
754 u_int16_t pg_buf_space;
Holger Freyther8c563cf2009-02-03 20:08:51 +0000755 u_int16_t rach_slot_count = -1;
756 u_int16_t rach_busy_count = -1;
757 u_int16_t rach_access_count = -1;
Harald Welteea280442009-02-02 22:29:56 +0000758
759 switch (rslh->data[0]) {
760 case RSL_IE_PAGING_LOAD:
761 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
Holger Freyther392209c2009-02-10 00:06:19 +0000762 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
Harald Welteea280442009-02-02 22:29:56 +0000763 break;
764 case RSL_IE_RACH_LOAD:
Holger Freyther8c563cf2009-02-03 20:08:51 +0000765 if (msg->data_len >= 7) {
766 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
767 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
768 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
769 }
Harald Welteea280442009-02-02 22:29:56 +0000770 break;
771 default:
772 break;
773 }
774
775 return 0;
776}
777
Harald Welte52b1f982008-12-23 20:25:15 +0000778static int abis_rsl_rx_cchan(struct msgb *msg)
779{
Harald Welteea280442009-02-02 22:29:56 +0000780 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000781 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000782
Harald Welte8470bf22008-12-25 23:28:35 +0000783 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
784
785 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000786 case RSL_MT_CHAN_RQD:
787 /* MS has requested a channel on the RACH */
788 rc = rsl_rx_chan_rqd(msg);
789 break;
Harald Welteea280442009-02-02 22:29:56 +0000790 case RSL_MT_CCCH_LOAD_IND:
791 /* current load on the CCCH */
792 rc = rsl_rx_ccch_load(msg);
793 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000794 case RSL_MT_DELETE_IND:
795 /* CCCH overloaded, IMM_ASSIGN was dropped */
796 case RSL_MT_CBCH_LOAD_IND:
797 /* current load on the CBCH */
Harald Welte8f5e2392009-02-03 12:57:37 +0000798 fprintf(stderr, "Unimplemented Abis RSL TRX message type "
799 "0x%02x\n", rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000800 break;
801 default:
802 fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000803 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000804 return -EINVAL;
805 }
Harald Welte8470bf22008-12-25 23:28:35 +0000806
807 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000808}
809
Harald Welte4b634542008-12-27 01:55:51 +0000810static int rsl_rx_rll_err_ind(struct msgb *msg)
811{
812 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
813 u_int8_t *rlm_cause = rllh->data;
814
815 DEBUGP(DRLL, "RLL ERROR INDICATION: chan_nr=0x%02x cause=0x%02x\n",
816 rllh->chan_nr, rlm_cause[1]);
817
818 return 0;
819}
Harald Welte52b1f982008-12-23 20:25:15 +0000820/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
821 0x02, 0x06,
822 0x01, 0x20,
823 0x02, 0x00,
824 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
825
826static int abis_rsl_rx_rll(struct msgb *msg)
827{
828 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
829 int rc;
Harald Welte8470bf22008-12-25 23:28:35 +0000830
831 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte52b1f982008-12-23 20:25:15 +0000832
833 switch (rllh->c.msg_type) {
834 case RSL_MT_DATA_IND:
Harald Welte4b634542008-12-27 01:55:51 +0000835 DEBUGP(DRLL, "DATA INDICATION chan_nr=0x%02x\n", rllh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000836 /* FIXME: Verify L3 info element */
Harald Welte702d8702008-12-26 20:25:35 +0000837 msg->l3h = &rllh->data[3];
Harald Welte52b1f982008-12-23 20:25:15 +0000838 rc = gsm0408_rcvmsg(msg);
839 break;
840 case RSL_MT_EST_IND:
Harald Welte8f5e2392009-02-03 12:57:37 +0000841 DEBUGP(DRLL, "ESTABLISH INDICATION chan_nr=0x%02x\n",
842 rllh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000843 /* FIXME: Verify L3 info element */
Harald Welte702d8702008-12-26 20:25:35 +0000844 msg->l3h = &rllh->data[3];
Harald Welte52b1f982008-12-23 20:25:15 +0000845 rc = gsm0408_rcvmsg(msg);
846 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000847 case RSL_MT_REL_IND:
Harald Welte8f5e2392009-02-03 12:57:37 +0000848 DEBUGP(DRLL, "RELEASE INDICATION chan_nr=0x%02x\n",
849 rllh->chan_nr);
Harald Welte2d5b6382008-12-27 19:46:06 +0000850 break;
851 case RSL_MT_REL_CONF:
Harald Welte8f5e2392009-02-03 12:57:37 +0000852 DEBUGP(DRLL, "RELEASE CONFIRMATION chan_nr=0x%02x\n",
853 rllh->chan_nr);
Harald Welte4b634542008-12-27 01:55:51 +0000854 break;
855 case RSL_MT_ERROR_IND:
856 rc = rsl_rx_rll_err_ind(msg);
857 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000858 case RSL_MT_UNIT_DATA_IND:
859 fprintf(stderr, "unimplemented Abis RLL message type 0x%02x\n",
860 rllh->c.msg_type);
861 break;
862 default:
863 fprintf(stderr, "unknown Abis RLL message type 0x%02x\n",
864 rllh->c.msg_type);
865 }
Harald Welte8470bf22008-12-25 23:28:35 +0000866 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000867}
868
Harald Welte75099262009-02-16 21:12:08 +0000869/* ip.access specific RSL extensions */
870int rsl_ipacc_bind(struct gsm_lchan *lchan)
871{
872 struct msgb *msg = rsl_msgb_alloc();
873 struct abis_rsl_dchan_hdr *dh;
874
875 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
876 init_dchan_hdr(dh, RSL_MT_IPAC_BIND);
877 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
878 dh->chan_nr = lchan2chan_nr(lchan);
879
880 msg->trx = lchan->ts->trx;
881
882 return abis_rsl_sendmsg(msg);
883}
884
885int rsl_ipacc_connect(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port, u_int16_t f8, u_int8_t fc)
886{
887 struct msgb *msg = rsl_msgb_alloc();
888 struct abis_rsl_dchan_hdr *dh;
889 u_int8_t *att_f8, *att_ip, *att_port;
890
891 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
892 init_dchan_hdr(dh, RSL_MT_IPAC_CONNECT);
893 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
894 dh->chan_nr = lchan2chan_nr(lchan);
895
896 att_f8 = msgb_put(msg, sizeof(f8)+1);
897 att_f8[0] = 0xf8;
898 att_f8[1] = f8 >> 8;
899 att_f8[2] = f8 & 0xff;
900
901 att_ip = msgb_put(msg, sizeof(ip)+1);
902 att_ip[0] = RSL_IE_IPAC_REMOTE_IP;
903 att_ip[1] = ip >> 24;
904 att_ip[2] = ip >> 16;
905 att_ip[3] = ip >> 8;
906 att_ip[4] = ip & 0xff;
907
908 att_port = msgb_put(msg, sizeof(port)+1);
909 att_port[0] = RSL_IE_IPAC_REMOTE_PORT;
910 att_port[1] = port >> 8;
911 att_port[2] = port & 0xff;
912
913 msgb_tv_put(msg, 0xf4, 1); /* F4 01 */
914 msgb_tv_put(msg, 0xfc, fc); /* FC 7F */
915 msg->trx = lchan->ts->trx;
916
917 return abis_rsl_sendmsg(msg);
918}
919
920static int abis_rsl_rx_ipacc_bindack(struct msgb *msg)
921{
922 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
923 struct tlv_parsed tv;
924 struct gsm_bts_trx_ts *ts = msg->lchan->ts;
925 u_int32_t ip;
926 u_int16_t port, attr_f8;
927
928 /* the BTS has acknowledged a local bind, it now tells us the IP
929 * address and port number to which it has bound the given logical
930 * channel */
931
932 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
933 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
934 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
935 !TLVP_PRESENT(&tv, 0xfc) ||
936 !TLVP_PRESENT(&tv, 0xf8)) {
937 DEBUGP(DRSL, "mandatory IE missing\n");
938 return -EINVAL;
939 }
940 ip = *((u_int32_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_IP));
941 port = *((u_int16_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_PORT));
942 attr_f8 = *((u_int16_t *) TLVP_VAL(&tv, 0xf8));
943
944 /* update our local information about this TS */
945 ts->abis_ip.bound_ip = ntohl(ip);
946 ts->abis_ip.bound_port = ntohl(port);
947 ts->abis_ip.attr_f8 = ntohs(attr_f8);
948 ts->abis_ip.attr_fc = *TLVP_VAL(&tv, 0xfc);
949
950 return 0;
951}
952
953static int abis_rsl_rx_ipacc_disc_ind(struct msgb *msg)
954{
955 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
956 struct tlv_parsed tv;
957
958 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
959 if (!TLVP_PRESENT(&tv, RSL_IE_CAUSE)) {
960 DEBUGP(DRSL, "mandatory IE missing\n");
961 return -EINVAL;
962 }
963
964 DEBUGP(DNM, "cause=0x%02x\n", *TLVP_VAL(&tv, RSL_IE_CAUSE));
965
966 return 0;
967}
968
969static int abis_rsl_rx_ipacc(struct msgb *msg)
970{
971 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
972 int rc = 0;
973
974 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
975
976 switch (rllh->c.msg_type) {
977 case RSL_MT_IPAC_BIND_ACK:
978 DEBUGP(DRSL, "IPAC_BIND_ACK\n");
979 rc = abis_rsl_rx_ipacc_bindack(msg);
980 break;
981 case RSL_MT_IPAC_BIND_NACK:
982 /* somehow the BTS was unable to bind the lchan to its local
983 * port?!? */
984 DEBUGP(DRSL, "IPAC_BIND_NACK\n");
985 break;
986 case RSL_MT_IPAC_CONNECT_ACK:
987 /* the BTS tells us that a connect operation was successful */
988 DEBUGP(DRSL, "IPAC_CONNECT_ACK\n");
989 break;
990 case RSL_MT_IPAC_CONNECT_NACK:
991 /* somehow the BTS was unable to connect the lchan to a remote
992 * port */
993 DEBUGP(DRSL, "IPAC_BIND_NACK\n");
994 break;
995 case RSL_MT_IPAC_DISCONNECT_IND:
996 DEBUGP(DRSL, "IPAC_DISCONNECT_IND\n");
997 rc = abis_rsl_rx_ipacc_disc_ind(msg);
998 break;
999 default:
1000 DEBUGP(DRSL, "Unknown ip.access msg_type 0x%02x\n", rllh->c.msg_type);
1001 break;
1002 }
1003
1004 return rc;
1005}
1006
1007
Harald Welte52b1f982008-12-23 20:25:15 +00001008/* Entry-point where L2 RSL from BTS enters */
Harald Welte8470bf22008-12-25 23:28:35 +00001009int abis_rsl_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001010{
1011 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
Harald Welte8f5e2392009-02-03 12:57:37 +00001012 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001013
1014 switch (rslh->msg_discr & 0xfe) {
1015 case ABIS_RSL_MDISC_RLL:
1016 rc = abis_rsl_rx_rll(msg);
1017 break;
1018 case ABIS_RSL_MDISC_DED_CHAN:
1019 rc = abis_rsl_rx_dchan(msg);
1020 break;
1021 case ABIS_RSL_MDISC_COM_CHAN:
Harald Welte52b1f982008-12-23 20:25:15 +00001022 rc = abis_rsl_rx_cchan(msg);
1023 break;
Harald Welte8470bf22008-12-25 23:28:35 +00001024 case ABIS_RSL_MDISC_TRX:
1025 rc = abis_rsl_rx_trx(msg);
1026 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001027 case ABIS_RSL_MDISC_LOC:
Harald Welte8f5e2392009-02-03 12:57:37 +00001028 fprintf(stderr, "unimplemented RSL msg disc 0x%02x\n",
1029 rslh->msg_discr);
1030 break;
Harald Welte75099262009-02-16 21:12:08 +00001031 case ABIS_RSL_MDISC_IPACCESS:
1032 rc = abis_rsl_rx_ipacc(msg);
1033 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001034 default:
1035 fprintf(stderr, "unknown RSL message discriminator 0x%02x\n",
1036 rslh->msg_discr);
1037 return -EINVAL;
1038 }
Harald Welte4f4a3902008-12-26 00:04:49 +00001039 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001040 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001041}
Holger Freyther3b72a892009-02-04 00:31:39 +00001042
1043
1044/* Section 3.3.2.3 . I think this looks like a table */
1045int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
1046{
1047 switch (ccch_conf) {
1048 case RSL_BCCH_CCCH_CONF_1_NC:
1049 return 1;
1050 case RSL_BCCH_CCCH_CONF_1_C:
1051 return 1;
1052 case RSL_BCCH_CCCH_CONF_2_NC:
1053 return 2;
1054 case RSL_BCCH_CCCH_CONF_3_NC:
1055 return 3;
1056 case RSL_BCCH_CCCH_CONF_4_NC:
1057 return 4;
1058 default:
1059 return -1;
1060 }
1061}
1062
1063int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
1064{
1065 switch (ccch_conf) {
1066 case RSL_BCCH_CCCH_CONF_1_NC:
1067 return 0;
1068 case RSL_BCCH_CCCH_CONF_1_C:
1069 return 1;
1070 case RSL_BCCH_CCCH_CONF_2_NC:
1071 return 0;
1072 case RSL_BCCH_CCCH_CONF_3_NC:
1073 return 0;
1074 case RSL_BCCH_CCCH_CONF_4_NC:
1075 return 0;
1076 default:
1077 return -1;
1078 }
1079}
1080
1081/* From Table 10.5.33 of GSM 04.08 */
1082int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1083{
1084 if (bts->chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1085 return MAX(1, (3 - bts->chan_desc.bs_ag_blks_res))
Holger Freyther3aa8d6c2009-02-04 02:14:45 +00001086 * (bts->chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001087 } else {
1088 return (9 - bts->chan_desc.bs_ag_blks_res)
Holger Freyther3aa8d6c2009-02-04 02:14:45 +00001089 * (bts->chan_desc.bs_pa_mfrms + 2);
Holger Freyther3b72a892009-02-04 00:31:39 +00001090 }
1091}