blob: 9a6212d6700c21160fee87194f2cbf6c5ec53adc [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
43struct gsm_lai {
44 u_int16_t mcc;
45 u_int16_t mnc;
46 u_int16_t lac;
47};
48
49static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
50{
51 u_int8_t dig[4];
52
53 /* MCC */
54 dig[1] = lai48->digits[0] & 0x0f;
55 dig[2] = lai48->digits[0] >> 4;
56 dig[3] = lai48->digits[1] & 0x0f;
57 lai->mcc = dig[3] * 100 + dig[2];
58
59 /* MNC */
60 dig[1] = lai48->digits[1] >> 4;
61 dig[2] = lai48->digits[2] & 0x0f;
62 dig[3] = lai48->digits[2] >> 4;
63 lai->mnc = dig[3] * 100 + dig[2];
64
65 lai->lac = lai48->lac;
66}
67
68static void to_bcd(u_int8_t *bcd, u_int16_t val)
69{
Harald Welte4b634542008-12-27 01:55:51 +000070 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000071 val = val / 10;
72 bcd[1] = val % 10;
73 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000074 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000075 val = val / 10;
76}
77
78static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
79 u_int16_t mnc, u_int16_t lac)
80{
81 u_int8_t bcd[3];
82
83 to_bcd(bcd, mcc);
84 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
85 lai48->digits[1] = bcd[2];
86
87 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +000088 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
89#if 0
Harald Welte8470bf22008-12-25 23:28:35 +000090 lai48->digits[1] |= bcd[2] << 4;
91 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +000092#else
93 lai48->digits[1] |= 0xf << 4;
94 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
95#endif
Harald Welte52b1f982008-12-23 20:25:15 +000096
Harald Welte4b634542008-12-27 01:55:51 +000097 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +000098}
99
100#define TMSI_LEN 4
101#define MID_TMSI_LEN (TMSI_LEN + 2)
102
Harald Welte75a983f2008-12-27 21:34:06 +0000103static void generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000104{
Harald Welte4b634542008-12-27 01:55:51 +0000105 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000106 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000107 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte75a983f2008-12-27 21:34:06 +0000108 *((u_int32_t *) &buf[3]) = htonl(tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000109}
110
Harald Welte8470bf22008-12-25 23:28:35 +0000111static struct msgb *gsm48_msgb_alloc(void)
112{
113 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
114}
115
Harald Welte52b1f982008-12-23 20:25:15 +0000116static int gsm0408_sendmsg(struct msgb *msg)
117{
Harald Welte8470bf22008-12-25 23:28:35 +0000118 if (msg->lchan)
119 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000120
Harald Welte4b634542008-12-27 01:55:51 +0000121 msg->l3h = msg->data;
122
Harald Welte8470bf22008-12-25 23:28:35 +0000123 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000124}
125
Harald Welte52b1f982008-12-23 20:25:15 +0000126
127/* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000128int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000129{
Harald Welte8470bf22008-12-25 23:28:35 +0000130 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000131 struct gsm48_hdr *gh;
132
Harald Welte8470bf22008-12-25 23:28:35 +0000133 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000134
135 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
136 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000137 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000138 gh->data[0] = cause;
139
140 DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n");
141
142 return gsm0408_sendmsg(msg);
143}
144
145/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000146int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000147{
Harald Welte8470bf22008-12-25 23:28:35 +0000148 struct gsm_bts *bts = lchan->ts->trx->bts;
149 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000150 struct gsm48_hdr *gh;
151 struct gsm48_loc_area_id *lai;
152 u_int8_t *mid;
153
Harald Welte8470bf22008-12-25 23:28:35 +0000154 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000155
156 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
157 gh->proto_discr = GSM48_PDISC_MM;
158 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
159
160 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
161 generate_lai(lai, bts->network->country_code,
162 bts->network->network_code, bts->location_area_code);
163
164 mid = msgb_put(msg, MID_TMSI_LEN);
165 generate_mid_from_tmsi(mid, tmsi);
166
167 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
168
169 return gsm0408_sendmsg(msg);
170}
171
Harald Weltefc977a82008-12-27 10:19:37 +0000172static char bcd2char(u_int8_t bcd)
173{
174 if (bcd < 0xa)
175 return '0' + bcd;
176 else
177 return 'A' + (bcd - 0xa);
178}
179
180/* 10.5.1.4 */
181static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
182{
183 int i;
184 u_int8_t mi_type;
185 char *str_cur = string;
186
187 mi_type = mi[0] & GSM_MI_TYPE_MASK;
188
189 switch (mi_type) {
190 case GSM_MI_TYPE_NONE:
191 break;
192 case GSM_MI_TYPE_TMSI:
193 for (i = 1; i < mi_len - 1; i++) {
194 if (str_cur + 2 >= string + str_len)
195 return str_cur - string;
196 *str_cur++ = bcd2char(mi[i] >> 4);
197 *str_cur++ = bcd2char(mi[i] & 0xf);
198 }
199 break;
200 case GSM_MI_TYPE_IMSI:
201 case GSM_MI_TYPE_IMEI:
202 case GSM_MI_TYPE_IMEISV:
203 if (mi[0] & GSM_MI_ODD)
204 *str_cur++ = bcd2char(mi[0] >> 4);
205
206 for (i = 1; i < mi_len - 1; i++) {
207 if (str_cur + 2 >= string + str_len)
208 return str_cur - string;
209 *str_cur++ = bcd2char(mi[i] & 0xf);
210 *str_cur++ = bcd2char(mi[i] >> 4);
211 }
212 break;
213 default:
214 break;
215 }
216
217 *str_cur++ = '\0';
218 return str_cur - string;
219}
220
Harald Welte231ad4f2008-12-27 11:15:38 +0000221/* Chapter 9.2.10 */
222static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
223{
224 struct msgb *msg = gsm48_msgb_alloc();
225 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000226
Harald Welte231ad4f2008-12-27 11:15:38 +0000227 msg->lchan = lchan;
228
229 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
230 gh->proto_discr = GSM48_PDISC_MM;
231 gh->msg_type = GSM48_MT_MM_ID_REQ;
232 gh->data[0] = id_type;
233
234 return gsm0408_sendmsg(msg);
235}
236
237#define MI_SIZE 32
238
239/* Chapter 9.2.11 */
240static int mm_rx_id_resp(struct msgb *msg)
241{
242 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000243 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000244 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
245 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000246 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000247
248 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000249 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000250 mi_type, mi_string);
251
Harald Welte75a983f2008-12-27 21:34:06 +0000252 switch (mi_type) {
253 case GSM_MI_TYPE_IMSI:
254 if (!lchan->subscr)
255 lchan->subscr = db_create_subscriber(mi_string);
256 if (lchan->subscr && lchan->subscr->authorized) {
257 /* FIXME: check if we've recently received UPDATE REQUEST */
258 db_subscriber_alloc_tmsi(lchan->subscr);
259 tmsi = strtoul(lchan->subscr->tmsi, NULL, 16);
260 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
261 }
262 break;
263 case GSM_MI_TYPE_IMEI:
264 /* update subscribe <-> IMEI mapping */
265 if (lchan->subscr)
266 db_subscriber_assoc_imei(lchan->subscr, mi_string);
267 break;
268 }
269 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000270}
271
272#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000273/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000274static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000275{
Harald Welte8470bf22008-12-25 23:28:35 +0000276 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000277 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000278 struct gsm48_loc_upd_req *lu;
279 struct gsm_subscriber *subscr;
Harald Welte8470bf22008-12-25 23:28:35 +0000280 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000281 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000282 char mi_string[MI_SIZE];
283 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000284
Harald Welte8470bf22008-12-25 23:28:35 +0000285 lu = (struct gsm48_loc_upd_req *) gh->data;
286
287 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000288
Harald Weltefc977a82008-12-27 10:19:37 +0000289 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
290
Harald Welte231ad4f2008-12-27 11:15:38 +0000291 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000292 switch (mi_type) {
293 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000294 /* we always want the IMEI, too */
295 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000296 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000297 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000298 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000299 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000300 /* we always want the IMEI, too */
301 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000302 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000303 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000304 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000305 /* send IDENTITY REQUEST message to get IMSI */
306 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000307 }
308 break;
309 case GSM_MI_TYPE_IMEI:
310 case GSM_MI_TYPE_IMEISV:
311 /* no sim card... FIXME: what to do ? */
312 fprintf(stderr, "Unimplemented mobile identity type\n");
313 break;
314 default:
315 fprintf(stderr, "Unknown mobile identity type\n");
316 break;
317 }
318
Harald Welte75a983f2008-12-27 21:34:06 +0000319 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000320 /* 0x16 is congestion */
Harald Welte8470bf22008-12-25 23:28:35 +0000321 gsm0408_loc_upd_rej(msg->lchan, 0x16);
Harald Welte98243f82008-12-27 19:46:41 +0000322 rsl_chan_release(msg->lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000323 return -EINVAL;
324 }
325
Harald Welte75a983f2008-12-27 21:34:06 +0000326 db_subscriber_alloc_tmsi(subscr);
327
Harald Welte8470bf22008-12-25 23:28:35 +0000328 msg->lchan->subscr = subscr;
Harald Welte52b1f982008-12-23 20:25:15 +0000329 subscr_update(subscr, bts);
Harald Welte75a983f2008-12-27 21:34:06 +0000330 tmsi = strtoul(subscr->tmsi, NULL, 16);
Harald Welte8470bf22008-12-25 23:28:35 +0000331
Harald Welte75a983f2008-12-27 21:34:06 +0000332 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000333}
334
Harald Welte4b634542008-12-27 01:55:51 +0000335static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
336{
337 struct msgb *msg = gsm48_msgb_alloc();
338 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
339
340 msg->lchan = lchan;
341
342 gh->proto_discr = GSM48_PDISC_MM;
343 gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
344
345 DEBUGP(DMM, "-> CM SERVICE ACK\n");
346
347 return gsm0408_sendmsg(msg);
348}
349
350static int gsm48_rx_mm_serv_req(struct msgb *msg)
351{
352 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000353 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000354
Harald Weltebcae43f2008-12-27 21:45:37 +0000355 DEBUGP(DMM, "CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
356
Harald Welte4b634542008-12-27 01:55:51 +0000357 return gsm48_tx_mm_serv_ack(msg->lchan);
358}
359
Harald Welte52b1f982008-12-23 20:25:15 +0000360static int gsm0408_rcv_mm(struct msgb *msg)
361{
362 struct gsm48_hdr *gh = msgb_l3(msg);
363 int rc;
364
365 switch (gh->msg_type & 0xbf) {
366 case GSM48_MT_MM_LOC_UPD_REQUEST:
367 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000368 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000369 break;
370 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000371 rc = mm_rx_id_resp(msg);
372 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000373 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000374 rc = gsm48_rx_mm_serv_req(msg);
375 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000376 case GSM48_MT_MM_STATUS:
377 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
378 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000379 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000380 case GSM48_MT_MM_TMSI_REALL_COMPL:
381 case GSM48_MT_MM_AUTH_RESP:
382 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000383 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
384 gh->msg_type);
385 break;
386 default:
387 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
388 gh->msg_type);
389 break;
390 }
391
392 return rc;
393}
394static int gsm0408_rcv_rr(struct msgb *msg)
395{
396 struct gsm48_hdr *gh = msgb_l3(msg);
397
398 switch (gh->msg_type) {
399 case GSM48_MT_RR_CLSM_CHG:
400 DEBUGP(DRR, "CLASSMARK CHANGE\n");
401 /* FIXME: what to do ?!? */
402 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000403 case GSM48_MT_RR_GPRS_SUSP_REQ:
404 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
405 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000406 case GSM48_MT_RR_PAG_RESP:
407 default:
408 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
409 gh->msg_type);
410 break;
411 }
412
413 return 0;
414}
415
Harald Welte4bc90a12008-12-27 16:32:52 +0000416/* Call Control */
417
418/* Send a 04.08 call control message, add transaction ID and TI flag */
419static int gsm48_cc_sendmsg(struct msgb *msg)
420{
421 struct gsm48_hdr *gh = msg->data;
422 struct gsm_call *call = &msg->lchan->call;
423
424 gh->proto_discr |= msg->lchan->call.transaction_id;
425
426 /* GSM 04.07 Section 11.2.3.1.3 */
427 switch (call->type) {
428 case GSM_CT_MO:
429 gh->proto_discr |= 0x80;
430 break;
431 case GSM_CT_MT:
432 break;
433 }
434
435 return gsm0408_sendmsg(msg);
436}
437
438
439static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
440{
441 struct msgb *msg = gsm48_msgb_alloc();
442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
443 u_int8_t *cause, *call_state;
444
445 msg->lchan = lchan;
446
447 gh->proto_discr = GSM48_PDISC_CC;
448 gh->msg_type = GSM48_MT_CC_STATUS;
449
450 cause = msgb_put(msg, 3);
451 cause[0] = 2;
452 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
453 cause[2] = 0x80 | 30; /* response to status inquiry */
454
455 call_state = msgb_put(msg, 1);
456 call_state[0] = 0xc0 | 0x00;
457
458 return gsm48_cc_sendmsg(msg);
459}
460
461static int gsm48_cc_tx_simple(struct gsm_lchan *lchan, u_int8_t msg_type)
462{
463 struct msgb *msg = gsm48_msgb_alloc();
464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
465
466 msg->lchan = lchan;
467
468 gh->proto_discr = GSM48_PDISC_CC;
469 gh->msg_type = msg_type;
470
471 return gsm48_cc_sendmsg(msg);
472}
473
474static int gsm48_cc_rx_status_enq(struct msgb *msg)
475{
476 return gsm48_cc_tx_status(msg->lchan);
477}
478
479static int gsm48_cc_rx_setup(struct msgb *msg)
480{
481 return gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CALL_CONF);
482}
483
484static int gsm0408_rcv_cc(struct msgb *msg)
485{
486 struct gsm48_hdr *gh = msgb_l3(msg);
487 u_int8_t msg_type = gh->msg_type & 0xbf;
488 struct gsm_call *call = &msg->lchan->call;
489 int rc = 0;
490
491 switch (msg_type) {
492 case GSM48_MT_CC_CALL_CONF:
493 /* Response to SETUP */
494 DEBUGP(DCC, "CALL CONFIRM\n");
495 break;
496 case GSM48_MT_CC_RELEASE_COMPL:
497 /* Answer from MS to RELEASE */
498 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
499 call->state = GSM_CSTATE_NULL;
500 break;
501 case GSM48_MT_CC_ALERTING:
502 DEBUGP(DCC, "ALERTING\n");
503 break;
504 case GSM48_MT_CC_CONNECT:
505 DEBUGP(DCC, "CONNECT\n");
506 /* MT: need to respond with CONNECT_ACK */
507 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT_ACK);
508 break;
509 case GSM48_MT_CC_CONNECT_ACK:
510 /* MO: Answer to CONNECT */
511 call->state = GSM_CSTATE_ACTIVE;
512 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
513 break;
514 case GSM48_MT_CC_RELEASE:
515 DEBUGP(DCC, "RELEASE\n");
516 /* need to respond with RELEASE_COMPLETE */
517 break;
518 case GSM48_MT_CC_STATUS_ENQ:
519 rc = gsm48_cc_rx_status_enq(msg);
520 break;
521 case GSM48_MT_CC_DISCONNECT:
522 /* Section 5.4.3.2 */
523 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
524 call->state = GSM_CSTATE_RELEASE_REQ;
525 /* FIXME: clear the network connection */
526 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_RELEASE);
527 break;
528 case GSM48_MT_CC_SETUP:
529 call->type = GSM_CT_MO;
530 call->state = GSM_CSTATE_INITIATED;
531 call->transaction_id = gh->proto_discr & 0xf0;
532 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
533 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT);
534 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
535 break;
536 case GSM48_MT_CC_EMERG_SETUP:
537 DEBUGP(DCC, "EMERGENCY SETUP\n");
538 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
539 break;
540 default:
541 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
542 msg_type);
543 break;
544 }
545
546 return rc;
547}
548
Harald Welte52b1f982008-12-23 20:25:15 +0000549/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
550int gsm0408_rcvmsg(struct msgb *msg)
551{
552 struct gsm48_hdr *gh = msgb_l3(msg);
553 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000554 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000555
556 switch (pdisc) {
557 case GSM48_PDISC_CC:
558 rc = gsm0408_rcv_cc(msg);
559 break;
560 case GSM48_PDISC_MM:
561 rc = gsm0408_rcv_mm(msg);
562 break;
563 case GSM48_PDISC_RR:
564 rc = gsm0408_rcv_rr(msg);
565 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000566 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000567 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000568 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000569 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000570 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000571 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
572 pdisc);
573 break;
574 default:
575 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
576 pdisc);
577 break;
578 }
579
580 return rc;
581}
Harald Welte8470bf22008-12-25 23:28:35 +0000582
583enum chreq_type {
584 CHREQ_T_EMERG_CALL,
585 CHREQ_T_CALL_REEST_TCH_F,
586 CHREQ_T_CALL_REEST_TCH_H,
587 CHREQ_T_CALL_REEST_TCH_H_DBL,
588 CHREQ_T_SDCCH,
589 CHREQ_T_TCH_F,
590 CHREQ_T_VOICE_CALL_TCH_H,
591 CHREQ_T_DATA_CALL_TCH_H,
592 CHREQ_T_LOCATION_UPD,
593 CHREQ_T_PAG_R_ANY,
594 CHREQ_T_PAG_R_TCH_F,
595 CHREQ_T_PAG_R_TCH_FH,
596};
597
598/* Section 9.1.8 / Table 9.9 */
599struct chreq {
600 u_int8_t val;
601 u_int8_t mask;
602 enum chreq_type type;
603};
604
605/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
606static const struct chreq chreq_type_neci1[] = {
607 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
608 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
609 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
610 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
611 { 0xe0, 0xe0, CHREQ_T_SDCCH },
612 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
613 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
614 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
615 { 0x10, 0xf0, CHREQ_T_SDCCH },
616 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
617 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
618 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
619};
620
621/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
622static const struct chreq chreq_type_neci0[] = {
623 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
624 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
625 { 0xe0, 0xe0, CHREQ_T_TCH_F },
626 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
627 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
628 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
629 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
630 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
631};
632
633static const enum gsm_chan_t ctype_by_chreq[] = {
634 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
635 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
636 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
637 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
638 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
639 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
640 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
641 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
642 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
643 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
644 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
645 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
646};
647
648enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
649{
650 int i;
651 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
652
653 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
654 const struct chreq *chr = &chreq_type_neci0[i];
655 if ((ra & chr->mask) == chr->val)
656 return ctype_by_chreq[chr->type];
657 }
658 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
659 return GSM_LCHAN_SDCCH;
660}