blob: c367102fb6f85b2e33ba75f1faac2b1721d2085c [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
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000196 gsm0411_send_sms(lchan, 0);
197
Harald Welte65e74cc2008-12-29 01:55:35 +0000198 return gsm48_cc_tx_setup(lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000199}
200
Harald Weltefc977a82008-12-27 10:19:37 +0000201static char bcd2char(u_int8_t bcd)
202{
203 if (bcd < 0xa)
204 return '0' + bcd;
205 else
206 return 'A' + (bcd - 0xa);
207}
208
209/* 10.5.1.4 */
210static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
211{
212 int i;
213 u_int8_t mi_type;
214 char *str_cur = string;
215
216 mi_type = mi[0] & GSM_MI_TYPE_MASK;
217
218 switch (mi_type) {
219 case GSM_MI_TYPE_NONE:
220 break;
221 case GSM_MI_TYPE_TMSI:
222 for (i = 1; i < mi_len - 1; i++) {
223 if (str_cur + 2 >= string + str_len)
224 return str_cur - string;
225 *str_cur++ = bcd2char(mi[i] >> 4);
226 *str_cur++ = bcd2char(mi[i] & 0xf);
227 }
228 break;
229 case GSM_MI_TYPE_IMSI:
230 case GSM_MI_TYPE_IMEI:
231 case GSM_MI_TYPE_IMEISV:
232 if (mi[0] & GSM_MI_ODD)
233 *str_cur++ = bcd2char(mi[0] >> 4);
234
235 for (i = 1; i < mi_len - 1; i++) {
236 if (str_cur + 2 >= string + str_len)
237 return str_cur - string;
238 *str_cur++ = bcd2char(mi[i] & 0xf);
239 *str_cur++ = bcd2char(mi[i] >> 4);
240 }
241 break;
242 default:
243 break;
244 }
245
246 *str_cur++ = '\0';
247 return str_cur - string;
248}
249
Harald Welte231ad4f2008-12-27 11:15:38 +0000250/* Chapter 9.2.10 */
251static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
252{
253 struct msgb *msg = gsm48_msgb_alloc();
254 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000255
Harald Welte231ad4f2008-12-27 11:15:38 +0000256 msg->lchan = lchan;
257
258 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
259 gh->proto_discr = GSM48_PDISC_MM;
260 gh->msg_type = GSM48_MT_MM_ID_REQ;
261 gh->data[0] = id_type;
262
Harald Welte65e74cc2008-12-29 01:55:35 +0000263 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000264}
265
266#define MI_SIZE 32
267
268/* Chapter 9.2.11 */
269static int mm_rx_id_resp(struct msgb *msg)
270{
271 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000272 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000273 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
274 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000275 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000276
277 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000278 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 mi_type, mi_string);
280
Harald Welte75a983f2008-12-27 21:34:06 +0000281 switch (mi_type) {
282 case GSM_MI_TYPE_IMSI:
283 if (!lchan->subscr)
284 lchan->subscr = db_create_subscriber(mi_string);
285 if (lchan->subscr && lchan->subscr->authorized) {
286 /* FIXME: check if we've recently received UPDATE REQUEST */
287 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte255539c2008-12-28 02:26:27 +0000288 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
Harald Welte75a983f2008-12-27 21:34:06 +0000289 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
290 }
291 break;
292 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000293 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000294 /* update subscribe <-> IMEI mapping */
295 if (lchan->subscr)
296 db_subscriber_assoc_imei(lchan->subscr, mi_string);
297 break;
298 }
299 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000300}
301
Harald Welte255539c2008-12-28 02:26:27 +0000302
303static void loc_upd_rej_cb(void *data)
304{
305 struct gsm_lchan *lchan = data;
306
307 gsm0408_loc_upd_rej(lchan, 0x16);
308 rsl_chan_release(lchan);
309}
310
Harald Welte231ad4f2008-12-27 11:15:38 +0000311#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000312/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000313static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000314{
Harald Welte8470bf22008-12-25 23:28:35 +0000315 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000316 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000317 struct gsm48_loc_upd_req *lu;
318 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000319 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000320 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000321 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000322 char mi_string[MI_SIZE];
323 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000324
Harald Welte8470bf22008-12-25 23:28:35 +0000325 lu = (struct gsm48_loc_upd_req *) gh->data;
326
327 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000328
Harald Weltefc977a82008-12-27 10:19:37 +0000329 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
330
Harald Welte231ad4f2008-12-27 11:15:38 +0000331 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000332 switch (mi_type) {
333 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000334 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000335 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000336 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000337 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000338 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000339 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000340 /* we always want the IMEI, too */
Harald Welte255539c2008-12-28 02:26:27 +0000341 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000342 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000343 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000344 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000345 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000346 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000347 }
348 break;
349 case GSM_MI_TYPE_IMEI:
350 case GSM_MI_TYPE_IMEISV:
351 /* no sim card... FIXME: what to do ? */
352 fprintf(stderr, "Unimplemented mobile identity type\n");
353 break;
354 default:
355 fprintf(stderr, "Unknown mobile identity type\n");
356 break;
357 }
358
Harald Welte255539c2008-12-28 02:26:27 +0000359 lchan->subscr = subscr;
360
Harald Welte75a983f2008-12-27 21:34:06 +0000361 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000362 /* 0x16 is congestion */
Harald Welte255539c2008-12-28 02:26:27 +0000363 lchan->timer.cb = loc_upd_rej_cb;
364 lchan->timer.data = lchan;
365 schedule_timer(&lchan->timer, 1, 0);
366 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000367 }
368
Harald Welte75a983f2008-12-27 21:34:06 +0000369 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000370 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000371
Harald Welte255539c2008-12-28 02:26:27 +0000372 tmsi = strtoul(subscr->tmsi, NULL, 10);
373
374 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000375}
376
Harald Welte4b634542008-12-27 01:55:51 +0000377static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
378{
Harald Welte4b634542008-12-27 01:55:51 +0000379 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000380 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000381}
382
383static int gsm48_rx_mm_serv_req(struct msgb *msg)
384{
385 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000386 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000387
Harald Welte65e74cc2008-12-29 01:55:35 +0000388 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
Harald Weltebcae43f2008-12-27 21:45:37 +0000389
Harald Welte4b634542008-12-27 01:55:51 +0000390 return gsm48_tx_mm_serv_ack(msg->lchan);
391}
392
Harald Welte52b1f982008-12-23 20:25:15 +0000393static int gsm0408_rcv_mm(struct msgb *msg)
394{
395 struct gsm48_hdr *gh = msgb_l3(msg);
396 int rc;
397
398 switch (gh->msg_type & 0xbf) {
399 case GSM48_MT_MM_LOC_UPD_REQUEST:
400 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000401 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000402 break;
403 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000404 rc = mm_rx_id_resp(msg);
405 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000406 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000407 rc = gsm48_rx_mm_serv_req(msg);
408 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000409 case GSM48_MT_MM_STATUS:
410 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
411 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000412 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000413 case GSM48_MT_MM_TMSI_REALL_COMPL:
414 case GSM48_MT_MM_AUTH_RESP:
415 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000416 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
417 gh->msg_type);
418 break;
419 default:
420 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
421 gh->msg_type);
422 break;
423 }
424
425 return rc;
426}
427static int gsm0408_rcv_rr(struct msgb *msg)
428{
429 struct gsm48_hdr *gh = msgb_l3(msg);
430
431 switch (gh->msg_type) {
432 case GSM48_MT_RR_CLSM_CHG:
433 DEBUGP(DRR, "CLASSMARK CHANGE\n");
434 /* FIXME: what to do ?!? */
435 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000436 case GSM48_MT_RR_GPRS_SUSP_REQ:
437 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
438 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000439 case GSM48_MT_RR_PAG_RESP:
440 default:
441 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
442 gh->msg_type);
443 break;
444 }
445
446 return 0;
447}
448
Harald Welte4bc90a12008-12-27 16:32:52 +0000449/* Call Control */
450
Harald Welte4bc90a12008-12-27 16:32:52 +0000451static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
452{
453 struct msgb *msg = gsm48_msgb_alloc();
454 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
455 u_int8_t *cause, *call_state;
456
Harald Welte65e74cc2008-12-29 01:55:35 +0000457 gh->proto_discr = GSM48_PDISC_CC;
458
Harald Welte4bc90a12008-12-27 16:32:52 +0000459 msg->lchan = lchan;
460
Harald Welte4bc90a12008-12-27 16:32:52 +0000461 gh->msg_type = GSM48_MT_CC_STATUS;
462
463 cause = msgb_put(msg, 3);
464 cause[0] = 2;
465 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
466 cause[2] = 0x80 | 30; /* response to status inquiry */
467
468 call_state = msgb_put(msg, 1);
469 call_state[0] = 0xc0 | 0x00;
470
Harald Welte65e74cc2008-12-29 01:55:35 +0000471 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000472}
473
Harald Welte6f4b7532008-12-29 00:39:37 +0000474static int gsm48_tx_simple(struct gsm_lchan *lchan,
475 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000476{
477 struct msgb *msg = gsm48_msgb_alloc();
478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
479
480 msg->lchan = lchan;
481
Harald Welte6f4b7532008-12-29 00:39:37 +0000482 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000483 gh->msg_type = msg_type;
484
Harald Welte65e74cc2008-12-29 01:55:35 +0000485 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000486}
487
488static int gsm48_cc_rx_status_enq(struct msgb *msg)
489{
490 return gsm48_cc_tx_status(msg->lchan);
491}
492
493static int gsm48_cc_rx_setup(struct msgb *msg)
494{
Harald Welte6f4b7532008-12-29 00:39:37 +0000495 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
496 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000497}
498
Harald Welte65e74cc2008-12-29 01:55:35 +0000499int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
500{
501 struct msgb *msg = gsm48_msgb_alloc();
502 struct gsm48_hdr *gh;
503 struct gsm_call *call = &lchan->call;
504
505 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
506
507 call->type = GSM_CT_MT;
508 msg->lchan = lchan;
509
510 gh->proto_discr = GSM48_PDISC_CC;
511 gh->msg_type = GSM48_MT_CC_SETUP;
512 gh->data[0] = 0x34;
513 gh->data[1] = 0x00;
514 gh->data[2] = 0x5c;
515 gh->data[3] = 0x04;
516 gh->data[4] = 0xb9;
517 gh->data[5] = 0x83;
518 gh->data[6] = 0x32;
519 gh->data[7] = 0x24;
520
521 DEBUGP(DCC, "Sending SETUP\n");
522
523 return gsm48_sendmsg(msg);
524}
525
Harald Welte4bc90a12008-12-27 16:32:52 +0000526static int gsm0408_rcv_cc(struct msgb *msg)
527{
528 struct gsm48_hdr *gh = msgb_l3(msg);
529 u_int8_t msg_type = gh->msg_type & 0xbf;
530 struct gsm_call *call = &msg->lchan->call;
531 int rc = 0;
532
533 switch (msg_type) {
534 case GSM48_MT_CC_CALL_CONF:
535 /* Response to SETUP */
536 DEBUGP(DCC, "CALL CONFIRM\n");
537 break;
538 case GSM48_MT_CC_RELEASE_COMPL:
539 /* Answer from MS to RELEASE */
540 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
541 call->state = GSM_CSTATE_NULL;
542 break;
543 case GSM48_MT_CC_ALERTING:
544 DEBUGP(DCC, "ALERTING\n");
545 break;
546 case GSM48_MT_CC_CONNECT:
547 DEBUGP(DCC, "CONNECT\n");
548 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000549 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
550 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000551 break;
552 case GSM48_MT_CC_CONNECT_ACK:
553 /* MO: Answer to CONNECT */
554 call->state = GSM_CSTATE_ACTIVE;
555 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
556 break;
557 case GSM48_MT_CC_RELEASE:
558 DEBUGP(DCC, "RELEASE\n");
559 /* need to respond with RELEASE_COMPLETE */
560 break;
561 case GSM48_MT_CC_STATUS_ENQ:
562 rc = gsm48_cc_rx_status_enq(msg);
563 break;
564 case GSM48_MT_CC_DISCONNECT:
565 /* Section 5.4.3.2 */
566 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
567 call->state = GSM_CSTATE_RELEASE_REQ;
568 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000569 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
570 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000571 break;
572 case GSM48_MT_CC_SETUP:
573 call->type = GSM_CT_MO;
574 call->state = GSM_CSTATE_INITIATED;
575 call->transaction_id = gh->proto_discr & 0xf0;
576 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000577 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
578 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000579 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
580 break;
581 case GSM48_MT_CC_EMERG_SETUP:
582 DEBUGP(DCC, "EMERGENCY SETUP\n");
583 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
584 break;
585 default:
586 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
587 msg_type);
588 break;
589 }
590
591 return rc;
592}
593
Harald Welte52b1f982008-12-23 20:25:15 +0000594/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
595int gsm0408_rcvmsg(struct msgb *msg)
596{
597 struct gsm48_hdr *gh = msgb_l3(msg);
598 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000599 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000600
601 switch (pdisc) {
602 case GSM48_PDISC_CC:
603 rc = gsm0408_rcv_cc(msg);
604 break;
605 case GSM48_PDISC_MM:
606 rc = gsm0408_rcv_mm(msg);
607 break;
608 case GSM48_PDISC_RR:
609 rc = gsm0408_rcv_rr(msg);
610 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000611 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000612 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000613 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000614 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000615 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000616 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
617 pdisc);
618 break;
619 default:
620 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
621 pdisc);
622 break;
623 }
624
625 return rc;
626}
Harald Welte8470bf22008-12-25 23:28:35 +0000627
628enum chreq_type {
629 CHREQ_T_EMERG_CALL,
630 CHREQ_T_CALL_REEST_TCH_F,
631 CHREQ_T_CALL_REEST_TCH_H,
632 CHREQ_T_CALL_REEST_TCH_H_DBL,
633 CHREQ_T_SDCCH,
634 CHREQ_T_TCH_F,
635 CHREQ_T_VOICE_CALL_TCH_H,
636 CHREQ_T_DATA_CALL_TCH_H,
637 CHREQ_T_LOCATION_UPD,
638 CHREQ_T_PAG_R_ANY,
639 CHREQ_T_PAG_R_TCH_F,
640 CHREQ_T_PAG_R_TCH_FH,
641};
642
643/* Section 9.1.8 / Table 9.9 */
644struct chreq {
645 u_int8_t val;
646 u_int8_t mask;
647 enum chreq_type type;
648};
649
650/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
651static const struct chreq chreq_type_neci1[] = {
652 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
653 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
654 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
655 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
656 { 0xe0, 0xe0, CHREQ_T_SDCCH },
657 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
658 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
659 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
660 { 0x10, 0xf0, CHREQ_T_SDCCH },
661 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
662 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
663 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
664};
665
666/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
667static const struct chreq chreq_type_neci0[] = {
668 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
669 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
670 { 0xe0, 0xe0, CHREQ_T_TCH_F },
671 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
672 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
673 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
674 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
675 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
676};
677
678static const enum gsm_chan_t ctype_by_chreq[] = {
679 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
680 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
681 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
682 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
683 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
684 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
685 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
686 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
687 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
688 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
689 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
690 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
691};
692
Harald Weltee14a57c2008-12-29 04:08:28 +0000693static const enum gsm_chreq_reason_t reason_by_chreq[] = {
694 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
695 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
696 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
697 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
698 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
699 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
700 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
701 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
702 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
703 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
704 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
705 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
706};
707
Harald Welte8470bf22008-12-25 23:28:35 +0000708enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
709{
710 int i;
711 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
712
713 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
714 const struct chreq *chr = &chreq_type_neci0[i];
715 if ((ra & chr->mask) == chr->val)
716 return ctype_by_chreq[chr->type];
717 }
718 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
719 return GSM_LCHAN_SDCCH;
720}
Harald Weltee14a57c2008-12-29 04:08:28 +0000721
722enum gsm_chreq_reason_t get_reason_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 reason_by_chreq[chr->type];
731 }
732 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
733 return GSM_CHREQ_REASON_OTHER;
734}