blob: 02ffe580b57e68a69701acbd43a3363174627c36 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 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>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 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
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#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>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/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 Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000064
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020065#include <assert.h>
66
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020067void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010068void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020069
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020070static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080071static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020072 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010073static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080074static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000075
Harald Welte52b1f982008-12-23 20:25:15 +000076struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 uint16_t mcc;
78 uint16_t mnc;
79 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000080};
81
Andreas Eversberga83d5112013-12-07 18:32:28 +010082static int apply_codec_restrictions(struct gsm_bts *bts,
83 struct gsm_mncc_bearer_cap *bcap)
84{
85 int i, j;
86
87 /* remove unsupported speech versions from list */
88 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
89 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
90 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
91 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
92 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
99 }
100 bcap->speech_ver[j] = -1;
101
102 return 0;
103}
104
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200105static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800106
Harald Welte31c00f72011-03-03 23:29:05 +0100107void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
108{
109 net->mncc_recv(net, msg);
110}
111
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
113 struct gsm_trans *trans)
114{
115 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
116
117 /* if we get passed a transaction reference, do some common
118 * work that the caller no longer has to do */
119 if (trans) {
120 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
121 msg->lchan = trans->conn->lchan;
122 }
123
124
125 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200126 struct e1inp_sign_link *sign_link =
127 msg->lchan->ts->trx->rsl_link;
128
129 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800130 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
131 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200132 "Sending '%s' to MS.\n",
133 sign_link->trx->bts->nr,
134 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800135 gh->proto_discr & 0xf0,
136 gsm48_cc_msg_name(gh->msg_type));
137 else
138 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending 0x%02x to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr, gh->msg_type);
143 }
144
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100145 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800146}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800148int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
149{
150 struct gsm48_hdr *gh;
151 struct msgb *ss_notify;
152
153 ss_notify = gsm0480_create_notifySS(message);
154 if (!ss_notify)
155 return -1;
156
157 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
158 uint8_t *data = msgb_push(ss_notify, 1);
159 data[0] = ss_notify->len - 1;
160 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
161 gh->msg_type = GSM48_MT_CC_FACILITY;
162 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
163}
164
Harald Weltecf149ee2012-01-23 16:40:24 +0100165void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100166{
167 if (!conn->sec_operation)
168 return;
169
170 talloc_free(conn->sec_operation);
171 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800172 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173}
174
Harald Weltecf149ee2012-01-23 16:40:24 +0100175void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100176{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
178 struct gsm_security_operation);
179}
180
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100181int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
182 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 struct gsm_network *net = conn->bts->network;
185 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186 struct gsm_security_operation *op;
187 struct gsm_auth_tuple atuple;
188 int status = -1, rc;
189
190 /* Check if we _can_ enable encryption. Cases where we can't:
191 * - Encryption disabled in config
192 * - Channel already secured (nothing to do)
193 * - Subscriber equipment doesn't support configured encryption
194 */
195 if (!net->a5_encryption) {
196 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200199 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
201 net->a5_encryption)) {
202 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
203 status = GSM_SECURITY_NOAVAIL;
204 }
205
206 /* If not done yet, try to get info for this user */
207 if (status < 0) {
208 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
209 if (rc <= 0)
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* Are we done yet ? */
214 if (status >= 0)
215 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800216 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100217 0;
218
219 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800220 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100221 return -EBUSY;
222
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 allocate_security_operation(conn);
224 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100225 op->cb = cb;
226 op->cb_data = cb_data;
227 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
228
229 /* FIXME: Should start a timer for completion ... */
230
231 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100232 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100233 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800234 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100235 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100236 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100237 return gsm0808_cipher_mode(conn, net->a5_encryption,
238 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100239 }
240
241 return -EINVAL; /* not reached */
242}
243
Holger Freyther73487a22008-12-31 18:53:57 +0000244static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
245 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000246{
247 if (!subscriber)
248 return 0;
249
Holger Freyther73487a22008-12-31 18:53:57 +0000250 /*
251 * Do not send accept yet as more information should arrive. Some
252 * phones will not send us the information and we will have to check
253 * what we want to do with that.
254 */
255 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
256 return 0;
257
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100258 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100272static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000273{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100274 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000275 return;
276
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800277 /* No need to keep the connection up */
278 release_anchor(conn);
279
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200280 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100281 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100282 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100283 if (release)
284 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000285}
286
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100287static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000288{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800289 if (conn->loc_operation)
290 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100291 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000292
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100293 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200294 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000295}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000296
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200297static int finish_lu(struct gsm_subscriber_connection *conn)
298{
299 int rc = 0;
300 int avoid_tmsi = conn->bts->network->avoid_tmsi;
301
302 /* We're all good */
303 if (avoid_tmsi) {
304 conn->subscr->tmsi = GSM_RESERVED_TMSI;
305 db_sync_subscriber(conn->subscr);
306 } else {
307 db_subscriber_alloc_tmsi(conn->subscr);
308 }
309
310 rc = gsm0408_loc_upd_acc(conn);
311 if (conn->bts->network->send_mm_info) {
312 /* send MM INFO with network name */
313 rc = gsm48_tx_mm_info(conn);
314 }
315
316 /* call subscr_update after putting the loc_upd_acc
317 * in the transmit queue, since S_SUBSCR_ATTACHED might
318 * trigger further action like SMS delivery */
319 subscr_update(conn->subscr, conn->bts,
320 GSM_SUBSCRIBER_UPDATE_ATTACHED);
321
322 /*
323 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
324 * MS needs to respond with a TMSI REALLOCATION COMPLETE
325 * (even if the TMSI is the same).
326 */
327 if (avoid_tmsi)
328 release_loc_updating_req(conn, 1);
329
330 return rc;
331}
332
Sylvain Munaut267fba02009-12-24 00:28:01 +0100333static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
334 struct msgb *msg, void *data, void *param)
335{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800336 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100337 int rc = 0;
338
339 switch (event) {
340 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100341 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100342 break;
343
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200344 case GSM_SECURITY_ALREADY:
345 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
346 "UPDATING after CM SERVICE REQUEST\n");
347 /* fall through */
348
Sylvain Munaut267fba02009-12-24 00:28:01 +0100349 case GSM_SECURITY_NOAVAIL:
350 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200351 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100352 break;
353
354 default:
355 rc = -EINVAL;
356 };
357
358 return rc;
359}
360
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100361static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000362{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100363 if (!conn->loc_operation)
364 return 0;
365
Sylvain Munaut267fba02009-12-24 00:28:01 +0100366 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800367 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 conn->loc_operation->key_seq,
369 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000370 return 0;
371}
372
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800373void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000374{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800375 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800376
377 /* avoid someone issuing a clear */
378 conn->in_release = 1;
379
Holger Freyther7c19f742009-06-06 13:54:35 +0000380 /*
381 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800382 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000383 */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100384 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100385
386 /* We might need to cancel the paging response or such. */
387 if (conn->sec_operation && conn->sec_operation->cb) {
388 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
389 NULL, conn, conn->sec_operation->cb_data);
390 }
391
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800392 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800393 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000394
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100395 /*
396 * Free all transactions that are associated with the released
397 * connection. The transaction code will inform the CC or SMS
398 * facilities that will send the release indications. As part of
399 * the CC REL_IND the remote leg might be released and this will
400 * trigger the call to trans_free. This is something the llist
401 * macro can not handle and we will need to re-iterate the list.
402 *
403 * TODO: Move the trans_list into the subscriber connection and
404 * create a pending list for MT transactions. These exist before
405 * we have a subscriber connection.
406 */
407restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800408 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100409 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200410 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100411 goto restart;
412 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800413 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000414}
415
Harald Welte371efe52010-12-22 23:17:50 +0100416void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
417{
418 struct gsm_trans *trans, *temp;
419
420 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
421
422 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100423 if (trans->protocol == protocol) {
424 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100425 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100426 }
Harald Welte371efe52010-12-22 23:17:50 +0100427 }
428}
429
Holger Freyther429e7762008-12-30 13:28:30 +0000430/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200431int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000432{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800433 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800434 struct msgb *msg;
435
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200436 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800437
438 msg = gsm48_create_loc_upd_rej(cause);
439 if (!msg) {
440 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
441 return -1;
442 }
Harald Welte52b1f982008-12-23 20:25:15 +0000443
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800444 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000445
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100446 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100447 "LAC=%u BTS=%u\n", conn->subscr ?
448 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800449 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100450
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800451 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000452}
453
454/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200455static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000456{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800457 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000458 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000459 struct gsm48_hdr *gh;
460 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200461 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000462
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800463 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000464
465 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
466 gh->proto_discr = GSM48_PDISC_MM;
467 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
468
469 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100470 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000471 bts->network->network_code, bts->location_area_code);
472
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200473 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
474 uint8_t mi[10];
475 int len;
476 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
477 mid = msgb_put(msg, len);
478 memcpy(mid, mi, len);
479 } else {
480 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
481 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
482 }
Harald Welte52b1f982008-12-23 20:25:15 +0000483
484 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
485
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200486 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100487
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800488 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000489}
490
Harald Weltebf5e8df2009-02-03 12:59:45 +0000491/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200492static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000493{
494 struct msgb *msg = gsm48_msgb_alloc();
495 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000496
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800497 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000498
499 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
500 gh->proto_discr = GSM48_PDISC_MM;
501 gh->msg_type = GSM48_MT_MM_ID_REQ;
502 gh->data[0] = id_type;
503
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800504 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000505}
506
Harald Welte231ad4f2008-12-27 11:15:38 +0000507
Harald Weltebf5e8df2009-02-03 12:59:45 +0000508/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800509static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000510{
511 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000512 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200513 struct gsm_bts *bts = lchan->ts->trx->bts;
514 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200515 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200516 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000517
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200518 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000519 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000520 mi_type, mi_string);
521
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200522 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100523
Harald Welte75a983f2008-12-27 21:34:06 +0000524 switch (mi_type) {
525 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200526 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100527 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100528 conn->subscr = subscr_get_by_imsi(net->subscr_group,
529 mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100530 if (!conn->subscr && net->create_subscriber)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100531 conn->subscr = subscr_create_subscriber(
532 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200533 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100534 if (!conn->subscr && conn->loc_operation) {
535 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
536 release_loc_updating_req(conn, 1);
537 return 0;
538 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100539 if (conn->loc_operation)
540 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000541 break;
542 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000543 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000544 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100545 if (conn->subscr) {
546 db_subscriber_assoc_imei(conn->subscr, mi_string);
547 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200548 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100549 if (conn->loc_operation)
550 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000551 break;
552 }
Holger Freyther73487a22008-12-31 18:53:57 +0000553
554 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100555 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000556}
557
Harald Welte255539c2008-12-28 02:26:27 +0000558
559static void loc_upd_rej_cb(void *data)
560{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 struct gsm_subscriber_connection *conn = data;
562 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100563 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000564
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100565 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800566 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000568}
569
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100570static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000571{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
573 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200574 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000575}
576
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200577static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000578{
579 switch (type) {
580 case GSM48_LUPD_NORMAL:
581 return "NORMAL";
582 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200583 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000584 case GSM48_LUPD_IMSI_ATT:
585 return "IMSI ATTACH";
586 default:
587 return "UNKNOWN";
588 }
589}
590
Harald Weltebf5e8df2009-02-03 12:59:45 +0000591/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800592static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000593{
Harald Welte8470bf22008-12-25 23:28:35 +0000594 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000595 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800596 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800597 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200598 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200599 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000600
Harald Welte8470bf22008-12-25 23:28:35 +0000601 lu = (struct gsm48_loc_upd_req *) gh->data;
602
603 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000604
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200605 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000606
Harald Weltea0368542009-06-27 02:58:43 +0200607 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000608 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000609
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200610 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100611
Harald Welte24ff6ee2009-12-22 00:41:05 +0100612 switch (lu->type) {
613 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200614 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100615 break;
616 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200617 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100618 break;
619 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200620 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100621 break;
622 }
623
Holger Freythereaf04692009-06-06 13:54:44 +0000624 /*
625 * Pseudo Spoof detection: Just drop a second/concurrent
626 * location updating request.
627 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100628 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200629 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100630 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800631 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000632 return 0;
633 }
634
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800635 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000636
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200637 conn->loc_operation->key_seq = lu->key_seq;
638
Harald Welte52b1f982008-12-23 20:25:15 +0000639 switch (mi_type) {
640 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200641 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000642 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200643 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100644 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000645
Jan Luebbe370b41d2009-08-12 10:19:34 +0200646 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100647 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100648 if (!subscr && bts->network->create_subscriber) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100649 subscr = subscr_create_subscriber(
650 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200651 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100652 if (!subscr) {
653 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
654 release_loc_updating_req(conn, 0);
655 return 0;
656 }
Harald Welte4b634542008-12-27 01:55:51 +0000657 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000658 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200659 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000660 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100661 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200662 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000663 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000664 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200665 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100666 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000667 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200668 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200669 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200670 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000671 break;
672 case GSM_MI_TYPE_IMEI:
673 case GSM_MI_TYPE_IMEISV:
674 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200675 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000676 break;
677 default:
Harald Weltea0368542009-06-27 02:58:43 +0200678 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000679 break;
680 }
681
Harald Welte24516ea2009-07-04 10:18:00 +0200682 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100683 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200684
Harald Welte4bfdfe72009-06-10 23:11:52 +0800685 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200686 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800687 /* FIXME: request id? close channel? */
688 return -EINVAL;
689 }
690
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100691 conn->subscr = subscr;
692 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000693
Harald Welte24516ea2009-07-04 10:18:00 +0200694 /* check if we can let the subscriber into our network immediately
695 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100696 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000697}
698
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100699/* Turn int into semi-octet representation: 98 => 0x89 */
700static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800701{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100702 uint8_t ret;
703
704 ret = value / 10;
705 ret |= (value % 10) << 4;
706
707 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100709
Harald Welte4bfdfe72009-06-10 23:11:52 +0800710
Harald Weltedb253af2008-12-30 17:56:55 +0000711/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800712int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000713{
714 struct msgb *msg = gsm48_msgb_alloc();
715 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800716 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100717 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200718 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200719 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100720
Harald Welte4bfdfe72009-06-10 23:11:52 +0800721 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100722 struct tm* gmt_time;
723 struct tm* local_time;
724 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100725 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000726
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800727 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000728
729 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
730 gh->proto_discr = GSM48_PDISC_MM;
731 gh->msg_type = GSM48_MT_MM_INFO;
732
733 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200734#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000735 name_len = strlen(net->name_long);
736 /* 10.5.3.5a */
737 ptr8 = msgb_put(msg, 3);
738 ptr8[0] = GSM48_IE_NAME_LONG;
739 ptr8[1] = name_len*2 +1;
740 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
741
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200742 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000743 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000744 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000745
746 /* FIXME: Use Cell Broadcast, not UCS-2, since
747 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200748#endif
749 name_len = (strlen(net->name_long)*7)/8;
750 name_pad = (8 - strlen(net->name_long)*7)%8;
751 if (name_pad > 0)
752 name_len++;
753 /* 10.5.3.5a */
754 ptr8 = msgb_put(msg, 3);
755 ptr8[0] = GSM48_IE_NAME_LONG;
756 ptr8[1] = name_len +1;
757 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
758
759 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100760 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200761
Harald Weltedb253af2008-12-30 17:56:55 +0000762 }
763
764 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200765#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000766 name_len = strlen(net->name_short);
767 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200768 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200769 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000770 ptr8[1] = name_len*2 + 1;
771 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
772
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200773 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000774 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000775 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200776#endif
777 name_len = (strlen(net->name_short)*7)/8;
778 name_pad = (8 - strlen(net->name_short)*7)%8;
779 if (name_pad > 0)
780 name_len++;
781 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200782 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200783 ptr8[0] = GSM48_IE_NAME_SHORT;
784 ptr8[1] = name_len +1;
785 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
786
787 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100788 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200789
Harald Weltedb253af2008-12-30 17:56:55 +0000790 }
791
Harald Weltedb253af2008-12-30 17:56:55 +0000792 /* Section 10.5.3.9 */
793 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100794 gmt_time = gmtime(&cur_t);
795
Harald Weltedb253af2008-12-30 17:56:55 +0000796 ptr8 = msgb_put(msg, 8);
797 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100798 ptr8[1] = bcdify(gmt_time->tm_year % 100);
799 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
800 ptr8[3] = bcdify(gmt_time->tm_mday);
801 ptr8[4] = bcdify(gmt_time->tm_hour);
802 ptr8[5] = bcdify(gmt_time->tm_min);
803 ptr8[6] = bcdify(gmt_time->tm_sec);
804
Harald Welte45f91712012-07-08 16:48:11 +0200805 if (bts->tz.override) {
806 /* Convert tz.hr and tz.mn to units */
807 if (bts->tz.hr < 0) {
808 tzunits = ((bts->tz.hr/-1)*4);
809 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100810 ptr8[7] = bcdify(tzunits);
811 /* Set negative time */
812 ptr8[7] |= 0x08;
813 }
814 else {
Harald Welte45f91712012-07-08 16:48:11 +0200815 tzunits = bts->tz.hr*4;
816 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100817 ptr8[7] = bcdify(tzunits);
818 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100819 /* Convert DST value */
820 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
821 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100822 }
823 else {
824 /* Need to get GSM offset and convert into 15 min units */
825 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
826 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200827#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100828 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200829#else
830#warning find a portable way to obtain the timezone offset
831 tzunits = 0;
832#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100833 if (tzunits < 0) {
834 tzunits = tzunits/-1;
835 ptr8[7] = bcdify(tzunits);
836 /* Flip it to negative */
837 ptr8[7] |= 0x08;
838 }
839 else
840 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100841
842 /* Does not support DST +2 */
843 if (local_time->tm_isdst)
844 dst = 1;
845 }
846
847 if (dst) {
848 ptr8 = msgb_put(msg, 3);
849 ptr8[0] = GSM48_IE_NET_DST;
850 ptr8[1] = 1;
851 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100852 }
Harald Weltedb253af2008-12-30 17:56:55 +0000853
Daniel Willmanneea93372009-08-13 03:42:07 +0200854 DEBUGP(DMM, "-> MM INFO\n");
855
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800856 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000857}
858
Harald Welte7984d5c2009-08-12 22:56:50 +0200859/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200860int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200861{
862 struct msgb *msg = gsm48_msgb_alloc();
863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200864 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200865
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200866 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200867
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800868 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200869 gh->proto_discr = GSM48_PDISC_MM;
870 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
871
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100872 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200873
Harald Welte7984d5c2009-08-12 22:56:50 +0200874 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200875 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200876 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200877
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800878 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200879}
880
881/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800882int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200883{
884 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800885 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200886}
887
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100888/*
889 * At the 30C3 phones miss their periodic update
890 * interval a lot and then remain unreachable. In case
891 * we still know the TMSI we can just attach it again.
892 */
893static void implit_attach(struct gsm_subscriber_connection *conn)
894{
895 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
896 return;
897
898 subscr_update(conn->subscr, conn->bts,
899 GSM_SUBSCRIBER_UPDATE_ATTACHED);
900}
901
902
Sylvain Munautba87f452009-12-24 00:28:46 +0100903static int _gsm48_rx_mm_serv_req_sec_cb(
904 unsigned int hooknum, unsigned int event,
905 struct msgb *msg, void *data, void *param)
906{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800907 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100908 int rc = 0;
909
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200910 /* auth failed or succeeded, the timer was stopped */
911 conn->expire_timer_stopped = 1;
912
Sylvain Munautba87f452009-12-24 00:28:46 +0100913 switch (event) {
914 case GSM_SECURITY_AUTH_FAILED:
915 /* Nothing to do */
916 break;
917
918 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200919 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800920 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100921 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100922 break;
923
924 case GSM_SECURITY_SUCCEEDED:
925 /* nothing to do. CIPHER MODE COMMAND is
926 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100927 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100928 break;
929
930 default:
931 rc = -EINVAL;
932 };
933
934 return rc;
935}
936
Harald Welte4ed0e922009-01-10 03:17:30 +0000937/*
938 * Handle CM Service Requests
939 * a) Verify that the packet is long enough to contain the information
940 * we require otherwsie reject with INCORRECT_MESSAGE
941 * b) Try to parse the TMSI. If we do not have one reject
942 * c) Check that we know the subscriber with the TMSI otherwise reject
943 * with a HLR cause
944 * d) Set the subscriber on the gsm_lchan and accept
945 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800946static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000947{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200948 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200949 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000950
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800951 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000952 struct gsm_subscriber *subscr;
953 struct gsm48_hdr *gh = msgb_l3(msg);
954 struct gsm48_service_request *req =
955 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800956 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200957 uint8_t classmark2_len = gh->data[1];
958 uint8_t *classmark2 = gh->data+2;
959 uint8_t mi_len = *(classmark2 + classmark2_len);
960 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000961
Harald Weltec9e02182009-05-01 19:07:53 +0000962 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000963 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000964 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800965 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000966 GSM48_REJECT_INCORRECT_MESSAGE);
967 }
968
969 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000970 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800971 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000972 GSM48_REJECT_INCORRECT_MESSAGE);
973 }
974
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200975 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000976 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200977
978 if (mi_type == GSM_MI_TYPE_IMSI) {
979 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
980 req->cm_service_type, mi_type, mi_string);
981 subscr = subscr_get_by_imsi(bts->network->subscr_group,
982 mi_string);
983 } else if (mi_type == GSM_MI_TYPE_TMSI) {
984 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
985 req->cm_service_type, mi_type, mi_string);
986 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
987 tmsi_from_string(mi_string));
988 } else {
989 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800990 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000991 GSM48_REJECT_INCORRECT_MESSAGE);
992 }
993
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200994 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100995
Harald Welte3ac7f102009-08-10 10:12:45 +0200996 if (is_siemens_bts(bts))
997 send_siemens_mrpci(msg->lchan, classmark2-1);
998
Holger Freythereb443982009-06-04 13:58:42 +0000999
Harald Welte2a139372009-02-22 21:14:55 +00001000 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001001 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001002 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001003 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001004
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001005 if (!conn->subscr)
1006 conn->subscr = subscr;
1007 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001008 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1009 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001010 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1011 subscr_put(subscr);
1012 }
1013
Harald Weltec2e302d2009-07-05 14:08:13 +02001014 subscr->equipment.classmark2_len = classmark2_len;
1015 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1016 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001017
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001018 /* we will send a MM message soon */
1019 conn->expire_timer_stopped = 1;
1020
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001021 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001022 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001023}
1024
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001025static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001026{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001027 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001028 struct gsm48_hdr *gh = msgb_l3(msg);
1029 struct gsm48_imsi_detach_ind *idi =
1030 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001031 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001032 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001033 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001034
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001035 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +01001036 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +00001037 mi_type, mi_string);
1038
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001039 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001040
Harald Welte2a139372009-02-22 21:14:55 +00001041 switch (mi_type) {
1042 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001043 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001044 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001045 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001046 break;
1047 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001048 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001049 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1050 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001051 break;
1052 case GSM_MI_TYPE_IMEI:
1053 case GSM_MI_TYPE_IMEISV:
1054 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001055 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001056 break;
1057 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001058 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001059 break;
1060 }
1061
Holger Freyther4a49e772009-04-12 05:37:29 +00001062 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001063 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001064 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001065 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001066
1067 subscr->equipment.classmark1 = idi->classmark1;
1068 db_sync_equipment(&subscr->equipment);
1069
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001070 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001071 } else
Harald Welte2a139372009-02-22 21:14:55 +00001072 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1073
Harald Welte31981a02009-12-20 09:58:40 +01001074 /* FIXME: iterate over all transactions and release them,
1075 * imagine an IMSI DETACH happening during an active call! */
1076
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001077 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001078 return 0;
1079}
1080
Harald Welted2a7f5a2009-06-05 20:08:20 +00001081static int gsm48_rx_mm_status(struct msgb *msg)
1082{
1083 struct gsm48_hdr *gh = msgb_l3(msg);
1084
1085 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1086
1087 return 0;
1088}
1089
Sylvain Munaut30a15382009-12-24 00:27:26 +01001090/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001091static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001092{
1093 struct gsm48_hdr *gh = msgb_l3(msg);
1094 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001095 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001096
Sylvain Munautc593cf12010-06-10 23:51:41 +02001097 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001098 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001099
1100 /* Safety check */
1101 if (!conn->sec_operation) {
1102 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1103 return -EIO;
1104 }
1105
1106 /* Validate SRES */
1107 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001108 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001109 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001110
1111 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001112 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001113
Sylvain Munaut30a15382009-12-24 00:27:26 +01001114 if (cb)
1115 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001116 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001117
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001118 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001119 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001120 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001121 }
1122
Sylvain Munautc593cf12010-06-10 23:51:41 +02001123 DEBUGPC(DMM, "OK\n");
1124
Sylvain Munaut30a15382009-12-24 00:27:26 +01001125 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001126 return gsm0808_cipher_mode(conn, net->a5_encryption,
1127 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128}
1129
Harald Weltebf5e8df2009-02-03 12:59:45 +00001130/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001131static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001132{
1133 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001134 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001135
1136 switch (gh->msg_type & 0xbf) {
1137 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001138 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001139 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001140 break;
1141 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001142 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001143 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001144 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001146 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001147 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001148 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001149 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001150 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001151 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001152 conn->subscr ?
1153 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001154 "unknown subscriber");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001155 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001156 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001157 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001158 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001159 break;
1160 case GSM48_MT_MM_CM_REEST_REQ:
1161 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1162 break;
1163 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001164 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001165 break;
1166 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001167 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001168 gh->msg_type);
1169 break;
1170 }
1171
1172 return rc;
1173}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001174
Harald Welte2d35ae62009-02-06 12:02:13 +00001175/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001176static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001177{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001178 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001179 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001180 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001181 uint8_t *classmark2_lv = gh->data + 1;
1182 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001183 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001184 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001185 int rc = 0;
1186
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001187 resp = (struct gsm48_pag_resp *) &gh->data[0];
1188 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1189 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001190 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1191 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001192
Harald Weltefe18d8f2009-02-22 21:14:24 +00001193 switch (mi_type) {
1194 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001195 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001196 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001197 break;
1198 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001199 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1200 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001201 break;
1202 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001203
1204 if (!subscr) {
1205 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001206 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001207 return -EINVAL;
1208 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001209 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001210 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001211 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001212
Harald Weltec2e302d2009-07-05 14:08:13 +02001213 subscr->equipment.classmark2_len = *classmark2_lv;
1214 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1215 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001216
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001217 /* We received a paging */
1218 conn->expire_timer_stopped = 1;
1219
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001220 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001221 return rc;
1222}
1223
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001224static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001225{
1226 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001227 uint8_t apdu_id_flags;
1228 uint8_t apdu_len;
1229 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001230
1231 apdu_id_flags = gh->data[0];
1232 apdu_len = gh->data[1];
1233 apdu_data = gh->data+2;
1234
Harald Welte70f92052012-07-16 13:22:19 +02001235 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001236 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001237
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001238 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001239}
1240
Harald Weltebf5e8df2009-02-03 12:59:45 +00001241/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001242static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001243{
1244 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001245 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001246
1247 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001248 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001249 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001250 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001251 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001252 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001253 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001254 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001255 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001256 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001257 break;
1258 }
1259
Harald Welte2d35ae62009-02-06 12:02:13 +00001260 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001261}
1262
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001263int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1264 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001265{
1266 struct msgb *msg = gsm48_msgb_alloc();
1267 struct gsm48_hdr *gh;
1268
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001269 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001270
1271 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1272 apdu_id, apdu_len);
1273
1274 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1275 gh->proto_discr = GSM48_PDISC_RR;
1276 gh->msg_type = GSM48_MT_RR_APP_INFO;
1277 gh->data[0] = apdu_id;
1278 gh->data[1] = apdu_len;
1279 memcpy(gh->data+2, apdu, apdu_len);
1280
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001281 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001282}
1283
Harald Welte4bc90a12008-12-27 16:32:52 +00001284/* Call Control */
1285
Harald Welte7584aea2009-02-11 11:44:12 +00001286/* The entire call control code is written in accordance with Figure 7.10c
1287 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1288 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1289 * it for voice */
1290
Harald Welte4bfdfe72009-06-10 23:11:52 +08001291static void new_cc_state(struct gsm_trans *trans, int state)
1292{
1293 if (state > 31 || state < 0)
1294 return;
1295
1296 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001297 gsm48_cc_state_name(trans->cc.state),
1298 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299
Harald Weltedcaf5652009-07-23 18:56:43 +02001300 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301}
1302
1303static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001304{
1305 struct msgb *msg = gsm48_msgb_alloc();
1306 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001307 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001308
Harald Welte4bc90a12008-12-27 16:32:52 +00001309 gh->msg_type = GSM48_MT_CC_STATUS;
1310
1311 cause = msgb_put(msg, 3);
1312 cause[0] = 2;
1313 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1314 cause[2] = 0x80 | 30; /* response to status inquiry */
1315
1316 call_state = msgb_put(msg, 1);
1317 call_state[0] = 0xc0 | 0x00;
1318
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001319 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001320}
1321
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001322static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001323 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001324{
1325 struct msgb *msg = gsm48_msgb_alloc();
1326 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1327
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001328 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001329
Harald Welte6f4b7532008-12-29 00:39:37 +00001330 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001331 gh->msg_type = msg_type;
1332
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001333 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001334}
1335
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1337{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001338 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001339 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001340 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001341 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342 }
1343}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001344
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1346 int msg_type, struct gsm_mncc *mncc)
1347{
1348 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001349 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350
1351 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001352 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001353 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001355 trans->conn->lchan->ts->trx->bts->nr,
1356 trans->conn->lchan->ts->trx->nr,
1357 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 (trans->subscr)?(trans->subscr->extension):"-",
1359 get_mncc_name(msg_type));
1360 else
1361 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1362 "Sending '%s' to MNCC.\n",
1363 (trans->subscr)?(trans->subscr->extension):"-",
1364 get_mncc_name(msg_type));
1365 else
1366 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1367 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1368
1369 mncc->msg_type = msg_type;
1370
Harald Welte966636f2009-06-26 19:39:35 +02001371 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 if (!msg)
1373 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001374
1375 data = msgb_put(msg, sizeof(struct gsm_mncc));
1376 memcpy(data, mncc, sizeof(struct gsm_mncc));
1377
Harald Welte54209c22010-12-22 23:43:29 +01001378 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379
1380 return 0;
1381}
1382
1383int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001384 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385{
1386 struct gsm_mncc rel;
1387
Harald Welte92f70c52009-06-12 01:54:08 +08001388 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001390 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001391 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1392 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1394}
1395
Harald Weltedcaf5652009-07-23 18:56:43 +02001396/* Call Control Specific transaction release.
1397 * gets called by trans_free, DO NOT CALL YOURSELF! */
1398void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400 gsm48_stop_cc_timer(trans);
1401
1402 /* send release to L4, if callref still exists */
1403 if (trans->callref) {
1404 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001405 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001406 GSM48_CAUSE_LOC_PRN_S_LU,
1407 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001409 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001411 if (trans->conn)
1412 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001413}
1414
1415static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001416
Harald Welte09e38af2009-02-16 22:52:23 +00001417/* call-back from paging the B-end of the connection */
1418static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001419 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001420{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001421 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001422 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001423
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001424 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001425
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001427 switch (event) {
1428 case GSM_PAGING_SUCCEEDED:
1429 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001430 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001431 /* Assign lchan */
1432 transt->conn = conn;
1433 /* send SETUP request to called party */
1434 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1435 break;
1436 case GSM_PAGING_EXPIRED:
1437 case GSM_PAGING_BUSY:
1438 DEBUGP(DCC, "Paging subscr %s expired!\n",
1439 transt->subscr->extension);
1440 /* Temporarily out of order */
1441 mncc_release_ind(transt->net, transt,
1442 transt->callref,
1443 GSM48_CAUSE_LOC_PRN_S_LU,
1444 GSM48_CC_CAUSE_DEST_OOO);
1445 transt->callref = 0;
1446 transt->paging_request = NULL;
1447 trans_free(transt);
1448 break;
1449 default:
1450 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1451 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001453
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001454 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001455 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001456}
Harald Welte7584aea2009-02-11 11:44:12 +00001457
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001458static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001459
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001460/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001461static int switch_for_handover(struct gsm_lchan *old_lchan,
1462 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001463{
1464 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001465
1466 if (ipacc_rtp_direct) {
1467 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1468 return 0;
1469 }
1470
1471 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001472 new_rs = new_lchan->abis_ip.rtp_socket;
1473 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001474
1475 if (!new_rs) {
1476 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1477 return -EIO;
1478 }
1479
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001480 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001481
1482 if (!old_rs) {
1483 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1484 return -EIO;
1485 }
1486
1487 /* copy rx_action and reference to other sock */
1488 new_rs->rx_action = old_rs->rx_action;
1489 new_rs->tx_action = old_rs->tx_action;
1490 new_rs->transmit = old_rs->transmit;
1491
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001492 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001493 case RTP_PROXY:
1494 other_rs = old_rs->proxy.other_sock;
1495 rtp_socket_proxy(new_rs, other_rs);
1496 /* delete reference to other end socket to prevent
1497 * rtp_socket_free() from removing the inverse reference */
1498 old_rs->proxy.other_sock = NULL;
1499 break;
1500 case RTP_RECV_UPSTREAM:
1501 new_rs->receive = old_rs->receive;
1502 break;
1503 case RTP_NONE:
1504 break;
1505 }
1506
1507 return 0;
1508}
1509
Harald Welte805f6442009-07-28 18:25:29 +02001510/* some other part of the code sends us a signal */
1511static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1512 void *handler_data, void *signal_data)
1513{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001514 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001515 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001516 struct gsm_network *net;
1517 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001518
1519 if (subsys != SS_ABISIP)
1520 return 0;
1521
1522 /* in case we use direct BTS-to-BTS RTP */
1523 if (ipacc_rtp_direct)
1524 return 0;
1525
Harald Welte805f6442009-07-28 18:25:29 +02001526 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001527 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001528 /* in case we don't use direct BTS-to-BTS RTP */
1529 /* the BTS has successfully bound a TCH to a local ip/port,
1530 * which means we can connect our UDP socket to it */
1531 if (lchan->abis_ip.rtp_socket) {
1532 rtp_socket_free(lchan->abis_ip.rtp_socket);
1533 lchan->abis_ip.rtp_socket = NULL;
1534 }
1535
1536 lchan->abis_ip.rtp_socket = rtp_socket_create();
1537 if (!lchan->abis_ip.rtp_socket)
1538 return -EIO;
1539
1540 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1541 lchan->abis_ip.bound_ip,
1542 lchan->abis_ip.bound_port);
1543 if (rc < 0)
1544 return -EIO;
1545
Harald Welteda7ab742009-12-19 22:23:05 +01001546 /* check if any transactions on this lchan still have
1547 * a tch_recv_mncc request pending */
1548 net = lchan->ts->trx->bts->network;
1549 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001550 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001551 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1552 tch_recv_mncc(net, trans->callref, 1);
1553 }
1554 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001555
1556 /*
1557 * TODO: this appears to be too early? Why not until after
1558 * the handover detect or the handover complete?
1559 *
1560 * Do we have a handover pending for this new lchan? In that
1561 * case re-route the audio from the old channel to the new one.
1562 */
1563 old_lchan = bsc_handover_pending(lchan);
1564 if (old_lchan)
1565 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001566 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001567 case S_ABISIP_DLCX_IND:
1568 /* the BTS tells us a RTP stream has been disconnected */
1569 if (lchan->abis_ip.rtp_socket) {
1570 rtp_socket_free(lchan->abis_ip.rtp_socket);
1571 lchan->abis_ip.rtp_socket = NULL;
1572 }
1573
1574 break;
Harald Welte805f6442009-07-28 18:25:29 +02001575 }
1576
1577 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001578}
1579
Harald Welte49f48b82009-02-17 15:29:33 +00001580/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001581static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001582{
Harald Welte11fa29c2009-02-19 17:24:39 +00001583 struct gsm_bts *bts = lchan->ts->trx->bts;
1584 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001585 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001586
Harald Welte11fa29c2009-02-19 17:24:39 +00001587 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1588 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1589 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1590
1591 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001592 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001593 return -EINVAL;
1594 }
Harald Welteda7ab742009-12-19 22:23:05 +01001595
1596 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001597 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001598 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001599 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001600 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001601 if (!lchan->abis_ip.rtp_socket) {
1602 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1603 "lchan\n");
1604 return -EIO;
1605 }
1606 if (!remote_lchan->abis_ip.rtp_socket) {
1607 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1608 "remote_lchan\n");
1609 return -EIO;
1610 }
1611
Harald Welte805f6442009-07-28 18:25:29 +02001612 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001613 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001614 if (rc < 0)
1615 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001616 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001617 if (rc < 0)
1618 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001619 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301620 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1621 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001622 } else {
1623 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301624 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1625 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301626 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001627 if (rc < 0)
1628 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301629 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1630 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301631 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001632 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001633 break;
1634 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001635 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001636 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001637 trau_mux_map_lchan(lchan, remote_lchan);
1638 break;
1639 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001640 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001641 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001642 }
Harald Welte49f48b82009-02-17 15:29:33 +00001643
1644 return 0;
1645}
1646
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001648static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001649{
Harald Weltedcaf5652009-07-23 18:56:43 +02001650 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1651 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001652
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001654 return -EIO;
1655
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001656 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 return -EIO;
1658
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001659 /* Which subscriber do we want to track trans1 or trans2? */
1660 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1661
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001663 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001664}
1665
Harald Welteda7ab742009-12-19 22:23:05 +01001666/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001667static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668{
1669 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001670 struct gsm_lchan *lchan;
1671 struct gsm_bts *bts;
1672 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001673
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001675 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 if (!trans)
1677 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001678 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001680
1681 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001682 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001683 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001685 /* store receive state */
1686 trans->tch_recv = enable;
1687
Harald Welteda7ab742009-12-19 22:23:05 +01001688 switch (bts->type) {
1689 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001690 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001691 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001692 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001693 return -EINVAL;
1694 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001695 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1696 * will not be connected to our RTP proxy and the socket will
1697 * not be assigned to the application interface. This method
1698 * will be called again, once the audio socket is created and
1699 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001700 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001701 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1702 return 0;
1703 }
1704 if (enable) {
1705 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001706 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001707 if (rc < 0)
1708 return rc;
1709 /* assign socket to application interface */
1710 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1711 net, callref);
1712 } else
1713 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1714 net, 0);
1715 break;
1716 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001717 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001718 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001719 /* In case we don't have a TCH with correct mode, the TRAU muxer
1720 * will not be asigned to the application interface. This is
1721 * performed by switch_trau_mux() after successful handover or
1722 * assignment. */
1723 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1724 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1725 return 0;
1726 }
Harald Welteda7ab742009-12-19 22:23:05 +01001727 if (enable)
1728 return trau_recv_lchan(lchan, callref);
1729 return trau_mux_unmap(NULL, callref);
1730 break;
1731 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001732 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001733 return -EINVAL;
1734 }
1735
1736 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737}
1738
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1740{
1741 DEBUGP(DCC, "-> STATUS ENQ\n");
1742 return gsm48_cc_tx_status(trans, msg);
1743}
1744
1745static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1746static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1747
1748static void gsm48_cc_timeout(void *arg)
1749{
1750 struct gsm_trans *trans = arg;
1751 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001752 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1753 int mo_location = GSM48_CAUSE_LOC_USER;
1754 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1755 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 struct gsm_mncc mo_rel, l4_rel;
1757
1758 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1759 mo_rel.callref = trans->callref;
1760 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1761 l4_rel.callref = trans->callref;
1762
Harald Weltedcaf5652009-07-23 18:56:43 +02001763 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 case 0x303:
1765 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001766 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 break;
1768 case 0x310:
1769 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001770 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 break;
1772 case 0x313:
1773 disconnect = 1;
1774 /* unknown, did not find it in the specs */
1775 break;
1776 case 0x301:
1777 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001778 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 break;
1780 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001781 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001783 gsm48_cc_tx_release(trans, &trans->cc.msg);
1784 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 break; /* stay in release state */
1786 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001787 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 return;
1789// release = 1;
1790// l4_cause = 14;
1791// break;
1792 case 0x306:
1793 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001794 mo_cause = trans->cc.msg.cause.value;
1795 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 break;
1797 case 0x323:
1798 disconnect = 1;
1799 break;
1800 default:
1801 release = 1;
1802 }
1803
1804 if (release && trans->callref) {
1805 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001806 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 l4_location, l4_cause);
1808 trans->callref = 0;
1809 }
1810
1811 if (disconnect && trans->callref) {
1812 /* process disconnect towards layer 4 */
1813 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001814 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815 }
1816
1817 /* process disconnect towards mobile station */
1818 if (disconnect || release) {
1819 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001820 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1821 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1822 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 mo_rel.cause.diag_len = 3;
1824
1825 if (disconnect)
1826 gsm48_cc_tx_disconnect(trans, &mo_rel);
1827 if (release)
1828 gsm48_cc_tx_release(trans, &mo_rel);
1829 }
1830
1831}
1832
1833static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1834 int sec, int micro)
1835{
1836 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001837 trans->cc.timer.cb = gsm48_cc_timeout;
1838 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001839 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001840 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841}
1842
1843static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1844{
1845 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001846 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1848 struct tlv_parsed tp;
1849 struct gsm_mncc setup;
1850
1851 memset(&setup, 0, sizeof(struct gsm_mncc));
1852 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001853 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001854 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 /* emergency setup is identified by msg_type */
1856 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1857 setup.emergency = 1;
1858
1859 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001860 setup.fields |= MNCC_F_CALLING;
1861 strncpy(setup.calling.number, trans->subscr->extension,
1862 sizeof(setup.calling.number)-1);
1863 strncpy(setup.imsi, trans->subscr->imsi,
1864 sizeof(setup.imsi)-1);
1865
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 /* bearer capability */
1867 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1868 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001869 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001871 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 }
1873 /* facility */
1874 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1875 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001876 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1878 }
1879 /* called party bcd number */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1881 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001882 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1884 }
1885 /* user-user */
1886 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1887 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001888 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1890 }
1891 /* ss-version */
1892 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1893 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001894 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1896 }
1897 /* CLIR suppression */
1898 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1899 setup.clir.sup = 1;
1900 /* CLIR invocation */
1901 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1902 setup.clir.inv = 1;
1903 /* cc cap */
1904 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1905 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001906 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1908 }
1909
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910 new_cc_state(trans, GSM_CSTATE_INITIATED);
1911
Harald Welte (local)47df3992009-12-26 19:45:03 +01001912 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1913 subscr_name(trans->subscr), trans->subscr->extension,
1914 setup.called.number);
1915
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001916 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001917
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001919 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920
Harald Welte13cac662009-07-29 12:10:35 +02001921 /* MNCC code will modify the channel asynchronously, we should
1922 * ipaccess-bind only after the modification has been made to the
1923 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 return 0;
1925}
1926
1927static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001928{
1929 struct msgb *msg = gsm48_msgb_alloc();
1930 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001932 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001933
Harald Welte7ccf7782009-02-17 01:43:01 +00001934 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001935
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 /* transaction id must not be assigned */
1937 if (trans->transaction_id != 0xff) { /* unasssigned */
1938 DEBUGP(DCC, "TX Setup with assigned transaction. "
1939 "This is not allowed!\n");
1940 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001941 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001942 GSM48_CAUSE_LOC_PRN_S_LU,
1943 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001945 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 return rc;
1947 }
1948
1949 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001950 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1951 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001952 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001954 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001955 GSM48_CAUSE_LOC_PRN_S_LU,
1956 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001958 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 return rc;
1960 }
Harald Welte78283ef2009-07-23 21:36:44 +02001961 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001962
Harald Welte65e74cc2008-12-29 01:55:35 +00001963 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001964
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001966
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 /* bearer capability */
1968 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 /* facility */
1971 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 /* progress */
1974 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 /* calling party BCD number */
1977 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* called party BCD number */
1980 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 /* user-user */
1983 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001984 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985 /* redirecting party BCD number */
1986 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001987 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* signal */
1989 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991
1992 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001993
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001994 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001995
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001996 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001997}
1998
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2000{
2001 struct gsm48_hdr *gh = msgb_l3(msg);
2002 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2003 struct tlv_parsed tp;
2004 struct gsm_mncc call_conf;
2005
2006 gsm48_stop_cc_timer(trans);
2007 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2008
2009 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2010 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002011 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002012 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013#if 0
2014 /* repeat */
2015 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2016 call_conf.repeat = 1;
2017 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2018 call_conf.repeat = 2;
2019#endif
2020 /* bearer capability */
2021 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2022 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002023 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002025 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 }
2027 /* cause */
2028 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2029 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2032 }
2033 /* cc cap */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2035 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2038 }
2039
Andreas Eversberg035b8742013-09-19 09:27:06 +02002040 /* IMSI of called subscriber */
2041 strncpy(call_conf.imsi, trans->subscr->imsi,
2042 sizeof(call_conf.imsi)-1);
2043
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2045
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002046 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002047 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048}
2049
2050static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2051{
2052 struct gsm_mncc *proceeding = arg;
2053 struct msgb *msg = gsm48_msgb_alloc();
2054 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2055
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2057
2058 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2059
2060 /* bearer capability */
2061 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002062 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 /* facility */
2064 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002065 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 /* progress */
2067 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002070 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071}
2072
2073static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2074{
2075 struct gsm48_hdr *gh = msgb_l3(msg);
2076 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2077 struct tlv_parsed tp;
2078 struct gsm_mncc alerting;
2079
2080 gsm48_stop_cc_timer(trans);
2081 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2082
2083 memset(&alerting, 0, sizeof(struct gsm_mncc));
2084 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002085 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 /* facility */
2087 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2088 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002089 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2091 }
2092
2093 /* progress */
2094 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2095 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2098 }
2099 /* ss-version */
2100 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2101 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002102 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2104 }
2105
2106 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2107
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002108 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002109 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110}
2111
2112static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2113{
2114 struct gsm_mncc *alerting = arg;
2115 struct msgb *msg = gsm48_msgb_alloc();
2116 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2117
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 gh->msg_type = GSM48_MT_CC_ALERTING;
2119
2120 /* facility */
2121 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 /* progress */
2124 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002125 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 /* user-user */
2127 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002128 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129
2130 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002132 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133}
2134
2135static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2136{
2137 struct gsm_mncc *progress = arg;
2138 struct msgb *msg = gsm48_msgb_alloc();
2139 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2140
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 gh->msg_type = GSM48_MT_CC_PROGRESS;
2142
2143 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002144 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 /* user-user */
2146 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002147 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002149 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150}
2151
2152static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2153{
2154 struct gsm_mncc *connect = arg;
2155 struct msgb *msg = gsm48_msgb_alloc();
2156 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2157
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 gh->msg_type = GSM48_MT_CC_CONNECT;
2159
2160 gsm48_stop_cc_timer(trans);
2161 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2162
2163 /* facility */
2164 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002165 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 /* progress */
2167 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 /* connected number */
2170 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002171 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 /* user-user */
2173 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002174 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175
2176 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2177
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002178 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179}
2180
2181static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2182{
2183 struct gsm48_hdr *gh = msgb_l3(msg);
2184 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2185 struct tlv_parsed tp;
2186 struct gsm_mncc connect;
2187
2188 gsm48_stop_cc_timer(trans);
2189
2190 memset(&connect, 0, sizeof(struct gsm_mncc));
2191 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002192 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002194 connect.fields |= MNCC_F_CONNECTED;
2195 strncpy(connect.connected.number, trans->subscr->extension,
2196 sizeof(connect.connected.number)-1);
2197 strncpy(connect.imsi, trans->subscr->imsi,
2198 sizeof(connect.imsi)-1);
2199
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 /* facility */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2202 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2205 }
2206 /* user-user */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2208 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2211 }
2212 /* ss-version */
2213 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2214 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002215 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2217 }
2218
2219 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002220 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002222 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223}
2224
2225
2226static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2227{
2228 struct gsm_mncc connect_ack;
2229
2230 gsm48_stop_cc_timer(trans);
2231
2232 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002233 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234
2235 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2236 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002237
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002238 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 &connect_ack);
2240}
2241
2242static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2243{
2244 struct msgb *msg = gsm48_msgb_alloc();
2245 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2246
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2248
2249 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2250
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002251 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252}
2253
2254static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2255{
2256 struct gsm48_hdr *gh = msgb_l3(msg);
2257 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2258 struct tlv_parsed tp;
2259 struct gsm_mncc disc;
2260
2261 gsm48_stop_cc_timer(trans);
2262
2263 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2264
2265 memset(&disc, 0, sizeof(struct gsm_mncc));
2266 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002267 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 /* cause */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2270 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2273 }
2274 /* facility */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2276 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2279 }
2280 /* user-user */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2282 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2285 }
2286 /* ss-version */
2287 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2288 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002289 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2291 }
2292
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002293 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294
2295}
2296
Harald Weltec66b71c2009-06-11 14:23:20 +08002297static struct gsm_mncc_cause default_cause = {
2298 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2299 .coding = 0,
2300 .rec = 0,
2301 .rec_val = 0,
2302 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2303 .diag_len = 0,
2304 .diag = { 0 },
2305};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306
2307static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2308{
2309 struct gsm_mncc *disc = arg;
2310 struct msgb *msg = gsm48_msgb_alloc();
2311 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2312
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2314
2315 gsm48_stop_cc_timer(trans);
2316 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2317
2318 /* cause */
2319 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002320 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 else
Harald Welte55c8f352010-03-07 23:40:35 +01002322 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323
2324 /* facility */
2325 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 /* progress */
2328 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002329 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 /* user-user */
2331 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002332 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333
2334 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002335 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336
2337 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2338
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002339 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2343{
2344 struct gsm48_hdr *gh = msgb_l3(msg);
2345 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2346 struct tlv_parsed tp;
2347 struct gsm_mncc rel;
2348 int rc;
2349
2350 gsm48_stop_cc_timer(trans);
2351
2352 memset(&rel, 0, sizeof(struct gsm_mncc));
2353 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002354 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 /* cause */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2357 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2360 }
2361 /* facility */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2363 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002364 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2366 }
2367 /* user-user */
2368 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2369 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002370 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2372 }
2373 /* ss-version */
2374 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2375 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002376 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2378 }
2379
Harald Weltedcaf5652009-07-23 18:56:43 +02002380 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002382 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002384 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002385 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002386 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002387 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 }
2389
2390 new_cc_state(trans, GSM_CSTATE_NULL);
2391
2392 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002393 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394
2395 return rc;
2396}
2397
2398static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2399{
2400 struct gsm_mncc *rel = arg;
2401 struct msgb *msg = gsm48_msgb_alloc();
2402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2403
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 gh->msg_type = GSM48_MT_CC_RELEASE;
2405
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 gsm48_stop_cc_timer(trans);
2407 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2408
2409 /* cause */
2410 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 /* facility */
2413 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002414 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 /* user-user */
2416 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002417 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418
Harald Weltedcaf5652009-07-23 18:56:43 +02002419 trans->cc.T308_second = 0;
2420 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421
Harald Weltedcaf5652009-07-23 18:56:43 +02002422 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2424
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002425 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426}
2427
2428static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2429{
2430 struct gsm48_hdr *gh = msgb_l3(msg);
2431 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2432 struct tlv_parsed tp;
2433 struct gsm_mncc rel;
2434 int rc = 0;
2435
2436 gsm48_stop_cc_timer(trans);
2437
2438 memset(&rel, 0, sizeof(struct gsm_mncc));
2439 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002440 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 /* cause */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2443 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002444 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2446 }
2447 /* facility */
2448 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2449 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002450 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2452 }
2453 /* user-user */
2454 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2455 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2458 }
2459 /* ss-version */
2460 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2461 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002462 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2464 }
2465
2466 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002467 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002469 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 MNCC_REJ_IND, &rel);
2471 break;
2472 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002473 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 MNCC_REL_CNF, &rel);
2475 break;
2476 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002477 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 MNCC_REL_IND, &rel);
2479 }
2480 }
2481
2482 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002483 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484
2485 return rc;
2486}
2487
2488static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2489{
2490 struct gsm_mncc *rel = arg;
2491 struct msgb *msg = gsm48_msgb_alloc();
2492 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002493 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2496
2497 trans->callref = 0;
2498
2499 gsm48_stop_cc_timer(trans);
2500
2501 /* cause */
2502 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002503 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 /* facility */
2505 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 /* user-user */
2508 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510
Harald Weltec7782de2010-12-21 19:31:41 +01002511 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2512
Harald Weltedcaf5652009-07-23 18:56:43 +02002513 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514
Harald Weltec7782de2010-12-21 19:31:41 +01002515 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516}
2517
2518static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2519{
2520 struct gsm48_hdr *gh = msgb_l3(msg);
2521 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2522 struct tlv_parsed tp;
2523 struct gsm_mncc fac;
2524
2525 memset(&fac, 0, sizeof(struct gsm_mncc));
2526 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002527 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 /* facility */
2529 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2530 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002531 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2533 }
2534 /* ss-version */
2535 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2536 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002537 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2539 }
2540
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002541 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542}
2543
2544static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2545{
2546 struct gsm_mncc *fac = arg;
2547 struct msgb *msg = gsm48_msgb_alloc();
2548 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2549
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 gh->msg_type = GSM48_MT_CC_FACILITY;
2551
2552 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002555 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002556}
2557
2558static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2559{
2560 struct gsm_mncc hold;
2561
2562 memset(&hold, 0, sizeof(struct gsm_mncc));
2563 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002564 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565}
2566
2567static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2568{
2569 struct msgb *msg = gsm48_msgb_alloc();
2570 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2571
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2573
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002574 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575}
2576
2577static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2578{
2579 struct gsm_mncc *hold_rej = arg;
2580 struct msgb *msg = gsm48_msgb_alloc();
2581 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2582
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2584
2585 /* cause */
2586 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002587 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 else
Harald Welte55c8f352010-03-07 23:40:35 +01002589 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002591 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592}
2593
2594static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2595{
2596 struct gsm_mncc retrieve;
2597
2598 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2599 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002600 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002601 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002602}
2603
2604static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2605{
2606 struct msgb *msg = gsm48_msgb_alloc();
2607 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2608
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2610
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002611 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612}
2613
2614static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2615{
2616 struct gsm_mncc *retrieve_rej = arg;
2617 struct msgb *msg = gsm48_msgb_alloc();
2618 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2619
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2621
2622 /* cause */
2623 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002624 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 else
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002628 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629}
2630
2631static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2632{
2633 struct gsm48_hdr *gh = msgb_l3(msg);
2634 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2635 struct tlv_parsed tp;
2636 struct gsm_mncc dtmf;
2637
2638 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2639 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002640 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 /* keypad facility */
2642 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2643 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002644 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2646 }
2647
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002648 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649}
2650
2651static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2652{
2653 struct gsm_mncc *dtmf = arg;
2654 struct msgb *msg = gsm48_msgb_alloc();
2655 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2656
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2658
2659 /* keypad */
2660 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002661 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002663 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2667{
2668 struct gsm_mncc *dtmf = arg;
2669 struct msgb *msg = gsm48_msgb_alloc();
2670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2671
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2673
2674 /* cause */
2675 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 else
Harald Welte55c8f352010-03-07 23:40:35 +01002678 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002680 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681}
2682
2683static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2684{
2685 struct msgb *msg = gsm48_msgb_alloc();
2686 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2687
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2689
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002690 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691}
2692
2693static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2694{
2695 struct gsm_mncc dtmf;
2696
2697 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2698 dtmf.callref = trans->callref;
2699
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002700 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701}
2702
2703static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2704{
2705 struct gsm48_hdr *gh = msgb_l3(msg);
2706 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2707 struct tlv_parsed tp;
2708 struct gsm_mncc modify;
2709
2710 memset(&modify, 0, sizeof(struct gsm_mncc));
2711 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002712 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 /* bearer capability */
2714 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2715 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002716 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002718 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 }
2720
2721 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2722
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002723 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724}
2725
2726static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2727{
2728 struct gsm_mncc *modify = arg;
2729 struct msgb *msg = gsm48_msgb_alloc();
2730 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2731
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 gh->msg_type = GSM48_MT_CC_MODIFY;
2733
2734 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2735
2736 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002737 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738
2739 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2740
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002741 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742}
2743
2744static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2745{
2746 struct gsm48_hdr *gh = msgb_l3(msg);
2747 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2748 struct tlv_parsed tp;
2749 struct gsm_mncc modify;
2750
2751 gsm48_stop_cc_timer(trans);
2752
2753 memset(&modify, 0, sizeof(struct gsm_mncc));
2754 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002755 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756 /* bearer capability */
2757 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2758 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002759 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002761 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 }
2763
2764 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2765
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002766 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767}
2768
2769static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2770{
2771 struct gsm_mncc *modify = arg;
2772 struct msgb *msg = gsm48_msgb_alloc();
2773 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2774
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2776
2777 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002778 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779
2780 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2781
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002782 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783}
2784
2785static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2786{
2787 struct gsm48_hdr *gh = msgb_l3(msg);
2788 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2789 struct tlv_parsed tp;
2790 struct gsm_mncc modify;
2791
2792 gsm48_stop_cc_timer(trans);
2793
2794 memset(&modify, 0, sizeof(struct gsm_mncc));
2795 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002796 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797 /* bearer capability */
2798 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2799 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002800 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002802 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 }
2804 /* cause */
2805 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2806 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002807 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2809 }
2810
2811 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2812
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002813 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814}
2815
2816static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2817{
2818 struct gsm_mncc *modify = arg;
2819 struct msgb *msg = gsm48_msgb_alloc();
2820 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2821
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2823
2824 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002825 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002827 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828
2829 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2830
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002831 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2835{
2836 struct gsm_mncc *notify = arg;
2837 struct msgb *msg = gsm48_msgb_alloc();
2838 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2839
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840 gh->msg_type = GSM48_MT_CC_NOTIFY;
2841
2842 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002843 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002845 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846}
2847
2848static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2849{
2850 struct gsm48_hdr *gh = msgb_l3(msg);
2851 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2852// struct tlv_parsed tp;
2853 struct gsm_mncc notify;
2854
2855 memset(&notify, 0, sizeof(struct gsm_mncc));
2856 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002857// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002859 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002861 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862}
2863
2864static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2865{
2866 struct gsm_mncc *user = arg;
2867 struct msgb *msg = gsm48_msgb_alloc();
2868 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2869
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870 gh->msg_type = GSM48_MT_CC_USER_INFO;
2871
2872 /* user-user */
2873 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002874 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 /* more data */
2876 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002877 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002879 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880}
2881
2882static int gsm48_cc_rx_userinfo(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 user;
2888
2889 memset(&user, 0, sizeof(struct gsm_mncc));
2890 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002891 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892 /* user-user */
2893 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2894 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002895 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2897 }
2898 /* more data */
2899 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2900 user.more = 1;
2901
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002902 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903}
2904
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002905static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906{
2907 struct gsm_mncc *mode = arg;
2908
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002909 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2910 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911}
2912
2913static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002914 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915 int type;
2916 int (*rout) (struct gsm_trans *trans, void *arg);
2917} downstatelist[] = {
2918 /* mobile originating call establishment */
2919 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2920 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2921 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2922 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2923 {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 */
2924 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2925 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2926 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2927 /* mobile terminating call establishment */
2928 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2929 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2930 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2931 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2932 /* signalling during call */
2933 {SBIT(GSM_CSTATE_ACTIVE),
2934 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2935 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2936 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2937 {ALL_STATES,
2938 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2939 {ALL_STATES,
2940 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2941 {ALL_STATES,
2942 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2945 {SBIT(GSM_CSTATE_ACTIVE),
2946 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2947 {SBIT(GSM_CSTATE_ACTIVE),
2948 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2949 {SBIT(GSM_CSTATE_ACTIVE),
2950 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2951 {SBIT(GSM_CSTATE_ACTIVE),
2952 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2953 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2954 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2955 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2956 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2957 {SBIT(GSM_CSTATE_ACTIVE),
2958 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2959 /* clearing */
2960 {SBIT(GSM_CSTATE_INITIATED),
2961 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2962 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2963 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2964 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2965 MNCC_REL_REQ, gsm48_cc_tx_release},
2966 /* special */
2967 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002968 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969};
2970
2971#define DOWNSLLEN \
2972 (sizeof(downstatelist) / sizeof(struct downstate))
2973
2974
Harald Welte76556372010-12-22 23:57:45 +01002975int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976{
Harald Welte1a6f7982009-08-09 18:52:33 +02002977 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002979 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002980 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981 struct gsm_mncc *data = arg, rel;
2982
Harald Welte04dc88f2010-12-22 21:45:05 +01002983 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2984
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985 /* handle special messages */
2986 switch(msg_type) {
2987 case MNCC_BRIDGE:
2988 return tch_bridge(net, arg);
2989 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002990 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002992 return tch_recv_mncc(net, data->callref, 1);
2993 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002994 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002995 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002996 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01002997 /* Find callref */
2998 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002999 if (!trans) {
3000 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003001 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003002 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003003 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003004 if (!trans->conn) {
3005 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003006 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003007 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003008 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3009 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003010 /* This should be LOGL_ERROR or NOTICE, but
3011 * unfortuantely it happens for a couple of frames at
3012 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003013 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003014 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003015 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003016 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003017 switch (bts->type) {
3018 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003019 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003020 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003021 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003022 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003023 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003024 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003025 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003026 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003027 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003028 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003029 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003030 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003031 }
3032 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 }
3034
3035 memset(&rel, 0, sizeof(struct gsm_mncc));
3036 rel.callref = data->callref;
3037
3038 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003039 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040
3041 /* Callref unknown */
3042 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003043 struct gsm_subscriber *subscr;
3044
Harald Welte4a3464c2009-07-04 10:11:24 +02003045 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003046 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3047 "Received '%s' from MNCC with "
3048 "unknown callref %d\n", data->called.number,
3049 get_mncc_name(msg_type), data->callref);
3050 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003051 return mncc_release_ind(net, NULL, data->callref,
3052 GSM48_CAUSE_LOC_PRN_S_LU,
3053 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003055 if (!data->called.number[0] && !data->imsi[0]) {
3056 DEBUGP(DCC, "(bts - trx - ts - ti) "
3057 "Received '%s' from MNCC with "
3058 "no number or IMSI\n", get_mncc_name(msg_type));
3059 /* Invalid number */
3060 return mncc_release_ind(net, NULL, data->callref,
3061 GSM48_CAUSE_LOC_PRN_S_LU,
3062 GSM48_CC_CAUSE_INV_NR_FORMAT);
3063 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003065 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003066 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003067 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003068 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003069 subscr = subscr_get_by_imsi(net->subscr_group,
3070 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003071
3072 /* update the subscriber we deal with */
3073 log_set_context(BSC_CTX_SUBSCR, subscr);
3074
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 /* If subscriber is not found */
3076 if (!subscr) {
3077 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3078 "Received '%s' from MNCC with "
3079 "unknown subscriber %s\n", data->called.number,
3080 get_mncc_name(msg_type), data->called.number);
3081 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003082 return mncc_release_ind(net, NULL, data->callref,
3083 GSM48_CAUSE_LOC_PRN_S_LU,
3084 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085 }
3086 /* If subscriber is not "attached" */
3087 if (!subscr->lac) {
3088 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3089 "Received '%s' from MNCC with "
3090 "detached subscriber %s\n", data->called.number,
3091 get_mncc_name(msg_type), data->called.number);
3092 subscr_put(subscr);
3093 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003094 return mncc_release_ind(net, NULL, data->callref,
3095 GSM48_CAUSE_LOC_PRN_S_LU,
3096 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003097 }
3098 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003099 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003100 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 DEBUGP(DCC, "No memory for trans.\n");
3102 subscr_put(subscr);
3103 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003104 mncc_release_ind(net, NULL, data->callref,
3105 GSM48_CAUSE_LOC_PRN_S_LU,
3106 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 return -ENOMEM;
3108 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003109 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003110 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003111
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003113 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114 /* find transaction with this subscriber already paging */
3115 llist_for_each_entry(transt, &net->trans_list, entry) {
3116 /* Transaction of our lchan? */
3117 if (transt == trans ||
3118 transt->subscr != subscr)
3119 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003120 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003121 "Received '%s' from MNCC with "
3122 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003123 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003124 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003125 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003126 subscr_put(subscr);
3127 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128 return 0;
3129 }
3130 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003131 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003132
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003133 /* Request a channel */
3134 trans->paging_request = subscr_request_channel(subscr,
3135 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3136 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003137 if (!trans->paging_request) {
3138 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3139 subscr_put(subscr);
3140 trans_free(trans);
3141 return 0;
3142 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003143 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 return 0;
3145 }
3146 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003147 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003148 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003149 } else {
3150 /* update the subscriber we deal with */
3151 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003153
3154 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003155 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003156
3157 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003158 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003160 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161 (trans->subscr)?(trans->subscr->extension):"-",
3162 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003163 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3164 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003165 if (msg_type == MNCC_REL_REQ)
3166 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3167 else
3168 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3169 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003170 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 return rc;
3172 }
3173
3174 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3175 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003176 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003178 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003179 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003180 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003181
3182 /* Find function for current state and message */
3183 for (i = 0; i < DOWNSLLEN; i++)
3184 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003185 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 break;
3187 if (i == DOWNSLLEN) {
3188 DEBUGP(DCC, "Message unhandled at this state.\n");
3189 return 0;
3190 }
3191
3192 rc = downstatelist[i].rout(trans, arg);
3193
3194 return rc;
3195}
3196
3197
3198static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003199 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003200 int type;
3201 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3202} datastatelist[] = {
3203 /* mobile originating call establishment */
3204 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3205 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3206 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3207 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3208 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3209 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3210 /* mobile terminating call establishment */
3211 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3212 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3213 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3214 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003215 {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 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003216 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3217 /* signalling during call */
3218 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3219 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3220 {SBIT(GSM_CSTATE_ACTIVE),
3221 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3222 {ALL_STATES,
3223 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3224 {ALL_STATES,
3225 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3226 {ALL_STATES,
3227 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3228 {SBIT(GSM_CSTATE_ACTIVE),
3229 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3230 {SBIT(GSM_CSTATE_ACTIVE),
3231 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3232 {SBIT(GSM_CSTATE_ACTIVE),
3233 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3234 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3235 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3236 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3237 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3238 {SBIT(GSM_CSTATE_ACTIVE),
3239 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3240 /* clearing */
3241 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3242 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3243 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3244 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3245 {ALL_STATES, /* 5.4.3.4 */
3246 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3247};
3248
3249#define DATASLLEN \
3250 (sizeof(datastatelist) / sizeof(struct datastate))
3251
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003252static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003253{
3254 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003255 uint8_t msg_type = gh->msg_type & 0xbf;
3256 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003257 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003258 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003259
Harald Welte4bfdfe72009-06-10 23:11:52 +08003260 if (msg_type & 0x80) {
3261 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3262 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003263 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003264
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003266 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003267
Harald Welte6f5aee02009-07-23 21:21:14 +02003268 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003270 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003271 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003272 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3273 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003274
3275 /* Create transaction */
3276 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003277 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003278 "creating new trans.\n", transaction_id);
3279 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003280 trans = trans_alloc(conn->bts->network, conn->subscr,
3281 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003282 transaction_id, new_callref++);
3283 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003284 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003285 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003286 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 GSM48_MT_CC_RELEASE_COMPL);
3288 return -ENOMEM;
3289 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003290 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003291 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003292 }
3293
3294 /* find function for current state and message */
3295 for (i = 0; i < DATASLLEN; i++)
3296 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003297 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003298 break;
3299 if (i == DATASLLEN) {
3300 DEBUGP(DCC, "Message unhandled at this state.\n");
3301 return 0;
3302 }
3303
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003304 assert(trans->subscr);
3305
Harald Welte4bfdfe72009-06-10 23:11:52 +08003306 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003307
3308 return rc;
3309}
3310
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003311/* Create a dummy to wait five seconds */
3312static void release_anchor(struct gsm_subscriber_connection *conn)
3313{
3314 if (!conn->anch_operation)
3315 return;
3316
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003317 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003318 talloc_free(conn->anch_operation);
3319 conn->anch_operation = NULL;
3320}
3321
3322static void anchor_timeout(void *_data)
3323{
3324 struct gsm_subscriber_connection *con = _data;
3325
3326 release_anchor(con);
3327 msc_release_connection(con);
3328}
3329
3330int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3331{
3332 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3333 if (!conn->anch_operation)
3334 return -1;
3335
3336 conn->anch_operation->timeout.data = conn;
3337 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003338 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003339 return 0;
3340}
3341
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003342/* here we get data from the BSC level... */
3343int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003344{
3345 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003346 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003347 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003348
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003349 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003350 if (silent_call_reroute(conn, msg))
3351 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003352
3353 switch (pdisc) {
3354 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003355 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003356 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003357 break;
3358 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003359 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003360 break;
3361 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003362 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003363 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003364 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003365 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003366 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003367 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003368 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003369 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003370 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3371 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003372 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003373 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003374 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003375 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003376 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003377 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003378 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3379 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003380 break;
3381 }
3382
3383 return rc;
3384}
Harald Welte8470bf22008-12-25 23:28:35 +00003385
Harald Welte805f6442009-07-28 18:25:29 +02003386/*
3387 * This will be ran by the linker when loading the DSO. We use it to
3388 * do system initialization, e.g. registration of signal handlers.
3389 */
3390static __attribute__((constructor)) void on_dso_load_0408(void)
3391{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003392 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003393}