blob: 68cc906fee72b692ba7904efc55b7494ca11f07e [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 */
Harald Welte121e9a42016-04-20 13:13:19 +0200237 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.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,
Harald Welte121e9a42016-04-20 13:13:19 +0200241 op->atuple.vec.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 "
Harald Welte7184bd02015-12-28 14:04:36 +0100450 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800451 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100452
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800453 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000454}
455
456/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200457static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000458{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800459 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100460 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000461 struct gsm48_hdr *gh;
462 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200463 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000464
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800465 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000466
467 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
468 gh->proto_discr = GSM48_PDISC_MM;
469 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
470
471 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100472 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000473 bts->network->network_code, bts->location_area_code);
474
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200475 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
476 uint8_t mi[10];
477 int len;
478 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
479 mid = msgb_put(msg, len);
480 memcpy(mid, mi, len);
481 } else {
482 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
483 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
484 }
Harald Welte52b1f982008-12-23 20:25:15 +0000485
486 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
487
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200488 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100489
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800490 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000491}
492
Harald Weltebf5e8df2009-02-03 12:59:45 +0000493/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200494static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000495{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100496 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000497 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000498
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800499 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000500
501 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
502 gh->proto_discr = GSM48_PDISC_MM;
503 gh->msg_type = GSM48_MT_MM_ID_REQ;
504 gh->data[0] = id_type;
505
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800506 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000507}
508
Maxe21cf382016-05-11 11:01:06 +0200509static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
510 const char *imsi)
511{
512 if (net->subscr_creation_mode != GSM_SUBSCR_DONT_CREATE)
513 return subscr_create_subscriber(net->subscr_group, imsi);
514
515 return NULL;
516}
Harald Welte231ad4f2008-12-27 11:15:38 +0000517
Harald Weltebf5e8df2009-02-03 12:59:45 +0000518/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800519static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000520{
521 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000522 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200523 struct gsm_bts *bts = lchan->ts->trx->bts;
524 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200525 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200526 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000527
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200528 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200529 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
530 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000531
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200532 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100533
Harald Welte75a983f2008-12-27 21:34:06 +0000534 switch (mi_type) {
535 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200536 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100537 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100538 conn->subscr = subscr_get_by_imsi(net->subscr_group,
539 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200540 if (!conn->subscr)
541 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200542 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100543 if (!conn->subscr && conn->loc_operation) {
544 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
545 release_loc_updating_req(conn, 1);
546 return 0;
547 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100548 if (conn->loc_operation)
549 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000550 break;
551 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000552 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000553 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100554 if (conn->subscr) {
555 db_subscriber_assoc_imei(conn->subscr, mi_string);
556 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200557 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100558 if (conn->loc_operation)
559 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000560 break;
561 }
Holger Freyther73487a22008-12-31 18:53:57 +0000562
563 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100564 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000565}
566
Harald Welte255539c2008-12-28 02:26:27 +0000567
568static void loc_upd_rej_cb(void *data)
569{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100570 struct gsm_subscriber_connection *conn = data;
571 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100572 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000573
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100574 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800575 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100576 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000577}
578
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100579static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000580{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100581 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
582 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200583 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000584}
585
Harald Welteb9845f92015-08-16 18:07:48 +0200586static const struct value_string lupd_names[] = {
587 { GSM48_LUPD_NORMAL, "NORMAL" },
588 { GSM48_LUPD_PERIODIC, "PERIODIC" },
589 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
590 { 0, NULL }
591};
Harald Welte2a139372009-02-22 21:14:55 +0000592
Harald Weltebf5e8df2009-02-03 12:59:45 +0000593/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800594static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000595{
Harald Welte8470bf22008-12-25 23:28:35 +0000596 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000597 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800598 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800599 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200600 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200601 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000602
Harald Welte8470bf22008-12-25 23:28:35 +0000603 lu = (struct gsm48_loc_upd_req *) gh->data;
604
605 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000606
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200607 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000608
Harald Welteb9845f92015-08-16 18:07:48 +0200609 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
610 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000611
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200612 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100613
Harald Welte24ff6ee2009-12-22 00:41:05 +0100614 switch (lu->type) {
615 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200616 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100617 break;
618 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200619 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100620 break;
621 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200622 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100623 break;
624 }
625
Holger Freythereaf04692009-06-06 13:54:44 +0000626 /*
627 * Pseudo Spoof detection: Just drop a second/concurrent
628 * location updating request.
629 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100630 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200631 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100632 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800633 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000634 return 0;
635 }
636
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800637 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000638
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200639 conn->loc_operation->key_seq = lu->key_seq;
640
Harald Welte52b1f982008-12-23 20:25:15 +0000641 switch (mi_type) {
642 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200643 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000644 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200645 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100646 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000647
Jan Luebbe370b41d2009-08-12 10:19:34 +0200648 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100649 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200650 if (!subscr)
651 subscr = subscr_create(bts->network, mi_string);
652
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100653 if (!subscr) {
654 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
655 release_loc_updating_req(conn, 0);
656 return 0;
657 }
Harald Welte4b634542008-12-27 01:55:51 +0000658 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000659 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200660 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000661 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100662 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200663 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000664 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000665 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200666 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100667 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000668 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200669 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200670 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200671 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000672 break;
673 case GSM_MI_TYPE_IMEI:
674 case GSM_MI_TYPE_IMEISV:
675 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200676 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000677 break;
678 default:
Harald Weltea0368542009-06-27 02:58:43 +0200679 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000680 break;
681 }
682
Harald Welte24516ea2009-07-04 10:18:00 +0200683 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100684 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200685
Harald Welte4bfdfe72009-06-10 23:11:52 +0800686 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200687 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800688 /* FIXME: request id? close channel? */
689 return -EINVAL;
690 }
691
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100692 conn->subscr = subscr;
693 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000694
Harald Welte24516ea2009-07-04 10:18:00 +0200695 /* check if we can let the subscriber into our network immediately
696 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100697 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000698}
699
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100700/* Turn int into semi-octet representation: 98 => 0x89 */
701static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800702{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100703 uint8_t ret;
704
705 ret = value / 10;
706 ret |= (value % 10) << 4;
707
708 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800709}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100710
Harald Welte4bfdfe72009-06-10 23:11:52 +0800711
Harald Weltedb253af2008-12-30 17:56:55 +0000712/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800713int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000714{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100715 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000716 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800717 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100718 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200719 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200720 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100721
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100723 struct tm* gmt_time;
724 struct tm* local_time;
725 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100726 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000727
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800728 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000729
730 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
731 gh->proto_discr = GSM48_PDISC_MM;
732 gh->msg_type = GSM48_MT_MM_INFO;
733
734 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200735#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000736 name_len = strlen(net->name_long);
737 /* 10.5.3.5a */
738 ptr8 = msgb_put(msg, 3);
739 ptr8[0] = GSM48_IE_NAME_LONG;
740 ptr8[1] = name_len*2 +1;
741 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
742
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200743 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000744 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000745 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000746
747 /* FIXME: Use Cell Broadcast, not UCS-2, since
748 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200749#endif
750 name_len = (strlen(net->name_long)*7)/8;
751 name_pad = (8 - strlen(net->name_long)*7)%8;
752 if (name_pad > 0)
753 name_len++;
754 /* 10.5.3.5a */
755 ptr8 = msgb_put(msg, 3);
756 ptr8[0] = GSM48_IE_NAME_LONG;
757 ptr8[1] = name_len +1;
758 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
759
760 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100761 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200762
Harald Weltedb253af2008-12-30 17:56:55 +0000763 }
764
765 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200766#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000767 name_len = strlen(net->name_short);
768 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200770 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000771 ptr8[1] = name_len*2 + 1;
772 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
773
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200774 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000775 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000776 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200777#endif
778 name_len = (strlen(net->name_short)*7)/8;
779 name_pad = (8 - strlen(net->name_short)*7)%8;
780 if (name_pad > 0)
781 name_len++;
782 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200783 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200784 ptr8[0] = GSM48_IE_NAME_SHORT;
785 ptr8[1] = name_len +1;
786 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
787
788 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100789 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200790
Harald Weltedb253af2008-12-30 17:56:55 +0000791 }
792
Harald Weltedb253af2008-12-30 17:56:55 +0000793 /* Section 10.5.3.9 */
794 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100795 gmt_time = gmtime(&cur_t);
796
Harald Weltedb253af2008-12-30 17:56:55 +0000797 ptr8 = msgb_put(msg, 8);
798 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100799 ptr8[1] = bcdify(gmt_time->tm_year % 100);
800 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
801 ptr8[3] = bcdify(gmt_time->tm_mday);
802 ptr8[4] = bcdify(gmt_time->tm_hour);
803 ptr8[5] = bcdify(gmt_time->tm_min);
804 ptr8[6] = bcdify(gmt_time->tm_sec);
805
Harald Welte45f91712012-07-08 16:48:11 +0200806 if (bts->tz.override) {
807 /* Convert tz.hr and tz.mn to units */
808 if (bts->tz.hr < 0) {
809 tzunits = ((bts->tz.hr/-1)*4);
810 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100811 ptr8[7] = bcdify(tzunits);
812 /* Set negative time */
813 ptr8[7] |= 0x08;
814 }
815 else {
Harald Welte45f91712012-07-08 16:48:11 +0200816 tzunits = bts->tz.hr*4;
817 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100818 ptr8[7] = bcdify(tzunits);
819 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100820 /* Convert DST value */
821 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
822 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100823 }
824 else {
825 /* Need to get GSM offset and convert into 15 min units */
826 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
827 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200828#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100829 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200830#else
831#warning find a portable way to obtain the timezone offset
832 tzunits = 0;
833#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100834 if (tzunits < 0) {
835 tzunits = tzunits/-1;
836 ptr8[7] = bcdify(tzunits);
837 /* Flip it to negative */
838 ptr8[7] |= 0x08;
839 }
840 else
841 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100842
843 /* Does not support DST +2 */
844 if (local_time->tm_isdst)
845 dst = 1;
846 }
847
848 if (dst) {
849 ptr8 = msgb_put(msg, 3);
850 ptr8[0] = GSM48_IE_NET_DST;
851 ptr8[1] = 1;
852 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100853 }
Harald Weltedb253af2008-12-30 17:56:55 +0000854
Daniel Willmanneea93372009-08-13 03:42:07 +0200855 DEBUGP(DMM, "-> MM INFO\n");
856
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800857 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000858}
859
Harald Welte7984d5c2009-08-12 22:56:50 +0200860/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200861int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200862{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100863 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200864 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200865 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200866
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200867 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200868
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800869 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200870 gh->proto_discr = GSM48_PDISC_MM;
871 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
872
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100873 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200874
Harald Welte7984d5c2009-08-12 22:56:50 +0200875 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200876 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200877 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200878
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800879 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200880}
881
882/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800883int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200884{
885 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800886 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200887}
888
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100889/*
890 * At the 30C3 phones miss their periodic update
891 * interval a lot and then remain unreachable. In case
892 * we still know the TMSI we can just attach it again.
893 */
894static void implit_attach(struct gsm_subscriber_connection *conn)
895{
896 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
897 return;
898
899 subscr_update(conn->subscr, conn->bts,
900 GSM_SUBSCRIBER_UPDATE_ATTACHED);
901}
902
903
Sylvain Munautba87f452009-12-24 00:28:46 +0100904static int _gsm48_rx_mm_serv_req_sec_cb(
905 unsigned int hooknum, unsigned int event,
906 struct msgb *msg, void *data, void *param)
907{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800908 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100909 int rc = 0;
910
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200911 /* auth failed or succeeded, the timer was stopped */
912 conn->expire_timer_stopped = 1;
913
Sylvain Munautba87f452009-12-24 00:28:46 +0100914 switch (event) {
915 case GSM_SECURITY_AUTH_FAILED:
916 /* Nothing to do */
917 break;
918
919 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200920 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800921 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100922 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100923 break;
924
925 case GSM_SECURITY_SUCCEEDED:
926 /* nothing to do. CIPHER MODE COMMAND is
927 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100928 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100929 break;
930
931 default:
932 rc = -EINVAL;
933 };
934
935 return rc;
936}
937
Harald Welte4ed0e922009-01-10 03:17:30 +0000938/*
939 * Handle CM Service Requests
940 * a) Verify that the packet is long enough to contain the information
941 * we require otherwsie reject with INCORRECT_MESSAGE
942 * b) Try to parse the TMSI. If we do not have one reject
943 * c) Check that we know the subscriber with the TMSI otherwise reject
944 * with a HLR cause
945 * d) Set the subscriber on the gsm_lchan and accept
946 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800947static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000948{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200949 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200950 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000951
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800952 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000953 struct gsm_subscriber *subscr;
954 struct gsm48_hdr *gh = msgb_l3(msg);
955 struct gsm48_service_request *req =
956 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800957 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200958 uint8_t classmark2_len = gh->data[1];
959 uint8_t *classmark2 = gh->data+2;
960 uint8_t mi_len = *(classmark2 + classmark2_len);
961 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000962
Harald Weltec9e02182009-05-01 19:07:53 +0000963 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000964 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000965 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800966 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000967 GSM48_REJECT_INCORRECT_MESSAGE);
968 }
969
970 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000971 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800972 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000973 GSM48_REJECT_INCORRECT_MESSAGE);
974 }
975
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200976 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000977 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200978
979 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200980 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
981 req->cm_service_type, gsm48_mi_type_name(mi_type),
982 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200983 subscr = subscr_get_by_imsi(bts->network->subscr_group,
984 mi_string);
985 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200986 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
987 req->cm_service_type, gsm48_mi_type_name(mi_type),
988 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200989 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
990 tmsi_from_string(mi_string));
991 } else {
992 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800993 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000994 GSM48_REJECT_INCORRECT_MESSAGE);
995 }
996
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200997 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100998
Harald Welte3ac7f102009-08-10 10:12:45 +0200999 if (is_siemens_bts(bts))
1000 send_siemens_mrpci(msg->lchan, classmark2-1);
1001
Holger Freythereb443982009-06-04 13:58:42 +00001002
Harald Welte2a139372009-02-22 21:14:55 +00001003 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001004 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001005 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001006 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001007
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001008 if (!conn->subscr)
1009 conn->subscr = subscr;
1010 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001011 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1012 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001013 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1014 subscr_put(subscr);
1015 }
1016
Harald Weltec2e302d2009-07-05 14:08:13 +02001017 subscr->equipment.classmark2_len = classmark2_len;
1018 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1019 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001020
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001021 /* we will send a MM message soon */
1022 conn->expire_timer_stopped = 1;
1023
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001024 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001025 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001026}
1027
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001028static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001029{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001030 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001031 struct gsm48_hdr *gh = msgb_l3(msg);
1032 struct gsm48_imsi_detach_ind *idi =
1033 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001034 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001035 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001036 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001037
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001038 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001039 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1040 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001041
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001042 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001043
Harald Welte2a139372009-02-22 21:14:55 +00001044 switch (mi_type) {
1045 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001046 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001047 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001048 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001049 break;
1050 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001051 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001052 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1053 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001054 break;
1055 case GSM_MI_TYPE_IMEI:
1056 case GSM_MI_TYPE_IMEISV:
1057 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001058 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001059 break;
1060 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001061 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001062 break;
1063 }
1064
Holger Freyther4a49e772009-04-12 05:37:29 +00001065 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001066 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001067 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001068 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001069
1070 subscr->equipment.classmark1 = idi->classmark1;
1071 db_sync_equipment(&subscr->equipment);
1072
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001073 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001074 } else
Harald Welte2a139372009-02-22 21:14:55 +00001075 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1076
Harald Welte31981a02009-12-20 09:58:40 +01001077 /* FIXME: iterate over all transactions and release them,
1078 * imagine an IMSI DETACH happening during an active call! */
1079
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001080 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001081 return 0;
1082}
1083
Harald Welted2a7f5a2009-06-05 20:08:20 +00001084static int gsm48_rx_mm_status(struct msgb *msg)
1085{
1086 struct gsm48_hdr *gh = msgb_l3(msg);
1087
1088 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1089
1090 return 0;
1091}
1092
Sylvain Munaut30a15382009-12-24 00:27:26 +01001093/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001094static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001095{
1096 struct gsm48_hdr *gh = msgb_l3(msg);
1097 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001098 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001099
Sylvain Munautc593cf12010-06-10 23:51:41 +02001100 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001101 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001102
1103 /* Safety check */
1104 if (!conn->sec_operation) {
1105 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1106 return -EIO;
1107 }
1108
1109 /* Validate SRES */
Harald Welte121e9a42016-04-20 13:13:19 +02001110 if (memcmp(conn->sec_operation->atuple.vec.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001111 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001112 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001113
1114 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001115 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001116
Sylvain Munaut30a15382009-12-24 00:27:26 +01001117 if (cb)
1118 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001119 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001120
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001121 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001122 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001123 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001124 }
1125
Sylvain Munautc593cf12010-06-10 23:51:41 +02001126 DEBUGPC(DMM, "OK\n");
1127
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001129 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001130 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001131}
1132
Harald Weltebf5e8df2009-02-03 12:59:45 +00001133/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001134static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001135{
1136 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001137 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001138
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001139 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001140 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001141 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001142 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001143 break;
1144 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001146 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001147 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001148 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001149 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001150 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001151 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001152 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001153 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001154 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001155 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001156 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001157 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001158 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001159 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001160 break;
1161 case GSM48_MT_MM_CM_REEST_REQ:
1162 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1163 break;
1164 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001165 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001166 break;
1167 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001168 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001169 gh->msg_type);
1170 break;
1171 }
1172
1173 return rc;
1174}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001175
Harald Welte2d35ae62009-02-06 12:02:13 +00001176/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001177static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001178{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001179 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001180 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001181 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001182 uint8_t *classmark2_lv = gh->data + 1;
1183 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001184 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001185 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001186 int rc = 0;
1187
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001188 resp = (struct gsm48_pag_resp *) &gh->data[0];
1189 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1190 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001191 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1192 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001193
Harald Weltefe18d8f2009-02-22 21:14:24 +00001194 switch (mi_type) {
1195 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001196 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001197 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001198 break;
1199 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001200 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1201 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001202 break;
1203 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001204
1205 if (!subscr) {
1206 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001207 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001208 return -EINVAL;
1209 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001210 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001211 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001212 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001213
Harald Weltec2e302d2009-07-05 14:08:13 +02001214 subscr->equipment.classmark2_len = *classmark2_lv;
1215 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1216 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001217
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001218 /* We received a paging */
1219 conn->expire_timer_stopped = 1;
1220
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001221 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001222 return rc;
1223}
1224
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001225static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226{
1227 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001228 uint8_t apdu_id_flags;
1229 uint8_t apdu_len;
1230 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001231
1232 apdu_id_flags = gh->data[0];
1233 apdu_len = gh->data[1];
1234 apdu_data = gh->data+2;
1235
Harald Welte70f92052012-07-16 13:22:19 +02001236 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001237 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001238
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001239 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001240}
1241
Harald Weltebf5e8df2009-02-03 12:59:45 +00001242/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001243static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001244{
1245 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001246 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001247
1248 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001249 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001250 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001251 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001252 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001253 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001255 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001256 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001257 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001258 break;
1259 }
1260
Harald Welte2d35ae62009-02-06 12:02:13 +00001261 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001262}
1263
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001264int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1265 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001267 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001268 struct gsm48_hdr *gh;
1269
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001270 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001271
1272 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1273 apdu_id, apdu_len);
1274
1275 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1276 gh->proto_discr = GSM48_PDISC_RR;
1277 gh->msg_type = GSM48_MT_RR_APP_INFO;
1278 gh->data[0] = apdu_id;
1279 gh->data[1] = apdu_len;
1280 memcpy(gh->data+2, apdu, apdu_len);
1281
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001282 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001283}
1284
Harald Welte4bc90a12008-12-27 16:32:52 +00001285/* Call Control */
1286
Harald Welte7584aea2009-02-11 11:44:12 +00001287/* The entire call control code is written in accordance with Figure 7.10c
1288 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1289 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1290 * it for voice */
1291
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292static void new_cc_state(struct gsm_trans *trans, int state)
1293{
1294 if (state > 31 || state < 0)
1295 return;
1296
1297 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001298 gsm48_cc_state_name(trans->cc.state),
1299 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300
Harald Weltedcaf5652009-07-23 18:56:43 +02001301 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302}
1303
1304static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001305{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001306 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001308 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001309
Harald Welte4bc90a12008-12-27 16:32:52 +00001310 gh->msg_type = GSM48_MT_CC_STATUS;
1311
1312 cause = msgb_put(msg, 3);
1313 cause[0] = 2;
1314 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1315 cause[2] = 0x80 | 30; /* response to status inquiry */
1316
1317 call_state = msgb_put(msg, 1);
1318 call_state[0] = 0xc0 | 0x00;
1319
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001320 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001321}
1322
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001323static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001324 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001325{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001326 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001327 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1328
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001329 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001330
Harald Welte6f4b7532008-12-29 00:39:37 +00001331 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001332 gh->msg_type = msg_type;
1333
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001334 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001335}
1336
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1338{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001339 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001340 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001341 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001342 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343 }
1344}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001345
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1347 int msg_type, struct gsm_mncc *mncc)
1348{
1349 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001350 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351
1352 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001353 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001354 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001356 trans->conn->lchan->ts->trx->bts->nr,
1357 trans->conn->lchan->ts->trx->nr,
1358 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 (trans->subscr)?(trans->subscr->extension):"-",
1360 get_mncc_name(msg_type));
1361 else
1362 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1363 "Sending '%s' to MNCC.\n",
1364 (trans->subscr)?(trans->subscr->extension):"-",
1365 get_mncc_name(msg_type));
1366 else
1367 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1368 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1369
1370 mncc->msg_type = msg_type;
1371
Harald Welte966636f2009-06-26 19:39:35 +02001372 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001373 if (!msg)
1374 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001375
1376 data = msgb_put(msg, sizeof(struct gsm_mncc));
1377 memcpy(data, mncc, sizeof(struct gsm_mncc));
1378
Harald Welte54209c22010-12-22 23:43:29 +01001379 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380
1381 return 0;
1382}
1383
1384int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001385 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386{
1387 struct gsm_mncc rel;
1388
Harald Welte92f70c52009-06-12 01:54:08 +08001389 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001391 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001392 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1393 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1395}
1396
Harald Weltedcaf5652009-07-23 18:56:43 +02001397/* Call Control Specific transaction release.
1398 * gets called by trans_free, DO NOT CALL YOURSELF! */
1399void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401 gsm48_stop_cc_timer(trans);
1402
1403 /* send release to L4, if callref still exists */
1404 if (trans->callref) {
1405 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001406 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001407 GSM48_CAUSE_LOC_PRN_S_LU,
1408 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001409 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001410 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001412 if (trans->conn)
1413 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414}
1415
1416static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001417
Harald Welte09e38af2009-02-16 22:52:23 +00001418/* call-back from paging the B-end of the connection */
1419static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001420 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001421{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001422 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001423 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001424
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001425 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001426
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001428 switch (event) {
1429 case GSM_PAGING_SUCCEEDED:
1430 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001431 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001432 /* Assign lchan */
1433 transt->conn = conn;
1434 /* send SETUP request to called party */
1435 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1436 break;
1437 case GSM_PAGING_EXPIRED:
1438 case GSM_PAGING_BUSY:
1439 DEBUGP(DCC, "Paging subscr %s expired!\n",
1440 transt->subscr->extension);
1441 /* Temporarily out of order */
1442 mncc_release_ind(transt->net, transt,
1443 transt->callref,
1444 GSM48_CAUSE_LOC_PRN_S_LU,
1445 GSM48_CC_CAUSE_DEST_OOO);
1446 transt->callref = 0;
1447 transt->paging_request = NULL;
1448 trans_free(transt);
1449 break;
1450 default:
1451 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1452 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001454
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001455 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001456 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001457}
Harald Welte7584aea2009-02-11 11:44:12 +00001458
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001459static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001460
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001461/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001462static int switch_for_handover(struct gsm_lchan *old_lchan,
1463 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001464{
1465 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001466
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001467 /* Ask the new socket to send to the already known port. */
1468 if (new_lchan->conn->mncc_rtp_bridge) {
1469 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1470 rsl_ipacc_mdcx(new_lchan,
1471 old_lchan->abis_ip.connect_ip,
1472 old_lchan->abis_ip.connect_port, 0);
1473 return 0;
1474 }
1475
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001476 if (ipacc_rtp_direct) {
1477 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1478 return 0;
1479 }
1480
1481 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001482 new_rs = new_lchan->abis_ip.rtp_socket;
1483 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001484
1485 if (!new_rs) {
1486 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1487 return -EIO;
1488 }
1489
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001490 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001491
1492 if (!old_rs) {
1493 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1494 return -EIO;
1495 }
1496
1497 /* copy rx_action and reference to other sock */
1498 new_rs->rx_action = old_rs->rx_action;
1499 new_rs->tx_action = old_rs->tx_action;
1500 new_rs->transmit = old_rs->transmit;
1501
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001502 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001503 case RTP_PROXY:
1504 other_rs = old_rs->proxy.other_sock;
1505 rtp_socket_proxy(new_rs, other_rs);
1506 /* delete reference to other end socket to prevent
1507 * rtp_socket_free() from removing the inverse reference */
1508 old_rs->proxy.other_sock = NULL;
1509 break;
1510 case RTP_RECV_UPSTREAM:
1511 new_rs->receive = old_rs->receive;
1512 break;
1513 case RTP_NONE:
1514 break;
1515 }
1516
1517 return 0;
1518}
1519
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001520static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1521{
1522 struct gsm_lchan *old_lchan;
1523 old_lchan = bsc_handover_pending(lchan);
1524 if (old_lchan)
1525 switch_for_handover(old_lchan, lchan);
1526}
1527
Harald Welte805f6442009-07-28 18:25:29 +02001528/* some other part of the code sends us a signal */
1529static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1530 void *handler_data, void *signal_data)
1531{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001532 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001533 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001534 struct gsm_network *net;
1535 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001536
1537 if (subsys != SS_ABISIP)
1538 return 0;
1539
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001540 /* RTP bridge handling */
1541 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1542 return tch_rtp_signal(lchan, signal);
1543
Harald Welte805f6442009-07-28 18:25:29 +02001544 /* in case we use direct BTS-to-BTS RTP */
1545 if (ipacc_rtp_direct)
1546 return 0;
1547
Harald Welte805f6442009-07-28 18:25:29 +02001548 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001549 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001550 /* in case we don't use direct BTS-to-BTS RTP */
1551 /* the BTS has successfully bound a TCH to a local ip/port,
1552 * which means we can connect our UDP socket to it */
1553 if (lchan->abis_ip.rtp_socket) {
1554 rtp_socket_free(lchan->abis_ip.rtp_socket);
1555 lchan->abis_ip.rtp_socket = NULL;
1556 }
1557
1558 lchan->abis_ip.rtp_socket = rtp_socket_create();
1559 if (!lchan->abis_ip.rtp_socket)
1560 return -EIO;
1561
1562 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1563 lchan->abis_ip.bound_ip,
1564 lchan->abis_ip.bound_port);
1565 if (rc < 0)
1566 return -EIO;
1567
Harald Welteda7ab742009-12-19 22:23:05 +01001568 /* check if any transactions on this lchan still have
1569 * a tch_recv_mncc request pending */
1570 net = lchan->ts->trx->bts->network;
1571 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001572 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001573 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1574 tch_recv_mncc(net, trans->callref, 1);
1575 }
1576 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001577
1578 /*
1579 * TODO: this appears to be too early? Why not until after
1580 * the handover detect or the handover complete?
1581 *
1582 * Do we have a handover pending for this new lchan? In that
1583 * case re-route the audio from the old channel to the new one.
1584 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001585 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001586 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001587 case S_ABISIP_DLCX_IND:
1588 /* the BTS tells us a RTP stream has been disconnected */
1589 if (lchan->abis_ip.rtp_socket) {
1590 rtp_socket_free(lchan->abis_ip.rtp_socket);
1591 lchan->abis_ip.rtp_socket = NULL;
1592 }
1593
1594 break;
Harald Welte805f6442009-07-28 18:25:29 +02001595 }
1596
1597 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001598}
1599
Harald Welte49f48b82009-02-17 15:29:33 +00001600/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001601static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001602{
Harald Welte11fa29c2009-02-19 17:24:39 +00001603 struct gsm_bts *bts = lchan->ts->trx->bts;
1604 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001605 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001606 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001607 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001608
Max8db12e42016-04-18 23:11:18 +02001609 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1610 "(bts=%u,trx=%u,ts=%u,%s)\n",
1611 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1612 get_value_string(gsm_chan_t_names, lt),
1613 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1614 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001615
1616 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001617 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001618 return -EINVAL;
1619 }
Harald Welteda7ab742009-12-19 22:23:05 +01001620
Max3ffce192016-04-25 15:22:00 +02001621 if (lt != rt) {
1622 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1623 " channel types: local = %s, remote = %s\n",
1624 get_value_string(gsm_chan_t_names, lt),
1625 get_value_string(gsm_chan_t_names, rt));
1626 return -EBADSLT;
1627 }
1628
1629 if (lm != rm) {
1630 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1631 " channel modes: local = %s, remote = %s\n",
1632 get_value_string(gsm48_chan_mode_names, lm),
1633 get_value_string(gsm48_chan_mode_names, rm));
1634 return -EMEDIUMTYPE;
1635 }
1636
Harald Welteda7ab742009-12-19 22:23:05 +01001637 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001638 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001639 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001640 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001641 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001642 if (!lchan->abis_ip.rtp_socket) {
1643 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1644 "lchan\n");
1645 return -EIO;
1646 }
1647 if (!remote_lchan->abis_ip.rtp_socket) {
1648 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1649 "remote_lchan\n");
1650 return -EIO;
1651 }
1652
Harald Welte805f6442009-07-28 18:25:29 +02001653 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001654 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001655 if (rc < 0)
1656 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001657 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001658 if (rc < 0)
1659 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001660 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301661 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1662 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001663 } else {
1664 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301665 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1666 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301667 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001668 if (rc < 0)
1669 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301670 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1671 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301672 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001673 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001674 break;
1675 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001676 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001677 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001678 trau_mux_map_lchan(lchan, remote_lchan);
1679 break;
1680 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001681 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001682 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001683 }
Harald Welte49f48b82009-02-17 15:29:33 +00001684
1685 return 0;
1686}
1687
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001689static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001690{
Harald Welte53d51f52015-12-03 14:59:04 +01001691 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1692 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001693
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001695 return -EIO;
1696
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001697 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001698 return -EIO;
1699
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001700 /* Which subscriber do we want to track trans1 or trans2? */
1701 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1702
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001704 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001705}
1706
Harald Welteda7ab742009-12-19 22:23:05 +01001707/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001708static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709{
1710 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001711 struct gsm_lchan *lchan;
1712 struct gsm_bts *bts;
1713 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001714
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001716 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 if (!trans)
1718 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001719 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001721
1722 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001723 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001724 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001726 /* store receive state */
1727 trans->tch_recv = enable;
1728
Harald Welteda7ab742009-12-19 22:23:05 +01001729 switch (bts->type) {
1730 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001731 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001732 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001733 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001734 return -EINVAL;
1735 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001736 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1737 * will not be connected to our RTP proxy and the socket will
1738 * not be assigned to the application interface. This method
1739 * will be called again, once the audio socket is created and
1740 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001741 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001742 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1743 return 0;
1744 }
1745 if (enable) {
1746 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001747 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001748 if (rc < 0)
1749 return rc;
1750 /* assign socket to application interface */
1751 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1752 net, callref);
1753 } else
1754 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1755 net, 0);
1756 break;
1757 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001758 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001759 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001760 /* In case we don't have a TCH with correct mode, the TRAU muxer
1761 * will not be asigned to the application interface. This is
1762 * performed by switch_trau_mux() after successful handover or
1763 * assignment. */
1764 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1765 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1766 return 0;
1767 }
Harald Welteda7ab742009-12-19 22:23:05 +01001768 if (enable)
1769 return trau_recv_lchan(lchan, callref);
1770 return trau_mux_unmap(NULL, callref);
1771 break;
1772 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001773 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001774 return -EINVAL;
1775 }
1776
1777 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778}
1779
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1781{
1782 DEBUGP(DCC, "-> STATUS ENQ\n");
1783 return gsm48_cc_tx_status(trans, msg);
1784}
1785
1786static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1787static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1788
1789static void gsm48_cc_timeout(void *arg)
1790{
1791 struct gsm_trans *trans = arg;
1792 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001793 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1794 int mo_location = GSM48_CAUSE_LOC_USER;
1795 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1796 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 struct gsm_mncc mo_rel, l4_rel;
1798
1799 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1800 mo_rel.callref = trans->callref;
1801 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1802 l4_rel.callref = trans->callref;
1803
Harald Weltedcaf5652009-07-23 18:56:43 +02001804 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 case 0x303:
1806 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001807 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 break;
1809 case 0x310:
1810 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001811 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 break;
1813 case 0x313:
1814 disconnect = 1;
1815 /* unknown, did not find it in the specs */
1816 break;
1817 case 0x301:
1818 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001819 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 break;
1821 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001822 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001824 gsm48_cc_tx_release(trans, &trans->cc.msg);
1825 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 break; /* stay in release state */
1827 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001828 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 return;
1830// release = 1;
1831// l4_cause = 14;
1832// break;
1833 case 0x306:
1834 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001835 mo_cause = trans->cc.msg.cause.value;
1836 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 break;
1838 case 0x323:
1839 disconnect = 1;
1840 break;
1841 default:
1842 release = 1;
1843 }
1844
1845 if (release && trans->callref) {
1846 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001847 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 l4_location, l4_cause);
1849 trans->callref = 0;
1850 }
1851
1852 if (disconnect && trans->callref) {
1853 /* process disconnect towards layer 4 */
1854 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001855 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 }
1857
1858 /* process disconnect towards mobile station */
1859 if (disconnect || release) {
1860 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001861 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1862 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1863 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 mo_rel.cause.diag_len = 3;
1865
1866 if (disconnect)
1867 gsm48_cc_tx_disconnect(trans, &mo_rel);
1868 if (release)
1869 gsm48_cc_tx_release(trans, &mo_rel);
1870 }
1871
1872}
1873
Max3ffce192016-04-25 15:22:00 +02001874/* disconnect both calls from the bridge */
1875static inline void disconnect_bridge(struct gsm_network *net,
1876 struct gsm_mncc_bridge *bridge, int err)
1877{
1878 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1879 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1880 struct gsm_mncc mx_rel;
1881 if (!trans0 || !trans1)
1882 return;
1883
1884 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1885 trans0->callref, trans1->callref, strerror(err));
1886
1887 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1888 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1889 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1890
1891 mx_rel.callref = trans0->callref;
1892 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1893
1894 mx_rel.callref = trans1->callref;
1895 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1896}
1897
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1899 int sec, int micro)
1900{
1901 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001902 trans->cc.timer.cb = gsm48_cc_timeout;
1903 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001904 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001905 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906}
1907
1908static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1909{
1910 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001911 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1913 struct tlv_parsed tp;
1914 struct gsm_mncc setup;
1915
1916 memset(&setup, 0, sizeof(struct gsm_mncc));
1917 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001918 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001919 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 /* emergency setup is identified by msg_type */
1921 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1922 setup.emergency = 1;
1923
1924 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001925 setup.fields |= MNCC_F_CALLING;
1926 strncpy(setup.calling.number, trans->subscr->extension,
1927 sizeof(setup.calling.number)-1);
1928 strncpy(setup.imsi, trans->subscr->imsi,
1929 sizeof(setup.imsi)-1);
1930
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 /* bearer capability */
1932 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1933 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001934 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001936 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 }
1938 /* facility */
1939 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1940 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001941 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1943 }
1944 /* called party bcd number */
1945 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1946 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001947 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1949 }
1950 /* user-user */
1951 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1952 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001953 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1955 }
1956 /* ss-version */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1958 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1961 }
1962 /* CLIR suppression */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1964 setup.clir.sup = 1;
1965 /* CLIR invocation */
1966 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1967 setup.clir.inv = 1;
1968 /* cc cap */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1970 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1973 }
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 new_cc_state(trans, GSM_CSTATE_INITIATED);
1976
Harald Welte (local)47df3992009-12-26 19:45:03 +01001977 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1978 subscr_name(trans->subscr), trans->subscr->extension,
1979 setup.called.number);
1980
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001981 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001982
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001984 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985
Harald Welte13cac662009-07-29 12:10:35 +02001986 /* MNCC code will modify the channel asynchronously, we should
1987 * ipaccess-bind only after the modification has been made to the
1988 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 return 0;
1990}
1991
1992static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001993{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001994 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001995 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001997 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001998
Harald Welte7ccf7782009-02-17 01:43:01 +00001999 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002000
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 /* transaction id must not be assigned */
2002 if (trans->transaction_id != 0xff) { /* unasssigned */
2003 DEBUGP(DCC, "TX Setup with assigned transaction. "
2004 "This is not allowed!\n");
2005 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002006 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002007 GSM48_CAUSE_LOC_PRN_S_LU,
2008 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002010 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 return rc;
2012 }
2013
2014 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002015 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2016 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002017 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002019 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002020 GSM48_CAUSE_LOC_PRN_S_LU,
2021 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002023 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 return rc;
2025 }
Harald Welte78283ef2009-07-23 21:36:44 +02002026 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002027
Harald Welte65e74cc2008-12-29 01:55:35 +00002028 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002029
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002031
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 /* bearer capability */
2033 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 /* facility */
2036 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002037 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 /* progress */
2039 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 /* calling party BCD number */
2042 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002043 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 /* called party BCD number */
2045 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 /* user-user */
2048 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002049 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 /* redirecting party BCD number */
2051 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002052 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 /* signal */
2054 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056
2057 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002058
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002059 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01002060
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002061 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002062}
2063
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2065{
2066 struct gsm48_hdr *gh = msgb_l3(msg);
2067 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2068 struct tlv_parsed tp;
2069 struct gsm_mncc call_conf;
2070
2071 gsm48_stop_cc_timer(trans);
2072 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2073
2074 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2075 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002076 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002077 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078#if 0
2079 /* repeat */
2080 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2081 call_conf.repeat = 1;
2082 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2083 call_conf.repeat = 2;
2084#endif
2085 /* bearer capability */
2086 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2087 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002088 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002090 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 }
2092 /* cause */
2093 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2094 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002095 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2097 }
2098 /* cc cap */
2099 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2100 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002101 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2103 }
2104
Andreas Eversberg035b8742013-09-19 09:27:06 +02002105 /* IMSI of called subscriber */
2106 strncpy(call_conf.imsi, trans->subscr->imsi,
2107 sizeof(call_conf.imsi)-1);
2108
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2110
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002111 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002112 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113}
2114
2115static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2116{
2117 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002118 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2120
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2122
2123 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2124
2125 /* bearer capability */
2126 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 /* facility */
2129 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002130 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 /* progress */
2132 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002135 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136}
2137
2138static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2139{
2140 struct gsm48_hdr *gh = msgb_l3(msg);
2141 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2142 struct tlv_parsed tp;
2143 struct gsm_mncc alerting;
2144
2145 gsm48_stop_cc_timer(trans);
2146 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2147
2148 memset(&alerting, 0, sizeof(struct gsm_mncc));
2149 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002150 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 /* facility */
2152 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2153 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2156 }
2157
2158 /* progress */
2159 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2160 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002161 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2163 }
2164 /* ss-version */
2165 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2166 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002167 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2169 }
2170
2171 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2172
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002173 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002174 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175}
2176
2177static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2178{
2179 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002180 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2182
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 gh->msg_type = GSM48_MT_CC_ALERTING;
2184
2185 /* facility */
2186 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 /* progress */
2189 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002190 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 /* user-user */
2192 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002193 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194
2195 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2196
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002197 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198}
2199
2200static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2201{
2202 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002203 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2205
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 gh->msg_type = GSM48_MT_CC_PROGRESS;
2207
2208 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 /* user-user */
2211 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002212 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002214 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215}
2216
2217static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2218{
2219 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002220 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2222
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 gh->msg_type = GSM48_MT_CC_CONNECT;
2224
2225 gsm48_stop_cc_timer(trans);
2226 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2227
2228 /* facility */
2229 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 /* progress */
2232 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002233 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 /* connected number */
2235 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* user-user */
2238 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002239 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240
2241 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2242
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002243 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244}
2245
2246static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2247{
2248 struct gsm48_hdr *gh = msgb_l3(msg);
2249 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2250 struct tlv_parsed tp;
2251 struct gsm_mncc connect;
2252
2253 gsm48_stop_cc_timer(trans);
2254
2255 memset(&connect, 0, sizeof(struct gsm_mncc));
2256 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002257 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002259 connect.fields |= MNCC_F_CONNECTED;
2260 strncpy(connect.connected.number, trans->subscr->extension,
2261 sizeof(connect.connected.number)-1);
2262 strncpy(connect.imsi, trans->subscr->imsi,
2263 sizeof(connect.imsi)-1);
2264
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 /* facility */
2266 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2267 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002268 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2270 }
2271 /* user-user */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2273 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2276 }
2277 /* ss-version */
2278 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2279 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002280 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2282 }
2283
2284 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002285 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002287 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288}
2289
2290
2291static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2292{
2293 struct gsm_mncc connect_ack;
2294
2295 gsm48_stop_cc_timer(trans);
2296
2297 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002298 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299
2300 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2301 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002302
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002303 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 &connect_ack);
2305}
2306
2307static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2308{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002309 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2311
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2313
2314 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2315
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002316 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317}
2318
2319static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2320{
2321 struct gsm48_hdr *gh = msgb_l3(msg);
2322 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2323 struct tlv_parsed tp;
2324 struct gsm_mncc disc;
2325
2326 gsm48_stop_cc_timer(trans);
2327
2328 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2329
2330 memset(&disc, 0, sizeof(struct gsm_mncc));
2331 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002332 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333 /* cause */
2334 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2335 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002336 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2338 }
2339 /* facility */
2340 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2341 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2344 }
2345 /* user-user */
2346 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2347 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2350 }
2351 /* ss-version */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2353 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2356 }
2357
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002358 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359
2360}
2361
Harald Weltec66b71c2009-06-11 14:23:20 +08002362static struct gsm_mncc_cause default_cause = {
2363 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2364 .coding = 0,
2365 .rec = 0,
2366 .rec_val = 0,
2367 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2368 .diag_len = 0,
2369 .diag = { 0 },
2370};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371
2372static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2373{
2374 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002375 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2377
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2379
2380 gsm48_stop_cc_timer(trans);
2381 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2382
2383 /* cause */
2384 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002385 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386 else
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388
2389 /* facility */
2390 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002391 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 /* progress */
2393 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002394 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 /* user-user */
2396 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398
2399 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002400 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401
2402 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2403
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002404 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2408{
2409 struct gsm48_hdr *gh = msgb_l3(msg);
2410 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2411 struct tlv_parsed tp;
2412 struct gsm_mncc rel;
2413 int rc;
2414
2415 gsm48_stop_cc_timer(trans);
2416
2417 memset(&rel, 0, sizeof(struct gsm_mncc));
2418 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002419 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 /* cause */
2421 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2422 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002423 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2425 }
2426 /* facility */
2427 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2428 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002429 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2431 }
2432 /* user-user */
2433 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2434 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002435 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2437 }
2438 /* ss-version */
2439 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2440 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002441 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2443 }
2444
Harald Weltedcaf5652009-07-23 18:56:43 +02002445 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002447 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002449 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002450 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002451 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002452 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 }
2454
2455 new_cc_state(trans, GSM_CSTATE_NULL);
2456
2457 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002458 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459
2460 return rc;
2461}
2462
2463static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2464{
2465 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002466 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2468
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 gh->msg_type = GSM48_MT_CC_RELEASE;
2470
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 gsm48_stop_cc_timer(trans);
2472 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2473
2474 /* cause */
2475 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002476 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 /* facility */
2478 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002479 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480 /* user-user */
2481 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002482 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483
Harald Weltedcaf5652009-07-23 18:56:43 +02002484 trans->cc.T308_second = 0;
2485 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486
Harald Weltedcaf5652009-07-23 18:56:43 +02002487 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2489
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002490 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491}
2492
2493static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2494{
2495 struct gsm48_hdr *gh = msgb_l3(msg);
2496 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2497 struct tlv_parsed tp;
2498 struct gsm_mncc rel;
2499 int rc = 0;
2500
2501 gsm48_stop_cc_timer(trans);
2502
2503 memset(&rel, 0, sizeof(struct gsm_mncc));
2504 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002505 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 /* cause */
2507 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2508 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2511 }
2512 /* facility */
2513 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2514 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002515 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2517 }
2518 /* user-user */
2519 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2520 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002521 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2523 }
2524 /* ss-version */
2525 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2526 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002527 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2529 }
2530
2531 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002532 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002534 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535 MNCC_REJ_IND, &rel);
2536 break;
2537 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002538 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 MNCC_REL_CNF, &rel);
2540 break;
2541 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002542 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 MNCC_REL_IND, &rel);
2544 }
2545 }
2546
2547 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002548 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549
2550 return rc;
2551}
2552
2553static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2554{
2555 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002556 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002558 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2561
2562 trans->callref = 0;
2563
2564 gsm48_stop_cc_timer(trans);
2565
2566 /* cause */
2567 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002568 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 /* facility */
2570 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002571 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 /* user-user */
2573 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575
Harald Weltec7782de2010-12-21 19:31:41 +01002576 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2577
Harald Weltedcaf5652009-07-23 18:56:43 +02002578 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579
Harald Weltec7782de2010-12-21 19:31:41 +01002580 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581}
2582
2583static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2584{
2585 struct gsm48_hdr *gh = msgb_l3(msg);
2586 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2587 struct tlv_parsed tp;
2588 struct gsm_mncc fac;
2589
2590 memset(&fac, 0, sizeof(struct gsm_mncc));
2591 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002592 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 /* facility */
2594 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2595 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2598 }
2599 /* ss-version */
2600 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2601 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002602 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2604 }
2605
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002606 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2610{
2611 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002612 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2614
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 gh->msg_type = GSM48_MT_CC_FACILITY;
2616
2617 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002620 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621}
2622
2623static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2624{
2625 struct gsm_mncc hold;
2626
2627 memset(&hold, 0, sizeof(struct gsm_mncc));
2628 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002629 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2633{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002634 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2636
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2638
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002639 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640}
2641
2642static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2643{
2644 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002645 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2647
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2649
2650 /* cause */
2651 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002652 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 else
Harald Welte55c8f352010-03-07 23:40:35 +01002654 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002656 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657}
2658
2659static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2660{
2661 struct gsm_mncc retrieve;
2662
2663 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2664 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002665 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002666 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667}
2668
2669static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2670{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002671 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2673
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2675
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002676 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2680{
2681 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002682 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2684
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2686
2687 /* cause */
2688 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002689 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 else
Harald Welte55c8f352010-03-07 23:40:35 +01002691 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002693 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694}
2695
2696static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2697{
2698 struct gsm48_hdr *gh = msgb_l3(msg);
2699 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2700 struct tlv_parsed tp;
2701 struct gsm_mncc dtmf;
2702
2703 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2704 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002705 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 /* keypad facility */
2707 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2708 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002709 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2711 }
2712
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002713 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714}
2715
2716static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2717{
2718 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002719 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2721
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2723
2724 /* keypad */
2725 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002726 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002728 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729}
2730
2731static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2732{
2733 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002734 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2736
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2738
2739 /* cause */
2740 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002741 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 else
Harald Welte55c8f352010-03-07 23:40:35 +01002743 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002745 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746}
2747
2748static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2749{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002750 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
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_STOP_DTMF_ACK;
2754
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002755 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756}
2757
2758static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2759{
2760 struct gsm_mncc dtmf;
2761
2762 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2763 dtmf.callref = trans->callref;
2764
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002765 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2769{
2770 struct gsm48_hdr *gh = msgb_l3(msg);
2771 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2772 struct tlv_parsed tp;
2773 struct gsm_mncc modify;
2774
2775 memset(&modify, 0, sizeof(struct gsm_mncc));
2776 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002777 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778 /* bearer capability */
2779 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2780 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002781 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002783 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 }
2785
2786 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2787
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002788 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789}
2790
2791static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2792{
2793 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002794 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2796
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797 gh->msg_type = GSM48_MT_CC_MODIFY;
2798
2799 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2800
2801 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002802 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803
2804 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2805
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002806 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807}
2808
2809static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2810{
2811 struct gsm48_hdr *gh = msgb_l3(msg);
2812 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2813 struct tlv_parsed tp;
2814 struct gsm_mncc modify;
2815
2816 gsm48_stop_cc_timer(trans);
2817
2818 memset(&modify, 0, sizeof(struct gsm_mncc));
2819 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002820 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 /* bearer capability */
2822 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2823 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002824 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002826 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 }
2828
2829 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2830
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002831 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2835{
2836 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002837 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2839
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2841
2842 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002843 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844
2845 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2846
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002847 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848}
2849
2850static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2851{
2852 struct gsm48_hdr *gh = msgb_l3(msg);
2853 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2854 struct tlv_parsed tp;
2855 struct gsm_mncc modify;
2856
2857 gsm48_stop_cc_timer(trans);
2858
2859 memset(&modify, 0, sizeof(struct gsm_mncc));
2860 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002861 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 /* bearer capability */
2863 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2864 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002865 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002866 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002867 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002868 }
2869 /* cause */
2870 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2871 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002872 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2874 }
2875
2876 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2877
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002878 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2882{
2883 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002884 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2886
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2888
2889 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002890 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002892 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893
2894 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2895
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002896 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897}
2898
2899static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2900{
2901 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002902 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2904
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905 gh->msg_type = GSM48_MT_CC_NOTIFY;
2906
2907 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002908 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002910 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911}
2912
2913static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2914{
2915 struct gsm48_hdr *gh = msgb_l3(msg);
2916 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2917// struct tlv_parsed tp;
2918 struct gsm_mncc notify;
2919
2920 memset(&notify, 0, sizeof(struct gsm_mncc));
2921 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002922// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002924 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002926 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927}
2928
2929static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2930{
2931 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002932 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2934
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935 gh->msg_type = GSM48_MT_CC_USER_INFO;
2936
2937 /* user-user */
2938 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002939 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 /* more data */
2941 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002942 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002944 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002945}
2946
2947static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2948{
2949 struct gsm48_hdr *gh = msgb_l3(msg);
2950 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2951 struct tlv_parsed tp;
2952 struct gsm_mncc user;
2953
2954 memset(&user, 0, sizeof(struct gsm_mncc));
2955 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002956 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 /* user-user */
2958 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2959 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002960 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2962 }
2963 /* more data */
2964 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2965 user.more = 1;
2966
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002967 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968}
2969
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002970static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971{
2972 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002973 struct gsm_lchan *lchan = trans->conn->lchan;
2974
2975 /*
2976 * We were forced to make an assignment a lot earlier and
2977 * we should avoid sending another assignment that might
2978 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
2979 * In case of rtp-bridge it is too late to change things
2980 * here.
2981 */
2982 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
2983 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002985 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2986 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987}
2988
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002989static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2990 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2991 uint32_t payload_msg_type)
2992{
2993 uint8_t data[sizeof(struct gsm_mncc)];
2994 struct gsm_mncc_rtp *rtp;
2995
2996 memset(&data, 0, sizeof(data));
2997 rtp = (struct gsm_mncc_rtp *) &data[0];
2998
2999 rtp->callref = callref;
3000 rtp->msg_type = cmd;
3001 rtp->ip = addr;
3002 rtp->port = port;
3003 rtp->payload_type = payload_type;
3004 rtp->payload_msg_type = payload_msg_type;
3005 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3006}
3007
3008static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3009{
3010 struct gsm_lchan *lchan;
3011 int msg_type;
3012
3013 lchan = trans->conn->lchan;
3014 switch (lchan->abis_ip.rtp_payload) {
3015 case RTP_PT_GSM_FULL:
3016 msg_type = GSM_TCHF_FRAME;
3017 break;
3018 case RTP_PT_GSM_EFR:
3019 msg_type = GSM_TCHF_FRAME_EFR;
3020 break;
3021 case RTP_PT_GSM_HALF:
3022 msg_type = GSM_TCHH_FRAME;
3023 break;
3024 case RTP_PT_AMR:
3025 msg_type = GSM_TCH_FRAME_AMR;
3026 break;
3027 default:
3028 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3029 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3030 msg_type = 0;
3031 break;
3032 }
3033
3034 return mncc_recv_rtp(net, trans->callref, cmd,
3035 lchan->abis_ip.bound_ip,
3036 lchan->abis_ip.bound_port,
3037 lchan->abis_ip.rtp_payload,
3038 msg_type);
3039}
3040
3041static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3042{
3043 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3044}
3045
3046static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3047{
3048 struct gsm_bts *bts;
3049 struct gsm_lchan *lchan;
3050 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003051 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003052
3053 /* Find callref */
3054 trans = trans_find_by_callref(net, callref);
3055 if (!trans) {
3056 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3057 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3058 return -EIO;
3059 }
3060 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3061 if (!trans->conn) {
3062 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3063 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3064 return 0;
3065 }
3066
3067 lchan = trans->conn->lchan;
3068 bts = lchan->ts->trx->bts;
3069 if (!is_ipaccess_bts(bts)) {
3070 /*
3071 * I want this to be straight forward and have no audio flow
3072 * through the nitb/osmo-mss system. This currently means that
3073 * this will not work with BS11/Nokia type BTS. We would need
3074 * to have a trau<->rtp bridge for these but still preferable
3075 * in another process.
3076 */
3077 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3078 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3079 return -EINVAL;
3080 }
3081
3082 trans->conn->mncc_rtp_bridge = 1;
3083 /*
3084 * *sigh* we need to pick a codec now. Pick the most generic one
3085 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003086 * similiar to the routine above.
3087 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003088 */
3089 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3090 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003091 m = mncc_codec_for_mode(lchan->type);
3092 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3093 get_value_string(gsm48_chan_mode_names, m),
3094 get_value_string(gsm_chan_t_names, lchan->type));
3095 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003096 lchan->type != GSM_LCHAN_TCH_H);
3097 }
3098
3099 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3100 return 0;
3101}
3102
3103static int tch_rtp_connect(struct gsm_network *net, void *arg)
3104{
3105 struct gsm_lchan *lchan;
3106 struct gsm_trans *trans;
3107 struct gsm_mncc_rtp *rtp = arg;
3108
3109 /* Find callref */
3110 trans = trans_find_by_callref(net, rtp->callref);
3111 if (!trans) {
3112 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3113 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3114 return -EIO;
3115 }
3116 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3117 if (!trans->conn) {
3118 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3119 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3120 return 0;
3121 }
3122
3123 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003124 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3125 get_value_string(gsm48_chan_mode_names,
3126 mncc_codec_for_mode(lchan->type)),
3127 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003128
3129 /* TODO: Check if payload_msg_type is compatible with what we have */
3130 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3131 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3132 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3133 }
3134
3135 /*
3136 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3137 * complains about both rtp and rtp payload2 being present in the
3138 * same package!
3139 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003140 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003141 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3142}
3143
3144static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3145{
3146 struct gsm_network *net;
3147 struct gsm_trans *tmp, *trans = NULL;
3148
3149 net = lchan->ts->trx->bts->network;
3150 llist_for_each_entry(tmp, &net->trans_list, entry) {
3151 if (!tmp->conn)
3152 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003153 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003154 continue;
3155 trans = tmp;
3156 break;
3157 }
3158
3159 if (!trans) {
3160 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3161 gsm_lchan_name(lchan));
3162 return 0;
3163 }
3164
3165 switch (signal) {
3166 case S_ABISIP_CRCX_ACK:
3167 if (lchan->conn->mncc_rtp_create_pending) {
3168 lchan->conn->mncc_rtp_create_pending = 0;
3169 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3170 gsm_lchan_name(lchan));
3171 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3172 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003173 /*
3174 * TODO: this appears to be too early? Why not until after
3175 * the handover detect or the handover complete?
3176 */
3177 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003178 break;
3179 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003180 if (lchan->conn->mncc_rtp_connect_pending) {
3181 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003182 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3183 gsm_lchan_name(lchan));
3184 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3185 }
3186 break;
3187 }
3188
3189 return 0;
3190}
3191
3192
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003194 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003195 int type;
3196 int (*rout) (struct gsm_trans *trans, void *arg);
3197} downstatelist[] = {
3198 /* mobile originating call establishment */
3199 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3200 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3201 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3202 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3203 {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 */
3204 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3205 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3206 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3207 /* mobile terminating call establishment */
3208 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3209 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3210 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3211 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3212 /* signalling during call */
3213 {SBIT(GSM_CSTATE_ACTIVE),
3214 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3215 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3216 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3217 {ALL_STATES,
3218 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3219 {ALL_STATES,
3220 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3221 {ALL_STATES,
3222 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3223 {SBIT(GSM_CSTATE_ACTIVE),
3224 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3225 {SBIT(GSM_CSTATE_ACTIVE),
3226 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3227 {SBIT(GSM_CSTATE_ACTIVE),
3228 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3229 {SBIT(GSM_CSTATE_ACTIVE),
3230 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3231 {SBIT(GSM_CSTATE_ACTIVE),
3232 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3233 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3234 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3235 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3236 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3237 {SBIT(GSM_CSTATE_ACTIVE),
3238 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3239 /* clearing */
3240 {SBIT(GSM_CSTATE_INITIATED),
3241 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3242 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3243 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3244 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3245 MNCC_REL_REQ, gsm48_cc_tx_release},
3246 /* special */
3247 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003248 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249};
3250
3251#define DOWNSLLEN \
3252 (sizeof(downstatelist) / sizeof(struct downstate))
3253
3254
Harald Welte76556372010-12-22 23:57:45 +01003255int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003256{
Harald Welte1a6f7982009-08-09 18:52:33 +02003257 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003258 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003259 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003260 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261 struct gsm_mncc *data = arg, rel;
3262
Harald Welte04dc88f2010-12-22 21:45:05 +01003263 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3264
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 /* handle special messages */
3266 switch(msg_type) {
3267 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003268 rc = tch_bridge(net, arg);
3269 if (rc < 0)
3270 disconnect_bridge(net, arg, -rc);
3271 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003272 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003273 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003274 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003275 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003276 case MNCC_RTP_CREATE:
3277 return tch_rtp_create(net, data->callref);
3278 case MNCC_RTP_CONNECT:
3279 return tch_rtp_connect(net, arg);
3280 case MNCC_RTP_FREE:
3281 /* unused right now */
3282 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003283 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003284 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003285 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003286 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003287 /* Find callref */
3288 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003289 if (!trans) {
3290 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003291 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003292 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003293 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003294 if (!trans->conn) {
3295 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003296 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003297 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003298 if (!trans->conn->lchan) {
3299 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3300 return 0;
3301 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003302 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3303 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003304 /* This should be LOGL_ERROR or NOTICE, but
3305 * unfortuantely it happens for a couple of frames at
3306 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003307 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003308 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003309 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003310 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003311 switch (bts->type) {
3312 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003313 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003314 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003315 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003316 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003317 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003318 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003319 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003320 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003321 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003322 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003323 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003324 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003325 }
3326 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003327 }
3328
3329 memset(&rel, 0, sizeof(struct gsm_mncc));
3330 rel.callref = data->callref;
3331
3332 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003333 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003334
3335 /* Callref unknown */
3336 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003337 struct gsm_subscriber *subscr;
3338
Harald Welte4a3464c2009-07-04 10:11:24 +02003339 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003340 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3341 "Received '%s' from MNCC with "
3342 "unknown callref %d\n", data->called.number,
3343 get_mncc_name(msg_type), data->callref);
3344 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003345 return mncc_release_ind(net, NULL, data->callref,
3346 GSM48_CAUSE_LOC_PRN_S_LU,
3347 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003348 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003349 if (!data->called.number[0] && !data->imsi[0]) {
3350 DEBUGP(DCC, "(bts - trx - ts - ti) "
3351 "Received '%s' from MNCC with "
3352 "no number or IMSI\n", get_mncc_name(msg_type));
3353 /* Invalid number */
3354 return mncc_release_ind(net, NULL, data->callref,
3355 GSM48_CAUSE_LOC_PRN_S_LU,
3356 GSM48_CC_CAUSE_INV_NR_FORMAT);
3357 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003358 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003359 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003360 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003361 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003362 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003363 subscr = subscr_get_by_imsi(net->subscr_group,
3364 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003365
3366 /* update the subscriber we deal with */
3367 log_set_context(BSC_CTX_SUBSCR, subscr);
3368
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 /* If subscriber is not found */
3370 if (!subscr) {
3371 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3372 "Received '%s' from MNCC with "
3373 "unknown subscriber %s\n", data->called.number,
3374 get_mncc_name(msg_type), data->called.number);
3375 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003376 return mncc_release_ind(net, NULL, data->callref,
3377 GSM48_CAUSE_LOC_PRN_S_LU,
3378 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003379 }
3380 /* If subscriber is not "attached" */
3381 if (!subscr->lac) {
3382 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3383 "Received '%s' from MNCC with "
3384 "detached subscriber %s\n", data->called.number,
3385 get_mncc_name(msg_type), data->called.number);
3386 subscr_put(subscr);
3387 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003388 return mncc_release_ind(net, NULL, data->callref,
3389 GSM48_CAUSE_LOC_PRN_S_LU,
3390 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003391 }
3392 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003393 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003394 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003395 DEBUGP(DCC, "No memory for trans.\n");
3396 subscr_put(subscr);
3397 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003398 mncc_release_ind(net, NULL, data->callref,
3399 GSM48_CAUSE_LOC_PRN_S_LU,
3400 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003401 return -ENOMEM;
3402 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003403 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003404 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003405
Harald Welte4bfdfe72009-06-10 23:11:52 +08003406 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003407 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003408 /* find transaction with this subscriber already paging */
3409 llist_for_each_entry(transt, &net->trans_list, entry) {
3410 /* Transaction of our lchan? */
3411 if (transt == trans ||
3412 transt->subscr != subscr)
3413 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003414 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003415 "Received '%s' from MNCC with "
3416 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003417 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003418 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003419 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003420 subscr_put(subscr);
3421 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003422 return 0;
3423 }
3424 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003425 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003426
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003427 /* Request a channel */
3428 trans->paging_request = subscr_request_channel(subscr,
3429 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3430 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003431 if (!trans->paging_request) {
3432 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3433 subscr_put(subscr);
3434 trans_free(trans);
3435 return 0;
3436 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003437 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003438 return 0;
3439 }
3440 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003441 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003442 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003443 } else {
3444 /* update the subscriber we deal with */
3445 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003447
3448 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003449 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003450
3451 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003452 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003453 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003454 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003455 (trans->subscr)?(trans->subscr->extension):"-",
3456 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003457 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3458 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003459 if (msg_type == MNCC_REL_REQ)
3460 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3461 else
3462 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3463 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003464 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003465 return rc;
3466 }
3467
3468 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3469 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003470 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003471 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003472 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003473 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003474 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003475
3476 /* Find function for current state and message */
3477 for (i = 0; i < DOWNSLLEN; i++)
3478 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003479 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480 break;
3481 if (i == DOWNSLLEN) {
3482 DEBUGP(DCC, "Message unhandled at this state.\n");
3483 return 0;
3484 }
3485
3486 rc = downstatelist[i].rout(trans, arg);
3487
3488 return rc;
3489}
3490
3491
3492static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003493 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003494 int type;
3495 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3496} datastatelist[] = {
3497 /* mobile originating call establishment */
3498 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3499 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3500 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3501 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3502 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3503 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3504 /* mobile terminating call establishment */
3505 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3506 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3507 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3508 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003509 {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 +08003510 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3511 /* signalling during call */
3512 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3513 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3514 {SBIT(GSM_CSTATE_ACTIVE),
3515 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3516 {ALL_STATES,
3517 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3518 {ALL_STATES,
3519 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3520 {ALL_STATES,
3521 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3522 {SBIT(GSM_CSTATE_ACTIVE),
3523 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3524 {SBIT(GSM_CSTATE_ACTIVE),
3525 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3526 {SBIT(GSM_CSTATE_ACTIVE),
3527 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3528 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3529 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3530 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3531 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3532 {SBIT(GSM_CSTATE_ACTIVE),
3533 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3534 /* clearing */
3535 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3536 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3537 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3538 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3539 {ALL_STATES, /* 5.4.3.4 */
3540 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3541};
3542
3543#define DATASLLEN \
3544 (sizeof(datastatelist) / sizeof(struct datastate))
3545
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003546static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003547{
3548 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003549 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003550 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003551 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003552 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003553
Harald Welte4bfdfe72009-06-10 23:11:52 +08003554 if (msg_type & 0x80) {
3555 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3556 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003557 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003558
Harald Welte4bfdfe72009-06-10 23:11:52 +08003559 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003560 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003561
Harald Welte6f5aee02009-07-23 21:21:14 +02003562 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003563 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003564 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003565 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003566 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3567 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003568
3569 /* Create transaction */
3570 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003571 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003572 "creating new trans.\n", transaction_id);
3573 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003574 trans = trans_alloc(conn->bts->network, conn->subscr,
3575 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003576 transaction_id, new_callref++);
3577 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003578 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003579 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003580 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003581 GSM48_MT_CC_RELEASE_COMPL);
3582 return -ENOMEM;
3583 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003584 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003585 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003586 }
3587
3588 /* find function for current state and message */
3589 for (i = 0; i < DATASLLEN; i++)
3590 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003591 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003592 break;
3593 if (i == DATASLLEN) {
3594 DEBUGP(DCC, "Message unhandled at this state.\n");
3595 return 0;
3596 }
3597
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003598 assert(trans->subscr);
3599
Harald Welte4bfdfe72009-06-10 23:11:52 +08003600 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003601
3602 return rc;
3603}
3604
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003605/* Create a dummy to wait five seconds */
3606static void release_anchor(struct gsm_subscriber_connection *conn)
3607{
3608 if (!conn->anch_operation)
3609 return;
3610
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003611 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003612 talloc_free(conn->anch_operation);
3613 conn->anch_operation = NULL;
3614}
3615
3616static void anchor_timeout(void *_data)
3617{
3618 struct gsm_subscriber_connection *con = _data;
3619
3620 release_anchor(con);
3621 msc_release_connection(con);
3622}
3623
3624int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3625{
3626 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3627 if (!conn->anch_operation)
3628 return -1;
3629
3630 conn->anch_operation->timeout.data = conn;
3631 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003632 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003633 return 0;
3634}
3635
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003636/* here we get data from the BSC level... */
3637int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003638{
3639 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003640 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003641 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003642
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003643 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003644 if (silent_call_reroute(conn, msg))
3645 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003646
3647 switch (pdisc) {
3648 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003649 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003650 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003651 break;
3652 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003653 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003654 break;
3655 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003656 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003657 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003658 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003659 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003660 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003661 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003662 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003663 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003664 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3665 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003666 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003667 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003668 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003669 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003670 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003671 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003672 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3673 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003674 break;
3675 }
3676
3677 return rc;
3678}
Harald Welte8470bf22008-12-25 23:28:35 +00003679
Harald Welte805f6442009-07-28 18:25:29 +02003680/*
3681 * This will be ran by the linker when loading the DSO. We use it to
3682 * do system initialization, e.g. registration of signal handlers.
3683 */
3684static __attribute__((constructor)) void on_dso_load_0408(void)
3685{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003686 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003687}