blob: 918116db12eba21f03710060d8312913479e38e9 [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);
46
Harald Welte52b1f982008-12-23 20:25:15 +000047struct gsm_lai {
48 u_int16_t mcc;
49 u_int16_t mnc;
50 u_int16_t lac;
51};
52
Holger Freyther07cc8d82008-12-29 06:23:46 +000053
54
Harald Welte52b1f982008-12-23 20:25:15 +000055static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
56{
57 u_int8_t dig[4];
58
59 /* MCC */
60 dig[1] = lai48->digits[0] & 0x0f;
61 dig[2] = lai48->digits[0] >> 4;
62 dig[3] = lai48->digits[1] & 0x0f;
63 lai->mcc = dig[3] * 100 + dig[2];
64
65 /* MNC */
66 dig[1] = lai48->digits[1] >> 4;
67 dig[2] = lai48->digits[2] & 0x0f;
68 dig[3] = lai48->digits[2] >> 4;
69 lai->mnc = dig[3] * 100 + dig[2];
70
71 lai->lac = lai48->lac;
72}
73
74static void to_bcd(u_int8_t *bcd, u_int16_t val)
75{
Harald Welte4b634542008-12-27 01:55:51 +000076 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000077 val = val / 10;
78 bcd[1] = val % 10;
79 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000080 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000081 val = val / 10;
82}
83
Holger Freyther17746612008-12-28 16:32:44 +000084void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +000085 u_int16_t mnc, u_int16_t lac)
86{
87 u_int8_t bcd[3];
88
89 to_bcd(bcd, mcc);
90 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
91 lai48->digits[1] = bcd[2];
92
93 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +000094 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
95#if 0
Harald Welte8470bf22008-12-25 23:28:35 +000096 lai48->digits[1] |= bcd[2] << 4;
97 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +000098#else
99 lai48->digits[1] |= 0xf << 4;
100 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
101#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000102
Harald Welte4b634542008-12-27 01:55:51 +0000103 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000104}
105
Harald Welte255539c2008-12-28 02:26:27 +0000106#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000107#define MID_TMSI_LEN (TMSI_LEN + 2)
108
Harald Welte255539c2008-12-28 02:26:27 +0000109int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000110{
Harald Welte65e74cc2008-12-29 01:55:35 +0000111 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000112
Harald Welte4b634542008-12-27 01:55:51 +0000113 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000114 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000115 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000116 *tptr = htonl(tmsi);
117
118 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000119}
120
Harald Welte8470bf22008-12-25 23:28:35 +0000121static struct msgb *gsm48_msgb_alloc(void)
122{
123 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
124}
125
Harald Welte65e74cc2008-12-29 01:55:35 +0000126static int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000127{
Harald Welte65e74cc2008-12-29 01:55:35 +0000128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
129
130 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000131 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000132
Harald Welte65e74cc2008-12-29 01:55:35 +0000133 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
134 /* Send a 04.08 call control message, add transaction
135 * ID and TI flag */
136 gh->proto_discr |= msg->lchan->call.transaction_id;
137
138 /* GSM 04.07 Section 11.2.3.1.3 */
139 switch (msg->lchan->call.type) {
140 case GSM_CT_MO:
141 gh->proto_discr |= 0x80;
142 break;
143 case GSM_CT_MT:
144 break;
145 }
146 }
147 }
148
Harald Welte4b634542008-12-27 01:55:51 +0000149 msg->l3h = msg->data;
150
Harald Welte8470bf22008-12-25 23:28:35 +0000151 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000152}
153
Harald Welte52b1f982008-12-23 20:25:15 +0000154
155/* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000156int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000157{
Harald Welte8470bf22008-12-25 23:28:35 +0000158 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000159 struct gsm48_hdr *gh;
160
Harald Welte8470bf22008-12-25 23:28:35 +0000161 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000162
163 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
164 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000165 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000166 gh->data[0] = cause;
167
168 DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n");
169
Harald Welte65e74cc2008-12-29 01:55:35 +0000170 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000171}
172
173/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000174int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000175{
Harald Welte8470bf22008-12-25 23:28:35 +0000176 struct gsm_bts *bts = lchan->ts->trx->bts;
177 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000178 struct gsm48_hdr *gh;
179 struct gsm48_loc_area_id *lai;
180 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000181 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000182
Harald Welte8470bf22008-12-25 23:28:35 +0000183 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000184
185 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
186 gh->proto_discr = GSM48_PDISC_MM;
187 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
188
189 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000190 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000191 bts->network->network_code, bts->location_area_code);
192
193 mid = msgb_put(msg, MID_TMSI_LEN);
194 generate_mid_from_tmsi(mid, tmsi);
195
196 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
197
Harald Welte65e74cc2008-12-29 01:55:35 +0000198 gsm48_sendmsg(msg);
199
Holger Freytherba4d28a2008-12-29 06:23:44 +0000200 /* free the channel afterwards */
Holger Freyther07cc8d82008-12-29 06:23:46 +0000201 ret = rsl_chan_release(lchan);
202
203 /* inform the upper layer on the progress */
204 if (bts->network->update_request_accepted)
205 (*bts->network->update_request_accepted)(bts, tmsi);
206
207 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000208}
209
Harald Weltefc977a82008-12-27 10:19:37 +0000210static char bcd2char(u_int8_t bcd)
211{
212 if (bcd < 0xa)
213 return '0' + bcd;
214 else
215 return 'A' + (bcd - 0xa);
216}
217
218/* 10.5.1.4 */
219static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
220{
221 int i;
222 u_int8_t mi_type;
223 char *str_cur = string;
224
225 mi_type = mi[0] & GSM_MI_TYPE_MASK;
226
227 switch (mi_type) {
228 case GSM_MI_TYPE_NONE:
229 break;
230 case GSM_MI_TYPE_TMSI:
231 for (i = 1; i < mi_len - 1; i++) {
232 if (str_cur + 2 >= string + str_len)
233 return str_cur - string;
234 *str_cur++ = bcd2char(mi[i] >> 4);
235 *str_cur++ = bcd2char(mi[i] & 0xf);
236 }
237 break;
238 case GSM_MI_TYPE_IMSI:
239 case GSM_MI_TYPE_IMEI:
240 case GSM_MI_TYPE_IMEISV:
241 if (mi[0] & GSM_MI_ODD)
242 *str_cur++ = bcd2char(mi[0] >> 4);
243
244 for (i = 1; i < mi_len - 1; i++) {
245 if (str_cur + 2 >= string + str_len)
246 return str_cur - string;
247 *str_cur++ = bcd2char(mi[i] & 0xf);
248 *str_cur++ = bcd2char(mi[i] >> 4);
249 }
250 break;
251 default:
252 break;
253 }
254
255 *str_cur++ = '\0';
256 return str_cur - string;
257}
258
Harald Welte231ad4f2008-12-27 11:15:38 +0000259/* Chapter 9.2.10 */
260static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
261{
262 struct msgb *msg = gsm48_msgb_alloc();
263 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000264
Harald Welte231ad4f2008-12-27 11:15:38 +0000265 msg->lchan = lchan;
266
267 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
268 gh->proto_discr = GSM48_PDISC_MM;
269 gh->msg_type = GSM48_MT_MM_ID_REQ;
270 gh->data[0] = id_type;
271
Harald Welte65e74cc2008-12-29 01:55:35 +0000272 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000273}
274
275#define MI_SIZE 32
276
277/* Chapter 9.2.11 */
278static int mm_rx_id_resp(struct msgb *msg)
279{
280 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000281 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000282 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
283 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000284 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000285
286 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000287 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000288 mi_type, mi_string);
289
Harald Welte75a983f2008-12-27 21:34:06 +0000290 switch (mi_type) {
291 case GSM_MI_TYPE_IMSI:
292 if (!lchan->subscr)
293 lchan->subscr = db_create_subscriber(mi_string);
294 if (lchan->subscr && lchan->subscr->authorized) {
295 /* FIXME: check if we've recently received UPDATE REQUEST */
296 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte255539c2008-12-28 02:26:27 +0000297 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
Harald Welte75a983f2008-12-27 21:34:06 +0000298 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
299 }
300 break;
301 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000302 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000303 /* update subscribe <-> IMEI mapping */
304 if (lchan->subscr)
305 db_subscriber_assoc_imei(lchan->subscr, mi_string);
306 break;
307 }
308 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000309}
310
Harald Welte255539c2008-12-28 02:26:27 +0000311
312static void loc_upd_rej_cb(void *data)
313{
314 struct gsm_lchan *lchan = data;
315
316 gsm0408_loc_upd_rej(lchan, 0x16);
317 rsl_chan_release(lchan);
318}
319
Harald Welte231ad4f2008-12-27 11:15:38 +0000320#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000321/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000322static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000323{
Harald Welte8470bf22008-12-25 23:28:35 +0000324 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000325 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000326 struct gsm48_loc_upd_req *lu;
327 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000328 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000329 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000330 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000331 char mi_string[MI_SIZE];
332 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000333
Harald Welte8470bf22008-12-25 23:28:35 +0000334 lu = (struct gsm48_loc_upd_req *) gh->data;
335
336 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000337
Harald Weltefc977a82008-12-27 10:19:37 +0000338 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
339
Harald Welte231ad4f2008-12-27 11:15:38 +0000340 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000341 switch (mi_type) {
342 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000343 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000344 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000345 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000346 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000347 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000348 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000349 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000350 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000351 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000352 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000353 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000354 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000355 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000356 }
357 break;
358 case GSM_MI_TYPE_IMEI:
359 case GSM_MI_TYPE_IMEISV:
360 /* no sim card... FIXME: what to do ? */
361 fprintf(stderr, "Unimplemented mobile identity type\n");
362 break;
363 default:
364 fprintf(stderr, "Unknown mobile identity type\n");
365 break;
366 }
367
Harald Welte255539c2008-12-28 02:26:27 +0000368 lchan->subscr = subscr;
369
Harald Welte75a983f2008-12-27 21:34:06 +0000370 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000371 /* 0x16 is congestion */
Harald Welte255539c2008-12-28 02:26:27 +0000372 lchan->timer.cb = loc_upd_rej_cb;
373 lchan->timer.data = lchan;
374 schedule_timer(&lchan->timer, 1, 0);
375 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000376 }
377
Harald Welte75a983f2008-12-27 21:34:06 +0000378 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000379 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000380
Harald Welte255539c2008-12-28 02:26:27 +0000381 tmsi = strtoul(subscr->tmsi, NULL, 10);
382
383 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000384}
385
Harald Welte4b634542008-12-27 01:55:51 +0000386static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
387{
Harald Welte4b634542008-12-27 01:55:51 +0000388 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000389 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000390}
391
392static int gsm48_rx_mm_serv_req(struct msgb *msg)
393{
394 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000395 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000396
Harald Welte65e74cc2008-12-29 01:55:35 +0000397 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000398
Harald Welte4b634542008-12-27 01:55:51 +0000399 return gsm48_tx_mm_serv_ack(msg->lchan);
400}
401
Harald Welte52b1f982008-12-23 20:25:15 +0000402static int gsm0408_rcv_mm(struct msgb *msg)
403{
404 struct gsm48_hdr *gh = msgb_l3(msg);
405 int rc;
406
407 switch (gh->msg_type & 0xbf) {
408 case GSM48_MT_MM_LOC_UPD_REQUEST:
409 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000410 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000411 break;
412 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000413 rc = mm_rx_id_resp(msg);
414 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000415 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000416 rc = gsm48_rx_mm_serv_req(msg);
417 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000418 case GSM48_MT_MM_STATUS:
419 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
420 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000421 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000422 case GSM48_MT_MM_TMSI_REALL_COMPL:
423 case GSM48_MT_MM_AUTH_RESP:
424 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000425 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
426 gh->msg_type);
427 break;
428 default:
429 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
430 gh->msg_type);
431 break;
432 }
433
434 return rc;
435}
436static int gsm0408_rcv_rr(struct msgb *msg)
437{
438 struct gsm48_hdr *gh = msgb_l3(msg);
439
440 switch (gh->msg_type) {
441 case GSM48_MT_RR_CLSM_CHG:
442 DEBUGP(DRR, "CLASSMARK CHANGE\n");
443 /* FIXME: what to do ?!? */
444 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000445 case GSM48_MT_RR_GPRS_SUSP_REQ:
446 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
447 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000448 case GSM48_MT_RR_PAG_RESP:
449 default:
450 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
451 gh->msg_type);
452 break;
453 }
454
455 return 0;
456}
457
Harald Welte4bc90a12008-12-27 16:32:52 +0000458/* Call Control */
459
Harald Welte4bc90a12008-12-27 16:32:52 +0000460static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
461{
462 struct msgb *msg = gsm48_msgb_alloc();
463 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
464 u_int8_t *cause, *call_state;
465
Harald Welte65e74cc2008-12-29 01:55:35 +0000466 gh->proto_discr = GSM48_PDISC_CC;
467
Harald Welte4bc90a12008-12-27 16:32:52 +0000468 msg->lchan = lchan;
469
Harald Welte4bc90a12008-12-27 16:32:52 +0000470 gh->msg_type = GSM48_MT_CC_STATUS;
471
472 cause = msgb_put(msg, 3);
473 cause[0] = 2;
474 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
475 cause[2] = 0x80 | 30; /* response to status inquiry */
476
477 call_state = msgb_put(msg, 1);
478 call_state[0] = 0xc0 | 0x00;
479
Harald Welte65e74cc2008-12-29 01:55:35 +0000480 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000481}
482
Harald Welte6f4b7532008-12-29 00:39:37 +0000483static int gsm48_tx_simple(struct gsm_lchan *lchan,
484 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000485{
486 struct msgb *msg = gsm48_msgb_alloc();
487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
488
489 msg->lchan = lchan;
490
Harald Welte6f4b7532008-12-29 00:39:37 +0000491 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000492 gh->msg_type = msg_type;
493
Harald Welte65e74cc2008-12-29 01:55:35 +0000494 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000495}
496
497static int gsm48_cc_rx_status_enq(struct msgb *msg)
498{
499 return gsm48_cc_tx_status(msg->lchan);
500}
501
502static int gsm48_cc_rx_setup(struct msgb *msg)
503{
Harald Welte6f4b7532008-12-29 00:39:37 +0000504 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
505 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000506}
507
Harald Welte65e74cc2008-12-29 01:55:35 +0000508int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
509{
510 struct msgb *msg = gsm48_msgb_alloc();
511 struct gsm48_hdr *gh;
512 struct gsm_call *call = &lchan->call;
513
514 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
515
516 call->type = GSM_CT_MT;
517 msg->lchan = lchan;
518
519 gh->proto_discr = GSM48_PDISC_CC;
520 gh->msg_type = GSM48_MT_CC_SETUP;
521 gh->data[0] = 0x34;
522 gh->data[1] = 0x00;
523 gh->data[2] = 0x5c;
524 gh->data[3] = 0x04;
525 gh->data[4] = 0xb9;
526 gh->data[5] = 0x83;
527 gh->data[6] = 0x32;
528 gh->data[7] = 0x24;
529
530 DEBUGP(DCC, "Sending SETUP\n");
531
532 return gsm48_sendmsg(msg);
533}
534
Harald Welte4bc90a12008-12-27 16:32:52 +0000535static int gsm0408_rcv_cc(struct msgb *msg)
536{
537 struct gsm48_hdr *gh = msgb_l3(msg);
538 u_int8_t msg_type = gh->msg_type & 0xbf;
539 struct gsm_call *call = &msg->lchan->call;
540 int rc = 0;
541
542 switch (msg_type) {
543 case GSM48_MT_CC_CALL_CONF:
544 /* Response to SETUP */
545 DEBUGP(DCC, "CALL CONFIRM\n");
546 break;
547 case GSM48_MT_CC_RELEASE_COMPL:
548 /* Answer from MS to RELEASE */
549 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
550 call->state = GSM_CSTATE_NULL;
551 break;
552 case GSM48_MT_CC_ALERTING:
553 DEBUGP(DCC, "ALERTING\n");
554 break;
555 case GSM48_MT_CC_CONNECT:
556 DEBUGP(DCC, "CONNECT\n");
557 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000558 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
559 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000560 break;
561 case GSM48_MT_CC_CONNECT_ACK:
562 /* MO: Answer to CONNECT */
563 call->state = GSM_CSTATE_ACTIVE;
564 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
565 break;
566 case GSM48_MT_CC_RELEASE:
567 DEBUGP(DCC, "RELEASE\n");
568 /* need to respond with RELEASE_COMPLETE */
569 break;
570 case GSM48_MT_CC_STATUS_ENQ:
571 rc = gsm48_cc_rx_status_enq(msg);
572 break;
573 case GSM48_MT_CC_DISCONNECT:
574 /* Section 5.4.3.2 */
575 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
576 call->state = GSM_CSTATE_RELEASE_REQ;
577 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000578 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
579 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000580 break;
581 case GSM48_MT_CC_SETUP:
582 call->type = GSM_CT_MO;
583 call->state = GSM_CSTATE_INITIATED;
584 call->transaction_id = gh->proto_discr & 0xf0;
585 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000586 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
587 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000588 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
589 break;
590 case GSM48_MT_CC_EMERG_SETUP:
591 DEBUGP(DCC, "EMERGENCY SETUP\n");
592 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
593 break;
594 default:
595 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
596 msg_type);
597 break;
598 }
599
600 return rc;
601}
602
Harald Welte52b1f982008-12-23 20:25:15 +0000603/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
604int gsm0408_rcvmsg(struct msgb *msg)
605{
606 struct gsm48_hdr *gh = msgb_l3(msg);
607 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000608 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000609
610 switch (pdisc) {
611 case GSM48_PDISC_CC:
612 rc = gsm0408_rcv_cc(msg);
613 break;
614 case GSM48_PDISC_MM:
615 rc = gsm0408_rcv_mm(msg);
616 break;
617 case GSM48_PDISC_RR:
618 rc = gsm0408_rcv_rr(msg);
619 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000620 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000621 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000622 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000623 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000624 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000625 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
626 pdisc);
627 break;
628 default:
629 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
630 pdisc);
631 break;
632 }
633
634 return rc;
635}
Harald Welte8470bf22008-12-25 23:28:35 +0000636
637enum chreq_type {
638 CHREQ_T_EMERG_CALL,
639 CHREQ_T_CALL_REEST_TCH_F,
640 CHREQ_T_CALL_REEST_TCH_H,
641 CHREQ_T_CALL_REEST_TCH_H_DBL,
642 CHREQ_T_SDCCH,
643 CHREQ_T_TCH_F,
644 CHREQ_T_VOICE_CALL_TCH_H,
645 CHREQ_T_DATA_CALL_TCH_H,
646 CHREQ_T_LOCATION_UPD,
647 CHREQ_T_PAG_R_ANY,
648 CHREQ_T_PAG_R_TCH_F,
649 CHREQ_T_PAG_R_TCH_FH,
650};
651
652/* Section 9.1.8 / Table 9.9 */
653struct chreq {
654 u_int8_t val;
655 u_int8_t mask;
656 enum chreq_type type;
657};
658
659/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
660static const struct chreq chreq_type_neci1[] = {
661 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
662 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
663 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
664 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
665 { 0xe0, 0xe0, CHREQ_T_SDCCH },
666 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
667 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
668 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
669 { 0x10, 0xf0, CHREQ_T_SDCCH },
670 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
671 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
672 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
673};
674
675/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
676static const struct chreq chreq_type_neci0[] = {
677 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
678 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
679 { 0xe0, 0xe0, CHREQ_T_TCH_F },
680 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
681 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
682 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
683 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
684 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
685};
686
687static const enum gsm_chan_t ctype_by_chreq[] = {
688 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
689 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
690 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
691 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
692 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
693 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
694 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
695 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
696 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
697 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
698 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
699 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
700};
701
Harald Weltee14a57c2008-12-29 04:08:28 +0000702static const enum gsm_chreq_reason_t reason_by_chreq[] = {
703 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
704 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
705 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
706 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
707 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
708 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
709 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
710 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
711 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
712 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
713 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
714 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
715};
716
Harald Welte8470bf22008-12-25 23:28:35 +0000717enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
718{
719 int i;
720 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
721
722 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
723 const struct chreq *chr = &chreq_type_neci0[i];
724 if ((ra & chr->mask) == chr->val)
725 return ctype_by_chreq[chr->type];
726 }
727 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
728 return GSM_LCHAN_SDCCH;
729}
Harald Weltee14a57c2008-12-29 04:08:28 +0000730
731enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
732{
733 int i;
734 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
735
736 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
737 const struct chreq *chr = &chreq_type_neci0[i];
738 if ((ra & chr->mask) == chr->val)
739 return reason_by_chreq[chr->type];
740 }
741 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
742 return GSM_CHREQ_REASON_OTHER;
743}