blob: d8fee8052fb6a9b49051df542dedf401b170f35f [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 Welte52b1f982008-12-23 20:25:15 +000045
Harald Welte8470bf22008-12-25 23:28:35 +000046#define GSM48_ALLOC_SIZE 1024
47#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000048
Harald Welte09e38af2009-02-16 22:52:23 +000049static const struct tlv_definition rsl_att_tlvdef = {
50 .def = {
51 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
52 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
53 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
54 [GSM48_IE_UTC] = { TLV_TYPE_TV },
55 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
56 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
57
58 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
59 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
60 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
61 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
62 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
63 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
64 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
65 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
66 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
67 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
68 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
69 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
70 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
73 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
76 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
77 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
78 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
79 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
80 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
81 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
82 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
83 /* FIXME: more elements */
84 },
85};
86
Harald Welte49f48b82009-02-17 15:29:33 +000087static inline int is_ipaccess_bts(struct gsm_bts *bts)
88{
89 switch (bts->type) {
90 case GSM_BTS_TYPE_NANOBTS_900:
91 case GSM_BTS_TYPE_NANOBTS_1800:
92 return 1;
93 default:
94 break;
95 }
96 return 0;
97}
Harald Welte09e38af2009-02-16 22:52:23 +000098
Harald Welte65e74cc2008-12-29 01:55:35 +000099static int gsm48_tx_simple(struct gsm_lchan *lchan,
100 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000101static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000102
Harald Welte52b1f982008-12-23 20:25:15 +0000103struct gsm_lai {
104 u_int16_t mcc;
105 u_int16_t mnc;
106 u_int16_t lac;
107};
108
Holger Freyther89824fc2008-12-30 16:18:18 +0000109static int authorize_everonye = 0;
110void gsm0408_allow_everyone(int everyone)
111{
112 printf("Allowing everyone?\n");
113 authorize_everonye = everyone;
114}
115
Holger Freythere97f7fb2008-12-31 18:52:11 +0000116static int reject_cause = 0;
117void gsm0408_set_reject_cause(int cause)
118{
119 reject_cause = cause;
120}
121
Holger Freyther73487a22008-12-31 18:53:57 +0000122static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
123 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000124{
125 if (!subscriber)
126 return 0;
127
Holger Freyther73487a22008-12-31 18:53:57 +0000128 /*
129 * Do not send accept yet as more information should arrive. Some
130 * phones will not send us the information and we will have to check
131 * what we want to do with that.
132 */
133 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
134 return 0;
135
Holger Freyther89824fc2008-12-30 16:18:18 +0000136 if (authorize_everonye)
137 return 1;
138
139 return subscriber->authorized;
140}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000141
Holger Freyther73487a22008-12-31 18:53:57 +0000142static void release_loc_updating_req(struct gsm_lchan *lchan)
143{
Harald Welte179f0642008-12-31 23:59:18 +0000144 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000145 return;
146
147 del_timer(&lchan->loc_operation->updating_timer);
148 free(lchan->loc_operation);
149 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000150 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000151}
152
153static void allocate_loc_updating_req(struct gsm_lchan *lchan)
154{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000155 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000156 release_loc_updating_req(lchan);
157
158 lchan->loc_operation = (struct gsm_loc_updating_operation *)
159 malloc(sizeof(*lchan->loc_operation));
160 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
161}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000162
Harald Welte52b1f982008-12-23 20:25:15 +0000163static void to_bcd(u_int8_t *bcd, u_int16_t val)
164{
Harald Welte4b634542008-12-27 01:55:51 +0000165 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000166 val = val / 10;
167 bcd[1] = val % 10;
168 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000169 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000170 val = val / 10;
171}
172
Holger Freyther17746612008-12-28 16:32:44 +0000173void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000174 u_int16_t mnc, u_int16_t lac)
175{
176 u_int8_t bcd[3];
177
178 to_bcd(bcd, mcc);
179 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
180 lai48->digits[1] = bcd[2];
181
182 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000183 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
184#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000185 lai48->digits[1] |= bcd[2] << 4;
186 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000187#else
188 lai48->digits[1] |= 0xf << 4;
189 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
190#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000191
Harald Welte4b634542008-12-27 01:55:51 +0000192 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000193}
194
Harald Welte255539c2008-12-28 02:26:27 +0000195#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000196#define MID_TMSI_LEN (TMSI_LEN + 2)
197
Harald Welte255539c2008-12-28 02:26:27 +0000198int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000199{
Harald Welte65e74cc2008-12-29 01:55:35 +0000200 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000201
Harald Welte4b634542008-12-27 01:55:51 +0000202 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000203 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000204 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000205 *tptr = htonl(tmsi);
206
207 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000208}
209
Harald Welte09e38af2009-02-16 22:52:23 +0000210static const char bcd_num_digits[] = {
211 '0', '1', '2', '3', '4', '5', '6', '7',
212 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
213};
214
215/* decode a 'called party BCD number' as in 10.5.4.7 */
216u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
217{
218 u_int8_t in_len = bcd_lv[0];
219 int i;
220
221 if (in_len < 1)
222 return 0;
223
224 for (i = 2; i <= in_len; i++) {
225 /* lower nibble */
226 output_len--;
227 if (output_len <= 1)
228 break;
229 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
230
231 /* higher nibble */
232 output_len--;
233 if (output_len <= 1)
234 break;
235 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
236 }
237 if (output_len >= 1)
238 *output++ = '\0';
239
240 /* return number type / calling plan */
241 return bcd_lv[1] & 0x3f;
242}
243
244/* convert a single ASCII character to call-control BCD */
245static int asc_to_bcd(const char asc)
246{
247 int i;
248
249 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
250 if (bcd_num_digits[i] == asc)
251 return i;
252 }
253 return -EINVAL;
254}
255
256/* convert a ASCII phone number to 'called party BCD number' */
257int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
258 u_int8_t type, const char *input)
259{
260 int in_len = strlen(input);
261 int i;
262 u_int8_t *bcd_cur = bcd_lv + 2;
263
264 if (in_len/2 + 1 > max_len)
265 return -EIO;
266
267 /* two digits per byte, plus type byte */
268 bcd_lv[0] = in_len/2 + 1;
269 if (in_len % 2)
270 bcd_lv[0]++;
271
272 /* if the caller wants to create a valid 'calling party BCD
273 * number', then the extension bit MUST NOT be set. For the
274 * 'called party BCD number' it MUST be set. *sigh */
275 bcd_lv[1] = type;
276
277 for (i = 0; i < in_len; i++) {
278 int rc = asc_to_bcd(input[i]);
279 if (rc < 0)
280 return rc;
281 if (i % 2 == 0)
282 *bcd_cur = rc;
283 else
284 *bcd_cur++ |= (rc << 4);
285 }
286 /* append padding nibble in case of odd length */
287 if (i % 2)
288 *bcd_cur++ |= 0xf0;
289
290 /* return how many bytes we used */
291 return (bcd_cur - bcd_lv);
292}
293
Holger Freyther819dd202009-01-04 03:52:50 +0000294struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000295{
296 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
297}
298
Holger Freyther3e2c3232009-01-04 03:55:31 +0000299int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000300{
Harald Welte65e74cc2008-12-29 01:55:35 +0000301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
302
303 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000304 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000305
Harald Welte65e74cc2008-12-29 01:55:35 +0000306 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
307 /* Send a 04.08 call control message, add transaction
308 * ID and TI flag */
309 gh->proto_discr |= msg->lchan->call.transaction_id;
310
311 /* GSM 04.07 Section 11.2.3.1.3 */
312 switch (msg->lchan->call.type) {
313 case GSM_CT_MO:
314 gh->proto_discr |= 0x80;
315 break;
316 case GSM_CT_MT:
317 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000318 case GSM_CT_NONE:
319 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000320 }
321 }
322 }
323
Harald Welte4b634542008-12-27 01:55:51 +0000324 msg->l3h = msg->data;
325
Harald Welte8470bf22008-12-25 23:28:35 +0000326 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000327}
328
Harald Welte52b1f982008-12-23 20:25:15 +0000329
Holger Freyther429e7762008-12-30 13:28:30 +0000330/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000331int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000332{
Harald Welte8470bf22008-12-25 23:28:35 +0000333 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000334 struct gsm48_hdr *gh;
335
Harald Welte8470bf22008-12-25 23:28:35 +0000336 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000337
338 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
339 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000340 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000341 gh->data[0] = cause;
342
Harald Weltedb253af2008-12-30 17:56:55 +0000343 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
344
Harald Welte65e74cc2008-12-29 01:55:35 +0000345 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000346}
347
348/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000349int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000350{
Harald Welte8470bf22008-12-25 23:28:35 +0000351 struct gsm_bts *bts = lchan->ts->trx->bts;
352 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000353 struct gsm48_hdr *gh;
354 struct gsm48_loc_area_id *lai;
355 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000356 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000357
Harald Welte8470bf22008-12-25 23:28:35 +0000358 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000359
360 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
361 gh->proto_discr = GSM48_PDISC_MM;
362 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
363
364 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000365 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000366 bts->network->network_code, bts->location_area_code);
367
368 mid = msgb_put(msg, MID_TMSI_LEN);
369 generate_mid_from_tmsi(mid, tmsi);
370
371 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
372
Harald Weltedb253af2008-12-30 17:56:55 +0000373 ret = gsm48_sendmsg(msg);
374
Harald Weltedb253af2008-12-30 17:56:55 +0000375 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000376
Holger Freyther07cc8d82008-12-29 06:23:46 +0000377 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000378}
379
Harald Weltefc977a82008-12-27 10:19:37 +0000380static char bcd2char(u_int8_t bcd)
381{
382 if (bcd < 0xa)
383 return '0' + bcd;
384 else
385 return 'A' + (bcd - 0xa);
386}
387
Harald Weltebf5e8df2009-02-03 12:59:45 +0000388/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000389static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
390{
391 int i;
392 u_int8_t mi_type;
393 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000394 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000395
396 mi_type = mi[0] & GSM_MI_TYPE_MASK;
397
398 switch (mi_type) {
399 case GSM_MI_TYPE_NONE:
400 break;
401 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000402 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
403 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
404 memcpy(&tmsi, &mi[1], 4);
405 tmsi = ntohl(tmsi);
406 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000407 }
408 break;
409 case GSM_MI_TYPE_IMSI:
410 case GSM_MI_TYPE_IMEI:
411 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000412 *str_cur++ = bcd2char(mi[0] >> 4);
413
414 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000415 if (str_cur + 2 >= string + str_len)
416 return str_cur - string;
417 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000418 /* skip last nibble in last input byte when GSM_EVEN */
419 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
420 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000421 }
422 break;
423 default:
424 break;
425 }
Harald Weltefc977a82008-12-27 10:19:37 +0000426 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000427
Harald Weltefc977a82008-12-27 10:19:37 +0000428 return str_cur - string;
429}
430
Harald Weltebf5e8df2009-02-03 12:59:45 +0000431/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000432static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
433{
434 struct msgb *msg = gsm48_msgb_alloc();
435 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000436
Harald Welte231ad4f2008-12-27 11:15:38 +0000437 msg->lchan = lchan;
438
439 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
440 gh->proto_discr = GSM48_PDISC_MM;
441 gh->msg_type = GSM48_MT_MM_ID_REQ;
442 gh->data[0] = id_type;
443
Harald Welte65e74cc2008-12-29 01:55:35 +0000444 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000445}
446
447#define MI_SIZE 32
448
Harald Weltebf5e8df2009-02-03 12:59:45 +0000449/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000450static int mm_rx_id_resp(struct msgb *msg)
451{
452 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000453 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000454 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
455 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000456 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000457
458 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000459 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000460 mi_type, mi_string);
461
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000462 /*
463 * Rogue messages could trick us but so is life
464 */
465 put_lchan(lchan);
466
Harald Welte75a983f2008-12-27 21:34:06 +0000467 switch (mi_type) {
468 case GSM_MI_TYPE_IMSI:
469 if (!lchan->subscr)
470 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000471 if (lchan->loc_operation)
472 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000473 break;
474 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000475 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000476 /* update subscribe <-> IMEI mapping */
477 if (lchan->subscr)
478 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000479 if (lchan->loc_operation)
480 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000481 break;
482 }
Holger Freyther73487a22008-12-31 18:53:57 +0000483
484 /* Check if we can let the mobile station enter */
485 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
486 db_subscriber_alloc_tmsi(lchan->subscr);
487 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
488 release_loc_updating_req(lchan);
489 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
490 }
491
Harald Welte75a983f2008-12-27 21:34:06 +0000492 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000493}
494
Harald Welte255539c2008-12-28 02:26:27 +0000495
496static void loc_upd_rej_cb(void *data)
497{
498 struct gsm_lchan *lchan = data;
499
Holger Freyther73487a22008-12-31 18:53:57 +0000500 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000501 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000502 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000503}
504
Holger Freytherb7193e42008-12-29 17:44:08 +0000505static void schedule_reject(struct gsm_lchan *lchan)
506{
Holger Freyther73487a22008-12-31 18:53:57 +0000507 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
508 lchan->loc_operation->updating_timer.data = lchan;
509 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000510}
511
Harald Welte231ad4f2008-12-27 11:15:38 +0000512#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000513/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000514static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000515{
Harald Welte8470bf22008-12-25 23:28:35 +0000516 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000517 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000518 struct gsm48_loc_upd_req *lu;
519 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000520 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000521 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000522 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000523 char mi_string[MI_SIZE];
524 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000525
Harald Welte8470bf22008-12-25 23:28:35 +0000526 lu = (struct gsm48_loc_upd_req *) gh->data;
527
528 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000529
Harald Weltefc977a82008-12-27 10:19:37 +0000530 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
531
Harald Welte231ad4f2008-12-27 11:15:38 +0000532 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000533
534 allocate_loc_updating_req(lchan);
535
Harald Welte52b1f982008-12-23 20:25:15 +0000536 switch (mi_type) {
537 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000538 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000539 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000540 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000541 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000542
Harald Welte52b1f982008-12-23 20:25:15 +0000543 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000544 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000545 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000546 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000547 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000548 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000549 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000550 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000551
Harald Welte52b1f982008-12-23 20:25:15 +0000552 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000553 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000554 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000555 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000556 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000557 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000558 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000559 }
560 break;
561 case GSM_MI_TYPE_IMEI:
562 case GSM_MI_TYPE_IMEISV:
563 /* no sim card... FIXME: what to do ? */
564 fprintf(stderr, "Unimplemented mobile identity type\n");
565 break;
566 default:
567 fprintf(stderr, "Unknown mobile identity type\n");
568 break;
569 }
570
Harald Welte255539c2008-12-28 02:26:27 +0000571 lchan->subscr = subscr;
572
Holger Freyther73487a22008-12-31 18:53:57 +0000573 /*
574 * Schedule the reject timer and check if we can let the
575 * subscriber into our network immediately or if we need to wait
576 * for identity responses.
577 */
578 schedule_reject(lchan);
579 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000580 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000581
Harald Welte75a983f2008-12-27 21:34:06 +0000582 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000583 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000584
Harald Welte255539c2008-12-28 02:26:27 +0000585 tmsi = strtoul(subscr->tmsi, NULL, 10);
586
Holger Freyther73487a22008-12-31 18:53:57 +0000587 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000588 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000589}
590
Harald Welte7584aea2009-02-11 11:44:12 +0000591/* 9.1.5 Channel mode modify */
592int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
593{
594 struct msgb *msg = gsm48_msgb_alloc();
595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
596 struct gsm48_chan_mode_modify *cmm =
597 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
598 u_int16_t arfcn = lchan->ts->trx->arfcn;
599
Harald Welte7ccf7782009-02-17 01:43:01 +0000600 DEBUGP(DRR, "-> CHANNEL MODE MODIFY\n");
601
Harald Welte7584aea2009-02-11 11:44:12 +0000602 msg->lchan = lchan;
603 gh->proto_discr = GSM48_PDISC_RR;
604 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
605
606 /* fill the channel information element, this code
607 * should probably be shared with rsl_rx_chan_rqd() */
608 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
609 cmm->chan_desc.h0.h = 0;
610 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
611 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
612 cmm->mode = mode;
613
614 return gsm48_sendmsg(msg);
615}
616
Harald Weltedb253af2008-12-30 17:56:55 +0000617/* Section 9.2.15a */
618int gsm48_tx_mm_info(struct gsm_lchan *lchan)
619{
620 struct msgb *msg = gsm48_msgb_alloc();
621 struct gsm48_hdr *gh;
622 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000623 u_int8_t *ptr8;
624 u_int16_t *ptr16;
625 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000626 int i;
627
628 msg->lchan = lchan;
629
630 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
631 gh->proto_discr = GSM48_PDISC_MM;
632 gh->msg_type = GSM48_MT_MM_INFO;
633
634 if (net->name_long) {
635 name_len = strlen(net->name_long);
636 /* 10.5.3.5a */
637 ptr8 = msgb_put(msg, 3);
638 ptr8[0] = GSM48_IE_NAME_LONG;
639 ptr8[1] = name_len*2 +1;
640 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
641
642 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
643 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000644 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000645
646 /* FIXME: Use Cell Broadcast, not UCS-2, since
647 * UCS-2 is only supported by later revisions of the spec */
648 }
649
650 if (net->name_short) {
651 name_len = strlen(net->name_short);
652 /* 10.5.3.5a */
653 ptr8 = (u_int8_t *) msgb_put(msg, 3);
654 ptr8[0] = GSM48_IE_NAME_LONG;
655 ptr8[1] = name_len*2 + 1;
656 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
657
Harald Weltee872cb12009-01-01 00:33:37 +0000658 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000659 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000660 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000661 }
662
663#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000664 /* move back to the top */
665 time_t cur_t;
666 struct tm* cur_time;
667 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000668 /* Section 10.5.3.9 */
669 cur_t = time(NULL);
670 cur_time = gmtime(cur_t);
671 ptr8 = msgb_put(msg, 8);
672 ptr8[0] = GSM48_IE_NET_TIME_TZ;
673 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
674 ptr8[2] = to_bcd8(cur_time->tm_mon);
675 ptr8[3] = to_bcd8(cur_time->tm_mday);
676 ptr8[4] = to_bcd8(cur_time->tm_hour);
677 ptr8[5] = to_bcd8(cur_time->tm_min);
678 ptr8[6] = to_bcd8(cur_time->tm_sec);
679 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
680 tz15min = (cur_time->tm_gmtoff)/(60*15);
681 ptr8[6] = to_bcd8(tz15min);
682 if (tz15min < 0)
683 ptr8[6] |= 0x80;
684#endif
685
686 return gsm48_sendmsg(msg);
687}
688
Harald Welte4b634542008-12-27 01:55:51 +0000689static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
690{
Harald Welte4b634542008-12-27 01:55:51 +0000691 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000692 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000693}
Harald Welteba4cf162009-01-10 01:49:35 +0000694
695/* 9.2.6 CM service reject */
696static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
697 enum gsm48_reject_value value)
698{
699 struct msgb *msg = gsm48_msgb_alloc();
700 struct gsm48_hdr *gh;
701
702 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
703
704 msg->lchan = lchan;
705 use_lchan(lchan);
706
707 gh->proto_discr = GSM48_PDISC_MM;
708 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
709 gh->data[0] = value;
710 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
711
712 return gsm48_sendmsg(msg);
713}
714
Harald Welte4ed0e922009-01-10 03:17:30 +0000715
716/*
717 * Handle CM Service Requests
718 * a) Verify that the packet is long enough to contain the information
719 * we require otherwsie reject with INCORRECT_MESSAGE
720 * b) Try to parse the TMSI. If we do not have one reject
721 * c) Check that we know the subscriber with the TMSI otherwise reject
722 * with a HLR cause
723 * d) Set the subscriber on the gsm_lchan and accept
724 */
Harald Welte4b634542008-12-27 01:55:51 +0000725static int gsm48_rx_mm_serv_req(struct msgb *msg)
726{
Harald Welteba4cf162009-01-10 01:49:35 +0000727 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000728 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000729
Harald Welteba4cf162009-01-10 01:49:35 +0000730 struct gsm_subscriber *subscr;
731 struct gsm48_hdr *gh = msgb_l3(msg);
732 struct gsm48_service_request *req =
733 (struct gsm48_service_request *)gh->data;
734
735 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
736 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
737 return gsm48_tx_mm_serv_rej(msg->lchan,
738 GSM48_REJECT_INCORRECT_MESSAGE);
739 }
740
741 if (msg->data_len < req->mi_len + 6) {
742 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
743 return gsm48_tx_mm_serv_rej(msg->lchan,
744 GSM48_REJECT_INCORRECT_MESSAGE);
745 }
746
747 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
748 if (mi_type != GSM_MI_TYPE_TMSI) {
749 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
750 return gsm48_tx_mm_serv_rej(msg->lchan,
751 GSM48_REJECT_INCORRECT_MESSAGE);
752 }
753
Harald Welte4ed0e922009-01-10 03:17:30 +0000754 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
755 subscr = subscr_get_by_tmsi(mi_string);
756 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
757 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000758
Harald Welte4ed0e922009-01-10 03:17:30 +0000759 if (!subscr)
760 return gsm48_tx_mm_serv_rej(msg->lchan,
761 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
762
763 if (!msg->lchan->subscr)
764 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000765 else if (msg->lchan->subscr != subscr) {
766 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
767 subscr_put(subscr);
768 }
769
Harald Welte4b634542008-12-27 01:55:51 +0000770 return gsm48_tx_mm_serv_ack(msg->lchan);
771}
772
Harald Weltebf5e8df2009-02-03 12:59:45 +0000773/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000774static int gsm0408_rcv_mm(struct msgb *msg)
775{
776 struct gsm48_hdr *gh = msgb_l3(msg);
777 int rc;
778
779 switch (gh->msg_type & 0xbf) {
780 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000781 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000782 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000783 break;
784 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000785 rc = mm_rx_id_resp(msg);
786 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000787 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000788 rc = gsm48_rx_mm_serv_req(msg);
789 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000790 case GSM48_MT_MM_STATUS:
791 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
792 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000793 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000794 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
795 msg->lchan->subscr ?
796 msg->lchan->subscr->imsi :
797 "unknown subscriber");
798 break;
799 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000800 case GSM48_MT_MM_AUTH_RESP:
801 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000802 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
803 gh->msg_type);
804 break;
805 default:
806 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
807 gh->msg_type);
808 break;
809 }
810
811 return rc;
812}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000813
Harald Welte2d35ae62009-02-06 12:02:13 +0000814/* Receive a PAGING RESPONSE message from the MS */
815static int gsm48_rr_rx_pag_resp(struct msgb *msg)
816{
817 struct gsm48_hdr *gh = msgb_l3(msg);
818 struct gsm48_paging_response *pr =
819 (struct gsm48_paging_response *) gh->data;
820 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
821 char mi_string[MI_SIZE];
822 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000823 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000824 int rc = 0;
825
826 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
827 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
828 mi_type, mi_string);
829 subscr = subscr_get_by_tmsi(mi_string);
830
831 if (!subscr) {
832 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000833 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000834 return -EINVAL;
835 }
836 DEBUGP(DRR, "<- Channel was requested by %s\n",
837 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000838
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000839 if (!msg->lchan->subscr)
840 msg->lchan->subscr = subscr;
841 else if (msg->lchan->subscr != subscr) {
842 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
843 subscr_put(subscr);
844 }
845
Harald Welte595ad7b2009-02-16 22:05:44 +0000846 sig_data.subscr = subscr;
847 sig_data.bts = msg->lchan->ts->trx->bts;
848 sig_data.lchan = msg->lchan;
849
850 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000851 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000852
Harald Welte7584aea2009-02-11 11:44:12 +0000853 /* FIXME: somehow signal the completion of the PAGING to
854 * the entity that requested the paging */
855
Harald Welte2d35ae62009-02-06 12:02:13 +0000856 return rc;
857}
858
Harald Weltebf5e8df2009-02-03 12:59:45 +0000859/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000860static int gsm0408_rcv_rr(struct msgb *msg)
861{
862 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000863 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000864
865 switch (gh->msg_type) {
866 case GSM48_MT_RR_CLSM_CHG:
867 DEBUGP(DRR, "CLASSMARK CHANGE\n");
868 /* FIXME: what to do ?!? */
869 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000870 case GSM48_MT_RR_GPRS_SUSP_REQ:
871 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
872 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000873 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000874 rc = gsm48_rr_rx_pag_resp(msg);
875 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000876 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
877 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
878 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000879 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000880 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000881 gh->msg_type);
882 break;
883 }
884
Harald Welte2d35ae62009-02-06 12:02:13 +0000885 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000886}
887
Holger Freythere64a7a32009-02-06 21:55:37 +0000888/* 7.1.7 and 9.1.7 Channel release*/
889int gsm48_send_rr_release(struct gsm_lchan *lchan)
890{
891 struct msgb *msg = gsm48_msgb_alloc();
892 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
893 u_int8_t *cause;
894
895 msg->lchan = lchan;
896 gh->proto_discr = GSM48_PDISC_RR;
897 gh->msg_type = GSM48_MT_RR_CHAN_REL;
898
899 cause = msgb_put(msg, 1);
900 cause[0] = GSM48_RR_CAUSE_NORMAL;
901
902 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
903 lchan->nr, lchan->type);
904
905 return gsm48_sendmsg(msg);
906}
907
Harald Welte4bc90a12008-12-27 16:32:52 +0000908/* Call Control */
909
Harald Welte7584aea2009-02-11 11:44:12 +0000910/* The entire call control code is written in accordance with Figure 7.10c
911 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
912 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
913 * it for voice */
914
Harald Welte4bc90a12008-12-27 16:32:52 +0000915static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
916{
917 struct msgb *msg = gsm48_msgb_alloc();
918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
919 u_int8_t *cause, *call_state;
920
Harald Welte65e74cc2008-12-29 01:55:35 +0000921 gh->proto_discr = GSM48_PDISC_CC;
922
Harald Welte4bc90a12008-12-27 16:32:52 +0000923 msg->lchan = lchan;
924
Harald Welte4bc90a12008-12-27 16:32:52 +0000925 gh->msg_type = GSM48_MT_CC_STATUS;
926
927 cause = msgb_put(msg, 3);
928 cause[0] = 2;
929 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
930 cause[2] = 0x80 | 30; /* response to status inquiry */
931
932 call_state = msgb_put(msg, 1);
933 call_state[0] = 0xc0 | 0x00;
934
Harald Welte65e74cc2008-12-29 01:55:35 +0000935 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000936}
937
Harald Welte6f4b7532008-12-29 00:39:37 +0000938static int gsm48_tx_simple(struct gsm_lchan *lchan,
939 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000940{
941 struct msgb *msg = gsm48_msgb_alloc();
942 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
943
944 msg->lchan = lchan;
945
Harald Welte6f4b7532008-12-29 00:39:37 +0000946 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000947 gh->msg_type = msg_type;
948
Harald Welte65e74cc2008-12-29 01:55:35 +0000949 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000950}
951
Harald Welte09e38af2009-02-16 22:52:23 +0000952/* call-back from paging the B-end of the connection */
953static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +0000954 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +0000955{
Harald Welte7ccf7782009-02-17 01:43:01 +0000956 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +0000957 struct gsm_call *remote_call = param;
958 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +0000959 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000960
961 if (hooknum != GSM_HOOK_RR_PAGING)
962 return -EINVAL;
963
964 switch (event) {
965 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000966 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +0000967 remote_call->remote_lchan = lchan;
968 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +0000969 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +0000970 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
971 if (is_ipaccess_bts(lchan->ts->trx->bts))
972 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +0000973 break;
974 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000975 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000976 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +0000977 /* FIXME: correct cause, etc */
978 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
979 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +0000980 break;
981 }
Harald Welte7ccf7782009-02-17 01:43:01 +0000982 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +0000983}
984
Harald Welte4bc90a12008-12-27 16:32:52 +0000985static int gsm48_cc_rx_status_enq(struct msgb *msg)
986{
987 return gsm48_cc_tx_status(msg->lchan);
988}
989
990static int gsm48_cc_rx_setup(struct msgb *msg)
991{
Harald Welte7584aea2009-02-11 11:44:12 +0000992 struct gsm_call *call = &msg->lchan->call;
993 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +0000994 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +0000995 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +0000996 char called_number[(43-2)*2 + 1] = "\0";
997 struct tlv_parsed tp;
998 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +0000999 int ret;
1000
1001 if (call->state == GSM_CSTATE_NULL ||
1002 call->state == GSM_CSTATE_RELEASE_REQ)
1003 use_lchan(msg->lchan);
1004
1005 call->type = GSM_CT_MO;
1006 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001007 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001008 call->transaction_id = gh->proto_discr & 0xf0;
1009
Harald Welte09e38af2009-02-16 22:52:23 +00001010 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
1011 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1012 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001013
Harald Welte09e38af2009-02-16 22:52:23 +00001014 /* Parse the number that was dialed and lookup subscriber */
1015 num_type = decode_bcd_number(called_number, sizeof(called_number),
1016 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001017
Harald Welte4b458152009-02-17 13:23:06 +00001018 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001019 called_number);
1020
Harald Welte09e38af2009-02-16 22:52:23 +00001021 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001022 if (!called_subscr) {
1023 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1024 put_lchan(msg->lchan);
1025 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001026 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001027 }
1028
Harald Welte09e38af2009-02-16 22:52:23 +00001029 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001030 call->called_subscr = called_subscr;
1031
Harald Welte7584aea2009-02-11 11:44:12 +00001032 /* start paging of the receiving end of the call */
Harald Welte09e38af2009-02-16 22:52:23 +00001033 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1034 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001035
1036 /* send a CALL PROCEEDING message to the MO */
1037 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1038 GSM48_MT_CC_CALL_PROC);
1039
Harald Welte49f48b82009-02-17 15:29:33 +00001040 if (is_ipaccess_bts(msg->trx->bts))
1041 rsl_ipacc_bind(msg->lchan);
1042
Harald Welte7584aea2009-02-11 11:44:12 +00001043 /* change TCH/F mode to voice */
1044 return gsm48_tx_chan_mode_modify(msg->lchan, 0x01);
Harald Welte09e38af2009-02-16 22:52:23 +00001045
1046err:
1047 /* FIXME: send some kind of RELEASE */
1048 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001049}
Harald Welte7584aea2009-02-11 11:44:12 +00001050
Harald Welte7ccf7782009-02-17 01:43:01 +00001051static int gsm48_cc_rx_alerting(struct msgb *msg)
1052{
1053 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001054
Harald Welte4b458152009-02-17 13:23:06 +00001055 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001056
1057 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001058 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001059 return -EIO;
1060
Harald Welte4b458152009-02-17 13:23:06 +00001061 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001062 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001063 GSM48_MT_CC_ALERTING);
1064}
1065
Harald Welte49f48b82009-02-17 15:29:33 +00001066/* map two ipaccess RTP streams onto each other */
1067static int ipacc_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1068{
1069 struct gsm_bts_trx_ts *ts;
1070
1071 ts = remote_lchan->ts;
1072 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1073 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1074
1075 ts = lchan->ts;
1076 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1077 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1078
1079 return 0;
1080}
1081
Harald Welte7ccf7782009-02-17 01:43:01 +00001082static int gsm48_cc_rx_connect(struct msgb *msg)
1083{
1084 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001085 int rc;
1086
Harald Welte4b458152009-02-17 13:23:06 +00001087 DEBUGP(DCC, "A -> CONNECT\n");
1088 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001089 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1090 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001091 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001092
Harald Welte49f48b82009-02-17 15:29:33 +00001093 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001094 return -EIO;
1095
Harald Welte49f48b82009-02-17 15:29:33 +00001096 if (is_ipaccess_bts(msg->trx->bts))
1097 ipacc_map(msg->lchan, call->remote_lchan);
1098
1099 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001100 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001101 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001102 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001103}
1104
1105static int gsm48_cc_rx_disconnect(struct msgb *msg)
1106{
1107 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001108 int rc;
1109
1110
1111 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001112 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001113 call->state = GSM_CSTATE_RELEASE_REQ;
1114 if (call->state != GSM_CSTATE_NULL)
1115 put_lchan(msg->lchan);
1116 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001117 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001118 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1119 GSM48_MT_CC_RELEASE);
1120
1121 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001122 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001123 return -EIO;
1124
Harald Welte4b458152009-02-17 13:23:06 +00001125 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001126 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001127 GSM48_MT_CC_DISCONNECT);
1128}
1129
1130static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1131
Harald Welte49f48b82009-02-17 15:29:33 +00001132int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1133 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001134{
1135 struct msgb *msg = gsm48_msgb_alloc();
1136 struct gsm48_hdr *gh;
1137 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001138 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001139
Harald Welte7ccf7782009-02-17 01:43:01 +00001140 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001141
1142 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001143
1144 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001145 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001146
1147 gh->proto_discr = GSM48_PDISC_CC;
1148 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001149
Harald Welte7584aea2009-02-11 11:44:12 +00001150 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001151 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001152 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001153 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001154 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1155 bcd_lv[0], bcd_lv+1);
1156 }
Harald Welte49f48b82009-02-17 15:29:33 +00001157 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001158 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001159 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001160 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1161 bcd_lv[0], bcd_lv+1);
1162 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001163
Harald Welte4b458152009-02-17 13:23:06 +00001164 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001165
1166 return gsm48_sendmsg(msg);
1167}
1168
Harald Welte4bc90a12008-12-27 16:32:52 +00001169static int gsm0408_rcv_cc(struct msgb *msg)
1170{
1171 struct gsm48_hdr *gh = msgb_l3(msg);
1172 u_int8_t msg_type = gh->msg_type & 0xbf;
1173 struct gsm_call *call = &msg->lchan->call;
1174 int rc = 0;
1175
1176 switch (msg_type) {
1177 case GSM48_MT_CC_CALL_CONF:
1178 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001179 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001180 /* we now need to MODIFY the channel */
1181 rc = gsm48_tx_chan_mode_modify(msg->lchan, 0x01);
Harald Welte4bc90a12008-12-27 16:32:52 +00001182 break;
1183 case GSM48_MT_CC_RELEASE_COMPL:
1184 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001185 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001186 call->state = GSM_CSTATE_NULL;
1187 break;
1188 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001189 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001190 break;
1191 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001192 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001193 break;
1194 case GSM48_MT_CC_CONNECT_ACK:
1195 /* MO: Answer to CONNECT */
1196 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001197 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001198 break;
1199 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001200 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001201 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001202 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001203 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1204 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte49f48b82009-02-17 15:29:33 +00001205 put_lchan(msg->lchan);
1206 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001207 break;
1208 case GSM48_MT_CC_STATUS_ENQ:
1209 rc = gsm48_cc_rx_status_enq(msg);
1210 break;
1211 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001212 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001213 break;
1214 case GSM48_MT_CC_SETUP:
Harald Welte7584aea2009-02-11 11:44:12 +00001215 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001216 break;
1217 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001218 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001219 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1220 break;
1221 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001222 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001223 msg_type);
1224 break;
1225 }
1226
1227 return rc;
1228}
1229
Harald Welte52b1f982008-12-23 20:25:15 +00001230/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1231int gsm0408_rcvmsg(struct msgb *msg)
1232{
1233 struct gsm48_hdr *gh = msgb_l3(msg);
1234 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001235 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001236
1237 switch (pdisc) {
1238 case GSM48_PDISC_CC:
1239 rc = gsm0408_rcv_cc(msg);
1240 break;
1241 case GSM48_PDISC_MM:
1242 rc = gsm0408_rcv_mm(msg);
1243 break;
1244 case GSM48_PDISC_RR:
1245 rc = gsm0408_rcv_rr(msg);
1246 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001247 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001248 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001249 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001250 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001251 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001252 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1253 pdisc);
1254 break;
1255 default:
1256 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1257 pdisc);
1258 break;
1259 }
1260
1261 return rc;
1262}
Harald Welte8470bf22008-12-25 23:28:35 +00001263
1264enum chreq_type {
1265 CHREQ_T_EMERG_CALL,
1266 CHREQ_T_CALL_REEST_TCH_F,
1267 CHREQ_T_CALL_REEST_TCH_H,
1268 CHREQ_T_CALL_REEST_TCH_H_DBL,
1269 CHREQ_T_SDCCH,
1270 CHREQ_T_TCH_F,
1271 CHREQ_T_VOICE_CALL_TCH_H,
1272 CHREQ_T_DATA_CALL_TCH_H,
1273 CHREQ_T_LOCATION_UPD,
1274 CHREQ_T_PAG_R_ANY,
1275 CHREQ_T_PAG_R_TCH_F,
1276 CHREQ_T_PAG_R_TCH_FH,
1277};
1278
1279/* Section 9.1.8 / Table 9.9 */
1280struct chreq {
1281 u_int8_t val;
1282 u_int8_t mask;
1283 enum chreq_type type;
1284};
1285
1286/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1287static const struct chreq chreq_type_neci1[] = {
1288 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1289 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1290 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1291 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1292 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1293 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1294 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1295 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1296 { 0x10, 0xf0, CHREQ_T_SDCCH },
1297 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1298 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1299 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1300};
1301
1302/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1303static const struct chreq chreq_type_neci0[] = {
1304 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1305 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1306 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1307 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1308 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1309 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1310 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1311 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1312};
1313
1314static const enum gsm_chan_t ctype_by_chreq[] = {
1315 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1316 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1317 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1318 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1319 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1320 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1321 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1322 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1323 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1324 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1325 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1326 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1327};
1328
Harald Weltee14a57c2008-12-29 04:08:28 +00001329static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1330 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1331 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1332 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1333 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1334 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1335 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1336 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1337 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1338 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1339 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1340 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1341 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1342};
1343
Harald Welte8470bf22008-12-25 23:28:35 +00001344enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1345{
1346 int i;
1347 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1348
1349 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1350 const struct chreq *chr = &chreq_type_neci0[i];
1351 if ((ra & chr->mask) == chr->val)
1352 return ctype_by_chreq[chr->type];
1353 }
1354 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1355 return GSM_LCHAN_SDCCH;
1356}
Harald Weltee14a57c2008-12-29 04:08:28 +00001357
1358enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1359{
1360 int i;
1361 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1362
1363 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1364 const struct chreq *chr = &chreq_type_neci0[i];
1365 if ((ra & chr->mask) == chr->val)
1366 return reason_by_chreq[chr->type];
1367 }
1368 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1369 return GSM_CHREQ_REASON_OTHER;
1370}