blob: 6f729e370a32a0a0824a816713084adb8154c20f [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 Welte2cf161b2009-06-20 22:36:41 +020047#include <openbsc/talloc.h>
Harald Welteaa0b29c2009-07-23 18:56:43 +020048#include <openbsc/transaction.h>
Harald Welte52b1f982008-12-23 20:25:15 +000049
Harald Welte8470bf22008-12-25 23:28:35 +000050#define GSM48_ALLOC_SIZE 1024
51#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000052
Harald Welte0c389302009-06-10 12:08:54 +080053#define GSM_MAX_FACILITY 128
54#define GSM_MAX_SSVERSION 128
55#define GSM_MAX_USERUSER 128
56
Harald Welte2cf161b2009-06-20 22:36:41 +020057static void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020058
Harald Welte09e38af2009-02-16 22:52:23 +000059static const struct tlv_definition rsl_att_tlvdef = {
60 .def = {
61 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
62 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
63 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
64 [GSM48_IE_UTC] = { TLV_TYPE_TV },
65 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
66 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
67
68 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
69 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
70 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
71 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
72 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
73 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
74 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080075 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000076 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
77 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080078 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
79 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000080 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
82 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
84 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
85 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
86 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
87 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
88 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
89 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
90 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
91 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
92 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
93 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080094 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
95 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000096 /* FIXME: more elements */
97 },
98};
Harald Weltecf5b3592009-05-01 18:28:42 +000099
100static const char *rr_cause_names[] = {
101 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
102 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
103 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
104 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
105 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
106 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
107 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
108 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
109 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
110 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
111 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
112 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
113 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
114 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
115 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
116 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
117 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
118};
119
Harald Welte4bfdfe72009-06-10 23:11:52 +0800120static const char *cc_state_names[] = {
121 "NULL",
122 "INITIATED",
123 "illegal state 2",
124 "MO_CALL_PROC",
125 "CALL_DELIVERED",
126 "illegal state 5",
127 "CALL_PRESENT",
128 "CALL_RECEIVED",
129 "CONNECT_REQUEST",
130 "MO_TERM_CALL_CONF",
131 "ACTIVE",
132 "DISCONNECT_REQ",
133 "DISCONNECT_IND",
134 "illegal state 13",
135 "illegal state 14",
136 "illegal state 15",
137 "illegal state 16",
138 "illegal state 17",
139 "illegal state 18",
140 "RELEASE_REQ",
141 "illegal state 20",
142 "illegal state 21",
143 "illegal state 22",
144 "illegal state 23",
145 "illegal state 24",
146 "illegal state 25",
147 "MO_ORIG_MODIFY",
148 "MO_TERM_MODIFY",
149 "CONNECT_IND",
150 "illegal state 29",
151 "illegal state 30",
152 "illegal state 31",
153};
154
155static const char *cc_msg_names[] = {
156 "unknown 0x00",
157 "ALERTING",
158 "CALL_PROC",
159 "PROGRESS",
160 "ESTAB",
161 "SETUP",
162 "ESTAB_CONF",
163 "CONNECT",
164 "CALL_CONF",
165 "START_CC",
166 "unknown 0x0a",
167 "RECALL",
168 "unknown 0x0c",
169 "unknown 0x0d",
170 "EMERG_SETUP",
171 "CONNECT_ACK",
172 "USER_INFO",
173 "unknown 0x11",
174 "unknown 0x12",
175 "MODIFY_REJECT",
176 "unknown 0x14",
177 "unknown 0x15",
178 "unknown 0x16",
179 "MODIFY",
180 "HOLD",
181 "HOLD_ACK",
182 "HOLD_REJ",
183 "unknown 0x1b",
184 "RETR",
185 "RETR_ACK",
186 "RETR_REJ",
187 "MODIFY_COMPL",
188 "unknown 0x20",
189 "unknown 0x21",
190 "unknown 0x22",
191 "unknown 0x23",
192 "unknown 0x24",
193 "DISCONNECT",
194 "unknown 0x26",
195 "unknown 0x27",
196 "unknown 0x28",
197 "unknown 0x29",
198 "RELEASE_COMPL",
199 "unknown 0x2b",
200 "unknown 0x2c",
201 "RELEASE",
202 "unknown 0x2e",
203 "unknown 0x2f",
204 "unknown 0x30",
205 "STOP_DTMF",
206 "STOP_DTMF_ACK",
207 "unknown 0x33",
208 "STATUS_ENQ",
209 "START_DTMF",
210 "START_DTMF_ACK",
211 "START_DTMF_REJ",
212 "unknown 0x38",
213 "CONG_CTRL",
214 "FACILITY",
215 "unknown 0x3b",
216 "STATUS",
217 "unknown 0x3c",
218 "NOTIFY",
219 "unknown 0x3f",
220};
221
Harald Weltecf5b3592009-05-01 18:28:42 +0000222static char strbuf[64];
223
224static const char *rr_cause_name(u_int8_t cause)
225{
226 if (cause < ARRAY_SIZE(rr_cause_names) &&
227 rr_cause_names[cause])
228 return rr_cause_names[cause];
229
230 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
231 return strbuf;
232}
233
Harald Weltef7c43522009-06-09 20:24:21 +0000234static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
235 int len)
236{
237 memset(rep, 0, sizeof(*rep));
238
239 if (data[0] & 0x80)
240 rep->flags |= MEAS_REP_F_BA1;
241 if (data[0] & 0x40)
242 rep->flags |= MEAS_REP_F_DTX;
243 if (data[1] & 0x40)
244 rep->flags |= MEAS_REP_F_VALID;
245
246 rep->rxlev_full = data[0] & 0x3f;
247 rep->rxlev_sub = data[1] & 0x3f;
248 rep->rxqual_full = (data[3] >> 4) & 0x7;
249 rep->rxqual_sub = (data[3] >> 1) & 0x7;
250 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
251 if (rep->num_cell < 1)
252 return;
253
254 /* an encoding nightmare in perfection */
255
256 rep->cell[0].rxlev = data[4] & 0x3f;
257 rep->cell[0].bcch_freq = data[5] >> 2;
258 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
259 if (rep->num_cell < 2)
260 return;
261
262 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
263 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
264 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
265 if (rep->num_cell < 3)
266 return;
267
268 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
269 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
270 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
271 if (rep->num_cell < 4)
272 return;
273
274 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
275 rep->cell[3].bcch_freq = data[11] & 0x1f;
276 rep->cell[3].bsic = data[12] >> 2;
277 if (rep->num_cell < 5)
278 return;
279
280 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
281 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
282 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
283 if (rep->num_cell < 6)
284 return;
285
286 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
287 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
288 rep->cell[5].bsic = data[16] & 0x3f;
289}
290
Holger Freytherd51524f2009-06-09 08:27:07 +0000291int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000292static int gsm48_tx_simple(struct gsm_lchan *lchan,
293 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000294static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000295
Harald Welte52b1f982008-12-23 20:25:15 +0000296struct gsm_lai {
297 u_int16_t mcc;
298 u_int16_t mnc;
299 u_int16_t lac;
300};
301
Holger Freyther89824fc2008-12-30 16:18:18 +0000302static int authorize_everonye = 0;
303void gsm0408_allow_everyone(int everyone)
304{
305 printf("Allowing everyone?\n");
306 authorize_everonye = everyone;
307}
308
Holger Freythere97f7fb2008-12-31 18:52:11 +0000309static int reject_cause = 0;
310void gsm0408_set_reject_cause(int cause)
311{
312 reject_cause = cause;
313}
314
Harald Welte4bfdfe72009-06-10 23:11:52 +0800315static u_int32_t new_callref = 0x80000001;
316
Holger Freyther73487a22008-12-31 18:53:57 +0000317static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
318 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000319{
320 if (!subscriber)
321 return 0;
322
Holger Freyther73487a22008-12-31 18:53:57 +0000323 /*
324 * Do not send accept yet as more information should arrive. Some
325 * phones will not send us the information and we will have to check
326 * what we want to do with that.
327 */
328 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
329 return 0;
330
Holger Freyther89824fc2008-12-30 16:18:18 +0000331 if (authorize_everonye)
332 return 1;
333
334 return subscriber->authorized;
335}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000336
Holger Freyther73487a22008-12-31 18:53:57 +0000337static void release_loc_updating_req(struct gsm_lchan *lchan)
338{
Harald Welte179f0642008-12-31 23:59:18 +0000339 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000340 return;
341
Harald Welteff117a82009-05-23 05:22:08 +0000342 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200343 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000344 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000345 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000346}
347
348static void allocate_loc_updating_req(struct gsm_lchan *lchan)
349{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000350 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000351 release_loc_updating_req(lchan);
352
Harald Welte2cf161b2009-06-20 22:36:41 +0200353 if (!tall_locop_ctx)
354 tall_locop_ctx = talloc_named_const(tall_bsc_ctx, 1,
355 "loc_updating_oper");
Harald Welte470ec292009-06-26 20:25:23 +0200356 lchan->loc_operation = talloc_zero(tall_locop_ctx,
357 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000358}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000359
Holger Freytherd51524f2009-06-09 08:27:07 +0000360static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
361{
362 u_int32_t tmsi;
363
364 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
365 db_subscriber_alloc_tmsi(lchan->subscr);
366 subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
367 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
368 release_loc_updating_req(lchan);
369 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
370 }
371
372 return 0;
373}
374
Holger Freyther7c19f742009-06-06 13:54:35 +0000375static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
376 void *handler_data, void *signal_data)
377{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800378 struct gsm_trans *trans, *temp;
379
Holger Freyther7c19f742009-06-06 13:54:35 +0000380 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
381 return 0;
382
383 /*
384 * Cancel any outstanding location updating request
385 * operation taking place on the lchan.
386 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200387 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200388 if (!lchan)
389 return 0;
390
Holger Freyther7c19f742009-06-06 13:54:35 +0000391 release_loc_updating_req(lchan);
392
Harald Welte4bfdfe72009-06-10 23:11:52 +0800393 /* Free all transactions that are associated with the released lchan */
Harald Welteaa0b29c2009-07-23 18:56:43 +0200394 /* FIXME: this is not neccessarily the right thing to do, we should
395 * only set trans->lchan to NULL and wait for another lchan to be
396 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800397 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
398 if (trans->lchan == lchan)
Harald Welteaa0b29c2009-07-23 18:56:43 +0200399 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800400 }
401
Holger Freyther7c19f742009-06-06 13:54:35 +0000402 return 0;
403}
404
405/*
406 * This will be ran by the linker when loading the DSO. We use it to
407 * do system initialization, e.g. registration of signal handlers.
408 */
409static __attribute__((constructor)) void on_dso_load_0408(void)
410{
411 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
412}
413
Harald Welte52b1f982008-12-23 20:25:15 +0000414static void to_bcd(u_int8_t *bcd, u_int16_t val)
415{
Harald Welte4b634542008-12-27 01:55:51 +0000416 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000417 val = val / 10;
418 bcd[1] = val % 10;
419 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000420 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000421 val = val / 10;
422}
423
Holger Freyther17746612008-12-28 16:32:44 +0000424void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000425 u_int16_t mnc, u_int16_t lac)
426{
427 u_int8_t bcd[3];
428
429 to_bcd(bcd, mcc);
430 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
431 lai48->digits[1] = bcd[2];
432
433 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000434 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
435#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000436 lai48->digits[1] |= bcd[2] << 4;
437 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000438#else
439 lai48->digits[1] |= 0xf << 4;
440 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
441#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000442
Harald Welte4b634542008-12-27 01:55:51 +0000443 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000444}
445
Harald Welte255539c2008-12-28 02:26:27 +0000446#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000447#define MID_TMSI_LEN (TMSI_LEN + 2)
448
Harald Welte255539c2008-12-28 02:26:27 +0000449int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000450{
Harald Welte65e74cc2008-12-29 01:55:35 +0000451 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000452
Harald Welte4b634542008-12-27 01:55:51 +0000453 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000454 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000455 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000456 *tptr = htonl(tmsi);
457
458 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000459}
460
Harald Welte09e38af2009-02-16 22:52:23 +0000461static const char bcd_num_digits[] = {
462 '0', '1', '2', '3', '4', '5', '6', '7',
463 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
464};
465
Harald Welte0c389302009-06-10 12:08:54 +0800466/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
467int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
468 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000469{
470 u_int8_t in_len = bcd_lv[0];
471 int i;
472
Harald Welte0c389302009-06-10 12:08:54 +0800473 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000474 /* lower nibble */
475 output_len--;
476 if (output_len <= 1)
477 break;
478 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
479
480 /* higher nibble */
481 output_len--;
482 if (output_len <= 1)
483 break;
484 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
485 }
486 if (output_len >= 1)
487 *output++ = '\0';
488
Harald Welte0c389302009-06-10 12:08:54 +0800489 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000490}
491
492/* convert a single ASCII character to call-control BCD */
493static int asc_to_bcd(const char asc)
494{
495 int i;
496
497 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
498 if (bcd_num_digits[i] == asc)
499 return i;
500 }
501 return -EINVAL;
502}
503
Harald Welte0c389302009-06-10 12:08:54 +0800504/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000505int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800506 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000507{
508 int in_len = strlen(input);
509 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800510 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000511
512 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800513 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000514 if (in_len % 2)
515 bcd_lv[0]++;
516
Harald Welte0c389302009-06-10 12:08:54 +0800517 if (bcd_lv[0] > max_len)
518 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000519
520 for (i = 0; i < in_len; i++) {
521 int rc = asc_to_bcd(input[i]);
522 if (rc < 0)
523 return rc;
524 if (i % 2 == 0)
525 *bcd_cur = rc;
526 else
527 *bcd_cur++ |= (rc << 4);
528 }
529 /* append padding nibble in case of odd length */
530 if (i % 2)
531 *bcd_cur++ |= 0xf0;
532
533 /* return how many bytes we used */
534 return (bcd_cur - bcd_lv);
535}
536
Harald Welte0c389302009-06-10 12:08:54 +0800537/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800538static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800539 const u_int8_t *lv)
540{
541 u_int8_t in_len = lv[0];
542 int i, s;
543
544 if (in_len < 1)
545 return -EINVAL;
546
Harald Welte4bfdfe72009-06-10 23:11:52 +0800547 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800548
549 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800550 bcap->transfer = lv[1] & 0x07;
551 bcap->mode = (lv[1] & 0x08) >> 3;
552 bcap->coding = (lv[1] & 0x10) >> 4;
553 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800554
555 i = 1;
556 s = 0;
557 while(!(lv[i] & 0x80)) {
558 i++; /* octet 3a etc */
559 if (in_len < i)
560 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800561 bcap->speech_ver[s++] = lv[i] & 0x0f;
562 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800563 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800564 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800565 if (s == 7) /* maximum speech versions + end of list */
566 return 0;
567 }
568
569 return 0;
570}
571
572/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800573static int encode_bearer_cap(struct msgb *msg, int lv_only,
574 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800575{
576 u_int8_t lv[32 + 1];
577 int i, s;
578
Harald Welte4bfdfe72009-06-10 23:11:52 +0800579 lv[1] = bcap->transfer;
580 lv[1] |= bcap->mode << 3;
581 lv[1] |= bcap->coding << 4;
582 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800583
584 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800585 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800586 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800587 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800588 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800590 }
591 lv[i] |= 0x80; /* last IE of octet 3 etc */
592
593 lv[0] = i;
594 if (lv_only)
595 msgb_lv_put(msg, lv[0], lv+1);
596 else
597 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
598
599 return 0;
600}
601
602/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800603static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800604{
605 u_int8_t in_len = lv[0];
606
607 if (in_len < 1)
608 return -EINVAL;
609
610 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800611 ccap->dtmf = lv[1] & 0x01;
612 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800613
614 return 0;
615}
616
617/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800618static int decode_called(struct gsm_mncc_number *called,
619 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800620{
621 u_int8_t in_len = lv[0];
622
623 if (in_len < 1)
624 return -EINVAL;
625
626 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627 called->plan = lv[1] & 0x0f;
628 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800629
630 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800631 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800632
633 return 0;
634}
635
636/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637static int encode_called(struct msgb *msg,
638 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800639{
640 u_int8_t lv[18];
641 int ret;
642
643 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 lv[1] = called->plan;
645 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800646
647 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800648 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800649 if (ret < 0)
650 return ret;
651
652 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
653
654 return 0;
655}
656
657/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658static int encode_callerid(struct msgb *msg, int ie,
659 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800660{
661 u_int8_t lv[13];
662 int h_len = 1;
663 int ret;
664
665 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800666 lv[1] = callerid->plan;
667 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800668
Harald Welte4bfdfe72009-06-10 23:11:52 +0800669 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800670 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800671 lv[2] = callerid->screen;
672 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800673 lv[2] |= 0x80;
674 h_len++;
675 } else
676 lv[1] |= 0x80;
677
678 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800679 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800680 if (ret < 0)
681 return ret;
682
683 msgb_tlv_put(msg, ie, lv[0], lv+1);
684
685 return 0;
686}
687
688/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800689static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800690 const u_int8_t *lv)
691{
692 u_int8_t in_len = lv[0];
693 int i;
694
695 if (in_len < 2)
696 return -EINVAL;
697
Harald Welte4bfdfe72009-06-10 23:11:52 +0800698 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800699
700 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800701 cause->location = lv[1] & 0x0f;
702 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800703
704 i = 1;
705 if (!(lv[i] & 0x80)) {
706 i++; /* octet 3a */
707 if (in_len < i+1)
708 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800709 cause->rec = 1;
710 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800711
712 }
713 i++;
714
715 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800716 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800717 i++;
718
719 if (in_len < i) /* no diag */
720 return 0;
721
722 if (in_len - (i-1) > 32) /* maximum 32 octets */
723 return 0;
724
725 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800726 memcpy(cause->diag, lv + i, in_len - (i-1));
727 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800728
729 return 0;
730}
731
732/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800733static int encode_cause(struct msgb *msg, int lv_only,
734 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800735{
736 u_int8_t lv[32+4];
737 int i;
738
Harald Welte4bfdfe72009-06-10 23:11:52 +0800739 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800740 return -EINVAL;
741
742 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800743 lv[1] = cause->location;
744 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800745
746 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800747 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800748 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800749 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800750 }
751 lv[i] |= 0x80; /* end of octet 3 */
752
753 /* octet 4 */
754 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800755 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800756
757 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800758 if (cause->diag_len) {
759 memcpy(lv + i, cause->diag, cause->diag_len);
760 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800761 }
762
763 lv[0] = i;
764 if (lv_only)
765 msgb_lv_put(msg, lv[0], lv+1);
766 else
767 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
768
769 return 0;
770}
771
772/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800773static int encode_calling(struct msgb *msg,
774 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800775{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800776 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800777}
778
779/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800780static int encode_connected(struct msgb *msg,
781 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800782{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800783 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800784}
785
786/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800787static int encode_redirecting(struct msgb *msg,
788 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800789{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800790 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800791}
792
793/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800794static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800795 const u_int8_t *lv)
796{
797 u_int8_t in_len = lv[0];
798
799 if (in_len < 1)
800 return -EINVAL;
801
Harald Welte4bfdfe72009-06-10 23:11:52 +0800802 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800803 return -EINVAL;
804
Harald Welte4bfdfe72009-06-10 23:11:52 +0800805 memcpy(facility->info, lv+1, in_len);
806 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800807
808 return 0;
809}
810
811/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800812static int encode_facility(struct msgb *msg, int lv_only,
813 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800814{
815 u_int8_t lv[GSM_MAX_FACILITY + 1];
816
Harald Welte4bfdfe72009-06-10 23:11:52 +0800817 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800818 return -EINVAL;
819
Harald Welte4bfdfe72009-06-10 23:11:52 +0800820 memcpy(lv+1, facility->info, facility->len);
821 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800822 if (lv_only)
823 msgb_lv_put(msg, lv[0], lv+1);
824 else
825 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
826
827 return 0;
828}
829
830/* decode 'notify' */
831static int decode_notify(int *notify, const u_int8_t *v)
832{
833 *notify = v[0] & 0x7f;
834
835 return 0;
836}
837
838/* encode 'notify' */
839static int encode_notify(struct msgb *msg, int notify)
840{
841 msgb_v_put(msg, notify | 0x80);
842
843 return 0;
844}
845
846/* encode 'signal' */
847static int encode_signal(struct msgb *msg, int signal)
848{
849 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
850
851 return 0;
852}
853
854/* decode 'keypad' */
855static int decode_keypad(int *keypad, const u_int8_t *lv)
856{
857 u_int8_t in_len = lv[0];
858
859 if (in_len < 1)
860 return -EINVAL;
861
862 *keypad = lv[1] & 0x7f;
863
864 return 0;
865}
866
867/* encode 'keypad' */
868static int encode_keypad(struct msgb *msg, int keypad)
869{
870 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
871
872 return 0;
873}
874
875/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800876static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800877 const u_int8_t *lv)
878{
879 u_int8_t in_len = lv[0];
880
881 if (in_len < 2)
882 return -EINVAL;
883
Harald Welte4bfdfe72009-06-10 23:11:52 +0800884 progress->coding = (lv[1] & 0x60) >> 5;
885 progress->location = lv[1] & 0x0f;
886 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800887
888 return 0;
889}
890
891/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800892static int encode_progress(struct msgb *msg, int lv_only,
893 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800894{
895 u_int8_t lv[3];
896
897 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800898 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
899 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800900 if (lv_only)
901 msgb_lv_put(msg, lv[0], lv+1);
902 else
903 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
904
905 return 0;
906}
907
908/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800909static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800910 const u_int8_t *lv)
911{
912 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800913 char *info = uu->info;
914 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800915 int i;
916
917 if (in_len < 1)
918 return -EINVAL;
919
Harald Welte4bfdfe72009-06-10 23:11:52 +0800920 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800921
922 for (i = 2; i <= in_len; i++) {
923 info_len--;
924 if (info_len <= 1)
925 break;
926 *info++ = lv[i];
927 }
928 if (info_len >= 1)
929 *info++ = '\0';
930
931 return 0;
932}
933
934/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800935static int encode_useruser(struct msgb *msg, int lv_only,
936 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800937{
938 u_int8_t lv[GSM_MAX_USERUSER + 2];
939
Harald Welte4bfdfe72009-06-10 23:11:52 +0800940 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800941 return -EINVAL;
942
Harald Welte4bfdfe72009-06-10 23:11:52 +0800943 lv[0] = 1 + strlen(uu->info);
944 lv[1] = uu->proto;
945 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800946 if (lv_only)
947 msgb_lv_put(msg, lv[0], lv+1);
948 else
949 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
950
951 return 0;
952}
953
954/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800955static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800956 const u_int8_t *lv)
957{
958 u_int8_t in_len = lv[0];
959
Harald Welte4bfdfe72009-06-10 23:11:52 +0800960 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800961 return -EINVAL;
962
Harald Welte4bfdfe72009-06-10 23:11:52 +0800963 memcpy(ssv->info, lv + 1, in_len);
964 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800965
966 return 0;
967}
968
969/* encode 'more data' */
970static int encode_more(struct msgb *msg)
971{
972 u_int8_t *ie;
973
974 ie = msgb_put(msg, 1);
975 ie[0] = GSM48_IE_MORE_DATA;
976
977 return 0;
978}
979
Holger Freyther819dd202009-01-04 03:52:50 +0000980struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000981{
Harald Welte966636f2009-06-26 19:39:35 +0200982 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM,
983 "GSM 04.08");
Harald Welte8470bf22008-12-25 23:28:35 +0000984}
985
Holger Freyther3e2c3232009-01-04 03:55:31 +0000986int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000987{
Harald Welte65e74cc2008-12-29 01:55:35 +0000988 if (msg->lchan) {
Harald Welte4bfdfe72009-06-10 23:11:52 +0800989 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000990 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000991
Harald Welte4bfdfe72009-06-10 23:11:52 +0800992 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
993 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
994 "Sending '%s' to MS.\n", msg->trx->bts->nr,
995 msg->trx->nr, msg->lchan->ts->nr,
996 gh->proto_discr & 0xf0,
997 cc_msg_names[gh->msg_type & 0x3f]);
998 else
999 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
1000 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
1001 msg->trx->nr, msg->lchan->ts->nr,
1002 gh->proto_discr, gh->msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +00001003 }
1004
Harald Welte4b634542008-12-27 01:55:51 +00001005 msg->l3h = msg->data;
1006
Harald Welte8470bf22008-12-25 23:28:35 +00001007 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +00001008}
1009
Holger Freyther429e7762008-12-30 13:28:30 +00001010/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +00001011int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +00001012{
Harald Welte8470bf22008-12-25 23:28:35 +00001013 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +00001014 struct gsm48_hdr *gh;
1015
Harald Welte8470bf22008-12-25 23:28:35 +00001016 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +00001017
1018 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1019 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +00001020 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +00001021 gh->data[0] = cause;
1022
Harald Weltedb253af2008-12-30 17:56:55 +00001023 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
1024
Harald Welte65e74cc2008-12-29 01:55:35 +00001025 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001026}
1027
1028/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +00001029int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +00001030{
Harald Welte8470bf22008-12-25 23:28:35 +00001031 struct gsm_bts *bts = lchan->ts->trx->bts;
1032 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +00001033 struct gsm48_hdr *gh;
1034 struct gsm48_loc_area_id *lai;
1035 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +00001036 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001037
Harald Welte8470bf22008-12-25 23:28:35 +00001038 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +00001039
1040 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1041 gh->proto_discr = GSM48_PDISC_MM;
1042 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
1043
1044 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +00001045 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +00001046 bts->network->network_code, bts->location_area_code);
1047
1048 mid = msgb_put(msg, MID_TMSI_LEN);
1049 generate_mid_from_tmsi(mid, tmsi);
1050
1051 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
1052
Harald Weltedb253af2008-12-30 17:56:55 +00001053 ret = gsm48_sendmsg(msg);
1054
Harald Weltedb253af2008-12-30 17:56:55 +00001055 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +00001056
Holger Freyther07cc8d82008-12-29 06:23:46 +00001057 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001058}
1059
Harald Weltefc977a82008-12-27 10:19:37 +00001060static char bcd2char(u_int8_t bcd)
1061{
1062 if (bcd < 0xa)
1063 return '0' + bcd;
1064 else
1065 return 'A' + (bcd - 0xa);
1066}
1067
Harald Weltebf5e8df2009-02-03 12:59:45 +00001068/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +00001069static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
1070{
1071 int i;
1072 u_int8_t mi_type;
1073 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +00001074 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +00001075
1076 mi_type = mi[0] & GSM_MI_TYPE_MASK;
1077
1078 switch (mi_type) {
1079 case GSM_MI_TYPE_NONE:
1080 break;
1081 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +00001082 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
1083 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
1084 memcpy(&tmsi, &mi[1], 4);
1085 tmsi = ntohl(tmsi);
1086 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +00001087 }
1088 break;
1089 case GSM_MI_TYPE_IMSI:
1090 case GSM_MI_TYPE_IMEI:
1091 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +00001092 *str_cur++ = bcd2char(mi[0] >> 4);
1093
1094 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +00001095 if (str_cur + 2 >= string + str_len)
1096 return str_cur - string;
1097 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +00001098 /* skip last nibble in last input byte when GSM_EVEN */
1099 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
1100 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +00001101 }
1102 break;
1103 default:
1104 break;
1105 }
Harald Weltefc977a82008-12-27 10:19:37 +00001106 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +00001107
Harald Weltefc977a82008-12-27 10:19:37 +00001108 return str_cur - string;
1109}
1110
Harald Weltebf5e8df2009-02-03 12:59:45 +00001111/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001112static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
1113{
1114 struct msgb *msg = gsm48_msgb_alloc();
1115 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +00001116
Harald Welte231ad4f2008-12-27 11:15:38 +00001117 msg->lchan = lchan;
1118
1119 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1120 gh->proto_discr = GSM48_PDISC_MM;
1121 gh->msg_type = GSM48_MT_MM_ID_REQ;
1122 gh->data[0] = id_type;
1123
Harald Welte65e74cc2008-12-29 01:55:35 +00001124 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001125}
1126
1127#define MI_SIZE 32
1128
Harald Weltebf5e8df2009-02-03 12:59:45 +00001129/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +00001130static int mm_rx_id_resp(struct msgb *msg)
1131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +00001133 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +00001134 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
1135 char mi_string[MI_SIZE];
1136
1137 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +00001138 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +00001139 mi_type, mi_string);
1140
Harald Welte75a983f2008-12-27 21:34:06 +00001141 switch (mi_type) {
1142 case GSM_MI_TYPE_IMSI:
1143 if (!lchan->subscr)
1144 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001145 if (lchan->loc_operation)
1146 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001147 break;
1148 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +00001149 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +00001150 /* update subscribe <-> IMEI mapping */
1151 if (lchan->subscr)
1152 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001153 if (lchan->loc_operation)
1154 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001155 break;
1156 }
Holger Freyther73487a22008-12-31 18:53:57 +00001157
1158 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +00001159 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001160}
1161
Harald Welte255539c2008-12-28 02:26:27 +00001162
1163static void loc_upd_rej_cb(void *data)
1164{
1165 struct gsm_lchan *lchan = data;
1166
Holger Freyther73487a22008-12-31 18:53:57 +00001167 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +00001168 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001169 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001170}
1171
Holger Freytherb7193e42008-12-29 17:44:08 +00001172static void schedule_reject(struct gsm_lchan *lchan)
1173{
Holger Freyther73487a22008-12-31 18:53:57 +00001174 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1175 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001176 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001177}
1178
Harald Welte2a139372009-02-22 21:14:55 +00001179static const char *lupd_name(u_int8_t type)
1180{
1181 switch (type) {
1182 case GSM48_LUPD_NORMAL:
1183 return "NORMAL";
1184 case GSM48_LUPD_PERIODIC:
1185 return "PEROIDOC";
1186 case GSM48_LUPD_IMSI_ATT:
1187 return "IMSI ATTACH";
1188 default:
1189 return "UNKNOWN";
1190 }
1191}
1192
Harald Welte231ad4f2008-12-27 11:15:38 +00001193#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +00001194/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001195static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001196{
Harald Welte8470bf22008-12-25 23:28:35 +00001197 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001198 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001199 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001200 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +00001201 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +00001202 char mi_string[MI_SIZE];
1203 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001204
Harald Welte8470bf22008-12-25 23:28:35 +00001205 lu = (struct gsm48_loc_upd_req *) gh->data;
1206
1207 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001208
Harald Weltefc977a82008-12-27 10:19:37 +00001209 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
1210
Harald Weltea0368542009-06-27 02:58:43 +02001211 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001212 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001213
Holger Freythereaf04692009-06-06 13:54:44 +00001214 /*
1215 * Pseudo Spoof detection: Just drop a second/concurrent
1216 * location updating request.
1217 */
1218 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001219 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001220 lchan->loc_operation);
1221 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1222 return 0;
1223 }
1224
Holger Freyther73487a22008-12-31 18:53:57 +00001225 allocate_loc_updating_req(lchan);
1226
Harald Welte52b1f982008-12-23 20:25:15 +00001227 switch (mi_type) {
1228 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001229 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001230 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001231 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001232 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001233
Harald Welte52b1f982008-12-23 20:25:15 +00001234 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +00001235 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +00001236 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001237 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001238 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001239 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001240 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001241 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001242
Harald Welte52b1f982008-12-23 20:25:15 +00001243 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +00001244 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001245 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001246 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001247 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001248 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001249 }
1250 break;
1251 case GSM_MI_TYPE_IMEI:
1252 case GSM_MI_TYPE_IMEISV:
1253 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001254 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001255 break;
1256 default:
Harald Weltea0368542009-06-27 02:58:43 +02001257 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001258 break;
1259 }
1260
Harald Welte24516ea2009-07-04 10:18:00 +02001261 /* schedule the reject timer */
1262 schedule_reject(lchan);
1263
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001265 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001266 /* FIXME: request id? close channel? */
1267 return -EINVAL;
1268 }
1269
Harald Welte255539c2008-12-28 02:26:27 +00001270 lchan->subscr = subscr;
1271
Harald Welte24516ea2009-07-04 10:18:00 +02001272 /* check if we can let the subscriber into our network immediately
1273 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001274 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001275}
1276
Harald Welte7584aea2009-02-11 11:44:12 +00001277/* 9.1.5 Channel mode modify */
1278int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1279{
1280 struct msgb *msg = gsm48_msgb_alloc();
1281 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1282 struct gsm48_chan_mode_modify *cmm =
1283 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001284 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001285
Harald Welte4a543e82009-02-28 13:17:55 +00001286 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001287
Harald Welte45b407a2009-05-23 15:51:12 +00001288 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001289 msg->lchan = lchan;
1290 gh->proto_discr = GSM48_PDISC_RR;
1291 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1292
1293 /* fill the channel information element, this code
1294 * should probably be shared with rsl_rx_chan_rqd() */
1295 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001296 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001297 cmm->chan_desc.h0.h = 0;
1298 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1299 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1300 cmm->mode = mode;
1301
1302 return gsm48_sendmsg(msg);
1303}
1304
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305#if 0
1306static u_int8_t to_bcd8(u_int8_t val)
1307{
1308 return ((val / 10) << 4) | (val % 10);
1309}
1310#endif
1311
Harald Weltedb253af2008-12-30 17:56:55 +00001312/* Section 9.2.15a */
1313int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1314{
1315 struct msgb *msg = gsm48_msgb_alloc();
1316 struct gsm48_hdr *gh;
1317 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001318 u_int8_t *ptr8;
1319 u_int16_t *ptr16;
1320 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +00001321 int i;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322#if 0
1323 time_t cur_t;
1324 struct tm* cur_time;
1325 int tz15min;
1326#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001327
1328 msg->lchan = lchan;
1329
1330 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1331 gh->proto_discr = GSM48_PDISC_MM;
1332 gh->msg_type = GSM48_MT_MM_INFO;
1333
1334 if (net->name_long) {
1335 name_len = strlen(net->name_long);
1336 /* 10.5.3.5a */
1337 ptr8 = msgb_put(msg, 3);
1338 ptr8[0] = GSM48_IE_NAME_LONG;
1339 ptr8[1] = name_len*2 +1;
1340 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1341
1342 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1343 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001344 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001345
1346 /* FIXME: Use Cell Broadcast, not UCS-2, since
1347 * UCS-2 is only supported by later revisions of the spec */
1348 }
1349
1350 if (net->name_short) {
1351 name_len = strlen(net->name_short);
1352 /* 10.5.3.5a */
1353 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001354 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001355 ptr8[1] = name_len*2 + 1;
1356 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1357
Harald Weltee872cb12009-01-01 00:33:37 +00001358 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001359 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001360 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001361 }
1362
1363#if 0
1364 /* Section 10.5.3.9 */
1365 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001367 ptr8 = msgb_put(msg, 8);
1368 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1369 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1370 ptr8[2] = to_bcd8(cur_time->tm_mon);
1371 ptr8[3] = to_bcd8(cur_time->tm_mday);
1372 ptr8[4] = to_bcd8(cur_time->tm_hour);
1373 ptr8[5] = to_bcd8(cur_time->tm_min);
1374 ptr8[6] = to_bcd8(cur_time->tm_sec);
1375 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1376 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001378 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001380#endif
1381
1382 return gsm48_sendmsg(msg);
1383}
1384
Harald Welte4b634542008-12-27 01:55:51 +00001385static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1386{
Harald Welte4b634542008-12-27 01:55:51 +00001387 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001388 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001389}
Harald Welteba4cf162009-01-10 01:49:35 +00001390
1391/* 9.2.6 CM service reject */
1392static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1393 enum gsm48_reject_value value)
1394{
1395 struct msgb *msg = gsm48_msgb_alloc();
1396 struct gsm48_hdr *gh;
1397
1398 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1399
1400 msg->lchan = lchan;
1401 use_lchan(lchan);
1402
1403 gh->proto_discr = GSM48_PDISC_MM;
1404 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1405 gh->data[0] = value;
1406 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1407
1408 return gsm48_sendmsg(msg);
1409}
1410
Harald Welte4ed0e922009-01-10 03:17:30 +00001411
1412/*
1413 * Handle CM Service Requests
1414 * a) Verify that the packet is long enough to contain the information
1415 * we require otherwsie reject with INCORRECT_MESSAGE
1416 * b) Try to parse the TMSI. If we do not have one reject
1417 * c) Check that we know the subscriber with the TMSI otherwise reject
1418 * with a HLR cause
1419 * d) Set the subscriber on the gsm_lchan and accept
1420 */
Harald Welte4b634542008-12-27 01:55:51 +00001421static int gsm48_rx_mm_serv_req(struct msgb *msg)
1422{
Harald Welteba4cf162009-01-10 01:49:35 +00001423 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +00001424 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001425
Harald Welteba4cf162009-01-10 01:49:35 +00001426 struct gsm_subscriber *subscr;
1427 struct gsm48_hdr *gh = msgb_l3(msg);
1428 struct gsm48_service_request *req =
1429 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001430 /* unfortunately in Phase1 the classmar2 length is variable */
1431 u_int8_t classmark2_len = gh->data[1];
1432 u_int8_t *classmark2 = gh->data+2;
1433 u_int8_t mi_len = *(classmark2 + classmark2_len);
1434 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001435
Harald Weltec9e02182009-05-01 19:07:53 +00001436 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001437 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001438 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001439 return gsm48_tx_mm_serv_rej(msg->lchan,
1440 GSM48_REJECT_INCORRECT_MESSAGE);
1441 }
1442
1443 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001444 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001445 return gsm48_tx_mm_serv_rej(msg->lchan,
1446 GSM48_REJECT_INCORRECT_MESSAGE);
1447 }
1448
Harald Weltec9e02182009-05-01 19:07:53 +00001449 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001450 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001451 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001452 return gsm48_tx_mm_serv_rej(msg->lchan,
1453 GSM48_REJECT_INCORRECT_MESSAGE);
1454 }
1455
Harald Weltec9e02182009-05-01 19:07:53 +00001456 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001457 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001458 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001459
Holger Freythereb443982009-06-04 13:58:42 +00001460 subscr = subscr_get_by_tmsi(mi_string);
1461
Harald Welte2a139372009-02-22 21:14:55 +00001462 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001463 if (!subscr)
1464 return gsm48_tx_mm_serv_rej(msg->lchan,
1465 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1466
1467 if (!msg->lchan->subscr)
1468 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001469 else if (msg->lchan->subscr != subscr) {
1470 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1471 subscr_put(subscr);
1472 }
1473
Harald Weltec2e302d2009-07-05 14:08:13 +02001474 subscr->equipment.classmark2_len = classmark2_len;
1475 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1476 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001477
Harald Welte4b634542008-12-27 01:55:51 +00001478 return gsm48_tx_mm_serv_ack(msg->lchan);
1479}
1480
Harald Welte2a139372009-02-22 21:14:55 +00001481static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1482{
1483 struct gsm48_hdr *gh = msgb_l3(msg);
1484 struct gsm48_imsi_detach_ind *idi =
1485 (struct gsm48_imsi_detach_ind *) gh->data;
1486 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
1487 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001488 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001489
1490 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
1491 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1492 mi_type, mi_string);
1493
1494 switch (mi_type) {
1495 case GSM_MI_TYPE_TMSI:
1496 subscr = subscr_get_by_tmsi(mi_string);
1497 break;
1498 case GSM_MI_TYPE_IMSI:
1499 subscr = subscr_get_by_imsi(mi_string);
1500 break;
1501 case GSM_MI_TYPE_IMEI:
1502 case GSM_MI_TYPE_IMEISV:
1503 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001504 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001505 break;
1506 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001507 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001508 break;
1509 }
1510
Holger Freyther4a49e772009-04-12 05:37:29 +00001511 if (subscr) {
1512 subscr_update(subscr, msg->trx->bts,
1513 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001514 DEBUGP(DMM, "Subscriber: %s\n",
1515 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001516 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001517 } else
Harald Welte2a139372009-02-22 21:14:55 +00001518 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1519
Harald Welte2a139372009-02-22 21:14:55 +00001520 return 0;
1521}
1522
Harald Welted2a7f5a2009-06-05 20:08:20 +00001523static int gsm48_rx_mm_status(struct msgb *msg)
1524{
1525 struct gsm48_hdr *gh = msgb_l3(msg);
1526
1527 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1528
1529 return 0;
1530}
1531
Harald Weltebf5e8df2009-02-03 12:59:45 +00001532/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001533static int gsm0408_rcv_mm(struct msgb *msg)
1534{
1535 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001537
1538 switch (gh->msg_type & 0xbf) {
1539 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001540 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001541 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001542 break;
1543 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001544 rc = mm_rx_id_resp(msg);
1545 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001546 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001547 rc = gsm48_rx_mm_serv_req(msg);
1548 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001549 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001550 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001551 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001552 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001553 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1554 msg->lchan->subscr ?
1555 msg->lchan->subscr->imsi :
1556 "unknown subscriber");
1557 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001558 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001559 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1560 break;
1561 case GSM48_MT_MM_CM_REEST_REQ:
1562 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1563 break;
1564 case GSM48_MT_MM_AUTH_RESP:
1565 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001566 break;
1567 default:
1568 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1569 gh->msg_type);
1570 break;
1571 }
1572
1573 return rc;
1574}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001575
Harald Welte2d35ae62009-02-06 12:02:13 +00001576/* Receive a PAGING RESPONSE message from the MS */
1577static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1578{
1579 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001580 u_int8_t *classmark2_lv = gh->data + 1;
1581 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1582 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001583 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001584 struct gsm_subscriber *subscr = NULL;
Harald Welte595ad7b2009-02-16 22:05:44 +00001585 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001586 int rc = 0;
1587
Harald Welte61548982009-02-22 21:26:29 +00001588 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001589 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1590 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001591 switch (mi_type) {
1592 case GSM_MI_TYPE_TMSI:
1593 subscr = subscr_get_by_tmsi(mi_string);
1594 break;
1595 case GSM_MI_TYPE_IMSI:
1596 subscr = subscr_get_by_imsi(mi_string);
1597 break;
1598 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001599
1600 if (!subscr) {
1601 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001602 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001603 return -EINVAL;
1604 }
1605 DEBUGP(DRR, "<- Channel was requested by %s\n",
1606 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001607
Harald Weltec2e302d2009-07-05 14:08:13 +02001608 subscr->equipment.classmark2_len = *classmark2_lv;
1609 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1610 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001611
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001612 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001613 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001614 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001615 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1616 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001617 return -EINVAL;
1618 } else {
1619 DEBUGP(DRR, "<- Channel already owned by us\n");
1620 subscr_put(subscr);
1621 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001622 }
1623
Harald Welte595ad7b2009-02-16 22:05:44 +00001624 sig_data.subscr = subscr;
1625 sig_data.bts = msg->lchan->ts->trx->bts;
1626 sig_data.lchan = msg->lchan;
1627
1628 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001629
1630 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001631 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001632
Harald Welte7584aea2009-02-11 11:44:12 +00001633 /* FIXME: somehow signal the completion of the PAGING to
1634 * the entity that requested the paging */
1635
Harald Welte2d35ae62009-02-06 12:02:13 +00001636 return rc;
1637}
1638
Harald Weltef7c43522009-06-09 20:24:21 +00001639static int gsm48_rx_rr_classmark(struct msgb *msg)
1640{
1641 struct gsm48_hdr *gh = msgb_l3(msg);
1642 struct gsm_subscriber *subscr = msg->lchan->subscr;
1643 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1644 u_int8_t cm2_len, cm3_len = 0;
1645 u_int8_t *cm2, *cm3 = NULL;
1646
1647 DEBUGP(DRR, "CLASSMARK CHANGE ");
1648
1649 /* classmark 2 */
1650 cm2_len = gh->data[0];
1651 cm2 = &gh->data[1];
1652 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1653
1654 if (payload_len > cm2_len + 1) {
1655 /* we must have a classmark3 */
1656 if (gh->data[cm2_len+1] != 0x20) {
1657 DEBUGPC(DRR, "ERR CM3 TAG\n");
1658 return -EINVAL;
1659 }
1660 if (cm2_len > 3) {
1661 DEBUGPC(DRR, "CM2 too long!\n");
1662 return -EINVAL;
1663 }
1664
1665 cm3_len = gh->data[cm2_len+2];
1666 cm3 = &gh->data[cm2_len+3];
1667 if (cm3_len > 14) {
1668 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1669 return -EINVAL;
1670 }
1671 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1672 }
1673 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001674 subscr->equipment.classmark2_len = cm2_len;
1675 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001676 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001677 subscr->equipment.classmark3_len = cm3_len;
1678 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001679 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001680 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001681 }
1682
Harald Weltef7c43522009-06-09 20:24:21 +00001683 return 0;
1684}
1685
Harald Weltecf5b3592009-05-01 18:28:42 +00001686static int gsm48_rx_rr_status(struct msgb *msg)
1687{
1688 struct gsm48_hdr *gh = msgb_l3(msg);
1689
1690 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1691 rr_cause_name(gh->data[0]));
1692
1693 return 0;
1694}
1695
Harald Weltef7c43522009-06-09 20:24:21 +00001696static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1697{
1698 struct gsm48_hdr *gh = msgb_l3(msg);
1699 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1700 static struct gsm_meas_rep meas_rep;
1701
Harald Welte10d0e672009-06-27 02:53:10 +02001702 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001703 parse_meas_rep(&meas_rep, gh->data, payload_len);
1704 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001705 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001706 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001707 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001708 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001709 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001710 else
Harald Welte10d0e672009-06-27 02:53:10 +02001711 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001712 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1713 meas_rep.rxqual_sub);
1714
Harald Welte10d0e672009-06-27 02:53:10 +02001715 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001716
1717 /* FIXME: put the results somwhere */
1718
1719 return 0;
1720}
1721
Harald Weltebf5e8df2009-02-03 12:59:45 +00001722/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001723static int gsm0408_rcv_rr(struct msgb *msg)
1724{
1725 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001726 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001727
1728 switch (gh->msg_type) {
1729 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001730 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001731 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001732 case GSM48_MT_RR_GPRS_SUSP_REQ:
1733 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1734 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001735 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001736 rc = gsm48_rr_rx_pag_resp(msg);
1737 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001738 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1739 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001740 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001741 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001742 case GSM48_MT_RR_STATUS:
1743 rc = gsm48_rx_rr_status(msg);
1744 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001745 case GSM48_MT_RR_MEAS_REP:
1746 rc = gsm48_rx_rr_meas_rep(msg);
1747 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001748 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001749 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001750 gh->msg_type);
1751 break;
1752 }
1753
Harald Welte2d35ae62009-02-06 12:02:13 +00001754 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001755}
1756
Holger Freythere64a7a32009-02-06 21:55:37 +00001757/* 7.1.7 and 9.1.7 Channel release*/
1758int gsm48_send_rr_release(struct gsm_lchan *lchan)
1759{
1760 struct msgb *msg = gsm48_msgb_alloc();
1761 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1762 u_int8_t *cause;
1763
1764 msg->lchan = lchan;
1765 gh->proto_discr = GSM48_PDISC_RR;
1766 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1767
1768 cause = msgb_put(msg, 1);
1769 cause[0] = GSM48_RR_CAUSE_NORMAL;
1770
1771 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1772 lchan->nr, lchan->type);
1773
Harald Welteae0f2362009-07-19 18:36:49 +02001774 /* Send actual release request to MS */
1775 gsm48_sendmsg(msg);
1776
1777 /* Deactivate the SACCH on the BTS side */
1778 return rsl_deact_sacch(lchan);
Holger Freythere64a7a32009-02-06 21:55:37 +00001779}
1780
Harald Welte4bc90a12008-12-27 16:32:52 +00001781/* Call Control */
1782
Harald Welte7584aea2009-02-11 11:44:12 +00001783/* The entire call control code is written in accordance with Figure 7.10c
1784 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1785 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1786 * it for voice */
1787
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788static void new_cc_state(struct gsm_trans *trans, int state)
1789{
1790 if (state > 31 || state < 0)
1791 return;
1792
1793 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteaa0b29c2009-07-23 18:56:43 +02001794 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795
Harald Welteaa0b29c2009-07-23 18:56:43 +02001796 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797}
1798
1799static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001800{
1801 struct msgb *msg = gsm48_msgb_alloc();
1802 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1803 u_int8_t *cause, *call_state;
1804
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
1806 msg->lchan = trans->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001807 gh->msg_type = GSM48_MT_CC_STATUS;
1808
1809 cause = msgb_put(msg, 3);
1810 cause[0] = 2;
1811 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1812 cause[2] = 0x80 | 30; /* response to status inquiry */
1813
1814 call_state = msgb_put(msg, 1);
1815 call_state[0] = 0xc0 | 0x00;
1816
Harald Welte65e74cc2008-12-29 01:55:35 +00001817 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001818}
1819
Harald Welte6f4b7532008-12-29 00:39:37 +00001820static int gsm48_tx_simple(struct gsm_lchan *lchan,
1821 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001822{
1823 struct msgb *msg = gsm48_msgb_alloc();
1824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1825
1826 msg->lchan = lchan;
1827
Harald Welte6f4b7532008-12-29 00:39:37 +00001828 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001829 gh->msg_type = msg_type;
1830
Harald Welte65e74cc2008-12-29 01:55:35 +00001831 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001832}
1833
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1835{
Harald Welteaa0b29c2009-07-23 18:56:43 +02001836 if (bsc_timer_pending(&trans->cc.timer)) {
1837 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1838 bsc_del_timer(&trans->cc.timer);
1839 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001840 }
1841}
1842
1843static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1844 int msg_type, struct gsm_mncc *mncc)
1845{
1846 struct msgb *msg;
1847
1848 if (trans)
1849 if (trans->lchan)
1850 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
1851 "Sending '%s' to MNCC.\n",
1852 trans->lchan->ts->trx->bts->nr,
1853 trans->lchan->ts->trx->nr,
1854 trans->lchan->ts->nr, trans->transaction_id,
1855 (trans->subscr)?(trans->subscr->extension):"-",
1856 get_mncc_name(msg_type));
1857 else
1858 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1859 "Sending '%s' to MNCC.\n",
1860 (trans->subscr)?(trans->subscr->extension):"-",
1861 get_mncc_name(msg_type));
1862 else
1863 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1864 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1865
1866 mncc->msg_type = msg_type;
1867
Harald Welte966636f2009-06-26 19:39:35 +02001868 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 if (!msg)
1870 return -ENOMEM;
1871 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1872 msgb_enqueue(&net->upqueue, msg);
1873
1874 return 0;
1875}
1876
1877int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1878 u_int32_t callref, int location, int value)
1879{
1880 struct gsm_mncc rel;
1881
Harald Welte92f70c52009-06-12 01:54:08 +08001882 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001884 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001885 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1886}
1887
Harald Welteaa0b29c2009-07-23 18:56:43 +02001888/* Call Control Specific transaction release.
1889 * gets called by trans_free, DO NOT CALL YOURSELF! */
1890void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892 gsm48_stop_cc_timer(trans);
1893
1894 /* send release to L4, if callref still exists */
1895 if (trans->callref) {
1896 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001897 mncc_release_ind(trans->network, trans, trans->callref,
1898 GSM48_CAUSE_LOC_PRN_S_LU,
1899 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 }
Harald Welteaa0b29c2009-07-23 18:56:43 +02001901 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welteaa0b29c2009-07-23 18:56:43 +02001903 if (trans->lchan)
1904 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1908
Harald Welte09e38af2009-02-16 22:52:23 +00001909/* call-back from paging the B-end of the connection */
1910static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001911 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001912{
Harald Welte7ccf7782009-02-17 01:43:01 +00001913 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 struct gsm_subscriber *subscr = param;
1915 struct gsm_trans *transt, *tmp;
1916 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001917
Harald Welte09e38af2009-02-16 22:52:23 +00001918 if (hooknum != GSM_HOOK_RR_PAGING)
1919 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920
1921 if (!subscr)
1922 return -EINVAL;
1923 net = subscr->net;
1924 if (!net) {
1925 DEBUGP(DCC, "Error Network not set!\n");
1926 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001927 }
Harald Welte7584aea2009-02-11 11:44:12 +00001928
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 /* check all tranactions (without lchan) for subscriber */
1930 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1931 if (transt->subscr != subscr || transt->lchan)
1932 continue;
1933 switch (event) {
1934 case GSM_PAGING_SUCCEEDED:
1935 if (!lchan) // paranoid
1936 break;
1937 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1938 subscr->extension);
1939 /* Assign lchan */
1940 if (!transt->lchan) {
1941 transt->lchan = lchan;
1942 use_lchan(lchan);
1943 }
1944 /* send SETUP request to called party */
Harald Welteaa0b29c2009-07-23 18:56:43 +02001945 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 if (is_ipaccess_bts(lchan->ts->trx->bts))
1947 rsl_ipacc_bind(lchan);
1948 break;
1949 case GSM_PAGING_EXPIRED:
1950 DEBUGP(DCC, "Paging subscr %s expired!\n",
1951 subscr->extension);
1952 /* Temporarily out of order */
1953 mncc_release_ind(transt->network, transt, transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001954 GSM48_CAUSE_LOC_PRN_S_LU,
1955 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 transt->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02001957 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 break;
1959 }
1960 }
Harald Welte09e38af2009-02-16 22:52:23 +00001961 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001962}
Harald Welte7584aea2009-02-11 11:44:12 +00001963
Harald Welte49f48b82009-02-17 15:29:33 +00001964/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001965static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001966{
Harald Welte11fa29c2009-02-19 17:24:39 +00001967 struct gsm_bts *bts = lchan->ts->trx->bts;
1968 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001969 struct gsm_bts_trx_ts *ts;
1970
Harald Welte11fa29c2009-02-19 17:24:39 +00001971 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1972 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1973 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1974
1975 if (bts->type != remote_bts->type) {
1976 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1977 return -EINVAL;
1978 }
Harald Welte49f48b82009-02-17 15:29:33 +00001979
Harald Welte11fa29c2009-02-19 17:24:39 +00001980 switch (bts->type) {
1981 case GSM_BTS_TYPE_NANOBTS_900:
1982 case GSM_BTS_TYPE_NANOBTS_1800:
1983 ts = remote_lchan->ts;
Harald Welte20855542009-07-12 09:50:35 +02001984 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
1985 ts->abis_ip.bound_port,
1986 lchan->ts->abis_ip.conn_id,
1987 ts->abis_ip.rtp_payload2);
Harald Welte11fa29c2009-02-19 17:24:39 +00001988
1989 ts = lchan->ts;
Harald Welte20855542009-07-12 09:50:35 +02001990 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
1991 ts->abis_ip.bound_port,
1992 remote_lchan->ts->abis_ip.conn_id,
1993 ts->abis_ip.rtp_payload2);
Harald Welte11fa29c2009-02-19 17:24:39 +00001994 break;
1995 case GSM_BTS_TYPE_BS11:
1996 trau_mux_map_lchan(lchan, remote_lchan);
1997 break;
1998 default:
1999 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2000 break;
2001 }
Harald Welte49f48b82009-02-17 15:29:33 +00002002
2003 return 0;
2004}
2005
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006/* bridge channels of two transactions */
2007static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002008{
Harald Welteaa0b29c2009-07-23 18:56:43 +02002009 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
2010 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002011
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002013 return -EIO;
2014
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 if (!trans1->lchan || !trans2->lchan)
2016 return -EIO;
2017
2018 /* through-connect channel */
2019 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002020}
2021
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022/* enable receive of channels to upqueue */
2023static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2024{
2025 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002026
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* Find callref */
Harald Welteaa0b29c2009-07-23 18:56:43 +02002028 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029 if (!trans)
2030 return -EIO;
2031 if (!trans->lchan)
2032 return 0;
2033
2034 // todo IPACCESS
2035 if (enable)
2036 return trau_recv_lchan(trans->lchan, data->callref);
2037 return trau_mux_unmap(NULL, data->callref);
2038}
2039
2040/* send a frame to channel */
2041static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2042{
2043 struct gsm_trans *trans;
2044
2045 /* Find callref */
Harald Welteaa0b29c2009-07-23 18:56:43 +02002046 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 if (!trans)
2048 return -EIO;
2049 if (!trans->lchan)
2050 return 0;
2051 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2052 trans->lchan->type != GSM_LCHAN_TCH_H)
2053 return 0;
2054
2055 // todo IPACCESS
2056 return trau_send_lchan(trans->lchan,
2057 (struct decoded_trau_frame *)frame->data);
2058}
2059
2060
2061static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2062{
2063 DEBUGP(DCC, "-> STATUS ENQ\n");
2064 return gsm48_cc_tx_status(trans, msg);
2065}
2066
2067static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2068static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2069
2070static void gsm48_cc_timeout(void *arg)
2071{
2072 struct gsm_trans *trans = arg;
2073 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002074 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2075 int mo_location = GSM48_CAUSE_LOC_USER;
2076 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2077 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 struct gsm_mncc mo_rel, l4_rel;
2079
2080 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2081 mo_rel.callref = trans->callref;
2082 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2083 l4_rel.callref = trans->callref;
2084
Harald Welteaa0b29c2009-07-23 18:56:43 +02002085 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 case 0x303:
2087 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002088 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 break;
2090 case 0x310:
2091 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002092 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 break;
2094 case 0x313:
2095 disconnect = 1;
2096 /* unknown, did not find it in the specs */
2097 break;
2098 case 0x301:
2099 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002100 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 break;
2102 case 0x308:
Harald Welteaa0b29c2009-07-23 18:56:43 +02002103 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 /* restart T308 a second time */
Harald Welteaa0b29c2009-07-23 18:56:43 +02002105 gsm48_cc_tx_release(trans, &trans->cc.msg);
2106 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 break; /* stay in release state */
2108 }
Harald Welteaa0b29c2009-07-23 18:56:43 +02002109 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 return;
2111// release = 1;
2112// l4_cause = 14;
2113// break;
2114 case 0x306:
2115 release = 1;
Harald Welteaa0b29c2009-07-23 18:56:43 +02002116 mo_cause = trans->cc.msg.cause.value;
2117 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 break;
2119 case 0x323:
2120 disconnect = 1;
2121 break;
2122 default:
2123 release = 1;
2124 }
2125
2126 if (release && trans->callref) {
2127 /* process release towards layer 4 */
2128 mncc_release_ind(trans->network, trans, trans->callref,
2129 l4_location, l4_cause);
2130 trans->callref = 0;
2131 }
2132
2133 if (disconnect && trans->callref) {
2134 /* process disconnect towards layer 4 */
2135 mncc_set_cause(&l4_rel, l4_location, l4_cause);
2136 mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &l4_rel);
2137 }
2138
2139 /* process disconnect towards mobile station */
2140 if (disconnect || release) {
2141 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Welteaa0b29c2009-07-23 18:56:43 +02002142 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2143 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2144 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 mo_rel.cause.diag_len = 3;
2146
2147 if (disconnect)
2148 gsm48_cc_tx_disconnect(trans, &mo_rel);
2149 if (release)
2150 gsm48_cc_tx_release(trans, &mo_rel);
2151 }
2152
2153}
2154
2155static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2156 int sec, int micro)
2157{
2158 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Welteaa0b29c2009-07-23 18:56:43 +02002159 trans->cc.timer.cb = gsm48_cc_timeout;
2160 trans->cc.timer.data = trans;
2161 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2162 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163}
2164
2165static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2166{
2167 struct gsm48_hdr *gh = msgb_l3(msg);
2168 u_int8_t msg_type = gh->msg_type & 0xbf;
2169 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2170 struct tlv_parsed tp;
2171 struct gsm_mncc setup;
2172
2173 memset(&setup, 0, sizeof(struct gsm_mncc));
2174 setup.callref = trans->callref;
2175 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2176 /* emergency setup is identified by msg_type */
2177 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2178 setup.emergency = 1;
2179
2180 /* use subscriber as calling party number */
2181 if (trans->subscr) {
2182 setup.fields |= MNCC_F_CALLING;
2183 strncpy(setup.calling.number, trans->subscr->extension,
2184 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002185 strncpy(setup.imsi, trans->subscr->imsi,
2186 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 }
2188 /* bearer capability */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2190 setup.fields |= MNCC_F_BEARER_CAP;
2191 decode_bearer_cap(&setup.bearer_cap,
2192 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2193 }
2194 /* facility */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2196 setup.fields |= MNCC_F_FACILITY;
2197 decode_facility(&setup.facility,
2198 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2199 }
2200 /* called party bcd number */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2202 setup.fields |= MNCC_F_CALLED;
2203 decode_called(&setup.called,
2204 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2205 }
2206 /* user-user */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2208 setup.fields |= MNCC_F_USERUSER;
2209 decode_useruser(&setup.useruser,
2210 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2211 }
2212 /* ss-version */
2213 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2214 setup.fields |= MNCC_F_SSVERSION;
2215 decode_ssversion(&setup.ssversion,
2216 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2217 }
2218 /* CLIR suppression */
2219 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2220 setup.clir.sup = 1;
2221 /* CLIR invocation */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2223 setup.clir.inv = 1;
2224 /* cc cap */
2225 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2226 setup.fields |= MNCC_F_CCCAP;
2227 decode_cccap(&setup.cccap,
2228 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2229 }
2230
2231 if (is_ipaccess_bts(msg->trx->bts))
2232 rsl_ipacc_bind(msg->lchan);
2233
2234 new_cc_state(trans, GSM_CSTATE_INITIATED);
2235
2236 /* indicate setup to MNCC */
2237 mncc_recvmsg(trans->network, trans, MNCC_SETUP_IND, &setup);
2238
2239 return 0;
2240}
2241
2242static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002243{
2244 struct msgb *msg = gsm48_msgb_alloc();
2245 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 struct gsm_mncc *setup = arg;
2247 struct gsm_trans *transt;
2248 u_int16_t trans_id_mask = 0;
2249 int rc, i;
Harald Welte65e74cc2008-12-29 01:55:35 +00002250
Harald Welte7ccf7782009-02-17 01:43:01 +00002251 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002252
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 /* transaction id must not be assigned */
2254 if (trans->transaction_id != 0xff) { /* unasssigned */
2255 DEBUGP(DCC, "TX Setup with assigned transaction. "
2256 "This is not allowed!\n");
2257 /* Temporarily out of order */
2258 rc = mncc_release_ind(trans->network, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002259 GSM48_CAUSE_LOC_PRN_S_LU,
2260 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 trans->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02002262 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 return rc;
2264 }
2265
2266 /* Get free transaction_id */
2267 llist_for_each_entry(transt, &trans->network->trans_list, entry) {
2268 /* Transaction of our lchan? */
2269 if (transt->lchan == trans->lchan &&
2270 transt->transaction_id != 0xff)
2271 trans_id_mask |= (1 << (transt->transaction_id >> 4));
2272 }
2273 /* Assign free transaction ID */
2274 if ((trans_id_mask & 0x007f) == 0x7f) {
2275 /* no free transaction ID */
2276 rc = mncc_release_ind(trans->network, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002277 GSM48_CAUSE_LOC_PRN_S_LU,
2278 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 trans->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02002280 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 return rc;
2282 }
2283 for (i = 0; i < 7; i++) {
2284 if ((trans_id_mask & (1 << i)) == 0) {
2285 trans->transaction_id = i << 4; /* flag = 0 */
2286 break;
2287 }
2288 }
Harald Welte49f48b82009-02-17 15:29:33 +00002289
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2291 msg->lchan = trans->lchan;
Harald Welte65e74cc2008-12-29 01:55:35 +00002292 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002293
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002295
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 /* bearer capability */
2297 if (setup->fields & MNCC_F_BEARER_CAP)
2298 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2299 /* facility */
2300 if (setup->fields & MNCC_F_FACILITY)
2301 encode_facility(msg, 0, &setup->facility);
2302 /* progress */
2303 if (setup->fields & MNCC_F_PROGRESS)
2304 encode_progress(msg, 0, &setup->progress);
2305 /* calling party BCD number */
2306 if (setup->fields & MNCC_F_CALLING)
2307 encode_calling(msg, &setup->calling);
2308 /* called party BCD number */
2309 if (setup->fields & MNCC_F_CALLED)
2310 encode_called(msg, &setup->called);
2311 /* user-user */
2312 if (setup->fields & MNCC_F_USERUSER)
2313 encode_useruser(msg, 0, &setup->useruser);
2314 /* redirecting party BCD number */
2315 if (setup->fields & MNCC_F_REDIRECTING)
2316 encode_redirecting(msg, &setup->redirecting);
2317 /* signal */
2318 if (setup->fields & MNCC_F_SIGNAL)
2319 encode_signal(msg, setup->signal);
2320
2321 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002322
2323 return gsm48_sendmsg(msg);
2324}
2325
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2327{
2328 struct gsm48_hdr *gh = msgb_l3(msg);
2329 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2330 struct tlv_parsed tp;
2331 struct gsm_mncc call_conf;
2332
2333 gsm48_stop_cc_timer(trans);
2334 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2335
2336 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2337 call_conf.callref = trans->callref;
2338 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2339#if 0
2340 /* repeat */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2342 call_conf.repeat = 1;
2343 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2344 call_conf.repeat = 2;
2345#endif
2346 /* bearer capability */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2348 call_conf.fields |= MNCC_F_BEARER_CAP;
2349 decode_bearer_cap(&call_conf.bearer_cap,
2350 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2351 }
2352 /* cause */
2353 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2354 call_conf.fields |= MNCC_F_CAUSE;
2355 decode_cause(&call_conf.cause,
2356 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2357 }
2358 /* cc cap */
2359 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2360 call_conf.fields |= MNCC_F_CCCAP;
2361 decode_cccap(&call_conf.cccap,
2362 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2363 }
2364
2365 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2366
2367 return mncc_recvmsg(trans->network, trans, MNCC_CALL_CONF_IND, &call_conf);
2368}
2369
2370static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2371{
2372 struct gsm_mncc *proceeding = arg;
2373 struct msgb *msg = gsm48_msgb_alloc();
2374 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2375
2376 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2377 msg->lchan = trans->lchan;
2378 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2379
2380 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2381
2382 /* bearer capability */
2383 if (proceeding->fields & MNCC_F_BEARER_CAP)
2384 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2385 /* facility */
2386 if (proceeding->fields & MNCC_F_FACILITY)
2387 encode_facility(msg, 0, &proceeding->facility);
2388 /* progress */
2389 if (proceeding->fields & MNCC_F_PROGRESS)
2390 encode_progress(msg, 0, &proceeding->progress);
2391
2392 return gsm48_sendmsg(msg);
2393}
2394
2395static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2396{
2397 struct gsm48_hdr *gh = msgb_l3(msg);
2398 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2399 struct tlv_parsed tp;
2400 struct gsm_mncc alerting;
2401
2402 gsm48_stop_cc_timer(trans);
2403 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2404
2405 memset(&alerting, 0, sizeof(struct gsm_mncc));
2406 alerting.callref = trans->callref;
2407 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2408 /* facility */
2409 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2410 alerting.fields |= MNCC_F_FACILITY;
2411 decode_facility(&alerting.facility,
2412 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2413 }
2414
2415 /* progress */
2416 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2417 alerting.fields |= MNCC_F_PROGRESS;
2418 decode_progress(&alerting.progress,
2419 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2420 }
2421 /* ss-version */
2422 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2423 alerting.fields |= MNCC_F_SSVERSION;
2424 decode_ssversion(&alerting.ssversion,
2425 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2426 }
2427
2428 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2429
2430 return mncc_recvmsg(trans->network, trans, MNCC_ALERT_IND, &alerting);
2431}
2432
2433static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2434{
2435 struct gsm_mncc *alerting = arg;
2436 struct msgb *msg = gsm48_msgb_alloc();
2437 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2438
2439 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2440 msg->lchan = trans->lchan;
2441 gh->msg_type = GSM48_MT_CC_ALERTING;
2442
2443 /* facility */
2444 if (alerting->fields & MNCC_F_FACILITY)
2445 encode_facility(msg, 0, &alerting->facility);
2446 /* progress */
2447 if (alerting->fields & MNCC_F_PROGRESS)
2448 encode_progress(msg, 0, &alerting->progress);
2449 /* user-user */
2450 if (alerting->fields & MNCC_F_USERUSER)
2451 encode_useruser(msg, 0, &alerting->useruser);
2452
2453 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2454
2455 return gsm48_sendmsg(msg);
2456}
2457
2458static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2459{
2460 struct gsm_mncc *progress = arg;
2461 struct msgb *msg = gsm48_msgb_alloc();
2462 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2463
2464 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2465 msg->lchan = trans->lchan;
2466 gh->msg_type = GSM48_MT_CC_PROGRESS;
2467
2468 /* progress */
2469 encode_progress(msg, 1, &progress->progress);
2470 /* user-user */
2471 if (progress->fields & MNCC_F_USERUSER)
2472 encode_useruser(msg, 0, &progress->useruser);
2473
2474 return gsm48_sendmsg(msg);
2475}
2476
2477static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2478{
2479 struct gsm_mncc *connect = arg;
2480 struct msgb *msg = gsm48_msgb_alloc();
2481 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2482
2483 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2484 msg->lchan = trans->lchan;
2485 gh->msg_type = GSM48_MT_CC_CONNECT;
2486
2487 gsm48_stop_cc_timer(trans);
2488 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2489
2490 /* facility */
2491 if (connect->fields & MNCC_F_FACILITY)
2492 encode_facility(msg, 0, &connect->facility);
2493 /* progress */
2494 if (connect->fields & MNCC_F_PROGRESS)
2495 encode_progress(msg, 0, &connect->progress);
2496 /* connected number */
2497 if (connect->fields & MNCC_F_CONNECTED)
2498 encode_connected(msg, &connect->connected);
2499 /* user-user */
2500 if (connect->fields & MNCC_F_USERUSER)
2501 encode_useruser(msg, 0, &connect->useruser);
2502
2503 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2504
2505 return gsm48_sendmsg(msg);
2506}
2507
2508static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2509{
2510 struct gsm48_hdr *gh = msgb_l3(msg);
2511 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2512 struct tlv_parsed tp;
2513 struct gsm_mncc connect;
2514
2515 gsm48_stop_cc_timer(trans);
2516
2517 memset(&connect, 0, sizeof(struct gsm_mncc));
2518 connect.callref = trans->callref;
2519 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2520 /* use subscriber as connected party number */
2521 if (trans->subscr) {
2522 connect.fields |= MNCC_F_CONNECTED;
2523 strncpy(connect.connected.number, trans->subscr->extension,
2524 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002525 strncpy(connect.imsi, trans->subscr->imsi,
2526 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 }
2528 /* facility */
2529 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2530 connect.fields |= MNCC_F_FACILITY;
2531 decode_facility(&connect.facility,
2532 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2533 }
2534 /* user-user */
2535 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2536 connect.fields |= MNCC_F_USERUSER;
2537 decode_useruser(&connect.useruser,
2538 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2539 }
2540 /* ss-version */
2541 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2542 connect.fields |= MNCC_F_SSVERSION;
2543 decode_ssversion(&connect.ssversion,
2544 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2545 }
2546
2547 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2548
2549 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_CNF, &connect);
2550}
2551
2552
2553static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2554{
2555 struct gsm_mncc connect_ack;
2556
2557 gsm48_stop_cc_timer(trans);
2558
2559 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2560
2561 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2562 connect_ack.callref = trans->callref;
2563 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_COMPL_IND,
2564 &connect_ack);
2565}
2566
2567static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2568{
2569 struct msgb *msg = gsm48_msgb_alloc();
2570 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2571
2572 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2573 msg->lchan = trans->lchan;
2574 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2575
2576 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2577
2578 return gsm48_sendmsg(msg);
2579}
2580
2581static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2582{
2583 struct gsm48_hdr *gh = msgb_l3(msg);
2584 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2585 struct tlv_parsed tp;
2586 struct gsm_mncc disc;
2587
2588 gsm48_stop_cc_timer(trans);
2589
2590 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2591
2592 memset(&disc, 0, sizeof(struct gsm_mncc));
2593 disc.callref = trans->callref;
2594 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2595 /* cause */
2596 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2597 disc.fields |= MNCC_F_CAUSE;
2598 decode_cause(&disc.cause,
2599 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2600 }
2601 /* facility */
2602 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2603 disc.fields |= MNCC_F_FACILITY;
2604 decode_facility(&disc.facility,
2605 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2606 }
2607 /* user-user */
2608 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2609 disc.fields |= MNCC_F_USERUSER;
2610 decode_useruser(&disc.useruser,
2611 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2612 }
2613 /* ss-version */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2615 disc.fields |= MNCC_F_SSVERSION;
2616 decode_ssversion(&disc.ssversion,
2617 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2618 }
2619
2620 return mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &disc);
2621
2622}
2623
Harald Weltec66b71c2009-06-11 14:23:20 +08002624static struct gsm_mncc_cause default_cause = {
2625 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2626 .coding = 0,
2627 .rec = 0,
2628 .rec_val = 0,
2629 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2630 .diag_len = 0,
2631 .diag = { 0 },
2632};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633
2634static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2635{
2636 struct gsm_mncc *disc = arg;
2637 struct msgb *msg = gsm48_msgb_alloc();
2638 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2639
2640 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2641 msg->lchan = trans->lchan;
2642 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2643
2644 gsm48_stop_cc_timer(trans);
2645 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2646
2647 /* cause */
2648 if (disc->fields & MNCC_F_CAUSE)
2649 encode_cause(msg, 1, &disc->cause);
2650 else
2651 encode_cause(msg, 1, &default_cause);
2652
2653 /* facility */
2654 if (disc->fields & MNCC_F_FACILITY)
2655 encode_facility(msg, 0, &disc->facility);
2656 /* progress */
2657 if (disc->fields & MNCC_F_PROGRESS)
2658 encode_progress(msg, 0, &disc->progress);
2659 /* user-user */
2660 if (disc->fields & MNCC_F_USERUSER)
2661 encode_useruser(msg, 0, &disc->useruser);
2662
2663 /* store disconnect cause for T306 expiry */
Harald Welteaa0b29c2009-07-23 18:56:43 +02002664 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665
2666 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2667
2668 return gsm48_sendmsg(msg);
2669}
2670
2671static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2672{
2673 struct gsm48_hdr *gh = msgb_l3(msg);
2674 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2675 struct tlv_parsed tp;
2676 struct gsm_mncc rel;
2677 int rc;
2678
2679 gsm48_stop_cc_timer(trans);
2680
2681 memset(&rel, 0, sizeof(struct gsm_mncc));
2682 rel.callref = trans->callref;
2683 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2684 /* cause */
2685 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2686 rel.fields |= MNCC_F_CAUSE;
2687 decode_cause(&rel.cause,
2688 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2689 }
2690 /* facility */
2691 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2692 rel.fields |= MNCC_F_FACILITY;
2693 decode_facility(&rel.facility,
2694 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2695 }
2696 /* user-user */
2697 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2698 rel.fields |= MNCC_F_USERUSER;
2699 decode_useruser(&rel.useruser,
2700 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2701 }
2702 /* ss-version */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2704 rel.fields |= MNCC_F_SSVERSION;
2705 decode_ssversion(&rel.ssversion,
2706 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2707 }
2708
Harald Welteaa0b29c2009-07-23 18:56:43 +02002709 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 /* release collision 5.4.5 */
2711 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_CNF, &rel);
2712 } else {
2713 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC | trans->transaction_id,
2714 GSM48_MT_CC_RELEASE_COMPL);
2715 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_IND, &rel);
2716 }
2717
2718 new_cc_state(trans, GSM_CSTATE_NULL);
2719
2720 trans->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02002721 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722
2723 return rc;
2724}
2725
2726static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2727{
2728 struct gsm_mncc *rel = arg;
2729 struct msgb *msg = gsm48_msgb_alloc();
2730 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2731
2732 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2733 msg->lchan = trans->lchan;
2734 gh->msg_type = GSM48_MT_CC_RELEASE;
2735
2736 trans->callref = 0;
2737
2738 gsm48_stop_cc_timer(trans);
2739 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2740
2741 /* cause */
2742 if (rel->fields & MNCC_F_CAUSE)
2743 encode_cause(msg, 0, &rel->cause);
2744 /* facility */
2745 if (rel->fields & MNCC_F_FACILITY)
2746 encode_facility(msg, 0, &rel->facility);
2747 /* user-user */
2748 if (rel->fields & MNCC_F_USERUSER)
2749 encode_useruser(msg, 0, &rel->useruser);
2750
Harald Welteaa0b29c2009-07-23 18:56:43 +02002751 trans->cc.T308_second = 0;
2752 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753
Harald Welteaa0b29c2009-07-23 18:56:43 +02002754 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2756
2757 return gsm48_sendmsg(msg);
2758}
2759
2760static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2761{
2762 struct gsm48_hdr *gh = msgb_l3(msg);
2763 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2764 struct tlv_parsed tp;
2765 struct gsm_mncc rel;
2766 int rc = 0;
2767
2768 gsm48_stop_cc_timer(trans);
2769
2770 memset(&rel, 0, sizeof(struct gsm_mncc));
2771 rel.callref = trans->callref;
2772 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2773 /* cause */
2774 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2775 rel.fields |= MNCC_F_CAUSE;
2776 decode_cause(&rel.cause,
2777 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2778 }
2779 /* facility */
2780 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2781 rel.fields |= MNCC_F_FACILITY;
2782 decode_facility(&rel.facility,
2783 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2784 }
2785 /* user-user */
2786 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2787 rel.fields |= MNCC_F_USERUSER;
2788 decode_useruser(&rel.useruser,
2789 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2790 }
2791 /* ss-version */
2792 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2793 rel.fields |= MNCC_F_SSVERSION;
2794 decode_ssversion(&rel.ssversion,
2795 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2796 }
2797
2798 if (trans->callref) {
Harald Welteaa0b29c2009-07-23 18:56:43 +02002799 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800 case GSM_CSTATE_CALL_PRESENT:
2801 rc = mncc_recvmsg(trans->network, trans,
2802 MNCC_REJ_IND, &rel);
2803 break;
2804 case GSM_CSTATE_RELEASE_REQ:
2805 rc = mncc_recvmsg(trans->network, trans,
2806 MNCC_REL_CNF, &rel);
2807 break;
2808 default:
2809 rc = mncc_recvmsg(trans->network, trans,
2810 MNCC_REL_IND, &rel);
2811 }
2812 }
2813
2814 trans->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02002815 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816
2817 return rc;
2818}
2819
2820static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2821{
2822 struct gsm_mncc *rel = arg;
2823 struct msgb *msg = gsm48_msgb_alloc();
2824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2825
2826 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2827 msg->lchan = trans->lchan;
2828 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2829
2830 trans->callref = 0;
2831
2832 gsm48_stop_cc_timer(trans);
2833
2834 /* cause */
2835 if (rel->fields & MNCC_F_CAUSE)
2836 encode_cause(msg, 0, &rel->cause);
2837 /* facility */
2838 if (rel->fields & MNCC_F_FACILITY)
2839 encode_facility(msg, 0, &rel->facility);
2840 /* user-user */
2841 if (rel->fields & MNCC_F_USERUSER)
2842 encode_useruser(msg, 0, &rel->useruser);
2843
Harald Welteaa0b29c2009-07-23 18:56:43 +02002844 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845
2846 return gsm48_sendmsg(msg);
2847}
2848
2849static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2850{
2851 struct gsm48_hdr *gh = msgb_l3(msg);
2852 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2853 struct tlv_parsed tp;
2854 struct gsm_mncc fac;
2855
2856 memset(&fac, 0, sizeof(struct gsm_mncc));
2857 fac.callref = trans->callref;
2858 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2859 /* facility */
2860 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2861 fac.fields |= MNCC_F_FACILITY;
2862 decode_facility(&fac.facility,
2863 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2864 }
2865 /* ss-version */
2866 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2867 fac.fields |= MNCC_F_SSVERSION;
2868 decode_ssversion(&fac.ssversion,
2869 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2870 }
2871
2872 return mncc_recvmsg(trans->network, trans, MNCC_FACILITY_IND, &fac);
2873}
2874
2875static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2876{
2877 struct gsm_mncc *fac = arg;
2878 struct msgb *msg = gsm48_msgb_alloc();
2879 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2880
2881 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2882 msg->lchan = trans->lchan;
2883 gh->msg_type = GSM48_MT_CC_FACILITY;
2884
2885 /* facility */
2886 encode_facility(msg, 1, &fac->facility);
2887
2888 return gsm48_sendmsg(msg);
2889}
2890
2891static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2892{
2893 struct gsm_mncc hold;
2894
2895 memset(&hold, 0, sizeof(struct gsm_mncc));
2896 hold.callref = trans->callref;
2897 return mncc_recvmsg(trans->network, trans, MNCC_HOLD_IND, &hold);
2898}
2899
2900static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2901{
2902 struct msgb *msg = gsm48_msgb_alloc();
2903 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2904
2905 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2906 msg->lchan = trans->lchan;
2907 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2908
2909 return gsm48_sendmsg(msg);
2910}
2911
2912static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2913{
2914 struct gsm_mncc *hold_rej = arg;
2915 struct msgb *msg = gsm48_msgb_alloc();
2916 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2917
2918 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2919 msg->lchan = trans->lchan;
2920 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2921
2922 /* cause */
2923 if (hold_rej->fields & MNCC_F_CAUSE)
2924 encode_cause(msg, 1, &hold_rej->cause);
2925 else
2926 encode_cause(msg, 1, &default_cause);
2927
2928 return gsm48_sendmsg(msg);
2929}
2930
2931static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2932{
2933 struct gsm_mncc retrieve;
2934
2935 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2936 retrieve.callref = trans->callref;
2937 return mncc_recvmsg(trans->network, trans, MNCC_RETRIEVE_IND, &retrieve);
2938}
2939
2940static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2941{
2942 struct msgb *msg = gsm48_msgb_alloc();
2943 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2944
2945 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2946 msg->lchan = trans->lchan;
2947 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2948
2949 return gsm48_sendmsg(msg);
2950}
2951
2952static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2953{
2954 struct gsm_mncc *retrieve_rej = arg;
2955 struct msgb *msg = gsm48_msgb_alloc();
2956 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2957
2958 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2959 msg->lchan = trans->lchan;
2960 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2961
2962 /* cause */
2963 if (retrieve_rej->fields & MNCC_F_CAUSE)
2964 encode_cause(msg, 1, &retrieve_rej->cause);
2965 else
2966 encode_cause(msg, 1, &default_cause);
2967
2968 return gsm48_sendmsg(msg);
2969}
2970
2971static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2972{
2973 struct gsm48_hdr *gh = msgb_l3(msg);
2974 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2975 struct tlv_parsed tp;
2976 struct gsm_mncc dtmf;
2977
2978 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2979 dtmf.callref = trans->callref;
2980 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2981 /* keypad facility */
2982 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2983 dtmf.fields |= MNCC_F_KEYPAD;
2984 decode_keypad(&dtmf.keypad,
2985 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2986 }
2987
2988 return mncc_recvmsg(trans->network, trans, MNCC_START_DTMF_IND, &dtmf);
2989}
2990
2991static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2992{
2993 struct gsm_mncc *dtmf = arg;
2994 struct msgb *msg = gsm48_msgb_alloc();
2995 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2996
2997 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2998 msg->lchan = trans->lchan;
2999 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3000
3001 /* keypad */
3002 if (dtmf->fields & MNCC_F_KEYPAD)
3003 encode_keypad(msg, dtmf->keypad);
3004
3005 return gsm48_sendmsg(msg);
3006}
3007
3008static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3009{
3010 struct gsm_mncc *dtmf = arg;
3011 struct msgb *msg = gsm48_msgb_alloc();
3012 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3013
3014 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3015 msg->lchan = trans->lchan;
3016 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3017
3018 /* cause */
3019 if (dtmf->fields & MNCC_F_CAUSE)
3020 encode_cause(msg, 1, &dtmf->cause);
3021 else
3022 encode_cause(msg, 1, &default_cause);
3023
3024 return gsm48_sendmsg(msg);
3025}
3026
3027static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3028{
3029 struct msgb *msg = gsm48_msgb_alloc();
3030 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3031
3032 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3033 msg->lchan = trans->lchan;
3034 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3035
3036 return gsm48_sendmsg(msg);
3037}
3038
3039static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3040{
3041 struct gsm_mncc dtmf;
3042
3043 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3044 dtmf.callref = trans->callref;
3045
3046 return mncc_recvmsg(trans->network, trans, MNCC_STOP_DTMF_IND, &dtmf);
3047}
3048
3049static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3050{
3051 struct gsm48_hdr *gh = msgb_l3(msg);
3052 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3053 struct tlv_parsed tp;
3054 struct gsm_mncc modify;
3055
3056 memset(&modify, 0, sizeof(struct gsm_mncc));
3057 modify.callref = trans->callref;
3058 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3059 /* bearer capability */
3060 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3061 modify.fields |= MNCC_F_BEARER_CAP;
3062 decode_bearer_cap(&modify.bearer_cap,
3063 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3064 }
3065
3066 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3067
3068 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_IND, &modify);
3069}
3070
3071static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3072{
3073 struct gsm_mncc *modify = arg;
3074 struct msgb *msg = gsm48_msgb_alloc();
3075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3076
3077 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3078 msg->lchan = trans->lchan;
3079 gh->msg_type = GSM48_MT_CC_MODIFY;
3080
3081 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3082
3083 /* bearer capability */
3084 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3085
3086 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3087
3088 return gsm48_sendmsg(msg);
3089}
3090
3091static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3092{
3093 struct gsm48_hdr *gh = msgb_l3(msg);
3094 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3095 struct tlv_parsed tp;
3096 struct gsm_mncc modify;
3097
3098 gsm48_stop_cc_timer(trans);
3099
3100 memset(&modify, 0, sizeof(struct gsm_mncc));
3101 modify.callref = trans->callref;
3102 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3103 /* bearer capability */
3104 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3105 modify.fields |= MNCC_F_BEARER_CAP;
3106 decode_bearer_cap(&modify.bearer_cap,
3107 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3108 }
3109
3110 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3111
3112 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_CNF, &modify);
3113}
3114
3115static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3116{
3117 struct gsm_mncc *modify = arg;
3118 struct msgb *msg = gsm48_msgb_alloc();
3119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3120
3121 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3122 msg->lchan = trans->lchan;
3123 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3124
3125 /* bearer capability */
3126 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3127
3128 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3129
3130 return gsm48_sendmsg(msg);
3131}
3132
3133static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3134{
3135 struct gsm48_hdr *gh = msgb_l3(msg);
3136 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3137 struct tlv_parsed tp;
3138 struct gsm_mncc modify;
3139
3140 gsm48_stop_cc_timer(trans);
3141
3142 memset(&modify, 0, sizeof(struct gsm_mncc));
3143 modify.callref = trans->callref;
3144 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3145 /* bearer capability */
3146 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3147 modify.fields |= GSM48_IE_BEARER_CAP;
3148 decode_bearer_cap(&modify.bearer_cap,
3149 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3150 }
3151 /* cause */
3152 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3153 modify.fields |= MNCC_F_CAUSE;
3154 decode_cause(&modify.cause,
3155 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3156 }
3157
3158 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3159
3160 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_REJ, &modify);
3161}
3162
3163static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3164{
3165 struct gsm_mncc *modify = arg;
3166 struct msgb *msg = gsm48_msgb_alloc();
3167 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3168
3169 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3170 msg->lchan = trans->lchan;
3171 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3172
3173 /* bearer capability */
3174 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3175 /* cause */
3176 encode_cause(msg, 1, &modify->cause);
3177
3178 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3179
3180 return gsm48_sendmsg(msg);
3181}
3182
3183static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3184{
3185 struct gsm_mncc *notify = arg;
3186 struct msgb *msg = gsm48_msgb_alloc();
3187 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3188
3189 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3190 msg->lchan = trans->lchan;
3191 gh->msg_type = GSM48_MT_CC_NOTIFY;
3192
3193 /* notify */
3194 encode_notify(msg, notify->notify);
3195
3196 return gsm48_sendmsg(msg);
3197}
3198
3199static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3200{
3201 struct gsm48_hdr *gh = msgb_l3(msg);
3202 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3203// struct tlv_parsed tp;
3204 struct gsm_mncc notify;
3205
3206 memset(&notify, 0, sizeof(struct gsm_mncc));
3207 notify.callref = trans->callref;
3208// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3209 if (payload_len >= 1)
3210 decode_notify(&notify.notify, gh->data);
3211
3212 return mncc_recvmsg(trans->network, trans, MNCC_NOTIFY_IND, &notify);
3213}
3214
3215static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3216{
3217 struct gsm_mncc *user = arg;
3218 struct msgb *msg = gsm48_msgb_alloc();
3219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3220
3221 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3222 msg->lchan = trans->lchan;
3223 gh->msg_type = GSM48_MT_CC_USER_INFO;
3224
3225 /* user-user */
3226 if (user->fields & MNCC_F_USERUSER)
3227 encode_useruser(msg, 1, &user->useruser);
3228 /* more data */
3229 if (user->more)
3230 encode_more(msg);
3231
3232 return gsm48_sendmsg(msg);
3233}
3234
3235static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3236{
3237 struct gsm48_hdr *gh = msgb_l3(msg);
3238 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3239 struct tlv_parsed tp;
3240 struct gsm_mncc user;
3241
3242 memset(&user, 0, sizeof(struct gsm_mncc));
3243 user.callref = trans->callref;
3244 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3245 /* user-user */
3246 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3247 user.fields |= MNCC_F_USERUSER;
3248 decode_useruser(&user.useruser,
3249 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3250 }
3251 /* more data */
3252 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3253 user.more = 1;
3254
3255 return mncc_recvmsg(trans->network, trans, MNCC_USERINFO_IND, &user);
3256}
3257
3258static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3259{
3260 struct gsm_mncc *mode = arg;
3261
3262 return gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3263}
3264
3265static struct downstate {
3266 u_int32_t states;
3267 int type;
3268 int (*rout) (struct gsm_trans *trans, void *arg);
3269} downstatelist[] = {
3270 /* mobile originating call establishment */
3271 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3272 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3273 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3274 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3275 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) | SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.2 | 5.2.1.6 | 5.2.1.6 */
3276 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3277 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3278 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3279 /* mobile terminating call establishment */
3280 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3281 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3282 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3283 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3284 /* signalling during call */
3285 {SBIT(GSM_CSTATE_ACTIVE),
3286 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3287 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3288 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3289 {ALL_STATES,
3290 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3291 {ALL_STATES,
3292 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3293 {ALL_STATES,
3294 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3295 {SBIT(GSM_CSTATE_ACTIVE),
3296 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3297 {SBIT(GSM_CSTATE_ACTIVE),
3298 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3299 {SBIT(GSM_CSTATE_ACTIVE),
3300 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3301 {SBIT(GSM_CSTATE_ACTIVE),
3302 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3303 {SBIT(GSM_CSTATE_ACTIVE),
3304 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3305 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3306 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3307 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3308 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3309 {SBIT(GSM_CSTATE_ACTIVE),
3310 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3311 /* clearing */
3312 {SBIT(GSM_CSTATE_INITIATED),
3313 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3314 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3315 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3316 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3317 MNCC_REL_REQ, gsm48_cc_tx_release},
3318 /* special */
3319 {ALL_STATES,
3320 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3321};
3322
3323#define DOWNSLLEN \
3324 (sizeof(downstatelist) / sizeof(struct downstate))
3325
3326
3327int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3328{
3329 int i, j, k, l, rc = 0;
3330 struct gsm_trans *trans = NULL, *transt;
3331 struct gsm_subscriber *subscr;
3332 struct gsm_lchan *lchan = NULL, *lchant;
3333 struct gsm_bts *bts = NULL;
3334 struct gsm_bts_trx *trx;
3335 struct gsm_bts_trx_ts *ts;
3336 struct gsm_mncc *data = arg, rel;
3337
3338 /* handle special messages */
3339 switch(msg_type) {
3340 case MNCC_BRIDGE:
3341 return tch_bridge(net, arg);
3342 case MNCC_FRAME_DROP:
3343 return tch_recv(net, arg, 0);
3344 case MNCC_FRAME_RECV:
3345 return tch_recv(net, arg, 1);
3346 case GSM_TRAU_FRAME:
3347 return tch_frame(net, arg);
3348 }
3349
3350 memset(&rel, 0, sizeof(struct gsm_mncc));
3351 rel.callref = data->callref;
3352
3353 /* Find callref */
Harald Welteaa0b29c2009-07-23 18:56:43 +02003354 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355
3356 /* Callref unknown */
3357 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003358 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003359 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3360 "Received '%s' from MNCC with "
3361 "unknown callref %d\n", data->called.number,
3362 get_mncc_name(msg_type), data->callref);
3363 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003364 return mncc_release_ind(net, NULL, data->callref,
3365 GSM48_CAUSE_LOC_PRN_S_LU,
3366 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003367 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003368 if (!data->called.number[0] && !data->imsi[0]) {
3369 DEBUGP(DCC, "(bts - trx - ts - ti) "
3370 "Received '%s' from MNCC with "
3371 "no number or IMSI\n", get_mncc_name(msg_type));
3372 /* Invalid number */
3373 return mncc_release_ind(net, NULL, data->callref,
3374 GSM48_CAUSE_LOC_PRN_S_LU,
3375 GSM48_CC_CAUSE_INV_NR_FORMAT);
3376 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003377 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003378 if (data->called.number[0])
3379 subscr = subscr_get_by_extension(data->called.number);
3380 else
3381 subscr = subscr_get_by_imsi(data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003382 /* If subscriber is not found */
3383 if (!subscr) {
3384 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3385 "Received '%s' from MNCC with "
3386 "unknown subscriber %s\n", data->called.number,
3387 get_mncc_name(msg_type), data->called.number);
3388 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003389 return mncc_release_ind(net, NULL, data->callref,
3390 GSM48_CAUSE_LOC_PRN_S_LU,
3391 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003392 }
3393 /* If subscriber is not "attached" */
3394 if (!subscr->lac) {
3395 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3396 "Received '%s' from MNCC with "
3397 "detached subscriber %s\n", data->called.number,
3398 get_mncc_name(msg_type), data->called.number);
3399 subscr_put(subscr);
3400 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003401 return mncc_release_ind(net, NULL, data->callref,
3402 GSM48_CAUSE_LOC_PRN_S_LU,
3403 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003404 }
3405 /* Create transaction */
Harald Welteaa0b29c2009-07-23 18:56:43 +02003406 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3407 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003408 DEBUGP(DCC, "No memory for trans.\n");
3409 subscr_put(subscr);
3410 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003411 mncc_release_ind(net, NULL, data->callref,
3412 GSM48_CAUSE_LOC_PRN_S_LU,
3413 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003414 return -ENOMEM;
3415 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003416 /* Find lchan */
3417 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +02003418 bts = gsm_bts_num(net, i);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003419 for (j = 0; j < bts->num_trx; j++) {
Harald Weltee441d9c2009-06-21 16:17:15 +02003420 trx = gsm_bts_trx_num(bts, j);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003421 for (k = 0; k < TRX_NR_TS; k++) {
3422 ts = &trx->ts[k];
3423 for (l = 0; l < TS_MAX_LCHAN; l++) {
3424 lchant = &ts->lchan[l];
3425 if (lchant->subscr == subscr) {
3426 lchan = lchant;
3427 break;
3428 }
3429 }
3430 }
3431 }
3432 }
3433
3434 /* If subscriber has no lchan */
3435 if (!lchan) {
3436 /* find transaction with this subscriber already paging */
3437 llist_for_each_entry(transt, &net->trans_list, entry) {
3438 /* Transaction of our lchan? */
3439 if (transt == trans ||
3440 transt->subscr != subscr)
3441 continue;
3442 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3443 "Received '%s' from MNCC with "
3444 "unallocated channel, paging already "
3445 "started.\n", bts->nr,
3446 data->called.number,
3447 get_mncc_name(msg_type));
3448 return 0;
3449 }
3450 /* store setup informations until paging was successfull */
Harald Welteaa0b29c2009-07-23 18:56:43 +02003451 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003452 /* start paging subscriber on all BTS with her location */
3453 subscr->net = net;
3454 bts = NULL;
3455 do {
3456 bts = gsm_bts_by_lac(net, subscr->lac, bts);
3457 if (!bts)
3458 break;
3459 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3460 "Received '%s' from MNCC with "
3461 "unallocated channel, paging.\n",
3462 bts->nr, data->called.number,
3463 get_mncc_name(msg_type));
3464 /* Trigger paging */
Harald Welte92f70c52009-06-12 01:54:08 +08003465 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003466 setup_trig_pag_evt, subscr);
3467 } while (1);
3468 return 0;
3469 }
3470 /* Assign lchan */
3471 trans->lchan = lchan;
3472 use_lchan(lchan);
3473 }
3474 lchan = trans->lchan;
3475
3476 /* if paging did not respond yet */
3477 if (!lchan) {
3478 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3479 "Received '%s' from MNCC in paging state\n",
3480 (trans->subscr)?(trans->subscr->extension):"-",
3481 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003482 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3483 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003484 if (msg_type == MNCC_REL_REQ)
3485 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3486 else
3487 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3488 trans->callref = 0;
Harald Welteaa0b29c2009-07-23 18:56:43 +02003489 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003490 return rc;
3491 }
3492
3493 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3494 "Received '%s' from MNCC in state %d (%s)\n",
3495 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3496 trans->transaction_id,
3497 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Welteaa0b29c2009-07-23 18:56:43 +02003498 get_mncc_name(msg_type), trans->cc.state,
3499 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003500
3501 /* Find function for current state and message */
3502 for (i = 0; i < DOWNSLLEN; i++)
3503 if ((msg_type == downstatelist[i].type)
Harald Welteaa0b29c2009-07-23 18:56:43 +02003504 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003505 break;
3506 if (i == DOWNSLLEN) {
3507 DEBUGP(DCC, "Message unhandled at this state.\n");
3508 return 0;
3509 }
3510
3511 rc = downstatelist[i].rout(trans, arg);
3512
3513 return rc;
3514}
3515
3516
3517static struct datastate {
3518 u_int32_t states;
3519 int type;
3520 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3521} datastatelist[] = {
3522 /* mobile originating call establishment */
3523 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3524 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3525 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3526 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3527 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3528 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3529 /* mobile terminating call establishment */
3530 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3531 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3532 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3533 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3534 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
3535 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3536 /* signalling during call */
3537 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3538 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3539 {SBIT(GSM_CSTATE_ACTIVE),
3540 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3541 {ALL_STATES,
3542 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3543 {ALL_STATES,
3544 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3545 {ALL_STATES,
3546 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3547 {SBIT(GSM_CSTATE_ACTIVE),
3548 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3549 {SBIT(GSM_CSTATE_ACTIVE),
3550 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3551 {SBIT(GSM_CSTATE_ACTIVE),
3552 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3553 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3554 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3555 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3556 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3557 {SBIT(GSM_CSTATE_ACTIVE),
3558 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3559 /* clearing */
3560 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3561 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3562 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3563 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3564 {ALL_STATES, /* 5.4.3.4 */
3565 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3566};
3567
3568#define DATASLLEN \
3569 (sizeof(datastatelist) / sizeof(struct datastate))
3570
Harald Welte4bc90a12008-12-27 16:32:52 +00003571static int gsm0408_rcv_cc(struct msgb *msg)
3572{
3573 struct gsm48_hdr *gh = msgb_l3(msg);
3574 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003575 u_int8_t transaction_id = (gh->proto_discr & 0xf0) ^ 0x80; /* flip */
3576 struct gsm_lchan *lchan = msg->lchan;
Harald Welteaa0b29c2009-07-23 18:56:43 +02003577 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003578 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003579
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580 if (msg_type & 0x80) {
3581 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3582 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003583 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003584
3585 /* Find transaction */
Harald Welteaa0b29c2009-07-23 18:56:43 +02003586 trans = trans_find_by_id(lchan, transaction_id);
3587
Harald Welte4bfdfe72009-06-10 23:11:52 +08003588 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3589 "Received '%s' from MS in state %d (%s)\n",
3590 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3591 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Welteaa0b29c2009-07-23 18:56:43 +02003592 cc_msg_names[msg_type], trans?(trans->cc.state):0,
3593 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003594
3595 /* Create transaction */
3596 if (!trans) {
3597 DEBUGP(DCC, "Unknown transaction ID %02x, "
3598 "creating new trans.\n", transaction_id);
3599 /* Create transaction */
Harald Welteaa0b29c2009-07-23 18:56:43 +02003600 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3601 transaction_id, new_callref++);
3602 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003603 DEBUGP(DCC, "No memory for trans.\n");
3604 rc = gsm48_tx_simple(msg->lchan,
3605 GSM48_PDISC_CC | transaction_id,
3606 GSM48_MT_CC_RELEASE_COMPL);
3607 return -ENOMEM;
3608 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003609 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003610 trans->lchan = lchan;
3611 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003612 }
3613
3614 /* find function for current state and message */
3615 for (i = 0; i < DATASLLEN; i++)
3616 if ((msg_type == datastatelist[i].type)
Harald Welteaa0b29c2009-07-23 18:56:43 +02003617 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003618 break;
3619 if (i == DATASLLEN) {
3620 DEBUGP(DCC, "Message unhandled at this state.\n");
3621 return 0;
3622 }
3623
3624 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003625
3626 return rc;
3627}
3628
Harald Welte52b1f982008-12-23 20:25:15 +00003629/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
3630int gsm0408_rcvmsg(struct msgb *msg)
3631{
3632 struct gsm48_hdr *gh = msgb_l3(msg);
3633 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003634 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003635
3636 switch (pdisc) {
3637 case GSM48_PDISC_CC:
3638 rc = gsm0408_rcv_cc(msg);
3639 break;
3640 case GSM48_PDISC_MM:
3641 rc = gsm0408_rcv_mm(msg);
3642 break;
3643 case GSM48_PDISC_RR:
3644 rc = gsm0408_rcv_rr(msg);
3645 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003646 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00003647 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003648 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003649 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003650 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00003651 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
3652 pdisc);
3653 break;
3654 default:
3655 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
3656 pdisc);
3657 break;
3658 }
3659
3660 return rc;
3661}
Harald Welte8470bf22008-12-25 23:28:35 +00003662
Harald Welte8470bf22008-12-25 23:28:35 +00003663/* Section 9.1.8 / Table 9.9 */
3664struct chreq {
3665 u_int8_t val;
3666 u_int8_t mask;
3667 enum chreq_type type;
3668};
3669
3670/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
3671static const struct chreq chreq_type_neci1[] = {
3672 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3673 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
3674 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
3675 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
3676 { 0xe0, 0xe0, CHREQ_T_SDCCH },
3677 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
3678 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3679 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
3680 { 0x10, 0xf0, CHREQ_T_SDCCH },
3681 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3682 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3683 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3684};
3685
3686/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
3687static const struct chreq chreq_type_neci0[] = {
3688 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3689 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
3690 { 0xe0, 0xe0, CHREQ_T_TCH_F },
3691 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3692 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
3693 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3694 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3695 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3696};
3697
3698static const enum gsm_chan_t ctype_by_chreq[] = {
3699 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
3700 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
3701 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
3702 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
3703 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
3704 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
3705 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3706 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3707 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
3708 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
3709 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
3710 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
3711};
3712
Harald Weltee14a57c2008-12-29 04:08:28 +00003713static const enum gsm_chreq_reason_t reason_by_chreq[] = {
3714 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
3715 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
3716 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
3717 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
3718 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
3719 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
3720 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3721 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3722 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
3723 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
3724 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
3725 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
3726};
3727
Harald Welte8470bf22008-12-25 23:28:35 +00003728enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3729{
3730 int i;
3731 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3732
3733 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3734 const struct chreq *chr = &chreq_type_neci0[i];
3735 if ((ra & chr->mask) == chr->val)
3736 return ctype_by_chreq[chr->type];
3737 }
3738 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
3739 return GSM_LCHAN_SDCCH;
3740}
Harald Weltee14a57c2008-12-29 04:08:28 +00003741
3742enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3743{
3744 int i;
3745 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3746
3747 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3748 const struct chreq *chr = &chreq_type_neci0[i];
3749 if ((ra & chr->mask) == chr->val)
3750 return reason_by_chreq[chr->type];
3751 }
3752 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
3753 return GSM_CHREQ_REASON_OTHER;
3754}
Harald Welte4bfdfe72009-06-10 23:11:52 +08003755
3756/* dequeue messages to layer 4 */
3757int bsc_upqueue(struct gsm_network *net)
3758{
3759 struct gsm_mncc *mncc;
3760 struct msgb *msg;
3761 int work = 0;
3762
3763 if (net)
3764 while ((msg = msgb_dequeue(&net->upqueue))) {
3765 mncc = (struct gsm_mncc *)msg->data;
3766 if (net->mncc_recv)
3767 net->mncc_recv(net, mncc->msg_type, mncc);
3768 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003769 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003770 }
3771
3772 return work;
3773}
Harald Welteaa0b29c2009-07-23 18:56:43 +02003774