blob: 274d3b6bf8b5fb9e6b4138c22a835e5bc1eccf4b [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
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
41#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000045#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000046#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte8470bf22008-12-25 23:28:35 +000048#define GSM48_ALLOC_SIZE 1024
49#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000050
Harald Welte09e38af2009-02-16 22:52:23 +000051static const struct tlv_definition rsl_att_tlvdef = {
52 .def = {
53 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
54 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
55 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
56 [GSM48_IE_UTC] = { TLV_TYPE_TV },
57 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
58 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
59
60 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
61 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
62 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
63 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
64 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
65 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
66 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
67 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
68 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
69 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
70 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
73 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
76 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
77 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
78 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
79 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
80 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
81 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
82 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
83 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
84 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
85 /* FIXME: more elements */
86 },
87};
Harald Weltecf5b3592009-05-01 18:28:42 +000088
89static const char *rr_cause_names[] = {
90 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
91 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
92 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
93 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
94 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
95 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
96 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
97 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
98 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
99 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
100 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
101 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
102 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
103 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
104 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
105 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
106 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
107};
108
109static char strbuf[64];
110
111static const char *rr_cause_name(u_int8_t cause)
112{
113 if (cause < ARRAY_SIZE(rr_cause_names) &&
114 rr_cause_names[cause])
115 return rr_cause_names[cause];
116
117 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
118 return strbuf;
119}
120
Harald Weltef7c43522009-06-09 20:24:21 +0000121static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
122 int len)
123{
124 memset(rep, 0, sizeof(*rep));
125
126 if (data[0] & 0x80)
127 rep->flags |= MEAS_REP_F_BA1;
128 if (data[0] & 0x40)
129 rep->flags |= MEAS_REP_F_DTX;
130 if (data[1] & 0x40)
131 rep->flags |= MEAS_REP_F_VALID;
132
133 rep->rxlev_full = data[0] & 0x3f;
134 rep->rxlev_sub = data[1] & 0x3f;
135 rep->rxqual_full = (data[3] >> 4) & 0x7;
136 rep->rxqual_sub = (data[3] >> 1) & 0x7;
137 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
138 if (rep->num_cell < 1)
139 return;
140
141 /* an encoding nightmare in perfection */
142
143 rep->cell[0].rxlev = data[4] & 0x3f;
144 rep->cell[0].bcch_freq = data[5] >> 2;
145 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
146 if (rep->num_cell < 2)
147 return;
148
149 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
150 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
151 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
152 if (rep->num_cell < 3)
153 return;
154
155 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
156 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
157 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
158 if (rep->num_cell < 4)
159 return;
160
161 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
162 rep->cell[3].bcch_freq = data[11] & 0x1f;
163 rep->cell[3].bsic = data[12] >> 2;
164 if (rep->num_cell < 5)
165 return;
166
167 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
168 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
169 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
170 if (rep->num_cell < 6)
171 return;
172
173 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
174 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
175 rep->cell[5].bsic = data[16] & 0x3f;
176}
177
Holger Freytherd51524f2009-06-09 08:27:07 +0000178int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000179static int gsm48_tx_simple(struct gsm_lchan *lchan,
180 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000181static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000182
Harald Welte52b1f982008-12-23 20:25:15 +0000183struct gsm_lai {
184 u_int16_t mcc;
185 u_int16_t mnc;
186 u_int16_t lac;
187};
188
Holger Freyther89824fc2008-12-30 16:18:18 +0000189static int authorize_everonye = 0;
190void gsm0408_allow_everyone(int everyone)
191{
192 printf("Allowing everyone?\n");
193 authorize_everonye = everyone;
194}
195
Holger Freythere97f7fb2008-12-31 18:52:11 +0000196static int reject_cause = 0;
197void gsm0408_set_reject_cause(int cause)
198{
199 reject_cause = cause;
200}
201
Holger Freyther73487a22008-12-31 18:53:57 +0000202static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
203 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000204{
205 if (!subscriber)
206 return 0;
207
Holger Freyther73487a22008-12-31 18:53:57 +0000208 /*
209 * Do not send accept yet as more information should arrive. Some
210 * phones will not send us the information and we will have to check
211 * what we want to do with that.
212 */
213 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
214 return 0;
215
Holger Freyther89824fc2008-12-30 16:18:18 +0000216 if (authorize_everonye)
217 return 1;
218
219 return subscriber->authorized;
220}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000221
Holger Freyther73487a22008-12-31 18:53:57 +0000222static void release_loc_updating_req(struct gsm_lchan *lchan)
223{
Harald Welte179f0642008-12-31 23:59:18 +0000224 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000225 return;
226
Harald Welteff117a82009-05-23 05:22:08 +0000227 bsc_del_timer(&lchan->loc_operation->updating_timer);
Holger Freyther73487a22008-12-31 18:53:57 +0000228 free(lchan->loc_operation);
229 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000230 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000231}
232
233static void allocate_loc_updating_req(struct gsm_lchan *lchan)
234{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000235 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000236 release_loc_updating_req(lchan);
237
238 lchan->loc_operation = (struct gsm_loc_updating_operation *)
239 malloc(sizeof(*lchan->loc_operation));
240 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
241}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000242
Holger Freytherd51524f2009-06-09 08:27:07 +0000243static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
244{
245 u_int32_t tmsi;
246
247 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
248 db_subscriber_alloc_tmsi(lchan->subscr);
249 subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
250 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
251 release_loc_updating_req(lchan);
252 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
253 }
254
255 return 0;
256}
257
Holger Freyther7c19f742009-06-06 13:54:35 +0000258static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
259 void *handler_data, void *signal_data)
260{
261 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
262 return 0;
263
264 /*
265 * Cancel any outstanding location updating request
266 * operation taking place on the lchan.
267 */
268 struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data;
269 release_loc_updating_req(lchan);
270
271 return 0;
272}
273
274/*
275 * This will be ran by the linker when loading the DSO. We use it to
276 * do system initialization, e.g. registration of signal handlers.
277 */
278static __attribute__((constructor)) void on_dso_load_0408(void)
279{
280 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
281}
282
Harald Welte52b1f982008-12-23 20:25:15 +0000283static void to_bcd(u_int8_t *bcd, u_int16_t val)
284{
Harald Welte4b634542008-12-27 01:55:51 +0000285 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000286 val = val / 10;
287 bcd[1] = val % 10;
288 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000289 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000290 val = val / 10;
291}
292
Holger Freyther17746612008-12-28 16:32:44 +0000293void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000294 u_int16_t mnc, u_int16_t lac)
295{
296 u_int8_t bcd[3];
297
298 to_bcd(bcd, mcc);
299 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
300 lai48->digits[1] = bcd[2];
301
302 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000303 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
304#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000305 lai48->digits[1] |= bcd[2] << 4;
306 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000307#else
308 lai48->digits[1] |= 0xf << 4;
309 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
310#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000311
Harald Welte4b634542008-12-27 01:55:51 +0000312 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000313}
314
Harald Welte255539c2008-12-28 02:26:27 +0000315#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000316#define MID_TMSI_LEN (TMSI_LEN + 2)
317
Harald Welte255539c2008-12-28 02:26:27 +0000318int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000319{
Harald Welte65e74cc2008-12-29 01:55:35 +0000320 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000321
Harald Welte4b634542008-12-27 01:55:51 +0000322 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000323 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000324 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000325 *tptr = htonl(tmsi);
326
327 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000328}
329
Harald Welte09e38af2009-02-16 22:52:23 +0000330static const char bcd_num_digits[] = {
331 '0', '1', '2', '3', '4', '5', '6', '7',
332 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
333};
334
335/* decode a 'called party BCD number' as in 10.5.4.7 */
336u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
337{
338 u_int8_t in_len = bcd_lv[0];
339 int i;
340
341 if (in_len < 1)
342 return 0;
343
344 for (i = 2; i <= in_len; i++) {
345 /* lower nibble */
346 output_len--;
347 if (output_len <= 1)
348 break;
349 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
350
351 /* higher nibble */
352 output_len--;
353 if (output_len <= 1)
354 break;
355 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
356 }
357 if (output_len >= 1)
358 *output++ = '\0';
359
360 /* return number type / calling plan */
361 return bcd_lv[1] & 0x3f;
362}
363
364/* convert a single ASCII character to call-control BCD */
365static int asc_to_bcd(const char asc)
366{
367 int i;
368
369 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
370 if (bcd_num_digits[i] == asc)
371 return i;
372 }
373 return -EINVAL;
374}
375
376/* convert a ASCII phone number to 'called party BCD number' */
377int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
378 u_int8_t type, const char *input)
379{
380 int in_len = strlen(input);
381 int i;
382 u_int8_t *bcd_cur = bcd_lv + 2;
383
384 if (in_len/2 + 1 > max_len)
385 return -EIO;
386
387 /* two digits per byte, plus type byte */
388 bcd_lv[0] = in_len/2 + 1;
389 if (in_len % 2)
390 bcd_lv[0]++;
391
392 /* if the caller wants to create a valid 'calling party BCD
393 * number', then the extension bit MUST NOT be set. For the
394 * 'called party BCD number' it MUST be set. *sigh */
395 bcd_lv[1] = type;
396
397 for (i = 0; i < in_len; i++) {
398 int rc = asc_to_bcd(input[i]);
399 if (rc < 0)
400 return rc;
401 if (i % 2 == 0)
402 *bcd_cur = rc;
403 else
404 *bcd_cur++ |= (rc << 4);
405 }
406 /* append padding nibble in case of odd length */
407 if (i % 2)
408 *bcd_cur++ |= 0xf0;
409
410 /* return how many bytes we used */
411 return (bcd_cur - bcd_lv);
412}
413
Holger Freyther819dd202009-01-04 03:52:50 +0000414struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000415{
416 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
417}
418
Holger Freyther3e2c3232009-01-04 03:55:31 +0000419int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000420{
Harald Welte65e74cc2008-12-29 01:55:35 +0000421 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
422
423 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000424 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000425
Harald Welte65e74cc2008-12-29 01:55:35 +0000426 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
427 /* Send a 04.08 call control message, add transaction
428 * ID and TI flag */
429 gh->proto_discr |= msg->lchan->call.transaction_id;
430
431 /* GSM 04.07 Section 11.2.3.1.3 */
432 switch (msg->lchan->call.type) {
433 case GSM_CT_MO:
434 gh->proto_discr |= 0x80;
435 break;
436 case GSM_CT_MT:
437 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000438 case GSM_CT_NONE:
439 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000440 }
441 }
442 }
443
Harald Welte4b634542008-12-27 01:55:51 +0000444 msg->l3h = msg->data;
445
Harald Welte8470bf22008-12-25 23:28:35 +0000446 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000447}
448
Harald Welte52b1f982008-12-23 20:25:15 +0000449
Holger Freyther429e7762008-12-30 13:28:30 +0000450/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000451int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000452{
Harald Welte8470bf22008-12-25 23:28:35 +0000453 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000454 struct gsm48_hdr *gh;
455
Harald Welte8470bf22008-12-25 23:28:35 +0000456 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000457
458 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
459 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000460 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000461 gh->data[0] = cause;
462
Harald Weltedb253af2008-12-30 17:56:55 +0000463 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
464
Harald Welte65e74cc2008-12-29 01:55:35 +0000465 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000466}
467
468/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000469int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000470{
Harald Welte8470bf22008-12-25 23:28:35 +0000471 struct gsm_bts *bts = lchan->ts->trx->bts;
472 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000473 struct gsm48_hdr *gh;
474 struct gsm48_loc_area_id *lai;
475 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000476 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000477
Harald Welte8470bf22008-12-25 23:28:35 +0000478 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000479
480 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
481 gh->proto_discr = GSM48_PDISC_MM;
482 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
483
484 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000485 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000486 bts->network->network_code, bts->location_area_code);
487
488 mid = msgb_put(msg, MID_TMSI_LEN);
489 generate_mid_from_tmsi(mid, tmsi);
490
491 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
492
Harald Weltedb253af2008-12-30 17:56:55 +0000493 ret = gsm48_sendmsg(msg);
494
Harald Weltedb253af2008-12-30 17:56:55 +0000495 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000496
Holger Freyther07cc8d82008-12-29 06:23:46 +0000497 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000498}
499
Harald Weltefc977a82008-12-27 10:19:37 +0000500static char bcd2char(u_int8_t bcd)
501{
502 if (bcd < 0xa)
503 return '0' + bcd;
504 else
505 return 'A' + (bcd - 0xa);
506}
507
Harald Weltebf5e8df2009-02-03 12:59:45 +0000508/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000509static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
510{
511 int i;
512 u_int8_t mi_type;
513 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000514 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000515
516 mi_type = mi[0] & GSM_MI_TYPE_MASK;
517
518 switch (mi_type) {
519 case GSM_MI_TYPE_NONE:
520 break;
521 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000522 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
523 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
524 memcpy(&tmsi, &mi[1], 4);
525 tmsi = ntohl(tmsi);
526 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000527 }
528 break;
529 case GSM_MI_TYPE_IMSI:
530 case GSM_MI_TYPE_IMEI:
531 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000532 *str_cur++ = bcd2char(mi[0] >> 4);
533
534 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000535 if (str_cur + 2 >= string + str_len)
536 return str_cur - string;
537 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000538 /* skip last nibble in last input byte when GSM_EVEN */
539 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
540 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000541 }
542 break;
543 default:
544 break;
545 }
Harald Weltefc977a82008-12-27 10:19:37 +0000546 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000547
Harald Weltefc977a82008-12-27 10:19:37 +0000548 return str_cur - string;
549}
550
Harald Weltebf5e8df2009-02-03 12:59:45 +0000551/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000552static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
553{
554 struct msgb *msg = gsm48_msgb_alloc();
555 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000556
Harald Welte231ad4f2008-12-27 11:15:38 +0000557 msg->lchan = lchan;
558
559 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
560 gh->proto_discr = GSM48_PDISC_MM;
561 gh->msg_type = GSM48_MT_MM_ID_REQ;
562 gh->data[0] = id_type;
563
Harald Welte65e74cc2008-12-29 01:55:35 +0000564 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000565}
566
567#define MI_SIZE 32
568
Harald Weltebf5e8df2009-02-03 12:59:45 +0000569/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000570static int mm_rx_id_resp(struct msgb *msg)
571{
572 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000573 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000574 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
575 char mi_string[MI_SIZE];
576
577 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000578 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000579 mi_type, mi_string);
580
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000581 /*
582 * Rogue messages could trick us but so is life
583 */
584 put_lchan(lchan);
585
Harald Welte75a983f2008-12-27 21:34:06 +0000586 switch (mi_type) {
587 case GSM_MI_TYPE_IMSI:
588 if (!lchan->subscr)
589 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000590 if (lchan->loc_operation)
591 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000592 break;
593 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000594 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000595 /* update subscribe <-> IMEI mapping */
596 if (lchan->subscr)
597 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000598 if (lchan->loc_operation)
599 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000600 break;
601 }
Holger Freyther73487a22008-12-31 18:53:57 +0000602
603 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000604 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000605}
606
Harald Welte255539c2008-12-28 02:26:27 +0000607
608static void loc_upd_rej_cb(void *data)
609{
610 struct gsm_lchan *lchan = data;
611
Holger Freyther73487a22008-12-31 18:53:57 +0000612 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000613 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000614 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000615}
616
Holger Freytherb7193e42008-12-29 17:44:08 +0000617static void schedule_reject(struct gsm_lchan *lchan)
618{
Holger Freyther73487a22008-12-31 18:53:57 +0000619 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
620 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000621 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000622}
623
Harald Welte2a139372009-02-22 21:14:55 +0000624static const char *lupd_name(u_int8_t type)
625{
626 switch (type) {
627 case GSM48_LUPD_NORMAL:
628 return "NORMAL";
629 case GSM48_LUPD_PERIODIC:
630 return "PEROIDOC";
631 case GSM48_LUPD_IMSI_ATT:
632 return "IMSI ATTACH";
633 default:
634 return "UNKNOWN";
635 }
636}
637
Harald Welte231ad4f2008-12-27 11:15:38 +0000638#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000639/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000640static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000641{
Harald Welte8470bf22008-12-25 23:28:35 +0000642 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000643 struct gsm48_loc_upd_req *lu;
644 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000645 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000646 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +0000647 char mi_string[MI_SIZE];
648 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000649
Harald Welte8470bf22008-12-25 23:28:35 +0000650 lu = (struct gsm48_loc_upd_req *) gh->data;
651
652 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000653
Harald Weltefc977a82008-12-27 10:19:37 +0000654 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
655
Holger Freyther79f4ae62009-06-02 03:25:04 +0000656 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000657 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000658
Holger Freythereaf04692009-06-06 13:54:44 +0000659 /*
660 * Pseudo Spoof detection: Just drop a second/concurrent
661 * location updating request.
662 */
663 if (lchan->loc_operation) {
664 DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n",
665 lchan->loc_operation);
666 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
667 return 0;
668 }
669
Holger Freyther73487a22008-12-31 18:53:57 +0000670 allocate_loc_updating_req(lchan);
671
Harald Welte52b1f982008-12-23 20:25:15 +0000672 switch (mi_type) {
673 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000674 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000675 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000676 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000677 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000678
Harald Welte52b1f982008-12-23 20:25:15 +0000679 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000680 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000681 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000682 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000683 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000684 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000685 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000686 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000687
Harald Welte52b1f982008-12-23 20:25:15 +0000688 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000689 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000690 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000691 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000692 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000693 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000694 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000695 }
696 break;
697 case GSM_MI_TYPE_IMEI:
698 case GSM_MI_TYPE_IMEISV:
699 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +0000700 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000701 break;
702 default:
Harald Welte2a139372009-02-22 21:14:55 +0000703 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000704 break;
705 }
706
Harald Welte255539c2008-12-28 02:26:27 +0000707 lchan->subscr = subscr;
708
Holger Freyther73487a22008-12-31 18:53:57 +0000709 /*
710 * Schedule the reject timer and check if we can let the
711 * subscriber into our network immediately or if we need to wait
712 * for identity responses.
713 */
714 schedule_reject(lchan);
Holger Freytherd51524f2009-06-09 08:27:07 +0000715 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000716}
717
Harald Welte7584aea2009-02-11 11:44:12 +0000718/* 9.1.5 Channel mode modify */
719int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
720{
721 struct msgb *msg = gsm48_msgb_alloc();
722 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
723 struct gsm48_chan_mode_modify *cmm =
724 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +0000725 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +0000726
Harald Welte4a543e82009-02-28 13:17:55 +0000727 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +0000728
Harald Welte45b407a2009-05-23 15:51:12 +0000729 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +0000730 msg->lchan = lchan;
731 gh->proto_discr = GSM48_PDISC_RR;
732 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
733
734 /* fill the channel information element, this code
735 * should probably be shared with rsl_rx_chan_rqd() */
736 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +0000737 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +0000738 cmm->chan_desc.h0.h = 0;
739 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
740 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
741 cmm->mode = mode;
742
743 return gsm48_sendmsg(msg);
744}
745
Harald Weltedb253af2008-12-30 17:56:55 +0000746/* Section 9.2.15a */
747int gsm48_tx_mm_info(struct gsm_lchan *lchan)
748{
749 struct msgb *msg = gsm48_msgb_alloc();
750 struct gsm48_hdr *gh;
751 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000752 u_int8_t *ptr8;
753 u_int16_t *ptr16;
754 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000755 int i;
756
757 msg->lchan = lchan;
758
759 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
760 gh->proto_discr = GSM48_PDISC_MM;
761 gh->msg_type = GSM48_MT_MM_INFO;
762
763 if (net->name_long) {
764 name_len = strlen(net->name_long);
765 /* 10.5.3.5a */
766 ptr8 = msgb_put(msg, 3);
767 ptr8[0] = GSM48_IE_NAME_LONG;
768 ptr8[1] = name_len*2 +1;
769 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
770
771 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
772 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000773 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000774
775 /* FIXME: Use Cell Broadcast, not UCS-2, since
776 * UCS-2 is only supported by later revisions of the spec */
777 }
778
779 if (net->name_short) {
780 name_len = strlen(net->name_short);
781 /* 10.5.3.5a */
782 ptr8 = (u_int8_t *) msgb_put(msg, 3);
783 ptr8[0] = GSM48_IE_NAME_LONG;
784 ptr8[1] = name_len*2 + 1;
785 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
786
Harald Weltee872cb12009-01-01 00:33:37 +0000787 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000788 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000789 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000790 }
791
792#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000793 /* move back to the top */
794 time_t cur_t;
795 struct tm* cur_time;
796 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000797 /* Section 10.5.3.9 */
798 cur_t = time(NULL);
799 cur_time = gmtime(cur_t);
800 ptr8 = msgb_put(msg, 8);
801 ptr8[0] = GSM48_IE_NET_TIME_TZ;
802 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
803 ptr8[2] = to_bcd8(cur_time->tm_mon);
804 ptr8[3] = to_bcd8(cur_time->tm_mday);
805 ptr8[4] = to_bcd8(cur_time->tm_hour);
806 ptr8[5] = to_bcd8(cur_time->tm_min);
807 ptr8[6] = to_bcd8(cur_time->tm_sec);
808 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
809 tz15min = (cur_time->tm_gmtoff)/(60*15);
810 ptr8[6] = to_bcd8(tz15min);
811 if (tz15min < 0)
812 ptr8[6] |= 0x80;
813#endif
814
815 return gsm48_sendmsg(msg);
816}
817
Harald Welte4b634542008-12-27 01:55:51 +0000818static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
819{
Harald Welte4b634542008-12-27 01:55:51 +0000820 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000821 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000822}
Harald Welteba4cf162009-01-10 01:49:35 +0000823
824/* 9.2.6 CM service reject */
825static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
826 enum gsm48_reject_value value)
827{
828 struct msgb *msg = gsm48_msgb_alloc();
829 struct gsm48_hdr *gh;
830
831 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
832
833 msg->lchan = lchan;
834 use_lchan(lchan);
835
836 gh->proto_discr = GSM48_PDISC_MM;
837 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
838 gh->data[0] = value;
839 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
840
841 return gsm48_sendmsg(msg);
842}
843
Harald Welte4ed0e922009-01-10 03:17:30 +0000844
845/*
846 * Handle CM Service Requests
847 * a) Verify that the packet is long enough to contain the information
848 * we require otherwsie reject with INCORRECT_MESSAGE
849 * b) Try to parse the TMSI. If we do not have one reject
850 * c) Check that we know the subscriber with the TMSI otherwise reject
851 * with a HLR cause
852 * d) Set the subscriber on the gsm_lchan and accept
853 */
Harald Welte4b634542008-12-27 01:55:51 +0000854static int gsm48_rx_mm_serv_req(struct msgb *msg)
855{
Harald Welteba4cf162009-01-10 01:49:35 +0000856 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000857 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000858
Harald Welteba4cf162009-01-10 01:49:35 +0000859 struct gsm_subscriber *subscr;
860 struct gsm48_hdr *gh = msgb_l3(msg);
861 struct gsm48_service_request *req =
862 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000863 /* unfortunately in Phase1 the classmar2 length is variable */
864 u_int8_t classmark2_len = gh->data[1];
865 u_int8_t *classmark2 = gh->data+2;
866 u_int8_t mi_len = *(classmark2 + classmark2_len);
867 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000868
Harald Weltec9e02182009-05-01 19:07:53 +0000869 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000870 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000871 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000872 return gsm48_tx_mm_serv_rej(msg->lchan,
873 GSM48_REJECT_INCORRECT_MESSAGE);
874 }
875
876 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000877 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000878 return gsm48_tx_mm_serv_rej(msg->lchan,
879 GSM48_REJECT_INCORRECT_MESSAGE);
880 }
881
Harald Weltec9e02182009-05-01 19:07:53 +0000882 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000883 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000884 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000885 return gsm48_tx_mm_serv_rej(msg->lchan,
886 GSM48_REJECT_INCORRECT_MESSAGE);
887 }
888
Harald Weltec9e02182009-05-01 19:07:53 +0000889 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000890 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000891 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000892
Holger Freythereb443982009-06-04 13:58:42 +0000893 subscr = subscr_get_by_tmsi(mi_string);
894
Harald Welte2a139372009-02-22 21:14:55 +0000895 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000896 if (!subscr)
897 return gsm48_tx_mm_serv_rej(msg->lchan,
898 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
899
900 if (!msg->lchan->subscr)
901 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000902 else if (msg->lchan->subscr != subscr) {
903 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
904 subscr_put(subscr);
905 }
906
Harald Weltef7c43522009-06-09 20:24:21 +0000907 subscr->classmark2_len = classmark2_len;
908 memcpy(subscr->classmark2, classmark2, classmark2_len);
909
Harald Welte4b634542008-12-27 01:55:51 +0000910 return gsm48_tx_mm_serv_ack(msg->lchan);
911}
912
Harald Welte2a139372009-02-22 21:14:55 +0000913static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
914{
915 struct gsm48_hdr *gh = msgb_l3(msg);
916 struct gsm48_imsi_detach_ind *idi =
917 (struct gsm48_imsi_detach_ind *) gh->data;
918 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
919 char mi_string[MI_SIZE];
920 struct gsm_subscriber *subscr;
921
922 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
923 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
924 mi_type, mi_string);
925
926 switch (mi_type) {
927 case GSM_MI_TYPE_TMSI:
928 subscr = subscr_get_by_tmsi(mi_string);
929 break;
930 case GSM_MI_TYPE_IMSI:
931 subscr = subscr_get_by_imsi(mi_string);
932 break;
933 case GSM_MI_TYPE_IMEI:
934 case GSM_MI_TYPE_IMEISV:
935 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000936 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000937 break;
938 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000939 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000940 break;
941 }
942
Holger Freyther4a49e772009-04-12 05:37:29 +0000943 if (subscr) {
944 subscr_update(subscr, msg->trx->bts,
945 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +0000946 DEBUGP(DMM, "Subscriber: %s\n",
947 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000948 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000949 } else
Harald Welte2a139372009-02-22 21:14:55 +0000950 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
951
952 put_lchan(msg->lchan);
953
954 return 0;
955}
956
Harald Welted2a7f5a2009-06-05 20:08:20 +0000957static int gsm48_rx_mm_status(struct msgb *msg)
958{
959 struct gsm48_hdr *gh = msgb_l3(msg);
960
961 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
962
963 return 0;
964}
965
Harald Weltebf5e8df2009-02-03 12:59:45 +0000966/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000967static int gsm0408_rcv_mm(struct msgb *msg)
968{
969 struct gsm48_hdr *gh = msgb_l3(msg);
970 int rc;
971
972 switch (gh->msg_type & 0xbf) {
973 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000974 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000975 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000976 break;
977 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000978 rc = mm_rx_id_resp(msg);
979 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000980 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000981 rc = gsm48_rx_mm_serv_req(msg);
982 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000983 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000984 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000985 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000986 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000987 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
988 msg->lchan->subscr ?
989 msg->lchan->subscr->imsi :
990 "unknown subscriber");
991 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000992 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000993 rc = gsm48_rx_mm_imsi_detach_ind(msg);
994 break;
995 case GSM48_MT_MM_CM_REEST_REQ:
996 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
997 break;
998 case GSM48_MT_MM_AUTH_RESP:
999 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001000 break;
1001 default:
1002 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1003 gh->msg_type);
1004 break;
1005 }
1006
1007 return rc;
1008}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001009
Harald Welte2d35ae62009-02-06 12:02:13 +00001010/* Receive a PAGING RESPONSE message from the MS */
1011static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1012{
1013 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001014 u_int8_t *classmark2_lv = gh->data + 1;
1015 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1016 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001017 char mi_string[MI_SIZE];
1018 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +00001019 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001020 int rc = 0;
1021
Harald Welte61548982009-02-22 21:26:29 +00001022 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001023 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1024 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001025 switch (mi_type) {
1026 case GSM_MI_TYPE_TMSI:
1027 subscr = subscr_get_by_tmsi(mi_string);
1028 break;
1029 case GSM_MI_TYPE_IMSI:
1030 subscr = subscr_get_by_imsi(mi_string);
1031 break;
1032 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001033
1034 if (!subscr) {
1035 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001036 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001037 return -EINVAL;
1038 }
1039 DEBUGP(DRR, "<- Channel was requested by %s\n",
1040 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001041
Harald Weltef7c43522009-06-09 20:24:21 +00001042 subscr->classmark2_len = *classmark2_lv;
1043 memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv);
1044
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001045 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001046 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001047 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001048 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1049 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001050 return -EINVAL;
1051 } else {
1052 DEBUGP(DRR, "<- Channel already owned by us\n");
1053 subscr_put(subscr);
1054 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001055 }
1056
Harald Welte595ad7b2009-02-16 22:05:44 +00001057 sig_data.subscr = subscr;
1058 sig_data.bts = msg->lchan->ts->trx->bts;
1059 sig_data.lchan = msg->lchan;
1060
1061 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +00001062 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001063
Harald Welte7584aea2009-02-11 11:44:12 +00001064 /* FIXME: somehow signal the completion of the PAGING to
1065 * the entity that requested the paging */
1066
Harald Welte2d35ae62009-02-06 12:02:13 +00001067 return rc;
1068}
1069
Harald Weltef7c43522009-06-09 20:24:21 +00001070static int gsm48_rx_rr_classmark(struct msgb *msg)
1071{
1072 struct gsm48_hdr *gh = msgb_l3(msg);
1073 struct gsm_subscriber *subscr = msg->lchan->subscr;
1074 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1075 u_int8_t cm2_len, cm3_len = 0;
1076 u_int8_t *cm2, *cm3 = NULL;
1077
1078 DEBUGP(DRR, "CLASSMARK CHANGE ");
1079
1080 /* classmark 2 */
1081 cm2_len = gh->data[0];
1082 cm2 = &gh->data[1];
1083 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1084
1085 if (payload_len > cm2_len + 1) {
1086 /* we must have a classmark3 */
1087 if (gh->data[cm2_len+1] != 0x20) {
1088 DEBUGPC(DRR, "ERR CM3 TAG\n");
1089 return -EINVAL;
1090 }
1091 if (cm2_len > 3) {
1092 DEBUGPC(DRR, "CM2 too long!\n");
1093 return -EINVAL;
1094 }
1095
1096 cm3_len = gh->data[cm2_len+2];
1097 cm3 = &gh->data[cm2_len+3];
1098 if (cm3_len > 14) {
1099 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1100 return -EINVAL;
1101 }
1102 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1103 }
1104 if (subscr) {
1105 subscr->classmark2_len = cm2_len;
1106 memcpy(subscr->classmark2, cm2, cm2_len);
1107 if (cm3) {
1108 subscr->classmark3_len = cm3_len;
1109 memcpy(subscr->classmark3, cm3, cm3_len);
1110 }
1111 }
1112
1113 /* FIXME: store the classmark2/3 values with the equipment register */
1114
1115 return 0;
1116}
1117
Harald Weltecf5b3592009-05-01 18:28:42 +00001118static int gsm48_rx_rr_status(struct msgb *msg)
1119{
1120 struct gsm48_hdr *gh = msgb_l3(msg);
1121
1122 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1123 rr_cause_name(gh->data[0]));
1124
1125 return 0;
1126}
1127
Harald Weltef7c43522009-06-09 20:24:21 +00001128static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1129{
1130 struct gsm48_hdr *gh = msgb_l3(msg);
1131 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1132 static struct gsm_meas_rep meas_rep;
1133
1134 DEBUGP(DRR, "MEASUREMENT REPORT ");
1135 parse_meas_rep(&meas_rep, gh->data, payload_len);
1136 if (meas_rep.flags & MEAS_REP_F_DTX)
1137 DEBUGPC(DRR, "DTX ");
1138 if (meas_rep.flags & MEAS_REP_F_BA1)
1139 DEBUGPC(DRR, "BA1 ");
1140 if (!(meas_rep.flags & MEAS_REP_F_VALID))
1141 DEBUGPC(DRR, "NOT VALID ");
1142 else
1143 DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
1144 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1145 meas_rep.rxqual_sub);
1146
1147 DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell);
1148
1149 /* FIXME: put the results somwhere */
1150
1151 return 0;
1152}
1153
Harald Weltebf5e8df2009-02-03 12:59:45 +00001154/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001155static int gsm0408_rcv_rr(struct msgb *msg)
1156{
1157 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001158 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001159
1160 switch (gh->msg_type) {
1161 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001162 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001163 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001164 case GSM48_MT_RR_GPRS_SUSP_REQ:
1165 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1166 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001167 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001168 rc = gsm48_rr_rx_pag_resp(msg);
1169 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001170 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1171 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001172 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001173 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001174 case GSM48_MT_RR_STATUS:
1175 rc = gsm48_rx_rr_status(msg);
1176 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001177 case GSM48_MT_RR_MEAS_REP:
1178 rc = gsm48_rx_rr_meas_rep(msg);
1179 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001180 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001181 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001182 gh->msg_type);
1183 break;
1184 }
1185
Harald Welte2d35ae62009-02-06 12:02:13 +00001186 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001187}
1188
Holger Freythere64a7a32009-02-06 21:55:37 +00001189/* 7.1.7 and 9.1.7 Channel release*/
1190int gsm48_send_rr_release(struct gsm_lchan *lchan)
1191{
1192 struct msgb *msg = gsm48_msgb_alloc();
1193 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1194 u_int8_t *cause;
1195
1196 msg->lchan = lchan;
1197 gh->proto_discr = GSM48_PDISC_RR;
1198 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1199
1200 cause = msgb_put(msg, 1);
1201 cause[0] = GSM48_RR_CAUSE_NORMAL;
1202
1203 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1204 lchan->nr, lchan->type);
1205
1206 return gsm48_sendmsg(msg);
1207}
1208
Harald Welte4bc90a12008-12-27 16:32:52 +00001209/* Call Control */
1210
Harald Welte7584aea2009-02-11 11:44:12 +00001211/* The entire call control code is written in accordance with Figure 7.10c
1212 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1213 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1214 * it for voice */
1215
Harald Welte4bc90a12008-12-27 16:32:52 +00001216static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1217{
1218 struct msgb *msg = gsm48_msgb_alloc();
1219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1220 u_int8_t *cause, *call_state;
1221
Harald Welte65e74cc2008-12-29 01:55:35 +00001222 gh->proto_discr = GSM48_PDISC_CC;
1223
Harald Welte4bc90a12008-12-27 16:32:52 +00001224 msg->lchan = lchan;
1225
Harald Welte4bc90a12008-12-27 16:32:52 +00001226 gh->msg_type = GSM48_MT_CC_STATUS;
1227
1228 cause = msgb_put(msg, 3);
1229 cause[0] = 2;
1230 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1231 cause[2] = 0x80 | 30; /* response to status inquiry */
1232
1233 call_state = msgb_put(msg, 1);
1234 call_state[0] = 0xc0 | 0x00;
1235
Harald Welte65e74cc2008-12-29 01:55:35 +00001236 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001237}
1238
Harald Welte6f4b7532008-12-29 00:39:37 +00001239static int gsm48_tx_simple(struct gsm_lchan *lchan,
1240 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001241{
1242 struct msgb *msg = gsm48_msgb_alloc();
1243 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1244
1245 msg->lchan = lchan;
1246
Harald Welte6f4b7532008-12-29 00:39:37 +00001247 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001248 gh->msg_type = msg_type;
1249
Harald Welte65e74cc2008-12-29 01:55:35 +00001250 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001251}
1252
Harald Welte09e38af2009-02-16 22:52:23 +00001253/* call-back from paging the B-end of the connection */
1254static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001255 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001256{
Harald Welte7ccf7782009-02-17 01:43:01 +00001257 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001258 struct gsm_call *remote_call = param;
1259 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001260 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001261
1262 if (hooknum != GSM_HOOK_RR_PAGING)
1263 return -EINVAL;
1264
1265 switch (event) {
1266 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001267 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001268 remote_call->remote_lchan = lchan;
1269 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001270 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001271 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1272 if (is_ipaccess_bts(lchan->ts->trx->bts))
1273 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001274 break;
1275 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001276 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001277 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001278 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001279 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001280 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001281 break;
1282 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001283 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001284}
1285
Harald Welte4bc90a12008-12-27 16:32:52 +00001286static int gsm48_cc_rx_status_enq(struct msgb *msg)
1287{
Harald Welte4a543e82009-02-28 13:17:55 +00001288 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001289 return gsm48_cc_tx_status(msg->lchan);
1290}
1291
1292static int gsm48_cc_rx_setup(struct msgb *msg)
1293{
Harald Welte7584aea2009-02-11 11:44:12 +00001294 struct gsm_call *call = &msg->lchan->call;
1295 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001296 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001297 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001298 char called_number[(43-2)*2 + 1] = "\0";
1299 struct tlv_parsed tp;
1300 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001301 int ret;
1302
1303 if (call->state == GSM_CSTATE_NULL ||
1304 call->state == GSM_CSTATE_RELEASE_REQ)
1305 use_lchan(msg->lchan);
1306
1307 call->type = GSM_CT_MO;
1308 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001309 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001310 call->transaction_id = gh->proto_discr & 0xf0;
1311
Harald Weltea4d49e92009-05-23 06:39:58 +00001312 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte09e38af2009-02-16 22:52:23 +00001313 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1314 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001315
Harald Welte09e38af2009-02-16 22:52:23 +00001316 /* Parse the number that was dialed and lookup subscriber */
1317 num_type = decode_bcd_number(called_number, sizeof(called_number),
1318 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001319
Harald Welte4b458152009-02-17 13:23:06 +00001320 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001321 called_number);
1322
Harald Welte09e38af2009-02-16 22:52:23 +00001323 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001324 if (!called_subscr) {
1325 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1326 put_lchan(msg->lchan);
1327 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001328 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001329 }
Harald Welte5a065df2009-02-22 21:13:18 +00001330 if (called_subscr == msg->lchan->subscr) {
1331 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1332 put_lchan(msg->lchan);
1333 subscr_put(called_subscr);
1334 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1335 GSM48_MT_CC_RELEASE_COMPL);
1336 }
Harald Welte7584aea2009-02-11 11:44:12 +00001337
Harald Welte09e38af2009-02-16 22:52:23 +00001338 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001339 call->called_subscr = called_subscr;
1340
Harald Welte7584aea2009-02-11 11:44:12 +00001341 /* start paging of the receiving end of the call */
Harald Weltee7b452a2009-05-23 06:34:37 +00001342 /* FIXME: we're assuming that the receiver is at the same BTS
1343 * than we are, which is obviously a wrong assumption in multi-BTS
1344 * case */
Harald Welte09e38af2009-02-16 22:52:23 +00001345 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1346 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001347
1348 /* send a CALL PROCEEDING message to the MO */
1349 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1350 GSM48_MT_CC_CALL_PROC);
1351
Harald Welte49f48b82009-02-17 15:29:33 +00001352 if (is_ipaccess_bts(msg->trx->bts))
1353 rsl_ipacc_bind(msg->lchan);
1354
Harald Welte7584aea2009-02-11 11:44:12 +00001355 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001356 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001357
1358err:
1359 /* FIXME: send some kind of RELEASE */
1360 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001361}
Harald Welte7584aea2009-02-11 11:44:12 +00001362
Harald Welte7ccf7782009-02-17 01:43:01 +00001363static int gsm48_cc_rx_alerting(struct msgb *msg)
1364{
1365 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001366
Harald Welte4b458152009-02-17 13:23:06 +00001367 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001368
1369 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001370 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001371 return -EIO;
1372
Harald Welte4b458152009-02-17 13:23:06 +00001373 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001374 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001375 GSM48_MT_CC_ALERTING);
1376}
1377
Harald Welte49f48b82009-02-17 15:29:33 +00001378/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001379static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001380{
Harald Welte11fa29c2009-02-19 17:24:39 +00001381 struct gsm_bts *bts = lchan->ts->trx->bts;
1382 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001383 struct gsm_bts_trx_ts *ts;
1384
Harald Welte11fa29c2009-02-19 17:24:39 +00001385 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1386 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1387 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1388
1389 if (bts->type != remote_bts->type) {
1390 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1391 return -EINVAL;
1392 }
Harald Welte49f48b82009-02-17 15:29:33 +00001393
Harald Welte11fa29c2009-02-19 17:24:39 +00001394 switch (bts->type) {
1395 case GSM_BTS_TYPE_NANOBTS_900:
1396 case GSM_BTS_TYPE_NANOBTS_1800:
1397 ts = remote_lchan->ts;
1398 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1399 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1400
1401 ts = lchan->ts;
1402 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1403 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1404 break;
1405 case GSM_BTS_TYPE_BS11:
1406 trau_mux_map_lchan(lchan, remote_lchan);
1407 break;
1408 default:
1409 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1410 break;
1411 }
Harald Welte49f48b82009-02-17 15:29:33 +00001412
1413 return 0;
1414}
1415
Harald Welte7ccf7782009-02-17 01:43:01 +00001416static int gsm48_cc_rx_connect(struct msgb *msg)
1417{
1418 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001419 int rc;
1420
Harald Welte4b458152009-02-17 13:23:06 +00001421 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001422
1423 rc = tch_map(msg->lchan, call->remote_lchan);
1424 if (rc)
1425 return -EIO;
1426
1427 if (!call->remote_lchan)
1428 return -EIO;
1429
Harald Welte4b458152009-02-17 13:23:06 +00001430 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001431 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1432 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001433 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001434
Harald Welte49f48b82009-02-17 15:29:33 +00001435
1436 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001437 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001438 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001439 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001440}
1441
1442static int gsm48_cc_rx_disconnect(struct msgb *msg)
1443{
1444 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001445 int rc;
1446
1447
1448 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001449 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001450 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001451 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001452 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001453 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1454 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001455
1456 /*
1457 * FIXME: This looks wrong! We should have a single
1458 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1459 * to the NULL state and relase the call
1460 */
1461 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001462
1463 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001464 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001465 return -EIO;
1466
Harald Welte4b458152009-02-17 13:23:06 +00001467 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001468 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001469 GSM48_MT_CC_DISCONNECT);
1470}
1471
1472static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1473
Harald Welte49f48b82009-02-17 15:29:33 +00001474int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1475 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001476{
1477 struct msgb *msg = gsm48_msgb_alloc();
1478 struct gsm48_hdr *gh;
1479 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001480 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001481
Harald Welte7ccf7782009-02-17 01:43:01 +00001482 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001483
1484 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001485
1486 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001487 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001488
1489 gh->proto_discr = GSM48_PDISC_CC;
1490 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001491
Harald Welte7584aea2009-02-11 11:44:12 +00001492 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001493 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001494 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001495 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001496 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1497 bcd_lv[0], bcd_lv+1);
1498 }
Harald Welte49f48b82009-02-17 15:29:33 +00001499 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001500 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001501 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001502 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1503 bcd_lv[0], bcd_lv+1);
1504 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001505
Harald Welte4b458152009-02-17 13:23:06 +00001506 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001507
1508 return gsm48_sendmsg(msg);
1509}
1510
Harald Welte4bc90a12008-12-27 16:32:52 +00001511static int gsm0408_rcv_cc(struct msgb *msg)
1512{
1513 struct gsm48_hdr *gh = msgb_l3(msg);
1514 u_int8_t msg_type = gh->msg_type & 0xbf;
1515 struct gsm_call *call = &msg->lchan->call;
1516 int rc = 0;
1517
1518 switch (msg_type) {
1519 case GSM48_MT_CC_CALL_CONF:
1520 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001521 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001522 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001523 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001524 break;
1525 case GSM48_MT_CC_RELEASE_COMPL:
1526 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001527 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001528 call->state = GSM_CSTATE_NULL;
1529 break;
1530 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001531 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001532 break;
1533 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001534 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001535 break;
1536 case GSM48_MT_CC_CONNECT_ACK:
1537 /* MO: Answer to CONNECT */
1538 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001539 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001540 break;
1541 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001542 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001543 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001544 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001545 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1546 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001547 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001548 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001549 break;
1550 case GSM48_MT_CC_STATUS_ENQ:
1551 rc = gsm48_cc_rx_status_enq(msg);
1552 break;
1553 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001554 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001555 break;
1556 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001557 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001558 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001559 break;
1560 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001561 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001562 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1563 break;
Harald Welte4a543e82009-02-28 13:17:55 +00001564 case GSM48_MT_CC_HOLD:
1565 DEBUGP(DCC, "-> HOLD (rejecting)\n");
1566 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1567 GSM48_MT_CC_HOLD_REJ);
1568 break;
1569 case GSM48_MT_CC_RETR:
1570 DEBUGP(DCC, "-> RETR (rejecting)\n");
1571 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1572 GSM48_MT_CC_RETR_REJ);
1573 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00001574 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001575 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001576 msg_type);
1577 break;
1578 }
1579
1580 return rc;
1581}
1582
Harald Welte52b1f982008-12-23 20:25:15 +00001583/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1584int gsm0408_rcvmsg(struct msgb *msg)
1585{
1586 struct gsm48_hdr *gh = msgb_l3(msg);
1587 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001588 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001589
1590 switch (pdisc) {
1591 case GSM48_PDISC_CC:
1592 rc = gsm0408_rcv_cc(msg);
1593 break;
1594 case GSM48_PDISC_MM:
1595 rc = gsm0408_rcv_mm(msg);
1596 break;
1597 case GSM48_PDISC_RR:
1598 rc = gsm0408_rcv_rr(msg);
1599 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001600 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001601 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001602 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001603 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001604 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001605 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1606 pdisc);
1607 break;
1608 default:
1609 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1610 pdisc);
1611 break;
1612 }
1613
1614 return rc;
1615}
Harald Welte8470bf22008-12-25 23:28:35 +00001616
1617enum chreq_type {
1618 CHREQ_T_EMERG_CALL,
1619 CHREQ_T_CALL_REEST_TCH_F,
1620 CHREQ_T_CALL_REEST_TCH_H,
1621 CHREQ_T_CALL_REEST_TCH_H_DBL,
1622 CHREQ_T_SDCCH,
1623 CHREQ_T_TCH_F,
1624 CHREQ_T_VOICE_CALL_TCH_H,
1625 CHREQ_T_DATA_CALL_TCH_H,
1626 CHREQ_T_LOCATION_UPD,
1627 CHREQ_T_PAG_R_ANY,
1628 CHREQ_T_PAG_R_TCH_F,
1629 CHREQ_T_PAG_R_TCH_FH,
1630};
1631
1632/* Section 9.1.8 / Table 9.9 */
1633struct chreq {
1634 u_int8_t val;
1635 u_int8_t mask;
1636 enum chreq_type type;
1637};
1638
1639/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1640static const struct chreq chreq_type_neci1[] = {
1641 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1642 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1643 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1644 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1645 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1646 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1647 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1648 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1649 { 0x10, 0xf0, CHREQ_T_SDCCH },
1650 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1651 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1652 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1653};
1654
1655/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1656static const struct chreq chreq_type_neci0[] = {
1657 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1658 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1659 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1660 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1661 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1662 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1663 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1664 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1665};
1666
1667static const enum gsm_chan_t ctype_by_chreq[] = {
1668 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1669 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1670 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1671 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1672 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1673 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1674 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1675 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1676 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1677 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1678 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1679 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1680};
1681
Harald Weltee14a57c2008-12-29 04:08:28 +00001682static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1683 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1684 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1685 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1686 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1687 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1688 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1689 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1690 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1691 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1692 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1693 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1694 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1695};
1696
Harald Welte8470bf22008-12-25 23:28:35 +00001697enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1698{
1699 int i;
1700 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1701
1702 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1703 const struct chreq *chr = &chreq_type_neci0[i];
1704 if ((ra & chr->mask) == chr->val)
1705 return ctype_by_chreq[chr->type];
1706 }
1707 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1708 return GSM_LCHAN_SDCCH;
1709}
Harald Weltee14a57c2008-12-29 04:08:28 +00001710
1711enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1712{
1713 int i;
1714 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1715
1716 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1717 const struct chreq *chr = &chreq_type_neci0[i];
1718 if ((ra & chr->mask) == chr->val)
1719 return reason_by_chreq[chr->type];
1720 }
1721 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1722 return GSM_CHREQ_REASON_OTHER;
1723}