blob: 90b88dfb3c2c9c37d0cb65561c3d60d683fb5ee9 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* 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
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
41#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000045#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000046#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte8470bf22008-12-25 23:28:35 +000048#define GSM48_ALLOC_SIZE 1024
49#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000050
Harald Welte0c389302009-06-10 12:08:54 +080051#define GSM_MAX_FACILITY 128
52#define GSM_MAX_SSVERSION 128
53#define GSM_MAX_USERUSER 128
54
Harald Welte09e38af2009-02-16 22:52:23 +000055static const struct tlv_definition rsl_att_tlvdef = {
56 .def = {
57 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
58 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
59 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
60 [GSM48_IE_UTC] = { TLV_TYPE_TV },
61 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
62 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
63
64 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
65 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
66 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
67 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
68 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
69 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
70 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080071 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000072 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
73 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080074 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000076 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
77 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
78 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
79 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
80 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
82 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
83 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
84 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
85 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
86 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
87 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
88 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
89 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080090 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
91 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000092 /* FIXME: more elements */
93 },
94};
Harald Weltecf5b3592009-05-01 18:28:42 +000095
96static const char *rr_cause_names[] = {
97 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
98 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
99 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
100 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
101 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
102 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
103 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
104 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
105 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
106 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
107 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
108 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
109 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
110 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
111 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
112 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
113 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
114};
115
116static char strbuf[64];
117
118static const char *rr_cause_name(u_int8_t cause)
119{
120 if (cause < ARRAY_SIZE(rr_cause_names) &&
121 rr_cause_names[cause])
122 return rr_cause_names[cause];
123
124 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
125 return strbuf;
126}
127
Harald Weltef7c43522009-06-09 20:24:21 +0000128static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
129 int len)
130{
131 memset(rep, 0, sizeof(*rep));
132
133 if (data[0] & 0x80)
134 rep->flags |= MEAS_REP_F_BA1;
135 if (data[0] & 0x40)
136 rep->flags |= MEAS_REP_F_DTX;
137 if (data[1] & 0x40)
138 rep->flags |= MEAS_REP_F_VALID;
139
140 rep->rxlev_full = data[0] & 0x3f;
141 rep->rxlev_sub = data[1] & 0x3f;
142 rep->rxqual_full = (data[3] >> 4) & 0x7;
143 rep->rxqual_sub = (data[3] >> 1) & 0x7;
144 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
145 if (rep->num_cell < 1)
146 return;
147
148 /* an encoding nightmare in perfection */
149
150 rep->cell[0].rxlev = data[4] & 0x3f;
151 rep->cell[0].bcch_freq = data[5] >> 2;
152 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
153 if (rep->num_cell < 2)
154 return;
155
156 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
157 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
158 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
159 if (rep->num_cell < 3)
160 return;
161
162 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
163 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
164 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
165 if (rep->num_cell < 4)
166 return;
167
168 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
169 rep->cell[3].bcch_freq = data[11] & 0x1f;
170 rep->cell[3].bsic = data[12] >> 2;
171 if (rep->num_cell < 5)
172 return;
173
174 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
175 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
176 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
177 if (rep->num_cell < 6)
178 return;
179
180 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
181 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
182 rep->cell[5].bsic = data[16] & 0x3f;
183}
184
Holger Freytherd51524f2009-06-09 08:27:07 +0000185int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000186static int gsm48_tx_simple(struct gsm_lchan *lchan,
187 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000188static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000189
Harald Welte52b1f982008-12-23 20:25:15 +0000190struct gsm_lai {
191 u_int16_t mcc;
192 u_int16_t mnc;
193 u_int16_t lac;
194};
195
Holger Freyther89824fc2008-12-30 16:18:18 +0000196static int authorize_everonye = 0;
197void gsm0408_allow_everyone(int everyone)
198{
199 printf("Allowing everyone?\n");
200 authorize_everonye = everyone;
201}
202
Holger Freythere97f7fb2008-12-31 18:52:11 +0000203static int reject_cause = 0;
204void gsm0408_set_reject_cause(int cause)
205{
206 reject_cause = cause;
207}
208
Holger Freyther73487a22008-12-31 18:53:57 +0000209static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
210 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000211{
212 if (!subscriber)
213 return 0;
214
Holger Freyther73487a22008-12-31 18:53:57 +0000215 /*
216 * Do not send accept yet as more information should arrive. Some
217 * phones will not send us the information and we will have to check
218 * what we want to do with that.
219 */
220 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
221 return 0;
222
Holger Freyther89824fc2008-12-30 16:18:18 +0000223 if (authorize_everonye)
224 return 1;
225
226 return subscriber->authorized;
227}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000228
Holger Freyther73487a22008-12-31 18:53:57 +0000229static void release_loc_updating_req(struct gsm_lchan *lchan)
230{
Harald Welte179f0642008-12-31 23:59:18 +0000231 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000232 return;
233
Harald Welteff117a82009-05-23 05:22:08 +0000234 bsc_del_timer(&lchan->loc_operation->updating_timer);
Holger Freyther73487a22008-12-31 18:53:57 +0000235 free(lchan->loc_operation);
236 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000237 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000238}
239
240static void allocate_loc_updating_req(struct gsm_lchan *lchan)
241{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000242 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000243 release_loc_updating_req(lchan);
244
245 lchan->loc_operation = (struct gsm_loc_updating_operation *)
246 malloc(sizeof(*lchan->loc_operation));
247 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
248}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000249
Holger Freytherd51524f2009-06-09 08:27:07 +0000250static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
251{
252 u_int32_t tmsi;
253
254 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
255 db_subscriber_alloc_tmsi(lchan->subscr);
256 subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
257 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
258 release_loc_updating_req(lchan);
259 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
260 }
261
262 return 0;
263}
264
Holger Freyther7c19f742009-06-06 13:54:35 +0000265static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
266 void *handler_data, void *signal_data)
267{
268 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
269 return 0;
270
271 /*
272 * Cancel any outstanding location updating request
273 * operation taking place on the lchan.
274 */
275 struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data;
276 release_loc_updating_req(lchan);
277
278 return 0;
279}
280
281/*
282 * This will be ran by the linker when loading the DSO. We use it to
283 * do system initialization, e.g. registration of signal handlers.
284 */
285static __attribute__((constructor)) void on_dso_load_0408(void)
286{
287 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
288}
289
Harald Welte52b1f982008-12-23 20:25:15 +0000290static void to_bcd(u_int8_t *bcd, u_int16_t val)
291{
Harald Welte4b634542008-12-27 01:55:51 +0000292 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000293 val = val / 10;
294 bcd[1] = val % 10;
295 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000296 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000297 val = val / 10;
298}
299
Holger Freyther17746612008-12-28 16:32:44 +0000300void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000301 u_int16_t mnc, u_int16_t lac)
302{
303 u_int8_t bcd[3];
304
305 to_bcd(bcd, mcc);
306 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
307 lai48->digits[1] = bcd[2];
308
309 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000310 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
311#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000312 lai48->digits[1] |= bcd[2] << 4;
313 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000314#else
315 lai48->digits[1] |= 0xf << 4;
316 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
317#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Harald Welte4b634542008-12-27 01:55:51 +0000319 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000320}
321
Harald Welte255539c2008-12-28 02:26:27 +0000322#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000323#define MID_TMSI_LEN (TMSI_LEN + 2)
324
Harald Welte255539c2008-12-28 02:26:27 +0000325int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000326{
Harald Welte65e74cc2008-12-29 01:55:35 +0000327 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000328
Harald Welte4b634542008-12-27 01:55:51 +0000329 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000330 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000331 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000332 *tptr = htonl(tmsi);
333
334 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000335}
336
Harald Welte09e38af2009-02-16 22:52:23 +0000337static const char bcd_num_digits[] = {
338 '0', '1', '2', '3', '4', '5', '6', '7',
339 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
340};
341
Harald Welte0c389302009-06-10 12:08:54 +0800342/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
343int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
344 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000345{
346 u_int8_t in_len = bcd_lv[0];
347 int i;
348
Harald Welte0c389302009-06-10 12:08:54 +0800349 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000350 /* lower nibble */
351 output_len--;
352 if (output_len <= 1)
353 break;
354 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
355
356 /* higher nibble */
357 output_len--;
358 if (output_len <= 1)
359 break;
360 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
361 }
362 if (output_len >= 1)
363 *output++ = '\0';
364
Harald Welte0c389302009-06-10 12:08:54 +0800365 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000366}
367
368/* convert a single ASCII character to call-control BCD */
369static int asc_to_bcd(const char asc)
370{
371 int i;
372
373 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
374 if (bcd_num_digits[i] == asc)
375 return i;
376 }
377 return -EINVAL;
378}
379
Harald Welte0c389302009-06-10 12:08:54 +0800380/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000381int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800382 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000383{
384 int in_len = strlen(input);
385 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800386 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000387
388 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800389 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000390 if (in_len % 2)
391 bcd_lv[0]++;
392
Harald Welte0c389302009-06-10 12:08:54 +0800393 if (bcd_lv[0] > max_len)
394 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000395
396 for (i = 0; i < in_len; i++) {
397 int rc = asc_to_bcd(input[i]);
398 if (rc < 0)
399 return rc;
400 if (i % 2 == 0)
401 *bcd_cur = rc;
402 else
403 *bcd_cur++ |= (rc << 4);
404 }
405 /* append padding nibble in case of odd length */
406 if (i % 2)
407 *bcd_cur++ |= 0xf0;
408
409 /* return how many bytes we used */
410 return (bcd_cur - bcd_lv);
411}
412
Harald Welte0c389302009-06-10 12:08:54 +0800413/* decode 'bearer capability' */
414static int decode_bearer_cap(int *transfer, int *mode, int *coding,
415 int *radio, int *speech_ctm, int *speech_ver,
416 const u_int8_t *lv)
417{
418 u_int8_t in_len = lv[0];
419 int i, s;
420
421 if (in_len < 1)
422 return -EINVAL;
423
424 speech_ver[0] = -1; /* end of list, of maximum 7 values */
425
426 /* octet 3 */
427 *transfer = lv[1] & 0x07;
428 *mode = (lv[1] & 0x08) >> 3;
429 *coding = (lv[1] & 0x10) >> 4;
430 *radio = (lv[1] & 0x60) >> 5;
431
432 i = 1;
433 s = 0;
434 while(!(lv[i] & 0x80)) {
435 i++; /* octet 3a etc */
436 if (in_len < i)
437 return 0;
438 speech_ver[s++] = lv[i] & 0x0f;
439 speech_ver[s] = -1; /* end of list */
440 if (i == 2) /* octet 3a */
441 *speech_ctm = (lv[i] & 0x20) >> 5;
442 if (s == 7) /* maximum speech versions + end of list */
443 return 0;
444 }
445
446 return 0;
447}
448
449/* encode 'bearer capability' */
450static int encode_bearer_cap(int lv_only, struct msgb *msg, int transfer,
451 int mode, int coding, int radio, int speech_ctm,
452 int *speech_ver)
453{
454 u_int8_t lv[32 + 1];
455 int i, s;
456
457 lv[1] = transfer;
458 lv[1] |= mode << 3;
459 lv[1] |= coding << 4;
460 lv[1] |= radio << 5;
461
462 i = 1;
463 for (s = 0; speech_ver[s] >= 0; s++) {
464 i++; /* octet 3a etc */
465 lv[i] = speech_ver[s];
466 if (i == 2) /* octet 3a */
467 lv[i] |= speech_ctm << 5;
468 }
469 lv[i] |= 0x80; /* last IE of octet 3 etc */
470
471 lv[0] = i;
472 if (lv_only)
473 msgb_lv_put(msg, lv[0], lv+1);
474 else
475 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
476
477 return 0;
478}
479
480/* decode 'call control cap' */
481static int decode_cccap(int *dtmf, int *pcp, const u_int8_t *lv)
482{
483 u_int8_t in_len = lv[0];
484
485 if (in_len < 1)
486 return -EINVAL;
487
488 /* octet 3 */
489 *dtmf = lv[1] & 0x01;
490 *pcp = (lv[1] & 0x02) >> 1;
491
492 return 0;
493}
494
495/* decode 'called party BCD number' */
496static int decode_called(int *type, int *plan, char *number,
497 int number_len, const u_int8_t *lv)
498{
499 u_int8_t in_len = lv[0];
500
501 if (in_len < 1)
502 return -EINVAL;
503
504 /* octet 3 */
505 *plan = lv[1] & 0x0f;
506 *type = (lv[1] & 0x70) >> 4;
507
508 /* octet 4..N */
509 decode_bcd_number(number, number_len, lv, 1);
510
511 return 0;
512}
513
514/* encode 'called party BCD number' */
515static int encode_called(struct msgb *msg, int type, int plan, char *number)
516{
517 u_int8_t lv[18];
518 int ret;
519
520 /* octet 3 */
521 lv[1] = plan;
522 lv[1] |= type << 4;
523
524 /* octet 4..N, octet 2 */
525 ret = encode_bcd_number(lv, sizeof(lv), 1, number);
526 if (ret < 0)
527 return ret;
528
529 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
530
531 return 0;
532}
533
534/* encode callerid of various IEs */
535static int encode_callerid(struct msgb *msg, int ie, int type, int plan,
536 int present, int screen, char *number)
537{
538 u_int8_t lv[13];
539 int h_len = 1;
540 int ret;
541
542 /* octet 3 */
543 lv[1] = plan;
544 lv[1] |= type << 4;
545
546 if (present || screen) {
547 /* octet 3a */
548 lv[2] = screen;
549 lv[2] |= present << 5;
550 lv[2] |= 0x80;
551 h_len++;
552 } else
553 lv[1] |= 0x80;
554
555 /* octet 4..N, octet 2 */
556 ret = encode_bcd_number(lv, sizeof(lv), h_len, number);
557 if (ret < 0)
558 return ret;
559
560 msgb_tlv_put(msg, ie, lv[0], lv+1);
561
562 return 0;
563}
564
565/* decode 'cause' */
566static int decode_cause(int *location, int *coding, int *rec, int *rec_val,
567 int *value, u_char *diag, u_int *diag_len,
568 const u_int8_t *lv)
569{
570 u_int8_t in_len = lv[0];
571 int i;
572
573 if (in_len < 2)
574 return -EINVAL;
575
576 *diag_len = 0;
577
578 /* octet 3 */
579 *location = lv[1] & 0x0f;
580 *coding = (lv[1] & 0x60) >> 5;
581
582 i = 1;
583 if (!(lv[i] & 0x80)) {
584 i++; /* octet 3a */
585 if (in_len < i+1)
586 return 0;
587 *rec = 1;
588 *rec_val = lv[i] & 0x7f;
589
590 }
591 i++;
592
593 /* octet 4 */
594 *value = lv[i] & 0x7f;
595 i++;
596
597 if (in_len < i) /* no diag */
598 return 0;
599
600 if (in_len - (i-1) > 32) /* maximum 32 octets */
601 return 0;
602
603 /* octet 5-N */
604 memcpy(diag, lv + i, in_len - (i-1));
605 *diag_len = in_len - (i-1);
606
607 return 0;
608}
609
610/* encode 'cause' */
611static int encode_cause(int lv_only, struct msgb *msg, int location,
612 int coding, int rec, int rec_val, int value,
613 u_char *diag, u_int diag_len)
614{
615 u_int8_t lv[32+4];
616 int i;
617
618 if (diag_len > 32)
619 return -EINVAL;
620
621 /* octet 3 */
622 lv[1] = location;
623 lv[1] |= coding << 5;
624
625 i = 1;
626 if (rec) {
627 i++; /* octet 3a */
628 lv[i] = rec_val;
629 }
630 lv[i] |= 0x80; /* end of octet 3 */
631
632 /* octet 4 */
633 i++;
634 lv[i] = 0x80 | value;
635
636 /* octet 5-N */
637 if (diag_len) {
638 memcpy(lv + i, diag, diag_len);
639 i += diag_len;
640 }
641
642 lv[0] = i;
643 if (lv_only)
644 msgb_lv_put(msg, lv[0], lv+1);
645 else
646 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
647
648 return 0;
649}
650
651/* encode 'calling number' */
652static int encode_calling(struct msgb *msg, int type, int plan, int present,
653 int screen, char *number)
654{
655 return encode_callerid(msg, GSM48_IE_CALLING_BCD, type, plan,
656 present, screen, number);
657}
658
659/* encode 'connected number' */
660static int encode_connected(struct msgb *msg, int type, int plan, int present,
661 int screen, char *number)
662{
663 return encode_callerid(msg, GSM48_IE_CONN_BCD, type, plan,
664 present, screen, number);
665}
666
667/* encode 'redirecting number' */
668static int encode_redirecting(struct msgb *msg, int type, int plan, int present,
669 int screen, char *number)
670{
671 return encode_callerid(msg, GSM48_IE_REDIR_BCD, type, plan,
672 present, screen, number);
673}
674
675/* decode 'facility' */
676static int decode_facility(char *info, int info_len, int *len,
677 const u_int8_t *lv)
678{
679 u_int8_t in_len = lv[0];
680
681 if (in_len < 1)
682 return -EINVAL;
683
684 if (in_len > info_len)
685 return -EINVAL;
686
687 memcpy(info, lv+1, in_len);
688 *len = in_len;
689
690 return 0;
691}
692
693/* encode 'facility' */
694static int encode_facility(int lv_only, struct msgb *msg, char *info, int len)
695{
696 u_int8_t lv[GSM_MAX_FACILITY + 1];
697
698 if (len < 1 || len > GSM_MAX_FACILITY)
699 return -EINVAL;
700
701 memcpy(lv+1, info, len);
702 lv[0] = len;
703 if (lv_only)
704 msgb_lv_put(msg, lv[0], lv+1);
705 else
706 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
707
708 return 0;
709}
710
711/* decode 'notify' */
712static int decode_notify(int *notify, const u_int8_t *v)
713{
714 *notify = v[0] & 0x7f;
715
716 return 0;
717}
718
719/* encode 'notify' */
720static int encode_notify(struct msgb *msg, int notify)
721{
722 msgb_v_put(msg, notify | 0x80);
723
724 return 0;
725}
726
727/* encode 'signal' */
728static int encode_signal(struct msgb *msg, int signal)
729{
730 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
731
732 return 0;
733}
734
735/* decode 'keypad' */
736static int decode_keypad(int *keypad, const u_int8_t *lv)
737{
738 u_int8_t in_len = lv[0];
739
740 if (in_len < 1)
741 return -EINVAL;
742
743 *keypad = lv[1] & 0x7f;
744
745 return 0;
746}
747
748/* encode 'keypad' */
749static int encode_keypad(struct msgb *msg, int keypad)
750{
751 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
752
753 return 0;
754}
755
756/* decode 'progress' */
757static int decode_progress(int *coding, int *location, int *descr,
758 const u_int8_t *lv)
759{
760 u_int8_t in_len = lv[0];
761
762 if (in_len < 2)
763 return -EINVAL;
764
765 *coding = (lv[1] & 0x60) >> 5;
766 *location = lv[1] & 0x0f;
767 *descr = lv[2] & 0x7f;
768
769 return 0;
770}
771
772/* encode 'progress' */
773static int encode_progress(int lv_only, struct msgb *msg, int coding,
774 int location, int descr)
775{
776 u_int8_t lv[3];
777
778 lv[0] = 2;
779 lv[1] = 0x80 | ((coding & 0x3) << 5) | (location & 0xf);
780 lv[2] = 0x80 | (descr & 0x7f);
781 if (lv_only)
782 msgb_lv_put(msg, lv[0], lv+1);
783 else
784 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
785
786 return 0;
787}
788
789/* decode 'user-user' */
790static int decode_useruser(int *proto, char *info, int info_len,
791 const u_int8_t *lv)
792{
793 u_int8_t in_len = lv[0];
794 int i;
795
796 if (in_len < 1)
797 return -EINVAL;
798
799 *proto = lv[1];
800
801 for (i = 2; i <= in_len; i++) {
802 info_len--;
803 if (info_len <= 1)
804 break;
805 *info++ = lv[i];
806 }
807 if (info_len >= 1)
808 *info++ = '\0';
809
810 return 0;
811}
812
813/* encode 'useruser' */
814static int encode_useruser(int lv_only, struct msgb *msg, int proto, char *info)
815{
816 u_int8_t lv[GSM_MAX_USERUSER + 2];
817
818 if (strlen(info) > GSM_MAX_USERUSER)
819 return -EINVAL;
820
821 lv[0] = 1 + strlen(info);
822 lv[1] = proto;
823 memcpy(lv + 2, info, strlen(info));
824 if (lv_only)
825 msgb_lv_put(msg, lv[0], lv+1);
826 else
827 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
828
829 return 0;
830}
831
832/* decode 'ss version' */
833static int decode_ssversion(char *info, int info_len, int *len,
834 const u_int8_t *lv)
835{
836 u_int8_t in_len = lv[0];
837
838 if (in_len < 1 || in_len < info_len)
839 return -EINVAL;
840
841 memcpy(info, lv + 1, in_len);
842 *len = in_len;
843
844 return 0;
845}
846
847/* encode 'more data' */
848static int encode_more(struct msgb *msg)
849{
850 u_int8_t *ie;
851
852 ie = msgb_put(msg, 1);
853 ie[0] = GSM48_IE_MORE_DATA;
854
855 return 0;
856}
857
Holger Freyther819dd202009-01-04 03:52:50 +0000858struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000859{
860 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
861}
862
Holger Freyther3e2c3232009-01-04 03:55:31 +0000863int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000864{
Harald Welte65e74cc2008-12-29 01:55:35 +0000865 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
866
867 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000868 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000869
Harald Welte65e74cc2008-12-29 01:55:35 +0000870 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
871 /* Send a 04.08 call control message, add transaction
872 * ID and TI flag */
873 gh->proto_discr |= msg->lchan->call.transaction_id;
874
875 /* GSM 04.07 Section 11.2.3.1.3 */
876 switch (msg->lchan->call.type) {
877 case GSM_CT_MO:
878 gh->proto_discr |= 0x80;
879 break;
880 case GSM_CT_MT:
881 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000882 case GSM_CT_NONE:
883 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000884 }
885 }
886 }
887
Harald Welte4b634542008-12-27 01:55:51 +0000888 msg->l3h = msg->data;
889
Harald Welte8470bf22008-12-25 23:28:35 +0000890 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000891}
892
Harald Welte52b1f982008-12-23 20:25:15 +0000893
Holger Freyther429e7762008-12-30 13:28:30 +0000894/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000895int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000896{
Harald Welte8470bf22008-12-25 23:28:35 +0000897 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000898 struct gsm48_hdr *gh;
899
Harald Welte8470bf22008-12-25 23:28:35 +0000900 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000901
902 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
903 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000904 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000905 gh->data[0] = cause;
906
Harald Weltedb253af2008-12-30 17:56:55 +0000907 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
908
Harald Welte65e74cc2008-12-29 01:55:35 +0000909 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000910}
911
912/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000913int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000914{
Harald Welte8470bf22008-12-25 23:28:35 +0000915 struct gsm_bts *bts = lchan->ts->trx->bts;
916 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000917 struct gsm48_hdr *gh;
918 struct gsm48_loc_area_id *lai;
919 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000920 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000921
Harald Welte8470bf22008-12-25 23:28:35 +0000922 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000923
924 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
925 gh->proto_discr = GSM48_PDISC_MM;
926 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
927
928 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000929 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000930 bts->network->network_code, bts->location_area_code);
931
932 mid = msgb_put(msg, MID_TMSI_LEN);
933 generate_mid_from_tmsi(mid, tmsi);
934
935 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
936
Harald Weltedb253af2008-12-30 17:56:55 +0000937 ret = gsm48_sendmsg(msg);
938
Harald Weltedb253af2008-12-30 17:56:55 +0000939 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000940
Holger Freyther07cc8d82008-12-29 06:23:46 +0000941 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000942}
943
Harald Weltefc977a82008-12-27 10:19:37 +0000944static char bcd2char(u_int8_t bcd)
945{
946 if (bcd < 0xa)
947 return '0' + bcd;
948 else
949 return 'A' + (bcd - 0xa);
950}
951
Harald Weltebf5e8df2009-02-03 12:59:45 +0000952/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000953static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
954{
955 int i;
956 u_int8_t mi_type;
957 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000958 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000959
960 mi_type = mi[0] & GSM_MI_TYPE_MASK;
961
962 switch (mi_type) {
963 case GSM_MI_TYPE_NONE:
964 break;
965 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000966 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
967 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
968 memcpy(&tmsi, &mi[1], 4);
969 tmsi = ntohl(tmsi);
970 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000971 }
972 break;
973 case GSM_MI_TYPE_IMSI:
974 case GSM_MI_TYPE_IMEI:
975 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000976 *str_cur++ = bcd2char(mi[0] >> 4);
977
978 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000979 if (str_cur + 2 >= string + str_len)
980 return str_cur - string;
981 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000982 /* skip last nibble in last input byte when GSM_EVEN */
983 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
984 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000985 }
986 break;
987 default:
988 break;
989 }
Harald Weltefc977a82008-12-27 10:19:37 +0000990 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000991
Harald Weltefc977a82008-12-27 10:19:37 +0000992 return str_cur - string;
993}
994
Harald Weltebf5e8df2009-02-03 12:59:45 +0000995/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000996static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
997{
998 struct msgb *msg = gsm48_msgb_alloc();
999 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +00001000
Harald Welte231ad4f2008-12-27 11:15:38 +00001001 msg->lchan = lchan;
1002
1003 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1004 gh->proto_discr = GSM48_PDISC_MM;
1005 gh->msg_type = GSM48_MT_MM_ID_REQ;
1006 gh->data[0] = id_type;
1007
Harald Welte65e74cc2008-12-29 01:55:35 +00001008 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001009}
1010
1011#define MI_SIZE 32
1012
Harald Weltebf5e8df2009-02-03 12:59:45 +00001013/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +00001014static int mm_rx_id_resp(struct msgb *msg)
1015{
1016 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +00001017 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +00001018 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
1019 char mi_string[MI_SIZE];
1020
1021 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +00001022 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +00001023 mi_type, mi_string);
1024
Harald Welte75a983f2008-12-27 21:34:06 +00001025 switch (mi_type) {
1026 case GSM_MI_TYPE_IMSI:
1027 if (!lchan->subscr)
1028 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001029 if (lchan->loc_operation)
1030 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001031 break;
1032 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +00001033 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +00001034 /* update subscribe <-> IMEI mapping */
1035 if (lchan->subscr)
1036 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001037 if (lchan->loc_operation)
1038 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001039 break;
1040 }
Holger Freyther73487a22008-12-31 18:53:57 +00001041
1042 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +00001043 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001044}
1045
Harald Welte255539c2008-12-28 02:26:27 +00001046
1047static void loc_upd_rej_cb(void *data)
1048{
1049 struct gsm_lchan *lchan = data;
1050
Holger Freyther73487a22008-12-31 18:53:57 +00001051 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +00001052 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001053 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001054}
1055
Holger Freytherb7193e42008-12-29 17:44:08 +00001056static void schedule_reject(struct gsm_lchan *lchan)
1057{
Holger Freyther73487a22008-12-31 18:53:57 +00001058 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1059 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001060 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001061}
1062
Harald Welte2a139372009-02-22 21:14:55 +00001063static const char *lupd_name(u_int8_t type)
1064{
1065 switch (type) {
1066 case GSM48_LUPD_NORMAL:
1067 return "NORMAL";
1068 case GSM48_LUPD_PERIODIC:
1069 return "PEROIDOC";
1070 case GSM48_LUPD_IMSI_ATT:
1071 return "IMSI ATTACH";
1072 default:
1073 return "UNKNOWN";
1074 }
1075}
1076
Harald Welte231ad4f2008-12-27 11:15:38 +00001077#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +00001078/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001079static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001080{
Harald Welte8470bf22008-12-25 23:28:35 +00001081 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001082 struct gsm48_loc_upd_req *lu;
1083 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +00001084 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +00001085 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +00001086 char mi_string[MI_SIZE];
1087 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001088
Harald Welte8470bf22008-12-25 23:28:35 +00001089 lu = (struct gsm48_loc_upd_req *) gh->data;
1090
1091 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001092
Harald Weltefc977a82008-12-27 10:19:37 +00001093 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
1094
Holger Freyther79f4ae62009-06-02 03:25:04 +00001095 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001096 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001097
Holger Freythereaf04692009-06-06 13:54:44 +00001098 /*
1099 * Pseudo Spoof detection: Just drop a second/concurrent
1100 * location updating request.
1101 */
1102 if (lchan->loc_operation) {
1103 DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n",
1104 lchan->loc_operation);
1105 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1106 return 0;
1107 }
1108
Holger Freyther73487a22008-12-31 18:53:57 +00001109 allocate_loc_updating_req(lchan);
1110
Harald Welte52b1f982008-12-23 20:25:15 +00001111 switch (mi_type) {
1112 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +00001113 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001114 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001115 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001116
Harald Welte52b1f982008-12-23 20:25:15 +00001117 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +00001118 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +00001119 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001120 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +00001121 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001122 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001123 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001124
Harald Welte52b1f982008-12-23 20:25:15 +00001125 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +00001126 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001127 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001128 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001129 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001130 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001131 }
1132 break;
1133 case GSM_MI_TYPE_IMEI:
1134 case GSM_MI_TYPE_IMEISV:
1135 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +00001136 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001137 break;
1138 default:
Harald Welte2a139372009-02-22 21:14:55 +00001139 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001140 break;
1141 }
1142
Harald Welte255539c2008-12-28 02:26:27 +00001143 lchan->subscr = subscr;
1144
Holger Freyther73487a22008-12-31 18:53:57 +00001145 /*
1146 * Schedule the reject timer and check if we can let the
1147 * subscriber into our network immediately or if we need to wait
1148 * for identity responses.
1149 */
1150 schedule_reject(lchan);
Holger Freytherd51524f2009-06-09 08:27:07 +00001151 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001152}
1153
Harald Welte7584aea2009-02-11 11:44:12 +00001154/* 9.1.5 Channel mode modify */
1155int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1156{
1157 struct msgb *msg = gsm48_msgb_alloc();
1158 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1159 struct gsm48_chan_mode_modify *cmm =
1160 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001161 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001162
Harald Welte4a543e82009-02-28 13:17:55 +00001163 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001164
Harald Welte45b407a2009-05-23 15:51:12 +00001165 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001166 msg->lchan = lchan;
1167 gh->proto_discr = GSM48_PDISC_RR;
1168 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1169
1170 /* fill the channel information element, this code
1171 * should probably be shared with rsl_rx_chan_rqd() */
1172 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001173 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001174 cmm->chan_desc.h0.h = 0;
1175 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1176 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1177 cmm->mode = mode;
1178
1179 return gsm48_sendmsg(msg);
1180}
1181
Harald Weltedb253af2008-12-30 17:56:55 +00001182/* Section 9.2.15a */
1183int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1184{
1185 struct msgb *msg = gsm48_msgb_alloc();
1186 struct gsm48_hdr *gh;
1187 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001188 u_int8_t *ptr8;
1189 u_int16_t *ptr16;
1190 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +00001191 int i;
1192
1193 msg->lchan = lchan;
1194
1195 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1196 gh->proto_discr = GSM48_PDISC_MM;
1197 gh->msg_type = GSM48_MT_MM_INFO;
1198
1199 if (net->name_long) {
1200 name_len = strlen(net->name_long);
1201 /* 10.5.3.5a */
1202 ptr8 = msgb_put(msg, 3);
1203 ptr8[0] = GSM48_IE_NAME_LONG;
1204 ptr8[1] = name_len*2 +1;
1205 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1206
1207 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1208 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001209 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001210
1211 /* FIXME: Use Cell Broadcast, not UCS-2, since
1212 * UCS-2 is only supported by later revisions of the spec */
1213 }
1214
1215 if (net->name_short) {
1216 name_len = strlen(net->name_short);
1217 /* 10.5.3.5a */
1218 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1219 ptr8[0] = GSM48_IE_NAME_LONG;
1220 ptr8[1] = name_len*2 + 1;
1221 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1222
Harald Weltee872cb12009-01-01 00:33:37 +00001223 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001224 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001225 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001226 }
1227
1228#if 0
Holger Freytherca362a62009-01-04 21:05:01 +00001229 /* move back to the top */
1230 time_t cur_t;
1231 struct tm* cur_time;
1232 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +00001233 /* Section 10.5.3.9 */
1234 cur_t = time(NULL);
1235 cur_time = gmtime(cur_t);
1236 ptr8 = msgb_put(msg, 8);
1237 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1238 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1239 ptr8[2] = to_bcd8(cur_time->tm_mon);
1240 ptr8[3] = to_bcd8(cur_time->tm_mday);
1241 ptr8[4] = to_bcd8(cur_time->tm_hour);
1242 ptr8[5] = to_bcd8(cur_time->tm_min);
1243 ptr8[6] = to_bcd8(cur_time->tm_sec);
1244 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1245 tz15min = (cur_time->tm_gmtoff)/(60*15);
1246 ptr8[6] = to_bcd8(tz15min);
1247 if (tz15min < 0)
1248 ptr8[6] |= 0x80;
1249#endif
1250
1251 return gsm48_sendmsg(msg);
1252}
1253
Harald Welte4b634542008-12-27 01:55:51 +00001254static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1255{
Harald Welte4b634542008-12-27 01:55:51 +00001256 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001257 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001258}
Harald Welteba4cf162009-01-10 01:49:35 +00001259
1260/* 9.2.6 CM service reject */
1261static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1262 enum gsm48_reject_value value)
1263{
1264 struct msgb *msg = gsm48_msgb_alloc();
1265 struct gsm48_hdr *gh;
1266
1267 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1268
1269 msg->lchan = lchan;
1270 use_lchan(lchan);
1271
1272 gh->proto_discr = GSM48_PDISC_MM;
1273 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1274 gh->data[0] = value;
1275 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1276
1277 return gsm48_sendmsg(msg);
1278}
1279
Harald Welte4ed0e922009-01-10 03:17:30 +00001280
1281/*
1282 * Handle CM Service Requests
1283 * a) Verify that the packet is long enough to contain the information
1284 * we require otherwsie reject with INCORRECT_MESSAGE
1285 * b) Try to parse the TMSI. If we do not have one reject
1286 * c) Check that we know the subscriber with the TMSI otherwise reject
1287 * with a HLR cause
1288 * d) Set the subscriber on the gsm_lchan and accept
1289 */
Harald Welte4b634542008-12-27 01:55:51 +00001290static int gsm48_rx_mm_serv_req(struct msgb *msg)
1291{
Harald Welteba4cf162009-01-10 01:49:35 +00001292 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +00001293 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001294
Harald Welteba4cf162009-01-10 01:49:35 +00001295 struct gsm_subscriber *subscr;
1296 struct gsm48_hdr *gh = msgb_l3(msg);
1297 struct gsm48_service_request *req =
1298 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001299 /* unfortunately in Phase1 the classmar2 length is variable */
1300 u_int8_t classmark2_len = gh->data[1];
1301 u_int8_t *classmark2 = gh->data+2;
1302 u_int8_t mi_len = *(classmark2 + classmark2_len);
1303 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001304
Harald Weltec9e02182009-05-01 19:07:53 +00001305 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001306 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001307 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001308 return gsm48_tx_mm_serv_rej(msg->lchan,
1309 GSM48_REJECT_INCORRECT_MESSAGE);
1310 }
1311
1312 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001313 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001314 return gsm48_tx_mm_serv_rej(msg->lchan,
1315 GSM48_REJECT_INCORRECT_MESSAGE);
1316 }
1317
Harald Weltec9e02182009-05-01 19:07:53 +00001318 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001319 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001320 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001321 return gsm48_tx_mm_serv_rej(msg->lchan,
1322 GSM48_REJECT_INCORRECT_MESSAGE);
1323 }
1324
Harald Weltec9e02182009-05-01 19:07:53 +00001325 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001326 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001327 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001328
Holger Freythereb443982009-06-04 13:58:42 +00001329 subscr = subscr_get_by_tmsi(mi_string);
1330
Harald Welte2a139372009-02-22 21:14:55 +00001331 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001332 if (!subscr)
1333 return gsm48_tx_mm_serv_rej(msg->lchan,
1334 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1335
1336 if (!msg->lchan->subscr)
1337 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001338 else if (msg->lchan->subscr != subscr) {
1339 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1340 subscr_put(subscr);
1341 }
1342
Harald Weltef7c43522009-06-09 20:24:21 +00001343 subscr->classmark2_len = classmark2_len;
1344 memcpy(subscr->classmark2, classmark2, classmark2_len);
1345
Harald Welte4b634542008-12-27 01:55:51 +00001346 return gsm48_tx_mm_serv_ack(msg->lchan);
1347}
1348
Harald Welte2a139372009-02-22 21:14:55 +00001349static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1350{
1351 struct gsm48_hdr *gh = msgb_l3(msg);
1352 struct gsm48_imsi_detach_ind *idi =
1353 (struct gsm48_imsi_detach_ind *) gh->data;
1354 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
1355 char mi_string[MI_SIZE];
1356 struct gsm_subscriber *subscr;
1357
1358 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
1359 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1360 mi_type, mi_string);
1361
1362 switch (mi_type) {
1363 case GSM_MI_TYPE_TMSI:
1364 subscr = subscr_get_by_tmsi(mi_string);
1365 break;
1366 case GSM_MI_TYPE_IMSI:
1367 subscr = subscr_get_by_imsi(mi_string);
1368 break;
1369 case GSM_MI_TYPE_IMEI:
1370 case GSM_MI_TYPE_IMEISV:
1371 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001372 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001373 break;
1374 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001375 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001376 break;
1377 }
1378
Holger Freyther4a49e772009-04-12 05:37:29 +00001379 if (subscr) {
1380 subscr_update(subscr, msg->trx->bts,
1381 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001382 DEBUGP(DMM, "Subscriber: %s\n",
1383 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001384 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001385 } else
Harald Welte2a139372009-02-22 21:14:55 +00001386 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1387
1388 put_lchan(msg->lchan);
1389
1390 return 0;
1391}
1392
Harald Welted2a7f5a2009-06-05 20:08:20 +00001393static int gsm48_rx_mm_status(struct msgb *msg)
1394{
1395 struct gsm48_hdr *gh = msgb_l3(msg);
1396
1397 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1398
1399 return 0;
1400}
1401
Harald Weltebf5e8df2009-02-03 12:59:45 +00001402/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001403static int gsm0408_rcv_mm(struct msgb *msg)
1404{
1405 struct gsm48_hdr *gh = msgb_l3(msg);
1406 int rc;
1407
1408 switch (gh->msg_type & 0xbf) {
1409 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +00001410 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001411 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001412 break;
1413 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001414 rc = mm_rx_id_resp(msg);
1415 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001416 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001417 rc = gsm48_rx_mm_serv_req(msg);
1418 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001419 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001420 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001421 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001422 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001423 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1424 msg->lchan->subscr ?
1425 msg->lchan->subscr->imsi :
1426 "unknown subscriber");
1427 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001428 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001429 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1430 break;
1431 case GSM48_MT_MM_CM_REEST_REQ:
1432 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1433 break;
1434 case GSM48_MT_MM_AUTH_RESP:
1435 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001436 break;
1437 default:
1438 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1439 gh->msg_type);
1440 break;
1441 }
1442
1443 return rc;
1444}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001445
Harald Welte2d35ae62009-02-06 12:02:13 +00001446/* Receive a PAGING RESPONSE message from the MS */
1447static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1448{
1449 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001450 u_int8_t *classmark2_lv = gh->data + 1;
1451 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1452 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001453 char mi_string[MI_SIZE];
1454 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +00001455 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001456 int rc = 0;
1457
Harald Welte61548982009-02-22 21:26:29 +00001458 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001459 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1460 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001461 switch (mi_type) {
1462 case GSM_MI_TYPE_TMSI:
1463 subscr = subscr_get_by_tmsi(mi_string);
1464 break;
1465 case GSM_MI_TYPE_IMSI:
1466 subscr = subscr_get_by_imsi(mi_string);
1467 break;
1468 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001469
1470 if (!subscr) {
1471 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001472 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001473 return -EINVAL;
1474 }
1475 DEBUGP(DRR, "<- Channel was requested by %s\n",
1476 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001477
Harald Weltef7c43522009-06-09 20:24:21 +00001478 subscr->classmark2_len = *classmark2_lv;
1479 memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv);
1480
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001481 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001482 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001483 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001484 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1485 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001486 return -EINVAL;
1487 } else {
1488 DEBUGP(DRR, "<- Channel already owned by us\n");
1489 subscr_put(subscr);
1490 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001491 }
1492
Harald Welte595ad7b2009-02-16 22:05:44 +00001493 sig_data.subscr = subscr;
1494 sig_data.bts = msg->lchan->ts->trx->bts;
1495 sig_data.lchan = msg->lchan;
1496
1497 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001498
1499 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001500 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001501
Harald Welte7584aea2009-02-11 11:44:12 +00001502 /* FIXME: somehow signal the completion of the PAGING to
1503 * the entity that requested the paging */
1504
Harald Welte2d35ae62009-02-06 12:02:13 +00001505 return rc;
1506}
1507
Harald Weltef7c43522009-06-09 20:24:21 +00001508static int gsm48_rx_rr_classmark(struct msgb *msg)
1509{
1510 struct gsm48_hdr *gh = msgb_l3(msg);
1511 struct gsm_subscriber *subscr = msg->lchan->subscr;
1512 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1513 u_int8_t cm2_len, cm3_len = 0;
1514 u_int8_t *cm2, *cm3 = NULL;
1515
1516 DEBUGP(DRR, "CLASSMARK CHANGE ");
1517
1518 /* classmark 2 */
1519 cm2_len = gh->data[0];
1520 cm2 = &gh->data[1];
1521 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1522
1523 if (payload_len > cm2_len + 1) {
1524 /* we must have a classmark3 */
1525 if (gh->data[cm2_len+1] != 0x20) {
1526 DEBUGPC(DRR, "ERR CM3 TAG\n");
1527 return -EINVAL;
1528 }
1529 if (cm2_len > 3) {
1530 DEBUGPC(DRR, "CM2 too long!\n");
1531 return -EINVAL;
1532 }
1533
1534 cm3_len = gh->data[cm2_len+2];
1535 cm3 = &gh->data[cm2_len+3];
1536 if (cm3_len > 14) {
1537 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1538 return -EINVAL;
1539 }
1540 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1541 }
1542 if (subscr) {
1543 subscr->classmark2_len = cm2_len;
1544 memcpy(subscr->classmark2, cm2, cm2_len);
1545 if (cm3) {
1546 subscr->classmark3_len = cm3_len;
1547 memcpy(subscr->classmark3, cm3, cm3_len);
1548 }
1549 }
1550
1551 /* FIXME: store the classmark2/3 values with the equipment register */
1552
1553 return 0;
1554}
1555
Harald Weltecf5b3592009-05-01 18:28:42 +00001556static int gsm48_rx_rr_status(struct msgb *msg)
1557{
1558 struct gsm48_hdr *gh = msgb_l3(msg);
1559
1560 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1561 rr_cause_name(gh->data[0]));
1562
1563 return 0;
1564}
1565
Harald Weltef7c43522009-06-09 20:24:21 +00001566static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1567{
1568 struct gsm48_hdr *gh = msgb_l3(msg);
1569 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1570 static struct gsm_meas_rep meas_rep;
1571
1572 DEBUGP(DRR, "MEASUREMENT REPORT ");
1573 parse_meas_rep(&meas_rep, gh->data, payload_len);
1574 if (meas_rep.flags & MEAS_REP_F_DTX)
1575 DEBUGPC(DRR, "DTX ");
1576 if (meas_rep.flags & MEAS_REP_F_BA1)
1577 DEBUGPC(DRR, "BA1 ");
1578 if (!(meas_rep.flags & MEAS_REP_F_VALID))
1579 DEBUGPC(DRR, "NOT VALID ");
1580 else
1581 DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
1582 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1583 meas_rep.rxqual_sub);
1584
1585 DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell);
1586
1587 /* FIXME: put the results somwhere */
1588
1589 return 0;
1590}
1591
Harald Weltebf5e8df2009-02-03 12:59:45 +00001592/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001593static int gsm0408_rcv_rr(struct msgb *msg)
1594{
1595 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001596 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001597
1598 switch (gh->msg_type) {
1599 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001600 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001601 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001602 case GSM48_MT_RR_GPRS_SUSP_REQ:
1603 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1604 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001605 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001606 rc = gsm48_rr_rx_pag_resp(msg);
1607 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001608 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1609 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001610 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001611 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001612 case GSM48_MT_RR_STATUS:
1613 rc = gsm48_rx_rr_status(msg);
1614 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001615 case GSM48_MT_RR_MEAS_REP:
1616 rc = gsm48_rx_rr_meas_rep(msg);
1617 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001618 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001619 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001620 gh->msg_type);
1621 break;
1622 }
1623
Harald Welte2d35ae62009-02-06 12:02:13 +00001624 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001625}
1626
Holger Freythere64a7a32009-02-06 21:55:37 +00001627/* 7.1.7 and 9.1.7 Channel release*/
1628int gsm48_send_rr_release(struct gsm_lchan *lchan)
1629{
1630 struct msgb *msg = gsm48_msgb_alloc();
1631 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1632 u_int8_t *cause;
1633
1634 msg->lchan = lchan;
1635 gh->proto_discr = GSM48_PDISC_RR;
1636 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1637
1638 cause = msgb_put(msg, 1);
1639 cause[0] = GSM48_RR_CAUSE_NORMAL;
1640
1641 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1642 lchan->nr, lchan->type);
1643
1644 return gsm48_sendmsg(msg);
1645}
1646
Harald Welte4bc90a12008-12-27 16:32:52 +00001647/* Call Control */
1648
Harald Welte7584aea2009-02-11 11:44:12 +00001649/* The entire call control code is written in accordance with Figure 7.10c
1650 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1651 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1652 * it for voice */
1653
Harald Welte4bc90a12008-12-27 16:32:52 +00001654static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1655{
1656 struct msgb *msg = gsm48_msgb_alloc();
1657 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1658 u_int8_t *cause, *call_state;
1659
Harald Welte65e74cc2008-12-29 01:55:35 +00001660 gh->proto_discr = GSM48_PDISC_CC;
1661
Harald Welte4bc90a12008-12-27 16:32:52 +00001662 msg->lchan = lchan;
1663
Harald Welte4bc90a12008-12-27 16:32:52 +00001664 gh->msg_type = GSM48_MT_CC_STATUS;
1665
1666 cause = msgb_put(msg, 3);
1667 cause[0] = 2;
1668 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1669 cause[2] = 0x80 | 30; /* response to status inquiry */
1670
1671 call_state = msgb_put(msg, 1);
1672 call_state[0] = 0xc0 | 0x00;
1673
Harald Welte65e74cc2008-12-29 01:55:35 +00001674 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001675}
1676
Harald Welte6f4b7532008-12-29 00:39:37 +00001677static int gsm48_tx_simple(struct gsm_lchan *lchan,
1678 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001679{
1680 struct msgb *msg = gsm48_msgb_alloc();
1681 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1682
1683 msg->lchan = lchan;
1684
Harald Welte6f4b7532008-12-29 00:39:37 +00001685 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001686 gh->msg_type = msg_type;
1687
Harald Welte65e74cc2008-12-29 01:55:35 +00001688 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001689}
1690
Harald Welte09e38af2009-02-16 22:52:23 +00001691/* call-back from paging the B-end of the connection */
1692static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001693 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001694{
Harald Welte7ccf7782009-02-17 01:43:01 +00001695 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001696 struct gsm_call *remote_call = param;
1697 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001698 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001699
1700 if (hooknum != GSM_HOOK_RR_PAGING)
1701 return -EINVAL;
1702
1703 switch (event) {
1704 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001705 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001706 remote_call->remote_lchan = lchan;
1707 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001708 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001709 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1710 if (is_ipaccess_bts(lchan->ts->trx->bts))
1711 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001712 break;
1713 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001714 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001715 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001716 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001717 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001718 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001719 break;
1720 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001721 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001722}
1723
Harald Welte4bc90a12008-12-27 16:32:52 +00001724static int gsm48_cc_rx_status_enq(struct msgb *msg)
1725{
Harald Welte4a543e82009-02-28 13:17:55 +00001726 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001727 return gsm48_cc_tx_status(msg->lchan);
1728}
1729
1730static int gsm48_cc_rx_setup(struct msgb *msg)
1731{
Harald Welte7584aea2009-02-11 11:44:12 +00001732 struct gsm_call *call = &msg->lchan->call;
1733 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001734 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001735 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001736 char called_number[(43-2)*2 + 1] = "\0";
1737 struct tlv_parsed tp;
Harald Welte7584aea2009-02-11 11:44:12 +00001738 int ret;
1739
1740 if (call->state == GSM_CSTATE_NULL ||
1741 call->state == GSM_CSTATE_RELEASE_REQ)
1742 use_lchan(msg->lchan);
1743
1744 call->type = GSM_CT_MO;
1745 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001746 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001747 call->transaction_id = gh->proto_discr & 0xf0;
1748
Harald Weltea4d49e92009-05-23 06:39:58 +00001749 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte09e38af2009-02-16 22:52:23 +00001750 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1751 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001752
Harald Welte09e38af2009-02-16 22:52:23 +00001753 /* Parse the number that was dialed and lookup subscriber */
Harald Welte0c389302009-06-10 12:08:54 +08001754 decode_bcd_number(called_number, sizeof(called_number),
1755 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1, 1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001756
Harald Welte4b458152009-02-17 13:23:06 +00001757 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001758 called_number);
1759
Harald Welte09e38af2009-02-16 22:52:23 +00001760 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001761 if (!called_subscr) {
1762 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1763 put_lchan(msg->lchan);
1764 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001765 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001766 }
Harald Welte5a065df2009-02-22 21:13:18 +00001767 if (called_subscr == msg->lchan->subscr) {
1768 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1769 put_lchan(msg->lchan);
1770 subscr_put(called_subscr);
1771 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1772 GSM48_MT_CC_RELEASE_COMPL);
1773 }
Harald Welte7584aea2009-02-11 11:44:12 +00001774
Harald Welte09e38af2009-02-16 22:52:23 +00001775 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001776 call->called_subscr = called_subscr;
1777
Harald Weltebe143102009-06-10 11:21:55 +08001778 /* Start paging subscriber on all BTS in LAC of subscriber */
Holger Freyther04866d42009-03-31 04:35:19 +02001779 subscr_get_channel(called_subscr, msg->trx->bts->network, RSL_CHANNEED_TCH_F,
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +02001780 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001781
1782 /* send a CALL PROCEEDING message to the MO */
1783 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1784 GSM48_MT_CC_CALL_PROC);
1785
Harald Welte49f48b82009-02-17 15:29:33 +00001786 if (is_ipaccess_bts(msg->trx->bts))
1787 rsl_ipacc_bind(msg->lchan);
1788
Harald Welte7584aea2009-02-11 11:44:12 +00001789 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001790 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001791
1792err:
1793 /* FIXME: send some kind of RELEASE */
1794 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001795}
Harald Welte7584aea2009-02-11 11:44:12 +00001796
Harald Welte7ccf7782009-02-17 01:43:01 +00001797static int gsm48_cc_rx_alerting(struct msgb *msg)
1798{
1799 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001800
Harald Welte4b458152009-02-17 13:23:06 +00001801 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001802
1803 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001804 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001805 return -EIO;
1806
Harald Welte4b458152009-02-17 13:23:06 +00001807 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001808 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001809 GSM48_MT_CC_ALERTING);
1810}
1811
Harald Welte49f48b82009-02-17 15:29:33 +00001812/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001813static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001814{
Harald Welte11fa29c2009-02-19 17:24:39 +00001815 struct gsm_bts *bts = lchan->ts->trx->bts;
1816 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001817 struct gsm_bts_trx_ts *ts;
1818
Harald Welte11fa29c2009-02-19 17:24:39 +00001819 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1820 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1821 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1822
1823 if (bts->type != remote_bts->type) {
1824 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1825 return -EINVAL;
1826 }
Harald Welte49f48b82009-02-17 15:29:33 +00001827
Harald Welte11fa29c2009-02-19 17:24:39 +00001828 switch (bts->type) {
1829 case GSM_BTS_TYPE_NANOBTS_900:
1830 case GSM_BTS_TYPE_NANOBTS_1800:
1831 ts = remote_lchan->ts;
1832 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1833 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1834
1835 ts = lchan->ts;
1836 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1837 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1838 break;
1839 case GSM_BTS_TYPE_BS11:
1840 trau_mux_map_lchan(lchan, remote_lchan);
1841 break;
1842 default:
1843 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1844 break;
1845 }
Harald Welte49f48b82009-02-17 15:29:33 +00001846
1847 return 0;
1848}
1849
Harald Welte7ccf7782009-02-17 01:43:01 +00001850static int gsm48_cc_rx_connect(struct msgb *msg)
1851{
1852 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001853 int rc;
1854
Harald Welte4b458152009-02-17 13:23:06 +00001855 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001856
1857 rc = tch_map(msg->lchan, call->remote_lchan);
1858 if (rc)
1859 return -EIO;
1860
1861 if (!call->remote_lchan)
1862 return -EIO;
1863
Harald Welte4b458152009-02-17 13:23:06 +00001864 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001865 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1866 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001867 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001868
Harald Welte49f48b82009-02-17 15:29:33 +00001869
1870 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001871 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001872 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001873 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001874}
1875
1876static int gsm48_cc_rx_disconnect(struct msgb *msg)
1877{
1878 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001879 int rc;
1880
1881
1882 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001883 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001884 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001885 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001886 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001887 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1888 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001889
1890 /*
1891 * FIXME: This looks wrong! We should have a single
1892 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1893 * to the NULL state and relase the call
1894 */
1895 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001896
1897 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001898 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001899 return -EIO;
1900
Harald Welte4b458152009-02-17 13:23:06 +00001901 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001902 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001903 GSM48_MT_CC_DISCONNECT);
1904}
1905
1906static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1907
Harald Welte49f48b82009-02-17 15:29:33 +00001908int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1909 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001910{
1911 struct msgb *msg = gsm48_msgb_alloc();
1912 struct gsm48_hdr *gh;
1913 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001914 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001915
Harald Welte7ccf7782009-02-17 01:43:01 +00001916 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001917
1918 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001919
1920 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001921 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001922
1923 gh->proto_discr = GSM48_PDISC_CC;
1924 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001925
Harald Welte7584aea2009-02-11 11:44:12 +00001926 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001927 if (calling_subscr) {
Harald Welte0c389302009-06-10 12:08:54 +08001928 bcd_lv[1] = 0xb9;
1929 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 1,
Harald Welte49f48b82009-02-17 15:29:33 +00001930 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001931 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1932 bcd_lv[0], bcd_lv+1);
1933 }
Harald Welte49f48b82009-02-17 15:29:33 +00001934 if (lchan->subscr) {
Harald Welte0c389302009-06-10 12:08:54 +08001935 bcd_lv[1] = 0xb9;
1936 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 1,
Harald Welte49f48b82009-02-17 15:29:33 +00001937 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001938 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1939 bcd_lv[0], bcd_lv+1);
1940 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001941
Harald Welte4b458152009-02-17 13:23:06 +00001942 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001943
1944 return gsm48_sendmsg(msg);
1945}
1946
Harald Welte4bc90a12008-12-27 16:32:52 +00001947static int gsm0408_rcv_cc(struct msgb *msg)
1948{
1949 struct gsm48_hdr *gh = msgb_l3(msg);
1950 u_int8_t msg_type = gh->msg_type & 0xbf;
1951 struct gsm_call *call = &msg->lchan->call;
1952 int rc = 0;
1953
1954 switch (msg_type) {
1955 case GSM48_MT_CC_CALL_CONF:
1956 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001957 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001958 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001959 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001960 break;
1961 case GSM48_MT_CC_RELEASE_COMPL:
1962 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001963 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001964 call->state = GSM_CSTATE_NULL;
1965 break;
1966 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001967 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001968 break;
1969 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001970 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001971 break;
1972 case GSM48_MT_CC_CONNECT_ACK:
1973 /* MO: Answer to CONNECT */
1974 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001975 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001976 break;
1977 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001978 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001979 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001980 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001981 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1982 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001983 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001984 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001985 break;
1986 case GSM48_MT_CC_STATUS_ENQ:
1987 rc = gsm48_cc_rx_status_enq(msg);
1988 break;
1989 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001990 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001991 break;
1992 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001993 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001994 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001995 break;
1996 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001997 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001998 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1999 break;
Harald Welte4a543e82009-02-28 13:17:55 +00002000 case GSM48_MT_CC_HOLD:
2001 DEBUGP(DCC, "-> HOLD (rejecting)\n");
2002 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
2003 GSM48_MT_CC_HOLD_REJ);
2004 break;
2005 case GSM48_MT_CC_RETR:
2006 DEBUGP(DCC, "-> RETR (rejecting)\n");
2007 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
2008 GSM48_MT_CC_RETR_REJ);
2009 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00002010 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00002011 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00002012 msg_type);
2013 break;
2014 }
2015
2016 return rc;
2017}
2018
Harald Welte52b1f982008-12-23 20:25:15 +00002019/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
2020int gsm0408_rcvmsg(struct msgb *msg)
2021{
2022 struct gsm48_hdr *gh = msgb_l3(msg);
2023 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002024 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00002025
2026 switch (pdisc) {
2027 case GSM48_PDISC_CC:
2028 rc = gsm0408_rcv_cc(msg);
2029 break;
2030 case GSM48_PDISC_MM:
2031 rc = gsm0408_rcv_mm(msg);
2032 break;
2033 case GSM48_PDISC_RR:
2034 rc = gsm0408_rcv_rr(msg);
2035 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002036 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00002037 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00002038 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002039 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002040 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00002041 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
2042 pdisc);
2043 break;
2044 default:
2045 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
2046 pdisc);
2047 break;
2048 }
2049
2050 return rc;
2051}
Harald Welte8470bf22008-12-25 23:28:35 +00002052
2053enum chreq_type {
2054 CHREQ_T_EMERG_CALL,
2055 CHREQ_T_CALL_REEST_TCH_F,
2056 CHREQ_T_CALL_REEST_TCH_H,
2057 CHREQ_T_CALL_REEST_TCH_H_DBL,
2058 CHREQ_T_SDCCH,
2059 CHREQ_T_TCH_F,
2060 CHREQ_T_VOICE_CALL_TCH_H,
2061 CHREQ_T_DATA_CALL_TCH_H,
2062 CHREQ_T_LOCATION_UPD,
2063 CHREQ_T_PAG_R_ANY,
2064 CHREQ_T_PAG_R_TCH_F,
2065 CHREQ_T_PAG_R_TCH_FH,
2066};
2067
2068/* Section 9.1.8 / Table 9.9 */
2069struct chreq {
2070 u_int8_t val;
2071 u_int8_t mask;
2072 enum chreq_type type;
2073};
2074
2075/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
2076static const struct chreq chreq_type_neci1[] = {
2077 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
2078 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
2079 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
2080 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
2081 { 0xe0, 0xe0, CHREQ_T_SDCCH },
2082 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
2083 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
2084 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
2085 { 0x10, 0xf0, CHREQ_T_SDCCH },
2086 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
2087 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
2088 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
2089};
2090
2091/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
2092static const struct chreq chreq_type_neci0[] = {
2093 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
2094 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
2095 { 0xe0, 0xe0, CHREQ_T_TCH_F },
2096 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
2097 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
2098 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
2099 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
2100 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
2101};
2102
2103static const enum gsm_chan_t ctype_by_chreq[] = {
2104 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
2105 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
2106 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
2107 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
2108 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
2109 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
2110 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
2111 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
2112 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
2113 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
2114 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
2115 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
2116};
2117
Harald Weltee14a57c2008-12-29 04:08:28 +00002118static const enum gsm_chreq_reason_t reason_by_chreq[] = {
2119 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
2120 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
2121 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
2122 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
2123 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
2124 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
2125 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
2126 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
2127 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
2128 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
2129 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
2130 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
2131};
2132
Harald Welte8470bf22008-12-25 23:28:35 +00002133enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
2134{
2135 int i;
2136 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
2137
2138 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
2139 const struct chreq *chr = &chreq_type_neci0[i];
2140 if ((ra & chr->mask) == chr->val)
2141 return ctype_by_chreq[chr->type];
2142 }
2143 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
2144 return GSM_LCHAN_SDCCH;
2145}
Harald Weltee14a57c2008-12-29 04:08:28 +00002146
2147enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
2148{
2149 int i;
2150 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
2151
2152 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
2153 const struct chreq *chr = &chreq_type_neci0[i];
2154 if ((ra & chr->mask) == chr->val)
2155 return reason_by_chreq[chr->type];
2156 }
2157 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
2158 return GSM_CHREQ_REASON_OTHER;
2159}