blob: 0560685872aba3f681eb84b80d333a7630d100f4 [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 Freyther89824fc2008-12-30 16:18:18 +000054static int authorize_everonye = 0;
55void gsm0408_allow_everyone(int everyone)
56{
57 printf("Allowing everyone?\n");
58 authorize_everonye = everyone;
59}
60
61static int authorize_subscriber(struct gsm_subscriber *subscriber)
62{
63 if (!subscriber)
64 return 0;
65
66 if (authorize_everonye)
67 return 1;
68
69 return subscriber->authorized;
70}
Holger Freyther07cc8d82008-12-29 06:23:46 +000071
72
Harald Welte52b1f982008-12-23 20:25:15 +000073static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
74{
75 u_int8_t dig[4];
76
77 /* MCC */
78 dig[1] = lai48->digits[0] & 0x0f;
79 dig[2] = lai48->digits[0] >> 4;
80 dig[3] = lai48->digits[1] & 0x0f;
81 lai->mcc = dig[3] * 100 + dig[2];
82
83 /* MNC */
84 dig[1] = lai48->digits[1] >> 4;
85 dig[2] = lai48->digits[2] & 0x0f;
86 dig[3] = lai48->digits[2] >> 4;
87 lai->mnc = dig[3] * 100 + dig[2];
88
89 lai->lac = lai48->lac;
90}
91
92static void to_bcd(u_int8_t *bcd, u_int16_t val)
93{
Harald Welte4b634542008-12-27 01:55:51 +000094 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000095 val = val / 10;
96 bcd[1] = val % 10;
97 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000098 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000099 val = val / 10;
100}
101
Holger Freyther17746612008-12-28 16:32:44 +0000102void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000103 u_int16_t mnc, u_int16_t lac)
104{
105 u_int8_t bcd[3];
106
107 to_bcd(bcd, mcc);
108 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
109 lai48->digits[1] = bcd[2];
110
111 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000112 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
113#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000114 lai48->digits[1] |= bcd[2] << 4;
115 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000116#else
117 lai48->digits[1] |= 0xf << 4;
118 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
119#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000120
Harald Welte4b634542008-12-27 01:55:51 +0000121 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000122}
123
Harald Welte255539c2008-12-28 02:26:27 +0000124#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000125#define MID_TMSI_LEN (TMSI_LEN + 2)
126
Harald Welte255539c2008-12-28 02:26:27 +0000127int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000128{
Harald Welte65e74cc2008-12-29 01:55:35 +0000129 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000130
Harald Welte4b634542008-12-27 01:55:51 +0000131 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000132 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000133 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000134 *tptr = htonl(tmsi);
135
136 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000137}
138
Harald Welte8470bf22008-12-25 23:28:35 +0000139static struct msgb *gsm48_msgb_alloc(void)
140{
141 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
142}
143
Harald Welte65e74cc2008-12-29 01:55:35 +0000144static int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000145{
Harald Welte65e74cc2008-12-29 01:55:35 +0000146 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
147
148 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000149 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000150
Harald Welte65e74cc2008-12-29 01:55:35 +0000151 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
152 /* Send a 04.08 call control message, add transaction
153 * ID and TI flag */
154 gh->proto_discr |= msg->lchan->call.transaction_id;
155
156 /* GSM 04.07 Section 11.2.3.1.3 */
157 switch (msg->lchan->call.type) {
158 case GSM_CT_MO:
159 gh->proto_discr |= 0x80;
160 break;
161 case GSM_CT_MT:
162 break;
163 }
164 }
165 }
166
Harald Welte4b634542008-12-27 01:55:51 +0000167 msg->l3h = msg->data;
168
Harald Welte8470bf22008-12-25 23:28:35 +0000169 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000170}
171
Harald Welte52b1f982008-12-23 20:25:15 +0000172
Holger Freyther429e7762008-12-30 13:28:30 +0000173/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000174int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000175{
Harald Welte8470bf22008-12-25 23:28:35 +0000176 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000177 struct gsm48_hdr *gh;
178
Harald Welte8470bf22008-12-25 23:28:35 +0000179 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000180
181 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
182 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000183 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000184 gh->data[0] = cause;
185
Holger Freyther429e7762008-12-30 13:28:30 +0000186 DEBUGP(DMM, "-> LOCATION UPDATING REJECT\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000187
Harald Welte65e74cc2008-12-29 01:55:35 +0000188 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000189}
190
191/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000192int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000193{
Harald Welte8470bf22008-12-25 23:28:35 +0000194 struct gsm_bts *bts = lchan->ts->trx->bts;
195 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000196 struct gsm48_hdr *gh;
197 struct gsm48_loc_area_id *lai;
198 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000199 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000200
Harald Welte8470bf22008-12-25 23:28:35 +0000201 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000202
203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
204 gh->proto_discr = GSM48_PDISC_MM;
205 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
206
207 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000208 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000209 bts->network->network_code, bts->location_area_code);
210
211 mid = msgb_put(msg, MID_TMSI_LEN);
212 generate_mid_from_tmsi(mid, tmsi);
213
Holger Freytherb7193e42008-12-29 17:44:08 +0000214 lchan->pending_update_request = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000215 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
216
Holger Freythereab55412008-12-30 16:18:15 +0000217 ret = gsm48_sendmsg(msg);
Holger Freyther07cc8d82008-12-29 06:23:46 +0000218
219 /* inform the upper layer on the progress */
Holger Freytherb7193e42008-12-29 17:44:08 +0000220 if (bts->network->update_request)
221 (*bts->network->update_request)(bts, tmsi, 1);
Holger Freyther07cc8d82008-12-29 06:23:46 +0000222
223 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000224}
225
Harald Weltefc977a82008-12-27 10:19:37 +0000226static char bcd2char(u_int8_t bcd)
227{
228 if (bcd < 0xa)
229 return '0' + bcd;
230 else
231 return 'A' + (bcd - 0xa);
232}
233
234/* 10.5.1.4 */
235static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
236{
237 int i;
238 u_int8_t mi_type;
239 char *str_cur = string;
240
241 mi_type = mi[0] & GSM_MI_TYPE_MASK;
242
243 switch (mi_type) {
244 case GSM_MI_TYPE_NONE:
245 break;
246 case GSM_MI_TYPE_TMSI:
247 for (i = 1; i < mi_len - 1; i++) {
248 if (str_cur + 2 >= string + str_len)
249 return str_cur - string;
250 *str_cur++ = bcd2char(mi[i] >> 4);
251 *str_cur++ = bcd2char(mi[i] & 0xf);
252 }
253 break;
254 case GSM_MI_TYPE_IMSI:
255 case GSM_MI_TYPE_IMEI:
256 case GSM_MI_TYPE_IMEISV:
257 if (mi[0] & GSM_MI_ODD)
258 *str_cur++ = bcd2char(mi[0] >> 4);
259
260 for (i = 1; i < mi_len - 1; i++) {
261 if (str_cur + 2 >= string + str_len)
262 return str_cur - string;
263 *str_cur++ = bcd2char(mi[i] & 0xf);
264 *str_cur++ = bcd2char(mi[i] >> 4);
265 }
266 break;
267 default:
268 break;
269 }
270
271 *str_cur++ = '\0';
272 return str_cur - string;
273}
274
Harald Welte231ad4f2008-12-27 11:15:38 +0000275/* Chapter 9.2.10 */
276static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
277{
278 struct msgb *msg = gsm48_msgb_alloc();
279 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000280
Harald Welte231ad4f2008-12-27 11:15:38 +0000281 msg->lchan = lchan;
282
283 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
284 gh->proto_discr = GSM48_PDISC_MM;
285 gh->msg_type = GSM48_MT_MM_ID_REQ;
286 gh->data[0] = id_type;
287
Harald Welte65e74cc2008-12-29 01:55:35 +0000288 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000289}
290
291#define MI_SIZE 32
292
293/* Chapter 9.2.11 */
294static int mm_rx_id_resp(struct msgb *msg)
295{
296 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000297 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000298 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
299 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000300 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000301
302 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000303 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000304 mi_type, mi_string);
305
Harald Welte75a983f2008-12-27 21:34:06 +0000306 switch (mi_type) {
307 case GSM_MI_TYPE_IMSI:
308 if (!lchan->subscr)
309 lchan->subscr = db_create_subscriber(mi_string);
Holger Freytherb7193e42008-12-29 17:44:08 +0000310
Holger Freyther429e7762008-12-30 13:28:30 +0000311 /* We have a pending UPDATING REQUEST handle it now */
Holger Freytherb7193e42008-12-29 17:44:08 +0000312 if (lchan->pending_update_request) {
Holger Freyther89824fc2008-12-30 16:18:18 +0000313 if (authorize_subscriber(lchan->subscr)) {
Holger Freytherb7193e42008-12-29 17:44:08 +0000314 db_subscriber_alloc_tmsi(lchan->subscr);
315 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
316 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
317 } else {
318 schedule_reject(lchan);
319 }
Harald Welte75a983f2008-12-27 21:34:06 +0000320 }
321 break;
322 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000323 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000324 /* update subscribe <-> IMEI mapping */
325 if (lchan->subscr)
326 db_subscriber_assoc_imei(lchan->subscr, mi_string);
327 break;
328 }
329 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000330}
331
Harald Welte255539c2008-12-28 02:26:27 +0000332
333static void loc_upd_rej_cb(void *data)
334{
335 struct gsm_lchan *lchan = data;
336
337 gsm0408_loc_upd_rej(lchan, 0x16);
338 rsl_chan_release(lchan);
339}
340
Holger Freytherb7193e42008-12-29 17:44:08 +0000341static void schedule_reject(struct gsm_lchan *lchan)
342{
343 lchan->timer.cb = loc_upd_rej_cb;
344 lchan->timer.data = lchan;
345 lchan->pending_update_request = 0;
346 schedule_timer(&lchan->timer, 1, 0);
347}
348
Harald Welte231ad4f2008-12-27 11:15:38 +0000349#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000350/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000351static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000352{
Harald Welte8470bf22008-12-25 23:28:35 +0000353 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000354 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000355 struct gsm48_loc_upd_req *lu;
356 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000357 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000358 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000359 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000360 char mi_string[MI_SIZE];
361 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000362
Harald Welte8470bf22008-12-25 23:28:35 +0000363 lu = (struct gsm48_loc_upd_req *) gh->data;
364
365 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000366
Harald Weltefc977a82008-12-27 10:19:37 +0000367 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
368
Harald Welte231ad4f2008-12-27 11:15:38 +0000369 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000370 switch (mi_type) {
371 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000372 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000373 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000374 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000375 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000376 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000377 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000378 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000379 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000380 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000381 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000382 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000383 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000384 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000385 }
386 break;
387 case GSM_MI_TYPE_IMEI:
388 case GSM_MI_TYPE_IMEISV:
389 /* no sim card... FIXME: what to do ? */
390 fprintf(stderr, "Unimplemented mobile identity type\n");
391 break;
392 default:
393 fprintf(stderr, "Unknown mobile identity type\n");
394 break;
395 }
396
Harald Welte255539c2008-12-28 02:26:27 +0000397 lchan->subscr = subscr;
398
Holger Freytherb7193e42008-12-29 17:44:08 +0000399 /* we know who we deal with and don't want him */
Holger Freyther89824fc2008-12-30 16:18:18 +0000400 if (subscr && !authorize_subscriber(subscr)) {
Holger Freytherb7193e42008-12-29 17:44:08 +0000401 schedule_reject(lchan);
402 return 0;
403 } else if (!subscr) {
404 /* we have asked for the imsi and should get a
405 * IDENTITY RESPONSE */
406 lchan->pending_update_request = 1;
Harald Welte255539c2008-12-28 02:26:27 +0000407 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000408 }
409
Harald Welte75a983f2008-12-27 21:34:06 +0000410 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000411 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000412
Harald Welte255539c2008-12-28 02:26:27 +0000413 tmsi = strtoul(subscr->tmsi, NULL, 10);
414
415 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000416}
417
Harald Welte4b634542008-12-27 01:55:51 +0000418static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
419{
Harald Welte4b634542008-12-27 01:55:51 +0000420 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000421 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000422}
423
424static int gsm48_rx_mm_serv_req(struct msgb *msg)
425{
426 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000427 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000428
Harald Welte65e74cc2008-12-29 01:55:35 +0000429 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000430
Harald Welte4b634542008-12-27 01:55:51 +0000431 return gsm48_tx_mm_serv_ack(msg->lchan);
432}
433
Harald Welte52b1f982008-12-23 20:25:15 +0000434static int gsm0408_rcv_mm(struct msgb *msg)
435{
436 struct gsm48_hdr *gh = msgb_l3(msg);
437 int rc;
438
439 switch (gh->msg_type & 0xbf) {
440 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000441 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000442 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000443 break;
444 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000445 rc = mm_rx_id_resp(msg);
446 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000447 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000448 rc = gsm48_rx_mm_serv_req(msg);
449 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000450 case GSM48_MT_MM_STATUS:
451 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
452 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000453 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000454 case GSM48_MT_MM_TMSI_REALL_COMPL:
455 case GSM48_MT_MM_AUTH_RESP:
456 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000457 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
458 gh->msg_type);
459 break;
460 default:
461 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
462 gh->msg_type);
463 break;
464 }
465
466 return rc;
467}
468static int gsm0408_rcv_rr(struct msgb *msg)
469{
470 struct gsm48_hdr *gh = msgb_l3(msg);
471
472 switch (gh->msg_type) {
473 case GSM48_MT_RR_CLSM_CHG:
474 DEBUGP(DRR, "CLASSMARK CHANGE\n");
475 /* FIXME: what to do ?!? */
476 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000477 case GSM48_MT_RR_GPRS_SUSP_REQ:
478 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
479 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000480 case GSM48_MT_RR_PAG_RESP:
481 default:
482 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
483 gh->msg_type);
484 break;
485 }
486
487 return 0;
488}
489
Harald Welte4bc90a12008-12-27 16:32:52 +0000490/* Call Control */
491
Harald Welte4bc90a12008-12-27 16:32:52 +0000492static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
493{
494 struct msgb *msg = gsm48_msgb_alloc();
495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
496 u_int8_t *cause, *call_state;
497
Harald Welte65e74cc2008-12-29 01:55:35 +0000498 gh->proto_discr = GSM48_PDISC_CC;
499
Harald Welte4bc90a12008-12-27 16:32:52 +0000500 msg->lchan = lchan;
501
Harald Welte4bc90a12008-12-27 16:32:52 +0000502 gh->msg_type = GSM48_MT_CC_STATUS;
503
504 cause = msgb_put(msg, 3);
505 cause[0] = 2;
506 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
507 cause[2] = 0x80 | 30; /* response to status inquiry */
508
509 call_state = msgb_put(msg, 1);
510 call_state[0] = 0xc0 | 0x00;
511
Harald Welte65e74cc2008-12-29 01:55:35 +0000512 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000513}
514
Harald Welte6f4b7532008-12-29 00:39:37 +0000515static int gsm48_tx_simple(struct gsm_lchan *lchan,
516 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000517{
518 struct msgb *msg = gsm48_msgb_alloc();
519 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
520
521 msg->lchan = lchan;
522
Harald Welte6f4b7532008-12-29 00:39:37 +0000523 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000524 gh->msg_type = msg_type;
525
Harald Welte65e74cc2008-12-29 01:55:35 +0000526 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000527}
528
529static int gsm48_cc_rx_status_enq(struct msgb *msg)
530{
531 return gsm48_cc_tx_status(msg->lchan);
532}
533
534static int gsm48_cc_rx_setup(struct msgb *msg)
535{
Harald Welte6f4b7532008-12-29 00:39:37 +0000536 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
537 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000538}
539
Harald Welte65e74cc2008-12-29 01:55:35 +0000540int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
541{
542 struct msgb *msg = gsm48_msgb_alloc();
543 struct gsm48_hdr *gh;
544 struct gsm_call *call = &lchan->call;
545
546 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
547
548 call->type = GSM_CT_MT;
549 msg->lchan = lchan;
550
551 gh->proto_discr = GSM48_PDISC_CC;
552 gh->msg_type = GSM48_MT_CC_SETUP;
553 gh->data[0] = 0x34;
554 gh->data[1] = 0x00;
555 gh->data[2] = 0x5c;
556 gh->data[3] = 0x04;
557 gh->data[4] = 0xb9;
558 gh->data[5] = 0x83;
559 gh->data[6] = 0x32;
560 gh->data[7] = 0x24;
561
562 DEBUGP(DCC, "Sending SETUP\n");
563
564 return gsm48_sendmsg(msg);
565}
566
Harald Welte4bc90a12008-12-27 16:32:52 +0000567static int gsm0408_rcv_cc(struct msgb *msg)
568{
569 struct gsm48_hdr *gh = msgb_l3(msg);
570 u_int8_t msg_type = gh->msg_type & 0xbf;
571 struct gsm_call *call = &msg->lchan->call;
Holger Freyther2eafef52008-12-29 06:42:17 +0000572 struct gsm_network *network = msg->lchan->ts->trx->bts->network;
Harald Welte4bc90a12008-12-27 16:32:52 +0000573 int rc = 0;
574
575 switch (msg_type) {
576 case GSM48_MT_CC_CALL_CONF:
577 /* Response to SETUP */
578 DEBUGP(DCC, "CALL CONFIRM\n");
579 break;
580 case GSM48_MT_CC_RELEASE_COMPL:
581 /* Answer from MS to RELEASE */
582 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
Holger Freytherb7193e42008-12-29 17:44:08 +0000583 if (network->call_state_changed)
584 (*network->call_state_changed)(msg->lchan, call->state);
Harald Welte4bc90a12008-12-27 16:32:52 +0000585 call->state = GSM_CSTATE_NULL;
586 break;
587 case GSM48_MT_CC_ALERTING:
588 DEBUGP(DCC, "ALERTING\n");
589 break;
590 case GSM48_MT_CC_CONNECT:
591 DEBUGP(DCC, "CONNECT\n");
592 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000593 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
594 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000595 break;
596 case GSM48_MT_CC_CONNECT_ACK:
597 /* MO: Answer to CONNECT */
598 call->state = GSM_CSTATE_ACTIVE;
599 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
600 break;
601 case GSM48_MT_CC_RELEASE:
602 DEBUGP(DCC, "RELEASE\n");
603 /* need to respond with RELEASE_COMPLETE */
604 break;
605 case GSM48_MT_CC_STATUS_ENQ:
606 rc = gsm48_cc_rx_status_enq(msg);
607 break;
608 case GSM48_MT_CC_DISCONNECT:
609 /* Section 5.4.3.2 */
610 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
611 call->state = GSM_CSTATE_RELEASE_REQ;
612 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000613 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
614 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000615 break;
616 case GSM48_MT_CC_SETUP:
617 call->type = GSM_CT_MO;
618 call->state = GSM_CSTATE_INITIATED;
619 call->transaction_id = gh->proto_discr & 0xf0;
620 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000621 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
622 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000623 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
624 break;
625 case GSM48_MT_CC_EMERG_SETUP:
626 DEBUGP(DCC, "EMERGENCY SETUP\n");
627 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
628 break;
629 default:
630 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
631 msg_type);
632 break;
633 }
634
635 return rc;
636}
637
Harald Welte52b1f982008-12-23 20:25:15 +0000638/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
639int gsm0408_rcvmsg(struct msgb *msg)
640{
641 struct gsm48_hdr *gh = msgb_l3(msg);
642 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000643 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000644
645 switch (pdisc) {
646 case GSM48_PDISC_CC:
647 rc = gsm0408_rcv_cc(msg);
648 break;
649 case GSM48_PDISC_MM:
650 rc = gsm0408_rcv_mm(msg);
651 break;
652 case GSM48_PDISC_RR:
653 rc = gsm0408_rcv_rr(msg);
654 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000655 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000656 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000657 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000658 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000659 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000660 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
661 pdisc);
662 break;
663 default:
664 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
665 pdisc);
666 break;
667 }
668
669 return rc;
670}
Harald Welte8470bf22008-12-25 23:28:35 +0000671
672enum chreq_type {
673 CHREQ_T_EMERG_CALL,
674 CHREQ_T_CALL_REEST_TCH_F,
675 CHREQ_T_CALL_REEST_TCH_H,
676 CHREQ_T_CALL_REEST_TCH_H_DBL,
677 CHREQ_T_SDCCH,
678 CHREQ_T_TCH_F,
679 CHREQ_T_VOICE_CALL_TCH_H,
680 CHREQ_T_DATA_CALL_TCH_H,
681 CHREQ_T_LOCATION_UPD,
682 CHREQ_T_PAG_R_ANY,
683 CHREQ_T_PAG_R_TCH_F,
684 CHREQ_T_PAG_R_TCH_FH,
685};
686
687/* Section 9.1.8 / Table 9.9 */
688struct chreq {
689 u_int8_t val;
690 u_int8_t mask;
691 enum chreq_type type;
692};
693
694/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
695static const struct chreq chreq_type_neci1[] = {
696 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
697 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
698 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
699 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
700 { 0xe0, 0xe0, CHREQ_T_SDCCH },
701 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
702 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
703 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
704 { 0x10, 0xf0, CHREQ_T_SDCCH },
705 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
706 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
707 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
708};
709
710/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
711static const struct chreq chreq_type_neci0[] = {
712 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
713 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
714 { 0xe0, 0xe0, CHREQ_T_TCH_F },
715 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
716 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
717 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
718 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
719 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
720};
721
722static const enum gsm_chan_t ctype_by_chreq[] = {
723 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
724 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
725 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
726 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
727 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
728 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
729 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
730 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
731 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
732 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
733 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
734 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
735};
736
Harald Weltee14a57c2008-12-29 04:08:28 +0000737static const enum gsm_chreq_reason_t reason_by_chreq[] = {
738 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
739 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
740 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
741 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
742 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
743 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
744 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
745 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
746 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
747 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
748 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
749 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
750};
751
Harald Welte8470bf22008-12-25 23:28:35 +0000752enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
753{
754 int i;
755 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
756
757 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
758 const struct chreq *chr = &chreq_type_neci0[i];
759 if ((ra & chr->mask) == chr->val)
760 return ctype_by_chreq[chr->type];
761 }
762 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
763 return GSM_LCHAN_SDCCH;
764}
Harald Weltee14a57c2008-12-29 04:08:28 +0000765
766enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
767{
768 int i;
769 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
770
771 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
772 const struct chreq *chr = &chreq_type_neci0[i];
773 if ((ra & chr->mask) == chr->val)
774 return reason_by_chreq[chr->type];
775 }
776 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
777 return GSM_CHREQ_REASON_OTHER;
778}