blob: 212b4ad6b419276963e818283a4f30ef4d6e9218 [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>
29
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/msgb.h>
31#include <openbsc/debug.h>
32#include <openbsc/gsm_data.h>
33#include <openbsc/gsm_subscriber.h>
34#include <openbsc/gsm_04_08.h>
35#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000036
Harald Welte8470bf22008-12-25 23:28:35 +000037#define GSM48_ALLOC_SIZE 1024
38#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000039
40struct gsm_lai {
41 u_int16_t mcc;
42 u_int16_t mnc;
43 u_int16_t lac;
44};
45
46static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48)
47{
48 u_int8_t dig[4];
49
50 /* MCC */
51 dig[1] = lai48->digits[0] & 0x0f;
52 dig[2] = lai48->digits[0] >> 4;
53 dig[3] = lai48->digits[1] & 0x0f;
54 lai->mcc = dig[3] * 100 + dig[2];
55
56 /* MNC */
57 dig[1] = lai48->digits[1] >> 4;
58 dig[2] = lai48->digits[2] & 0x0f;
59 dig[3] = lai48->digits[2] >> 4;
60 lai->mnc = dig[3] * 100 + dig[2];
61
62 lai->lac = lai48->lac;
63}
64
65static void to_bcd(u_int8_t *bcd, u_int16_t val)
66{
67 bcd[0] = val % 10;
68 val = val / 10;
69 bcd[1] = val % 10;
70 val = val / 10;
71 bcd[2] = val % 10;
72 val = val / 10;
73}
74
75static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
76 u_int16_t mnc, u_int16_t lac)
77{
78 u_int8_t bcd[3];
79
80 to_bcd(bcd, mcc);
81 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
82 lai48->digits[1] = bcd[2];
83
84 to_bcd(bcd, mnc);
Harald Welte8470bf22008-12-25 23:28:35 +000085 lai48->digits[1] |= bcd[2] << 4;
86 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte52b1f982008-12-23 20:25:15 +000087
88 lai48->lac = lac;
89}
90
91#define TMSI_LEN 4
92#define MID_TMSI_LEN (TMSI_LEN + 2)
93
94static void generate_mid_from_tmsi(u_int8_t *buf, u_int8_t *tmsi_bcd)
95{
96 buf[0] = MID_TMSI_LEN;
97 buf[1] = 0xf0 | GSM_MI_TYPE_TMSI;
98 buf[2] = tmsi_bcd[0];
99 buf[3] = tmsi_bcd[1];
100 buf[4] = tmsi_bcd[2];
101 buf[5] = tmsi_bcd[3];
102}
103
Harald Welte8470bf22008-12-25 23:28:35 +0000104static struct msgb *gsm48_msgb_alloc(void)
105{
106 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
107}
108
Harald Welte52b1f982008-12-23 20:25:15 +0000109static int gsm0408_sendmsg(struct msgb *msg)
110{
Harald Welte8470bf22008-12-25 23:28:35 +0000111 if (msg->lchan)
112 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000113
Harald Welte8470bf22008-12-25 23:28:35 +0000114 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000115}
116
117static int gsm0408_rcv_cc(struct msgb *msg)
118{
119 struct gsm48_hdr *gh = msgb_l3(msg);
120
121 switch (gh->msg_type & 0xbf) {
122 case GSM48_MT_CC_CALL_CONF:
123 /* Response to SETUP */
124 DEBUGP(DCC, "CALL CONFIRM\n");
125 break;
126 case GSM48_MT_CC_RELEASE_COMPL:
127 DEBUGP(DCC, "RELEASE COMPLETE\n");
128 break;
129 case GSM48_MT_CC_ALERTING:
130 DEBUGP(DCC, "ALERTING\n");
131 break;
132 case GSM48_MT_CC_CONNECT:
133 DEBUGP(DCC, "CONNECT\n");
134 /* need to respond with CONNECT_ACK */
135 break;
136 case GSM48_MT_CC_RELEASE:
137 DEBUGP(DCC, "RELEASE\n");
138 /* need to respond with RELEASE_COMPLETE */
139 break;
140 case GSM48_MT_CC_EMERG_SETUP:
141 //DEBUGP(DCC, "EMERGENCY SETUP\n");
142 case GSM48_MT_CC_SETUP:
143 //DEBUGP(DCC, "SETUP\n");
144 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
145 default:
146 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
147 gh->msg_type);
148 break;
149 }
Harald Welte8470bf22008-12-25 23:28:35 +0000150
151 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000152}
153
154/* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000155int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000156{
Harald Welte8470bf22008-12-25 23:28:35 +0000157 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000158 struct gsm48_hdr *gh;
159
Harald Welte8470bf22008-12-25 23:28:35 +0000160 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000161
162 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
163 gh->proto_discr = GSM48_PDISC_MM;
164 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
165 gh->data[0] = cause;
166
167 DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n");
168
169 return gsm0408_sendmsg(msg);
170}
171
172/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte8470bf22008-12-25 23:28:35 +0000173int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int8_t *tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000174{
Harald Welte8470bf22008-12-25 23:28:35 +0000175 struct gsm_bts *bts = lchan->ts->trx->bts;
176 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000177 struct gsm48_hdr *gh;
178 struct gsm48_loc_area_id *lai;
179 u_int8_t *mid;
180
Harald Welte8470bf22008-12-25 23:28:35 +0000181 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000182
183 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
184 gh->proto_discr = GSM48_PDISC_MM;
185 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
186
187 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
188 generate_lai(lai, bts->network->country_code,
189 bts->network->network_code, bts->location_area_code);
190
191 mid = msgb_put(msg, MID_TMSI_LEN);
192 generate_mid_from_tmsi(mid, tmsi);
193
194 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
195
196 return gsm0408_sendmsg(msg);
197}
198
Harald Welte52b1f982008-12-23 20:25:15 +0000199/* Chapter 9.2.15 */
200static int mm_loc_upd_req(struct msgb *msg)
201{
Harald Welte8470bf22008-12-25 23:28:35 +0000202 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000203 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000204 struct gsm48_loc_upd_req *lu;
205 struct gsm_subscriber *subscr;
Harald Welte8470bf22008-12-25 23:28:35 +0000206 u_int8_t mi_type;
Harald Welte52b1f982008-12-23 20:25:15 +0000207
Harald Welte8470bf22008-12-25 23:28:35 +0000208 lu = (struct gsm48_loc_upd_req *) gh->data;
209
210 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000211
212 switch (mi_type) {
213 case GSM_MI_TYPE_IMSI:
214 /* look up subscriber based on IMSI */
215 subscr = subscr_get_by_imsi(&lu->mi[1]);
216 break;
217 case GSM_MI_TYPE_TMSI:
218 /* look up the subscriber based on TMSI, request IMSI if it fails */
219 subscr = subscr_get_by_tmsi(&lu->mi[1]);
220 if (!subscr) {
221 /* FIXME: send IDENTITY REQUEST message to get IMSI */
222 //gsm0408_identity_request(...GSM_MI_TYPE_IMSI);
223 }
224 break;
225 case GSM_MI_TYPE_IMEI:
226 case GSM_MI_TYPE_IMEISV:
227 /* no sim card... FIXME: what to do ? */
228 fprintf(stderr, "Unimplemented mobile identity type\n");
229 break;
230 default:
231 fprintf(stderr, "Unknown mobile identity type\n");
232 break;
233 }
234
235 if (!subscr) {
236 /* 0x16 is congestion */
Harald Welte8470bf22008-12-25 23:28:35 +0000237 gsm0408_loc_upd_rej(msg->lchan, 0x16);
Harald Welte52b1f982008-12-23 20:25:15 +0000238 return -EINVAL;
239 }
240
Harald Welte8470bf22008-12-25 23:28:35 +0000241 msg->lchan->subscr = subscr;
Harald Welte52b1f982008-12-23 20:25:15 +0000242 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000243
244 return gsm0408_loc_upd_acc(msg->lchan, subscr->tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000245}
246
247static int gsm0408_rcv_mm(struct msgb *msg)
248{
249 struct gsm48_hdr *gh = msgb_l3(msg);
250 int rc;
251
252 switch (gh->msg_type & 0xbf) {
253 case GSM48_MT_MM_LOC_UPD_REQUEST:
254 DEBUGP(DMM, "LOCATION UPDATE REQUEST\n");
255 rc = mm_loc_upd_req(msg);
256 break;
257 case GSM48_MT_MM_ID_RESP:
258 case GSM48_MT_MM_TMSI_REALL_COMPL:
259 case GSM48_MT_MM_AUTH_RESP:
260 case GSM48_MT_MM_IMSI_DETACH_IND:
261 case GSM48_MT_MM_CM_SERV_REQ:
262 case GSM48_MT_MM_CM_REEST_REQ:
263 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
264 gh->msg_type);
265 break;
266 default:
267 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
268 gh->msg_type);
269 break;
270 }
271
272 return rc;
273}
274static int gsm0408_rcv_rr(struct msgb *msg)
275{
276 struct gsm48_hdr *gh = msgb_l3(msg);
277
278 switch (gh->msg_type) {
279 case GSM48_MT_RR_CLSM_CHG:
280 DEBUGP(DRR, "CLASSMARK CHANGE\n");
281 /* FIXME: what to do ?!? */
282 break;
283 case GSM48_MT_RR_PAG_RESP:
284 default:
285 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
286 gh->msg_type);
287 break;
288 }
289
290 return 0;
291}
292
293/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
294int gsm0408_rcvmsg(struct msgb *msg)
295{
296 struct gsm48_hdr *gh = msgb_l3(msg);
297 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000298 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000299
300 switch (pdisc) {
301 case GSM48_PDISC_CC:
302 rc = gsm0408_rcv_cc(msg);
303 break;
304 case GSM48_PDISC_MM:
305 rc = gsm0408_rcv_mm(msg);
306 break;
307 case GSM48_PDISC_RR:
308 rc = gsm0408_rcv_rr(msg);
309 break;
310 case GSM48_PDISC_MM_GPRS:
311 case GSM48_PDISC_SM:
312 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
313 pdisc);
314 break;
315 default:
316 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
317 pdisc);
318 break;
319 }
320
321 return rc;
322}
Harald Welte8470bf22008-12-25 23:28:35 +0000323
324enum chreq_type {
325 CHREQ_T_EMERG_CALL,
326 CHREQ_T_CALL_REEST_TCH_F,
327 CHREQ_T_CALL_REEST_TCH_H,
328 CHREQ_T_CALL_REEST_TCH_H_DBL,
329 CHREQ_T_SDCCH,
330 CHREQ_T_TCH_F,
331 CHREQ_T_VOICE_CALL_TCH_H,
332 CHREQ_T_DATA_CALL_TCH_H,
333 CHREQ_T_LOCATION_UPD,
334 CHREQ_T_PAG_R_ANY,
335 CHREQ_T_PAG_R_TCH_F,
336 CHREQ_T_PAG_R_TCH_FH,
337};
338
339/* Section 9.1.8 / Table 9.9 */
340struct chreq {
341 u_int8_t val;
342 u_int8_t mask;
343 enum chreq_type type;
344};
345
346/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
347static const struct chreq chreq_type_neci1[] = {
348 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
349 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
350 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
351 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
352 { 0xe0, 0xe0, CHREQ_T_SDCCH },
353 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
354 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
355 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
356 { 0x10, 0xf0, CHREQ_T_SDCCH },
357 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
358 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
359 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
360};
361
362/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
363static const struct chreq chreq_type_neci0[] = {
364 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
365 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
366 { 0xe0, 0xe0, CHREQ_T_TCH_F },
367 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
368 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
369 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
370 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
371 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
372};
373
374static const enum gsm_chan_t ctype_by_chreq[] = {
375 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
376 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
377 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
378 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
379 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
380 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
381 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
382 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
383 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
384 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
385 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
386 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
387};
388
389enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
390{
391 int i;
392 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
393
394 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
395 const struct chreq *chr = &chreq_type_neci0[i];
396 if ((ra & chr->mask) == chr->val)
397 return ctype_by_chreq[chr->type];
398 }
399 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
400 return GSM_LCHAN_SDCCH;
401}