blob: bb017461270722a6218b9f0f9fd1156a27d968ff [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;
156}
157
158enum gsm_chan_t get_ctype_by_chreq(struct gsm_network *network, u_int8_t ra)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200159{
160 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100161 int length;
162 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200163
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800164 if (network->neci) {
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100165 chreq = chreq_type_neci1;
166 length = ARRAY_SIZE(chreq_type_neci1);
167 } else {
168 chreq = chreq_type_neci0;
169 length = ARRAY_SIZE(chreq_type_neci0);
170 }
171
172
173 for (i = 0; i < length; i++) {
174 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200175 if ((ra & chr->mask) == chr->val)
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800176 return network->ctype_by_chreq[chr->type];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200177 }
Harald Welte50720e72009-12-24 11:50:20 +0100178 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200179 return GSM_LCHAN_SDCCH;
180}
181
Holger Hans Peter Freyther78891072010-09-06 09:36:02 +0800182enum gsm_chreq_reason_t get_reason_by_chreq(u_int8_t ra, int neci)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200183{
184 int i;
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100185 int length;
186 const struct chreq *chreq;
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200187
Holger Hans Peter Freyther843b44d2009-11-17 16:38:25 +0100188 if (neci) {
189 chreq = chreq_type_neci1;
190 length = ARRAY_SIZE(chreq_type_neci1);
191 } else {
192 chreq = chreq_type_neci0;
193 length = ARRAY_SIZE(chreq_type_neci0);
194 }
195
196 for (i = 0; i < length; i++) {
197 const struct chreq *chr = &chreq[i];
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200198 if ((ra & chr->mask) == chr->val)
199 return reason_by_chreq[chr->type];
200 }
Harald Welte50720e72009-12-24 11:50:20 +0100201 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200202 return GSM_CHREQ_REASON_OTHER;
203}
204
205/* 7.1.7 and 9.1.7: RR CHANnel RELease */
206int gsm48_send_rr_release(struct gsm_lchan *lchan)
207{
208 struct msgb *msg = gsm48_msgb_alloc();
209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
210 u_int8_t *cause;
211
212 msg->lchan = lchan;
213 gh->proto_discr = GSM48_PDISC_RR;
214 gh->msg_type = GSM48_MT_RR_CHAN_REL;
215
216 cause = msgb_put(msg, 1);
217 cause[0] = GSM48_RR_CAUSE_NORMAL;
218
219 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
220 lchan->nr, lchan->type);
221
222 /* Send actual release request to MS */
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800223 gsm48_sendmsg(msg);
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200224 /* FIXME: Start Timer T3109 */
225
226 /* Deactivate the SACCH on the BTS side */
227 return rsl_deact_sacch(lchan);
228}
229
Holger Hans Peter Freytheradc14782009-08-21 04:57:35 +0200230int send_siemens_mrpci(struct gsm_lchan *lchan,
231 u_int8_t *classmark2_lv)
232{
233 struct rsl_mrpci mrpci;
234
235 if (classmark2_lv[0] < 2)
236 return -EINVAL;
237
238 mrpci.power_class = classmark2_lv[1] & 0x7;
239 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
240 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
241 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
242
243 return rsl_siemens_mrpci(lchan, &mrpci);
244}
245
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800246int 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 +0200247{
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800248 /* Check the size for the classmark */
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800249 if (length < 1 + *classmark2_lv)
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800250 return -1;
251
252 u_int8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800253 if (length < 2 + *classmark2_lv + mi_lv[0])
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800254 return -2;
255
256 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200257 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
258}
259
Holger Hans Peter Freytherb7b9c1d2010-05-16 01:07:20 +0800260int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
261 char *mi_string, u_int8_t *mi_type)
262{
263 static const uint32_t classmark_offset =
264 offsetof(struct gsm48_pag_resp, classmark2);
265 u_int8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
266 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
267 mi_string, mi_type);
268}
269
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800270int gsm48_handle_paging_resp(struct gsm_subscriber_connection *conn,
271 struct msgb *msg, struct gsm_subscriber *subscr)
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200272{
273 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
274 struct gsm48_hdr *gh = msgb_l3(msg);
275 u_int8_t *classmark2_lv = gh->data + 1;
276 struct paging_signal_data sig_data;
277
278 if (is_siemens_bts(bts))
279 send_siemens_mrpci(msg->lchan, classmark2_lv);
280
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800281 if (!conn->subscr) {
282 conn->subscr = subscr;
283 } else if (conn->subscr != subscr) {
Harald Welte50720e72009-12-24 11:50:20 +0100284 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200285 subscr_put(subscr);
286 return -EINVAL;
287 } else {
288 DEBUGP(DRR, "<- Channel already owned by us\n");
289 subscr_put(subscr);
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800290 subscr = conn->subscr;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200291 }
292
293 sig_data.subscr = subscr;
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800294 sig_data.bts = conn->bts;
295 sig_data.conn = conn;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200296
Holger Hans Peter Freytherd91b7372010-05-10 01:22:39 +0800297 counter_inc(bts->network->stats.paging.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100298
Sylvain Munautef24dff2009-12-19 12:38:10 +0100299 dispatch_signal(SS_PAGING, S_PAGING_SUCCEEDED, &sig_data);
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200300
301 /* Stop paging on the bts we received the paging response */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800302 paging_request_stop(conn->bts, subscr, conn);
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200303 return 0;
304}
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200305
306/* Chapter 9.1.9: Ciphering Mode Command */
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200307int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200308{
309 struct msgb *msg = gsm48_msgb_alloc();
310 struct gsm48_hdr *gh;
311 u_int8_t ciph_mod_set;
312
313 msg->lchan = lchan;
314
315 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
316
317 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
318 ciph_mod_set = 0;
319 else
320 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
321
322 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
323 gh->proto_discr = GSM48_PDISC_RR;
324 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
Holger Hans Peter Freytherca6bc1d2009-10-05 14:00:14 +0200325 gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
Holger Hans Peter Freytherea4088a2009-10-05 13:25:06 +0200326
327 return rsl_encryption_cmd(msg);
328}
329
Harald Welte8c83af62009-11-29 20:02:53 +0100330static void gsm48_cell_desc(struct gsm48_cell_desc *cd,
331 const struct gsm_bts *bts)
332{
333 cd->ncc = (bts->bsic >> 3 & 0x7);
334 cd->bcc = (bts->bsic & 0x7);
335 cd->arfcn_hi = bts->c0->arfcn >> 8;
336 cd->arfcn_lo = bts->c0->arfcn & 0xff;
337}
338
Harald Weltea39b0f22010-06-14 22:26:10 +0200339void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd,
340 const struct gsm_lchan *lchan)
Harald Welte8c83af62009-11-29 20:02:53 +0100341{
342 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
343
344 cd->chan_nr = lchan2chan_nr(lchan);
Harald Weltea39b0f22010-06-14 22:26:10 +0200345 if (!lchan->ts->hopping.enabled) {
346 cd->h0.tsc = lchan->ts->trx->bts->tsc;
347 cd->h0.h = 0;
348 cd->h0.arfcn_high = arfcn >> 8;
349 cd->h0.arfcn_low = arfcn & 0xff;
350 } else {
351 cd->h1.tsc = lchan->ts->trx->bts->tsc;
352 cd->h1.h = 1;
353 cd->h1.maio_high = lchan->ts->hopping.maio >> 2;
354 cd->h1.maio_low = lchan->ts->hopping.maio & 0x03;
355 cd->h1.hsn = lchan->ts->hopping.hsn;
356 }
Harald Welte8c83af62009-11-29 20:02:53 +0100357}
358
Harald Welte29aca172010-06-15 16:45:51 +0200359#define GSM48_HOCMD_CCHDESC_LEN 16
360
Harald Welte8c83af62009-11-29 20:02:53 +0100361/* Chapter 9.1.15: Handover Command */
Harald Welte7a7a0d52009-12-17 00:25:18 +0100362int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
363 u_int8_t power_command, u_int8_t ho_ref)
Harald Welte8c83af62009-11-29 20:02:53 +0100364{
365 struct msgb *msg = gsm48_msgb_alloc();
366 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
367 struct gsm48_ho_cmd *ho =
368 (struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho));
Harald Welte8c83af62009-11-29 20:02:53 +0100369
370 msg->lchan = old_lchan;
Harald Welte7a7a0d52009-12-17 00:25:18 +0100371 gh->proto_discr = GSM48_PDISC_RR;
372 gh->msg_type = GSM48_MT_RR_HANDO_CMD;
Harald Welte8c83af62009-11-29 20:02:53 +0100373
374 /* mandatory bits */
375 gsm48_cell_desc(&ho->cell_desc, new_lchan->ts->trx->bts);
Harald Weltea39b0f22010-06-14 22:26:10 +0200376 gsm48_lchan2chan_desc(&ho->chan_desc, new_lchan);
Harald Welte7a7a0d52009-12-17 00:25:18 +0100377 ho->ho_ref = ho_ref;
Harald Welte8c83af62009-11-29 20:02:53 +0100378 ho->power_command = power_command;
379
Harald Welte29aca172010-06-15 16:45:51 +0200380 if (new_lchan->ts->hopping.enabled) {
381 struct gsm_bts *bts = new_lchan->ts->trx->bts;
382 struct gsm48_system_information_type_1 *si1;
383 uint8_t *cur;
384
385 si1 = GSM_BTS_SI(bts, SYSINFO_TYPE_1);
386 /* Copy the Cell Chan Desc (ARFCNS in this cell) */
387 msgb_put_u8(msg, GSM48_IE_CELL_CH_DESC);
388 cur = msgb_put(msg, GSM48_HOCMD_CCHDESC_LEN);
389 memcpy(cur, si1->cell_channel_description,
390 GSM48_HOCMD_CCHDESC_LEN);
391 /* Copy the Mobile Allocation */
392 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE,
393 new_lchan->ts->hopping.ma_len,
394 new_lchan->ts->hopping.ma_data);
395 }
Harald Welte8c83af62009-11-29 20:02:53 +0100396 /* FIXME: optional bits for type of synchronization? */
397
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800398 return gsm48_sendmsg(msg);
Harald Welte8c83af62009-11-29 20:02:53 +0100399}
400
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200401/* Chapter 9.1.2: Assignment Command */
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100402int 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 +0200403{
404 struct msgb *msg = gsm48_msgb_alloc();
405 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
406 struct gsm48_ass_cmd *ass =
407 (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200408
409 DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
410
Holger Hans Peter Freyther550197f2010-01-28 09:43:49 +0100411 msg->lchan = dest_lchan;
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200412 gh->proto_discr = GSM48_PDISC_RR;
413 gh->msg_type = GSM48_MT_RR_ASS_CMD;
414
415 /*
416 * fill the channel information element, this code
417 * should probably be shared with rsl_rx_chan_rqd(),
418 * gsm48_tx_chan_mode_modify. But beware that 10.5.2.5
419 * 10.5.2.5.a have slightly different semantic for
420 * the chan_desc. But as long as multi-slot configurations
421 * are not used we seem to be fine.
422 */
Harald Weltea39b0f22010-06-14 22:26:10 +0200423 gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200424 ass->power_command = power_command;
425
Harald Welte29aca172010-06-15 16:45:51 +0200426 /* optional: cell channel description */
427
Holger Hans Peter Freyther1b891fd2010-01-28 11:51:24 +0100428 msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
429
Harald Welte29aca172010-06-15 16:45:51 +0200430 /* mobile allocation in case of hopping */
431 if (lchan->ts->hopping.enabled) {
432 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
433 lchan->ts->hopping.ma_data);
434 }
435
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100436 /* in case of multi rate we need to attach a config */
437 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100438 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100439 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
440 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100441 } else {
442 u_int8_t *data = msgb_put(msg, 4);
443 data[0] = GSM48_IE_MUL_RATE_CFG;
444 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100445 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100446 }
447 }
448
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800449 return gsm48_sendmsg(msg);
Holger Hans Peter Freythere81a6102009-10-22 11:47:45 +0200450}
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200451
452/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100453int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200454{
455 struct msgb *msg = gsm48_msgb_alloc();
456 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
457 struct gsm48_chan_mode_modify *cmm =
458 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200459
460 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
461
462 lchan->tch_mode = mode;
463 msg->lchan = lchan;
464 gh->proto_discr = GSM48_PDISC_RR;
465 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
466
467 /* fill the channel information element, this code
468 * should probably be shared with rsl_rx_chan_rqd() */
Harald Weltea39b0f22010-06-14 22:26:10 +0200469 gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200470 cmm->mode = mode;
471
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100472 /* in case of multi rate we need to attach a config */
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100473 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
474 if (lchan->mr_conf.ver == 0) {
Harald Welte50720e72009-12-24 11:50:20 +0100475 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
476 "without multirate config.\n");
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100477 } else {
478 u_int8_t *data = msgb_put(msg, 4);
479 data[0] = GSM48_IE_MUL_RATE_CFG;
480 data[1] = 0x2;
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100481 memcpy(&data[2], &lchan->mr_conf, 2);
Holger Hans Peter Freythera6bcc742009-11-16 22:49:24 +0100482 }
483 }
484
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800485 return gsm48_sendmsg(msg);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200486}
487
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100488int gsm48_lchan_modify(struct gsm_lchan *lchan, u_int8_t lchan_mode)
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200489{
490 int rc;
491
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100492 rc = gsm48_tx_chan_mode_modify(lchan, lchan_mode);
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200493 if (rc < 0)
494 return rc;
495
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +0200496 return rc;
497}
498
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200499int gsm48_rx_rr_modif_ack(struct msgb *msg)
500{
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100501 int rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200502 struct gsm48_hdr *gh = msgb_l3(msg);
503 struct gsm48_chan_mode_modify *mod =
504 (struct gsm48_chan_mode_modify *) gh->data;
505
506 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
507
508 if (mod->mode != msg->lchan->tch_mode) {
Harald Welte50720e72009-12-24 11:50:20 +0100509 LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200510 msg->lchan->tch_mode, mod->mode);
511 return -1;
512 }
513
514 /* update the channel type */
515 switch (mod->mode) {
516 case GSM48_CMODE_SIGN:
517 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
518 break;
519 case GSM48_CMODE_SPEECH_V1:
520 case GSM48_CMODE_SPEECH_EFR:
521 case GSM48_CMODE_SPEECH_AMR:
522 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
523 break;
524 case GSM48_CMODE_DATA_14k5:
525 case GSM48_CMODE_DATA_12k0:
526 case GSM48_CMODE_DATA_6k0:
527 case GSM48_CMODE_DATA_3k6:
528 msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
529 break;
530 }
531
532 /* We've successfully modified the MS side of the channel,
533 * now go on to modify the BTS side of the channel */
Holger Hans Peter Freyther25b1e252009-11-18 19:30:24 +0100534 rc = rsl_chan_mode_modify_req(msg->lchan);
535
536 /* FIXME: we not only need to do this after mode modify, but
537 * also after channel activation */
538 if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
539 rsl_ipacc_crcx(msg->lchan);
540 return rc;
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200541}
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100542
543int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
544{
545 struct gsm48_hdr *gh = msgb_l3(msg);
546 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
547 u_int8_t *data = gh->data;
548 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
549 struct bitvec *nbv = &bts->si_common.neigh_list;
Harald Weltea06c35e2009-12-25 22:46:25 +0100550 struct gsm_meas_rep_cell *mrc;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100551
552 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
553 return -EINVAL;
554
555 if (data[0] & 0x80)
556 rep->flags |= MEAS_REP_F_BA1;
557 if (data[0] & 0x40)
558 rep->flags |= MEAS_REP_F_UL_DTX;
559 if ((data[1] & 0x40) == 0x00)
560 rep->flags |= MEAS_REP_F_DL_VALID;
561
562 rep->dl.full.rx_lev = data[0] & 0x3f;
563 rep->dl.sub.rx_lev = data[1] & 0x3f;
564 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
565 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
566
567 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
568 if (rep->num_cell < 1 || rep->num_cell > 6)
569 return 0;
570
571 /* an encoding nightmare in perfection */
Harald Weltea06c35e2009-12-25 22:46:25 +0100572 mrc = &rep->cell[0];
573 mrc->rxlev = data[3] & 0x3f;
Harald Welte950eca92009-12-25 22:49:34 +0100574 mrc->neigh_idx = data[4] >> 3;
Harald Welteeaa95d52009-12-25 23:01:54 +0100575 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100576 mrc->bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100577 if (rep->num_cell < 2)
578 return 0;
579
Harald Weltea06c35e2009-12-25 22:46:25 +0100580 mrc = &rep->cell[1];
581 mrc->rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
Harald Welte950eca92009-12-25 22:49:34 +0100582 mrc->neigh_idx = (data[6] >> 2) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100583 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100584 mrc->bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100585 if (rep->num_cell < 3)
586 return 0;
587
Harald Weltea06c35e2009-12-25 22:46:25 +0100588 mrc = &rep->cell[2];
589 mrc->rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
Harald Welte950eca92009-12-25 22:49:34 +0100590 mrc->neigh_idx = (data[8] >> 1) & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100591 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Welte (local)65898582009-12-26 14:12:43 +0100592 mrc->bsic = ((data[8] & 0x01) << 5) | (data[9] >> 3);
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100593 if (rep->num_cell < 4)
594 return 0;
595
Harald Weltea06c35e2009-12-25 22:46:25 +0100596 mrc = &rep->cell[3];
597 mrc->rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
Harald Welte950eca92009-12-25 22:49:34 +0100598 mrc->neigh_idx = data[10] & 0x1f;
Harald Welteeaa95d52009-12-25 23:01:54 +0100599 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100600 mrc->bsic = data[11] >> 2;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100601 if (rep->num_cell < 5)
602 return 0;
603
Harald Weltea06c35e2009-12-25 22:46:25 +0100604 mrc = &rep->cell[4];
605 mrc->rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
Harald Welte950eca92009-12-25 22:49:34 +0100606 mrc->neigh_idx = ((data[12] & 0xf) << 1) | (data[13] >> 7);
Harald Welteeaa95d52009-12-25 23:01:54 +0100607 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100608 mrc->bsic = (data[13] >> 1) & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100609 if (rep->num_cell < 6)
610 return 0;
611
Harald Weltea06c35e2009-12-25 22:46:25 +0100612 mrc = &rep->cell[5];
Harald Welte950eca92009-12-25 22:49:34 +0100613 mrc->rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
614 mrc->neigh_idx = ((data[14] & 0x07) << 2) | (data[15] >> 6);
Harald Welteeaa95d52009-12-25 23:01:54 +0100615 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
Harald Weltea06c35e2009-12-25 22:46:25 +0100616 mrc->bsic = data[15] & 0x3f;
Holger Hans Peter Freyther92ffd922009-12-22 07:45:17 +0100617
618 return 0;
619}
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800620
621struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
622{
623 struct msgb *msg;
624 struct gsm48_hdr *gh;
625
626 msg = gsm48_msgb_alloc();
627 if (!msg)
628 return NULL;
629
630 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
631 gh->proto_discr = GSM48_PDISC_MM;
632 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
633 gh->data[0] = value;
634
635 return msg;
636}
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800637
638struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
639{
640 struct gsm48_hdr *gh;
641 struct msgb *msg;
642
643 msg = gsm48_msgb_alloc();
644 if (!msg)
645 return NULL;
646
647 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
648 gh->proto_discr = GSM48_PDISC_MM;
649 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
650 gh->data[0] = cause;
651 return msg;
652}