blob: 052991c3b3a759ecf4a44a7c6a6a717866b59fa4 [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
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001025 /*
1026 * Rogue messages could trick us but so is life
1027 */
1028 put_lchan(lchan);
1029
Harald Welte75a983f2008-12-27 21:34:06 +00001030 switch (mi_type) {
1031 case GSM_MI_TYPE_IMSI:
1032 if (!lchan->subscr)
1033 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001034 if (lchan->loc_operation)
1035 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001036 break;
1037 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +00001038 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +00001039 /* update subscribe <-> IMEI mapping */
1040 if (lchan->subscr)
1041 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001042 if (lchan->loc_operation)
1043 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001044 break;
1045 }
Holger Freyther73487a22008-12-31 18:53:57 +00001046
1047 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +00001048 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001049}
1050
Harald Welte255539c2008-12-28 02:26:27 +00001051
1052static void loc_upd_rej_cb(void *data)
1053{
1054 struct gsm_lchan *lchan = data;
1055
Holger Freyther73487a22008-12-31 18:53:57 +00001056 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +00001057 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001058 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001059}
1060
Holger Freytherb7193e42008-12-29 17:44:08 +00001061static void schedule_reject(struct gsm_lchan *lchan)
1062{
Holger Freyther73487a22008-12-31 18:53:57 +00001063 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1064 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001065 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001066}
1067
Harald Welte2a139372009-02-22 21:14:55 +00001068static const char *lupd_name(u_int8_t type)
1069{
1070 switch (type) {
1071 case GSM48_LUPD_NORMAL:
1072 return "NORMAL";
1073 case GSM48_LUPD_PERIODIC:
1074 return "PEROIDOC";
1075 case GSM48_LUPD_IMSI_ATT:
1076 return "IMSI ATTACH";
1077 default:
1078 return "UNKNOWN";
1079 }
1080}
1081
Harald Welte231ad4f2008-12-27 11:15:38 +00001082#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +00001083/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001084static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001085{
Harald Welte8470bf22008-12-25 23:28:35 +00001086 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001087 struct gsm48_loc_upd_req *lu;
1088 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +00001089 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +00001090 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +00001091 char mi_string[MI_SIZE];
1092 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001093
Harald Welte8470bf22008-12-25 23:28:35 +00001094 lu = (struct gsm48_loc_upd_req *) gh->data;
1095
1096 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001097
Harald Weltefc977a82008-12-27 10:19:37 +00001098 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
1099
Holger Freyther79f4ae62009-06-02 03:25:04 +00001100 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001101 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001102
Holger Freythereaf04692009-06-06 13:54:44 +00001103 /*
1104 * Pseudo Spoof detection: Just drop a second/concurrent
1105 * location updating request.
1106 */
1107 if (lchan->loc_operation) {
1108 DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n",
1109 lchan->loc_operation);
1110 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1111 return 0;
1112 }
1113
Holger Freyther73487a22008-12-31 18:53:57 +00001114 allocate_loc_updating_req(lchan);
1115
Harald Welte52b1f982008-12-23 20:25:15 +00001116 switch (mi_type) {
1117 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +00001118 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001119 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +00001120 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001121 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001122
Harald Welte52b1f982008-12-23 20:25:15 +00001123 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +00001124 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +00001125 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001126 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +00001127 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001128 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +00001129 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001130 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001131
Harald Welte52b1f982008-12-23 20:25:15 +00001132 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +00001133 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001134 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001135 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001136 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001137 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001138 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001139 }
1140 break;
1141 case GSM_MI_TYPE_IMEI:
1142 case GSM_MI_TYPE_IMEISV:
1143 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +00001144 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001145 break;
1146 default:
Harald Welte2a139372009-02-22 21:14:55 +00001147 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001148 break;
1149 }
1150
Harald Welte255539c2008-12-28 02:26:27 +00001151 lchan->subscr = subscr;
1152
Holger Freyther73487a22008-12-31 18:53:57 +00001153 /*
1154 * Schedule the reject timer and check if we can let the
1155 * subscriber into our network immediately or if we need to wait
1156 * for identity responses.
1157 */
1158 schedule_reject(lchan);
Holger Freytherd51524f2009-06-09 08:27:07 +00001159 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001160}
1161
Harald Welte7584aea2009-02-11 11:44:12 +00001162/* 9.1.5 Channel mode modify */
1163int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1164{
1165 struct msgb *msg = gsm48_msgb_alloc();
1166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1167 struct gsm48_chan_mode_modify *cmm =
1168 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001169 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001170
Harald Welte4a543e82009-02-28 13:17:55 +00001171 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001172
Harald Welte45b407a2009-05-23 15:51:12 +00001173 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001174 msg->lchan = lchan;
1175 gh->proto_discr = GSM48_PDISC_RR;
1176 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1177
1178 /* fill the channel information element, this code
1179 * should probably be shared with rsl_rx_chan_rqd() */
1180 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001181 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001182 cmm->chan_desc.h0.h = 0;
1183 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1184 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1185 cmm->mode = mode;
1186
1187 return gsm48_sendmsg(msg);
1188}
1189
Harald Weltedb253af2008-12-30 17:56:55 +00001190/* Section 9.2.15a */
1191int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1192{
1193 struct msgb *msg = gsm48_msgb_alloc();
1194 struct gsm48_hdr *gh;
1195 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001196 u_int8_t *ptr8;
1197 u_int16_t *ptr16;
1198 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +00001199 int i;
1200
1201 msg->lchan = lchan;
1202
1203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1204 gh->proto_discr = GSM48_PDISC_MM;
1205 gh->msg_type = GSM48_MT_MM_INFO;
1206
1207 if (net->name_long) {
1208 name_len = strlen(net->name_long);
1209 /* 10.5.3.5a */
1210 ptr8 = msgb_put(msg, 3);
1211 ptr8[0] = GSM48_IE_NAME_LONG;
1212 ptr8[1] = name_len*2 +1;
1213 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1214
1215 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1216 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001217 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001218
1219 /* FIXME: Use Cell Broadcast, not UCS-2, since
1220 * UCS-2 is only supported by later revisions of the spec */
1221 }
1222
1223 if (net->name_short) {
1224 name_len = strlen(net->name_short);
1225 /* 10.5.3.5a */
1226 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1227 ptr8[0] = GSM48_IE_NAME_LONG;
1228 ptr8[1] = name_len*2 + 1;
1229 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1230
Harald Weltee872cb12009-01-01 00:33:37 +00001231 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001232 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001233 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001234 }
1235
1236#if 0
Holger Freytherca362a62009-01-04 21:05:01 +00001237 /* move back to the top */
1238 time_t cur_t;
1239 struct tm* cur_time;
1240 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +00001241 /* Section 10.5.3.9 */
1242 cur_t = time(NULL);
1243 cur_time = gmtime(cur_t);
1244 ptr8 = msgb_put(msg, 8);
1245 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1246 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1247 ptr8[2] = to_bcd8(cur_time->tm_mon);
1248 ptr8[3] = to_bcd8(cur_time->tm_mday);
1249 ptr8[4] = to_bcd8(cur_time->tm_hour);
1250 ptr8[5] = to_bcd8(cur_time->tm_min);
1251 ptr8[6] = to_bcd8(cur_time->tm_sec);
1252 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1253 tz15min = (cur_time->tm_gmtoff)/(60*15);
1254 ptr8[6] = to_bcd8(tz15min);
1255 if (tz15min < 0)
1256 ptr8[6] |= 0x80;
1257#endif
1258
1259 return gsm48_sendmsg(msg);
1260}
1261
Harald Welte4b634542008-12-27 01:55:51 +00001262static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1263{
Harald Welte4b634542008-12-27 01:55:51 +00001264 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001265 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001266}
Harald Welteba4cf162009-01-10 01:49:35 +00001267
1268/* 9.2.6 CM service reject */
1269static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1270 enum gsm48_reject_value value)
1271{
1272 struct msgb *msg = gsm48_msgb_alloc();
1273 struct gsm48_hdr *gh;
1274
1275 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1276
1277 msg->lchan = lchan;
1278 use_lchan(lchan);
1279
1280 gh->proto_discr = GSM48_PDISC_MM;
1281 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1282 gh->data[0] = value;
1283 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1284
1285 return gsm48_sendmsg(msg);
1286}
1287
Harald Welte4ed0e922009-01-10 03:17:30 +00001288
1289/*
1290 * Handle CM Service Requests
1291 * a) Verify that the packet is long enough to contain the information
1292 * we require otherwsie reject with INCORRECT_MESSAGE
1293 * b) Try to parse the TMSI. If we do not have one reject
1294 * c) Check that we know the subscriber with the TMSI otherwise reject
1295 * with a HLR cause
1296 * d) Set the subscriber on the gsm_lchan and accept
1297 */
Harald Welte4b634542008-12-27 01:55:51 +00001298static int gsm48_rx_mm_serv_req(struct msgb *msg)
1299{
Harald Welteba4cf162009-01-10 01:49:35 +00001300 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +00001301 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001302
Harald Welteba4cf162009-01-10 01:49:35 +00001303 struct gsm_subscriber *subscr;
1304 struct gsm48_hdr *gh = msgb_l3(msg);
1305 struct gsm48_service_request *req =
1306 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001307 /* unfortunately in Phase1 the classmar2 length is variable */
1308 u_int8_t classmark2_len = gh->data[1];
1309 u_int8_t *classmark2 = gh->data+2;
1310 u_int8_t mi_len = *(classmark2 + classmark2_len);
1311 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001312
Harald Weltec9e02182009-05-01 19:07:53 +00001313 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001314 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001315 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001316 return gsm48_tx_mm_serv_rej(msg->lchan,
1317 GSM48_REJECT_INCORRECT_MESSAGE);
1318 }
1319
1320 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001321 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001322 return gsm48_tx_mm_serv_rej(msg->lchan,
1323 GSM48_REJECT_INCORRECT_MESSAGE);
1324 }
1325
Harald Weltec9e02182009-05-01 19:07:53 +00001326 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001327 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001328 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001329 return gsm48_tx_mm_serv_rej(msg->lchan,
1330 GSM48_REJECT_INCORRECT_MESSAGE);
1331 }
1332
Harald Weltec9e02182009-05-01 19:07:53 +00001333 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001334 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001335 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001336
Holger Freythereb443982009-06-04 13:58:42 +00001337 subscr = subscr_get_by_tmsi(mi_string);
1338
Harald Welte2a139372009-02-22 21:14:55 +00001339 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001340 if (!subscr)
1341 return gsm48_tx_mm_serv_rej(msg->lchan,
1342 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1343
1344 if (!msg->lchan->subscr)
1345 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001346 else if (msg->lchan->subscr != subscr) {
1347 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1348 subscr_put(subscr);
1349 }
1350
Harald Weltef7c43522009-06-09 20:24:21 +00001351 subscr->classmark2_len = classmark2_len;
1352 memcpy(subscr->classmark2, classmark2, classmark2_len);
1353
Harald Welte4b634542008-12-27 01:55:51 +00001354 return gsm48_tx_mm_serv_ack(msg->lchan);
1355}
1356
Harald Welte2a139372009-02-22 21:14:55 +00001357static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1358{
1359 struct gsm48_hdr *gh = msgb_l3(msg);
1360 struct gsm48_imsi_detach_ind *idi =
1361 (struct gsm48_imsi_detach_ind *) gh->data;
1362 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
1363 char mi_string[MI_SIZE];
1364 struct gsm_subscriber *subscr;
1365
1366 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
1367 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1368 mi_type, mi_string);
1369
1370 switch (mi_type) {
1371 case GSM_MI_TYPE_TMSI:
1372 subscr = subscr_get_by_tmsi(mi_string);
1373 break;
1374 case GSM_MI_TYPE_IMSI:
1375 subscr = subscr_get_by_imsi(mi_string);
1376 break;
1377 case GSM_MI_TYPE_IMEI:
1378 case GSM_MI_TYPE_IMEISV:
1379 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001380 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001381 break;
1382 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001383 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001384 break;
1385 }
1386
Holger Freyther4a49e772009-04-12 05:37:29 +00001387 if (subscr) {
1388 subscr_update(subscr, msg->trx->bts,
1389 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001390 DEBUGP(DMM, "Subscriber: %s\n",
1391 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001392 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001393 } else
Harald Welte2a139372009-02-22 21:14:55 +00001394 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1395
1396 put_lchan(msg->lchan);
1397
1398 return 0;
1399}
1400
Harald Welted2a7f5a2009-06-05 20:08:20 +00001401static int gsm48_rx_mm_status(struct msgb *msg)
1402{
1403 struct gsm48_hdr *gh = msgb_l3(msg);
1404
1405 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1406
1407 return 0;
1408}
1409
Harald Weltebf5e8df2009-02-03 12:59:45 +00001410/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001411static int gsm0408_rcv_mm(struct msgb *msg)
1412{
1413 struct gsm48_hdr *gh = msgb_l3(msg);
1414 int rc;
1415
1416 switch (gh->msg_type & 0xbf) {
1417 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +00001418 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001419 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001420 break;
1421 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001422 rc = mm_rx_id_resp(msg);
1423 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001424 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001425 rc = gsm48_rx_mm_serv_req(msg);
1426 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001427 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001428 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001429 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001430 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001431 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1432 msg->lchan->subscr ?
1433 msg->lchan->subscr->imsi :
1434 "unknown subscriber");
1435 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001436 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001437 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1438 break;
1439 case GSM48_MT_MM_CM_REEST_REQ:
1440 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1441 break;
1442 case GSM48_MT_MM_AUTH_RESP:
1443 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001444 break;
1445 default:
1446 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1447 gh->msg_type);
1448 break;
1449 }
1450
1451 return rc;
1452}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001453
Harald Welte2d35ae62009-02-06 12:02:13 +00001454/* Receive a PAGING RESPONSE message from the MS */
1455static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1456{
1457 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001458 u_int8_t *classmark2_lv = gh->data + 1;
1459 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1460 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001461 char mi_string[MI_SIZE];
1462 struct gsm_subscriber *subscr;
Harald Weltebe143102009-06-10 11:21:55 +08001463 struct gsm_bts *bts;
Harald Welte595ad7b2009-02-16 22:05:44 +00001464 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001465 int rc = 0;
1466
Harald Welte61548982009-02-22 21:26:29 +00001467 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001468 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1469 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001470 switch (mi_type) {
1471 case GSM_MI_TYPE_TMSI:
1472 subscr = subscr_get_by_tmsi(mi_string);
1473 break;
1474 case GSM_MI_TYPE_IMSI:
1475 subscr = subscr_get_by_imsi(mi_string);
1476 break;
1477 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001478
1479 if (!subscr) {
1480 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001481 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001482 return -EINVAL;
1483 }
1484 DEBUGP(DRR, "<- Channel was requested by %s\n",
1485 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001486
Harald Weltef7c43522009-06-09 20:24:21 +00001487 subscr->classmark2_len = *classmark2_lv;
1488 memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv);
1489
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001490 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001491 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001492 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001493 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1494 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001495 return -EINVAL;
1496 } else {
1497 DEBUGP(DRR, "<- Channel already owned by us\n");
1498 subscr_put(subscr);
1499 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001500 }
1501
Harald Welte595ad7b2009-02-16 22:05:44 +00001502 sig_data.subscr = subscr;
1503 sig_data.bts = msg->lchan->ts->trx->bts;
1504 sig_data.lchan = msg->lchan;
1505
1506 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001507
1508 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001509 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001510
Harald Weltebe143102009-06-10 11:21:55 +08001511 /* Stop paging on all other bts' */
1512 bts = NULL;
1513 do {
1514 bts = gsm_bts_by_lac(msg->trx->bts->network, subscr->lac, bts);
1515 if (!bts)
1516 break;
1517 if (bts == msg->trx->bts)
1518 continue;
1519 /* Stop paging */
1520 paging_request_stop(bts, subscr, NULL);
1521 } while (1);
1522
Harald Welte7584aea2009-02-11 11:44:12 +00001523 /* FIXME: somehow signal the completion of the PAGING to
1524 * the entity that requested the paging */
1525
Harald Welte2d35ae62009-02-06 12:02:13 +00001526 return rc;
1527}
1528
Harald Weltef7c43522009-06-09 20:24:21 +00001529static int gsm48_rx_rr_classmark(struct msgb *msg)
1530{
1531 struct gsm48_hdr *gh = msgb_l3(msg);
1532 struct gsm_subscriber *subscr = msg->lchan->subscr;
1533 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1534 u_int8_t cm2_len, cm3_len = 0;
1535 u_int8_t *cm2, *cm3 = NULL;
1536
1537 DEBUGP(DRR, "CLASSMARK CHANGE ");
1538
1539 /* classmark 2 */
1540 cm2_len = gh->data[0];
1541 cm2 = &gh->data[1];
1542 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1543
1544 if (payload_len > cm2_len + 1) {
1545 /* we must have a classmark3 */
1546 if (gh->data[cm2_len+1] != 0x20) {
1547 DEBUGPC(DRR, "ERR CM3 TAG\n");
1548 return -EINVAL;
1549 }
1550 if (cm2_len > 3) {
1551 DEBUGPC(DRR, "CM2 too long!\n");
1552 return -EINVAL;
1553 }
1554
1555 cm3_len = gh->data[cm2_len+2];
1556 cm3 = &gh->data[cm2_len+3];
1557 if (cm3_len > 14) {
1558 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1559 return -EINVAL;
1560 }
1561 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1562 }
1563 if (subscr) {
1564 subscr->classmark2_len = cm2_len;
1565 memcpy(subscr->classmark2, cm2, cm2_len);
1566 if (cm3) {
1567 subscr->classmark3_len = cm3_len;
1568 memcpy(subscr->classmark3, cm3, cm3_len);
1569 }
1570 }
1571
1572 /* FIXME: store the classmark2/3 values with the equipment register */
1573
1574 return 0;
1575}
1576
Harald Weltecf5b3592009-05-01 18:28:42 +00001577static int gsm48_rx_rr_status(struct msgb *msg)
1578{
1579 struct gsm48_hdr *gh = msgb_l3(msg);
1580
1581 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1582 rr_cause_name(gh->data[0]));
1583
1584 return 0;
1585}
1586
Harald Weltef7c43522009-06-09 20:24:21 +00001587static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1588{
1589 struct gsm48_hdr *gh = msgb_l3(msg);
1590 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1591 static struct gsm_meas_rep meas_rep;
1592
1593 DEBUGP(DRR, "MEASUREMENT REPORT ");
1594 parse_meas_rep(&meas_rep, gh->data, payload_len);
1595 if (meas_rep.flags & MEAS_REP_F_DTX)
1596 DEBUGPC(DRR, "DTX ");
1597 if (meas_rep.flags & MEAS_REP_F_BA1)
1598 DEBUGPC(DRR, "BA1 ");
1599 if (!(meas_rep.flags & MEAS_REP_F_VALID))
1600 DEBUGPC(DRR, "NOT VALID ");
1601 else
1602 DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
1603 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1604 meas_rep.rxqual_sub);
1605
1606 DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell);
1607
1608 /* FIXME: put the results somwhere */
1609
1610 return 0;
1611}
1612
Harald Weltebf5e8df2009-02-03 12:59:45 +00001613/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001614static int gsm0408_rcv_rr(struct msgb *msg)
1615{
1616 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001617 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001618
1619 switch (gh->msg_type) {
1620 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001621 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001622 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001623 case GSM48_MT_RR_GPRS_SUSP_REQ:
1624 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1625 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001626 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001627 rc = gsm48_rr_rx_pag_resp(msg);
1628 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001629 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1630 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001631 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001632 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001633 case GSM48_MT_RR_STATUS:
1634 rc = gsm48_rx_rr_status(msg);
1635 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001636 case GSM48_MT_RR_MEAS_REP:
1637 rc = gsm48_rx_rr_meas_rep(msg);
1638 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001639 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001640 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001641 gh->msg_type);
1642 break;
1643 }
1644
Harald Welte2d35ae62009-02-06 12:02:13 +00001645 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001646}
1647
Holger Freythere64a7a32009-02-06 21:55:37 +00001648/* 7.1.7 and 9.1.7 Channel release*/
1649int gsm48_send_rr_release(struct gsm_lchan *lchan)
1650{
1651 struct msgb *msg = gsm48_msgb_alloc();
1652 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1653 u_int8_t *cause;
1654
1655 msg->lchan = lchan;
1656 gh->proto_discr = GSM48_PDISC_RR;
1657 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1658
1659 cause = msgb_put(msg, 1);
1660 cause[0] = GSM48_RR_CAUSE_NORMAL;
1661
1662 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1663 lchan->nr, lchan->type);
1664
1665 return gsm48_sendmsg(msg);
1666}
1667
Harald Welte4bc90a12008-12-27 16:32:52 +00001668/* Call Control */
1669
Harald Welte7584aea2009-02-11 11:44:12 +00001670/* The entire call control code is written in accordance with Figure 7.10c
1671 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1672 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1673 * it for voice */
1674
Harald Welte4bc90a12008-12-27 16:32:52 +00001675static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1676{
1677 struct msgb *msg = gsm48_msgb_alloc();
1678 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1679 u_int8_t *cause, *call_state;
1680
Harald Welte65e74cc2008-12-29 01:55:35 +00001681 gh->proto_discr = GSM48_PDISC_CC;
1682
Harald Welte4bc90a12008-12-27 16:32:52 +00001683 msg->lchan = lchan;
1684
Harald Welte4bc90a12008-12-27 16:32:52 +00001685 gh->msg_type = GSM48_MT_CC_STATUS;
1686
1687 cause = msgb_put(msg, 3);
1688 cause[0] = 2;
1689 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1690 cause[2] = 0x80 | 30; /* response to status inquiry */
1691
1692 call_state = msgb_put(msg, 1);
1693 call_state[0] = 0xc0 | 0x00;
1694
Harald Welte65e74cc2008-12-29 01:55:35 +00001695 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001696}
1697
Harald Welte6f4b7532008-12-29 00:39:37 +00001698static int gsm48_tx_simple(struct gsm_lchan *lchan,
1699 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001700{
1701 struct msgb *msg = gsm48_msgb_alloc();
1702 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1703
1704 msg->lchan = lchan;
1705
Harald Welte6f4b7532008-12-29 00:39:37 +00001706 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001707 gh->msg_type = msg_type;
1708
Harald Welte65e74cc2008-12-29 01:55:35 +00001709 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001710}
1711
Harald Welte09e38af2009-02-16 22:52:23 +00001712/* call-back from paging the B-end of the connection */
1713static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001714 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001715{
Harald Welte7ccf7782009-02-17 01:43:01 +00001716 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001717 struct gsm_call *remote_call = param;
1718 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001719 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001720
1721 if (hooknum != GSM_HOOK_RR_PAGING)
1722 return -EINVAL;
1723
1724 switch (event) {
1725 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001726 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001727 remote_call->remote_lchan = lchan;
1728 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001729 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001730 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1731 if (is_ipaccess_bts(lchan->ts->trx->bts))
1732 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001733 break;
1734 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001735 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001736 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001737 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001738 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001739 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001740 break;
1741 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001742 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001743}
1744
Harald Welte4bc90a12008-12-27 16:32:52 +00001745static int gsm48_cc_rx_status_enq(struct msgb *msg)
1746{
Harald Welte4a543e82009-02-28 13:17:55 +00001747 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001748 return gsm48_cc_tx_status(msg->lchan);
1749}
1750
1751static int gsm48_cc_rx_setup(struct msgb *msg)
1752{
Harald Welte7584aea2009-02-11 11:44:12 +00001753 struct gsm_call *call = &msg->lchan->call;
1754 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001756 struct gsm_subscriber *called_subscr;
Harald Weltebe143102009-06-10 11:21:55 +08001757 struct gsm_bts *bts;
Harald Welte09e38af2009-02-16 22:52:23 +00001758 char called_number[(43-2)*2 + 1] = "\0";
1759 struct tlv_parsed tp;
Harald Welte7584aea2009-02-11 11:44:12 +00001760 int ret;
1761
1762 if (call->state == GSM_CSTATE_NULL ||
1763 call->state == GSM_CSTATE_RELEASE_REQ)
1764 use_lchan(msg->lchan);
1765
1766 call->type = GSM_CT_MO;
1767 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001768 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001769 call->transaction_id = gh->proto_discr & 0xf0;
1770
Harald Weltea4d49e92009-05-23 06:39:58 +00001771 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte09e38af2009-02-16 22:52:23 +00001772 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1773 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001774
Harald Welte09e38af2009-02-16 22:52:23 +00001775 /* Parse the number that was dialed and lookup subscriber */
Harald Welte0c389302009-06-10 12:08:54 +08001776 decode_bcd_number(called_number, sizeof(called_number),
1777 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1, 1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001778
Harald Welte4b458152009-02-17 13:23:06 +00001779 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001780 called_number);
1781
Harald Welte09e38af2009-02-16 22:52:23 +00001782 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001783 if (!called_subscr) {
1784 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1785 put_lchan(msg->lchan);
1786 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001787 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001788 }
Harald Welte5a065df2009-02-22 21:13:18 +00001789 if (called_subscr == msg->lchan->subscr) {
1790 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1791 put_lchan(msg->lchan);
1792 subscr_put(called_subscr);
1793 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1794 GSM48_MT_CC_RELEASE_COMPL);
1795 }
Harald Welte7584aea2009-02-11 11:44:12 +00001796
Harald Welte09e38af2009-02-16 22:52:23 +00001797 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001798 call->called_subscr = called_subscr;
1799
Harald Weltebe143102009-06-10 11:21:55 +08001800 /* Start paging subscriber on all BTS in LAC of subscriber */
1801 bts = NULL;
1802 do {
1803 bts = gsm_bts_by_lac(msg->trx->bts->network,
1804 msg->lchan->subscr->lac, bts);
1805 if (!bts)
1806 break;
1807 /* Trigger paging */
1808 paging_request(bts, called_subscr, RSL_CHANNEED_TCH_F,
1809 setup_trig_pag_evt, call);
1810 } while (1);
Harald Welte7584aea2009-02-11 11:44:12 +00001811
1812 /* send a CALL PROCEEDING message to the MO */
1813 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1814 GSM48_MT_CC_CALL_PROC);
1815
Harald Welte49f48b82009-02-17 15:29:33 +00001816 if (is_ipaccess_bts(msg->trx->bts))
1817 rsl_ipacc_bind(msg->lchan);
1818
Harald Welte7584aea2009-02-11 11:44:12 +00001819 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001820 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001821
1822err:
1823 /* FIXME: send some kind of RELEASE */
1824 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001825}
Harald Welte7584aea2009-02-11 11:44:12 +00001826
Harald Welte7ccf7782009-02-17 01:43:01 +00001827static int gsm48_cc_rx_alerting(struct msgb *msg)
1828{
1829 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001830
Harald Welte4b458152009-02-17 13:23:06 +00001831 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001832
1833 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001834 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001835 return -EIO;
1836
Harald Welte4b458152009-02-17 13:23:06 +00001837 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001838 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001839 GSM48_MT_CC_ALERTING);
1840}
1841
Harald Welte49f48b82009-02-17 15:29:33 +00001842/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001843static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001844{
Harald Welte11fa29c2009-02-19 17:24:39 +00001845 struct gsm_bts *bts = lchan->ts->trx->bts;
1846 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001847 struct gsm_bts_trx_ts *ts;
1848
Harald Welte11fa29c2009-02-19 17:24:39 +00001849 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1850 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1851 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1852
1853 if (bts->type != remote_bts->type) {
1854 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1855 return -EINVAL;
1856 }
Harald Welte49f48b82009-02-17 15:29:33 +00001857
Harald Welte11fa29c2009-02-19 17:24:39 +00001858 switch (bts->type) {
1859 case GSM_BTS_TYPE_NANOBTS_900:
1860 case GSM_BTS_TYPE_NANOBTS_1800:
1861 ts = remote_lchan->ts;
1862 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1863 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1864
1865 ts = lchan->ts;
1866 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1867 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1868 break;
1869 case GSM_BTS_TYPE_BS11:
1870 trau_mux_map_lchan(lchan, remote_lchan);
1871 break;
1872 default:
1873 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1874 break;
1875 }
Harald Welte49f48b82009-02-17 15:29:33 +00001876
1877 return 0;
1878}
1879
Harald Welte7ccf7782009-02-17 01:43:01 +00001880static int gsm48_cc_rx_connect(struct msgb *msg)
1881{
1882 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001883 int rc;
1884
Harald Welte4b458152009-02-17 13:23:06 +00001885 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001886
1887 rc = tch_map(msg->lchan, call->remote_lchan);
1888 if (rc)
1889 return -EIO;
1890
1891 if (!call->remote_lchan)
1892 return -EIO;
1893
Harald Welte4b458152009-02-17 13:23:06 +00001894 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001895 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1896 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001897 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001898
Harald Welte49f48b82009-02-17 15:29:33 +00001899
1900 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001901 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001902 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001903 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001904}
1905
1906static int gsm48_cc_rx_disconnect(struct msgb *msg)
1907{
1908 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001909 int rc;
1910
1911
1912 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001913 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001914 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001915 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001916 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001917 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1918 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001919
1920 /*
1921 * FIXME: This looks wrong! We should have a single
1922 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1923 * to the NULL state and relase the call
1924 */
1925 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001926
1927 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001928 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001929 return -EIO;
1930
Harald Welte4b458152009-02-17 13:23:06 +00001931 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001932 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001933 GSM48_MT_CC_DISCONNECT);
1934}
1935
1936static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1937
Harald Welte49f48b82009-02-17 15:29:33 +00001938int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1939 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001940{
1941 struct msgb *msg = gsm48_msgb_alloc();
1942 struct gsm48_hdr *gh;
1943 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001944 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001945
Harald Welte7ccf7782009-02-17 01:43:01 +00001946 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001947
1948 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001949
1950 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001951 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001952
1953 gh->proto_discr = GSM48_PDISC_CC;
1954 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001955
Harald Welte7584aea2009-02-11 11:44:12 +00001956 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001957 if (calling_subscr) {
Harald Welte0c389302009-06-10 12:08:54 +08001958 bcd_lv[1] = 0xb9;
1959 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 1,
Harald Welte49f48b82009-02-17 15:29:33 +00001960 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001961 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1962 bcd_lv[0], bcd_lv+1);
1963 }
Harald Welte49f48b82009-02-17 15:29:33 +00001964 if (lchan->subscr) {
Harald Welte0c389302009-06-10 12:08:54 +08001965 bcd_lv[1] = 0xb9;
1966 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 1,
Harald Welte49f48b82009-02-17 15:29:33 +00001967 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001968 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1969 bcd_lv[0], bcd_lv+1);
1970 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001971
Harald Welte4b458152009-02-17 13:23:06 +00001972 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001973
1974 return gsm48_sendmsg(msg);
1975}
1976
Harald Welte4bc90a12008-12-27 16:32:52 +00001977static int gsm0408_rcv_cc(struct msgb *msg)
1978{
1979 struct gsm48_hdr *gh = msgb_l3(msg);
1980 u_int8_t msg_type = gh->msg_type & 0xbf;
1981 struct gsm_call *call = &msg->lchan->call;
1982 int rc = 0;
1983
1984 switch (msg_type) {
1985 case GSM48_MT_CC_CALL_CONF:
1986 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001987 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001988 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001989 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001990 break;
1991 case GSM48_MT_CC_RELEASE_COMPL:
1992 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001993 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001994 call->state = GSM_CSTATE_NULL;
1995 break;
1996 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001997 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001998 break;
1999 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00002000 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002001 break;
2002 case GSM48_MT_CC_CONNECT_ACK:
2003 /* MO: Answer to CONNECT */
2004 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00002005 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00002006 break;
2007 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00002008 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00002009 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00002010 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00002011 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
2012 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00002013 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00002014 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002015 break;
2016 case GSM48_MT_CC_STATUS_ENQ:
2017 rc = gsm48_cc_rx_status_enq(msg);
2018 break;
2019 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00002020 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002021 break;
2022 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00002023 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00002024 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002025 break;
2026 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00002027 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00002028 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
2029 break;
Harald Welte4a543e82009-02-28 13:17:55 +00002030 case GSM48_MT_CC_HOLD:
2031 DEBUGP(DCC, "-> HOLD (rejecting)\n");
2032 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
2033 GSM48_MT_CC_HOLD_REJ);
2034 break;
2035 case GSM48_MT_CC_RETR:
2036 DEBUGP(DCC, "-> RETR (rejecting)\n");
2037 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
2038 GSM48_MT_CC_RETR_REJ);
2039 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00002040 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00002041 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00002042 msg_type);
2043 break;
2044 }
2045
2046 return rc;
2047}
2048
Harald Welte52b1f982008-12-23 20:25:15 +00002049/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
2050int gsm0408_rcvmsg(struct msgb *msg)
2051{
2052 struct gsm48_hdr *gh = msgb_l3(msg);
2053 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002054 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00002055
2056 switch (pdisc) {
2057 case GSM48_PDISC_CC:
2058 rc = gsm0408_rcv_cc(msg);
2059 break;
2060 case GSM48_PDISC_MM:
2061 rc = gsm0408_rcv_mm(msg);
2062 break;
2063 case GSM48_PDISC_RR:
2064 rc = gsm0408_rcv_rr(msg);
2065 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002066 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00002067 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00002068 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002069 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002070 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00002071 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
2072 pdisc);
2073 break;
2074 default:
2075 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
2076 pdisc);
2077 break;
2078 }
2079
2080 return rc;
2081}
Harald Welte8470bf22008-12-25 23:28:35 +00002082
2083enum chreq_type {
2084 CHREQ_T_EMERG_CALL,
2085 CHREQ_T_CALL_REEST_TCH_F,
2086 CHREQ_T_CALL_REEST_TCH_H,
2087 CHREQ_T_CALL_REEST_TCH_H_DBL,
2088 CHREQ_T_SDCCH,
2089 CHREQ_T_TCH_F,
2090 CHREQ_T_VOICE_CALL_TCH_H,
2091 CHREQ_T_DATA_CALL_TCH_H,
2092 CHREQ_T_LOCATION_UPD,
2093 CHREQ_T_PAG_R_ANY,
2094 CHREQ_T_PAG_R_TCH_F,
2095 CHREQ_T_PAG_R_TCH_FH,
2096};
2097
2098/* Section 9.1.8 / Table 9.9 */
2099struct chreq {
2100 u_int8_t val;
2101 u_int8_t mask;
2102 enum chreq_type type;
2103};
2104
2105/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
2106static const struct chreq chreq_type_neci1[] = {
2107 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
2108 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
2109 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
2110 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
2111 { 0xe0, 0xe0, CHREQ_T_SDCCH },
2112 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
2113 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
2114 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
2115 { 0x10, 0xf0, CHREQ_T_SDCCH },
2116 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
2117 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
2118 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
2119};
2120
2121/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
2122static const struct chreq chreq_type_neci0[] = {
2123 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
2124 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
2125 { 0xe0, 0xe0, CHREQ_T_TCH_F },
2126 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
2127 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
2128 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
2129 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
2130 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
2131};
2132
2133static const enum gsm_chan_t ctype_by_chreq[] = {
2134 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
2135 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
2136 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
2137 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
2138 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
2139 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
2140 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
2141 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
2142 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
2143 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
2144 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
2145 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
2146};
2147
Harald Weltee14a57c2008-12-29 04:08:28 +00002148static const enum gsm_chreq_reason_t reason_by_chreq[] = {
2149 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
2150 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
2151 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
2152 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
2153 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
2154 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
2155 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
2156 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
2157 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
2158 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
2159 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
2160 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
2161};
2162
Harald Welte8470bf22008-12-25 23:28:35 +00002163enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
2164{
2165 int i;
2166 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
2167
2168 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
2169 const struct chreq *chr = &chreq_type_neci0[i];
2170 if ((ra & chr->mask) == chr->val)
2171 return ctype_by_chreq[chr->type];
2172 }
2173 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
2174 return GSM_LCHAN_SDCCH;
2175}
Harald Weltee14a57c2008-12-29 04:08:28 +00002176
2177enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
2178{
2179 int i;
2180 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
2181
2182 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
2183 const struct chreq *chr = &chreq_type_neci0[i];
2184 if ((ra & chr->mask) == chr->val)
2185 return reason_by_chreq[chr->type];
2186 }
2187 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
2188 return GSM_CHREQ_REASON_OTHER;
2189}