blob: 0f3e91d929a5ab9c65c8e3f5d01f50914a3b3192 [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
4/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freytherba4d28a2008-12-29 06:23:44 +00005 * (C) 2008 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 Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte75a983f2008-12-27 21:34:06 +000032#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000033#include <openbsc/msgb.h>
34#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000037#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/gsm_04_08.h>
39#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000040
Harald Welte8470bf22008-12-25 23:28:35 +000041#define GSM48_ALLOC_SIZE 1024
42#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000043
Harald Welte65e74cc2008-12-29 01:55:35 +000044static int gsm48_tx_simple(struct gsm_lchan *lchan,
45 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000046static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000047
Harald Welte52b1f982008-12-23 20:25:15 +000048struct gsm_lai {
49 u_int16_t mcc;
50 u_int16_t mnc;
51 u_int16_t lac;
52};
53
Holger Freyther07cc8d82008-12-29 06:23:46 +000054
55
Harald Welte52b1f982008-12-23 20:25:15 +000056static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
57{
58 u_int8_t dig[4];
59
60 /* MCC */
61 dig[1] = lai48->digits[0] & 0x0f;
62 dig[2] = lai48->digits[0] >> 4;
63 dig[3] = lai48->digits[1] & 0x0f;
64 lai->mcc = dig[3] * 100 + dig[2];
65
66 /* MNC */
67 dig[1] = lai48->digits[1] >> 4;
68 dig[2] = lai48->digits[2] & 0x0f;
69 dig[3] = lai48->digits[2] >> 4;
70 lai->mnc = dig[3] * 100 + dig[2];
71
72 lai->lac = lai48->lac;
73}
74
75static void to_bcd(u_int8_t *bcd, u_int16_t val)
76{
Harald Welte4b634542008-12-27 01:55:51 +000077 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000078 val = val / 10;
79 bcd[1] = val % 10;
80 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000081 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000082 val = val / 10;
83}
84
Holger Freyther17746612008-12-28 16:32:44 +000085void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +000086 u_int16_t mnc, u_int16_t lac)
87{
88 u_int8_t bcd[3];
89
90 to_bcd(bcd, mcc);
91 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
92 lai48->digits[1] = bcd[2];
93
94 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +000095 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
96#if 0
Harald Welte8470bf22008-12-25 23:28:35 +000097 lai48->digits[1] |= bcd[2] << 4;
98 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +000099#else
100 lai48->digits[1] |= 0xf << 4;
101 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
102#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000103
Harald Welte4b634542008-12-27 01:55:51 +0000104 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000105}
106
Harald Welte255539c2008-12-28 02:26:27 +0000107#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000108#define MID_TMSI_LEN (TMSI_LEN + 2)
109
Harald Welte255539c2008-12-28 02:26:27 +0000110int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000111{
Harald Welte65e74cc2008-12-29 01:55:35 +0000112 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000113
Harald Welte4b634542008-12-27 01:55:51 +0000114 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000115 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000116 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000117 *tptr = htonl(tmsi);
118
119 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000120}
121
Harald Welte8470bf22008-12-25 23:28:35 +0000122static struct msgb *gsm48_msgb_alloc(void)
123{
124 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
125}
126
Harald Welte65e74cc2008-12-29 01:55:35 +0000127static int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000128{
Harald Welte65e74cc2008-12-29 01:55:35 +0000129 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
130
131 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000132 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000133
Harald Welte65e74cc2008-12-29 01:55:35 +0000134 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
135 /* Send a 04.08 call control message, add transaction
136 * ID and TI flag */
137 gh->proto_discr |= msg->lchan->call.transaction_id;
138
139 /* GSM 04.07 Section 11.2.3.1.3 */
140 switch (msg->lchan->call.type) {
141 case GSM_CT_MO:
142 gh->proto_discr |= 0x80;
143 break;
144 case GSM_CT_MT:
145 break;
146 }
147 }
148 }
149
Harald Welte4b634542008-12-27 01:55:51 +0000150 msg->l3h = msg->data;
151
Harald Welte8470bf22008-12-25 23:28:35 +0000152 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000153}
154
Harald Welte52b1f982008-12-23 20:25:15 +0000155
Holger Freyther429e7762008-12-30 13:28:30 +0000156/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000157int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000158{
Harald Welte8470bf22008-12-25 23:28:35 +0000159 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000160 struct gsm48_hdr *gh;
161
Harald Welte8470bf22008-12-25 23:28:35 +0000162 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000163
164 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
165 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000166 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000167 gh->data[0] = cause;
168
Holger Freyther429e7762008-12-30 13:28:30 +0000169 DEBUGP(DMM, "-> LOCATION UPDATING REJECT\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000170
Harald Welte65e74cc2008-12-29 01:55:35 +0000171 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000172}
173
174/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000175int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000176{
Harald Welte8470bf22008-12-25 23:28:35 +0000177 struct gsm_bts *bts = lchan->ts->trx->bts;
178 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000179 struct gsm48_hdr *gh;
180 struct gsm48_loc_area_id *lai;
181 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000182 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000183
Harald Welte8470bf22008-12-25 23:28:35 +0000184 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000185
186 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
187 gh->proto_discr = GSM48_PDISC_MM;
188 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
189
190 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000191 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000192 bts->network->network_code, bts->location_area_code);
193
194 mid = msgb_put(msg, MID_TMSI_LEN);
195 generate_mid_from_tmsi(mid, tmsi);
196
Holger Freytherb7193e42008-12-29 17:44:08 +0000197 lchan->pending_update_request = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000198 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
199
Holger Freyther07cc8d82008-12-29 06:23:46 +0000200
201 /* inform the upper layer on the progress */
Holger Freytherb7193e42008-12-29 17:44:08 +0000202 if (bts->network->update_request)
203 (*bts->network->update_request)(bts, tmsi, 1);
Holger Freyther07cc8d82008-12-29 06:23:46 +0000204
205 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000206}
207
Harald Weltefc977a82008-12-27 10:19:37 +0000208static char bcd2char(u_int8_t bcd)
209{
210 if (bcd < 0xa)
211 return '0' + bcd;
212 else
213 return 'A' + (bcd - 0xa);
214}
215
216/* 10.5.1.4 */
217static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
218{
219 int i;
220 u_int8_t mi_type;
221 char *str_cur = string;
222
223 mi_type = mi[0] & GSM_MI_TYPE_MASK;
224
225 switch (mi_type) {
226 case GSM_MI_TYPE_NONE:
227 break;
228 case GSM_MI_TYPE_TMSI:
229 for (i = 1; i < mi_len - 1; i++) {
230 if (str_cur + 2 >= string + str_len)
231 return str_cur - string;
232 *str_cur++ = bcd2char(mi[i] >> 4);
233 *str_cur++ = bcd2char(mi[i] & 0xf);
234 }
235 break;
236 case GSM_MI_TYPE_IMSI:
237 case GSM_MI_TYPE_IMEI:
238 case GSM_MI_TYPE_IMEISV:
239 if (mi[0] & GSM_MI_ODD)
240 *str_cur++ = bcd2char(mi[0] >> 4);
241
242 for (i = 1; i < mi_len - 1; i++) {
243 if (str_cur + 2 >= string + str_len)
244 return str_cur - string;
245 *str_cur++ = bcd2char(mi[i] & 0xf);
246 *str_cur++ = bcd2char(mi[i] >> 4);
247 }
248 break;
249 default:
250 break;
251 }
252
253 *str_cur++ = '\0';
254 return str_cur - string;
255}
256
Harald Welte231ad4f2008-12-27 11:15:38 +0000257/* Chapter 9.2.10 */
258static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
259{
260 struct msgb *msg = gsm48_msgb_alloc();
261 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000262
Harald Welte231ad4f2008-12-27 11:15:38 +0000263 msg->lchan = lchan;
264
265 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
266 gh->proto_discr = GSM48_PDISC_MM;
267 gh->msg_type = GSM48_MT_MM_ID_REQ;
268 gh->data[0] = id_type;
269
Harald Welte65e74cc2008-12-29 01:55:35 +0000270 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000271}
272
273#define MI_SIZE 32
274
275/* Chapter 9.2.11 */
276static int mm_rx_id_resp(struct msgb *msg)
277{
278 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000279 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000280 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
281 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000282 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000283
284 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000285 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000286 mi_type, mi_string);
287
Harald Welte75a983f2008-12-27 21:34:06 +0000288 switch (mi_type) {
289 case GSM_MI_TYPE_IMSI:
290 if (!lchan->subscr)
291 lchan->subscr = db_create_subscriber(mi_string);
Holger Freytherb7193e42008-12-29 17:44:08 +0000292
Holger Freyther429e7762008-12-30 13:28:30 +0000293 /* We have a pending UPDATING REQUEST handle it now */
Holger Freytherb7193e42008-12-29 17:44:08 +0000294 if (lchan->pending_update_request) {
295 if (lchan->subscr->authorized) {
296 db_subscriber_alloc_tmsi(lchan->subscr);
297 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
298 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
299 } else {
300 schedule_reject(lchan);
301 }
Harald Welte75a983f2008-12-27 21:34:06 +0000302 }
303 break;
304 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000305 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000306 /* update subscribe <-> IMEI mapping */
307 if (lchan->subscr)
308 db_subscriber_assoc_imei(lchan->subscr, mi_string);
309 break;
310 }
311 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000312}
313
Harald Welte255539c2008-12-28 02:26:27 +0000314
315static void loc_upd_rej_cb(void *data)
316{
317 struct gsm_lchan *lchan = data;
318
319 gsm0408_loc_upd_rej(lchan, 0x16);
320 rsl_chan_release(lchan);
321}
322
Holger Freytherb7193e42008-12-29 17:44:08 +0000323static void schedule_reject(struct gsm_lchan *lchan)
324{
325 lchan->timer.cb = loc_upd_rej_cb;
326 lchan->timer.data = lchan;
327 lchan->pending_update_request = 0;
328 schedule_timer(&lchan->timer, 1, 0);
329}
330
Harald Welte231ad4f2008-12-27 11:15:38 +0000331#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000332/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000333static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000334{
Harald Welte8470bf22008-12-25 23:28:35 +0000335 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000336 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000337 struct gsm48_loc_upd_req *lu;
338 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000339 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000340 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000341 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000342 char mi_string[MI_SIZE];
343 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000344
Harald Welte8470bf22008-12-25 23:28:35 +0000345 lu = (struct gsm48_loc_upd_req *) gh->data;
346
347 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000348
Harald Weltefc977a82008-12-27 10:19:37 +0000349 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
350
Harald Welte231ad4f2008-12-27 11:15:38 +0000351 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000352 switch (mi_type) {
353 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000354 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000355 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000356 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000357 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000358 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000359 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000360 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000361 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000362 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000363 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000364 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000365 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000366 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000367 }
368 break;
369 case GSM_MI_TYPE_IMEI:
370 case GSM_MI_TYPE_IMEISV:
371 /* no sim card... FIXME: what to do ? */
372 fprintf(stderr, "Unimplemented mobile identity type\n");
373 break;
374 default:
375 fprintf(stderr, "Unknown mobile identity type\n");
376 break;
377 }
378
Harald Welte255539c2008-12-28 02:26:27 +0000379 lchan->subscr = subscr;
380
Holger Freytherb7193e42008-12-29 17:44:08 +0000381 /* we know who we deal with and don't want him */
382 if (subscr && !subscr->authorized) {
383 schedule_reject(lchan);
384 return 0;
385 } else if (!subscr) {
386 /* we have asked for the imsi and should get a
387 * IDENTITY RESPONSE */
388 lchan->pending_update_request = 1;
Harald Welte255539c2008-12-28 02:26:27 +0000389 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000390 }
391
Harald Welte75a983f2008-12-27 21:34:06 +0000392 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000393 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000394
Harald Welte255539c2008-12-28 02:26:27 +0000395 tmsi = strtoul(subscr->tmsi, NULL, 10);
396
397 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000398}
399
Harald Welte4b634542008-12-27 01:55:51 +0000400static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
401{
Harald Welte4b634542008-12-27 01:55:51 +0000402 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000403 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000404}
405
406static int gsm48_rx_mm_serv_req(struct msgb *msg)
407{
408 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000409 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000410
Harald Welte65e74cc2008-12-29 01:55:35 +0000411 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000412
Harald Welte4b634542008-12-27 01:55:51 +0000413 return gsm48_tx_mm_serv_ack(msg->lchan);
414}
415
Harald Welte52b1f982008-12-23 20:25:15 +0000416static int gsm0408_rcv_mm(struct msgb *msg)
417{
418 struct gsm48_hdr *gh = msgb_l3(msg);
419 int rc;
420
421 switch (gh->msg_type & 0xbf) {
422 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000423 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000424 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000425 break;
426 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000427 rc = mm_rx_id_resp(msg);
428 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000429 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000430 rc = gsm48_rx_mm_serv_req(msg);
431 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000432 case GSM48_MT_MM_STATUS:
433 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
434 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000435 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000436 case GSM48_MT_MM_TMSI_REALL_COMPL:
437 case GSM48_MT_MM_AUTH_RESP:
438 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000439 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
440 gh->msg_type);
441 break;
442 default:
443 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
444 gh->msg_type);
445 break;
446 }
447
448 return rc;
449}
450static int gsm0408_rcv_rr(struct msgb *msg)
451{
452 struct gsm48_hdr *gh = msgb_l3(msg);
453
454 switch (gh->msg_type) {
455 case GSM48_MT_RR_CLSM_CHG:
456 DEBUGP(DRR, "CLASSMARK CHANGE\n");
457 /* FIXME: what to do ?!? */
458 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000459 case GSM48_MT_RR_GPRS_SUSP_REQ:
460 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
461 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000462 case GSM48_MT_RR_PAG_RESP:
463 default:
464 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
465 gh->msg_type);
466 break;
467 }
468
469 return 0;
470}
471
Harald Welte4bc90a12008-12-27 16:32:52 +0000472/* Call Control */
473
Harald Welte4bc90a12008-12-27 16:32:52 +0000474static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
475{
476 struct msgb *msg = gsm48_msgb_alloc();
477 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
478 u_int8_t *cause, *call_state;
479
Harald Welte65e74cc2008-12-29 01:55:35 +0000480 gh->proto_discr = GSM48_PDISC_CC;
481
Harald Welte4bc90a12008-12-27 16:32:52 +0000482 msg->lchan = lchan;
483
Harald Welte4bc90a12008-12-27 16:32:52 +0000484 gh->msg_type = GSM48_MT_CC_STATUS;
485
486 cause = msgb_put(msg, 3);
487 cause[0] = 2;
488 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
489 cause[2] = 0x80 | 30; /* response to status inquiry */
490
491 call_state = msgb_put(msg, 1);
492 call_state[0] = 0xc0 | 0x00;
493
Harald Welte65e74cc2008-12-29 01:55:35 +0000494 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000495}
496
Harald Welte6f4b7532008-12-29 00:39:37 +0000497static int gsm48_tx_simple(struct gsm_lchan *lchan,
498 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000499{
500 struct msgb *msg = gsm48_msgb_alloc();
501 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
502
503 msg->lchan = lchan;
504
Harald Welte6f4b7532008-12-29 00:39:37 +0000505 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000506 gh->msg_type = msg_type;
507
Harald Welte65e74cc2008-12-29 01:55:35 +0000508 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000509}
510
511static int gsm48_cc_rx_status_enq(struct msgb *msg)
512{
513 return gsm48_cc_tx_status(msg->lchan);
514}
515
516static int gsm48_cc_rx_setup(struct msgb *msg)
517{
Harald Welte6f4b7532008-12-29 00:39:37 +0000518 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
519 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000520}
521
Harald Welte65e74cc2008-12-29 01:55:35 +0000522int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
523{
524 struct msgb *msg = gsm48_msgb_alloc();
525 struct gsm48_hdr *gh;
526 struct gsm_call *call = &lchan->call;
527
528 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
529
530 call->type = GSM_CT_MT;
531 msg->lchan = lchan;
532
533 gh->proto_discr = GSM48_PDISC_CC;
534 gh->msg_type = GSM48_MT_CC_SETUP;
535 gh->data[0] = 0x34;
536 gh->data[1] = 0x00;
537 gh->data[2] = 0x5c;
538 gh->data[3] = 0x04;
539 gh->data[4] = 0xb9;
540 gh->data[5] = 0x83;
541 gh->data[6] = 0x32;
542 gh->data[7] = 0x24;
543
544 DEBUGP(DCC, "Sending SETUP\n");
545
546 return gsm48_sendmsg(msg);
547}
548
Harald Welte4bc90a12008-12-27 16:32:52 +0000549static int gsm0408_rcv_cc(struct msgb *msg)
550{
551 struct gsm48_hdr *gh = msgb_l3(msg);
552 u_int8_t msg_type = gh->msg_type & 0xbf;
553 struct gsm_call *call = &msg->lchan->call;
Holger Freyther2eafef52008-12-29 06:42:17 +0000554 struct gsm_network *network = msg->lchan->ts->trx->bts->network;
Harald Welte4bc90a12008-12-27 16:32:52 +0000555 int rc = 0;
556
557 switch (msg_type) {
558 case GSM48_MT_CC_CALL_CONF:
559 /* Response to SETUP */
560 DEBUGP(DCC, "CALL CONFIRM\n");
561 break;
562 case GSM48_MT_CC_RELEASE_COMPL:
563 /* Answer from MS to RELEASE */
564 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
Holger Freytherb7193e42008-12-29 17:44:08 +0000565 if (network->call_state_changed)
566 (*network->call_state_changed)(msg->lchan, call->state);
Harald Welte4bc90a12008-12-27 16:32:52 +0000567 call->state = GSM_CSTATE_NULL;
568 break;
569 case GSM48_MT_CC_ALERTING:
570 DEBUGP(DCC, "ALERTING\n");
571 break;
572 case GSM48_MT_CC_CONNECT:
573 DEBUGP(DCC, "CONNECT\n");
574 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000575 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
576 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000577 break;
578 case GSM48_MT_CC_CONNECT_ACK:
579 /* MO: Answer to CONNECT */
580 call->state = GSM_CSTATE_ACTIVE;
581 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
582 break;
583 case GSM48_MT_CC_RELEASE:
584 DEBUGP(DCC, "RELEASE\n");
585 /* need to respond with RELEASE_COMPLETE */
586 break;
587 case GSM48_MT_CC_STATUS_ENQ:
588 rc = gsm48_cc_rx_status_enq(msg);
589 break;
590 case GSM48_MT_CC_DISCONNECT:
591 /* Section 5.4.3.2 */
592 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
593 call->state = GSM_CSTATE_RELEASE_REQ;
594 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000595 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
596 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000597 break;
598 case GSM48_MT_CC_SETUP:
599 call->type = GSM_CT_MO;
600 call->state = GSM_CSTATE_INITIATED;
601 call->transaction_id = gh->proto_discr & 0xf0;
602 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000603 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
604 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000605 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
606 break;
607 case GSM48_MT_CC_EMERG_SETUP:
608 DEBUGP(DCC, "EMERGENCY SETUP\n");
609 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
610 break;
611 default:
612 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
613 msg_type);
614 break;
615 }
616
617 return rc;
618}
619
Harald Welte52b1f982008-12-23 20:25:15 +0000620/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
621int gsm0408_rcvmsg(struct msgb *msg)
622{
623 struct gsm48_hdr *gh = msgb_l3(msg);
624 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000625 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000626
627 switch (pdisc) {
628 case GSM48_PDISC_CC:
629 rc = gsm0408_rcv_cc(msg);
630 break;
631 case GSM48_PDISC_MM:
632 rc = gsm0408_rcv_mm(msg);
633 break;
634 case GSM48_PDISC_RR:
635 rc = gsm0408_rcv_rr(msg);
636 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000637 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000638 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000639 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000640 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000641 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000642 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
643 pdisc);
644 break;
645 default:
646 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
647 pdisc);
648 break;
649 }
650
651 return rc;
652}
Harald Welte8470bf22008-12-25 23:28:35 +0000653
654enum chreq_type {
655 CHREQ_T_EMERG_CALL,
656 CHREQ_T_CALL_REEST_TCH_F,
657 CHREQ_T_CALL_REEST_TCH_H,
658 CHREQ_T_CALL_REEST_TCH_H_DBL,
659 CHREQ_T_SDCCH,
660 CHREQ_T_TCH_F,
661 CHREQ_T_VOICE_CALL_TCH_H,
662 CHREQ_T_DATA_CALL_TCH_H,
663 CHREQ_T_LOCATION_UPD,
664 CHREQ_T_PAG_R_ANY,
665 CHREQ_T_PAG_R_TCH_F,
666 CHREQ_T_PAG_R_TCH_FH,
667};
668
669/* Section 9.1.8 / Table 9.9 */
670struct chreq {
671 u_int8_t val;
672 u_int8_t mask;
673 enum chreq_type type;
674};
675
676/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
677static const struct chreq chreq_type_neci1[] = {
678 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
679 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
680 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
681 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
682 { 0xe0, 0xe0, CHREQ_T_SDCCH },
683 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
684 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
685 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
686 { 0x10, 0xf0, CHREQ_T_SDCCH },
687 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
688 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
689 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
690};
691
692/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
693static const struct chreq chreq_type_neci0[] = {
694 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
695 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
696 { 0xe0, 0xe0, CHREQ_T_TCH_F },
697 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
698 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
699 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
700 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
701 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
702};
703
704static const enum gsm_chan_t ctype_by_chreq[] = {
705 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
706 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
707 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
708 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
709 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
710 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
711 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
712 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
713 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
714 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
715 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
716 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
717};
718
Harald Weltee14a57c2008-12-29 04:08:28 +0000719static const enum gsm_chreq_reason_t reason_by_chreq[] = {
720 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
721 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
722 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
723 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
724 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
725 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
726 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
727 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
728 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
729 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
730 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
731 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
732};
733
Harald Welte8470bf22008-12-25 23:28:35 +0000734enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
735{
736 int i;
737 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
738
739 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
740 const struct chreq *chr = &chreq_type_neci0[i];
741 if ((ra & chr->mask) == chr->val)
742 return ctype_by_chreq[chr->type];
743 }
744 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
745 return GSM_LCHAN_SDCCH;
746}
Harald Weltee14a57c2008-12-29 04:08:28 +0000747
748enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
749{
750 int i;
751 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
752
753 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
754 const struct chreq *chr = &chreq_type_neci0[i];
755 if ((ra & chr->mask) == chr->val)
756 return reason_by_chreq[chr->type];
757 }
758 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
759 return GSM_CHREQ_REASON_OTHER;
760}