blob: fb02de2068ddd769c5d22afef19c7da99311d77d [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
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200298static 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
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200313static void loc_updating_failure(struct gsm_subscriber_connection *conn, int release)
314{
315 if (!conn->loc_operation)
316 return;
317 LOGP(DMM, LOGL_ERROR, "Location Updating failed for %s\n",
318 subscr_name(conn->subscr));
319 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_FAILED]);
320 _release_loc_updating_req(conn, release);
321}
322
323static void loc_updating_success(struct gsm_subscriber_connection *conn, int release)
324{
325 if (!conn->loc_operation)
326 return;
327 LOGP(DMM, LOGL_INFO, "Location Updating completed for %s\n",
328 subscr_name(conn->subscr));
329 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_COMPLETED]);
330 _release_loc_updating_req(conn, release);
331}
332
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100333static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000334{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800335 if (conn->loc_operation)
336 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200337 loc_updating_failure(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000338
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100339 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200340 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000341}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000342
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200343static int finish_lu(struct gsm_subscriber_connection *conn)
344{
345 int rc = 0;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200346 int avoid_tmsi = conn->network->avoid_tmsi;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200347
348 /* We're all good */
349 if (avoid_tmsi) {
350 conn->subscr->tmsi = GSM_RESERVED_TMSI;
351 db_sync_subscriber(conn->subscr);
352 } else {
353 db_subscriber_alloc_tmsi(conn->subscr);
354 }
355
356 rc = gsm0408_loc_upd_acc(conn);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200357 if (conn->network->send_mm_info) {
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200358 /* send MM INFO with network name */
359 rc = gsm48_tx_mm_info(conn);
360 }
361
362 /* call subscr_update after putting the loc_upd_acc
363 * in the transmit queue, since S_SUBSCR_ATTACHED might
364 * trigger further action like SMS delivery */
365 subscr_update(conn->subscr, conn->bts,
366 GSM_SUBSCRIBER_UPDATE_ATTACHED);
367
368 /*
369 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
370 * MS needs to respond with a TMSI REALLOCATION COMPLETE
371 * (even if the TMSI is the same).
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200372 * If avoid_tmsi == true, we don't send a TMSI, we don't
373 * expect a reply and Location Updating is done.
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200374 */
375 if (avoid_tmsi)
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200376 loc_updating_success(conn, 1);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200377
378 return rc;
379}
380
Sylvain Munaut267fba02009-12-24 00:28:01 +0100381static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
382 struct msgb *msg, void *data, void *param)
383{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800384 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100385 int rc = 0;
386
387 switch (event) {
388 case GSM_SECURITY_AUTH_FAILED:
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200389 loc_updating_failure(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100390 break;
391
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200392 case GSM_SECURITY_ALREADY:
393 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
394 "UPDATING after CM SERVICE REQUEST\n");
395 /* fall through */
396
Sylvain Munaut267fba02009-12-24 00:28:01 +0100397 case GSM_SECURITY_NOAVAIL:
398 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200399 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100400 break;
401
402 default:
403 rc = -EINVAL;
404 };
405
406 return rc;
407}
408
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100409static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000410{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100411 if (!conn->loc_operation)
412 return 0;
413
Sylvain Munaut267fba02009-12-24 00:28:01 +0100414 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800415 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100416 conn->loc_operation->key_seq,
417 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000418 return 0;
419}
420
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800421void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000422{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800423 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800424
425 /* avoid someone issuing a clear */
426 conn->in_release = 1;
427
Holger Freyther7c19f742009-06-06 13:54:35 +0000428 /*
429 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800430 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000431 */
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200432 loc_updating_failure(conn, 0);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100433
434 /* We might need to cancel the paging response or such. */
435 if (conn->sec_operation && conn->sec_operation->cb) {
436 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
437 NULL, conn, conn->sec_operation->cb_data);
438 }
439
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800440 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800441 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000442
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100443 /*
444 * Free all transactions that are associated with the released
445 * connection. The transaction code will inform the CC or SMS
446 * facilities that will send the release indications. As part of
447 * the CC REL_IND the remote leg might be released and this will
448 * trigger the call to trans_free. This is something the llist
449 * macro can not handle and we will need to re-iterate the list.
450 *
451 * TODO: Move the trans_list into the subscriber connection and
452 * create a pending list for MT transactions. These exist before
453 * we have a subscriber connection.
454 */
455restart:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200456 llist_for_each_entry_safe(trans, temp, &conn->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100457 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200458 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100459 goto restart;
460 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800461 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000462}
463
Harald Welte371efe52010-12-22 23:17:50 +0100464void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
465{
466 struct gsm_trans *trans, *temp;
467
468 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
469
470 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100471 if (trans->protocol == protocol) {
472 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100473 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100474 }
Harald Welte371efe52010-12-22 23:17:50 +0100475 }
476}
477
Holger Freyther429e7762008-12-30 13:28:30 +0000478/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200479int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000480{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800481 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800482 struct msgb *msg;
483
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800484 msg = gsm48_create_loc_upd_rej(cause);
485 if (!msg) {
486 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
487 return -1;
488 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200489
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800490 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000491
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100492 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte7184bd02015-12-28 14:04:36 +0100493 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800494 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100495
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800496 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000497}
498
499/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200500static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000501{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100502 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000503 struct gsm48_hdr *gh;
504 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200505 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200506
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800507 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000508
509 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
510 gh->proto_discr = GSM48_PDISC_MM;
511 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
512
513 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200514 gsm48_generate_lai(lai, conn->network->country_code,
515 conn->network->network_code,
516 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000517
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200518 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
519 uint8_t mi[10];
520 int len;
521 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
522 mid = msgb_put(msg, len);
523 memcpy(mid, mi, len);
524 } else {
525 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
526 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
527 }
Harald Welte52b1f982008-12-23 20:25:15 +0000528
529 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
530
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800531 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000532}
533
Harald Weltebf5e8df2009-02-03 12:59:45 +0000534/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200535static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000536{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100537 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000538 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000539
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800540 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000541
542 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
543 gh->proto_discr = GSM48_PDISC_MM;
544 gh->msg_type = GSM48_MT_MM_ID_REQ;
545 gh->data[0] = id_type;
546
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800547 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000548}
549
Maxe21cf382016-05-11 11:01:06 +0200550static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
551 const char *imsi)
552{
Maxe6052c42016-06-30 10:25:49 +0200553 if (!net->auto_create_subscr)
Maxddee01f2016-05-24 14:23:27 +0200554 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200555
Maxe6052c42016-06-30 10:25:49 +0200556 if (!subscr_regexp_check(net, imsi))
557 return NULL;
Maxddee01f2016-05-24 14:23:27 +0200558
Maxe6052c42016-06-30 10:25:49 +0200559 return subscr_create_subscriber(net->subscr_group, imsi);
Maxe21cf382016-05-11 11:01:06 +0200560}
Harald Welte231ad4f2008-12-27 11:15:38 +0000561
Harald Weltebf5e8df2009-02-03 12:59:45 +0000562/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800563static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000564{
565 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200566 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200567 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200568 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000569
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200570 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200571 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
572 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000573
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200574 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100575
Harald Welte75a983f2008-12-27 21:34:06 +0000576 switch (mi_type) {
577 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200578 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100579 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100580 conn->subscr = subscr_get_by_imsi(net->subscr_group,
581 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200582 if (!conn->subscr)
583 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200584 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100585 if (!conn->subscr && conn->loc_operation) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200586 gsm0408_loc_upd_rej(conn, net->reject_cause);
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200587 loc_updating_failure(conn, 1);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100588 return 0;
589 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100590 if (conn->loc_operation)
591 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000592 break;
593 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000594 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000595 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100596 if (conn->subscr) {
597 db_subscriber_assoc_imei(conn->subscr, mi_string);
598 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200599 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100600 if (conn->loc_operation)
601 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000602 break;
603 }
Holger Freyther73487a22008-12-31 18:53:57 +0000604
605 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100606 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000607}
608
Harald Welte255539c2008-12-28 02:26:27 +0000609
610static void loc_upd_rej_cb(void *data)
611{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100612 struct gsm_subscriber_connection *conn = data;
Harald Welte255539c2008-12-28 02:26:27 +0000613
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100614 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200615 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200616 loc_updating_failure(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000617}
618
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100619static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000620{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200621 osmo_timer_setup(&conn->loc_operation->updating_timer, loc_upd_rej_cb,
622 conn);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200623 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000624}
625
Harald Welteb9845f92015-08-16 18:07:48 +0200626static const struct value_string lupd_names[] = {
627 { GSM48_LUPD_NORMAL, "NORMAL" },
628 { GSM48_LUPD_PERIODIC, "PERIODIC" },
629 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
630 { 0, NULL }
631};
Harald Welte2a139372009-02-22 21:14:55 +0000632
Harald Weltebf5e8df2009-02-03 12:59:45 +0000633/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800634static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000635{
Harald Welte8470bf22008-12-25 23:28:35 +0000636 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000637 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200639 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200640 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000641
Harald Welte8470bf22008-12-25 23:28:35 +0000642 lu = (struct gsm48_loc_upd_req *) gh->data;
643
644 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000645
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200646 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000647
Harald Welteb9845f92015-08-16 18:07:48 +0200648 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
649 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000650
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200651 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100652
Harald Welte24ff6ee2009-12-22 00:41:05 +0100653 switch (lu->type) {
654 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200655 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100656 break;
657 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200658 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100659 break;
660 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200661 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100662 break;
663 }
664
Holger Freythereaf04692009-06-06 13:54:44 +0000665 /*
666 * Pseudo Spoof detection: Just drop a second/concurrent
667 * location updating request.
668 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100669 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200670 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100671 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800672 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000673 return 0;
674 }
675
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800676 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000677
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200678 conn->loc_operation->key_seq = lu->key_seq;
679
Harald Welte52b1f982008-12-23 20:25:15 +0000680 switch (mi_type) {
681 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200682 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000683 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200684 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100685 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000686
Jan Luebbe370b41d2009-08-12 10:19:34 +0200687 /* look up subscriber based on IMSI, create if not found */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200688 subscr = subscr_get_by_imsi(conn->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200689 if (!subscr)
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200690 subscr = subscr_create(conn->network, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100691 if (!subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200692 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Neels Hofmeyr36891a72016-05-09 13:18:03 +0200693 loc_updating_failure(conn, 0); /* FIXME: set release == true? */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100694 return 0;
695 }
Harald Welte4b634542008-12-27 01:55:51 +0000696 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000697 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200698 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000699 /* look up the subscriber based on TMSI, request IMSI if it fails */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200700 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200701 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000702 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000703 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200704 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100705 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000706 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200707 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200708 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200709 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000710 break;
711 case GSM_MI_TYPE_IMEI:
712 case GSM_MI_TYPE_IMEISV:
713 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200714 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000715 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200716 default:
Harald Weltea0368542009-06-27 02:58:43 +0200717 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000718 break;
719 }
720
Harald Welte24516ea2009-07-04 10:18:00 +0200721 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100722 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200723
Harald Welte4bfdfe72009-06-10 23:11:52 +0800724 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200725 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800726 /* FIXME: request id? close channel? */
727 return -EINVAL;
728 }
729
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100730 conn->subscr = subscr;
731 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000732
Harald Welte24516ea2009-07-04 10:18:00 +0200733 /* check if we can let the subscriber into our network immediately
734 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100735 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000736}
737
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100738/* Turn int into semi-octet representation: 98 => 0x89 */
739static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800740{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100741 uint8_t ret;
742
743 ret = value / 10;
744 ret |= (value % 10) << 4;
745
746 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800747}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100748
Harald Welte4bfdfe72009-06-10 23:11:52 +0800749
Harald Weltedb253af2008-12-30 17:56:55 +0000750/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800751int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000752{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100753 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000754 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200755 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200756 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200757 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100758
Harald Welte4bfdfe72009-06-10 23:11:52 +0800759 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100760 struct tm* gmt_time;
761 struct tm* local_time;
762 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100763 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000764
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800765 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000766
767 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
768 gh->proto_discr = GSM48_PDISC_MM;
769 gh->msg_type = GSM48_MT_MM_INFO;
770
771 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200772#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000773 name_len = strlen(net->name_long);
774 /* 10.5.3.5a */
775 ptr8 = msgb_put(msg, 3);
776 ptr8[0] = GSM48_IE_NAME_LONG;
777 ptr8[1] = name_len*2 +1;
778 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
779
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200780 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000781 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000782 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000783
784 /* FIXME: Use Cell Broadcast, not UCS-2, since
785 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200786#endif
787 name_len = (strlen(net->name_long)*7)/8;
788 name_pad = (8 - strlen(net->name_long)*7)%8;
789 if (name_pad > 0)
790 name_len++;
791 /* 10.5.3.5a */
792 ptr8 = msgb_put(msg, 3);
793 ptr8[0] = GSM48_IE_NAME_LONG;
794 ptr8[1] = name_len +1;
795 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
796
797 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100798 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200799
Harald Weltedb253af2008-12-30 17:56:55 +0000800 }
801
802 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200803#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000804 name_len = strlen(net->name_short);
805 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200806 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200807 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000808 ptr8[1] = name_len*2 + 1;
809 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
810
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200811 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000812 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000813 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200814#endif
815 name_len = (strlen(net->name_short)*7)/8;
816 name_pad = (8 - strlen(net->name_short)*7)%8;
817 if (name_pad > 0)
818 name_len++;
819 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200820 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200821 ptr8[0] = GSM48_IE_NAME_SHORT;
822 ptr8[1] = name_len +1;
823 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
824
825 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100826 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200827
Harald Weltedb253af2008-12-30 17:56:55 +0000828 }
829
Harald Weltedb253af2008-12-30 17:56:55 +0000830 /* Section 10.5.3.9 */
831 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100832 gmt_time = gmtime(&cur_t);
833
Harald Weltedb253af2008-12-30 17:56:55 +0000834 ptr8 = msgb_put(msg, 8);
835 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100836 ptr8[1] = bcdify(gmt_time->tm_year % 100);
837 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
838 ptr8[3] = bcdify(gmt_time->tm_mday);
839 ptr8[4] = bcdify(gmt_time->tm_hour);
840 ptr8[5] = bcdify(gmt_time->tm_min);
841 ptr8[6] = bcdify(gmt_time->tm_sec);
842
Neels Hofmeyr73983952016-05-10 13:29:33 +0200843 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200844 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200845 if (net->tz.hr < 0) {
846 tzunits = ((net->tz.hr/-1)*4);
847 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100848 ptr8[7] = bcdify(tzunits);
849 /* Set negative time */
850 ptr8[7] |= 0x08;
851 }
852 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200853 tzunits = net->tz.hr*4;
854 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100855 ptr8[7] = bcdify(tzunits);
856 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100857 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200858 if (net->tz.dst >= 0 && net->tz.dst <= 2)
859 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100860 }
861 else {
862 /* Need to get GSM offset and convert into 15 min units */
863 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
864 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200865#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100866 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200867#else
868#warning find a portable way to obtain the timezone offset
869 tzunits = 0;
870#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100871 if (tzunits < 0) {
872 tzunits = tzunits/-1;
873 ptr8[7] = bcdify(tzunits);
874 /* Flip it to negative */
875 ptr8[7] |= 0x08;
876 }
877 else
878 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100879
880 /* Does not support DST +2 */
881 if (local_time->tm_isdst)
882 dst = 1;
883 }
884
885 if (dst) {
886 ptr8 = msgb_put(msg, 3);
887 ptr8[0] = GSM48_IE_NET_DST;
888 ptr8[1] = 1;
889 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100890 }
Harald Weltedb253af2008-12-30 17:56:55 +0000891
Daniel Willmanneea93372009-08-13 03:42:07 +0200892 DEBUGP(DMM, "-> MM INFO\n");
893
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800894 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000895}
896
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100897/*! Send an Authentication Request to MS on the given subscriber connection
898 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
899 * \param[in] conn Subscriber connection to send on.
900 * \param[in] rand Random challenge token to send, must be 16 bytes long.
901 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
902 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
903 * \param[in] key_seq auth tuple's sequence number.
904 */
905int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
906 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200907{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100908 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200909 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200910 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200911
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100912 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100913 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100914 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200915
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800916 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200917 gh->proto_discr = GSM48_PDISC_MM;
918 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
919
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100920 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200921
Harald Welte7984d5c2009-08-12 22:56:50 +0200922 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100923 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200924 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200925 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200926
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100927
928 /* 16 bytes AUTN */
929 if (autn)
930 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
931
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800932 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200933}
934
935/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800936int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200937{
938 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800939 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200940}
941
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100942/*
943 * At the 30C3 phones miss their periodic update
944 * interval a lot and then remain unreachable. In case
945 * we still know the TMSI we can just attach it again.
946 */
947static void implit_attach(struct gsm_subscriber_connection *conn)
948{
949 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
950 return;
951
952 subscr_update(conn->subscr, conn->bts,
953 GSM_SUBSCRIBER_UPDATE_ATTACHED);
954}
955
956
Sylvain Munautba87f452009-12-24 00:28:46 +0100957static int _gsm48_rx_mm_serv_req_sec_cb(
958 unsigned int hooknum, unsigned int event,
959 struct msgb *msg, void *data, void *param)
960{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800961 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100962 int rc = 0;
963
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200964 /* auth failed or succeeded, the timer was stopped */
965 conn->expire_timer_stopped = 1;
966
Sylvain Munautba87f452009-12-24 00:28:46 +0100967 switch (event) {
968 case GSM_SECURITY_AUTH_FAILED:
969 /* Nothing to do */
970 break;
971
972 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200973 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800974 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100975 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100976 break;
977
978 case GSM_SECURITY_SUCCEEDED:
979 /* nothing to do. CIPHER MODE COMMAND is
980 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100981 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100982 break;
983
984 default:
985 rc = -EINVAL;
986 };
987
988 return rc;
989}
990
Harald Welte4ed0e922009-01-10 03:17:30 +0000991/*
992 * Handle CM Service Requests
993 * a) Verify that the packet is long enough to contain the information
994 * we require otherwsie reject with INCORRECT_MESSAGE
995 * b) Try to parse the TMSI. If we do not have one reject
996 * c) Check that we know the subscriber with the TMSI otherwise reject
997 * with a HLR cause
998 * d) Set the subscriber on the gsm_lchan and accept
999 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001000static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +00001001{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001002 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001003 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001004
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001005 struct gsm_network *network = conn->network;
Harald Welteba4cf162009-01-10 01:49:35 +00001006 struct gsm_subscriber *subscr;
1007 struct gsm48_hdr *gh = msgb_l3(msg);
1008 struct gsm48_service_request *req =
1009 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +08001010 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001011 uint8_t classmark2_len = gh->data[1];
1012 uint8_t *classmark2 = gh->data+2;
1013 uint8_t mi_len = *(classmark2 + classmark2_len);
1014 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001015
Harald Weltec9e02182009-05-01 19:07:53 +00001016 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001017 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001018 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001019 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001020 GSM48_REJECT_INCORRECT_MESSAGE);
1021 }
1022
1023 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001024 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001025 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001026 GSM48_REJECT_INCORRECT_MESSAGE);
1027 }
1028
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001029 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001030 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001031
1032 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001033 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1034 req->cm_service_type, gsm48_mi_type_name(mi_type),
1035 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001036 subscr = subscr_get_by_imsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001037 mi_string);
1038 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001039 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1040 req->cm_service_type, gsm48_mi_type_name(mi_type),
1041 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001042 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001043 tmsi_from_string(mi_string));
1044 } else {
1045 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001046 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001047 GSM48_REJECT_INCORRECT_MESSAGE);
1048 }
1049
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001050 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001051
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001052 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +02001053 send_siemens_mrpci(msg->lchan, classmark2-1);
1054
Holger Freythereb443982009-06-04 13:58:42 +00001055
Harald Welte2a139372009-02-22 21:14:55 +00001056 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001057 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001058 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001059 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001060
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001061 if (!conn->subscr)
1062 conn->subscr = subscr;
1063 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001064 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1065 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001066 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1067 subscr_put(subscr);
1068 }
1069
Harald Weltec2e302d2009-07-05 14:08:13 +02001070 subscr->equipment.classmark2_len = classmark2_len;
1071 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1072 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001073
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001074 /* we will send a MM message soon */
1075 conn->expire_timer_stopped = 1;
1076
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001077 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001078 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001079}
1080
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001081static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001082{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001083 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +00001084 struct gsm48_hdr *gh = msgb_l3(msg);
1085 struct gsm48_imsi_detach_ind *idi =
1086 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001087 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001088 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001089 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001090
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001091 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001092 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1093 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001094
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001095 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001096
Harald Welte2a139372009-02-22 21:14:55 +00001097 switch (mi_type) {
1098 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001099 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001100 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001101 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001102 break;
1103 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001104 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001105 subscr = subscr_get_by_imsi(network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001106 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001107 break;
1108 case GSM_MI_TYPE_IMEI:
1109 case GSM_MI_TYPE_IMEISV:
1110 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001111 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001112 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001113 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001114 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001115 break;
1116 }
1117
Holger Freyther4a49e772009-04-12 05:37:29 +00001118 if (subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001119 subscr_update(subscr, conn->bts,
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001120 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001121 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001122
1123 subscr->equipment.classmark1 = idi->classmark1;
1124 db_sync_equipment(&subscr->equipment);
1125
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001126 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001127 } else
Harald Welte2a139372009-02-22 21:14:55 +00001128 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1129
Harald Welte31981a02009-12-20 09:58:40 +01001130 /* FIXME: iterate over all transactions and release them,
1131 * imagine an IMSI DETACH happening during an active call! */
1132
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001133 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001134 return 0;
1135}
1136
Harald Welted2a7f5a2009-06-05 20:08:20 +00001137static int gsm48_rx_mm_status(struct msgb *msg)
1138{
1139 struct gsm48_hdr *gh = msgb_l3(msg);
1140
1141 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1142
1143 return 0;
1144}
1145
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001146static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
1147 struct gsm_subscriber_connection *conn,
1148 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001149{
1150 struct gsm48_hdr *gh = msgb_l3(msg);
1151 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001152
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001153 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
1154 LOGP(DMM, LOGL_ERROR,
1155 "%s: MM AUTHENTICATION RESPONSE:"
1156 " l3 length invalid: %u\n",
1157 subscr_name(conn->subscr), msgb_l3len(msg));
1158 return -EINVAL;
1159 }
1160
1161 *res_len = sizeof(ar->sres);
1162 memcpy(res, ar->sres, sizeof(ar->sres));
1163 return 0;
1164}
1165
1166static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
1167 struct gsm_subscriber_connection *conn,
1168 struct msgb *msg)
1169{
1170 struct gsm48_hdr *gh;
1171 uint8_t *data;
1172 uint8_t iei;
1173 uint8_t ie_len;
1174 unsigned int data_len;
1175
1176 /* First parse the GSM part */
1177 if (parse_gsm_auth_resp(res, res_len, conn, msg))
1178 return -EINVAL;
1179 OSMO_ASSERT(*res_len == 4);
1180
1181 /* Then add the extended res part */
1182 gh = msgb_l3(msg);
1183 data = gh->data + sizeof(struct gsm48_auth_resp);
1184 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
1185
1186 if (data_len < 3) {
1187 LOGP(DMM, LOGL_ERROR,
1188 "%s: MM AUTHENTICATION RESPONSE:"
1189 " l3 length invalid: %u\n",
1190 subscr_name(conn->subscr), msgb_l3len(msg));
1191 return -EINVAL;
1192 }
1193
1194 iei = data[0];
1195 ie_len = data[1];
1196 if (iei != GSM48_IE_AUTH_RES_EXT) {
1197 LOGP(DMM, LOGL_ERROR,
1198 "%s: MM R99 AUTHENTICATION RESPONSE:"
1199 " expected IEI 0x%02x, got 0x%02x\n",
1200 subscr_name(conn->subscr),
1201 GSM48_IE_AUTH_RES_EXT, iei);
1202 return -EINVAL;
1203 }
1204
1205 if (ie_len > 12) {
1206 LOGP(DMM, LOGL_ERROR,
1207 "%s: MM R99 AUTHENTICATION RESPONSE:"
1208 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
1209 subscr_name(conn->subscr), GSM48_IE_AUTH_RES_EXT, ie_len);
1210 return -EINVAL;
1211 }
1212
1213 *res_len += ie_len;
1214 memcpy(res + 4, &data[2], ie_len);
1215 return 0;
1216}
1217
1218/* Chapter 9.2.3: Authentication Response */
1219static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
1220{
1221 struct gsm_network *net = conn->network;
1222 uint8_t res[16];
1223 uint8_t res_len;
1224 int rc;
1225 bool is_r99;
1226
1227 if (!conn->subscr) {
1228 LOGP(DMM, LOGL_ERROR,
1229 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
1230 gsm48_tx_mm_auth_rej(conn);
1231 release_security_operation(conn);
1232 return -EINVAL;
1233 }
1234
1235 if (msgb_l3len(msg) >
1236 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
1237 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
1238 is_r99 = true;
1239 } else {
1240 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
1241 is_r99 = false;
1242 }
1243
1244 if (rc) {
1245 gsm48_tx_mm_auth_rej(conn);
1246 release_security_operation(conn);
1247 return -EINVAL;
1248 }
1249
1250 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
1251 subscr_name(conn->subscr),
1252 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
1253 osmo_hexdump_nospc(res, res_len));
1254
1255 /* Future: vlr_sub_rx_auth_resp(conn->vsub, is_r99,
Neels Hofmeyrd3270a92017-02-02 01:07:28 +01001256 * conn->via_ran == RAN_UTRAN_IU,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001257 * res, res_len);
1258 */
1259
1260 if (res_len != 4) {
1261 LOGP(DMM, LOGL_ERROR,
1262 "%s: MM AUTHENTICATION RESPONSE:"
1263 " UMTS authentication not supported\n",
1264 subscr_name(conn->subscr));
1265 }
Sylvain Munaut30a15382009-12-24 00:27:26 +01001266
1267 /* Safety check */
1268 if (!conn->sec_operation) {
1269 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1270 return -EIO;
1271 }
1272
1273 /* Validate SRES */
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001274 if (memcmp(conn->sec_operation->atuple.vec.sres, res, 4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001275 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001276 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001277
1278 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001279 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001280
Sylvain Munaut30a15382009-12-24 00:27:26 +01001281 if (cb)
1282 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001283 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001284
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001285 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001286 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001287 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001288 }
1289
Sylvain Munautc593cf12010-06-10 23:51:41 +02001290 DEBUGPC(DMM, "OK\n");
1291
Sylvain Munaut30a15382009-12-24 00:27:26 +01001292 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001293 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001294 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001295}
1296
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001297static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1298{
1299 struct gsm48_hdr *gh = msgb_l3(msg);
1300 uint8_t cause;
1301 uint8_t auts_tag;
1302 uint8_t auts_len;
1303 uint8_t *auts;
1304 int rc;
1305
1306 if (!conn->sec_operation) {
1307 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION FAILURE:"
1308 " No authentication/cipher operation in progress\n",
1309 subscr_name(conn->subscr));
1310 return -EINVAL;
1311 }
1312
1313 if (!conn->subscr) {
1314 LOGP(DMM, LOGL_ERROR,
1315 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
1316 gsm48_tx_mm_auth_rej(conn);
1317 release_security_operation(conn);
1318 return -EINVAL;
1319 }
1320
1321 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1322 LOGP(DMM, LOGL_ERROR,
1323 "%s: MM R99 AUTHENTICATION FAILURE:"
1324 " l3 length invalid: %u\n",
1325 subscr_name(conn->subscr), msgb_l3len(msg));
1326 gsm48_tx_mm_auth_rej(conn);
1327 release_security_operation(conn);
1328 return -EINVAL;
1329 }
1330
1331 cause = gh->data[0];
1332
1333 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1334 LOGP(DMM, LOGL_INFO,
1335 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
1336 subscr_name(conn->subscr), cause);
1337 rc = gsm48_tx_mm_auth_rej(conn);
1338 release_security_operation(conn);
1339 return rc;
1340 }
1341
1342 /* This is a Synch Failure procedure, which should pass an AUTS to
1343 * resynchronize the sequence nr with the HLR. Expecting exactly one
1344 * TLV with 14 bytes of AUTS. */
1345
1346 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1347 LOGP(DMM, LOGL_INFO,
1348 "%s: MM R99 AUTHENTICATION FAILURE:"
1349 " invalid Synch Failure: missing AUTS IE\n",
1350 subscr_name(conn->subscr));
1351 gsm48_tx_mm_auth_rej(conn);
1352 release_security_operation(conn);
1353 return -EINVAL;
1354 }
1355
1356 auts_tag = gh->data[1];
1357 auts_len = gh->data[2];
1358 auts = &gh->data[3];
1359
1360 if (auts_tag != GSM48_IE_AUTS
1361 || auts_len != 14) {
1362 LOGP(DMM, LOGL_INFO,
1363 "%s: MM R99 AUTHENTICATION FAILURE:"
1364 " invalid Synch Failure:"
1365 " expected AUTS IE 0x%02x of 14 bytes,"
1366 " got IE 0x%02x of %u bytes\n",
1367 subscr_name(conn->subscr),
1368 GSM48_IE_AUTS, auts_tag, auts_len);
1369 gsm48_tx_mm_auth_rej(conn);
1370 release_security_operation(conn);
1371 return -EINVAL;
1372 }
1373
1374 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1375 LOGP(DMM, LOGL_INFO,
1376 "%s: MM R99 AUTHENTICATION FAILURE:"
1377 " invalid Synch Failure msg: message truncated (%u)\n",
1378 subscr_name(conn->subscr), msgb_l3len(msg));
1379 gsm48_tx_mm_auth_rej(conn);
1380 release_security_operation(conn);
1381 return -EINVAL;
1382 }
1383
1384 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1385 * large enough. */
1386
1387 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
1388 subscr_name(conn->subscr), osmo_hexdump_nospc(auts, 14));
1389
1390 /* Future: vlr_sub_rx_auth_fail(conn->vsub, auts); */
1391
1392 LOGP(DMM, LOGL_ERROR, "%s: MM R99 AUTHENTICATION not supported\n",
1393 subscr_name(conn->subscr));
1394 rc = gsm48_tx_mm_auth_rej(conn);
1395 release_security_operation(conn);
1396 return rc;
1397}
1398
Harald Weltebf5e8df2009-02-03 12:59:45 +00001399/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001400static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001401{
1402 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001404
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001405 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001406 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001407 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001408 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001409 break;
1410 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001411 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001412 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001413 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001414 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001415 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001416 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001417 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001418 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001419 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001420 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001421 subscr_name(conn->subscr));
Neels Hofmeyr36891a72016-05-09 13:18:03 +02001422 loc_updating_success(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001423 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001424 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001425 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001426 break;
1427 case GSM48_MT_MM_CM_REEST_REQ:
1428 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1429 break;
1430 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001431 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001432 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001433 case GSM48_MT_MM_AUTH_FAIL:
1434 rc = gsm48_rx_mm_auth_fail(conn, msg);
1435 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001436 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001437 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001438 gh->msg_type);
1439 break;
1440 }
1441
1442 return rc;
1443}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001444
Harald Welte2d35ae62009-02-06 12:02:13 +00001445/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001446static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001447{
1448 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001449 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001450 uint8_t *classmark2_lv = gh->data + 1;
1451 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001452 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 struct gsm_subscriber *subscr = NULL;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001454 struct bsc_subscr *bsub;
1455 uint32_t tmsi;
Harald Welte2d35ae62009-02-06 12:02:13 +00001456 int rc = 0;
1457
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001458 resp = (struct gsm48_pag_resp *) &gh->data[0];
1459 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1460 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001461 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1462 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001463
Harald Weltefe18d8f2009-02-22 21:14:24 +00001464 switch (mi_type) {
1465 case GSM_MI_TYPE_TMSI:
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001466 tmsi = tmsi_from_string(mi_string);
1467 subscr = subscr_get_by_tmsi(conn->network->subscr_group, tmsi);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001468 break;
1469 case GSM_MI_TYPE_IMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001470 subscr = subscr_get_by_imsi(conn->network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001471 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001472 break;
1473 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001474
1475 if (!subscr) {
1476 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001477 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001478 return -EINVAL;
1479 }
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001480
1481 if (!conn->subscr) {
1482 conn->subscr = subscr;
1483 } else if (conn->subscr != subscr) {
1484 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
1485 subscr_put(subscr);
1486 return -EINVAL;
1487 } else {
1488 DEBUGP(DRR, "<- Channel already owned by us\n");
1489 subscr_put(subscr);
1490 subscr = conn->subscr;
1491 }
1492
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01001493 log_set_context(LOG_CTX_VLR_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001494 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001495 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001496
Harald Weltec2e302d2009-07-05 14:08:13 +02001497 subscr->equipment.classmark2_len = *classmark2_lv;
1498 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1499 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001500
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001501 /* TODO MSC split -- creating a BSC subscriber directly from MSC data
1502 * structures in RAM. At some point the MSC will send a message to the
1503 * BSC instead. */
1504 bsub = bsc_subscr_find_or_create_by_imsi(conn->network->bsc_subscribers,
1505 subscr->imsi);
1506 bsub->tmsi = subscr->tmsi;
1507 bsub->lac = subscr->lac;
1508
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001509 /* We received a paging */
1510 conn->expire_timer_stopped = 1;
1511
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001512 rc = gsm48_handle_paging_resp(conn, msg, bsub);
Harald Welte2d35ae62009-02-06 12:02:13 +00001513 return rc;
1514}
1515
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001516static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001517{
1518 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001519 uint8_t apdu_id_flags;
1520 uint8_t apdu_len;
1521 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001522
1523 apdu_id_flags = gh->data[0];
1524 apdu_len = gh->data[1];
1525 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001526
Max8dc8f232017-02-09 19:13:02 +01001527 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001528 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001529
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001530 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001531}
1532
Harald Weltebf5e8df2009-02-03 12:59:45 +00001533/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001534static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001535{
1536 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001537 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001538
1539 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001540 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001541 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001542 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001543 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001544 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001545 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001546 default:
Philippd37981e2016-10-10 15:11:41 +02001547 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1548 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001549 break;
1550 }
1551
Harald Welte2d35ae62009-02-06 12:02:13 +00001552 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001553}
1554
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001555int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1556 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001557{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001558 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001559 struct gsm48_hdr *gh;
1560
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001561 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001562
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001563 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1564 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001565
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001566 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1567 gh->proto_discr = GSM48_PDISC_RR;
1568 gh->msg_type = GSM48_MT_RR_APP_INFO;
1569 gh->data[0] = apdu_id;
1570 gh->data[1] = apdu_len;
1571 memcpy(gh->data+2, apdu, apdu_len);
1572
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001573 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001574}
1575
Alexander Couzens92f552f2016-08-23 07:32:27 +02001576/* FIXME: this count_statistics is a state machine behaviour. we should convert
1577 * the complete call control into a state machine. Afterwards we can move this
1578 * code into state transitions.
1579 */
1580static void count_statistics(struct gsm_trans *trans, int new_state)
1581{
1582 int old_state = trans->cc.state;
1583 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1584
1585 if (old_state == new_state)
1586 return;
1587
1588 /* state incoming */
1589 switch (new_state) {
1590 case GSM_CSTATE_ACTIVE:
1591 osmo_counter_inc(trans->net->active_calls);
1592 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1593 break;
1594 }
1595
1596 /* state outgoing */
1597 switch (old_state) {
1598 case GSM_CSTATE_ACTIVE:
1599 osmo_counter_dec(trans->net->active_calls);
1600 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1601 new_state == GSM_CSTATE_DISCONNECT_IND)
1602 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1603 else
1604 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1605 break;
1606 }
1607}
1608
Harald Welte4bc90a12008-12-27 16:32:52 +00001609/* Call Control */
1610
Harald Welte7584aea2009-02-11 11:44:12 +00001611/* The entire call control code is written in accordance with Figure 7.10c
1612 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1613 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1614 * it for voice */
1615
Harald Welte4bfdfe72009-06-10 23:11:52 +08001616static void new_cc_state(struct gsm_trans *trans, int state)
1617{
1618 if (state > 31 || state < 0)
1619 return;
1620
1621 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001622 gsm48_cc_state_name(trans->cc.state),
1623 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624
Alexander Couzens92f552f2016-08-23 07:32:27 +02001625 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001626 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627}
1628
1629static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001630{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001631 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001632 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001633 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001634
Harald Welte4bc90a12008-12-27 16:32:52 +00001635 gh->msg_type = GSM48_MT_CC_STATUS;
1636
1637 cause = msgb_put(msg, 3);
1638 cause[0] = 2;
1639 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1640 cause[2] = 0x80 | 30; /* response to status inquiry */
1641
1642 call_state = msgb_put(msg, 1);
1643 call_state[0] = 0xc0 | 0x00;
1644
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001645 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001646}
1647
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001648static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001649 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001650{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001651 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001652 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1653
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001654 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001655
Harald Welte6f4b7532008-12-29 00:39:37 +00001656 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001657 gh->msg_type = msg_type;
1658
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001659 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001660}
1661
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1663{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001664 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001665 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001666 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001667 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 }
1669}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001670
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1672 int msg_type, struct gsm_mncc *mncc)
1673{
1674 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001675 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676
1677 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001678 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001679 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001681 trans->conn->lchan->ts->trx->bts->nr,
1682 trans->conn->lchan->ts->trx->nr,
1683 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 (trans->subscr)?(trans->subscr->extension):"-",
1685 get_mncc_name(msg_type));
1686 else
1687 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1688 "Sending '%s' to MNCC.\n",
1689 (trans->subscr)?(trans->subscr->extension):"-",
1690 get_mncc_name(msg_type));
1691 else
1692 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1693 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1694
1695 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001696
Harald Welte966636f2009-06-26 19:39:35 +02001697 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001698 if (!msg)
1699 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001700
1701 data = msgb_put(msg, sizeof(struct gsm_mncc));
1702 memcpy(data, mncc, sizeof(struct gsm_mncc));
1703
Harald Welte54209c22010-12-22 23:43:29 +01001704 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705
1706 return 0;
1707}
1708
1709int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001710 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711{
1712 struct gsm_mncc rel;
1713
Harald Welte92f70c52009-06-12 01:54:08 +08001714 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001716 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001717 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1718 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1720}
1721
Harald Weltedcaf5652009-07-23 18:56:43 +02001722/* Call Control Specific transaction release.
1723 * gets called by trans_free, DO NOT CALL YOURSELF! */
1724void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 gsm48_stop_cc_timer(trans);
1727
1728 /* send release to L4, if callref still exists */
1729 if (trans->callref) {
1730 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001731 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001732 GSM48_CAUSE_LOC_PRN_S_LU,
1733 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001735 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001737 if (trans->conn)
1738 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739}
1740
1741static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001742
Harald Welte09e38af2009-02-16 22:52:23 +00001743/* call-back from paging the B-end of the connection */
1744static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001745 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001746{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001747 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001748 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001749
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001750 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001751
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001753 switch (event) {
1754 case GSM_PAGING_SUCCEEDED:
1755 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001756 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001757 /* Assign lchan */
1758 transt->conn = conn;
1759 /* send SETUP request to called party */
1760 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1761 break;
1762 case GSM_PAGING_EXPIRED:
1763 case GSM_PAGING_BUSY:
1764 DEBUGP(DCC, "Paging subscr %s expired!\n",
1765 transt->subscr->extension);
1766 /* Temporarily out of order */
1767 mncc_release_ind(transt->net, transt,
1768 transt->callref,
1769 GSM48_CAUSE_LOC_PRN_S_LU,
1770 GSM48_CC_CAUSE_DEST_OOO);
1771 transt->callref = 0;
1772 transt->paging_request = NULL;
1773 trans_free(transt);
1774 break;
1775 default:
1776 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1777 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001779
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001780 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001781 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001782}
Harald Welte7584aea2009-02-11 11:44:12 +00001783
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001784static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001785
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001786/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001787static int switch_for_handover(struct gsm_lchan *old_lchan,
1788 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001789{
1790 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001791
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001792 /* Ask the new socket to send to the already known port. */
1793 if (new_lchan->conn->mncc_rtp_bridge) {
1794 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1795 rsl_ipacc_mdcx(new_lchan,
1796 old_lchan->abis_ip.connect_ip,
1797 old_lchan->abis_ip.connect_port, 0);
1798 return 0;
1799 }
1800
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001801 if (ipacc_rtp_direct) {
1802 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1803 return 0;
1804 }
1805
1806 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001807 new_rs = new_lchan->abis_ip.rtp_socket;
1808 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001809
1810 if (!new_rs) {
1811 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1812 return -EIO;
1813 }
1814
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001815 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001816
1817 if (!old_rs) {
1818 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1819 return -EIO;
1820 }
1821
1822 /* copy rx_action and reference to other sock */
1823 new_rs->rx_action = old_rs->rx_action;
1824 new_rs->tx_action = old_rs->tx_action;
1825 new_rs->transmit = old_rs->transmit;
1826
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001827 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001828 case RTP_PROXY:
1829 other_rs = old_rs->proxy.other_sock;
1830 rtp_socket_proxy(new_rs, other_rs);
1831 /* delete reference to other end socket to prevent
1832 * rtp_socket_free() from removing the inverse reference */
1833 old_rs->proxy.other_sock = NULL;
1834 break;
1835 case RTP_RECV_UPSTREAM:
1836 new_rs->receive = old_rs->receive;
1837 break;
1838 case RTP_NONE:
1839 break;
1840 }
1841
1842 return 0;
1843}
1844
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001845static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1846{
1847 struct gsm_lchan *old_lchan;
1848 old_lchan = bsc_handover_pending(lchan);
1849 if (old_lchan)
1850 switch_for_handover(old_lchan, lchan);
1851}
1852
Harald Welte805f6442009-07-28 18:25:29 +02001853/* some other part of the code sends us a signal */
1854static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1855 void *handler_data, void *signal_data)
1856{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001857 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001858 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001859 struct gsm_network *net;
1860 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001861
1862 if (subsys != SS_ABISIP)
1863 return 0;
1864
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001865 /* RTP bridge handling */
1866 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1867 return tch_rtp_signal(lchan, signal);
1868
Harald Welte805f6442009-07-28 18:25:29 +02001869 /* in case we use direct BTS-to-BTS RTP */
1870 if (ipacc_rtp_direct)
1871 return 0;
1872
Harald Welte805f6442009-07-28 18:25:29 +02001873 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001874 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001875 /* in case we don't use direct BTS-to-BTS RTP */
1876 /* the BTS has successfully bound a TCH to a local ip/port,
1877 * which means we can connect our UDP socket to it */
1878 if (lchan->abis_ip.rtp_socket) {
1879 rtp_socket_free(lchan->abis_ip.rtp_socket);
1880 lchan->abis_ip.rtp_socket = NULL;
1881 }
1882
1883 lchan->abis_ip.rtp_socket = rtp_socket_create();
1884 if (!lchan->abis_ip.rtp_socket)
1885 return -EIO;
1886
1887 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1888 lchan->abis_ip.bound_ip,
1889 lchan->abis_ip.bound_port);
1890 if (rc < 0)
1891 return -EIO;
1892
Harald Welteda7ab742009-12-19 22:23:05 +01001893 /* check if any transactions on this lchan still have
1894 * a tch_recv_mncc request pending */
1895 net = lchan->ts->trx->bts->network;
1896 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001897 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001898 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1899 tch_recv_mncc(net, trans->callref, 1);
1900 }
1901 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001902
1903 /*
1904 * TODO: this appears to be too early? Why not until after
1905 * the handover detect or the handover complete?
1906 *
1907 * Do we have a handover pending for this new lchan? In that
1908 * case re-route the audio from the old channel to the new one.
1909 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001910 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001911 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001912 case S_ABISIP_DLCX_IND:
1913 /* the BTS tells us a RTP stream has been disconnected */
1914 if (lchan->abis_ip.rtp_socket) {
1915 rtp_socket_free(lchan->abis_ip.rtp_socket);
1916 lchan->abis_ip.rtp_socket = NULL;
1917 }
1918
1919 break;
Harald Welte805f6442009-07-28 18:25:29 +02001920 }
1921
1922 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001923}
1924
Harald Welte49f48b82009-02-17 15:29:33 +00001925/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001926static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001927{
Harald Welte11fa29c2009-02-19 17:24:39 +00001928 struct gsm_bts *bts = lchan->ts->trx->bts;
1929 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001930 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001931 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001932 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001933
Max8db12e42016-04-18 23:11:18 +02001934 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1935 "(bts=%u,trx=%u,ts=%u,%s)\n",
1936 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1937 get_value_string(gsm_chan_t_names, lt),
1938 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1939 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001940
1941 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001942 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001943 return -EINVAL;
1944 }
Harald Welteda7ab742009-12-19 22:23:05 +01001945
Max3ffce192016-04-25 15:22:00 +02001946 if (lt != rt) {
1947 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1948 " channel types: local = %s, remote = %s\n",
1949 get_value_string(gsm_chan_t_names, lt),
1950 get_value_string(gsm_chan_t_names, rt));
1951 return -EBADSLT;
1952 }
1953
1954 if (lm != rm) {
1955 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1956 " channel modes: local = %s, remote = %s\n",
1957 get_value_string(gsm48_chan_mode_names, lm),
1958 get_value_string(gsm48_chan_mode_names, rm));
1959 return -EMEDIUMTYPE;
1960 }
1961
Harald Welteda7ab742009-12-19 22:23:05 +01001962 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001963 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001964 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01001965 case GSM_BTS_TYPE_OSMOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001966 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001967 if (!lchan->abis_ip.rtp_socket) {
1968 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1969 "lchan\n");
1970 return -EIO;
1971 }
1972 if (!remote_lchan->abis_ip.rtp_socket) {
1973 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1974 "remote_lchan\n");
1975 return -EIO;
1976 }
1977
Harald Welte805f6442009-07-28 18:25:29 +02001978 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001979 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001980 if (rc < 0)
1981 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001982 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001983 if (rc < 0)
1984 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001985 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301986 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1987 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001988 } else {
1989 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301990 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1991 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301992 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001993 if (rc < 0)
1994 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301995 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1996 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301997 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001998 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001999 break;
2000 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002001 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02002002 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00002003 trau_mux_map_lchan(lchan, remote_lchan);
2004 break;
2005 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002006 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002007 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00002008 }
Harald Welte49f48b82009-02-17 15:29:33 +00002009
2010 return 0;
2011}
2012
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01002014static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00002015{
Harald Welte53d51f52015-12-03 14:59:04 +01002016 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
2017 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002018
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002020 return -EIO;
2021
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002022 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 return -EIO;
2024
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002025 /* Which subscriber do we want to track trans1 or trans2? */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01002026 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002027
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002029 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002030}
2031
Harald Welteda7ab742009-12-19 22:23:05 +01002032/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002033static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034{
2035 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01002036 struct gsm_lchan *lchan;
2037 struct gsm_bts *bts;
2038 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00002039
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01002041 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 if (!trans)
2043 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002044 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002046
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01002047 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002048 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01002049 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002051 /* store receive state */
2052 trans->tch_recv = enable;
2053
Harald Welteda7ab742009-12-19 22:23:05 +01002054 switch (bts->type) {
2055 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01002056 case GSM_BTS_TYPE_OSMOBTS:
Harald Welteda7ab742009-12-19 22:23:05 +01002057 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02002058 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002059 return -EINVAL;
2060 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002061 /* In case, we don't have a RTP socket to the BTS yet, the BTS
2062 * will not be connected to our RTP proxy and the socket will
2063 * not be assigned to the application interface. This method
2064 * will be called again, once the audio socket is created and
2065 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01002066 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01002067 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2068 return 0;
2069 }
2070 if (enable) {
2071 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01002072 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01002073 if (rc < 0)
2074 return rc;
2075 /* assign socket to application interface */
2076 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2077 net, callref);
2078 } else
2079 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2080 net, 0);
2081 break;
2082 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002083 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002084 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002085 /* In case we don't have a TCH with correct mode, the TRAU muxer
2086 * will not be asigned to the application interface. This is
2087 * performed by switch_trau_mux() after successful handover or
2088 * assignment. */
2089 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
2090 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2091 return 0;
2092 }
Harald Welteda7ab742009-12-19 22:23:05 +01002093 if (enable)
2094 return trau_recv_lchan(lchan, callref);
2095 return trau_mux_unmap(NULL, callref);
2096 break;
2097 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002098 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002099 return -EINVAL;
2100 }
2101
2102 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103}
2104
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2106{
2107 DEBUGP(DCC, "-> STATUS ENQ\n");
2108 return gsm48_cc_tx_status(trans, msg);
2109}
2110
2111static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2112static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2113
2114static void gsm48_cc_timeout(void *arg)
2115{
2116 struct gsm_trans *trans = arg;
2117 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002118 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2119 int mo_location = GSM48_CAUSE_LOC_USER;
2120 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2121 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122 struct gsm_mncc mo_rel, l4_rel;
2123
2124 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2125 mo_rel.callref = trans->callref;
2126 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2127 l4_rel.callref = trans->callref;
2128
Harald Weltedcaf5652009-07-23 18:56:43 +02002129 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 case 0x303:
2131 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002132 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133 break;
2134 case 0x310:
2135 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002136 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 break;
2138 case 0x313:
2139 disconnect = 1;
2140 /* unknown, did not find it in the specs */
2141 break;
2142 case 0x301:
2143 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002144 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 break;
2146 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002147 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002149 gsm48_cc_tx_release(trans, &trans->cc.msg);
2150 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 break; /* stay in release state */
2152 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002153 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154 return;
2155// release = 1;
2156// l4_cause = 14;
2157// break;
2158 case 0x306:
2159 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002160 mo_cause = trans->cc.msg.cause.value;
2161 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 break;
2163 case 0x323:
2164 disconnect = 1;
2165 break;
2166 default:
2167 release = 1;
2168 }
2169
2170 if (release && trans->callref) {
2171 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002172 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 l4_location, l4_cause);
2174 trans->callref = 0;
2175 }
2176
2177 if (disconnect && trans->callref) {
2178 /* process disconnect towards layer 4 */
2179 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002180 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 }
2182
2183 /* process disconnect towards mobile station */
2184 if (disconnect || release) {
2185 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002186 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2187 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2188 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189 mo_rel.cause.diag_len = 3;
2190
2191 if (disconnect)
2192 gsm48_cc_tx_disconnect(trans, &mo_rel);
2193 if (release)
2194 gsm48_cc_tx_release(trans, &mo_rel);
2195 }
2196
2197}
2198
Max3ffce192016-04-25 15:22:00 +02002199/* disconnect both calls from the bridge */
2200static inline void disconnect_bridge(struct gsm_network *net,
2201 struct gsm_mncc_bridge *bridge, int err)
2202{
2203 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
2204 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
2205 struct gsm_mncc mx_rel;
2206 if (!trans0 || !trans1)
2207 return;
2208
2209 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
2210 trans0->callref, trans1->callref, strerror(err));
2211
2212 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
2213 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
2214 GSM48_CC_CAUSE_CHAN_UNACCEPT);
2215
2216 mx_rel.callref = trans0->callref;
2217 gsm48_cc_tx_disconnect(trans0, &mx_rel);
2218
2219 mx_rel.callref = trans1->callref;
2220 gsm48_cc_tx_disconnect(trans1, &mx_rel);
2221}
2222
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2224 int sec, int micro)
2225{
2226 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02002227 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02002228 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02002229 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230}
2231
2232static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2233{
2234 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002235 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2237 struct tlv_parsed tp;
2238 struct gsm_mncc setup;
2239
2240 memset(&setup, 0, sizeof(struct gsm_mncc));
2241 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002242 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002243 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 /* emergency setup is identified by msg_type */
2245 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2246 setup.emergency = 1;
2247
2248 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002249 setup.fields |= MNCC_F_CALLING;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002250 osmo_strlcpy(setup.calling.number, trans->subscr->extension,
2251 sizeof(setup.calling.number));
2252 osmo_strlcpy(setup.imsi, trans->subscr->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002253
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 /* bearer capability */
2255 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2256 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002259 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 }
2261 /* facility */
2262 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2263 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2266 }
2267 /* called party bcd number */
2268 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2269 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01002270 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2272 }
2273 /* user-user */
2274 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2275 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002276 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2278 }
2279 /* ss-version */
2280 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2281 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002282 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2284 }
2285 /* CLIR suppression */
2286 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2287 setup.clir.sup = 1;
2288 /* CLIR invocation */
2289 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2290 setup.clir.inv = 1;
2291 /* cc cap */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2293 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2296 }
2297
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 new_cc_state(trans, GSM_CSTATE_INITIATED);
2299
Harald Welte (local)47df3992009-12-26 19:45:03 +01002300 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2301 subscr_name(trans->subscr), trans->subscr->extension,
2302 setup.called.number);
2303
Alexander Couzensb847a212016-08-02 11:34:11 +02002304 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002305
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002307 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308
Harald Welte13cac662009-07-29 12:10:35 +02002309 /* MNCC code will modify the channel asynchronously, we should
2310 * ipaccess-bind only after the modification has been made to the
2311 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 return 0;
2313}
2314
2315static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002316{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002317 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002318 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002320 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002321
Harald Welte7ccf7782009-02-17 01:43:01 +00002322 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002323
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* transaction id must not be assigned */
2325 if (trans->transaction_id != 0xff) { /* unasssigned */
2326 DEBUGP(DCC, "TX Setup with assigned transaction. "
2327 "This is not allowed!\n");
2328 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002329 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002330 GSM48_CAUSE_LOC_PRN_S_LU,
2331 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002333 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 return rc;
2335 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002336
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002338 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2339 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002340 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002342 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002343 GSM48_CAUSE_LOC_PRN_S_LU,
2344 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002346 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 return rc;
2348 }
Harald Welte78283ef2009-07-23 21:36:44 +02002349 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002350
Harald Welte65e74cc2008-12-29 01:55:35 +00002351 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002352
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002354
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 /* bearer capability */
2356 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002357 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 /* facility */
2359 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 /* progress */
2362 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002363 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 /* calling party BCD number */
2365 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002366 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 /* called party BCD number */
2368 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002369 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 /* user-user */
2371 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002372 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373 /* redirecting party BCD number */
2374 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002375 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 /* signal */
2377 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002378 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002379
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002381
Alexander Couzensb847a212016-08-02 11:34:11 +02002382 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002383
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002384 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002385}
2386
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2388{
2389 struct gsm48_hdr *gh = msgb_l3(msg);
2390 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2391 struct tlv_parsed tp;
2392 struct gsm_mncc call_conf;
2393
2394 gsm48_stop_cc_timer(trans);
2395 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2396
2397 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2398 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002399 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002400 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401#if 0
2402 /* repeat */
2403 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2404 call_conf.repeat = 1;
2405 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2406 call_conf.repeat = 2;
2407#endif
2408 /* bearer capability */
2409 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2410 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002413 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 }
2415 /* cause */
2416 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2417 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002418 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2420 }
2421 /* cc cap */
2422 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2423 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002424 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2426 }
2427
Andreas Eversberg035b8742013-09-19 09:27:06 +02002428 /* IMSI of called subscriber */
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002429 osmo_strlcpy(call_conf.imsi, trans->subscr->imsi,
2430 sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02002431
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2433
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002434 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002435 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436}
2437
2438static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2439{
2440 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002441 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2443
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2445
2446 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2447
2448 /* bearer capability */
2449 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002450 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 /* facility */
2452 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002453 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 /* progress */
2455 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002458 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459}
2460
2461static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2462{
2463 struct gsm48_hdr *gh = msgb_l3(msg);
2464 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2465 struct tlv_parsed tp;
2466 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002467
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 gsm48_stop_cc_timer(trans);
2469 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2470
2471 memset(&alerting, 0, sizeof(struct gsm_mncc));
2472 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002473 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 /* facility */
2475 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2476 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002477 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2479 }
2480
2481 /* progress */
2482 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2483 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002484 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2486 }
2487 /* ss-version */
2488 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2489 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002490 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2492 }
2493
2494 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2495
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002496 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002497 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
2500static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2501{
2502 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002503 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2505
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 gh->msg_type = GSM48_MT_CC_ALERTING;
2507
2508 /* facility */
2509 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002510 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 /* progress */
2512 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002513 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 /* user-user */
2515 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002516 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517
2518 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002519
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_progress(struct gsm_trans *trans, void *arg)
2524{
2525 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002526 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
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_PROGRESS;
2530
2531 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002532 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 /* user-user */
2534 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002535 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002537 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538}
2539
2540static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2541{
2542 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002543 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2545
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546 gh->msg_type = GSM48_MT_CC_CONNECT;
2547
2548 gsm48_stop_cc_timer(trans);
2549 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2550
2551 /* facility */
2552 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 /* progress */
2555 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002556 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 /* connected number */
2558 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002559 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 /* user-user */
2561 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002562 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563
2564 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2565
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002566 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567}
2568
2569static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2570{
2571 struct gsm48_hdr *gh = msgb_l3(msg);
2572 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2573 struct tlv_parsed tp;
2574 struct gsm_mncc connect;
2575
2576 gsm48_stop_cc_timer(trans);
2577
2578 memset(&connect, 0, sizeof(struct gsm_mncc));
2579 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002580 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002582 connect.fields |= MNCC_F_CONNECTED;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002583 osmo_strlcpy(connect.connected.number, trans->subscr->extension,
2584 sizeof(connect.connected.number));
2585 osmo_strlcpy(connect.imsi, trans->subscr->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002586
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587 /* facility */
2588 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2589 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002590 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2592 }
2593 /* user-user */
2594 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2595 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2598 }
2599 /* ss-version */
2600 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2601 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002602 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2604 }
2605
2606 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002607 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002609 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610}
2611
2612
2613static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2614{
2615 struct gsm_mncc connect_ack;
2616
2617 gsm48_stop_cc_timer(trans);
2618
2619 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002620 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002621
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2623 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002624
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002625 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 &connect_ack);
2627}
2628
2629static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2630{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002631 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2633
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2635
2636 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2637
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002638 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639}
2640
2641static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2642{
2643 struct gsm48_hdr *gh = msgb_l3(msg);
2644 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2645 struct tlv_parsed tp;
2646 struct gsm_mncc disc;
2647
2648 gsm48_stop_cc_timer(trans);
2649
2650 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2651
2652 memset(&disc, 0, sizeof(struct gsm_mncc));
2653 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002654 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 /* cause */
2656 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2657 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002658 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2660 }
2661 /* facility */
2662 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2663 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002664 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2666 }
2667 /* user-user */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2669 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002670 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2672 }
2673 /* ss-version */
2674 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2675 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2678 }
2679
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002680 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681
2682}
2683
Harald Weltec66b71c2009-06-11 14:23:20 +08002684static struct gsm_mncc_cause default_cause = {
2685 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2686 .coding = 0,
2687 .rec = 0,
2688 .rec_val = 0,
2689 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2690 .diag_len = 0,
2691 .diag = { 0 },
2692};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693
2694static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2695{
2696 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002697 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2699
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2701
2702 gsm48_stop_cc_timer(trans);
2703 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2704
2705 /* cause */
2706 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002707 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 else
Harald Welte55c8f352010-03-07 23:40:35 +01002709 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710
2711 /* facility */
2712 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002713 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 /* progress */
2715 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002716 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 /* user-user */
2718 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002719 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720
2721 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002722 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723
2724 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2725
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002726 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727}
2728
2729static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2730{
2731 struct gsm48_hdr *gh = msgb_l3(msg);
2732 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2733 struct tlv_parsed tp;
2734 struct gsm_mncc rel;
2735 int rc;
2736
2737 gsm48_stop_cc_timer(trans);
2738
2739 memset(&rel, 0, sizeof(struct gsm_mncc));
2740 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002741 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 /* cause */
2743 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2744 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002745 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2747 }
2748 /* facility */
2749 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2750 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002751 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2753 }
2754 /* user-user */
2755 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2756 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002757 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2759 }
2760 /* ss-version */
2761 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2762 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002763 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2765 }
2766
Harald Weltedcaf5652009-07-23 18:56:43 +02002767 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002769 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002771 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002772 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002773 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002774 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775 }
2776
2777 new_cc_state(trans, GSM_CSTATE_NULL);
2778
2779 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002780 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002781
2782 return rc;
2783}
2784
2785static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2786{
2787 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002788 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2790
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 gh->msg_type = GSM48_MT_CC_RELEASE;
2792
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 gsm48_stop_cc_timer(trans);
2794 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2795
2796 /* cause */
2797 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002798 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002799 /* facility */
2800 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002801 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 /* user-user */
2803 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002804 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805
Harald Weltedcaf5652009-07-23 18:56:43 +02002806 trans->cc.T308_second = 0;
2807 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808
Harald Weltedcaf5652009-07-23 18:56:43 +02002809 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2811
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002812 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813}
2814
2815static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2816{
2817 struct gsm48_hdr *gh = msgb_l3(msg);
2818 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2819 struct tlv_parsed tp;
2820 struct gsm_mncc rel;
2821 int rc = 0;
2822
2823 gsm48_stop_cc_timer(trans);
2824
2825 memset(&rel, 0, sizeof(struct gsm_mncc));
2826 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002827 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828 /* cause */
2829 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2830 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002831 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2833 }
2834 /* facility */
2835 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2836 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002837 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2839 }
2840 /* user-user */
2841 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2842 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002843 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2845 }
2846 /* ss-version */
2847 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2848 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002849 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2851 }
2852
2853 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002854 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002856 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 MNCC_REJ_IND, &rel);
2858 break;
2859 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002860 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861 MNCC_REL_CNF, &rel);
2862 break;
2863 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002864 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 MNCC_REL_IND, &rel);
2866 }
2867 }
2868
2869 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002870 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871
2872 return rc;
2873}
2874
2875static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2876{
2877 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002878 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002880 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881
Harald Welte4bfdfe72009-06-10 23:11:52 +08002882 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2883
2884 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002885
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886 gsm48_stop_cc_timer(trans);
2887
2888 /* cause */
2889 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002890 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 /* facility */
2892 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002893 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 /* user-user */
2895 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002896 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897
Harald Weltec7782de2010-12-21 19:31:41 +01002898 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2899
Harald Weltedcaf5652009-07-23 18:56:43 +02002900 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901
Harald Weltec7782de2010-12-21 19:31:41 +01002902 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903}
2904
2905static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2906{
2907 struct gsm48_hdr *gh = msgb_l3(msg);
2908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2909 struct tlv_parsed tp;
2910 struct gsm_mncc fac;
2911
2912 memset(&fac, 0, sizeof(struct gsm_mncc));
2913 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002914 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915 /* facility */
2916 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2917 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002918 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2920 }
2921 /* ss-version */
2922 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2923 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002924 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2926 }
2927
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002928 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929}
2930
2931static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2932{
2933 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002934 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2936
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937 gh->msg_type = GSM48_MT_CC_FACILITY;
2938
2939 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002940 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002942 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943}
2944
2945static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2946{
2947 struct gsm_mncc hold;
2948
2949 memset(&hold, 0, sizeof(struct gsm_mncc));
2950 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002951 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952}
2953
2954static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2955{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002956 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2958
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2960
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_tx_hold_rej(struct gsm_trans *trans, void *arg)
2965{
2966 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002967 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2969
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2971
2972 /* cause */
2973 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002974 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 else
Harald Welte55c8f352010-03-07 23:40:35 +01002976 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002978 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979}
2980
2981static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2982{
2983 struct gsm_mncc retrieve;
2984
2985 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2986 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002987 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002988 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989}
2990
2991static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2992{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002993 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2995
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2997
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_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
3002{
3003 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003004 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3006
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 gh->msg_type = GSM48_MT_CC_RETR_REJ;
3008
3009 /* cause */
3010 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01003011 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 else
Harald Welte55c8f352010-03-07 23:40:35 +01003013 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003014
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003015 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016}
3017
3018static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
3019{
3020 struct gsm48_hdr *gh = msgb_l3(msg);
3021 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3022 struct tlv_parsed tp;
3023 struct gsm_mncc dtmf;
3024
3025 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3026 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003027 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028 /* keypad facility */
3029 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
3030 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01003031 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003032 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
3033 }
3034
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003035 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036}
3037
3038static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
3039{
3040 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003041 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3043
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3045
3046 /* keypad */
3047 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01003048 gsm48_encode_keypad(msg, dtmf->keypad);
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_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3054{
3055 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003056 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3058
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3060
3061 /* cause */
3062 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01003063 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 else
Harald Welte55c8f352010-03-07 23:40:35 +01003065 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003067 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068}
3069
3070static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3071{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003072 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3074
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3076
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003077 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078}
3079
3080static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3081{
3082 struct gsm_mncc dtmf;
3083
3084 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3085 dtmf.callref = trans->callref;
3086
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003087 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088}
3089
3090static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3091{
3092 struct gsm48_hdr *gh = msgb_l3(msg);
3093 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3094 struct tlv_parsed tp;
3095 struct gsm_mncc modify;
3096
3097 memset(&modify, 0, sizeof(struct gsm_mncc));
3098 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003099 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 /* bearer capability */
3101 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3102 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003103 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003105 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 }
3107
3108 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3109
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003110 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003111}
3112
3113static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3114{
3115 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003116 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003117 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3118
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 gh->msg_type = GSM48_MT_CC_MODIFY;
3120
3121 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3122
3123 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003124 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125
3126 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3127
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003128 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129}
3130
3131static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3132{
3133 struct gsm48_hdr *gh = msgb_l3(msg);
3134 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3135 struct tlv_parsed tp;
3136 struct gsm_mncc modify;
3137
3138 gsm48_stop_cc_timer(trans);
3139
3140 memset(&modify, 0, sizeof(struct gsm_mncc));
3141 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003142 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 /* bearer capability */
3144 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3145 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003146 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003147 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003148 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003149 }
3150
3151 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3152
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003153 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154}
3155
3156static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3157{
3158 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003159 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003160 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3161
Harald Welte4bfdfe72009-06-10 23:11:52 +08003162 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3163
3164 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003165 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166
3167 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3168
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003169 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170}
3171
3172static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3173{
3174 struct gsm48_hdr *gh = msgb_l3(msg);
3175 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3176 struct tlv_parsed tp;
3177 struct gsm_mncc modify;
3178
3179 gsm48_stop_cc_timer(trans);
3180
3181 memset(&modify, 0, sizeof(struct gsm_mncc));
3182 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003183 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003184 /* bearer capability */
3185 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3186 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003187 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003188 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003189 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003190 }
3191 /* cause */
3192 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3193 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01003194 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003195 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3196 }
3197
3198 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3199
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003200 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003201}
3202
3203static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3204{
3205 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3208
Harald Welte4bfdfe72009-06-10 23:11:52 +08003209 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3210
3211 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003212 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003213 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01003214 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003215
3216 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3217
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003218 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003219}
3220
3221static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3222{
3223 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003224 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003225 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3226
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227 gh->msg_type = GSM48_MT_CC_NOTIFY;
3228
3229 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01003230 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003231
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003232 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003233}
3234
3235static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3236{
3237 struct gsm48_hdr *gh = msgb_l3(msg);
3238 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3239// struct tlv_parsed tp;
3240 struct gsm_mncc notify;
3241
3242 memset(&notify, 0, sizeof(struct gsm_mncc));
3243 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003244// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01003246 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003248 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249}
3250
3251static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3252{
3253 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003254 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3256
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257 gh->msg_type = GSM48_MT_CC_USER_INFO;
3258
3259 /* user-user */
3260 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01003261 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 /* more data */
3263 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01003264 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003266 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003267}
3268
3269static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3270{
3271 struct gsm48_hdr *gh = msgb_l3(msg);
3272 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3273 struct tlv_parsed tp;
3274 struct gsm_mncc user;
3275
3276 memset(&user, 0, sizeof(struct gsm_mncc));
3277 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003278 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 /* user-user */
3280 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3281 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01003282 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3284 }
3285 /* more data */
3286 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3287 user.more = 1;
3288
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003289 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003290}
3291
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003292static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003293{
3294 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003295 struct gsm_lchan *lchan = trans->conn->lchan;
3296
3297 /*
3298 * We were forced to make an assignment a lot earlier and
3299 * we should avoid sending another assignment that might
3300 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3301 * In case of rtp-bridge it is too late to change things
3302 * here.
3303 */
3304 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3305 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003306
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003307 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3308 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003309}
3310
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003311static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3312 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3313 uint32_t payload_msg_type)
3314{
3315 uint8_t data[sizeof(struct gsm_mncc)];
3316 struct gsm_mncc_rtp *rtp;
3317
3318 memset(&data, 0, sizeof(data));
3319 rtp = (struct gsm_mncc_rtp *) &data[0];
3320
3321 rtp->callref = callref;
3322 rtp->msg_type = cmd;
3323 rtp->ip = addr;
3324 rtp->port = port;
3325 rtp->payload_type = payload_type;
3326 rtp->payload_msg_type = payload_msg_type;
3327 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3328}
3329
3330static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3331{
3332 struct gsm_lchan *lchan;
3333 int msg_type;
3334
3335 lchan = trans->conn->lchan;
3336 switch (lchan->abis_ip.rtp_payload) {
3337 case RTP_PT_GSM_FULL:
3338 msg_type = GSM_TCHF_FRAME;
3339 break;
3340 case RTP_PT_GSM_EFR:
3341 msg_type = GSM_TCHF_FRAME_EFR;
3342 break;
3343 case RTP_PT_GSM_HALF:
3344 msg_type = GSM_TCHH_FRAME;
3345 break;
3346 case RTP_PT_AMR:
3347 msg_type = GSM_TCH_FRAME_AMR;
3348 break;
3349 default:
3350 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3351 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3352 msg_type = 0;
3353 break;
3354 }
3355
3356 return mncc_recv_rtp(net, trans->callref, cmd,
3357 lchan->abis_ip.bound_ip,
3358 lchan->abis_ip.bound_port,
3359 lchan->abis_ip.rtp_payload,
3360 msg_type);
3361}
3362
3363static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3364{
3365 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3366}
3367
3368static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3369{
3370 struct gsm_bts *bts;
3371 struct gsm_lchan *lchan;
3372 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003373 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003374
3375 /* Find callref */
3376 trans = trans_find_by_callref(net, callref);
3377 if (!trans) {
3378 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3379 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3380 return -EIO;
3381 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003382 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003383 if (!trans->conn) {
3384 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3385 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3386 return 0;
3387 }
3388
3389 lchan = trans->conn->lchan;
3390 bts = lchan->ts->trx->bts;
3391 if (!is_ipaccess_bts(bts)) {
3392 /*
3393 * I want this to be straight forward and have no audio flow
3394 * through the nitb/osmo-mss system. This currently means that
3395 * this will not work with BS11/Nokia type BTS. We would need
3396 * to have a trau<->rtp bridge for these but still preferable
3397 * in another process.
3398 */
3399 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3400 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3401 return -EINVAL;
3402 }
3403
3404 trans->conn->mncc_rtp_bridge = 1;
3405 /*
3406 * *sigh* we need to pick a codec now. Pick the most generic one
3407 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003408 * similiar to the routine above.
3409 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003410 */
3411 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3412 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003413 m = mncc_codec_for_mode(lchan->type);
3414 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3415 get_value_string(gsm48_chan_mode_names, m),
3416 get_value_string(gsm_chan_t_names, lchan->type));
3417 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003418 lchan->type != GSM_LCHAN_TCH_H);
3419 }
3420
3421 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3422 return 0;
3423}
3424
3425static int tch_rtp_connect(struct gsm_network *net, void *arg)
3426{
3427 struct gsm_lchan *lchan;
3428 struct gsm_trans *trans;
3429 struct gsm_mncc_rtp *rtp = arg;
3430
3431 /* Find callref */
3432 trans = trans_find_by_callref(net, rtp->callref);
3433 if (!trans) {
3434 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3435 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3436 return -EIO;
3437 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003438 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003439 if (!trans->conn) {
3440 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3441 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3442 return 0;
3443 }
3444
3445 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003446 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3447 get_value_string(gsm48_chan_mode_names,
3448 mncc_codec_for_mode(lchan->type)),
3449 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003450
3451 /* TODO: Check if payload_msg_type is compatible with what we have */
3452 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3453 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3454 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3455 }
3456
3457 /*
3458 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3459 * complains about both rtp and rtp payload2 being present in the
3460 * same package!
3461 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003462 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003463 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3464}
3465
3466static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3467{
3468 struct gsm_network *net;
3469 struct gsm_trans *tmp, *trans = NULL;
3470
3471 net = lchan->ts->trx->bts->network;
3472 llist_for_each_entry(tmp, &net->trans_list, entry) {
3473 if (!tmp->conn)
3474 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003475 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003476 continue;
3477 trans = tmp;
3478 break;
3479 }
3480
3481 if (!trans) {
3482 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3483 gsm_lchan_name(lchan));
3484 return 0;
3485 }
3486
3487 switch (signal) {
3488 case S_ABISIP_CRCX_ACK:
3489 if (lchan->conn->mncc_rtp_create_pending) {
3490 lchan->conn->mncc_rtp_create_pending = 0;
3491 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3492 gsm_lchan_name(lchan));
3493 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3494 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003495 /*
3496 * TODO: this appears to be too early? Why not until after
3497 * the handover detect or the handover complete?
3498 */
3499 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003500 break;
3501 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003502 if (lchan->conn->mncc_rtp_connect_pending) {
3503 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003504 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3505 gsm_lchan_name(lchan));
3506 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3507 }
3508 break;
3509 }
3510
3511 return 0;
3512}
3513
3514
Harald Welte4bfdfe72009-06-10 23:11:52 +08003515static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003516 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003517 int type;
3518 int (*rout) (struct gsm_trans *trans, void *arg);
3519} downstatelist[] = {
3520 /* mobile originating call establishment */
3521 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3522 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3523 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3524 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3525 {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 */
3526 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3527 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3528 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3529 /* mobile terminating call establishment */
3530 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3531 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3532 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3533 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3534 /* signalling during call */
3535 {SBIT(GSM_CSTATE_ACTIVE),
3536 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3537 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3538 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3539 {ALL_STATES,
3540 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3541 {ALL_STATES,
3542 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3543 {ALL_STATES,
3544 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3545 {SBIT(GSM_CSTATE_ACTIVE),
3546 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3547 {SBIT(GSM_CSTATE_ACTIVE),
3548 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3549 {SBIT(GSM_CSTATE_ACTIVE),
3550 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3551 {SBIT(GSM_CSTATE_ACTIVE),
3552 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3553 {SBIT(GSM_CSTATE_ACTIVE),
3554 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3555 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3556 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3557 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3558 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3559 {SBIT(GSM_CSTATE_ACTIVE),
3560 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3561 /* clearing */
3562 {SBIT(GSM_CSTATE_INITIATED),
3563 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3564 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3565 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3566 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3567 MNCC_REL_REQ, gsm48_cc_tx_release},
3568 /* special */
3569 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003570 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003571};
3572
3573#define DOWNSLLEN \
3574 (sizeof(downstatelist) / sizeof(struct downstate))
3575
3576
Harald Welte76556372010-12-22 23:57:45 +01003577int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003578{
Harald Welte1a6f7982009-08-09 18:52:33 +02003579 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003581 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003582 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003583 struct gsm_mncc *data = arg, rel;
3584
Harald Welte04dc88f2010-12-22 21:45:05 +01003585 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3586
Harald Welte4bfdfe72009-06-10 23:11:52 +08003587 /* handle special messages */
3588 switch(msg_type) {
3589 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003590 rc = tch_bridge(net, arg);
3591 if (rc < 0)
3592 disconnect_bridge(net, arg, -rc);
3593 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003594 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003595 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003596 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003597 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003598 case MNCC_RTP_CREATE:
3599 return tch_rtp_create(net, data->callref);
3600 case MNCC_RTP_CONNECT:
3601 return tch_rtp_connect(net, arg);
3602 case MNCC_RTP_FREE:
3603 /* unused right now */
3604 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003605 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003606 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003607 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003608 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003609 /* Find callref */
3610 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003611 if (!trans) {
3612 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003613 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003614 }
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003615 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003616 if (!trans->conn) {
3617 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003618 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003619 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003620 if (!trans->conn->lchan) {
3621 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3622 return 0;
3623 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003624 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3625 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003626 /* This should be LOGL_ERROR or NOTICE, but
3627 * unfortuantely it happens for a couple of frames at
3628 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003629 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003630 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003631 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003632 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003633 switch (bts->type) {
3634 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01003635 case GSM_BTS_TYPE_OSMOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01003636 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003637 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003638 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003639 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003640 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003641 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003642 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003643 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003644 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003645 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003646 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003647 }
3648 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003649 }
3650
3651 memset(&rel, 0, sizeof(struct gsm_mncc));
3652 rel.callref = data->callref;
3653
3654 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003655 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003656
3657 /* Callref unknown */
3658 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003659 struct gsm_subscriber *subscr;
3660
Harald Welte4a3464c2009-07-04 10:11:24 +02003661 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003662 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3663 "Received '%s' from MNCC with "
3664 "unknown callref %d\n", data->called.number,
3665 get_mncc_name(msg_type), data->callref);
3666 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003667 return mncc_release_ind(net, NULL, data->callref,
3668 GSM48_CAUSE_LOC_PRN_S_LU,
3669 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003670 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003671 if (!data->called.number[0] && !data->imsi[0]) {
3672 DEBUGP(DCC, "(bts - trx - ts - ti) "
3673 "Received '%s' from MNCC with "
3674 "no number or IMSI\n", get_mncc_name(msg_type));
3675 /* Invalid number */
3676 return mncc_release_ind(net, NULL, data->callref,
3677 GSM48_CAUSE_LOC_PRN_S_LU,
3678 GSM48_CC_CAUSE_INV_NR_FORMAT);
3679 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003680 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003681 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003682 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003683 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003684 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003685 subscr = subscr_get_by_imsi(net->subscr_group,
3686 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003687
3688 /* update the subscriber we deal with */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003689 log_set_context(LOG_CTX_VLR_SUBSCR, subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003690
Harald Welte4bfdfe72009-06-10 23:11:52 +08003691 /* If subscriber is not found */
3692 if (!subscr) {
3693 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3694 "Received '%s' from MNCC with "
3695 "unknown subscriber %s\n", data->called.number,
3696 get_mncc_name(msg_type), data->called.number);
3697 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003698 return mncc_release_ind(net, NULL, data->callref,
3699 GSM48_CAUSE_LOC_PRN_S_LU,
3700 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003701 }
3702 /* If subscriber is not "attached" */
3703 if (!subscr->lac) {
3704 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3705 "Received '%s' from MNCC with "
3706 "detached subscriber %s\n", data->called.number,
3707 get_mncc_name(msg_type), data->called.number);
3708 subscr_put(subscr);
3709 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003710 return mncc_release_ind(net, NULL, data->callref,
3711 GSM48_CAUSE_LOC_PRN_S_LU,
3712 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003713 }
3714 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003715 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003716 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003717 DEBUGP(DCC, "No memory for trans.\n");
3718 subscr_put(subscr);
3719 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003720 mncc_release_ind(net, NULL, data->callref,
3721 GSM48_CAUSE_LOC_PRN_S_LU,
3722 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003723 return -ENOMEM;
3724 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003725 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003726 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003727
Harald Welte4bfdfe72009-06-10 23:11:52 +08003728 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003729 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003730 /* find transaction with this subscriber already paging */
3731 llist_for_each_entry(transt, &net->trans_list, entry) {
3732 /* Transaction of our lchan? */
3733 if (transt == trans ||
3734 transt->subscr != subscr)
3735 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003736 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003737 "Received '%s' from MNCC with "
3738 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003739 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003740 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003741 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003742 subscr_put(subscr);
3743 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003744 return 0;
3745 }
3746 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003747 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003748
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003749 /* Request a channel */
3750 trans->paging_request = subscr_request_channel(subscr,
3751 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3752 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003753 if (!trans->paging_request) {
3754 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3755 subscr_put(subscr);
3756 trans_free(trans);
3757 return 0;
3758 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003759 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003760 return 0;
3761 }
3762 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003763 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003764 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003765 } else {
3766 /* update the subscriber we deal with */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +01003767 log_set_context(LOG_CTX_VLR_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003768 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003769
3770 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003771 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003772
3773 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003774 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003775 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003776 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003777 (trans->subscr)?(trans->subscr->extension):"-",
3778 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003779 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3780 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003781 if (msg_type == MNCC_REL_REQ)
3782 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3783 else
3784 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3785 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003786 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003787 return rc;
3788 }
3789
3790 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3791 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003792 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003793 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003794 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003795 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003796 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003797
3798 /* Find function for current state and message */
3799 for (i = 0; i < DOWNSLLEN; i++)
3800 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003801 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003802 break;
3803 if (i == DOWNSLLEN) {
3804 DEBUGP(DCC, "Message unhandled at this state.\n");
3805 return 0;
3806 }
3807
3808 rc = downstatelist[i].rout(trans, arg);
3809
3810 return rc;
3811}
3812
3813
3814static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003815 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003816 int type;
3817 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3818} datastatelist[] = {
3819 /* mobile originating call establishment */
3820 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3821 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3822 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3823 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3824 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3825 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3826 /* mobile terminating call establishment */
3827 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3828 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3829 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3830 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003831 {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 +08003832 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3833 /* signalling during call */
3834 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3835 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3836 {SBIT(GSM_CSTATE_ACTIVE),
3837 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3838 {ALL_STATES,
3839 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3840 {ALL_STATES,
3841 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3842 {ALL_STATES,
3843 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3844 {SBIT(GSM_CSTATE_ACTIVE),
3845 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3846 {SBIT(GSM_CSTATE_ACTIVE),
3847 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3848 {SBIT(GSM_CSTATE_ACTIVE),
3849 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3850 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3851 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3852 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3853 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3854 {SBIT(GSM_CSTATE_ACTIVE),
3855 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3856 /* clearing */
3857 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3858 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3859 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3860 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3861 {ALL_STATES, /* 5.4.3.4 */
3862 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3863};
3864
3865#define DATASLLEN \
3866 (sizeof(datastatelist) / sizeof(struct datastate))
3867
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003868static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003869{
3870 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003871 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003872 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003873 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003874 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003875
Harald Welte4bfdfe72009-06-10 23:11:52 +08003876 if (msg_type & 0x80) {
3877 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3878 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003879 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003880
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003881 if (!conn->subscr) {
3882 LOGP(DCC, LOGL_ERROR, "Invalid conn, no subscriber\n");
3883 return -EINVAL;
3884 }
3885
Harald Welte4bfdfe72009-06-10 23:11:52 +08003886 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003887 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003888
Harald Welte6f5aee02009-07-23 21:21:14 +02003889 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003890 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003891 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003892 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003893 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3894 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003895
3896 /* Create transaction */
3897 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003898 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003899 "creating new trans.\n", transaction_id);
3900 /* Create transaction */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02003901 trans = trans_alloc(conn->network, conn->subscr,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003902 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003903 transaction_id, new_callref++);
3904 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003905 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003906 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003907 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003908 GSM48_MT_CC_RELEASE_COMPL);
3909 return -ENOMEM;
3910 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003911 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003912 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003913 }
3914
3915 /* find function for current state and message */
3916 for (i = 0; i < DATASLLEN; i++)
3917 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003918 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003919 break;
3920 if (i == DATASLLEN) {
3921 DEBUGP(DCC, "Message unhandled at this state.\n");
3922 return 0;
3923 }
3924
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003925 assert(trans->subscr);
3926
Harald Welte4bfdfe72009-06-10 23:11:52 +08003927 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003928
3929 return rc;
3930}
3931
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003932/* Create a dummy to wait five seconds */
3933static void release_anchor(struct gsm_subscriber_connection *conn)
3934{
3935 if (!conn->anch_operation)
3936 return;
3937
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003938 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003939 talloc_free(conn->anch_operation);
3940 conn->anch_operation = NULL;
3941}
3942
3943static void anchor_timeout(void *_data)
3944{
3945 struct gsm_subscriber_connection *con = _data;
3946
3947 release_anchor(con);
3948 msc_release_connection(con);
3949}
3950
3951int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3952{
3953 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3954 if (!conn->anch_operation)
3955 return -1;
3956
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02003957 osmo_timer_setup(&conn->anch_operation->timeout, anchor_timeout, conn);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003958 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003959 return 0;
3960}
3961
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003962struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
3963{
3964 struct gsm_subscriber_connection *conn;
3965
3966 conn = talloc_zero(network, struct gsm_subscriber_connection);
3967 if (!conn)
3968 return NULL;
3969
3970 conn->network = network;
3971 llist_add_tail(&conn->entry, &network->subscr_conns);
3972 return conn;
3973}
3974
3975void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
3976{
3977 if (!conn)
3978 return;
3979
3980 if (conn->subscr) {
3981 subscr_put(conn->subscr);
3982 conn->subscr = NULL;
3983 }
3984
3985 llist_del(&conn->entry);
3986 talloc_free(conn);
3987}
3988
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003989/* 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 +08003990int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003991{
3992 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003993 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003994 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003995
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003996 OSMO_ASSERT(conn);
3997 OSMO_ASSERT(msg);
3998
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003999 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01004000#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08004001 if (silent_call_reroute(conn, msg))
4002 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01004003#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02004004
Harald Welte52b1f982008-12-23 20:25:15 +00004005 switch (pdisc) {
4006 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08004007 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08004008 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00004009 break;
4010 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08004011 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00004012 break;
4013 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08004014 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00004015 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00004016 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08004017 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08004018 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00004019 break;
Harald Welte52b1f982008-12-23 20:25:15 +00004020 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00004021 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01004022 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
4023 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02004024 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00004025 break;
Harald Welte6eafe912009-10-16 08:32:58 +02004026 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08004027 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08004028 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02004029 break;
Harald Welte52b1f982008-12-23 20:25:15 +00004030 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01004031 LOGP(DRLL, LOGL_NOTICE, "Unknown "
4032 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02004033 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00004034 break;
4035 }
4036
4037 return rc;
4038}
Harald Welte8470bf22008-12-25 23:28:35 +00004039
Harald Welte805f6442009-07-28 18:25:29 +02004040/*
Neels Hofmeyrcc7db182016-12-18 23:52:38 +01004041 * This will be run by the linker when loading the DSO. We use it to
Harald Welte805f6442009-07-28 18:25:29 +02004042 * do system initialization, e.g. registration of signal handlers.
4043 */
4044static __attribute__((constructor)) void on_dso_load_0408(void)
4045{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02004046 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02004047}