blob: 628c9233d6264ce35f492e7699d79dbe9b3f69a2 [file] [log] [blame]
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0
3 * utility functions
4 */
5
6/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26#include <stdio.h>
27#include <stdlib.h>
Holger Hans Peter Freytheradc14782009-08-21 04:57:35 +020028#include <errno.h>
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020029#include <netinet/in.h>
30
Harald Weltedfe6c7d2010-02-20 16:24:02 +010031#include <osmocore/msgb.h>
Harald Welteafedeab2010-03-04 10:55:40 +010032#include <osmocore/gsm48.h>
Holger Hans Peter Freythere2fee352010-12-22 12:32:56 +010033
34#include <openbsc/abis_rsl.h>
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020035#include <openbsc/debug.h>
36#include <openbsc/gsm_04_08.h>
37#include <openbsc/transaction.h>
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +020038#include <openbsc/paging.h>
39#include <openbsc/signal.h>
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020040
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020041/* should ip.access BTS use direct RTP streams between each other (1),
42 * or should OpenBSC always act as RTP relay/proxy in between (0) ? */
43int ipacc_rtp_direct = 1;
44
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080045static int gsm48_sendmsg(struct msgb *msg)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020046{
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080047 if (msg->lchan)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020048 msg->trx = msg->lchan->ts->trx;
49
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020050 msg->l3h = msg->data;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020051 return rsl_data_request(msg, 0);
52}
53
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020054/* Section 9.1.8 / Table 9.9 */
55struct chreq {
56 u_int8_t val;
57 u_int8_t mask;
58 enum chreq_type type;
59};
60
61/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
62static const struct chreq chreq_type_neci1[] = {
63 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
64 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
65 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
66 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
Sylvain Munautcfcf7e82010-12-27 00:59:23 +010067 { 0xe0, 0xe0, CHREQ_T_TCH_F },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020068 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
69 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
70 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
71 { 0x10, 0xf0, CHREQ_T_SDCCH },
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +010072 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI1 },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020073 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
74 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +010075 { 0x67, 0xff, CHREQ_T_LMU },
76 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
77 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
78 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
79 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020080};
81
82/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
83static const struct chreq chreq_type_neci0[] = {
84 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
85 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
86 { 0xe0, 0xe0, CHREQ_T_TCH_F },
87 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
88 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +010089 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI0 },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020090 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
91 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +010092 { 0x67, 0xff, CHREQ_T_LMU },
93 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
94 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
95 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
96 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020097};
98
99static const enum gsm_chan_t ctype_by_chreq[] = {
100 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
101 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
102 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
103 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
104 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
105 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
106 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
107 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
108 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +0100109 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_SDCCH,
110 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_SDCCH,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200111 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
112 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +0100113 [CHREQ_T_LMU] = GSM_LCHAN_SDCCH,
114 [CHREQ_T_RESERVED_SDCCH] = GSM_LCHAN_SDCCH,
115 [CHREQ_T_RESERVED_IGNORE] = GSM_LCHAN_UNKNOWN,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200116};
117
118static const enum gsm_chreq_reason_t reason_by_chreq[] = {
119 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
120 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
121 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
122 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
123 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
124 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
Harald Welte9385c112009-12-12 20:57:52 +0100125 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_CALL,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200126 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
127 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +0100128 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_CHREQ_REASON_PAG,
129 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_CHREQ_REASON_PAG,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200130 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
131 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +0100132 [CHREQ_T_LMU] = GSM_CHREQ_REASON_OTHER,
133 [CHREQ_T_RESERVED_SDCCH] = GSM_CHREQ_REASON_OTHER,
134 [CHREQ_T_RESERVED_IGNORE] = GSM_CHREQ_REASON_OTHER,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200135};
136
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800137/* verify that the two tables match */
138static_assert(sizeof(ctype_by_chreq) ==
139 sizeof(((struct gsm_network *) NULL)->ctype_by_chreq), assert_size);
140
141/*
142 * Update channel types for request based on policy. E.g. in the
143 * case of a TCH/H network/bsc use TCH/H for the emergency calls,
144 * for early assignment assign a SDCCH and some other options.
145 */
146void gsm_net_update_ctype(struct gsm_network *network)
147{
148 /* copy over the data */
149 memcpy(network->ctype_by_chreq, ctype_by_chreq, sizeof(ctype_by_chreq));
150
151 /*
152 * Use TCH/H for emergency calls when this cell allows TCH/H. Maybe it
153 * is better to iterate over the BTS/TRX and check if no TCH/F is available
154 * and then set it to TCH/H.
155 */
156 if (network->neci)
157 network->ctype_by_chreq[CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_H;
Holger Hans Peter Freyther76fc4a32010-09-06 09:41:50 +0800158
159 if (network->pag_any_tch) {
160 if (network->neci) {
161 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_H;
162 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_H;
163 } else {
164 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_F;
165 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_F;
166 }
167 }
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800168}
169
170enum gsm_chan_t get_ctype_by_chreq(struct gsm_network *network, u_int8_t ra)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200171{
172 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100173 int length;
174 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200175
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800176 if (network->neci) {
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100177 chreq = chreq_type_neci1;
178 length = ARRAY_SIZE(chreq_type_neci1);
179 } else {
180 chreq = chreq_type_neci0;
181 length = ARRAY_SIZE(chreq_type_neci0);
182 }
183
184
185 for (i = 0; i < length; i++) {
186 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200187 if ((ra & chr->mask) == chr->val)
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800188 return network->ctype_by_chreq[chr->type];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200189 }
Harald Welte50720e72009-12-24 11:50:20 +0100190 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200191 return GSM_LCHAN_SDCCH;
192}
193
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800194enum gsm_chreq_reason_t get_reason_by_chreq(u_int8_t ra, int neci)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200195{
196 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100197 int length;
198 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200199
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100200 if (neci) {
201 chreq = chreq_type_neci1;
202 length = ARRAY_SIZE(chreq_type_neci1);
203 } else {
204 chreq = chreq_type_neci0;
205 length = ARRAY_SIZE(chreq_type_neci0);
206 }
207
208 for (i = 0; i < length; i++) {
209 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200210 if ((ra & chr->mask) == chr->val)
211 return reason_by_chreq[chr->type];
212 }
Harald Welte50720e72009-12-24 11:50:20 +0100213 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200214 return GSM_CHREQ_REASON_OTHER;
215}
216
217/* 7.1.7 and 9.1.7: RR CHANnel RELease */
218int gsm48_send_rr_release(struct gsm_lchan *lchan)
219{
220 struct msgb *msg = gsm48_msgb_alloc();
221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
222 u_int8_t *cause;
223
224 msg->lchan = lchan;
225 gh->proto_discr = GSM48_PDISC_RR;
226 gh->msg_type = GSM48_MT_RR_CHAN_REL;
227
228 cause = msgb_put(msg, 1);
229 cause[0] = GSM48_RR_CAUSE_NORMAL;
230
231 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
232 lchan->nr, lchan->type);
233
234 /* Send actual release request to MS */
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800235 gsm48_sendmsg(msg);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200236 /* FIXME: Start Timer T3109 */
237
238 /* Deactivate the SACCH on the BTS side */
239 return rsl_deact_sacch(lchan);
240}
241
Holger Hans Peter Freytheradc14782009-08-21 04:57:35 +0200242int send_siemens_mrpci(struct gsm_lchan *lchan,
243 u_int8_t *classmark2_lv)
244{
245 struct rsl_mrpci mrpci;
246
247 if (classmark2_lv[0] < 2)
248 return -EINVAL;
249
250 mrpci.power_class = classmark2_lv[1] & 0x7;
251 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
252 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
253 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
254
255 return rsl_siemens_mrpci(lchan, &mrpci);
256}
257
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800258int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200259{
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800260 /* Check the size for the classmark */
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800261 if (length < 1 + *classmark2_lv)
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800262 return -1;
263
264 u_int8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800265 if (length < 2 + *classmark2_lv + mi_lv[0])
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800266 return -2;
267
268 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200269 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
270}
271
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800272int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
273 char *mi_string, u_int8_t *mi_type)
274{
275 static const uint32_t classmark_offset =
276 offsetof(struct gsm48_pag_resp, classmark2);
277 u_int8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
278 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
279 mi_string, mi_type);
280}
281
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800282int gsm48_handle_paging_resp(struct gsm_subscriber_connection *conn,
283 struct msgb *msg, struct gsm_subscriber *subscr)
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200284{
285 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
286 struct gsm48_hdr *gh = msgb_l3(msg);
287 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200288
289 if (is_siemens_bts(bts))
290 send_siemens_mrpci(msg->lchan, classmark2_lv);
291
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800292 if (!conn->subscr) {
293 conn->subscr = subscr;
294 } else if (conn->subscr != subscr) {
Harald Welte50720e72009-12-24 11:50:20 +0100295 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200296 subscr_put(subscr);
297 return -EINVAL;
298 } else {
299 DEBUGP(DRR, "<- Channel already owned by us\n");
300 subscr_put(subscr);
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800301 subscr = conn->subscr;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200302 }
303
Holger Hans Peter Freytherd91b7372010-05-10 01:22:39 +0800304 counter_inc(bts->network->stats.paging.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100305
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200306 /* Stop paging on the bts we received the paging response */
Sylvain Munautc7ff2572010-12-01 21:41:35 +0100307 paging_request_stop(conn->bts, subscr, conn, msg);
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200308 return 0;
309}
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200310
311/* Chapter 9.1.9: Ciphering Mode Command */
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200312int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200313{
314 struct msgb *msg = gsm48_msgb_alloc();
315 struct gsm48_hdr *gh;
316 u_int8_t ciph_mod_set;
317
318 msg->lchan = lchan;
319
320 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
321
322 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
323 ciph_mod_set = 0;
324 else
325 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
326
327 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
328 gh->proto_discr = GSM48_PDISC_RR;
329 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200330 gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200331
332 return rsl_encryption_cmd(msg);
333}
334
Harald Welte8c83af62009-11-29 20:02:53 +0100335static void gsm48_cell_desc(struct gsm48_cell_desc *cd,
336 const struct gsm_bts *bts)
337{
338 cd->ncc = (bts->bsic >> 3 & 0x7);
339 cd->bcc = (bts->bsic & 0x7);
340 cd->arfcn_hi = bts->c0->arfcn >> 8;
341 cd->arfcn_lo = bts->c0->arfcn & 0xff;
342}
343
Harald Weltea39b0f22010-06-14 22:26:10 +0200344void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd,
345 const struct gsm_lchan *lchan)
Harald Welte8c83af62009-11-29 20:02:53 +0100346{
347 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
348
349 cd->chan_nr = lchan2chan_nr(lchan);
Harald Weltea39b0f22010-06-14 22:26:10 +0200350 if (!lchan->ts->hopping.enabled) {
351 cd->h0.tsc = lchan->ts->trx->bts->tsc;
352 cd->h0.h = 0;
353 cd->h0.arfcn_high = arfcn >> 8;
354 cd->h0.arfcn_low = arfcn & 0xff;
355 } else {
356 cd->h1.tsc = lchan->ts->trx->bts->tsc;
357 cd->h1.h = 1;
358 cd->h1.maio_high = lchan->ts->hopping.maio >> 2;
359 cd->h1.maio_low = lchan->ts->hopping.maio & 0x03;
360 cd->h1.hsn = lchan->ts->hopping.hsn;
361 }
Harald Welte8c83af62009-11-29 20:02:53 +0100362}
363
Harald Welte29aca172010-06-15 16:45:51 +0200364#define GSM48_HOCMD_CCHDESC_LEN 16
365
Harald Welte8c83af62009-11-29 20:02:53 +0100366/* Chapter 9.1.15: Handover Command */
Harald Welte7a7a0d52009-12-17 00:25:18 +0100367int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
368 u_int8_t power_command, u_int8_t ho_ref)
Harald Welte8c83af62009-11-29 20:02:53 +0100369{
370 struct msgb *msg = gsm48_msgb_alloc();
371 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
372 struct gsm48_ho_cmd *ho =
373 (struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho));
Harald Welte8c83af62009-11-29 20:02:53 +0100374
375 msg->lchan = old_lchan;
Harald Welte7a7a0d52009-12-17 00:25:18 +0100376 gh->proto_discr = GSM48_PDISC_RR;
377 gh->msg_type = GSM48_MT_RR_HANDO_CMD;
Harald Welte8c83af62009-11-29 20:02:53 +0100378
379 /* mandatory bits */
380 gsm48_cell_desc(&ho->cell_desc, new_lchan->ts->trx->bts);
Harald Weltea39b0f22010-06-14 22:26:10 +0200381 gsm48_lchan2chan_desc(&ho->chan_desc, new_lchan);
Harald Welte7a7a0d52009-12-17 00:25:18 +0100382 ho->ho_ref = ho_ref;
Harald Welte8c83af62009-11-29 20:02:53 +0100383 ho->power_command = power_command;
384
Harald Welte29aca172010-06-15 16:45:51 +0200385 if (new_lchan->ts->hopping.enabled) {
386 struct gsm_bts *bts = new_lchan->ts->trx->bts;
387 struct gsm48_system_information_type_1 *si1;
388 uint8_t *cur;
389
390 si1 = GSM_BTS_SI(bts, SYSINFO_TYPE_1);
391 /* Copy the Cell Chan Desc (ARFCNS in this cell) */
392 msgb_put_u8(msg, GSM48_IE_CELL_CH_DESC);
393 cur = msgb_put(msg, GSM48_HOCMD_CCHDESC_LEN);
394 memcpy(cur, si1->cell_channel_description,
395 GSM48_HOCMD_CCHDESC_LEN);
396 /* Copy the Mobile Allocation */
397 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE,
398 new_lchan->ts->hopping.ma_len,
399 new_lchan->ts->hopping.ma_data);
400 }
Harald Welte8c83af62009-11-29 20:02:53 +0100401 /* FIXME: optional bits for type of synchronization? */
402
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800403 return gsm48_sendmsg(msg);
Harald Welte8c83af62009-11-29 20:02:53 +0100404}
405
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200406/* Chapter 9.1.2: Assignment Command */
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100407int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, u_int8_t power_command)
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200408{
409 struct msgb *msg = gsm48_msgb_alloc();
410 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
411 struct gsm48_ass_cmd *ass =
412 (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200413
414 DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
415
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100416 msg->lchan = dest_lchan;
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200417 gh->proto_discr = GSM48_PDISC_RR;
418 gh->msg_type = GSM48_MT_RR_ASS_CMD;
419
420 /*
421 * fill the channel information element, this code
422 * should probably be shared with rsl_rx_chan_rqd(),
423 * gsm48_tx_chan_mode_modify. But beware that 10.5.2.5
424 * 10.5.2.5.a have slightly different semantic for
425 * the chan_desc. But as long as multi-slot configurations
426 * are not used we seem to be fine.
427 */
Harald Weltea39b0f22010-06-14 22:26:10 +0200428 gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200429 ass->power_command = power_command;
430
Harald Welte29aca172010-06-15 16:45:51 +0200431 /* optional: cell channel description */
432
Holger Hans Peter Freyther1b891fd2010-01-28 11:51:24 +0100433 msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
434
Harald Welte29aca172010-06-15 16:45:51 +0200435 /* mobile allocation in case of hopping */
436 if (lchan->ts->hopping.enabled) {
437 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
438 lchan->ts->hopping.ma_data);
439 }
440
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100441 /* in case of multi rate we need to attach a config */
442 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100443 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100444 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
445 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100446 } else {
447 u_int8_t *data = msgb_put(msg, 4);
448 data[0] = GSM48_IE_MUL_RATE_CFG;
449 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100450 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100451 }
452 }
453
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800454 return gsm48_sendmsg(msg);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200455}
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200456
457/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100458int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200459{
460 struct msgb *msg = gsm48_msgb_alloc();
461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
462 struct gsm48_chan_mode_modify *cmm =
463 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200464
465 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
466
467 lchan->tch_mode = mode;
468 msg->lchan = lchan;
469 gh->proto_discr = GSM48_PDISC_RR;
470 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
471
472 /* fill the channel information element, this code
473 * should probably be shared with rsl_rx_chan_rqd() */
Harald Weltea39b0f22010-06-14 22:26:10 +0200474 gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200475 cmm->mode = mode;
476
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100477 /* in case of multi rate we need to attach a config */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100478 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
479 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100480 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
481 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100482 } else {
483 u_int8_t *data = msgb_put(msg, 4);
484 data[0] = GSM48_IE_MUL_RATE_CFG;
485 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100486 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100487 }
488 }
489
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800490 return gsm48_sendmsg(msg);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200491}
492
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100493int gsm48_lchan_modify(struct gsm_lchan *lchan, u_int8_t lchan_mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200494{
495 int rc;
496
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100497 rc = gsm48_tx_chan_mode_modify(lchan, lchan_mode);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200498 if (rc < 0)
499 return rc;
500
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200501 return rc;
502}
503
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200504int gsm48_rx_rr_modif_ack(struct msgb *msg)
505{
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100506 int rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200507 struct gsm48_hdr *gh = msgb_l3(msg);
508 struct gsm48_chan_mode_modify *mod =
509 (struct gsm48_chan_mode_modify *) gh->data;
510
511 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
512
513 if (mod->mode != msg->lchan->tch_mode) {
Harald Welte50720e72009-12-24 11:50:20 +0100514 LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200515 msg->lchan->tch_mode, mod->mode);
516 return -1;
517 }
518
519 /* update the channel type */
520 switch (mod->mode) {
521 case GSM48_CMODE_SIGN:
522 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
523 break;
524 case GSM48_CMODE_SPEECH_V1:
525 case GSM48_CMODE_SPEECH_EFR:
526 case GSM48_CMODE_SPEECH_AMR:
527 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
528 break;
529 case GSM48_CMODE_DATA_14k5:
530 case GSM48_CMODE_DATA_12k0:
531 case GSM48_CMODE_DATA_6k0:
532 case GSM48_CMODE_DATA_3k6:
533 msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
534 break;
535 }
536
537 /* We've successfully modified the MS side of the channel,
538 * now go on to modify the BTS side of the channel */
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100539 rc = rsl_chan_mode_modify_req(msg->lchan);
540
541 /* FIXME: we not only need to do this after mode modify, but
542 * also after channel activation */
543 if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
544 rsl_ipacc_crcx(msg->lchan);
545 return rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200546}
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100547
548int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
549{
550 struct gsm48_hdr *gh = msgb_l3(msg);
551 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
552 u_int8_t *data = gh->data;
553 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
554 struct bitvec *nbv = &bts->si_common.neigh_list;
Harald Weltea06c35e2009-12-25 22:46:25 +0100555 struct gsm_meas_rep_cell *mrc;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100556
557 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
558 return -EINVAL;
559
560 if (data[0] & 0x80)
561 rep->flags |= MEAS_REP_F_BA1;
562 if (data[0] & 0x40)
563 rep->flags |= MEAS_REP_F_UL_DTX;
564 if ((data[1] & 0x40) == 0x00)
565 rep->flags |= MEAS_REP_F_DL_VALID;
566
567 rep->dl.full.rx_lev = data[0] & 0x3f;
568 rep->dl.sub.rx_lev = data[1] & 0x3f;
569 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
570 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
571
572 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
573 if (rep->num_cell < 1 || rep->num_cell > 6)
574 return 0;
575
576 /* an encoding nightmare in perfection */
Harald Weltea06c35e2009-12-25 22:46:25 +0100577 mrc = &rep->cell[0];
578 mrc->rxlev = data[3] & 0x3f;
Harald Welte950eca92009-12-25 22:49:34 +0100579 mrc->neigh_idx = data[4] >> 3;
Harald Welteeaa95d52009-12-25 23:01:54 +0100580 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100581 mrc->bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100582 if (rep->num_cell < 2)
583 return 0;
584
Harald Weltea06c35e2009-12-25 22:46:25 +0100585 mrc = &rep->cell[1];
586 mrc->rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
Harald Welte950eca92009-12-25 22:49:34 +0100587 mrc->neigh_idx = (data[6] >> 2) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100588 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100589 mrc->bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100590 if (rep->num_cell < 3)
591 return 0;
592
Harald Weltea06c35e2009-12-25 22:46:25 +0100593 mrc = &rep->cell[2];
594 mrc->rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
Harald Welte950eca92009-12-25 22:49:34 +0100595 mrc->neigh_idx = (data[8] >> 1) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100596 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Welte (local)65898582009-12-26 14:12:43 +0100597 mrc->bsic = ((data[8] & 0x01) << 5) | (data[9] >> 3);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100598 if (rep->num_cell < 4)
599 return 0;
600
Harald Weltea06c35e2009-12-25 22:46:25 +0100601 mrc = &rep->cell[3];
602 mrc->rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
Harald Welte950eca92009-12-25 22:49:34 +0100603 mrc->neigh_idx = data[10] & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100604 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100605 mrc->bsic = data[11] >> 2;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100606 if (rep->num_cell < 5)
607 return 0;
608
Harald Weltea06c35e2009-12-25 22:46:25 +0100609 mrc = &rep->cell[4];
610 mrc->rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
Harald Welte950eca92009-12-25 22:49:34 +0100611 mrc->neigh_idx = ((data[12] & 0xf) << 1) | (data[13] >> 7);
Harald Welteeaa95d52009-12-25 23:01:54 +0100612 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100613 mrc->bsic = (data[13] >> 1) & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100614 if (rep->num_cell < 6)
615 return 0;
616
Harald Weltea06c35e2009-12-25 22:46:25 +0100617 mrc = &rep->cell[5];
Harald Welte950eca92009-12-25 22:49:34 +0100618 mrc->rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
619 mrc->neigh_idx = ((data[14] & 0x07) << 2) | (data[15] >> 6);
Harald Welteeaa95d52009-12-25 23:01:54 +0100620 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100621 mrc->bsic = data[15] & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100622
623 return 0;
624}
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800625
626struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
627{
628 struct msgb *msg;
629 struct gsm48_hdr *gh;
630
631 msg = gsm48_msgb_alloc();
632 if (!msg)
633 return NULL;
634
635 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
636 gh->proto_discr = GSM48_PDISC_MM;
637 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
638 gh->data[0] = value;
639
640 return msg;
641}
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800642
643struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
644{
645 struct gsm48_hdr *gh;
646 struct msgb *msg;
647
648 msg = gsm48_msgb_alloc();
649 if (!msg)
650 return NULL;
651
652 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
653 gh->proto_discr = GSM48_PDISC_MM;
654 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
655 gh->data[0] = cause;
656 return msg;
657}