blob: a22e3c242d4928456df11b3277ded089b971d295 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080061
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/gsm/gsm48.h>
63#include <osmocom/gsm/gsm0480.h>
64#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020065#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/core/msgb.h>
67#include <osmocom/core/talloc.h>
68#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000069
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020070#include <assert.h>
71
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020072void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010073void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020074
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020075static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
76
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020077static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080078static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020079 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010080static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080081static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000082
Harald Welte52b1f982008-12-23 20:25:15 +000083struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020084 uint16_t mcc;
85 uint16_t mnc;
86 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000087};
88
Andreas Eversberga83d5112013-12-07 18:32:28 +010089static int apply_codec_restrictions(struct gsm_bts *bts,
90 struct gsm_mncc_bearer_cap *bcap)
91{
92 int i, j;
93
94 /* remove unsupported speech versions from list */
95 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
96 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
97 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
98 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
99 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
100 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
101 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
102 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
103 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
104 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
105 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
106 }
107 bcap->speech_ver[j] = -1;
108
109 return 0;
110}
111
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200112static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800113
Harald Welte31c00f72011-03-03 23:29:05 +0100114void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
115{
116 net->mncc_recv(net, msg);
117}
118
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800119static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
120 struct gsm_trans *trans)
121{
122 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
123
124 /* if we get passed a transaction reference, do some common
125 * work that the caller no longer has to do */
126 if (trans) {
127 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
128 msg->lchan = trans->conn->lchan;
129 }
130
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800131 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200132 struct e1inp_sign_link *sign_link =
133 msg->lchan->ts->trx->rsl_link;
134
135 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100136 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800137 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200138 "Sending '%s' to MS.\n",
139 sign_link->trx->bts->nr,
140 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800141 gh->proto_discr & 0xf0,
142 gsm48_cc_msg_name(gh->msg_type));
143 else
144 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200145 "Sending 0x%02x to MS.\n",
146 sign_link->trx->bts->nr,
147 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800148 gh->proto_discr, gh->msg_type);
149 }
150
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100151 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800152}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100153
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800154int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
155{
156 struct gsm48_hdr *gh;
157 struct msgb *ss_notify;
158
159 ss_notify = gsm0480_create_notifySS(message);
160 if (!ss_notify)
161 return -1;
162
163 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
164 uint8_t *data = msgb_push(ss_notify, 1);
165 data[0] = ss_notify->len - 1;
166 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
167 gh->msg_type = GSM48_MT_CC_FACILITY;
168 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
169}
170
Harald Weltecf149ee2012-01-23 16:40:24 +0100171void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100172{
173 if (!conn->sec_operation)
174 return;
175
176 talloc_free(conn->sec_operation);
177 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800178 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100179}
180
Harald Weltecf149ee2012-01-23 16:40:24 +0100181void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100182{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
184 struct gsm_security_operation);
185}
186
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100187int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
188 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100189{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200190 struct gsm_network *net = conn->network;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800191 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100192 struct gsm_security_operation *op;
193 struct gsm_auth_tuple atuple;
194 int status = -1, rc;
195
196 /* Check if we _can_ enable encryption. Cases where we can't:
197 * - Encryption disabled in config
198 * - Channel already secured (nothing to do)
199 * - Subscriber equipment doesn't support configured encryption
200 */
201 if (!net->a5_encryption) {
202 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800203 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100204 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200205 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100206 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
207 net->a5_encryption)) {
208 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
209 status = GSM_SECURITY_NOAVAIL;
210 }
211
212 /* If not done yet, try to get info for this user */
213 if (status < 0) {
214 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
215 if (rc <= 0)
216 status = GSM_SECURITY_NOAVAIL;
217 }
218
219 /* Are we done yet ? */
220 if (status >= 0)
221 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800222 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100223 0;
224
225 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800226 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100227 return -EBUSY;
228
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800229 allocate_security_operation(conn);
230 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100231 op->cb = cb;
232 op->cb_data = cb_data;
233 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
234
235 /* FIXME: Should start a timer for completion ... */
236
237 /* Then do whatever is needed ... */
Neels Hofmeyrd2fa7a52016-03-10 23:30:37 +0100238 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100239 /* Start authentication */
Harald Welte121e9a42016-04-20 13:13:19 +0200240 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100241 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100242 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100243 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +0200244 op->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100245 }
246
247 return -EINVAL; /* not reached */
248}
249
Maxddee01f2016-05-24 14:23:27 +0200250static bool subscr_regexp_check(const struct gsm_network *net, const char *imsi)
251{
252 if (!net->authorized_reg_str)
253 return false;
254
255 if (regexec(&net->authorized_regexp, imsi, 0, NULL, 0) != REG_NOMATCH)
256 return true;
257
258 return false;
259}
260
Holger Freyther73487a22008-12-31 18:53:57 +0000261static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
262 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000263{
264 if (!subscriber)
265 return 0;
266
Holger Freyther73487a22008-12-31 18:53:57 +0000267 /*
268 * Do not send accept yet as more information should arrive. Some
269 * phones will not send us the information and we will have to check
270 * what we want to do with that.
271 */
272 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
273 return 0;
274
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100275 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200276 case GSM_AUTH_POLICY_CLOSED:
277 return subscriber->authorized;
Maxddee01f2016-05-24 14:23:27 +0200278 case GSM_AUTH_POLICY_REGEXP:
279 if (subscriber->authorized)
280 return 1;
281 if (subscr_regexp_check(subscriber->group->net,
282 subscriber->imsi))
283 subscriber->authorized = 1;
284 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200285 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200286 if (subscriber->authorized)
287 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200288 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200289 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000290 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200291 default:
292 return 0;
293 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000294}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000295
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100296static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000297{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100298 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000299 return;
300
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800301 /* No need to keep the connection up */
302 release_anchor(conn);
303
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200304 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100305 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100306 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100307 if (release)
308 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000309}
310
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100311static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000312{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800313 if (conn->loc_operation)
314 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100315 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000316
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100317 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200318 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000319}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000320
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200321static int finish_lu(struct gsm_subscriber_connection *conn)
322{
323 int rc = 0;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200324 int avoid_tmsi = conn->network->avoid_tmsi;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200325
326 /* We're all good */
327 if (avoid_tmsi) {
328 conn->subscr->tmsi = GSM_RESERVED_TMSI;
329 db_sync_subscriber(conn->subscr);
330 } else {
331 db_subscriber_alloc_tmsi(conn->subscr);
332 }
333
334 rc = gsm0408_loc_upd_acc(conn);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200335 if (conn->network->send_mm_info) {
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200336 /* send MM INFO with network name */
337 rc = gsm48_tx_mm_info(conn);
338 }
339
340 /* call subscr_update after putting the loc_upd_acc
341 * in the transmit queue, since S_SUBSCR_ATTACHED might
342 * trigger further action like SMS delivery */
343 subscr_update(conn->subscr, conn->bts,
344 GSM_SUBSCRIBER_UPDATE_ATTACHED);
345
346 /*
347 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
348 * MS needs to respond with a TMSI REALLOCATION COMPLETE
349 * (even if the TMSI is the same).
350 */
351 if (avoid_tmsi)
352 release_loc_updating_req(conn, 1);
353
354 return rc;
355}
356
Sylvain Munaut267fba02009-12-24 00:28:01 +0100357static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
358 struct msgb *msg, void *data, void *param)
359{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800360 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100361 int rc = 0;
362
363 switch (event) {
364 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100365 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100366 break;
367
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200368 case GSM_SECURITY_ALREADY:
369 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
370 "UPDATING after CM SERVICE REQUEST\n");
371 /* fall through */
372
Sylvain Munaut267fba02009-12-24 00:28:01 +0100373 case GSM_SECURITY_NOAVAIL:
374 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200375 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100376 break;
377
378 default:
379 rc = -EINVAL;
380 };
381
382 return rc;
383}
384
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100385static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000386{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100387 if (!conn->loc_operation)
388 return 0;
389
Sylvain Munaut267fba02009-12-24 00:28:01 +0100390 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800391 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100392 conn->loc_operation->key_seq,
393 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000394 return 0;
395}
396
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800397void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000398{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800399 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800400
401 /* avoid someone issuing a clear */
402 conn->in_release = 1;
403
Holger Freyther7c19f742009-06-06 13:54:35 +0000404 /*
405 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800406 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000407 */
Neels Hofmeyraea28ce2016-05-04 15:30:30 +0200408 release_loc_updating_req(conn, 0);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100409
410 /* We might need to cancel the paging response or such. */
411 if (conn->sec_operation && conn->sec_operation->cb) {
412 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
413 NULL, conn, conn->sec_operation->cb_data);
414 }
415
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800416 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800417 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000418
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100419 /*
420 * Free all transactions that are associated with the released
421 * connection. The transaction code will inform the CC or SMS
422 * facilities that will send the release indications. As part of
423 * the CC REL_IND the remote leg might be released and this will
424 * trigger the call to trans_free. This is something the llist
425 * macro can not handle and we will need to re-iterate the list.
426 *
427 * TODO: Move the trans_list into the subscriber connection and
428 * create a pending list for MT transactions. These exist before
429 * we have a subscriber connection.
430 */
431restart:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200432 llist_for_each_entry_safe(trans, temp, &conn->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100433 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200434 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100435 goto restart;
436 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800437 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000438}
439
Harald Welte371efe52010-12-22 23:17:50 +0100440void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
441{
442 struct gsm_trans *trans, *temp;
443
444 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
445
446 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100447 if (trans->protocol == protocol) {
448 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100449 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100450 }
Harald Welte371efe52010-12-22 23:17:50 +0100451 }
452}
453
Holger Freyther429e7762008-12-30 13:28:30 +0000454/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000456{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800457 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800458 struct msgb *msg;
459
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200460 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_REJECT]);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800461
462 msg = gsm48_create_loc_upd_rej(cause);
463 if (!msg) {
464 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
465 return -1;
466 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200467
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800468 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000469
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100470 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte7184bd02015-12-28 14:04:36 +0100471 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800472 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100473
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800474 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000475}
476
477/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200478static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000479{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100480 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000481 struct gsm48_hdr *gh;
482 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200483 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200484
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800485 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000486
487 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
488 gh->proto_discr = GSM48_PDISC_MM;
489 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
490
491 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200492 gsm48_generate_lai(lai, conn->network->country_code,
493 conn->network->network_code,
494 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000495
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200496 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
497 uint8_t mi[10];
498 int len;
499 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
500 mid = msgb_put(msg, len);
501 memcpy(mid, mi, len);
502 } else {
503 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
504 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
505 }
Harald Welte52b1f982008-12-23 20:25:15 +0000506
507 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
508
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200509 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_ACCEPT]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100510
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800511 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000512}
513
Harald Weltebf5e8df2009-02-03 12:59:45 +0000514/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200515static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000516{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000518 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000519
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800520 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000521
522 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
523 gh->proto_discr = GSM48_PDISC_MM;
524 gh->msg_type = GSM48_MT_MM_ID_REQ;
525 gh->data[0] = id_type;
526
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800527 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000528}
529
Maxe21cf382016-05-11 11:01:06 +0200530static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
531 const char *imsi)
532{
Maxe6052c42016-06-30 10:25:49 +0200533 if (!net->auto_create_subscr)
Maxddee01f2016-05-24 14:23:27 +0200534 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200535
Maxe6052c42016-06-30 10:25:49 +0200536 if (!subscr_regexp_check(net, imsi))
537 return NULL;
Maxddee01f2016-05-24 14:23:27 +0200538
Maxe6052c42016-06-30 10:25:49 +0200539 return subscr_create_subscriber(net->subscr_group, imsi);
Maxe21cf382016-05-11 11:01:06 +0200540}
Harald Welte231ad4f2008-12-27 11:15:38 +0000541
Harald Weltebf5e8df2009-02-03 12:59:45 +0000542/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800543static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000544{
545 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200546 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200547 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200548 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000549
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200550 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200551 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
552 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000553
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200554 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100555
Harald Welte75a983f2008-12-27 21:34:06 +0000556 switch (mi_type) {
557 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200558 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100559 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100560 conn->subscr = subscr_get_by_imsi(net->subscr_group,
561 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200562 if (!conn->subscr)
563 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200564 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100565 if (!conn->subscr && conn->loc_operation) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200566 gsm0408_loc_upd_rej(conn, net->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 release_loc_updating_req(conn, 1);
568 return 0;
569 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100570 if (conn->loc_operation)
571 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000572 break;
573 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000574 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000575 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100576 if (conn->subscr) {
577 db_subscriber_assoc_imei(conn->subscr, mi_string);
578 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200579 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100580 if (conn->loc_operation)
581 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000582 break;
583 }
Holger Freyther73487a22008-12-31 18:53:57 +0000584
585 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100586 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000587}
588
Harald Welte255539c2008-12-28 02:26:27 +0000589
590static void loc_upd_rej_cb(void *data)
591{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100592 struct gsm_subscriber_connection *conn = data;
Harald Welte255539c2008-12-28 02:26:27 +0000593
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100594 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200595 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100596 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000597}
598
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100599static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000600{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100601 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
602 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200603 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000604}
605
Harald Welteb9845f92015-08-16 18:07:48 +0200606static const struct value_string lupd_names[] = {
607 { GSM48_LUPD_NORMAL, "NORMAL" },
608 { GSM48_LUPD_PERIODIC, "PERIODIC" },
609 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
610 { 0, NULL }
611};
Harald Welte2a139372009-02-22 21:14:55 +0000612
Harald Weltebf5e8df2009-02-03 12:59:45 +0000613/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800614static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000615{
Harald Welte8470bf22008-12-25 23:28:35 +0000616 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000617 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800618 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200619 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200620 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000621
Harald Welte8470bf22008-12-25 23:28:35 +0000622 lu = (struct gsm48_loc_upd_req *) gh->data;
623
624 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000625
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200626 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000627
Harald Welteb9845f92015-08-16 18:07:48 +0200628 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
629 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000630
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200631 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100632
Harald Welte24ff6ee2009-12-22 00:41:05 +0100633 switch (lu->type) {
634 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200635 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100636 break;
637 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200638 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100639 break;
640 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200641 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100642 break;
643 }
644
Holger Freythereaf04692009-06-06 13:54:44 +0000645 /*
646 * Pseudo Spoof detection: Just drop a second/concurrent
647 * location updating request.
648 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100649 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200650 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100651 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800652 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000653 return 0;
654 }
655
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800656 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000657
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200658 conn->loc_operation->key_seq = lu->key_seq;
659
Harald Welte52b1f982008-12-23 20:25:15 +0000660 switch (mi_type) {
661 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200662 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000663 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200664 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100665 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000666
Jan Luebbe370b41d2009-08-12 10:19:34 +0200667 /* look up subscriber based on IMSI, create if not found */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200668 subscr = subscr_get_by_imsi(conn->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200669 if (!subscr)
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200670 subscr = subscr_create(conn->network, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100671 if (!subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200672 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100673 release_loc_updating_req(conn, 0);
674 return 0;
675 }
Harald Welte4b634542008-12-27 01:55:51 +0000676 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000677 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200678 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000679 /* look up the subscriber based on TMSI, request IMSI if it fails */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200680 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200681 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000682 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000683 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200684 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100685 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000686 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200687 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200688 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200689 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000690 break;
691 case GSM_MI_TYPE_IMEI:
692 case GSM_MI_TYPE_IMEISV:
693 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200694 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000695 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200696 default:
Harald Weltea0368542009-06-27 02:58:43 +0200697 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000698 break;
699 }
700
Harald Welte24516ea2009-07-04 10:18:00 +0200701 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100702 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200703
Harald Welte4bfdfe72009-06-10 23:11:52 +0800704 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200705 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 /* FIXME: request id? close channel? */
707 return -EINVAL;
708 }
709
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100710 conn->subscr = subscr;
711 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000712
Harald Welte24516ea2009-07-04 10:18:00 +0200713 /* check if we can let the subscriber into our network immediately
714 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100715 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000716}
717
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100718/* Turn int into semi-octet representation: 98 => 0x89 */
719static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800720{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100721 uint8_t ret;
722
723 ret = value / 10;
724 ret |= (value % 10) << 4;
725
726 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800727}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100728
Harald Welte4bfdfe72009-06-10 23:11:52 +0800729
Harald Weltedb253af2008-12-30 17:56:55 +0000730/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800731int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000732{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100733 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000734 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200735 struct gsm_network *net = conn->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100736 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200737 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200738 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100739
Harald Welte4bfdfe72009-06-10 23:11:52 +0800740 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100741 struct tm* gmt_time;
742 struct tm* local_time;
743 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100744 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000745
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800746 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000747
748 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
749 gh->proto_discr = GSM48_PDISC_MM;
750 gh->msg_type = GSM48_MT_MM_INFO;
751
752 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200753#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000754 name_len = strlen(net->name_long);
755 /* 10.5.3.5a */
756 ptr8 = msgb_put(msg, 3);
757 ptr8[0] = GSM48_IE_NAME_LONG;
758 ptr8[1] = name_len*2 +1;
759 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
760
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200761 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000762 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000763 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000764
765 /* FIXME: Use Cell Broadcast, not UCS-2, since
766 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200767#endif
768 name_len = (strlen(net->name_long)*7)/8;
769 name_pad = (8 - strlen(net->name_long)*7)%8;
770 if (name_pad > 0)
771 name_len++;
772 /* 10.5.3.5a */
773 ptr8 = msgb_put(msg, 3);
774 ptr8[0] = GSM48_IE_NAME_LONG;
775 ptr8[1] = name_len +1;
776 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
777
778 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100779 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200780
Harald Weltedb253af2008-12-30 17:56:55 +0000781 }
782
783 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200784#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000785 name_len = strlen(net->name_short);
786 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200787 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200788 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000789 ptr8[1] = name_len*2 + 1;
790 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
791
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200792 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000793 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000794 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200795#endif
796 name_len = (strlen(net->name_short)*7)/8;
797 name_pad = (8 - strlen(net->name_short)*7)%8;
798 if (name_pad > 0)
799 name_len++;
800 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200801 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200802 ptr8[0] = GSM48_IE_NAME_SHORT;
803 ptr8[1] = name_len +1;
804 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
805
806 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100807 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200808
Harald Weltedb253af2008-12-30 17:56:55 +0000809 }
810
Harald Weltedb253af2008-12-30 17:56:55 +0000811 /* Section 10.5.3.9 */
812 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100813 gmt_time = gmtime(&cur_t);
814
Harald Weltedb253af2008-12-30 17:56:55 +0000815 ptr8 = msgb_put(msg, 8);
816 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100817 ptr8[1] = bcdify(gmt_time->tm_year % 100);
818 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
819 ptr8[3] = bcdify(gmt_time->tm_mday);
820 ptr8[4] = bcdify(gmt_time->tm_hour);
821 ptr8[5] = bcdify(gmt_time->tm_min);
822 ptr8[6] = bcdify(gmt_time->tm_sec);
823
Harald Welte45f91712012-07-08 16:48:11 +0200824 if (bts->tz.override) {
825 /* Convert tz.hr and tz.mn to units */
826 if (bts->tz.hr < 0) {
827 tzunits = ((bts->tz.hr/-1)*4);
828 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100829 ptr8[7] = bcdify(tzunits);
830 /* Set negative time */
831 ptr8[7] |= 0x08;
832 }
833 else {
Harald Welte45f91712012-07-08 16:48:11 +0200834 tzunits = bts->tz.hr*4;
835 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100836 ptr8[7] = bcdify(tzunits);
837 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100838 /* Convert DST value */
839 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
840 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100841 }
842 else {
843 /* Need to get GSM offset and convert into 15 min units */
844 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
845 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200846#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100847 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200848#else
849#warning find a portable way to obtain the timezone offset
850 tzunits = 0;
851#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100852 if (tzunits < 0) {
853 tzunits = tzunits/-1;
854 ptr8[7] = bcdify(tzunits);
855 /* Flip it to negative */
856 ptr8[7] |= 0x08;
857 }
858 else
859 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100860
861 /* Does not support DST +2 */
862 if (local_time->tm_isdst)
863 dst = 1;
864 }
865
866 if (dst) {
867 ptr8 = msgb_put(msg, 3);
868 ptr8[0] = GSM48_IE_NET_DST;
869 ptr8[1] = 1;
870 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100871 }
Harald Weltedb253af2008-12-30 17:56:55 +0000872
Daniel Willmanneea93372009-08-13 03:42:07 +0200873 DEBUGP(DMM, "-> MM INFO\n");
874
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800875 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000876}
877
Harald Welte7984d5c2009-08-12 22:56:50 +0200878/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200879int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200880{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100881 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200882 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200883 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200884
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200885 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200886
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800887 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200888 gh->proto_discr = GSM48_PDISC_MM;
889 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
890
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100891 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200892
Harald Welte7984d5c2009-08-12 22:56:50 +0200893 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200894 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200895 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200896
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800897 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200898}
899
900/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800901int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200902{
903 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800904 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200905}
906
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100907/*
908 * At the 30C3 phones miss their periodic update
909 * interval a lot and then remain unreachable. In case
910 * we still know the TMSI we can just attach it again.
911 */
912static void implit_attach(struct gsm_subscriber_connection *conn)
913{
914 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
915 return;
916
917 subscr_update(conn->subscr, conn->bts,
918 GSM_SUBSCRIBER_UPDATE_ATTACHED);
919}
920
921
Sylvain Munautba87f452009-12-24 00:28:46 +0100922static int _gsm48_rx_mm_serv_req_sec_cb(
923 unsigned int hooknum, unsigned int event,
924 struct msgb *msg, void *data, void *param)
925{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800926 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100927 int rc = 0;
928
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200929 /* auth failed or succeeded, the timer was stopped */
930 conn->expire_timer_stopped = 1;
931
Sylvain Munautba87f452009-12-24 00:28:46 +0100932 switch (event) {
933 case GSM_SECURITY_AUTH_FAILED:
934 /* Nothing to do */
935 break;
936
937 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200938 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800939 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100940 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100941 break;
942
943 case GSM_SECURITY_SUCCEEDED:
944 /* nothing to do. CIPHER MODE COMMAND is
945 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100946 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100947 break;
948
949 default:
950 rc = -EINVAL;
951 };
952
953 return rc;
954}
955
Harald Welte4ed0e922009-01-10 03:17:30 +0000956/*
957 * Handle CM Service Requests
958 * a) Verify that the packet is long enough to contain the information
959 * we require otherwsie reject with INCORRECT_MESSAGE
960 * b) Try to parse the TMSI. If we do not have one reject
961 * c) Check that we know the subscriber with the TMSI otherwise reject
962 * with a HLR cause
963 * d) Set the subscriber on the gsm_lchan and accept
964 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800965static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000966{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200967 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200968 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000969
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200970 struct gsm_network *network = conn->network;
Harald Welteba4cf162009-01-10 01:49:35 +0000971 struct gsm_subscriber *subscr;
972 struct gsm48_hdr *gh = msgb_l3(msg);
973 struct gsm48_service_request *req =
974 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800975 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200976 uint8_t classmark2_len = gh->data[1];
977 uint8_t *classmark2 = gh->data+2;
978 uint8_t mi_len = *(classmark2 + classmark2_len);
979 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000980
Harald Weltec9e02182009-05-01 19:07:53 +0000981 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000982 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000983 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800984 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000985 GSM48_REJECT_INCORRECT_MESSAGE);
986 }
987
988 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000989 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800990 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000991 GSM48_REJECT_INCORRECT_MESSAGE);
992 }
993
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200994 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000995 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200996
997 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200998 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
999 req->cm_service_type, gsm48_mi_type_name(mi_type),
1000 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001001 subscr = subscr_get_by_imsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001002 mi_string);
1003 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001004 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1005 req->cm_service_type, gsm48_mi_type_name(mi_type),
1006 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001007 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001008 tmsi_from_string(mi_string));
1009 } else {
1010 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001011 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001012 GSM48_REJECT_INCORRECT_MESSAGE);
1013 }
1014
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001015 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001016
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001017 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +02001018 send_siemens_mrpci(msg->lchan, classmark2-1);
1019
Holger Freythereb443982009-06-04 13:58:42 +00001020
Harald Welte2a139372009-02-22 21:14:55 +00001021 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001022 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001023 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001024 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001025
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001026 if (!conn->subscr)
1027 conn->subscr = subscr;
1028 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001029 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1030 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001031 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1032 subscr_put(subscr);
1033 }
1034
Harald Weltec2e302d2009-07-05 14:08:13 +02001035 subscr->equipment.classmark2_len = classmark2_len;
1036 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1037 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001038
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001039 /* we will send a MM message soon */
1040 conn->expire_timer_stopped = 1;
1041
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001042 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001043 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001044}
1045
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001046static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001047{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001048 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +00001049 struct gsm48_hdr *gh = msgb_l3(msg);
1050 struct gsm48_imsi_detach_ind *idi =
1051 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001052 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001053 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001054 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001055
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001056 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001057 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1058 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001059
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001060 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001061
Harald Welte2a139372009-02-22 21:14:55 +00001062 switch (mi_type) {
1063 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001064 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001065 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001066 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001067 break;
1068 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001069 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001070 subscr = subscr_get_by_imsi(network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001071 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001072 break;
1073 case GSM_MI_TYPE_IMEI:
1074 case GSM_MI_TYPE_IMEISV:
1075 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001076 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001077 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001078 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001079 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001080 break;
1081 }
1082
Holger Freyther4a49e772009-04-12 05:37:29 +00001083 if (subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001084 subscr_update(subscr, conn->bts,
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001085 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001086 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001087
1088 subscr->equipment.classmark1 = idi->classmark1;
1089 db_sync_equipment(&subscr->equipment);
1090
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001091 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001092 } else
Harald Welte2a139372009-02-22 21:14:55 +00001093 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1094
Harald Welte31981a02009-12-20 09:58:40 +01001095 /* FIXME: iterate over all transactions and release them,
1096 * imagine an IMSI DETACH happening during an active call! */
1097
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001098 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001099 return 0;
1100}
1101
Harald Welted2a7f5a2009-06-05 20:08:20 +00001102static int gsm48_rx_mm_status(struct msgb *msg)
1103{
1104 struct gsm48_hdr *gh = msgb_l3(msg);
1105
1106 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1107
1108 return 0;
1109}
1110
Sylvain Munaut30a15382009-12-24 00:27:26 +01001111/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001112static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001113{
1114 struct gsm48_hdr *gh = msgb_l3(msg);
1115 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001116 struct gsm_network *net = conn->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001117
Sylvain Munautc593cf12010-06-10 23:51:41 +02001118 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001119 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001120
1121 /* Safety check */
1122 if (!conn->sec_operation) {
1123 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1124 return -EIO;
1125 }
1126
1127 /* Validate SRES */
Harald Welte121e9a42016-04-20 13:13:19 +02001128 if (memcmp(conn->sec_operation->atuple.vec.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001129 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001130 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001131
1132 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001133 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001134
Sylvain Munaut30a15382009-12-24 00:27:26 +01001135 if (cb)
1136 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001137 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001138
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001139 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001140 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001141 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001142 }
1143
Sylvain Munautc593cf12010-06-10 23:51:41 +02001144 DEBUGPC(DMM, "OK\n");
1145
Sylvain Munaut30a15382009-12-24 00:27:26 +01001146 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001147 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001148 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001149}
1150
Harald Weltebf5e8df2009-02-03 12:59:45 +00001151/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001152static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001153{
1154 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001155 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001156
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001157 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001158 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001159 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001160 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001161 break;
1162 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001163 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001164 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001165 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001166 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001167 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001168 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001169 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001170 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001171 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001172 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001173 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001174 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001175 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001176 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001177 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001178 break;
1179 case GSM48_MT_MM_CM_REEST_REQ:
1180 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1181 break;
1182 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001183 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001184 break;
1185 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001186 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001187 gh->msg_type);
1188 break;
1189 }
1190
1191 return rc;
1192}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001193
Harald Welte2d35ae62009-02-06 12:02:13 +00001194/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001195static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001196{
1197 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001198 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001199 uint8_t *classmark2_lv = gh->data + 1;
1200 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001201 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001202 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001203 int rc = 0;
1204
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001205 resp = (struct gsm48_pag_resp *) &gh->data[0];
1206 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1207 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001208 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1209 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001210
Harald Weltefe18d8f2009-02-22 21:14:24 +00001211 switch (mi_type) {
1212 case GSM_MI_TYPE_TMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001213 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001214 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001215 break;
1216 case GSM_MI_TYPE_IMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001217 subscr = subscr_get_by_imsi(conn->network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001218 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001219 break;
1220 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001221
1222 if (!subscr) {
1223 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001224 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001225 return -EINVAL;
1226 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001227 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001228 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001229 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001230
Harald Weltec2e302d2009-07-05 14:08:13 +02001231 subscr->equipment.classmark2_len = *classmark2_lv;
1232 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1233 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001234
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001235 /* We received a paging */
1236 conn->expire_timer_stopped = 1;
1237
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001238 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001239 return rc;
1240}
1241
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001242static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243{
1244 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001245 uint8_t apdu_id_flags;
1246 uint8_t apdu_len;
1247 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248
1249 apdu_id_flags = gh->data[0];
1250 apdu_len = gh->data[1];
1251 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001252
Harald Welte70f92052012-07-16 13:22:19 +02001253 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001254 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001255
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001256 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001257}
1258
Harald Weltebf5e8df2009-02-03 12:59:45 +00001259/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001260static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001261{
1262 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001263 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001264
1265 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001266 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001267 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001268 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001269 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001270 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001271 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001272 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001273 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001274 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001275 break;
1276 }
1277
Harald Welte2d35ae62009-02-06 12:02:13 +00001278 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001279}
1280
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001281int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1282 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001283{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001284 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001285 struct gsm48_hdr *gh;
1286
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001287 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001288
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001289 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1290 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001291
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001292 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1293 gh->proto_discr = GSM48_PDISC_RR;
1294 gh->msg_type = GSM48_MT_RR_APP_INFO;
1295 gh->data[0] = apdu_id;
1296 gh->data[1] = apdu_len;
1297 memcpy(gh->data+2, apdu, apdu_len);
1298
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001299 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001300}
1301
Alexander Couzens92f552f2016-08-23 07:32:27 +02001302/* FIXME: this count_statistics is a state machine behaviour. we should convert
1303 * the complete call control into a state machine. Afterwards we can move this
1304 * code into state transitions.
1305 */
1306static void count_statistics(struct gsm_trans *trans, int new_state)
1307{
1308 int old_state = trans->cc.state;
1309 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1310
1311 if (old_state == new_state)
1312 return;
1313
1314 /* state incoming */
1315 switch (new_state) {
1316 case GSM_CSTATE_ACTIVE:
1317 osmo_counter_inc(trans->net->active_calls);
1318 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1319 break;
1320 }
1321
1322 /* state outgoing */
1323 switch (old_state) {
1324 case GSM_CSTATE_ACTIVE:
1325 osmo_counter_dec(trans->net->active_calls);
1326 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1327 new_state == GSM_CSTATE_DISCONNECT_IND)
1328 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1329 else
1330 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1331 break;
1332 }
1333}
1334
Harald Welte4bc90a12008-12-27 16:32:52 +00001335/* Call Control */
1336
Harald Welte7584aea2009-02-11 11:44:12 +00001337/* The entire call control code is written in accordance with Figure 7.10c
1338 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1339 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1340 * it for voice */
1341
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342static void new_cc_state(struct gsm_trans *trans, int state)
1343{
1344 if (state > 31 || state < 0)
1345 return;
1346
1347 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001348 gsm48_cc_state_name(trans->cc.state),
1349 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350
Alexander Couzens92f552f2016-08-23 07:32:27 +02001351 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001352 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353}
1354
1355static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001356{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001357 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001358 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001359 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001360
Harald Welte4bc90a12008-12-27 16:32:52 +00001361 gh->msg_type = GSM48_MT_CC_STATUS;
1362
1363 cause = msgb_put(msg, 3);
1364 cause[0] = 2;
1365 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1366 cause[2] = 0x80 | 30; /* response to status inquiry */
1367
1368 call_state = msgb_put(msg, 1);
1369 call_state[0] = 0xc0 | 0x00;
1370
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001371 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001372}
1373
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001374static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001375 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001376{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001377 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1379
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001380 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001381
Harald Welte6f4b7532008-12-29 00:39:37 +00001382 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001383 gh->msg_type = msg_type;
1384
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001385 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001386}
1387
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1389{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001390 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001392 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001393 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 }
1395}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001396
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1398 int msg_type, struct gsm_mncc *mncc)
1399{
1400 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001401 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402
1403 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001404 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001405 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001407 trans->conn->lchan->ts->trx->bts->nr,
1408 trans->conn->lchan->ts->trx->nr,
1409 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 (trans->subscr)?(trans->subscr->extension):"-",
1411 get_mncc_name(msg_type));
1412 else
1413 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1414 "Sending '%s' to MNCC.\n",
1415 (trans->subscr)?(trans->subscr->extension):"-",
1416 get_mncc_name(msg_type));
1417 else
1418 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1419 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1420
1421 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001422
Harald Welte966636f2009-06-26 19:39:35 +02001423 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 if (!msg)
1425 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001426
1427 data = msgb_put(msg, sizeof(struct gsm_mncc));
1428 memcpy(data, mncc, sizeof(struct gsm_mncc));
1429
Harald Welte54209c22010-12-22 23:43:29 +01001430 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431
1432 return 0;
1433}
1434
1435int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001436 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001437{
1438 struct gsm_mncc rel;
1439
Harald Welte92f70c52009-06-12 01:54:08 +08001440 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001442 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001443 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1444 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1446}
1447
Harald Weltedcaf5652009-07-23 18:56:43 +02001448/* Call Control Specific transaction release.
1449 * gets called by trans_free, DO NOT CALL YOURSELF! */
1450void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001451{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 gsm48_stop_cc_timer(trans);
1453
1454 /* send release to L4, if callref still exists */
1455 if (trans->callref) {
1456 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001457 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001458 GSM48_CAUSE_LOC_PRN_S_LU,
1459 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001460 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001461 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001463 if (trans->conn)
1464 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001465}
1466
1467static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001468
Harald Welte09e38af2009-02-16 22:52:23 +00001469/* call-back from paging the B-end of the connection */
1470static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001471 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001472{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001473 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001474 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001475
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001476 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001477
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001479 switch (event) {
1480 case GSM_PAGING_SUCCEEDED:
1481 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001482 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001483 /* Assign lchan */
1484 transt->conn = conn;
1485 /* send SETUP request to called party */
1486 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1487 break;
1488 case GSM_PAGING_EXPIRED:
1489 case GSM_PAGING_BUSY:
1490 DEBUGP(DCC, "Paging subscr %s expired!\n",
1491 transt->subscr->extension);
1492 /* Temporarily out of order */
1493 mncc_release_ind(transt->net, transt,
1494 transt->callref,
1495 GSM48_CAUSE_LOC_PRN_S_LU,
1496 GSM48_CC_CAUSE_DEST_OOO);
1497 transt->callref = 0;
1498 transt->paging_request = NULL;
1499 trans_free(transt);
1500 break;
1501 default:
1502 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1503 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001505
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001506 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001507 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001508}
Harald Welte7584aea2009-02-11 11:44:12 +00001509
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001510static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001511
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001512/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001513static int switch_for_handover(struct gsm_lchan *old_lchan,
1514 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001515{
1516 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001517
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001518 /* Ask the new socket to send to the already known port. */
1519 if (new_lchan->conn->mncc_rtp_bridge) {
1520 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1521 rsl_ipacc_mdcx(new_lchan,
1522 old_lchan->abis_ip.connect_ip,
1523 old_lchan->abis_ip.connect_port, 0);
1524 return 0;
1525 }
1526
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001527 if (ipacc_rtp_direct) {
1528 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1529 return 0;
1530 }
1531
1532 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001533 new_rs = new_lchan->abis_ip.rtp_socket;
1534 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001535
1536 if (!new_rs) {
1537 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1538 return -EIO;
1539 }
1540
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001541 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001542
1543 if (!old_rs) {
1544 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1545 return -EIO;
1546 }
1547
1548 /* copy rx_action and reference to other sock */
1549 new_rs->rx_action = old_rs->rx_action;
1550 new_rs->tx_action = old_rs->tx_action;
1551 new_rs->transmit = old_rs->transmit;
1552
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001553 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001554 case RTP_PROXY:
1555 other_rs = old_rs->proxy.other_sock;
1556 rtp_socket_proxy(new_rs, other_rs);
1557 /* delete reference to other end socket to prevent
1558 * rtp_socket_free() from removing the inverse reference */
1559 old_rs->proxy.other_sock = NULL;
1560 break;
1561 case RTP_RECV_UPSTREAM:
1562 new_rs->receive = old_rs->receive;
1563 break;
1564 case RTP_NONE:
1565 break;
1566 }
1567
1568 return 0;
1569}
1570
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001571static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1572{
1573 struct gsm_lchan *old_lchan;
1574 old_lchan = bsc_handover_pending(lchan);
1575 if (old_lchan)
1576 switch_for_handover(old_lchan, lchan);
1577}
1578
Harald Welte805f6442009-07-28 18:25:29 +02001579/* some other part of the code sends us a signal */
1580static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1581 void *handler_data, void *signal_data)
1582{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001583 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001584 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001585 struct gsm_network *net;
1586 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001587
1588 if (subsys != SS_ABISIP)
1589 return 0;
1590
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001591 /* RTP bridge handling */
1592 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1593 return tch_rtp_signal(lchan, signal);
1594
Harald Welte805f6442009-07-28 18:25:29 +02001595 /* in case we use direct BTS-to-BTS RTP */
1596 if (ipacc_rtp_direct)
1597 return 0;
1598
Harald Welte805f6442009-07-28 18:25:29 +02001599 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001600 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001601 /* in case we don't use direct BTS-to-BTS RTP */
1602 /* the BTS has successfully bound a TCH to a local ip/port,
1603 * which means we can connect our UDP socket to it */
1604 if (lchan->abis_ip.rtp_socket) {
1605 rtp_socket_free(lchan->abis_ip.rtp_socket);
1606 lchan->abis_ip.rtp_socket = NULL;
1607 }
1608
1609 lchan->abis_ip.rtp_socket = rtp_socket_create();
1610 if (!lchan->abis_ip.rtp_socket)
1611 return -EIO;
1612
1613 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1614 lchan->abis_ip.bound_ip,
1615 lchan->abis_ip.bound_port);
1616 if (rc < 0)
1617 return -EIO;
1618
Harald Welteda7ab742009-12-19 22:23:05 +01001619 /* check if any transactions on this lchan still have
1620 * a tch_recv_mncc request pending */
1621 net = lchan->ts->trx->bts->network;
1622 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001623 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001624 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1625 tch_recv_mncc(net, trans->callref, 1);
1626 }
1627 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001628
1629 /*
1630 * TODO: this appears to be too early? Why not until after
1631 * the handover detect or the handover complete?
1632 *
1633 * Do we have a handover pending for this new lchan? In that
1634 * case re-route the audio from the old channel to the new one.
1635 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001636 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001637 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001638 case S_ABISIP_DLCX_IND:
1639 /* the BTS tells us a RTP stream has been disconnected */
1640 if (lchan->abis_ip.rtp_socket) {
1641 rtp_socket_free(lchan->abis_ip.rtp_socket);
1642 lchan->abis_ip.rtp_socket = NULL;
1643 }
1644
1645 break;
Harald Welte805f6442009-07-28 18:25:29 +02001646 }
1647
1648 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001649}
1650
Harald Welte49f48b82009-02-17 15:29:33 +00001651/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001652static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001653{
Harald Welte11fa29c2009-02-19 17:24:39 +00001654 struct gsm_bts *bts = lchan->ts->trx->bts;
1655 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001656 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001657 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001658 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001659
Max8db12e42016-04-18 23:11:18 +02001660 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1661 "(bts=%u,trx=%u,ts=%u,%s)\n",
1662 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1663 get_value_string(gsm_chan_t_names, lt),
1664 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1665 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001666
1667 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001668 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001669 return -EINVAL;
1670 }
Harald Welteda7ab742009-12-19 22:23:05 +01001671
Max3ffce192016-04-25 15:22:00 +02001672 if (lt != rt) {
1673 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1674 " channel types: local = %s, remote = %s\n",
1675 get_value_string(gsm_chan_t_names, lt),
1676 get_value_string(gsm_chan_t_names, rt));
1677 return -EBADSLT;
1678 }
1679
1680 if (lm != rm) {
1681 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1682 " channel modes: local = %s, remote = %s\n",
1683 get_value_string(gsm48_chan_mode_names, lm),
1684 get_value_string(gsm48_chan_mode_names, rm));
1685 return -EMEDIUMTYPE;
1686 }
1687
Harald Welteda7ab742009-12-19 22:23:05 +01001688 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001689 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001690 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001691 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001692 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001693 if (!lchan->abis_ip.rtp_socket) {
1694 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1695 "lchan\n");
1696 return -EIO;
1697 }
1698 if (!remote_lchan->abis_ip.rtp_socket) {
1699 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1700 "remote_lchan\n");
1701 return -EIO;
1702 }
1703
Harald Welte805f6442009-07-28 18:25:29 +02001704 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001705 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001706 if (rc < 0)
1707 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001708 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001709 if (rc < 0)
1710 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001711 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301712 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1713 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001714 } else {
1715 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301716 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1717 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301718 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001719 if (rc < 0)
1720 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301721 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1722 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301723 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001724 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001725 break;
1726 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001727 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001728 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001729 trau_mux_map_lchan(lchan, remote_lchan);
1730 break;
1731 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001732 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001733 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001734 }
Harald Welte49f48b82009-02-17 15:29:33 +00001735
1736 return 0;
1737}
1738
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001740static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001741{
Harald Welte53d51f52015-12-03 14:59:04 +01001742 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1743 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001744
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001746 return -EIO;
1747
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001748 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 return -EIO;
1750
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001751 /* Which subscriber do we want to track trans1 or trans2? */
1752 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1753
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001755 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001756}
1757
Harald Welteda7ab742009-12-19 22:23:05 +01001758/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001759static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760{
1761 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001762 struct gsm_lchan *lchan;
1763 struct gsm_bts *bts;
1764 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001765
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001767 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 if (!trans)
1769 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001770 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001772
1773 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001774 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001775 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001777 /* store receive state */
1778 trans->tch_recv = enable;
1779
Harald Welteda7ab742009-12-19 22:23:05 +01001780 switch (bts->type) {
1781 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001782 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001783 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001784 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001785 return -EINVAL;
1786 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001787 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1788 * will not be connected to our RTP proxy and the socket will
1789 * not be assigned to the application interface. This method
1790 * will be called again, once the audio socket is created and
1791 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001792 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001793 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1794 return 0;
1795 }
1796 if (enable) {
1797 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001798 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001799 if (rc < 0)
1800 return rc;
1801 /* assign socket to application interface */
1802 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1803 net, callref);
1804 } else
1805 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1806 net, 0);
1807 break;
1808 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001809 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001810 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001811 /* In case we don't have a TCH with correct mode, the TRAU muxer
1812 * will not be asigned to the application interface. This is
1813 * performed by switch_trau_mux() after successful handover or
1814 * assignment. */
1815 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1816 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1817 return 0;
1818 }
Harald Welteda7ab742009-12-19 22:23:05 +01001819 if (enable)
1820 return trau_recv_lchan(lchan, callref);
1821 return trau_mux_unmap(NULL, callref);
1822 break;
1823 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001824 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001825 return -EINVAL;
1826 }
1827
1828 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829}
1830
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1832{
1833 DEBUGP(DCC, "-> STATUS ENQ\n");
1834 return gsm48_cc_tx_status(trans, msg);
1835}
1836
1837static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1838static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1839
1840static void gsm48_cc_timeout(void *arg)
1841{
1842 struct gsm_trans *trans = arg;
1843 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001844 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1845 int mo_location = GSM48_CAUSE_LOC_USER;
1846 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1847 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 struct gsm_mncc mo_rel, l4_rel;
1849
1850 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1851 mo_rel.callref = trans->callref;
1852 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1853 l4_rel.callref = trans->callref;
1854
Harald Weltedcaf5652009-07-23 18:56:43 +02001855 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 case 0x303:
1857 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001858 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 break;
1860 case 0x310:
1861 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001862 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 break;
1864 case 0x313:
1865 disconnect = 1;
1866 /* unknown, did not find it in the specs */
1867 break;
1868 case 0x301:
1869 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001870 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 break;
1872 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001873 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001875 gsm48_cc_tx_release(trans, &trans->cc.msg);
1876 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 break; /* stay in release state */
1878 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001879 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 return;
1881// release = 1;
1882// l4_cause = 14;
1883// break;
1884 case 0x306:
1885 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001886 mo_cause = trans->cc.msg.cause.value;
1887 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 break;
1889 case 0x323:
1890 disconnect = 1;
1891 break;
1892 default:
1893 release = 1;
1894 }
1895
1896 if (release && trans->callref) {
1897 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001898 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 l4_location, l4_cause);
1900 trans->callref = 0;
1901 }
1902
1903 if (disconnect && trans->callref) {
1904 /* process disconnect towards layer 4 */
1905 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001906 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 }
1908
1909 /* process disconnect towards mobile station */
1910 if (disconnect || release) {
1911 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001912 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1913 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1914 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 mo_rel.cause.diag_len = 3;
1916
1917 if (disconnect)
1918 gsm48_cc_tx_disconnect(trans, &mo_rel);
1919 if (release)
1920 gsm48_cc_tx_release(trans, &mo_rel);
1921 }
1922
1923}
1924
Max3ffce192016-04-25 15:22:00 +02001925/* disconnect both calls from the bridge */
1926static inline void disconnect_bridge(struct gsm_network *net,
1927 struct gsm_mncc_bridge *bridge, int err)
1928{
1929 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1930 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1931 struct gsm_mncc mx_rel;
1932 if (!trans0 || !trans1)
1933 return;
1934
1935 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1936 trans0->callref, trans1->callref, strerror(err));
1937
1938 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1939 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1940 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1941
1942 mx_rel.callref = trans0->callref;
1943 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1944
1945 mx_rel.callref = trans1->callref;
1946 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1947}
1948
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1950 int sec, int micro)
1951{
1952 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001953 trans->cc.timer.cb = gsm48_cc_timeout;
1954 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001955 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001956 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957}
1958
1959static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1960{
1961 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001962 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1964 struct tlv_parsed tp;
1965 struct gsm_mncc setup;
1966
1967 memset(&setup, 0, sizeof(struct gsm_mncc));
1968 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001969 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001970 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* emergency setup is identified by msg_type */
1972 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1973 setup.emergency = 1;
1974
1975 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001976 setup.fields |= MNCC_F_CALLING;
1977 strncpy(setup.calling.number, trans->subscr->extension,
1978 sizeof(setup.calling.number)-1);
1979 strncpy(setup.imsi, trans->subscr->imsi,
1980 sizeof(setup.imsi)-1);
1981
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 /* bearer capability */
1983 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1984 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001987 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 }
1989 /* facility */
1990 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1991 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001992 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1994 }
1995 /* called party bcd number */
1996 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1997 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2000 }
2001 /* user-user */
2002 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2003 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2006 }
2007 /* ss-version */
2008 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2009 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002010 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2012 }
2013 /* CLIR suppression */
2014 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2015 setup.clir.sup = 1;
2016 /* CLIR invocation */
2017 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2018 setup.clir.inv = 1;
2019 /* cc cap */
2020 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2021 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002022 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2024 }
2025
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 new_cc_state(trans, GSM_CSTATE_INITIATED);
2027
Harald Welte (local)47df3992009-12-26 19:45:03 +01002028 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2029 subscr_name(trans->subscr), trans->subscr->extension,
2030 setup.called.number);
2031
Alexander Couzensb847a212016-08-02 11:34:11 +02002032 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002033
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002035 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036
Harald Welte13cac662009-07-29 12:10:35 +02002037 /* MNCC code will modify the channel asynchronously, we should
2038 * ipaccess-bind only after the modification has been made to the
2039 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 return 0;
2041}
2042
2043static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002044{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002045 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002046 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002048 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002049
Harald Welte7ccf7782009-02-17 01:43:01 +00002050 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002051
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* transaction id must not be assigned */
2053 if (trans->transaction_id != 0xff) { /* unasssigned */
2054 DEBUGP(DCC, "TX Setup with assigned transaction. "
2055 "This is not allowed!\n");
2056 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002057 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002058 GSM48_CAUSE_LOC_PRN_S_LU,
2059 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002061 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 return rc;
2063 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002064
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002066 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2067 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002068 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002070 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002071 GSM48_CAUSE_LOC_PRN_S_LU,
2072 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002074 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 return rc;
2076 }
Harald Welte78283ef2009-07-23 21:36:44 +02002077 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002078
Harald Welte65e74cc2008-12-29 01:55:35 +00002079 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002080
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002082
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* bearer capability */
2084 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002085 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 /* facility */
2087 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002088 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 /* progress */
2090 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002091 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 /* calling party BCD number */
2093 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* called party BCD number */
2096 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 /* user-user */
2099 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002100 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 /* redirecting party BCD number */
2102 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002103 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 /* signal */
2105 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002106 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002107
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002109
Alexander Couzensb847a212016-08-02 11:34:11 +02002110 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002111
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002112 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002113}
2114
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2116{
2117 struct gsm48_hdr *gh = msgb_l3(msg);
2118 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2119 struct tlv_parsed tp;
2120 struct gsm_mncc call_conf;
2121
2122 gsm48_stop_cc_timer(trans);
2123 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2124
2125 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2126 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002127 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002128 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129#if 0
2130 /* repeat */
2131 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2132 call_conf.repeat = 1;
2133 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2134 call_conf.repeat = 2;
2135#endif
2136 /* bearer capability */
2137 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2138 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002141 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 }
2143 /* cause */
2144 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2145 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002146 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2148 }
2149 /* cc cap */
2150 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2151 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2154 }
2155
Andreas Eversberg035b8742013-09-19 09:27:06 +02002156 /* IMSI of called subscriber */
2157 strncpy(call_conf.imsi, trans->subscr->imsi,
2158 sizeof(call_conf.imsi)-1);
2159
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2161
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002162 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002163 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164}
2165
2166static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2167{
2168 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002169 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2171
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2173
2174 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2175
2176 /* bearer capability */
2177 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 /* facility */
2180 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 /* progress */
2183 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002184 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002186 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187}
2188
2189static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2190{
2191 struct gsm48_hdr *gh = msgb_l3(msg);
2192 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2193 struct tlv_parsed tp;
2194 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002195
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 gsm48_stop_cc_timer(trans);
2197 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2198
2199 memset(&alerting, 0, sizeof(struct gsm_mncc));
2200 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002201 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202 /* facility */
2203 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2204 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002205 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2207 }
2208
2209 /* progress */
2210 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2211 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002212 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2214 }
2215 /* ss-version */
2216 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2217 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002218 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2220 }
2221
2222 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2223
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002224 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002225 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226}
2227
2228static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2229{
2230 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002231 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2233
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 gh->msg_type = GSM48_MT_CC_ALERTING;
2235
2236 /* facility */
2237 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002238 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 /* progress */
2240 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002241 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 /* user-user */
2243 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002244 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245
2246 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002247
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002248 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249}
2250
2251static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2252{
2253 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002254 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2256
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 gh->msg_type = GSM48_MT_CC_PROGRESS;
2258
2259 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 /* user-user */
2262 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002263 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002265 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266}
2267
2268static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2269{
2270 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002271 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2273
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 gh->msg_type = GSM48_MT_CC_CONNECT;
2275
2276 gsm48_stop_cc_timer(trans);
2277 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2278
2279 /* facility */
2280 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002281 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002282 /* progress */
2283 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 /* connected number */
2286 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002287 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 /* user-user */
2289 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291
2292 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2293
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002294 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295}
2296
2297static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2298{
2299 struct gsm48_hdr *gh = msgb_l3(msg);
2300 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2301 struct tlv_parsed tp;
2302 struct gsm_mncc connect;
2303
2304 gsm48_stop_cc_timer(trans);
2305
2306 memset(&connect, 0, sizeof(struct gsm_mncc));
2307 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002308 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002310 connect.fields |= MNCC_F_CONNECTED;
2311 strncpy(connect.connected.number, trans->subscr->extension,
2312 sizeof(connect.connected.number)-1);
2313 strncpy(connect.imsi, trans->subscr->imsi,
2314 sizeof(connect.imsi)-1);
2315
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 /* facility */
2317 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2318 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002319 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2321 }
2322 /* user-user */
2323 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2324 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002325 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2327 }
2328 /* ss-version */
2329 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2330 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002331 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2333 }
2334
2335 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002336 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002338 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339}
2340
2341
2342static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2343{
2344 struct gsm_mncc connect_ack;
2345
2346 gsm48_stop_cc_timer(trans);
2347
2348 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002349 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002350
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2352 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002353
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002354 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 &connect_ack);
2356}
2357
2358static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2359{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002360 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2362
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2364
2365 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2366
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002367 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368}
2369
2370static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2371{
2372 struct gsm48_hdr *gh = msgb_l3(msg);
2373 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2374 struct tlv_parsed tp;
2375 struct gsm_mncc disc;
2376
2377 gsm48_stop_cc_timer(trans);
2378
2379 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2380
2381 memset(&disc, 0, sizeof(struct gsm_mncc));
2382 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002383 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 /* cause */
2385 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2386 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2389 }
2390 /* facility */
2391 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2392 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002393 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2395 }
2396 /* user-user */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2398 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2401 }
2402 /* ss-version */
2403 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2404 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2407 }
2408
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002409 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410
2411}
2412
Harald Weltec66b71c2009-06-11 14:23:20 +08002413static struct gsm_mncc_cause default_cause = {
2414 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2415 .coding = 0,
2416 .rec = 0,
2417 .rec_val = 0,
2418 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2419 .diag_len = 0,
2420 .diag = { 0 },
2421};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422
2423static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2424{
2425 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002426 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2428
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2430
2431 gsm48_stop_cc_timer(trans);
2432 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2433
2434 /* cause */
2435 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002436 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 else
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
2440 /* facility */
2441 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 /* progress */
2444 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002445 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 /* user-user */
2447 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002448 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449
2450 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002451 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452
2453 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2454
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002455 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456}
2457
2458static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2459{
2460 struct gsm48_hdr *gh = msgb_l3(msg);
2461 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2462 struct tlv_parsed tp;
2463 struct gsm_mncc rel;
2464 int rc;
2465
2466 gsm48_stop_cc_timer(trans);
2467
2468 memset(&rel, 0, sizeof(struct gsm_mncc));
2469 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002470 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 /* cause */
2472 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2473 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002474 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2476 }
2477 /* facility */
2478 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2479 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002480 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2482 }
2483 /* user-user */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2485 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2488 }
2489 /* ss-version */
2490 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2491 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2494 }
2495
Harald Weltedcaf5652009-07-23 18:56:43 +02002496 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002498 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002500 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002501 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002502 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002503 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 }
2505
2506 new_cc_state(trans, GSM_CSTATE_NULL);
2507
2508 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002509 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510
2511 return rc;
2512}
2513
2514static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2515{
2516 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2519
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 gh->msg_type = GSM48_MT_CC_RELEASE;
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gsm48_stop_cc_timer(trans);
2523 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2524
2525 /* cause */
2526 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002527 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 /* facility */
2529 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002530 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531 /* user-user */
2532 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002533 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534
Harald Weltedcaf5652009-07-23 18:56:43 +02002535 trans->cc.T308_second = 0;
2536 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537
Harald Weltedcaf5652009-07-23 18:56:43 +02002538 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2540
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002541 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542}
2543
2544static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2545{
2546 struct gsm48_hdr *gh = msgb_l3(msg);
2547 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2548 struct tlv_parsed tp;
2549 struct gsm_mncc rel;
2550 int rc = 0;
2551
2552 gsm48_stop_cc_timer(trans);
2553
2554 memset(&rel, 0, sizeof(struct gsm_mncc));
2555 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002556 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 /* cause */
2558 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2559 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002560 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2562 }
2563 /* facility */
2564 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2565 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2568 }
2569 /* user-user */
2570 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2571 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002572 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2574 }
2575 /* ss-version */
2576 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2577 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2580 }
2581
2582 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002583 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002585 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 MNCC_REJ_IND, &rel);
2587 break;
2588 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002589 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 MNCC_REL_CNF, &rel);
2591 break;
2592 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002593 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 MNCC_REL_IND, &rel);
2595 }
2596 }
2597
2598 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002599 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600
2601 return rc;
2602}
2603
2604static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2605{
2606 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002607 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002609 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2612
2613 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002614
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 gsm48_stop_cc_timer(trans);
2616
2617 /* cause */
2618 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002619 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 /* facility */
2621 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002622 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 /* user-user */
2624 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002625 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626
Harald Weltec7782de2010-12-21 19:31:41 +01002627 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2628
Harald Weltedcaf5652009-07-23 18:56:43 +02002629 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630
Harald Weltec7782de2010-12-21 19:31:41 +01002631 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632}
2633
2634static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2635{
2636 struct gsm48_hdr *gh = msgb_l3(msg);
2637 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2638 struct tlv_parsed tp;
2639 struct gsm_mncc fac;
2640
2641 memset(&fac, 0, sizeof(struct gsm_mncc));
2642 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002643 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644 /* facility */
2645 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2646 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002647 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2649 }
2650 /* ss-version */
2651 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2652 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002653 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2655 }
2656
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002657 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658}
2659
2660static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2661{
2662 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002663 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2665
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 gh->msg_type = GSM48_MT_CC_FACILITY;
2667
2668 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002669 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002671 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672}
2673
2674static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2675{
2676 struct gsm_mncc hold;
2677
2678 memset(&hold, 0, sizeof(struct gsm_mncc));
2679 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002680 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681}
2682
2683static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2684{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002685 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002686 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2687
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2689
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002690 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691}
2692
2693static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2694{
2695 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002696 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2698
Harald Welte4bfdfe72009-06-10 23:11:52 +08002699 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2700
2701 /* cause */
2702 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002703 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002704 else
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002707 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708}
2709
2710static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2711{
2712 struct gsm_mncc retrieve;
2713
2714 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2715 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002716 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002717 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2721{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002722 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2724
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2726
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002727 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728}
2729
2730static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2731{
2732 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002733 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2735
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2737
2738 /* cause */
2739 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002740 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741 else
Harald Welte55c8f352010-03-07 23:40:35 +01002742 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002744 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745}
2746
2747static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2748{
2749 struct gsm48_hdr *gh = msgb_l3(msg);
2750 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2751 struct tlv_parsed tp;
2752 struct gsm_mncc dtmf;
2753
2754 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2755 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002756 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 /* keypad facility */
2758 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2759 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002760 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2762 }
2763
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002764 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765}
2766
2767static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2768{
2769 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002770 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002771 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2772
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2774
2775 /* keypad */
2776 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002777 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002779 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780}
2781
2782static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2783{
2784 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002785 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2787
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2789
2790 /* cause */
2791 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002792 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 else
Harald Welte55c8f352010-03-07 23:40:35 +01002794 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002796 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797}
2798
2799static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2800{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002801 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2803
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
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_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2810{
2811 struct gsm_mncc dtmf;
2812
2813 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2814 dtmf.callref = trans->callref;
2815
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002816 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817}
2818
2819static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2820{
2821 struct gsm48_hdr *gh = msgb_l3(msg);
2822 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2823 struct tlv_parsed tp;
2824 struct gsm_mncc modify;
2825
2826 memset(&modify, 0, sizeof(struct gsm_mncc));
2827 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002828 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829 /* bearer capability */
2830 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2831 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002832 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002834 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 }
2836
2837 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2838
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002839 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840}
2841
2842static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2843{
2844 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002845 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2847
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848 gh->msg_type = GSM48_MT_CC_MODIFY;
2849
2850 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2851
2852 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002853 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854
2855 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2856
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002857 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858}
2859
2860static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2861{
2862 struct gsm48_hdr *gh = msgb_l3(msg);
2863 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2864 struct tlv_parsed tp;
2865 struct gsm_mncc modify;
2866
2867 gsm48_stop_cc_timer(trans);
2868
2869 memset(&modify, 0, sizeof(struct gsm_mncc));
2870 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002871 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872 /* bearer capability */
2873 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2874 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002875 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002877 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878 }
2879
2880 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2881
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002882 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883}
2884
2885static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2886{
2887 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002888 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2890
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2892
2893 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002894 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895
2896 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2897
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002898 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899}
2900
2901static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2902{
2903 struct gsm48_hdr *gh = msgb_l3(msg);
2904 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2905 struct tlv_parsed tp;
2906 struct gsm_mncc modify;
2907
2908 gsm48_stop_cc_timer(trans);
2909
2910 memset(&modify, 0, sizeof(struct gsm_mncc));
2911 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002912 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 /* bearer capability */
2914 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2915 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002916 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002918 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919 }
2920 /* cause */
2921 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2922 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002923 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2925 }
2926
2927 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2928
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002929 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930}
2931
2932static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2933{
2934 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002935 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2937
Harald Welte4bfdfe72009-06-10 23:11:52 +08002938 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2939
2940 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002941 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002943 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944
2945 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2946
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002947 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948}
2949
2950static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2951{
2952 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002953 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2955
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 gh->msg_type = GSM48_MT_CC_NOTIFY;
2957
2958 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002959 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002961 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962}
2963
2964static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2965{
2966 struct gsm48_hdr *gh = msgb_l3(msg);
2967 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2968// struct tlv_parsed tp;
2969 struct gsm_mncc notify;
2970
2971 memset(&notify, 0, sizeof(struct gsm_mncc));
2972 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002973// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002975 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002977 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978}
2979
2980static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2981{
2982 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002983 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2985
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 gh->msg_type = GSM48_MT_CC_USER_INFO;
2987
2988 /* user-user */
2989 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002990 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991 /* more data */
2992 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002993 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002995 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996}
2997
2998static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2999{
3000 struct gsm48_hdr *gh = msgb_l3(msg);
3001 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3002 struct tlv_parsed tp;
3003 struct gsm_mncc user;
3004
3005 memset(&user, 0, sizeof(struct gsm_mncc));
3006 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003007 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 /* user-user */
3009 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3010 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01003011 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3013 }
3014 /* more data */
3015 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3016 user.more = 1;
3017
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003018 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003019}
3020
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003021static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022{
3023 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003024 struct gsm_lchan *lchan = trans->conn->lchan;
3025
3026 /*
3027 * We were forced to make an assignment a lot earlier and
3028 * we should avoid sending another assignment that might
3029 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3030 * In case of rtp-bridge it is too late to change things
3031 * here.
3032 */
3033 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3034 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003036 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3037 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038}
3039
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003040static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3041 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3042 uint32_t payload_msg_type)
3043{
3044 uint8_t data[sizeof(struct gsm_mncc)];
3045 struct gsm_mncc_rtp *rtp;
3046
3047 memset(&data, 0, sizeof(data));
3048 rtp = (struct gsm_mncc_rtp *) &data[0];
3049
3050 rtp->callref = callref;
3051 rtp->msg_type = cmd;
3052 rtp->ip = addr;
3053 rtp->port = port;
3054 rtp->payload_type = payload_type;
3055 rtp->payload_msg_type = payload_msg_type;
3056 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3057}
3058
3059static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3060{
3061 struct gsm_lchan *lchan;
3062 int msg_type;
3063
3064 lchan = trans->conn->lchan;
3065 switch (lchan->abis_ip.rtp_payload) {
3066 case RTP_PT_GSM_FULL:
3067 msg_type = GSM_TCHF_FRAME;
3068 break;
3069 case RTP_PT_GSM_EFR:
3070 msg_type = GSM_TCHF_FRAME_EFR;
3071 break;
3072 case RTP_PT_GSM_HALF:
3073 msg_type = GSM_TCHH_FRAME;
3074 break;
3075 case RTP_PT_AMR:
3076 msg_type = GSM_TCH_FRAME_AMR;
3077 break;
3078 default:
3079 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3080 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3081 msg_type = 0;
3082 break;
3083 }
3084
3085 return mncc_recv_rtp(net, trans->callref, cmd,
3086 lchan->abis_ip.bound_ip,
3087 lchan->abis_ip.bound_port,
3088 lchan->abis_ip.rtp_payload,
3089 msg_type);
3090}
3091
3092static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3093{
3094 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3095}
3096
3097static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3098{
3099 struct gsm_bts *bts;
3100 struct gsm_lchan *lchan;
3101 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003102 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003103
3104 /* Find callref */
3105 trans = trans_find_by_callref(net, callref);
3106 if (!trans) {
3107 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3108 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3109 return -EIO;
3110 }
3111 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3112 if (!trans->conn) {
3113 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3114 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3115 return 0;
3116 }
3117
3118 lchan = trans->conn->lchan;
3119 bts = lchan->ts->trx->bts;
3120 if (!is_ipaccess_bts(bts)) {
3121 /*
3122 * I want this to be straight forward and have no audio flow
3123 * through the nitb/osmo-mss system. This currently means that
3124 * this will not work with BS11/Nokia type BTS. We would need
3125 * to have a trau<->rtp bridge for these but still preferable
3126 * in another process.
3127 */
3128 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3129 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3130 return -EINVAL;
3131 }
3132
3133 trans->conn->mncc_rtp_bridge = 1;
3134 /*
3135 * *sigh* we need to pick a codec now. Pick the most generic one
3136 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003137 * similiar to the routine above.
3138 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003139 */
3140 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3141 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003142 m = mncc_codec_for_mode(lchan->type);
3143 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3144 get_value_string(gsm48_chan_mode_names, m),
3145 get_value_string(gsm_chan_t_names, lchan->type));
3146 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003147 lchan->type != GSM_LCHAN_TCH_H);
3148 }
3149
3150 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3151 return 0;
3152}
3153
3154static int tch_rtp_connect(struct gsm_network *net, void *arg)
3155{
3156 struct gsm_lchan *lchan;
3157 struct gsm_trans *trans;
3158 struct gsm_mncc_rtp *rtp = arg;
3159
3160 /* Find callref */
3161 trans = trans_find_by_callref(net, rtp->callref);
3162 if (!trans) {
3163 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3164 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3165 return -EIO;
3166 }
3167 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3168 if (!trans->conn) {
3169 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3170 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3171 return 0;
3172 }
3173
3174 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003175 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3176 get_value_string(gsm48_chan_mode_names,
3177 mncc_codec_for_mode(lchan->type)),
3178 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003179
3180 /* TODO: Check if payload_msg_type is compatible with what we have */
3181 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3182 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3183 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3184 }
3185
3186 /*
3187 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3188 * complains about both rtp and rtp payload2 being present in the
3189 * same package!
3190 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003191 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003192 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3193}
3194
3195static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3196{
3197 struct gsm_network *net;
3198 struct gsm_trans *tmp, *trans = NULL;
3199
3200 net = lchan->ts->trx->bts->network;
3201 llist_for_each_entry(tmp, &net->trans_list, entry) {
3202 if (!tmp->conn)
3203 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003204 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003205 continue;
3206 trans = tmp;
3207 break;
3208 }
3209
3210 if (!trans) {
3211 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3212 gsm_lchan_name(lchan));
3213 return 0;
3214 }
3215
3216 switch (signal) {
3217 case S_ABISIP_CRCX_ACK:
3218 if (lchan->conn->mncc_rtp_create_pending) {
3219 lchan->conn->mncc_rtp_create_pending = 0;
3220 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3221 gsm_lchan_name(lchan));
3222 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3223 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003224 /*
3225 * TODO: this appears to be too early? Why not until after
3226 * the handover detect or the handover complete?
3227 */
3228 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003229 break;
3230 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003231 if (lchan->conn->mncc_rtp_connect_pending) {
3232 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003233 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3234 gsm_lchan_name(lchan));
3235 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3236 }
3237 break;
3238 }
3239
3240 return 0;
3241}
3242
3243
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003245 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003246 int type;
3247 int (*rout) (struct gsm_trans *trans, void *arg);
3248} downstatelist[] = {
3249 /* mobile originating call establishment */
3250 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3251 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3252 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3253 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3254 {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 */
3255 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3256 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3257 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3258 /* mobile terminating call establishment */
3259 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3260 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3261 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3262 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3263 /* signalling during call */
3264 {SBIT(GSM_CSTATE_ACTIVE),
3265 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3266 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3267 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3268 {ALL_STATES,
3269 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3270 {ALL_STATES,
3271 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3272 {ALL_STATES,
3273 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3274 {SBIT(GSM_CSTATE_ACTIVE),
3275 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3276 {SBIT(GSM_CSTATE_ACTIVE),
3277 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3278 {SBIT(GSM_CSTATE_ACTIVE),
3279 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3280 {SBIT(GSM_CSTATE_ACTIVE),
3281 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3282 {SBIT(GSM_CSTATE_ACTIVE),
3283 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3284 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3285 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3286 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3287 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3288 {SBIT(GSM_CSTATE_ACTIVE),
3289 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3290 /* clearing */
3291 {SBIT(GSM_CSTATE_INITIATED),
3292 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3293 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3294 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3295 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3296 MNCC_REL_REQ, gsm48_cc_tx_release},
3297 /* special */
3298 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003299 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003300};
3301
3302#define DOWNSLLEN \
3303 (sizeof(downstatelist) / sizeof(struct downstate))
3304
3305
Harald Welte76556372010-12-22 23:57:45 +01003306int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003307{
Harald Welte1a6f7982009-08-09 18:52:33 +02003308 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003309 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003310 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003311 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003312 struct gsm_mncc *data = arg, rel;
3313
Harald Welte04dc88f2010-12-22 21:45:05 +01003314 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3315
Harald Welte4bfdfe72009-06-10 23:11:52 +08003316 /* handle special messages */
3317 switch(msg_type) {
3318 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003319 rc = tch_bridge(net, arg);
3320 if (rc < 0)
3321 disconnect_bridge(net, arg, -rc);
3322 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003323 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003324 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003325 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003326 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003327 case MNCC_RTP_CREATE:
3328 return tch_rtp_create(net, data->callref);
3329 case MNCC_RTP_CONNECT:
3330 return tch_rtp_connect(net, arg);
3331 case MNCC_RTP_FREE:
3332 /* unused right now */
3333 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003334 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003335 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003336 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003337 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003338 /* Find callref */
3339 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003340 if (!trans) {
3341 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003342 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003343 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003344 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003345 if (!trans->conn) {
3346 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003347 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003348 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003349 if (!trans->conn->lchan) {
3350 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3351 return 0;
3352 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003353 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3354 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003355 /* This should be LOGL_ERROR or NOTICE, but
3356 * unfortuantely it happens for a couple of frames at
3357 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003358 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003359 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003360 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003361 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003362 switch (bts->type) {
3363 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003364 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003365 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003366 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003367 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003368 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003369 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003370 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003371 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003372 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003373 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003374 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003375 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003376 }
3377 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003378 }
3379
3380 memset(&rel, 0, sizeof(struct gsm_mncc));
3381 rel.callref = data->callref;
3382
3383 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003384 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003385
3386 /* Callref unknown */
3387 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003388 struct gsm_subscriber *subscr;
3389
Harald Welte4a3464c2009-07-04 10:11:24 +02003390 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003391 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3392 "Received '%s' from MNCC with "
3393 "unknown callref %d\n", data->called.number,
3394 get_mncc_name(msg_type), data->callref);
3395 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003396 return mncc_release_ind(net, NULL, data->callref,
3397 GSM48_CAUSE_LOC_PRN_S_LU,
3398 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003399 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003400 if (!data->called.number[0] && !data->imsi[0]) {
3401 DEBUGP(DCC, "(bts - trx - ts - ti) "
3402 "Received '%s' from MNCC with "
3403 "no number or IMSI\n", get_mncc_name(msg_type));
3404 /* Invalid number */
3405 return mncc_release_ind(net, NULL, data->callref,
3406 GSM48_CAUSE_LOC_PRN_S_LU,
3407 GSM48_CC_CAUSE_INV_NR_FORMAT);
3408 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003409 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003410 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003411 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003412 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003413 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003414 subscr = subscr_get_by_imsi(net->subscr_group,
3415 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003416
3417 /* update the subscriber we deal with */
3418 log_set_context(BSC_CTX_SUBSCR, subscr);
3419
Harald Welte4bfdfe72009-06-10 23:11:52 +08003420 /* If subscriber is not found */
3421 if (!subscr) {
3422 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3423 "Received '%s' from MNCC with "
3424 "unknown subscriber %s\n", data->called.number,
3425 get_mncc_name(msg_type), data->called.number);
3426 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003427 return mncc_release_ind(net, NULL, data->callref,
3428 GSM48_CAUSE_LOC_PRN_S_LU,
3429 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003430 }
3431 /* If subscriber is not "attached" */
3432 if (!subscr->lac) {
3433 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3434 "Received '%s' from MNCC with "
3435 "detached subscriber %s\n", data->called.number,
3436 get_mncc_name(msg_type), data->called.number);
3437 subscr_put(subscr);
3438 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003439 return mncc_release_ind(net, NULL, data->callref,
3440 GSM48_CAUSE_LOC_PRN_S_LU,
3441 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003442 }
3443 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003444 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003445 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446 DEBUGP(DCC, "No memory for trans.\n");
3447 subscr_put(subscr);
3448 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003449 mncc_release_ind(net, NULL, data->callref,
3450 GSM48_CAUSE_LOC_PRN_S_LU,
3451 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003452 return -ENOMEM;
3453 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003454 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003455 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003456
Harald Welte4bfdfe72009-06-10 23:11:52 +08003457 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003458 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003459 /* find transaction with this subscriber already paging */
3460 llist_for_each_entry(transt, &net->trans_list, entry) {
3461 /* Transaction of our lchan? */
3462 if (transt == trans ||
3463 transt->subscr != subscr)
3464 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003465 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003466 "Received '%s' from MNCC with "
3467 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003468 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003469 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003470 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003471 subscr_put(subscr);
3472 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003473 return 0;
3474 }
3475 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003476 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003477
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003478 /* Request a channel */
3479 trans->paging_request = subscr_request_channel(subscr,
3480 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3481 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003482 if (!trans->paging_request) {
3483 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3484 subscr_put(subscr);
3485 trans_free(trans);
3486 return 0;
3487 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003488 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003489 return 0;
3490 }
3491 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003492 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003493 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003494 } else {
3495 /* update the subscriber we deal with */
3496 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003497 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003498
3499 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003500 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003501
3502 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003503 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003504 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003505 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 (trans->subscr)?(trans->subscr->extension):"-",
3507 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003508 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3509 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003510 if (msg_type == MNCC_REL_REQ)
3511 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3512 else
3513 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3514 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003515 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003516 return rc;
3517 }
3518
3519 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3520 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003521 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003522 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003523 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003524 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003525 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003526
3527 /* Find function for current state and message */
3528 for (i = 0; i < DOWNSLLEN; i++)
3529 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003530 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003531 break;
3532 if (i == DOWNSLLEN) {
3533 DEBUGP(DCC, "Message unhandled at this state.\n");
3534 return 0;
3535 }
3536
3537 rc = downstatelist[i].rout(trans, arg);
3538
3539 return rc;
3540}
3541
3542
3543static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003544 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003545 int type;
3546 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3547} datastatelist[] = {
3548 /* mobile originating call establishment */
3549 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3550 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3551 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3552 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3553 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3554 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3555 /* mobile terminating call establishment */
3556 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3557 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3558 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3559 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003560 {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 +08003561 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3562 /* signalling during call */
3563 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3564 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3565 {SBIT(GSM_CSTATE_ACTIVE),
3566 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3567 {ALL_STATES,
3568 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3569 {ALL_STATES,
3570 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3571 {ALL_STATES,
3572 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3573 {SBIT(GSM_CSTATE_ACTIVE),
3574 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3575 {SBIT(GSM_CSTATE_ACTIVE),
3576 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3577 {SBIT(GSM_CSTATE_ACTIVE),
3578 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3579 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3580 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3581 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3582 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3583 {SBIT(GSM_CSTATE_ACTIVE),
3584 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3585 /* clearing */
3586 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3587 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3588 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3589 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3590 {ALL_STATES, /* 5.4.3.4 */
3591 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3592};
3593
3594#define DATASLLEN \
3595 (sizeof(datastatelist) / sizeof(struct datastate))
3596
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003597static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003598{
3599 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003600 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003601 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003602 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003603 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003604
Harald Welte4bfdfe72009-06-10 23:11:52 +08003605 if (msg_type & 0x80) {
3606 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3607 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003608 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003609
Harald Welte4bfdfe72009-06-10 23:11:52 +08003610 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003611 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003612
Harald Welte6f5aee02009-07-23 21:21:14 +02003613 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003614 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003615 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003616 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003617 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3618 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003619
3620 /* Create transaction */
3621 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003622 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003623 "creating new trans.\n", transaction_id);
3624 /* Create transaction */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02003625 trans = trans_alloc(conn->network, conn->subscr,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003626 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003627 transaction_id, new_callref++);
3628 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003629 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003630 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003631 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003632 GSM48_MT_CC_RELEASE_COMPL);
3633 return -ENOMEM;
3634 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003635 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003636 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003637 }
3638
3639 /* find function for current state and message */
3640 for (i = 0; i < DATASLLEN; i++)
3641 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003642 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003643 break;
3644 if (i == DATASLLEN) {
3645 DEBUGP(DCC, "Message unhandled at this state.\n");
3646 return 0;
3647 }
3648
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003649 assert(trans->subscr);
3650
Harald Welte4bfdfe72009-06-10 23:11:52 +08003651 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003652
3653 return rc;
3654}
3655
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003656/* Create a dummy to wait five seconds */
3657static void release_anchor(struct gsm_subscriber_connection *conn)
3658{
3659 if (!conn->anch_operation)
3660 return;
3661
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003662 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003663 talloc_free(conn->anch_operation);
3664 conn->anch_operation = NULL;
3665}
3666
3667static void anchor_timeout(void *_data)
3668{
3669 struct gsm_subscriber_connection *con = _data;
3670
3671 release_anchor(con);
3672 msc_release_connection(con);
3673}
3674
3675int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3676{
3677 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3678 if (!conn->anch_operation)
3679 return -1;
3680
3681 conn->anch_operation->timeout.data = conn;
3682 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003683 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003684 return 0;
3685}
3686
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003687/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003688int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003689{
3690 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003691 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003692 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003693
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003694 OSMO_ASSERT(conn);
3695 OSMO_ASSERT(msg);
3696
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003697 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003698 if (silent_call_reroute(conn, msg))
3699 return silent_call_rx(conn, msg);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003700
Harald Welte52b1f982008-12-23 20:25:15 +00003701 switch (pdisc) {
3702 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003703 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003704 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003705 break;
3706 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003707 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003708 break;
3709 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003710 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003711 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003712 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003713 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003714 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003715 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003716 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003717 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003718 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3719 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003720 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003721 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003722 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003723 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003724 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003725 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003726 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003727 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3728 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003729 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003730 break;
3731 }
3732
3733 return rc;
3734}
Harald Welte8470bf22008-12-25 23:28:35 +00003735
Harald Welte805f6442009-07-28 18:25:29 +02003736/*
3737 * This will be ran by the linker when loading the DSO. We use it to
3738 * do system initialization, e.g. registration of signal handlers.
3739 */
3740static __attribute__((constructor)) void on_dso_load_0408(void)
3741{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003742 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003743}