blob: 0f00db151a31c4f902476b60711df5859c2911a8 [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080012 * (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 Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080018 *
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
Harald Welteeaf4a522012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut28c47162009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
39#include <openbsc/gsm_04_11.h>
40#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080042#include <openbsc/abis_rsl.h>
43#include <openbsc/chan_alloc.h>
44#include <openbsc/paging.h>
45#include <openbsc/signal.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte59b04682009-06-10 05:40:52 +080047#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Holger Hans Peter Freyther8d88a922015-08-20 19:10:58 +020055#include <openbsc/mncc_int.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020056#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010057#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080058
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010059#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
Max3a816e82016-04-18 23:11:18 +020062#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010063#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/gsm/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080066
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +020067#include <assert.h>
68
Harald Welte (local)8751ee92009-08-15 02:30:58 +020069void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010070void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020071
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +020072static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
73
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +020074static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080075static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020076 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010077static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080078static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080079
80struct gsm_lai {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020081 uint16_t mcc;
82 uint16_t mnc;
83 uint16_t lac;
Harald Welte59b04682009-06-10 05:40:52 +080084};
85
Andreas Eversberg976493b2013-12-07 18:32:28 +010086static int apply_codec_restrictions(struct gsm_bts *bts,
87 struct gsm_mncc_bearer_cap *bcap)
88{
89 int i, j;
90
91 /* remove unsupported speech versions from list */
92 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
103 }
104 bcap->speech_ver[j] = -1;
105
106 return 0;
107}
108
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200109static uint32_t new_callref = 0x80000001;
Harald Welte03740842009-06-10 23:11:52 +0800110
Harald Welte4f272cc2011-03-03 23:29:05 +0100111void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
112{
113 net->mncc_recv(net, msg);
114}
115
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800116static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
117 struct gsm_trans *trans)
118{
119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
120
121 /* if we get passed a transaction reference, do some common
122 * work that the caller no longer has to do */
123 if (trans) {
124 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
125 msg->lchan = trans->conn->lchan;
126 }
127
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800128 if (msg->lchan) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200129 struct e1inp_sign_link *sign_link =
130 msg->lchan->ts->trx->rsl_link;
131
132 msg->dst = sign_link;
Neels Hofmeyrf94dfd52016-03-14 16:13:24 +0100133 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800134 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200135 "Sending '%s' to MS.\n",
136 sign_link->trx->bts->nr,
137 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800138 gh->proto_discr & 0xf0,
139 gsm48_cc_msg_name(gh->msg_type));
140 else
141 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200142 "Sending 0x%02x to MS.\n",
143 sign_link->trx->bts->nr,
144 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800145 gh->proto_discr, gh->msg_type);
146 }
147
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100148 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800149}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100150
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800151int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
152{
153 struct gsm48_hdr *gh;
154 struct msgb *ss_notify;
155
156 ss_notify = gsm0480_create_notifySS(message);
157 if (!ss_notify)
158 return -1;
159
160 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
161 uint8_t *data = msgb_push(ss_notify, 1);
162 data[0] = ss_notify->len - 1;
163 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
164 gh->msg_type = GSM48_MT_CC_FACILITY;
165 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
166}
167
Harald Weltefe01ed72012-01-23 16:40:24 +0100168void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100169{
170 if (!conn->sec_operation)
171 return;
172
173 talloc_free(conn->sec_operation);
174 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800175 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100176}
177
Harald Weltefe01ed72012-01-23 16:40:24 +0100178void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100179{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100180 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
181 struct gsm_security_operation);
182}
183
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100184int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
185 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100186{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800187 struct gsm_network *net = conn->bts->network;
188 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100189 struct gsm_security_operation *op;
190 struct gsm_auth_tuple atuple;
191 int status = -1, rc;
192
193 /* Check if we _can_ enable encryption. Cases where we can't:
194 * - Encryption disabled in config
195 * - Channel already secured (nothing to do)
196 * - Subscriber equipment doesn't support configured encryption
197 */
198 if (!net->a5_encryption) {
199 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800200 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100201 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversbergee995352013-07-10 08:58:03 +0200202 status = GSM_SECURITY_ALREADY;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100203 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
204 net->a5_encryption)) {
205 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
206 status = GSM_SECURITY_NOAVAIL;
207 }
208
209 /* If not done yet, try to get info for this user */
210 if (status < 0) {
211 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
212 if (rc <= 0)
213 status = GSM_SECURITY_NOAVAIL;
214 }
215
216 /* Are we done yet ? */
217 if (status >= 0)
218 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800219 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100220 0;
221
222 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800223 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100224 return -EBUSY;
225
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800226 allocate_security_operation(conn);
227 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100228 op->cb = cb;
229 op->cb_data = cb_data;
230 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
231
232 /* FIXME: Should start a timer for completion ... */
233
234 /* Then do whatever is needed ... */
Neels Hofmeyrf0131672016-03-10 23:30:37 +0100235 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100236 /* Start authentication */
Harald Welte34ef4c52016-04-20 13:13:19 +0200237 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100238 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100239 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100240 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte34ef4c52016-04-20 13:13:19 +0200241 op->atuple.vec.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100242 }
243
244 return -EINVAL; /* not reached */
245}
246
Harald Welte59b04682009-06-10 05:40:52 +0800247static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
248 struct gsm_subscriber *subscriber)
249{
250 if (!subscriber)
251 return 0;
252
253 /*
254 * Do not send accept yet as more information should arrive. Some
255 * phones will not send us the information and we will have to check
256 * what we want to do with that.
257 */
258 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
259 return 0;
260
Jacob Erlbeckda8770b2014-12-03 09:28:24 +0100261 switch (subscriber->group->net->auth_policy) {
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200262 case GSM_AUTH_POLICY_CLOSED:
263 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200264 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200265 if (subscriber->authorized)
266 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200267 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200268 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800269 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200270 default:
271 return 0;
272 }
Harald Welte59b04682009-06-10 05:40:52 +0800273}
274
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100275static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Harald Welte59b04682009-06-10 05:40:52 +0800276{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100277 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800278 return;
279
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800280 /* No need to keep the connection up */
281 release_anchor(conn);
282
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200283 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100284 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100285 conn->loc_operation = NULL;
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100286 if (release)
287 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800288}
289
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100290static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800291{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800292 if (conn->loc_operation)
293 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100294 release_loc_updating_req(conn, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800295
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100296 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200297 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800298}
299
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200300static int finish_lu(struct gsm_subscriber_connection *conn)
301{
302 int rc = 0;
303 int avoid_tmsi = conn->bts->network->avoid_tmsi;
304
305 /* We're all good */
306 if (avoid_tmsi) {
307 conn->subscr->tmsi = GSM_RESERVED_TMSI;
308 db_sync_subscriber(conn->subscr);
309 } else {
310 db_subscriber_alloc_tmsi(conn->subscr);
311 }
312
313 rc = gsm0408_loc_upd_acc(conn);
314 if (conn->bts->network->send_mm_info) {
315 /* send MM INFO with network name */
316 rc = gsm48_tx_mm_info(conn);
317 }
318
319 /* call subscr_update after putting the loc_upd_acc
320 * in the transmit queue, since S_SUBSCR_ATTACHED might
321 * trigger further action like SMS delivery */
322 subscr_update(conn->subscr, conn->bts,
323 GSM_SUBSCRIBER_UPDATE_ATTACHED);
324
325 /*
326 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
327 * MS needs to respond with a TMSI REALLOCATION COMPLETE
328 * (even if the TMSI is the same).
329 */
330 if (avoid_tmsi)
331 release_loc_updating_req(conn, 1);
332
333 return rc;
334}
335
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100336static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
337 struct msgb *msg, void *data, void *param)
338{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800339 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100340 int rc = 0;
341
342 switch (event) {
343 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100344 release_loc_updating_req(conn, 1);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100345 break;
346
Andreas Eversberge36b4c72013-07-10 08:58:03 +0200347 case GSM_SECURITY_ALREADY:
348 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
349 "UPDATING after CM SERVICE REQUEST\n");
350 /* fall through */
351
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100352 case GSM_SECURITY_NOAVAIL:
353 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200354 rc = finish_lu(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100355 break;
356
357 default:
358 rc = -EINVAL;
359 };
360
361 return rc;
362}
363
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100364static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800365{
Holger Hans Peter Freyther89e3f8a2013-12-27 17:37:27 +0100366 if (!conn->loc_operation)
367 return 0;
368
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100369 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800370 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100371 conn->loc_operation->key_seq,
372 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800373 return 0;
374}
375
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800376void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800377{
Harald Welte03740842009-06-10 23:11:52 +0800378 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800379
380 /* avoid someone issuing a clear */
381 conn->in_release = 1;
382
Harald Welte59b04682009-06-10 05:40:52 +0800383 /*
384 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800385 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800386 */
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100387 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100388
389 /* We might need to cancel the paging response or such. */
390 if (conn->sec_operation && conn->sec_operation->cb) {
391 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
392 NULL, conn, conn->sec_operation->cb_data);
393 }
394
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800395 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800396 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800397
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100398 /*
399 * Free all transactions that are associated with the released
400 * connection. The transaction code will inform the CC or SMS
401 * facilities that will send the release indications. As part of
402 * the CC REL_IND the remote leg might be released and this will
403 * trigger the call to trans_free. This is something the llist
404 * macro can not handle and we will need to re-iterate the list.
405 *
406 * TODO: Move the trans_list into the subscriber connection and
407 * create a pending list for MT transactions. These exist before
408 * we have a subscriber connection.
409 */
410restart:
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800411 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100412 if (trans->conn == conn) {
Harald Weltec2189a62009-07-23 18:56:43 +0200413 trans_free(trans);
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100414 goto restart;
415 }
Harald Welte03740842009-06-10 23:11:52 +0800416 }
Harald Welte59b04682009-06-10 05:40:52 +0800417}
418
Harald Weltee8d16172010-12-22 23:17:50 +0100419void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
420{
421 struct gsm_trans *trans, *temp;
422
423 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
424
425 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100426 if (trans->protocol == protocol) {
427 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100428 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100429 }
Harald Weltee8d16172010-12-22 23:17:50 +0100430 }
431}
432
Harald Welte59b04682009-06-10 05:40:52 +0800433/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200434int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800435{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800436 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800437 struct msgb *msg;
438
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200439 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800440
441 msg = gsm48_create_loc_upd_rej(cause);
442 if (!msg) {
443 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
444 return -1;
445 }
Harald Welte59b04682009-06-10 05:40:52 +0800446
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800447 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800448
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100449 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100450 "LAC=%u BTS=%u\n", conn->subscr ?
451 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800452 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100453
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800454 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800455}
456
457/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200458static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800459{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800460 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +0100461 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte59b04682009-06-10 05:40:52 +0800462 struct gsm48_hdr *gh;
463 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200464 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800465
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800466 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800467
468 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
469 gh->proto_discr = GSM48_PDISC_MM;
470 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
471
472 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100473 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800474 bts->network->network_code, bts->location_area_code);
475
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200476 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
477 uint8_t mi[10];
478 int len;
479 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
480 mid = msgb_put(msg, len);
481 memcpy(mid, mi, len);
482 } else {
483 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
484 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
485 }
Harald Welte59b04682009-06-10 05:40:52 +0800486
487 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
488
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200489 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100490
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800491 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800492}
493
Harald Welte59b04682009-06-10 05:40:52 +0800494/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200495static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800496{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +0100497 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte59b04682009-06-10 05:40:52 +0800498 struct gsm48_hdr *gh;
499
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800500 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800501
502 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
503 gh->proto_discr = GSM48_PDISC_MM;
504 gh->msg_type = GSM48_MT_MM_ID_REQ;
505 gh->data[0] = id_type;
506
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800507 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800508}
509
Max927d1a42016-05-11 11:01:06 +0200510static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
511 const char *imsi)
512{
513 if (net->subscr_creation_mode != GSM_SUBSCR_DONT_CREATE)
514 return subscr_create_subscriber(net->subscr_group, imsi);
515
516 return NULL;
517}
Harald Welte59b04682009-06-10 05:40:52 +0800518
519/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800520static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800521{
522 struct gsm48_hdr *gh = msgb_l3(msg);
523 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200524 struct gsm_bts *bts = lchan->ts->trx->bts;
525 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200526 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200527 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800528
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200529 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte7e3e9332015-08-16 18:07:48 +0200530 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
531 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800532
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200533 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100534
Harald Welte59b04682009-06-10 05:40:52 +0800535 switch (mi_type) {
536 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200537 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100538 if (!conn->subscr) {
Jacob Erlbeckda8770b2014-12-03 09:28:24 +0100539 conn->subscr = subscr_get_by_imsi(net->subscr_group,
540 mi_string);
Max927d1a42016-05-11 11:01:06 +0200541 if (!conn->subscr)
542 conn->subscr = subscr_create(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200543 }
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100544 if (!conn->subscr && conn->loc_operation) {
545 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
546 release_loc_updating_req(conn, 1);
547 return 0;
548 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100549 if (conn->loc_operation)
550 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800551 break;
552 case GSM_MI_TYPE_IMEI:
553 case GSM_MI_TYPE_IMEISV:
554 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100555 if (conn->subscr) {
556 db_subscriber_assoc_imei(conn->subscr, mi_string);
557 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200558 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100559 if (conn->loc_operation)
560 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800561 break;
562 }
563
564 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100565 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800566}
567
568
569static void loc_upd_rej_cb(void *data)
570{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100571 struct gsm_subscriber_connection *conn = data;
572 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100573 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800574
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100575 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800576 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100577 release_loc_updating_req(conn, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800578}
579
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100580static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800581{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100582 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
583 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200584 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800585}
586
Harald Welte7e3e9332015-08-16 18:07:48 +0200587static const struct value_string lupd_names[] = {
588 { GSM48_LUPD_NORMAL, "NORMAL" },
589 { GSM48_LUPD_PERIODIC, "PERIODIC" },
590 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
591 { 0, NULL }
592};
Harald Welte59b04682009-06-10 05:40:52 +0800593
Harald Welte59b04682009-06-10 05:40:52 +0800594/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800595static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800596{
597 struct gsm48_hdr *gh = msgb_l3(msg);
598 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800599 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800600 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200601 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200602 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800603
604 lu = (struct gsm48_loc_upd_req *) gh->data;
605
606 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
607
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200608 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800609
Harald Welte7e3e9332015-08-16 18:07:48 +0200610 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
611 mi_string, get_value_string(lupd_names, lu->type));
Harald Welte59b04682009-06-10 05:40:52 +0800612
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200613 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100614
Harald Welte3edc5a92009-12-22 00:41:05 +0100615 switch (lu->type) {
616 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200617 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100618 break;
619 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200620 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100621 break;
622 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200623 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100624 break;
625 }
626
Harald Welte59b04682009-06-10 05:40:52 +0800627 /*
628 * Pseudo Spoof detection: Just drop a second/concurrent
629 * location updating request.
630 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100631 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200632 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100633 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800634 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800635 return 0;
636 }
637
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800638 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800639
Sylvain Munaut360fc582010-06-10 13:36:59 +0200640 conn->loc_operation->key_seq = lu->key_seq;
641
Harald Welte59b04682009-06-10 05:40:52 +0800642 switch (mi_type) {
643 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200644 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800645 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200646 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100647 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800648
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200649 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeckda8770b2014-12-03 09:28:24 +0100650 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Max927d1a42016-05-11 11:01:06 +0200651 if (!subscr)
652 subscr = subscr_create(bts->network, mi_string);
653
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +0100654 if (!subscr) {
655 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
656 release_loc_updating_req(conn, 0);
657 return 0;
658 }
Harald Welte59b04682009-06-10 05:40:52 +0800659 break;
660 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200661 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800662 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeckda8770b2014-12-03 09:28:24 +0100663 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200664 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800665 if (!subscr) {
666 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200667 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100668 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800669 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200670 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200671 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200672 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800673 break;
674 case GSM_MI_TYPE_IMEI:
675 case GSM_MI_TYPE_IMEISV:
676 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200677 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800678 break;
679 default:
Harald Welte79639662009-06-27 02:58:43 +0200680 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800681 break;
682 }
683
Harald Welteccd69362009-07-04 10:18:00 +0200684 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100685 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200686
Harald Welte03740842009-06-10 23:11:52 +0800687 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200688 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800689 /* FIXME: request id? close channel? */
690 return -EINVAL;
691 }
692
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100693 conn->subscr = subscr;
694 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800695
Harald Welteccd69362009-07-04 10:18:00 +0200696 /* check if we can let the subscriber into our network immediately
697 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100698 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800699}
700
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100701/* Turn int into semi-octet representation: 98 => 0x89 */
702static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800703{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100704 uint8_t ret;
705
706 ret = value / 10;
707 ret |= (value % 10) << 4;
708
709 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800710}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100711
Harald Welte03740842009-06-10 23:11:52 +0800712
Harald Welte59b04682009-06-10 05:40:52 +0800713/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800714int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800715{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +0100716 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Welte59b04682009-06-10 05:40:52 +0800717 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800718 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100719 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200720 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200721 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100722
Harald Welte03740842009-06-10 23:11:52 +0800723 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100724 struct tm* gmt_time;
725 struct tm* local_time;
726 int tzunits;
Jacob Erlbecke7339632014-01-07 15:05:16 +0100727 int dst = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800728
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800729 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800730
731 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
732 gh->proto_discr = GSM48_PDISC_MM;
733 gh->msg_type = GSM48_MT_MM_INFO;
734
735 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200736#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800737 name_len = strlen(net->name_long);
738 /* 10.5.3.5a */
739 ptr8 = msgb_put(msg, 3);
740 ptr8[0] = GSM48_IE_NAME_LONG;
741 ptr8[1] = name_len*2 +1;
742 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
743
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200744 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800745 for (i = 0; i < name_len; i++)
746 ptr16[i] = htons(net->name_long[i]);
747
748 /* FIXME: Use Cell Broadcast, not UCS-2, since
749 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200750#endif
751 name_len = (strlen(net->name_long)*7)/8;
752 name_pad = (8 - strlen(net->name_long)*7)%8;
753 if (name_pad > 0)
754 name_len++;
755 /* 10.5.3.5a */
756 ptr8 = msgb_put(msg, 3);
757 ptr8[0] = GSM48_IE_NAME_LONG;
758 ptr8[1] = name_len +1;
759 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
760
761 ptr8 = msgb_put(msg, name_len);
Harald Welte373cb772015-01-01 12:41:17 +0100762 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmannc8188202009-08-13 03:42:07 +0200763
Harald Welte59b04682009-06-10 05:40:52 +0800764 }
765
766 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200767#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800768 name_len = strlen(net->name_short);
769 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200770 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200771 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800772 ptr8[1] = name_len*2 + 1;
773 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
774
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200775 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800776 for (i = 0; i < name_len; i++)
777 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200778#endif
779 name_len = (strlen(net->name_short)*7)/8;
780 name_pad = (8 - strlen(net->name_short)*7)%8;
781 if (name_pad > 0)
782 name_len++;
783 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200784 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200785 ptr8[0] = GSM48_IE_NAME_SHORT;
786 ptr8[1] = name_len +1;
787 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
788
789 ptr8 = msgb_put(msg, name_len);
Harald Welte373cb772015-01-01 12:41:17 +0100790 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmannc8188202009-08-13 03:42:07 +0200791
Harald Welte59b04682009-06-10 05:40:52 +0800792 }
793
Harald Welte59b04682009-06-10 05:40:52 +0800794 /* Section 10.5.3.9 */
795 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100796 gmt_time = gmtime(&cur_t);
797
Harald Welte59b04682009-06-10 05:40:52 +0800798 ptr8 = msgb_put(msg, 8);
799 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100800 ptr8[1] = bcdify(gmt_time->tm_year % 100);
801 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
802 ptr8[3] = bcdify(gmt_time->tm_mday);
803 ptr8[4] = bcdify(gmt_time->tm_hour);
804 ptr8[5] = bcdify(gmt_time->tm_min);
805 ptr8[6] = bcdify(gmt_time->tm_sec);
806
Harald Welte5c3abf92012-07-08 16:48:11 +0200807 if (bts->tz.override) {
808 /* Convert tz.hr and tz.mn to units */
809 if (bts->tz.hr < 0) {
810 tzunits = ((bts->tz.hr/-1)*4);
811 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100812 ptr8[7] = bcdify(tzunits);
813 /* Set negative time */
814 ptr8[7] |= 0x08;
815 }
816 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200817 tzunits = bts->tz.hr*4;
818 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100819 ptr8[7] = bcdify(tzunits);
820 }
Jacob Erlbecke7339632014-01-07 15:05:16 +0100821 /* Convert DST value */
822 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
823 dst = bts->tz.dst;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100824 }
825 else {
826 /* Need to get GSM offset and convert into 15 min units */
827 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
828 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200829#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100830 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200831#else
832#warning find a portable way to obtain the timezone offset
833 tzunits = 0;
834#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100835 if (tzunits < 0) {
836 tzunits = tzunits/-1;
837 ptr8[7] = bcdify(tzunits);
838 /* Flip it to negative */
839 ptr8[7] |= 0x08;
840 }
841 else
842 ptr8[7] = bcdify(tzunits);
Jacob Erlbecke7339632014-01-07 15:05:16 +0100843
844 /* Does not support DST +2 */
845 if (local_time->tm_isdst)
846 dst = 1;
847 }
848
849 if (dst) {
850 ptr8 = msgb_put(msg, 3);
851 ptr8[0] = GSM48_IE_NET_DST;
852 ptr8[1] = 1;
853 ptr8[2] = dst;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100854 }
Harald Welte59b04682009-06-10 05:40:52 +0800855
Daniel Willmannc8188202009-08-13 03:42:07 +0200856 DEBUGP(DMM, "-> MM INFO\n");
857
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800858 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800859}
860
Harald Welte36970f62009-08-12 22:56:50 +0200861/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200862int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200863{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +0100864 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte36970f62009-08-12 22:56:50 +0200865 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200866 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200867
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200868 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200869
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800870 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200871 gh->proto_discr = GSM48_PDISC_MM;
872 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
873
Sylvain Munaut0789c592009-12-24 00:23:46 +0100874 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200875
Harald Welte36970f62009-08-12 22:56:50 +0200876 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200877 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200878 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200879
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800880 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200881}
882
883/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800884int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200885{
886 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800887 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200888}
889
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100890/*
891 * At the 30C3 phones miss their periodic update
892 * interval a lot and then remain unreachable. In case
893 * we still know the TMSI we can just attach it again.
894 */
895static void implit_attach(struct gsm_subscriber_connection *conn)
896{
897 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
898 return;
899
900 subscr_update(conn->subscr, conn->bts,
901 GSM_SUBSCRIBER_UPDATE_ATTACHED);
902}
903
904
Sylvain Munautbadc7112009-12-24 00:28:46 +0100905static int _gsm48_rx_mm_serv_req_sec_cb(
906 unsigned int hooknum, unsigned int event,
907 struct msgb *msg, void *data, void *param)
908{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800909 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100910 int rc = 0;
911
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200912 /* auth failed or succeeded, the timer was stopped */
913 conn->expire_timer_stopped = 1;
914
Sylvain Munautbadc7112009-12-24 00:28:46 +0100915 switch (event) {
916 case GSM_SECURITY_AUTH_FAILED:
917 /* Nothing to do */
918 break;
919
920 case GSM_SECURITY_NOAVAIL:
Andreas Eversbergee995352013-07-10 08:58:03 +0200921 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800922 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100923 implit_attach(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100924 break;
925
926 case GSM_SECURITY_SUCCEEDED:
927 /* nothing to do. CIPHER MODE COMMAND is
928 * implicit CM SERV ACK */
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100929 implit_attach(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100930 break;
931
932 default:
933 rc = -EINVAL;
934 };
935
936 return rc;
937}
938
Harald Welte59b04682009-06-10 05:40:52 +0800939/*
940 * Handle CM Service Requests
941 * a) Verify that the packet is long enough to contain the information
942 * we require otherwsie reject with INCORRECT_MESSAGE
943 * b) Try to parse the TMSI. If we do not have one reject
944 * c) Check that we know the subscriber with the TMSI otherwise reject
945 * with a HLR cause
946 * d) Set the subscriber on the gsm_lchan and accept
947 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800948static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800949{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200950 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200951 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800952
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800953 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800954 struct gsm_subscriber *subscr;
955 struct gsm48_hdr *gh = msgb_l3(msg);
956 struct gsm48_service_request *req =
957 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800958 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200959 uint8_t classmark2_len = gh->data[1];
960 uint8_t *classmark2 = gh->data+2;
961 uint8_t mi_len = *(classmark2 + classmark2_len);
962 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800963
964 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
965 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
966 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800967 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800968 GSM48_REJECT_INCORRECT_MESSAGE);
969 }
970
971 if (msg->data_len < req->mi_len + 6) {
972 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800973 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800974 GSM48_REJECT_INCORRECT_MESSAGE);
975 }
976
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200977 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800978 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200979
980 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welte7e3e9332015-08-16 18:07:48 +0200981 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
982 req->cm_service_type, gsm48_mi_type_name(mi_type),
983 mi_string);
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200984 subscr = subscr_get_by_imsi(bts->network->subscr_group,
985 mi_string);
986 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welte7e3e9332015-08-16 18:07:48 +0200987 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
988 req->cm_service_type, gsm48_mi_type_name(mi_type),
989 mi_string);
Holger Hans Peter Freyther1387df72015-07-13 20:33:08 +0200990 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
991 tmsi_from_string(mi_string));
992 } else {
993 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800994 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800995 GSM48_REJECT_INCORRECT_MESSAGE);
996 }
997
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200998 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100999
Harald Welte583ceaf2009-08-10 10:12:45 +02001000 if (is_siemens_bts(bts))
1001 send_siemens_mrpci(msg->lchan, classmark2-1);
1002
Harald Welte59b04682009-06-10 05:40:52 +08001003
1004 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
1005 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001006 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freythercfc95292015-07-13 14:28:29 +02001007 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte59b04682009-06-10 05:40:52 +08001008
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001009 if (!conn->subscr)
1010 conn->subscr = subscr;
1011 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +01001012 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1013 else {
Harald Welte59b04682009-06-10 05:40:52 +08001014 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1015 subscr_put(subscr);
1016 }
1017
Harald Weltef6845a72009-07-05 14:08:13 +02001018 subscr->equipment.classmark2_len = classmark2_len;
1019 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1020 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001021
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +02001022 /* we will send a MM message soon */
1023 conn->expire_timer_stopped = 1;
1024
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001025 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +01001026 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001027}
1028
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001029static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001030{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001031 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001032 struct gsm48_hdr *gh = msgb_l3(msg);
1033 struct gsm48_imsi_detach_ind *idi =
1034 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001035 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001036 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001037 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001038
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001039 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte7e3e9332015-08-16 18:07:48 +02001040 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1041 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001042
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001043 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +01001044
Harald Welte59b04682009-06-10 05:40:52 +08001045 switch (mi_type) {
1046 case GSM_MI_TYPE_TMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +01001047 DEBUGPC(DMM, "\n");
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01001048 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001049 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001050 break;
1051 case GSM_MI_TYPE_IMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +01001052 DEBUGPC(DMM, "\n");
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01001053 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1054 mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001055 break;
1056 case GSM_MI_TYPE_IMEI:
1057 case GSM_MI_TYPE_IMEISV:
1058 /* no sim card... FIXME: what to do ? */
Daniel Willmann2be30fe2012-12-28 19:29:44 +01001059 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001060 break;
1061 default:
Daniel Willmann2be30fe2012-12-28 19:29:44 +01001062 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001063 break;
1064 }
1065
1066 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001067 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +08001068 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +01001069 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +02001070
1071 subscr->equipment.classmark1 = idi->classmark1;
1072 db_sync_equipment(&subscr->equipment);
1073
Harald Welte59b04682009-06-10 05:40:52 +08001074 subscr_put(subscr);
1075 } else
1076 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1077
Harald Welte43250dd2009-12-20 09:58:40 +01001078 /* FIXME: iterate over all transactions and release them,
1079 * imagine an IMSI DETACH happening during an active call! */
1080
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001081 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001082 return 0;
1083}
1084
1085static int gsm48_rx_mm_status(struct msgb *msg)
1086{
1087 struct gsm48_hdr *gh = msgb_l3(msg);
1088
1089 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1090
1091 return 0;
1092}
1093
Sylvain Munaut28c47162009-12-24 00:27:26 +01001094/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001095static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001096{
1097 struct gsm48_hdr *gh = msgb_l3(msg);
1098 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001099 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001100
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001101 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001102 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +01001103
1104 /* Safety check */
1105 if (!conn->sec_operation) {
1106 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1107 return -EIO;
1108 }
1109
1110 /* Validate SRES */
Harald Welte34ef4c52016-04-20 13:13:19 +02001111 if (memcmp(conn->sec_operation->atuple.vec.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001112 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001113 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001114
1115 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte34ef4c52016-04-20 13:13:19 +02001116 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001117
Sylvain Munaut28c47162009-12-24 00:27:26 +01001118 if (cb)
1119 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001120 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001121
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001122 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001123 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001124 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001125 }
1126
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001127 DEBUGPC(DMM, "OK\n");
1128
Sylvain Munaut28c47162009-12-24 00:27:26 +01001129 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001130 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte34ef4c52016-04-20 13:13:19 +02001131 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001132}
1133
Harald Welte59b04682009-06-10 05:40:52 +08001134/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001135static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001136{
1137 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001138 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001139
Neels Hofmeyrf94dfd52016-03-14 16:13:24 +01001140 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte59b04682009-06-10 05:40:52 +08001141 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001142 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001143 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001144 break;
1145 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001146 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001147 break;
1148 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001149 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001150 break;
1151 case GSM48_MT_MM_STATUS:
1152 rc = gsm48_rx_mm_status(msg);
1153 break;
1154 case GSM48_MT_MM_TMSI_REALL_COMPL:
1155 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001156 conn->subscr ?
1157 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001158 "unknown subscriber");
Holger Hans Peter Freytherd3af8022015-01-27 10:27:53 +01001159 release_loc_updating_req(conn, 1);
Harald Welte59b04682009-06-10 05:40:52 +08001160 break;
1161 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001162 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001163 break;
1164 case GSM48_MT_MM_CM_REEST_REQ:
1165 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1166 break;
1167 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001168 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001169 break;
1170 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001171 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001172 gh->msg_type);
1173 break;
1174 }
1175
1176 return rc;
1177}
1178
1179/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001180static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001181{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001182 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001183 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001184 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001185 uint8_t *classmark2_lv = gh->data + 1;
1186 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001187 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001188 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001189 int rc = 0;
1190
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001191 resp = (struct gsm48_pag_resp *) &gh->data[0];
1192 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1193 mi_string, &mi_type);
Harald Welte7e3e9332015-08-16 18:07:48 +02001194 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1195 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001196
Harald Welte59b04682009-06-10 05:40:52 +08001197 switch (mi_type) {
1198 case GSM_MI_TYPE_TMSI:
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01001199 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001200 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001201 break;
1202 case GSM_MI_TYPE_IMSI:
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01001203 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1204 mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001205 break;
1206 }
1207
1208 if (!subscr) {
1209 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1210 /* FIXME: request id? close channel? */
1211 return -EINVAL;
1212 }
Holger Hans Peter Freyther66f55e62014-12-28 15:00:36 +01001213 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001214 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001215 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001216
Harald Weltef6845a72009-07-05 14:08:13 +02001217 subscr->equipment.classmark2_len = *classmark2_lv;
1218 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1219 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001220
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +02001221 /* We received a paging */
1222 conn->expire_timer_stopped = 1;
1223
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001224 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001225 return rc;
1226}
1227
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001228static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001229{
1230 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001231 uint8_t apdu_id_flags;
1232 uint8_t apdu_len;
1233 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001234
1235 apdu_id_flags = gh->data[0];
1236 apdu_len = gh->data[1];
1237 apdu_data = gh->data+2;
1238
Harald Welted4f21662012-07-16 13:22:19 +02001239 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001240 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001241
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001242 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001243}
1244
Harald Welte59b04682009-06-10 05:40:52 +08001245/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001246static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001247{
1248 struct gsm48_hdr *gh = msgb_l3(msg);
1249 int rc = 0;
1250
1251 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001252 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001253 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001254 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001255 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001256 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001257 break;
Harald Welte59b04682009-06-10 05:40:52 +08001258 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001259 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001260 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001261 break;
1262 }
1263
1264 return rc;
1265}
1266
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001267int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1268 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001269{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01001270 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)b5103922009-08-15 23:32:44 +02001271 struct gsm48_hdr *gh;
1272
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001273 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001274
1275 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1276 apdu_id, apdu_len);
1277
1278 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1279 gh->proto_discr = GSM48_PDISC_RR;
1280 gh->msg_type = GSM48_MT_RR_APP_INFO;
1281 gh->data[0] = apdu_id;
1282 gh->data[1] = apdu_len;
1283 memcpy(gh->data+2, apdu, apdu_len);
1284
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001285 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001286}
1287
Harald Welte59b04682009-06-10 05:40:52 +08001288/* Call Control */
1289
1290/* The entire call control code is written in accordance with Figure 7.10c
1291 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1292 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1293 * it for voice */
1294
Harald Welte03740842009-06-10 23:11:52 +08001295static void new_cc_state(struct gsm_trans *trans, int state)
1296{
1297 if (state > 31 || state < 0)
1298 return;
1299
1300 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001301 gsm48_cc_state_name(trans->cc.state),
1302 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001303
Harald Weltec2189a62009-07-23 18:56:43 +02001304 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001305}
1306
1307static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001308{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01001309 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte59b04682009-06-10 05:40:52 +08001310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001311 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001312
Harald Welte59b04682009-06-10 05:40:52 +08001313 gh->msg_type = GSM48_MT_CC_STATUS;
1314
1315 cause = msgb_put(msg, 3);
1316 cause[0] = 2;
1317 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1318 cause[2] = 0x80 | 30; /* response to status inquiry */
1319
1320 call_state = msgb_put(msg, 1);
1321 call_state[0] = 0xc0 | 0x00;
1322
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001323 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001324}
1325
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001326static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001327 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001328{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01001329 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte59b04682009-06-10 05:40:52 +08001330 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1331
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001332 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001333
1334 gh->proto_discr = pdisc;
1335 gh->msg_type = msg_type;
1336
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001337 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001338}
1339
Harald Welte03740842009-06-10 23:11:52 +08001340static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1341{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001342 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001343 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001344 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001345 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001346 }
1347}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001348
Harald Welte03740842009-06-10 23:11:52 +08001349static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1350 int msg_type, struct gsm_mncc *mncc)
1351{
1352 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001353 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001354
1355 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001356 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001357 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001358 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001359 trans->conn->lchan->ts->trx->bts->nr,
1360 trans->conn->lchan->ts->trx->nr,
1361 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001362 (trans->subscr)?(trans->subscr->extension):"-",
1363 get_mncc_name(msg_type));
1364 else
1365 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1366 "Sending '%s' to MNCC.\n",
1367 (trans->subscr)?(trans->subscr->extension):"-",
1368 get_mncc_name(msg_type));
1369 else
1370 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1371 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1372
1373 mncc->msg_type = msg_type;
1374
Harald Welte9cfc9352009-06-26 19:39:35 +02001375 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001376 if (!msg)
1377 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001378
1379 data = msgb_put(msg, sizeof(struct gsm_mncc));
1380 memcpy(data, mncc, sizeof(struct gsm_mncc));
1381
Harald Weltebe0cb682010-12-22 23:43:29 +01001382 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001383
1384 return 0;
1385}
1386
1387int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001388 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001389{
1390 struct gsm_mncc rel;
1391
Harald Weltecb0595f2009-06-12 01:54:08 +08001392 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001393 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001394 mncc_set_cause(&rel, location, value);
Andreas Eversberg59e79ee2013-01-25 08:38:29 +01001395 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1396 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001397 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1398}
1399
Harald Weltec2189a62009-07-23 18:56:43 +02001400/* Call Control Specific transaction release.
1401 * gets called by trans_free, DO NOT CALL YOURSELF! */
1402void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001403{
Harald Welte03740842009-06-10 23:11:52 +08001404 gsm48_stop_cc_timer(trans);
1405
1406 /* send release to L4, if callref still exists */
1407 if (trans->callref) {
1408 /* Ressource unavailable */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01001409 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001410 GSM48_CAUSE_LOC_PRN_S_LU,
1411 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001412 }
Harald Weltec2189a62009-07-23 18:56:43 +02001413 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001414 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001415 if (trans->conn)
1416 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001417}
1418
1419static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001420
Harald Welte59b04682009-06-10 05:40:52 +08001421/* call-back from paging the B-end of the connection */
1422static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001423 struct msgb *msg, void *_conn, void *_transt)
Harald Welte59b04682009-06-10 05:40:52 +08001424{
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001425 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001426 struct gsm_trans *transt = _transt;
Harald Welte1ff81b52009-06-26 20:17:06 +02001427
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001428 OSMO_ASSERT(!transt->conn);
Harald Welte59b04682009-06-10 05:40:52 +08001429
Harald Welte03740842009-06-10 23:11:52 +08001430 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001431 switch (event) {
1432 case GSM_PAGING_SUCCEEDED:
1433 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov89a4fa72015-05-26 13:36:58 +03001434 OSMO_ASSERT(conn);
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001435 /* Assign lchan */
1436 transt->conn = conn;
1437 /* send SETUP request to called party */
1438 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1439 break;
1440 case GSM_PAGING_EXPIRED:
1441 case GSM_PAGING_BUSY:
1442 DEBUGP(DCC, "Paging subscr %s expired!\n",
1443 transt->subscr->extension);
1444 /* Temporarily out of order */
1445 mncc_release_ind(transt->net, transt,
1446 transt->callref,
1447 GSM48_CAUSE_LOC_PRN_S_LU,
1448 GSM48_CC_CAUSE_DEST_OOO);
1449 transt->callref = 0;
1450 transt->paging_request = NULL;
1451 trans_free(transt);
1452 break;
1453 default:
1454 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1455 break;
Harald Welte03740842009-06-10 23:11:52 +08001456 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001457
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02001458 transt->paging_request = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001459 return 0;
1460}
1461
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001462static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001463
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001464/* handle audio path for handover */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001465static int switch_for_handover(struct gsm_lchan *old_lchan,
1466 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001467{
1468 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001469
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02001470 /* Ask the new socket to send to the already known port. */
1471 if (new_lchan->conn->mncc_rtp_bridge) {
1472 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1473 rsl_ipacc_mdcx(new_lchan,
1474 old_lchan->abis_ip.connect_ip,
1475 old_lchan->abis_ip.connect_port, 0);
1476 return 0;
1477 }
1478
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001479 if (ipacc_rtp_direct) {
1480 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1481 return 0;
1482 }
1483
1484 /* RTP Proxy mode */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001485 new_rs = new_lchan->abis_ip.rtp_socket;
1486 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001487
1488 if (!new_rs) {
1489 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1490 return -EIO;
1491 }
1492
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001493 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001494
1495 if (!old_rs) {
1496 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1497 return -EIO;
1498 }
1499
1500 /* copy rx_action and reference to other sock */
1501 new_rs->rx_action = old_rs->rx_action;
1502 new_rs->tx_action = old_rs->tx_action;
1503 new_rs->transmit = old_rs->transmit;
1504
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001505 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001506 case RTP_PROXY:
1507 other_rs = old_rs->proxy.other_sock;
1508 rtp_socket_proxy(new_rs, other_rs);
1509 /* delete reference to other end socket to prevent
1510 * rtp_socket_free() from removing the inverse reference */
1511 old_rs->proxy.other_sock = NULL;
1512 break;
1513 case RTP_RECV_UPSTREAM:
1514 new_rs->receive = old_rs->receive;
1515 break;
1516 case RTP_NONE:
1517 break;
1518 }
1519
1520 return 0;
1521}
1522
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02001523static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1524{
1525 struct gsm_lchan *old_lchan;
1526 old_lchan = bsc_handover_pending(lchan);
1527 if (old_lchan)
1528 switch_for_handover(old_lchan, lchan);
1529}
1530
Harald Welte3c062072009-07-28 18:25:29 +02001531/* some other part of the code sends us a signal */
1532static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1533 void *handler_data, void *signal_data)
1534{
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02001535 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001536 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001537 struct gsm_network *net;
1538 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001539
1540 if (subsys != SS_ABISIP)
1541 return 0;
1542
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02001543 /* RTP bridge handling */
1544 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1545 return tch_rtp_signal(lchan, signal);
1546
Harald Welte3c062072009-07-28 18:25:29 +02001547 /* in case we use direct BTS-to-BTS RTP */
1548 if (ipacc_rtp_direct)
1549 return 0;
1550
Harald Welte3c062072009-07-28 18:25:29 +02001551 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001552 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001553 /* in case we don't use direct BTS-to-BTS RTP */
1554 /* the BTS has successfully bound a TCH to a local ip/port,
1555 * which means we can connect our UDP socket to it */
1556 if (lchan->abis_ip.rtp_socket) {
1557 rtp_socket_free(lchan->abis_ip.rtp_socket);
1558 lchan->abis_ip.rtp_socket = NULL;
1559 }
1560
1561 lchan->abis_ip.rtp_socket = rtp_socket_create();
1562 if (!lchan->abis_ip.rtp_socket)
1563 return -EIO;
1564
1565 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1566 lchan->abis_ip.bound_ip,
1567 lchan->abis_ip.bound_port);
1568 if (rc < 0)
1569 return -EIO;
1570
Harald Welte3971ad52009-12-19 22:23:05 +01001571 /* check if any transactions on this lchan still have
1572 * a tch_recv_mncc request pending */
1573 net = lchan->ts->trx->bts->network;
1574 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001575 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001576 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1577 tch_recv_mncc(net, trans->callref, 1);
1578 }
1579 }
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001580
1581 /*
1582 * TODO: this appears to be too early? Why not until after
1583 * the handover detect or the handover complete?
1584 *
1585 * Do we have a handover pending for this new lchan? In that
1586 * case re-route the audio from the old channel to the new one.
1587 */
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02001588 maybe_switch_for_handover(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001589 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001590 case S_ABISIP_DLCX_IND:
1591 /* the BTS tells us a RTP stream has been disconnected */
1592 if (lchan->abis_ip.rtp_socket) {
1593 rtp_socket_free(lchan->abis_ip.rtp_socket);
1594 lchan->abis_ip.rtp_socket = NULL;
1595 }
1596
1597 break;
Harald Welte3c062072009-07-28 18:25:29 +02001598 }
1599
1600 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001601}
1602
Harald Welte59b04682009-06-10 05:40:52 +08001603/* map two ipaccess RTP streams onto each other */
1604static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1605{
1606 struct gsm_bts *bts = lchan->ts->trx->bts;
1607 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max3a816e82016-04-18 23:11:18 +02001608 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max30aa2322016-04-25 15:22:00 +02001609 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte3c062072009-07-28 18:25:29 +02001610 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001611
Max3a816e82016-04-18 23:11:18 +02001612 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1613 "(bts=%u,trx=%u,ts=%u,%s)\n",
1614 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1615 get_value_string(gsm_chan_t_names, lt),
1616 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1617 get_value_string(gsm_chan_t_names, rt));
Harald Welte59b04682009-06-10 05:40:52 +08001618
1619 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001620 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001621 return -EINVAL;
1622 }
Harald Welte3971ad52009-12-19 22:23:05 +01001623
Max30aa2322016-04-25 15:22:00 +02001624 if (lt != rt) {
1625 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1626 " channel types: local = %s, remote = %s\n",
1627 get_value_string(gsm_chan_t_names, lt),
1628 get_value_string(gsm_chan_t_names, rt));
1629 return -EBADSLT;
1630 }
1631
1632 if (lm != rm) {
1633 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1634 " channel modes: local = %s, remote = %s\n",
1635 get_value_string(gsm48_chan_mode_names, lm),
1636 get_value_string(gsm48_chan_mode_names, rm));
1637 return -EMEDIUMTYPE;
1638 }
1639
Harald Welte3971ad52009-12-19 22:23:05 +01001640 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001641 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001642 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001643 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001644 if (!ipacc_rtp_direct) {
Andreas Eversbergac60f202014-01-22 08:57:18 +01001645 if (!lchan->abis_ip.rtp_socket) {
1646 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1647 "lchan\n");
1648 return -EIO;
1649 }
1650 if (!remote_lchan->abis_ip.rtp_socket) {
1651 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1652 "remote_lchan\n");
1653 return -EIO;
1654 }
1655
Harald Welte3c062072009-07-28 18:25:29 +02001656 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001657 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001658 if (rc < 0)
1659 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001660 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001661 if (rc < 0)
1662 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001663 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301664 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1665 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001666 } else {
1667 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301668 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1669 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301670 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001671 if (rc < 0)
1672 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301673 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1674 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301675 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001676 }
Harald Welte59b04682009-06-10 05:40:52 +08001677 break;
1678 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001679 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001680 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001681 trau_mux_map_lchan(lchan, remote_lchan);
1682 break;
1683 default:
Harald Welte8099a712011-08-09 21:53:20 +02001684 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001685 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001686 }
1687
1688 return 0;
1689}
1690
Harald Welte03740842009-06-10 23:11:52 +08001691/* bridge channels of two transactions */
Harald Welte71a3c392015-12-03 14:59:04 +01001692static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte59b04682009-06-10 05:40:52 +08001693{
Harald Welte71a3c392015-12-03 14:59:04 +01001694 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1695 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001696
Harald Welte03740842009-06-10 23:11:52 +08001697 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001698 return -EIO;
1699
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001700 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001701 return -EIO;
1702
Holger Hans Peter Freyther0baeca82013-12-29 20:24:37 +01001703 /* Which subscriber do we want to track trans1 or trans2? */
1704 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1705
Harald Welte03740842009-06-10 23:11:52 +08001706 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001707 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001708}
1709
Harald Welte3971ad52009-12-19 22:23:05 +01001710/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001711static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001712{
1713 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001714 struct gsm_lchan *lchan;
1715 struct gsm_bts *bts;
1716 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001717
Harald Welte03740842009-06-10 23:11:52 +08001718 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001719 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001720 if (!trans)
1721 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001722 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001723 return 0;
Holger Hans Peter Freyther0baeca82013-12-29 20:24:37 +01001724
1725 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001726 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001727 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001728
Andreas Eversberg02063672013-12-05 14:37:11 +01001729 /* store receive state */
1730 trans->tch_recv = enable;
1731
Harald Welte3971ad52009-12-19 22:23:05 +01001732 switch (bts->type) {
1733 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001734 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001735 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001736 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001737 return -EINVAL;
1738 }
Andreas Eversberg02063672013-12-05 14:37:11 +01001739 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1740 * will not be connected to our RTP proxy and the socket will
1741 * not be assigned to the application interface. This method
1742 * will be called again, once the audio socket is created and
1743 * connected. */
Harald Welte3971ad52009-12-19 22:23:05 +01001744 if (!lchan->abis_ip.rtp_socket) {
Harald Welte3971ad52009-12-19 22:23:05 +01001745 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1746 return 0;
1747 }
1748 if (enable) {
1749 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001750 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001751 if (rc < 0)
1752 return rc;
1753 /* assign socket to application interface */
1754 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1755 net, callref);
1756 } else
1757 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1758 net, 0);
1759 break;
1760 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001761 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001762 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversberg02063672013-12-05 14:37:11 +01001763 /* In case we don't have a TCH with correct mode, the TRAU muxer
1764 * will not be asigned to the application interface. This is
1765 * performed by switch_trau_mux() after successful handover or
1766 * assignment. */
1767 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1768 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1769 return 0;
1770 }
Harald Welte3971ad52009-12-19 22:23:05 +01001771 if (enable)
1772 return trau_recv_lchan(lchan, callref);
1773 return trau_mux_unmap(NULL, callref);
1774 break;
1775 default:
Harald Welte8099a712011-08-09 21:53:20 +02001776 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001777 return -EINVAL;
1778 }
1779
1780 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001781}
1782
Harald Welte03740842009-06-10 23:11:52 +08001783static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1784{
1785 DEBUGP(DCC, "-> STATUS ENQ\n");
1786 return gsm48_cc_tx_status(trans, msg);
1787}
1788
1789static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1790static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1791
1792static void gsm48_cc_timeout(void *arg)
1793{
1794 struct gsm_trans *trans = arg;
1795 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001796 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1797 int mo_location = GSM48_CAUSE_LOC_USER;
1798 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1799 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001800 struct gsm_mncc mo_rel, l4_rel;
1801
1802 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1803 mo_rel.callref = trans->callref;
1804 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1805 l4_rel.callref = trans->callref;
1806
Harald Weltec2189a62009-07-23 18:56:43 +02001807 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001808 case 0x303:
1809 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001810 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001811 break;
1812 case 0x310:
1813 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001814 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001815 break;
1816 case 0x313:
1817 disconnect = 1;
1818 /* unknown, did not find it in the specs */
1819 break;
1820 case 0x301:
1821 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001822 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001823 break;
1824 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001825 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001826 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001827 gsm48_cc_tx_release(trans, &trans->cc.msg);
1828 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001829 break; /* stay in release state */
1830 }
Harald Weltec2189a62009-07-23 18:56:43 +02001831 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001832 return;
1833// release = 1;
1834// l4_cause = 14;
1835// break;
1836 case 0x306:
1837 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001838 mo_cause = trans->cc.msg.cause.value;
1839 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001840 break;
1841 case 0x323:
1842 disconnect = 1;
1843 break;
1844 default:
1845 release = 1;
1846 }
1847
1848 if (release && trans->callref) {
1849 /* process release towards layer 4 */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01001850 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001851 l4_location, l4_cause);
1852 trans->callref = 0;
1853 }
1854
1855 if (disconnect && trans->callref) {
1856 /* process disconnect towards layer 4 */
1857 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01001858 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001859 }
1860
1861 /* process disconnect towards mobile station */
1862 if (disconnect || release) {
1863 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001864 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1865 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1866 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001867 mo_rel.cause.diag_len = 3;
1868
1869 if (disconnect)
1870 gsm48_cc_tx_disconnect(trans, &mo_rel);
1871 if (release)
1872 gsm48_cc_tx_release(trans, &mo_rel);
1873 }
1874
1875}
1876
Max30aa2322016-04-25 15:22:00 +02001877/* disconnect both calls from the bridge */
1878static inline void disconnect_bridge(struct gsm_network *net,
1879 struct gsm_mncc_bridge *bridge, int err)
1880{
1881 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1882 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1883 struct gsm_mncc mx_rel;
1884 if (!trans0 || !trans1)
1885 return;
1886
1887 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1888 trans0->callref, trans1->callref, strerror(err));
1889
1890 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1891 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1892 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1893
1894 mx_rel.callref = trans0->callref;
1895 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1896
1897 mx_rel.callref = trans1->callref;
1898 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1899}
1900
Harald Welte03740842009-06-10 23:11:52 +08001901static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1902 int sec, int micro)
1903{
1904 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001905 trans->cc.timer.cb = gsm48_cc_timeout;
1906 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001907 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001908 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001909}
1910
1911static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1912{
1913 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrf94dfd52016-03-14 16:13:24 +01001914 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte03740842009-06-10 23:11:52 +08001915 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1916 struct tlv_parsed tp;
1917 struct gsm_mncc setup;
1918
1919 memset(&setup, 0, sizeof(struct gsm_mncc));
1920 setup.callref = trans->callref;
Andreas Eversberg65ff9862014-01-16 16:04:12 +01001921 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001922 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001923 /* emergency setup is identified by msg_type */
1924 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1925 setup.emergency = 1;
1926
1927 /* use subscriber as calling party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02001928 setup.fields |= MNCC_F_CALLING;
1929 strncpy(setup.calling.number, trans->subscr->extension,
1930 sizeof(setup.calling.number)-1);
1931 strncpy(setup.imsi, trans->subscr->imsi,
1932 sizeof(setup.imsi)-1);
1933
Harald Welte03740842009-06-10 23:11:52 +08001934 /* bearer capability */
1935 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1936 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001937 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001938 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberg976493b2013-12-07 18:32:28 +01001939 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001940 }
1941 /* facility */
1942 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1943 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001944 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001945 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1946 }
1947 /* called party bcd number */
1948 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1949 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001950 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001951 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1952 }
1953 /* user-user */
1954 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1955 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001956 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001957 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1958 }
1959 /* ss-version */
1960 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1961 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001962 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001963 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1964 }
1965 /* CLIR suppression */
1966 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1967 setup.clir.sup = 1;
1968 /* CLIR invocation */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1970 setup.clir.inv = 1;
1971 /* cc cap */
1972 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1973 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001974 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001975 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1976 }
1977
Harald Welte03740842009-06-10 23:11:52 +08001978 new_cc_state(trans, GSM_CSTATE_INITIATED);
1979
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001980 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1981 subscr_name(trans->subscr), trans->subscr->extension,
1982 setup.called.number);
1983
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01001984 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001985
Harald Welte03740842009-06-10 23:11:52 +08001986 /* indicate setup to MNCC */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01001987 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001988
Harald Welteca745e22009-07-29 12:10:35 +02001989 /* MNCC code will modify the channel asynchronously, we should
1990 * ipaccess-bind only after the modification has been made to the
1991 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001992 return 0;
1993}
1994
1995static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001996{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01001997 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte59b04682009-06-10 05:40:52 +08001998 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001999 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02002000 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002001
2002 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2003
Harald Welte03740842009-06-10 23:11:52 +08002004 /* transaction id must not be assigned */
2005 if (trans->transaction_id != 0xff) { /* unasssigned */
2006 DEBUGP(DCC, "TX Setup with assigned transaction. "
2007 "This is not allowed!\n");
2008 /* Temporarily out of order */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002009 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002010 GSM48_CAUSE_LOC_PRN_S_LU,
2011 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002012 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002013 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002014 return rc;
2015 }
2016
2017 /* Get free transaction_id */
Jacob Erlbecke2d80142014-12-02 14:22:53 +01002018 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2019 GSM48_PDISC_CC, 0);
Harald Welte165fc332009-07-23 21:36:44 +02002020 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08002021 /* no free transaction ID */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002022 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002023 GSM48_CAUSE_LOC_PRN_S_LU,
2024 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002025 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002026 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002027 return rc;
2028 }
Harald Welte165fc332009-07-23 21:36:44 +02002029 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002030
Harald Welte59b04682009-06-10 05:40:52 +08002031 gh->msg_type = GSM48_MT_CC_SETUP;
2032
Harald Welte03740842009-06-10 23:11:52 +08002033 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08002034
Harald Welte03740842009-06-10 23:11:52 +08002035 /* bearer capability */
2036 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01002037 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002038 /* facility */
2039 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002040 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08002041 /* progress */
2042 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002043 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08002044 /* calling party BCD number */
2045 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01002046 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08002047 /* called party BCD number */
2048 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002049 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08002050 /* user-user */
2051 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002052 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002053 /* redirecting party BCD number */
2054 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01002055 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08002056 /* signal */
2057 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01002058 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08002059
2060 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08002061
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002062 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01002063
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002064 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08002065}
2066
Harald Welte03740842009-06-10 23:11:52 +08002067static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2068{
2069 struct gsm48_hdr *gh = msgb_l3(msg);
2070 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2071 struct tlv_parsed tp;
2072 struct gsm_mncc call_conf;
2073
2074 gsm48_stop_cc_timer(trans);
2075 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2076
2077 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2078 call_conf.callref = trans->callref;
Andreas Eversberg65ff9862014-01-16 16:04:12 +01002079 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01002080 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002081#if 0
2082 /* repeat */
2083 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2084 call_conf.repeat = 1;
2085 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2086 call_conf.repeat = 2;
2087#endif
2088 /* bearer capability */
2089 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2090 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002091 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002092 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberg976493b2013-12-07 18:32:28 +01002093 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002094 }
2095 /* cause */
2096 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2097 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002098 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08002099 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2100 }
2101 /* cc cap */
2102 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2103 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002104 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08002105 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2106 }
2107
Andreas Eversbergbbbc7b32013-09-19 09:27:06 +02002108 /* IMSI of called subscriber */
2109 strncpy(call_conf.imsi, trans->subscr->imsi,
2110 sizeof(call_conf.imsi)-1);
2111
Harald Welte03740842009-06-10 23:11:52 +08002112 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2113
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002114 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte0abe5a62009-07-23 19:06:52 +02002115 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08002116}
2117
2118static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2119{
2120 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002121 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte03740842009-06-10 23:11:52 +08002122 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2123
Harald Welte03740842009-06-10 23:11:52 +08002124 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2125
2126 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2127
2128 /* bearer capability */
2129 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01002130 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002131 /* facility */
2132 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002133 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08002134 /* progress */
2135 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002136 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08002137
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002138 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002139}
2140
2141static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2142{
2143 struct gsm48_hdr *gh = msgb_l3(msg);
2144 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2145 struct tlv_parsed tp;
2146 struct gsm_mncc alerting;
2147
2148 gsm48_stop_cc_timer(trans);
2149 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2150
2151 memset(&alerting, 0, sizeof(struct gsm_mncc));
2152 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002153 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002154 /* facility */
2155 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2156 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002157 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08002158 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2159 }
2160
2161 /* progress */
2162 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2163 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01002164 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08002165 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2166 }
2167 /* ss-version */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2169 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002170 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002171 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2172 }
2173
2174 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2175
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002176 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte0abe5a62009-07-23 19:06:52 +02002177 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002178}
2179
2180static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2181{
2182 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002183 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte03740842009-06-10 23:11:52 +08002184 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2185
Harald Welte03740842009-06-10 23:11:52 +08002186 gh->msg_type = GSM48_MT_CC_ALERTING;
2187
2188 /* facility */
2189 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002190 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002191 /* progress */
2192 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002193 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002194 /* user-user */
2195 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002196 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002197
2198 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2199
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002200 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002201}
2202
2203static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2204{
2205 struct gsm_mncc *progress = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte03740842009-06-10 23:11:52 +08002207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2208
Harald Welte03740842009-06-10 23:11:52 +08002209 gh->msg_type = GSM48_MT_CC_PROGRESS;
2210
2211 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002212 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002213 /* user-user */
2214 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002215 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002216
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002217 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002218}
2219
2220static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2221{
2222 struct gsm_mncc *connect = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002223 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte03740842009-06-10 23:11:52 +08002224 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2225
Harald Welte03740842009-06-10 23:11:52 +08002226 gh->msg_type = GSM48_MT_CC_CONNECT;
2227
2228 gsm48_stop_cc_timer(trans);
2229 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2230
2231 /* facility */
2232 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002233 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002234 /* progress */
2235 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002236 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002237 /* connected number */
2238 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002239 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002240 /* user-user */
2241 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002242 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002243
2244 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2245
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002246 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2250{
2251 struct gsm48_hdr *gh = msgb_l3(msg);
2252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2253 struct tlv_parsed tp;
2254 struct gsm_mncc connect;
2255
2256 gsm48_stop_cc_timer(trans);
2257
2258 memset(&connect, 0, sizeof(struct gsm_mncc));
2259 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002260 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002261 /* use subscriber as connected party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02002262 connect.fields |= MNCC_F_CONNECTED;
2263 strncpy(connect.connected.number, trans->subscr->extension,
2264 sizeof(connect.connected.number)-1);
2265 strncpy(connect.imsi, trans->subscr->imsi,
2266 sizeof(connect.imsi)-1);
2267
Harald Welte03740842009-06-10 23:11:52 +08002268 /* facility */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2270 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002271 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2273 }
2274 /* user-user */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2276 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002277 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2279 }
2280 /* ss-version */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2282 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002283 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2285 }
2286
2287 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002288 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002289
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002290 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002291}
2292
2293
2294static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2295{
2296 struct gsm_mncc connect_ack;
2297
2298 gsm48_stop_cc_timer(trans);
2299
2300 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002301 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002302
2303 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2304 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002305
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002306 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002307 &connect_ack);
2308}
2309
2310static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2311{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002312 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte03740842009-06-10 23:11:52 +08002313 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2314
Harald Welte03740842009-06-10 23:11:52 +08002315 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2316
2317 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2318
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002319 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002320}
2321
2322static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2323{
2324 struct gsm48_hdr *gh = msgb_l3(msg);
2325 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2326 struct tlv_parsed tp;
2327 struct gsm_mncc disc;
2328
2329 gsm48_stop_cc_timer(trans);
2330
2331 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2332
2333 memset(&disc, 0, sizeof(struct gsm_mncc));
2334 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002335 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002336 /* cause */
2337 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2338 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002339 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002340 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2341 }
2342 /* facility */
2343 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2344 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002345 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002346 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2347 }
2348 /* user-user */
2349 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2350 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002351 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002352 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2353 }
2354 /* ss-version */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2356 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002357 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002358 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2359 }
2360
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002361 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002362
2363}
2364
Harald Weltebbc636a2009-06-11 14:23:20 +08002365static struct gsm_mncc_cause default_cause = {
2366 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2367 .coding = 0,
2368 .rec = 0,
2369 .rec_val = 0,
2370 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2371 .diag_len = 0,
2372 .diag = { 0 },
2373};
Harald Welte03740842009-06-10 23:11:52 +08002374
2375static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2376{
2377 struct gsm_mncc *disc = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002378 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte03740842009-06-10 23:11:52 +08002379 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2380
Harald Welte03740842009-06-10 23:11:52 +08002381 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2382
2383 gsm48_stop_cc_timer(trans);
2384 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2385
2386 /* cause */
2387 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002388 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002389 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002390 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002391
2392 /* facility */
2393 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002394 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002395 /* progress */
2396 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002397 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002398 /* user-user */
2399 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002400 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002401
2402 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002403 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002404
2405 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2406
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002407 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002408}
2409
2410static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2411{
2412 struct gsm48_hdr *gh = msgb_l3(msg);
2413 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2414 struct tlv_parsed tp;
2415 struct gsm_mncc rel;
2416 int rc;
2417
2418 gsm48_stop_cc_timer(trans);
2419
2420 memset(&rel, 0, sizeof(struct gsm_mncc));
2421 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002422 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002423 /* cause */
2424 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2425 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002426 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002427 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2428 }
2429 /* facility */
2430 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2431 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002432 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002433 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2434 }
2435 /* user-user */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2437 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002438 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002439 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2440 }
2441 /* ss-version */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2443 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002444 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2446 }
2447
Harald Weltec2189a62009-07-23 18:56:43 +02002448 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002449 /* release collision 5.4.5 */
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002450 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002451 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002452 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002453 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002454 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002455 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002456 }
2457
2458 new_cc_state(trans, GSM_CSTATE_NULL);
2459
2460 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002461 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002462
2463 return rc;
2464}
2465
2466static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2467{
2468 struct gsm_mncc *rel = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002469 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte03740842009-06-10 23:11:52 +08002470 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2471
Harald Welte03740842009-06-10 23:11:52 +08002472 gh->msg_type = GSM48_MT_CC_RELEASE;
2473
Harald Welte03740842009-06-10 23:11:52 +08002474 gsm48_stop_cc_timer(trans);
2475 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2476
2477 /* cause */
2478 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002479 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002480 /* facility */
2481 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002482 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002483 /* user-user */
2484 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002485 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002486
Harald Weltec2189a62009-07-23 18:56:43 +02002487 trans->cc.T308_second = 0;
2488 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002489
Harald Weltec2189a62009-07-23 18:56:43 +02002490 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002491 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2492
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002493 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2497{
2498 struct gsm48_hdr *gh = msgb_l3(msg);
2499 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2500 struct tlv_parsed tp;
2501 struct gsm_mncc rel;
2502 int rc = 0;
2503
2504 gsm48_stop_cc_timer(trans);
2505
2506 memset(&rel, 0, sizeof(struct gsm_mncc));
2507 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002508 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002509 /* cause */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2511 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002512 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002513 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2514 }
2515 /* facility */
2516 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2517 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002518 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002519 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2520 }
2521 /* user-user */
2522 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2523 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002524 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002525 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2526 }
2527 /* ss-version */
2528 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2529 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002530 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002531 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2532 }
2533
2534 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002535 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002536 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002537 rc = mncc_recvmsg(trans->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002538 MNCC_REJ_IND, &rel);
2539 break;
2540 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002541 rc = mncc_recvmsg(trans->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002542 MNCC_REL_CNF, &rel);
2543 break;
2544 default:
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002545 rc = mncc_recvmsg(trans->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002546 MNCC_REL_IND, &rel);
2547 }
2548 }
2549
2550 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002551 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002552
2553 return rc;
2554}
2555
2556static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2557{
2558 struct gsm_mncc *rel = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002559 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte03740842009-06-10 23:11:52 +08002560 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002561 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002562
Harald Welte03740842009-06-10 23:11:52 +08002563 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2564
2565 trans->callref = 0;
2566
2567 gsm48_stop_cc_timer(trans);
2568
2569 /* cause */
2570 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002571 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002572 /* facility */
2573 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002574 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002575 /* user-user */
2576 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002577 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002578
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002579 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2580
Harald Weltec2189a62009-07-23 18:56:43 +02002581 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002582
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002583 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002584}
2585
2586static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2587{
2588 struct gsm48_hdr *gh = msgb_l3(msg);
2589 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2590 struct tlv_parsed tp;
2591 struct gsm_mncc fac;
2592
2593 memset(&fac, 0, sizeof(struct gsm_mncc));
2594 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002595 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002596 /* facility */
2597 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2598 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002599 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002600 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2601 }
2602 /* ss-version */
2603 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2604 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002605 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002606 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2607 }
2608
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002609 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002610}
2611
2612static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2613{
2614 struct gsm_mncc *fac = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002615 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte03740842009-06-10 23:11:52 +08002616 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2617
Harald Welte03740842009-06-10 23:11:52 +08002618 gh->msg_type = GSM48_MT_CC_FACILITY;
2619
2620 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002621 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002622
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002623 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002624}
2625
2626static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2627{
2628 struct gsm_mncc hold;
2629
2630 memset(&hold, 0, sizeof(struct gsm_mncc));
2631 hold.callref = trans->callref;
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002632 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2636{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002637 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte03740842009-06-10 23:11:52 +08002638 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2639
Harald Welte03740842009-06-10 23:11:52 +08002640 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2641
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002642 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002643}
2644
2645static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2646{
2647 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002648 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte03740842009-06-10 23:11:52 +08002649 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2650
Harald Welte03740842009-06-10 23:11:52 +08002651 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2652
2653 /* cause */
2654 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002655 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002656 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002657 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002658
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002659 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002660}
2661
2662static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2663{
2664 struct gsm_mncc retrieve;
2665
2666 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2667 retrieve.callref = trans->callref;
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002668 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte0abe5a62009-07-23 19:06:52 +02002669 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2673{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002674 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte03740842009-06-10 23:11:52 +08002675 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2676
Harald Welte03740842009-06-10 23:11:52 +08002677 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2678
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002679 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002680}
2681
2682static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2683{
2684 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002685 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte03740842009-06-10 23:11:52 +08002686 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2687
Harald Welte03740842009-06-10 23:11:52 +08002688 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2689
2690 /* cause */
2691 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002692 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002693 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002694 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002695
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002696 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002697}
2698
2699static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2700{
2701 struct gsm48_hdr *gh = msgb_l3(msg);
2702 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2703 struct tlv_parsed tp;
2704 struct gsm_mncc dtmf;
2705
2706 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2707 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002708 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002709 /* keypad facility */
2710 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2711 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002712 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002713 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2714 }
2715
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002716 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002717}
2718
2719static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2720{
2721 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002722 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte03740842009-06-10 23:11:52 +08002723 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2724
Harald Welte03740842009-06-10 23:11:52 +08002725 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2726
2727 /* keypad */
2728 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002729 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002730
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002731 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002732}
2733
2734static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2735{
2736 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002737 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte03740842009-06-10 23:11:52 +08002738 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2739
Harald Welte03740842009-06-10 23:11:52 +08002740 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2741
2742 /* cause */
2743 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002744 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002745 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002746 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002747
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002748 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002749}
2750
2751static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2752{
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002753 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte03740842009-06-10 23:11:52 +08002754 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2755
Harald Welte03740842009-06-10 23:11:52 +08002756 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2757
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002758 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002759}
2760
2761static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2762{
2763 struct gsm_mncc dtmf;
2764
2765 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2766 dtmf.callref = trans->callref;
2767
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002768 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002769}
2770
2771static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2772{
2773 struct gsm48_hdr *gh = msgb_l3(msg);
2774 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2775 struct tlv_parsed tp;
2776 struct gsm_mncc modify;
2777
2778 memset(&modify, 0, sizeof(struct gsm_mncc));
2779 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002780 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002781 /* bearer capability */
2782 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2783 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002784 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002785 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberg976493b2013-12-07 18:32:28 +01002786 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002787 }
2788
2789 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2790
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002791 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002792}
2793
2794static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2795{
2796 struct gsm_mncc *modify = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002797 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte03740842009-06-10 23:11:52 +08002798 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2799
Harald Welte03740842009-06-10 23:11:52 +08002800 gh->msg_type = GSM48_MT_CC_MODIFY;
2801
2802 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2803
2804 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002805 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002806
2807 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2808
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002809 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002810}
2811
2812static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2813{
2814 struct gsm48_hdr *gh = msgb_l3(msg);
2815 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2816 struct tlv_parsed tp;
2817 struct gsm_mncc modify;
2818
2819 gsm48_stop_cc_timer(trans);
2820
2821 memset(&modify, 0, sizeof(struct gsm_mncc));
2822 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002823 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002824 /* bearer capability */
2825 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2826 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002827 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002828 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberg976493b2013-12-07 18:32:28 +01002829 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002830 }
2831
2832 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2833
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002834 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002835}
2836
2837static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2838{
2839 struct gsm_mncc *modify = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002840 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte03740842009-06-10 23:11:52 +08002841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2842
Harald Welte03740842009-06-10 23:11:52 +08002843 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2844
2845 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002846 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002847
2848 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2849
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002850 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002851}
2852
2853static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2854{
2855 struct gsm48_hdr *gh = msgb_l3(msg);
2856 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2857 struct tlv_parsed tp;
2858 struct gsm_mncc modify;
2859
2860 gsm48_stop_cc_timer(trans);
2861
2862 memset(&modify, 0, sizeof(struct gsm_mncc));
2863 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002864 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002865 /* bearer capability */
2866 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2867 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002868 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002869 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberg976493b2013-12-07 18:32:28 +01002870 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002871 }
2872 /* cause */
2873 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2874 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002875 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002876 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2877 }
2878
2879 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2880
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002881 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002882}
2883
2884static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2885{
2886 struct gsm_mncc *modify = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002887 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte03740842009-06-10 23:11:52 +08002888 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2889
Harald Welte03740842009-06-10 23:11:52 +08002890 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2891
2892 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002893 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002894 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002895 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002896
2897 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2898
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002899 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002900}
2901
2902static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2903{
2904 struct gsm_mncc *notify = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002905 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte03740842009-06-10 23:11:52 +08002906 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2907
Harald Welte03740842009-06-10 23:11:52 +08002908 gh->msg_type = GSM48_MT_CC_NOTIFY;
2909
2910 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002911 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002912
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002913 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002914}
2915
2916static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2917{
2918 struct gsm48_hdr *gh = msgb_l3(msg);
2919 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2920// struct tlv_parsed tp;
2921 struct gsm_mncc notify;
2922
2923 memset(&notify, 0, sizeof(struct gsm_mncc));
2924 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002925// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002926 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002927 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002928
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002929 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002930}
2931
2932static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2933{
2934 struct gsm_mncc *user = arg;
Holger Hans Peter Freytherdd5012a2016-01-25 22:03:25 +01002935 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte03740842009-06-10 23:11:52 +08002936 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2937
Harald Welte03740842009-06-10 23:11:52 +08002938 gh->msg_type = GSM48_MT_CC_USER_INFO;
2939
2940 /* user-user */
2941 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002942 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002943 /* more data */
2944 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002945 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002946
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002947 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002948}
2949
2950static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2951{
2952 struct gsm48_hdr *gh = msgb_l3(msg);
2953 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2954 struct tlv_parsed tp;
2955 struct gsm_mncc user;
2956
2957 memset(&user, 0, sizeof(struct gsm_mncc));
2958 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002959 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002960 /* user-user */
2961 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2962 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002963 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002964 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2965 }
2966 /* more data */
2967 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2968 user.more = 1;
2969
Jacob Erlbecke0b37e12014-12-02 11:58:00 +01002970 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002971}
2972
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002973static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002974{
2975 struct gsm_mncc *mode = arg;
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02002976 struct gsm_lchan *lchan = trans->conn->lchan;
2977
2978 /*
2979 * We were forced to make an assignment a lot earlier and
2980 * we should avoid sending another assignment that might
2981 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
2982 * In case of rtp-bridge it is too late to change things
2983 * here.
2984 */
2985 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
2986 return 0;
Harald Welte03740842009-06-10 23:11:52 +08002987
Andreas Eversberg4c8fc932013-03-31 11:50:35 +02002988 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2989 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte03740842009-06-10 23:11:52 +08002990}
2991
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02002992static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2993 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2994 uint32_t payload_msg_type)
2995{
2996 uint8_t data[sizeof(struct gsm_mncc)];
2997 struct gsm_mncc_rtp *rtp;
2998
2999 memset(&data, 0, sizeof(data));
3000 rtp = (struct gsm_mncc_rtp *) &data[0];
3001
3002 rtp->callref = callref;
3003 rtp->msg_type = cmd;
3004 rtp->ip = addr;
3005 rtp->port = port;
3006 rtp->payload_type = payload_type;
3007 rtp->payload_msg_type = payload_msg_type;
3008 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3009}
3010
3011static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3012{
3013 struct gsm_lchan *lchan;
3014 int msg_type;
3015
3016 lchan = trans->conn->lchan;
3017 switch (lchan->abis_ip.rtp_payload) {
3018 case RTP_PT_GSM_FULL:
3019 msg_type = GSM_TCHF_FRAME;
3020 break;
3021 case RTP_PT_GSM_EFR:
3022 msg_type = GSM_TCHF_FRAME_EFR;
3023 break;
3024 case RTP_PT_GSM_HALF:
3025 msg_type = GSM_TCHH_FRAME;
3026 break;
3027 case RTP_PT_AMR:
3028 msg_type = GSM_TCH_FRAME_AMR;
3029 break;
3030 default:
3031 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3032 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3033 msg_type = 0;
3034 break;
3035 }
3036
3037 return mncc_recv_rtp(net, trans->callref, cmd,
3038 lchan->abis_ip.bound_ip,
3039 lchan->abis_ip.bound_port,
3040 lchan->abis_ip.rtp_payload,
3041 msg_type);
3042}
3043
3044static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3045{
3046 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3047}
3048
3049static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3050{
3051 struct gsm_bts *bts;
3052 struct gsm_lchan *lchan;
3053 struct gsm_trans *trans;
Max3a816e82016-04-18 23:11:18 +02003054 enum gsm48_chan_mode m;
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003055
3056 /* Find callref */
3057 trans = trans_find_by_callref(net, callref);
3058 if (!trans) {
3059 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3060 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3061 return -EIO;
3062 }
3063 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3064 if (!trans->conn) {
3065 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3066 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3067 return 0;
3068 }
3069
3070 lchan = trans->conn->lchan;
3071 bts = lchan->ts->trx->bts;
3072 if (!is_ipaccess_bts(bts)) {
3073 /*
3074 * I want this to be straight forward and have no audio flow
3075 * through the nitb/osmo-mss system. This currently means that
3076 * this will not work with BS11/Nokia type BTS. We would need
3077 * to have a trau<->rtp bridge for these but still preferable
3078 * in another process.
3079 */
3080 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3081 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3082 return -EINVAL;
3083 }
3084
3085 trans->conn->mncc_rtp_bridge = 1;
3086 /*
3087 * *sigh* we need to pick a codec now. Pick the most generic one
3088 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther8d88a922015-08-20 19:10:58 +02003089 * similiar to the routine above.
3090 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003091 */
3092 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3093 trans->conn->mncc_rtp_create_pending = 1;
Max3a816e82016-04-18 23:11:18 +02003094 m = mncc_codec_for_mode(lchan->type);
3095 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3096 get_value_string(gsm48_chan_mode_names, m),
3097 get_value_string(gsm_chan_t_names, lchan->type));
3098 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003099 lchan->type != GSM_LCHAN_TCH_H);
3100 }
3101
3102 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3103 return 0;
3104}
3105
3106static int tch_rtp_connect(struct gsm_network *net, void *arg)
3107{
3108 struct gsm_lchan *lchan;
3109 struct gsm_trans *trans;
3110 struct gsm_mncc_rtp *rtp = arg;
3111
3112 /* Find callref */
3113 trans = trans_find_by_callref(net, rtp->callref);
3114 if (!trans) {
3115 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3116 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3117 return -EIO;
3118 }
3119 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3120 if (!trans->conn) {
3121 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3122 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3123 return 0;
3124 }
3125
3126 lchan = trans->conn->lchan;
Max3a816e82016-04-18 23:11:18 +02003127 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3128 get_value_string(gsm48_chan_mode_names,
3129 mncc_codec_for_mode(lchan->type)),
3130 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003131
3132 /* TODO: Check if payload_msg_type is compatible with what we have */
3133 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3134 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3135 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3136 }
3137
3138 /*
3139 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3140 * complains about both rtp and rtp payload2 being present in the
3141 * same package!
3142 */
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02003143 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003144 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3145}
3146
3147static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3148{
3149 struct gsm_network *net;
3150 struct gsm_trans *tmp, *trans = NULL;
3151
3152 net = lchan->ts->trx->bts->network;
3153 llist_for_each_entry(tmp, &net->trans_list, entry) {
3154 if (!tmp->conn)
3155 continue;
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02003156 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003157 continue;
3158 trans = tmp;
3159 break;
3160 }
3161
3162 if (!trans) {
3163 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3164 gsm_lchan_name(lchan));
3165 return 0;
3166 }
3167
3168 switch (signal) {
3169 case S_ABISIP_CRCX_ACK:
3170 if (lchan->conn->mncc_rtp_create_pending) {
3171 lchan->conn->mncc_rtp_create_pending = 0;
3172 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3173 gsm_lchan_name(lchan));
3174 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3175 }
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02003176 /*
3177 * TODO: this appears to be too early? Why not until after
3178 * the handover detect or the handover complete?
3179 */
3180 maybe_switch_for_handover(lchan);
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003181 break;
3182 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherbfe773f2015-08-04 14:41:21 +02003183 if (lchan->conn->mncc_rtp_connect_pending) {
3184 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003185 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3186 gsm_lchan_name(lchan));
3187 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3188 }
3189 break;
3190 }
3191
3192 return 0;
3193}
3194
3195
Harald Welte03740842009-06-10 23:11:52 +08003196static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003197 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003198 int type;
3199 int (*rout) (struct gsm_trans *trans, void *arg);
3200} downstatelist[] = {
3201 /* mobile originating call establishment */
3202 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3203 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3204 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3205 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3206 {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 */
3207 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3208 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3209 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3210 /* mobile terminating call establishment */
3211 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3212 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3213 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3214 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3215 /* signalling during call */
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3218 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3219 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3220 {ALL_STATES,
3221 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3222 {ALL_STATES,
3223 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3224 {ALL_STATES,
3225 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3226 {SBIT(GSM_CSTATE_ACTIVE),
3227 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3228 {SBIT(GSM_CSTATE_ACTIVE),
3229 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3230 {SBIT(GSM_CSTATE_ACTIVE),
3231 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3232 {SBIT(GSM_CSTATE_ACTIVE),
3233 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3234 {SBIT(GSM_CSTATE_ACTIVE),
3235 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3236 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3237 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3238 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3239 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3240 {SBIT(GSM_CSTATE_ACTIVE),
3241 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3242 /* clearing */
3243 {SBIT(GSM_CSTATE_INITIATED),
3244 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3245 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3246 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3247 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3248 MNCC_REL_REQ, gsm48_cc_tx_release},
3249 /* special */
3250 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003251 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08003252};
3253
3254#define DOWNSLLEN \
3255 (sizeof(downstatelist) / sizeof(struct downstate))
3256
3257
Harald Weltef4e4b8a2010-12-22 23:57:45 +01003258int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08003259{
Harald Welteaa60edb2009-08-09 18:52:33 +02003260 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08003261 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003262 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01003263 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003264 struct gsm_mncc *data = arg, rel;
3265
Harald Welte17b56522010-12-22 21:45:05 +01003266 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3267
Harald Welte03740842009-06-10 23:11:52 +08003268 /* handle special messages */
3269 switch(msg_type) {
3270 case MNCC_BRIDGE:
Max30aa2322016-04-25 15:22:00 +02003271 rc = tch_bridge(net, arg);
3272 if (rc < 0)
3273 disconnect_bridge(net, arg, -rc);
3274 return rc;
Harald Welte03740842009-06-10 23:11:52 +08003275 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01003276 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08003277 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01003278 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freyther493d4ee2015-08-04 13:32:09 +02003279 case MNCC_RTP_CREATE:
3280 return tch_rtp_create(net, data->callref);
3281 case MNCC_RTP_CONNECT:
3282 return tch_rtp_connect(net, arg);
3283 case MNCC_RTP_FREE:
3284 /* unused right now */
3285 return -EIO;
Harald Welte3971ad52009-12-19 22:23:05 +01003286 case GSM_TCHF_FRAME:
Andreas Eversberg2c202cd2013-12-06 16:59:10 +01003287 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg39ff0842014-01-17 19:06:38 +01003288 case GSM_TCHH_FRAME:
Andreas Eversberga3773f62012-03-08 14:39:19 +01003289 case GSM_TCH_FRAME_AMR:
Harald Welte3971ad52009-12-19 22:23:05 +01003290 /* Find callref */
3291 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01003292 if (!trans) {
3293 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01003294 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01003295 }
Holger Hans Peter Freyther0baeca82013-12-29 20:24:37 +01003296 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte17b56522010-12-22 21:45:05 +01003297 if (!trans->conn) {
3298 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01003299 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01003300 }
Andreas Eversbergf214cd32013-03-11 08:20:48 +01003301 if (!trans->conn->lchan) {
3302 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3303 return 0;
3304 }
Andreas Eversberg39ff0842014-01-17 19:06:38 +01003305 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3306 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte17b56522010-12-22 21:45:05 +01003307 /* This should be LOGL_ERROR or NOTICE, but
3308 * unfortuantely it happens for a couple of frames at
3309 * the beginning of every RTP connection */
Andreas Eversberg39ff0842014-01-17 19:06:38 +01003310 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welte3971ad52009-12-19 22:23:05 +01003311 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01003312 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003313 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01003314 switch (bts->type) {
3315 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02003316 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01003317 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01003318 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01003319 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01003320 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003321 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01003322 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03003323 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02003324 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003325 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01003326 default:
Harald Welte8099a712011-08-09 21:53:20 +02003327 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01003328 }
3329 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08003330 }
3331
3332 memset(&rel, 0, sizeof(struct gsm_mncc));
3333 rel.callref = data->callref;
3334
3335 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02003336 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08003337
3338 /* Callref unknown */
3339 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003340 struct gsm_subscriber *subscr;
3341
Harald Welte6e1536e2009-07-04 10:11:24 +02003342 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08003343 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3344 "Received '%s' from MNCC with "
3345 "unknown callref %d\n", data->called.number,
3346 get_mncc_name(msg_type), data->callref);
3347 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003348 return mncc_release_ind(net, NULL, data->callref,
3349 GSM48_CAUSE_LOC_PRN_S_LU,
3350 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08003351 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003352 if (!data->called.number[0] && !data->imsi[0]) {
3353 DEBUGP(DCC, "(bts - trx - ts - ti) "
3354 "Received '%s' from MNCC with "
3355 "no number or IMSI\n", get_mncc_name(msg_type));
3356 /* Invalid number */
3357 return mncc_release_ind(net, NULL, data->callref,
3358 GSM48_CAUSE_LOC_PRN_S_LU,
3359 GSM48_CC_CAUSE_INV_NR_FORMAT);
3360 }
Harald Welte03740842009-06-10 23:11:52 +08003361 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003362 if (data->called.number[0])
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01003363 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte75350412009-07-23 18:46:00 +02003364 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003365 else
Jacob Erlbeckda8770b2014-12-03 09:28:24 +01003366 subscr = subscr_get_by_imsi(net->subscr_group,
3367 data->imsi);
Holger Hans Peter Freyther0baeca82013-12-29 20:24:37 +01003368
3369 /* update the subscriber we deal with */
3370 log_set_context(BSC_CTX_SUBSCR, subscr);
3371
Harald Welte03740842009-06-10 23:11:52 +08003372 /* If subscriber is not found */
3373 if (!subscr) {
3374 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3375 "Received '%s' from MNCC with "
3376 "unknown subscriber %s\n", data->called.number,
3377 get_mncc_name(msg_type), data->called.number);
3378 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003379 return mncc_release_ind(net, NULL, data->callref,
3380 GSM48_CAUSE_LOC_PRN_S_LU,
3381 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08003382 }
3383 /* If subscriber is not "attached" */
3384 if (!subscr->lac) {
3385 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3386 "Received '%s' from MNCC with "
3387 "detached subscriber %s\n", data->called.number,
3388 get_mncc_name(msg_type), data->called.number);
3389 subscr_put(subscr);
3390 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003391 return mncc_release_ind(net, NULL, data->callref,
3392 GSM48_CAUSE_LOC_PRN_S_LU,
3393 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08003394 }
3395 /* Create transaction */
Jacob Erlbeck59923ae2014-12-02 14:22:53 +01003396 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltec2189a62009-07-23 18:56:43 +02003397 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003398 DEBUGP(DCC, "No memory for trans.\n");
3399 subscr_put(subscr);
3400 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003401 mncc_release_ind(net, NULL, data->callref,
3402 GSM48_CAUSE_LOC_PRN_S_LU,
3403 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003404 return -ENOMEM;
3405 }
Harald Welte03740842009-06-10 23:11:52 +08003406 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003407 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003408
Harald Welte03740842009-06-10 23:11:52 +08003409 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003410 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003411 /* find transaction with this subscriber already paging */
3412 llist_for_each_entry(transt, &net->trans_list, entry) {
3413 /* Transaction of our lchan? */
3414 if (transt == trans ||
3415 transt->subscr != subscr)
3416 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003417 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003418 "Received '%s' from MNCC with "
3419 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003420 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003421 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003422 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003423 subscr_put(subscr);
3424 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003425 return 0;
3426 }
3427 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003428 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003429
Holger Hans Peter Freyther61623df2015-04-06 12:03:45 +02003430 /* Request a channel */
3431 trans->paging_request = subscr_request_channel(subscr,
3432 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3433 trans);
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003434 if (!trans->paging_request) {
3435 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3436 subscr_put(subscr);
3437 trans_free(trans);
3438 return 0;
3439 }
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003440 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003441 return 0;
3442 }
3443 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003444 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003445 subscr_put(subscr);
Holger Hans Peter Freyther0baeca82013-12-29 20:24:37 +01003446 } else {
3447 /* update the subscriber we deal with */
3448 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte03740842009-06-10 23:11:52 +08003449 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003450
3451 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003452 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003453
3454 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003455 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003456 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003457 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003458 (trans->subscr)?(trans->subscr->extension):"-",
3459 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003460 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3461 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003462 if (msg_type == MNCC_REL_REQ)
3463 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3464 else
3465 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3466 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003467 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003468 return rc;
3469 }
3470
3471 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3472 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003473 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003474 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003475 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003476 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003477 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003478
3479 /* Find function for current state and message */
3480 for (i = 0; i < DOWNSLLEN; i++)
3481 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003482 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003483 break;
3484 if (i == DOWNSLLEN) {
3485 DEBUGP(DCC, "Message unhandled at this state.\n");
3486 return 0;
3487 }
3488
3489 rc = downstatelist[i].rout(trans, arg);
3490
3491 return rc;
3492}
3493
3494
3495static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003496 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003497 int type;
3498 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3499} datastatelist[] = {
3500 /* mobile originating call establishment */
3501 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3502 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3503 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3504 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3505 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3506 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3507 /* mobile terminating call establishment */
3508 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3509 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3510 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3511 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003512 {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 Welte03740842009-06-10 23:11:52 +08003513 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3514 /* signalling during call */
3515 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3516 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3517 {SBIT(GSM_CSTATE_ACTIVE),
3518 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3519 {ALL_STATES,
3520 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3521 {ALL_STATES,
3522 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3523 {ALL_STATES,
3524 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3525 {SBIT(GSM_CSTATE_ACTIVE),
3526 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3527 {SBIT(GSM_CSTATE_ACTIVE),
3528 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3529 {SBIT(GSM_CSTATE_ACTIVE),
3530 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3531 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3532 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3533 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3534 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3535 {SBIT(GSM_CSTATE_ACTIVE),
3536 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3537 /* clearing */
3538 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3539 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3540 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3541 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3542 {ALL_STATES, /* 5.4.3.4 */
3543 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3544};
3545
3546#define DATASLLEN \
3547 (sizeof(datastatelist) / sizeof(struct datastate))
3548
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003549static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003550{
3551 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrf94dfd52016-03-14 16:13:24 +01003552 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyrf1c283c2016-03-14 16:13:25 +01003553 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltec2189a62009-07-23 18:56:43 +02003554 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003555 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003556
Harald Welte03740842009-06-10 23:11:52 +08003557 if (msg_type & 0x80) {
3558 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3559 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003560 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003561
Harald Welte03740842009-06-10 23:11:52 +08003562 /* Find transaction */
Jacob Erlbecke2d80142014-12-02 14:22:53 +01003563 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003564
Harald Welte4861c822009-07-23 21:21:14 +02003565 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003566 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003567 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003568 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003569 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3570 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003571
3572 /* Create transaction */
3573 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003574 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003575 "creating new trans.\n", transaction_id);
3576 /* Create transaction */
Jacob Erlbeck59923ae2014-12-02 14:22:53 +01003577 trans = trans_alloc(conn->bts->network, conn->subscr,
3578 GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003579 transaction_id, new_callref++);
3580 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003581 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003582 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003583 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003584 GSM48_MT_CC_RELEASE_COMPL);
3585 return -ENOMEM;
3586 }
Harald Welte03740842009-06-10 23:11:52 +08003587 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003588 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003589 }
3590
3591 /* find function for current state and message */
3592 for (i = 0; i < DATASLLEN; i++)
3593 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003594 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003595 break;
3596 if (i == DATASLLEN) {
3597 DEBUGP(DCC, "Message unhandled at this state.\n");
3598 return 0;
3599 }
3600
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02003601 assert(trans->subscr);
3602
Harald Welte03740842009-06-10 23:11:52 +08003603 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003604
3605 return rc;
3606}
3607
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003608/* Create a dummy to wait five seconds */
3609static void release_anchor(struct gsm_subscriber_connection *conn)
3610{
3611 if (!conn->anch_operation)
3612 return;
3613
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003614 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003615 talloc_free(conn->anch_operation);
3616 conn->anch_operation = NULL;
3617}
3618
3619static void anchor_timeout(void *_data)
3620{
3621 struct gsm_subscriber_connection *con = _data;
3622
3623 release_anchor(con);
3624 msc_release_connection(con);
3625}
3626
3627int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3628{
3629 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3630 if (!conn->anch_operation)
3631 return -1;
3632
3633 conn->anch_operation->timeout.data = conn;
3634 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003635 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003636 return 0;
3637}
3638
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003639/* here we get data from the BSC level... */
3640int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003641{
3642 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrf94dfd52016-03-14 16:13:24 +01003643 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte59b04682009-06-10 05:40:52 +08003644 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003645
Jacob Erlbeckc8a92452014-01-30 21:01:12 +01003646 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003647 if (silent_call_reroute(conn, msg))
3648 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003649
3650 switch (pdisc) {
3651 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003652 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003653 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003654 break;
3655 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003656 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003657 break;
3658 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003659 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003660 break;
3661 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003662 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003663 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003664 break;
3665 case GSM48_PDISC_MM_GPRS:
3666 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003667 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3668 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003669 break;
Harald Welte03115042009-10-16 08:32:58 +02003670 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003671 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003672 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003673 break;
Harald Welte59b04682009-06-10 05:40:52 +08003674 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003675 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3676 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003677 break;
3678 }
3679
3680 return rc;
3681}
3682
Harald Welte3c062072009-07-28 18:25:29 +02003683/*
3684 * This will be ran by the linker when loading the DSO. We use it to
3685 * do system initialization, e.g. registration of signal handlers.
3686 */
3687static __attribute__((constructor)) void on_dso_load_0408(void)
3688{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003689 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003690}