blob: c62f04d57b912e294b606eb1f79a015d430491d5 [file] [log] [blame]
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02001/* 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 * utility functions
4 */
5
6/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26#include <stdio.h>
27#include <stdlib.h>
28#include <netinet/in.h>
29
30#include <openbsc/msgb.h>
31#include <openbsc/debug.h>
32#include <openbsc/gsm_04_08.h>
33#include <openbsc/transaction.h>
34
35#define GSM48_ALLOC_SIZE 1024
36#define GSM48_ALLOC_HEADROOM 128
37
38/* should ip.access BTS use direct RTP streams between each other (1),
39 * or should OpenBSC always act as RTP relay/proxy in between (0) ? */
40int ipacc_rtp_direct = 1;
41
42
43const char *gsm0408_cc_msg_names[] = {
44 "unknown 0x00",
45 "ALERTING",
46 "CALL_PROC",
47 "PROGRESS",
48 "ESTAB",
49 "SETUP",
50 "ESTAB_CONF",
51 "CONNECT",
52 "CALL_CONF",
53 "START_CC",
54 "unknown 0x0a",
55 "RECALL",
56 "unknown 0x0c",
57 "unknown 0x0d",
58 "EMERG_SETUP",
59 "CONNECT_ACK",
60 "USER_INFO",
61 "unknown 0x11",
62 "unknown 0x12",
63 "MODIFY_REJECT",
64 "unknown 0x14",
65 "unknown 0x15",
66 "unknown 0x16",
67 "MODIFY",
68 "HOLD",
69 "HOLD_ACK",
70 "HOLD_REJ",
71 "unknown 0x1b",
72 "RETR",
73 "RETR_ACK",
74 "RETR_REJ",
75 "MODIFY_COMPL",
76 "unknown 0x20",
77 "unknown 0x21",
78 "unknown 0x22",
79 "unknown 0x23",
80 "unknown 0x24",
81 "DISCONNECT",
82 "unknown 0x26",
83 "unknown 0x27",
84 "unknown 0x28",
85 "unknown 0x29",
86 "RELEASE_COMPL",
87 "unknown 0x2b",
88 "unknown 0x2c",
89 "RELEASE",
90 "unknown 0x2e",
91 "unknown 0x2f",
92 "unknown 0x30",
93 "STOP_DTMF",
94 "STOP_DTMF_ACK",
95 "unknown 0x33",
96 "STATUS_ENQ",
97 "START_DTMF",
98 "START_DTMF_ACK",
99 "START_DTMF_REJ",
100 "unknown 0x38",
101 "CONG_CTRL",
102 "FACILITY",
103 "unknown 0x3b",
104 "STATUS",
105 "unknown 0x3c",
106 "NOTIFY",
107 "unknown 0x3f",
108};
109
110
111struct msgb *gsm48_msgb_alloc(void)
112{
113 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM,
114 "GSM 04.08");
115}
116
117int gsm48_sendmsg(struct msgb *msg, struct gsm_trans *trans)
118{
119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
120
121 /* if we get passed a transaction reference, do some common
122 * work that the caller no longer has to do */
123 if (trans) {
124 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
125 msg->lchan = trans->lchan;
126 }
127
128 if (msg->lchan) {
129 msg->trx = msg->lchan->ts->trx;
130
131 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
132 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
133 "Sending '%s' to MS.\n", msg->trx->bts->nr,
134 msg->trx->nr, msg->lchan->ts->nr,
135 gh->proto_discr & 0xf0,
136 gsm0408_cc_msg_names[gh->msg_type & 0x3f]);
137 else
138 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
139 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
140 msg->trx->nr, msg->lchan->ts->nr,
141 gh->proto_discr, gh->msg_type);
142 }
143
144 msg->l3h = msg->data;
145
146 return rsl_data_request(msg, 0);
147}
148
149static void to_bcd(u_int8_t *bcd, u_int16_t val)
150{
151 bcd[2] = val % 10;
152 val = val / 10;
153 bcd[1] = val % 10;
154 val = val / 10;
155 bcd[0] = val % 10;
156 val = val / 10;
157}
158
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200159static char bcd2char(u_int8_t bcd)
160{
161 if (bcd < 0xa)
162 return '0' + bcd;
163 else
164 return 'A' + (bcd - 0xa);
165}
166
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200167/* only works for numbers in ascci */
168static u_int8_t char2bcd(char c)
169{
170 return c - 0x30;
171}
172
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200173
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200174void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
175 u_int16_t mnc, u_int16_t lac)
176{
177 u_int8_t bcd[3];
178
179 to_bcd(bcd, mcc);
180 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
181 lai48->digits[1] = bcd[2];
182
183 to_bcd(bcd, mnc);
184 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
185#if 0
186 lai48->digits[1] |= bcd[2] << 4;
187 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
188#else
189 lai48->digits[1] |= 0xf << 4;
190 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
191#endif
192
193 lai48->lac = htons(lac);
194}
195
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200196int gsm48_generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200197{
198 u_int32_t *tptr = (u_int32_t *) &buf[3];
199
200 buf[0] = GSM48_IE_MOBILE_ID;
201 buf[1] = GSM48_TMSI_LEN;
202 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
203 *tptr = htonl(tmsi);
204
205 return 7;
206}
207
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200208int gsm48_generate_mid_from_imsi(u_int8_t *buf, const char *imsi)
209{
210 unsigned int length = strlen(imsi), i, off = 0;
211 u_int8_t odd = (length & 0x1) == 1;
212
213 buf[0] = GSM48_IE_MOBILE_ID;
214 buf[2] = char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3);
215
216 /* if the length is even we will fill half of the last octet */
217 if (odd)
218 buf[1] = (length + 1) >> 1;
219 else
220 buf[1] = (length + 2) >> 1;
221
222 for (i = 1; i < buf[1]; ++i) {
223 u_int8_t lower, upper;
224
225 lower = char2bcd(imsi[++off]);
226 if (!odd && off + 1 == length)
227 upper = 0x0f;
228 else
229 upper = char2bcd(imsi[++off]) & 0x0f;
230
231 buf[2 + i] = (upper << 4) | lower;
232 }
233
234 return 2 + buf[1];
235}
236
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200237/* Section 9.1.8 / Table 9.9 */
238struct chreq {
239 u_int8_t val;
240 u_int8_t mask;
241 enum chreq_type type;
242};
243
244/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
245static const struct chreq chreq_type_neci1[] = {
246 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
247 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
248 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
249 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
250 { 0xe0, 0xe0, CHREQ_T_SDCCH },
251 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
252 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
253 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
254 { 0x10, 0xf0, CHREQ_T_SDCCH },
255 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
256 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
257 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
258};
259
260/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
261static const struct chreq chreq_type_neci0[] = {
262 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
263 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
264 { 0xe0, 0xe0, CHREQ_T_TCH_F },
265 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
266 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
267 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
268 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
269 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
270};
271
272static const enum gsm_chan_t ctype_by_chreq[] = {
273 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
274 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
275 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
276 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
277 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
278 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
279 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
280 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
281 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
282 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
283 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
284 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
285};
286
287static const enum gsm_chreq_reason_t reason_by_chreq[] = {
288 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
289 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
290 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
291 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
292 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
293 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
294 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
295 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
296 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
297 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
298 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
299 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
300};
301
302enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
303{
304 int i;
305 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
306
307 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
308 const struct chreq *chr = &chreq_type_neci0[i];
309 if ((ra & chr->mask) == chr->val)
310 return ctype_by_chreq[chr->type];
311 }
312 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
313 return GSM_LCHAN_SDCCH;
314}
315
316enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
317{
318 int i;
319 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
320
321 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
322 const struct chreq *chr = &chreq_type_neci0[i];
323 if ((ra & chr->mask) == chr->val)
324 return reason_by_chreq[chr->type];
325 }
326 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
327 return GSM_CHREQ_REASON_OTHER;
328}
329
330/* 7.1.7 and 9.1.7: RR CHANnel RELease */
331int gsm48_send_rr_release(struct gsm_lchan *lchan)
332{
333 struct msgb *msg = gsm48_msgb_alloc();
334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
335 u_int8_t *cause;
336
337 msg->lchan = lchan;
338 gh->proto_discr = GSM48_PDISC_RR;
339 gh->msg_type = GSM48_MT_RR_CHAN_REL;
340
341 cause = msgb_put(msg, 1);
342 cause[0] = GSM48_RR_CAUSE_NORMAL;
343
344 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
345 lchan->nr, lchan->type);
346
347 /* Send actual release request to MS */
348 gsm48_sendmsg(msg, NULL);
349 /* FIXME: Start Timer T3109 */
350
351 /* Deactivate the SACCH on the BTS side */
352 return rsl_deact_sacch(lchan);
353}
354
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200355/* Convert Mobile Identity (10.5.1.4) to string */
356int gsm48_mi_to_string(char *string, const int str_len, const u_int8_t *mi, const int mi_len)
357{
358 int i;
359 u_int8_t mi_type;
360 char *str_cur = string;
361 u_int32_t tmsi;
362
363 mi_type = mi[0] & GSM_MI_TYPE_MASK;
364
365 switch (mi_type) {
366 case GSM_MI_TYPE_NONE:
367 break;
368 case GSM_MI_TYPE_TMSI:
369 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
370 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
371 memcpy(&tmsi, &mi[1], 4);
372 tmsi = ntohl(tmsi);
373 return snprintf(string, str_len, "%u", tmsi);
374 }
375 break;
376 case GSM_MI_TYPE_IMSI:
377 case GSM_MI_TYPE_IMEI:
378 case GSM_MI_TYPE_IMEISV:
379 *str_cur++ = bcd2char(mi[0] >> 4);
380
381 for (i = 1; i < mi_len; i++) {
382 if (str_cur + 2 >= string + str_len)
383 return str_cur - string;
384 *str_cur++ = bcd2char(mi[i] & 0xf);
385 /* skip last nibble in last input byte when GSM_EVEN */
386 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
387 *str_cur++ = bcd2char(mi[i] >> 4);
388 }
389 break;
390 default:
391 break;
392 }
393 *str_cur++ = '\0';
394
395 return str_cur - string;
396}
397