blob: c910d7192ef80f15e8ded4dcc661f4af4e1bfd59 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080061
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/gsm/gsm48.h>
63#include <osmocom/gsm/gsm0480.h>
64#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020065#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/core/msgb.h>
67#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010068#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010069#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000070
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020071#include <assert.h>
72
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020073void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010074void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020075
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020076static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
77
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020078static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080079static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020080 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010081static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080082static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Andreas Eversberga83d5112013-12-07 18:32:28 +010090static int apply_codec_restrictions(struct gsm_bts *bts,
91 struct gsm_mncc_bearer_cap *bcap)
92{
93 int i, j;
94
95 /* remove unsupported speech versions from list */
96 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
103 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
104 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
105 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
106 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
107 }
108 bcap->speech_ver[j] = -1;
109
110 return 0;
111}
112
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200113static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800114
Harald Welte31c00f72011-03-03 23:29:05 +0100115void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
116{
117 net->mncc_recv(net, msg);
118}
119
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
121 struct gsm_trans *trans)
122{
123 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
124
125 /* if we get passed a transaction reference, do some common
126 * work that the caller no longer has to do */
127 if (trans) {
128 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
129 msg->lchan = trans->conn->lchan;
130 }
131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800132 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200133 struct e1inp_sign_link *sign_link =
134 msg->lchan->ts->trx->rsl_link;
135
136 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100137 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800138 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending '%s' to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr & 0xf0,
143 gsm48_cc_msg_name(gh->msg_type));
144 else
145 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200146 "Sending 0x%02x to MS.\n",
147 sign_link->trx->bts->nr,
148 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800149 gh->proto_discr, gh->msg_type);
150 }
151
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100152 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800153}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800155int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
156{
157 struct gsm48_hdr *gh;
158 struct msgb *ss_notify;
159
160 ss_notify = gsm0480_create_notifySS(message);
161 if (!ss_notify)
162 return -1;
163
164 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
165 uint8_t *data = msgb_push(ss_notify, 1);
166 data[0] = ss_notify->len - 1;
167 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
168 gh->msg_type = GSM48_MT_CC_FACILITY;
169 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
170}
171
Harald Weltecf149ee2012-01-23 16:40:24 +0100172void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173{
174 if (!conn->sec_operation)
175 return;
176
177 talloc_free(conn->sec_operation);
178 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800179 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100180}
181
Harald Weltecf149ee2012-01-23 16:40:24 +0100182void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100184 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
185 struct gsm_security_operation);
186}
187
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100188int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
189 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100190{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200191 struct gsm_network *net = conn->network;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800192 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100193 struct gsm_security_operation *op;
194 struct gsm_auth_tuple atuple;
195 int status = -1, rc;
196
197 /* Check if we _can_ enable encryption. Cases where we can't:
198 * - Encryption disabled in config
199 * - Channel already secured (nothing to do)
200 * - Subscriber equipment doesn't support configured encryption
201 */
202 if (!net->a5_encryption) {
203 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800204 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100205 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200206 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100207 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
208 net->a5_encryption)) {
209 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* If not done yet, try to get info for this user */
214 if (status < 0) {
215 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
216 if (rc <= 0)
217 status = GSM_SECURITY_NOAVAIL;
218 }
219
220 /* Are we done yet ? */
221 if (status >= 0)
222 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100224 0;
225
226 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800227 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100228 return -EBUSY;
229
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800230 allocate_security_operation(conn);
231 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100232 op->cb = cb;
233 op->cb_data = cb_data;
234 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
235
236 /* FIXME: Should start a timer for completion ... */
237
238 /* Then do whatever is needed ... */
Neels Hofmeyrd2fa7a52016-03-10 23:30:37 +0100239 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100240 /* Start authentication */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100241 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.rand, NULL,
242 op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100243 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100244 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100245 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +0200246 op->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100247 }
248
249 return -EINVAL; /* not reached */
250}
251
Maxddee01f2016-05-24 14:23:27 +0200252static bool subscr_regexp_check(const struct gsm_network *net, const char *imsi)
253{
254 if (!net->authorized_reg_str)
255 return false;
256
257 if (regexec(&net->authorized_regexp, imsi, 0, NULL, 0) != REG_NOMATCH)
258 return true;
259
260 return false;
261}
262
Holger Freyther73487a22008-12-31 18:53:57 +0000263static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
264 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000265{
266 if (!subscriber)
267 return 0;
268
Holger Freyther73487a22008-12-31 18:53:57 +0000269 /*
270 * Do not send accept yet as more information should arrive. Some
271 * phones will not send us the information and we will have to check
272 * what we want to do with that.
273 */
274 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
275 return 0;
276
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100277 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200278 case GSM_AUTH_POLICY_CLOSED:
279 return subscriber->authorized;
Maxddee01f2016-05-24 14:23:27 +0200280 case GSM_AUTH_POLICY_REGEXP:
281 if (subscriber->authorized)
282 return 1;
283 if (subscr_regexp_check(subscriber->group->net,
284 subscriber->imsi))
285 subscriber->authorized = 1;
286 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200287 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200288 if (subscriber->authorized)
289 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200290 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200291 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000292 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200293 default:
294 return 0;
295 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000296}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000297
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100298static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000299{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100300 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000301 return;
302
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800303 /* No need to keep the connection up */
304 release_anchor(conn);
305
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200306 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100307 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100308 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100309 if (release)
310 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000311}
312
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100313static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000314{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800315 if (conn->loc_operation)
316 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100317 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000318
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100319 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200320 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000321}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000322
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200323static int finish_lu(struct gsm_subscriber_connection *conn)
324{
325 int rc = 0;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200326 int avoid_tmsi = conn->network->avoid_tmsi;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200327
328 /* We're all good */
329 if (avoid_tmsi) {
330 conn->subscr->tmsi = GSM_RESERVED_TMSI;
331 db_sync_subscriber(conn->subscr);
332 } else {
333 db_subscriber_alloc_tmsi(conn->subscr);
334 }
335
336 rc = gsm0408_loc_upd_acc(conn);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200337 if (conn->network->send_mm_info) {
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200338 /* send MM INFO with network name */
339 rc = gsm48_tx_mm_info(conn);
340 }
341
342 /* call subscr_update after putting the loc_upd_acc
343 * in the transmit queue, since S_SUBSCR_ATTACHED might
344 * trigger further action like SMS delivery */
345 subscr_update(conn->subscr, conn->bts,
346 GSM_SUBSCRIBER_UPDATE_ATTACHED);
347
348 /*
349 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
350 * MS needs to respond with a TMSI REALLOCATION COMPLETE
351 * (even if the TMSI is the same).
352 */
353 if (avoid_tmsi)
354 release_loc_updating_req(conn, 1);
355
356 return rc;
357}
358
Sylvain Munaut267fba02009-12-24 00:28:01 +0100359static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
360 struct msgb *msg, void *data, void *param)
361{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800362 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100363 int rc = 0;
364
365 switch (event) {
366 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100367 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 break;
369
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200370 case GSM_SECURITY_ALREADY:
371 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
372 "UPDATING after CM SERVICE REQUEST\n");
373 /* fall through */
374
Sylvain Munaut267fba02009-12-24 00:28:01 +0100375 case GSM_SECURITY_NOAVAIL:
376 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200377 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100378 break;
379
380 default:
381 rc = -EINVAL;
382 };
383
384 return rc;
385}
386
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100387static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000388{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100389 if (!conn->loc_operation)
390 return 0;
391
Sylvain Munaut267fba02009-12-24 00:28:01 +0100392 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800393 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100394 conn->loc_operation->key_seq,
395 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000396 return 0;
397}
398
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800399void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000400{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800401 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800402
403 /* avoid someone issuing a clear */
404 conn->in_release = 1;
405
Holger Freyther7c19f742009-06-06 13:54:35 +0000406 /*
407 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800408 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000409 */
Neels Hofmeyraea28ce2016-05-04 15:30:30 +0200410 release_loc_updating_req(conn, 0);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100411
412 /* We might need to cancel the paging response or such. */
413 if (conn->sec_operation && conn->sec_operation->cb) {
414 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
415 NULL, conn, conn->sec_operation->cb_data);
416 }
417
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800418 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800419 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000420
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100421 /*
422 * Free all transactions that are associated with the released
423 * connection. The transaction code will inform the CC or SMS
424 * facilities that will send the release indications. As part of
425 * the CC REL_IND the remote leg might be released and this will
426 * trigger the call to trans_free. This is something the llist
427 * macro can not handle and we will need to re-iterate the list.
428 *
429 * TODO: Move the trans_list into the subscriber connection and
430 * create a pending list for MT transactions. These exist before
431 * we have a subscriber connection.
432 */
433restart:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200434 llist_for_each_entry_safe(trans, temp, &conn->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100435 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200436 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100437 goto restart;
438 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800439 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000440}
441
Harald Welte371efe52010-12-22 23:17:50 +0100442void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
443{
444 struct gsm_trans *trans, *temp;
445
446 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
447
448 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100449 if (trans->protocol == protocol) {
450 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100451 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100452 }
Harald Welte371efe52010-12-22 23:17:50 +0100453 }
454}
455
Holger Freyther429e7762008-12-30 13:28:30 +0000456/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200457int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000458{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800459 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800460 struct msgb *msg;
461
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200462 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_REJECT]);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800463
464 msg = gsm48_create_loc_upd_rej(cause);
465 if (!msg) {
466 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
467 return -1;
468 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200469
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800470 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000471
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100472 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte7184bd02015-12-28 14:04:36 +0100473 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800474 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100475
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800476 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000477}
478
479/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200480static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000481{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100482 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000483 struct gsm48_hdr *gh;
484 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200485 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200486
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800487 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000488
489 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
490 gh->proto_discr = GSM48_PDISC_MM;
491 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
492
493 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200494 gsm48_generate_lai(lai, conn->network->country_code,
495 conn->network->network_code,
496 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000497
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200498 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
499 uint8_t mi[10];
500 int len;
501 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
502 mid = msgb_put(msg, len);
503 memcpy(mid, mi, len);
504 } else {
505 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
506 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
507 }
Harald Welte52b1f982008-12-23 20:25:15 +0000508
509 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
510
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200511 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_ACCEPT]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100512
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800513 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000514}
515
Harald Weltebf5e8df2009-02-03 12:59:45 +0000516/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000518{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100519 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000520 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000521
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800522 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000523
524 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
525 gh->proto_discr = GSM48_PDISC_MM;
526 gh->msg_type = GSM48_MT_MM_ID_REQ;
527 gh->data[0] = id_type;
528
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800529 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000530}
531
Maxe21cf382016-05-11 11:01:06 +0200532static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
533 const char *imsi)
534{
Maxe6052c42016-06-30 10:25:49 +0200535 if (!net->auto_create_subscr)
Maxddee01f2016-05-24 14:23:27 +0200536 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200537
Maxe6052c42016-06-30 10:25:49 +0200538 if (!subscr_regexp_check(net, imsi))
539 return NULL;
Maxddee01f2016-05-24 14:23:27 +0200540
Maxe6052c42016-06-30 10:25:49 +0200541 return subscr_create_subscriber(net->subscr_group, imsi);
Maxe21cf382016-05-11 11:01:06 +0200542}
Harald Welte231ad4f2008-12-27 11:15:38 +0000543
Harald Weltebf5e8df2009-02-03 12:59:45 +0000544/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800545static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000546{
547 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200548 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200549 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200550 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000551
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200552 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200553 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
554 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000555
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200556 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100557
Harald Welte75a983f2008-12-27 21:34:06 +0000558 switch (mi_type) {
559 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200560 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100562 conn->subscr = subscr_get_by_imsi(net->subscr_group,
563 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200564 if (!conn->subscr)
565 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200566 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 if (!conn->subscr && conn->loc_operation) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200568 gsm0408_loc_upd_rej(conn, net->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100569 release_loc_updating_req(conn, 1);
570 return 0;
571 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 if (conn->loc_operation)
573 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000574 break;
575 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000576 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000577 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578 if (conn->subscr) {
579 db_subscriber_assoc_imei(conn->subscr, mi_string);
580 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200581 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100582 if (conn->loc_operation)
583 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000584 break;
585 }
Holger Freyther73487a22008-12-31 18:53:57 +0000586
587 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100588 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000589}
590
Harald Welte255539c2008-12-28 02:26:27 +0000591
592static void loc_upd_rej_cb(void *data)
593{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100594 struct gsm_subscriber_connection *conn = data;
Harald Welte255539c2008-12-28 02:26:27 +0000595
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100596 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200597 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100598 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000599}
600
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100601static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000602{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
604 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200605 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000606}
607
Harald Welteb9845f92015-08-16 18:07:48 +0200608static const struct value_string lupd_names[] = {
609 { GSM48_LUPD_NORMAL, "NORMAL" },
610 { GSM48_LUPD_PERIODIC, "PERIODIC" },
611 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
612 { 0, NULL }
613};
Harald Welte2a139372009-02-22 21:14:55 +0000614
Harald Weltebf5e8df2009-02-03 12:59:45 +0000615/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800616static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000617{
Harald Welte8470bf22008-12-25 23:28:35 +0000618 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000619 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800620 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200621 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200622 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000623
Harald Welte8470bf22008-12-25 23:28:35 +0000624 lu = (struct gsm48_loc_upd_req *) gh->data;
625
626 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000627
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200628 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000629
Harald Welteb9845f92015-08-16 18:07:48 +0200630 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
631 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000632
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200633 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100634
Harald Welte24ff6ee2009-12-22 00:41:05 +0100635 switch (lu->type) {
636 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200637 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100638 break;
639 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200640 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100641 break;
642 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200643 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100644 break;
645 }
646
Holger Freythereaf04692009-06-06 13:54:44 +0000647 /*
648 * Pseudo Spoof detection: Just drop a second/concurrent
649 * location updating request.
650 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100651 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200652 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100653 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800654 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000655 return 0;
656 }
657
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800658 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000659
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200660 conn->loc_operation->key_seq = lu->key_seq;
661
Harald Welte52b1f982008-12-23 20:25:15 +0000662 switch (mi_type) {
663 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200664 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000665 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200666 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100667 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000668
Jan Luebbe370b41d2009-08-12 10:19:34 +0200669 /* look up subscriber based on IMSI, create if not found */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200670 subscr = subscr_get_by_imsi(conn->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200671 if (!subscr)
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200672 subscr = subscr_create(conn->network, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100673 if (!subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200674 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100675 release_loc_updating_req(conn, 0);
676 return 0;
677 }
Harald Welte4b634542008-12-27 01:55:51 +0000678 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000679 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200680 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000681 /* look up the subscriber based on TMSI, request IMSI if it fails */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200682 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200683 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000684 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000685 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200686 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100687 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000688 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200689 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200690 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200691 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000692 break;
693 case GSM_MI_TYPE_IMEI:
694 case GSM_MI_TYPE_IMEISV:
695 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200696 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000697 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200698 default:
Harald Weltea0368542009-06-27 02:58:43 +0200699 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000700 break;
701 }
702
Harald Welte24516ea2009-07-04 10:18:00 +0200703 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100704 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200705
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200707 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 /* FIXME: request id? close channel? */
709 return -EINVAL;
710 }
711
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100712 conn->subscr = subscr;
713 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000714
Harald Welte24516ea2009-07-04 10:18:00 +0200715 /* check if we can let the subscriber into our network immediately
716 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100717 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000718}
719
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100720/* Turn int into semi-octet representation: 98 => 0x89 */
721static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100723 uint8_t ret;
724
725 ret = value / 10;
726 ret |= (value % 10) << 4;
727
728 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800729}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100730
Harald Welte4bfdfe72009-06-10 23:11:52 +0800731
Harald Weltedb253af2008-12-30 17:56:55 +0000732/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800733int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000734{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100735 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000736 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200737 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200738 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200739 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100740
Harald Welte4bfdfe72009-06-10 23:11:52 +0800741 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100742 struct tm* gmt_time;
743 struct tm* local_time;
744 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100745 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000746
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800747 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000748
749 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
750 gh->proto_discr = GSM48_PDISC_MM;
751 gh->msg_type = GSM48_MT_MM_INFO;
752
753 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200754#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000755 name_len = strlen(net->name_long);
756 /* 10.5.3.5a */
757 ptr8 = msgb_put(msg, 3);
758 ptr8[0] = GSM48_IE_NAME_LONG;
759 ptr8[1] = name_len*2 +1;
760 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
761
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200762 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000763 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000764 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000765
766 /* FIXME: Use Cell Broadcast, not UCS-2, since
767 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200768#endif
769 name_len = (strlen(net->name_long)*7)/8;
770 name_pad = (8 - strlen(net->name_long)*7)%8;
771 if (name_pad > 0)
772 name_len++;
773 /* 10.5.3.5a */
774 ptr8 = msgb_put(msg, 3);
775 ptr8[0] = GSM48_IE_NAME_LONG;
776 ptr8[1] = name_len +1;
777 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
778
779 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100780 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200781
Harald Weltedb253af2008-12-30 17:56:55 +0000782 }
783
784 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200785#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000786 name_len = strlen(net->name_short);
787 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200788 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200789 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000790 ptr8[1] = name_len*2 + 1;
791 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
792
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200793 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000794 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000795 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200796#endif
797 name_len = (strlen(net->name_short)*7)/8;
798 name_pad = (8 - strlen(net->name_short)*7)%8;
799 if (name_pad > 0)
800 name_len++;
801 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200802 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200803 ptr8[0] = GSM48_IE_NAME_SHORT;
804 ptr8[1] = name_len +1;
805 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
806
807 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100808 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200809
Harald Weltedb253af2008-12-30 17:56:55 +0000810 }
811
Harald Weltedb253af2008-12-30 17:56:55 +0000812 /* Section 10.5.3.9 */
813 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100814 gmt_time = gmtime(&cur_t);
815
Harald Weltedb253af2008-12-30 17:56:55 +0000816 ptr8 = msgb_put(msg, 8);
817 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100818 ptr8[1] = bcdify(gmt_time->tm_year % 100);
819 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
820 ptr8[3] = bcdify(gmt_time->tm_mday);
821 ptr8[4] = bcdify(gmt_time->tm_hour);
822 ptr8[5] = bcdify(gmt_time->tm_min);
823 ptr8[6] = bcdify(gmt_time->tm_sec);
824
Neels Hofmeyr73983952016-05-10 13:29:33 +0200825 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200826 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200827 if (net->tz.hr < 0) {
828 tzunits = ((net->tz.hr/-1)*4);
829 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100830 ptr8[7] = bcdify(tzunits);
831 /* Set negative time */
832 ptr8[7] |= 0x08;
833 }
834 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200835 tzunits = net->tz.hr*4;
836 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100837 ptr8[7] = bcdify(tzunits);
838 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100839 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200840 if (net->tz.dst >= 0 && net->tz.dst <= 2)
841 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100842 }
843 else {
844 /* Need to get GSM offset and convert into 15 min units */
845 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
846 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200847#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100848 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200849#else
850#warning find a portable way to obtain the timezone offset
851 tzunits = 0;
852#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100853 if (tzunits < 0) {
854 tzunits = tzunits/-1;
855 ptr8[7] = bcdify(tzunits);
856 /* Flip it to negative */
857 ptr8[7] |= 0x08;
858 }
859 else
860 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100861
862 /* Does not support DST +2 */
863 if (local_time->tm_isdst)
864 dst = 1;
865 }
866
867 if (dst) {
868 ptr8 = msgb_put(msg, 3);
869 ptr8[0] = GSM48_IE_NET_DST;
870 ptr8[1] = 1;
871 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100872 }
Harald Weltedb253af2008-12-30 17:56:55 +0000873
Daniel Willmanneea93372009-08-13 03:42:07 +0200874 DEBUGP(DMM, "-> MM INFO\n");
875
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800876 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000877}
878
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100879/*! Send an Authentication Request to MS on the given subscriber connection
880 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
881 * \param[in] conn Subscriber connection to send on.
882 * \param[in] rand Random challenge token to send, must be 16 bytes long.
883 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
884 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
885 * \param[in] key_seq auth tuple's sequence number.
886 */
887int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
888 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200889{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100890 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200892 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200893
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200894 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100895 if (autn)
896 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200897
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800898 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200899 gh->proto_discr = GSM48_PDISC_MM;
900 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
901
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100902 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200903
Harald Welte7984d5c2009-08-12 22:56:50 +0200904 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100905 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200906 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200907 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200908
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100909
910 /* 16 bytes AUTN */
911 if (autn)
912 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
913
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800914 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200915}
916
917/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800918int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200919{
920 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800921 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200922}
923
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100924/*
925 * At the 30C3 phones miss their periodic update
926 * interval a lot and then remain unreachable. In case
927 * we still know the TMSI we can just attach it again.
928 */
929static void implit_attach(struct gsm_subscriber_connection *conn)
930{
931 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
932 return;
933
934 subscr_update(conn->subscr, conn->bts,
935 GSM_SUBSCRIBER_UPDATE_ATTACHED);
936}
937
938
Sylvain Munautba87f452009-12-24 00:28:46 +0100939static int _gsm48_rx_mm_serv_req_sec_cb(
940 unsigned int hooknum, unsigned int event,
941 struct msgb *msg, void *data, void *param)
942{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800943 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100944 int rc = 0;
945
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200946 /* auth failed or succeeded, the timer was stopped */
947 conn->expire_timer_stopped = 1;
948
Sylvain Munautba87f452009-12-24 00:28:46 +0100949 switch (event) {
950 case GSM_SECURITY_AUTH_FAILED:
951 /* Nothing to do */
952 break;
953
954 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200955 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800956 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100957 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100958 break;
959
960 case GSM_SECURITY_SUCCEEDED:
961 /* nothing to do. CIPHER MODE COMMAND is
962 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100963 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100964 break;
965
966 default:
967 rc = -EINVAL;
968 };
969
970 return rc;
971}
972
Harald Welte4ed0e922009-01-10 03:17:30 +0000973/*
974 * Handle CM Service Requests
975 * a) Verify that the packet is long enough to contain the information
976 * we require otherwsie reject with INCORRECT_MESSAGE
977 * b) Try to parse the TMSI. If we do not have one reject
978 * c) Check that we know the subscriber with the TMSI otherwise reject
979 * with a HLR cause
980 * d) Set the subscriber on the gsm_lchan and accept
981 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800982static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000983{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200984 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200985 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000986
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200987 struct gsm_network *network = conn->network;
Harald Welteba4cf162009-01-10 01:49:35 +0000988 struct gsm_subscriber *subscr;
989 struct gsm48_hdr *gh = msgb_l3(msg);
990 struct gsm48_service_request *req =
991 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800992 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200993 uint8_t classmark2_len = gh->data[1];
994 uint8_t *classmark2 = gh->data+2;
995 uint8_t mi_len = *(classmark2 + classmark2_len);
996 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000997
Harald Weltec9e02182009-05-01 19:07:53 +0000998 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000999 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001000 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001001 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001002 GSM48_REJECT_INCORRECT_MESSAGE);
1003 }
1004
1005 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001006 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001007 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001008 GSM48_REJECT_INCORRECT_MESSAGE);
1009 }
1010
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001011 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001012 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001013
1014 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001015 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1016 req->cm_service_type, gsm48_mi_type_name(mi_type),
1017 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001018 subscr = subscr_get_by_imsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001019 mi_string);
1020 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001021 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1022 req->cm_service_type, gsm48_mi_type_name(mi_type),
1023 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001024 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001025 tmsi_from_string(mi_string));
1026 } else {
1027 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001028 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001029 GSM48_REJECT_INCORRECT_MESSAGE);
1030 }
1031
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001032 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001033
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001034 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +02001035 send_siemens_mrpci(msg->lchan, classmark2-1);
1036
Holger Freythereb443982009-06-04 13:58:42 +00001037
Harald Welte2a139372009-02-22 21:14:55 +00001038 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001039 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001041 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001042
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043 if (!conn->subscr)
1044 conn->subscr = subscr;
1045 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001046 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1047 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001048 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1049 subscr_put(subscr);
1050 }
1051
Harald Weltec2e302d2009-07-05 14:08:13 +02001052 subscr->equipment.classmark2_len = classmark2_len;
1053 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1054 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001055
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001056 /* we will send a MM message soon */
1057 conn->expire_timer_stopped = 1;
1058
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001060 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001061}
1062
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001063static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001064{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001065 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +00001066 struct gsm48_hdr *gh = msgb_l3(msg);
1067 struct gsm48_imsi_detach_ind *idi =
1068 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001069 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001070 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001071 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001072
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001073 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001074 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1075 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001076
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001077 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001078
Harald Welte2a139372009-02-22 21:14:55 +00001079 switch (mi_type) {
1080 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001081 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001082 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001083 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001084 break;
1085 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001086 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001087 subscr = subscr_get_by_imsi(network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001088 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001089 break;
1090 case GSM_MI_TYPE_IMEI:
1091 case GSM_MI_TYPE_IMEISV:
1092 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001093 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001094 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001095 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001096 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001097 break;
1098 }
1099
Holger Freyther4a49e772009-04-12 05:37:29 +00001100 if (subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001101 subscr_update(subscr, conn->bts,
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001102 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001103 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001104
1105 subscr->equipment.classmark1 = idi->classmark1;
1106 db_sync_equipment(&subscr->equipment);
1107
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001108 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001109 } else
Harald Welte2a139372009-02-22 21:14:55 +00001110 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1111
Harald Welte31981a02009-12-20 09:58:40 +01001112 /* FIXME: iterate over all transactions and release them,
1113 * imagine an IMSI DETACH happening during an active call! */
1114
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001115 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001116 return 0;
1117}
1118
Harald Welted2a7f5a2009-06-05 20:08:20 +00001119static int gsm48_rx_mm_status(struct msgb *msg)
1120{
1121 struct gsm48_hdr *gh = msgb_l3(msg);
1122
1123 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1124
1125 return 0;
1126}
1127
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001128static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
1129 struct gsm_subscriber_connection *conn,
1130 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
1133 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001134
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001135 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
1136 LOGP(DMM, LOGL_ERROR,
1137 "%s: MM AUTHENTICATION RESPONSE:"
1138 " l3 length invalid: %u\n",
1139 subscr_name(conn->subscr), msgb_l3len(msg));
1140 return -EINVAL;
1141 }
1142
1143 *res_len = sizeof(ar->sres);
1144 memcpy(res, ar->sres, sizeof(ar->sres));
1145 return 0;
1146}
1147
1148static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
1149 struct gsm_subscriber_connection *conn,
1150 struct msgb *msg)
1151{
1152 struct gsm48_hdr *gh;
1153 uint8_t *data;
1154 uint8_t iei;
1155 uint8_t ie_len;
1156 unsigned int data_len;
1157
1158 /* First parse the GSM part */
1159 if (parse_gsm_auth_resp(res, res_len, conn, msg))
1160 return -EINVAL;
1161 OSMO_ASSERT(*res_len == 4);
1162
1163 /* Then add the extended res part */
1164 gh = msgb_l3(msg);
1165 data = gh->data + sizeof(struct gsm48_auth_resp);
1166 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
1167
1168 if (data_len < 3) {
1169 LOGP(DMM, LOGL_ERROR,
1170 "%s: MM AUTHENTICATION RESPONSE:"
1171 " l3 length invalid: %u\n",
1172 subscr_name(conn->subscr), msgb_l3len(msg));
1173 return -EINVAL;
1174 }
1175
1176 iei = data[0];
1177 ie_len = data[1];
1178 if (iei != GSM48_IE_AUTH_RES_EXT) {
1179 LOGP(DMM, LOGL_ERROR,
1180 "%s: MM R99 AUTHENTICATION RESPONSE:"
1181 " expected IEI 0x%02x, got 0x%02x\n",
1182 subscr_name(conn->subscr),
1183 GSM48_IE_AUTH_RES_EXT, iei);
1184 return -EINVAL;
1185 }
1186
1187 if (ie_len > 12) {
1188 LOGP(DMM, LOGL_ERROR,
1189 "%s: MM R99 AUTHENTICATION RESPONSE:"
1190 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
1191 subscr_name(conn->subscr), GSM48_IE_AUTH_RES_EXT, ie_len);
1192 return -EINVAL;
1193 }
1194
1195 *res_len += ie_len;
1196 memcpy(res + 4, &data[2], ie_len);
1197 return 0;
1198}
1199
1200/* Chapter 9.2.3: Authentication Response */
1201static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
1202{
1203 struct gsm_network *net = conn->network;
1204 uint8_t res[16];
1205 uint8_t res_len;
1206 int rc;
1207 bool is_r99;
1208
1209 if (!conn->subscr) {
1210 LOGP(DMM, LOGL_ERROR,
1211 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
1212 gsm48_tx_mm_auth_rej(conn);
1213 release_security_operation(conn);
1214 return -EINVAL;
1215 }
1216
1217 if (msgb_l3len(msg) >
1218 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
1219 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
1220 is_r99 = true;
1221 } else {
1222 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
1223 is_r99 = false;
1224 }
1225
1226 if (rc) {
1227 gsm48_tx_mm_auth_rej(conn);
1228 release_security_operation(conn);
1229 return -EINVAL;
1230 }
1231
1232 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
1233 subscr_name(conn->subscr),
1234 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
1235 osmo_hexdump_nospc(res, res_len));
1236
1237 /* Future: vlr_sub_rx_auth_resp(conn->vsub, is_r99,
Neels Hofmeyrd3270a92017-02-02 01:07:28 +01001238 * conn->via_ran == RAN_UTRAN_IU,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001239 * res, res_len);
1240 */
1241
1242 if (res_len != 4) {
1243 LOGP(DMM, LOGL_ERROR,
1244 "%s: MM AUTHENTICATION RESPONSE:"
1245 " UMTS authentication not supported\n",
1246 subscr_name(conn->subscr));
1247 }
Sylvain Munaut30a15382009-12-24 00:27:26 +01001248
1249 /* Safety check */
1250 if (!conn->sec_operation) {
1251 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1252 return -EIO;
1253 }
1254
1255 /* Validate SRES */
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001256 if (memcmp(conn->sec_operation->atuple.vec.sres, res, 4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001257 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001258 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001259
1260 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001261 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001262
Sylvain Munaut30a15382009-12-24 00:27:26 +01001263 if (cb)
1264 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001265 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001266
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001267 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001268 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001269 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001270 }
1271
Sylvain Munautc593cf12010-06-10 23:51:41 +02001272 DEBUGPC(DMM, "OK\n");
1273
Sylvain Munaut30a15382009-12-24 00:27:26 +01001274 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001275 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001276 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001277}
1278
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001279static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1280{
1281 struct gsm48_hdr *gh = msgb_l3(msg);
1282 uint8_t cause;
1283 uint8_t auts_tag;
1284 uint8_t auts_len;
1285 uint8_t *auts;
1286 int rc;
1287
1288 if (!conn->sec_operation) {
1289 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION FAILURE:"
1290 " No authentication/cipher operation in progress\n",
1291 subscr_name(conn->subscr));
1292 return -EINVAL;
1293 }
1294
1295 if (!conn->subscr) {
1296 LOGP(DMM, LOGL_ERROR,
1297 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
1298 gsm48_tx_mm_auth_rej(conn);
1299 release_security_operation(conn);
1300 return -EINVAL;
1301 }
1302
1303 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1304 LOGP(DMM, LOGL_ERROR,
1305 "%s: MM R99 AUTHENTICATION FAILURE:"
1306 " l3 length invalid: %u\n",
1307 subscr_name(conn->subscr), msgb_l3len(msg));
1308 gsm48_tx_mm_auth_rej(conn);
1309 release_security_operation(conn);
1310 return -EINVAL;
1311 }
1312
1313 cause = gh->data[0];
1314
1315 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1316 LOGP(DMM, LOGL_INFO,
1317 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
1318 subscr_name(conn->subscr), cause);
1319 rc = gsm48_tx_mm_auth_rej(conn);
1320 release_security_operation(conn);
1321 return rc;
1322 }
1323
1324 /* This is a Synch Failure procedure, which should pass an AUTS to
1325 * resynchronize the sequence nr with the HLR. Expecting exactly one
1326 * TLV with 14 bytes of AUTS. */
1327
1328 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1329 LOGP(DMM, LOGL_INFO,
1330 "%s: MM R99 AUTHENTICATION FAILURE:"
1331 " invalid Synch Failure: missing AUTS IE\n",
1332 subscr_name(conn->subscr));
1333 gsm48_tx_mm_auth_rej(conn);
1334 release_security_operation(conn);
1335 return -EINVAL;
1336 }
1337
1338 auts_tag = gh->data[1];
1339 auts_len = gh->data[2];
1340 auts = &gh->data[3];
1341
1342 if (auts_tag != GSM48_IE_AUTS
1343 || auts_len != 14) {
1344 LOGP(DMM, LOGL_INFO,
1345 "%s: MM R99 AUTHENTICATION FAILURE:"
1346 " invalid Synch Failure:"
1347 " expected AUTS IE 0x%02x of 14 bytes,"
1348 " got IE 0x%02x of %u bytes\n",
1349 subscr_name(conn->subscr),
1350 GSM48_IE_AUTS, auts_tag, auts_len);
1351 gsm48_tx_mm_auth_rej(conn);
1352 release_security_operation(conn);
1353 return -EINVAL;
1354 }
1355
1356 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1357 LOGP(DMM, LOGL_INFO,
1358 "%s: MM R99 AUTHENTICATION FAILURE:"
1359 " invalid Synch Failure msg: message truncated (%u)\n",
1360 subscr_name(conn->subscr), msgb_l3len(msg));
1361 gsm48_tx_mm_auth_rej(conn);
1362 release_security_operation(conn);
1363 return -EINVAL;
1364 }
1365
1366 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1367 * large enough. */
1368
1369 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
1370 subscr_name(conn->subscr), osmo_hexdump_nospc(auts, 14));
1371
1372 /* Future: vlr_sub_rx_auth_fail(conn->vsub, auts); */
1373
1374 LOGP(DMM, LOGL_ERROR, "%s: MM R99 AUTHENTICATION not supported\n",
1375 subscr_name(conn->subscr));
1376 rc = gsm48_tx_mm_auth_rej(conn);
1377 release_security_operation(conn);
1378 return rc;
1379}
1380
Harald Weltebf5e8df2009-02-03 12:59:45 +00001381/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001382static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001383{
1384 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001386
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001387 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001388 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001389 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001390 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001391 break;
1392 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001393 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001394 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001395 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001396 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001397 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001398 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001399 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001400 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001401 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001402 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001403 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001404 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001405 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001406 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001407 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001408 break;
1409 case GSM48_MT_MM_CM_REEST_REQ:
1410 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1411 break;
1412 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001413 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001414 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001415 case GSM48_MT_MM_AUTH_FAIL:
1416 rc = gsm48_rx_mm_auth_fail(conn, msg);
1417 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001418 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001419 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001420 gh->msg_type);
1421 break;
1422 }
1423
1424 return rc;
1425}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001426
Harald Welte2d35ae62009-02-06 12:02:13 +00001427/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001428static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001429{
1430 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001431 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001432 uint8_t *classmark2_lv = gh->data + 1;
1433 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001434 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 struct gsm_subscriber *subscr = NULL;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001436 struct bsc_subscr *bsub;
1437 uint32_t tmsi;
Harald Welte2d35ae62009-02-06 12:02:13 +00001438 int rc = 0;
1439
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001440 resp = (struct gsm48_pag_resp *) &gh->data[0];
1441 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1442 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001443 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1444 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001445
Harald Weltefe18d8f2009-02-22 21:14:24 +00001446 switch (mi_type) {
1447 case GSM_MI_TYPE_TMSI:
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001448 tmsi = tmsi_from_string(mi_string);
1449 subscr = subscr_get_by_tmsi(conn->network->subscr_group, tmsi);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001450 break;
1451 case GSM_MI_TYPE_IMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001452 subscr = subscr_get_by_imsi(conn->network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001453 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001454 break;
1455 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001456
1457 if (!subscr) {
1458 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001459 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001460 return -EINVAL;
1461 }
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001462
1463 if (!conn->subscr) {
1464 conn->subscr = subscr;
1465 } else if (conn->subscr != subscr) {
1466 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
1467 subscr_put(subscr);
1468 return -EINVAL;
1469 } else {
1470 DEBUGP(DRR, "<- Channel already owned by us\n");
1471 subscr_put(subscr);
1472 subscr = conn->subscr;
1473 }
1474
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01001475 log_set_context(LOG_CTX_VLR_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001476 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001477 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001478
Harald Weltec2e302d2009-07-05 14:08:13 +02001479 subscr->equipment.classmark2_len = *classmark2_lv;
1480 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1481 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001482
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001483 /* TODO MSC split -- creating a BSC subscriber directly from MSC data
1484 * structures in RAM. At some point the MSC will send a message to the
1485 * BSC instead. */
1486 bsub = bsc_subscr_find_or_create_by_imsi(conn->network->bsc_subscribers,
1487 subscr->imsi);
1488 bsub->tmsi = subscr->tmsi;
1489 bsub->lac = subscr->lac;
1490
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001491 /* We received a paging */
1492 conn->expire_timer_stopped = 1;
1493
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001494 rc = gsm48_handle_paging_resp(conn, msg, bsub);
Harald Welte2d35ae62009-02-06 12:02:13 +00001495 return rc;
1496}
1497
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001498static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001499{
1500 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001501 uint8_t apdu_id_flags;
1502 uint8_t apdu_len;
1503 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001504
1505 apdu_id_flags = gh->data[0];
1506 apdu_len = gh->data[1];
1507 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001508
Max8dc8f232017-02-09 19:13:02 +01001509 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001510 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001511
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001512 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001513}
1514
Harald Weltebf5e8df2009-02-03 12:59:45 +00001515/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001516static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001517{
1518 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001519 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001520
1521 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001522 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001523 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001524 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001525 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001526 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001527 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001528 default:
Philippd37981e2016-10-10 15:11:41 +02001529 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1530 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001531 break;
1532 }
1533
Harald Welte2d35ae62009-02-06 12:02:13 +00001534 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001535}
1536
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001537int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1538 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001539{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001540 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001541 struct gsm48_hdr *gh;
1542
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001543 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001544
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001545 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1546 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001547
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001548 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1549 gh->proto_discr = GSM48_PDISC_RR;
1550 gh->msg_type = GSM48_MT_RR_APP_INFO;
1551 gh->data[0] = apdu_id;
1552 gh->data[1] = apdu_len;
1553 memcpy(gh->data+2, apdu, apdu_len);
1554
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001555 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001556}
1557
Alexander Couzens92f552f2016-08-23 07:32:27 +02001558/* FIXME: this count_statistics is a state machine behaviour. we should convert
1559 * the complete call control into a state machine. Afterwards we can move this
1560 * code into state transitions.
1561 */
1562static void count_statistics(struct gsm_trans *trans, int new_state)
1563{
1564 int old_state = trans->cc.state;
1565 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1566
1567 if (old_state == new_state)
1568 return;
1569
1570 /* state incoming */
1571 switch (new_state) {
1572 case GSM_CSTATE_ACTIVE:
1573 osmo_counter_inc(trans->net->active_calls);
1574 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1575 break;
1576 }
1577
1578 /* state outgoing */
1579 switch (old_state) {
1580 case GSM_CSTATE_ACTIVE:
1581 osmo_counter_dec(trans->net->active_calls);
1582 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1583 new_state == GSM_CSTATE_DISCONNECT_IND)
1584 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1585 else
1586 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1587 break;
1588 }
1589}
1590
Harald Welte4bc90a12008-12-27 16:32:52 +00001591/* Call Control */
1592
Harald Welte7584aea2009-02-11 11:44:12 +00001593/* The entire call control code is written in accordance with Figure 7.10c
1594 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1595 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1596 * it for voice */
1597
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598static void new_cc_state(struct gsm_trans *trans, int state)
1599{
1600 if (state > 31 || state < 0)
1601 return;
1602
1603 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001604 gsm48_cc_state_name(trans->cc.state),
1605 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606
Alexander Couzens92f552f2016-08-23 07:32:27 +02001607 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001608 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001609}
1610
1611static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001612{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001613 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001614 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001615 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001616
Harald Welte4bc90a12008-12-27 16:32:52 +00001617 gh->msg_type = GSM48_MT_CC_STATUS;
1618
1619 cause = msgb_put(msg, 3);
1620 cause[0] = 2;
1621 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1622 cause[2] = 0x80 | 30; /* response to status inquiry */
1623
1624 call_state = msgb_put(msg, 1);
1625 call_state[0] = 0xc0 | 0x00;
1626
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001627 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001628}
1629
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001630static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001631 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001632{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001633 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001634 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1635
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001636 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001637
Harald Welte6f4b7532008-12-29 00:39:37 +00001638 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001639 gh->msg_type = msg_type;
1640
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001641 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001642}
1643
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1645{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001646 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001647 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001648 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001649 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 }
1651}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001652
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1654 int msg_type, struct gsm_mncc *mncc)
1655{
1656 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001657 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658
1659 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001660 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001661 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001663 trans->conn->lchan->ts->trx->bts->nr,
1664 trans->conn->lchan->ts->trx->nr,
1665 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 (trans->subscr)?(trans->subscr->extension):"-",
1667 get_mncc_name(msg_type));
1668 else
1669 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1670 "Sending '%s' to MNCC.\n",
1671 (trans->subscr)?(trans->subscr->extension):"-",
1672 get_mncc_name(msg_type));
1673 else
1674 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1675 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1676
1677 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001678
Harald Welte966636f2009-06-26 19:39:35 +02001679 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 if (!msg)
1681 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001682
1683 data = msgb_put(msg, sizeof(struct gsm_mncc));
1684 memcpy(data, mncc, sizeof(struct gsm_mncc));
1685
Harald Welte54209c22010-12-22 23:43:29 +01001686 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001687
1688 return 0;
1689}
1690
1691int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001692 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693{
1694 struct gsm_mncc rel;
1695
Harald Welte92f70c52009-06-12 01:54:08 +08001696 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001698 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001699 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1700 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1702}
1703
Harald Weltedcaf5652009-07-23 18:56:43 +02001704/* Call Control Specific transaction release.
1705 * gets called by trans_free, DO NOT CALL YOURSELF! */
1706void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 gsm48_stop_cc_timer(trans);
1709
1710 /* send release to L4, if callref still exists */
1711 if (trans->callref) {
1712 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001713 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001714 GSM48_CAUSE_LOC_PRN_S_LU,
1715 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001717 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001719 if (trans->conn)
1720 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721}
1722
1723static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001724
Harald Welte09e38af2009-02-16 22:52:23 +00001725/* call-back from paging the B-end of the connection */
1726static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001727 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001728{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001729 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001730 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001731
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001732 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001735 switch (event) {
1736 case GSM_PAGING_SUCCEEDED:
1737 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001738 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001739 /* Assign lchan */
1740 transt->conn = conn;
1741 /* send SETUP request to called party */
1742 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1743 break;
1744 case GSM_PAGING_EXPIRED:
1745 case GSM_PAGING_BUSY:
1746 DEBUGP(DCC, "Paging subscr %s expired!\n",
1747 transt->subscr->extension);
1748 /* Temporarily out of order */
1749 mncc_release_ind(transt->net, transt,
1750 transt->callref,
1751 GSM48_CAUSE_LOC_PRN_S_LU,
1752 GSM48_CC_CAUSE_DEST_OOO);
1753 transt->callref = 0;
1754 transt->paging_request = NULL;
1755 trans_free(transt);
1756 break;
1757 default:
1758 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1759 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001761
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001762 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001763 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001764}
Harald Welte7584aea2009-02-11 11:44:12 +00001765
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001766static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001767
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001768/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001769static int switch_for_handover(struct gsm_lchan *old_lchan,
1770 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001771{
1772 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001773
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001774 /* Ask the new socket to send to the already known port. */
1775 if (new_lchan->conn->mncc_rtp_bridge) {
1776 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1777 rsl_ipacc_mdcx(new_lchan,
1778 old_lchan->abis_ip.connect_ip,
1779 old_lchan->abis_ip.connect_port, 0);
1780 return 0;
1781 }
1782
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001783 if (ipacc_rtp_direct) {
1784 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1785 return 0;
1786 }
1787
1788 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001789 new_rs = new_lchan->abis_ip.rtp_socket;
1790 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001791
1792 if (!new_rs) {
1793 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1794 return -EIO;
1795 }
1796
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001797 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001798
1799 if (!old_rs) {
1800 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1801 return -EIO;
1802 }
1803
1804 /* copy rx_action and reference to other sock */
1805 new_rs->rx_action = old_rs->rx_action;
1806 new_rs->tx_action = old_rs->tx_action;
1807 new_rs->transmit = old_rs->transmit;
1808
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001809 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001810 case RTP_PROXY:
1811 other_rs = old_rs->proxy.other_sock;
1812 rtp_socket_proxy(new_rs, other_rs);
1813 /* delete reference to other end socket to prevent
1814 * rtp_socket_free() from removing the inverse reference */
1815 old_rs->proxy.other_sock = NULL;
1816 break;
1817 case RTP_RECV_UPSTREAM:
1818 new_rs->receive = old_rs->receive;
1819 break;
1820 case RTP_NONE:
1821 break;
1822 }
1823
1824 return 0;
1825}
1826
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001827static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1828{
1829 struct gsm_lchan *old_lchan;
1830 old_lchan = bsc_handover_pending(lchan);
1831 if (old_lchan)
1832 switch_for_handover(old_lchan, lchan);
1833}
1834
Harald Welte805f6442009-07-28 18:25:29 +02001835/* some other part of the code sends us a signal */
1836static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1837 void *handler_data, void *signal_data)
1838{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001839 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001840 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001841 struct gsm_network *net;
1842 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001843
1844 if (subsys != SS_ABISIP)
1845 return 0;
1846
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001847 /* RTP bridge handling */
1848 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1849 return tch_rtp_signal(lchan, signal);
1850
Harald Welte805f6442009-07-28 18:25:29 +02001851 /* in case we use direct BTS-to-BTS RTP */
1852 if (ipacc_rtp_direct)
1853 return 0;
1854
Harald Welte805f6442009-07-28 18:25:29 +02001855 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001856 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001857 /* in case we don't use direct BTS-to-BTS RTP */
1858 /* the BTS has successfully bound a TCH to a local ip/port,
1859 * which means we can connect our UDP socket to it */
1860 if (lchan->abis_ip.rtp_socket) {
1861 rtp_socket_free(lchan->abis_ip.rtp_socket);
1862 lchan->abis_ip.rtp_socket = NULL;
1863 }
1864
1865 lchan->abis_ip.rtp_socket = rtp_socket_create();
1866 if (!lchan->abis_ip.rtp_socket)
1867 return -EIO;
1868
1869 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1870 lchan->abis_ip.bound_ip,
1871 lchan->abis_ip.bound_port);
1872 if (rc < 0)
1873 return -EIO;
1874
Harald Welteda7ab742009-12-19 22:23:05 +01001875 /* check if any transactions on this lchan still have
1876 * a tch_recv_mncc request pending */
1877 net = lchan->ts->trx->bts->network;
1878 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001879 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001880 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1881 tch_recv_mncc(net, trans->callref, 1);
1882 }
1883 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001884
1885 /*
1886 * TODO: this appears to be too early? Why not until after
1887 * the handover detect or the handover complete?
1888 *
1889 * Do we have a handover pending for this new lchan? In that
1890 * case re-route the audio from the old channel to the new one.
1891 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001892 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001893 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001894 case S_ABISIP_DLCX_IND:
1895 /* the BTS tells us a RTP stream has been disconnected */
1896 if (lchan->abis_ip.rtp_socket) {
1897 rtp_socket_free(lchan->abis_ip.rtp_socket);
1898 lchan->abis_ip.rtp_socket = NULL;
1899 }
1900
1901 break;
Harald Welte805f6442009-07-28 18:25:29 +02001902 }
1903
1904 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001905}
1906
Harald Welte49f48b82009-02-17 15:29:33 +00001907/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001908static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001909{
Harald Welte11fa29c2009-02-19 17:24:39 +00001910 struct gsm_bts *bts = lchan->ts->trx->bts;
1911 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001912 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001913 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001914 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001915
Max8db12e42016-04-18 23:11:18 +02001916 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1917 "(bts=%u,trx=%u,ts=%u,%s)\n",
1918 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1919 get_value_string(gsm_chan_t_names, lt),
1920 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1921 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001922
1923 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001924 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001925 return -EINVAL;
1926 }
Harald Welteda7ab742009-12-19 22:23:05 +01001927
Max3ffce192016-04-25 15:22:00 +02001928 if (lt != rt) {
1929 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1930 " channel types: local = %s, remote = %s\n",
1931 get_value_string(gsm_chan_t_names, lt),
1932 get_value_string(gsm_chan_t_names, rt));
1933 return -EBADSLT;
1934 }
1935
1936 if (lm != rm) {
1937 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1938 " channel modes: local = %s, remote = %s\n",
1939 get_value_string(gsm48_chan_mode_names, lm),
1940 get_value_string(gsm48_chan_mode_names, rm));
1941 return -EMEDIUMTYPE;
1942 }
1943
Harald Welteda7ab742009-12-19 22:23:05 +01001944 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001945 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001946 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001947 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001948 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001949 if (!lchan->abis_ip.rtp_socket) {
1950 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1951 "lchan\n");
1952 return -EIO;
1953 }
1954 if (!remote_lchan->abis_ip.rtp_socket) {
1955 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1956 "remote_lchan\n");
1957 return -EIO;
1958 }
1959
Harald Welte805f6442009-07-28 18:25:29 +02001960 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001961 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001962 if (rc < 0)
1963 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001964 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001965 if (rc < 0)
1966 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001967 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301968 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1969 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001970 } else {
1971 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301972 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1973 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301974 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001975 if (rc < 0)
1976 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301977 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1978 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301979 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001980 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001981 break;
1982 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001983 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001984 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001985 trau_mux_map_lchan(lchan, remote_lchan);
1986 break;
1987 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001988 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001989 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001990 }
Harald Welte49f48b82009-02-17 15:29:33 +00001991
1992 return 0;
1993}
1994
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001996static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001997{
Harald Welte53d51f52015-12-03 14:59:04 +01001998 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1999 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002000
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002002 return -EIO;
2003
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002004 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 return -EIO;
2006
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002007 /* Which subscriber do we want to track trans1 or trans2? */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01002008 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002009
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002011 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002012}
2013
Harald Welteda7ab742009-12-19 22:23:05 +01002014/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002015static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016{
2017 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01002018 struct gsm_lchan *lchan;
2019 struct gsm_bts *bts;
2020 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00002021
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01002023 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 if (!trans)
2025 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002026 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002028
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01002029 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002030 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01002031 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002033 /* store receive state */
2034 trans->tch_recv = enable;
2035
Harald Welteda7ab742009-12-19 22:23:05 +01002036 switch (bts->type) {
2037 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002038 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01002039 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02002040 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002041 return -EINVAL;
2042 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002043 /* In case, we don't have a RTP socket to the BTS yet, the BTS
2044 * will not be connected to our RTP proxy and the socket will
2045 * not be assigned to the application interface. This method
2046 * will be called again, once the audio socket is created and
2047 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01002048 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01002049 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2050 return 0;
2051 }
2052 if (enable) {
2053 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01002054 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01002055 if (rc < 0)
2056 return rc;
2057 /* assign socket to application interface */
2058 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2059 net, callref);
2060 } else
2061 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2062 net, 0);
2063 break;
2064 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002065 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002066 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002067 /* In case we don't have a TCH with correct mode, the TRAU muxer
2068 * will not be asigned to the application interface. This is
2069 * performed by switch_trau_mux() after successful handover or
2070 * assignment. */
2071 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
2072 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2073 return 0;
2074 }
Harald Welteda7ab742009-12-19 22:23:05 +01002075 if (enable)
2076 return trau_recv_lchan(lchan, callref);
2077 return trau_mux_unmap(NULL, callref);
2078 break;
2079 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002080 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002081 return -EINVAL;
2082 }
2083
2084 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085}
2086
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2088{
2089 DEBUGP(DCC, "-> STATUS ENQ\n");
2090 return gsm48_cc_tx_status(trans, msg);
2091}
2092
2093static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2094static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2095
2096static void gsm48_cc_timeout(void *arg)
2097{
2098 struct gsm_trans *trans = arg;
2099 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002100 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2101 int mo_location = GSM48_CAUSE_LOC_USER;
2102 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2103 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 struct gsm_mncc mo_rel, l4_rel;
2105
2106 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2107 mo_rel.callref = trans->callref;
2108 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2109 l4_rel.callref = trans->callref;
2110
Harald Weltedcaf5652009-07-23 18:56:43 +02002111 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 case 0x303:
2113 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002114 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 break;
2116 case 0x310:
2117 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002118 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 break;
2120 case 0x313:
2121 disconnect = 1;
2122 /* unknown, did not find it in the specs */
2123 break;
2124 case 0x301:
2125 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002126 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 break;
2128 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002129 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002131 gsm48_cc_tx_release(trans, &trans->cc.msg);
2132 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133 break; /* stay in release state */
2134 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002135 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 return;
2137// release = 1;
2138// l4_cause = 14;
2139// break;
2140 case 0x306:
2141 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002142 mo_cause = trans->cc.msg.cause.value;
2143 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 break;
2145 case 0x323:
2146 disconnect = 1;
2147 break;
2148 default:
2149 release = 1;
2150 }
2151
2152 if (release && trans->callref) {
2153 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002154 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 l4_location, l4_cause);
2156 trans->callref = 0;
2157 }
2158
2159 if (disconnect && trans->callref) {
2160 /* process disconnect towards layer 4 */
2161 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002162 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 }
2164
2165 /* process disconnect towards mobile station */
2166 if (disconnect || release) {
2167 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002168 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2169 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2170 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 mo_rel.cause.diag_len = 3;
2172
2173 if (disconnect)
2174 gsm48_cc_tx_disconnect(trans, &mo_rel);
2175 if (release)
2176 gsm48_cc_tx_release(trans, &mo_rel);
2177 }
2178
2179}
2180
Max3ffce192016-04-25 15:22:00 +02002181/* disconnect both calls from the bridge */
2182static inline void disconnect_bridge(struct gsm_network *net,
2183 struct gsm_mncc_bridge *bridge, int err)
2184{
2185 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
2186 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
2187 struct gsm_mncc mx_rel;
2188 if (!trans0 || !trans1)
2189 return;
2190
2191 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
2192 trans0->callref, trans1->callref, strerror(err));
2193
2194 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
2195 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
2196 GSM48_CC_CAUSE_CHAN_UNACCEPT);
2197
2198 mx_rel.callref = trans0->callref;
2199 gsm48_cc_tx_disconnect(trans0, &mx_rel);
2200
2201 mx_rel.callref = trans1->callref;
2202 gsm48_cc_tx_disconnect(trans1, &mx_rel);
2203}
2204
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2206 int sec, int micro)
2207{
2208 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002209 trans->cc.timer.cb = gsm48_cc_timeout;
2210 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02002211 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02002212 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213}
2214
2215static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2216{
2217 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002218 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2220 struct tlv_parsed tp;
2221 struct gsm_mncc setup;
2222
2223 memset(&setup, 0, sizeof(struct gsm_mncc));
2224 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002225 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002226 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 /* emergency setup is identified by msg_type */
2228 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2229 setup.emergency = 1;
2230
2231 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002232 setup.fields |= MNCC_F_CALLING;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002233 osmo_strlcpy(setup.calling.number, trans->subscr->extension,
2234 sizeof(setup.calling.number));
2235 osmo_strlcpy(setup.imsi, trans->subscr->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002236
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* bearer capability */
2238 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2239 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002240 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002242 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 }
2244 /* facility */
2245 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2246 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002247 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2249 }
2250 /* called party bcd number */
2251 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2252 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01002253 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2255 }
2256 /* user-user */
2257 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2258 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2261 }
2262 /* ss-version */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2264 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2267 }
2268 /* CLIR suppression */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2270 setup.clir.sup = 1;
2271 /* CLIR invocation */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2273 setup.clir.inv = 1;
2274 /* cc cap */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2276 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2279 }
2280
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 new_cc_state(trans, GSM_CSTATE_INITIATED);
2282
Harald Welte (local)47df3992009-12-26 19:45:03 +01002283 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2284 subscr_name(trans->subscr), trans->subscr->extension,
2285 setup.called.number);
2286
Alexander Couzensb847a212016-08-02 11:34:11 +02002287 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002288
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002290 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291
Harald Welte13cac662009-07-29 12:10:35 +02002292 /* MNCC code will modify the channel asynchronously, we should
2293 * ipaccess-bind only after the modification has been made to the
2294 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 return 0;
2296}
2297
2298static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002299{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002300 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002301 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002303 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002304
Harald Welte7ccf7782009-02-17 01:43:01 +00002305 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002306
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 /* transaction id must not be assigned */
2308 if (trans->transaction_id != 0xff) { /* unasssigned */
2309 DEBUGP(DCC, "TX Setup with assigned transaction. "
2310 "This is not allowed!\n");
2311 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002312 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002313 GSM48_CAUSE_LOC_PRN_S_LU,
2314 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002316 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317 return rc;
2318 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002319
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002321 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2322 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002323 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002325 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002326 GSM48_CAUSE_LOC_PRN_S_LU,
2327 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002329 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 return rc;
2331 }
Harald Welte78283ef2009-07-23 21:36:44 +02002332 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002333
Harald Welte65e74cc2008-12-29 01:55:35 +00002334 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002335
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002337
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 /* bearer capability */
2339 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002340 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 /* facility */
2342 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 /* progress */
2345 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002346 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 /* calling party BCD number */
2348 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002349 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 /* called party BCD number */
2351 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002352 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 /* user-user */
2354 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002355 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356 /* redirecting party BCD number */
2357 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 /* signal */
2360 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002361 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002362
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002364
Alexander Couzensb847a212016-08-02 11:34:11 +02002365 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002366
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002367 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002368}
2369
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2371{
2372 struct gsm48_hdr *gh = msgb_l3(msg);
2373 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2374 struct tlv_parsed tp;
2375 struct gsm_mncc call_conf;
2376
2377 gsm48_stop_cc_timer(trans);
2378 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2379
2380 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2381 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002382 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002383 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384#if 0
2385 /* repeat */
2386 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2387 call_conf.repeat = 1;
2388 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2389 call_conf.repeat = 2;
2390#endif
2391 /* bearer capability */
2392 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2393 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002394 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002396 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397 }
2398 /* cause */
2399 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2400 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002401 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2403 }
2404 /* cc cap */
2405 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2406 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002407 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2409 }
2410
Andreas Eversberg035b8742013-09-19 09:27:06 +02002411 /* IMSI of called subscriber */
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002412 osmo_strlcpy(call_conf.imsi, trans->subscr->imsi,
2413 sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02002414
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2416
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002417 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002418 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419}
2420
2421static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2422{
2423 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002424 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2426
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2428
2429 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2430
2431 /* bearer capability */
2432 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002433 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 /* facility */
2435 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002436 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 /* progress */
2438 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002441 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2445{
2446 struct gsm48_hdr *gh = msgb_l3(msg);
2447 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2448 struct tlv_parsed tp;
2449 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002450
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 gsm48_stop_cc_timer(trans);
2452 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2453
2454 memset(&alerting, 0, sizeof(struct gsm_mncc));
2455 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002456 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 /* facility */
2458 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2459 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002460 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2462 }
2463
2464 /* progress */
2465 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2466 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002467 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2469 }
2470 /* ss-version */
2471 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2472 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002473 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2475 }
2476
2477 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2478
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002479 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002480 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2484{
2485 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002486 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2488
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 gh->msg_type = GSM48_MT_CC_ALERTING;
2490
2491 /* facility */
2492 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002493 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494 /* progress */
2495 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002496 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 /* user-user */
2498 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002499 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500
2501 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002502
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002503 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2507{
2508 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002509 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2511
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512 gh->msg_type = GSM48_MT_CC_PROGRESS;
2513
2514 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002515 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516 /* user-user */
2517 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002518 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002520 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521}
2522
2523static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2524{
2525 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002526 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2528
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 gh->msg_type = GSM48_MT_CC_CONNECT;
2530
2531 gsm48_stop_cc_timer(trans);
2532 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2533
2534 /* facility */
2535 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002536 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 /* progress */
2538 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002539 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 /* connected number */
2541 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002542 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 /* user-user */
2544 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002545 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546
2547 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2548
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002549 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550}
2551
2552static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2553{
2554 struct gsm48_hdr *gh = msgb_l3(msg);
2555 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2556 struct tlv_parsed tp;
2557 struct gsm_mncc connect;
2558
2559 gsm48_stop_cc_timer(trans);
2560
2561 memset(&connect, 0, sizeof(struct gsm_mncc));
2562 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002563 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002565 connect.fields |= MNCC_F_CONNECTED;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002566 osmo_strlcpy(connect.connected.number, trans->subscr->extension,
2567 sizeof(connect.connected.number));
2568 osmo_strlcpy(connect.imsi, trans->subscr->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002569
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 /* facility */
2571 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2572 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002573 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2575 }
2576 /* user-user */
2577 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2578 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002579 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2581 }
2582 /* ss-version */
2583 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2584 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002585 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2587 }
2588
2589 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002590 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002592 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593}
2594
2595
2596static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2597{
2598 struct gsm_mncc connect_ack;
2599
2600 gsm48_stop_cc_timer(trans);
2601
2602 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002603 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002604
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2606 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002607
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002608 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 &connect_ack);
2610}
2611
2612static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2613{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002614 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2616
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2618
2619 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2620
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002621 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622}
2623
2624static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2625{
2626 struct gsm48_hdr *gh = msgb_l3(msg);
2627 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2628 struct tlv_parsed tp;
2629 struct gsm_mncc disc;
2630
2631 gsm48_stop_cc_timer(trans);
2632
2633 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2634
2635 memset(&disc, 0, sizeof(struct gsm_mncc));
2636 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002637 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 /* cause */
2639 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2640 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002641 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2643 }
2644 /* facility */
2645 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2646 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002647 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2649 }
2650 /* user-user */
2651 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2652 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002653 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2655 }
2656 /* ss-version */
2657 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2658 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002659 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2661 }
2662
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002663 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664
2665}
2666
Harald Weltec66b71c2009-06-11 14:23:20 +08002667static struct gsm_mncc_cause default_cause = {
2668 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2669 .coding = 0,
2670 .rec = 0,
2671 .rec_val = 0,
2672 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2673 .diag_len = 0,
2674 .diag = { 0 },
2675};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676
2677static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2678{
2679 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002680 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2682
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2684
2685 gsm48_stop_cc_timer(trans);
2686 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2687
2688 /* cause */
2689 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002690 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 else
Harald Welte55c8f352010-03-07 23:40:35 +01002692 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693
2694 /* facility */
2695 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002696 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 /* progress */
2698 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002699 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 /* user-user */
2701 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002702 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703
2704 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002705 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706
2707 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2708
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002709 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710}
2711
2712static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2713{
2714 struct gsm48_hdr *gh = msgb_l3(msg);
2715 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2716 struct tlv_parsed tp;
2717 struct gsm_mncc rel;
2718 int rc;
2719
2720 gsm48_stop_cc_timer(trans);
2721
2722 memset(&rel, 0, sizeof(struct gsm_mncc));
2723 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002724 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725 /* cause */
2726 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2727 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002728 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2730 }
2731 /* facility */
2732 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2733 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002734 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2736 }
2737 /* user-user */
2738 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2739 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002740 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2742 }
2743 /* ss-version */
2744 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2745 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002746 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2748 }
2749
Harald Weltedcaf5652009-07-23 18:56:43 +02002750 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002752 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002754 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002755 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002756 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002757 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758 }
2759
2760 new_cc_state(trans, GSM_CSTATE_NULL);
2761
2762 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002763 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764
2765 return rc;
2766}
2767
2768static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002771 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_RELEASE;
2775
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 gsm48_stop_cc_timer(trans);
2777 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2778
2779 /* cause */
2780 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002781 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782 /* facility */
2783 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002784 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002785 /* user-user */
2786 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002787 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788
Harald Weltedcaf5652009-07-23 18:56:43 +02002789 trans->cc.T308_second = 0;
2790 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791
Harald Weltedcaf5652009-07-23 18:56:43 +02002792 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2794
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002795 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796}
2797
2798static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2799{
2800 struct gsm48_hdr *gh = msgb_l3(msg);
2801 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2802 struct tlv_parsed tp;
2803 struct gsm_mncc rel;
2804 int rc = 0;
2805
2806 gsm48_stop_cc_timer(trans);
2807
2808 memset(&rel, 0, sizeof(struct gsm_mncc));
2809 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002810 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811 /* cause */
2812 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2813 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002814 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2816 }
2817 /* facility */
2818 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2819 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002820 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2822 }
2823 /* user-user */
2824 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2825 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002826 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2828 }
2829 /* ss-version */
2830 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2831 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002832 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2834 }
2835
2836 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002837 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002839 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840 MNCC_REJ_IND, &rel);
2841 break;
2842 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002843 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 MNCC_REL_CNF, &rel);
2845 break;
2846 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002847 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848 MNCC_REL_IND, &rel);
2849 }
2850 }
2851
2852 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002853 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854
2855 return rc;
2856}
2857
2858static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2859{
2860 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002861 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002863 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2866
2867 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002868
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869 gsm48_stop_cc_timer(trans);
2870
2871 /* cause */
2872 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002873 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874 /* facility */
2875 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002876 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877 /* user-user */
2878 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002879 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880
Harald Weltec7782de2010-12-21 19:31:41 +01002881 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2882
Harald Weltedcaf5652009-07-23 18:56:43 +02002883 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884
Harald Weltec7782de2010-12-21 19:31:41 +01002885 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886}
2887
2888static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2889{
2890 struct gsm48_hdr *gh = msgb_l3(msg);
2891 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2892 struct tlv_parsed tp;
2893 struct gsm_mncc fac;
2894
2895 memset(&fac, 0, sizeof(struct gsm_mncc));
2896 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002897 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898 /* facility */
2899 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2900 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002901 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2903 }
2904 /* ss-version */
2905 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2906 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002907 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2909 }
2910
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002911 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912}
2913
2914static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2915{
2916 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002917 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2919
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920 gh->msg_type = GSM48_MT_CC_FACILITY;
2921
2922 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002923 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002925 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926}
2927
2928static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2929{
2930 struct gsm_mncc hold;
2931
2932 memset(&hold, 0, sizeof(struct gsm_mncc));
2933 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002934 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935}
2936
2937static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2938{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002939 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2941
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2943
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002944 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002945}
2946
2947static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2948{
2949 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002950 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2952
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2954
2955 /* cause */
2956 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002957 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958 else
Harald Welte55c8f352010-03-07 23:40:35 +01002959 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002961 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962}
2963
2964static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2965{
2966 struct gsm_mncc retrieve;
2967
2968 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2969 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002970 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002971 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972}
2973
2974static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2975{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002976 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2978
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2980
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002981 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982}
2983
2984static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2985{
2986 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002987 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2989
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2991
2992 /* cause */
2993 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002994 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995 else
Harald Welte55c8f352010-03-07 23:40:35 +01002996 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002998 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999}
3000
3001static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
3002{
3003 struct gsm48_hdr *gh = msgb_l3(msg);
3004 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3005 struct tlv_parsed tp;
3006 struct gsm_mncc dtmf;
3007
3008 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3009 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003010 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 /* keypad facility */
3012 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
3013 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01003014 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
3016 }
3017
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003018 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003019}
3020
3021static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
3022{
3023 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003024 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3026
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3028
3029 /* keypad */
3030 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01003031 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003032
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003033 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034}
3035
3036static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3037{
3038 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003039 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3041
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3043
3044 /* cause */
3045 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01003046 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 else
Harald Welte55c8f352010-03-07 23:40:35 +01003048 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003050 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051}
3052
3053static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3054{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003055 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3057
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3059
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003060 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003061}
3062
3063static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3064{
3065 struct gsm_mncc dtmf;
3066
3067 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3068 dtmf.callref = trans->callref;
3069
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003070 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071}
3072
3073static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3074{
3075 struct gsm48_hdr *gh = msgb_l3(msg);
3076 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3077 struct tlv_parsed tp;
3078 struct gsm_mncc modify;
3079
3080 memset(&modify, 0, sizeof(struct gsm_mncc));
3081 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003082 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 /* bearer capability */
3084 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3085 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003086 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003088 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003089 }
3090
3091 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3092
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003093 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003094}
3095
3096static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3097{
3098 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003099 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3101
Harald Welte4bfdfe72009-06-10 23:11:52 +08003102 gh->msg_type = GSM48_MT_CC_MODIFY;
3103
3104 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3105
3106 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003107 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003108
3109 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3110
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003111 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112}
3113
3114static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3115{
3116 struct gsm48_hdr *gh = msgb_l3(msg);
3117 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3118 struct tlv_parsed tp;
3119 struct gsm_mncc modify;
3120
3121 gsm48_stop_cc_timer(trans);
3122
3123 memset(&modify, 0, sizeof(struct gsm_mncc));
3124 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003125 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003126 /* bearer capability */
3127 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3128 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003129 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003131 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 }
3133
3134 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3135
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003136 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003137}
3138
3139static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3140{
3141 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003142 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3144
Harald Welte4bfdfe72009-06-10 23:11:52 +08003145 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3146
3147 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003148 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003149
3150 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3151
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003152 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153}
3154
3155static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3156{
3157 struct gsm48_hdr *gh = msgb_l3(msg);
3158 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3159 struct tlv_parsed tp;
3160 struct gsm_mncc modify;
3161
3162 gsm48_stop_cc_timer(trans);
3163
3164 memset(&modify, 0, sizeof(struct gsm_mncc));
3165 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003166 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167 /* bearer capability */
3168 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3169 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003170 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003172 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 }
3174 /* cause */
3175 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3176 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01003177 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3179 }
3180
3181 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3182
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003183 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003184}
3185
3186static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3187{
3188 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003189 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003190 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3191
Harald Welte4bfdfe72009-06-10 23:11:52 +08003192 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3193
3194 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003195 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003196 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01003197 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003198
3199 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3200
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003201 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003202}
3203
3204static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3205{
3206 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003207 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003208 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3209
Harald Welte4bfdfe72009-06-10 23:11:52 +08003210 gh->msg_type = GSM48_MT_CC_NOTIFY;
3211
3212 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01003213 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003215 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003216}
3217
3218static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3219{
3220 struct gsm48_hdr *gh = msgb_l3(msg);
3221 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3222// struct tlv_parsed tp;
3223 struct gsm_mncc notify;
3224
3225 memset(&notify, 0, sizeof(struct gsm_mncc));
3226 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003227// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003228 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01003229 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003230
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003231 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003232}
3233
3234static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3235{
3236 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003237 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003238 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3239
Harald Welte4bfdfe72009-06-10 23:11:52 +08003240 gh->msg_type = GSM48_MT_CC_USER_INFO;
3241
3242 /* user-user */
3243 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01003244 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 /* more data */
3246 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01003247 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003248
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003249 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003250}
3251
3252static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3253{
3254 struct gsm48_hdr *gh = msgb_l3(msg);
3255 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3256 struct tlv_parsed tp;
3257 struct gsm_mncc user;
3258
3259 memset(&user, 0, sizeof(struct gsm_mncc));
3260 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003261 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 /* user-user */
3263 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3264 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01003265 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3267 }
3268 /* more data */
3269 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3270 user.more = 1;
3271
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003272 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273}
3274
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003275static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003276{
3277 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003278 struct gsm_lchan *lchan = trans->conn->lchan;
3279
3280 /*
3281 * We were forced to make an assignment a lot earlier and
3282 * we should avoid sending another assignment that might
3283 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3284 * In case of rtp-bridge it is too late to change things
3285 * here.
3286 */
3287 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3288 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003290 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3291 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003292}
3293
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003294static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3295 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3296 uint32_t payload_msg_type)
3297{
3298 uint8_t data[sizeof(struct gsm_mncc)];
3299 struct gsm_mncc_rtp *rtp;
3300
3301 memset(&data, 0, sizeof(data));
3302 rtp = (struct gsm_mncc_rtp *) &data[0];
3303
3304 rtp->callref = callref;
3305 rtp->msg_type = cmd;
3306 rtp->ip = addr;
3307 rtp->port = port;
3308 rtp->payload_type = payload_type;
3309 rtp->payload_msg_type = payload_msg_type;
3310 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3311}
3312
3313static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3314{
3315 struct gsm_lchan *lchan;
3316 int msg_type;
3317
3318 lchan = trans->conn->lchan;
3319 switch (lchan->abis_ip.rtp_payload) {
3320 case RTP_PT_GSM_FULL:
3321 msg_type = GSM_TCHF_FRAME;
3322 break;
3323 case RTP_PT_GSM_EFR:
3324 msg_type = GSM_TCHF_FRAME_EFR;
3325 break;
3326 case RTP_PT_GSM_HALF:
3327 msg_type = GSM_TCHH_FRAME;
3328 break;
3329 case RTP_PT_AMR:
3330 msg_type = GSM_TCH_FRAME_AMR;
3331 break;
3332 default:
3333 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3334 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3335 msg_type = 0;
3336 break;
3337 }
3338
3339 return mncc_recv_rtp(net, trans->callref, cmd,
3340 lchan->abis_ip.bound_ip,
3341 lchan->abis_ip.bound_port,
3342 lchan->abis_ip.rtp_payload,
3343 msg_type);
3344}
3345
3346static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3347{
3348 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3349}
3350
3351static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3352{
3353 struct gsm_bts *bts;
3354 struct gsm_lchan *lchan;
3355 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003356 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003357
3358 /* Find callref */
3359 trans = trans_find_by_callref(net, callref);
3360 if (!trans) {
3361 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3362 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3363 return -EIO;
3364 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003365 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003366 if (!trans->conn) {
3367 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3368 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3369 return 0;
3370 }
3371
3372 lchan = trans->conn->lchan;
3373 bts = lchan->ts->trx->bts;
3374 if (!is_ipaccess_bts(bts)) {
3375 /*
3376 * I want this to be straight forward and have no audio flow
3377 * through the nitb/osmo-mss system. This currently means that
3378 * this will not work with BS11/Nokia type BTS. We would need
3379 * to have a trau<->rtp bridge for these but still preferable
3380 * in another process.
3381 */
3382 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3383 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3384 return -EINVAL;
3385 }
3386
3387 trans->conn->mncc_rtp_bridge = 1;
3388 /*
3389 * *sigh* we need to pick a codec now. Pick the most generic one
3390 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003391 * similiar to the routine above.
3392 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003393 */
3394 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3395 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003396 m = mncc_codec_for_mode(lchan->type);
3397 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3398 get_value_string(gsm48_chan_mode_names, m),
3399 get_value_string(gsm_chan_t_names, lchan->type));
3400 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003401 lchan->type != GSM_LCHAN_TCH_H);
3402 }
3403
3404 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3405 return 0;
3406}
3407
3408static int tch_rtp_connect(struct gsm_network *net, void *arg)
3409{
3410 struct gsm_lchan *lchan;
3411 struct gsm_trans *trans;
3412 struct gsm_mncc_rtp *rtp = arg;
3413
3414 /* Find callref */
3415 trans = trans_find_by_callref(net, rtp->callref);
3416 if (!trans) {
3417 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3418 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3419 return -EIO;
3420 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003421 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003422 if (!trans->conn) {
3423 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3424 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3425 return 0;
3426 }
3427
3428 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003429 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3430 get_value_string(gsm48_chan_mode_names,
3431 mncc_codec_for_mode(lchan->type)),
3432 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003433
3434 /* TODO: Check if payload_msg_type is compatible with what we have */
3435 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3436 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3437 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3438 }
3439
3440 /*
3441 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3442 * complains about both rtp and rtp payload2 being present in the
3443 * same package!
3444 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003445 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003446 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3447}
3448
3449static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3450{
3451 struct gsm_network *net;
3452 struct gsm_trans *tmp, *trans = NULL;
3453
3454 net = lchan->ts->trx->bts->network;
3455 llist_for_each_entry(tmp, &net->trans_list, entry) {
3456 if (!tmp->conn)
3457 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003458 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003459 continue;
3460 trans = tmp;
3461 break;
3462 }
3463
3464 if (!trans) {
3465 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3466 gsm_lchan_name(lchan));
3467 return 0;
3468 }
3469
3470 switch (signal) {
3471 case S_ABISIP_CRCX_ACK:
3472 if (lchan->conn->mncc_rtp_create_pending) {
3473 lchan->conn->mncc_rtp_create_pending = 0;
3474 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3475 gsm_lchan_name(lchan));
3476 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3477 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003478 /*
3479 * TODO: this appears to be too early? Why not until after
3480 * the handover detect or the handover complete?
3481 */
3482 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003483 break;
3484 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003485 if (lchan->conn->mncc_rtp_connect_pending) {
3486 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003487 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3488 gsm_lchan_name(lchan));
3489 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3490 }
3491 break;
3492 }
3493
3494 return 0;
3495}
3496
3497
Harald Welte4bfdfe72009-06-10 23:11:52 +08003498static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003499 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003500 int type;
3501 int (*rout) (struct gsm_trans *trans, void *arg);
3502} downstatelist[] = {
3503 /* mobile originating call establishment */
3504 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3505 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3506 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3507 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3508 {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 */
3509 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3510 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3511 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3512 /* mobile terminating call establishment */
3513 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3514 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3515 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3516 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3517 /* signalling during call */
3518 {SBIT(GSM_CSTATE_ACTIVE),
3519 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3520 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3521 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3522 {ALL_STATES,
3523 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3524 {ALL_STATES,
3525 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3526 {ALL_STATES,
3527 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3528 {SBIT(GSM_CSTATE_ACTIVE),
3529 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3530 {SBIT(GSM_CSTATE_ACTIVE),
3531 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3532 {SBIT(GSM_CSTATE_ACTIVE),
3533 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3534 {SBIT(GSM_CSTATE_ACTIVE),
3535 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3536 {SBIT(GSM_CSTATE_ACTIVE),
3537 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3538 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3539 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3540 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3541 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3542 {SBIT(GSM_CSTATE_ACTIVE),
3543 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3544 /* clearing */
3545 {SBIT(GSM_CSTATE_INITIATED),
3546 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3547 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3548 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3549 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3550 MNCC_REL_REQ, gsm48_cc_tx_release},
3551 /* special */
3552 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003553 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003554};
3555
3556#define DOWNSLLEN \
3557 (sizeof(downstatelist) / sizeof(struct downstate))
3558
3559
Harald Welte76556372010-12-22 23:57:45 +01003560int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003561{
Harald Welte1a6f7982009-08-09 18:52:33 +02003562 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003563 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003564 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003565 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003566 struct gsm_mncc *data = arg, rel;
3567
Harald Welte04dc88f2010-12-22 21:45:05 +01003568 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3569
Harald Welte4bfdfe72009-06-10 23:11:52 +08003570 /* handle special messages */
3571 switch(msg_type) {
3572 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003573 rc = tch_bridge(net, arg);
3574 if (rc < 0)
3575 disconnect_bridge(net, arg, -rc);
3576 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003577 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003578 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003579 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003580 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003581 case MNCC_RTP_CREATE:
3582 return tch_rtp_create(net, data->callref);
3583 case MNCC_RTP_CONNECT:
3584 return tch_rtp_connect(net, arg);
3585 case MNCC_RTP_FREE:
3586 /* unused right now */
3587 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003588 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003589 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003590 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003591 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003592 /* Find callref */
3593 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003594 if (!trans) {
3595 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003596 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003597 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003598 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003599 if (!trans->conn) {
3600 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003601 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003602 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003603 if (!trans->conn->lchan) {
3604 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3605 return 0;
3606 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003607 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3608 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003609 /* This should be LOGL_ERROR or NOTICE, but
3610 * unfortuantely it happens for a couple of frames at
3611 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003612 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003613 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003614 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003615 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003616 switch (bts->type) {
3617 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003618 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003619 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003620 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003621 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003622 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003623 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003624 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003625 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003626 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003627 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003628 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003629 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003630 }
3631 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003632 }
3633
3634 memset(&rel, 0, sizeof(struct gsm_mncc));
3635 rel.callref = data->callref;
3636
3637 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003638 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003639
3640 /* Callref unknown */
3641 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003642 struct gsm_subscriber *subscr;
3643
Harald Welte4a3464c2009-07-04 10:11:24 +02003644 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003645 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3646 "Received '%s' from MNCC with "
3647 "unknown callref %d\n", data->called.number,
3648 get_mncc_name(msg_type), data->callref);
3649 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003650 return mncc_release_ind(net, NULL, data->callref,
3651 GSM48_CAUSE_LOC_PRN_S_LU,
3652 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003653 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003654 if (!data->called.number[0] && !data->imsi[0]) {
3655 DEBUGP(DCC, "(bts - trx - ts - ti) "
3656 "Received '%s' from MNCC with "
3657 "no number or IMSI\n", get_mncc_name(msg_type));
3658 /* Invalid number */
3659 return mncc_release_ind(net, NULL, data->callref,
3660 GSM48_CAUSE_LOC_PRN_S_LU,
3661 GSM48_CC_CAUSE_INV_NR_FORMAT);
3662 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003663 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003664 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003665 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003666 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003667 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003668 subscr = subscr_get_by_imsi(net->subscr_group,
3669 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003670
3671 /* update the subscriber we deal with */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003672 log_set_context(LOG_CTX_VLR_SUBSCR, subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003673
Harald Welte4bfdfe72009-06-10 23:11:52 +08003674 /* If subscriber is not found */
3675 if (!subscr) {
3676 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3677 "Received '%s' from MNCC with "
3678 "unknown subscriber %s\n", data->called.number,
3679 get_mncc_name(msg_type), data->called.number);
3680 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003681 return mncc_release_ind(net, NULL, data->callref,
3682 GSM48_CAUSE_LOC_PRN_S_LU,
3683 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003684 }
3685 /* If subscriber is not "attached" */
3686 if (!subscr->lac) {
3687 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3688 "Received '%s' from MNCC with "
3689 "detached subscriber %s\n", data->called.number,
3690 get_mncc_name(msg_type), data->called.number);
3691 subscr_put(subscr);
3692 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003693 return mncc_release_ind(net, NULL, data->callref,
3694 GSM48_CAUSE_LOC_PRN_S_LU,
3695 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003696 }
3697 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003698 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003699 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003700 DEBUGP(DCC, "No memory for trans.\n");
3701 subscr_put(subscr);
3702 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003703 mncc_release_ind(net, NULL, data->callref,
3704 GSM48_CAUSE_LOC_PRN_S_LU,
3705 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003706 return -ENOMEM;
3707 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003708 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003709 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003710
Harald Welte4bfdfe72009-06-10 23:11:52 +08003711 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003712 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003713 /* find transaction with this subscriber already paging */
3714 llist_for_each_entry(transt, &net->trans_list, entry) {
3715 /* Transaction of our lchan? */
3716 if (transt == trans ||
3717 transt->subscr != subscr)
3718 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003719 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003720 "Received '%s' from MNCC with "
3721 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003722 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003723 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003724 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003725 subscr_put(subscr);
3726 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003727 return 0;
3728 }
3729 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003730 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003731
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003732 /* Request a channel */
3733 trans->paging_request = subscr_request_channel(subscr,
3734 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3735 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003736 if (!trans->paging_request) {
3737 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3738 subscr_put(subscr);
3739 trans_free(trans);
3740 return 0;
3741 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003742 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003743 return 0;
3744 }
3745 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003746 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003747 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003748 } else {
3749 /* update the subscriber we deal with */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003750 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003751 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003752
3753 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003754 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003755
3756 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003757 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003758 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003759 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003760 (trans->subscr)?(trans->subscr->extension):"-",
3761 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003762 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3763 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003764 if (msg_type == MNCC_REL_REQ)
3765 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3766 else
3767 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3768 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003769 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003770 return rc;
3771 }
3772
3773 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3774 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003775 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003776 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003777 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003778 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003779 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003780
3781 /* Find function for current state and message */
3782 for (i = 0; i < DOWNSLLEN; i++)
3783 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003784 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003785 break;
3786 if (i == DOWNSLLEN) {
3787 DEBUGP(DCC, "Message unhandled at this state.\n");
3788 return 0;
3789 }
3790
3791 rc = downstatelist[i].rout(trans, arg);
3792
3793 return rc;
3794}
3795
3796
3797static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003798 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003799 int type;
3800 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3801} datastatelist[] = {
3802 /* mobile originating call establishment */
3803 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3804 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3805 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3806 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3807 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3808 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3809 /* mobile terminating call establishment */
3810 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3811 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3812 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3813 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003814 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003815 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3816 /* signalling during call */
3817 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3818 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3819 {SBIT(GSM_CSTATE_ACTIVE),
3820 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3821 {ALL_STATES,
3822 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3823 {ALL_STATES,
3824 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3825 {ALL_STATES,
3826 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3827 {SBIT(GSM_CSTATE_ACTIVE),
3828 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3829 {SBIT(GSM_CSTATE_ACTIVE),
3830 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3831 {SBIT(GSM_CSTATE_ACTIVE),
3832 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3833 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3834 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3835 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3836 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3837 {SBIT(GSM_CSTATE_ACTIVE),
3838 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3839 /* clearing */
3840 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3841 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3842 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3843 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3844 {ALL_STATES, /* 5.4.3.4 */
3845 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3846};
3847
3848#define DATASLLEN \
3849 (sizeof(datastatelist) / sizeof(struct datastate))
3850
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003851static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003852{
3853 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003854 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003855 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003856 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003857 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003858
Harald Welte4bfdfe72009-06-10 23:11:52 +08003859 if (msg_type & 0x80) {
3860 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3861 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003862 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003863
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003864 if (!conn->subscr) {
3865 LOGP(DCC, LOGL_ERROR, "Invalid conn, no subscriber\n");
3866 return -EINVAL;
3867 }
3868
Harald Welte4bfdfe72009-06-10 23:11:52 +08003869 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003870 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003871
Harald Welte6f5aee02009-07-23 21:21:14 +02003872 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003873 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003874 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003875 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003876 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3877 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003878
3879 /* Create transaction */
3880 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003881 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003882 "creating new trans.\n", transaction_id);
3883 /* Create transaction */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02003884 trans = trans_alloc(conn->network, conn->subscr,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003885 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003886 transaction_id, new_callref++);
3887 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003888 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003889 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003890 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003891 GSM48_MT_CC_RELEASE_COMPL);
3892 return -ENOMEM;
3893 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003894 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003895 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003896 }
3897
3898 /* find function for current state and message */
3899 for (i = 0; i < DATASLLEN; i++)
3900 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003901 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003902 break;
3903 if (i == DATASLLEN) {
3904 DEBUGP(DCC, "Message unhandled at this state.\n");
3905 return 0;
3906 }
3907
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003908 assert(trans->subscr);
3909
Harald Welte4bfdfe72009-06-10 23:11:52 +08003910 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003911
3912 return rc;
3913}
3914
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003915/* Create a dummy to wait five seconds */
3916static void release_anchor(struct gsm_subscriber_connection *conn)
3917{
3918 if (!conn->anch_operation)
3919 return;
3920
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003921 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003922 talloc_free(conn->anch_operation);
3923 conn->anch_operation = NULL;
3924}
3925
3926static void anchor_timeout(void *_data)
3927{
3928 struct gsm_subscriber_connection *con = _data;
3929
3930 release_anchor(con);
3931 msc_release_connection(con);
3932}
3933
3934int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3935{
3936 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3937 if (!conn->anch_operation)
3938 return -1;
3939
3940 conn->anch_operation->timeout.data = conn;
3941 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003942 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003943 return 0;
3944}
3945
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003946struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
3947{
3948 struct gsm_subscriber_connection *conn;
3949
3950 conn = talloc_zero(network, struct gsm_subscriber_connection);
3951 if (!conn)
3952 return NULL;
3953
3954 conn->network = network;
3955 llist_add_tail(&conn->entry, &network->subscr_conns);
3956 return conn;
3957}
3958
3959void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
3960{
3961 if (!conn)
3962 return;
3963
3964 if (conn->subscr) {
3965 subscr_put(conn->subscr);
3966 conn->subscr = NULL;
3967 }
3968
3969 llist_del(&conn->entry);
3970 talloc_free(conn);
3971}
3972
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003973/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003974int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003975{
3976 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003977 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003978 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003979
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003980 OSMO_ASSERT(conn);
3981 OSMO_ASSERT(msg);
3982
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003983 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003984#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003985 if (silent_call_reroute(conn, msg))
3986 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003987#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003988
Harald Welte52b1f982008-12-23 20:25:15 +00003989 switch (pdisc) {
3990 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003991 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003992 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003993 break;
3994 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003995 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003996 break;
3997 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003998 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003999 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00004000 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08004001 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08004002 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00004003 break;
Harald Welte52b1f982008-12-23 20:25:15 +00004004 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00004005 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01004006 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
4007 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02004008 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00004009 break;
Harald Welte6eafe912009-10-16 08:32:58 +02004010 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08004011 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08004012 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02004013 break;
Harald Welte52b1f982008-12-23 20:25:15 +00004014 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01004015 LOGP(DRLL, LOGL_NOTICE, "Unknown "
4016 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02004017 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00004018 break;
4019 }
4020
4021 return rc;
4022}
Harald Welte8470bf22008-12-25 23:28:35 +00004023
Harald Welte805f6442009-07-28 18:25:29 +02004024/*
Neels Hofmeyrcc7db182016-12-18 23:52:38 +01004025 * This will be run by the linker when loading the DSO. We use it to
Harald Welte805f6442009-07-28 18:25:29 +02004026 * do system initialization, e.g. registration of signal handlers.
4027 */
4028static __attribute__((constructor)) void on_dso_load_0408(void)
4029{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02004030 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02004031}