blob: c45e754f2ab2fc35744459a116a202acb7692965 [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>
36#include <openbsc/gsm_04_08.h>
37#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000038
Harald Welte8470bf22008-12-25 23:28:35 +000039#define GSM48_ALLOC_SIZE 1024
40#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000041
42struct gsm_lai {
43 u_int16_t mcc;
44 u_int16_t mnc;
45 u_int16_t lac;
46};
47
48static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
49{
50 u_int8_t dig[4];
51
52 /* MCC */
53 dig[1] = lai48->digits[0] & 0x0f;
54 dig[2] = lai48->digits[0] >> 4;
55 dig[3] = lai48->digits[1] & 0x0f;
56 lai->mcc = dig[3] * 100 + dig[2];
57
58 /* MNC */
59 dig[1] = lai48->digits[1] >> 4;
60 dig[2] = lai48->digits[2] & 0x0f;
61 dig[3] = lai48->digits[2] >> 4;
62 lai->mnc = dig[3] * 100 + dig[2];
63
64 lai->lac = lai48->lac;
65}
66
67static void to_bcd(u_int8_t *bcd, u_int16_t val)
68{
Harald Welte4b634542008-12-27 01:55:51 +000069 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000070 val = val / 10;
71 bcd[1] = val % 10;
72 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +000073 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +000074 val = val / 10;
75}
76
77static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
78 u_int16_t mnc, u_int16_t lac)
79{
80 u_int8_t bcd[3];
81
82 to_bcd(bcd, mcc);
83 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
84 lai48->digits[1] = bcd[2];
85
86 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +000087 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
88#if 0
Harald Welte8470bf22008-12-25 23:28:35 +000089 lai48->digits[1] |= bcd[2] << 4;
90 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +000091#else
92 lai48->digits[1] |= 0xf << 4;
93 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
94#endif
Harald Welte52b1f982008-12-23 20:25:15 +000095
Harald Welte4b634542008-12-27 01:55:51 +000096 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +000097}
98
99#define TMSI_LEN 4
100#define MID_TMSI_LEN (TMSI_LEN + 2)
101
Harald Welte75a983f2008-12-27 21:34:06 +0000102static void generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000103{
Harald Welte4b634542008-12-27 01:55:51 +0000104 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000105 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000106 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte75a983f2008-12-27 21:34:06 +0000107 *((u_int32_t *) &buf[3]) = htonl(tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000108}
109
Harald Welte8470bf22008-12-25 23:28:35 +0000110static struct msgb *gsm48_msgb_alloc(void)
111{
112 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
113}
114
Harald Welte52b1f982008-12-23 20:25:15 +0000115static int gsm0408_sendmsg(struct msgb *msg)
116{
Harald Welte8470bf22008-12-25 23:28:35 +0000117 if (msg->lchan)
118 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000119
Harald Welte4b634542008-12-27 01:55:51 +0000120 msg->l3h = msg->data;
121
Harald Welte8470bf22008-12-25 23:28:35 +0000122 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000123}
124
Harald Welte52b1f982008-12-23 20:25:15 +0000125
126/* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000127int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000128{
Harald Welte8470bf22008-12-25 23:28:35 +0000129 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000130 struct gsm48_hdr *gh;
131
Harald Welte8470bf22008-12-25 23:28:35 +0000132 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000133
134 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
135 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000136 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000137 gh->data[0] = cause;
138
139 DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n");
140
141 return gsm0408_sendmsg(msg);
142}
143
144/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000145int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000146{
Harald Welte8470bf22008-12-25 23:28:35 +0000147 struct gsm_bts *bts = lchan->ts->trx->bts;
148 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000149 struct gsm48_hdr *gh;
150 struct gsm48_loc_area_id *lai;
151 u_int8_t *mid;
152
Harald Welte8470bf22008-12-25 23:28:35 +0000153 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000154
155 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
156 gh->proto_discr = GSM48_PDISC_MM;
157 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
158
159 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
160 generate_lai(lai, bts->network->country_code,
161 bts->network->network_code, bts->location_area_code);
162
163 mid = msgb_put(msg, MID_TMSI_LEN);
164 generate_mid_from_tmsi(mid, tmsi);
165
166 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
167
168 return gsm0408_sendmsg(msg);
169}
170
Harald Weltefc977a82008-12-27 10:19:37 +0000171static char bcd2char(u_int8_t bcd)
172{
173 if (bcd < 0xa)
174 return '0' + bcd;
175 else
176 return 'A' + (bcd - 0xa);
177}
178
179/* 10.5.1.4 */
180static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
181{
182 int i;
183 u_int8_t mi_type;
184 char *str_cur = string;
185
186 mi_type = mi[0] & GSM_MI_TYPE_MASK;
187
188 switch (mi_type) {
189 case GSM_MI_TYPE_NONE:
190 break;
191 case GSM_MI_TYPE_TMSI:
192 for (i = 1; i < mi_len - 1; i++) {
193 if (str_cur + 2 >= string + str_len)
194 return str_cur - string;
195 *str_cur++ = bcd2char(mi[i] >> 4);
196 *str_cur++ = bcd2char(mi[i] & 0xf);
197 }
198 break;
199 case GSM_MI_TYPE_IMSI:
200 case GSM_MI_TYPE_IMEI:
201 case GSM_MI_TYPE_IMEISV:
202 if (mi[0] & GSM_MI_ODD)
203 *str_cur++ = bcd2char(mi[0] >> 4);
204
205 for (i = 1; i < mi_len - 1; i++) {
206 if (str_cur + 2 >= string + str_len)
207 return str_cur - string;
208 *str_cur++ = bcd2char(mi[i] & 0xf);
209 *str_cur++ = bcd2char(mi[i] >> 4);
210 }
211 break;
212 default:
213 break;
214 }
215
216 *str_cur++ = '\0';
217 return str_cur - string;
218}
219
Harald Welte231ad4f2008-12-27 11:15:38 +0000220/* Chapter 9.2.10 */
221static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
222{
223 struct msgb *msg = gsm48_msgb_alloc();
224 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000225
Harald Welte231ad4f2008-12-27 11:15:38 +0000226 msg->lchan = lchan;
227
228 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
229 gh->proto_discr = GSM48_PDISC_MM;
230 gh->msg_type = GSM48_MT_MM_ID_REQ;
231 gh->data[0] = id_type;
232
233 return gsm0408_sendmsg(msg);
234}
235
236#define MI_SIZE 32
237
238/* Chapter 9.2.11 */
239static int mm_rx_id_resp(struct msgb *msg)
240{
241 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000242 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000243 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
244 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000245 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000246
247 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000248 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000249 mi_type, mi_string);
250
Harald Welte75a983f2008-12-27 21:34:06 +0000251 switch (mi_type) {
252 case GSM_MI_TYPE_IMSI:
253 if (!lchan->subscr)
254 lchan->subscr = db_create_subscriber(mi_string);
255 if (lchan->subscr && lchan->subscr->authorized) {
256 /* FIXME: check if we've recently received UPDATE REQUEST */
257 db_subscriber_alloc_tmsi(lchan->subscr);
258 tmsi = strtoul(lchan->subscr->tmsi, NULL, 16);
259 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
260 }
261 break;
262 case GSM_MI_TYPE_IMEI:
263 /* update subscribe <-> IMEI mapping */
264 if (lchan->subscr)
265 db_subscriber_assoc_imei(lchan->subscr, mi_string);
266 break;
267 }
268 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000269}
270
271#define MI_SIZE 32
Harald Welte52b1f982008-12-23 20:25:15 +0000272/* Chapter 9.2.15 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000273static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000274{
Harald Welte8470bf22008-12-25 23:28:35 +0000275 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000276 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000277 struct gsm48_loc_upd_req *lu;
278 struct gsm_subscriber *subscr;
Harald Welte8470bf22008-12-25 23:28:35 +0000279 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000280 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000281 char mi_string[MI_SIZE];
282 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000283
Harald Welte8470bf22008-12-25 23:28:35 +0000284 lu = (struct gsm48_loc_upd_req *) gh->data;
285
286 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000287
Harald Weltefc977a82008-12-27 10:19:37 +0000288 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
289
Harald Welte231ad4f2008-12-27 11:15:38 +0000290 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000291 switch (mi_type) {
292 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000293 /* we always want the IMEI, too */
294 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000295 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000296 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000297 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000298 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000299 /* we always want the IMEI, too */
300 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV);
Harald Welte52b1f982008-12-23 20:25:15 +0000301 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Weltefc977a82008-12-27 10:19:37 +0000302 subscr = subscr_get_by_tmsi(lu->mi);
Harald Welte52b1f982008-12-23 20:25:15 +0000303 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000304 /* send IDENTITY REQUEST message to get IMSI */
305 rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMSI);
Harald Welte52b1f982008-12-23 20:25:15 +0000306 }
307 break;
308 case GSM_MI_TYPE_IMEI:
309 case GSM_MI_TYPE_IMEISV:
310 /* no sim card... FIXME: what to do ? */
311 fprintf(stderr, "Unimplemented mobile identity type\n");
312 break;
313 default:
314 fprintf(stderr, "Unknown mobile identity type\n");
315 break;
316 }
317
Harald Welte75a983f2008-12-27 21:34:06 +0000318 if (!subscr || !subscr->authorized) {
Harald Welte52b1f982008-12-23 20:25:15 +0000319 /* 0x16 is congestion */
Harald Welte8470bf22008-12-25 23:28:35 +0000320 gsm0408_loc_upd_rej(msg->lchan, 0x16);
Harald Welte98243f82008-12-27 19:46:41 +0000321 rsl_chan_release(msg->lchan);
Harald Welte52b1f982008-12-23 20:25:15 +0000322 return -EINVAL;
323 }
324
Harald Welte75a983f2008-12-27 21:34:06 +0000325 db_subscriber_alloc_tmsi(subscr);
326
Harald Welte8470bf22008-12-25 23:28:35 +0000327 msg->lchan->subscr = subscr;
Harald Welte52b1f982008-12-23 20:25:15 +0000328 subscr_update(subscr, bts);
Harald Welte75a983f2008-12-27 21:34:06 +0000329 tmsi = strtoul(subscr->tmsi, NULL, 16);
Harald Welte8470bf22008-12-25 23:28:35 +0000330
Harald Welte75a983f2008-12-27 21:34:06 +0000331 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000332}
333
Harald Welte4b634542008-12-27 01:55:51 +0000334static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
335{
336 struct msgb *msg = gsm48_msgb_alloc();
337 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
338
339 msg->lchan = lchan;
340
341 gh->proto_discr = GSM48_PDISC_MM;
342 gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
343
344 DEBUGP(DMM, "-> CM SERVICE ACK\n");
345
346 return gsm0408_sendmsg(msg);
347}
348
349static int gsm48_rx_mm_serv_req(struct msgb *msg)
350{
351 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000352 u_int8_t serv_type = gh->data[0] & 0x0f;
Harald Welte4b634542008-12-27 01:55:51 +0000353
Harald Weltebcae43f2008-12-27 21:45:37 +0000354 DEBUGP(DMM, "CM SERVICE REQUEST serv_type=0x%02x\n", serv_type);
355
Harald Welte4b634542008-12-27 01:55:51 +0000356 return gsm48_tx_mm_serv_ack(msg->lchan);
357}
358
Harald Welte52b1f982008-12-23 20:25:15 +0000359static int gsm0408_rcv_mm(struct msgb *msg)
360{
361 struct gsm48_hdr *gh = msgb_l3(msg);
362 int rc;
363
364 switch (gh->msg_type & 0xbf) {
365 case GSM48_MT_MM_LOC_UPD_REQUEST:
366 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000367 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000368 break;
369 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000370 rc = mm_rx_id_resp(msg);
371 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000372 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000373 rc = gsm48_rx_mm_serv_req(msg);
374 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000375 case GSM48_MT_MM_STATUS:
376 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
377 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000378 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000379 case GSM48_MT_MM_TMSI_REALL_COMPL:
380 case GSM48_MT_MM_AUTH_RESP:
381 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000382 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
383 gh->msg_type);
384 break;
385 default:
386 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
387 gh->msg_type);
388 break;
389 }
390
391 return rc;
392}
393static int gsm0408_rcv_rr(struct msgb *msg)
394{
395 struct gsm48_hdr *gh = msgb_l3(msg);
396
397 switch (gh->msg_type) {
398 case GSM48_MT_RR_CLSM_CHG:
399 DEBUGP(DRR, "CLASSMARK CHANGE\n");
400 /* FIXME: what to do ?!? */
401 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000402 case GSM48_MT_RR_GPRS_SUSP_REQ:
403 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
404 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000405 case GSM48_MT_RR_PAG_RESP:
406 default:
407 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
408 gh->msg_type);
409 break;
410 }
411
412 return 0;
413}
414
Harald Welte4bc90a12008-12-27 16:32:52 +0000415/* Call Control */
416
417/* Send a 04.08 call control message, add transaction ID and TI flag */
418static int gsm48_cc_sendmsg(struct msgb *msg)
419{
420 struct gsm48_hdr *gh = msg->data;
421 struct gsm_call *call = &msg->lchan->call;
422
423 gh->proto_discr |= msg->lchan->call.transaction_id;
424
425 /* GSM 04.07 Section 11.2.3.1.3 */
426 switch (call->type) {
427 case GSM_CT_MO:
428 gh->proto_discr |= 0x80;
429 break;
430 case GSM_CT_MT:
431 break;
432 }
433
434 return gsm0408_sendmsg(msg);
435}
436
437
438static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
439{
440 struct msgb *msg = gsm48_msgb_alloc();
441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
442 u_int8_t *cause, *call_state;
443
444 msg->lchan = lchan;
445
446 gh->proto_discr = GSM48_PDISC_CC;
447 gh->msg_type = GSM48_MT_CC_STATUS;
448
449 cause = msgb_put(msg, 3);
450 cause[0] = 2;
451 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
452 cause[2] = 0x80 | 30; /* response to status inquiry */
453
454 call_state = msgb_put(msg, 1);
455 call_state[0] = 0xc0 | 0x00;
456
457 return gsm48_cc_sendmsg(msg);
458}
459
460static int gsm48_cc_tx_simple(struct gsm_lchan *lchan, u_int8_t msg_type)
461{
462 struct msgb *msg = gsm48_msgb_alloc();
463 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
464
465 msg->lchan = lchan;
466
467 gh->proto_discr = GSM48_PDISC_CC;
468 gh->msg_type = msg_type;
469
470 return gsm48_cc_sendmsg(msg);
471}
472
473static int gsm48_cc_rx_status_enq(struct msgb *msg)
474{
475 return gsm48_cc_tx_status(msg->lchan);
476}
477
478static int gsm48_cc_rx_setup(struct msgb *msg)
479{
480 return gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CALL_CONF);
481}
482
483static int gsm0408_rcv_cc(struct msgb *msg)
484{
485 struct gsm48_hdr *gh = msgb_l3(msg);
486 u_int8_t msg_type = gh->msg_type & 0xbf;
487 struct gsm_call *call = &msg->lchan->call;
488 int rc = 0;
489
490 switch (msg_type) {
491 case GSM48_MT_CC_CALL_CONF:
492 /* Response to SETUP */
493 DEBUGP(DCC, "CALL CONFIRM\n");
494 break;
495 case GSM48_MT_CC_RELEASE_COMPL:
496 /* Answer from MS to RELEASE */
497 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
498 call->state = GSM_CSTATE_NULL;
499 break;
500 case GSM48_MT_CC_ALERTING:
501 DEBUGP(DCC, "ALERTING\n");
502 break;
503 case GSM48_MT_CC_CONNECT:
504 DEBUGP(DCC, "CONNECT\n");
505 /* MT: need to respond with CONNECT_ACK */
506 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT_ACK);
507 break;
508 case GSM48_MT_CC_CONNECT_ACK:
509 /* MO: Answer to CONNECT */
510 call->state = GSM_CSTATE_ACTIVE;
511 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
512 break;
513 case GSM48_MT_CC_RELEASE:
514 DEBUGP(DCC, "RELEASE\n");
515 /* need to respond with RELEASE_COMPLETE */
516 break;
517 case GSM48_MT_CC_STATUS_ENQ:
518 rc = gsm48_cc_rx_status_enq(msg);
519 break;
520 case GSM48_MT_CC_DISCONNECT:
521 /* Section 5.4.3.2 */
522 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
523 call->state = GSM_CSTATE_RELEASE_REQ;
524 /* FIXME: clear the network connection */
525 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_RELEASE);
526 break;
527 case GSM48_MT_CC_SETUP:
528 call->type = GSM_CT_MO;
529 call->state = GSM_CSTATE_INITIATED;
530 call->transaction_id = gh->proto_discr & 0xf0;
531 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
532 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT);
533 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
534 break;
535 case GSM48_MT_CC_EMERG_SETUP:
536 DEBUGP(DCC, "EMERGENCY SETUP\n");
537 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
538 break;
539 default:
540 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
541 msg_type);
542 break;
543 }
544
545 return rc;
546}
547
Harald Weltebcae43f2008-12-27 21:45:37 +0000548static int gsm0408_rcv_sms(struct msgb *msg)
549{
550 DEBUGP(DSMS, "SMS Message\n");
551 return 0;
552}
553
Harald Welte52b1f982008-12-23 20:25:15 +0000554/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
555int gsm0408_rcvmsg(struct msgb *msg)
556{
557 struct gsm48_hdr *gh = msgb_l3(msg);
558 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000559 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000560
561 switch (pdisc) {
562 case GSM48_PDISC_CC:
563 rc = gsm0408_rcv_cc(msg);
564 break;
565 case GSM48_PDISC_MM:
566 rc = gsm0408_rcv_mm(msg);
567 break;
568 case GSM48_PDISC_RR:
569 rc = gsm0408_rcv_rr(msg);
570 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000571 case GSM48_PDISC_SMS:
572 rc = gsm0408_rcv_sms(msg);
573 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000574 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000575 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000576 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
577 pdisc);
578 break;
579 default:
580 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
581 pdisc);
582 break;
583 }
584
585 return rc;
586}
Harald Welte8470bf22008-12-25 23:28:35 +0000587
588enum chreq_type {
589 CHREQ_T_EMERG_CALL,
590 CHREQ_T_CALL_REEST_TCH_F,
591 CHREQ_T_CALL_REEST_TCH_H,
592 CHREQ_T_CALL_REEST_TCH_H_DBL,
593 CHREQ_T_SDCCH,
594 CHREQ_T_TCH_F,
595 CHREQ_T_VOICE_CALL_TCH_H,
596 CHREQ_T_DATA_CALL_TCH_H,
597 CHREQ_T_LOCATION_UPD,
598 CHREQ_T_PAG_R_ANY,
599 CHREQ_T_PAG_R_TCH_F,
600 CHREQ_T_PAG_R_TCH_FH,
601};
602
603/* Section 9.1.8 / Table 9.9 */
604struct chreq {
605 u_int8_t val;
606 u_int8_t mask;
607 enum chreq_type type;
608};
609
610/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
611static const struct chreq chreq_type_neci1[] = {
612 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
613 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
614 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
615 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
616 { 0xe0, 0xe0, CHREQ_T_SDCCH },
617 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
618 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
619 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
620 { 0x10, 0xf0, CHREQ_T_SDCCH },
621 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
622 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
623 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
624};
625
626/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
627static const struct chreq chreq_type_neci0[] = {
628 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
629 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
630 { 0xe0, 0xe0, CHREQ_T_TCH_F },
631 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
632 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
633 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
634 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
635 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
636};
637
638static const enum gsm_chan_t ctype_by_chreq[] = {
639 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
640 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
641 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
642 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
643 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
644 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
645 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
646 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
647 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
648 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
649 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
650 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
651};
652
653enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
654{
655 int i;
656 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
657
658 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
659 const struct chreq *chr = &chreq_type_neci0[i];
660 if ((ra & chr->mask) == chr->val)
661 return ctype_by_chreq[chr->type];
662 }
663 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
664 return GSM_LCHAN_SDCCH;
665}