blob: 83399d021cada4972b7be07a4f7588a0cb93173b [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;
105 buf[1] = MID_TMSI_LEN;
106 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);
352
353 DEBUGP(DMM, "CM SERVICE REQUEST\n");
354 return gsm48_tx_mm_serv_ack(msg->lchan);
355}
356
Harald Welte52b1f982008-12-23 20:25:15 +0000357static int gsm0408_rcv_mm(struct msgb *msg)
358{
359 struct gsm48_hdr *gh = msgb_l3(msg);
360 int rc;
361
362 switch (gh->msg_type & 0xbf) {
363 case GSM48_MT_MM_LOC_UPD_REQUEST:
364 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000365 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000366 break;
367 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000368 rc = mm_rx_id_resp(msg);
369 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000370 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000371 rc = gsm48_rx_mm_serv_req(msg);
372 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000373 case GSM48_MT_MM_STATUS:
374 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
375 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000376 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000377 case GSM48_MT_MM_TMSI_REALL_COMPL:
378 case GSM48_MT_MM_AUTH_RESP:
379 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000380 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
381 gh->msg_type);
382 break;
383 default:
384 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
385 gh->msg_type);
386 break;
387 }
388
389 return rc;
390}
391static int gsm0408_rcv_rr(struct msgb *msg)
392{
393 struct gsm48_hdr *gh = msgb_l3(msg);
394
395 switch (gh->msg_type) {
396 case GSM48_MT_RR_CLSM_CHG:
397 DEBUGP(DRR, "CLASSMARK CHANGE\n");
398 /* FIXME: what to do ?!? */
399 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000400 case GSM48_MT_RR_GPRS_SUSP_REQ:
401 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
402 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000403 case GSM48_MT_RR_PAG_RESP:
404 default:
405 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
406 gh->msg_type);
407 break;
408 }
409
410 return 0;
411}
412
Harald Welte4bc90a12008-12-27 16:32:52 +0000413/* Call Control */
414
415/* Send a 04.08 call control message, add transaction ID and TI flag */
416static int gsm48_cc_sendmsg(struct msgb *msg)
417{
418 struct gsm48_hdr *gh = msg->data;
419 struct gsm_call *call = &msg->lchan->call;
420
421 gh->proto_discr |= msg->lchan->call.transaction_id;
422
423 /* GSM 04.07 Section 11.2.3.1.3 */
424 switch (call->type) {
425 case GSM_CT_MO:
426 gh->proto_discr |= 0x80;
427 break;
428 case GSM_CT_MT:
429 break;
430 }
431
432 return gsm0408_sendmsg(msg);
433}
434
435
436static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
437{
438 struct msgb *msg = gsm48_msgb_alloc();
439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
440 u_int8_t *cause, *call_state;
441
442 msg->lchan = lchan;
443
444 gh->proto_discr = GSM48_PDISC_CC;
445 gh->msg_type = GSM48_MT_CC_STATUS;
446
447 cause = msgb_put(msg, 3);
448 cause[0] = 2;
449 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
450 cause[2] = 0x80 | 30; /* response to status inquiry */
451
452 call_state = msgb_put(msg, 1);
453 call_state[0] = 0xc0 | 0x00;
454
455 return gsm48_cc_sendmsg(msg);
456}
457
458static int gsm48_cc_tx_simple(struct gsm_lchan *lchan, u_int8_t msg_type)
459{
460 struct msgb *msg = gsm48_msgb_alloc();
461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
462
463 msg->lchan = lchan;
464
465 gh->proto_discr = GSM48_PDISC_CC;
466 gh->msg_type = msg_type;
467
468 return gsm48_cc_sendmsg(msg);
469}
470
471static int gsm48_cc_rx_status_enq(struct msgb *msg)
472{
473 return gsm48_cc_tx_status(msg->lchan);
474}
475
476static int gsm48_cc_rx_setup(struct msgb *msg)
477{
478 return gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CALL_CONF);
479}
480
481static int gsm0408_rcv_cc(struct msgb *msg)
482{
483 struct gsm48_hdr *gh = msgb_l3(msg);
484 u_int8_t msg_type = gh->msg_type & 0xbf;
485 struct gsm_call *call = &msg->lchan->call;
486 int rc = 0;
487
488 switch (msg_type) {
489 case GSM48_MT_CC_CALL_CONF:
490 /* Response to SETUP */
491 DEBUGP(DCC, "CALL CONFIRM\n");
492 break;
493 case GSM48_MT_CC_RELEASE_COMPL:
494 /* Answer from MS to RELEASE */
495 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
496 call->state = GSM_CSTATE_NULL;
497 break;
498 case GSM48_MT_CC_ALERTING:
499 DEBUGP(DCC, "ALERTING\n");
500 break;
501 case GSM48_MT_CC_CONNECT:
502 DEBUGP(DCC, "CONNECT\n");
503 /* MT: need to respond with CONNECT_ACK */
504 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT_ACK);
505 break;
506 case GSM48_MT_CC_CONNECT_ACK:
507 /* MO: Answer to CONNECT */
508 call->state = GSM_CSTATE_ACTIVE;
509 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
510 break;
511 case GSM48_MT_CC_RELEASE:
512 DEBUGP(DCC, "RELEASE\n");
513 /* need to respond with RELEASE_COMPLETE */
514 break;
515 case GSM48_MT_CC_STATUS_ENQ:
516 rc = gsm48_cc_rx_status_enq(msg);
517 break;
518 case GSM48_MT_CC_DISCONNECT:
519 /* Section 5.4.3.2 */
520 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
521 call->state = GSM_CSTATE_RELEASE_REQ;
522 /* FIXME: clear the network connection */
523 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_RELEASE);
524 break;
525 case GSM48_MT_CC_SETUP:
526 call->type = GSM_CT_MO;
527 call->state = GSM_CSTATE_INITIATED;
528 call->transaction_id = gh->proto_discr & 0xf0;
529 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
530 rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT);
531 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
532 break;
533 case GSM48_MT_CC_EMERG_SETUP:
534 DEBUGP(DCC, "EMERGENCY SETUP\n");
535 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
536 break;
537 default:
538 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
539 msg_type);
540 break;
541 }
542
543 return rc;
544}
545
Harald Welte52b1f982008-12-23 20:25:15 +0000546/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
547int gsm0408_rcvmsg(struct msgb *msg)
548{
549 struct gsm48_hdr *gh = msgb_l3(msg);
550 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000551 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000552
553 switch (pdisc) {
554 case GSM48_PDISC_CC:
555 rc = gsm0408_rcv_cc(msg);
556 break;
557 case GSM48_PDISC_MM:
558 rc = gsm0408_rcv_mm(msg);
559 break;
560 case GSM48_PDISC_RR:
561 rc = gsm0408_rcv_rr(msg);
562 break;
563 case GSM48_PDISC_MM_GPRS:
564 case GSM48_PDISC_SM:
565 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
566 pdisc);
567 break;
568 default:
569 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
570 pdisc);
571 break;
572 }
573
574 return rc;
575}
Harald Welte8470bf22008-12-25 23:28:35 +0000576
577enum chreq_type {
578 CHREQ_T_EMERG_CALL,
579 CHREQ_T_CALL_REEST_TCH_F,
580 CHREQ_T_CALL_REEST_TCH_H,
581 CHREQ_T_CALL_REEST_TCH_H_DBL,
582 CHREQ_T_SDCCH,
583 CHREQ_T_TCH_F,
584 CHREQ_T_VOICE_CALL_TCH_H,
585 CHREQ_T_DATA_CALL_TCH_H,
586 CHREQ_T_LOCATION_UPD,
587 CHREQ_T_PAG_R_ANY,
588 CHREQ_T_PAG_R_TCH_F,
589 CHREQ_T_PAG_R_TCH_FH,
590};
591
592/* Section 9.1.8 / Table 9.9 */
593struct chreq {
594 u_int8_t val;
595 u_int8_t mask;
596 enum chreq_type type;
597};
598
599/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
600static const struct chreq chreq_type_neci1[] = {
601 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
602 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
603 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
604 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
605 { 0xe0, 0xe0, CHREQ_T_SDCCH },
606 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
607 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
608 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
609 { 0x10, 0xf0, CHREQ_T_SDCCH },
610 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
611 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
612 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
613};
614
615/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
616static const struct chreq chreq_type_neci0[] = {
617 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
618 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
619 { 0xe0, 0xe0, CHREQ_T_TCH_F },
620 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
621 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
622 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
623 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
624 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
625};
626
627static const enum gsm_chan_t ctype_by_chreq[] = {
628 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
629 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
630 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
631 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
632 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
633 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
634 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
635 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
636 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
637 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
638 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
639 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
640};
641
642enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
643{
644 int i;
645 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
646
647 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
648 const struct chreq *chr = &chreq_type_neci0[i];
649 if ((ra & chr->mask) == chr->val)
650 return ctype_by_chreq[chr->type];
651 }
652 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
653 return GSM_LCHAN_SDCCH;
654}