blob: a1a8f37e87e91a103a6d726ac1c92860e2766041 [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>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Welte4b634542008-12-27 01:55:51 +000029#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000030
Harald Welte75a983f2008-12-27 21:34:06 +000031#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000032#include <openbsc/msgb.h>
33#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
35#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000036#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/gsm_04_08.h>
38#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000039
Harald Welte8470bf22008-12-25 23:28:35 +000040#define GSM48_ALLOC_SIZE 1024
41#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000042
Harald Welte65e74cc2008-12-29 01:55:35 +000043static int gsm48_tx_simple(struct gsm_lchan *lchan,
44 u_int8_t pdisc, u_int8_t msg_type);
45
Harald Welte52b1f982008-12-23 20:25:15 +000046struct gsm_lai {
47 u_int16_t mcc;
48 u_int16_t mnc;
49 u_int16_t lac;
50};
51
52static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
53{
54 u_int8_t dig[4];
55
56 /* MCC */
57 dig[1] = lai48->digits[0] & 0x0f;
58 dig[2] = lai48->digits[0] >> 4;
59 dig[3] = lai48->digits[1] & 0x0f;
60 lai->mcc = dig[3] * 100 + dig[2];
61
62 /* MNC */
63 dig[1] = lai48->digits[1] >> 4;
64 dig[2] = lai48->digits[2] & 0x0f;
65 dig[3] = lai48->digits[2] >> 4;
66 lai->mnc = dig[3] * 100 + dig[2];
67
68 lai->lac = lai48->lac;
69}
70
71static void to_bcd(u_int8_t *bcd, u_int16_t val)
72{
Harald Welte4b634542008-12-27 01:55:51 +000073 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000074 val = val / 10;
75 bcd[1] = val % 10;
76 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000077 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000078 val = val / 10;
79}
80
Holger Freyther17746612008-12-28 16:32:44 +000081void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +000082 u_int16_t mnc, u_int16_t lac)
83{
84 u_int8_t bcd[3];
85
86 to_bcd(bcd, mcc);
87 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
88 lai48->digits[1] = bcd[2];
89
90 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +000091 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
92#if 0
Harald Welte8470bf22008-12-25 23:28:35 +000093 lai48->digits[1] |= bcd[2] << 4;
94 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +000095#else
96 lai48->digits[1] |= 0xf << 4;
97 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
98#endif
Harald Welte52b1f982008-12-23 20:25:15 +000099
Harald Welte4b634542008-12-27 01:55:51 +0000100 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000101}
102
Harald Welte255539c2008-12-28 02:26:27 +0000103#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000104#define MID_TMSI_LEN (TMSI_LEN + 2)
105
Harald Welte255539c2008-12-28 02:26:27 +0000106int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000107{
Harald Welte65e74cc2008-12-29 01:55:35 +0000108 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000109
Harald Welte4b634542008-12-27 01:55:51 +0000110 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000111 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000112 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000113 *tptr = htonl(tmsi);
114
115 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000116}
117
Harald Welte8470bf22008-12-25 23:28:35 +0000118static struct msgb *gsm48_msgb_alloc(void)
119{
120 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
121}
122
Harald Welte65e74cc2008-12-29 01:55:35 +0000123static int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000124{
Harald Welte65e74cc2008-12-29 01:55:35 +0000125 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
126
127 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000128 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000129
Harald Welte65e74cc2008-12-29 01:55:35 +0000130 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
131 /* Send a 04.08 call control message, add transaction
132 * ID and TI flag */
133 gh->proto_discr |= msg->lchan->call.transaction_id;
134
135 /* GSM 04.07 Section 11.2.3.1.3 */
136 switch (msg->lchan->call.type) {
137 case GSM_CT_MO:
138 gh->proto_discr |= 0x80;
139 break;
140 case GSM_CT_MT:
141 break;
142 }
143 }
144 }
145
Harald Welte4b634542008-12-27 01:55:51 +0000146 msg->l3h = msg->data;
147
Harald Welte8470bf22008-12-25 23:28:35 +0000148 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000149}
150
Harald Welte52b1f982008-12-23 20:25:15 +0000151
152/* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000153int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000154{
Harald Welte8470bf22008-12-25 23:28:35 +0000155 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000156 struct gsm48_hdr *gh;
157
Harald Welte8470bf22008-12-25 23:28:35 +0000158 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000159
160 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
161 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000162 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000163 gh->data[0] = cause;
164
165 DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n");
166
Harald Welte65e74cc2008-12-29 01:55:35 +0000167 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000168}
169
170/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000171int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000172{
Harald Welte8470bf22008-12-25 23:28:35 +0000173 struct gsm_bts *bts = lchan->ts->trx->bts;
174 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000175 struct gsm48_hdr *gh;
176 struct gsm48_loc_area_id *lai;
177 u_int8_t *mid;
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));
182 gh->proto_discr = GSM48_PDISC_MM;
183 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
184
185 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000186 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000187 bts->network->network_code, bts->location_area_code);
188
189 mid = msgb_put(msg, MID_TMSI_LEN);
190 generate_mid_from_tmsi(mid, tmsi);
191
192 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
193
Harald Welte65e74cc2008-12-29 01:55:35 +0000194 gsm48_sendmsg(msg);
195
196 return gsm48_cc_tx_setup(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
198
Harald Weltefc977a82008-12-27 10:19:37 +0000199static char bcd2char(u_int8_t bcd)
200{
201 if (bcd < 0xa)
202 return '0' + bcd;
203 else
204 return 'A' + (bcd - 0xa);
205}
206
207/* 10.5.1.4 */
208static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
209{
210 int i;
211 u_int8_t mi_type;
212 char *str_cur = string;
213
214 mi_type = mi[0] & GSM_MI_TYPE_MASK;
215
216 switch (mi_type) {
217 case GSM_MI_TYPE_NONE:
218 break;
219 case GSM_MI_TYPE_TMSI:
220 for (i = 1; i < mi_len - 1; i++) {
221 if (str_cur + 2 >= string + str_len)
222 return str_cur - string;
223 *str_cur++ = bcd2char(mi[i] >> 4);
224 *str_cur++ = bcd2char(mi[i] & 0xf);
225 }
226 break;
227 case GSM_MI_TYPE_IMSI:
228 case GSM_MI_TYPE_IMEI:
229 case GSM_MI_TYPE_IMEISV:
230 if (mi[0] & GSM_MI_ODD)
231 *str_cur++ = bcd2char(mi[0] >> 4);
232
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] & 0xf);
237 *str_cur++ = bcd2char(mi[i] >> 4);
238 }
239 break;
240 default:
241 break;
242 }
243
244 *str_cur++ = '\0';
245 return str_cur - string;
246}
247
Harald Welte231ad4f2008-12-27 11:15:38 +0000248/* Chapter 9.2.10 */
249static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
250{
251 struct msgb *msg = gsm48_msgb_alloc();
252 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000253
Harald Welte231ad4f2008-12-27 11:15:38 +0000254 msg->lchan = lchan;
255
256 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
257 gh->proto_discr = GSM48_PDISC_MM;
258 gh->msg_type = GSM48_MT_MM_ID_REQ;
259 gh->data[0] = id_type;
260
Harald Welte65e74cc2008-12-29 01:55:35 +0000261 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000262}
263
264#define MI_SIZE 32
265
266/* Chapter 9.2.11 */
267static int mm_rx_id_resp(struct msgb *msg)
268{
269 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000270 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000271 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
272 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000273 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000274
275 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000276 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000277 mi_type, mi_string);
278
Harald Welte75a983f2008-12-27 21:34:06 +0000279 switch (mi_type) {
280 case GSM_MI_TYPE_IMSI:
281 if (!lchan->subscr)
282 lchan->subscr = db_create_subscriber(mi_string);
283 if (lchan->subscr && lchan->subscr->authorized) {
284 /* FIXME: check if we've recently received UPDATE REQUEST */
285 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte255539c2008-12-28 02:26:27 +0000286 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
Harald Welte75a983f2008-12-27 21:34:06 +0000287 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
288 }
289 break;
290 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000291 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000292 /* update subscribe <-> IMEI mapping */
293 if (lchan->subscr)
294 db_subscriber_assoc_imei(lchan->subscr, mi_string);
295 break;
296 }
297 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000298}
299
Harald Welte255539c2008-12-28 02:26:27 +0000300
301static void loc_upd_rej_cb(void *data)
302{
303 struct gsm_lchan *lchan = data;
304
305 gsm0408_loc_upd_rej(lchan, 0x16);
306 rsl_chan_release(lchan);
307}
308
Harald Welte231ad4f2008-12-27 11:15:38 +0000309#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000310/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000311static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000312{
Harald Welte8470bf22008-12-25 23:28:35 +0000313 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000314 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000315 struct gsm48_loc_upd_req *lu;
316 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000317 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000318 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000319 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000320 char mi_string[MI_SIZE];
321 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000322
Harald Welte8470bf22008-12-25 23:28:35 +0000323 lu = (struct gsm48_loc_upd_req *) gh->data;
324
325 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000326
Harald Weltefc977a82008-12-27 10:19:37 +0000327 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
328
Harald Welte231ad4f2008-12-27 11:15:38 +0000329 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000330 switch (mi_type) {
331 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000332 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000333 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000334 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000335 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000336 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000337 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000338 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000339 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000340 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000341 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000342 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000343 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000344 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000345 }
346 break;
347 case GSM_MI_TYPE_IMEI:
348 case GSM_MI_TYPE_IMEISV:
349 /* no sim card... FIXME: what to do ? */
350 fprintf(stderr, "Unimplemented mobile identity type\n");
351 break;
352 default:
353 fprintf(stderr, "Unknown mobile identity type\n");
354 break;
355 }
356
Harald Welte255539c2008-12-28 02:26:27 +0000357 lchan->subscr = subscr;
358
Harald Welte75a983f2008-12-27 21:34:06 +0000359 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000360 /* 0x16 is congestion */
Harald Welte255539c2008-12-28 02:26:27 +0000361 lchan->timer.cb = loc_upd_rej_cb;
362 lchan->timer.data = lchan;
363 schedule_timer(&lchan->timer, 1, 0);
364 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000365 }
366
Harald Welte75a983f2008-12-27 21:34:06 +0000367 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000368 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000369
Harald Welte255539c2008-12-28 02:26:27 +0000370 tmsi = strtoul(subscr->tmsi, NULL, 10);
371
372 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000373}
374
Harald Welte4b634542008-12-27 01:55:51 +0000375static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
376{
Harald Welte4b634542008-12-27 01:55:51 +0000377 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000378 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000379}
380
381static int gsm48_rx_mm_serv_req(struct msgb *msg)
382{
383 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000384 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000385
Harald Welte65e74cc2008-12-29 01:55:35 +0000386 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000387
Harald Welte4b634542008-12-27 01:55:51 +0000388 return gsm48_tx_mm_serv_ack(msg->lchan);
389}
390
Harald Welte52b1f982008-12-23 20:25:15 +0000391static int gsm0408_rcv_mm(struct msgb *msg)
392{
393 struct gsm48_hdr *gh = msgb_l3(msg);
394 int rc;
395
396 switch (gh->msg_type & 0xbf) {
397 case GSM48_MT_MM_LOC_UPD_REQUEST:
398 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000399 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000400 break;
401 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000402 rc = mm_rx_id_resp(msg);
403 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000404 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000405 rc = gsm48_rx_mm_serv_req(msg);
406 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000407 case GSM48_MT_MM_STATUS:
408 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
409 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000410 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000411 case GSM48_MT_MM_TMSI_REALL_COMPL:
412 case GSM48_MT_MM_AUTH_RESP:
413 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000414 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
415 gh->msg_type);
416 break;
417 default:
418 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
419 gh->msg_type);
420 break;
421 }
422
423 return rc;
424}
425static int gsm0408_rcv_rr(struct msgb *msg)
426{
427 struct gsm48_hdr *gh = msgb_l3(msg);
428
429 switch (gh->msg_type) {
430 case GSM48_MT_RR_CLSM_CHG:
431 DEBUGP(DRR, "CLASSMARK CHANGE\n");
432 /* FIXME: what to do ?!? */
433 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000434 case GSM48_MT_RR_GPRS_SUSP_REQ:
435 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
436 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000437 case GSM48_MT_RR_PAG_RESP:
438 default:
439 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
440 gh->msg_type);
441 break;
442 }
443
444 return 0;
445}
446
Harald Welte4bc90a12008-12-27 16:32:52 +0000447/* Call Control */
448
Harald Welte4bc90a12008-12-27 16:32:52 +0000449static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
450{
451 struct msgb *msg = gsm48_msgb_alloc();
452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
453 u_int8_t *cause, *call_state;
454
Harald Welte65e74cc2008-12-29 01:55:35 +0000455 gh->proto_discr = GSM48_PDISC_CC;
456
Harald Welte4bc90a12008-12-27 16:32:52 +0000457 msg->lchan = lchan;
458
Harald Welte4bc90a12008-12-27 16:32:52 +0000459 gh->msg_type = GSM48_MT_CC_STATUS;
460
461 cause = msgb_put(msg, 3);
462 cause[0] = 2;
463 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
464 cause[2] = 0x80 | 30; /* response to status inquiry */
465
466 call_state = msgb_put(msg, 1);
467 call_state[0] = 0xc0 | 0x00;
468
Harald Welte65e74cc2008-12-29 01:55:35 +0000469 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000470}
471
Harald Welte6f4b7532008-12-29 00:39:37 +0000472static int gsm48_tx_simple(struct gsm_lchan *lchan,
473 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000474{
475 struct msgb *msg = gsm48_msgb_alloc();
476 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
477
478 msg->lchan = lchan;
479
Harald Welte6f4b7532008-12-29 00:39:37 +0000480 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000481 gh->msg_type = msg_type;
482
Harald Welte65e74cc2008-12-29 01:55:35 +0000483 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000484}
485
486static int gsm48_cc_rx_status_enq(struct msgb *msg)
487{
488 return gsm48_cc_tx_status(msg->lchan);
489}
490
491static int gsm48_cc_rx_setup(struct msgb *msg)
492{
Harald Welte6f4b7532008-12-29 00:39:37 +0000493 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
494 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000495}
496
Harald Welte65e74cc2008-12-29 01:55:35 +0000497int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
498{
499 struct msgb *msg = gsm48_msgb_alloc();
500 struct gsm48_hdr *gh;
501 struct gsm_call *call = &lchan->call;
502
503 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
504
505 call->type = GSM_CT_MT;
506 msg->lchan = lchan;
507
508 gh->proto_discr = GSM48_PDISC_CC;
509 gh->msg_type = GSM48_MT_CC_SETUP;
510 gh->data[0] = 0x34;
511 gh->data[1] = 0x00;
512 gh->data[2] = 0x5c;
513 gh->data[3] = 0x04;
514 gh->data[4] = 0xb9;
515 gh->data[5] = 0x83;
516 gh->data[6] = 0x32;
517 gh->data[7] = 0x24;
518
519 DEBUGP(DCC, "Sending SETUP\n");
520
521 return gsm48_sendmsg(msg);
522}
523
Harald Welte4bc90a12008-12-27 16:32:52 +0000524static int gsm0408_rcv_cc(struct msgb *msg)
525{
526 struct gsm48_hdr *gh = msgb_l3(msg);
527 u_int8_t msg_type = gh->msg_type & 0xbf;
528 struct gsm_call *call = &msg->lchan->call;
529 int rc = 0;
530
531 switch (msg_type) {
532 case GSM48_MT_CC_CALL_CONF:
533 /* Response to SETUP */
534 DEBUGP(DCC, "CALL CONFIRM\n");
535 break;
536 case GSM48_MT_CC_RELEASE_COMPL:
537 /* Answer from MS to RELEASE */
538 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
539 call->state = GSM_CSTATE_NULL;
540 break;
541 case GSM48_MT_CC_ALERTING:
542 DEBUGP(DCC, "ALERTING\n");
543 break;
544 case GSM48_MT_CC_CONNECT:
545 DEBUGP(DCC, "CONNECT\n");
546 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000547 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
548 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000549 break;
550 case GSM48_MT_CC_CONNECT_ACK:
551 /* MO: Answer to CONNECT */
552 call->state = GSM_CSTATE_ACTIVE;
553 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
554 break;
555 case GSM48_MT_CC_RELEASE:
556 DEBUGP(DCC, "RELEASE\n");
557 /* need to respond with RELEASE_COMPLETE */
558 break;
559 case GSM48_MT_CC_STATUS_ENQ:
560 rc = gsm48_cc_rx_status_enq(msg);
561 break;
562 case GSM48_MT_CC_DISCONNECT:
563 /* Section 5.4.3.2 */
564 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
565 call->state = GSM_CSTATE_RELEASE_REQ;
566 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000567 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
568 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000569 break;
570 case GSM48_MT_CC_SETUP:
571 call->type = GSM_CT_MO;
572 call->state = GSM_CSTATE_INITIATED;
573 call->transaction_id = gh->proto_discr & 0xf0;
574 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000575 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
576 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000577 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
578 break;
579 case GSM48_MT_CC_EMERG_SETUP:
580 DEBUGP(DCC, "EMERGENCY SETUP\n");
581 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
582 break;
583 default:
584 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
585 msg_type);
586 break;
587 }
588
589 return rc;
590}
591
Harald Welte52b1f982008-12-23 20:25:15 +0000592/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
593int gsm0408_rcvmsg(struct msgb *msg)
594{
595 struct gsm48_hdr *gh = msgb_l3(msg);
596 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000597 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000598
599 switch (pdisc) {
600 case GSM48_PDISC_CC:
601 rc = gsm0408_rcv_cc(msg);
602 break;
603 case GSM48_PDISC_MM:
604 rc = gsm0408_rcv_mm(msg);
605 break;
606 case GSM48_PDISC_RR:
607 rc = gsm0408_rcv_rr(msg);
608 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000609 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000610 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000611 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000612 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000613 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000614 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
615 pdisc);
616 break;
617 default:
618 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
619 pdisc);
620 break;
621 }
622
623 return rc;
624}
Harald Welte8470bf22008-12-25 23:28:35 +0000625
626enum chreq_type {
627 CHREQ_T_EMERG_CALL,
628 CHREQ_T_CALL_REEST_TCH_F,
629 CHREQ_T_CALL_REEST_TCH_H,
630 CHREQ_T_CALL_REEST_TCH_H_DBL,
631 CHREQ_T_SDCCH,
632 CHREQ_T_TCH_F,
633 CHREQ_T_VOICE_CALL_TCH_H,
634 CHREQ_T_DATA_CALL_TCH_H,
635 CHREQ_T_LOCATION_UPD,
636 CHREQ_T_PAG_R_ANY,
637 CHREQ_T_PAG_R_TCH_F,
638 CHREQ_T_PAG_R_TCH_FH,
639};
640
641/* Section 9.1.8 / Table 9.9 */
642struct chreq {
643 u_int8_t val;
644 u_int8_t mask;
645 enum chreq_type type;
646};
647
648/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
649static const struct chreq chreq_type_neci1[] = {
650 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
651 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
652 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
653 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
654 { 0xe0, 0xe0, CHREQ_T_SDCCH },
655 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
656 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
657 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
658 { 0x10, 0xf0, CHREQ_T_SDCCH },
659 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
660 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
661 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
662};
663
664/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
665static const struct chreq chreq_type_neci0[] = {
666 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
667 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
668 { 0xe0, 0xe0, CHREQ_T_TCH_F },
669 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
670 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
671 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
672 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
673 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
674};
675
676static const enum gsm_chan_t ctype_by_chreq[] = {
677 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
678 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
679 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
680 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
681 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
682 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
683 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
684 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
685 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
686 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
687 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
688 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
689};
690
Harald Weltee14a57c2008-12-29 04:08:28 +0000691static const enum gsm_chreq_reason_t reason_by_chreq[] = {
692 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
693 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
694 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
695 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
696 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
697 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
698 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
699 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
700 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
701 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
702 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
703 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
704};
705
Harald Welte8470bf22008-12-25 23:28:35 +0000706enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
707{
708 int i;
709 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
710
711 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
712 const struct chreq *chr = &chreq_type_neci0[i];
713 if ((ra & chr->mask) == chr->val)
714 return ctype_by_chreq[chr->type];
715 }
716 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
717 return GSM_LCHAN_SDCCH;
718}
Harald Weltee14a57c2008-12-29 04:08:28 +0000719
720enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
721{
722 int i;
723 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
724
725 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
726 const struct chreq *chr = &chreq_type_neci0[i];
727 if ((ra & chr->mask) == chr->val)
728 return reason_by_chreq[chr->type];
729 }
730 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
731 return GSM_CHREQ_REASON_OTHER;
732}