blob: e225bf3a1e42ba0e6d4c32369701c2c1df08ca7f [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>
Daniel Willmanneea93372009-08-13 03:42:07 +020038#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000040#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_04_08.h>
42#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000046#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020049#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020050#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020051#include <openbsc/ussd.h>
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 Welte (local)d19e58b2009-08-15 02:30:58 +020057void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020058
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020059extern int ipacc_rtp_direct;
Harald Welte805f6442009-07-28 18:25:29 +020060
Harald Welte09e38af2009-02-16 22:52:23 +000061static const struct tlv_definition rsl_att_tlvdef = {
62 .def = {
63 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
65 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
66 [GSM48_IE_UTC] = { TLV_TYPE_TV },
67 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
68 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
69
70 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
71 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
72 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
73 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
74 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
75 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
76 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080077 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000078 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
79 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080080 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000082 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
85 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
87 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
88 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
90 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
91 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
92 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
94 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
95 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080096 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
97 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000098 /* FIXME: more elements */
99 },
100};
Harald Weltecf5b3592009-05-01 18:28:42 +0000101
102static const char *rr_cause_names[] = {
103 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
104 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
105 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
106 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
107 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
108 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
109 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
110 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
111 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
112 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
113 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
114 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
115 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
116 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
117 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
118 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
119 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
120};
121
Harald Welte4bfdfe72009-06-10 23:11:52 +0800122static const char *cc_state_names[] = {
123 "NULL",
124 "INITIATED",
125 "illegal state 2",
126 "MO_CALL_PROC",
127 "CALL_DELIVERED",
128 "illegal state 5",
129 "CALL_PRESENT",
130 "CALL_RECEIVED",
131 "CONNECT_REQUEST",
132 "MO_TERM_CALL_CONF",
133 "ACTIVE",
134 "DISCONNECT_REQ",
135 "DISCONNECT_IND",
136 "illegal state 13",
137 "illegal state 14",
138 "illegal state 15",
139 "illegal state 16",
140 "illegal state 17",
141 "illegal state 18",
142 "RELEASE_REQ",
143 "illegal state 20",
144 "illegal state 21",
145 "illegal state 22",
146 "illegal state 23",
147 "illegal state 24",
148 "illegal state 25",
149 "MO_ORIG_MODIFY",
150 "MO_TERM_MODIFY",
151 "CONNECT_IND",
152 "illegal state 29",
153 "illegal state 30",
154 "illegal state 31",
155};
156
Harald Weltecf5b3592009-05-01 18:28:42 +0000157static char strbuf[64];
158
159static const char *rr_cause_name(u_int8_t cause)
160{
161 if (cause < ARRAY_SIZE(rr_cause_names) &&
162 rr_cause_names[cause])
163 return rr_cause_names[cause];
164
165 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
166 return strbuf;
167}
168
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100169int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +0000170{
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100171 struct gsm48_hdr *gh = msgb_l3(msg);
172 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
173 u_int8_t *data = gh->data;
174
175 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
176 return -EINVAL;
Harald Weltef7c43522009-06-09 20:24:21 +0000177
178 if (data[0] & 0x80)
179 rep->flags |= MEAS_REP_F_BA1;
180 if (data[0] & 0x40)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100181 rep->flags |= MEAS_REP_F_UL_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200182 if ((data[1] & 0x40) == 0x00)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100183 rep->flags |= MEAS_REP_F_DL_VALID;
Harald Weltef7c43522009-06-09 20:24:21 +0000184
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100185 rep->dl.full.rx_lev = data[0] & 0x3f;
186 rep->dl.sub.rx_lev = data[1] & 0x3f;
187 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
188 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
189
Harald Weltef7c43522009-06-09 20:24:21 +0000190 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
191 if (rep->num_cell < 1)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100192 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000193
194 /* an encoding nightmare in perfection */
195
196 rep->cell[0].rxlev = data[4] & 0x3f;
197 rep->cell[0].bcch_freq = data[5] >> 2;
198 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
199 if (rep->num_cell < 2)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100200 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000201
202 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
203 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
204 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
205 if (rep->num_cell < 3)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100206 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000207
208 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
209 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
210 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
211 if (rep->num_cell < 4)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100212 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000213
214 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
215 rep->cell[3].bcch_freq = data[11] & 0x1f;
216 rep->cell[3].bsic = data[12] >> 2;
217 if (rep->num_cell < 5)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100218 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000219
220 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
221 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
222 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
223 if (rep->num_cell < 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100224 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000225
226 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
227 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
228 rep->cell[5].bsic = data[16] & 0x3f;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100229
230 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000231}
232
Holger Freytherd51524f2009-06-09 08:27:07 +0000233int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000234static int gsm48_tx_simple(struct gsm_lchan *lchan,
235 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000236static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000237
Harald Welte52b1f982008-12-23 20:25:15 +0000238struct gsm_lai {
239 u_int16_t mcc;
240 u_int16_t mnc;
241 u_int16_t lac;
242};
243
Holger Freythere97f7fb2008-12-31 18:52:11 +0000244static int reject_cause = 0;
245void gsm0408_set_reject_cause(int cause)
246{
247 reject_cause = cause;
248}
249
Harald Welte4bfdfe72009-06-10 23:11:52 +0800250static u_int32_t new_callref = 0x80000001;
251
Holger Freyther73487a22008-12-31 18:53:57 +0000252static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
253 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000254{
255 if (!subscriber)
256 return 0;
257
Holger Freyther73487a22008-12-31 18:53:57 +0000258 /*
259 * Do not send accept yet as more information should arrive. Some
260 * phones will not send us the information and we will have to check
261 * what we want to do with that.
262 */
263 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
264 return 0;
265
Jan Luebbe06513f22009-08-12 12:48:00 +0200266 switch (subscriber->net->auth_policy) {
267 case GSM_AUTH_POLICY_CLOSED:
268 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200269 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200270 if (subscriber->authorized)
271 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200272 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200273 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000274 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200275 default:
276 return 0;
277 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000278}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000279
Holger Freyther73487a22008-12-31 18:53:57 +0000280static void release_loc_updating_req(struct gsm_lchan *lchan)
281{
Harald Welte179f0642008-12-31 23:59:18 +0000282 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000283 return;
284
Harald Welteff117a82009-05-23 05:22:08 +0000285 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200286 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000287 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000288 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000289}
290
291static void allocate_loc_updating_req(struct gsm_lchan *lchan)
292{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000293 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000294 release_loc_updating_req(lchan);
295
Harald Welte470ec292009-06-26 20:25:23 +0200296 lchan->loc_operation = talloc_zero(tall_locop_ctx,
297 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000298}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000299
Holger Freytherd51524f2009-06-09 08:27:07 +0000300static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
301{
Holger Freytherd51524f2009-06-09 08:27:07 +0000302 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200303 int rc;
304
Holger Freytherd51524f2009-06-09 08:27:07 +0000305 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000306 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200307 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welteee5ad162009-08-09 19:07:00 +0200308 /* call subscr_update after putting the loc_upd_acc
309 * in the transmit queue, since S_SUBSCR_ATTACHED might
310 * trigger further action like SMS delivery */
311 subscr_update(lchan->subscr, msg->trx->bts,
312 GSM_SUBSCRIBER_UPDATE_ATTACHED);
313 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000314 }
315
316 return 0;
317}
318
Holger Freyther7c19f742009-06-06 13:54:35 +0000319static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
320 void *handler_data, void *signal_data)
321{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800322 struct gsm_trans *trans, *temp;
323
Holger Freyther7c19f742009-06-06 13:54:35 +0000324 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
325 return 0;
326
327 /*
328 * Cancel any outstanding location updating request
329 * operation taking place on the lchan.
330 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200331 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200332 if (!lchan)
333 return 0;
334
Holger Freyther7c19f742009-06-06 13:54:35 +0000335 release_loc_updating_req(lchan);
336
Harald Welte4bfdfe72009-06-10 23:11:52 +0800337 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200338 /* FIXME: this is not neccessarily the right thing to do, we should
339 * only set trans->lchan to NULL and wait for another lchan to be
340 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800341 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
342 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200343 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800344 }
345
Holger Freyther7c19f742009-06-06 13:54:35 +0000346 return 0;
347}
348
Harald Welte09e38af2009-02-16 22:52:23 +0000349static const char bcd_num_digits[] = {
350 '0', '1', '2', '3', '4', '5', '6', '7',
351 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
352};
353
Harald Welte0c389302009-06-10 12:08:54 +0800354/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
355int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
356 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000357{
358 u_int8_t in_len = bcd_lv[0];
359 int i;
360
Harald Welte0c389302009-06-10 12:08:54 +0800361 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000362 /* lower nibble */
363 output_len--;
364 if (output_len <= 1)
365 break;
366 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
367
368 /* higher nibble */
369 output_len--;
370 if (output_len <= 1)
371 break;
372 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
373 }
374 if (output_len >= 1)
375 *output++ = '\0';
376
Harald Welte0c389302009-06-10 12:08:54 +0800377 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000378}
379
380/* convert a single ASCII character to call-control BCD */
381static int asc_to_bcd(const char asc)
382{
383 int i;
384
385 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
386 if (bcd_num_digits[i] == asc)
387 return i;
388 }
389 return -EINVAL;
390}
391
Harald Welte0c389302009-06-10 12:08:54 +0800392/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000393int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800394 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000395{
396 int in_len = strlen(input);
397 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800398 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000399
400 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800401 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000402 if (in_len % 2)
403 bcd_lv[0]++;
404
Harald Welte0c389302009-06-10 12:08:54 +0800405 if (bcd_lv[0] > max_len)
406 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000407
408 for (i = 0; i < in_len; i++) {
409 int rc = asc_to_bcd(input[i]);
410 if (rc < 0)
411 return rc;
412 if (i % 2 == 0)
413 *bcd_cur = rc;
414 else
415 *bcd_cur++ |= (rc << 4);
416 }
417 /* append padding nibble in case of odd length */
418 if (i % 2)
419 *bcd_cur++ |= 0xf0;
420
421 /* return how many bytes we used */
422 return (bcd_cur - bcd_lv);
423}
424
Harald Welte0c389302009-06-10 12:08:54 +0800425/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800426static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800427 const u_int8_t *lv)
428{
429 u_int8_t in_len = lv[0];
430 int i, s;
431
432 if (in_len < 1)
433 return -EINVAL;
434
Harald Welte4bfdfe72009-06-10 23:11:52 +0800435 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800436
437 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800438 bcap->transfer = lv[1] & 0x07;
439 bcap->mode = (lv[1] & 0x08) >> 3;
440 bcap->coding = (lv[1] & 0x10) >> 4;
441 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800442
443 i = 1;
444 s = 0;
445 while(!(lv[i] & 0x80)) {
446 i++; /* octet 3a etc */
447 if (in_len < i)
448 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800449 bcap->speech_ver[s++] = lv[i] & 0x0f;
450 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800451 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800452 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800453 if (s == 7) /* maximum speech versions + end of list */
454 return 0;
455 }
456
457 return 0;
458}
459
460/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800461static int encode_bearer_cap(struct msgb *msg, int lv_only,
462 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800463{
464 u_int8_t lv[32 + 1];
465 int i, s;
466
Harald Welte4bfdfe72009-06-10 23:11:52 +0800467 lv[1] = bcap->transfer;
468 lv[1] |= bcap->mode << 3;
469 lv[1] |= bcap->coding << 4;
470 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800471
472 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800473 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800474 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800475 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800476 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800477 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800478 }
479 lv[i] |= 0x80; /* last IE of octet 3 etc */
480
481 lv[0] = i;
482 if (lv_only)
483 msgb_lv_put(msg, lv[0], lv+1);
484 else
485 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
486
487 return 0;
488}
489
490/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800491static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800492{
493 u_int8_t in_len = lv[0];
494
495 if (in_len < 1)
496 return -EINVAL;
497
498 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800499 ccap->dtmf = lv[1] & 0x01;
500 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800501
502 return 0;
503}
504
505/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800506static int decode_called(struct gsm_mncc_number *called,
507 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800508{
509 u_int8_t in_len = lv[0];
510
511 if (in_len < 1)
512 return -EINVAL;
513
514 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800515 called->plan = lv[1] & 0x0f;
516 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800517
518 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800520
521 return 0;
522}
523
524/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800525static int encode_called(struct msgb *msg,
526 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800527{
528 u_int8_t lv[18];
529 int ret;
530
531 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800532 lv[1] = called->plan;
533 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800534
535 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800537 if (ret < 0)
538 return ret;
539
540 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
541
542 return 0;
543}
544
545/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800546static int encode_callerid(struct msgb *msg, int ie,
547 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800548{
549 u_int8_t lv[13];
550 int h_len = 1;
551 int ret;
552
553 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554 lv[1] = callerid->plan;
555 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800556
Harald Welte4bfdfe72009-06-10 23:11:52 +0800557 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800558 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800559 lv[2] = callerid->screen;
560 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800561 lv[2] |= 0x80;
562 h_len++;
563 } else
564 lv[1] |= 0x80;
565
566 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800567 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800568 if (ret < 0)
569 return ret;
570
571 msgb_tlv_put(msg, ie, lv[0], lv+1);
572
573 return 0;
574}
575
576/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800577static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800578 const u_int8_t *lv)
579{
580 u_int8_t in_len = lv[0];
581 int i;
582
583 if (in_len < 2)
584 return -EINVAL;
585
Harald Welte4bfdfe72009-06-10 23:11:52 +0800586 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800587
588 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 cause->location = lv[1] & 0x0f;
590 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800591
592 i = 1;
593 if (!(lv[i] & 0x80)) {
594 i++; /* octet 3a */
595 if (in_len < i+1)
596 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800597 cause->rec = 1;
598 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800599
600 }
601 i++;
602
603 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800604 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800605 i++;
606
607 if (in_len < i) /* no diag */
608 return 0;
609
610 if (in_len - (i-1) > 32) /* maximum 32 octets */
611 return 0;
612
613 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800614 memcpy(cause->diag, lv + i, in_len - (i-1));
615 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800616
617 return 0;
618}
619
620/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621static int encode_cause(struct msgb *msg, int lv_only,
622 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800623{
624 u_int8_t lv[32+4];
625 int i;
626
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800628 return -EINVAL;
629
630 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800631 lv[1] = cause->location;
632 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800633
634 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800636 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800638 }
639 lv[i] |= 0x80; /* end of octet 3 */
640
641 /* octet 4 */
642 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800643 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800644
645 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800646 if (cause->diag_len) {
647 memcpy(lv + i, cause->diag, cause->diag_len);
648 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800649 }
650
651 lv[0] = i;
652 if (lv_only)
653 msgb_lv_put(msg, lv[0], lv+1);
654 else
655 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
656
657 return 0;
658}
659
660/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661static int encode_calling(struct msgb *msg,
662 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800663{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800664 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800665}
666
667/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800668static int encode_connected(struct msgb *msg,
669 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800670{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800671 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800672}
673
674/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800675static int encode_redirecting(struct msgb *msg,
676 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800677{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800678 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800679}
680
681/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800683 const u_int8_t *lv)
684{
685 u_int8_t in_len = lv[0];
686
687 if (in_len < 1)
688 return -EINVAL;
689
Harald Welte4bfdfe72009-06-10 23:11:52 +0800690 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800691 return -EINVAL;
692
Harald Welte4bfdfe72009-06-10 23:11:52 +0800693 memcpy(facility->info, lv+1, in_len);
694 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800695
696 return 0;
697}
698
699/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700static int encode_facility(struct msgb *msg, int lv_only,
701 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800702{
703 u_int8_t lv[GSM_MAX_FACILITY + 1];
704
Harald Welte4bfdfe72009-06-10 23:11:52 +0800705 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800706 return -EINVAL;
707
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 memcpy(lv+1, facility->info, facility->len);
709 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800710 if (lv_only)
711 msgb_lv_put(msg, lv[0], lv+1);
712 else
713 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
714
715 return 0;
716}
717
718/* decode 'notify' */
719static int decode_notify(int *notify, const u_int8_t *v)
720{
721 *notify = v[0] & 0x7f;
722
723 return 0;
724}
725
726/* encode 'notify' */
727static int encode_notify(struct msgb *msg, int notify)
728{
729 msgb_v_put(msg, notify | 0x80);
730
731 return 0;
732}
733
734/* encode 'signal' */
735static int encode_signal(struct msgb *msg, int signal)
736{
737 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
738
739 return 0;
740}
741
742/* decode 'keypad' */
743static int decode_keypad(int *keypad, const u_int8_t *lv)
744{
745 u_int8_t in_len = lv[0];
746
747 if (in_len < 1)
748 return -EINVAL;
749
750 *keypad = lv[1] & 0x7f;
751
752 return 0;
753}
754
755/* encode 'keypad' */
756static int encode_keypad(struct msgb *msg, int keypad)
757{
758 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
759
760 return 0;
761}
762
763/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800764static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800765 const u_int8_t *lv)
766{
767 u_int8_t in_len = lv[0];
768
769 if (in_len < 2)
770 return -EINVAL;
771
Harald Welte4bfdfe72009-06-10 23:11:52 +0800772 progress->coding = (lv[1] & 0x60) >> 5;
773 progress->location = lv[1] & 0x0f;
774 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800775
776 return 0;
777}
778
779/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800780static int encode_progress(struct msgb *msg, int lv_only,
781 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800782{
783 u_int8_t lv[3];
784
785 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800786 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
787 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800788 if (lv_only)
789 msgb_lv_put(msg, lv[0], lv+1);
790 else
791 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
792
793 return 0;
794}
795
796/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800797static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800798 const u_int8_t *lv)
799{
800 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800801 char *info = uu->info;
802 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800803 int i;
804
805 if (in_len < 1)
806 return -EINVAL;
807
Harald Welte4bfdfe72009-06-10 23:11:52 +0800808 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800809
810 for (i = 2; i <= in_len; i++) {
811 info_len--;
812 if (info_len <= 1)
813 break;
814 *info++ = lv[i];
815 }
816 if (info_len >= 1)
817 *info++ = '\0';
818
819 return 0;
820}
821
822/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823static int encode_useruser(struct msgb *msg, int lv_only,
824 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800825{
826 u_int8_t lv[GSM_MAX_USERUSER + 2];
827
Harald Welte4bfdfe72009-06-10 23:11:52 +0800828 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800829 return -EINVAL;
830
Harald Welte4bfdfe72009-06-10 23:11:52 +0800831 lv[0] = 1 + strlen(uu->info);
832 lv[1] = uu->proto;
833 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800834 if (lv_only)
835 msgb_lv_put(msg, lv[0], lv+1);
836 else
837 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
838
839 return 0;
840}
841
842/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800844 const u_int8_t *lv)
845{
846 u_int8_t in_len = lv[0];
847
Harald Welte4bfdfe72009-06-10 23:11:52 +0800848 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800849 return -EINVAL;
850
Harald Welte4bfdfe72009-06-10 23:11:52 +0800851 memcpy(ssv->info, lv + 1, in_len);
852 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800853
854 return 0;
855}
856
857/* encode 'more data' */
858static int encode_more(struct msgb *msg)
859{
860 u_int8_t *ie;
861
862 ie = msgb_put(msg, 1);
863 ie[0] = GSM48_IE_MORE_DATA;
864
865 return 0;
866}
867
Holger Freyther429e7762008-12-30 13:28:30 +0000868/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000869int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000870{
Harald Welte8470bf22008-12-25 23:28:35 +0000871 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000872 struct gsm48_hdr *gh;
873
Harald Welte8470bf22008-12-25 23:28:35 +0000874 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000875
876 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
877 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000878 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000879 gh->data[0] = cause;
880
Harald Weltedb253af2008-12-30 17:56:55 +0000881 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
882
Harald Welte39e2ead2009-07-23 21:13:03 +0200883 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000884}
885
886/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000887int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000888{
Harald Welte8470bf22008-12-25 23:28:35 +0000889 struct gsm_bts *bts = lchan->ts->trx->bts;
890 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000891 struct gsm48_hdr *gh;
892 struct gsm48_loc_area_id *lai;
893 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000894 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000895
Harald Welte8470bf22008-12-25 23:28:35 +0000896 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000897
898 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
899 gh->proto_discr = GSM48_PDISC_MM;
900 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
901
902 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000903 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000904 bts->network->network_code, bts->location_area_code);
905
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200906 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200907 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000908
909 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
910
Harald Welte39e2ead2009-07-23 21:13:03 +0200911 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000912
Harald Welteee5ad162009-08-09 19:07:00 +0200913 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000914 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000915
Holger Freyther07cc8d82008-12-29 06:23:46 +0000916 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000917}
918
Harald Weltebf5e8df2009-02-03 12:59:45 +0000919/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000920static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
921{
922 struct msgb *msg = gsm48_msgb_alloc();
923 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000924
Harald Welte231ad4f2008-12-27 11:15:38 +0000925 msg->lchan = lchan;
926
927 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
928 gh->proto_discr = GSM48_PDISC_MM;
929 gh->msg_type = GSM48_MT_MM_ID_REQ;
930 gh->data[0] = id_type;
931
Harald Welte39e2ead2009-07-23 21:13:03 +0200932 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000933}
934
Harald Welte231ad4f2008-12-27 11:15:38 +0000935
Harald Weltebf5e8df2009-02-03 12:59:45 +0000936/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000937static int mm_rx_id_resp(struct msgb *msg)
938{
939 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000940 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200941 struct gsm_bts *bts = lchan->ts->trx->bts;
942 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000943 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200944 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000945
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200946 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000947 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000948 mi_type, mi_string);
949
Harald Welte75a983f2008-12-27 21:34:06 +0000950 switch (mi_type) {
951 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200952 /* look up subscriber based on IMSI, create if not found */
953 if (!lchan->subscr) {
954 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200955 if (!lchan->subscr)
956 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200957 }
Holger Freyther73487a22008-12-31 18:53:57 +0000958 if (lchan->loc_operation)
959 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000960 break;
961 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000962 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000963 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200964 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000965 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200966 db_sync_equipment(&lchan->subscr->equipment);
967 }
Holger Freyther73487a22008-12-31 18:53:57 +0000968 if (lchan->loc_operation)
969 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000970 break;
971 }
Holger Freyther73487a22008-12-31 18:53:57 +0000972
973 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000974 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000975}
976
Harald Welte255539c2008-12-28 02:26:27 +0000977
978static void loc_upd_rej_cb(void *data)
979{
980 struct gsm_lchan *lchan = data;
981
Holger Freyther73487a22008-12-31 18:53:57 +0000982 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000983 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000984 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000985}
986
Holger Freytherb7193e42008-12-29 17:44:08 +0000987static void schedule_reject(struct gsm_lchan *lchan)
988{
Holger Freyther73487a22008-12-31 18:53:57 +0000989 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
990 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000991 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000992}
993
Harald Welte2a139372009-02-22 21:14:55 +0000994static const char *lupd_name(u_int8_t type)
995{
996 switch (type) {
997 case GSM48_LUPD_NORMAL:
998 return "NORMAL";
999 case GSM48_LUPD_PERIODIC:
1000 return "PEROIDOC";
1001 case GSM48_LUPD_IMSI_ATT:
1002 return "IMSI ATTACH";
1003 default:
1004 return "UNKNOWN";
1005 }
1006}
1007
Harald Weltebf5e8df2009-02-03 12:59:45 +00001008/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001009static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001010{
Harald Welte8470bf22008-12-25 23:28:35 +00001011 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001012 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001013 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001014 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001015 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001016 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001017 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001018 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001019
Harald Welte8470bf22008-12-25 23:28:35 +00001020 lu = (struct gsm48_loc_upd_req *) gh->data;
1021
1022 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001023
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001024 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001025
Harald Weltea0368542009-06-27 02:58:43 +02001026 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001027 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001028
Holger Freythereaf04692009-06-06 13:54:44 +00001029 /*
1030 * Pseudo Spoof detection: Just drop a second/concurrent
1031 * location updating request.
1032 */
1033 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001034 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001035 lchan->loc_operation);
1036 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1037 return 0;
1038 }
1039
Holger Freyther73487a22008-12-31 18:53:57 +00001040 allocate_loc_updating_req(lchan);
1041
Harald Welte52b1f982008-12-23 20:25:15 +00001042 switch (mi_type) {
1043 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001044 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001045 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001046 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001047 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001048
Jan Luebbe370b41d2009-08-12 10:19:34 +02001049 /* look up subscriber based on IMSI, create if not found */
1050 subscr = subscr_get_by_imsi(bts->network, mi_string);
1051 if (!subscr) {
1052 subscr = db_create_subscriber(bts->network, mi_string);
1053 }
Harald Welte4b634542008-12-27 01:55:51 +00001054 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001055 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001056 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001057 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001058 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001059 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001060
Harald Welte52b1f982008-12-23 20:25:15 +00001061 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001062 subscr = subscr_get_by_tmsi(bts->network,
1063 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001064 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001065 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001066 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001067 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001068 }
1069 break;
1070 case GSM_MI_TYPE_IMEI:
1071 case GSM_MI_TYPE_IMEISV:
1072 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001073 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001074 break;
1075 default:
Harald Weltea0368542009-06-27 02:58:43 +02001076 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001077 break;
1078 }
1079
Harald Welte24516ea2009-07-04 10:18:00 +02001080 /* schedule the reject timer */
1081 schedule_reject(lchan);
1082
Harald Welte4bfdfe72009-06-10 23:11:52 +08001083 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001084 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001085 /* FIXME: request id? close channel? */
1086 return -EINVAL;
1087 }
1088
Harald Welte255539c2008-12-28 02:26:27 +00001089 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001090 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001091
Harald Welte24516ea2009-07-04 10:18:00 +02001092 /* check if we can let the subscriber into our network immediately
1093 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001094 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001095}
1096
Harald Welte4bfdfe72009-06-10 23:11:52 +08001097#if 0
1098static u_int8_t to_bcd8(u_int8_t val)
1099{
1100 return ((val / 10) << 4) | (val % 10);
1101}
1102#endif
1103
Harald Weltedb253af2008-12-30 17:56:55 +00001104/* Section 9.2.15a */
1105int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1106{
1107 struct msgb *msg = gsm48_msgb_alloc();
1108 struct gsm48_hdr *gh;
1109 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001110 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001111 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001112#if 0
1113 time_t cur_t;
1114 struct tm* cur_time;
1115 int tz15min;
1116#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001117
1118 msg->lchan = lchan;
1119
1120 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1121 gh->proto_discr = GSM48_PDISC_MM;
1122 gh->msg_type = GSM48_MT_MM_INFO;
1123
1124 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001125#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001126 name_len = strlen(net->name_long);
1127 /* 10.5.3.5a */
1128 ptr8 = msgb_put(msg, 3);
1129 ptr8[0] = GSM48_IE_NAME_LONG;
1130 ptr8[1] = name_len*2 +1;
1131 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1132
1133 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1134 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001135 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001136
1137 /* FIXME: Use Cell Broadcast, not UCS-2, since
1138 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001139#endif
1140 name_len = (strlen(net->name_long)*7)/8;
1141 name_pad = (8 - strlen(net->name_long)*7)%8;
1142 if (name_pad > 0)
1143 name_len++;
1144 /* 10.5.3.5a */
1145 ptr8 = msgb_put(msg, 3);
1146 ptr8[0] = GSM48_IE_NAME_LONG;
1147 ptr8[1] = name_len +1;
1148 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1149
1150 ptr8 = msgb_put(msg, name_len);
1151 gsm_7bit_encode(ptr8, net->name_long);
1152
Harald Weltedb253af2008-12-30 17:56:55 +00001153 }
1154
1155 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001156#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001157 name_len = strlen(net->name_short);
1158 /* 10.5.3.5a */
1159 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001160 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001161 ptr8[1] = name_len*2 + 1;
1162 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1163
Harald Weltee872cb12009-01-01 00:33:37 +00001164 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001165 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001166 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001167#endif
1168 name_len = (strlen(net->name_short)*7)/8;
1169 name_pad = (8 - strlen(net->name_short)*7)%8;
1170 if (name_pad > 0)
1171 name_len++;
1172 /* 10.5.3.5a */
1173 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1174 ptr8[0] = GSM48_IE_NAME_SHORT;
1175 ptr8[1] = name_len +1;
1176 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1177
1178 ptr8 = msgb_put(msg, name_len);
1179 gsm_7bit_encode(ptr8, net->name_short);
1180
Harald Weltedb253af2008-12-30 17:56:55 +00001181 }
1182
1183#if 0
1184 /* Section 10.5.3.9 */
1185 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001186 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001187 ptr8 = msgb_put(msg, 8);
1188 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1189 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1190 ptr8[2] = to_bcd8(cur_time->tm_mon);
1191 ptr8[3] = to_bcd8(cur_time->tm_mday);
1192 ptr8[4] = to_bcd8(cur_time->tm_hour);
1193 ptr8[5] = to_bcd8(cur_time->tm_min);
1194 ptr8[6] = to_bcd8(cur_time->tm_sec);
1195 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1196 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001197 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001198 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001199 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001200#endif
1201
Daniel Willmanneea93372009-08-13 03:42:07 +02001202 DEBUGP(DMM, "-> MM INFO\n");
1203
Harald Welte39e2ead2009-07-23 21:13:03 +02001204 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001205}
1206
Harald Welte7984d5c2009-08-12 22:56:50 +02001207/* Section 9.2.2 */
1208int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1209{
1210 struct msgb *msg = gsm48_msgb_alloc();
1211 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001212 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001213
1214 DEBUGP(DMM, "-> AUTH REQ\n");
1215
1216 msg->lchan = lchan;
1217 gh->proto_discr = GSM48_PDISC_MM;
1218 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1219
Sylvain Munaut849f5542009-09-27 11:10:17 +02001220 /* Key Sequence: FIXME fixed to 0 */
1221 ar->key_seq = 0;
1222
Harald Welte7984d5c2009-08-12 22:56:50 +02001223 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001224 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001225 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001226
1227 return gsm48_sendmsg(msg, NULL);
1228}
1229
1230/* Section 9.2.1 */
1231int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1232{
1233 DEBUGP(DMM, "-> AUTH REJECT\n");
1234 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1235}
1236
Harald Welte4b634542008-12-27 01:55:51 +00001237static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1238{
Harald Welte4b634542008-12-27 01:55:51 +00001239 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001240 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001241}
Harald Welteba4cf162009-01-10 01:49:35 +00001242
1243/* 9.2.6 CM service reject */
1244static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1245 enum gsm48_reject_value value)
1246{
1247 struct msgb *msg = gsm48_msgb_alloc();
1248 struct gsm48_hdr *gh;
1249
1250 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1251
1252 msg->lchan = lchan;
1253 use_lchan(lchan);
1254
1255 gh->proto_discr = GSM48_PDISC_MM;
1256 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1257 gh->data[0] = value;
1258 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1259
Harald Welte39e2ead2009-07-23 21:13:03 +02001260 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001261}
1262
Harald Welte4ed0e922009-01-10 03:17:30 +00001263/*
1264 * Handle CM Service Requests
1265 * a) Verify that the packet is long enough to contain the information
1266 * we require otherwsie reject with INCORRECT_MESSAGE
1267 * b) Try to parse the TMSI. If we do not have one reject
1268 * c) Check that we know the subscriber with the TMSI otherwise reject
1269 * with a HLR cause
1270 * d) Set the subscriber on the gsm_lchan and accept
1271 */
Harald Welte4b634542008-12-27 01:55:51 +00001272static int gsm48_rx_mm_serv_req(struct msgb *msg)
1273{
Harald Welteba4cf162009-01-10 01:49:35 +00001274 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001275 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001276
Harald Welte9176bd42009-07-23 18:46:00 +02001277 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001278 struct gsm_subscriber *subscr;
1279 struct gsm48_hdr *gh = msgb_l3(msg);
1280 struct gsm48_service_request *req =
1281 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001282 /* unfortunately in Phase1 the classmar2 length is variable */
1283 u_int8_t classmark2_len = gh->data[1];
1284 u_int8_t *classmark2 = gh->data+2;
1285 u_int8_t mi_len = *(classmark2 + classmark2_len);
1286 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001287
Harald Weltec9e02182009-05-01 19:07:53 +00001288 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001289 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001290 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001291 return gsm48_tx_mm_serv_rej(msg->lchan,
1292 GSM48_REJECT_INCORRECT_MESSAGE);
1293 }
1294
1295 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001296 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001297 return gsm48_tx_mm_serv_rej(msg->lchan,
1298 GSM48_REJECT_INCORRECT_MESSAGE);
1299 }
1300
Harald Weltec9e02182009-05-01 19:07:53 +00001301 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001302 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001303 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001304 return gsm48_tx_mm_serv_rej(msg->lchan,
1305 GSM48_REJECT_INCORRECT_MESSAGE);
1306 }
1307
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001308 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001309 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001310 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001311
Harald Welte3ac7f102009-08-10 10:12:45 +02001312 if (is_siemens_bts(bts))
1313 send_siemens_mrpci(msg->lchan, classmark2-1);
1314
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001315 subscr = subscr_get_by_tmsi(bts->network,
1316 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001317
Harald Welte2a139372009-02-22 21:14:55 +00001318 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001319 if (!subscr)
1320 return gsm48_tx_mm_serv_rej(msg->lchan,
1321 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1322
1323 if (!msg->lchan->subscr)
1324 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001325 else if (msg->lchan->subscr != subscr) {
1326 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1327 subscr_put(subscr);
1328 }
1329
Harald Weltec2e302d2009-07-05 14:08:13 +02001330 subscr->equipment.classmark2_len = classmark2_len;
1331 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1332 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001333
Harald Welte4b634542008-12-27 01:55:51 +00001334 return gsm48_tx_mm_serv_ack(msg->lchan);
1335}
1336
Harald Welte2a139372009-02-22 21:14:55 +00001337static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1338{
Harald Welte9176bd42009-07-23 18:46:00 +02001339 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001340 struct gsm48_hdr *gh = msgb_l3(msg);
1341 struct gsm48_imsi_detach_ind *idi =
1342 (struct gsm48_imsi_detach_ind *) gh->data;
1343 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001344 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001346
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001347 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001348 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1349 mi_type, mi_string);
1350
1351 switch (mi_type) {
1352 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001353 subscr = subscr_get_by_tmsi(bts->network,
1354 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001355 break;
1356 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001357 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001358 break;
1359 case GSM_MI_TYPE_IMEI:
1360 case GSM_MI_TYPE_IMEISV:
1361 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001362 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001363 break;
1364 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001365 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001366 break;
1367 }
1368
Holger Freyther4a49e772009-04-12 05:37:29 +00001369 if (subscr) {
1370 subscr_update(subscr, msg->trx->bts,
1371 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001372 DEBUGP(DMM, "Subscriber: %s\n",
1373 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001374
1375 subscr->equipment.classmark1 = idi->classmark1;
1376 db_sync_equipment(&subscr->equipment);
1377
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001378 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001379 } else
Harald Welte2a139372009-02-22 21:14:55 +00001380 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1381
Harald Welte2a139372009-02-22 21:14:55 +00001382 return 0;
1383}
1384
Harald Welted2a7f5a2009-06-05 20:08:20 +00001385static int gsm48_rx_mm_status(struct msgb *msg)
1386{
1387 struct gsm48_hdr *gh = msgb_l3(msg);
1388
1389 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1390
1391 return 0;
1392}
1393
Harald Weltebf5e8df2009-02-03 12:59:45 +00001394/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001395static int gsm0408_rcv_mm(struct msgb *msg)
1396{
1397 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001399
1400 switch (gh->msg_type & 0xbf) {
1401 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001402 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001403 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001404 break;
1405 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001406 rc = mm_rx_id_resp(msg);
1407 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001408 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001409 rc = gsm48_rx_mm_serv_req(msg);
1410 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001411 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001412 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001413 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001414 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001415 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1416 msg->lchan->subscr ?
1417 msg->lchan->subscr->imsi :
1418 "unknown subscriber");
1419 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001420 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001421 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1422 break;
1423 case GSM48_MT_MM_CM_REEST_REQ:
1424 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1425 break;
1426 case GSM48_MT_MM_AUTH_RESP:
1427 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001428 break;
1429 default:
1430 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1431 gh->msg_type);
1432 break;
1433 }
1434
1435 return rc;
1436}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001437
Harald Welte2d35ae62009-02-06 12:02:13 +00001438/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001439static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001440{
Harald Welte9176bd42009-07-23 18:46:00 +02001441 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001442 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001443 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001444 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001445 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001446 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001447 int rc = 0;
1448
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001449 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001450 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1451 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001452
Harald Weltefe18d8f2009-02-22 21:14:24 +00001453 switch (mi_type) {
1454 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001455 subscr = subscr_get_by_tmsi(bts->network,
1456 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001457 break;
1458 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001459 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001460 break;
1461 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001462
1463 if (!subscr) {
1464 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001465 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001466 return -EINVAL;
1467 }
1468 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001469 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001470
Harald Weltec2e302d2009-07-05 14:08:13 +02001471 subscr->equipment.classmark2_len = *classmark2_lv;
1472 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1473 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001474
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001475 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001476 return rc;
1477}
1478
Harald Weltef7c43522009-06-09 20:24:21 +00001479static int gsm48_rx_rr_classmark(struct msgb *msg)
1480{
1481 struct gsm48_hdr *gh = msgb_l3(msg);
1482 struct gsm_subscriber *subscr = msg->lchan->subscr;
1483 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1484 u_int8_t cm2_len, cm3_len = 0;
1485 u_int8_t *cm2, *cm3 = NULL;
1486
1487 DEBUGP(DRR, "CLASSMARK CHANGE ");
1488
1489 /* classmark 2 */
1490 cm2_len = gh->data[0];
1491 cm2 = &gh->data[1];
1492 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1493
1494 if (payload_len > cm2_len + 1) {
1495 /* we must have a classmark3 */
1496 if (gh->data[cm2_len+1] != 0x20) {
1497 DEBUGPC(DRR, "ERR CM3 TAG\n");
1498 return -EINVAL;
1499 }
1500 if (cm2_len > 3) {
1501 DEBUGPC(DRR, "CM2 too long!\n");
1502 return -EINVAL;
1503 }
1504
1505 cm3_len = gh->data[cm2_len+2];
1506 cm3 = &gh->data[cm2_len+3];
1507 if (cm3_len > 14) {
1508 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1509 return -EINVAL;
1510 }
1511 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1512 }
1513 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001514 subscr->equipment.classmark2_len = cm2_len;
1515 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001516 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001517 subscr->equipment.classmark3_len = cm3_len;
1518 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001519 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001520 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001521 }
1522
Harald Weltef7c43522009-06-09 20:24:21 +00001523 return 0;
1524}
1525
Harald Weltecf5b3592009-05-01 18:28:42 +00001526static int gsm48_rx_rr_status(struct msgb *msg)
1527{
1528 struct gsm48_hdr *gh = msgb_l3(msg);
1529
1530 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1531 rr_cause_name(gh->data[0]));
1532
1533 return 0;
1534}
1535
Harald Weltef7c43522009-06-09 20:24:21 +00001536static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1537{
Harald Weltef7c43522009-06-09 20:24:21 +00001538 static struct gsm_meas_rep meas_rep;
1539
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001540 /* This shouldn't actually end up here, as RSL treats
1541 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1542 * directly into gsm48_parse_meas_rep */
1543 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1544 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001545
1546 return 0;
1547}
1548
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001549static int gsm48_rx_rr_app_info(struct msgb *msg)
1550{
1551 struct gsm48_hdr *gh = msgb_l3(msg);
1552 u_int8_t apdu_id_flags;
1553 u_int8_t apdu_len;
1554 u_int8_t *apdu_data;
1555
1556 apdu_id_flags = gh->data[0];
1557 apdu_len = gh->data[1];
1558 apdu_data = gh->data+2;
1559
1560 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1561 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1562
Harald Welte (local)026531e2009-08-16 10:40:10 +02001563 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001564}
1565
Harald Welted011e8b2009-11-29 22:45:52 +01001566/* Chapter 9.1.16 Handover complete */
1567static gsm48_rx_rr_ho_compl(struct msgb *msg)
1568{
1569 struct gsm48_hdr *gh = msgb_l3(msg);
1570
1571 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1572 rr_cause_name(gh->data[0]));
1573
1574 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1575 /* FIXME: release old channel */
1576
1577 return 0;
1578}
1579
1580/* Chapter 9.1.17 Handover Failure */
1581static gsm48_rx_rr_ho_fail(struct msgb *msg)
1582{
1583 struct gsm48_hdr *gh = msgb_l3(msg);
1584
1585 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1586 rr_cause_name(gh->data[0]));
1587
1588 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1589 /* FIXME: release allocated new channel */
1590
1591 return 0;
1592}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001593
Harald Weltebf5e8df2009-02-03 12:59:45 +00001594/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001595static int gsm0408_rcv_rr(struct msgb *msg)
1596{
1597 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001598 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001599
1600 switch (gh->msg_type) {
1601 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001602 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001603 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001604 case GSM48_MT_RR_GPRS_SUSP_REQ:
1605 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1606 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001607 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001608 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001609 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001610 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001611 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001612 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001613 case GSM48_MT_RR_STATUS:
1614 rc = gsm48_rx_rr_status(msg);
1615 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001616 case GSM48_MT_RR_MEAS_REP:
1617 rc = gsm48_rx_rr_meas_rep(msg);
1618 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001619 case GSM48_MT_RR_APP_INFO:
1620 rc = gsm48_rx_rr_app_info(msg);
1621 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001622 case GSM48_MT_RR_CIPH_M_COMPL:
1623 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1624 /* FIXME: check for MI (if any) */
1625 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001626 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001627 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001628 break;
1629 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001630 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001631 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001632 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001633 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001634 gh->msg_type);
1635 break;
1636 }
1637
Harald Welte2d35ae62009-02-06 12:02:13 +00001638 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001639}
1640
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001641int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001642 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001643{
1644 struct msgb *msg = gsm48_msgb_alloc();
1645 struct gsm48_hdr *gh;
1646
1647 msg->lchan = lchan;
1648
1649 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1650 apdu_id, apdu_len);
1651
1652 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1653 gh->proto_discr = GSM48_PDISC_RR;
1654 gh->msg_type = GSM48_MT_RR_APP_INFO;
1655 gh->data[0] = apdu_id;
1656 gh->data[1] = apdu_len;
1657 memcpy(gh->data+2, apdu, apdu_len);
1658
1659 return gsm48_sendmsg(msg, NULL);
1660}
1661
Harald Welte4bc90a12008-12-27 16:32:52 +00001662/* Call Control */
1663
Harald Welte7584aea2009-02-11 11:44:12 +00001664/* The entire call control code is written in accordance with Figure 7.10c
1665 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1666 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1667 * it for voice */
1668
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669static void new_cc_state(struct gsm_trans *trans, int state)
1670{
1671 if (state > 31 || state < 0)
1672 return;
1673
1674 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001675 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676
Harald Weltedcaf5652009-07-23 18:56:43 +02001677 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678}
1679
1680static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001681{
1682 struct msgb *msg = gsm48_msgb_alloc();
1683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1684 u_int8_t *cause, *call_state;
1685
Harald Welte4bc90a12008-12-27 16:32:52 +00001686 gh->msg_type = GSM48_MT_CC_STATUS;
1687
1688 cause = msgb_put(msg, 3);
1689 cause[0] = 2;
1690 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1691 cause[2] = 0x80 | 30; /* response to status inquiry */
1692
1693 call_state = msgb_put(msg, 1);
1694 call_state[0] = 0xc0 | 0x00;
1695
Harald Welte39e2ead2009-07-23 21:13:03 +02001696 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001697}
1698
Harald Welte6f4b7532008-12-29 00:39:37 +00001699static int gsm48_tx_simple(struct gsm_lchan *lchan,
1700 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001701{
1702 struct msgb *msg = gsm48_msgb_alloc();
1703 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1704
1705 msg->lchan = lchan;
1706
Harald Welte6f4b7532008-12-29 00:39:37 +00001707 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001708 gh->msg_type = msg_type;
1709
Harald Welte39e2ead2009-07-23 21:13:03 +02001710 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001711}
1712
Harald Welte4bfdfe72009-06-10 23:11:52 +08001713static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1714{
Harald Weltedcaf5652009-07-23 18:56:43 +02001715 if (bsc_timer_pending(&trans->cc.timer)) {
1716 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1717 bsc_del_timer(&trans->cc.timer);
1718 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719 }
1720}
1721
1722static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1723 int msg_type, struct gsm_mncc *mncc)
1724{
1725 struct msgb *msg;
1726
1727 if (trans)
1728 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001729 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 "Sending '%s' to MNCC.\n",
1731 trans->lchan->ts->trx->bts->nr,
1732 trans->lchan->ts->trx->nr,
1733 trans->lchan->ts->nr, trans->transaction_id,
1734 (trans->subscr)?(trans->subscr->extension):"-",
1735 get_mncc_name(msg_type));
1736 else
1737 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1738 "Sending '%s' to MNCC.\n",
1739 (trans->subscr)?(trans->subscr->extension):"-",
1740 get_mncc_name(msg_type));
1741 else
1742 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1743 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1744
1745 mncc->msg_type = msg_type;
1746
Harald Welte966636f2009-06-26 19:39:35 +02001747 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 if (!msg)
1749 return -ENOMEM;
1750 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1751 msgb_enqueue(&net->upqueue, msg);
1752
1753 return 0;
1754}
1755
1756int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1757 u_int32_t callref, int location, int value)
1758{
1759 struct gsm_mncc rel;
1760
Harald Welte92f70c52009-06-12 01:54:08 +08001761 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001763 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1765}
1766
Harald Weltedcaf5652009-07-23 18:56:43 +02001767/* Call Control Specific transaction release.
1768 * gets called by trans_free, DO NOT CALL YOURSELF! */
1769void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 gsm48_stop_cc_timer(trans);
1772
1773 /* send release to L4, if callref still exists */
1774 if (trans->callref) {
1775 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001776 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001777 GSM48_CAUSE_LOC_PRN_S_LU,
1778 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001780 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001782 if (trans->lchan)
1783 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784}
1785
1786static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1787
Harald Welte09e38af2009-02-16 22:52:23 +00001788/* call-back from paging the B-end of the connection */
1789static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001790 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001791{
Harald Welte7ccf7782009-02-17 01:43:01 +00001792 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 struct gsm_subscriber *subscr = param;
1794 struct gsm_trans *transt, *tmp;
1795 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001796
Harald Welte09e38af2009-02-16 22:52:23 +00001797 if (hooknum != GSM_HOOK_RR_PAGING)
1798 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799
1800 if (!subscr)
1801 return -EINVAL;
1802 net = subscr->net;
1803 if (!net) {
1804 DEBUGP(DCC, "Error Network not set!\n");
1805 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001806 }
Harald Welte7584aea2009-02-11 11:44:12 +00001807
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* check all tranactions (without lchan) for subscriber */
1809 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1810 if (transt->subscr != subscr || transt->lchan)
1811 continue;
1812 switch (event) {
1813 case GSM_PAGING_SUCCEEDED:
1814 if (!lchan) // paranoid
1815 break;
1816 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1817 subscr->extension);
1818 /* Assign lchan */
1819 if (!transt->lchan) {
1820 transt->lchan = lchan;
1821 use_lchan(lchan);
1822 }
1823 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001824 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 break;
1826 case GSM_PAGING_EXPIRED:
1827 DEBUGP(DCC, "Paging subscr %s expired!\n",
1828 subscr->extension);
1829 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001830 mncc_release_ind(transt->subscr->net, transt,
1831 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001832 GSM48_CAUSE_LOC_PRN_S_LU,
1833 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001835 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 break;
1837 }
1838 }
Harald Welte09e38af2009-02-16 22:52:23 +00001839 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001840}
Harald Welte7584aea2009-02-11 11:44:12 +00001841
Harald Welte805f6442009-07-28 18:25:29 +02001842/* some other part of the code sends us a signal */
1843static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1844 void *handler_data, void *signal_data)
1845{
1846 struct gsm_lchan *lchan = signal_data;
1847 struct gsm_bts_trx_ts *ts;
1848 int rc;
1849
1850 if (subsys != SS_ABISIP)
1851 return 0;
1852
1853 /* in case we use direct BTS-to-BTS RTP */
1854 if (ipacc_rtp_direct)
1855 return 0;
1856
1857 ts = lchan->ts;
1858
1859 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001860 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001861 /* the BTS has successfully bound a TCH to a local ip/port,
1862 * which means we can connect our UDP socket to it */
1863 if (ts->abis_ip.rtp_socket) {
1864 rtp_socket_free(ts->abis_ip.rtp_socket);
1865 ts->abis_ip.rtp_socket = NULL;
1866 }
1867
1868 ts->abis_ip.rtp_socket = rtp_socket_create();
1869 if (!ts->abis_ip.rtp_socket)
1870 goto out_err;
1871
1872 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1873 ts->abis_ip.bound_ip,
1874 ts->abis_ip.bound_port);
1875 if (rc < 0)
1876 goto out_err;
1877 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001878 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001879 /* the BTS tells us a RTP stream has been disconnected */
1880 if (ts->abis_ip.rtp_socket) {
1881 rtp_socket_free(ts->abis_ip.rtp_socket);
1882 ts->abis_ip.rtp_socket = NULL;
1883 }
1884 break;
1885 }
1886
1887 return 0;
1888out_err:
1889 /* FIXME: do something */
1890 return 0;
1891}
1892
1893/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1894static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1895{
1896 struct gsm_bts_trx_ts *ts = lchan->ts;
1897 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1898 int rc;
1899
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001900 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001901 ntohs(rs->rtp.sin_local.sin_port),
1902 ts->abis_ip.conn_id,
1903 /* FIXME: use RTP payload of bound socket, not BTS*/
1904 ts->abis_ip.rtp_payload2);
1905
1906 return rc;
1907}
1908
Harald Welte49f48b82009-02-17 15:29:33 +00001909/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001910static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001911{
Harald Welte11fa29c2009-02-19 17:24:39 +00001912 struct gsm_bts *bts = lchan->ts->trx->bts;
1913 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001914 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02001915 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001916
Harald Welte11fa29c2009-02-19 17:24:39 +00001917 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1918 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1919 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1920
1921 if (bts->type != remote_bts->type) {
1922 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1923 return -EINVAL;
1924 }
Harald Welte49f48b82009-02-17 15:29:33 +00001925
Harald Welte11fa29c2009-02-19 17:24:39 +00001926 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001927 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001928 if (!ipacc_rtp_direct) {
1929 /* connect the TCH's to our RTP proxy */
1930 rc = ipacc_connect_proxy_bind(lchan);
1931 if (rc < 0)
1932 return rc;
1933 rc = ipacc_connect_proxy_bind(remote_lchan);
1934
1935 /* connect them with each other */
1936 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
1937 remote_lchan->ts->abis_ip.rtp_socket);
1938 } else {
1939 /* directly connect TCH RTP streams to each other */
1940 ts = remote_lchan->ts;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001941 rc = rsl_ipacc_mdcx(lchan, ts->abis_ip.bound_ip,
Harald Welte805f6442009-07-28 18:25:29 +02001942 ts->abis_ip.bound_port,
1943 lchan->ts->abis_ip.conn_id,
1944 ts->abis_ip.rtp_payload2);
1945 if (rc < 0)
1946 return rc;
1947 ts = lchan->ts;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001948 rc = rsl_ipacc_mdcx(remote_lchan, ts->abis_ip.bound_ip,
Harald Welte805f6442009-07-28 18:25:29 +02001949 ts->abis_ip.bound_port,
1950 remote_lchan->ts->abis_ip.conn_id,
1951 ts->abis_ip.rtp_payload2);
1952 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001953 break;
1954 case GSM_BTS_TYPE_BS11:
1955 trau_mux_map_lchan(lchan, remote_lchan);
1956 break;
1957 default:
1958 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001959 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001960 break;
1961 }
Harald Welte49f48b82009-02-17 15:29:33 +00001962
1963 return 0;
1964}
1965
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966/* bridge channels of two transactions */
1967static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001968{
Harald Weltedcaf5652009-07-23 18:56:43 +02001969 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1970 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001971
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001973 return -EIO;
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 if (!trans1->lchan || !trans2->lchan)
1976 return -EIO;
1977
1978 /* through-connect channel */
1979 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001980}
1981
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982/* enable receive of channels to upqueue */
1983static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1984{
1985 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001986
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001988 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 if (!trans)
1990 return -EIO;
1991 if (!trans->lchan)
1992 return 0;
1993
1994 // todo IPACCESS
1995 if (enable)
1996 return trau_recv_lchan(trans->lchan, data->callref);
1997 return trau_mux_unmap(NULL, data->callref);
1998}
1999
2000/* send a frame to channel */
2001static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2002{
2003 struct gsm_trans *trans;
2004
2005 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002006 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 if (!trans)
2008 return -EIO;
2009 if (!trans->lchan)
2010 return 0;
2011 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2012 trans->lchan->type != GSM_LCHAN_TCH_H)
2013 return 0;
2014
2015 // todo IPACCESS
2016 return trau_send_lchan(trans->lchan,
2017 (struct decoded_trau_frame *)frame->data);
2018}
2019
2020
2021static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2022{
2023 DEBUGP(DCC, "-> STATUS ENQ\n");
2024 return gsm48_cc_tx_status(trans, msg);
2025}
2026
2027static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2028static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2029
2030static void gsm48_cc_timeout(void *arg)
2031{
2032 struct gsm_trans *trans = arg;
2033 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002034 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2035 int mo_location = GSM48_CAUSE_LOC_USER;
2036 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2037 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 struct gsm_mncc mo_rel, l4_rel;
2039
2040 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2041 mo_rel.callref = trans->callref;
2042 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2043 l4_rel.callref = trans->callref;
2044
Harald Weltedcaf5652009-07-23 18:56:43 +02002045 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 case 0x303:
2047 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002048 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 break;
2050 case 0x310:
2051 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002052 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 break;
2054 case 0x313:
2055 disconnect = 1;
2056 /* unknown, did not find it in the specs */
2057 break;
2058 case 0x301:
2059 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002060 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 break;
2062 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002063 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002065 gsm48_cc_tx_release(trans, &trans->cc.msg);
2066 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 break; /* stay in release state */
2068 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002069 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 return;
2071// release = 1;
2072// l4_cause = 14;
2073// break;
2074 case 0x306:
2075 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002076 mo_cause = trans->cc.msg.cause.value;
2077 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 break;
2079 case 0x323:
2080 disconnect = 1;
2081 break;
2082 default:
2083 release = 1;
2084 }
2085
2086 if (release && trans->callref) {
2087 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002088 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 l4_location, l4_cause);
2090 trans->callref = 0;
2091 }
2092
2093 if (disconnect && trans->callref) {
2094 /* process disconnect towards layer 4 */
2095 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002096 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 }
2098
2099 /* process disconnect towards mobile station */
2100 if (disconnect || release) {
2101 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002102 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2103 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2104 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 mo_rel.cause.diag_len = 3;
2106
2107 if (disconnect)
2108 gsm48_cc_tx_disconnect(trans, &mo_rel);
2109 if (release)
2110 gsm48_cc_tx_release(trans, &mo_rel);
2111 }
2112
2113}
2114
2115static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2116 int sec, int micro)
2117{
2118 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002119 trans->cc.timer.cb = gsm48_cc_timeout;
2120 trans->cc.timer.data = trans;
2121 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2122 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123}
2124
2125static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2126{
2127 struct gsm48_hdr *gh = msgb_l3(msg);
2128 u_int8_t msg_type = gh->msg_type & 0xbf;
2129 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2130 struct tlv_parsed tp;
2131 struct gsm_mncc setup;
2132
2133 memset(&setup, 0, sizeof(struct gsm_mncc));
2134 setup.callref = trans->callref;
2135 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2136 /* emergency setup is identified by msg_type */
2137 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2138 setup.emergency = 1;
2139
2140 /* use subscriber as calling party number */
2141 if (trans->subscr) {
2142 setup.fields |= MNCC_F_CALLING;
2143 strncpy(setup.calling.number, trans->subscr->extension,
2144 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002145 strncpy(setup.imsi, trans->subscr->imsi,
2146 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 }
2148 /* bearer capability */
2149 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2150 setup.fields |= MNCC_F_BEARER_CAP;
2151 decode_bearer_cap(&setup.bearer_cap,
2152 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2153 }
2154 /* facility */
2155 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2156 setup.fields |= MNCC_F_FACILITY;
2157 decode_facility(&setup.facility,
2158 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2159 }
2160 /* called party bcd number */
2161 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2162 setup.fields |= MNCC_F_CALLED;
2163 decode_called(&setup.called,
2164 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2165 }
2166 /* user-user */
2167 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2168 setup.fields |= MNCC_F_USERUSER;
2169 decode_useruser(&setup.useruser,
2170 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2171 }
2172 /* ss-version */
2173 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2174 setup.fields |= MNCC_F_SSVERSION;
2175 decode_ssversion(&setup.ssversion,
2176 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2177 }
2178 /* CLIR suppression */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2180 setup.clir.sup = 1;
2181 /* CLIR invocation */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2183 setup.clir.inv = 1;
2184 /* cc cap */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2186 setup.fields |= MNCC_F_CCCAP;
2187 decode_cccap(&setup.cccap,
2188 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2189 }
2190
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 new_cc_state(trans, GSM_CSTATE_INITIATED);
2192
2193 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002194 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195
Harald Welte13cac662009-07-29 12:10:35 +02002196 /* MNCC code will modify the channel asynchronously, we should
2197 * ipaccess-bind only after the modification has been made to the
2198 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 return 0;
2200}
2201
2202static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002203{
2204 struct msgb *msg = gsm48_msgb_alloc();
2205 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002207 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002208
Harald Welte7ccf7782009-02-17 01:43:01 +00002209 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002210
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 /* transaction id must not be assigned */
2212 if (trans->transaction_id != 0xff) { /* unasssigned */
2213 DEBUGP(DCC, "TX Setup with assigned transaction. "
2214 "This is not allowed!\n");
2215 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002216 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002217 GSM48_CAUSE_LOC_PRN_S_LU,
2218 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002220 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 return rc;
2222 }
2223
2224 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002225 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2226 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002228 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002229 GSM48_CAUSE_LOC_PRN_S_LU,
2230 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002232 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 return rc;
2234 }
Harald Welte78283ef2009-07-23 21:36:44 +02002235 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002236
Harald Welte65e74cc2008-12-29 01:55:35 +00002237 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002238
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002240
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 /* bearer capability */
2242 if (setup->fields & MNCC_F_BEARER_CAP)
2243 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2244 /* facility */
2245 if (setup->fields & MNCC_F_FACILITY)
2246 encode_facility(msg, 0, &setup->facility);
2247 /* progress */
2248 if (setup->fields & MNCC_F_PROGRESS)
2249 encode_progress(msg, 0, &setup->progress);
2250 /* calling party BCD number */
2251 if (setup->fields & MNCC_F_CALLING)
2252 encode_calling(msg, &setup->calling);
2253 /* called party BCD number */
2254 if (setup->fields & MNCC_F_CALLED)
2255 encode_called(msg, &setup->called);
2256 /* user-user */
2257 if (setup->fields & MNCC_F_USERUSER)
2258 encode_useruser(msg, 0, &setup->useruser);
2259 /* redirecting party BCD number */
2260 if (setup->fields & MNCC_F_REDIRECTING)
2261 encode_redirecting(msg, &setup->redirecting);
2262 /* signal */
2263 if (setup->fields & MNCC_F_SIGNAL)
2264 encode_signal(msg, setup->signal);
2265
2266 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002267
Harald Welte39e2ead2009-07-23 21:13:03 +02002268 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002269}
2270
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2272{
2273 struct gsm48_hdr *gh = msgb_l3(msg);
2274 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2275 struct tlv_parsed tp;
2276 struct gsm_mncc call_conf;
2277
2278 gsm48_stop_cc_timer(trans);
2279 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2280
2281 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2282 call_conf.callref = trans->callref;
2283 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2284#if 0
2285 /* repeat */
2286 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2287 call_conf.repeat = 1;
2288 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2289 call_conf.repeat = 2;
2290#endif
2291 /* bearer capability */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2293 call_conf.fields |= MNCC_F_BEARER_CAP;
2294 decode_bearer_cap(&call_conf.bearer_cap,
2295 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2296 }
2297 /* cause */
2298 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2299 call_conf.fields |= MNCC_F_CAUSE;
2300 decode_cause(&call_conf.cause,
2301 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2302 }
2303 /* cc cap */
2304 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2305 call_conf.fields |= MNCC_F_CCCAP;
2306 decode_cccap(&call_conf.cccap,
2307 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2308 }
2309
2310 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2311
Harald Welte596fed42009-07-23 19:06:52 +02002312 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2313 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314}
2315
2316static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2317{
2318 struct gsm_mncc *proceeding = arg;
2319 struct msgb *msg = gsm48_msgb_alloc();
2320 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2321
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2323
2324 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2325
2326 /* bearer capability */
2327 if (proceeding->fields & MNCC_F_BEARER_CAP)
2328 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2329 /* facility */
2330 if (proceeding->fields & MNCC_F_FACILITY)
2331 encode_facility(msg, 0, &proceeding->facility);
2332 /* progress */
2333 if (proceeding->fields & MNCC_F_PROGRESS)
2334 encode_progress(msg, 0, &proceeding->progress);
2335
Harald Welte39e2ead2009-07-23 21:13:03 +02002336 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337}
2338
2339static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2340{
2341 struct gsm48_hdr *gh = msgb_l3(msg);
2342 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2343 struct tlv_parsed tp;
2344 struct gsm_mncc alerting;
2345
2346 gsm48_stop_cc_timer(trans);
2347 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2348
2349 memset(&alerting, 0, sizeof(struct gsm_mncc));
2350 alerting.callref = trans->callref;
2351 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2352 /* facility */
2353 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2354 alerting.fields |= MNCC_F_FACILITY;
2355 decode_facility(&alerting.facility,
2356 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2357 }
2358
2359 /* progress */
2360 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2361 alerting.fields |= MNCC_F_PROGRESS;
2362 decode_progress(&alerting.progress,
2363 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2364 }
2365 /* ss-version */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2367 alerting.fields |= MNCC_F_SSVERSION;
2368 decode_ssversion(&alerting.ssversion,
2369 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2370 }
2371
2372 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2373
Harald Welte596fed42009-07-23 19:06:52 +02002374 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2375 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376}
2377
2378static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2379{
2380 struct gsm_mncc *alerting = arg;
2381 struct msgb *msg = gsm48_msgb_alloc();
2382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2383
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 gh->msg_type = GSM48_MT_CC_ALERTING;
2385
2386 /* facility */
2387 if (alerting->fields & MNCC_F_FACILITY)
2388 encode_facility(msg, 0, &alerting->facility);
2389 /* progress */
2390 if (alerting->fields & MNCC_F_PROGRESS)
2391 encode_progress(msg, 0, &alerting->progress);
2392 /* user-user */
2393 if (alerting->fields & MNCC_F_USERUSER)
2394 encode_useruser(msg, 0, &alerting->useruser);
2395
2396 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2397
Harald Welte39e2ead2009-07-23 21:13:03 +02002398 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399}
2400
2401static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2402{
2403 struct gsm_mncc *progress = arg;
2404 struct msgb *msg = gsm48_msgb_alloc();
2405 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2406
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407 gh->msg_type = GSM48_MT_CC_PROGRESS;
2408
2409 /* progress */
2410 encode_progress(msg, 1, &progress->progress);
2411 /* user-user */
2412 if (progress->fields & MNCC_F_USERUSER)
2413 encode_useruser(msg, 0, &progress->useruser);
2414
Harald Welte39e2ead2009-07-23 21:13:03 +02002415 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416}
2417
2418static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2419{
2420 struct gsm_mncc *connect = arg;
2421 struct msgb *msg = gsm48_msgb_alloc();
2422 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2423
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 gh->msg_type = GSM48_MT_CC_CONNECT;
2425
2426 gsm48_stop_cc_timer(trans);
2427 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2428
2429 /* facility */
2430 if (connect->fields & MNCC_F_FACILITY)
2431 encode_facility(msg, 0, &connect->facility);
2432 /* progress */
2433 if (connect->fields & MNCC_F_PROGRESS)
2434 encode_progress(msg, 0, &connect->progress);
2435 /* connected number */
2436 if (connect->fields & MNCC_F_CONNECTED)
2437 encode_connected(msg, &connect->connected);
2438 /* user-user */
2439 if (connect->fields & MNCC_F_USERUSER)
2440 encode_useruser(msg, 0, &connect->useruser);
2441
2442 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2443
Harald Welte39e2ead2009-07-23 21:13:03 +02002444 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445}
2446
2447static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2448{
2449 struct gsm48_hdr *gh = msgb_l3(msg);
2450 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2451 struct tlv_parsed tp;
2452 struct gsm_mncc connect;
2453
2454 gsm48_stop_cc_timer(trans);
2455
2456 memset(&connect, 0, sizeof(struct gsm_mncc));
2457 connect.callref = trans->callref;
2458 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2459 /* use subscriber as connected party number */
2460 if (trans->subscr) {
2461 connect.fields |= MNCC_F_CONNECTED;
2462 strncpy(connect.connected.number, trans->subscr->extension,
2463 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002464 strncpy(connect.imsi, trans->subscr->imsi,
2465 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 }
2467 /* facility */
2468 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2469 connect.fields |= MNCC_F_FACILITY;
2470 decode_facility(&connect.facility,
2471 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2472 }
2473 /* user-user */
2474 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2475 connect.fields |= MNCC_F_USERUSER;
2476 decode_useruser(&connect.useruser,
2477 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2478 }
2479 /* ss-version */
2480 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2481 connect.fields |= MNCC_F_SSVERSION;
2482 decode_ssversion(&connect.ssversion,
2483 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2484 }
2485
2486 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2487
Harald Welte596fed42009-07-23 19:06:52 +02002488 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489}
2490
2491
2492static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2493{
2494 struct gsm_mncc connect_ack;
2495
2496 gsm48_stop_cc_timer(trans);
2497
2498 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2499
2500 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2501 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002502 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 &connect_ack);
2504}
2505
2506static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2507{
2508 struct msgb *msg = gsm48_msgb_alloc();
2509 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2510
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2512
2513 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2514
Harald Welte39e2ead2009-07-23 21:13:03 +02002515 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516}
2517
2518static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2519{
2520 struct gsm48_hdr *gh = msgb_l3(msg);
2521 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2522 struct tlv_parsed tp;
2523 struct gsm_mncc disc;
2524
2525 gsm48_stop_cc_timer(trans);
2526
2527 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2528
2529 memset(&disc, 0, sizeof(struct gsm_mncc));
2530 disc.callref = trans->callref;
2531 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2532 /* cause */
2533 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2534 disc.fields |= MNCC_F_CAUSE;
2535 decode_cause(&disc.cause,
2536 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2537 }
2538 /* facility */
2539 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2540 disc.fields |= MNCC_F_FACILITY;
2541 decode_facility(&disc.facility,
2542 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2543 }
2544 /* user-user */
2545 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2546 disc.fields |= MNCC_F_USERUSER;
2547 decode_useruser(&disc.useruser,
2548 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2549 }
2550 /* ss-version */
2551 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2552 disc.fields |= MNCC_F_SSVERSION;
2553 decode_ssversion(&disc.ssversion,
2554 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2555 }
2556
Harald Welte596fed42009-07-23 19:06:52 +02002557 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558
2559}
2560
Harald Weltec66b71c2009-06-11 14:23:20 +08002561static struct gsm_mncc_cause default_cause = {
2562 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2563 .coding = 0,
2564 .rec = 0,
2565 .rec_val = 0,
2566 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2567 .diag_len = 0,
2568 .diag = { 0 },
2569};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570
2571static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2572{
2573 struct gsm_mncc *disc = arg;
2574 struct msgb *msg = gsm48_msgb_alloc();
2575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2576
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2578
2579 gsm48_stop_cc_timer(trans);
2580 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2581
2582 /* cause */
2583 if (disc->fields & MNCC_F_CAUSE)
2584 encode_cause(msg, 1, &disc->cause);
2585 else
2586 encode_cause(msg, 1, &default_cause);
2587
2588 /* facility */
2589 if (disc->fields & MNCC_F_FACILITY)
2590 encode_facility(msg, 0, &disc->facility);
2591 /* progress */
2592 if (disc->fields & MNCC_F_PROGRESS)
2593 encode_progress(msg, 0, &disc->progress);
2594 /* user-user */
2595 if (disc->fields & MNCC_F_USERUSER)
2596 encode_useruser(msg, 0, &disc->useruser);
2597
2598 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002599 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600
2601 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2602
Harald Welte39e2ead2009-07-23 21:13:03 +02002603 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604}
2605
2606static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2607{
2608 struct gsm48_hdr *gh = msgb_l3(msg);
2609 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2610 struct tlv_parsed tp;
2611 struct gsm_mncc rel;
2612 int rc;
2613
2614 gsm48_stop_cc_timer(trans);
2615
2616 memset(&rel, 0, sizeof(struct gsm_mncc));
2617 rel.callref = trans->callref;
2618 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2619 /* cause */
2620 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2621 rel.fields |= MNCC_F_CAUSE;
2622 decode_cause(&rel.cause,
2623 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2624 }
2625 /* facility */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2627 rel.fields |= MNCC_F_FACILITY;
2628 decode_facility(&rel.facility,
2629 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2630 }
2631 /* user-user */
2632 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2633 rel.fields |= MNCC_F_USERUSER;
2634 decode_useruser(&rel.useruser,
2635 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2636 }
2637 /* ss-version */
2638 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2639 rel.fields |= MNCC_F_SSVERSION;
2640 decode_ssversion(&rel.ssversion,
2641 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2642 }
2643
Harald Weltedcaf5652009-07-23 18:56:43 +02002644 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002646 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002648 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002649 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002650 GSM48_MT_CC_RELEASE_COMPL);
2651 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 }
2653
2654 new_cc_state(trans, GSM_CSTATE_NULL);
2655
2656 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002657 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658
2659 return rc;
2660}
2661
2662static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2663{
2664 struct gsm_mncc *rel = arg;
2665 struct msgb *msg = gsm48_msgb_alloc();
2666 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2667
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668 gh->msg_type = GSM48_MT_CC_RELEASE;
2669
2670 trans->callref = 0;
2671
2672 gsm48_stop_cc_timer(trans);
2673 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2674
2675 /* cause */
2676 if (rel->fields & MNCC_F_CAUSE)
2677 encode_cause(msg, 0, &rel->cause);
2678 /* facility */
2679 if (rel->fields & MNCC_F_FACILITY)
2680 encode_facility(msg, 0, &rel->facility);
2681 /* user-user */
2682 if (rel->fields & MNCC_F_USERUSER)
2683 encode_useruser(msg, 0, &rel->useruser);
2684
Harald Weltedcaf5652009-07-23 18:56:43 +02002685 trans->cc.T308_second = 0;
2686 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687
Harald Weltedcaf5652009-07-23 18:56:43 +02002688 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2690
Harald Welte39e2ead2009-07-23 21:13:03 +02002691 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692}
2693
2694static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2695{
2696 struct gsm48_hdr *gh = msgb_l3(msg);
2697 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2698 struct tlv_parsed tp;
2699 struct gsm_mncc rel;
2700 int rc = 0;
2701
2702 gsm48_stop_cc_timer(trans);
2703
2704 memset(&rel, 0, sizeof(struct gsm_mncc));
2705 rel.callref = trans->callref;
2706 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2707 /* cause */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2709 rel.fields |= MNCC_F_CAUSE;
2710 decode_cause(&rel.cause,
2711 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2712 }
2713 /* facility */
2714 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2715 rel.fields |= MNCC_F_FACILITY;
2716 decode_facility(&rel.facility,
2717 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2718 }
2719 /* user-user */
2720 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2721 rel.fields |= MNCC_F_USERUSER;
2722 decode_useruser(&rel.useruser,
2723 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2724 }
2725 /* ss-version */
2726 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2727 rel.fields |= MNCC_F_SSVERSION;
2728 decode_ssversion(&rel.ssversion,
2729 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2730 }
2731
2732 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002733 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002735 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736 MNCC_REJ_IND, &rel);
2737 break;
2738 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002739 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 MNCC_REL_CNF, &rel);
2741 break;
2742 default:
Harald Welte596fed42009-07-23 19:06:52 +02002743 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 MNCC_REL_IND, &rel);
2745 }
2746 }
2747
2748 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002749 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750
2751 return rc;
2752}
2753
2754static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2755{
2756 struct gsm_mncc *rel = arg;
2757 struct msgb *msg = gsm48_msgb_alloc();
2758 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2759
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2761
2762 trans->callref = 0;
2763
2764 gsm48_stop_cc_timer(trans);
2765
2766 /* cause */
2767 if (rel->fields & MNCC_F_CAUSE)
2768 encode_cause(msg, 0, &rel->cause);
2769 /* facility */
2770 if (rel->fields & MNCC_F_FACILITY)
2771 encode_facility(msg, 0, &rel->facility);
2772 /* user-user */
2773 if (rel->fields & MNCC_F_USERUSER)
2774 encode_useruser(msg, 0, &rel->useruser);
2775
Harald Weltedcaf5652009-07-23 18:56:43 +02002776 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777
Harald Welte39e2ead2009-07-23 21:13:03 +02002778 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779}
2780
2781static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2782{
2783 struct gsm48_hdr *gh = msgb_l3(msg);
2784 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2785 struct tlv_parsed tp;
2786 struct gsm_mncc fac;
2787
2788 memset(&fac, 0, sizeof(struct gsm_mncc));
2789 fac.callref = trans->callref;
2790 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2791 /* facility */
2792 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2793 fac.fields |= MNCC_F_FACILITY;
2794 decode_facility(&fac.facility,
2795 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2796 }
2797 /* ss-version */
2798 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2799 fac.fields |= MNCC_F_SSVERSION;
2800 decode_ssversion(&fac.ssversion,
2801 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2802 }
2803
Harald Welte596fed42009-07-23 19:06:52 +02002804 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805}
2806
2807static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2808{
2809 struct gsm_mncc *fac = arg;
2810 struct msgb *msg = gsm48_msgb_alloc();
2811 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2812
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813 gh->msg_type = GSM48_MT_CC_FACILITY;
2814
2815 /* facility */
2816 encode_facility(msg, 1, &fac->facility);
2817
Harald Welte39e2ead2009-07-23 21:13:03 +02002818 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819}
2820
2821static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2822{
2823 struct gsm_mncc hold;
2824
2825 memset(&hold, 0, sizeof(struct gsm_mncc));
2826 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002827 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828}
2829
2830static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2831{
2832 struct msgb *msg = gsm48_msgb_alloc();
2833 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2834
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2836
Harald Welte39e2ead2009-07-23 21:13:03 +02002837 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838}
2839
2840static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2841{
2842 struct gsm_mncc *hold_rej = arg;
2843 struct msgb *msg = gsm48_msgb_alloc();
2844 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2845
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2847
2848 /* cause */
2849 if (hold_rej->fields & MNCC_F_CAUSE)
2850 encode_cause(msg, 1, &hold_rej->cause);
2851 else
2852 encode_cause(msg, 1, &default_cause);
2853
Harald Welte39e2ead2009-07-23 21:13:03 +02002854 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855}
2856
2857static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2858{
2859 struct gsm_mncc retrieve;
2860
2861 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2862 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002863 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2864 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865}
2866
2867static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2868{
2869 struct msgb *msg = gsm48_msgb_alloc();
2870 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2871
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2873
Harald Welte39e2ead2009-07-23 21:13:03 +02002874 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875}
2876
2877static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2878{
2879 struct gsm_mncc *retrieve_rej = arg;
2880 struct msgb *msg = gsm48_msgb_alloc();
2881 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2882
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2884
2885 /* cause */
2886 if (retrieve_rej->fields & MNCC_F_CAUSE)
2887 encode_cause(msg, 1, &retrieve_rej->cause);
2888 else
2889 encode_cause(msg, 1, &default_cause);
2890
Harald Welte39e2ead2009-07-23 21:13:03 +02002891 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892}
2893
2894static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2895{
2896 struct gsm48_hdr *gh = msgb_l3(msg);
2897 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2898 struct tlv_parsed tp;
2899 struct gsm_mncc dtmf;
2900
2901 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2902 dtmf.callref = trans->callref;
2903 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2904 /* keypad facility */
2905 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2906 dtmf.fields |= MNCC_F_KEYPAD;
2907 decode_keypad(&dtmf.keypad,
2908 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2909 }
2910
Harald Welte596fed42009-07-23 19:06:52 +02002911 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912}
2913
2914static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2915{
2916 struct gsm_mncc *dtmf = arg;
2917 struct msgb *msg = gsm48_msgb_alloc();
2918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2919
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2921
2922 /* keypad */
2923 if (dtmf->fields & MNCC_F_KEYPAD)
2924 encode_keypad(msg, dtmf->keypad);
2925
Harald Welte39e2ead2009-07-23 21:13:03 +02002926 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927}
2928
2929static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2930{
2931 struct gsm_mncc *dtmf = arg;
2932 struct msgb *msg = gsm48_msgb_alloc();
2933 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2934
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2936
2937 /* cause */
2938 if (dtmf->fields & MNCC_F_CAUSE)
2939 encode_cause(msg, 1, &dtmf->cause);
2940 else
2941 encode_cause(msg, 1, &default_cause);
2942
Harald Welte39e2ead2009-07-23 21:13:03 +02002943 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944}
2945
2946static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2947{
2948 struct msgb *msg = gsm48_msgb_alloc();
2949 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2950
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2952
Harald Welte39e2ead2009-07-23 21:13:03 +02002953 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954}
2955
2956static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2957{
2958 struct gsm_mncc dtmf;
2959
2960 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2961 dtmf.callref = trans->callref;
2962
Harald Welte596fed42009-07-23 19:06:52 +02002963 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964}
2965
2966static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2967{
2968 struct gsm48_hdr *gh = msgb_l3(msg);
2969 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2970 struct tlv_parsed tp;
2971 struct gsm_mncc modify;
2972
2973 memset(&modify, 0, sizeof(struct gsm_mncc));
2974 modify.callref = trans->callref;
2975 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2976 /* bearer capability */
2977 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2978 modify.fields |= MNCC_F_BEARER_CAP;
2979 decode_bearer_cap(&modify.bearer_cap,
2980 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2981 }
2982
2983 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2984
Harald Welte596fed42009-07-23 19:06:52 +02002985 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986}
2987
2988static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2989{
2990 struct gsm_mncc *modify = arg;
2991 struct msgb *msg = gsm48_msgb_alloc();
2992 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2993
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994 gh->msg_type = GSM48_MT_CC_MODIFY;
2995
2996 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2997
2998 /* bearer capability */
2999 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3000
3001 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3002
Harald Welte39e2ead2009-07-23 21:13:03 +02003003 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004}
3005
3006static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3007{
3008 struct gsm48_hdr *gh = msgb_l3(msg);
3009 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3010 struct tlv_parsed tp;
3011 struct gsm_mncc modify;
3012
3013 gsm48_stop_cc_timer(trans);
3014
3015 memset(&modify, 0, sizeof(struct gsm_mncc));
3016 modify.callref = trans->callref;
3017 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3018 /* bearer capability */
3019 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3020 modify.fields |= MNCC_F_BEARER_CAP;
3021 decode_bearer_cap(&modify.bearer_cap,
3022 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3023 }
3024
3025 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3026
Harald Welte596fed42009-07-23 19:06:52 +02003027 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028}
3029
3030static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3031{
3032 struct gsm_mncc *modify = arg;
3033 struct msgb *msg = gsm48_msgb_alloc();
3034 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3035
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3037
3038 /* bearer capability */
3039 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3040
3041 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3042
Harald Welte39e2ead2009-07-23 21:13:03 +02003043 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044}
3045
3046static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3047{
3048 struct gsm48_hdr *gh = msgb_l3(msg);
3049 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3050 struct tlv_parsed tp;
3051 struct gsm_mncc modify;
3052
3053 gsm48_stop_cc_timer(trans);
3054
3055 memset(&modify, 0, sizeof(struct gsm_mncc));
3056 modify.callref = trans->callref;
3057 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3058 /* bearer capability */
3059 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3060 modify.fields |= GSM48_IE_BEARER_CAP;
3061 decode_bearer_cap(&modify.bearer_cap,
3062 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3063 }
3064 /* cause */
3065 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3066 modify.fields |= MNCC_F_CAUSE;
3067 decode_cause(&modify.cause,
3068 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3069 }
3070
3071 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3072
Harald Welte596fed42009-07-23 19:06:52 +02003073 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003074}
3075
3076static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3077{
3078 struct gsm_mncc *modify = arg;
3079 struct msgb *msg = gsm48_msgb_alloc();
3080 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3081
Harald Welte4bfdfe72009-06-10 23:11:52 +08003082 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3083
3084 /* bearer capability */
3085 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3086 /* cause */
3087 encode_cause(msg, 1, &modify->cause);
3088
3089 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3090
Harald Welte39e2ead2009-07-23 21:13:03 +02003091 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092}
3093
3094static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3095{
3096 struct gsm_mncc *notify = arg;
3097 struct msgb *msg = gsm48_msgb_alloc();
3098 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3099
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 gh->msg_type = GSM48_MT_CC_NOTIFY;
3101
3102 /* notify */
3103 encode_notify(msg, notify->notify);
3104
Harald Welte39e2ead2009-07-23 21:13:03 +02003105 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106}
3107
3108static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3109{
3110 struct gsm48_hdr *gh = msgb_l3(msg);
3111 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3112// struct tlv_parsed tp;
3113 struct gsm_mncc notify;
3114
3115 memset(&notify, 0, sizeof(struct gsm_mncc));
3116 notify.callref = trans->callref;
3117// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3118 if (payload_len >= 1)
3119 decode_notify(&notify.notify, gh->data);
3120
Harald Welte596fed42009-07-23 19:06:52 +02003121 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003122}
3123
3124static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3125{
3126 struct gsm_mncc *user = arg;
3127 struct msgb *msg = gsm48_msgb_alloc();
3128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3129
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 gh->msg_type = GSM48_MT_CC_USER_INFO;
3131
3132 /* user-user */
3133 if (user->fields & MNCC_F_USERUSER)
3134 encode_useruser(msg, 1, &user->useruser);
3135 /* more data */
3136 if (user->more)
3137 encode_more(msg);
3138
Harald Welte39e2ead2009-07-23 21:13:03 +02003139 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003140}
3141
3142static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3143{
3144 struct gsm48_hdr *gh = msgb_l3(msg);
3145 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3146 struct tlv_parsed tp;
3147 struct gsm_mncc user;
3148
3149 memset(&user, 0, sizeof(struct gsm_mncc));
3150 user.callref = trans->callref;
3151 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3152 /* user-user */
3153 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3154 user.fields |= MNCC_F_USERUSER;
3155 decode_useruser(&user.useruser,
3156 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3157 }
3158 /* more data */
3159 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3160 user.more = 1;
3161
Harald Welte596fed42009-07-23 19:06:52 +02003162 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003163}
3164
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003165static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166{
3167 struct gsm_mncc *mode = arg;
3168
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003169 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170}
3171
3172static struct downstate {
3173 u_int32_t states;
3174 int type;
3175 int (*rout) (struct gsm_trans *trans, void *arg);
3176} downstatelist[] = {
3177 /* mobile originating call establishment */
3178 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3179 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3180 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3181 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3182 {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 */
3183 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3184 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3185 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3186 /* mobile terminating call establishment */
3187 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3188 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3189 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3190 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3191 /* signalling during call */
3192 {SBIT(GSM_CSTATE_ACTIVE),
3193 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3194 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3195 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3196 {ALL_STATES,
3197 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3198 {ALL_STATES,
3199 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3200 {ALL_STATES,
3201 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3202 {SBIT(GSM_CSTATE_ACTIVE),
3203 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3204 {SBIT(GSM_CSTATE_ACTIVE),
3205 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3206 {SBIT(GSM_CSTATE_ACTIVE),
3207 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3208 {SBIT(GSM_CSTATE_ACTIVE),
3209 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3210 {SBIT(GSM_CSTATE_ACTIVE),
3211 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3212 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3213 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3214 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3215 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3218 /* clearing */
3219 {SBIT(GSM_CSTATE_INITIATED),
3220 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3221 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3222 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3223 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3224 MNCC_REL_REQ, gsm48_cc_tx_release},
3225 /* special */
3226 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003227 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003228};
3229
3230#define DOWNSLLEN \
3231 (sizeof(downstatelist) / sizeof(struct downstate))
3232
3233
3234int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3235{
Harald Welte1a6f7982009-08-09 18:52:33 +02003236 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003237 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003238 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003239 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003240 struct gsm_mncc *data = arg, rel;
3241
3242 /* handle special messages */
3243 switch(msg_type) {
3244 case MNCC_BRIDGE:
3245 return tch_bridge(net, arg);
3246 case MNCC_FRAME_DROP:
3247 return tch_recv(net, arg, 0);
3248 case MNCC_FRAME_RECV:
3249 return tch_recv(net, arg, 1);
3250 case GSM_TRAU_FRAME:
3251 return tch_frame(net, arg);
3252 }
3253
3254 memset(&rel, 0, sizeof(struct gsm_mncc));
3255 rel.callref = data->callref;
3256
3257 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003258 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259
3260 /* Callref unknown */
3261 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003262 struct gsm_subscriber *subscr;
3263
Harald Welte4a3464c2009-07-04 10:11:24 +02003264 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3266 "Received '%s' from MNCC with "
3267 "unknown callref %d\n", data->called.number,
3268 get_mncc_name(msg_type), data->callref);
3269 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003270 return mncc_release_ind(net, NULL, data->callref,
3271 GSM48_CAUSE_LOC_PRN_S_LU,
3272 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003274 if (!data->called.number[0] && !data->imsi[0]) {
3275 DEBUGP(DCC, "(bts - trx - ts - ti) "
3276 "Received '%s' from MNCC with "
3277 "no number or IMSI\n", get_mncc_name(msg_type));
3278 /* Invalid number */
3279 return mncc_release_ind(net, NULL, data->callref,
3280 GSM48_CAUSE_LOC_PRN_S_LU,
3281 GSM48_CC_CAUSE_INV_NR_FORMAT);
3282 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003284 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003285 subscr = subscr_get_by_extension(net,
3286 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003287 else
Harald Welte9176bd42009-07-23 18:46:00 +02003288 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289 /* If subscriber is not found */
3290 if (!subscr) {
3291 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3292 "Received '%s' from MNCC with "
3293 "unknown subscriber %s\n", data->called.number,
3294 get_mncc_name(msg_type), data->called.number);
3295 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003296 return mncc_release_ind(net, NULL, data->callref,
3297 GSM48_CAUSE_LOC_PRN_S_LU,
3298 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003299 }
3300 /* If subscriber is not "attached" */
3301 if (!subscr->lac) {
3302 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3303 "Received '%s' from MNCC with "
3304 "detached subscriber %s\n", data->called.number,
3305 get_mncc_name(msg_type), data->called.number);
3306 subscr_put(subscr);
3307 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003308 return mncc_release_ind(net, NULL, data->callref,
3309 GSM48_CAUSE_LOC_PRN_S_LU,
3310 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003311 }
3312 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003313 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3314 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003315 DEBUGP(DCC, "No memory for trans.\n");
3316 subscr_put(subscr);
3317 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003318 mncc_release_ind(net, NULL, data->callref,
3319 GSM48_CAUSE_LOC_PRN_S_LU,
3320 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003321 return -ENOMEM;
3322 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003323 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003324 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003325 /* If subscriber has no lchan */
3326 if (!lchan) {
3327 /* find transaction with this subscriber already paging */
3328 llist_for_each_entry(transt, &net->trans_list, entry) {
3329 /* Transaction of our lchan? */
3330 if (transt == trans ||
3331 transt->subscr != subscr)
3332 continue;
3333 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3334 "Received '%s' from MNCC with "
3335 "unallocated channel, paging already "
3336 "started.\n", bts->nr,
3337 data->called.number,
3338 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003339 subscr_put(subscr);
3340 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003341 return 0;
3342 }
3343 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003344 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003345 /* Trigger paging */
3346 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3347 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003348 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003349 return 0;
3350 }
3351 /* Assign lchan */
3352 trans->lchan = lchan;
3353 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003354 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 }
3356 lchan = trans->lchan;
3357
3358 /* if paging did not respond yet */
3359 if (!lchan) {
3360 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3361 "Received '%s' from MNCC in paging state\n",
3362 (trans->subscr)?(trans->subscr->extension):"-",
3363 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003364 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3365 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003366 if (msg_type == MNCC_REL_REQ)
3367 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3368 else
3369 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3370 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003371 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003372 return rc;
3373 }
3374
3375 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3376 "Received '%s' from MNCC in state %d (%s)\n",
3377 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3378 trans->transaction_id,
3379 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003380 get_mncc_name(msg_type), trans->cc.state,
3381 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003382
3383 /* Find function for current state and message */
3384 for (i = 0; i < DOWNSLLEN; i++)
3385 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003386 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003387 break;
3388 if (i == DOWNSLLEN) {
3389 DEBUGP(DCC, "Message unhandled at this state.\n");
3390 return 0;
3391 }
3392
3393 rc = downstatelist[i].rout(trans, arg);
3394
3395 return rc;
3396}
3397
3398
3399static struct datastate {
3400 u_int32_t states;
3401 int type;
3402 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3403} datastatelist[] = {
3404 /* mobile originating call establishment */
3405 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3406 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3407 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3408 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3409 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3410 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3411 /* mobile terminating call establishment */
3412 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3413 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3414 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3415 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3416 {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 */
3417 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3418 /* signalling during call */
3419 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3420 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3421 {SBIT(GSM_CSTATE_ACTIVE),
3422 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3423 {ALL_STATES,
3424 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3425 {ALL_STATES,
3426 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3427 {ALL_STATES,
3428 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3429 {SBIT(GSM_CSTATE_ACTIVE),
3430 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3431 {SBIT(GSM_CSTATE_ACTIVE),
3432 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3433 {SBIT(GSM_CSTATE_ACTIVE),
3434 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3435 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3436 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3437 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3438 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3439 {SBIT(GSM_CSTATE_ACTIVE),
3440 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3441 /* clearing */
3442 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3443 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3444 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3445 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3446 {ALL_STATES, /* 5.4.3.4 */
3447 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3448};
3449
3450#define DATASLLEN \
3451 (sizeof(datastatelist) / sizeof(struct datastate))
3452
Harald Welte4bc90a12008-12-27 16:32:52 +00003453static int gsm0408_rcv_cc(struct msgb *msg)
3454{
3455 struct gsm48_hdr *gh = msgb_l3(msg);
3456 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003457 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003458 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003459 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003460 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003461
Harald Welte4bfdfe72009-06-10 23:11:52 +08003462 if (msg_type & 0x80) {
3463 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3464 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003465 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003466
3467 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003468 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003469
Harald Welte6f5aee02009-07-23 21:21:14 +02003470 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003471 "Received '%s' from MS in state %d (%s)\n",
3472 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3473 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003474 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003475 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476
3477 /* Create transaction */
3478 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003479 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480 "creating new trans.\n", transaction_id);
3481 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003482 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3483 transaction_id, new_callref++);
3484 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003485 DEBUGP(DCC, "No memory for trans.\n");
3486 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003487 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003488 GSM48_MT_CC_RELEASE_COMPL);
3489 return -ENOMEM;
3490 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003491 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003492 trans->lchan = lchan;
3493 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003494 }
3495
3496 /* find function for current state and message */
3497 for (i = 0; i < DATASLLEN; i++)
3498 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003499 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003500 break;
3501 if (i == DATASLLEN) {
3502 DEBUGP(DCC, "Message unhandled at this state.\n");
3503 return 0;
3504 }
3505
3506 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003507
3508 return rc;
3509}
3510
Harald Welte52b1f982008-12-23 20:25:15 +00003511/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02003512int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003513{
3514 struct gsm48_hdr *gh = msgb_l3(msg);
3515 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003516 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003517
3518 switch (pdisc) {
3519 case GSM48_PDISC_CC:
3520 rc = gsm0408_rcv_cc(msg);
3521 break;
3522 case GSM48_PDISC_MM:
3523 rc = gsm0408_rcv_mm(msg);
3524 break;
3525 case GSM48_PDISC_RR:
3526 rc = gsm0408_rcv_rr(msg);
3527 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003528 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003529 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003530 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003531 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003532 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003533 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003534 pdisc);
3535 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003536 case GSM48_PDISC_NC_SS:
3537 rc = handle_rcv_ussd(msg);
3538 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003539 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003540 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003541 pdisc);
3542 break;
3543 }
3544
3545 return rc;
3546}
Harald Welte8470bf22008-12-25 23:28:35 +00003547
Harald Welte4bfdfe72009-06-10 23:11:52 +08003548/* dequeue messages to layer 4 */
3549int bsc_upqueue(struct gsm_network *net)
3550{
3551 struct gsm_mncc *mncc;
3552 struct msgb *msg;
3553 int work = 0;
3554
3555 if (net)
3556 while ((msg = msgb_dequeue(&net->upqueue))) {
3557 mncc = (struct gsm_mncc *)msg->data;
3558 if (net->mncc_recv)
3559 net->mncc_recv(net, mncc->msg_type, mncc);
3560 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003561 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003562 }
3563
3564 return work;
3565}
Harald Weltedcaf5652009-07-23 18:56:43 +02003566
Harald Welte805f6442009-07-28 18:25:29 +02003567/*
3568 * This will be ran by the linker when loading the DSO. We use it to
3569 * do system initialization, e.g. registration of signal handlers.
3570 */
3571static __attribute__((constructor)) void on_dso_load_0408(void)
3572{
Harald Welte805f6442009-07-28 18:25:29 +02003573 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3574 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3575}