blob: 1a64731e01f98866aed9c1645aa3becd9d3c0727 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080061
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/gsm/gsm48.h>
63#include <osmocom/gsm/gsm0480.h>
64#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020065#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/core/msgb.h>
67#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010068#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010069#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000070
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020071#include <assert.h>
72
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020073void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010074void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020075
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020076static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
77
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020078static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080079static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020080 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010081static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080082static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Andreas Eversberga83d5112013-12-07 18:32:28 +010090static int apply_codec_restrictions(struct gsm_bts *bts,
91 struct gsm_mncc_bearer_cap *bcap)
92{
93 int i, j;
94
95 /* remove unsupported speech versions from list */
96 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
103 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
104 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
105 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
106 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
107 }
108 bcap->speech_ver[j] = -1;
109
110 return 0;
111}
112
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200113static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800114
Harald Welte31c00f72011-03-03 23:29:05 +0100115void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
116{
117 net->mncc_recv(net, msg);
118}
119
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
121 struct gsm_trans *trans)
122{
123 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
124
125 /* if we get passed a transaction reference, do some common
126 * work that the caller no longer has to do */
127 if (trans) {
128 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
129 msg->lchan = trans->conn->lchan;
130 }
131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800132 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200133 struct e1inp_sign_link *sign_link =
134 msg->lchan->ts->trx->rsl_link;
135
136 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100137 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800138 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending '%s' to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr & 0xf0,
143 gsm48_cc_msg_name(gh->msg_type));
144 else
145 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200146 "Sending 0x%02x to MS.\n",
147 sign_link->trx->bts->nr,
148 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800149 gh->proto_discr, gh->msg_type);
150 }
151
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100152 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800153}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800155int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
156{
157 struct gsm48_hdr *gh;
158 struct msgb *ss_notify;
159
160 ss_notify = gsm0480_create_notifySS(message);
161 if (!ss_notify)
162 return -1;
163
164 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
165 uint8_t *data = msgb_push(ss_notify, 1);
166 data[0] = ss_notify->len - 1;
167 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
168 gh->msg_type = GSM48_MT_CC_FACILITY;
169 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
170}
171
Harald Weltecf149ee2012-01-23 16:40:24 +0100172void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173{
174 if (!conn->sec_operation)
175 return;
176
177 talloc_free(conn->sec_operation);
178 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800179 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100180}
181
Harald Weltecf149ee2012-01-23 16:40:24 +0100182void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100184 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
185 struct gsm_security_operation);
186}
187
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100188int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
189 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100190{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200191 struct gsm_network *net = conn->network;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800192 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100193 struct gsm_security_operation *op;
194 struct gsm_auth_tuple atuple;
195 int status = -1, rc;
196
197 /* Check if we _can_ enable encryption. Cases where we can't:
198 * - Encryption disabled in config
199 * - Channel already secured (nothing to do)
200 * - Subscriber equipment doesn't support configured encryption
201 */
202 if (!net->a5_encryption) {
203 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800204 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100205 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200206 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100207 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
208 net->a5_encryption)) {
209 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* If not done yet, try to get info for this user */
214 if (status < 0) {
215 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
216 if (rc <= 0)
217 status = GSM_SECURITY_NOAVAIL;
218 }
219
220 /* Are we done yet ? */
221 if (status >= 0)
222 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100224 0;
225
226 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800227 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100228 return -EBUSY;
229
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800230 allocate_security_operation(conn);
231 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100232 op->cb = cb;
233 op->cb_data = cb_data;
234 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
235
236 /* FIXME: Should start a timer for completion ... */
237
238 /* Then do whatever is needed ... */
Neels Hofmeyrd2fa7a52016-03-10 23:30:37 +0100239 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100240 /* Start authentication */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100241 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.rand, NULL,
242 op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100243 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100244 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100245 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +0200246 op->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100247 }
248
249 return -EINVAL; /* not reached */
250}
251
Maxddee01f2016-05-24 14:23:27 +0200252static bool subscr_regexp_check(const struct gsm_network *net, const char *imsi)
253{
254 if (!net->authorized_reg_str)
255 return false;
256
257 if (regexec(&net->authorized_regexp, imsi, 0, NULL, 0) != REG_NOMATCH)
258 return true;
259
260 return false;
261}
262
Holger Freyther73487a22008-12-31 18:53:57 +0000263static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
264 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000265{
266 if (!subscriber)
267 return 0;
268
Holger Freyther73487a22008-12-31 18:53:57 +0000269 /*
270 * Do not send accept yet as more information should arrive. Some
271 * phones will not send us the information and we will have to check
272 * what we want to do with that.
273 */
274 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
275 return 0;
276
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100277 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200278 case GSM_AUTH_POLICY_CLOSED:
279 return subscriber->authorized;
Maxddee01f2016-05-24 14:23:27 +0200280 case GSM_AUTH_POLICY_REGEXP:
281 if (subscriber->authorized)
282 return 1;
283 if (subscr_regexp_check(subscriber->group->net,
284 subscriber->imsi))
285 subscriber->authorized = 1;
286 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200287 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200288 if (subscriber->authorized)
289 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200290 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200291 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000292 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200293 default:
294 return 0;
295 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000296}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000297
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100298static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000299{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100300 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000301 return;
302
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800303 /* No need to keep the connection up */
304 release_anchor(conn);
305
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200306 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100307 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100308 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100309 if (release)
310 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000311}
312
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100313static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000314{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800315 if (conn->loc_operation)
316 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100317 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000318
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100319 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200320 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000321}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000322
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200323static int finish_lu(struct gsm_subscriber_connection *conn)
324{
325 int rc = 0;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200326 int avoid_tmsi = conn->network->avoid_tmsi;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200327
328 /* We're all good */
329 if (avoid_tmsi) {
330 conn->subscr->tmsi = GSM_RESERVED_TMSI;
331 db_sync_subscriber(conn->subscr);
332 } else {
333 db_subscriber_alloc_tmsi(conn->subscr);
334 }
335
336 rc = gsm0408_loc_upd_acc(conn);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200337 if (conn->network->send_mm_info) {
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200338 /* send MM INFO with network name */
339 rc = gsm48_tx_mm_info(conn);
340 }
341
342 /* call subscr_update after putting the loc_upd_acc
343 * in the transmit queue, since S_SUBSCR_ATTACHED might
344 * trigger further action like SMS delivery */
345 subscr_update(conn->subscr, conn->bts,
346 GSM_SUBSCRIBER_UPDATE_ATTACHED);
347
348 /*
349 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
350 * MS needs to respond with a TMSI REALLOCATION COMPLETE
351 * (even if the TMSI is the same).
352 */
353 if (avoid_tmsi)
354 release_loc_updating_req(conn, 1);
355
356 return rc;
357}
358
Sylvain Munaut267fba02009-12-24 00:28:01 +0100359static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
360 struct msgb *msg, void *data, void *param)
361{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800362 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100363 int rc = 0;
364
365 switch (event) {
366 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100367 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 break;
369
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200370 case GSM_SECURITY_ALREADY:
371 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
372 "UPDATING after CM SERVICE REQUEST\n");
373 /* fall through */
374
Sylvain Munaut267fba02009-12-24 00:28:01 +0100375 case GSM_SECURITY_NOAVAIL:
376 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200377 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100378 break;
379
380 default:
381 rc = -EINVAL;
382 };
383
384 return rc;
385}
386
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100387static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000388{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100389 if (!conn->loc_operation)
390 return 0;
391
Sylvain Munaut267fba02009-12-24 00:28:01 +0100392 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800393 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100394 conn->loc_operation->key_seq,
395 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000396 return 0;
397}
398
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800399void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000400{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800401 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800402
403 /* avoid someone issuing a clear */
404 conn->in_release = 1;
405
Holger Freyther7c19f742009-06-06 13:54:35 +0000406 /*
407 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800408 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000409 */
Neels Hofmeyraea28ce2016-05-04 15:30:30 +0200410 release_loc_updating_req(conn, 0);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100411
412 /* We might need to cancel the paging response or such. */
413 if (conn->sec_operation && conn->sec_operation->cb) {
414 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
415 NULL, conn, conn->sec_operation->cb_data);
416 }
417
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800418 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800419 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000420
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100421 /*
422 * Free all transactions that are associated with the released
423 * connection. The transaction code will inform the CC or SMS
424 * facilities that will send the release indications. As part of
425 * the CC REL_IND the remote leg might be released and this will
426 * trigger the call to trans_free. This is something the llist
427 * macro can not handle and we will need to re-iterate the list.
428 *
429 * TODO: Move the trans_list into the subscriber connection and
430 * create a pending list for MT transactions. These exist before
431 * we have a subscriber connection.
432 */
433restart:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200434 llist_for_each_entry_safe(trans, temp, &conn->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100435 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200436 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100437 goto restart;
438 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800439 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000440}
441
Harald Welte371efe52010-12-22 23:17:50 +0100442void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
443{
444 struct gsm_trans *trans, *temp;
445
446 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
447
448 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100449 if (trans->protocol == protocol) {
450 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100451 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100452 }
Harald Welte371efe52010-12-22 23:17:50 +0100453 }
454}
455
Holger Freyther429e7762008-12-30 13:28:30 +0000456/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200457int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000458{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800459 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800460 struct msgb *msg;
461
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200462 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_REJECT]);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800463
464 msg = gsm48_create_loc_upd_rej(cause);
465 if (!msg) {
466 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
467 return -1;
468 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200469
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800470 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000471
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100472 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte7184bd02015-12-28 14:04:36 +0100473 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800474 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100475
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800476 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000477}
478
479/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200480static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000481{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100482 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000483 struct gsm48_hdr *gh;
484 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200485 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200486
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800487 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000488
489 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
490 gh->proto_discr = GSM48_PDISC_MM;
491 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
492
493 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200494 gsm48_generate_lai(lai, conn->network->country_code,
495 conn->network->network_code,
496 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000497
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200498 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
499 uint8_t mi[10];
500 int len;
501 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
502 mid = msgb_put(msg, len);
503 memcpy(mid, mi, len);
504 } else {
505 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
506 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
507 }
Harald Welte52b1f982008-12-23 20:25:15 +0000508
509 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
510
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200511 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_ACCEPT]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100512
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800513 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000514}
515
Harald Weltebf5e8df2009-02-03 12:59:45 +0000516/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000518{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100519 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000520 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000521
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800522 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000523
524 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
525 gh->proto_discr = GSM48_PDISC_MM;
526 gh->msg_type = GSM48_MT_MM_ID_REQ;
527 gh->data[0] = id_type;
528
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800529 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000530}
531
Maxe21cf382016-05-11 11:01:06 +0200532static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
533 const char *imsi)
534{
Maxe6052c42016-06-30 10:25:49 +0200535 if (!net->auto_create_subscr)
Maxddee01f2016-05-24 14:23:27 +0200536 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200537
Maxe6052c42016-06-30 10:25:49 +0200538 if (!subscr_regexp_check(net, imsi))
539 return NULL;
Maxddee01f2016-05-24 14:23:27 +0200540
Maxe6052c42016-06-30 10:25:49 +0200541 return subscr_create_subscriber(net->subscr_group, imsi);
Maxe21cf382016-05-11 11:01:06 +0200542}
Harald Welte231ad4f2008-12-27 11:15:38 +0000543
Harald Weltebf5e8df2009-02-03 12:59:45 +0000544/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800545static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000546{
547 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200548 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200549 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200550 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000551
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200552 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200553 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
554 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000555
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200556 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100557
Harald Welte75a983f2008-12-27 21:34:06 +0000558 switch (mi_type) {
559 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200560 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100562 conn->subscr = subscr_get_by_imsi(net->subscr_group,
563 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200564 if (!conn->subscr)
565 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200566 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 if (!conn->subscr && conn->loc_operation) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200568 gsm0408_loc_upd_rej(conn, net->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100569 release_loc_updating_req(conn, 1);
570 return 0;
571 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 if (conn->loc_operation)
573 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000574 break;
575 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000576 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000577 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578 if (conn->subscr) {
579 db_subscriber_assoc_imei(conn->subscr, mi_string);
580 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200581 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100582 if (conn->loc_operation)
583 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000584 break;
585 }
Holger Freyther73487a22008-12-31 18:53:57 +0000586
587 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100588 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000589}
590
Harald Welte255539c2008-12-28 02:26:27 +0000591
592static void loc_upd_rej_cb(void *data)
593{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100594 struct gsm_subscriber_connection *conn = data;
Harald Welte255539c2008-12-28 02:26:27 +0000595
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100596 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200597 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100598 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000599}
600
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100601static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000602{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
604 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200605 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000606}
607
Harald Welteb9845f92015-08-16 18:07:48 +0200608static const struct value_string lupd_names[] = {
609 { GSM48_LUPD_NORMAL, "NORMAL" },
610 { GSM48_LUPD_PERIODIC, "PERIODIC" },
611 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
612 { 0, NULL }
613};
Harald Welte2a139372009-02-22 21:14:55 +0000614
Harald Weltebf5e8df2009-02-03 12:59:45 +0000615/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800616static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000617{
Harald Welte8470bf22008-12-25 23:28:35 +0000618 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000619 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800620 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200621 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200622 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000623
Harald Welte8470bf22008-12-25 23:28:35 +0000624 lu = (struct gsm48_loc_upd_req *) gh->data;
625
626 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000627
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200628 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000629
Harald Welteb9845f92015-08-16 18:07:48 +0200630 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
631 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000632
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200633 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100634
Harald Welte24ff6ee2009-12-22 00:41:05 +0100635 switch (lu->type) {
636 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200637 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100638 break;
639 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200640 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100641 break;
642 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200643 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100644 break;
645 }
646
Holger Freythereaf04692009-06-06 13:54:44 +0000647 /*
648 * Pseudo Spoof detection: Just drop a second/concurrent
649 * location updating request.
650 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100651 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200652 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100653 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800654 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000655 return 0;
656 }
657
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800658 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000659
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200660 conn->loc_operation->key_seq = lu->key_seq;
661
Harald Welte52b1f982008-12-23 20:25:15 +0000662 switch (mi_type) {
663 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200664 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000665 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200666 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100667 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000668
Jan Luebbe370b41d2009-08-12 10:19:34 +0200669 /* look up subscriber based on IMSI, create if not found */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200670 subscr = subscr_get_by_imsi(conn->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200671 if (!subscr)
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200672 subscr = subscr_create(conn->network, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100673 if (!subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200674 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100675 release_loc_updating_req(conn, 0);
676 return 0;
677 }
Harald Welte4b634542008-12-27 01:55:51 +0000678 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000679 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200680 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000681 /* look up the subscriber based on TMSI, request IMSI if it fails */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200682 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200683 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000684 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000685 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200686 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100687 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000688 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200689 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200690 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200691 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000692 break;
693 case GSM_MI_TYPE_IMEI:
694 case GSM_MI_TYPE_IMEISV:
695 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200696 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000697 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200698 default:
Harald Weltea0368542009-06-27 02:58:43 +0200699 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000700 break;
701 }
702
Harald Welte24516ea2009-07-04 10:18:00 +0200703 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100704 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200705
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200707 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 /* FIXME: request id? close channel? */
709 return -EINVAL;
710 }
711
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100712 conn->subscr = subscr;
713 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000714
Harald Welte24516ea2009-07-04 10:18:00 +0200715 /* check if we can let the subscriber into our network immediately
716 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100717 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000718}
719
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100720/* Turn int into semi-octet representation: 98 => 0x89 */
721static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100723 uint8_t ret;
724
725 ret = value / 10;
726 ret |= (value % 10) << 4;
727
728 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800729}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100730
Harald Welte4bfdfe72009-06-10 23:11:52 +0800731
Harald Weltedb253af2008-12-30 17:56:55 +0000732/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800733int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000734{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100735 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000736 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200737 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200738 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200739 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100740
Harald Welte4bfdfe72009-06-10 23:11:52 +0800741 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100742 struct tm* gmt_time;
743 struct tm* local_time;
744 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100745 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000746
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800747 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000748
749 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
750 gh->proto_discr = GSM48_PDISC_MM;
751 gh->msg_type = GSM48_MT_MM_INFO;
752
753 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200754#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000755 name_len = strlen(net->name_long);
756 /* 10.5.3.5a */
757 ptr8 = msgb_put(msg, 3);
758 ptr8[0] = GSM48_IE_NAME_LONG;
759 ptr8[1] = name_len*2 +1;
760 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
761
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200762 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000763 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000764 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000765
766 /* FIXME: Use Cell Broadcast, not UCS-2, since
767 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200768#endif
769 name_len = (strlen(net->name_long)*7)/8;
770 name_pad = (8 - strlen(net->name_long)*7)%8;
771 if (name_pad > 0)
772 name_len++;
773 /* 10.5.3.5a */
774 ptr8 = msgb_put(msg, 3);
775 ptr8[0] = GSM48_IE_NAME_LONG;
776 ptr8[1] = name_len +1;
777 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
778
779 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100780 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200781
Harald Weltedb253af2008-12-30 17:56:55 +0000782 }
783
784 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200785#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000786 name_len = strlen(net->name_short);
787 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200788 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200789 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000790 ptr8[1] = name_len*2 + 1;
791 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
792
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200793 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000794 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000795 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200796#endif
797 name_len = (strlen(net->name_short)*7)/8;
798 name_pad = (8 - strlen(net->name_short)*7)%8;
799 if (name_pad > 0)
800 name_len++;
801 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200802 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200803 ptr8[0] = GSM48_IE_NAME_SHORT;
804 ptr8[1] = name_len +1;
805 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
806
807 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100808 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200809
Harald Weltedb253af2008-12-30 17:56:55 +0000810 }
811
Harald Weltedb253af2008-12-30 17:56:55 +0000812 /* Section 10.5.3.9 */
813 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100814 gmt_time = gmtime(&cur_t);
815
Harald Weltedb253af2008-12-30 17:56:55 +0000816 ptr8 = msgb_put(msg, 8);
817 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100818 ptr8[1] = bcdify(gmt_time->tm_year % 100);
819 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
820 ptr8[3] = bcdify(gmt_time->tm_mday);
821 ptr8[4] = bcdify(gmt_time->tm_hour);
822 ptr8[5] = bcdify(gmt_time->tm_min);
823 ptr8[6] = bcdify(gmt_time->tm_sec);
824
Neels Hofmeyr73983952016-05-10 13:29:33 +0200825 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200826 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200827 if (net->tz.hr < 0) {
828 tzunits = ((net->tz.hr/-1)*4);
829 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100830 ptr8[7] = bcdify(tzunits);
831 /* Set negative time */
832 ptr8[7] |= 0x08;
833 }
834 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200835 tzunits = net->tz.hr*4;
836 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100837 ptr8[7] = bcdify(tzunits);
838 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100839 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200840 if (net->tz.dst >= 0 && net->tz.dst <= 2)
841 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100842 }
843 else {
844 /* Need to get GSM offset and convert into 15 min units */
845 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
846 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200847#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100848 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200849#else
850#warning find a portable way to obtain the timezone offset
851 tzunits = 0;
852#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100853 if (tzunits < 0) {
854 tzunits = tzunits/-1;
855 ptr8[7] = bcdify(tzunits);
856 /* Flip it to negative */
857 ptr8[7] |= 0x08;
858 }
859 else
860 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100861
862 /* Does not support DST +2 */
863 if (local_time->tm_isdst)
864 dst = 1;
865 }
866
867 if (dst) {
868 ptr8 = msgb_put(msg, 3);
869 ptr8[0] = GSM48_IE_NET_DST;
870 ptr8[1] = 1;
871 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100872 }
Harald Weltedb253af2008-12-30 17:56:55 +0000873
Daniel Willmanneea93372009-08-13 03:42:07 +0200874 DEBUGP(DMM, "-> MM INFO\n");
875
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800876 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000877}
878
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100879/*! Send an Authentication Request to MS on the given subscriber connection
880 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
881 * \param[in] conn Subscriber connection to send on.
882 * \param[in] rand Random challenge token to send, must be 16 bytes long.
883 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
884 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
885 * \param[in] key_seq auth tuple's sequence number.
886 */
887int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
888 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200889{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100890 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200892 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200893
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200894 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100895 if (autn)
896 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200897
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800898 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200899 gh->proto_discr = GSM48_PDISC_MM;
900 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
901
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100902 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200903
Harald Welte7984d5c2009-08-12 22:56:50 +0200904 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100905 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200906 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200907 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200908
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100909
910 /* 16 bytes AUTN */
911 if (autn)
912 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
913
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800914 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200915}
916
917/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800918int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200919{
920 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800921 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200922}
923
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100924/*
925 * At the 30C3 phones miss their periodic update
926 * interval a lot and then remain unreachable. In case
927 * we still know the TMSI we can just attach it again.
928 */
929static void implit_attach(struct gsm_subscriber_connection *conn)
930{
931 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
932 return;
933
934 subscr_update(conn->subscr, conn->bts,
935 GSM_SUBSCRIBER_UPDATE_ATTACHED);
936}
937
938
Sylvain Munautba87f452009-12-24 00:28:46 +0100939static int _gsm48_rx_mm_serv_req_sec_cb(
940 unsigned int hooknum, unsigned int event,
941 struct msgb *msg, void *data, void *param)
942{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800943 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100944 int rc = 0;
945
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200946 /* auth failed or succeeded, the timer was stopped */
947 conn->expire_timer_stopped = 1;
948
Sylvain Munautba87f452009-12-24 00:28:46 +0100949 switch (event) {
950 case GSM_SECURITY_AUTH_FAILED:
951 /* Nothing to do */
952 break;
953
954 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200955 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800956 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100957 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100958 break;
959
960 case GSM_SECURITY_SUCCEEDED:
961 /* nothing to do. CIPHER MODE COMMAND is
962 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100963 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100964 break;
965
966 default:
967 rc = -EINVAL;
968 };
969
970 return rc;
971}
972
Harald Welte4ed0e922009-01-10 03:17:30 +0000973/*
974 * Handle CM Service Requests
975 * a) Verify that the packet is long enough to contain the information
976 * we require otherwsie reject with INCORRECT_MESSAGE
977 * b) Try to parse the TMSI. If we do not have one reject
978 * c) Check that we know the subscriber with the TMSI otherwise reject
979 * with a HLR cause
980 * d) Set the subscriber on the gsm_lchan and accept
981 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800982static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000983{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200984 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200985 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000986
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200987 struct gsm_network *network = conn->network;
Harald Welteba4cf162009-01-10 01:49:35 +0000988 struct gsm_subscriber *subscr;
989 struct gsm48_hdr *gh = msgb_l3(msg);
990 struct gsm48_service_request *req =
991 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800992 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200993 uint8_t classmark2_len = gh->data[1];
994 uint8_t *classmark2 = gh->data+2;
995 uint8_t mi_len = *(classmark2 + classmark2_len);
996 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000997
Harald Weltec9e02182009-05-01 19:07:53 +0000998 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000999 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001000 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001001 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001002 GSM48_REJECT_INCORRECT_MESSAGE);
1003 }
1004
1005 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001006 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001007 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001008 GSM48_REJECT_INCORRECT_MESSAGE);
1009 }
1010
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001011 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001012 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001013
1014 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001015 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1016 req->cm_service_type, gsm48_mi_type_name(mi_type),
1017 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001018 subscr = subscr_get_by_imsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001019 mi_string);
1020 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001021 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1022 req->cm_service_type, gsm48_mi_type_name(mi_type),
1023 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001024 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001025 tmsi_from_string(mi_string));
1026 } else {
1027 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001028 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001029 GSM48_REJECT_INCORRECT_MESSAGE);
1030 }
1031
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001032 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001033
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001034 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +02001035 send_siemens_mrpci(msg->lchan, classmark2-1);
1036
Holger Freythereb443982009-06-04 13:58:42 +00001037
Harald Welte2a139372009-02-22 21:14:55 +00001038 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001039 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001041 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001042
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043 if (!conn->subscr)
1044 conn->subscr = subscr;
1045 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001046 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1047 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001048 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1049 subscr_put(subscr);
1050 }
1051
Harald Weltec2e302d2009-07-05 14:08:13 +02001052 subscr->equipment.classmark2_len = classmark2_len;
1053 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1054 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001055
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001056 /* we will send a MM message soon */
1057 conn->expire_timer_stopped = 1;
1058
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001060 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001061}
1062
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001063static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001064{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001065 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +00001066 struct gsm48_hdr *gh = msgb_l3(msg);
1067 struct gsm48_imsi_detach_ind *idi =
1068 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001069 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001070 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001071 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001072
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001073 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001074 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1075 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001076
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001077 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001078
Harald Welte2a139372009-02-22 21:14:55 +00001079 switch (mi_type) {
1080 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001081 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001082 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001083 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001084 break;
1085 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001086 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001087 subscr = subscr_get_by_imsi(network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001088 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001089 break;
1090 case GSM_MI_TYPE_IMEI:
1091 case GSM_MI_TYPE_IMEISV:
1092 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001093 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001094 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001095 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001096 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001097 break;
1098 }
1099
Holger Freyther4a49e772009-04-12 05:37:29 +00001100 if (subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001101 subscr_update(subscr, conn->bts,
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001102 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001103 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001104
1105 subscr->equipment.classmark1 = idi->classmark1;
1106 db_sync_equipment(&subscr->equipment);
1107
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001108 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001109 } else
Harald Welte2a139372009-02-22 21:14:55 +00001110 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1111
Harald Welte31981a02009-12-20 09:58:40 +01001112 /* FIXME: iterate over all transactions and release them,
1113 * imagine an IMSI DETACH happening during an active call! */
1114
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001115 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001116 return 0;
1117}
1118
Harald Welted2a7f5a2009-06-05 20:08:20 +00001119static int gsm48_rx_mm_status(struct msgb *msg)
1120{
1121 struct gsm48_hdr *gh = msgb_l3(msg);
1122
1123 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1124
1125 return 0;
1126}
1127
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001128static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
1129 struct gsm_subscriber_connection *conn,
1130 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
1133 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001134
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001135 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
1136 LOGP(DMM, LOGL_ERROR,
1137 "%s: MM AUTHENTICATION RESPONSE:"
1138 " l3 length invalid: %u\n",
1139 subscr_name(conn->subscr), msgb_l3len(msg));
1140 return -EINVAL;
1141 }
1142
1143 *res_len = sizeof(ar->sres);
1144 memcpy(res, ar->sres, sizeof(ar->sres));
1145 return 0;
1146}
1147
1148static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
1149 struct gsm_subscriber_connection *conn,
1150 struct msgb *msg)
1151{
1152 struct gsm48_hdr *gh;
1153 uint8_t *data;
1154 uint8_t iei;
1155 uint8_t ie_len;
1156 unsigned int data_len;
1157
1158 /* First parse the GSM part */
1159 if (parse_gsm_auth_resp(res, res_len, conn, msg))
1160 return -EINVAL;
1161 OSMO_ASSERT(*res_len == 4);
1162
1163 /* Then add the extended res part */
1164 gh = msgb_l3(msg);
1165 data = gh->data + sizeof(struct gsm48_auth_resp);
1166 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
1167
1168 if (data_len < 3) {
1169 LOGP(DMM, LOGL_ERROR,
1170 "%s: MM AUTHENTICATION RESPONSE:"
1171 " l3 length invalid: %u\n",
1172 subscr_name(conn->subscr), msgb_l3len(msg));
1173 return -EINVAL;
1174 }
1175
1176 iei = data[0];
1177 ie_len = data[1];
1178 if (iei != GSM48_IE_AUTH_RES_EXT) {
1179 LOGP(DMM, LOGL_ERROR,
1180 "%s: MM R99 AUTHENTICATION RESPONSE:"
1181 " expected IEI 0x%02x, got 0x%02x\n",
1182 subscr_name(conn->subscr),
1183 GSM48_IE_AUTH_RES_EXT, iei);
1184 return -EINVAL;
1185 }
1186
1187 if (ie_len > 12) {
1188 LOGP(DMM, LOGL_ERROR,
1189 "%s: MM R99 AUTHENTICATION RESPONSE:"
1190 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
1191 subscr_name(conn->subscr), GSM48_IE_AUTH_RES_EXT, ie_len);
1192 return -EINVAL;
1193 }
1194
1195 *res_len += ie_len;
1196 memcpy(res + 4, &data[2], ie_len);
1197 return 0;
1198}
1199
1200/* Chapter 9.2.3: Authentication Response */
1201static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
1202{
1203 struct gsm_network *net = conn->network;
1204 uint8_t res[16];
1205 uint8_t res_len;
1206 int rc;
1207 bool is_r99;
1208
1209 if (!conn->subscr) {
1210 LOGP(DMM, LOGL_ERROR,
1211 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
1212 gsm48_tx_mm_auth_rej(conn);
1213 release_security_operation(conn);
1214 return -EINVAL;
1215 }
1216
1217 if (msgb_l3len(msg) >
1218 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
1219 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
1220 is_r99 = true;
1221 } else {
1222 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
1223 is_r99 = false;
1224 }
1225
1226 if (rc) {
1227 gsm48_tx_mm_auth_rej(conn);
1228 release_security_operation(conn);
1229 return -EINVAL;
1230 }
1231
1232 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
1233 subscr_name(conn->subscr),
1234 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
1235 osmo_hexdump_nospc(res, res_len));
1236
1237 /* Future: vlr_sub_rx_auth_resp(conn->vsub, is_r99,
1238 * conn->via_iface == IFACE_IU,
1239 * res, res_len);
1240 */
1241
1242 if (res_len != 4) {
1243 LOGP(DMM, LOGL_ERROR,
1244 "%s: MM AUTHENTICATION RESPONSE:"
1245 " UMTS authentication not supported\n",
1246 subscr_name(conn->subscr));
1247 }
Sylvain Munaut30a15382009-12-24 00:27:26 +01001248
1249 /* Safety check */
1250 if (!conn->sec_operation) {
1251 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1252 return -EIO;
1253 }
1254
1255 /* Validate SRES */
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001256 if (memcmp(conn->sec_operation->atuple.vec.sres, res, 4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001257 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001258 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001259
1260 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001261 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001262
Sylvain Munaut30a15382009-12-24 00:27:26 +01001263 if (cb)
1264 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001265 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001266
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001267 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001268 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001269 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001270 }
1271
Sylvain Munautc593cf12010-06-10 23:51:41 +02001272 DEBUGPC(DMM, "OK\n");
1273
Sylvain Munaut30a15382009-12-24 00:27:26 +01001274 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001275 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001276 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001277}
1278
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001279static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1280{
1281 struct gsm48_hdr *gh = msgb_l3(msg);
1282 uint8_t cause;
1283 uint8_t auts_tag;
1284 uint8_t auts_len;
1285 uint8_t *auts;
1286 int rc;
1287
1288 if (!conn->sec_operation) {
1289 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION FAILURE:"
1290 " No authentication/cipher operation in progress\n",
1291 subscr_name(conn->subscr));
1292 return -EINVAL;
1293 }
1294
1295 if (!conn->subscr) {
1296 LOGP(DMM, LOGL_ERROR,
1297 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
1298 gsm48_tx_mm_auth_rej(conn);
1299 release_security_operation(conn);
1300 return -EINVAL;
1301 }
1302
1303 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1304 LOGP(DMM, LOGL_ERROR,
1305 "%s: MM R99 AUTHENTICATION FAILURE:"
1306 " l3 length invalid: %u\n",
1307 subscr_name(conn->subscr), msgb_l3len(msg));
1308 gsm48_tx_mm_auth_rej(conn);
1309 release_security_operation(conn);
1310 return -EINVAL;
1311 }
1312
1313 cause = gh->data[0];
1314
1315 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1316 LOGP(DMM, LOGL_INFO,
1317 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
1318 subscr_name(conn->subscr), cause);
1319 rc = gsm48_tx_mm_auth_rej(conn);
1320 release_security_operation(conn);
1321 return rc;
1322 }
1323
1324 /* This is a Synch Failure procedure, which should pass an AUTS to
1325 * resynchronize the sequence nr with the HLR. Expecting exactly one
1326 * TLV with 14 bytes of AUTS. */
1327
1328 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1329 LOGP(DMM, LOGL_INFO,
1330 "%s: MM R99 AUTHENTICATION FAILURE:"
1331 " invalid Synch Failure: missing AUTS IE\n",
1332 subscr_name(conn->subscr));
1333 gsm48_tx_mm_auth_rej(conn);
1334 release_security_operation(conn);
1335 return -EINVAL;
1336 }
1337
1338 auts_tag = gh->data[1];
1339 auts_len = gh->data[2];
1340 auts = &gh->data[3];
1341
1342 if (auts_tag != GSM48_IE_AUTS
1343 || auts_len != 14) {
1344 LOGP(DMM, LOGL_INFO,
1345 "%s: MM R99 AUTHENTICATION FAILURE:"
1346 " invalid Synch Failure:"
1347 " expected AUTS IE 0x%02x of 14 bytes,"
1348 " got IE 0x%02x of %u bytes\n",
1349 subscr_name(conn->subscr),
1350 GSM48_IE_AUTS, auts_tag, auts_len);
1351 gsm48_tx_mm_auth_rej(conn);
1352 release_security_operation(conn);
1353 return -EINVAL;
1354 }
1355
1356 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1357 LOGP(DMM, LOGL_INFO,
1358 "%s: MM R99 AUTHENTICATION FAILURE:"
1359 " invalid Synch Failure msg: message truncated (%u)\n",
1360 subscr_name(conn->subscr), msgb_l3len(msg));
1361 gsm48_tx_mm_auth_rej(conn);
1362 release_security_operation(conn);
1363 return -EINVAL;
1364 }
1365
1366 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1367 * large enough. */
1368
1369 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
1370 subscr_name(conn->subscr), osmo_hexdump_nospc(auts, 14));
1371
1372 /* Future: vlr_sub_rx_auth_fail(conn->vsub, auts); */
1373
1374 LOGP(DMM, LOGL_ERROR, "%s: MM R99 AUTHENTICATION not supported\n",
1375 subscr_name(conn->subscr));
1376 rc = gsm48_tx_mm_auth_rej(conn);
1377 release_security_operation(conn);
1378 return rc;
1379}
1380
Harald Weltebf5e8df2009-02-03 12:59:45 +00001381/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001382static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001383{
1384 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001386
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001387 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001388 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001389 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001390 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001391 break;
1392 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001393 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001394 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001395 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001396 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001397 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001398 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001399 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001400 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001401 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001402 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001403 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001404 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001405 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001406 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001407 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001408 break;
1409 case GSM48_MT_MM_CM_REEST_REQ:
1410 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1411 break;
1412 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001413 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001414 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001415 case GSM48_MT_MM_AUTH_FAIL:
1416 rc = gsm48_rx_mm_auth_fail(conn, msg);
1417 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001418 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001419 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001420 gh->msg_type);
1421 break;
1422 }
1423
1424 return rc;
1425}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001426
Harald Welte2d35ae62009-02-06 12:02:13 +00001427/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001428static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001429{
1430 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001431 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001432 uint8_t *classmark2_lv = gh->data + 1;
1433 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001434 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001436 int rc = 0;
1437
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001438 resp = (struct gsm48_pag_resp *) &gh->data[0];
1439 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1440 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001441 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1442 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001443
Harald Weltefe18d8f2009-02-22 21:14:24 +00001444 switch (mi_type) {
1445 case GSM_MI_TYPE_TMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001446 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001447 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001448 break;
1449 case GSM_MI_TYPE_IMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001450 subscr = subscr_get_by_imsi(conn->network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001451 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001452 break;
1453 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001454
1455 if (!subscr) {
1456 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001457 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001458 return -EINVAL;
1459 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001460 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001461 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001462 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001463
Harald Weltec2e302d2009-07-05 14:08:13 +02001464 subscr->equipment.classmark2_len = *classmark2_lv;
1465 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1466 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001467
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001468 /* We received a paging */
1469 conn->expire_timer_stopped = 1;
1470
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001471 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001472 return rc;
1473}
1474
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001475static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001476{
1477 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001478 uint8_t apdu_id_flags;
1479 uint8_t apdu_len;
1480 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001481
1482 apdu_id_flags = gh->data[0];
1483 apdu_len = gh->data[1];
1484 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001485
Max8dc8f232017-02-09 19:13:02 +01001486 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001487 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001488
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001489 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001490}
1491
Harald Weltebf5e8df2009-02-03 12:59:45 +00001492/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001493static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001494{
1495 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001496 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001497
1498 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001499 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001500 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001501 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001502 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001503 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001504 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001505 default:
Philippd37981e2016-10-10 15:11:41 +02001506 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1507 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001508 break;
1509 }
1510
Harald Welte2d35ae62009-02-06 12:02:13 +00001511 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001512}
1513
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001514int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1515 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001516{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001518 struct gsm48_hdr *gh;
1519
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001520 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001521
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001522 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1523 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001524
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001525 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1526 gh->proto_discr = GSM48_PDISC_RR;
1527 gh->msg_type = GSM48_MT_RR_APP_INFO;
1528 gh->data[0] = apdu_id;
1529 gh->data[1] = apdu_len;
1530 memcpy(gh->data+2, apdu, apdu_len);
1531
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001532 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001533}
1534
Alexander Couzens92f552f2016-08-23 07:32:27 +02001535/* FIXME: this count_statistics is a state machine behaviour. we should convert
1536 * the complete call control into a state machine. Afterwards we can move this
1537 * code into state transitions.
1538 */
1539static void count_statistics(struct gsm_trans *trans, int new_state)
1540{
1541 int old_state = trans->cc.state;
1542 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1543
1544 if (old_state == new_state)
1545 return;
1546
1547 /* state incoming */
1548 switch (new_state) {
1549 case GSM_CSTATE_ACTIVE:
1550 osmo_counter_inc(trans->net->active_calls);
1551 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1552 break;
1553 }
1554
1555 /* state outgoing */
1556 switch (old_state) {
1557 case GSM_CSTATE_ACTIVE:
1558 osmo_counter_dec(trans->net->active_calls);
1559 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1560 new_state == GSM_CSTATE_DISCONNECT_IND)
1561 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1562 else
1563 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1564 break;
1565 }
1566}
1567
Harald Welte4bc90a12008-12-27 16:32:52 +00001568/* Call Control */
1569
Harald Welte7584aea2009-02-11 11:44:12 +00001570/* The entire call control code is written in accordance with Figure 7.10c
1571 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1572 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1573 * it for voice */
1574
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575static void new_cc_state(struct gsm_trans *trans, int state)
1576{
1577 if (state > 31 || state < 0)
1578 return;
1579
1580 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001581 gsm48_cc_state_name(trans->cc.state),
1582 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583
Alexander Couzens92f552f2016-08-23 07:32:27 +02001584 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001585 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586}
1587
1588static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001589{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001590 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001591 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001592 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001593
Harald Welte4bc90a12008-12-27 16:32:52 +00001594 gh->msg_type = GSM48_MT_CC_STATUS;
1595
1596 cause = msgb_put(msg, 3);
1597 cause[0] = 2;
1598 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1599 cause[2] = 0x80 | 30; /* response to status inquiry */
1600
1601 call_state = msgb_put(msg, 1);
1602 call_state[0] = 0xc0 | 0x00;
1603
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001604 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001605}
1606
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001607static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001608 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001609{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001610 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001611 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1612
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001613 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001614
Harald Welte6f4b7532008-12-29 00:39:37 +00001615 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001616 gh->msg_type = msg_type;
1617
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001618 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001619}
1620
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1622{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001623 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001624 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001625 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001626 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627 }
1628}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001629
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1631 int msg_type, struct gsm_mncc *mncc)
1632{
1633 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001634 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001635
1636 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001637 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001638 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001640 trans->conn->lchan->ts->trx->bts->nr,
1641 trans->conn->lchan->ts->trx->nr,
1642 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 (trans->subscr)?(trans->subscr->extension):"-",
1644 get_mncc_name(msg_type));
1645 else
1646 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1647 "Sending '%s' to MNCC.\n",
1648 (trans->subscr)?(trans->subscr->extension):"-",
1649 get_mncc_name(msg_type));
1650 else
1651 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1652 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1653
1654 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001655
Harald Welte966636f2009-06-26 19:39:35 +02001656 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 if (!msg)
1658 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001659
1660 data = msgb_put(msg, sizeof(struct gsm_mncc));
1661 memcpy(data, mncc, sizeof(struct gsm_mncc));
1662
Harald Welte54209c22010-12-22 23:43:29 +01001663 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001664
1665 return 0;
1666}
1667
1668int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001669 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670{
1671 struct gsm_mncc rel;
1672
Harald Welte92f70c52009-06-12 01:54:08 +08001673 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001675 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001676 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1677 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1679}
1680
Harald Weltedcaf5652009-07-23 18:56:43 +02001681/* Call Control Specific transaction release.
1682 * gets called by trans_free, DO NOT CALL YOURSELF! */
1683void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 gsm48_stop_cc_timer(trans);
1686
1687 /* send release to L4, if callref still exists */
1688 if (trans->callref) {
1689 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001690 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001691 GSM48_CAUSE_LOC_PRN_S_LU,
1692 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001694 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001696 if (trans->conn)
1697 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001698}
1699
1700static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001701
Harald Welte09e38af2009-02-16 22:52:23 +00001702/* call-back from paging the B-end of the connection */
1703static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001704 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001705{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001706 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001707 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001708
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001709 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001710
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001712 switch (event) {
1713 case GSM_PAGING_SUCCEEDED:
1714 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001715 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001716 /* Assign lchan */
1717 transt->conn = conn;
1718 /* send SETUP request to called party */
1719 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1720 break;
1721 case GSM_PAGING_EXPIRED:
1722 case GSM_PAGING_BUSY:
1723 DEBUGP(DCC, "Paging subscr %s expired!\n",
1724 transt->subscr->extension);
1725 /* Temporarily out of order */
1726 mncc_release_ind(transt->net, transt,
1727 transt->callref,
1728 GSM48_CAUSE_LOC_PRN_S_LU,
1729 GSM48_CC_CAUSE_DEST_OOO);
1730 transt->callref = 0;
1731 transt->paging_request = NULL;
1732 trans_free(transt);
1733 break;
1734 default:
1735 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1736 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001738
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001739 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001740 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001741}
Harald Welte7584aea2009-02-11 11:44:12 +00001742
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001743static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001744
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001745/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001746static int switch_for_handover(struct gsm_lchan *old_lchan,
1747 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001748{
1749 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001750
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001751 /* Ask the new socket to send to the already known port. */
1752 if (new_lchan->conn->mncc_rtp_bridge) {
1753 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1754 rsl_ipacc_mdcx(new_lchan,
1755 old_lchan->abis_ip.connect_ip,
1756 old_lchan->abis_ip.connect_port, 0);
1757 return 0;
1758 }
1759
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001760 if (ipacc_rtp_direct) {
1761 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1762 return 0;
1763 }
1764
1765 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001766 new_rs = new_lchan->abis_ip.rtp_socket;
1767 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001768
1769 if (!new_rs) {
1770 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1771 return -EIO;
1772 }
1773
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001774 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001775
1776 if (!old_rs) {
1777 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1778 return -EIO;
1779 }
1780
1781 /* copy rx_action and reference to other sock */
1782 new_rs->rx_action = old_rs->rx_action;
1783 new_rs->tx_action = old_rs->tx_action;
1784 new_rs->transmit = old_rs->transmit;
1785
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001786 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001787 case RTP_PROXY:
1788 other_rs = old_rs->proxy.other_sock;
1789 rtp_socket_proxy(new_rs, other_rs);
1790 /* delete reference to other end socket to prevent
1791 * rtp_socket_free() from removing the inverse reference */
1792 old_rs->proxy.other_sock = NULL;
1793 break;
1794 case RTP_RECV_UPSTREAM:
1795 new_rs->receive = old_rs->receive;
1796 break;
1797 case RTP_NONE:
1798 break;
1799 }
1800
1801 return 0;
1802}
1803
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001804static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1805{
1806 struct gsm_lchan *old_lchan;
1807 old_lchan = bsc_handover_pending(lchan);
1808 if (old_lchan)
1809 switch_for_handover(old_lchan, lchan);
1810}
1811
Harald Welte805f6442009-07-28 18:25:29 +02001812/* some other part of the code sends us a signal */
1813static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1814 void *handler_data, void *signal_data)
1815{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001816 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001817 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001818 struct gsm_network *net;
1819 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001820
1821 if (subsys != SS_ABISIP)
1822 return 0;
1823
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001824 /* RTP bridge handling */
1825 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1826 return tch_rtp_signal(lchan, signal);
1827
Harald Welte805f6442009-07-28 18:25:29 +02001828 /* in case we use direct BTS-to-BTS RTP */
1829 if (ipacc_rtp_direct)
1830 return 0;
1831
Harald Welte805f6442009-07-28 18:25:29 +02001832 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001833 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001834 /* in case we don't use direct BTS-to-BTS RTP */
1835 /* the BTS has successfully bound a TCH to a local ip/port,
1836 * which means we can connect our UDP socket to it */
1837 if (lchan->abis_ip.rtp_socket) {
1838 rtp_socket_free(lchan->abis_ip.rtp_socket);
1839 lchan->abis_ip.rtp_socket = NULL;
1840 }
1841
1842 lchan->abis_ip.rtp_socket = rtp_socket_create();
1843 if (!lchan->abis_ip.rtp_socket)
1844 return -EIO;
1845
1846 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1847 lchan->abis_ip.bound_ip,
1848 lchan->abis_ip.bound_port);
1849 if (rc < 0)
1850 return -EIO;
1851
Harald Welteda7ab742009-12-19 22:23:05 +01001852 /* check if any transactions on this lchan still have
1853 * a tch_recv_mncc request pending */
1854 net = lchan->ts->trx->bts->network;
1855 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001856 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001857 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1858 tch_recv_mncc(net, trans->callref, 1);
1859 }
1860 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001861
1862 /*
1863 * TODO: this appears to be too early? Why not until after
1864 * the handover detect or the handover complete?
1865 *
1866 * Do we have a handover pending for this new lchan? In that
1867 * case re-route the audio from the old channel to the new one.
1868 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001869 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001870 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001871 case S_ABISIP_DLCX_IND:
1872 /* the BTS tells us a RTP stream has been disconnected */
1873 if (lchan->abis_ip.rtp_socket) {
1874 rtp_socket_free(lchan->abis_ip.rtp_socket);
1875 lchan->abis_ip.rtp_socket = NULL;
1876 }
1877
1878 break;
Harald Welte805f6442009-07-28 18:25:29 +02001879 }
1880
1881 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001882}
1883
Harald Welte49f48b82009-02-17 15:29:33 +00001884/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001885static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001886{
Harald Welte11fa29c2009-02-19 17:24:39 +00001887 struct gsm_bts *bts = lchan->ts->trx->bts;
1888 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001889 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001890 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001891 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001892
Max8db12e42016-04-18 23:11:18 +02001893 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1894 "(bts=%u,trx=%u,ts=%u,%s)\n",
1895 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1896 get_value_string(gsm_chan_t_names, lt),
1897 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1898 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001899
1900 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001901 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001902 return -EINVAL;
1903 }
Harald Welteda7ab742009-12-19 22:23:05 +01001904
Max3ffce192016-04-25 15:22:00 +02001905 if (lt != rt) {
1906 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1907 " channel types: local = %s, remote = %s\n",
1908 get_value_string(gsm_chan_t_names, lt),
1909 get_value_string(gsm_chan_t_names, rt));
1910 return -EBADSLT;
1911 }
1912
1913 if (lm != rm) {
1914 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1915 " channel modes: local = %s, remote = %s\n",
1916 get_value_string(gsm48_chan_mode_names, lm),
1917 get_value_string(gsm48_chan_mode_names, rm));
1918 return -EMEDIUMTYPE;
1919 }
1920
Harald Welteda7ab742009-12-19 22:23:05 +01001921 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001922 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001923 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001924 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001925 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001926 if (!lchan->abis_ip.rtp_socket) {
1927 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1928 "lchan\n");
1929 return -EIO;
1930 }
1931 if (!remote_lchan->abis_ip.rtp_socket) {
1932 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1933 "remote_lchan\n");
1934 return -EIO;
1935 }
1936
Harald Welte805f6442009-07-28 18:25:29 +02001937 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001938 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001939 if (rc < 0)
1940 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001941 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001942 if (rc < 0)
1943 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001944 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301945 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1946 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001947 } else {
1948 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301949 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1950 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301951 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001952 if (rc < 0)
1953 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301954 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1955 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301956 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001957 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001958 break;
1959 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001960 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001961 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001962 trau_mux_map_lchan(lchan, remote_lchan);
1963 break;
1964 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001965 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001966 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001967 }
Harald Welte49f48b82009-02-17 15:29:33 +00001968
1969 return 0;
1970}
1971
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001973static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001974{
Harald Welte53d51f52015-12-03 14:59:04 +01001975 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1976 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001977
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001979 return -EIO;
1980
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001981 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 return -EIO;
1983
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001984 /* Which subscriber do we want to track trans1 or trans2? */
1985 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1986
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001988 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001989}
1990
Harald Welteda7ab742009-12-19 22:23:05 +01001991/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001992static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993{
1994 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001995 struct gsm_lchan *lchan;
1996 struct gsm_bts *bts;
1997 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001998
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01002000 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 if (!trans)
2002 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002003 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002005
2006 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002007 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01002008 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002010 /* store receive state */
2011 trans->tch_recv = enable;
2012
Harald Welteda7ab742009-12-19 22:23:05 +01002013 switch (bts->type) {
2014 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002015 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01002016 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02002017 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002018 return -EINVAL;
2019 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002020 /* In case, we don't have a RTP socket to the BTS yet, the BTS
2021 * will not be connected to our RTP proxy and the socket will
2022 * not be assigned to the application interface. This method
2023 * will be called again, once the audio socket is created and
2024 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01002025 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01002026 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2027 return 0;
2028 }
2029 if (enable) {
2030 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01002031 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01002032 if (rc < 0)
2033 return rc;
2034 /* assign socket to application interface */
2035 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2036 net, callref);
2037 } else
2038 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2039 net, 0);
2040 break;
2041 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002042 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002043 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01002044 /* In case we don't have a TCH with correct mode, the TRAU muxer
2045 * will not be asigned to the application interface. This is
2046 * performed by switch_trau_mux() after successful handover or
2047 * assignment. */
2048 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
2049 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2050 return 0;
2051 }
Harald Welteda7ab742009-12-19 22:23:05 +01002052 if (enable)
2053 return trau_recv_lchan(lchan, callref);
2054 return trau_mux_unmap(NULL, callref);
2055 break;
2056 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002057 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002058 return -EINVAL;
2059 }
2060
2061 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062}
2063
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2065{
2066 DEBUGP(DCC, "-> STATUS ENQ\n");
2067 return gsm48_cc_tx_status(trans, msg);
2068}
2069
2070static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2071static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2072
2073static void gsm48_cc_timeout(void *arg)
2074{
2075 struct gsm_trans *trans = arg;
2076 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002077 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2078 int mo_location = GSM48_CAUSE_LOC_USER;
2079 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2080 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 struct gsm_mncc mo_rel, l4_rel;
2082
2083 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2084 mo_rel.callref = trans->callref;
2085 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2086 l4_rel.callref = trans->callref;
2087
Harald Weltedcaf5652009-07-23 18:56:43 +02002088 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 case 0x303:
2090 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002091 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 break;
2093 case 0x310:
2094 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002095 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 break;
2097 case 0x313:
2098 disconnect = 1;
2099 /* unknown, did not find it in the specs */
2100 break;
2101 case 0x301:
2102 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002103 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 break;
2105 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002106 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002108 gsm48_cc_tx_release(trans, &trans->cc.msg);
2109 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 break; /* stay in release state */
2111 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002112 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 return;
2114// release = 1;
2115// l4_cause = 14;
2116// break;
2117 case 0x306:
2118 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002119 mo_cause = trans->cc.msg.cause.value;
2120 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 break;
2122 case 0x323:
2123 disconnect = 1;
2124 break;
2125 default:
2126 release = 1;
2127 }
2128
2129 if (release && trans->callref) {
2130 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002131 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132 l4_location, l4_cause);
2133 trans->callref = 0;
2134 }
2135
2136 if (disconnect && trans->callref) {
2137 /* process disconnect towards layer 4 */
2138 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002139 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140 }
2141
2142 /* process disconnect towards mobile station */
2143 if (disconnect || release) {
2144 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002145 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2146 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2147 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148 mo_rel.cause.diag_len = 3;
2149
2150 if (disconnect)
2151 gsm48_cc_tx_disconnect(trans, &mo_rel);
2152 if (release)
2153 gsm48_cc_tx_release(trans, &mo_rel);
2154 }
2155
2156}
2157
Max3ffce192016-04-25 15:22:00 +02002158/* disconnect both calls from the bridge */
2159static inline void disconnect_bridge(struct gsm_network *net,
2160 struct gsm_mncc_bridge *bridge, int err)
2161{
2162 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
2163 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
2164 struct gsm_mncc mx_rel;
2165 if (!trans0 || !trans1)
2166 return;
2167
2168 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
2169 trans0->callref, trans1->callref, strerror(err));
2170
2171 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
2172 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
2173 GSM48_CC_CAUSE_CHAN_UNACCEPT);
2174
2175 mx_rel.callref = trans0->callref;
2176 gsm48_cc_tx_disconnect(trans0, &mx_rel);
2177
2178 mx_rel.callref = trans1->callref;
2179 gsm48_cc_tx_disconnect(trans1, &mx_rel);
2180}
2181
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2183 int sec, int micro)
2184{
2185 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002186 trans->cc.timer.cb = gsm48_cc_timeout;
2187 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02002188 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02002189 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190}
2191
2192static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2193{
2194 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002195 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2197 struct tlv_parsed tp;
2198 struct gsm_mncc setup;
2199
2200 memset(&setup, 0, sizeof(struct gsm_mncc));
2201 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002202 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002203 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 /* emergency setup is identified by msg_type */
2205 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2206 setup.emergency = 1;
2207
2208 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002209 setup.fields |= MNCC_F_CALLING;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002210 osmo_strlcpy(setup.calling.number, trans->subscr->extension,
2211 sizeof(setup.calling.number));
2212 osmo_strlcpy(setup.imsi, trans->subscr->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002213
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* bearer capability */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2216 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002217 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002219 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 }
2221 /* facility */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2223 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2226 }
2227 /* called party bcd number */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2229 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2232 }
2233 /* user-user */
2234 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2235 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2238 }
2239 /* ss-version */
2240 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2241 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2244 }
2245 /* CLIR suppression */
2246 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2247 setup.clir.sup = 1;
2248 /* CLIR invocation */
2249 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2250 setup.clir.inv = 1;
2251 /* cc cap */
2252 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2253 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002254 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2256 }
2257
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 new_cc_state(trans, GSM_CSTATE_INITIATED);
2259
Harald Welte (local)47df3992009-12-26 19:45:03 +01002260 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2261 subscr_name(trans->subscr), trans->subscr->extension,
2262 setup.called.number);
2263
Alexander Couzensb847a212016-08-02 11:34:11 +02002264 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002265
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002267 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268
Harald Welte13cac662009-07-29 12:10:35 +02002269 /* MNCC code will modify the channel asynchronously, we should
2270 * ipaccess-bind only after the modification has been made to the
2271 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 return 0;
2273}
2274
2275static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002276{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002277 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002278 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002280 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002281
Harald Welte7ccf7782009-02-17 01:43:01 +00002282 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002283
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 /* transaction id must not be assigned */
2285 if (trans->transaction_id != 0xff) { /* unasssigned */
2286 DEBUGP(DCC, "TX Setup with assigned transaction. "
2287 "This is not allowed!\n");
2288 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002289 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002290 GSM48_CAUSE_LOC_PRN_S_LU,
2291 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002293 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 return rc;
2295 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002296
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002298 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2299 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002300 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002302 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002303 GSM48_CAUSE_LOC_PRN_S_LU,
2304 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002306 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 return rc;
2308 }
Harald Welte78283ef2009-07-23 21:36:44 +02002309 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002310
Harald Welte65e74cc2008-12-29 01:55:35 +00002311 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002312
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002314
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 /* bearer capability */
2316 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002317 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318 /* facility */
2319 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002320 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 /* progress */
2322 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* calling party BCD number */
2325 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 /* called party BCD number */
2328 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002329 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 /* user-user */
2331 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002332 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333 /* redirecting party BCD number */
2334 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002335 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 /* signal */
2337 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002338 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002339
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002341
Alexander Couzensb847a212016-08-02 11:34:11 +02002342 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002343
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002344 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002345}
2346
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2348{
2349 struct gsm48_hdr *gh = msgb_l3(msg);
2350 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2351 struct tlv_parsed tp;
2352 struct gsm_mncc call_conf;
2353
2354 gsm48_stop_cc_timer(trans);
2355 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2356
2357 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2358 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002359 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002360 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361#if 0
2362 /* repeat */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2364 call_conf.repeat = 1;
2365 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2366 call_conf.repeat = 2;
2367#endif
2368 /* bearer capability */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2370 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002373 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374 }
2375 /* cause */
2376 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2377 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002378 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2380 }
2381 /* cc cap */
2382 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2383 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002384 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2386 }
2387
Andreas Eversberg035b8742013-09-19 09:27:06 +02002388 /* IMSI of called subscriber */
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002389 osmo_strlcpy(call_conf.imsi, trans->subscr->imsi,
2390 sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02002391
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2393
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002394 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002395 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396}
2397
2398static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2399{
2400 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002401 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2403
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2405
2406 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2407
2408 /* bearer capability */
2409 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002410 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 /* facility */
2412 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002413 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 /* progress */
2415 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002416 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002418 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419}
2420
2421static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2422{
2423 struct gsm48_hdr *gh = msgb_l3(msg);
2424 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2425 struct tlv_parsed tp;
2426 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002427
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428 gsm48_stop_cc_timer(trans);
2429 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2430
2431 memset(&alerting, 0, sizeof(struct gsm_mncc));
2432 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002433 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 /* facility */
2435 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2436 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002437 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2439 }
2440
2441 /* progress */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2443 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002444 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2446 }
2447 /* ss-version */
2448 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2449 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002450 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2452 }
2453
2454 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2455
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002456 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002457 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458}
2459
2460static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2461{
2462 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002463 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2465
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 gh->msg_type = GSM48_MT_CC_ALERTING;
2467
2468 /* facility */
2469 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002470 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 /* progress */
2472 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002473 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 /* user-user */
2475 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002476 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477
2478 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002479
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002480 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2484{
2485 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002486 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2488
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 gh->msg_type = GSM48_MT_CC_PROGRESS;
2490
2491 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 /* user-user */
2494 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002497 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
2500static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2501{
2502 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002503 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
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_CONNECT;
2507
2508 gsm48_stop_cc_timer(trans);
2509 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2510
2511 /* facility */
2512 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002513 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 /* progress */
2515 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002516 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 /* connected number */
2518 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002519 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 /* user-user */
2521 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002522 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523
2524 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2525
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002526 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527}
2528
2529static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2530{
2531 struct gsm48_hdr *gh = msgb_l3(msg);
2532 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2533 struct tlv_parsed tp;
2534 struct gsm_mncc connect;
2535
2536 gsm48_stop_cc_timer(trans);
2537
2538 memset(&connect, 0, sizeof(struct gsm_mncc));
2539 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002540 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002542 connect.fields |= MNCC_F_CONNECTED;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002543 osmo_strlcpy(connect.connected.number, trans->subscr->extension,
2544 sizeof(connect.connected.number));
2545 osmo_strlcpy(connect.imsi, trans->subscr->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002546
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 /* facility */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2549 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002550 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2552 }
2553 /* user-user */
2554 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2555 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002556 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2558 }
2559 /* ss-version */
2560 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2561 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002562 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2564 }
2565
2566 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002567 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002568
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002569 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570}
2571
2572
2573static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2574{
2575 struct gsm_mncc connect_ack;
2576
2577 gsm48_stop_cc_timer(trans);
2578
2579 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002580 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002581
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2583 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002584
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002585 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 &connect_ack);
2587}
2588
2589static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2590{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002591 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2593
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2595
2596 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2597
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002598 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2602{
2603 struct gsm48_hdr *gh = msgb_l3(msg);
2604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2605 struct tlv_parsed tp;
2606 struct gsm_mncc disc;
2607
2608 gsm48_stop_cc_timer(trans);
2609
2610 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2611
2612 memset(&disc, 0, sizeof(struct gsm_mncc));
2613 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002614 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 /* cause */
2616 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2617 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2620 }
2621 /* facility */
2622 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2623 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002624 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2626 }
2627 /* user-user */
2628 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2629 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002630 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2632 }
2633 /* ss-version */
2634 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2635 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002636 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2638 }
2639
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002640 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641
2642}
2643
Harald Weltec66b71c2009-06-11 14:23:20 +08002644static struct gsm_mncc_cause default_cause = {
2645 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2646 .coding = 0,
2647 .rec = 0,
2648 .rec_val = 0,
2649 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2650 .diag_len = 0,
2651 .diag = { 0 },
2652};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653
2654static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2655{
2656 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002657 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2659
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2661
2662 gsm48_stop_cc_timer(trans);
2663 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2664
2665 /* cause */
2666 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002667 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668 else
Harald Welte55c8f352010-03-07 23:40:35 +01002669 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670
2671 /* facility */
2672 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674 /* progress */
2675 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 /* user-user */
2678 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002679 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680
2681 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002682 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683
2684 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2685
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002686 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687}
2688
2689static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2690{
2691 struct gsm48_hdr *gh = msgb_l3(msg);
2692 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2693 struct tlv_parsed tp;
2694 struct gsm_mncc rel;
2695 int rc;
2696
2697 gsm48_stop_cc_timer(trans);
2698
2699 memset(&rel, 0, sizeof(struct gsm_mncc));
2700 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002701 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 /* cause */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2704 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2707 }
2708 /* facility */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2710 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002711 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2713 }
2714 /* user-user */
2715 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2716 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002717 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2719 }
2720 /* ss-version */
2721 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2722 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002723 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2725 }
2726
Harald Weltedcaf5652009-07-23 18:56:43 +02002727 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002729 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002731 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002732 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002733 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002734 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 }
2736
2737 new_cc_state(trans, GSM_CSTATE_NULL);
2738
2739 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002740 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741
2742 return rc;
2743}
2744
2745static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2746{
2747 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002748 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2750
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 gh->msg_type = GSM48_MT_CC_RELEASE;
2752
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 gsm48_stop_cc_timer(trans);
2754 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2755
2756 /* cause */
2757 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002758 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002759 /* facility */
2760 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002761 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 /* user-user */
2763 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002764 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765
Harald Weltedcaf5652009-07-23 18:56:43 +02002766 trans->cc.T308_second = 0;
2767 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768
Harald Weltedcaf5652009-07-23 18:56:43 +02002769 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2771
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002772 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773}
2774
2775static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2776{
2777 struct gsm48_hdr *gh = msgb_l3(msg);
2778 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2779 struct tlv_parsed tp;
2780 struct gsm_mncc rel;
2781 int rc = 0;
2782
2783 gsm48_stop_cc_timer(trans);
2784
2785 memset(&rel, 0, sizeof(struct gsm_mncc));
2786 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002787 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 /* cause */
2789 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2790 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002791 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2793 }
2794 /* facility */
2795 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2796 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002797 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2799 }
2800 /* user-user */
2801 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2802 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002803 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2805 }
2806 /* ss-version */
2807 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2808 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002809 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2811 }
2812
2813 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002814 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002816 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 MNCC_REJ_IND, &rel);
2818 break;
2819 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002820 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 MNCC_REL_CNF, &rel);
2822 break;
2823 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002824 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 MNCC_REL_IND, &rel);
2826 }
2827 }
2828
2829 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002830 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002831
2832 return rc;
2833}
2834
2835static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2836{
2837 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002838 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002840 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2843
2844 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002845
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846 gsm48_stop_cc_timer(trans);
2847
2848 /* cause */
2849 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002850 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 /* facility */
2852 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002853 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854 /* user-user */
2855 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002856 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857
Harald Weltec7782de2010-12-21 19:31:41 +01002858 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2859
Harald Weltedcaf5652009-07-23 18:56:43 +02002860 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861
Harald Weltec7782de2010-12-21 19:31:41 +01002862 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863}
2864
2865static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2866{
2867 struct gsm48_hdr *gh = msgb_l3(msg);
2868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2869 struct tlv_parsed tp;
2870 struct gsm_mncc fac;
2871
2872 memset(&fac, 0, sizeof(struct gsm_mncc));
2873 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002874 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 /* facility */
2876 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2877 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002878 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2880 }
2881 /* ss-version */
2882 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2883 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002884 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2886 }
2887
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002888 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889}
2890
2891static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2892{
2893 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002894 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2896
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 gh->msg_type = GSM48_MT_CC_FACILITY;
2898
2899 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002900 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002902 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903}
2904
2905static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2906{
2907 struct gsm_mncc hold;
2908
2909 memset(&hold, 0, sizeof(struct gsm_mncc));
2910 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002911 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912}
2913
2914static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2915{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002916 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2918
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2920
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002921 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922}
2923
2924static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2925{
2926 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002927 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2929
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2931
2932 /* cause */
2933 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002934 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935 else
Harald Welte55c8f352010-03-07 23:40:35 +01002936 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002938 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939}
2940
2941static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2942{
2943 struct gsm_mncc retrieve;
2944
2945 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2946 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002947 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002948 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949}
2950
2951static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2952{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002953 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2955
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2957
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002958 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959}
2960
2961static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2962{
2963 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002964 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2966
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2968
2969 /* cause */
2970 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002971 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972 else
Harald Welte55c8f352010-03-07 23:40:35 +01002973 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002975 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976}
2977
2978static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2979{
2980 struct gsm48_hdr *gh = msgb_l3(msg);
2981 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2982 struct tlv_parsed tp;
2983 struct gsm_mncc dtmf;
2984
2985 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2986 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002987 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 /* keypad facility */
2989 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2990 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002991 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2993 }
2994
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002995 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996}
2997
2998static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2999{
3000 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003001 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3003
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3005
3006 /* keypad */
3007 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01003008 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003010 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011}
3012
3013static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3014{
3015 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003016 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3018
Harald Welte4bfdfe72009-06-10 23:11:52 +08003019 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3020
3021 /* cause */
3022 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01003023 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024 else
Harald Welte55c8f352010-03-07 23:40:35 +01003025 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003027 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028}
3029
3030static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3031{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003032 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3034
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3036
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003037 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038}
3039
3040static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3041{
3042 struct gsm_mncc dtmf;
3043
3044 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3045 dtmf.callref = trans->callref;
3046
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003047 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048}
3049
3050static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3051{
3052 struct gsm48_hdr *gh = msgb_l3(msg);
3053 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3054 struct tlv_parsed tp;
3055 struct gsm_mncc modify;
3056
3057 memset(&modify, 0, sizeof(struct gsm_mncc));
3058 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003059 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 /* bearer capability */
3061 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3062 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003063 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003065 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 }
3067
3068 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3069
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003070 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071}
3072
3073static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3074{
3075 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003076 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003077 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3078
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 gh->msg_type = GSM48_MT_CC_MODIFY;
3080
3081 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3082
3083 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003084 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085
3086 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3087
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003088 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003089}
3090
3091static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3092{
3093 struct gsm48_hdr *gh = msgb_l3(msg);
3094 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3095 struct tlv_parsed tp;
3096 struct gsm_mncc modify;
3097
3098 gsm48_stop_cc_timer(trans);
3099
3100 memset(&modify, 0, sizeof(struct gsm_mncc));
3101 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003102 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103 /* bearer capability */
3104 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3105 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003106 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003108 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003109 }
3110
3111 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3112
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003113 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114}
3115
3116static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3117{
3118 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003119 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003120 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3121
Harald Welte4bfdfe72009-06-10 23:11:52 +08003122 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3123
3124 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003125 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003126
3127 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3128
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003129 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130}
3131
3132static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3133{
3134 struct gsm48_hdr *gh = msgb_l3(msg);
3135 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3136 struct tlv_parsed tp;
3137 struct gsm_mncc modify;
3138
3139 gsm48_stop_cc_timer(trans);
3140
3141 memset(&modify, 0, sizeof(struct gsm_mncc));
3142 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003143 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 /* bearer capability */
3145 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3146 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01003147 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01003149 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 }
3151 /* cause */
3152 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3153 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01003154 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003155 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3156 }
3157
3158 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3159
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003160 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161}
3162
3163static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3164{
3165 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003166 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3168
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3170
3171 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01003172 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01003174 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003175
3176 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3177
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003178 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003179}
3180
3181static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3182{
3183 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003184 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003185 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3186
Harald Welte4bfdfe72009-06-10 23:11:52 +08003187 gh->msg_type = GSM48_MT_CC_NOTIFY;
3188
3189 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01003190 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003191
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003192 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193}
3194
3195static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3196{
3197 struct gsm48_hdr *gh = msgb_l3(msg);
3198 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3199// struct tlv_parsed tp;
3200 struct gsm_mncc notify;
3201
3202 memset(&notify, 0, sizeof(struct gsm_mncc));
3203 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003204// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003205 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01003206 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003207
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003208 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003209}
3210
3211static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3212{
3213 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01003214 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08003215 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3216
Harald Welte4bfdfe72009-06-10 23:11:52 +08003217 gh->msg_type = GSM48_MT_CC_USER_INFO;
3218
3219 /* user-user */
3220 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01003221 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003222 /* more data */
3223 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01003224 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003225
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003226 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227}
3228
3229static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3230{
3231 struct gsm48_hdr *gh = msgb_l3(msg);
3232 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3233 struct tlv_parsed tp;
3234 struct gsm_mncc user;
3235
3236 memset(&user, 0, sizeof(struct gsm_mncc));
3237 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003238 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003239 /* user-user */
3240 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3241 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01003242 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3244 }
3245 /* more data */
3246 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3247 user.more = 1;
3248
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003249 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003250}
3251
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003252static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253{
3254 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003255 struct gsm_lchan *lchan = trans->conn->lchan;
3256
3257 /*
3258 * We were forced to make an assignment a lot earlier and
3259 * we should avoid sending another assignment that might
3260 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3261 * In case of rtp-bridge it is too late to change things
3262 * here.
3263 */
3264 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3265 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003267 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3268 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269}
3270
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003271static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3272 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3273 uint32_t payload_msg_type)
3274{
3275 uint8_t data[sizeof(struct gsm_mncc)];
3276 struct gsm_mncc_rtp *rtp;
3277
3278 memset(&data, 0, sizeof(data));
3279 rtp = (struct gsm_mncc_rtp *) &data[0];
3280
3281 rtp->callref = callref;
3282 rtp->msg_type = cmd;
3283 rtp->ip = addr;
3284 rtp->port = port;
3285 rtp->payload_type = payload_type;
3286 rtp->payload_msg_type = payload_msg_type;
3287 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3288}
3289
3290static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3291{
3292 struct gsm_lchan *lchan;
3293 int msg_type;
3294
3295 lchan = trans->conn->lchan;
3296 switch (lchan->abis_ip.rtp_payload) {
3297 case RTP_PT_GSM_FULL:
3298 msg_type = GSM_TCHF_FRAME;
3299 break;
3300 case RTP_PT_GSM_EFR:
3301 msg_type = GSM_TCHF_FRAME_EFR;
3302 break;
3303 case RTP_PT_GSM_HALF:
3304 msg_type = GSM_TCHH_FRAME;
3305 break;
3306 case RTP_PT_AMR:
3307 msg_type = GSM_TCH_FRAME_AMR;
3308 break;
3309 default:
3310 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3311 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3312 msg_type = 0;
3313 break;
3314 }
3315
3316 return mncc_recv_rtp(net, trans->callref, cmd,
3317 lchan->abis_ip.bound_ip,
3318 lchan->abis_ip.bound_port,
3319 lchan->abis_ip.rtp_payload,
3320 msg_type);
3321}
3322
3323static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3324{
3325 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3326}
3327
3328static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3329{
3330 struct gsm_bts *bts;
3331 struct gsm_lchan *lchan;
3332 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003333 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003334
3335 /* Find callref */
3336 trans = trans_find_by_callref(net, callref);
3337 if (!trans) {
3338 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3339 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3340 return -EIO;
3341 }
3342 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3343 if (!trans->conn) {
3344 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3345 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3346 return 0;
3347 }
3348
3349 lchan = trans->conn->lchan;
3350 bts = lchan->ts->trx->bts;
3351 if (!is_ipaccess_bts(bts)) {
3352 /*
3353 * I want this to be straight forward and have no audio flow
3354 * through the nitb/osmo-mss system. This currently means that
3355 * this will not work with BS11/Nokia type BTS. We would need
3356 * to have a trau<->rtp bridge for these but still preferable
3357 * in another process.
3358 */
3359 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3360 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3361 return -EINVAL;
3362 }
3363
3364 trans->conn->mncc_rtp_bridge = 1;
3365 /*
3366 * *sigh* we need to pick a codec now. Pick the most generic one
3367 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003368 * similiar to the routine above.
3369 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003370 */
3371 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3372 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003373 m = mncc_codec_for_mode(lchan->type);
3374 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3375 get_value_string(gsm48_chan_mode_names, m),
3376 get_value_string(gsm_chan_t_names, lchan->type));
3377 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003378 lchan->type != GSM_LCHAN_TCH_H);
3379 }
3380
3381 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3382 return 0;
3383}
3384
3385static int tch_rtp_connect(struct gsm_network *net, void *arg)
3386{
3387 struct gsm_lchan *lchan;
3388 struct gsm_trans *trans;
3389 struct gsm_mncc_rtp *rtp = arg;
3390
3391 /* Find callref */
3392 trans = trans_find_by_callref(net, rtp->callref);
3393 if (!trans) {
3394 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3395 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3396 return -EIO;
3397 }
3398 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3399 if (!trans->conn) {
3400 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3401 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3402 return 0;
3403 }
3404
3405 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003406 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3407 get_value_string(gsm48_chan_mode_names,
3408 mncc_codec_for_mode(lchan->type)),
3409 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003410
3411 /* TODO: Check if payload_msg_type is compatible with what we have */
3412 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3413 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3414 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3415 }
3416
3417 /*
3418 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3419 * complains about both rtp and rtp payload2 being present in the
3420 * same package!
3421 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003422 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003423 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3424}
3425
3426static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3427{
3428 struct gsm_network *net;
3429 struct gsm_trans *tmp, *trans = NULL;
3430
3431 net = lchan->ts->trx->bts->network;
3432 llist_for_each_entry(tmp, &net->trans_list, entry) {
3433 if (!tmp->conn)
3434 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003435 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003436 continue;
3437 trans = tmp;
3438 break;
3439 }
3440
3441 if (!trans) {
3442 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3443 gsm_lchan_name(lchan));
3444 return 0;
3445 }
3446
3447 switch (signal) {
3448 case S_ABISIP_CRCX_ACK:
3449 if (lchan->conn->mncc_rtp_create_pending) {
3450 lchan->conn->mncc_rtp_create_pending = 0;
3451 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3452 gsm_lchan_name(lchan));
3453 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3454 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003455 /*
3456 * TODO: this appears to be too early? Why not until after
3457 * the handover detect or the handover complete?
3458 */
3459 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003460 break;
3461 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003462 if (lchan->conn->mncc_rtp_connect_pending) {
3463 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003464 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3465 gsm_lchan_name(lchan));
3466 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3467 }
3468 break;
3469 }
3470
3471 return 0;
3472}
3473
3474
Harald Welte4bfdfe72009-06-10 23:11:52 +08003475static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003476 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003477 int type;
3478 int (*rout) (struct gsm_trans *trans, void *arg);
3479} downstatelist[] = {
3480 /* mobile originating call establishment */
3481 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3482 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3483 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3484 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3485 {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 */
3486 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3487 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3488 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3489 /* mobile terminating call establishment */
3490 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3491 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3492 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3493 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3494 /* signalling during call */
3495 {SBIT(GSM_CSTATE_ACTIVE),
3496 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3497 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3498 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3499 {ALL_STATES,
3500 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3501 {ALL_STATES,
3502 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3503 {ALL_STATES,
3504 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3505 {SBIT(GSM_CSTATE_ACTIVE),
3506 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3507 {SBIT(GSM_CSTATE_ACTIVE),
3508 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3509 {SBIT(GSM_CSTATE_ACTIVE),
3510 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3511 {SBIT(GSM_CSTATE_ACTIVE),
3512 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3513 {SBIT(GSM_CSTATE_ACTIVE),
3514 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3515 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3516 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3517 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3518 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3519 {SBIT(GSM_CSTATE_ACTIVE),
3520 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3521 /* clearing */
3522 {SBIT(GSM_CSTATE_INITIATED),
3523 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3524 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3525 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3526 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3527 MNCC_REL_REQ, gsm48_cc_tx_release},
3528 /* special */
3529 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003530 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003531};
3532
3533#define DOWNSLLEN \
3534 (sizeof(downstatelist) / sizeof(struct downstate))
3535
3536
Harald Welte76556372010-12-22 23:57:45 +01003537int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003538{
Harald Welte1a6f7982009-08-09 18:52:33 +02003539 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003540 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003541 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003542 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003543 struct gsm_mncc *data = arg, rel;
3544
Harald Welte04dc88f2010-12-22 21:45:05 +01003545 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3546
Harald Welte4bfdfe72009-06-10 23:11:52 +08003547 /* handle special messages */
3548 switch(msg_type) {
3549 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003550 rc = tch_bridge(net, arg);
3551 if (rc < 0)
3552 disconnect_bridge(net, arg, -rc);
3553 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003554 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003555 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003556 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003557 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003558 case MNCC_RTP_CREATE:
3559 return tch_rtp_create(net, data->callref);
3560 case MNCC_RTP_CONNECT:
3561 return tch_rtp_connect(net, arg);
3562 case MNCC_RTP_FREE:
3563 /* unused right now */
3564 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003565 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003566 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003567 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003568 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003569 /* Find callref */
3570 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003571 if (!trans) {
3572 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003573 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003574 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003575 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003576 if (!trans->conn) {
3577 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003578 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003579 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003580 if (!trans->conn->lchan) {
3581 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3582 return 0;
3583 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003584 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3585 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003586 /* This should be LOGL_ERROR or NOTICE, but
3587 * unfortuantely it happens for a couple of frames at
3588 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003589 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003590 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003591 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003592 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003593 switch (bts->type) {
3594 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003595 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003596 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003597 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003598 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003599 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003600 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003601 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003602 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003603 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003604 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003605 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003606 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003607 }
3608 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003609 }
3610
3611 memset(&rel, 0, sizeof(struct gsm_mncc));
3612 rel.callref = data->callref;
3613
3614 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003615 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003616
3617 /* Callref unknown */
3618 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003619 struct gsm_subscriber *subscr;
3620
Harald Welte4a3464c2009-07-04 10:11:24 +02003621 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003622 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3623 "Received '%s' from MNCC with "
3624 "unknown callref %d\n", data->called.number,
3625 get_mncc_name(msg_type), data->callref);
3626 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003627 return mncc_release_ind(net, NULL, data->callref,
3628 GSM48_CAUSE_LOC_PRN_S_LU,
3629 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003630 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003631 if (!data->called.number[0] && !data->imsi[0]) {
3632 DEBUGP(DCC, "(bts - trx - ts - ti) "
3633 "Received '%s' from MNCC with "
3634 "no number or IMSI\n", get_mncc_name(msg_type));
3635 /* Invalid number */
3636 return mncc_release_ind(net, NULL, data->callref,
3637 GSM48_CAUSE_LOC_PRN_S_LU,
3638 GSM48_CC_CAUSE_INV_NR_FORMAT);
3639 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003640 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003641 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003642 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003643 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003644 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003645 subscr = subscr_get_by_imsi(net->subscr_group,
3646 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003647
3648 /* update the subscriber we deal with */
3649 log_set_context(BSC_CTX_SUBSCR, subscr);
3650
Harald Welte4bfdfe72009-06-10 23:11:52 +08003651 /* If subscriber is not found */
3652 if (!subscr) {
3653 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3654 "Received '%s' from MNCC with "
3655 "unknown subscriber %s\n", data->called.number,
3656 get_mncc_name(msg_type), data->called.number);
3657 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003658 return mncc_release_ind(net, NULL, data->callref,
3659 GSM48_CAUSE_LOC_PRN_S_LU,
3660 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003661 }
3662 /* If subscriber is not "attached" */
3663 if (!subscr->lac) {
3664 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3665 "Received '%s' from MNCC with "
3666 "detached subscriber %s\n", data->called.number,
3667 get_mncc_name(msg_type), data->called.number);
3668 subscr_put(subscr);
3669 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003670 return mncc_release_ind(net, NULL, data->callref,
3671 GSM48_CAUSE_LOC_PRN_S_LU,
3672 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003673 }
3674 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003675 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003676 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003677 DEBUGP(DCC, "No memory for trans.\n");
3678 subscr_put(subscr);
3679 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003680 mncc_release_ind(net, NULL, data->callref,
3681 GSM48_CAUSE_LOC_PRN_S_LU,
3682 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003683 return -ENOMEM;
3684 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003685 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003686 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003687
Harald Welte4bfdfe72009-06-10 23:11:52 +08003688 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003689 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003690 /* find transaction with this subscriber already paging */
3691 llist_for_each_entry(transt, &net->trans_list, entry) {
3692 /* Transaction of our lchan? */
3693 if (transt == trans ||
3694 transt->subscr != subscr)
3695 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003696 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003697 "Received '%s' from MNCC with "
3698 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003699 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003700 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003701 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003702 subscr_put(subscr);
3703 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003704 return 0;
3705 }
3706 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003707 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003708
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003709 /* Request a channel */
3710 trans->paging_request = subscr_request_channel(subscr,
3711 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3712 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003713 if (!trans->paging_request) {
3714 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3715 subscr_put(subscr);
3716 trans_free(trans);
3717 return 0;
3718 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003719 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003720 return 0;
3721 }
3722 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003723 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003724 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003725 } else {
3726 /* update the subscriber we deal with */
3727 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003728 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003729
3730 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003731 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003732
3733 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003734 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003735 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003736 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003737 (trans->subscr)?(trans->subscr->extension):"-",
3738 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003739 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3740 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003741 if (msg_type == MNCC_REL_REQ)
3742 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3743 else
3744 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3745 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003746 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003747 return rc;
3748 }
3749
3750 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3751 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003752 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003753 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003754 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003755 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003756 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003757
3758 /* Find function for current state and message */
3759 for (i = 0; i < DOWNSLLEN; i++)
3760 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003761 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003762 break;
3763 if (i == DOWNSLLEN) {
3764 DEBUGP(DCC, "Message unhandled at this state.\n");
3765 return 0;
3766 }
3767
3768 rc = downstatelist[i].rout(trans, arg);
3769
3770 return rc;
3771}
3772
3773
3774static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003775 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003776 int type;
3777 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3778} datastatelist[] = {
3779 /* mobile originating call establishment */
3780 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3781 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3782 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3783 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3784 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3785 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3786 /* mobile terminating call establishment */
3787 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3788 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3789 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3790 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003791 {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 +08003792 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3793 /* signalling during call */
3794 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3795 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3796 {SBIT(GSM_CSTATE_ACTIVE),
3797 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3798 {ALL_STATES,
3799 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3800 {ALL_STATES,
3801 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3802 {ALL_STATES,
3803 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3804 {SBIT(GSM_CSTATE_ACTIVE),
3805 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3806 {SBIT(GSM_CSTATE_ACTIVE),
3807 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3808 {SBIT(GSM_CSTATE_ACTIVE),
3809 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3810 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3811 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3812 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3813 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3814 {SBIT(GSM_CSTATE_ACTIVE),
3815 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3816 /* clearing */
3817 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3818 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3819 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3820 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3821 {ALL_STATES, /* 5.4.3.4 */
3822 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3823};
3824
3825#define DATASLLEN \
3826 (sizeof(datastatelist) / sizeof(struct datastate))
3827
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003828static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003829{
3830 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003831 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003832 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003833 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003834 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003835
Harald Welte4bfdfe72009-06-10 23:11:52 +08003836 if (msg_type & 0x80) {
3837 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3838 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003839 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003840
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003841 if (!conn->subscr) {
3842 LOGP(DCC, LOGL_ERROR, "Invalid conn, no subscriber\n");
3843 return -EINVAL;
3844 }
3845
Harald Welte4bfdfe72009-06-10 23:11:52 +08003846 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003847 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003848
Harald Welte6f5aee02009-07-23 21:21:14 +02003849 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003850 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003851 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003852 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003853 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3854 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003855
3856 /* Create transaction */
3857 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003858 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003859 "creating new trans.\n", transaction_id);
3860 /* Create transaction */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02003861 trans = trans_alloc(conn->network, conn->subscr,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003862 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003863 transaction_id, new_callref++);
3864 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003865 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003866 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003867 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003868 GSM48_MT_CC_RELEASE_COMPL);
3869 return -ENOMEM;
3870 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003871 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003872 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003873 }
3874
3875 /* find function for current state and message */
3876 for (i = 0; i < DATASLLEN; i++)
3877 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003878 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003879 break;
3880 if (i == DATASLLEN) {
3881 DEBUGP(DCC, "Message unhandled at this state.\n");
3882 return 0;
3883 }
3884
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003885 assert(trans->subscr);
3886
Harald Welte4bfdfe72009-06-10 23:11:52 +08003887 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003888
3889 return rc;
3890}
3891
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003892/* Create a dummy to wait five seconds */
3893static void release_anchor(struct gsm_subscriber_connection *conn)
3894{
3895 if (!conn->anch_operation)
3896 return;
3897
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003898 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003899 talloc_free(conn->anch_operation);
3900 conn->anch_operation = NULL;
3901}
3902
3903static void anchor_timeout(void *_data)
3904{
3905 struct gsm_subscriber_connection *con = _data;
3906
3907 release_anchor(con);
3908 msc_release_connection(con);
3909}
3910
3911int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3912{
3913 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3914 if (!conn->anch_operation)
3915 return -1;
3916
3917 conn->anch_operation->timeout.data = conn;
3918 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003919 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003920 return 0;
3921}
3922
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003923struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
3924{
3925 struct gsm_subscriber_connection *conn;
3926
3927 conn = talloc_zero(network, struct gsm_subscriber_connection);
3928 if (!conn)
3929 return NULL;
3930
3931 conn->network = network;
3932 llist_add_tail(&conn->entry, &network->subscr_conns);
3933 return conn;
3934}
3935
3936void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
3937{
3938 if (!conn)
3939 return;
3940
3941 if (conn->subscr) {
3942 subscr_put(conn->subscr);
3943 conn->subscr = NULL;
3944 }
3945
3946 llist_del(&conn->entry);
3947 talloc_free(conn);
3948}
3949
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003950/* 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 +08003951int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003952{
3953 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003954 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003955 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003956
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003957 OSMO_ASSERT(conn);
3958 OSMO_ASSERT(msg);
3959
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003960 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003961 if (silent_call_reroute(conn, msg))
3962 return silent_call_rx(conn, msg);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003963
Harald Welte52b1f982008-12-23 20:25:15 +00003964 switch (pdisc) {
3965 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003966 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003967 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003968 break;
3969 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003970 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003971 break;
3972 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003973 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003974 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003975 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003976 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003977 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003978 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003979 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003980 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003981 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3982 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003983 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003984 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003985 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003986 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003987 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003988 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003989 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003990 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3991 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003992 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003993 break;
3994 }
3995
3996 return rc;
3997}
Harald Welte8470bf22008-12-25 23:28:35 +00003998
Harald Welte805f6442009-07-28 18:25:29 +02003999/*
Neels Hofmeyrcc7db182016-12-18 23:52:38 +01004000 * This will be run by the linker when loading the DSO. We use it to
Harald Welte805f6442009-07-28 18:25:29 +02004001 * do system initialization, e.g. registration of signal handlers.
4002 */
4003static __attribute__((constructor)) void on_dso_load_0408(void)
4004{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02004005 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02004006}