blob: 7b78d48e2b76e695021619f8848f9d95cf56d691 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020055#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020056#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010057#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080058
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010059#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020062#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010063#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000066
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020067#include <assert.h>
68
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020069void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010070void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020071
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020072static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
73
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020074static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080075static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020076 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010077static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080078static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000079
Harald Welte52b1f982008-12-23 20:25:15 +000080struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020081 uint16_t mcc;
82 uint16_t mnc;
83 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000084};
85
Andreas Eversberga83d5112013-12-07 18:32:28 +010086static int apply_codec_restrictions(struct gsm_bts *bts,
87 struct gsm_mncc_bearer_cap *bcap)
88{
89 int i, j;
90
91 /* remove unsupported speech versions from list */
92 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
103 }
104 bcap->speech_ver[j] = -1;
105
106 return 0;
107}
108
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200109static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800110
Harald Welte31c00f72011-03-03 23:29:05 +0100111void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
112{
113 net->mncc_recv(net, msg);
114}
115
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800116static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
117 struct gsm_trans *trans)
118{
119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
120
121 /* if we get passed a transaction reference, do some common
122 * work that the caller no longer has to do */
123 if (trans) {
124 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
125 msg->lchan = trans->conn->lchan;
126 }
127
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800128 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200129 struct e1inp_sign_link *sign_link =
130 msg->lchan->ts->trx->rsl_link;
131
132 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100133 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800134 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200135 "Sending '%s' to MS.\n",
136 sign_link->trx->bts->nr,
137 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800138 gh->proto_discr & 0xf0,
139 gsm48_cc_msg_name(gh->msg_type));
140 else
141 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200142 "Sending 0x%02x to MS.\n",
143 sign_link->trx->bts->nr,
144 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800145 gh->proto_discr, gh->msg_type);
146 }
147
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100148 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800149}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100150
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800151int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
152{
153 struct gsm48_hdr *gh;
154 struct msgb *ss_notify;
155
156 ss_notify = gsm0480_create_notifySS(message);
157 if (!ss_notify)
158 return -1;
159
160 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
161 uint8_t *data = msgb_push(ss_notify, 1);
162 data[0] = ss_notify->len - 1;
163 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
164 gh->msg_type = GSM48_MT_CC_FACILITY;
165 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
166}
167
Harald Weltecf149ee2012-01-23 16:40:24 +0100168void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100169{
170 if (!conn->sec_operation)
171 return;
172
173 talloc_free(conn->sec_operation);
174 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800175 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100176}
177
Harald Weltecf149ee2012-01-23 16:40:24 +0100178void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100179{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100180 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
181 struct gsm_security_operation);
182}
183
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100184int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
185 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800187 struct gsm_network *net = conn->bts->network;
188 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100189 struct gsm_security_operation *op;
190 struct gsm_auth_tuple atuple;
191 int status = -1, rc;
192
193 /* Check if we _can_ enable encryption. Cases where we can't:
194 * - Encryption disabled in config
195 * - Channel already secured (nothing to do)
196 * - Subscriber equipment doesn't support configured encryption
197 */
198 if (!net->a5_encryption) {
199 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800200 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100201 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200202 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100203 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
204 net->a5_encryption)) {
205 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
206 status = GSM_SECURITY_NOAVAIL;
207 }
208
209 /* If not done yet, try to get info for this user */
210 if (status < 0) {
211 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
212 if (rc <= 0)
213 status = GSM_SECURITY_NOAVAIL;
214 }
215
216 /* Are we done yet ? */
217 if (status >= 0)
218 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800219 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100220 0;
221
222 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100224 return -EBUSY;
225
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800226 allocate_security_operation(conn);
227 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100228 op->cb = cb;
229 op->cb_data = cb_data;
230 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
231
232 /* FIXME: Should start a timer for completion ... */
233
234 /* Then do whatever is needed ... */
Neels Hofmeyrd2fa7a52016-03-10 23:30:37 +0100235 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100236 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800237 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100238 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100239 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100240 return gsm0808_cipher_mode(conn, net->a5_encryption,
241 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100242 }
243
244 return -EINVAL; /* not reached */
245}
246
Holger Freyther73487a22008-12-31 18:53:57 +0000247static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
248 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000249{
250 if (!subscriber)
251 return 0;
252
Holger Freyther73487a22008-12-31 18:53:57 +0000253 /*
254 * Do not send accept yet as more information should arrive. Some
255 * phones will not send us the information and we will have to check
256 * what we want to do with that.
257 */
258 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
259 return 0;
260
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100261 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200262 case GSM_AUTH_POLICY_CLOSED:
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200265 if (subscriber->authorized)
266 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200267 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200268 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000269 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200270 default:
271 return 0;
272 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000273}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000274
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100275static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000276{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100277 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000278 return;
279
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800280 /* No need to keep the connection up */
281 release_anchor(conn);
282
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200283 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100284 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100285 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100286 if (release)
287 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000288}
289
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100290static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000291{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800292 if (conn->loc_operation)
293 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100294 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000295
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100296 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200297 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000298}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000299
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200300static int finish_lu(struct gsm_subscriber_connection *conn)
301{
302 int rc = 0;
303 int avoid_tmsi = conn->bts->network->avoid_tmsi;
304
305 /* We're all good */
306 if (avoid_tmsi) {
307 conn->subscr->tmsi = GSM_RESERVED_TMSI;
308 db_sync_subscriber(conn->subscr);
309 } else {
310 db_subscriber_alloc_tmsi(conn->subscr);
311 }
312
313 rc = gsm0408_loc_upd_acc(conn);
314 if (conn->bts->network->send_mm_info) {
315 /* send MM INFO with network name */
316 rc = gsm48_tx_mm_info(conn);
317 }
318
319 /* call subscr_update after putting the loc_upd_acc
320 * in the transmit queue, since S_SUBSCR_ATTACHED might
321 * trigger further action like SMS delivery */
322 subscr_update(conn->subscr, conn->bts,
323 GSM_SUBSCRIBER_UPDATE_ATTACHED);
324
325 /*
326 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
327 * MS needs to respond with a TMSI REALLOCATION COMPLETE
328 * (even if the TMSI is the same).
329 */
330 if (avoid_tmsi)
331 release_loc_updating_req(conn, 1);
332
333 return rc;
334}
335
Sylvain Munaut267fba02009-12-24 00:28:01 +0100336static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
337 struct msgb *msg, void *data, void *param)
338{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800339 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100340 int rc = 0;
341
342 switch (event) {
343 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100344 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100345 break;
346
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200347 case GSM_SECURITY_ALREADY:
348 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
349 "UPDATING after CM SERVICE REQUEST\n");
350 /* fall through */
351
Sylvain Munaut267fba02009-12-24 00:28:01 +0100352 case GSM_SECURITY_NOAVAIL:
353 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200354 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100355 break;
356
357 default:
358 rc = -EINVAL;
359 };
360
361 return rc;
362}
363
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100364static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000365{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100366 if (!conn->loc_operation)
367 return 0;
368
Sylvain Munaut267fba02009-12-24 00:28:01 +0100369 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800370 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100371 conn->loc_operation->key_seq,
372 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000373 return 0;
374}
375
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800376void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000377{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800378 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800379
380 /* avoid someone issuing a clear */
381 conn->in_release = 1;
382
Holger Freyther7c19f742009-06-06 13:54:35 +0000383 /*
384 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800385 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000386 */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100387 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100388
389 /* We might need to cancel the paging response or such. */
390 if (conn->sec_operation && conn->sec_operation->cb) {
391 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
392 NULL, conn, conn->sec_operation->cb_data);
393 }
394
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800395 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800396 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000397
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100398 /*
399 * Free all transactions that are associated with the released
400 * connection. The transaction code will inform the CC or SMS
401 * facilities that will send the release indications. As part of
402 * the CC REL_IND the remote leg might be released and this will
403 * trigger the call to trans_free. This is something the llist
404 * macro can not handle and we will need to re-iterate the list.
405 *
406 * TODO: Move the trans_list into the subscriber connection and
407 * create a pending list for MT transactions. These exist before
408 * we have a subscriber connection.
409 */
410restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800411 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100412 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200413 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100414 goto restart;
415 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800416 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000417}
418
Harald Welte371efe52010-12-22 23:17:50 +0100419void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
420{
421 struct gsm_trans *trans, *temp;
422
423 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
424
425 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100426 if (trans->protocol == protocol) {
427 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100428 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100429 }
Harald Welte371efe52010-12-22 23:17:50 +0100430 }
431}
432
Holger Freyther429e7762008-12-30 13:28:30 +0000433/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200434int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000435{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800436 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800437 struct msgb *msg;
438
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200439 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800440
441 msg = gsm48_create_loc_upd_rej(cause);
442 if (!msg) {
443 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
444 return -1;
445 }
Harald Welte52b1f982008-12-23 20:25:15 +0000446
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800447 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000448
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100449 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100450 "LAC=%u BTS=%u\n", conn->subscr ?
451 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800452 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100453
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800454 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000455}
456
457/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200458static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000459{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800460 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100461 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000462 struct gsm48_hdr *gh;
463 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200464 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000465
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800466 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000467
468 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
469 gh->proto_discr = GSM48_PDISC_MM;
470 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
471
472 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100473 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000474 bts->network->network_code, bts->location_area_code);
475
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200476 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
477 uint8_t mi[10];
478 int len;
479 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
480 mid = msgb_put(msg, len);
481 memcpy(mid, mi, len);
482 } else {
483 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
484 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
485 }
Harald Welte52b1f982008-12-23 20:25:15 +0000486
487 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
488
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200489 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100490
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800491 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000492}
493
Harald Weltebf5e8df2009-02-03 12:59:45 +0000494/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200495static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000496{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100497 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000498 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000499
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800500 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000501
502 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
503 gh->proto_discr = GSM48_PDISC_MM;
504 gh->msg_type = GSM48_MT_MM_ID_REQ;
505 gh->data[0] = id_type;
506
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800507 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000508}
509
Harald Welte231ad4f2008-12-27 11:15:38 +0000510
Harald Weltebf5e8df2009-02-03 12:59:45 +0000511/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800512static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000513{
514 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000515 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200516 struct gsm_bts *bts = lchan->ts->trx->bts;
517 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200518 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200519 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000520
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200521 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200522 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
523 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000524
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200525 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100526
Harald Welte75a983f2008-12-27 21:34:06 +0000527 switch (mi_type) {
528 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200529 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100530 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100531 conn->subscr = subscr_get_by_imsi(net->subscr_group,
532 mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100533 if (!conn->subscr && net->create_subscriber)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100534 conn->subscr = subscr_create_subscriber(
535 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200536 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100537 if (!conn->subscr && conn->loc_operation) {
538 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
539 release_loc_updating_req(conn, 1);
540 return 0;
541 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100542 if (conn->loc_operation)
543 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000544 break;
545 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000546 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000547 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100548 if (conn->subscr) {
549 db_subscriber_assoc_imei(conn->subscr, mi_string);
550 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200551 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100552 if (conn->loc_operation)
553 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000554 break;
555 }
Holger Freyther73487a22008-12-31 18:53:57 +0000556
557 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100558 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000559}
560
Harald Welte255539c2008-12-28 02:26:27 +0000561
562static void loc_upd_rej_cb(void *data)
563{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100564 struct gsm_subscriber_connection *conn = data;
565 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100566 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000567
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100568 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800569 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100570 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000571}
572
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100573static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000574{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100575 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
576 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200577 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000578}
579
Harald Welteb9845f92015-08-16 18:07:48 +0200580static const struct value_string lupd_names[] = {
581 { GSM48_LUPD_NORMAL, "NORMAL" },
582 { GSM48_LUPD_PERIODIC, "PERIODIC" },
583 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
584 { 0, NULL }
585};
Harald Welte2a139372009-02-22 21:14:55 +0000586
Harald Weltebf5e8df2009-02-03 12:59:45 +0000587/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800588static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000589{
Harald Welte8470bf22008-12-25 23:28:35 +0000590 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000591 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800593 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200594 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200595 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000596
Harald Welte8470bf22008-12-25 23:28:35 +0000597 lu = (struct gsm48_loc_upd_req *) gh->data;
598
599 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000600
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200601 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000602
Harald Welteb9845f92015-08-16 18:07:48 +0200603 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
604 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000605
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200606 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100607
Harald Welte24ff6ee2009-12-22 00:41:05 +0100608 switch (lu->type) {
609 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200610 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100611 break;
612 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200613 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100614 break;
615 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200616 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100617 break;
618 }
619
Holger Freythereaf04692009-06-06 13:54:44 +0000620 /*
621 * Pseudo Spoof detection: Just drop a second/concurrent
622 * location updating request.
623 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100624 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200625 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100626 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800627 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000628 return 0;
629 }
630
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800631 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000632
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200633 conn->loc_operation->key_seq = lu->key_seq;
634
Harald Welte52b1f982008-12-23 20:25:15 +0000635 switch (mi_type) {
636 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200637 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000638 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200639 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100640 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000641
Jan Luebbe370b41d2009-08-12 10:19:34 +0200642 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100643 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100644 if (!subscr && bts->network->create_subscriber) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100645 subscr = subscr_create_subscriber(
646 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200647 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100648 if (!subscr) {
649 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
650 release_loc_updating_req(conn, 0);
651 return 0;
652 }
Harald Welte4b634542008-12-27 01:55:51 +0000653 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000654 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200655 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000656 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100657 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200658 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000659 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000660 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200661 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100662 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000663 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200664 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200665 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200666 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000667 break;
668 case GSM_MI_TYPE_IMEI:
669 case GSM_MI_TYPE_IMEISV:
670 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200671 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000672 break;
673 default:
Harald Weltea0368542009-06-27 02:58:43 +0200674 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000675 break;
676 }
677
Harald Welte24516ea2009-07-04 10:18:00 +0200678 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100679 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200680
Harald Welte4bfdfe72009-06-10 23:11:52 +0800681 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200682 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683 /* FIXME: request id? close channel? */
684 return -EINVAL;
685 }
686
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100687 conn->subscr = subscr;
688 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000689
Harald Welte24516ea2009-07-04 10:18:00 +0200690 /* check if we can let the subscriber into our network immediately
691 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100692 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000693}
694
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100695/* Turn int into semi-octet representation: 98 => 0x89 */
696static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800697{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100698 uint8_t ret;
699
700 ret = value / 10;
701 ret |= (value % 10) << 4;
702
703 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800704}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100705
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706
Harald Weltedb253af2008-12-30 17:56:55 +0000707/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800708int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000709{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100710 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000711 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800712 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100713 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200714 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200715 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100716
Harald Welte4bfdfe72009-06-10 23:11:52 +0800717 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100718 struct tm* gmt_time;
719 struct tm* local_time;
720 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100721 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000722
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800723 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000724
725 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
726 gh->proto_discr = GSM48_PDISC_MM;
727 gh->msg_type = GSM48_MT_MM_INFO;
728
729 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200730#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000731 name_len = strlen(net->name_long);
732 /* 10.5.3.5a */
733 ptr8 = msgb_put(msg, 3);
734 ptr8[0] = GSM48_IE_NAME_LONG;
735 ptr8[1] = name_len*2 +1;
736 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
737
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200738 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000739 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000740 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000741
742 /* FIXME: Use Cell Broadcast, not UCS-2, since
743 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200744#endif
745 name_len = (strlen(net->name_long)*7)/8;
746 name_pad = (8 - strlen(net->name_long)*7)%8;
747 if (name_pad > 0)
748 name_len++;
749 /* 10.5.3.5a */
750 ptr8 = msgb_put(msg, 3);
751 ptr8[0] = GSM48_IE_NAME_LONG;
752 ptr8[1] = name_len +1;
753 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
754
755 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100756 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200757
Harald Weltedb253af2008-12-30 17:56:55 +0000758 }
759
760 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200761#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000762 name_len = strlen(net->name_short);
763 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200764 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200765 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000766 ptr8[1] = name_len*2 + 1;
767 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
768
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000770 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000771 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200772#endif
773 name_len = (strlen(net->name_short)*7)/8;
774 name_pad = (8 - strlen(net->name_short)*7)%8;
775 if (name_pad > 0)
776 name_len++;
777 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200778 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200779 ptr8[0] = GSM48_IE_NAME_SHORT;
780 ptr8[1] = name_len +1;
781 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
782
783 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100784 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200785
Harald Weltedb253af2008-12-30 17:56:55 +0000786 }
787
Harald Weltedb253af2008-12-30 17:56:55 +0000788 /* Section 10.5.3.9 */
789 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100790 gmt_time = gmtime(&cur_t);
791
Harald Weltedb253af2008-12-30 17:56:55 +0000792 ptr8 = msgb_put(msg, 8);
793 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100794 ptr8[1] = bcdify(gmt_time->tm_year % 100);
795 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
796 ptr8[3] = bcdify(gmt_time->tm_mday);
797 ptr8[4] = bcdify(gmt_time->tm_hour);
798 ptr8[5] = bcdify(gmt_time->tm_min);
799 ptr8[6] = bcdify(gmt_time->tm_sec);
800
Harald Welte45f91712012-07-08 16:48:11 +0200801 if (bts->tz.override) {
802 /* Convert tz.hr and tz.mn to units */
803 if (bts->tz.hr < 0) {
804 tzunits = ((bts->tz.hr/-1)*4);
805 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100806 ptr8[7] = bcdify(tzunits);
807 /* Set negative time */
808 ptr8[7] |= 0x08;
809 }
810 else {
Harald Welte45f91712012-07-08 16:48:11 +0200811 tzunits = bts->tz.hr*4;
812 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100813 ptr8[7] = bcdify(tzunits);
814 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100815 /* Convert DST value */
816 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
817 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100818 }
819 else {
820 /* Need to get GSM offset and convert into 15 min units */
821 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
822 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200823#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100824 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200825#else
826#warning find a portable way to obtain the timezone offset
827 tzunits = 0;
828#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100829 if (tzunits < 0) {
830 tzunits = tzunits/-1;
831 ptr8[7] = bcdify(tzunits);
832 /* Flip it to negative */
833 ptr8[7] |= 0x08;
834 }
835 else
836 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100837
838 /* Does not support DST +2 */
839 if (local_time->tm_isdst)
840 dst = 1;
841 }
842
843 if (dst) {
844 ptr8 = msgb_put(msg, 3);
845 ptr8[0] = GSM48_IE_NET_DST;
846 ptr8[1] = 1;
847 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100848 }
Harald Weltedb253af2008-12-30 17:56:55 +0000849
Daniel Willmanneea93372009-08-13 03:42:07 +0200850 DEBUGP(DMM, "-> MM INFO\n");
851
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800852 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000853}
854
Harald Welte7984d5c2009-08-12 22:56:50 +0200855/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200856int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200857{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100858 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200859 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200860 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200861
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200862 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200863
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800864 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200865 gh->proto_discr = GSM48_PDISC_MM;
866 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
867
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100868 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200869
Harald Welte7984d5c2009-08-12 22:56:50 +0200870 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200871 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200872 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200873
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800874 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200875}
876
877/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800878int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200879{
880 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800881 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200882}
883
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100884/*
885 * At the 30C3 phones miss their periodic update
886 * interval a lot and then remain unreachable. In case
887 * we still know the TMSI we can just attach it again.
888 */
889static void implit_attach(struct gsm_subscriber_connection *conn)
890{
891 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
892 return;
893
894 subscr_update(conn->subscr, conn->bts,
895 GSM_SUBSCRIBER_UPDATE_ATTACHED);
896}
897
898
Sylvain Munautba87f452009-12-24 00:28:46 +0100899static int _gsm48_rx_mm_serv_req_sec_cb(
900 unsigned int hooknum, unsigned int event,
901 struct msgb *msg, void *data, void *param)
902{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800903 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100904 int rc = 0;
905
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200906 /* auth failed or succeeded, the timer was stopped */
907 conn->expire_timer_stopped = 1;
908
Sylvain Munautba87f452009-12-24 00:28:46 +0100909 switch (event) {
910 case GSM_SECURITY_AUTH_FAILED:
911 /* Nothing to do */
912 break;
913
914 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200915 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800916 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100917 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100918 break;
919
920 case GSM_SECURITY_SUCCEEDED:
921 /* nothing to do. CIPHER MODE COMMAND is
922 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100923 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100924 break;
925
926 default:
927 rc = -EINVAL;
928 };
929
930 return rc;
931}
932
Harald Welte4ed0e922009-01-10 03:17:30 +0000933/*
934 * Handle CM Service Requests
935 * a) Verify that the packet is long enough to contain the information
936 * we require otherwsie reject with INCORRECT_MESSAGE
937 * b) Try to parse the TMSI. If we do not have one reject
938 * c) Check that we know the subscriber with the TMSI otherwise reject
939 * with a HLR cause
940 * d) Set the subscriber on the gsm_lchan and accept
941 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800942static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000943{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200944 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200945 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000946
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800947 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000948 struct gsm_subscriber *subscr;
949 struct gsm48_hdr *gh = msgb_l3(msg);
950 struct gsm48_service_request *req =
951 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800952 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200953 uint8_t classmark2_len = gh->data[1];
954 uint8_t *classmark2 = gh->data+2;
955 uint8_t mi_len = *(classmark2 + classmark2_len);
956 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000957
Harald Weltec9e02182009-05-01 19:07:53 +0000958 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000959 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000960 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800961 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000962 GSM48_REJECT_INCORRECT_MESSAGE);
963 }
964
965 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000966 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800967 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000968 GSM48_REJECT_INCORRECT_MESSAGE);
969 }
970
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200971 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000972 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200973
974 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200975 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
976 req->cm_service_type, gsm48_mi_type_name(mi_type),
977 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200978 subscr = subscr_get_by_imsi(bts->network->subscr_group,
979 mi_string);
980 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200981 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
982 req->cm_service_type, gsm48_mi_type_name(mi_type),
983 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200984 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
985 tmsi_from_string(mi_string));
986 } else {
987 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800988 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000989 GSM48_REJECT_INCORRECT_MESSAGE);
990 }
991
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200992 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100993
Harald Welte3ac7f102009-08-10 10:12:45 +0200994 if (is_siemens_bts(bts))
995 send_siemens_mrpci(msg->lchan, classmark2-1);
996
Holger Freythereb443982009-06-04 13:58:42 +0000997
Harald Welte2a139372009-02-22 21:14:55 +0000998 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000999 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001000 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001001 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001002
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001003 if (!conn->subscr)
1004 conn->subscr = subscr;
1005 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001006 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1007 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001008 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1009 subscr_put(subscr);
1010 }
1011
Harald Weltec2e302d2009-07-05 14:08:13 +02001012 subscr->equipment.classmark2_len = classmark2_len;
1013 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1014 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001015
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001016 /* we will send a MM message soon */
1017 conn->expire_timer_stopped = 1;
1018
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001019 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001020 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001021}
1022
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001023static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001024{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001025 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001026 struct gsm48_hdr *gh = msgb_l3(msg);
1027 struct gsm48_imsi_detach_ind *idi =
1028 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001029 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001030 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001031 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001032
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001033 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001034 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1035 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001036
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001037 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001038
Harald Welte2a139372009-02-22 21:14:55 +00001039 switch (mi_type) {
1040 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001041 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001042 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001043 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001044 break;
1045 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001046 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001047 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1048 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001049 break;
1050 case GSM_MI_TYPE_IMEI:
1051 case GSM_MI_TYPE_IMEISV:
1052 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001053 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001054 break;
1055 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001056 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001057 break;
1058 }
1059
Holger Freyther4a49e772009-04-12 05:37:29 +00001060 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001061 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001062 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001063 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001064
1065 subscr->equipment.classmark1 = idi->classmark1;
1066 db_sync_equipment(&subscr->equipment);
1067
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001068 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001069 } else
Harald Welte2a139372009-02-22 21:14:55 +00001070 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1071
Harald Welte31981a02009-12-20 09:58:40 +01001072 /* FIXME: iterate over all transactions and release them,
1073 * imagine an IMSI DETACH happening during an active call! */
1074
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001075 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001076 return 0;
1077}
1078
Harald Welted2a7f5a2009-06-05 20:08:20 +00001079static int gsm48_rx_mm_status(struct msgb *msg)
1080{
1081 struct gsm48_hdr *gh = msgb_l3(msg);
1082
1083 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1084
1085 return 0;
1086}
1087
Sylvain Munaut30a15382009-12-24 00:27:26 +01001088/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001089static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001090{
1091 struct gsm48_hdr *gh = msgb_l3(msg);
1092 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001093 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001094
Sylvain Munautc593cf12010-06-10 23:51:41 +02001095 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001096 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001097
1098 /* Safety check */
1099 if (!conn->sec_operation) {
1100 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1101 return -EIO;
1102 }
1103
1104 /* Validate SRES */
1105 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001106 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001107 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001108
1109 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001110 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001111
Sylvain Munaut30a15382009-12-24 00:27:26 +01001112 if (cb)
1113 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001114 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001115
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001116 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001117 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001118 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001119 }
1120
Sylvain Munautc593cf12010-06-10 23:51:41 +02001121 DEBUGPC(DMM, "OK\n");
1122
Sylvain Munaut30a15382009-12-24 00:27:26 +01001123 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001124 return gsm0808_cipher_mode(conn, net->a5_encryption,
1125 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001126}
1127
Harald Weltebf5e8df2009-02-03 12:59:45 +00001128/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001129static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001130{
1131 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001132 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001133
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001134 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001135 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001136 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001137 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001138 break;
1139 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001140 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001141 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001142 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001143 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001144 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001145 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001146 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001147 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001148 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001149 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001150 conn->subscr ?
1151 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001152 "unknown subscriber");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001153 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001154 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001155 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001156 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001157 break;
1158 case GSM48_MT_MM_CM_REEST_REQ:
1159 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1160 break;
1161 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001163 break;
1164 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001165 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001166 gh->msg_type);
1167 break;
1168 }
1169
1170 return rc;
1171}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001172
Harald Welte2d35ae62009-02-06 12:02:13 +00001173/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001174static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001175{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001176 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001177 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001178 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001179 uint8_t *classmark2_lv = gh->data + 1;
1180 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001181 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001182 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001183 int rc = 0;
1184
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001185 resp = (struct gsm48_pag_resp *) &gh->data[0];
1186 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1187 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001188 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1189 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001190
Harald Weltefe18d8f2009-02-22 21:14:24 +00001191 switch (mi_type) {
1192 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001193 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001194 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001195 break;
1196 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001197 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1198 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001199 break;
1200 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001201
1202 if (!subscr) {
1203 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001204 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001205 return -EINVAL;
1206 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001207 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001208 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001209 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001210
Harald Weltec2e302d2009-07-05 14:08:13 +02001211 subscr->equipment.classmark2_len = *classmark2_lv;
1212 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1213 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001214
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001215 /* We received a paging */
1216 conn->expire_timer_stopped = 1;
1217
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001218 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001219 return rc;
1220}
1221
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001222static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001223{
1224 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001225 uint8_t apdu_id_flags;
1226 uint8_t apdu_len;
1227 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001228
1229 apdu_id_flags = gh->data[0];
1230 apdu_len = gh->data[1];
1231 apdu_data = gh->data+2;
1232
Harald Welte70f92052012-07-16 13:22:19 +02001233 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001234 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001235
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001236 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001237}
1238
Harald Weltebf5e8df2009-02-03 12:59:45 +00001239/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001240static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001241{
1242 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001243 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001244
1245 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001246 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001247 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001248 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001249 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001250 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001251 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001252 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001253 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001254 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001255 break;
1256 }
1257
Harald Welte2d35ae62009-02-06 12:02:13 +00001258 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001259}
1260
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001261int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1262 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001263{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001264 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001265 struct gsm48_hdr *gh;
1266
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001267 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001268
1269 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1270 apdu_id, apdu_len);
1271
1272 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1273 gh->proto_discr = GSM48_PDISC_RR;
1274 gh->msg_type = GSM48_MT_RR_APP_INFO;
1275 gh->data[0] = apdu_id;
1276 gh->data[1] = apdu_len;
1277 memcpy(gh->data+2, apdu, apdu_len);
1278
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001279 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001280}
1281
Harald Welte4bc90a12008-12-27 16:32:52 +00001282/* Call Control */
1283
Harald Welte7584aea2009-02-11 11:44:12 +00001284/* The entire call control code is written in accordance with Figure 7.10c
1285 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1286 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1287 * it for voice */
1288
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289static void new_cc_state(struct gsm_trans *trans, int state)
1290{
1291 if (state > 31 || state < 0)
1292 return;
1293
1294 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001295 gsm48_cc_state_name(trans->cc.state),
1296 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297
Harald Weltedcaf5652009-07-23 18:56:43 +02001298 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299}
1300
1301static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001302{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001303 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001304 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001305 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001306
Harald Welte4bc90a12008-12-27 16:32:52 +00001307 gh->msg_type = GSM48_MT_CC_STATUS;
1308
1309 cause = msgb_put(msg, 3);
1310 cause[0] = 2;
1311 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1312 cause[2] = 0x80 | 30; /* response to status inquiry */
1313
1314 call_state = msgb_put(msg, 1);
1315 call_state[0] = 0xc0 | 0x00;
1316
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001317 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001318}
1319
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001320static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001321 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001322{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001323 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1325
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001326 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001327
Harald Welte6f4b7532008-12-29 00:39:37 +00001328 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001329 gh->msg_type = msg_type;
1330
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001331 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001332}
1333
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1335{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001336 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001337 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001338 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001339 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 }
1341}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001342
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1344 int msg_type, struct gsm_mncc *mncc)
1345{
1346 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001347 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348
1349 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001350 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001351 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001353 trans->conn->lchan->ts->trx->bts->nr,
1354 trans->conn->lchan->ts->trx->nr,
1355 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356 (trans->subscr)?(trans->subscr->extension):"-",
1357 get_mncc_name(msg_type));
1358 else
1359 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1360 "Sending '%s' to MNCC.\n",
1361 (trans->subscr)?(trans->subscr->extension):"-",
1362 get_mncc_name(msg_type));
1363 else
1364 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1365 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1366
1367 mncc->msg_type = msg_type;
1368
Harald Welte966636f2009-06-26 19:39:35 +02001369 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370 if (!msg)
1371 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001372
1373 data = msgb_put(msg, sizeof(struct gsm_mncc));
1374 memcpy(data, mncc, sizeof(struct gsm_mncc));
1375
Harald Welte54209c22010-12-22 23:43:29 +01001376 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377
1378 return 0;
1379}
1380
1381int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001382 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383{
1384 struct gsm_mncc rel;
1385
Harald Welte92f70c52009-06-12 01:54:08 +08001386 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001388 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001389 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1390 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001391 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1392}
1393
Harald Weltedcaf5652009-07-23 18:56:43 +02001394/* Call Control Specific transaction release.
1395 * gets called by trans_free, DO NOT CALL YOURSELF! */
1396void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 gsm48_stop_cc_timer(trans);
1399
1400 /* send release to L4, if callref still exists */
1401 if (trans->callref) {
1402 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001403 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001404 GSM48_CAUSE_LOC_PRN_S_LU,
1405 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001407 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001409 if (trans->conn)
1410 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411}
1412
1413static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001414
Harald Welte09e38af2009-02-16 22:52:23 +00001415/* call-back from paging the B-end of the connection */
1416static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001417 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001418{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001419 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001420 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001421
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001422 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001423
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001425 switch (event) {
1426 case GSM_PAGING_SUCCEEDED:
1427 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001428 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001429 /* Assign lchan */
1430 transt->conn = conn;
1431 /* send SETUP request to called party */
1432 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1433 break;
1434 case GSM_PAGING_EXPIRED:
1435 case GSM_PAGING_BUSY:
1436 DEBUGP(DCC, "Paging subscr %s expired!\n",
1437 transt->subscr->extension);
1438 /* Temporarily out of order */
1439 mncc_release_ind(transt->net, transt,
1440 transt->callref,
1441 GSM48_CAUSE_LOC_PRN_S_LU,
1442 GSM48_CC_CAUSE_DEST_OOO);
1443 transt->callref = 0;
1444 transt->paging_request = NULL;
1445 trans_free(transt);
1446 break;
1447 default:
1448 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1449 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001450 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001451
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001452 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001453 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001454}
Harald Welte7584aea2009-02-11 11:44:12 +00001455
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001456static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001457
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001458/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001459static int switch_for_handover(struct gsm_lchan *old_lchan,
1460 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001461{
1462 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001463
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001464 /* Ask the new socket to send to the already known port. */
1465 if (new_lchan->conn->mncc_rtp_bridge) {
1466 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1467 rsl_ipacc_mdcx(new_lchan,
1468 old_lchan->abis_ip.connect_ip,
1469 old_lchan->abis_ip.connect_port, 0);
1470 return 0;
1471 }
1472
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001473 if (ipacc_rtp_direct) {
1474 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1475 return 0;
1476 }
1477
1478 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001479 new_rs = new_lchan->abis_ip.rtp_socket;
1480 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001481
1482 if (!new_rs) {
1483 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1484 return -EIO;
1485 }
1486
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001487 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001488
1489 if (!old_rs) {
1490 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1491 return -EIO;
1492 }
1493
1494 /* copy rx_action and reference to other sock */
1495 new_rs->rx_action = old_rs->rx_action;
1496 new_rs->tx_action = old_rs->tx_action;
1497 new_rs->transmit = old_rs->transmit;
1498
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001499 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001500 case RTP_PROXY:
1501 other_rs = old_rs->proxy.other_sock;
1502 rtp_socket_proxy(new_rs, other_rs);
1503 /* delete reference to other end socket to prevent
1504 * rtp_socket_free() from removing the inverse reference */
1505 old_rs->proxy.other_sock = NULL;
1506 break;
1507 case RTP_RECV_UPSTREAM:
1508 new_rs->receive = old_rs->receive;
1509 break;
1510 case RTP_NONE:
1511 break;
1512 }
1513
1514 return 0;
1515}
1516
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001517static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1518{
1519 struct gsm_lchan *old_lchan;
1520 old_lchan = bsc_handover_pending(lchan);
1521 if (old_lchan)
1522 switch_for_handover(old_lchan, lchan);
1523}
1524
Harald Welte805f6442009-07-28 18:25:29 +02001525/* some other part of the code sends us a signal */
1526static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1527 void *handler_data, void *signal_data)
1528{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001529 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001530 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001531 struct gsm_network *net;
1532 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001533
1534 if (subsys != SS_ABISIP)
1535 return 0;
1536
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001537 /* RTP bridge handling */
1538 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1539 return tch_rtp_signal(lchan, signal);
1540
Harald Welte805f6442009-07-28 18:25:29 +02001541 /* in case we use direct BTS-to-BTS RTP */
1542 if (ipacc_rtp_direct)
1543 return 0;
1544
Harald Welte805f6442009-07-28 18:25:29 +02001545 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001546 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001547 /* in case we don't use direct BTS-to-BTS RTP */
1548 /* the BTS has successfully bound a TCH to a local ip/port,
1549 * which means we can connect our UDP socket to it */
1550 if (lchan->abis_ip.rtp_socket) {
1551 rtp_socket_free(lchan->abis_ip.rtp_socket);
1552 lchan->abis_ip.rtp_socket = NULL;
1553 }
1554
1555 lchan->abis_ip.rtp_socket = rtp_socket_create();
1556 if (!lchan->abis_ip.rtp_socket)
1557 return -EIO;
1558
1559 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1560 lchan->abis_ip.bound_ip,
1561 lchan->abis_ip.bound_port);
1562 if (rc < 0)
1563 return -EIO;
1564
Harald Welteda7ab742009-12-19 22:23:05 +01001565 /* check if any transactions on this lchan still have
1566 * a tch_recv_mncc request pending */
1567 net = lchan->ts->trx->bts->network;
1568 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001569 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001570 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1571 tch_recv_mncc(net, trans->callref, 1);
1572 }
1573 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001574
1575 /*
1576 * TODO: this appears to be too early? Why not until after
1577 * the handover detect or the handover complete?
1578 *
1579 * Do we have a handover pending for this new lchan? In that
1580 * case re-route the audio from the old channel to the new one.
1581 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001582 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001583 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001584 case S_ABISIP_DLCX_IND:
1585 /* the BTS tells us a RTP stream has been disconnected */
1586 if (lchan->abis_ip.rtp_socket) {
1587 rtp_socket_free(lchan->abis_ip.rtp_socket);
1588 lchan->abis_ip.rtp_socket = NULL;
1589 }
1590
1591 break;
Harald Welte805f6442009-07-28 18:25:29 +02001592 }
1593
1594 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001595}
1596
Harald Welte49f48b82009-02-17 15:29:33 +00001597/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001598static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001599{
Harald Welte11fa29c2009-02-19 17:24:39 +00001600 struct gsm_bts *bts = lchan->ts->trx->bts;
1601 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001602 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Harald Welte805f6442009-07-28 18:25:29 +02001603 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001604
Max8db12e42016-04-18 23:11:18 +02001605 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1606 "(bts=%u,trx=%u,ts=%u,%s)\n",
1607 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1608 get_value_string(gsm_chan_t_names, lt),
1609 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1610 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001611
1612 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001613 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001614 return -EINVAL;
1615 }
Harald Welteda7ab742009-12-19 22:23:05 +01001616
1617 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001618 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001619 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001620 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001621 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001622 if (!lchan->abis_ip.rtp_socket) {
1623 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1624 "lchan\n");
1625 return -EIO;
1626 }
1627 if (!remote_lchan->abis_ip.rtp_socket) {
1628 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1629 "remote_lchan\n");
1630 return -EIO;
1631 }
1632
Harald Welte805f6442009-07-28 18:25:29 +02001633 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001634 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001635 if (rc < 0)
1636 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001637 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001638 if (rc < 0)
1639 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001640 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301641 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1642 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001643 } else {
1644 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301645 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1646 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301647 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001648 if (rc < 0)
1649 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301650 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1651 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301652 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001653 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001654 break;
1655 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001656 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001657 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001658 trau_mux_map_lchan(lchan, remote_lchan);
1659 break;
1660 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001661 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001662 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001663 }
Harald Welte49f48b82009-02-17 15:29:33 +00001664
1665 return 0;
1666}
1667
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001669static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001670{
Harald Welte53d51f52015-12-03 14:59:04 +01001671 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1672 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001673
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001675 return -EIO;
1676
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001677 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 return -EIO;
1679
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001680 /* Which subscriber do we want to track trans1 or trans2? */
1681 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1682
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001684 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001685}
1686
Harald Welteda7ab742009-12-19 22:23:05 +01001687/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001688static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689{
1690 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001691 struct gsm_lchan *lchan;
1692 struct gsm_bts *bts;
1693 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001694
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001696 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 if (!trans)
1698 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001699 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001701
1702 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001703 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001704 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001706 /* store receive state */
1707 trans->tch_recv = enable;
1708
Harald Welteda7ab742009-12-19 22:23:05 +01001709 switch (bts->type) {
1710 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001711 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001712 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001713 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001714 return -EINVAL;
1715 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001716 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1717 * will not be connected to our RTP proxy and the socket will
1718 * not be assigned to the application interface. This method
1719 * will be called again, once the audio socket is created and
1720 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001721 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001722 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1723 return 0;
1724 }
1725 if (enable) {
1726 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001727 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001728 if (rc < 0)
1729 return rc;
1730 /* assign socket to application interface */
1731 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1732 net, callref);
1733 } else
1734 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1735 net, 0);
1736 break;
1737 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001738 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001739 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001740 /* In case we don't have a TCH with correct mode, the TRAU muxer
1741 * will not be asigned to the application interface. This is
1742 * performed by switch_trau_mux() after successful handover or
1743 * assignment. */
1744 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1745 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1746 return 0;
1747 }
Harald Welteda7ab742009-12-19 22:23:05 +01001748 if (enable)
1749 return trau_recv_lchan(lchan, callref);
1750 return trau_mux_unmap(NULL, callref);
1751 break;
1752 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001753 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001754 return -EINVAL;
1755 }
1756
1757 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758}
1759
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1761{
1762 DEBUGP(DCC, "-> STATUS ENQ\n");
1763 return gsm48_cc_tx_status(trans, msg);
1764}
1765
1766static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1767static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1768
1769static void gsm48_cc_timeout(void *arg)
1770{
1771 struct gsm_trans *trans = arg;
1772 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001773 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1774 int mo_location = GSM48_CAUSE_LOC_USER;
1775 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1776 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 struct gsm_mncc mo_rel, l4_rel;
1778
1779 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1780 mo_rel.callref = trans->callref;
1781 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1782 l4_rel.callref = trans->callref;
1783
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 case 0x303:
1786 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001787 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 break;
1789 case 0x310:
1790 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001791 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 break;
1793 case 0x313:
1794 disconnect = 1;
1795 /* unknown, did not find it in the specs */
1796 break;
1797 case 0x301:
1798 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001799 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800 break;
1801 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001802 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001804 gsm48_cc_tx_release(trans, &trans->cc.msg);
1805 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 break; /* stay in release state */
1807 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001808 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 return;
1810// release = 1;
1811// l4_cause = 14;
1812// break;
1813 case 0x306:
1814 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001815 mo_cause = trans->cc.msg.cause.value;
1816 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 break;
1818 case 0x323:
1819 disconnect = 1;
1820 break;
1821 default:
1822 release = 1;
1823 }
1824
1825 if (release && trans->callref) {
1826 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001827 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001828 l4_location, l4_cause);
1829 trans->callref = 0;
1830 }
1831
1832 if (disconnect && trans->callref) {
1833 /* process disconnect towards layer 4 */
1834 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001835 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 }
1837
1838 /* process disconnect towards mobile station */
1839 if (disconnect || release) {
1840 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001841 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1842 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1843 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 mo_rel.cause.diag_len = 3;
1845
1846 if (disconnect)
1847 gsm48_cc_tx_disconnect(trans, &mo_rel);
1848 if (release)
1849 gsm48_cc_tx_release(trans, &mo_rel);
1850 }
1851
1852}
1853
1854static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1855 int sec, int micro)
1856{
1857 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001858 trans->cc.timer.cb = gsm48_cc_timeout;
1859 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001860 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001861 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862}
1863
1864static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1865{
1866 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001867 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1869 struct tlv_parsed tp;
1870 struct gsm_mncc setup;
1871
1872 memset(&setup, 0, sizeof(struct gsm_mncc));
1873 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001874 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001875 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 /* emergency setup is identified by msg_type */
1877 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1878 setup.emergency = 1;
1879
1880 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001881 setup.fields |= MNCC_F_CALLING;
1882 strncpy(setup.calling.number, trans->subscr->extension,
1883 sizeof(setup.calling.number)-1);
1884 strncpy(setup.imsi, trans->subscr->imsi,
1885 sizeof(setup.imsi)-1);
1886
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 /* bearer capability */
1888 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1889 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001890 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001892 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 }
1894 /* facility */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1896 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001897 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1899 }
1900 /* called party bcd number */
1901 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1902 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001903 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1905 }
1906 /* user-user */
1907 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1908 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001909 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1911 }
1912 /* ss-version */
1913 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1914 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001915 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1917 }
1918 /* CLIR suppression */
1919 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1920 setup.clir.sup = 1;
1921 /* CLIR invocation */
1922 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1923 setup.clir.inv = 1;
1924 /* cc cap */
1925 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1926 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1929 }
1930
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 new_cc_state(trans, GSM_CSTATE_INITIATED);
1932
Harald Welte (local)47df3992009-12-26 19:45:03 +01001933 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1934 subscr_name(trans->subscr), trans->subscr->extension,
1935 setup.called.number);
1936
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001937 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001938
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001940 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941
Harald Welte13cac662009-07-29 12:10:35 +02001942 /* MNCC code will modify the channel asynchronously, we should
1943 * ipaccess-bind only after the modification has been made to the
1944 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 return 0;
1946}
1947
1948static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001949{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001950 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001951 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001953 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001954
Harald Welte7ccf7782009-02-17 01:43:01 +00001955 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* transaction id must not be assigned */
1958 if (trans->transaction_id != 0xff) { /* unasssigned */
1959 DEBUGP(DCC, "TX Setup with assigned transaction. "
1960 "This is not allowed!\n");
1961 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001962 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001963 GSM48_CAUSE_LOC_PRN_S_LU,
1964 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001966 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 return rc;
1968 }
1969
1970 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001971 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1972 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001973 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001975 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001976 GSM48_CAUSE_LOC_PRN_S_LU,
1977 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001979 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 return rc;
1981 }
Harald Welte78283ef2009-07-23 21:36:44 +02001982 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001983
Harald Welte65e74cc2008-12-29 01:55:35 +00001984 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001985
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001987
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* bearer capability */
1989 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 /* facility */
1992 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001993 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 /* progress */
1995 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001996 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997 /* calling party BCD number */
1998 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 /* called party BCD number */
2001 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 /* user-user */
2004 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 /* redirecting party BCD number */
2007 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 /* signal */
2010 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002011 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012
2013 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002014
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002015 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01002016
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002017 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002018}
2019
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2021{
2022 struct gsm48_hdr *gh = msgb_l3(msg);
2023 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2024 struct tlv_parsed tp;
2025 struct gsm_mncc call_conf;
2026
2027 gsm48_stop_cc_timer(trans);
2028 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2029
2030 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2031 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002032 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002033 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034#if 0
2035 /* repeat */
2036 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2037 call_conf.repeat = 1;
2038 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2039 call_conf.repeat = 2;
2040#endif
2041 /* bearer capability */
2042 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2043 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002044 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002046 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 }
2048 /* cause */
2049 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2050 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2053 }
2054 /* cc cap */
2055 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2056 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002057 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2059 }
2060
Andreas Eversberg035b8742013-09-19 09:27:06 +02002061 /* IMSI of called subscriber */
2062 strncpy(call_conf.imsi, trans->subscr->imsi,
2063 sizeof(call_conf.imsi)-1);
2064
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2066
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002067 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002068 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069}
2070
2071static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2072{
2073 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002074 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2076
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2078
2079 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2080
2081 /* bearer capability */
2082 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002083 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 /* facility */
2085 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002086 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 /* progress */
2088 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002089 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002091 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092}
2093
2094static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2095{
2096 struct gsm48_hdr *gh = msgb_l3(msg);
2097 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2098 struct tlv_parsed tp;
2099 struct gsm_mncc alerting;
2100
2101 gsm48_stop_cc_timer(trans);
2102 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2103
2104 memset(&alerting, 0, sizeof(struct gsm_mncc));
2105 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002106 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 /* facility */
2108 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2109 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2112 }
2113
2114 /* progress */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2116 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2119 }
2120 /* ss-version */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2122 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2125 }
2126
2127 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2128
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002129 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002130 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131}
2132
2133static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2134{
2135 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002136 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2138
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139 gh->msg_type = GSM48_MT_CC_ALERTING;
2140
2141 /* facility */
2142 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 /* progress */
2145 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002146 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 /* user-user */
2148 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002149 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150
2151 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2152
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002153 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154}
2155
2156static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2157{
2158 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002159 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2161
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 gh->msg_type = GSM48_MT_CC_PROGRESS;
2163
2164 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002165 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 /* user-user */
2167 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002170 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171}
2172
2173static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2174{
2175 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002176 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2178
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 gh->msg_type = GSM48_MT_CC_CONNECT;
2180
2181 gsm48_stop_cc_timer(trans);
2182 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2183
2184 /* facility */
2185 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* progress */
2188 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 /* connected number */
2191 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 /* user-user */
2194 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002195 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196
2197 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2198
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002199 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200}
2201
2202static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2203{
2204 struct gsm48_hdr *gh = msgb_l3(msg);
2205 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2206 struct tlv_parsed tp;
2207 struct gsm_mncc connect;
2208
2209 gsm48_stop_cc_timer(trans);
2210
2211 memset(&connect, 0, sizeof(struct gsm_mncc));
2212 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002213 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002215 connect.fields |= MNCC_F_CONNECTED;
2216 strncpy(connect.connected.number, trans->subscr->extension,
2217 sizeof(connect.connected.number)-1);
2218 strncpy(connect.imsi, trans->subscr->imsi,
2219 sizeof(connect.imsi)-1);
2220
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 /* facility */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2223 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2226 }
2227 /* user-user */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2229 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2232 }
2233 /* ss-version */
2234 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2235 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2238 }
2239
2240 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002241 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002243 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244}
2245
2246
2247static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2248{
2249 struct gsm_mncc connect_ack;
2250
2251 gsm48_stop_cc_timer(trans);
2252
2253 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002254 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255
2256 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2257 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002258
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002259 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 &connect_ack);
2261}
2262
2263static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2264{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002265 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2267
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2269
2270 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2271
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002272 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273}
2274
2275static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2276{
2277 struct gsm48_hdr *gh = msgb_l3(msg);
2278 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2279 struct tlv_parsed tp;
2280 struct gsm_mncc disc;
2281
2282 gsm48_stop_cc_timer(trans);
2283
2284 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2285
2286 memset(&disc, 0, sizeof(struct gsm_mncc));
2287 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002288 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 /* cause */
2290 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2291 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002292 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2294 }
2295 /* facility */
2296 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2297 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002298 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2300 }
2301 /* user-user */
2302 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2303 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002304 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2306 }
2307 /* ss-version */
2308 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2309 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002310 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2312 }
2313
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002314 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315
2316}
2317
Harald Weltec66b71c2009-06-11 14:23:20 +08002318static struct gsm_mncc_cause default_cause = {
2319 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2320 .coding = 0,
2321 .rec = 0,
2322 .rec_val = 0,
2323 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2324 .diag_len = 0,
2325 .diag = { 0 },
2326};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327
2328static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2329{
2330 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002331 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2333
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2335
2336 gsm48_stop_cc_timer(trans);
2337 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2338
2339 /* cause */
2340 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002341 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 else
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344
2345 /* facility */
2346 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002347 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 /* progress */
2349 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002350 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 /* user-user */
2352 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354
2355 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002356 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357
2358 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2359
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002360 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361}
2362
2363static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2364{
2365 struct gsm48_hdr *gh = msgb_l3(msg);
2366 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2367 struct tlv_parsed tp;
2368 struct gsm_mncc rel;
2369 int rc;
2370
2371 gsm48_stop_cc_timer(trans);
2372
2373 memset(&rel, 0, sizeof(struct gsm_mncc));
2374 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002375 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 /* cause */
2377 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2378 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002379 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2381 }
2382 /* facility */
2383 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2384 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002385 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2387 }
2388 /* user-user */
2389 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2390 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002391 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2393 }
2394 /* ss-version */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2396 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2399 }
2400
Harald Weltedcaf5652009-07-23 18:56:43 +02002401 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002403 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002405 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002406 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002407 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002408 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 }
2410
2411 new_cc_state(trans, GSM_CSTATE_NULL);
2412
2413 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002414 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415
2416 return rc;
2417}
2418
2419static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2420{
2421 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002422 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2424
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 gh->msg_type = GSM48_MT_CC_RELEASE;
2426
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 gsm48_stop_cc_timer(trans);
2428 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2429
2430 /* cause */
2431 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002432 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 /* facility */
2434 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002435 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 /* user-user */
2437 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
Harald Weltedcaf5652009-07-23 18:56:43 +02002440 trans->cc.T308_second = 0;
2441 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442
Harald Weltedcaf5652009-07-23 18:56:43 +02002443 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2445
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002446 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2450{
2451 struct gsm48_hdr *gh = msgb_l3(msg);
2452 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2453 struct tlv_parsed tp;
2454 struct gsm_mncc rel;
2455 int rc = 0;
2456
2457 gsm48_stop_cc_timer(trans);
2458
2459 memset(&rel, 0, sizeof(struct gsm_mncc));
2460 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002461 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 /* cause */
2463 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2464 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002465 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2467 }
2468 /* facility */
2469 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2470 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002471 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2473 }
2474 /* user-user */
2475 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2476 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002477 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2479 }
2480 /* ss-version */
2481 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2482 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002483 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2485 }
2486
2487 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002488 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002490 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 MNCC_REJ_IND, &rel);
2492 break;
2493 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002494 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 MNCC_REL_CNF, &rel);
2496 break;
2497 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002498 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 MNCC_REL_IND, &rel);
2500 }
2501 }
2502
2503 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002504 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505
2506 return rc;
2507}
2508
2509static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2510{
2511 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002512 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002514 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2517
2518 trans->callref = 0;
2519
2520 gsm48_stop_cc_timer(trans);
2521
2522 /* cause */
2523 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002524 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 /* facility */
2526 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002527 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 /* user-user */
2529 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002530 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531
Harald Weltec7782de2010-12-21 19:31:41 +01002532 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2533
Harald Weltedcaf5652009-07-23 18:56:43 +02002534 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535
Harald Weltec7782de2010-12-21 19:31:41 +01002536 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537}
2538
2539static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2540{
2541 struct gsm48_hdr *gh = msgb_l3(msg);
2542 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2543 struct tlv_parsed tp;
2544 struct gsm_mncc fac;
2545
2546 memset(&fac, 0, sizeof(struct gsm_mncc));
2547 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002548 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 /* facility */
2550 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2551 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002552 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2554 }
2555 /* ss-version */
2556 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2557 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002558 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2560 }
2561
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002562 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563}
2564
2565static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2566{
2567 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002568 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2570
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 gh->msg_type = GSM48_MT_CC_FACILITY;
2572
2573 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002576 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577}
2578
2579static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2580{
2581 struct gsm_mncc hold;
2582
2583 memset(&hold, 0, sizeof(struct gsm_mncc));
2584 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002585 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586}
2587
2588static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2589{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002590 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2592
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2594
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002595 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596}
2597
2598static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2599{
2600 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002601 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2603
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2605
2606 /* cause */
2607 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002608 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 else
Harald Welte55c8f352010-03-07 23:40:35 +01002610 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002612 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2616{
2617 struct gsm_mncc retrieve;
2618
2619 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2620 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002621 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002622 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623}
2624
2625static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2626{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002627 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2629
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2631
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002632 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002638 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2642
2643 /* cause */
2644 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 else
Harald Welte55c8f352010-03-07 23:40:35 +01002647 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002649 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650}
2651
2652static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2653{
2654 struct gsm48_hdr *gh = msgb_l3(msg);
2655 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2656 struct tlv_parsed tp;
2657 struct gsm_mncc dtmf;
2658
2659 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2660 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002661 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662 /* keypad facility */
2663 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2664 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002665 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2667 }
2668
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002669 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2673{
2674 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002675 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2677
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2679
2680 /* keypad */
2681 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002682 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002684 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685}
2686
2687static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2688{
2689 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002690 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2692
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2694
2695 /* cause */
2696 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002697 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 else
Harald Welte55c8f352010-03-07 23:40:35 +01002699 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002701 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702}
2703
2704static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2705{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002706 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2708
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2710
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002711 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712}
2713
2714static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2715{
2716 struct gsm_mncc dtmf;
2717
2718 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2719 dtmf.callref = trans->callref;
2720
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002721 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722}
2723
2724static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2725{
2726 struct gsm48_hdr *gh = msgb_l3(msg);
2727 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2728 struct tlv_parsed tp;
2729 struct gsm_mncc modify;
2730
2731 memset(&modify, 0, sizeof(struct gsm_mncc));
2732 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002733 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 /* bearer capability */
2735 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2736 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002737 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002739 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 }
2741
2742 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2743
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002744 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745}
2746
2747static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2748{
2749 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002750 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2752
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 gh->msg_type = GSM48_MT_CC_MODIFY;
2754
2755 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2756
2757 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002758 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002759
2760 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2761
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002762 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763}
2764
2765static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2766{
2767 struct gsm48_hdr *gh = msgb_l3(msg);
2768 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2769 struct tlv_parsed tp;
2770 struct gsm_mncc modify;
2771
2772 gsm48_stop_cc_timer(trans);
2773
2774 memset(&modify, 0, sizeof(struct gsm_mncc));
2775 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002776 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777 /* bearer capability */
2778 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2779 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002780 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002781 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002782 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783 }
2784
2785 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2786
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002787 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788}
2789
2790static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2791{
2792 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002793 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2795
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2797
2798 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002799 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800
2801 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2802
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002803 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804}
2805
2806static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2807{
2808 struct gsm48_hdr *gh = msgb_l3(msg);
2809 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2810 struct tlv_parsed tp;
2811 struct gsm_mncc modify;
2812
2813 gsm48_stop_cc_timer(trans);
2814
2815 memset(&modify, 0, sizeof(struct gsm_mncc));
2816 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002817 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 /* bearer capability */
2819 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2820 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002821 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002823 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002824 }
2825 /* cause */
2826 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2827 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002828 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2830 }
2831
2832 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2833
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002834 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835}
2836
2837static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2838{
2839 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002840 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2842
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2844
2845 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002846 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002848 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849
2850 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2851
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002852 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853}
2854
2855static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2856{
2857 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002858 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2860
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861 gh->msg_type = GSM48_MT_CC_NOTIFY;
2862
2863 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002864 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002866 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867}
2868
2869static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2870{
2871 struct gsm48_hdr *gh = msgb_l3(msg);
2872 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2873// struct tlv_parsed tp;
2874 struct gsm_mncc notify;
2875
2876 memset(&notify, 0, sizeof(struct gsm_mncc));
2877 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002878// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002880 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002882 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883}
2884
2885static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2886{
2887 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002888 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2890
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 gh->msg_type = GSM48_MT_CC_USER_INFO;
2892
2893 /* user-user */
2894 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002895 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896 /* more data */
2897 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002898 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002900 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901}
2902
2903static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2904{
2905 struct gsm48_hdr *gh = msgb_l3(msg);
2906 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2907 struct tlv_parsed tp;
2908 struct gsm_mncc user;
2909
2910 memset(&user, 0, sizeof(struct gsm_mncc));
2911 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002912 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 /* user-user */
2914 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2915 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002916 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2918 }
2919 /* more data */
2920 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2921 user.more = 1;
2922
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002923 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924}
2925
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002926static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927{
2928 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002929 struct gsm_lchan *lchan = trans->conn->lchan;
2930
2931 /*
2932 * We were forced to make an assignment a lot earlier and
2933 * we should avoid sending another assignment that might
2934 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
2935 * In case of rtp-bridge it is too late to change things
2936 * here.
2937 */
2938 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
2939 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002941 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2942 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943}
2944
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002945static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2946 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2947 uint32_t payload_msg_type)
2948{
2949 uint8_t data[sizeof(struct gsm_mncc)];
2950 struct gsm_mncc_rtp *rtp;
2951
2952 memset(&data, 0, sizeof(data));
2953 rtp = (struct gsm_mncc_rtp *) &data[0];
2954
2955 rtp->callref = callref;
2956 rtp->msg_type = cmd;
2957 rtp->ip = addr;
2958 rtp->port = port;
2959 rtp->payload_type = payload_type;
2960 rtp->payload_msg_type = payload_msg_type;
2961 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2962}
2963
2964static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2965{
2966 struct gsm_lchan *lchan;
2967 int msg_type;
2968
2969 lchan = trans->conn->lchan;
2970 switch (lchan->abis_ip.rtp_payload) {
2971 case RTP_PT_GSM_FULL:
2972 msg_type = GSM_TCHF_FRAME;
2973 break;
2974 case RTP_PT_GSM_EFR:
2975 msg_type = GSM_TCHF_FRAME_EFR;
2976 break;
2977 case RTP_PT_GSM_HALF:
2978 msg_type = GSM_TCHH_FRAME;
2979 break;
2980 case RTP_PT_AMR:
2981 msg_type = GSM_TCH_FRAME_AMR;
2982 break;
2983 default:
2984 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
2985 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
2986 msg_type = 0;
2987 break;
2988 }
2989
2990 return mncc_recv_rtp(net, trans->callref, cmd,
2991 lchan->abis_ip.bound_ip,
2992 lchan->abis_ip.bound_port,
2993 lchan->abis_ip.rtp_payload,
2994 msg_type);
2995}
2996
2997static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2998{
2999 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3000}
3001
3002static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3003{
3004 struct gsm_bts *bts;
3005 struct gsm_lchan *lchan;
3006 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003007 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003008
3009 /* Find callref */
3010 trans = trans_find_by_callref(net, callref);
3011 if (!trans) {
3012 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3013 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3014 return -EIO;
3015 }
3016 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3017 if (!trans->conn) {
3018 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3019 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3020 return 0;
3021 }
3022
3023 lchan = trans->conn->lchan;
3024 bts = lchan->ts->trx->bts;
3025 if (!is_ipaccess_bts(bts)) {
3026 /*
3027 * I want this to be straight forward and have no audio flow
3028 * through the nitb/osmo-mss system. This currently means that
3029 * this will not work with BS11/Nokia type BTS. We would need
3030 * to have a trau<->rtp bridge for these but still preferable
3031 * in another process.
3032 */
3033 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3034 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3035 return -EINVAL;
3036 }
3037
3038 trans->conn->mncc_rtp_bridge = 1;
3039 /*
3040 * *sigh* we need to pick a codec now. Pick the most generic one
3041 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003042 * similiar to the routine above.
3043 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003044 */
3045 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3046 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003047 m = mncc_codec_for_mode(lchan->type);
3048 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3049 get_value_string(gsm48_chan_mode_names, m),
3050 get_value_string(gsm_chan_t_names, lchan->type));
3051 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003052 lchan->type != GSM_LCHAN_TCH_H);
3053 }
3054
3055 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3056 return 0;
3057}
3058
3059static int tch_rtp_connect(struct gsm_network *net, void *arg)
3060{
3061 struct gsm_lchan *lchan;
3062 struct gsm_trans *trans;
3063 struct gsm_mncc_rtp *rtp = arg;
3064
3065 /* Find callref */
3066 trans = trans_find_by_callref(net, rtp->callref);
3067 if (!trans) {
3068 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3069 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3070 return -EIO;
3071 }
3072 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3073 if (!trans->conn) {
3074 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3075 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3076 return 0;
3077 }
3078
3079 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003080 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3081 get_value_string(gsm48_chan_mode_names,
3082 mncc_codec_for_mode(lchan->type)),
3083 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003084
3085 /* TODO: Check if payload_msg_type is compatible with what we have */
3086 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3087 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3088 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3089 }
3090
3091 /*
3092 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3093 * complains about both rtp and rtp payload2 being present in the
3094 * same package!
3095 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003096 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003097 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3098}
3099
3100static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3101{
3102 struct gsm_network *net;
3103 struct gsm_trans *tmp, *trans = NULL;
3104
3105 net = lchan->ts->trx->bts->network;
3106 llist_for_each_entry(tmp, &net->trans_list, entry) {
3107 if (!tmp->conn)
3108 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003109 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003110 continue;
3111 trans = tmp;
3112 break;
3113 }
3114
3115 if (!trans) {
3116 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3117 gsm_lchan_name(lchan));
3118 return 0;
3119 }
3120
3121 switch (signal) {
3122 case S_ABISIP_CRCX_ACK:
3123 if (lchan->conn->mncc_rtp_create_pending) {
3124 lchan->conn->mncc_rtp_create_pending = 0;
3125 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3126 gsm_lchan_name(lchan));
3127 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3128 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003129 /*
3130 * TODO: this appears to be too early? Why not until after
3131 * the handover detect or the handover complete?
3132 */
3133 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003134 break;
3135 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003136 if (lchan->conn->mncc_rtp_connect_pending) {
3137 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003138 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3139 gsm_lchan_name(lchan));
3140 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3141 }
3142 break;
3143 }
3144
3145 return 0;
3146}
3147
3148
Harald Welte4bfdfe72009-06-10 23:11:52 +08003149static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003150 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 int type;
3152 int (*rout) (struct gsm_trans *trans, void *arg);
3153} downstatelist[] = {
3154 /* mobile originating call establishment */
3155 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3156 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3157 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3158 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3159 {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 */
3160 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3161 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3162 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3163 /* mobile terminating call establishment */
3164 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3165 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3166 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3167 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3168 /* signalling during call */
3169 {SBIT(GSM_CSTATE_ACTIVE),
3170 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3171 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3172 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3173 {ALL_STATES,
3174 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3175 {ALL_STATES,
3176 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3177 {ALL_STATES,
3178 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3179 {SBIT(GSM_CSTATE_ACTIVE),
3180 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3181 {SBIT(GSM_CSTATE_ACTIVE),
3182 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3185 {SBIT(GSM_CSTATE_ACTIVE),
3186 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3187 {SBIT(GSM_CSTATE_ACTIVE),
3188 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3189 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3190 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3191 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3192 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3193 {SBIT(GSM_CSTATE_ACTIVE),
3194 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3195 /* clearing */
3196 {SBIT(GSM_CSTATE_INITIATED),
3197 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3198 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3199 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3200 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3201 MNCC_REL_REQ, gsm48_cc_tx_release},
3202 /* special */
3203 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003204 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003205};
3206
3207#define DOWNSLLEN \
3208 (sizeof(downstatelist) / sizeof(struct downstate))
3209
3210
Harald Welte76556372010-12-22 23:57:45 +01003211int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003212{
Harald Welte1a6f7982009-08-09 18:52:33 +02003213 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003215 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003216 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003217 struct gsm_mncc *data = arg, rel;
3218
Harald Welte04dc88f2010-12-22 21:45:05 +01003219 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3220
Harald Welte4bfdfe72009-06-10 23:11:52 +08003221 /* handle special messages */
3222 switch(msg_type) {
3223 case MNCC_BRIDGE:
3224 return tch_bridge(net, arg);
3225 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003226 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003228 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003229 case MNCC_RTP_CREATE:
3230 return tch_rtp_create(net, data->callref);
3231 case MNCC_RTP_CONNECT:
3232 return tch_rtp_connect(net, arg);
3233 case MNCC_RTP_FREE:
3234 /* unused right now */
3235 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003236 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003237 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003238 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003239 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003240 /* Find callref */
3241 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003242 if (!trans) {
3243 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003244 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003245 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003246 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003247 if (!trans->conn) {
3248 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003249 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003250 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003251 if (!trans->conn->lchan) {
3252 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3253 return 0;
3254 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003255 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3256 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003257 /* This should be LOGL_ERROR or NOTICE, but
3258 * unfortuantely it happens for a couple of frames at
3259 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003260 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003261 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003262 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003263 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003264 switch (bts->type) {
3265 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003266 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003267 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003268 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003269 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003270 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003271 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003272 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003273 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003274 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003275 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003276 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003277 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003278 }
3279 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003280 }
3281
3282 memset(&rel, 0, sizeof(struct gsm_mncc));
3283 rel.callref = data->callref;
3284
3285 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003286 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287
3288 /* Callref unknown */
3289 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003290 struct gsm_subscriber *subscr;
3291
Harald Welte4a3464c2009-07-04 10:11:24 +02003292 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003293 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3294 "Received '%s' from MNCC with "
3295 "unknown callref %d\n", data->called.number,
3296 get_mncc_name(msg_type), data->callref);
3297 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003298 return mncc_release_ind(net, NULL, data->callref,
3299 GSM48_CAUSE_LOC_PRN_S_LU,
3300 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003301 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003302 if (!data->called.number[0] && !data->imsi[0]) {
3303 DEBUGP(DCC, "(bts - trx - ts - ti) "
3304 "Received '%s' from MNCC with "
3305 "no number or IMSI\n", get_mncc_name(msg_type));
3306 /* Invalid number */
3307 return mncc_release_ind(net, NULL, data->callref,
3308 GSM48_CAUSE_LOC_PRN_S_LU,
3309 GSM48_CC_CAUSE_INV_NR_FORMAT);
3310 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003311 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003312 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003313 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003314 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003315 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003316 subscr = subscr_get_by_imsi(net->subscr_group,
3317 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003318
3319 /* update the subscriber we deal with */
3320 log_set_context(BSC_CTX_SUBSCR, subscr);
3321
Harald Welte4bfdfe72009-06-10 23:11:52 +08003322 /* If subscriber is not found */
3323 if (!subscr) {
3324 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3325 "Received '%s' from MNCC with "
3326 "unknown subscriber %s\n", data->called.number,
3327 get_mncc_name(msg_type), data->called.number);
3328 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003329 return mncc_release_ind(net, NULL, data->callref,
3330 GSM48_CAUSE_LOC_PRN_S_LU,
3331 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003332 }
3333 /* If subscriber is not "attached" */
3334 if (!subscr->lac) {
3335 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3336 "Received '%s' from MNCC with "
3337 "detached subscriber %s\n", data->called.number,
3338 get_mncc_name(msg_type), data->called.number);
3339 subscr_put(subscr);
3340 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003341 return mncc_release_ind(net, NULL, data->callref,
3342 GSM48_CAUSE_LOC_PRN_S_LU,
3343 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003344 }
3345 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003346 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003347 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003348 DEBUGP(DCC, "No memory for trans.\n");
3349 subscr_put(subscr);
3350 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003351 mncc_release_ind(net, NULL, data->callref,
3352 GSM48_CAUSE_LOC_PRN_S_LU,
3353 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003354 return -ENOMEM;
3355 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003356 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003357 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003358
Harald Welte4bfdfe72009-06-10 23:11:52 +08003359 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003360 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003361 /* find transaction with this subscriber already paging */
3362 llist_for_each_entry(transt, &net->trans_list, entry) {
3363 /* Transaction of our lchan? */
3364 if (transt == trans ||
3365 transt->subscr != subscr)
3366 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003367 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003368 "Received '%s' from MNCC with "
3369 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003370 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003371 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003372 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003373 subscr_put(subscr);
3374 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003375 return 0;
3376 }
3377 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003378 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003379
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003380 /* Request a channel */
3381 trans->paging_request = subscr_request_channel(subscr,
3382 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3383 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003384 if (!trans->paging_request) {
3385 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3386 subscr_put(subscr);
3387 trans_free(trans);
3388 return 0;
3389 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003390 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003391 return 0;
3392 }
3393 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003394 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003395 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003396 } else {
3397 /* update the subscriber we deal with */
3398 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003399 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003400
3401 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003402 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003403
3404 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003405 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003406 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003407 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003408 (trans->subscr)?(trans->subscr->extension):"-",
3409 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003410 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3411 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003412 if (msg_type == MNCC_REL_REQ)
3413 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3414 else
3415 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3416 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003417 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003418 return rc;
3419 }
3420
3421 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3422 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003423 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003424 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003425 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003426 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003427 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003428
3429 /* Find function for current state and message */
3430 for (i = 0; i < DOWNSLLEN; i++)
3431 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003432 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003433 break;
3434 if (i == DOWNSLLEN) {
3435 DEBUGP(DCC, "Message unhandled at this state.\n");
3436 return 0;
3437 }
3438
3439 rc = downstatelist[i].rout(trans, arg);
3440
3441 return rc;
3442}
3443
3444
3445static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003446 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003447 int type;
3448 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3449} datastatelist[] = {
3450 /* mobile originating call establishment */
3451 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3452 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3453 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3454 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3455 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3456 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3457 /* mobile terminating call establishment */
3458 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3459 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3460 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3461 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003462 {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 +08003463 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3464 /* signalling during call */
3465 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3466 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3467 {SBIT(GSM_CSTATE_ACTIVE),
3468 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3469 {ALL_STATES,
3470 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3471 {ALL_STATES,
3472 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3473 {ALL_STATES,
3474 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3475 {SBIT(GSM_CSTATE_ACTIVE),
3476 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3477 {SBIT(GSM_CSTATE_ACTIVE),
3478 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3479 {SBIT(GSM_CSTATE_ACTIVE),
3480 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3481 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3482 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3483 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3484 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3485 {SBIT(GSM_CSTATE_ACTIVE),
3486 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3487 /* clearing */
3488 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3489 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3490 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3491 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3492 {ALL_STATES, /* 5.4.3.4 */
3493 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3494};
3495
3496#define DATASLLEN \
3497 (sizeof(datastatelist) / sizeof(struct datastate))
3498
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003499static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003500{
3501 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003502 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003503 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003504 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003505 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003506
Harald Welte4bfdfe72009-06-10 23:11:52 +08003507 if (msg_type & 0x80) {
3508 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3509 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003510 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003511
Harald Welte4bfdfe72009-06-10 23:11:52 +08003512 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003513 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003514
Harald Welte6f5aee02009-07-23 21:21:14 +02003515 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003516 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003517 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003518 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003519 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3520 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003521
3522 /* Create transaction */
3523 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003524 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003525 "creating new trans.\n", transaction_id);
3526 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003527 trans = trans_alloc(conn->bts->network, conn->subscr,
3528 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003529 transaction_id, new_callref++);
3530 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003531 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003532 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003533 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003534 GSM48_MT_CC_RELEASE_COMPL);
3535 return -ENOMEM;
3536 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003537 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003538 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003539 }
3540
3541 /* find function for current state and message */
3542 for (i = 0; i < DATASLLEN; i++)
3543 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003544 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003545 break;
3546 if (i == DATASLLEN) {
3547 DEBUGP(DCC, "Message unhandled at this state.\n");
3548 return 0;
3549 }
3550
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003551 assert(trans->subscr);
3552
Harald Welte4bfdfe72009-06-10 23:11:52 +08003553 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003554
3555 return rc;
3556}
3557
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003558/* Create a dummy to wait five seconds */
3559static void release_anchor(struct gsm_subscriber_connection *conn)
3560{
3561 if (!conn->anch_operation)
3562 return;
3563
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003564 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003565 talloc_free(conn->anch_operation);
3566 conn->anch_operation = NULL;
3567}
3568
3569static void anchor_timeout(void *_data)
3570{
3571 struct gsm_subscriber_connection *con = _data;
3572
3573 release_anchor(con);
3574 msc_release_connection(con);
3575}
3576
3577int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3578{
3579 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3580 if (!conn->anch_operation)
3581 return -1;
3582
3583 conn->anch_operation->timeout.data = conn;
3584 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003585 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003586 return 0;
3587}
3588
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003589/* here we get data from the BSC level... */
3590int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003591{
3592 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003593 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003594 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003595
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003596 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003597 if (silent_call_reroute(conn, msg))
3598 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003599
3600 switch (pdisc) {
3601 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003602 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003603 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003604 break;
3605 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003606 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003607 break;
3608 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003609 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003610 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003611 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003612 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003613 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003614 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003615 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003616 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003617 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3618 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003619 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003620 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003621 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003622 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003623 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003624 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003625 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3626 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003627 break;
3628 }
3629
3630 return rc;
3631}
Harald Welte8470bf22008-12-25 23:28:35 +00003632
Harald Welte805f6442009-07-28 18:25:29 +02003633/*
3634 * This will be ran by the linker when loading the DSO. We use it to
3635 * do system initialization, e.g. registration of signal handlers.
3636 */
3637static __attribute__((constructor)) void on_dso_load_0408(void)
3638{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003639 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003640}