blob: 64d69d880291c001842464f2f9a4ac50a3697b4c [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
Daniel Willmannfad5d0d2008-12-29 16:04:14 +0000200 gsm0411_send_sms(lchan, 0);
201
Holger Freytherba4d28a2008-12-29 06:23:44 +0000202 /* free the channel afterwards */
Holger Freyther07cc8d82008-12-29 06:23:46 +0000203 ret = rsl_chan_release(lchan);
204
205 /* inform the upper layer on the progress */
206 if (bts->network->update_request_accepted)
207 (*bts->network->update_request_accepted)(bts, tmsi);
208
209 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000210}
211
Harald Weltefc977a82008-12-27 10:19:37 +0000212static char bcd2char(u_int8_t bcd)
213{
214 if (bcd < 0xa)
215 return '0' + bcd;
216 else
217 return 'A' + (bcd - 0xa);
218}
219
220/* 10.5.1.4 */
221static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
222{
223 int i;
224 u_int8_t mi_type;
225 char *str_cur = string;
226
227 mi_type = mi[0] & GSM_MI_TYPE_MASK;
228
229 switch (mi_type) {
230 case GSM_MI_TYPE_NONE:
231 break;
232 case GSM_MI_TYPE_TMSI:
233 for (i = 1; i < mi_len - 1; i++) {
234 if (str_cur + 2 >= string + str_len)
235 return str_cur - string;
236 *str_cur++ = bcd2char(mi[i] >> 4);
237 *str_cur++ = bcd2char(mi[i] & 0xf);
238 }
239 break;
240 case GSM_MI_TYPE_IMSI:
241 case GSM_MI_TYPE_IMEI:
242 case GSM_MI_TYPE_IMEISV:
243 if (mi[0] & GSM_MI_ODD)
244 *str_cur++ = bcd2char(mi[0] >> 4);
245
246 for (i = 1; i < mi_len - 1; i++) {
247 if (str_cur + 2 >= string + str_len)
248 return str_cur - string;
249 *str_cur++ = bcd2char(mi[i] & 0xf);
250 *str_cur++ = bcd2char(mi[i] >> 4);
251 }
252 break;
253 default:
254 break;
255 }
256
257 *str_cur++ = '\0';
258 return str_cur - string;
259}
260
Harald Welte231ad4f2008-12-27 11:15:38 +0000261/* Chapter 9.2.10 */
262static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
263{
264 struct msgb *msg = gsm48_msgb_alloc();
265 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000266
Harald Welte231ad4f2008-12-27 11:15:38 +0000267 msg->lchan = lchan;
268
269 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
270 gh->proto_discr = GSM48_PDISC_MM;
271 gh->msg_type = GSM48_MT_MM_ID_REQ;
272 gh->data[0] = id_type;
273
Harald Welte65e74cc2008-12-29 01:55:35 +0000274 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000275}
276
277#define MI_SIZE 32
278
279/* Chapter 9.2.11 */
280static int mm_rx_id_resp(struct msgb *msg)
281{
282 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000283 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000284 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
285 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000286 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000287
288 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000289 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000290 mi_type, mi_string);
291
Harald Welte75a983f2008-12-27 21:34:06 +0000292 switch (mi_type) {
293 case GSM_MI_TYPE_IMSI:
294 if (!lchan->subscr)
295 lchan->subscr = db_create_subscriber(mi_string);
296 if (lchan->subscr && lchan->subscr->authorized) {
297 /* FIXME: check if we've recently received UPDATE REQUEST */
298 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte255539c2008-12-28 02:26:27 +0000299 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
Harald Welte75a983f2008-12-27 21:34:06 +0000300 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
301 }
302 break;
303 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000304 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000305 /* update subscribe <-> IMEI mapping */
306 if (lchan->subscr)
307 db_subscriber_assoc_imei(lchan->subscr, mi_string);
308 break;
309 }
310 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000311}
312
Harald Welte255539c2008-12-28 02:26:27 +0000313
314static void loc_upd_rej_cb(void *data)
315{
316 struct gsm_lchan *lchan = data;
317
318 gsm0408_loc_upd_rej(lchan, 0x16);
319 rsl_chan_release(lchan);
320}
321
Harald Welte231ad4f2008-12-27 11:15:38 +0000322#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000323/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000324static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000325{
Harald Welte8470bf22008-12-25 23:28:35 +0000326 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000327 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000328 struct gsm48_loc_upd_req *lu;
329 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000330 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000331 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000332 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000333 char mi_string[MI_SIZE];
334 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000335
Harald Welte8470bf22008-12-25 23:28:35 +0000336 lu = (struct gsm48_loc_upd_req *) gh->data;
337
338 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000339
Harald Weltefc977a82008-12-27 10:19:37 +0000340 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
341
Harald Welte231ad4f2008-12-27 11:15:38 +0000342 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000343 switch (mi_type) {
344 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000345 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000346 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000347 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000348 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000349 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000350 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000351 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000352 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000353 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000354 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000355 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000356 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000357 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000358 }
359 break;
360 case GSM_MI_TYPE_IMEI:
361 case GSM_MI_TYPE_IMEISV:
362 /* no sim card... FIXME: what to do ? */
363 fprintf(stderr, "Unimplemented mobile identity type\n");
364 break;
365 default:
366 fprintf(stderr, "Unknown mobile identity type\n");
367 break;
368 }
369
Harald Welte255539c2008-12-28 02:26:27 +0000370 lchan->subscr = subscr;
371
Harald Welte75a983f2008-12-27 21:34:06 +0000372 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000373 /* 0x16 is congestion */
Harald Welte255539c2008-12-28 02:26:27 +0000374 lchan->timer.cb = loc_upd_rej_cb;
375 lchan->timer.data = lchan;
376 schedule_timer(&lchan->timer, 1, 0);
377 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000378 }
379
Harald Welte75a983f2008-12-27 21:34:06 +0000380 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000381 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000382
Harald Welte255539c2008-12-28 02:26:27 +0000383 tmsi = strtoul(subscr->tmsi, NULL, 10);
384
385 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000386}
387
Harald Welte4b634542008-12-27 01:55:51 +0000388static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
389{
Harald Welte4b634542008-12-27 01:55:51 +0000390 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000391 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000392}
393
394static int gsm48_rx_mm_serv_req(struct msgb *msg)
395{
396 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000397 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000398
Harald Welte65e74cc2008-12-29 01:55:35 +0000399 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000400
Harald Welte4b634542008-12-27 01:55:51 +0000401 return gsm48_tx_mm_serv_ack(msg->lchan);
402}
403
Harald Welte52b1f982008-12-23 20:25:15 +0000404static int gsm0408_rcv_mm(struct msgb *msg)
405{
406 struct gsm48_hdr *gh = msgb_l3(msg);
407 int rc;
408
409 switch (gh->msg_type & 0xbf) {
410 case GSM48_MT_MM_LOC_UPD_REQUEST:
411 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000412 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000413 break;
414 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000415 rc = mm_rx_id_resp(msg);
416 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000417 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000418 rc = gsm48_rx_mm_serv_req(msg);
419 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000420 case GSM48_MT_MM_STATUS:
421 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
422 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000423 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000424 case GSM48_MT_MM_TMSI_REALL_COMPL:
425 case GSM48_MT_MM_AUTH_RESP:
426 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000427 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
428 gh->msg_type);
429 break;
430 default:
431 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
432 gh->msg_type);
433 break;
434 }
435
436 return rc;
437}
438static int gsm0408_rcv_rr(struct msgb *msg)
439{
440 struct gsm48_hdr *gh = msgb_l3(msg);
441
442 switch (gh->msg_type) {
443 case GSM48_MT_RR_CLSM_CHG:
444 DEBUGP(DRR, "CLASSMARK CHANGE\n");
445 /* FIXME: what to do ?!? */
446 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000447 case GSM48_MT_RR_GPRS_SUSP_REQ:
448 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
449 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000450 case GSM48_MT_RR_PAG_RESP:
451 default:
452 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
453 gh->msg_type);
454 break;
455 }
456
457 return 0;
458}
459
Harald Welte4bc90a12008-12-27 16:32:52 +0000460/* Call Control */
461
Harald Welte4bc90a12008-12-27 16:32:52 +0000462static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
463{
464 struct msgb *msg = gsm48_msgb_alloc();
465 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
466 u_int8_t *cause, *call_state;
467
Harald Welte65e74cc2008-12-29 01:55:35 +0000468 gh->proto_discr = GSM48_PDISC_CC;
469
Harald Welte4bc90a12008-12-27 16:32:52 +0000470 msg->lchan = lchan;
471
Harald Welte4bc90a12008-12-27 16:32:52 +0000472 gh->msg_type = GSM48_MT_CC_STATUS;
473
474 cause = msgb_put(msg, 3);
475 cause[0] = 2;
476 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
477 cause[2] = 0x80 | 30; /* response to status inquiry */
478
479 call_state = msgb_put(msg, 1);
480 call_state[0] = 0xc0 | 0x00;
481
Harald Welte65e74cc2008-12-29 01:55:35 +0000482 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000483}
484
Harald Welte6f4b7532008-12-29 00:39:37 +0000485static int gsm48_tx_simple(struct gsm_lchan *lchan,
486 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000487{
488 struct msgb *msg = gsm48_msgb_alloc();
489 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
490
491 msg->lchan = lchan;
492
Harald Welte6f4b7532008-12-29 00:39:37 +0000493 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000494 gh->msg_type = msg_type;
495
Harald Welte65e74cc2008-12-29 01:55:35 +0000496 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000497}
498
499static int gsm48_cc_rx_status_enq(struct msgb *msg)
500{
501 return gsm48_cc_tx_status(msg->lchan);
502}
503
504static int gsm48_cc_rx_setup(struct msgb *msg)
505{
Harald Welte6f4b7532008-12-29 00:39:37 +0000506 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
507 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000508}
509
Harald Welte65e74cc2008-12-29 01:55:35 +0000510int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
511{
512 struct msgb *msg = gsm48_msgb_alloc();
513 struct gsm48_hdr *gh;
514 struct gsm_call *call = &lchan->call;
515
516 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
517
518 call->type = GSM_CT_MT;
519 msg->lchan = lchan;
520
521 gh->proto_discr = GSM48_PDISC_CC;
522 gh->msg_type = GSM48_MT_CC_SETUP;
523 gh->data[0] = 0x34;
524 gh->data[1] = 0x00;
525 gh->data[2] = 0x5c;
526 gh->data[3] = 0x04;
527 gh->data[4] = 0xb9;
528 gh->data[5] = 0x83;
529 gh->data[6] = 0x32;
530 gh->data[7] = 0x24;
531
532 DEBUGP(DCC, "Sending SETUP\n");
533
534 return gsm48_sendmsg(msg);
535}
536
Harald Welte4bc90a12008-12-27 16:32:52 +0000537static int gsm0408_rcv_cc(struct msgb *msg)
538{
539 struct gsm48_hdr *gh = msgb_l3(msg);
540 u_int8_t msg_type = gh->msg_type & 0xbf;
541 struct gsm_call *call = &msg->lchan->call;
Holger Freyther2eafef52008-12-29 06:42:17 +0000542 struct gsm_network *network = msg->lchan->ts->trx->bts->network;
Harald Welte4bc90a12008-12-27 16:32:52 +0000543 int rc = 0;
544
545 switch (msg_type) {
546 case GSM48_MT_CC_CALL_CONF:
547 /* Response to SETUP */
548 DEBUGP(DCC, "CALL CONFIRM\n");
549 break;
550 case GSM48_MT_CC_RELEASE_COMPL:
551 /* Answer from MS to RELEASE */
552 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
553 call->state = GSM_CSTATE_NULL;
Holger Freyther2eafef52008-12-29 06:42:17 +0000554 if (network->call_released)
555 (*network->call_released)(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000556 break;
557 case GSM48_MT_CC_ALERTING:
558 DEBUGP(DCC, "ALERTING\n");
559 break;
560 case GSM48_MT_CC_CONNECT:
561 DEBUGP(DCC, "CONNECT\n");
562 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000563 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
564 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000565 break;
566 case GSM48_MT_CC_CONNECT_ACK:
567 /* MO: Answer to CONNECT */
568 call->state = GSM_CSTATE_ACTIVE;
569 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
570 break;
571 case GSM48_MT_CC_RELEASE:
572 DEBUGP(DCC, "RELEASE\n");
573 /* need to respond with RELEASE_COMPLETE */
574 break;
575 case GSM48_MT_CC_STATUS_ENQ:
576 rc = gsm48_cc_rx_status_enq(msg);
577 break;
578 case GSM48_MT_CC_DISCONNECT:
579 /* Section 5.4.3.2 */
580 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
581 call->state = GSM_CSTATE_RELEASE_REQ;
582 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000583 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
584 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000585 break;
586 case GSM48_MT_CC_SETUP:
587 call->type = GSM_CT_MO;
588 call->state = GSM_CSTATE_INITIATED;
589 call->transaction_id = gh->proto_discr & 0xf0;
590 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000591 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
592 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000593 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
594 break;
595 case GSM48_MT_CC_EMERG_SETUP:
596 DEBUGP(DCC, "EMERGENCY SETUP\n");
597 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
598 break;
599 default:
600 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
601 msg_type);
602 break;
603 }
604
605 return rc;
606}
607
Harald Welte52b1f982008-12-23 20:25:15 +0000608/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
609int gsm0408_rcvmsg(struct msgb *msg)
610{
611 struct gsm48_hdr *gh = msgb_l3(msg);
612 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000613 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000614
615 switch (pdisc) {
616 case GSM48_PDISC_CC:
617 rc = gsm0408_rcv_cc(msg);
618 break;
619 case GSM48_PDISC_MM:
620 rc = gsm0408_rcv_mm(msg);
621 break;
622 case GSM48_PDISC_RR:
623 rc = gsm0408_rcv_rr(msg);
624 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000625 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000626 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000627 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000628 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000629 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000630 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
631 pdisc);
632 break;
633 default:
634 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
635 pdisc);
636 break;
637 }
638
639 return rc;
640}
Harald Welte8470bf22008-12-25 23:28:35 +0000641
642enum chreq_type {
643 CHREQ_T_EMERG_CALL,
644 CHREQ_T_CALL_REEST_TCH_F,
645 CHREQ_T_CALL_REEST_TCH_H,
646 CHREQ_T_CALL_REEST_TCH_H_DBL,
647 CHREQ_T_SDCCH,
648 CHREQ_T_TCH_F,
649 CHREQ_T_VOICE_CALL_TCH_H,
650 CHREQ_T_DATA_CALL_TCH_H,
651 CHREQ_T_LOCATION_UPD,
652 CHREQ_T_PAG_R_ANY,
653 CHREQ_T_PAG_R_TCH_F,
654 CHREQ_T_PAG_R_TCH_FH,
655};
656
657/* Section 9.1.8 / Table 9.9 */
658struct chreq {
659 u_int8_t val;
660 u_int8_t mask;
661 enum chreq_type type;
662};
663
664/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
665static const struct chreq chreq_type_neci1[] = {
666 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
667 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
668 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
669 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
670 { 0xe0, 0xe0, CHREQ_T_SDCCH },
671 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
672 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
673 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
674 { 0x10, 0xf0, CHREQ_T_SDCCH },
675 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
676 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
677 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
678};
679
680/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
681static const struct chreq chreq_type_neci0[] = {
682 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
683 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
684 { 0xe0, 0xe0, CHREQ_T_TCH_F },
685 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
686 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
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
692static const enum gsm_chan_t ctype_by_chreq[] = {
693 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
694 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
695 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
696 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
697 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
698 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
699 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
700 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
701 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
702 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
703 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
704 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
705};
706
Harald Weltee14a57c2008-12-29 04:08:28 +0000707static const enum gsm_chreq_reason_t reason_by_chreq[] = {
708 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
709 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
710 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
711 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
712 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
713 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
714 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
715 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
716 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
717 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
718 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
719 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
720};
721
Harald Welte8470bf22008-12-25 23:28:35 +0000722enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
723{
724 int i;
725 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
726
727 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
728 const struct chreq *chr = &chreq_type_neci0[i];
729 if ((ra & chr->mask) == chr->val)
730 return ctype_by_chreq[chr->type];
731 }
732 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
733 return GSM_LCHAN_SDCCH;
734}
Harald Weltee14a57c2008-12-29 04:08:28 +0000735
736enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
737{
738 int i;
739 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
740
741 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
742 const struct chreq *chr = &chreq_type_neci0[i];
743 if ((ra & chr->mask) == chr->val)
744 return reason_by_chreq[chr->type];
745 }
746 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
747 return GSM_CHREQ_REASON_OTHER;
748}