blob: e771ea3b22f3190d48f0c1e2985af4cfc731af0f [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
167
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200168void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
169 u_int16_t mnc, u_int16_t lac)
170{
171 u_int8_t bcd[3];
172
173 to_bcd(bcd, mcc);
174 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
175 lai48->digits[1] = bcd[2];
176
177 to_bcd(bcd, mnc);
178 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
179#if 0
180 lai48->digits[1] |= bcd[2] << 4;
181 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
182#else
183 lai48->digits[1] |= 0xf << 4;
184 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
185#endif
186
187 lai48->lac = htons(lac);
188}
189
190int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
191{
192 u_int32_t *tptr = (u_int32_t *) &buf[3];
193
194 buf[0] = GSM48_IE_MOBILE_ID;
195 buf[1] = GSM48_TMSI_LEN;
196 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
197 *tptr = htonl(tmsi);
198
199 return 7;
200}
201
202/* Section 9.1.8 / Table 9.9 */
203struct chreq {
204 u_int8_t val;
205 u_int8_t mask;
206 enum chreq_type type;
207};
208
209/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
210static const struct chreq chreq_type_neci1[] = {
211 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
212 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
213 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
214 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
215 { 0xe0, 0xe0, CHREQ_T_SDCCH },
216 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
217 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
218 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
219 { 0x10, 0xf0, CHREQ_T_SDCCH },
220 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
221 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
222 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
223};
224
225/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
226static const struct chreq chreq_type_neci0[] = {
227 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
228 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
229 { 0xe0, 0xe0, CHREQ_T_TCH_F },
230 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
231 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
232 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
233 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
234 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
235};
236
237static const enum gsm_chan_t ctype_by_chreq[] = {
238 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
239 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
240 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
241 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
242 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
243 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
244 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
245 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
246 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
247 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
248 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
249 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
250};
251
252static const enum gsm_chreq_reason_t reason_by_chreq[] = {
253 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
254 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
255 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
256 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
257 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
258 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
259 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
260 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
261 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
262 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
263 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
264 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
265};
266
267enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
268{
269 int i;
270 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
271
272 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
273 const struct chreq *chr = &chreq_type_neci0[i];
274 if ((ra & chr->mask) == chr->val)
275 return ctype_by_chreq[chr->type];
276 }
277 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
278 return GSM_LCHAN_SDCCH;
279}
280
281enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
282{
283 int i;
284 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
285
286 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
287 const struct chreq *chr = &chreq_type_neci0[i];
288 if ((ra & chr->mask) == chr->val)
289 return reason_by_chreq[chr->type];
290 }
291 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
292 return GSM_CHREQ_REASON_OTHER;
293}
294
295/* 7.1.7 and 9.1.7: RR CHANnel RELease */
296int gsm48_send_rr_release(struct gsm_lchan *lchan)
297{
298 struct msgb *msg = gsm48_msgb_alloc();
299 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
300 u_int8_t *cause;
301
302 msg->lchan = lchan;
303 gh->proto_discr = GSM48_PDISC_RR;
304 gh->msg_type = GSM48_MT_RR_CHAN_REL;
305
306 cause = msgb_put(msg, 1);
307 cause[0] = GSM48_RR_CAUSE_NORMAL;
308
309 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
310 lchan->nr, lchan->type);
311
312 /* Send actual release request to MS */
313 gsm48_sendmsg(msg, NULL);
314 /* FIXME: Start Timer T3109 */
315
316 /* Deactivate the SACCH on the BTS side */
317 return rsl_deact_sacch(lchan);
318}
319
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200320/* Convert Mobile Identity (10.5.1.4) to string */
321int gsm48_mi_to_string(char *string, const int str_len, const u_int8_t *mi, const int mi_len)
322{
323 int i;
324 u_int8_t mi_type;
325 char *str_cur = string;
326 u_int32_t tmsi;
327
328 mi_type = mi[0] & GSM_MI_TYPE_MASK;
329
330 switch (mi_type) {
331 case GSM_MI_TYPE_NONE:
332 break;
333 case GSM_MI_TYPE_TMSI:
334 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
335 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
336 memcpy(&tmsi, &mi[1], 4);
337 tmsi = ntohl(tmsi);
338 return snprintf(string, str_len, "%u", tmsi);
339 }
340 break;
341 case GSM_MI_TYPE_IMSI:
342 case GSM_MI_TYPE_IMEI:
343 case GSM_MI_TYPE_IMEISV:
344 *str_cur++ = bcd2char(mi[0] >> 4);
345
346 for (i = 1; i < mi_len; i++) {
347 if (str_cur + 2 >= string + str_len)
348 return str_cur - string;
349 *str_cur++ = bcd2char(mi[i] & 0xf);
350 /* skip last nibble in last input byte when GSM_EVEN */
351 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
352 *str_cur++ = bcd2char(mi[i] >> 4);
353 }
354 break;
355 default:
356 break;
357 }
358 *str_cur++ = '\0';
359
360 return str_cur - string;
361}
362