blob: b19c92c99bc2fc5e8472d5aee63d7ca7dcfa91a6 [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 Freythereab55412008-12-30 16:18:15 +0000200 ret = gsm48_sendmsg(msg);
Holger Freyther07cc8d82008-12-29 06:23:46 +0000201
202 /* inform the upper layer on the progress */
Holger Freytherb7193e42008-12-29 17:44:08 +0000203 if (bts->network->update_request)
204 (*bts->network->update_request)(bts, tmsi, 1);
Holger Freyther07cc8d82008-12-29 06:23:46 +0000205
206 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000207}
208
Harald Weltefc977a82008-12-27 10:19:37 +0000209static char bcd2char(u_int8_t bcd)
210{
211 if (bcd < 0xa)
212 return '0' + bcd;
213 else
214 return 'A' + (bcd - 0xa);
215}
216
217/* 10.5.1.4 */
218static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
219{
220 int i;
221 u_int8_t mi_type;
222 char *str_cur = string;
223
224 mi_type = mi[0] & GSM_MI_TYPE_MASK;
225
226 switch (mi_type) {
227 case GSM_MI_TYPE_NONE:
228 break;
229 case GSM_MI_TYPE_TMSI:
230 for (i = 1; i < mi_len - 1; i++) {
231 if (str_cur + 2 >= string + str_len)
232 return str_cur - string;
233 *str_cur++ = bcd2char(mi[i] >> 4);
234 *str_cur++ = bcd2char(mi[i] & 0xf);
235 }
236 break;
237 case GSM_MI_TYPE_IMSI:
238 case GSM_MI_TYPE_IMEI:
239 case GSM_MI_TYPE_IMEISV:
240 if (mi[0] & GSM_MI_ODD)
241 *str_cur++ = bcd2char(mi[0] >> 4);
242
243 for (i = 1; i < mi_len - 1; i++) {
244 if (str_cur + 2 >= string + str_len)
245 return str_cur - string;
246 *str_cur++ = bcd2char(mi[i] & 0xf);
247 *str_cur++ = bcd2char(mi[i] >> 4);
248 }
249 break;
250 default:
251 break;
252 }
253
254 *str_cur++ = '\0';
255 return str_cur - string;
256}
257
Harald Welte231ad4f2008-12-27 11:15:38 +0000258/* Chapter 9.2.10 */
259static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
260{
261 struct msgb *msg = gsm48_msgb_alloc();
262 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000263
Harald Welte231ad4f2008-12-27 11:15:38 +0000264 msg->lchan = lchan;
265
266 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
267 gh->proto_discr = GSM48_PDISC_MM;
268 gh->msg_type = GSM48_MT_MM_ID_REQ;
269 gh->data[0] = id_type;
270
Harald Welte65e74cc2008-12-29 01:55:35 +0000271 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000272}
273
274#define MI_SIZE 32
275
276/* Chapter 9.2.11 */
277static int mm_rx_id_resp(struct msgb *msg)
278{
279 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000280 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000281 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
282 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000283 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000284
285 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000286 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000287 mi_type, mi_string);
288
Harald Welte75a983f2008-12-27 21:34:06 +0000289 switch (mi_type) {
290 case GSM_MI_TYPE_IMSI:
291 if (!lchan->subscr)
292 lchan->subscr = db_create_subscriber(mi_string);
Holger Freytherb7193e42008-12-29 17:44:08 +0000293
Holger Freyther429e7762008-12-30 13:28:30 +0000294 /* We have a pending UPDATING REQUEST handle it now */
Holger Freytherb7193e42008-12-29 17:44:08 +0000295 if (lchan->pending_update_request) {
296 if (lchan->subscr->authorized) {
297 db_subscriber_alloc_tmsi(lchan->subscr);
298 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
299 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
300 } else {
301 schedule_reject(lchan);
302 }
Harald Welte75a983f2008-12-27 21:34:06 +0000303 }
304 break;
305 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000306 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000307 /* update subscribe <-> IMEI mapping */
308 if (lchan->subscr)
309 db_subscriber_assoc_imei(lchan->subscr, mi_string);
310 break;
311 }
312 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000313}
314
Harald Welte255539c2008-12-28 02:26:27 +0000315
316static void loc_upd_rej_cb(void *data)
317{
318 struct gsm_lchan *lchan = data;
319
320 gsm0408_loc_upd_rej(lchan, 0x16);
321 rsl_chan_release(lchan);
322}
323
Holger Freytherb7193e42008-12-29 17:44:08 +0000324static void schedule_reject(struct gsm_lchan *lchan)
325{
326 lchan->timer.cb = loc_upd_rej_cb;
327 lchan->timer.data = lchan;
328 lchan->pending_update_request = 0;
329 schedule_timer(&lchan->timer, 1, 0);
330}
331
Harald Welte231ad4f2008-12-27 11:15:38 +0000332#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000333/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000334static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000335{
Harald Welte8470bf22008-12-25 23:28:35 +0000336 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000337 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000338 struct gsm48_loc_upd_req *lu;
339 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000340 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000341 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000342 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000343 char mi_string[MI_SIZE];
344 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000345
Harald Welte8470bf22008-12-25 23:28:35 +0000346 lu = (struct gsm48_loc_upd_req *) gh->data;
347
348 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000349
Harald Weltefc977a82008-12-27 10:19:37 +0000350 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
351
Harald Welte231ad4f2008-12-27 11:15:38 +0000352 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000353 switch (mi_type) {
354 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000355 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000356 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000357 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000358 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000359 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000360 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000361 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000362 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000363 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000364 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000365 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000366 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000367 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000368 }
369 break;
370 case GSM_MI_TYPE_IMEI:
371 case GSM_MI_TYPE_IMEISV:
372 /* no sim card... FIXME: what to do ? */
373 fprintf(stderr, "Unimplemented mobile identity type\n");
374 break;
375 default:
376 fprintf(stderr, "Unknown mobile identity type\n");
377 break;
378 }
379
Harald Welte255539c2008-12-28 02:26:27 +0000380 lchan->subscr = subscr;
381
Holger Freytherb7193e42008-12-29 17:44:08 +0000382 /* we know who we deal with and don't want him */
383 if (subscr && !subscr->authorized) {
384 schedule_reject(lchan);
385 return 0;
386 } else if (!subscr) {
387 /* we have asked for the imsi and should get a
388 * IDENTITY RESPONSE */
389 lchan->pending_update_request = 1;
Harald Welte255539c2008-12-28 02:26:27 +0000390 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000391 }
392
Harald Welte75a983f2008-12-27 21:34:06 +0000393 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000394 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000395
Harald Welte255539c2008-12-28 02:26:27 +0000396 tmsi = strtoul(subscr->tmsi, NULL, 10);
397
398 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000399}
400
Harald Welte4b634542008-12-27 01:55:51 +0000401static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
402{
Harald Welte4b634542008-12-27 01:55:51 +0000403 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000404 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000405}
406
407static int gsm48_rx_mm_serv_req(struct msgb *msg)
408{
409 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000410 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000411
Harald Welte65e74cc2008-12-29 01:55:35 +0000412 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000413
Harald Welte4b634542008-12-27 01:55:51 +0000414 return gsm48_tx_mm_serv_ack(msg->lchan);
415}
416
Harald Welte52b1f982008-12-23 20:25:15 +0000417static int gsm0408_rcv_mm(struct msgb *msg)
418{
419 struct gsm48_hdr *gh = msgb_l3(msg);
420 int rc;
421
422 switch (gh->msg_type & 0xbf) {
423 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000424 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000425 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000426 break;
427 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000428 rc = mm_rx_id_resp(msg);
429 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000430 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000431 rc = gsm48_rx_mm_serv_req(msg);
432 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000433 case GSM48_MT_MM_STATUS:
434 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
435 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000436 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000437 case GSM48_MT_MM_TMSI_REALL_COMPL:
438 case GSM48_MT_MM_AUTH_RESP:
439 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000440 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
441 gh->msg_type);
442 break;
443 default:
444 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
445 gh->msg_type);
446 break;
447 }
448
449 return rc;
450}
451static int gsm0408_rcv_rr(struct msgb *msg)
452{
453 struct gsm48_hdr *gh = msgb_l3(msg);
454
455 switch (gh->msg_type) {
456 case GSM48_MT_RR_CLSM_CHG:
457 DEBUGP(DRR, "CLASSMARK CHANGE\n");
458 /* FIXME: what to do ?!? */
459 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000460 case GSM48_MT_RR_GPRS_SUSP_REQ:
461 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
462 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000463 case GSM48_MT_RR_PAG_RESP:
464 default:
465 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
466 gh->msg_type);
467 break;
468 }
469
470 return 0;
471}
472
Harald Welte4bc90a12008-12-27 16:32:52 +0000473/* Call Control */
474
Harald Welte4bc90a12008-12-27 16:32:52 +0000475static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
476{
477 struct msgb *msg = gsm48_msgb_alloc();
478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
479 u_int8_t *cause, *call_state;
480
Harald Welte65e74cc2008-12-29 01:55:35 +0000481 gh->proto_discr = GSM48_PDISC_CC;
482
Harald Welte4bc90a12008-12-27 16:32:52 +0000483 msg->lchan = lchan;
484
Harald Welte4bc90a12008-12-27 16:32:52 +0000485 gh->msg_type = GSM48_MT_CC_STATUS;
486
487 cause = msgb_put(msg, 3);
488 cause[0] = 2;
489 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
490 cause[2] = 0x80 | 30; /* response to status inquiry */
491
492 call_state = msgb_put(msg, 1);
493 call_state[0] = 0xc0 | 0x00;
494
Harald Welte65e74cc2008-12-29 01:55:35 +0000495 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000496}
497
Harald Welte6f4b7532008-12-29 00:39:37 +0000498static int gsm48_tx_simple(struct gsm_lchan *lchan,
499 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000500{
501 struct msgb *msg = gsm48_msgb_alloc();
502 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
503
504 msg->lchan = lchan;
505
Harald Welte6f4b7532008-12-29 00:39:37 +0000506 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000507 gh->msg_type = msg_type;
508
Harald Welte65e74cc2008-12-29 01:55:35 +0000509 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000510}
511
512static int gsm48_cc_rx_status_enq(struct msgb *msg)
513{
514 return gsm48_cc_tx_status(msg->lchan);
515}
516
517static int gsm48_cc_rx_setup(struct msgb *msg)
518{
Harald Welte6f4b7532008-12-29 00:39:37 +0000519 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
520 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000521}
522
Harald Welte65e74cc2008-12-29 01:55:35 +0000523int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
524{
525 struct msgb *msg = gsm48_msgb_alloc();
526 struct gsm48_hdr *gh;
527 struct gsm_call *call = &lchan->call;
528
529 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
530
531 call->type = GSM_CT_MT;
532 msg->lchan = lchan;
533
534 gh->proto_discr = GSM48_PDISC_CC;
535 gh->msg_type = GSM48_MT_CC_SETUP;
536 gh->data[0] = 0x34;
537 gh->data[1] = 0x00;
538 gh->data[2] = 0x5c;
539 gh->data[3] = 0x04;
540 gh->data[4] = 0xb9;
541 gh->data[5] = 0x83;
542 gh->data[6] = 0x32;
543 gh->data[7] = 0x24;
544
545 DEBUGP(DCC, "Sending SETUP\n");
546
547 return gsm48_sendmsg(msg);
548}
549
Harald Welte4bc90a12008-12-27 16:32:52 +0000550static int gsm0408_rcv_cc(struct msgb *msg)
551{
552 struct gsm48_hdr *gh = msgb_l3(msg);
553 u_int8_t msg_type = gh->msg_type & 0xbf;
554 struct gsm_call *call = &msg->lchan->call;
Holger Freyther2eafef52008-12-29 06:42:17 +0000555 struct gsm_network *network = msg->lchan->ts->trx->bts->network;
Harald Welte4bc90a12008-12-27 16:32:52 +0000556 int rc = 0;
557
558 switch (msg_type) {
559 case GSM48_MT_CC_CALL_CONF:
560 /* Response to SETUP */
561 DEBUGP(DCC, "CALL CONFIRM\n");
562 break;
563 case GSM48_MT_CC_RELEASE_COMPL:
564 /* Answer from MS to RELEASE */
565 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
Holger Freytherb7193e42008-12-29 17:44:08 +0000566 if (network->call_state_changed)
567 (*network->call_state_changed)(msg->lchan, call->state);
Harald Welte4bc90a12008-12-27 16:32:52 +0000568 call->state = GSM_CSTATE_NULL;
569 break;
570 case GSM48_MT_CC_ALERTING:
571 DEBUGP(DCC, "ALERTING\n");
572 break;
573 case GSM48_MT_CC_CONNECT:
574 DEBUGP(DCC, "CONNECT\n");
575 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000576 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
577 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000578 break;
579 case GSM48_MT_CC_CONNECT_ACK:
580 /* MO: Answer to CONNECT */
581 call->state = GSM_CSTATE_ACTIVE;
582 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
583 break;
584 case GSM48_MT_CC_RELEASE:
585 DEBUGP(DCC, "RELEASE\n");
586 /* need to respond with RELEASE_COMPLETE */
587 break;
588 case GSM48_MT_CC_STATUS_ENQ:
589 rc = gsm48_cc_rx_status_enq(msg);
590 break;
591 case GSM48_MT_CC_DISCONNECT:
592 /* Section 5.4.3.2 */
593 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
594 call->state = GSM_CSTATE_RELEASE_REQ;
595 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000596 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
597 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000598 break;
599 case GSM48_MT_CC_SETUP:
600 call->type = GSM_CT_MO;
601 call->state = GSM_CSTATE_INITIATED;
602 call->transaction_id = gh->proto_discr & 0xf0;
603 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000604 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
605 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000606 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
607 break;
608 case GSM48_MT_CC_EMERG_SETUP:
609 DEBUGP(DCC, "EMERGENCY SETUP\n");
610 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
611 break;
612 default:
613 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
614 msg_type);
615 break;
616 }
617
618 return rc;
619}
620
Harald Welte52b1f982008-12-23 20:25:15 +0000621/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
622int gsm0408_rcvmsg(struct msgb *msg)
623{
624 struct gsm48_hdr *gh = msgb_l3(msg);
625 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000626 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000627
628 switch (pdisc) {
629 case GSM48_PDISC_CC:
630 rc = gsm0408_rcv_cc(msg);
631 break;
632 case GSM48_PDISC_MM:
633 rc = gsm0408_rcv_mm(msg);
634 break;
635 case GSM48_PDISC_RR:
636 rc = gsm0408_rcv_rr(msg);
637 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000638 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000639 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000640 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000641 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000642 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000643 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
644 pdisc);
645 break;
646 default:
647 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
648 pdisc);
649 break;
650 }
651
652 return rc;
653}
Harald Welte8470bf22008-12-25 23:28:35 +0000654
655enum chreq_type {
656 CHREQ_T_EMERG_CALL,
657 CHREQ_T_CALL_REEST_TCH_F,
658 CHREQ_T_CALL_REEST_TCH_H,
659 CHREQ_T_CALL_REEST_TCH_H_DBL,
660 CHREQ_T_SDCCH,
661 CHREQ_T_TCH_F,
662 CHREQ_T_VOICE_CALL_TCH_H,
663 CHREQ_T_DATA_CALL_TCH_H,
664 CHREQ_T_LOCATION_UPD,
665 CHREQ_T_PAG_R_ANY,
666 CHREQ_T_PAG_R_TCH_F,
667 CHREQ_T_PAG_R_TCH_FH,
668};
669
670/* Section 9.1.8 / Table 9.9 */
671struct chreq {
672 u_int8_t val;
673 u_int8_t mask;
674 enum chreq_type type;
675};
676
677/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
678static const struct chreq chreq_type_neci1[] = {
679 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
680 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
681 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
682 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
683 { 0xe0, 0xe0, CHREQ_T_SDCCH },
684 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
685 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
686 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
687 { 0x10, 0xf0, CHREQ_T_SDCCH },
688 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
689 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
690 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
691};
692
693/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
694static const struct chreq chreq_type_neci0[] = {
695 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
696 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
697 { 0xe0, 0xe0, CHREQ_T_TCH_F },
698 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
699 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
700 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
701 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
702 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
703};
704
705static const enum gsm_chan_t ctype_by_chreq[] = {
706 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
707 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
708 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
709 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
710 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
711 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
712 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
713 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
714 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
715 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
716 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
717 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
718};
719
Harald Weltee14a57c2008-12-29 04:08:28 +0000720static const enum gsm_chreq_reason_t reason_by_chreq[] = {
721 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
722 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
723 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
724 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
725 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
726 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
727 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
728 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
729 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
730 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
731 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
732 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
733};
734
Harald Welte8470bf22008-12-25 23:28:35 +0000735enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
736{
737 int i;
738 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
739
740 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
741 const struct chreq *chr = &chreq_type_neci0[i];
742 if ((ra & chr->mask) == chr->val)
743 return ctype_by_chreq[chr->type];
744 }
745 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
746 return GSM_LCHAN_SDCCH;
747}
Harald Weltee14a57c2008-12-29 04:08:28 +0000748
749enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
750{
751 int i;
752 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
753
754 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
755 const struct chreq *chr = &chreq_type_neci0[i];
756 if ((ra & chr->mask) == chr->val)
757 return reason_by_chreq[chr->type];
758 }
759 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
760 return GSM_CHREQ_REASON_OTHER;
761}