blob: a8e8bd76afd2397959c03f317987260e901cb728 [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
Harald Welte4bfdfe72009-06-10 23:11:52 +0800244static u_int32_t new_callref = 0x80000001;
245
Holger Freyther73487a22008-12-31 18:53:57 +0000246static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
247 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000248{
249 if (!subscriber)
250 return 0;
251
Holger Freyther73487a22008-12-31 18:53:57 +0000252 /*
253 * Do not send accept yet as more information should arrive. Some
254 * phones will not send us the information and we will have to check
255 * what we want to do with that.
256 */
257 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
258 return 0;
259
Jan Luebbe06513f22009-08-12 12:48:00 +0200260 switch (subscriber->net->auth_policy) {
261 case GSM_AUTH_POLICY_CLOSED:
262 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200263 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200264 if (subscriber->authorized)
265 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200266 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000268 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200269 default:
270 return 0;
271 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000272}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000273
Holger Freyther73487a22008-12-31 18:53:57 +0000274static void release_loc_updating_req(struct gsm_lchan *lchan)
275{
Harald Welte179f0642008-12-31 23:59:18 +0000276 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000277 return;
278
Harald Welteff117a82009-05-23 05:22:08 +0000279 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200280 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000281 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000282 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000283}
284
285static void allocate_loc_updating_req(struct gsm_lchan *lchan)
286{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000287 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000288 release_loc_updating_req(lchan);
289
Harald Welte470ec292009-06-26 20:25:23 +0200290 lchan->loc_operation = talloc_zero(tall_locop_ctx,
291 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000292}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000293
Holger Freytherd51524f2009-06-09 08:27:07 +0000294static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
295{
Holger Freytherd51524f2009-06-09 08:27:07 +0000296 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200297 int rc;
298
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000300 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200301 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welteee5ad162009-08-09 19:07:00 +0200302 /* call subscr_update after putting the loc_upd_acc
303 * in the transmit queue, since S_SUBSCR_ATTACHED might
304 * trigger further action like SMS delivery */
305 subscr_update(lchan->subscr, msg->trx->bts,
306 GSM_SUBSCRIBER_UPDATE_ATTACHED);
307 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000308 }
309
310 return 0;
311}
312
Holger Freyther7c19f742009-06-06 13:54:35 +0000313static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
314 void *handler_data, void *signal_data)
315{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800316 struct gsm_trans *trans, *temp;
317
Holger Freyther7c19f742009-06-06 13:54:35 +0000318 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
319 return 0;
320
321 /*
322 * Cancel any outstanding location updating request
323 * operation taking place on the lchan.
324 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200325 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200326 if (!lchan)
327 return 0;
328
Holger Freyther7c19f742009-06-06 13:54:35 +0000329 release_loc_updating_req(lchan);
330
Harald Welte4bfdfe72009-06-10 23:11:52 +0800331 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200332 /* FIXME: this is not neccessarily the right thing to do, we should
333 * only set trans->lchan to NULL and wait for another lchan to be
334 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800335 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
336 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200337 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800338 }
339
Holger Freyther7c19f742009-06-06 13:54:35 +0000340 return 0;
341}
342
Harald Welte09e38af2009-02-16 22:52:23 +0000343static const char bcd_num_digits[] = {
344 '0', '1', '2', '3', '4', '5', '6', '7',
345 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
346};
347
Harald Welte0c389302009-06-10 12:08:54 +0800348/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
349int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
350 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000351{
352 u_int8_t in_len = bcd_lv[0];
353 int i;
354
Harald Welte0c389302009-06-10 12:08:54 +0800355 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000356 /* lower nibble */
357 output_len--;
358 if (output_len <= 1)
359 break;
360 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
361
362 /* higher nibble */
363 output_len--;
364 if (output_len <= 1)
365 break;
366 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
367 }
368 if (output_len >= 1)
369 *output++ = '\0';
370
Harald Welte0c389302009-06-10 12:08:54 +0800371 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000372}
373
374/* convert a single ASCII character to call-control BCD */
375static int asc_to_bcd(const char asc)
376{
377 int i;
378
379 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
380 if (bcd_num_digits[i] == asc)
381 return i;
382 }
383 return -EINVAL;
384}
385
Harald Welte0c389302009-06-10 12:08:54 +0800386/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000387int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800388 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000389{
390 int in_len = strlen(input);
391 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800392 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000393
394 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800395 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000396 if (in_len % 2)
397 bcd_lv[0]++;
398
Harald Welte0c389302009-06-10 12:08:54 +0800399 if (bcd_lv[0] > max_len)
400 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000401
402 for (i = 0; i < in_len; i++) {
403 int rc = asc_to_bcd(input[i]);
404 if (rc < 0)
405 return rc;
406 if (i % 2 == 0)
407 *bcd_cur = rc;
408 else
409 *bcd_cur++ |= (rc << 4);
410 }
411 /* append padding nibble in case of odd length */
412 if (i % 2)
413 *bcd_cur++ |= 0xf0;
414
415 /* return how many bytes we used */
416 return (bcd_cur - bcd_lv);
417}
418
Harald Welte0c389302009-06-10 12:08:54 +0800419/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800421 const u_int8_t *lv)
422{
423 u_int8_t in_len = lv[0];
424 int i, s;
425
426 if (in_len < 1)
427 return -EINVAL;
428
Harald Welte4bfdfe72009-06-10 23:11:52 +0800429 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800430
431 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432 bcap->transfer = lv[1] & 0x07;
433 bcap->mode = (lv[1] & 0x08) >> 3;
434 bcap->coding = (lv[1] & 0x10) >> 4;
435 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800436
437 i = 1;
438 s = 0;
439 while(!(lv[i] & 0x80)) {
440 i++; /* octet 3a etc */
441 if (in_len < i)
442 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800443 bcap->speech_ver[s++] = lv[i] & 0x0f;
444 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800445 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800446 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800447 if (s == 7) /* maximum speech versions + end of list */
448 return 0;
449 }
450
451 return 0;
452}
453
454/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800455static int encode_bearer_cap(struct msgb *msg, int lv_only,
456 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800457{
458 u_int8_t lv[32 + 1];
459 int i, s;
460
Harald Welte4bfdfe72009-06-10 23:11:52 +0800461 lv[1] = bcap->transfer;
462 lv[1] |= bcap->mode << 3;
463 lv[1] |= bcap->coding << 4;
464 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800465
466 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800467 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800468 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800469 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800470 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800471 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800472 }
473 lv[i] |= 0x80; /* last IE of octet 3 etc */
474
475 lv[0] = i;
476 if (lv_only)
477 msgb_lv_put(msg, lv[0], lv+1);
478 else
479 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
480
481 return 0;
482}
483
484/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800485static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800486{
487 u_int8_t in_len = lv[0];
488
489 if (in_len < 1)
490 return -EINVAL;
491
492 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800493 ccap->dtmf = lv[1] & 0x01;
494 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800495
496 return 0;
497}
498
499/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800500static int decode_called(struct gsm_mncc_number *called,
501 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800502{
503 u_int8_t in_len = lv[0];
504
505 if (in_len < 1)
506 return -EINVAL;
507
508 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800509 called->plan = lv[1] & 0x0f;
510 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800511
512 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800513 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800514
515 return 0;
516}
517
518/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519static int encode_called(struct msgb *msg,
520 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800521{
522 u_int8_t lv[18];
523 int ret;
524
525 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800526 lv[1] = called->plan;
527 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800528
529 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800530 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800531 if (ret < 0)
532 return ret;
533
534 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
535
536 return 0;
537}
538
539/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800540static int encode_callerid(struct msgb *msg, int ie,
541 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800542{
543 u_int8_t lv[13];
544 int h_len = 1;
545 int ret;
546
547 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800548 lv[1] = callerid->plan;
549 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800550
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800552 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800553 lv[2] = callerid->screen;
554 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800555 lv[2] |= 0x80;
556 h_len++;
557 } else
558 lv[1] |= 0x80;
559
560 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800561 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800562 if (ret < 0)
563 return ret;
564
565 msgb_tlv_put(msg, ie, lv[0], lv+1);
566
567 return 0;
568}
569
570/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800571static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800572 const u_int8_t *lv)
573{
574 u_int8_t in_len = lv[0];
575 int i;
576
577 if (in_len < 2)
578 return -EINVAL;
579
Harald Welte4bfdfe72009-06-10 23:11:52 +0800580 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800581
582 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800583 cause->location = lv[1] & 0x0f;
584 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800585
586 i = 1;
587 if (!(lv[i] & 0x80)) {
588 i++; /* octet 3a */
589 if (in_len < i+1)
590 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800591 cause->rec = 1;
592 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800593
594 }
595 i++;
596
597 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800598 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800599 i++;
600
601 if (in_len < i) /* no diag */
602 return 0;
603
604 if (in_len - (i-1) > 32) /* maximum 32 octets */
605 return 0;
606
607 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800608 memcpy(cause->diag, lv + i, in_len - (i-1));
609 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800610
611 return 0;
612}
613
614/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615static int encode_cause(struct msgb *msg, int lv_only,
616 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800617{
618 u_int8_t lv[32+4];
619 int i;
620
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800622 return -EINVAL;
623
624 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800625 lv[1] = cause->location;
626 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800627
628 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800630 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800631 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800632 }
633 lv[i] |= 0x80; /* end of octet 3 */
634
635 /* octet 4 */
636 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800638
639 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800640 if (cause->diag_len) {
641 memcpy(lv + i, cause->diag, cause->diag_len);
642 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800643 }
644
645 lv[0] = i;
646 if (lv_only)
647 msgb_lv_put(msg, lv[0], lv+1);
648 else
649 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
650
651 return 0;
652}
653
654/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655static int encode_calling(struct msgb *msg,
656 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800657{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800659}
660
661/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800662static int encode_connected(struct msgb *msg,
663 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800664{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800665 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800666}
667
668/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800669static int encode_redirecting(struct msgb *msg,
670 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800671{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800672 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800673}
674
675/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800676static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800677 const u_int8_t *lv)
678{
679 u_int8_t in_len = lv[0];
680
681 if (in_len < 1)
682 return -EINVAL;
683
Harald Welte4bfdfe72009-06-10 23:11:52 +0800684 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800685 return -EINVAL;
686
Harald Welte4bfdfe72009-06-10 23:11:52 +0800687 memcpy(facility->info, lv+1, in_len);
688 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800689
690 return 0;
691}
692
693/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800694static int encode_facility(struct msgb *msg, int lv_only,
695 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800696{
697 u_int8_t lv[GSM_MAX_FACILITY + 1];
698
Harald Welte4bfdfe72009-06-10 23:11:52 +0800699 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800700 return -EINVAL;
701
Harald Welte4bfdfe72009-06-10 23:11:52 +0800702 memcpy(lv+1, facility->info, facility->len);
703 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800704 if (lv_only)
705 msgb_lv_put(msg, lv[0], lv+1);
706 else
707 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
708
709 return 0;
710}
711
712/* decode 'notify' */
713static int decode_notify(int *notify, const u_int8_t *v)
714{
715 *notify = v[0] & 0x7f;
716
717 return 0;
718}
719
720/* encode 'notify' */
721static int encode_notify(struct msgb *msg, int notify)
722{
723 msgb_v_put(msg, notify | 0x80);
724
725 return 0;
726}
727
728/* encode 'signal' */
729static int encode_signal(struct msgb *msg, int signal)
730{
731 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
732
733 return 0;
734}
735
736/* decode 'keypad' */
737static int decode_keypad(int *keypad, const u_int8_t *lv)
738{
739 u_int8_t in_len = lv[0];
740
741 if (in_len < 1)
742 return -EINVAL;
743
744 *keypad = lv[1] & 0x7f;
745
746 return 0;
747}
748
749/* encode 'keypad' */
750static int encode_keypad(struct msgb *msg, int keypad)
751{
752 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
753
754 return 0;
755}
756
757/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800758static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800759 const u_int8_t *lv)
760{
761 u_int8_t in_len = lv[0];
762
763 if (in_len < 2)
764 return -EINVAL;
765
Harald Welte4bfdfe72009-06-10 23:11:52 +0800766 progress->coding = (lv[1] & 0x60) >> 5;
767 progress->location = lv[1] & 0x0f;
768 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800769
770 return 0;
771}
772
773/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800774static int encode_progress(struct msgb *msg, int lv_only,
775 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800776{
777 u_int8_t lv[3];
778
779 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800780 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
781 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800782 if (lv_only)
783 msgb_lv_put(msg, lv[0], lv+1);
784 else
785 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
786
787 return 0;
788}
789
790/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800791static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800792 const u_int8_t *lv)
793{
794 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800795 char *info = uu->info;
796 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800797 int i;
798
799 if (in_len < 1)
800 return -EINVAL;
801
Harald Welte4bfdfe72009-06-10 23:11:52 +0800802 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800803
804 for (i = 2; i <= in_len; i++) {
805 info_len--;
806 if (info_len <= 1)
807 break;
808 *info++ = lv[i];
809 }
810 if (info_len >= 1)
811 *info++ = '\0';
812
813 return 0;
814}
815
816/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800817static int encode_useruser(struct msgb *msg, int lv_only,
818 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800819{
820 u_int8_t lv[GSM_MAX_USERUSER + 2];
821
Harald Welte4bfdfe72009-06-10 23:11:52 +0800822 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800823 return -EINVAL;
824
Harald Welte4bfdfe72009-06-10 23:11:52 +0800825 lv[0] = 1 + strlen(uu->info);
826 lv[1] = uu->proto;
827 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800828 if (lv_only)
829 msgb_lv_put(msg, lv[0], lv+1);
830 else
831 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
832
833 return 0;
834}
835
836/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800837static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800838 const u_int8_t *lv)
839{
840 u_int8_t in_len = lv[0];
841
Harald Welte4bfdfe72009-06-10 23:11:52 +0800842 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800843 return -EINVAL;
844
Harald Welte4bfdfe72009-06-10 23:11:52 +0800845 memcpy(ssv->info, lv + 1, in_len);
846 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800847
848 return 0;
849}
850
851/* encode 'more data' */
852static int encode_more(struct msgb *msg)
853{
854 u_int8_t *ie;
855
856 ie = msgb_put(msg, 1);
857 ie[0] = GSM48_IE_MORE_DATA;
858
859 return 0;
860}
861
Holger Freyther429e7762008-12-30 13:28:30 +0000862/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000863int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000864{
Harald Welte8470bf22008-12-25 23:28:35 +0000865 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000866 struct gsm48_hdr *gh;
867
Harald Welte8470bf22008-12-25 23:28:35 +0000868 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000869
870 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
871 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000872 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000873 gh->data[0] = cause;
874
Harald Weltedb253af2008-12-30 17:56:55 +0000875 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
876
Harald Welte39e2ead2009-07-23 21:13:03 +0200877 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000878}
879
880/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000881int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000882{
Harald Welte8470bf22008-12-25 23:28:35 +0000883 struct gsm_bts *bts = lchan->ts->trx->bts;
884 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000885 struct gsm48_hdr *gh;
886 struct gsm48_loc_area_id *lai;
887 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000888 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000889
Harald Welte8470bf22008-12-25 23:28:35 +0000890 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000891
892 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
893 gh->proto_discr = GSM48_PDISC_MM;
894 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
895
896 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000897 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000898 bts->network->network_code, bts->location_area_code);
899
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200900 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200901 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000902
903 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
904
Harald Welte39e2ead2009-07-23 21:13:03 +0200905 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000906
Harald Welteee5ad162009-08-09 19:07:00 +0200907 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000908 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000909
Holger Freyther07cc8d82008-12-29 06:23:46 +0000910 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000911}
912
Harald Weltebf5e8df2009-02-03 12:59:45 +0000913/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000914static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
915{
916 struct msgb *msg = gsm48_msgb_alloc();
917 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000918
Harald Welte231ad4f2008-12-27 11:15:38 +0000919 msg->lchan = lchan;
920
921 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
922 gh->proto_discr = GSM48_PDISC_MM;
923 gh->msg_type = GSM48_MT_MM_ID_REQ;
924 gh->data[0] = id_type;
925
Harald Welte39e2ead2009-07-23 21:13:03 +0200926 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000927}
928
Harald Welte231ad4f2008-12-27 11:15:38 +0000929
Harald Weltebf5e8df2009-02-03 12:59:45 +0000930/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000931static int mm_rx_id_resp(struct msgb *msg)
932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000934 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200935 struct gsm_bts *bts = lchan->ts->trx->bts;
936 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000937 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200938 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000939
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200940 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000941 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000942 mi_type, mi_string);
943
Harald Welte75a983f2008-12-27 21:34:06 +0000944 switch (mi_type) {
945 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200946 /* look up subscriber based on IMSI, create if not found */
947 if (!lchan->subscr) {
948 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200949 if (!lchan->subscr)
950 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200951 }
Holger Freyther73487a22008-12-31 18:53:57 +0000952 if (lchan->loc_operation)
953 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000954 break;
955 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000956 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000957 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200958 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000959 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200960 db_sync_equipment(&lchan->subscr->equipment);
961 }
Holger Freyther73487a22008-12-31 18:53:57 +0000962 if (lchan->loc_operation)
963 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000964 break;
965 }
Holger Freyther73487a22008-12-31 18:53:57 +0000966
967 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000968 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000969}
970
Harald Welte255539c2008-12-28 02:26:27 +0000971
972static void loc_upd_rej_cb(void *data)
973{
974 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100975 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000976
Holger Freyther73487a22008-12-31 18:53:57 +0000977 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100978 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000979 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000980}
981
Holger Freytherb7193e42008-12-29 17:44:08 +0000982static void schedule_reject(struct gsm_lchan *lchan)
983{
Holger Freyther73487a22008-12-31 18:53:57 +0000984 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
985 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000986 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000987}
988
Harald Welte2a139372009-02-22 21:14:55 +0000989static const char *lupd_name(u_int8_t type)
990{
991 switch (type) {
992 case GSM48_LUPD_NORMAL:
993 return "NORMAL";
994 case GSM48_LUPD_PERIODIC:
995 return "PEROIDOC";
996 case GSM48_LUPD_IMSI_ATT:
997 return "IMSI ATTACH";
998 default:
999 return "UNKNOWN";
1000 }
1001}
1002
Harald Weltebf5e8df2009-02-03 12:59:45 +00001003/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001004static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001005{
Harald Welte8470bf22008-12-25 23:28:35 +00001006 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001007 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001008 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001009 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001010 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001011 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001012 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001013 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001014
Harald Welte8470bf22008-12-25 23:28:35 +00001015 lu = (struct gsm48_loc_upd_req *) gh->data;
1016
1017 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001018
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001019 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001020
Harald Weltea0368542009-06-27 02:58:43 +02001021 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001022 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001023
Holger Freythereaf04692009-06-06 13:54:44 +00001024 /*
1025 * Pseudo Spoof detection: Just drop a second/concurrent
1026 * location updating request.
1027 */
1028 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001029 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001030 lchan->loc_operation);
1031 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1032 return 0;
1033 }
1034
Holger Freyther73487a22008-12-31 18:53:57 +00001035 allocate_loc_updating_req(lchan);
1036
Harald Welte52b1f982008-12-23 20:25:15 +00001037 switch (mi_type) {
1038 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001039 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001040 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001041 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001042 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001043
Jan Luebbe370b41d2009-08-12 10:19:34 +02001044 /* look up subscriber based on IMSI, create if not found */
1045 subscr = subscr_get_by_imsi(bts->network, mi_string);
1046 if (!subscr) {
1047 subscr = db_create_subscriber(bts->network, mi_string);
1048 }
Harald Welte4b634542008-12-27 01:55:51 +00001049 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001050 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001051 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001052 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001053 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001054 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001055
Harald Welte52b1f982008-12-23 20:25:15 +00001056 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001057 subscr = subscr_get_by_tmsi(bts->network,
1058 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001059 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001060 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001061 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001062 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001063 }
1064 break;
1065 case GSM_MI_TYPE_IMEI:
1066 case GSM_MI_TYPE_IMEISV:
1067 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001069 break;
1070 default:
Harald Weltea0368542009-06-27 02:58:43 +02001071 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001072 break;
1073 }
1074
Harald Welte24516ea2009-07-04 10:18:00 +02001075 /* schedule the reject timer */
1076 schedule_reject(lchan);
1077
Harald Welte4bfdfe72009-06-10 23:11:52 +08001078 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001079 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001080 /* FIXME: request id? close channel? */
1081 return -EINVAL;
1082 }
1083
Harald Welte255539c2008-12-28 02:26:27 +00001084 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001085 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001086
Harald Welte24516ea2009-07-04 10:18:00 +02001087 /* check if we can let the subscriber into our network immediately
1088 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001089 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001090}
1091
Harald Welte4bfdfe72009-06-10 23:11:52 +08001092#if 0
1093static u_int8_t to_bcd8(u_int8_t val)
1094{
1095 return ((val / 10) << 4) | (val % 10);
1096}
1097#endif
1098
Harald Weltedb253af2008-12-30 17:56:55 +00001099/* Section 9.2.15a */
1100int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1101{
1102 struct msgb *msg = gsm48_msgb_alloc();
1103 struct gsm48_hdr *gh;
1104 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001105 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001106 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001107#if 0
1108 time_t cur_t;
1109 struct tm* cur_time;
1110 int tz15min;
1111#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001112
1113 msg->lchan = lchan;
1114
1115 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1116 gh->proto_discr = GSM48_PDISC_MM;
1117 gh->msg_type = GSM48_MT_MM_INFO;
1118
1119 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001120#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001121 name_len = strlen(net->name_long);
1122 /* 10.5.3.5a */
1123 ptr8 = msgb_put(msg, 3);
1124 ptr8[0] = GSM48_IE_NAME_LONG;
1125 ptr8[1] = name_len*2 +1;
1126 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1127
1128 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1129 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001130 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001131
1132 /* FIXME: Use Cell Broadcast, not UCS-2, since
1133 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001134#endif
1135 name_len = (strlen(net->name_long)*7)/8;
1136 name_pad = (8 - strlen(net->name_long)*7)%8;
1137 if (name_pad > 0)
1138 name_len++;
1139 /* 10.5.3.5a */
1140 ptr8 = msgb_put(msg, 3);
1141 ptr8[0] = GSM48_IE_NAME_LONG;
1142 ptr8[1] = name_len +1;
1143 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1144
1145 ptr8 = msgb_put(msg, name_len);
1146 gsm_7bit_encode(ptr8, net->name_long);
1147
Harald Weltedb253af2008-12-30 17:56:55 +00001148 }
1149
1150 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001151#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001152 name_len = strlen(net->name_short);
1153 /* 10.5.3.5a */
1154 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001155 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001156 ptr8[1] = name_len*2 + 1;
1157 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1158
Harald Weltee872cb12009-01-01 00:33:37 +00001159 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001160 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001161 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001162#endif
1163 name_len = (strlen(net->name_short)*7)/8;
1164 name_pad = (8 - strlen(net->name_short)*7)%8;
1165 if (name_pad > 0)
1166 name_len++;
1167 /* 10.5.3.5a */
1168 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1169 ptr8[0] = GSM48_IE_NAME_SHORT;
1170 ptr8[1] = name_len +1;
1171 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1172
1173 ptr8 = msgb_put(msg, name_len);
1174 gsm_7bit_encode(ptr8, net->name_short);
1175
Harald Weltedb253af2008-12-30 17:56:55 +00001176 }
1177
1178#if 0
1179 /* Section 10.5.3.9 */
1180 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001181 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001182 ptr8 = msgb_put(msg, 8);
1183 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1184 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1185 ptr8[2] = to_bcd8(cur_time->tm_mon);
1186 ptr8[3] = to_bcd8(cur_time->tm_mday);
1187 ptr8[4] = to_bcd8(cur_time->tm_hour);
1188 ptr8[5] = to_bcd8(cur_time->tm_min);
1189 ptr8[6] = to_bcd8(cur_time->tm_sec);
1190 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1191 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001192 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001193 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001194 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001195#endif
1196
Daniel Willmanneea93372009-08-13 03:42:07 +02001197 DEBUGP(DMM, "-> MM INFO\n");
1198
Harald Welte39e2ead2009-07-23 21:13:03 +02001199 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001200}
1201
Harald Welte7984d5c2009-08-12 22:56:50 +02001202/* Section 9.2.2 */
1203int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1204{
1205 struct msgb *msg = gsm48_msgb_alloc();
1206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001207 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001208
1209 DEBUGP(DMM, "-> AUTH REQ\n");
1210
1211 msg->lchan = lchan;
1212 gh->proto_discr = GSM48_PDISC_MM;
1213 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1214
Sylvain Munaut849f5542009-09-27 11:10:17 +02001215 /* Key Sequence: FIXME fixed to 0 */
1216 ar->key_seq = 0;
1217
Harald Welte7984d5c2009-08-12 22:56:50 +02001218 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001219 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001220 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001221
1222 return gsm48_sendmsg(msg, NULL);
1223}
1224
1225/* Section 9.2.1 */
1226int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1227{
1228 DEBUGP(DMM, "-> AUTH REJECT\n");
1229 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1230}
1231
Harald Welte4b634542008-12-27 01:55:51 +00001232static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1233{
Harald Welte4b634542008-12-27 01:55:51 +00001234 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001235 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001236}
Harald Welteba4cf162009-01-10 01:49:35 +00001237
1238/* 9.2.6 CM service reject */
1239static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1240 enum gsm48_reject_value value)
1241{
1242 struct msgb *msg = gsm48_msgb_alloc();
1243 struct gsm48_hdr *gh;
1244
1245 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1246
1247 msg->lchan = lchan;
1248 use_lchan(lchan);
1249
1250 gh->proto_discr = GSM48_PDISC_MM;
1251 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1252 gh->data[0] = value;
1253 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1254
Harald Welte39e2ead2009-07-23 21:13:03 +02001255 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001256}
1257
Harald Welte4ed0e922009-01-10 03:17:30 +00001258/*
1259 * Handle CM Service Requests
1260 * a) Verify that the packet is long enough to contain the information
1261 * we require otherwsie reject with INCORRECT_MESSAGE
1262 * b) Try to parse the TMSI. If we do not have one reject
1263 * c) Check that we know the subscriber with the TMSI otherwise reject
1264 * with a HLR cause
1265 * d) Set the subscriber on the gsm_lchan and accept
1266 */
Harald Welte4b634542008-12-27 01:55:51 +00001267static int gsm48_rx_mm_serv_req(struct msgb *msg)
1268{
Harald Welteba4cf162009-01-10 01:49:35 +00001269 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001270 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001271
Harald Welte9176bd42009-07-23 18:46:00 +02001272 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001273 struct gsm_subscriber *subscr;
1274 struct gsm48_hdr *gh = msgb_l3(msg);
1275 struct gsm48_service_request *req =
1276 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001277 /* unfortunately in Phase1 the classmar2 length is variable */
1278 u_int8_t classmark2_len = gh->data[1];
1279 u_int8_t *classmark2 = gh->data+2;
1280 u_int8_t mi_len = *(classmark2 + classmark2_len);
1281 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001282
Harald Weltec9e02182009-05-01 19:07:53 +00001283 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001284 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001285 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001286 return gsm48_tx_mm_serv_rej(msg->lchan,
1287 GSM48_REJECT_INCORRECT_MESSAGE);
1288 }
1289
1290 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001291 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001292 return gsm48_tx_mm_serv_rej(msg->lchan,
1293 GSM48_REJECT_INCORRECT_MESSAGE);
1294 }
1295
Harald Weltec9e02182009-05-01 19:07:53 +00001296 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001297 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001298 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001299 return gsm48_tx_mm_serv_rej(msg->lchan,
1300 GSM48_REJECT_INCORRECT_MESSAGE);
1301 }
1302
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001303 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001304 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001305 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001306
Harald Welte3ac7f102009-08-10 10:12:45 +02001307 if (is_siemens_bts(bts))
1308 send_siemens_mrpci(msg->lchan, classmark2-1);
1309
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001310 subscr = subscr_get_by_tmsi(bts->network,
1311 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001312
Harald Welte2a139372009-02-22 21:14:55 +00001313 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001314 if (!subscr)
1315 return gsm48_tx_mm_serv_rej(msg->lchan,
1316 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1317
1318 if (!msg->lchan->subscr)
1319 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001320 else if (msg->lchan->subscr != subscr) {
1321 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1322 subscr_put(subscr);
1323 }
1324
Harald Weltec2e302d2009-07-05 14:08:13 +02001325 subscr->equipment.classmark2_len = classmark2_len;
1326 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1327 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001328
Harald Welte4b634542008-12-27 01:55:51 +00001329 return gsm48_tx_mm_serv_ack(msg->lchan);
1330}
1331
Harald Welte2a139372009-02-22 21:14:55 +00001332static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1333{
Harald Welte9176bd42009-07-23 18:46:00 +02001334 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001335 struct gsm48_hdr *gh = msgb_l3(msg);
1336 struct gsm48_imsi_detach_ind *idi =
1337 (struct gsm48_imsi_detach_ind *) gh->data;
1338 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001339 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001341
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001342 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001343 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1344 mi_type, mi_string);
1345
1346 switch (mi_type) {
1347 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001348 subscr = subscr_get_by_tmsi(bts->network,
1349 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001350 break;
1351 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001352 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001353 break;
1354 case GSM_MI_TYPE_IMEI:
1355 case GSM_MI_TYPE_IMEISV:
1356 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001357 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001358 break;
1359 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001360 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001361 break;
1362 }
1363
Holger Freyther4a49e772009-04-12 05:37:29 +00001364 if (subscr) {
1365 subscr_update(subscr, msg->trx->bts,
1366 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001367 DEBUGP(DMM, "Subscriber: %s\n",
1368 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001369
1370 subscr->equipment.classmark1 = idi->classmark1;
1371 db_sync_equipment(&subscr->equipment);
1372
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001373 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001374 } else
Harald Welte2a139372009-02-22 21:14:55 +00001375 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1376
Harald Welte2a139372009-02-22 21:14:55 +00001377 return 0;
1378}
1379
Harald Welted2a7f5a2009-06-05 20:08:20 +00001380static int gsm48_rx_mm_status(struct msgb *msg)
1381{
1382 struct gsm48_hdr *gh = msgb_l3(msg);
1383
1384 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1385
1386 return 0;
1387}
1388
Harald Weltebf5e8df2009-02-03 12:59:45 +00001389/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001390static int gsm0408_rcv_mm(struct msgb *msg)
1391{
1392 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001394
1395 switch (gh->msg_type & 0xbf) {
1396 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001397 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001398 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001399 break;
1400 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001401 rc = mm_rx_id_resp(msg);
1402 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001403 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001404 rc = gsm48_rx_mm_serv_req(msg);
1405 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001406 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001407 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001408 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001409 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001410 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1411 msg->lchan->subscr ?
1412 msg->lchan->subscr->imsi :
1413 "unknown subscriber");
1414 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001415 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001416 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1417 break;
1418 case GSM48_MT_MM_CM_REEST_REQ:
1419 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1420 break;
1421 case GSM48_MT_MM_AUTH_RESP:
1422 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001423 break;
1424 default:
1425 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1426 gh->msg_type);
1427 break;
1428 }
1429
1430 return rc;
1431}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001432
Harald Welte2d35ae62009-02-06 12:02:13 +00001433/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001434static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001435{
Harald Welte9176bd42009-07-23 18:46:00 +02001436 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001437 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001438 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001439 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001440 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001442 int rc = 0;
1443
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001444 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001445 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1446 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001447
Harald Weltefe18d8f2009-02-22 21:14:24 +00001448 switch (mi_type) {
1449 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001450 subscr = subscr_get_by_tmsi(bts->network,
1451 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001452 break;
1453 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001454 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001455 break;
1456 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001457
1458 if (!subscr) {
1459 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001460 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001461 return -EINVAL;
1462 }
1463 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001464 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001465
Harald Weltec2e302d2009-07-05 14:08:13 +02001466 subscr->equipment.classmark2_len = *classmark2_lv;
1467 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1468 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001469
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001470 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001471 return rc;
1472}
1473
Harald Weltef7c43522009-06-09 20:24:21 +00001474static int gsm48_rx_rr_classmark(struct msgb *msg)
1475{
1476 struct gsm48_hdr *gh = msgb_l3(msg);
1477 struct gsm_subscriber *subscr = msg->lchan->subscr;
1478 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1479 u_int8_t cm2_len, cm3_len = 0;
1480 u_int8_t *cm2, *cm3 = NULL;
1481
1482 DEBUGP(DRR, "CLASSMARK CHANGE ");
1483
1484 /* classmark 2 */
1485 cm2_len = gh->data[0];
1486 cm2 = &gh->data[1];
1487 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1488
1489 if (payload_len > cm2_len + 1) {
1490 /* we must have a classmark3 */
1491 if (gh->data[cm2_len+1] != 0x20) {
1492 DEBUGPC(DRR, "ERR CM3 TAG\n");
1493 return -EINVAL;
1494 }
1495 if (cm2_len > 3) {
1496 DEBUGPC(DRR, "CM2 too long!\n");
1497 return -EINVAL;
1498 }
1499
1500 cm3_len = gh->data[cm2_len+2];
1501 cm3 = &gh->data[cm2_len+3];
1502 if (cm3_len > 14) {
1503 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1504 return -EINVAL;
1505 }
1506 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1507 }
1508 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001509 subscr->equipment.classmark2_len = cm2_len;
1510 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001511 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001512 subscr->equipment.classmark3_len = cm3_len;
1513 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001514 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001515 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001516 }
1517
Harald Weltef7c43522009-06-09 20:24:21 +00001518 return 0;
1519}
1520
Harald Weltecf5b3592009-05-01 18:28:42 +00001521static int gsm48_rx_rr_status(struct msgb *msg)
1522{
1523 struct gsm48_hdr *gh = msgb_l3(msg);
1524
1525 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1526 rr_cause_name(gh->data[0]));
1527
1528 return 0;
1529}
1530
Harald Weltef7c43522009-06-09 20:24:21 +00001531static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1532{
Harald Weltef7c43522009-06-09 20:24:21 +00001533 static struct gsm_meas_rep meas_rep;
1534
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001535 /* This shouldn't actually end up here, as RSL treats
1536 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1537 * directly into gsm48_parse_meas_rep */
1538 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1539 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001540
1541 return 0;
1542}
1543
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001544static int gsm48_rx_rr_app_info(struct msgb *msg)
1545{
1546 struct gsm48_hdr *gh = msgb_l3(msg);
1547 u_int8_t apdu_id_flags;
1548 u_int8_t apdu_len;
1549 u_int8_t *apdu_data;
1550
1551 apdu_id_flags = gh->data[0];
1552 apdu_len = gh->data[1];
1553 apdu_data = gh->data+2;
1554
1555 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1556 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1557
Harald Welte (local)026531e2009-08-16 10:40:10 +02001558 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001559}
1560
Harald Welted011e8b2009-11-29 22:45:52 +01001561/* Chapter 9.1.16 Handover complete */
1562static gsm48_rx_rr_ho_compl(struct msgb *msg)
1563{
1564 struct gsm48_hdr *gh = msgb_l3(msg);
1565
1566 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1567 rr_cause_name(gh->data[0]));
1568
1569 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1570 /* FIXME: release old channel */
1571
1572 return 0;
1573}
1574
1575/* Chapter 9.1.17 Handover Failure */
1576static gsm48_rx_rr_ho_fail(struct msgb *msg)
1577{
1578 struct gsm48_hdr *gh = msgb_l3(msg);
1579
1580 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1581 rr_cause_name(gh->data[0]));
1582
1583 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1584 /* FIXME: release allocated new channel */
1585
1586 return 0;
1587}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001588
Harald Weltebf5e8df2009-02-03 12:59:45 +00001589/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001590static int gsm0408_rcv_rr(struct msgb *msg)
1591{
1592 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001593 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001594
1595 switch (gh->msg_type) {
1596 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001597 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001598 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001599 case GSM48_MT_RR_GPRS_SUSP_REQ:
1600 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1601 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001602 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001603 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001604 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001605 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001606 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001607 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001608 case GSM48_MT_RR_STATUS:
1609 rc = gsm48_rx_rr_status(msg);
1610 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001611 case GSM48_MT_RR_MEAS_REP:
1612 rc = gsm48_rx_rr_meas_rep(msg);
1613 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001614 case GSM48_MT_RR_APP_INFO:
1615 rc = gsm48_rx_rr_app_info(msg);
1616 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001617 case GSM48_MT_RR_CIPH_M_COMPL:
1618 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1619 /* FIXME: check for MI (if any) */
1620 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001621 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001622 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001623 break;
1624 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001625 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001626 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001627 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001628 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001629 gh->msg_type);
1630 break;
1631 }
1632
Harald Welte2d35ae62009-02-06 12:02:13 +00001633 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001634}
1635
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001636int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001637 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001638{
1639 struct msgb *msg = gsm48_msgb_alloc();
1640 struct gsm48_hdr *gh;
1641
1642 msg->lchan = lchan;
1643
1644 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1645 apdu_id, apdu_len);
1646
1647 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1648 gh->proto_discr = GSM48_PDISC_RR;
1649 gh->msg_type = GSM48_MT_RR_APP_INFO;
1650 gh->data[0] = apdu_id;
1651 gh->data[1] = apdu_len;
1652 memcpy(gh->data+2, apdu, apdu_len);
1653
1654 return gsm48_sendmsg(msg, NULL);
1655}
1656
Harald Welte4bc90a12008-12-27 16:32:52 +00001657/* Call Control */
1658
Harald Welte7584aea2009-02-11 11:44:12 +00001659/* The entire call control code is written in accordance with Figure 7.10c
1660 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1661 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1662 * it for voice */
1663
Harald Welte4bfdfe72009-06-10 23:11:52 +08001664static void new_cc_state(struct gsm_trans *trans, int state)
1665{
1666 if (state > 31 || state < 0)
1667 return;
1668
1669 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001670 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671
Harald Weltedcaf5652009-07-23 18:56:43 +02001672 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673}
1674
1675static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001676{
1677 struct msgb *msg = gsm48_msgb_alloc();
1678 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1679 u_int8_t *cause, *call_state;
1680
Harald Welte4bc90a12008-12-27 16:32:52 +00001681 gh->msg_type = GSM48_MT_CC_STATUS;
1682
1683 cause = msgb_put(msg, 3);
1684 cause[0] = 2;
1685 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1686 cause[2] = 0x80 | 30; /* response to status inquiry */
1687
1688 call_state = msgb_put(msg, 1);
1689 call_state[0] = 0xc0 | 0x00;
1690
Harald Welte39e2ead2009-07-23 21:13:03 +02001691 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001692}
1693
Harald Welte6f4b7532008-12-29 00:39:37 +00001694static int gsm48_tx_simple(struct gsm_lchan *lchan,
1695 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001696{
1697 struct msgb *msg = gsm48_msgb_alloc();
1698 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1699
1700 msg->lchan = lchan;
1701
Harald Welte6f4b7532008-12-29 00:39:37 +00001702 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001703 gh->msg_type = msg_type;
1704
Harald Welte39e2ead2009-07-23 21:13:03 +02001705 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001706}
1707
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1709{
Harald Weltedcaf5652009-07-23 18:56:43 +02001710 if (bsc_timer_pending(&trans->cc.timer)) {
1711 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1712 bsc_del_timer(&trans->cc.timer);
1713 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 }
1715}
1716
1717static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1718 int msg_type, struct gsm_mncc *mncc)
1719{
1720 struct msgb *msg;
1721
1722 if (trans)
1723 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001724 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 "Sending '%s' to MNCC.\n",
1726 trans->lchan->ts->trx->bts->nr,
1727 trans->lchan->ts->trx->nr,
1728 trans->lchan->ts->nr, trans->transaction_id,
1729 (trans->subscr)?(trans->subscr->extension):"-",
1730 get_mncc_name(msg_type));
1731 else
1732 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1733 "Sending '%s' to MNCC.\n",
1734 (trans->subscr)?(trans->subscr->extension):"-",
1735 get_mncc_name(msg_type));
1736 else
1737 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1738 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1739
1740 mncc->msg_type = msg_type;
1741
Harald Welte966636f2009-06-26 19:39:35 +02001742 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 if (!msg)
1744 return -ENOMEM;
1745 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1746 msgb_enqueue(&net->upqueue, msg);
1747
1748 return 0;
1749}
1750
1751int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1752 u_int32_t callref, int location, int value)
1753{
1754 struct gsm_mncc rel;
1755
Harald Welte92f70c52009-06-12 01:54:08 +08001756 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001758 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1760}
1761
Harald Weltedcaf5652009-07-23 18:56:43 +02001762/* Call Control Specific transaction release.
1763 * gets called by trans_free, DO NOT CALL YOURSELF! */
1764void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 gsm48_stop_cc_timer(trans);
1767
1768 /* send release to L4, if callref still exists */
1769 if (trans->callref) {
1770 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001771 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001772 GSM48_CAUSE_LOC_PRN_S_LU,
1773 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001775 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001777 if (trans->lchan)
1778 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779}
1780
1781static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1782
Harald Welte09e38af2009-02-16 22:52:23 +00001783/* call-back from paging the B-end of the connection */
1784static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001785 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001786{
Harald Welte7ccf7782009-02-17 01:43:01 +00001787 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 struct gsm_subscriber *subscr = param;
1789 struct gsm_trans *transt, *tmp;
1790 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001791
Harald Welte09e38af2009-02-16 22:52:23 +00001792 if (hooknum != GSM_HOOK_RR_PAGING)
1793 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794
1795 if (!subscr)
1796 return -EINVAL;
1797 net = subscr->net;
1798 if (!net) {
1799 DEBUGP(DCC, "Error Network not set!\n");
1800 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001801 }
Harald Welte7584aea2009-02-11 11:44:12 +00001802
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 /* check all tranactions (without lchan) for subscriber */
1804 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1805 if (transt->subscr != subscr || transt->lchan)
1806 continue;
1807 switch (event) {
1808 case GSM_PAGING_SUCCEEDED:
1809 if (!lchan) // paranoid
1810 break;
1811 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1812 subscr->extension);
1813 /* Assign lchan */
1814 if (!transt->lchan) {
1815 transt->lchan = lchan;
1816 use_lchan(lchan);
1817 }
1818 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001819 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 break;
1821 case GSM_PAGING_EXPIRED:
1822 DEBUGP(DCC, "Paging subscr %s expired!\n",
1823 subscr->extension);
1824 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001825 mncc_release_ind(transt->subscr->net, transt,
1826 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001827 GSM48_CAUSE_LOC_PRN_S_LU,
1828 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001830 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 break;
1832 }
1833 }
Harald Welte09e38af2009-02-16 22:52:23 +00001834 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001835}
Harald Welte7584aea2009-02-11 11:44:12 +00001836
Harald Welte805f6442009-07-28 18:25:29 +02001837/* some other part of the code sends us a signal */
1838static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1839 void *handler_data, void *signal_data)
1840{
1841 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001842 int rc;
1843
1844 if (subsys != SS_ABISIP)
1845 return 0;
1846
1847 /* in case we use direct BTS-to-BTS RTP */
1848 if (ipacc_rtp_direct)
1849 return 0;
1850
Harald Welte805f6442009-07-28 18:25:29 +02001851 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001852 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001853 /* the BTS has successfully bound a TCH to a local ip/port,
1854 * which means we can connect our UDP socket to it */
Harald Welte2c828992009-12-02 01:56:49 +05301855 if (lchan->abis_ip.rtp_socket) {
1856 rtp_socket_free(lchan->abis_ip.rtp_socket);
1857 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001858 }
1859
Harald Welte2c828992009-12-02 01:56:49 +05301860 lchan->abis_ip.rtp_socket = rtp_socket_create();
1861 if (!lchan->abis_ip.rtp_socket)
Harald Welte805f6442009-07-28 18:25:29 +02001862 goto out_err;
1863
Harald Welte2c828992009-12-02 01:56:49 +05301864 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1865 lchan->abis_ip.bound_ip,
1866 lchan->abis_ip.bound_port);
Harald Welte805f6442009-07-28 18:25:29 +02001867 if (rc < 0)
1868 goto out_err;
1869 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001870 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001871 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte2c828992009-12-02 01:56:49 +05301872 if (lchan->abis_ip.rtp_socket) {
1873 rtp_socket_free(lchan->abis_ip.rtp_socket);
1874 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001875 }
1876 break;
1877 }
1878
1879 return 0;
1880out_err:
1881 /* FIXME: do something */
1882 return 0;
1883}
1884
1885/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1886static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1887{
Harald Welte2c828992009-12-02 01:56:49 +05301888 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte805f6442009-07-28 18:25:29 +02001889 int rc;
1890
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001891 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001892 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte2c828992009-12-02 01:56:49 +05301893 lchan->abis_ip.conn_id,
Harald Welte805f6442009-07-28 18:25:29 +02001894 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte2c828992009-12-02 01:56:49 +05301895 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001896
1897 return rc;
1898}
1899
Harald Welte49f48b82009-02-17 15:29:33 +00001900/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001901static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001902{
Harald Welte11fa29c2009-02-19 17:24:39 +00001903 struct gsm_bts *bts = lchan->ts->trx->bts;
1904 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001905 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001906
Harald Welte11fa29c2009-02-19 17:24:39 +00001907 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1908 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1909 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1910
1911 if (bts->type != remote_bts->type) {
1912 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1913 return -EINVAL;
1914 }
Harald Welte49f48b82009-02-17 15:29:33 +00001915
Harald Welte11fa29c2009-02-19 17:24:39 +00001916 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001917 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001918 if (!ipacc_rtp_direct) {
1919 /* connect the TCH's to our RTP proxy */
1920 rc = ipacc_connect_proxy_bind(lchan);
1921 if (rc < 0)
1922 return rc;
1923 rc = ipacc_connect_proxy_bind(remote_lchan);
1924
1925 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301926 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1927 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001928 } else {
1929 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301930 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1931 remote_lchan->abis_ip.bound_port,
1932 lchan->abis_ip.conn_id,
1933 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001934 if (rc < 0)
1935 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301936 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1937 lchan->abis_ip.bound_port,
1938 remote_lchan->abis_ip.conn_id,
1939 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001940 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001941 break;
1942 case GSM_BTS_TYPE_BS11:
1943 trau_mux_map_lchan(lchan, remote_lchan);
1944 break;
1945 default:
1946 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001947 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001948 break;
1949 }
Harald Welte49f48b82009-02-17 15:29:33 +00001950
1951 return 0;
1952}
1953
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954/* bridge channels of two transactions */
1955static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001956{
Harald Weltedcaf5652009-07-23 18:56:43 +02001957 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1958 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001959
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001961 return -EIO;
1962
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 if (!trans1->lchan || !trans2->lchan)
1964 return -EIO;
1965
1966 /* through-connect channel */
1967 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001968}
1969
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970/* enable receive of channels to upqueue */
1971static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1972{
1973 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001976 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 if (!trans)
1978 return -EIO;
1979 if (!trans->lchan)
1980 return 0;
1981
1982 // todo IPACCESS
1983 if (enable)
1984 return trau_recv_lchan(trans->lchan, data->callref);
1985 return trau_mux_unmap(NULL, data->callref);
1986}
1987
1988/* send a frame to channel */
1989static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
1990{
1991 struct gsm_trans *trans;
1992
1993 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001994 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 if (!trans)
1996 return -EIO;
1997 if (!trans->lchan)
1998 return 0;
1999 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2000 trans->lchan->type != GSM_LCHAN_TCH_H)
2001 return 0;
2002
2003 // todo IPACCESS
2004 return trau_send_lchan(trans->lchan,
2005 (struct decoded_trau_frame *)frame->data);
2006}
2007
2008
2009static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2010{
2011 DEBUGP(DCC, "-> STATUS ENQ\n");
2012 return gsm48_cc_tx_status(trans, msg);
2013}
2014
2015static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2016static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2017
2018static void gsm48_cc_timeout(void *arg)
2019{
2020 struct gsm_trans *trans = arg;
2021 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002022 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2023 int mo_location = GSM48_CAUSE_LOC_USER;
2024 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2025 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 struct gsm_mncc mo_rel, l4_rel;
2027
2028 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2029 mo_rel.callref = trans->callref;
2030 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2031 l4_rel.callref = trans->callref;
2032
Harald Weltedcaf5652009-07-23 18:56:43 +02002033 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 case 0x303:
2035 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002036 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 break;
2038 case 0x310:
2039 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002040 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 break;
2042 case 0x313:
2043 disconnect = 1;
2044 /* unknown, did not find it in the specs */
2045 break;
2046 case 0x301:
2047 disconnect = 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 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002051 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002053 gsm48_cc_tx_release(trans, &trans->cc.msg);
2054 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 break; /* stay in release state */
2056 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002057 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 return;
2059// release = 1;
2060// l4_cause = 14;
2061// break;
2062 case 0x306:
2063 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002064 mo_cause = trans->cc.msg.cause.value;
2065 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 break;
2067 case 0x323:
2068 disconnect = 1;
2069 break;
2070 default:
2071 release = 1;
2072 }
2073
2074 if (release && trans->callref) {
2075 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002076 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 l4_location, l4_cause);
2078 trans->callref = 0;
2079 }
2080
2081 if (disconnect && trans->callref) {
2082 /* process disconnect towards layer 4 */
2083 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002084 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 }
2086
2087 /* process disconnect towards mobile station */
2088 if (disconnect || release) {
2089 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002090 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2091 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2092 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 mo_rel.cause.diag_len = 3;
2094
2095 if (disconnect)
2096 gsm48_cc_tx_disconnect(trans, &mo_rel);
2097 if (release)
2098 gsm48_cc_tx_release(trans, &mo_rel);
2099 }
2100
2101}
2102
2103static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2104 int sec, int micro)
2105{
2106 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002107 trans->cc.timer.cb = gsm48_cc_timeout;
2108 trans->cc.timer.data = trans;
2109 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2110 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111}
2112
2113static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2114{
2115 struct gsm48_hdr *gh = msgb_l3(msg);
2116 u_int8_t msg_type = gh->msg_type & 0xbf;
2117 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2118 struct tlv_parsed tp;
2119 struct gsm_mncc setup;
2120
2121 memset(&setup, 0, sizeof(struct gsm_mncc));
2122 setup.callref = trans->callref;
2123 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2124 /* emergency setup is identified by msg_type */
2125 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2126 setup.emergency = 1;
2127
2128 /* use subscriber as calling party number */
2129 if (trans->subscr) {
2130 setup.fields |= MNCC_F_CALLING;
2131 strncpy(setup.calling.number, trans->subscr->extension,
2132 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002133 strncpy(setup.imsi, trans->subscr->imsi,
2134 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 }
2136 /* bearer capability */
2137 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2138 setup.fields |= MNCC_F_BEARER_CAP;
2139 decode_bearer_cap(&setup.bearer_cap,
2140 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2141 }
2142 /* facility */
2143 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2144 setup.fields |= MNCC_F_FACILITY;
2145 decode_facility(&setup.facility,
2146 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2147 }
2148 /* called party bcd number */
2149 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2150 setup.fields |= MNCC_F_CALLED;
2151 decode_called(&setup.called,
2152 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2153 }
2154 /* user-user */
2155 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2156 setup.fields |= MNCC_F_USERUSER;
2157 decode_useruser(&setup.useruser,
2158 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2159 }
2160 /* ss-version */
2161 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2162 setup.fields |= MNCC_F_SSVERSION;
2163 decode_ssversion(&setup.ssversion,
2164 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2165 }
2166 /* CLIR suppression */
2167 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2168 setup.clir.sup = 1;
2169 /* CLIR invocation */
2170 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2171 setup.clir.inv = 1;
2172 /* cc cap */
2173 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2174 setup.fields |= MNCC_F_CCCAP;
2175 decode_cccap(&setup.cccap,
2176 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2177 }
2178
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 new_cc_state(trans, GSM_CSTATE_INITIATED);
2180
2181 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002182 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183
Harald Welte13cac662009-07-29 12:10:35 +02002184 /* MNCC code will modify the channel asynchronously, we should
2185 * ipaccess-bind only after the modification has been made to the
2186 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 return 0;
2188}
2189
2190static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002191{
2192 struct msgb *msg = gsm48_msgb_alloc();
2193 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002195 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002196
Harald Welte7ccf7782009-02-17 01:43:01 +00002197 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002198
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 /* transaction id must not be assigned */
2200 if (trans->transaction_id != 0xff) { /* unasssigned */
2201 DEBUGP(DCC, "TX Setup with assigned transaction. "
2202 "This is not allowed!\n");
2203 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002204 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002205 GSM48_CAUSE_LOC_PRN_S_LU,
2206 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002208 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 return rc;
2210 }
2211
2212 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002213 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2214 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 /* no free transaction ID */
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 }
Harald Welte78283ef2009-07-23 21:36:44 +02002223 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002224
Harald Welte65e74cc2008-12-29 01:55:35 +00002225 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002226
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002228
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 /* bearer capability */
2230 if (setup->fields & MNCC_F_BEARER_CAP)
2231 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2232 /* facility */
2233 if (setup->fields & MNCC_F_FACILITY)
2234 encode_facility(msg, 0, &setup->facility);
2235 /* progress */
2236 if (setup->fields & MNCC_F_PROGRESS)
2237 encode_progress(msg, 0, &setup->progress);
2238 /* calling party BCD number */
2239 if (setup->fields & MNCC_F_CALLING)
2240 encode_calling(msg, &setup->calling);
2241 /* called party BCD number */
2242 if (setup->fields & MNCC_F_CALLED)
2243 encode_called(msg, &setup->called);
2244 /* user-user */
2245 if (setup->fields & MNCC_F_USERUSER)
2246 encode_useruser(msg, 0, &setup->useruser);
2247 /* redirecting party BCD number */
2248 if (setup->fields & MNCC_F_REDIRECTING)
2249 encode_redirecting(msg, &setup->redirecting);
2250 /* signal */
2251 if (setup->fields & MNCC_F_SIGNAL)
2252 encode_signal(msg, setup->signal);
2253
2254 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002255
Harald Welte39e2ead2009-07-23 21:13:03 +02002256 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002257}
2258
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2260{
2261 struct gsm48_hdr *gh = msgb_l3(msg);
2262 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2263 struct tlv_parsed tp;
2264 struct gsm_mncc call_conf;
2265
2266 gsm48_stop_cc_timer(trans);
2267 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2268
2269 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2270 call_conf.callref = trans->callref;
2271 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2272#if 0
2273 /* repeat */
2274 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2275 call_conf.repeat = 1;
2276 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2277 call_conf.repeat = 2;
2278#endif
2279 /* bearer capability */
2280 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2281 call_conf.fields |= MNCC_F_BEARER_CAP;
2282 decode_bearer_cap(&call_conf.bearer_cap,
2283 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2284 }
2285 /* cause */
2286 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2287 call_conf.fields |= MNCC_F_CAUSE;
2288 decode_cause(&call_conf.cause,
2289 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2290 }
2291 /* cc cap */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2293 call_conf.fields |= MNCC_F_CCCAP;
2294 decode_cccap(&call_conf.cccap,
2295 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2296 }
2297
2298 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2299
Harald Welte596fed42009-07-23 19:06:52 +02002300 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2301 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302}
2303
2304static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2305{
2306 struct gsm_mncc *proceeding = arg;
2307 struct msgb *msg = gsm48_msgb_alloc();
2308 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2309
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2311
2312 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2313
2314 /* bearer capability */
2315 if (proceeding->fields & MNCC_F_BEARER_CAP)
2316 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2317 /* facility */
2318 if (proceeding->fields & MNCC_F_FACILITY)
2319 encode_facility(msg, 0, &proceeding->facility);
2320 /* progress */
2321 if (proceeding->fields & MNCC_F_PROGRESS)
2322 encode_progress(msg, 0, &proceeding->progress);
2323
Harald Welte39e2ead2009-07-23 21:13:03 +02002324 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325}
2326
2327static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2328{
2329 struct gsm48_hdr *gh = msgb_l3(msg);
2330 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2331 struct tlv_parsed tp;
2332 struct gsm_mncc alerting;
2333
2334 gsm48_stop_cc_timer(trans);
2335 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2336
2337 memset(&alerting, 0, sizeof(struct gsm_mncc));
2338 alerting.callref = trans->callref;
2339 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2340 /* facility */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2342 alerting.fields |= MNCC_F_FACILITY;
2343 decode_facility(&alerting.facility,
2344 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2345 }
2346
2347 /* progress */
2348 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2349 alerting.fields |= MNCC_F_PROGRESS;
2350 decode_progress(&alerting.progress,
2351 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2352 }
2353 /* ss-version */
2354 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2355 alerting.fields |= MNCC_F_SSVERSION;
2356 decode_ssversion(&alerting.ssversion,
2357 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2358 }
2359
2360 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2361
Harald Welte596fed42009-07-23 19:06:52 +02002362 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2363 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364}
2365
2366static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2367{
2368 struct gsm_mncc *alerting = arg;
2369 struct msgb *msg = gsm48_msgb_alloc();
2370 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2371
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 gh->msg_type = GSM48_MT_CC_ALERTING;
2373
2374 /* facility */
2375 if (alerting->fields & MNCC_F_FACILITY)
2376 encode_facility(msg, 0, &alerting->facility);
2377 /* progress */
2378 if (alerting->fields & MNCC_F_PROGRESS)
2379 encode_progress(msg, 0, &alerting->progress);
2380 /* user-user */
2381 if (alerting->fields & MNCC_F_USERUSER)
2382 encode_useruser(msg, 0, &alerting->useruser);
2383
2384 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2385
Harald Welte39e2ead2009-07-23 21:13:03 +02002386 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387}
2388
2389static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2390{
2391 struct gsm_mncc *progress = arg;
2392 struct msgb *msg = gsm48_msgb_alloc();
2393 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2394
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 gh->msg_type = GSM48_MT_CC_PROGRESS;
2396
2397 /* progress */
2398 encode_progress(msg, 1, &progress->progress);
2399 /* user-user */
2400 if (progress->fields & MNCC_F_USERUSER)
2401 encode_useruser(msg, 0, &progress->useruser);
2402
Harald Welte39e2ead2009-07-23 21:13:03 +02002403 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404}
2405
2406static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2407{
2408 struct gsm_mncc *connect = arg;
2409 struct msgb *msg = gsm48_msgb_alloc();
2410 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2411
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 gh->msg_type = GSM48_MT_CC_CONNECT;
2413
2414 gsm48_stop_cc_timer(trans);
2415 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2416
2417 /* facility */
2418 if (connect->fields & MNCC_F_FACILITY)
2419 encode_facility(msg, 0, &connect->facility);
2420 /* progress */
2421 if (connect->fields & MNCC_F_PROGRESS)
2422 encode_progress(msg, 0, &connect->progress);
2423 /* connected number */
2424 if (connect->fields & MNCC_F_CONNECTED)
2425 encode_connected(msg, &connect->connected);
2426 /* user-user */
2427 if (connect->fields & MNCC_F_USERUSER)
2428 encode_useruser(msg, 0, &connect->useruser);
2429
2430 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2431
Harald Welte39e2ead2009-07-23 21:13:03 +02002432 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433}
2434
2435static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2436{
2437 struct gsm48_hdr *gh = msgb_l3(msg);
2438 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2439 struct tlv_parsed tp;
2440 struct gsm_mncc connect;
2441
2442 gsm48_stop_cc_timer(trans);
2443
2444 memset(&connect, 0, sizeof(struct gsm_mncc));
2445 connect.callref = trans->callref;
2446 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2447 /* use subscriber as connected party number */
2448 if (trans->subscr) {
2449 connect.fields |= MNCC_F_CONNECTED;
2450 strncpy(connect.connected.number, trans->subscr->extension,
2451 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002452 strncpy(connect.imsi, trans->subscr->imsi,
2453 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 }
2455 /* facility */
2456 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2457 connect.fields |= MNCC_F_FACILITY;
2458 decode_facility(&connect.facility,
2459 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2460 }
2461 /* user-user */
2462 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2463 connect.fields |= MNCC_F_USERUSER;
2464 decode_useruser(&connect.useruser,
2465 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2466 }
2467 /* ss-version */
2468 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2469 connect.fields |= MNCC_F_SSVERSION;
2470 decode_ssversion(&connect.ssversion,
2471 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2472 }
2473
2474 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2475
Harald Welte596fed42009-07-23 19:06:52 +02002476 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477}
2478
2479
2480static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2481{
2482 struct gsm_mncc connect_ack;
2483
2484 gsm48_stop_cc_timer(trans);
2485
2486 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2487
2488 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2489 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002490 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 &connect_ack);
2492}
2493
2494static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2495{
2496 struct msgb *msg = gsm48_msgb_alloc();
2497 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2498
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2500
2501 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2502
Harald Welte39e2ead2009-07-23 21:13:03 +02002503 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2507{
2508 struct gsm48_hdr *gh = msgb_l3(msg);
2509 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2510 struct tlv_parsed tp;
2511 struct gsm_mncc disc;
2512
2513 gsm48_stop_cc_timer(trans);
2514
2515 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2516
2517 memset(&disc, 0, sizeof(struct gsm_mncc));
2518 disc.callref = trans->callref;
2519 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2520 /* cause */
2521 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2522 disc.fields |= MNCC_F_CAUSE;
2523 decode_cause(&disc.cause,
2524 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2525 }
2526 /* facility */
2527 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2528 disc.fields |= MNCC_F_FACILITY;
2529 decode_facility(&disc.facility,
2530 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2531 }
2532 /* user-user */
2533 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2534 disc.fields |= MNCC_F_USERUSER;
2535 decode_useruser(&disc.useruser,
2536 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2537 }
2538 /* ss-version */
2539 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2540 disc.fields |= MNCC_F_SSVERSION;
2541 decode_ssversion(&disc.ssversion,
2542 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2543 }
2544
Harald Welte596fed42009-07-23 19:06:52 +02002545 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546
2547}
2548
Harald Weltec66b71c2009-06-11 14:23:20 +08002549static struct gsm_mncc_cause default_cause = {
2550 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2551 .coding = 0,
2552 .rec = 0,
2553 .rec_val = 0,
2554 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2555 .diag_len = 0,
2556 .diag = { 0 },
2557};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558
2559static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2560{
2561 struct gsm_mncc *disc = arg;
2562 struct msgb *msg = gsm48_msgb_alloc();
2563 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2564
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2566
2567 gsm48_stop_cc_timer(trans);
2568 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2569
2570 /* cause */
2571 if (disc->fields & MNCC_F_CAUSE)
2572 encode_cause(msg, 1, &disc->cause);
2573 else
2574 encode_cause(msg, 1, &default_cause);
2575
2576 /* facility */
2577 if (disc->fields & MNCC_F_FACILITY)
2578 encode_facility(msg, 0, &disc->facility);
2579 /* progress */
2580 if (disc->fields & MNCC_F_PROGRESS)
2581 encode_progress(msg, 0, &disc->progress);
2582 /* user-user */
2583 if (disc->fields & MNCC_F_USERUSER)
2584 encode_useruser(msg, 0, &disc->useruser);
2585
2586 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002587 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588
2589 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2590
Harald Welte39e2ead2009-07-23 21:13:03 +02002591 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592}
2593
2594static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2595{
2596 struct gsm48_hdr *gh = msgb_l3(msg);
2597 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2598 struct tlv_parsed tp;
2599 struct gsm_mncc rel;
2600 int rc;
2601
2602 gsm48_stop_cc_timer(trans);
2603
2604 memset(&rel, 0, sizeof(struct gsm_mncc));
2605 rel.callref = trans->callref;
2606 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2607 /* cause */
2608 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2609 rel.fields |= MNCC_F_CAUSE;
2610 decode_cause(&rel.cause,
2611 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2612 }
2613 /* facility */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2615 rel.fields |= MNCC_F_FACILITY;
2616 decode_facility(&rel.facility,
2617 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2618 }
2619 /* user-user */
2620 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2621 rel.fields |= MNCC_F_USERUSER;
2622 decode_useruser(&rel.useruser,
2623 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2624 }
2625 /* ss-version */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2627 rel.fields |= MNCC_F_SSVERSION;
2628 decode_ssversion(&rel.ssversion,
2629 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2630 }
2631
Harald Weltedcaf5652009-07-23 18:56:43 +02002632 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002634 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002636 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002637 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002638 GSM48_MT_CC_RELEASE_COMPL);
2639 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640 }
2641
2642 new_cc_state(trans, GSM_CSTATE_NULL);
2643
2644 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002645 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646
2647 return rc;
2648}
2649
2650static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2651{
2652 struct gsm_mncc *rel = arg;
2653 struct msgb *msg = gsm48_msgb_alloc();
2654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2655
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 gh->msg_type = GSM48_MT_CC_RELEASE;
2657
2658 trans->callref = 0;
2659
2660 gsm48_stop_cc_timer(trans);
2661 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2662
2663 /* cause */
2664 if (rel->fields & MNCC_F_CAUSE)
2665 encode_cause(msg, 0, &rel->cause);
2666 /* facility */
2667 if (rel->fields & MNCC_F_FACILITY)
2668 encode_facility(msg, 0, &rel->facility);
2669 /* user-user */
2670 if (rel->fields & MNCC_F_USERUSER)
2671 encode_useruser(msg, 0, &rel->useruser);
2672
Harald Weltedcaf5652009-07-23 18:56:43 +02002673 trans->cc.T308_second = 0;
2674 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
Harald Weltedcaf5652009-07-23 18:56:43 +02002676 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2678
Harald Welte39e2ead2009-07-23 21:13:03 +02002679 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680}
2681
2682static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2683{
2684 struct gsm48_hdr *gh = msgb_l3(msg);
2685 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2686 struct tlv_parsed tp;
2687 struct gsm_mncc rel;
2688 int rc = 0;
2689
2690 gsm48_stop_cc_timer(trans);
2691
2692 memset(&rel, 0, sizeof(struct gsm_mncc));
2693 rel.callref = trans->callref;
2694 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2695 /* cause */
2696 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2697 rel.fields |= MNCC_F_CAUSE;
2698 decode_cause(&rel.cause,
2699 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2700 }
2701 /* facility */
2702 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2703 rel.fields |= MNCC_F_FACILITY;
2704 decode_facility(&rel.facility,
2705 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2706 }
2707 /* user-user */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2709 rel.fields |= MNCC_F_USERUSER;
2710 decode_useruser(&rel.useruser,
2711 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2712 }
2713 /* ss-version */
2714 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2715 rel.fields |= MNCC_F_SSVERSION;
2716 decode_ssversion(&rel.ssversion,
2717 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2718 }
2719
2720 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002721 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002723 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 MNCC_REJ_IND, &rel);
2725 break;
2726 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002727 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 MNCC_REL_CNF, &rel);
2729 break;
2730 default:
Harald Welte596fed42009-07-23 19:06:52 +02002731 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 MNCC_REL_IND, &rel);
2733 }
2734 }
2735
2736 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002737 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738
2739 return rc;
2740}
2741
2742static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2743{
2744 struct gsm_mncc *rel = arg;
2745 struct msgb *msg = gsm48_msgb_alloc();
2746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2747
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2749
2750 trans->callref = 0;
2751
2752 gsm48_stop_cc_timer(trans);
2753
2754 /* cause */
2755 if (rel->fields & MNCC_F_CAUSE)
2756 encode_cause(msg, 0, &rel->cause);
2757 /* facility */
2758 if (rel->fields & MNCC_F_FACILITY)
2759 encode_facility(msg, 0, &rel->facility);
2760 /* user-user */
2761 if (rel->fields & MNCC_F_USERUSER)
2762 encode_useruser(msg, 0, &rel->useruser);
2763
Harald Weltedcaf5652009-07-23 18:56:43 +02002764 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765
Harald Welte39e2ead2009-07-23 21:13:03 +02002766 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767}
2768
2769static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2770{
2771 struct gsm48_hdr *gh = msgb_l3(msg);
2772 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2773 struct tlv_parsed tp;
2774 struct gsm_mncc fac;
2775
2776 memset(&fac, 0, sizeof(struct gsm_mncc));
2777 fac.callref = trans->callref;
2778 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2779 /* facility */
2780 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2781 fac.fields |= MNCC_F_FACILITY;
2782 decode_facility(&fac.facility,
2783 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2784 }
2785 /* ss-version */
2786 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2787 fac.fields |= MNCC_F_SSVERSION;
2788 decode_ssversion(&fac.ssversion,
2789 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2790 }
2791
Harald Welte596fed42009-07-23 19:06:52 +02002792 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793}
2794
2795static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2796{
2797 struct gsm_mncc *fac = arg;
2798 struct msgb *msg = gsm48_msgb_alloc();
2799 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2800
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801 gh->msg_type = GSM48_MT_CC_FACILITY;
2802
2803 /* facility */
2804 encode_facility(msg, 1, &fac->facility);
2805
Harald Welte39e2ead2009-07-23 21:13:03 +02002806 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807}
2808
2809static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2810{
2811 struct gsm_mncc hold;
2812
2813 memset(&hold, 0, sizeof(struct gsm_mncc));
2814 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002815 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816}
2817
2818static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2819{
2820 struct msgb *msg = gsm48_msgb_alloc();
2821 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2822
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2824
Harald Welte39e2ead2009-07-23 21:13:03 +02002825 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826}
2827
2828static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2829{
2830 struct gsm_mncc *hold_rej = arg;
2831 struct msgb *msg = gsm48_msgb_alloc();
2832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2833
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2835
2836 /* cause */
2837 if (hold_rej->fields & MNCC_F_CAUSE)
2838 encode_cause(msg, 1, &hold_rej->cause);
2839 else
2840 encode_cause(msg, 1, &default_cause);
2841
Harald Welte39e2ead2009-07-23 21:13:03 +02002842 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843}
2844
2845static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2846{
2847 struct gsm_mncc retrieve;
2848
2849 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2850 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002851 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2852 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853}
2854
2855static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2856{
2857 struct msgb *msg = gsm48_msgb_alloc();
2858 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2859
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2861
Harald Welte39e2ead2009-07-23 21:13:03 +02002862 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863}
2864
2865static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2866{
2867 struct gsm_mncc *retrieve_rej = arg;
2868 struct msgb *msg = gsm48_msgb_alloc();
2869 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2870
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2872
2873 /* cause */
2874 if (retrieve_rej->fields & MNCC_F_CAUSE)
2875 encode_cause(msg, 1, &retrieve_rej->cause);
2876 else
2877 encode_cause(msg, 1, &default_cause);
2878
Harald Welte39e2ead2009-07-23 21:13:03 +02002879 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880}
2881
2882static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2883{
2884 struct gsm48_hdr *gh = msgb_l3(msg);
2885 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2886 struct tlv_parsed tp;
2887 struct gsm_mncc dtmf;
2888
2889 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2890 dtmf.callref = trans->callref;
2891 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2892 /* keypad facility */
2893 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2894 dtmf.fields |= MNCC_F_KEYPAD;
2895 decode_keypad(&dtmf.keypad,
2896 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2897 }
2898
Harald Welte596fed42009-07-23 19:06:52 +02002899 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900}
2901
2902static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2903{
2904 struct gsm_mncc *dtmf = arg;
2905 struct msgb *msg = gsm48_msgb_alloc();
2906 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2907
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2909
2910 /* keypad */
2911 if (dtmf->fields & MNCC_F_KEYPAD)
2912 encode_keypad(msg, dtmf->keypad);
2913
Harald Welte39e2ead2009-07-23 21:13:03 +02002914 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915}
2916
2917static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2918{
2919 struct gsm_mncc *dtmf = arg;
2920 struct msgb *msg = gsm48_msgb_alloc();
2921 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2922
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2924
2925 /* cause */
2926 if (dtmf->fields & MNCC_F_CAUSE)
2927 encode_cause(msg, 1, &dtmf->cause);
2928 else
2929 encode_cause(msg, 1, &default_cause);
2930
Harald Welte39e2ead2009-07-23 21:13:03 +02002931 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932}
2933
2934static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2935{
2936 struct msgb *msg = gsm48_msgb_alloc();
2937 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2938
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2940
Harald Welte39e2ead2009-07-23 21:13:03 +02002941 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942}
2943
2944static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2945{
2946 struct gsm_mncc dtmf;
2947
2948 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2949 dtmf.callref = trans->callref;
2950
Harald Welte596fed42009-07-23 19:06:52 +02002951 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952}
2953
2954static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2955{
2956 struct gsm48_hdr *gh = msgb_l3(msg);
2957 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2958 struct tlv_parsed tp;
2959 struct gsm_mncc modify;
2960
2961 memset(&modify, 0, sizeof(struct gsm_mncc));
2962 modify.callref = trans->callref;
2963 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2964 /* bearer capability */
2965 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2966 modify.fields |= MNCC_F_BEARER_CAP;
2967 decode_bearer_cap(&modify.bearer_cap,
2968 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2969 }
2970
2971 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2972
Harald Welte596fed42009-07-23 19:06:52 +02002973 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974}
2975
2976static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2977{
2978 struct gsm_mncc *modify = arg;
2979 struct msgb *msg = gsm48_msgb_alloc();
2980 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2981
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 gh->msg_type = GSM48_MT_CC_MODIFY;
2983
2984 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2985
2986 /* bearer capability */
2987 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2988
2989 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2990
Harald Welte39e2ead2009-07-23 21:13:03 +02002991 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992}
2993
2994static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2995{
2996 struct gsm48_hdr *gh = msgb_l3(msg);
2997 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2998 struct tlv_parsed tp;
2999 struct gsm_mncc modify;
3000
3001 gsm48_stop_cc_timer(trans);
3002
3003 memset(&modify, 0, sizeof(struct gsm_mncc));
3004 modify.callref = trans->callref;
3005 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3006 /* bearer capability */
3007 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3008 modify.fields |= MNCC_F_BEARER_CAP;
3009 decode_bearer_cap(&modify.bearer_cap,
3010 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3011 }
3012
3013 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3014
Harald Welte596fed42009-07-23 19:06:52 +02003015 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016}
3017
3018static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3019{
3020 struct gsm_mncc *modify = arg;
3021 struct msgb *msg = gsm48_msgb_alloc();
3022 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3023
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3025
3026 /* bearer capability */
3027 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3028
3029 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3030
Harald Welte39e2ead2009-07-23 21:13:03 +02003031 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003032}
3033
3034static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3035{
3036 struct gsm48_hdr *gh = msgb_l3(msg);
3037 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3038 struct tlv_parsed tp;
3039 struct gsm_mncc modify;
3040
3041 gsm48_stop_cc_timer(trans);
3042
3043 memset(&modify, 0, sizeof(struct gsm_mncc));
3044 modify.callref = trans->callref;
3045 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3046 /* bearer capability */
3047 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3048 modify.fields |= GSM48_IE_BEARER_CAP;
3049 decode_bearer_cap(&modify.bearer_cap,
3050 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3051 }
3052 /* cause */
3053 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3054 modify.fields |= MNCC_F_CAUSE;
3055 decode_cause(&modify.cause,
3056 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3057 }
3058
3059 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3060
Harald Welte596fed42009-07-23 19:06:52 +02003061 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062}
3063
3064static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3065{
3066 struct gsm_mncc *modify = arg;
3067 struct msgb *msg = gsm48_msgb_alloc();
3068 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3069
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3071
3072 /* bearer capability */
3073 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3074 /* cause */
3075 encode_cause(msg, 1, &modify->cause);
3076
3077 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3078
Harald Welte39e2ead2009-07-23 21:13:03 +02003079 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080}
3081
3082static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3083{
3084 struct gsm_mncc *notify = arg;
3085 struct msgb *msg = gsm48_msgb_alloc();
3086 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3087
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 gh->msg_type = GSM48_MT_CC_NOTIFY;
3089
3090 /* notify */
3091 encode_notify(msg, notify->notify);
3092
Harald Welte39e2ead2009-07-23 21:13:03 +02003093 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003094}
3095
3096static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3097{
3098 struct gsm48_hdr *gh = msgb_l3(msg);
3099 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3100// struct tlv_parsed tp;
3101 struct gsm_mncc notify;
3102
3103 memset(&notify, 0, sizeof(struct gsm_mncc));
3104 notify.callref = trans->callref;
3105// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3106 if (payload_len >= 1)
3107 decode_notify(&notify.notify, gh->data);
3108
Harald Welte596fed42009-07-23 19:06:52 +02003109 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110}
3111
3112static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3113{
3114 struct gsm_mncc *user = arg;
3115 struct msgb *msg = gsm48_msgb_alloc();
3116 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3117
Harald Welte4bfdfe72009-06-10 23:11:52 +08003118 gh->msg_type = GSM48_MT_CC_USER_INFO;
3119
3120 /* user-user */
3121 if (user->fields & MNCC_F_USERUSER)
3122 encode_useruser(msg, 1, &user->useruser);
3123 /* more data */
3124 if (user->more)
3125 encode_more(msg);
3126
Harald Welte39e2ead2009-07-23 21:13:03 +02003127 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128}
3129
3130static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3131{
3132 struct gsm48_hdr *gh = msgb_l3(msg);
3133 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3134 struct tlv_parsed tp;
3135 struct gsm_mncc user;
3136
3137 memset(&user, 0, sizeof(struct gsm_mncc));
3138 user.callref = trans->callref;
3139 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3140 /* user-user */
3141 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3142 user.fields |= MNCC_F_USERUSER;
3143 decode_useruser(&user.useruser,
3144 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3145 }
3146 /* more data */
3147 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3148 user.more = 1;
3149
Harald Welte596fed42009-07-23 19:06:52 +02003150 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151}
3152
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003153static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154{
3155 struct gsm_mncc *mode = arg;
3156
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003157 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003158}
3159
3160static struct downstate {
3161 u_int32_t states;
3162 int type;
3163 int (*rout) (struct gsm_trans *trans, void *arg);
3164} downstatelist[] = {
3165 /* mobile originating call establishment */
3166 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3167 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3168 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3169 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3170 {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 */
3171 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3172 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3173 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3174 /* mobile terminating call establishment */
3175 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3176 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3177 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3178 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3179 /* signalling during call */
3180 {SBIT(GSM_CSTATE_ACTIVE),
3181 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3182 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3183 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3184 {ALL_STATES,
3185 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3186 {ALL_STATES,
3187 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3188 {ALL_STATES,
3189 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3190 {SBIT(GSM_CSTATE_ACTIVE),
3191 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3192 {SBIT(GSM_CSTATE_ACTIVE),
3193 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3194 {SBIT(GSM_CSTATE_ACTIVE),
3195 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3200 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3201 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3202 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3203 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3204 {SBIT(GSM_CSTATE_ACTIVE),
3205 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3206 /* clearing */
3207 {SBIT(GSM_CSTATE_INITIATED),
3208 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3209 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3210 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3211 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3212 MNCC_REL_REQ, gsm48_cc_tx_release},
3213 /* special */
3214 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003215 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003216};
3217
3218#define DOWNSLLEN \
3219 (sizeof(downstatelist) / sizeof(struct downstate))
3220
3221
3222int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3223{
Harald Welte1a6f7982009-08-09 18:52:33 +02003224 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003225 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003226 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003228 struct gsm_mncc *data = arg, rel;
3229
3230 /* handle special messages */
3231 switch(msg_type) {
3232 case MNCC_BRIDGE:
3233 return tch_bridge(net, arg);
3234 case MNCC_FRAME_DROP:
3235 return tch_recv(net, arg, 0);
3236 case MNCC_FRAME_RECV:
3237 return tch_recv(net, arg, 1);
3238 case GSM_TRAU_FRAME:
3239 return tch_frame(net, arg);
3240 }
3241
3242 memset(&rel, 0, sizeof(struct gsm_mncc));
3243 rel.callref = data->callref;
3244
3245 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003246 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247
3248 /* Callref unknown */
3249 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003250 struct gsm_subscriber *subscr;
3251
Harald Welte4a3464c2009-07-04 10:11:24 +02003252 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3254 "Received '%s' from MNCC with "
3255 "unknown callref %d\n", data->called.number,
3256 get_mncc_name(msg_type), data->callref);
3257 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003258 return mncc_release_ind(net, NULL, data->callref,
3259 GSM48_CAUSE_LOC_PRN_S_LU,
3260 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003262 if (!data->called.number[0] && !data->imsi[0]) {
3263 DEBUGP(DCC, "(bts - trx - ts - ti) "
3264 "Received '%s' from MNCC with "
3265 "no number or IMSI\n", get_mncc_name(msg_type));
3266 /* Invalid number */
3267 return mncc_release_ind(net, NULL, data->callref,
3268 GSM48_CAUSE_LOC_PRN_S_LU,
3269 GSM48_CC_CAUSE_INV_NR_FORMAT);
3270 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003271 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003272 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003273 subscr = subscr_get_by_extension(net,
3274 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003275 else
Harald Welte9176bd42009-07-23 18:46:00 +02003276 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003277 /* If subscriber is not found */
3278 if (!subscr) {
3279 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3280 "Received '%s' from MNCC with "
3281 "unknown subscriber %s\n", data->called.number,
3282 get_mncc_name(msg_type), data->called.number);
3283 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003284 return mncc_release_ind(net, NULL, data->callref,
3285 GSM48_CAUSE_LOC_PRN_S_LU,
3286 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 }
3288 /* If subscriber is not "attached" */
3289 if (!subscr->lac) {
3290 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3291 "Received '%s' from MNCC with "
3292 "detached subscriber %s\n", data->called.number,
3293 get_mncc_name(msg_type), data->called.number);
3294 subscr_put(subscr);
3295 /* Temporarily out of order */
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_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003299 }
3300 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003301 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3302 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 DEBUGP(DCC, "No memory for trans.\n");
3304 subscr_put(subscr);
3305 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003306 mncc_release_ind(net, NULL, data->callref,
3307 GSM48_CAUSE_LOC_PRN_S_LU,
3308 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003309 return -ENOMEM;
3310 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003311 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003312 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 /* If subscriber has no lchan */
3314 if (!lchan) {
3315 /* find transaction with this subscriber already paging */
3316 llist_for_each_entry(transt, &net->trans_list, entry) {
3317 /* Transaction of our lchan? */
3318 if (transt == trans ||
3319 transt->subscr != subscr)
3320 continue;
3321 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3322 "Received '%s' from MNCC with "
3323 "unallocated channel, paging already "
3324 "started.\n", bts->nr,
3325 data->called.number,
3326 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003327 subscr_put(subscr);
3328 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 return 0;
3330 }
3331 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003332 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003333 /* Trigger paging */
3334 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3335 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003336 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003337 return 0;
3338 }
3339 /* Assign lchan */
3340 trans->lchan = lchan;
3341 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003342 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003343 }
3344 lchan = trans->lchan;
3345
3346 /* if paging did not respond yet */
3347 if (!lchan) {
3348 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3349 "Received '%s' from MNCC in paging state\n",
3350 (trans->subscr)?(trans->subscr->extension):"-",
3351 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003352 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3353 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003354 if (msg_type == MNCC_REL_REQ)
3355 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3356 else
3357 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3358 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003359 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003360 return rc;
3361 }
3362
3363 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3364 "Received '%s' from MNCC in state %d (%s)\n",
3365 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3366 trans->transaction_id,
3367 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003368 get_mncc_name(msg_type), trans->cc.state,
3369 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003370
3371 /* Find function for current state and message */
3372 for (i = 0; i < DOWNSLLEN; i++)
3373 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003374 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003375 break;
3376 if (i == DOWNSLLEN) {
3377 DEBUGP(DCC, "Message unhandled at this state.\n");
3378 return 0;
3379 }
3380
3381 rc = downstatelist[i].rout(trans, arg);
3382
3383 return rc;
3384}
3385
3386
3387static struct datastate {
3388 u_int32_t states;
3389 int type;
3390 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3391} datastatelist[] = {
3392 /* mobile originating call establishment */
3393 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3394 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3395 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3396 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3397 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3398 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3399 /* mobile terminating call establishment */
3400 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3401 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3402 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3403 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3404 {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 */
3405 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3406 /* signalling during call */
3407 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3408 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3409 {SBIT(GSM_CSTATE_ACTIVE),
3410 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3411 {ALL_STATES,
3412 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3413 {ALL_STATES,
3414 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3415 {ALL_STATES,
3416 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3417 {SBIT(GSM_CSTATE_ACTIVE),
3418 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3419 {SBIT(GSM_CSTATE_ACTIVE),
3420 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3421 {SBIT(GSM_CSTATE_ACTIVE),
3422 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3423 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3424 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3425 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3426 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3427 {SBIT(GSM_CSTATE_ACTIVE),
3428 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3429 /* clearing */
3430 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3431 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3432 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3433 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3434 {ALL_STATES, /* 5.4.3.4 */
3435 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3436};
3437
3438#define DATASLLEN \
3439 (sizeof(datastatelist) / sizeof(struct datastate))
3440
Harald Welte4bc90a12008-12-27 16:32:52 +00003441static int gsm0408_rcv_cc(struct msgb *msg)
3442{
3443 struct gsm48_hdr *gh = msgb_l3(msg);
3444 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003445 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003447 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003448 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003449
Harald Welte4bfdfe72009-06-10 23:11:52 +08003450 if (msg_type & 0x80) {
3451 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3452 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003453 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003454
3455 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003456 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003457
Harald Welte6f5aee02009-07-23 21:21:14 +02003458 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003459 "Received '%s' from MS in state %d (%s)\n",
3460 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3461 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003462 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003463 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003464
3465 /* Create transaction */
3466 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003467 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003468 "creating new trans.\n", transaction_id);
3469 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003470 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3471 transaction_id, new_callref++);
3472 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003473 DEBUGP(DCC, "No memory for trans.\n");
3474 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003475 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476 GSM48_MT_CC_RELEASE_COMPL);
3477 return -ENOMEM;
3478 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003479 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480 trans->lchan = lchan;
3481 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003482 }
3483
3484 /* find function for current state and message */
3485 for (i = 0; i < DATASLLEN; i++)
3486 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003487 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003488 break;
3489 if (i == DATASLLEN) {
3490 DEBUGP(DCC, "Message unhandled at this state.\n");
3491 return 0;
3492 }
3493
3494 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003495
3496 return rc;
3497}
3498
Harald Welte52b1f982008-12-23 20:25:15 +00003499/* 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 +02003500int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003501{
3502 struct gsm48_hdr *gh = msgb_l3(msg);
3503 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003504 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003505
3506 switch (pdisc) {
3507 case GSM48_PDISC_CC:
3508 rc = gsm0408_rcv_cc(msg);
3509 break;
3510 case GSM48_PDISC_MM:
3511 rc = gsm0408_rcv_mm(msg);
3512 break;
3513 case GSM48_PDISC_RR:
3514 rc = gsm0408_rcv_rr(msg);
3515 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003516 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003517 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003518 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003519 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003520 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003521 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003522 pdisc);
3523 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003524 case GSM48_PDISC_NC_SS:
3525 rc = handle_rcv_ussd(msg);
3526 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003527 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003528 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003529 pdisc);
3530 break;
3531 }
3532
3533 return rc;
3534}
Harald Welte8470bf22008-12-25 23:28:35 +00003535
Harald Welte4bfdfe72009-06-10 23:11:52 +08003536/* dequeue messages to layer 4 */
3537int bsc_upqueue(struct gsm_network *net)
3538{
3539 struct gsm_mncc *mncc;
3540 struct msgb *msg;
3541 int work = 0;
3542
3543 if (net)
3544 while ((msg = msgb_dequeue(&net->upqueue))) {
3545 mncc = (struct gsm_mncc *)msg->data;
3546 if (net->mncc_recv)
3547 net->mncc_recv(net, mncc->msg_type, mncc);
3548 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003549 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003550 }
3551
3552 return work;
3553}
Harald Weltedcaf5652009-07-23 18:56:43 +02003554
Harald Welte805f6442009-07-28 18:25:29 +02003555/*
3556 * This will be ran by the linker when loading the DSO. We use it to
3557 * do system initialization, e.g. registration of signal handlers.
3558 */
3559static __attribute__((constructor)) void on_dso_load_0408(void)
3560{
Harald Welte805f6442009-07-28 18:25:29 +02003561 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3562 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3563}