blob: 0cf206c200b98b9b0bbd18486ddb74420446e165 [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
4/* (C) 2008 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>
28
Harald Welte8470bf22008-12-25 23:28:35 +000029#include <openbsc/gsm_data.h>
30#include <openbsc/gsm_04_08.h>
31#include <openbsc/abis_rsl.h>
32#include <openbsc/chan_alloc.h>
33#include <openbsc/debug.h>
34#include <openbsc/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Harald Welte8470bf22008-12-25 23:28:35 +000036#define RSL_ALLOC_SIZE 1024
37#define RSL_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000038
39static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
40{
41 /* mask off the transparent bit ? */
42 msg_type &= 0xfe;
43
Harald Welte8470bf22008-12-25 23:28:35 +000044 if ((msg_type & 0xf0) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +000045 return ABIS_RSL_MDISC_RLL;
Harald Welte8470bf22008-12-25 23:28:35 +000046 if ((msg_type & 0xf0) == 0x10) {
Harald Welte52b1f982008-12-23 20:25:15 +000047 if (msg_type >= 0x19 && msg_type <= 0x22)
48 return ABIS_RSL_MDISC_TRX;
49 else
50 return ABIS_RSL_MDISC_COM_CHAN;
51 }
Harald Welte8470bf22008-12-25 23:28:35 +000052 if ((msg_type & 0xc) == 0x00)
Harald Welte52b1f982008-12-23 20:25:15 +000053 return ABIS_RSL_MDISC_DED_CHAN;
54
55 return ABIS_RSL_MDISC_LOC;
56}
57
58static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
59 u_int8_t msg_type)
60{
61 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
62 dh->c.msg_type = msg_type;
63 dh->ie_chan = RSL_IE_CHAN_NR;
64}
65
66static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
67 u_int8_t msg_type)
68{
69 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
70 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
71 dh->c.msg_type = msg_type;
72 dh->ie_chan = RSL_IE_CHAN_NR;
73 dh->ie_link_id = RSL_IE_LINK_IDENT;
74}
75
76
77/* encode channel number as per Section 9.3.1 */
78u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
79{
80 u_int8_t ret;
81
82 ret = (timeslot & 0x07) | type;
83
84 switch (type) {
85 case RSL_CHAN_Lm_ACCHs:
86 subch &= 0x01;
87 break;
88 case RSL_CHAN_SDCCH4_ACCH:
89 subch &= 0x07;
90 break;
91 case RSL_CHAN_SDCCH8_ACCH:
92 subch &= 0x07;
93 break;
94 default:
95 /* no subchannels allowed */
96 subch = 0x00;
97 break;
98 }
99 ret |= (subch << 3);
100
101 return ret;
102}
103
Harald Welte8470bf22008-12-25 23:28:35 +0000104/* determine logical channel based on TRX and channel number IE */
105struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
106{
107 struct gsm_lchan *lchan;
108 u_int8_t ts_nr = chan_nr & 0x07;
109 u_int8_t cbits = chan_nr >> 3;
110 u_int8_t lch_idx;
111 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
112
113 if (cbits == 0x01) {
114 lch_idx = 0; /* TCH/F */
115 if (ts->pchan != GSM_PCHAN_TCH_F)
116 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
117 chan_nr, ts->pchan);
118 } else if ((cbits & 0x1e) == 0x02) {
119 lch_idx = cbits & 0x1; /* TCH/H */
120 if (ts->pchan != GSM_PCHAN_TCH_H)
121 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
122 chan_nr, ts->pchan);
123 } else if ((cbits & 0x1c) == 0x04) {
124 lch_idx = cbits & 0x3; /* SDCCH/4 */
125 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
126 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
127 chan_nr, ts->pchan);
128 } else if ((cbits & 0x18) == 0x08) {
129 lch_idx = cbits & 0x7; /* SDCCH/8 */
130 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
131 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
132 chan_nr, ts->pchan);
133 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
134 lch_idx = 0;
135 if (ts->pchan != GSM_PCHAN_CCCH &&
136 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
137 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
138 chan_nr, ts->pchan);
139 /* FIXME: we should not return first sdcch4 !!! */
140 } else {
141 fprintf(stderr, "unknown chan_nr=0x%02x\n", chan_nr);
142 return NULL;
143 }
144
145 lchan = &ts->lchan[lch_idx];
146
147 return lchan;
148}
149
150u_int8_t lchan2chan_nr(struct gsm_lchan *lchan)
151{
152 struct gsm_bts_trx_ts *ts = lchan->ts;
153 u_int8_t cbits, chan_nr;
154
155 switch (ts->pchan) {
156 case GSM_PCHAN_TCH_F:
157 cbits = 0x01;
158 break;
159 case GSM_PCHAN_TCH_H:
160 cbits = 0x02;
161 cbits += lchan->nr;
162 break;
163 case GSM_PCHAN_CCCH_SDCCH4:
164 cbits = 0x04;
165 cbits += lchan->nr;
166 break;
167 case GSM_PCHAN_SDCCH8_SACCH8C:
168 cbits = 0x08;
169 cbits += lchan->nr;
170 break;
171 default:
172 case GSM_PCHAN_CCCH:
173 cbits = 0x10;
174 break;
175 }
176
177 chan_nr = (cbits << 3) | (ts->nr & 0x7);
178
179 return chan_nr;
180}
181
Harald Welte52b1f982008-12-23 20:25:15 +0000182/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
183u_int64_t str_to_imsi(const char *imsi_str)
184{
185 u_int64_t ret;
186
187 ret = strtoull(imsi_str, NULL, 10);
188
189 return ret;
190}
191
192/* Table 5 Clause 7 TS 05.02 */
193unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
194{
195 if (!bs_ccch_sdcch_comb)
196 return 9 - bs_ag_blks_res;
197 else
198 return 3 - bs_ag_blks_res;
199}
200
201/* Chapter 6.5.2 of TS 05.02 */
202unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
203 unsigned int n_pag_blocks)
204{
205 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
206}
207
208/* Chapter 6.5.2 of TS 05.02 */
209unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
210 int n_pag_blocks)
211{
212 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
213}
214
Harald Welte8470bf22008-12-25 23:28:35 +0000215static struct msgb *rsl_msgb_alloc(void)
216{
217 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM);
218}
219
Harald Welte52b1f982008-12-23 20:25:15 +0000220/* Send a BCCH_INFO message as per Chapter 8.5.1 */
221int rsl_bcch_info(struct gsm_bts *bts, u_int8_t type,
222 const u_int8_t *data, int len)
223{
224 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000225 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000226
227 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
228 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
229 dh->chan_nr = RSL_CHAN_BCCH;
230
231 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
232 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
233
Harald Welte8470bf22008-12-25 23:28:35 +0000234 msg->trx = bts->c0;
235
236 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000237}
238
239int rsl_sacch_filling(struct gsm_bts *bts, u_int8_t type,
240 const u_int8_t *data, int len)
241{
242 struct abis_rsl_common_hdr *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000243 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000244
245 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
246 ch->msg_discr = ABIS_RSL_MDISC_TRX;
247 ch->msg_type = RSL_MT_SACCH_FILL;
248
249 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
Harald Welte702d8702008-12-26 20:25:35 +0000250 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
Harald Welte52b1f982008-12-23 20:25:15 +0000251
Harald Welte8470bf22008-12-25 23:28:35 +0000252 msg->trx = bts->c0;
253
254 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000255}
256
257/* Chapter 8.4.1 */
258int rsl_chan_activate(struct gsm_bts *bts, u_int8_t chan_nr,
259 u_int8_t act_type,
260 struct rsl_ie_chan_mode *chan_mode,
261 struct rsl_ie_chan_ident *chan_ident,
262 u_int8_t bs_power, u_int8_t ms_power,
263 u_int8_t ta)
264{
265 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000266 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000267 u_int8_t encr_info = 0x01;
268
269 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
270 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
271 dh->chan_nr = chan_nr;
272
273 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
274 /* For compatibility with Phase 1 */
275 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
276 (u_int8_t *) chan_mode);
277 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
Harald Welte702d8702008-12-26 20:25:35 +0000278 (u_int8_t *) chan_ident);
Harald Welte52b1f982008-12-23 20:25:15 +0000279 /* FIXME: this shoould be optional */
Harald Welte702d8702008-12-26 20:25:35 +0000280#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000281 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
282 (u_int8_t *) &encr_info);
283 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
Harald Welte702d8702008-12-26 20:25:35 +0000284#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000285 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
286 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
287
Harald Welte8470bf22008-12-25 23:28:35 +0000288 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000289}
290
291#define TSC 7
292
293int rsl_chan_activate_tch_f(struct gsm_bts_trx_ts *ts)
294{
295 u_int8_t chan_nr = rsl_enc_chan_nr(RSL_CHAN_Bm_ACCHs, 0, ts->nr);
296 u_int16_t arfcn = ts->trx->arfcn;
297 struct rsl_ie_chan_mode cm;
298 struct rsl_ie_chan_ident ci;
299
300 cm.dtx_dtu = 0;
301 cm.spd_ind = RSL_CMOD_SPD_SPEECH;
302 cm.chan_rt = RSL_CMOD_CRT_TCH_Bm;
303 cm.chan_rate = RSL_CMOD_SP_GSM1;
304
305 ci.chan_desc.iei = 0x64;
306 ci.chan_desc.chan_nr = chan_nr;
307 /* FIXME: this doesn't support hopping */
308 ci.chan_desc.oct3 = (TSC << 5) | ((arfcn & 0x3ff) >> 8);
309 ci.chan_desc.oct4 = arfcn & 0xff;
310#if 0
311 ci.mobile_alloc.tag = 0x72;
312 ci.mobile_alloc.len = 0; /* as per Section 9.3.5 */
313#endif
314
315 return rsl_chan_activate(ts->trx->bts, chan_nr, 0x01, &cm, &ci, 0x01, 0x0f, 0x00);
316}
317
Harald Welte702d8702008-12-26 20:25:35 +0000318int rsl_chan_activate_sdcch4(struct gsm_bts_trx_ts *ts, int subslot)
Harald Welte52b1f982008-12-23 20:25:15 +0000319{
Harald Welte702d8702008-12-26 20:25:35 +0000320 u_int8_t chan_nr = rsl_enc_chan_nr(RSL_CHAN_SDCCH4_ACCH, subslot, ts->nr);
Harald Welte52b1f982008-12-23 20:25:15 +0000321 u_int16_t arfcn = ts->trx->arfcn;
322 struct rsl_ie_chan_mode cm;
323 struct rsl_ie_chan_ident ci;
324
325 cm.dtx_dtu = 0x00;
326 cm.spd_ind = RSL_CMOD_SPD_SIGN;
327 cm.chan_rt = RSL_CMOD_CRT_SDCCH;
328 cm.chan_rate = 0x00;
329
330 ci.chan_desc.iei = 0x64;
331 ci.chan_desc.chan_nr = chan_nr;
332 ci.chan_desc.oct3 = (TSC << 5) | ((arfcn & 0x3ff) >> 8);
333 ci.chan_desc.oct4 = arfcn & 0xff;
334
Holger Freytherfacfce92008-12-27 09:38:51 +0000335 /* FIXME: we're sending BS power IE, which Abissim doesn't */
Harald Welte52b1f982008-12-23 20:25:15 +0000336 return rsl_chan_activate(ts->trx->bts, chan_nr, 0x00, &cm, &ci, 0x01, 0x0f, 0x00);
337}
338
Harald Welte4b634542008-12-27 01:55:51 +0000339int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type, u_int8_t ta)
340{
341 struct abis_rsl_dchan_hdr *dh;
342 struct msgb *msg = rsl_msgb_alloc();
343 /* FXIME: don't hardcode these!! */
344 u_int8_t encr_info = 0x01;
345 u_int8_t ms_power = 0x0f;
346 u_int8_t bs_power = 0x01;
347
348 u_int8_t chan_nr = lchan2chan_nr(lchan);
349 u_int16_t arfcn = lchan->ts->trx->arfcn;
350 struct rsl_ie_chan_mode cm;
351 struct rsl_ie_chan_ident ci;
352
353 /* FIXME: what to do with data calls ? */
354 cm.dtx_dtu = 0x00;
355 switch (lchan->type) {
356 case GSM_LCHAN_SDCCH:
357 cm.spd_ind = RSL_CMOD_SPD_SIGN;
358 cm.chan_rt = RSL_CMOD_CRT_SDCCH;
359 cm.chan_rate = 0x00;
360 break;
361 case GSM_LCHAN_TCH_F:
362 cm.spd_ind = RSL_CMOD_SPD_SPEECH;
363 cm.chan_rt = RSL_CMOD_CRT_TCH_Bm;
364 cm.chan_rate = 0x11; /* speech coding alg version 2*/
365 break;
366 }
367
368 ci.chan_desc.iei = 0x64;
369 ci.chan_desc.chan_nr = chan_nr;
370 ci.chan_desc.oct3 = (TSC << 5) | ((arfcn & 0x3ff) >> 8);
371 ci.chan_desc.oct4 = arfcn & 0xff;
372
373 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
374 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
375 dh->chan_nr = chan_nr;
376
377 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
378 /* For compatibility with Phase 1 */
379 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
380 (u_int8_t *) &cm);
381 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
382 (u_int8_t *) &ci);
383 /* FIXME: this shoould be optional */
384#if 0
385 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
386 (u_int8_t *) &encr_info);
387 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
388#endif
389 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
390 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
391
392 return abis_rsl_sendmsg(msg);
393}
394
Harald Welte8470bf22008-12-25 23:28:35 +0000395int rsl_chan_release(struct gsm_lchan *lchan)
Harald Welte52b1f982008-12-23 20:25:15 +0000396{
397 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000398 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000399
400 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
401 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
Harald Welte8470bf22008-12-25 23:28:35 +0000402 dh->chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000403
Harald Welte8470bf22008-12-25 23:28:35 +0000404 msg->lchan = lchan;
405 msg->trx = lchan->ts->trx;
406
407 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000408}
409
410int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
411 u_int8_t *ms_ident, u_int8_t chan_needed)
412{
413 struct abis_rsl_dchan_hdr *dh;
Harald Welte8470bf22008-12-25 23:28:35 +0000414 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000415
416 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
417 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
418 dh->chan_nr = RSL_CHAN_PCH_AGCH;
419
420 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
421 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len, ms_ident);
422 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
423
Harald Welte8470bf22008-12-25 23:28:35 +0000424 msg->trx = bts->c0;
425
426 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000427}
428
429int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
430{
431 int i, len = strlen(str_in);
432
433 for (i = 0; i < len; i++) {
434 int num = str_in[i] - 0x30;
435 if (num < 0 || num > 9)
436 return -1;
437 if (i % 2 == 0)
438 bcd_out[i/2] = num;
439 else
440 bcd_out[i/2] |= (num << 4);
441 }
442
443 return 0;
444}
445
446# if 0
447int rsl_paging_cmd_imsi(struct gsm_bts *bts, u_int8_t chan_needed, const char *imsi_str)
448{
449 /* FIXME: derive the MS Identity */
450 return rsl_paging_cmd(bts, paging_group, x, y, chan_needed);
451}
452#endif
453
Harald Welte702d8702008-12-26 20:25:35 +0000454/* Chapter 8.5.6 */
Harald Welte52b1f982008-12-23 20:25:15 +0000455int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
456{
Harald Welte8470bf22008-12-25 23:28:35 +0000457 struct msgb *msg = rsl_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000458 struct abis_rsl_dchan_hdr *dh;
459
460 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
461 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
462 dh->chan_nr = RSL_CHAN_PCH_AGCH;
463
464 /* If phase 2, FULL_IMM_ASS_INFO */
465
466 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
467
Harald Welte8470bf22008-12-25 23:28:35 +0000468 msg->trx = bts->c0;
469
470 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000471}
472
Harald Welte8470bf22008-12-25 23:28:35 +0000473/* Send "DATA REQUEST" message with given L3 Info payload */
Harald Welte52b1f982008-12-23 20:25:15 +0000474/* Chapter 8.3.1 */
Harald Welte8470bf22008-12-25 23:28:35 +0000475int rsl_data_request(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +0000476{
Harald Welte8470bf22008-12-25 23:28:35 +0000477 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000478 struct abis_rsl_rll_hdr *rh;
479
Harald Welte8470bf22008-12-25 23:28:35 +0000480 if (msg->lchan == NULL) {
481 fprintf(stderr, "cannot send DATA REQUEST to unknown lchan\n");
482 return -EINVAL;
483 }
Harald Welte52b1f982008-12-23 20:25:15 +0000484
Harald Welte8470bf22008-12-25 23:28:35 +0000485 /* First push the L3 IE tag and length */
Harald Welte4b634542008-12-27 01:55:51 +0000486 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welte8470bf22008-12-25 23:28:35 +0000487
488 /* Then push the RSL header */
Harald Welte52b1f982008-12-23 20:25:15 +0000489 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
490 init_llm_hdr(rh, RSL_MT_DATA_REQ);
Harald Welte8470bf22008-12-25 23:28:35 +0000491 rh->chan_nr = lchan2chan_nr(msg->lchan);
492 rh->link_id = link_id;
Harald Welte52b1f982008-12-23 20:25:15 +0000493
Harald Welte8470bf22008-12-25 23:28:35 +0000494 msg->trx = msg->lchan->ts->trx;
495
496 return abis_rsl_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000497}
498
Harald Welte702d8702008-12-26 20:25:35 +0000499/* Chapter 8.4.2: Channel Activate Acknowledge */
500static int rsl_rx_chan_act_ack(struct msgb *msg)
501{
502 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
503
504 /* BTS has confirmed channel activation, we now need
505 * to assign the activated channel to the MS */
Harald Welte4b634542008-12-27 01:55:51 +0000506 if (rslh->ie_chan != RSL_IE_CHAN_NR)
507 return -EINVAL;
508
509 DEBUGP(DRSL, "Channel Activate ACK Channel 0x%02x\n", rslh->chan_nr);
Harald Welte702d8702008-12-26 20:25:35 +0000510
Harald Welte4b634542008-12-27 01:55:51 +0000511 return 0;
512}
Harald Welte702d8702008-12-26 20:25:35 +0000513
Harald Welte4b634542008-12-27 01:55:51 +0000514/* Chapter 8.4.3: Channel Activate NACK */
515static int rsl_rx_chan_act_nack(struct msgb *msg)
516{
517 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
518
519 /* BTS has confirmed channel activation, we now need
520 * to assign the activated channel to the MS */
521 if (rslh->ie_chan != RSL_IE_CHAN_NR)
522 return -EINVAL;
523
524 DEBUGP(DRSL, "Channel Activate NACK Channel 0x%02x\n", rslh->chan_nr);
525
526 return 0;
Harald Welte702d8702008-12-26 20:25:35 +0000527}
528
Harald Welte52b1f982008-12-23 20:25:15 +0000529static int abis_rsl_rx_dchan(struct msgb *msg)
530{
Harald Welte8470bf22008-12-25 23:28:35 +0000531 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
532 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000533
Harald Welte8470bf22008-12-25 23:28:35 +0000534 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
535
536 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000537 case RSL_MT_CHAN_ACTIV_ACK:
Harald Welte4b634542008-12-27 01:55:51 +0000538 rc = rsl_rx_chan_act_ack(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000539 rc = rsl_rx_chan_act_ack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000540 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000541 case RSL_MT_CHAN_ACTIV_NACK:
Harald Welte4b634542008-12-27 01:55:51 +0000542 rc = rsl_rx_chan_act_nack(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000543 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000544 case RSL_MT_CONN_FAIL:
Harald Welte14537e52008-12-27 10:29:08 +0000545 DEBUGP(DRSL, "rsl_rx_dchan: Connection Fail, release channel\n");
546 rc = rsl_chan_release(msg->lchan);
Harald Welte8470bf22008-12-25 23:28:35 +0000547 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000548 case RSL_MT_MEAS_RES:
Harald Welte8470bf22008-12-25 23:28:35 +0000549 DEBUGP(DRSL, "rsl_rx_dchan: Measurement Result\n");
550 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000551 case RSL_MT_MODE_MODIFY_ACK:
552 case RSL_MT_MODE_MODIFY_NACK:
553 case RSL_MT_PHY_CONTEXT_CONF:
554 case RSL_MT_PREPROC_MEAS_RES:
555 case RSL_MT_RF_CHAN_REL_ACK:
556 case RSL_MT_TALKER_DET:
557 case RSL_MT_LISTENER_DET:
558 case RSL_MT_REMOTE_CODEC_CONF_REP:
559 case RSL_MT_MR_CODEC_MOD_ACK:
560 case RSL_MT_MR_CODEC_MOD_NACK:
561 case RSL_MT_MR_CODEC_MOD_PER:
562 fprintf(stderr, "Unimplemented Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000563 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000564 break;
565 default:
566 fprintf(stderr, "unknown Abis RSL DChan msg 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000567 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000568 return -EINVAL;
569 }
Harald Welte8470bf22008-12-25 23:28:35 +0000570 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000571}
572
Harald Welte702d8702008-12-26 20:25:35 +0000573static int rsl_rx_error_rep(struct msgb *msg)
574{
575 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
576 u_int8_t cause_len;
577
578 if (rslh->data[0] != RSL_IE_CAUSE)
579 return -EINVAL;
580
581 cause_len = rslh->data[1];
Harald Welte4b634542008-12-27 01:55:51 +0000582 fprintf(stdout, "RSL ERROR REPORT, Cause ");
Harald Welte702d8702008-12-26 20:25:35 +0000583 hexdump(&rslh->data[2], cause_len);
584
585 return 0;
586}
587
Harald Welte52b1f982008-12-23 20:25:15 +0000588static int abis_rsl_rx_trx(struct msgb *msg)
589{
Harald Welte702d8702008-12-26 20:25:35 +0000590 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000591 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000592
593 switch (rslh->msg_type) {
Harald Welte702d8702008-12-26 20:25:35 +0000594 case RSL_MT_ERROR_REPORT:
595 rc = rsl_rx_error_rep(msg);
596 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000597 case RSL_MT_RF_RES_IND:
598 /* interference on idle channels of TRX */
599 case RSL_MT_OVERLOAD:
600 /* indicate CCCH / ACCH / processor overload */
Harald Welte52b1f982008-12-23 20:25:15 +0000601 fprintf(stderr, "Unimplemented Abis RSL TRX message type 0x%02x\n",
602 rslh->msg_type);
603 break;
604 default:
605 fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
606 rslh->msg_type);
607 return -EINVAL;
608 }
Harald Welte8470bf22008-12-25 23:28:35 +0000609 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000610}
611
Harald Welte8470bf22008-12-25 23:28:35 +0000612/* MS has requested a channel on the RACH */
Harald Welte52b1f982008-12-23 20:25:15 +0000613static int rsl_rx_chan_rqd(struct msgb *msg)
614{
Harald Welte702d8702008-12-26 20:25:35 +0000615 struct gsm_bts *bts = msg->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000616 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
617 struct gsm48_req_ref *rqd_ref;
Harald Welte52b1f982008-12-23 20:25:15 +0000618 struct gsm48_imm_ass ia;
Harald Welte8470bf22008-12-25 23:28:35 +0000619 enum gsm_chan_t lctype;
620 struct gsm_lchan *lchan;
621 u_int8_t rqd_ta;
622
Harald Welte52b1f982008-12-23 20:25:15 +0000623 u_int16_t arfcn;
624 u_int8_t ts_number, subch;
625
Harald Welte8470bf22008-12-25 23:28:35 +0000626 /* parse request reference to be used in immediate assign */
627 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
628 return -EINVAL;
629
630 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
631
632 /* parse access delay and use as TA */
633 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
634 return -EINVAL;
635 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
636
637 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
638 * request reference RA */
639 lctype = get_ctype_by_chreq(bts, rqd_ref->ra);
640
641 /* check availability / allocate channel */
642 lchan = lchan_alloc(bts, lctype);
643 if (!lchan) {
644 fprintf(stderr, "CHAN RQD: no resources\n");
645 /* FIXME: send some kind of reject ?!? */
646 return -ENOMEM;
647 }
648
649 ts_number = lchan->ts->nr;
650 arfcn = lchan->ts->trx->arfcn;
651 subch = lchan->nr;
Harald Welte52b1f982008-12-23 20:25:15 +0000652
Harald Welte8470bf22008-12-25 23:28:35 +0000653 DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %u\n",
654 arfcn, ts_number, subch, lchan->type);
655
Harald Welte4b634542008-12-27 01:55:51 +0000656#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000657 /* send CHANNEL ACTIVATION on RSL to BTS */
Harald Welte702d8702008-12-26 20:25:35 +0000658 if (lchan->ts->pchan == GSM_PCHAN_CCCH_SDCCH4)
659 rsl_chan_activate_sdcch4(lchan->ts, subch);
Harald Welte8470bf22008-12-25 23:28:35 +0000660 else
661 rsl_chan_activate_tch_f(lchan->ts);
Harald Welte4b634542008-12-27 01:55:51 +0000662#else
663 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta);
664#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000665
666 /* create IMMEDIATE ASSIGN 04.08 messge */
667 memset(&ia, 0, sizeof(ia));
668 ia.l2_plen = 0x2d;
669 ia.proto_discr = GSM48_PDISC_RR;
670 ia.msg_type = GSM48_MT_RR_IMM_ASS;
671 ia.page_mode = GSM48_PM_NORMAL;
Harald Welte4b634542008-12-27 01:55:51 +0000672 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000673 ia.chan_desc.h0.h = 0;
674 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
675 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
676 ia.chan_desc.h0.tsc = 7;
Harald Welte8470bf22008-12-25 23:28:35 +0000677 /* use request reference extracted from CHAN_RQD */
678 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
679 ia.timing_advance = rqd_ta;
Harald Welte52b1f982008-12-23 20:25:15 +0000680 ia.mob_alloc_len = 0;
681
682 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
683 return rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
684}
685
686static int abis_rsl_rx_cchan(struct msgb *msg)
687{
Harald Welte8470bf22008-12-25 23:28:35 +0000688 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg) ;
689 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000690
Harald Welte8470bf22008-12-25 23:28:35 +0000691 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
692
693 switch (rslh->c.msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000694 case RSL_MT_CHAN_RQD:
695 /* MS has requested a channel on the RACH */
696 rc = rsl_rx_chan_rqd(msg);
697 break;
698 case RSL_MT_DELETE_IND:
699 /* CCCH overloaded, IMM_ASSIGN was dropped */
700 case RSL_MT_CBCH_LOAD_IND:
701 /* current load on the CBCH */
702 case RSL_MT_CCCH_LOAD_IND:
703 /* current load on the CCCH */
704 fprintf(stderr, "Unimplemented Abis RSL TRX message type 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000705 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000706 break;
707 default:
708 fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
Harald Welte8470bf22008-12-25 23:28:35 +0000709 rslh->c.msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000710 return -EINVAL;
711 }
Harald Welte8470bf22008-12-25 23:28:35 +0000712
713 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000714}
715
Harald Welte4b634542008-12-27 01:55:51 +0000716static int rsl_rx_rll_err_ind(struct msgb *msg)
717{
718 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
719 u_int8_t *rlm_cause = rllh->data;
720
721 DEBUGP(DRLL, "RLL ERROR INDICATION: chan_nr=0x%02x cause=0x%02x\n",
722 rllh->chan_nr, rlm_cause[1]);
723
724 return 0;
725}
Harald Welte52b1f982008-12-23 20:25:15 +0000726/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
727 0x02, 0x06,
728 0x01, 0x20,
729 0x02, 0x00,
730 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
731
732static int abis_rsl_rx_rll(struct msgb *msg)
733{
734 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
735 int rc;
Harald Welte8470bf22008-12-25 23:28:35 +0000736
737 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
Harald Welte52b1f982008-12-23 20:25:15 +0000738
739 switch (rllh->c.msg_type) {
740 case RSL_MT_DATA_IND:
Harald Welte4b634542008-12-27 01:55:51 +0000741 DEBUGP(DRLL, "DATA INDICATION chan_nr=0x%02x\n", rllh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000742 /* FIXME: Verify L3 info element */
Harald Welte702d8702008-12-26 20:25:35 +0000743 msg->l3h = &rllh->data[3];
Harald Welte52b1f982008-12-23 20:25:15 +0000744 rc = gsm0408_rcvmsg(msg);
745 break;
746 case RSL_MT_EST_IND:
Harald Welte4b634542008-12-27 01:55:51 +0000747 DEBUGP(DRLL, "ESTABLISH INDICATION chan_nr=0x%02x\n", rllh->chan_nr);
Harald Welte8470bf22008-12-25 23:28:35 +0000748 /* FIXME: Verify L3 info element */
Harald Welte702d8702008-12-26 20:25:35 +0000749 msg->l3h = &rllh->data[3];
Harald Welte52b1f982008-12-23 20:25:15 +0000750 rc = gsm0408_rcvmsg(msg);
751 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000752 case RSL_MT_REL_IND:
Harald Welte4b634542008-12-27 01:55:51 +0000753 DEBUGP(DRLL, "RELEASE INDICATION chan_nr=0x%02x\n", rllh->chan_nr);
Harald Welte0f7bf9c2008-12-27 10:18:47 +0000754 rc = rsl_chan_release(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000755 lchan_free(msg->lchan);
Harald Welte4b634542008-12-27 01:55:51 +0000756 break;
757 case RSL_MT_ERROR_IND:
758 rc = rsl_rx_rll_err_ind(msg);
759 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000760 case RSL_MT_UNIT_DATA_IND:
761 fprintf(stderr, "unimplemented Abis RLL message type 0x%02x\n",
762 rllh->c.msg_type);
763 break;
764 default:
765 fprintf(stderr, "unknown Abis RLL message type 0x%02x\n",
766 rllh->c.msg_type);
767 }
Harald Welte8470bf22008-12-25 23:28:35 +0000768 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000769}
770
771/* Entry-point where L2 RSL from BTS enters */
Harald Welte8470bf22008-12-25 23:28:35 +0000772int abis_rsl_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000773{
774 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
775 unsigned int l2_len = (void *)msg->tail - msgb_l2(msg);
776 int rc;
777
778 switch (rslh->msg_discr & 0xfe) {
779 case ABIS_RSL_MDISC_RLL:
780 rc = abis_rsl_rx_rll(msg);
781 break;
782 case ABIS_RSL_MDISC_DED_CHAN:
783 rc = abis_rsl_rx_dchan(msg);
784 break;
785 case ABIS_RSL_MDISC_COM_CHAN:
Harald Welte52b1f982008-12-23 20:25:15 +0000786 rc = abis_rsl_rx_cchan(msg);
787 break;
Harald Welte8470bf22008-12-25 23:28:35 +0000788 case ABIS_RSL_MDISC_TRX:
789 rc = abis_rsl_rx_trx(msg);
790 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000791 case ABIS_RSL_MDISC_LOC:
792 default:
793 fprintf(stderr, "unknown RSL message discriminator 0x%02x\n",
794 rslh->msg_discr);
795 return -EINVAL;
796 }
Harald Welte4f4a3902008-12-26 00:04:49 +0000797 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000798 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000799}