blob: e8d54ac98b73a21d3a5c5a53385286872b7fbb9c [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 Freyther1494a762009-08-01 07:26:59 +020033#include <openbsc/debug.h>
34#include <openbsc/gsm_04_08.h>
35#include <openbsc/transaction.h>
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +020036#include <openbsc/paging.h>
37#include <openbsc/signal.h>
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020038
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020039/* should ip.access BTS use direct RTP streams between each other (1),
40 * or should OpenBSC always act as RTP relay/proxy in between (0) ? */
41int ipacc_rtp_direct = 1;
42
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080043static int gsm48_sendmsg(struct msgb *msg)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020044{
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080045 if (msg->lchan)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020046 msg->trx = msg->lchan->ts->trx;
47
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020048 msg->l3h = msg->data;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020049 return rsl_data_request(msg, 0);
50}
51
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020052/* Section 9.1.8 / Table 9.9 */
53struct chreq {
54 u_int8_t val;
55 u_int8_t mask;
56 enum chreq_type type;
57};
58
59/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
60static const struct chreq chreq_type_neci1[] = {
61 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
62 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
63 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
64 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
65 { 0xe0, 0xe0, CHREQ_T_SDCCH },
66 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
67 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
68 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
69 { 0x10, 0xf0, CHREQ_T_SDCCH },
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +010070 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI1 },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020071 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
72 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +010073 { 0x67, 0xff, CHREQ_T_LMU },
74 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
75 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
76 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
77 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020078};
79
80/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
81static const struct chreq chreq_type_neci0[] = {
82 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
83 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
84 { 0xe0, 0xe0, CHREQ_T_TCH_F },
85 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
86 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +010087 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI0 },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020088 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
89 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +010090 { 0x67, 0xff, CHREQ_T_LMU },
91 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
92 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
93 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
94 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020095};
96
97static const enum gsm_chan_t ctype_by_chreq[] = {
98 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
99 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
100 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
101 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
102 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
103 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
104 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
105 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
106 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +0100107 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_SDCCH,
108 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_SDCCH,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200109 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
110 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +0100111 [CHREQ_T_LMU] = GSM_LCHAN_SDCCH,
112 [CHREQ_T_RESERVED_SDCCH] = GSM_LCHAN_SDCCH,
113 [CHREQ_T_RESERVED_IGNORE] = GSM_LCHAN_UNKNOWN,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200114};
115
116static const enum gsm_chreq_reason_t reason_by_chreq[] = {
117 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
118 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
119 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
120 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
121 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
122 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
Harald Welte9385c112009-12-12 20:57:52 +0100123 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_CALL,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200124 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
125 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
Holger Hans Peter Freyther135a7c62009-11-17 16:46:46 +0100126 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_CHREQ_REASON_PAG,
127 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_CHREQ_REASON_PAG,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200128 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
129 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
Holger Hans Peter Freytherd61654c2009-11-21 20:20:43 +0100130 [CHREQ_T_LMU] = GSM_CHREQ_REASON_OTHER,
131 [CHREQ_T_RESERVED_SDCCH] = GSM_CHREQ_REASON_OTHER,
132 [CHREQ_T_RESERVED_IGNORE] = GSM_CHREQ_REASON_OTHER,
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200133};
134
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800135/* verify that the two tables match */
136static_assert(sizeof(ctype_by_chreq) ==
137 sizeof(((struct gsm_network *) NULL)->ctype_by_chreq), assert_size);
138
139/*
140 * Update channel types for request based on policy. E.g. in the
141 * case of a TCH/H network/bsc use TCH/H for the emergency calls,
142 * for early assignment assign a SDCCH and some other options.
143 */
144void gsm_net_update_ctype(struct gsm_network *network)
145{
146 /* copy over the data */
147 memcpy(network->ctype_by_chreq, ctype_by_chreq, sizeof(ctype_by_chreq));
148
149 /*
150 * Use TCH/H for emergency calls when this cell allows TCH/H. Maybe it
151 * is better to iterate over the BTS/TRX and check if no TCH/F is available
152 * and then set it to TCH/H.
153 */
154 if (network->neci)
155 network->ctype_by_chreq[CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_H;
Holger Hans Peter Freyther76fc4a32010-09-06 09:41:50 +0800156
157 if (network->pag_any_tch) {
158 if (network->neci) {
159 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_H;
160 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_H;
161 } else {
162 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_F;
163 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_F;
164 }
165 }
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800166}
167
168enum gsm_chan_t get_ctype_by_chreq(struct gsm_network *network, u_int8_t ra)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200169{
170 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100171 int length;
172 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200173
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800174 if (network->neci) {
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100175 chreq = chreq_type_neci1;
176 length = ARRAY_SIZE(chreq_type_neci1);
177 } else {
178 chreq = chreq_type_neci0;
179 length = ARRAY_SIZE(chreq_type_neci0);
180 }
181
182
183 for (i = 0; i < length; i++) {
184 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200185 if ((ra & chr->mask) == chr->val)
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800186 return network->ctype_by_chreq[chr->type];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200187 }
Harald Welte50720e72009-12-24 11:50:20 +0100188 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200189 return GSM_LCHAN_SDCCH;
190}
191
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800192enum gsm_chreq_reason_t get_reason_by_chreq(u_int8_t ra, int neci)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200193{
194 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100195 int length;
196 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200197
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100198 if (neci) {
199 chreq = chreq_type_neci1;
200 length = ARRAY_SIZE(chreq_type_neci1);
201 } else {
202 chreq = chreq_type_neci0;
203 length = ARRAY_SIZE(chreq_type_neci0);
204 }
205
206 for (i = 0; i < length; i++) {
207 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200208 if ((ra & chr->mask) == chr->val)
209 return reason_by_chreq[chr->type];
210 }
Harald Welte50720e72009-12-24 11:50:20 +0100211 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200212 return GSM_CHREQ_REASON_OTHER;
213}
214
215/* 7.1.7 and 9.1.7: RR CHANnel RELease */
216int gsm48_send_rr_release(struct gsm_lchan *lchan)
217{
218 struct msgb *msg = gsm48_msgb_alloc();
219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
220 u_int8_t *cause;
221
222 msg->lchan = lchan;
223 gh->proto_discr = GSM48_PDISC_RR;
224 gh->msg_type = GSM48_MT_RR_CHAN_REL;
225
226 cause = msgb_put(msg, 1);
227 cause[0] = GSM48_RR_CAUSE_NORMAL;
228
229 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
230 lchan->nr, lchan->type);
231
232 /* Send actual release request to MS */
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800233 gsm48_sendmsg(msg);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200234 /* FIXME: Start Timer T3109 */
235
236 /* Deactivate the SACCH on the BTS side */
237 return rsl_deact_sacch(lchan);
238}
239
Holger Hans Peter Freytheradc14782009-08-21 04:57:35 +0200240int send_siemens_mrpci(struct gsm_lchan *lchan,
241 u_int8_t *classmark2_lv)
242{
243 struct rsl_mrpci mrpci;
244
245 if (classmark2_lv[0] < 2)
246 return -EINVAL;
247
248 mrpci.power_class = classmark2_lv[1] & 0x7;
249 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
250 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
251 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
252
253 return rsl_siemens_mrpci(lchan, &mrpci);
254}
255
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800256int 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 +0200257{
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800258 /* Check the size for the classmark */
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800259 if (length < 1 + *classmark2_lv)
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800260 return -1;
261
262 u_int8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800263 if (length < 2 + *classmark2_lv + mi_lv[0])
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800264 return -2;
265
266 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200267 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
268}
269
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800270int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
271 char *mi_string, u_int8_t *mi_type)
272{
273 static const uint32_t classmark_offset =
274 offsetof(struct gsm48_pag_resp, classmark2);
275 u_int8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
276 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
277 mi_string, mi_type);
278}
279
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800280int gsm48_handle_paging_resp(struct gsm_subscriber_connection *conn,
281 struct msgb *msg, struct gsm_subscriber *subscr)
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200282{
283 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
284 struct gsm48_hdr *gh = msgb_l3(msg);
285 u_int8_t *classmark2_lv = gh->data + 1;
286 struct paging_signal_data sig_data;
287
288 if (is_siemens_bts(bts))
289 send_siemens_mrpci(msg->lchan, classmark2_lv);
290
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800291 if (!conn->subscr) {
292 conn->subscr = subscr;
293 } else if (conn->subscr != subscr) {
Harald Welte50720e72009-12-24 11:50:20 +0100294 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200295 subscr_put(subscr);
296 return -EINVAL;
297 } else {
298 DEBUGP(DRR, "<- Channel already owned by us\n");
299 subscr_put(subscr);
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800300 subscr = conn->subscr;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200301 }
302
303 sig_data.subscr = subscr;
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800304 sig_data.bts = conn->bts;
305 sig_data.conn = conn;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200306
Holger Hans Peter Freytherd91b7372010-05-10 01:22:39 +0800307 counter_inc(bts->network->stats.paging.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100308
Sylvain Munautef24dff2009-12-19 12:38:10 +0100309 dispatch_signal(SS_PAGING, S_PAGING_SUCCEEDED, &sig_data);
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200310
311 /* Stop paging on the bts we received the paging response */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800312 paging_request_stop(conn->bts, subscr, conn);
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200313 return 0;
314}
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200315
316/* Chapter 9.1.9: Ciphering Mode Command */
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200317int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200318{
319 struct msgb *msg = gsm48_msgb_alloc();
320 struct gsm48_hdr *gh;
321 u_int8_t ciph_mod_set;
322
323 msg->lchan = lchan;
324
325 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
326
327 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
328 ciph_mod_set = 0;
329 else
330 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
331
332 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
333 gh->proto_discr = GSM48_PDISC_RR;
334 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200335 gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200336
337 return rsl_encryption_cmd(msg);
338}
339
Harald Welte8c83af62009-11-29 20:02:53 +0100340static void gsm48_cell_desc(struct gsm48_cell_desc *cd,
341 const struct gsm_bts *bts)
342{
343 cd->ncc = (bts->bsic >> 3 & 0x7);
344 cd->bcc = (bts->bsic & 0x7);
345 cd->arfcn_hi = bts->c0->arfcn >> 8;
346 cd->arfcn_lo = bts->c0->arfcn & 0xff;
347}
348
Harald Weltea39b0f22010-06-14 22:26:10 +0200349void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd,
350 const struct gsm_lchan *lchan)
Harald Welte8c83af62009-11-29 20:02:53 +0100351{
352 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
353
354 cd->chan_nr = lchan2chan_nr(lchan);
Harald Weltea39b0f22010-06-14 22:26:10 +0200355 if (!lchan->ts->hopping.enabled) {
356 cd->h0.tsc = lchan->ts->trx->bts->tsc;
357 cd->h0.h = 0;
358 cd->h0.arfcn_high = arfcn >> 8;
359 cd->h0.arfcn_low = arfcn & 0xff;
360 } else {
361 cd->h1.tsc = lchan->ts->trx->bts->tsc;
362 cd->h1.h = 1;
363 cd->h1.maio_high = lchan->ts->hopping.maio >> 2;
364 cd->h1.maio_low = lchan->ts->hopping.maio & 0x03;
365 cd->h1.hsn = lchan->ts->hopping.hsn;
366 }
Harald Welte8c83af62009-11-29 20:02:53 +0100367}
368
Harald Welte29aca172010-06-15 16:45:51 +0200369#define GSM48_HOCMD_CCHDESC_LEN 16
370
Harald Welte8c83af62009-11-29 20:02:53 +0100371/* Chapter 9.1.15: Handover Command */
Harald Welte7a7a0d52009-12-17 00:25:18 +0100372int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
373 u_int8_t power_command, u_int8_t ho_ref)
Harald Welte8c83af62009-11-29 20:02:53 +0100374{
375 struct msgb *msg = gsm48_msgb_alloc();
376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
377 struct gsm48_ho_cmd *ho =
378 (struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho));
Harald Welte8c83af62009-11-29 20:02:53 +0100379
380 msg->lchan = old_lchan;
Harald Welte7a7a0d52009-12-17 00:25:18 +0100381 gh->proto_discr = GSM48_PDISC_RR;
382 gh->msg_type = GSM48_MT_RR_HANDO_CMD;
Harald Welte8c83af62009-11-29 20:02:53 +0100383
384 /* mandatory bits */
385 gsm48_cell_desc(&ho->cell_desc, new_lchan->ts->trx->bts);
Harald Weltea39b0f22010-06-14 22:26:10 +0200386 gsm48_lchan2chan_desc(&ho->chan_desc, new_lchan);
Harald Welte7a7a0d52009-12-17 00:25:18 +0100387 ho->ho_ref = ho_ref;
Harald Welte8c83af62009-11-29 20:02:53 +0100388 ho->power_command = power_command;
389
Harald Welte29aca172010-06-15 16:45:51 +0200390 if (new_lchan->ts->hopping.enabled) {
391 struct gsm_bts *bts = new_lchan->ts->trx->bts;
392 struct gsm48_system_information_type_1 *si1;
393 uint8_t *cur;
394
395 si1 = GSM_BTS_SI(bts, SYSINFO_TYPE_1);
396 /* Copy the Cell Chan Desc (ARFCNS in this cell) */
397 msgb_put_u8(msg, GSM48_IE_CELL_CH_DESC);
398 cur = msgb_put(msg, GSM48_HOCMD_CCHDESC_LEN);
399 memcpy(cur, si1->cell_channel_description,
400 GSM48_HOCMD_CCHDESC_LEN);
401 /* Copy the Mobile Allocation */
402 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE,
403 new_lchan->ts->hopping.ma_len,
404 new_lchan->ts->hopping.ma_data);
405 }
Harald Welte8c83af62009-11-29 20:02:53 +0100406 /* FIXME: optional bits for type of synchronization? */
407
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800408 return gsm48_sendmsg(msg);
Harald Welte8c83af62009-11-29 20:02:53 +0100409}
410
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200411/* Chapter 9.1.2: Assignment Command */
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100412int 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 +0200413{
414 struct msgb *msg = gsm48_msgb_alloc();
415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
416 struct gsm48_ass_cmd *ass =
417 (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200418
419 DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
420
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100421 msg->lchan = dest_lchan;
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200422 gh->proto_discr = GSM48_PDISC_RR;
423 gh->msg_type = GSM48_MT_RR_ASS_CMD;
424
425 /*
426 * fill the channel information element, this code
427 * should probably be shared with rsl_rx_chan_rqd(),
428 * gsm48_tx_chan_mode_modify. But beware that 10.5.2.5
429 * 10.5.2.5.a have slightly different semantic for
430 * the chan_desc. But as long as multi-slot configurations
431 * are not used we seem to be fine.
432 */
Harald Weltea39b0f22010-06-14 22:26:10 +0200433 gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200434 ass->power_command = power_command;
435
Harald Welte29aca172010-06-15 16:45:51 +0200436 /* optional: cell channel description */
437
Holger Hans Peter Freyther1b891fd2010-01-28 11:51:24 +0100438 msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
439
Harald Welte29aca172010-06-15 16:45:51 +0200440 /* mobile allocation in case of hopping */
441 if (lchan->ts->hopping.enabled) {
442 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
443 lchan->ts->hopping.ma_data);
444 }
445
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100446 /* in case of multi rate we need to attach a config */
447 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100448 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100449 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
450 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100451 } else {
452 u_int8_t *data = msgb_put(msg, 4);
453 data[0] = GSM48_IE_MUL_RATE_CFG;
454 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100455 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100456 }
457 }
458
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800459 return gsm48_sendmsg(msg);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200460}
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200461
462/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100463int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200464{
465 struct msgb *msg = gsm48_msgb_alloc();
466 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
467 struct gsm48_chan_mode_modify *cmm =
468 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200469
470 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
471
472 lchan->tch_mode = mode;
473 msg->lchan = lchan;
474 gh->proto_discr = GSM48_PDISC_RR;
475 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
476
477 /* fill the channel information element, this code
478 * should probably be shared with rsl_rx_chan_rqd() */
Harald Weltea39b0f22010-06-14 22:26:10 +0200479 gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200480 cmm->mode = mode;
481
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100482 /* in case of multi rate we need to attach a config */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100483 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
484 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100485 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
486 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100487 } else {
488 u_int8_t *data = msgb_put(msg, 4);
489 data[0] = GSM48_IE_MUL_RATE_CFG;
490 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100491 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100492 }
493 }
494
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800495 return gsm48_sendmsg(msg);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200496}
497
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100498int gsm48_lchan_modify(struct gsm_lchan *lchan, u_int8_t lchan_mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200499{
500 int rc;
501
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100502 rc = gsm48_tx_chan_mode_modify(lchan, lchan_mode);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200503 if (rc < 0)
504 return rc;
505
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200506 return rc;
507}
508
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200509int gsm48_rx_rr_modif_ack(struct msgb *msg)
510{
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100511 int rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200512 struct gsm48_hdr *gh = msgb_l3(msg);
513 struct gsm48_chan_mode_modify *mod =
514 (struct gsm48_chan_mode_modify *) gh->data;
515
516 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
517
518 if (mod->mode != msg->lchan->tch_mode) {
Harald Welte50720e72009-12-24 11:50:20 +0100519 LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200520 msg->lchan->tch_mode, mod->mode);
521 return -1;
522 }
523
524 /* update the channel type */
525 switch (mod->mode) {
526 case GSM48_CMODE_SIGN:
527 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
528 break;
529 case GSM48_CMODE_SPEECH_V1:
530 case GSM48_CMODE_SPEECH_EFR:
531 case GSM48_CMODE_SPEECH_AMR:
532 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
533 break;
534 case GSM48_CMODE_DATA_14k5:
535 case GSM48_CMODE_DATA_12k0:
536 case GSM48_CMODE_DATA_6k0:
537 case GSM48_CMODE_DATA_3k6:
538 msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
539 break;
540 }
541
542 /* We've successfully modified the MS side of the channel,
543 * now go on to modify the BTS side of the channel */
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100544 rc = rsl_chan_mode_modify_req(msg->lchan);
545
546 /* FIXME: we not only need to do this after mode modify, but
547 * also after channel activation */
548 if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
549 rsl_ipacc_crcx(msg->lchan);
550 return rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200551}
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100552
553int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
554{
555 struct gsm48_hdr *gh = msgb_l3(msg);
556 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
557 u_int8_t *data = gh->data;
558 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
559 struct bitvec *nbv = &bts->si_common.neigh_list;
Harald Weltea06c35e2009-12-25 22:46:25 +0100560 struct gsm_meas_rep_cell *mrc;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100561
562 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
563 return -EINVAL;
564
565 if (data[0] & 0x80)
566 rep->flags |= MEAS_REP_F_BA1;
567 if (data[0] & 0x40)
568 rep->flags |= MEAS_REP_F_UL_DTX;
569 if ((data[1] & 0x40) == 0x00)
570 rep->flags |= MEAS_REP_F_DL_VALID;
571
572 rep->dl.full.rx_lev = data[0] & 0x3f;
573 rep->dl.sub.rx_lev = data[1] & 0x3f;
574 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
575 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
576
577 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
578 if (rep->num_cell < 1 || rep->num_cell > 6)
579 return 0;
580
581 /* an encoding nightmare in perfection */
Harald Weltea06c35e2009-12-25 22:46:25 +0100582 mrc = &rep->cell[0];
583 mrc->rxlev = data[3] & 0x3f;
Harald Welte950eca92009-12-25 22:49:34 +0100584 mrc->neigh_idx = data[4] >> 3;
Harald Welteeaa95d52009-12-25 23:01:54 +0100585 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100586 mrc->bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100587 if (rep->num_cell < 2)
588 return 0;
589
Harald Weltea06c35e2009-12-25 22:46:25 +0100590 mrc = &rep->cell[1];
591 mrc->rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
Harald Welte950eca92009-12-25 22:49:34 +0100592 mrc->neigh_idx = (data[6] >> 2) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100593 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100594 mrc->bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100595 if (rep->num_cell < 3)
596 return 0;
597
Harald Weltea06c35e2009-12-25 22:46:25 +0100598 mrc = &rep->cell[2];
599 mrc->rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
Harald Welte950eca92009-12-25 22:49:34 +0100600 mrc->neigh_idx = (data[8] >> 1) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100601 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Welte (local)65898582009-12-26 14:12:43 +0100602 mrc->bsic = ((data[8] & 0x01) << 5) | (data[9] >> 3);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100603 if (rep->num_cell < 4)
604 return 0;
605
Harald Weltea06c35e2009-12-25 22:46:25 +0100606 mrc = &rep->cell[3];
607 mrc->rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
Harald Welte950eca92009-12-25 22:49:34 +0100608 mrc->neigh_idx = data[10] & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100609 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100610 mrc->bsic = data[11] >> 2;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100611 if (rep->num_cell < 5)
612 return 0;
613
Harald Weltea06c35e2009-12-25 22:46:25 +0100614 mrc = &rep->cell[4];
615 mrc->rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
Harald Welte950eca92009-12-25 22:49:34 +0100616 mrc->neigh_idx = ((data[12] & 0xf) << 1) | (data[13] >> 7);
Harald Welteeaa95d52009-12-25 23:01:54 +0100617 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100618 mrc->bsic = (data[13] >> 1) & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100619 if (rep->num_cell < 6)
620 return 0;
621
Harald Weltea06c35e2009-12-25 22:46:25 +0100622 mrc = &rep->cell[5];
Harald Welte950eca92009-12-25 22:49:34 +0100623 mrc->rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
624 mrc->neigh_idx = ((data[14] & 0x07) << 2) | (data[15] >> 6);
Harald Welteeaa95d52009-12-25 23:01:54 +0100625 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100626 mrc->bsic = data[15] & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100627
628 return 0;
629}
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800630
631struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
632{
633 struct msgb *msg;
634 struct gsm48_hdr *gh;
635
636 msg = gsm48_msgb_alloc();
637 if (!msg)
638 return NULL;
639
640 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
641 gh->proto_discr = GSM48_PDISC_MM;
642 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
643 gh->data[0] = value;
644
645 return msg;
646}
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800647
648struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
649{
650 struct gsm48_hdr *gh;
651 struct msgb *msg;
652
653 msg = gsm48_msgb_alloc();
654 if (!msg)
655 return NULL;
656
657 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
658 gh->proto_discr = GSM48_PDISC_MM;
659 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
660 gh->data[0] = cause;
661 return msg;
662}