blob: df95e7979953e40356787bae855e81e86e34129b [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>
33#include <openbsc/abis_rsl.h>
34#include <openbsc/chan_alloc.h>
35#include <openbsc/debug.h>
36#include <openbsc/tlv.h>
37#include <openbsc/paging.h>
38#include <openbsc/signal.h>
39
40#define RSL_ALLOC_SIZE 1024
41#define RSL_ALLOC_HEADROOM 128
42
43#define MAX(a, b) (a) >= (b) ? (a) : (b)
44
45static const struct tlv_definition rsl_att_tlvdef = {
46 .def = {
47 [RSL_IE_CHAN_NR] = { TLV_TYPE_TV },
48 [RSL_IE_LINK_IDENT] = { TLV_TYPE_TV },
49 [RSL_IE_ACT_TYPE] = { TLV_TYPE_TV },
50 [RSL_IE_BS_POWER] = { TLV_TYPE_TV },
51 [RSL_IE_CHAN_IDENT] = { TLV_TYPE_TLV },
52 [RSL_IE_CHAN_MODE] = { TLV_TYPE_TLV },
53 [RSL_IE_ENCR_INFO] = { TLV_TYPE_TLV },
54 [RSL_IE_FRAME_NUMBER] = { TLV_TYPE_FIXED, 2 },
55 [RSL_IE_HANDO_REF] = { TLV_TYPE_TV },
56 [RSL_IE_L1_INFO] = { TLV_TYPE_FIXED, 2 },
57 [RSL_IE_L3_INFO] = { TLV_TYPE_TL16V },
58 [RSL_IE_MS_IDENTITY] = { TLV_TYPE_TLV },
59 [RSL_IE_MS_POWER] = { TLV_TYPE_TV },
60 [RSL_IE_PAGING_GROUP] = { TLV_TYPE_TV },
61 [RSL_IE_PAGING_LOAD] = { TLV_TYPE_FIXED, 2 },
62 [RSL_IE_PYHS_CONTEXT] = { TLV_TYPE_TLV },
63 [RSL_IE_ACCESS_DELAY] = { TLV_TYPE_TV },
64 [RSL_IE_RACH_LOAD] = { TLV_TYPE_TLV },
65 [RSL_IE_REQ_REFERENCE] = { TLV_TYPE_FIXED, 3 },
66 [RSL_IE_RELEASE_MODE] = { TLV_TYPE_TV },
67 [RSL_IE_RESOURCE_INFO] = { TLV_TYPE_TLV },
68 [RSL_IE_RLM_CAUSE] = { TLV_TYPE_TLV },
69 [RSL_IE_STARTNG_TIME] = { TLV_TYPE_FIXED, 2 },
70 [RSL_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
71 [RSL_IE_UPLINK_MEAS] = { TLV_TYPE_TLV },
72 [RSL_IE_CAUSE] = { TLV_TYPE_TLV },
73 [RSL_IE_MEAS_RES_NR] = { TLV_TYPE_TV },
74 [RSL_IE_MSG_ID] = { TLV_TYPE_TV },
75 [RSL_IE_SYSINFO_TYPE] = { TLV_TYPE_TV },
76 [RSL_IE_MS_POWER_PARAM] = { TLV_TYPE_TLV },
77 [RSL_IE_BS_POWER_PARAM] = { TLV_TYPE_TLV },
78 [RSL_IE_PREPROC_PARAM] = { TLV_TYPE_TLV },
79 [RSL_IE_PREPROC_MEAS] = { TLV_TYPE_TLV },
80 [RSL_IE_IMM_ASS_INFO] = { TLV_TYPE_TLV },
81 [RSL_IE_SMSCB_INFO] = { TLV_TYPE_FIXED, 23 },
82 [RSL_IE_MS_TIMING_OFFSET] = { TLV_TYPE_TV },
83 [RSL_IE_ERR_MSG] = { TLV_TYPE_TLV },
84 [RSL_IE_FULL_BCCH_INFO] = { TLV_TYPE_TLV },
85 [RSL_IE_CHAN_NEEDED] = { TLV_TYPE_TV },
86 [RSL_IE_CB_CMD_TYPE] = { TLV_TYPE_TV },
87 [RSL_IE_SMSCB_MSG] = { TLV_TYPE_TLV },
88 [RSL_IE_FULL_IMM_ASS_INFO] = { TLV_TYPE_TLV },
89 [RSL_IE_SACCH_INFO] = { TLV_TYPE_TLV },
90 [RSL_IE_CBCH_LOAD_INFO] = { TLV_TYPE_TV },
91 [RSL_IE_SMSCB_CHAN_INDICATOR] = { TLV_TYPE_TV },
92 [RSL_IE_GROUP_CALL_REF] = { TLV_TYPE_TLV },
93 [RSL_IE_CHAN_DESC] = { TLV_TYPE_TLV },
94 [RSL_IE_NCH_DRX_INFO] = { TLV_TYPE_TLV },
95 [RSL_IE_CMD_INDICATOR] = { TLV_TYPE_TLV },
96 [RSL_IE_EMLPP_PRIO] = { TLV_TYPE_TV },
97 [RSL_IE_UIC] = { TLV_TYPE_TLV },
98 [RSL_IE_MAIN_CHAN_REF] = { TLV_TYPE_TV },
99 [RSL_IE_MR_CONFIG] = { TLV_TYPE_TLV },
100 [RSL_IE_MR_CONTROL] = { TLV_TYPE_TV },
101 [RSL_IE_SUP_CODEC_TYPES] = { TLV_TYPE_TLV },
102 [RSL_IE_CODEC_CONFIG] = { TLV_TYPE_TLV },
103 [RSL_IE_RTD] = { TLV_TYPE_TV },
104 [RSL_IE_TFO_STATUS] = { TLV_TYPE_TV },
105 [RSL_IE_LLP_APDU] = { TLV_TYPE_TLV },
Harald Welte1610d302009-07-12 09:56:39 +0200106 [RSL_IE_IPAC_PROXY_UDP] = { TLV_TYPE_FIXED, 2 },
107 [RSL_IE_IPAC_BSCMPL_TOUT] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800108 [RSL_IE_IPAC_REMOTE_IP] = { TLV_TYPE_FIXED, 4 },
109 [RSL_IE_IPAC_REMOTE_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200110 [RSL_IE_IPAC_RTP_PAYLOAD] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800111 [RSL_IE_IPAC_LOCAL_PORT] = { TLV_TYPE_FIXED, 2 },
Harald Welteb9498952009-07-12 09:45:05 +0200112 [RSL_IE_IPAC_SPEECH_MODE] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200113 [RSL_IE_IPAC_LOCAL_IP] = { TLV_TYPE_FIXED, 4 },
Harald Welteb9498952009-07-12 09:45:05 +0200114 [RSL_IE_IPAC_CONN_ID] = { TLV_TYPE_FIXED, 2 },
Harald Welte1610d302009-07-12 09:56:39 +0200115 [RSL_IE_IPAC_RTP_CSD_FMT] = { TLV_TYPE_TV },
116 [RSL_IE_IPAC_RTP_JIT_BUF] = { TLV_TYPE_FIXED, 2 },
117 [RSL_IE_IPAC_RTP_COMPR] = { TLV_TYPE_TV },
Harald Welteb9498952009-07-12 09:45:05 +0200118 [RSL_IE_IPAC_RTP_PAYLOAD2] = { TLV_TYPE_TV },
Harald Welte1610d302009-07-12 09:56:39 +0200119 [RSL_IE_IPAC_RTP_MPLEX] = { TLV_TYPE_FIXED, 8 },
120 [RSL_IE_IPAC_RTP_MPLEX_ID] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +0800121 },
122};
123#define rsl_tlv_parse(dec, buf, len) \
124 tlv_parse(dec, &rsl_att_tlvdef, buf, len, 0, 0)
125
126static u_int8_t mdisc_by_msgtype(u_int8_t msg_type)
127{
128 /* mask off the transparent bit ? */
129 msg_type &= 0xfe;
130
131 if ((msg_type & 0xf0) == 0x00)
132 return ABIS_RSL_MDISC_RLL;
133 if ((msg_type & 0xf0) == 0x10) {
134 if (msg_type >= 0x19 && msg_type <= 0x22)
135 return ABIS_RSL_MDISC_TRX;
136 else
137 return ABIS_RSL_MDISC_COM_CHAN;
138 }
139 if ((msg_type & 0xe0) == 0x20)
140 return ABIS_RSL_MDISC_DED_CHAN;
141
142 return ABIS_RSL_MDISC_LOC;
143}
144
145static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh,
146 u_int8_t msg_type)
147{
148 dh->c.msg_discr = mdisc_by_msgtype(msg_type);
149 dh->c.msg_type = msg_type;
150 dh->ie_chan = RSL_IE_CHAN_NR;
151}
152
153static inline void init_llm_hdr(struct abis_rsl_rll_hdr *dh,
154 u_int8_t msg_type)
155{
156 /* dh->c.msg_discr = mdisc_by_msgtype(msg_type); */
157 dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
158 dh->c.msg_type = msg_type;
159 dh->ie_chan = RSL_IE_CHAN_NR;
160 dh->ie_link_id = RSL_IE_LINK_IDENT;
161}
162
163
164/* encode channel number as per Section 9.3.1 */
165u_int8_t rsl_enc_chan_nr(u_int8_t type, u_int8_t subch, u_int8_t timeslot)
166{
167 u_int8_t ret;
168
169 ret = (timeslot & 0x07) | type;
170
171 switch (type) {
172 case RSL_CHAN_Lm_ACCHs:
173 subch &= 0x01;
174 break;
175 case RSL_CHAN_SDCCH4_ACCH:
176 subch &= 0x07;
177 break;
178 case RSL_CHAN_SDCCH8_ACCH:
179 subch &= 0x07;
180 break;
181 default:
182 /* no subchannels allowed */
183 subch = 0x00;
184 break;
185 }
186 ret |= (subch << 3);
187
188 return ret;
189}
190
191/* determine logical channel based on TRX and channel number IE */
192struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, u_int8_t chan_nr)
193{
194 struct gsm_lchan *lchan;
195 u_int8_t ts_nr = chan_nr & 0x07;
196 u_int8_t cbits = chan_nr >> 3;
197 u_int8_t lch_idx;
198 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
199
200 if (cbits == 0x01) {
201 lch_idx = 0; /* TCH/F */
202 if (ts->pchan != GSM_PCHAN_TCH_F)
203 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
204 chan_nr, ts->pchan);
205 } else if ((cbits & 0x1e) == 0x02) {
206 lch_idx = cbits & 0x1; /* TCH/H */
207 if (ts->pchan != GSM_PCHAN_TCH_H)
208 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
209 chan_nr, ts->pchan);
210 } else if ((cbits & 0x1c) == 0x04) {
211 lch_idx = cbits & 0x3; /* SDCCH/4 */
212 if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
213 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
214 chan_nr, ts->pchan);
215 } else if ((cbits & 0x18) == 0x08) {
216 lch_idx = cbits & 0x7; /* SDCCH/8 */
217 if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C)
218 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
219 chan_nr, ts->pchan);
220 } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) {
221 lch_idx = 0;
222 if (ts->pchan != GSM_PCHAN_CCCH &&
223 ts->pchan != GSM_PCHAN_CCCH_SDCCH4)
224 fprintf(stderr, "chan_nr=0x%02x but pchan=%u\n",
225 chan_nr, ts->pchan);
226 /* FIXME: we should not return first sdcch4 !!! */
227 } else {
228 fprintf(stderr, "unknown chan_nr=0x%02x\n", chan_nr);
229 return NULL;
230 }
231
232 lchan = &ts->lchan[lch_idx];
233
234 return lchan;
235}
236
237u_int8_t lchan2chan_nr(struct gsm_lchan *lchan)
238{
239 struct gsm_bts_trx_ts *ts = lchan->ts;
240 u_int8_t cbits, chan_nr;
241
242 switch (ts->pchan) {
243 case GSM_PCHAN_TCH_F:
244 cbits = 0x01;
245 break;
246 case GSM_PCHAN_TCH_H:
247 cbits = 0x02;
248 cbits += lchan->nr;
249 break;
250 case GSM_PCHAN_CCCH_SDCCH4:
251 cbits = 0x04;
252 cbits += lchan->nr;
253 break;
254 case GSM_PCHAN_SDCCH8_SACCH8C:
255 cbits = 0x08;
256 cbits += lchan->nr;
257 break;
258 default:
259 case GSM_PCHAN_CCCH:
260 cbits = 0x10;
261 break;
262 }
263
264 chan_nr = (cbits << 3) | (ts->nr & 0x7);
265
266 return chan_nr;
267}
268
269/* As per TS 03.03 Section 2.2, the IMSI has 'not more than 15 digits' */
270u_int64_t str_to_imsi(const char *imsi_str)
271{
272 u_int64_t ret;
273
274 ret = strtoull(imsi_str, NULL, 10);
275
276 return ret;
277}
278
279/* Table 5 Clause 7 TS 05.02 */
280unsigned int n_pag_blocks(int bs_ccch_sdcch_comb, unsigned int bs_ag_blks_res)
281{
282 if (!bs_ccch_sdcch_comb)
283 return 9 - bs_ag_blks_res;
284 else
285 return 3 - bs_ag_blks_res;
286}
287
288/* Chapter 6.5.2 of TS 05.02 */
289unsigned int get_ccch_group(u_int64_t imsi, unsigned int bs_cc_chans,
290 unsigned int n_pag_blocks)
291{
292 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks;
293}
294
295/* Chapter 6.5.2 of TS 05.02 */
296unsigned int get_paging_group(u_int64_t imsi, unsigned int bs_cc_chans,
297 int n_pag_blocks)
298{
299 return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks;
300}
301
302static struct msgb *rsl_msgb_alloc(void)
303{
Harald Welte9cfc9352009-06-26 19:39:35 +0200304 return msgb_alloc_headroom(RSL_ALLOC_SIZE, RSL_ALLOC_HEADROOM,
305 "RSL");
Harald Welte59b04682009-06-10 05:40:52 +0800306}
307
308#define MACBLOCK_SIZE 23
309static void pad_macblock(u_int8_t *out, const u_int8_t *in, int len)
310{
311 memcpy(out, in, len);
312
313 if (len < MACBLOCK_SIZE)
314 memset(out+len, 0x2b, MACBLOCK_SIZE-len);
315}
316
317static void print_rsl_cause(u_int8_t *cause_tlv)
318{
319 u_int8_t cause_len;
320 int i;
321
322 if (cause_tlv[0] != RSL_IE_CAUSE)
323 return;
324
325 cause_len = cause_tlv[1];
326 DEBUGPC(DRSL, "CAUSE: ");
327 for (i = 0; i < cause_len; i++)
328 DEBUGPC(DRSL, "%02x ", cause_tlv[2+i]);
329}
330
331/* Send a BCCH_INFO message as per Chapter 8.5.1 */
332int rsl_bcch_info(struct gsm_bts_trx *trx, u_int8_t type,
333 const u_int8_t *data, int len)
334{
335 struct abis_rsl_dchan_hdr *dh;
336 struct msgb *msg = rsl_msgb_alloc();
337
338 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof*dh);
339 init_dchan_hdr(dh, RSL_MT_BCCH_INFO);
340 dh->chan_nr = RSL_CHAN_BCCH;
341
342 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
343 msgb_tlv_put(msg, RSL_IE_FULL_BCCH_INFO, len, data);
344
345 msg->trx = trx;
346
347 return abis_rsl_sendmsg(msg);
348}
349
350int rsl_sacch_filling(struct gsm_bts_trx *trx, u_int8_t type,
351 const u_int8_t *data, int len)
352{
353 struct abis_rsl_common_hdr *ch;
354 struct msgb *msg = rsl_msgb_alloc();
355
356 ch = (struct abis_rsl_common_hdr *) msgb_put(msg, sizeof(*ch));
357 ch->msg_discr = ABIS_RSL_MDISC_TRX;
358 ch->msg_type = RSL_MT_SACCH_FILL;
359
360 msgb_tv_put(msg, RSL_IE_SYSINFO_TYPE, type);
361 msgb_tl16v_put(msg, RSL_IE_L3_INFO, len, data);
362
363 msg->trx = trx;
364
365 return abis_rsl_sendmsg(msg);
366}
367
Harald Welte91afe4c2009-06-20 18:15:19 +0200368int rsl_chan_bs_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int db)
369{
370 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200371 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200372 u_int8_t chan_nr = lchan2chan_nr(lchan);
373
374 db = abs(db);
375 if (db > 30)
376 return -EINVAL;
377
Harald Welteed831842009-06-27 03:09:08 +0200378 msg = rsl_msgb_alloc();
379
Harald Welte91afe4c2009-06-20 18:15:19 +0200380 lchan->bs_power = db/2;
381 if (fpc)
382 lchan->bs_power |= 0x10;
383
384 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
385 init_dchan_hdr(dh, RSL_MT_BS_POWER_CONTROL);
386 dh->chan_nr = chan_nr;
387
388 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
389
390 msg->trx = lchan->ts->trx;
391
392 return abis_rsl_sendmsg(msg);
393}
394
395/* determine power control level for given dBm value, as indicated
396 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
397static int ms_pwr_ctl_lvl(struct gsm_bts *bts, unsigned int dbm)
398{
399 switch (bts->band) {
400 case GSM_BAND_400:
401 case GSM_BAND_900:
402 case GSM_BAND_850:
403 if (dbm >= 39)
404 return 0;
405 else if (dbm < 5)
406 return 19;
407 else
408 return 2 + ((39 - dbm) / 2);
409 break;
410 case GSM_BAND_1800:
411 if (dbm >= 36)
412 return 29;
413 else if (dbm >= 34)
414 return 30;
415 else if (dbm >= 32)
416 return 31;
417 else
418 return (30 - dbm) / 2;
419 break;
420 case GSM_BAND_1900:
421 if (dbm >= 33)
422 return 30;
423 else if (dbm >= 32)
424 return 31;
425 else
426 return (30 - dbm) / 2;
427 break;
428 }
429 return -EINVAL;
430}
431
Harald Weltea1467eb2009-06-20 18:44:35 +0200432static int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl)
433{
434 lvl &= 0x1f;
435
436 switch (band) {
437 case GSM_BAND_400:
438 case GSM_BAND_900:
439 case GSM_BAND_850:
440 if (lvl < 2)
441 return 39;
442 else if (lvl < 20)
443 return 39 - ((lvl - 2) * 2) ;
444 else
445 return 5;
446 break;
447 case GSM_BAND_1800:
448 if (lvl < 16)
449 return 30 - (lvl * 2);
450 else if (lvl < 29)
451 return 0;
452 else
453 return 36 - ((lvl - 29) * 2);
454 break;
455 case GSM_BAND_1900:
456 if (lvl < 16)
457 return 30 - (lvl * 2);
458 else if (lvl < 30)
459 return -EINVAL;
460 else
461 return 33 - (lvl - 30);
462 break;
463 }
464 return -EINVAL;
465}
466
Harald Welte91afe4c2009-06-20 18:15:19 +0200467int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan, unsigned int fpc, int dbm)
468{
469 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200470 struct msgb *msg;
Harald Welte91afe4c2009-06-20 18:15:19 +0200471 u_int8_t chan_nr = lchan2chan_nr(lchan);
472 int ctl_lvl;
473
474 ctl_lvl = ms_pwr_ctl_lvl(lchan->ts->trx->bts, dbm);
475 if (ctl_lvl < 0)
476 return ctl_lvl;
477
Harald Welteed831842009-06-27 03:09:08 +0200478 msg = rsl_msgb_alloc();
479
Harald Welte91afe4c2009-06-20 18:15:19 +0200480 lchan->ms_power = ctl_lvl;
481
482 if (fpc)
483 lchan->ms_power |= 0x20;
484
485 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
486 init_dchan_hdr(dh, RSL_MT_MS_POWER_CONTROL);
487 dh->chan_nr = chan_nr;
488
489 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
490
491 msg->trx = lchan->ts->trx;
492
493 return abis_rsl_sendmsg(msg);
494}
495
Harald Welte59b04682009-06-10 05:40:52 +0800496/* Chapter 8.4.1 */
497#if 0
498int rsl_chan_activate(struct gsm_bts_trx *trx, u_int8_t chan_nr,
499 u_int8_t act_type,
500 struct rsl_ie_chan_mode *chan_mode,
501 struct rsl_ie_chan_ident *chan_ident,
502 u_int8_t bs_power, u_int8_t ms_power,
503 u_int8_t ta)
504{
505 struct abis_rsl_dchan_hdr *dh;
506 struct msgb *msg = rsl_msgb_alloc();
507
508 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
509 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
510 dh->chan_nr = chan_nr;
511
512 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
513 /* For compatibility with Phase 1 */
514 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(*chan_mode),
515 (u_int8_t *) chan_mode);
516 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
517 (u_int8_t *) chan_ident);
518#if 0
519 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
520 (u_int8_t *) &encr_info);
521#endif
522 msgb_tv_put(msg, RSL_IE_BS_POWER, bs_power);
523 msgb_tv_put(msg, RSL_IE_MS_POWER, ms_power);
524 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
525
526 msg->trx = trx;
527
528 return abis_rsl_sendmsg(msg);
529}
530#endif
531
532int rsl_chan_activate_lchan(struct gsm_lchan *lchan, u_int8_t act_type,
533 u_int8_t ta, u_int8_t mode)
534{
535 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200536 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800537
538 u_int8_t chan_nr = lchan2chan_nr(lchan);
539 u_int16_t arfcn = lchan->ts->trx->arfcn;
540 struct rsl_ie_chan_mode cm;
541 struct rsl_ie_chan_ident ci;
542
543 memset(&cm, 0, sizeof(cm));
544 /* FIXME: what to do with data calls ? */
545 cm.dtx_dtu = 0x00;
546 switch (lchan->type) {
547 case GSM_LCHAN_SDCCH:
548 cm.spd_ind = RSL_CMOD_SPD_SIGN;
549 cm.chan_rt = RSL_CMOD_CRT_SDCCH;
550 cm.chan_rate = 0x00;
551 break;
552 case GSM_LCHAN_TCH_F:
553 cm.chan_rt = RSL_CMOD_CRT_TCH_Bm;
554 switch (mode) {
555 case RSL_CMOD_SPD_SIGN:
556 cm.spd_ind = RSL_CMOD_SPD_SIGN;
557 cm.chan_rate = 0x00;
558 break;
559 case RSL_CMOD_SPD_SPEECH:
560 cm.spd_ind = RSL_CMOD_SPD_SPEECH;
561 cm.chan_rate = RSL_CMOD_SP_GSM2;
562 break;
563 }
564 break;
565 case GSM_LCHAN_TCH_H:
566 DEBUGP(DRSL, "Unimplemented TCH_H activation\n");
567 return -1;
568 case GSM_LCHAN_UNKNOWN:
569 case GSM_LCHAN_NONE:
570 return -1;
571 }
572
573 memset(&ci, 0, sizeof(ci));
574 ci.chan_desc.iei = 0x64;
575 ci.chan_desc.chan_nr = chan_nr;
576 ci.chan_desc.oct3 = (lchan->ts->trx->bts->tsc << 5) | ((arfcn & 0x3ff) >> 8);
577 ci.chan_desc.oct4 = arfcn & 0xff;
578
Harald Welteed831842009-06-27 03:09:08 +0200579 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800580 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
581 init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
582 dh->chan_nr = chan_nr;
583
584 msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
585 /* For compatibility with Phase 1 */
586 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
587 (u_int8_t *) &cm);
588 msgb_tlv_put(msg, RSL_IE_CHAN_IDENT, 4,
589 (u_int8_t *) &ci);
590#if 0
591 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
592 (u_int8_t *) &encr_info);
593#endif
594 msgb_tv_put(msg, RSL_IE_BS_POWER, lchan->bs_power);
595 msgb_tv_put(msg, RSL_IE_MS_POWER, lchan->ms_power);
596 msgb_tv_put(msg, RSL_IE_TIMING_ADVANCE, ta);
597
598 msg->trx = lchan->ts->trx;
599
600 return abis_rsl_sendmsg(msg);
601}
602
603/* Chapter 8.4.9 */
604int rsl_chan_mode_modify_req(struct gsm_lchan *lchan)
605{
606 struct abis_rsl_dchan_hdr *dh;
Harald Welteed831842009-06-27 03:09:08 +0200607 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800608
609 u_int8_t chan_nr = lchan2chan_nr(lchan);
610 struct rsl_ie_chan_mode cm;
611
612 memset(&cm, 0, sizeof(cm));
613
614 /* FIXME: what to do with data calls ? */
615 cm.dtx_dtu = 0x00;
616 switch (lchan->type) {
617 /* todo more modes */
618 case GSM_LCHAN_TCH_F:
619 cm.spd_ind = RSL_CMOD_SPD_SPEECH;
620 cm.chan_rt = RSL_CMOD_CRT_TCH_Bm;
621 switch(lchan->tch_mode) {
622 case GSM48_CMODE_SPEECH_V1:
623 cm.chan_rate = RSL_CMOD_SP_GSM1;
624 break;
625 case GSM48_CMODE_SPEECH_EFR:
626 cm.chan_rate = RSL_CMOD_SP_GSM2;
627 break;
628 default:
629 DEBUGP(DRSL, "Unimplemented channel modification\n");
630 return -1;
631 }
632 break;
633 default:
634 DEBUGP(DRSL, "Unimplemented channel modification\n");
635 return -1;
636 }
637
Harald Welteed831842009-06-27 03:09:08 +0200638 msg = rsl_msgb_alloc();
Harald Welte59b04682009-06-10 05:40:52 +0800639 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
640 init_dchan_hdr(dh, RSL_MT_MODE_MODIFY_REQ);
641 dh->chan_nr = chan_nr;
642
643 msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
644 (u_int8_t *) &cm);
645#if 0
646 msgb_tlv_put(msg, RSL_IE_ENCR_INFO, 1,
647 (u_int8_t *) &encr_info);
648#endif
649
650 msg->trx = lchan->ts->trx;
651
652 return abis_rsl_sendmsg(msg);
653}
654
655/* Chapter 9.1.7 of 04.08 */
656int rsl_chan_release(struct gsm_lchan *lchan)
657{
658 struct abis_rsl_dchan_hdr *dh;
659 struct msgb *msg = rsl_msgb_alloc();
660
661 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
662 init_dchan_hdr(dh, RSL_MT_RF_CHAN_REL);
663 dh->chan_nr = lchan2chan_nr(lchan);
664
665 msg->lchan = lchan;
666 msg->trx = lchan->ts->trx;
667
668 DEBUGP(DRSL, "Channel Release CMD channel=%s chan_nr=0x%02x\n",
669 gsm_ts_name(lchan->ts), dh->chan_nr);
670
671 return abis_rsl_sendmsg(msg);
672}
673
674int rsl_paging_cmd(struct gsm_bts *bts, u_int8_t paging_group, u_int8_t len,
675 u_int8_t *ms_ident, u_int8_t chan_needed)
676{
677 struct abis_rsl_dchan_hdr *dh;
678 struct msgb *msg = rsl_msgb_alloc();
679
680 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
681 init_dchan_hdr(dh, RSL_MT_PAGING_CMD);
682 dh->chan_nr = RSL_CHAN_PCH_AGCH;
683
684 msgb_tv_put(msg, RSL_IE_PAGING_GROUP, paging_group);
685 msgb_tlv_put(msg, RSL_IE_MS_IDENTITY, len-2, ms_ident+2);
686 msgb_tv_put(msg, RSL_IE_CHAN_NEEDED, chan_needed);
687
688 msg->trx = bts->c0;
689
690 return abis_rsl_sendmsg(msg);
691}
692
693int rsl_paging_cmd_subscr(struct gsm_bts *bts, u_int8_t chan_need,
694 struct gsm_subscriber *subscr)
695{
696#if 0
697 u_int8_t mi[128];
698 unsigned int mi_len;
699 u_int8_t paging_group;
700#endif
701
702 return -1;
703}
704
705int imsi_str2bcd(u_int8_t *bcd_out, const char *str_in)
706{
707 int i, len = strlen(str_in);
708
709 for (i = 0; i < len; i++) {
710 int num = str_in[i] - 0x30;
711 if (num < 0 || num > 9)
712 return -1;
713 if (i % 2 == 0)
714 bcd_out[i/2] = num;
715 else
716 bcd_out[i/2] |= (num << 4);
717 }
718
719 return 0;
720}
721
722/* Chapter 8.5.6 */
723int rsl_imm_assign_cmd(struct gsm_bts *bts, u_int8_t len, u_int8_t *val)
724{
725 struct msgb *msg = rsl_msgb_alloc();
726 struct abis_rsl_dchan_hdr *dh;
727 u_int8_t buf[MACBLOCK_SIZE];
728
729 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
730 init_dchan_hdr(dh, RSL_MT_IMMEDIATE_ASSIGN_CMD);
731 dh->chan_nr = RSL_CHAN_PCH_AGCH;
732
733 switch (bts->type) {
734 case GSM_BTS_TYPE_BS11:
735 msgb_tlv_put(msg, RSL_IE_IMM_ASS_INFO, len, val);
736 break;
737 default:
738 /* If phase 2, construct a FULL_IMM_ASS_INFO */
739 pad_macblock(buf, val, len);
740 msgb_tlv_put(msg, RSL_IE_FULL_IMM_ASS_INFO, MACBLOCK_SIZE, buf);
741 break;
742 }
743
744 msg->trx = bts->c0;
745
746 return abis_rsl_sendmsg(msg);
747}
748
749/* Send "DATA REQUEST" message with given L3 Info payload */
750/* Chapter 8.3.1 */
751int rsl_data_request(struct msgb *msg, u_int8_t link_id)
752{
753 u_int8_t l3_len = msg->tail - (u_int8_t *)msgb_l3(msg);
754 struct abis_rsl_rll_hdr *rh;
755
756 if (msg->lchan == NULL) {
757 fprintf(stderr, "cannot send DATA REQUEST to unknown lchan\n");
758 return -EINVAL;
759 }
760
761 /* First push the L3 IE tag and length */
762 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
763
764 /* Then push the RSL header */
765 rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
766 init_llm_hdr(rh, RSL_MT_DATA_REQ);
767 rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
768 rh->chan_nr = lchan2chan_nr(msg->lchan);
769 rh->link_id = link_id;
770
771 msg->trx = msg->lchan->ts->trx;
772
773 return abis_rsl_sendmsg(msg);
774}
775
776/* Chapter 8.4.2: Channel Activate Acknowledge */
777static int rsl_rx_chan_act_ack(struct msgb *msg)
778{
779 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
780
781 /* BTS has confirmed channel activation, we now need
782 * to assign the activated channel to the MS */
783 if (rslh->ie_chan != RSL_IE_CHAN_NR)
784 return -EINVAL;
785
786 return 0;
787}
788
789/* Chapter 8.4.3: Channel Activate NACK */
790static int rsl_rx_chan_act_nack(struct msgb *msg)
791{
792 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
793 struct tlv_parsed tp;
794
795 /* BTS has rejected channel activation ?!? */
796 if (dh->ie_chan != RSL_IE_CHAN_NR)
797 return -EINVAL;
798
799 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
800 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE))
801 DEBUGPC(DRSL, "CAUSE=0x%02x ", *TLVP_VAL(&tp, RSL_IE_CAUSE));
802
803 return 0;
804}
805
806/* Chapter 8.4.4: Connection Failure Indication */
807static int rsl_rx_conn_fail(struct msgb *msg)
808{
809 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
810 struct tlv_parsed tp;
811
812 DEBUGPC(DRSL, "CONNECTION FAIL: ");
813 print_rsl_cause(dh->data);
814
815 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
816
817 if (msg->trx->bts->type == GSM_BTS_TYPE_BS11) {
818 /* FIXME: we have no idea what cause 0x18 is !!! */
819 if (TLVP_PRESENT(&tp, RSL_IE_CAUSE) &&
820 TLVP_LEN(&tp, RSL_IE_CAUSE) >= 1 &&
821 *TLVP_VAL(&tp, RSL_IE_CAUSE) == 0x18) {
Harald Weltedf0c6502009-07-04 10:05:51 +0200822 DEBUGPC(DRSL, "Cause 0x18 IGNORING\n");
823 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800824 }
825 }
826
827 DEBUGPC(DRSL, "RELEASING.\n");
828
829 /* FIXME: only free it after channel release ACK */
830 return rsl_chan_release(msg->lchan);
831}
832
833static int rsl_rx_meas_res(struct msgb *msg)
834{
835 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
836 struct tlv_parsed tp;
837
Harald Welte02993682009-06-27 02:53:10 +0200838 DEBUGPC(DMEAS, "MEASUREMENT RESULT ");
Harald Welte59b04682009-06-10 05:40:52 +0800839 rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
840
841 if (TLVP_PRESENT(&tp, RSL_IE_MEAS_RES_NR))
Harald Welte02993682009-06-27 02:53:10 +0200842 DEBUGPC(DMEAS, "NR=%d ", *TLVP_VAL(&tp, RSL_IE_MEAS_RES_NR));
Harald Welte59b04682009-06-10 05:40:52 +0800843 if (TLVP_PRESENT(&tp, RSL_IE_UPLINK_MEAS)) {
844 u_int8_t len = TLVP_LEN(&tp, RSL_IE_UPLINK_MEAS);
845 const u_int8_t *val = TLVP_VAL(&tp, RSL_IE_UPLINK_MEAS);
846 if (len >= 3) {
847 if (val[0] & 0x40)
Harald Welte02993682009-06-27 02:53:10 +0200848 DEBUGPC(DMEAS, "DTXd ");
849 DEBUGPC(DMEAS, "RXL-FULL-up=%d RXL-SUB-up=%d ",
Harald Welte59b04682009-06-10 05:40:52 +0800850 val[0] & 0x3f, val[1] & 0x3f);
Harald Welte02993682009-06-27 02:53:10 +0200851 DEBUGPC(DMEAS, "RXQ-FULL-up=%d RXQ-SUB-up=%d ",
Harald Welte59b04682009-06-10 05:40:52 +0800852 val[2]>>3 & 0x7, val[2] & 0x7);
853 }
854 }
855 if (TLVP_PRESENT(&tp, RSL_IE_BS_POWER))
Harald Welte02993682009-06-27 02:53:10 +0200856 DEBUGPC(DMEAS, "BS_POWER=%d ", *TLVP_VAL(&tp, RSL_IE_BS_POWER));
Harald Welte59b04682009-06-10 05:40:52 +0800857 if (TLVP_PRESENT(&tp, RSL_IE_MS_TIMING_OFFSET))
Harald Welte02993682009-06-27 02:53:10 +0200858 DEBUGPC(DMEAS, "MS_TO=%d ",
Harald Welte59b04682009-06-10 05:40:52 +0800859 *TLVP_VAL(&tp, RSL_IE_MS_TIMING_OFFSET));
Harald Weltea1467eb2009-06-20 18:44:35 +0200860 if (TLVP_PRESENT(&tp, RSL_IE_L1_INFO)) {
Harald Welteb9498952009-07-12 09:45:05 +0200861 const u_int8_t *val = TLVP_VAL(&tp, RSL_IE_L1_INFO);
Harald Weltea1467eb2009-06-20 18:44:35 +0200862 u_int8_t pwr_lvl = val[0] >> 3;
Harald Welte02993682009-06-27 02:53:10 +0200863 DEBUGPC(DMEAS, "L1_MS_PWR=%ddBm ",
Harald Weltea1467eb2009-06-20 18:44:35 +0200864 ms_pwr_dbm(msg->trx->bts->band, pwr_lvl));
Harald Welte02993682009-06-27 02:53:10 +0200865 DEBUGPC(DMEAS, "L1_FPC=%u ", val[0] & 0x04 ? 1 : 0);
866 DEBUGPC(DMEAS, "L1_TA=%u ", val[1]);
Harald Weltea1467eb2009-06-20 18:44:35 +0200867 }
Harald Welte59b04682009-06-10 05:40:52 +0800868 if (TLVP_PRESENT(&tp, RSL_IE_L3_INFO)) {
Harald Welte02993682009-06-27 02:53:10 +0200869 DEBUGPC(DMEAS, "L3\n");
Harald Welte59b04682009-06-10 05:40:52 +0800870 msg->l3h = TLVP_VAL(&tp, RSL_IE_L3_INFO);
871 return gsm0408_rcvmsg(msg);
872 } else
Harald Welte02993682009-06-27 02:53:10 +0200873 DEBUGPC(DMEAS, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800874
875 return 0;
876}
877
878static int abis_rsl_rx_dchan(struct msgb *msg)
879{
880 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
881 int rc = 0;
882 char *ts_name;
883
884 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
885 ts_name = gsm_ts_name(msg->lchan->ts);
886
Harald Welte02993682009-06-27 02:53:10 +0200887 if (rslh->c.msg_type != RSL_MT_MEAS_RES)
888 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ", ts_name, rslh->chan_nr);
Harald Welte59b04682009-06-10 05:40:52 +0800889
890 switch (rslh->c.msg_type) {
891 case RSL_MT_CHAN_ACTIV_ACK:
892 DEBUGPC(DRSL, "CHANNEL ACTIVATE ACK\n");
893 rc = rsl_rx_chan_act_ack(msg);
894 break;
895 case RSL_MT_CHAN_ACTIV_NACK:
896 DEBUGPC(DRSL, "CHANNEL ACTIVATE NACK\n");
897 rc = rsl_rx_chan_act_nack(msg);
898 break;
899 case RSL_MT_CONN_FAIL:
900 rc = rsl_rx_conn_fail(msg);
901 break;
902 case RSL_MT_MEAS_RES:
903 rc = rsl_rx_meas_res(msg);
904 break;
905 case RSL_MT_RF_CHAN_REL_ACK:
906 DEBUGPC(DRSL, "RF CHANNEL RELEASE ACK\n");
907 lchan_free(msg->lchan);
908 break;
909 case RSL_MT_MODE_MODIFY_ACK:
910 DEBUGPC(DRSL, "CHANNEL MODE MODIFY ACK\n");
911 break;
912 case RSL_MT_MODE_MODIFY_NACK:
913 DEBUGPC(DRSL, "CHANNEL MODE MODIFY NACK\n");
914 break;
915 case RSL_MT_PHY_CONTEXT_CONF:
916 case RSL_MT_PREPROC_MEAS_RES:
917 case RSL_MT_TALKER_DET:
918 case RSL_MT_LISTENER_DET:
919 case RSL_MT_REMOTE_CODEC_CONF_REP:
920 case RSL_MT_MR_CODEC_MOD_ACK:
921 case RSL_MT_MR_CODEC_MOD_NACK:
922 case RSL_MT_MR_CODEC_MOD_PER:
923 DEBUGPC(DRSL, "Unimplemented Abis RSL DChan msg 0x%02x\n",
924 rslh->c.msg_type);
925 break;
926 default:
927 DEBUGPC(DRSL, "unknown Abis RSL DChan msg 0x%02x\n",
928 rslh->c.msg_type);
929 return -EINVAL;
930 }
931
932 return rc;
933}
934
935static int rsl_rx_error_rep(struct msgb *msg)
936{
937 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
938
939 DEBUGP(DRSL, "ERROR REPORT ");
940 print_rsl_cause(rslh->data);
941 DEBUGPC(DRSL, "\n");
942
943 return 0;
944}
945
946static int abis_rsl_rx_trx(struct msgb *msg)
947{
948 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
949 int rc = 0;
950
951 switch (rslh->msg_type) {
952 case RSL_MT_ERROR_REPORT:
953 rc = rsl_rx_error_rep(msg);
954 break;
955 case RSL_MT_RF_RES_IND:
956 /* interference on idle channels of TRX */
957 //DEBUGP(DRSL, "TRX: RF Interference Indication\n");
958 break;
959 case RSL_MT_OVERLOAD:
960 /* indicate CCCH / ACCH / processor overload */
961 DEBUGP(DRSL, "TRX: CCCH/ACCH/CPU Overload\n");
962 break;
963 default:
964 DEBUGP(DRSL, "Unknown Abis RSL TRX message type 0x%02x\n",
965 rslh->msg_type);
966 return -EINVAL;
967 }
968 return rc;
969}
970
971/* MS has requested a channel on the RACH */
972static int rsl_rx_chan_rqd(struct msgb *msg)
973{
974 struct gsm_bts *bts = msg->trx->bts;
975 struct abis_rsl_dchan_hdr *rqd_hdr = msgb_l2(msg);
976 struct gsm48_req_ref *rqd_ref;
977 struct gsm48_imm_ass ia;
978 enum gsm_chan_t lctype;
979 enum gsm_chreq_reason_t chreq_reason;
980 struct gsm_lchan *lchan;
981 u_int8_t rqd_ta;
982 int ret;
983
984 u_int16_t arfcn;
985 u_int8_t ts_number, subch;
986
987 /* parse request reference to be used in immediate assign */
988 if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
989 return -EINVAL;
990
991 rqd_ref = (struct gsm48_req_ref *) &rqd_hdr->data[1];
992
993 /* parse access delay and use as TA */
994 if (rqd_hdr->data[sizeof(struct gsm48_req_ref)+1] != RSL_IE_ACCESS_DELAY)
995 return -EINVAL;
996 rqd_ta = rqd_hdr->data[sizeof(struct gsm48_req_ref)+2];
997
998 /* determine channel type (SDCCH/TCH_F/TCH_H) based on
999 * request reference RA */
1000 lctype = get_ctype_by_chreq(bts, rqd_ref->ra);
1001 chreq_reason = get_reason_by_chreq(bts, rqd_ref->ra);
1002
1003 /* check availability / allocate channel */
1004 lchan = lchan_alloc(bts, lctype);
1005 if (!lchan) {
1006 fprintf(stderr, "CHAN RQD: no resources\n");
1007 /* FIXME: send some kind of reject ?!? */
1008 return -ENOMEM;
1009 }
1010
1011 ts_number = lchan->ts->nr;
1012 arfcn = lchan->ts->trx->arfcn;
1013 subch = lchan->nr;
1014
1015 lchan->ms_power = lchan->bs_power = 0x0f; /* 30dB reduction */
1016 rsl_chan_activate_lchan(lchan, 0x00, rqd_ta, RSL_CMOD_SPD_SIGN);
1017
1018 /* create IMMEDIATE ASSIGN 04.08 messge */
1019 memset(&ia, 0, sizeof(ia));
1020 ia.l2_plen = 0x2d;
1021 ia.proto_discr = GSM48_PDISC_RR;
1022 ia.msg_type = GSM48_MT_RR_IMM_ASS;
1023 ia.page_mode = GSM48_PM_SAME;
1024 ia.chan_desc.chan_nr = lchan2chan_nr(lchan);
1025 ia.chan_desc.h0.h = 0;
1026 ia.chan_desc.h0.arfcn_high = arfcn >> 8;
1027 ia.chan_desc.h0.arfcn_low = arfcn & 0xff;
1028 ia.chan_desc.h0.tsc = 7;
1029 /* use request reference extracted from CHAN_RQD */
1030 memcpy(&ia.req_ref, rqd_ref, sizeof(ia.req_ref));
1031 ia.timing_advance = rqd_ta;
1032 ia.mob_alloc_len = 0;
1033
1034 DEBUGP(DRSL, "Activating ARFCN(%u) TS(%u) SS(%u) lctype %s "
1035 "chan_nr=0x%02x r=%s ra=0x%02x\n",
1036 arfcn, ts_number, subch, gsm_lchan_name(lchan->type),
1037 ia.chan_desc.chan_nr, gsm_chreq_name(chreq_reason),
1038 rqd_ref->ra);
1039
1040 /* FIXME: Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
1041
1042 /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
1043 ret = rsl_imm_assign_cmd(bts, sizeof(ia), (u_int8_t *) &ia);
1044
1045 return ret;
1046}
1047
1048/* MS has requested a channel on the RACH */
1049static int rsl_rx_ccch_load(struct msgb *msg)
1050{
1051 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1052 u_int16_t pg_buf_space;
1053 u_int16_t rach_slot_count = -1;
1054 u_int16_t rach_busy_count = -1;
1055 u_int16_t rach_access_count = -1;
1056
1057 switch (rslh->data[0]) {
1058 case RSL_IE_PAGING_LOAD:
1059 pg_buf_space = rslh->data[1] << 8 | rslh->data[2];
1060 paging_update_buffer_space(msg->trx->bts, pg_buf_space);
1061 break;
1062 case RSL_IE_RACH_LOAD:
1063 if (msg->data_len >= 7) {
1064 rach_slot_count = rslh->data[2] << 8 | rslh->data[3];
1065 rach_busy_count = rslh->data[4] << 8 | rslh->data[5];
1066 rach_access_count = rslh->data[6] << 8 | rslh->data[7];
1067 }
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 return 0;
1074}
1075
1076static int abis_rsl_rx_cchan(struct msgb *msg)
1077{
1078 struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
1079 int rc = 0;
1080
1081 msg->lchan = lchan_lookup(msg->trx, rslh->chan_nr);
1082
1083 switch (rslh->c.msg_type) {
1084 case RSL_MT_CHAN_RQD:
1085 /* MS has requested a channel on the RACH */
1086 rc = rsl_rx_chan_rqd(msg);
1087 break;
1088 case RSL_MT_CCCH_LOAD_IND:
1089 /* current load on the CCCH */
1090 rc = rsl_rx_ccch_load(msg);
1091 break;
1092 case RSL_MT_DELETE_IND:
1093 /* CCCH overloaded, IMM_ASSIGN was dropped */
1094 case RSL_MT_CBCH_LOAD_IND:
1095 /* current load on the CBCH */
1096 fprintf(stderr, "Unimplemented Abis RSL TRX message type "
1097 "0x%02x\n", rslh->c.msg_type);
1098 break;
1099 default:
1100 fprintf(stderr, "Unknown Abis RSL TRX message type 0x%02x\n",
1101 rslh->c.msg_type);
1102 return -EINVAL;
1103 }
1104
1105 return rc;
1106}
1107
1108static int rsl_rx_rll_err_ind(struct msgb *msg)
1109{
1110 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1111 u_int8_t *rlm_cause = rllh->data;
1112
1113 DEBUGPC(DRLL, "cause=0x%02x", rlm_cause[1]);
1114
Harald Welte692f5852009-07-04 09:40:05 +02001115 if (rlm_cause[1] == RLL_CAUSE_T200_EXPIRED)
1116 return rsl_chan_release(msg->lchan);
1117
Harald Welte59b04682009-06-10 05:40:52 +08001118 return 0;
1119}
1120
1121/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
1122 0x02, 0x06,
1123 0x01, 0x20,
1124 0x02, 0x00,
1125 0x0b, 0x00, 0x0f, 0x05, 0x08, ... */
1126
1127static int abis_rsl_rx_rll(struct msgb *msg)
1128{
1129 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1130 int rc = 0;
1131 char *ts_name;
1132
1133 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
1134 ts_name = gsm_ts_name(msg->lchan->ts);
1135 DEBUGP(DRLL, "channel=%s chan_nr=0x%02x ", ts_name, rllh->chan_nr);
1136
1137 switch (rllh->c.msg_type) {
1138 case RSL_MT_DATA_IND:
1139 DEBUGPC(DRLL, "DATA INDICATION\n");
1140 if (msgb_l2len(msg) >
1141 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1142 rllh->data[0] == RSL_IE_L3_INFO) {
1143 msg->l3h = &rllh->data[3];
1144 return gsm0408_rcvmsg(msg);
1145 }
1146 break;
1147 case RSL_MT_EST_IND:
1148 DEBUGPC(DRLL, "ESTABLISH INDICATION\n");
1149 if (msgb_l2len(msg) >
1150 sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
1151 rllh->data[0] == RSL_IE_L3_INFO) {
1152 msg->l3h = &rllh->data[3];
1153 return gsm0408_rcvmsg(msg);
1154 }
1155 break;
1156 case RSL_MT_REL_IND:
1157 DEBUGPC(DRLL, "RELEASE INDICATION ");
1158 break;
1159 case RSL_MT_REL_CONF:
1160 DEBUGPC(DRLL, "RELEASE CONFIRMATION ");
1161 break;
1162 case RSL_MT_ERROR_IND:
1163 DEBUGPC(DRLL, "ERROR INDICATION ");
1164 rc = rsl_rx_rll_err_ind(msg);
1165 break;
1166 case RSL_MT_UNIT_DATA_IND:
1167 DEBUGPC(DRLL, "unimplemented Abis RLL message type 0x%02x ",
1168 rllh->c.msg_type);
1169 break;
1170 default:
1171 DEBUGPC(DRLL, "unknown Abis RLL message type 0x%02x ",
1172 rllh->c.msg_type);
1173 }
1174 DEBUGPC(DRLL, "\n");
1175 return rc;
1176}
1177
1178/* ip.access specific RSL extensions */
1179int rsl_ipacc_bind(struct gsm_lchan *lchan)
1180{
1181 struct msgb *msg = rsl_msgb_alloc();
1182 struct abis_rsl_dchan_hdr *dh;
1183
1184 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1185 init_dchan_hdr(dh, RSL_MT_IPAC_BIND);
1186 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1187 dh->chan_nr = lchan2chan_nr(lchan);
1188
1189 msg->trx = lchan->ts->trx;
1190
1191 return abis_rsl_sendmsg(msg);
1192}
1193
Harald Welte8cdeaad2009-07-12 09:50:35 +02001194int rsl_ipacc_connect(struct gsm_lchan *lchan, u_int32_t ip, u_int16_t port,
1195 u_int16_t conn_id, u_int8_t rtp_payload2)
Harald Welte59b04682009-06-10 05:40:52 +08001196{
1197 struct msgb *msg = rsl_msgb_alloc();
1198 struct abis_rsl_dchan_hdr *dh;
1199 u_int8_t *att_f8, *att_ip, *att_port;
1200
1201 dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
1202 init_dchan_hdr(dh, RSL_MT_IPAC_CONNECT);
1203 dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
1204 dh->chan_nr = lchan2chan_nr(lchan);
1205
Harald Welte8cdeaad2009-07-12 09:50:35 +02001206 att_f8 = msgb_put(msg, sizeof(conn_id)+1);
Harald Welteb9498952009-07-12 09:45:05 +02001207 att_f8[0] = RSL_IE_IPAC_CONN_ID;
Harald Welte8cdeaad2009-07-12 09:50:35 +02001208 att_f8[1] = conn_id >> 8;
1209 att_f8[2] = conn_id & 0xff;
Harald Welte59b04682009-06-10 05:40:52 +08001210
1211 att_ip = msgb_put(msg, sizeof(ip)+1);
1212 att_ip[0] = RSL_IE_IPAC_REMOTE_IP;
1213 att_ip[1] = ip >> 24;
1214 att_ip[2] = ip >> 16;
1215 att_ip[3] = ip >> 8;
1216 att_ip[4] = ip & 0xff;
1217 //att_ip[4] = 11;
1218
1219 att_port = msgb_put(msg, sizeof(port)+1);
1220 att_port[0] = RSL_IE_IPAC_REMOTE_PORT;
1221 att_port[1] = port >> 8;
1222 att_port[2] = port & 0xff;
1223
Harald Welteb9498952009-07-12 09:45:05 +02001224 msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, 1); /* F4 01 */
Harald Welte8cdeaad2009-07-12 09:50:35 +02001225 msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2); /* FC 7F */
Harald Welte59b04682009-06-10 05:40:52 +08001226 msg->trx = lchan->ts->trx;
1227
1228 return abis_rsl_sendmsg(msg);
1229}
1230
1231static int abis_rsl_rx_ipacc_bindack(struct msgb *msg)
1232{
1233 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1234 struct tlv_parsed tv;
1235 struct gsm_bts_trx_ts *ts = msg->lchan->ts;
1236 struct in_addr ip;
1237 u_int16_t port, attr_f8;
1238
1239 /* the BTS has acknowledged a local bind, it now tells us the IP
1240 * address and port number to which it has bound the given logical
1241 * channel */
1242
1243 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1244 if (!TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_PORT) ||
1245 !TLVP_PRESENT(&tv, RSL_IE_IPAC_LOCAL_IP) ||
Harald Welteb9498952009-07-12 09:45:05 +02001246 !TLVP_PRESENT(&tv, RSL_IE_IPAC_RTP_PAYLOAD2) ||
1247 !TLVP_PRESENT(&tv, RSL_IE_IPAC_CONN_ID)) {
Harald Welte59b04682009-06-10 05:40:52 +08001248 DEBUGPC(DRSL, "mandatory IE missing");
1249 return -EINVAL;
1250 }
1251 ip.s_addr = *((u_int32_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_IP));
1252 port = *((u_int16_t *) TLVP_VAL(&tv, RSL_IE_IPAC_LOCAL_PORT));
1253 attr_f8 = *((u_int16_t *) TLVP_VAL(&tv, 0xf8));
1254
Harald Welteb9498952009-07-12 09:45:05 +02001255 DEBUGPC(DRSL, "IP=%s PORT=%d RTP_PAYLOAD2=%d CONN_ID=%d",
Harald Welte59b04682009-06-10 05:40:52 +08001256 inet_ntoa(ip), ntohs(port), *TLVP_VAL(&tv, 0xfc),
1257 ntohs(attr_f8));
1258
1259 /* update our local information about this TS */
1260 ts->abis_ip.bound_ip = ntohl(ip.s_addr);
1261 ts->abis_ip.bound_port = ntohs(port);
Harald Welte8cdeaad2009-07-12 09:50:35 +02001262 ts->abis_ip.conn_id = ntohs(attr_f8);
1263 ts->abis_ip.rtp_payload2 = *TLVP_VAL(&tv, RSL_IE_IPAC_RTP_PAYLOAD2);
Harald Welte59b04682009-06-10 05:40:52 +08001264
1265 dispatch_signal(SS_ABISIP, S_ABISIP_BIND_ACK, msg->lchan);
1266
1267 return 0;
1268}
1269
1270static int abis_rsl_rx_ipacc_disc_ind(struct msgb *msg)
1271{
1272 struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
1273 struct tlv_parsed tv;
1274
1275 rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
1276 if (!TLVP_PRESENT(&tv, RSL_IE_CAUSE)) {
1277 DEBUGPC(DRSL, "mandatory IE missing! ");
1278 return -EINVAL;
1279 }
1280
1281 DEBUGPC(DRSL, "cause=0x%02x ", *TLVP_VAL(&tv, RSL_IE_CAUSE));
1282
1283 return 0;
1284}
1285
1286static int abis_rsl_rx_ipacc(struct msgb *msg)
1287{
1288 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1289 int rc = 0;
1290
1291 msg->lchan = lchan_lookup(msg->trx, rllh->chan_nr);
1292 DEBUGP(DRSL, "channel=%s chan_nr=0x%02x ",
1293 gsm_ts_name(msg->lchan->ts), rllh->chan_nr);
1294
1295 switch (rllh->c.msg_type) {
1296 case RSL_MT_IPAC_BIND_ACK:
1297 DEBUGPC(DRSL, "IPAC_BIND_ACK ");
1298 rc = abis_rsl_rx_ipacc_bindack(msg);
1299 break;
1300 case RSL_MT_IPAC_BIND_NACK:
1301 /* somehow the BTS was unable to bind the lchan to its local
1302 * port?!? */
1303 DEBUGPC(DRSL, "IPAC_BIND_NACK ");
1304 break;
1305 case RSL_MT_IPAC_CONNECT_ACK:
1306 /* the BTS tells us that a connect operation was successful */
1307 DEBUGPC(DRSL, "IPAC_CONNECT_ACK ");
1308 break;
1309 case RSL_MT_IPAC_CONNECT_NACK:
1310 /* somehow the BTS was unable to connect the lchan to a remote
1311 * port */
1312 DEBUGPC(DRSL, "IPAC_CONNECT_NACK ");
1313 break;
1314 case RSL_MT_IPAC_DISCONNECT_IND:
1315 DEBUGPC(DRSL, "IPAC_DISCONNECT_IND ");
1316 rc = abis_rsl_rx_ipacc_disc_ind(msg);
1317 break;
1318 default:
1319 DEBUGPC(DRSL, "Unknown ip.access msg_type 0x%02x", rllh->c.msg_type);
1320 break;
1321 }
1322 DEBUGPC(DRSL, "\n");
1323
1324 return rc;
1325}
1326
1327
1328/* Entry-point where L2 RSL from BTS enters */
1329int abis_rsl_rcvmsg(struct msgb *msg)
1330{
1331 struct abis_rsl_common_hdr *rslh = msgb_l2(msg) ;
1332 int rc = 0;
1333
1334 switch (rslh->msg_discr & 0xfe) {
1335 case ABIS_RSL_MDISC_RLL:
1336 rc = abis_rsl_rx_rll(msg);
1337 break;
1338 case ABIS_RSL_MDISC_DED_CHAN:
1339 rc = abis_rsl_rx_dchan(msg);
1340 break;
1341 case ABIS_RSL_MDISC_COM_CHAN:
1342 rc = abis_rsl_rx_cchan(msg);
1343 break;
1344 case ABIS_RSL_MDISC_TRX:
1345 rc = abis_rsl_rx_trx(msg);
1346 break;
1347 case ABIS_RSL_MDISC_LOC:
1348 fprintf(stderr, "unimplemented RSL msg disc 0x%02x\n",
1349 rslh->msg_discr);
1350 break;
1351 case ABIS_RSL_MDISC_IPACCESS:
1352 rc = abis_rsl_rx_ipacc(msg);
1353 break;
1354 default:
1355 fprintf(stderr, "unknown RSL message discriminator 0x%02x\n",
1356 rslh->msg_discr);
1357 return -EINVAL;
1358 }
1359 msgb_free(msg);
1360 return rc;
1361}
1362
1363
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001364/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
Harald Welte59b04682009-06-10 05:40:52 +08001365int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
1366{
1367 switch (ccch_conf) {
1368 case RSL_BCCH_CCCH_CONF_1_NC:
1369 return 1;
1370 case RSL_BCCH_CCCH_CONF_1_C:
1371 return 1;
1372 case RSL_BCCH_CCCH_CONF_2_NC:
1373 return 2;
1374 case RSL_BCCH_CCCH_CONF_3_NC:
1375 return 3;
1376 case RSL_BCCH_CCCH_CONF_4_NC:
1377 return 4;
1378 default:
1379 return -1;
1380 }
1381}
1382
Holger Hans Peter Freyther4e0fdfd2009-07-09 20:43:16 +02001383/* Section 3.3.2.3 TS 05.02 */
Harald Welte59b04682009-06-10 05:40:52 +08001384int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
1385{
1386 switch (ccch_conf) {
1387 case RSL_BCCH_CCCH_CONF_1_NC:
1388 return 0;
1389 case RSL_BCCH_CCCH_CONF_1_C:
1390 return 1;
1391 case RSL_BCCH_CCCH_CONF_2_NC:
1392 return 0;
1393 case RSL_BCCH_CCCH_CONF_3_NC:
1394 return 0;
1395 case RSL_BCCH_CCCH_CONF_4_NC:
1396 return 0;
1397 default:
1398 return -1;
1399 }
1400}
1401
1402/* From Table 10.5.33 of GSM 04.08 */
1403int rsl_number_of_paging_subchannels(struct gsm_bts *bts)
1404{
1405 if (bts->chan_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C) {
1406 return MAX(1, (3 - bts->chan_desc.bs_ag_blks_res))
1407 * (bts->chan_desc.bs_pa_mfrms + 2);
1408 } else {
1409 return (9 - bts->chan_desc.bs_ag_blks_res)
1410 * (bts->chan_desc.bs_pa_mfrms + 2);
1411 }
1412}