blob: 67044971bae59ba2c554fe7e164a0e797dcfc652 [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{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800190 struct gsm_network *net = conn->bts->network;
191 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;
324 int avoid_tmsi = conn->bts->network->avoid_tmsi;
325
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);
335 if (conn->bts->network->send_mm_info) {
336 /* 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:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800432 llist_for_each_entry_safe(trans, temp, &conn->bts->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
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200460 osmo_counter_inc(bts->network->stats.loc_upd_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 }
Harald Welte52b1f982008-12-23 20:25:15 +0000467
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 Freytherdc5db242010-06-15 14:07:27 +0800480 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100481 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000482 struct gsm48_hdr *gh;
483 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200484 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000485
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800486 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000487
488 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
489 gh->proto_discr = GSM48_PDISC_MM;
490 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
491
492 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100493 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000494 bts->network->network_code, bts->location_area_code);
495
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
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200509 osmo_counter_inc(bts->network->stats.loc_upd_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{
Maxddee01f2016-05-24 14:23:27 +0200533 if (net->subscr_creation_mode == GSM_SUBSCR_DONT_CREATE)
534 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200535
Maxddee01f2016-05-24 14:23:27 +0200536 if (net->subscr_creation_mode & GSM_SUBSCR_CREAT_W_REGEXP)
537 if (!subscr_regexp_check(net, imsi))
538 return NULL;
539
Max0fcd2e22016-06-07 15:32:16 +0200540 return subscr_create_subscriber(net->subscr_group, imsi, net->ext_min,
541 net->ext_max);
Maxe21cf382016-05-11 11:01:06 +0200542}
Harald Welte231ad4f2008-12-27 11:15:38 +0000543
Harald Weltebf5e8df2009-02-03 12:59:45 +0000544/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800545static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000546{
547 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000548 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200549 struct gsm_bts *bts = lchan->ts->trx->bts;
550 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200551 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200552 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000553
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200554 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200555 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
556 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000557
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200558 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100559
Harald Welte75a983f2008-12-27 21:34:06 +0000560 switch (mi_type) {
561 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200562 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100563 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100564 conn->subscr = subscr_get_by_imsi(net->subscr_group,
565 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200566 if (!conn->subscr)
567 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200568 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100569 if (!conn->subscr && conn->loc_operation) {
570 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
571 release_loc_updating_req(conn, 1);
572 return 0;
573 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100574 if (conn->loc_operation)
575 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000576 break;
577 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000578 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000579 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100580 if (conn->subscr) {
581 db_subscriber_assoc_imei(conn->subscr, mi_string);
582 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200583 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100584 if (conn->loc_operation)
585 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000586 break;
587 }
Holger Freyther73487a22008-12-31 18:53:57 +0000588
589 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100590 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000591}
592
Harald Welte255539c2008-12-28 02:26:27 +0000593
594static void loc_upd_rej_cb(void *data)
595{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100596 struct gsm_subscriber_connection *conn = data;
597 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100598 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000599
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100600 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800601 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100602 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000603}
604
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100605static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000606{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100607 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
608 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200609 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000610}
611
Harald Welteb9845f92015-08-16 18:07:48 +0200612static const struct value_string lupd_names[] = {
613 { GSM48_LUPD_NORMAL, "NORMAL" },
614 { GSM48_LUPD_PERIODIC, "PERIODIC" },
615 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
616 { 0, NULL }
617};
Harald Welte2a139372009-02-22 21:14:55 +0000618
Harald Weltebf5e8df2009-02-03 12:59:45 +0000619/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800620static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000621{
Harald Welte8470bf22008-12-25 23:28:35 +0000622 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000623 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800625 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200626 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200627 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000628
Harald Welte8470bf22008-12-25 23:28:35 +0000629 lu = (struct gsm48_loc_upd_req *) gh->data;
630
631 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000632
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200633 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000634
Harald Welteb9845f92015-08-16 18:07:48 +0200635 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
636 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000637
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200638 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100639
Harald Welte24ff6ee2009-12-22 00:41:05 +0100640 switch (lu->type) {
641 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200642 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100643 break;
644 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200645 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100646 break;
647 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200648 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100649 break;
650 }
651
Holger Freythereaf04692009-06-06 13:54:44 +0000652 /*
653 * Pseudo Spoof detection: Just drop a second/concurrent
654 * location updating request.
655 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100656 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200657 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100658 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800659 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000660 return 0;
661 }
662
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800663 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000664
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200665 conn->loc_operation->key_seq = lu->key_seq;
666
Harald Welte52b1f982008-12-23 20:25:15 +0000667 switch (mi_type) {
668 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200669 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000670 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200671 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100672 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000673
Jan Luebbe370b41d2009-08-12 10:19:34 +0200674 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100675 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200676 if (!subscr)
677 subscr = subscr_create(bts->network, mi_string);
678
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100679 if (!subscr) {
680 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
681 release_loc_updating_req(conn, 0);
682 return 0;
683 }
Harald Welte4b634542008-12-27 01:55:51 +0000684 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000685 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200686 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000687 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100688 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200689 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000690 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000691 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200692 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100693 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000694 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200695 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200696 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200697 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000698 break;
699 case GSM_MI_TYPE_IMEI:
700 case GSM_MI_TYPE_IMEISV:
701 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200702 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000703 break;
704 default:
Harald Weltea0368542009-06-27 02:58:43 +0200705 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000706 break;
707 }
708
Harald Welte24516ea2009-07-04 10:18:00 +0200709 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100710 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200711
Harald Welte4bfdfe72009-06-10 23:11:52 +0800712 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200713 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800714 /* FIXME: request id? close channel? */
715 return -EINVAL;
716 }
717
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100718 conn->subscr = subscr;
719 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000720
Harald Welte24516ea2009-07-04 10:18:00 +0200721 /* check if we can let the subscriber into our network immediately
722 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100723 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000724}
725
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100726/* Turn int into semi-octet representation: 98 => 0x89 */
727static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800728{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100729 uint8_t ret;
730
731 ret = value / 10;
732 ret |= (value % 10) << 4;
733
734 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800735}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100736
Harald Welte4bfdfe72009-06-10 23:11:52 +0800737
Harald Weltedb253af2008-12-30 17:56:55 +0000738/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800739int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000740{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100741 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000742 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800743 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100744 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200745 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200746 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100747
Harald Welte4bfdfe72009-06-10 23:11:52 +0800748 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100749 struct tm* gmt_time;
750 struct tm* local_time;
751 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100752 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000753
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800754 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000755
756 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
757 gh->proto_discr = GSM48_PDISC_MM;
758 gh->msg_type = GSM48_MT_MM_INFO;
759
760 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200761#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000762 name_len = strlen(net->name_long);
763 /* 10.5.3.5a */
764 ptr8 = msgb_put(msg, 3);
765 ptr8[0] = GSM48_IE_NAME_LONG;
766 ptr8[1] = name_len*2 +1;
767 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
768
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000770 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000771 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000772
773 /* FIXME: Use Cell Broadcast, not UCS-2, since
774 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200775#endif
776 name_len = (strlen(net->name_long)*7)/8;
777 name_pad = (8 - strlen(net->name_long)*7)%8;
778 if (name_pad > 0)
779 name_len++;
780 /* 10.5.3.5a */
781 ptr8 = msgb_put(msg, 3);
782 ptr8[0] = GSM48_IE_NAME_LONG;
783 ptr8[1] = name_len +1;
784 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
785
786 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100787 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200788
Harald Weltedb253af2008-12-30 17:56:55 +0000789 }
790
791 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200792#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000793 name_len = strlen(net->name_short);
794 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200795 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200796 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000797 ptr8[1] = name_len*2 + 1;
798 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
799
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200800 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000801 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000802 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200803#endif
804 name_len = (strlen(net->name_short)*7)/8;
805 name_pad = (8 - strlen(net->name_short)*7)%8;
806 if (name_pad > 0)
807 name_len++;
808 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200809 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200810 ptr8[0] = GSM48_IE_NAME_SHORT;
811 ptr8[1] = name_len +1;
812 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
813
814 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100815 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200816
Harald Weltedb253af2008-12-30 17:56:55 +0000817 }
818
Harald Weltedb253af2008-12-30 17:56:55 +0000819 /* Section 10.5.3.9 */
820 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100821 gmt_time = gmtime(&cur_t);
822
Harald Weltedb253af2008-12-30 17:56:55 +0000823 ptr8 = msgb_put(msg, 8);
824 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100825 ptr8[1] = bcdify(gmt_time->tm_year % 100);
826 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
827 ptr8[3] = bcdify(gmt_time->tm_mday);
828 ptr8[4] = bcdify(gmt_time->tm_hour);
829 ptr8[5] = bcdify(gmt_time->tm_min);
830 ptr8[6] = bcdify(gmt_time->tm_sec);
831
Harald Welte45f91712012-07-08 16:48:11 +0200832 if (bts->tz.override) {
833 /* Convert tz.hr and tz.mn to units */
834 if (bts->tz.hr < 0) {
835 tzunits = ((bts->tz.hr/-1)*4);
836 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100837 ptr8[7] = bcdify(tzunits);
838 /* Set negative time */
839 ptr8[7] |= 0x08;
840 }
841 else {
Harald Welte45f91712012-07-08 16:48:11 +0200842 tzunits = bts->tz.hr*4;
843 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100844 ptr8[7] = bcdify(tzunits);
845 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100846 /* Convert DST value */
847 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
848 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100849 }
850 else {
851 /* Need to get GSM offset and convert into 15 min units */
852 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
853 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200854#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100855 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200856#else
857#warning find a portable way to obtain the timezone offset
858 tzunits = 0;
859#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100860 if (tzunits < 0) {
861 tzunits = tzunits/-1;
862 ptr8[7] = bcdify(tzunits);
863 /* Flip it to negative */
864 ptr8[7] |= 0x08;
865 }
866 else
867 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100868
869 /* Does not support DST +2 */
870 if (local_time->tm_isdst)
871 dst = 1;
872 }
873
874 if (dst) {
875 ptr8 = msgb_put(msg, 3);
876 ptr8[0] = GSM48_IE_NET_DST;
877 ptr8[1] = 1;
878 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100879 }
Harald Weltedb253af2008-12-30 17:56:55 +0000880
Daniel Willmanneea93372009-08-13 03:42:07 +0200881 DEBUGP(DMM, "-> MM INFO\n");
882
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800883 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000884}
885
Harald Welte7984d5c2009-08-12 22:56:50 +0200886/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200887int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200888{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100889 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200890 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200891 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200892
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200893 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200894
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800895 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200896 gh->proto_discr = GSM48_PDISC_MM;
897 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
898
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100899 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200900
Harald Welte7984d5c2009-08-12 22:56:50 +0200901 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200902 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200903 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200904
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800905 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200906}
907
908/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800909int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200910{
911 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800912 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200913}
914
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100915/*
916 * At the 30C3 phones miss their periodic update
917 * interval a lot and then remain unreachable. In case
918 * we still know the TMSI we can just attach it again.
919 */
920static void implit_attach(struct gsm_subscriber_connection *conn)
921{
922 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
923 return;
924
925 subscr_update(conn->subscr, conn->bts,
926 GSM_SUBSCRIBER_UPDATE_ATTACHED);
927}
928
929
Sylvain Munautba87f452009-12-24 00:28:46 +0100930static int _gsm48_rx_mm_serv_req_sec_cb(
931 unsigned int hooknum, unsigned int event,
932 struct msgb *msg, void *data, void *param)
933{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800934 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100935 int rc = 0;
936
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200937 /* auth failed or succeeded, the timer was stopped */
938 conn->expire_timer_stopped = 1;
939
Sylvain Munautba87f452009-12-24 00:28:46 +0100940 switch (event) {
941 case GSM_SECURITY_AUTH_FAILED:
942 /* Nothing to do */
943 break;
944
945 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200946 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800947 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100948 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100949 break;
950
951 case GSM_SECURITY_SUCCEEDED:
952 /* nothing to do. CIPHER MODE COMMAND is
953 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100954 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100955 break;
956
957 default:
958 rc = -EINVAL;
959 };
960
961 return rc;
962}
963
Harald Welte4ed0e922009-01-10 03:17:30 +0000964/*
965 * Handle CM Service Requests
966 * a) Verify that the packet is long enough to contain the information
967 * we require otherwsie reject with INCORRECT_MESSAGE
968 * b) Try to parse the TMSI. If we do not have one reject
969 * c) Check that we know the subscriber with the TMSI otherwise reject
970 * with a HLR cause
971 * d) Set the subscriber on the gsm_lchan and accept
972 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800973static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000974{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200975 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200976 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000977
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800978 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000979 struct gsm_subscriber *subscr;
980 struct gsm48_hdr *gh = msgb_l3(msg);
981 struct gsm48_service_request *req =
982 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800983 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200984 uint8_t classmark2_len = gh->data[1];
985 uint8_t *classmark2 = gh->data+2;
986 uint8_t mi_len = *(classmark2 + classmark2_len);
987 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000988
Harald Weltec9e02182009-05-01 19:07:53 +0000989 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000990 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000991 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800992 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000993 GSM48_REJECT_INCORRECT_MESSAGE);
994 }
995
996 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000997 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800998 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000999 GSM48_REJECT_INCORRECT_MESSAGE);
1000 }
1001
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001002 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001003 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001004
1005 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001006 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1007 req->cm_service_type, gsm48_mi_type_name(mi_type),
1008 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001009 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1010 mi_string);
1011 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001012 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1013 req->cm_service_type, gsm48_mi_type_name(mi_type),
1014 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001015 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
1016 tmsi_from_string(mi_string));
1017 } else {
1018 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001019 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001020 GSM48_REJECT_INCORRECT_MESSAGE);
1021 }
1022
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001023 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001024
Harald Welte3ac7f102009-08-10 10:12:45 +02001025 if (is_siemens_bts(bts))
1026 send_siemens_mrpci(msg->lchan, classmark2-1);
1027
Holger Freythereb443982009-06-04 13:58:42 +00001028
Harald Welte2a139372009-02-22 21:14:55 +00001029 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001030 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001031 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001032 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001033
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001034 if (!conn->subscr)
1035 conn->subscr = subscr;
1036 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001037 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1038 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001039 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1040 subscr_put(subscr);
1041 }
1042
Harald Weltec2e302d2009-07-05 14:08:13 +02001043 subscr->equipment.classmark2_len = classmark2_len;
1044 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1045 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001046
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001047 /* we will send a MM message soon */
1048 conn->expire_timer_stopped = 1;
1049
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001050 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001051 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001052}
1053
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001054static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001055{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001056 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001057 struct gsm48_hdr *gh = msgb_l3(msg);
1058 struct gsm48_imsi_detach_ind *idi =
1059 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001060 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001061 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001062 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001063
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001064 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001065 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1066 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001067
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001068 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001069
Harald Welte2a139372009-02-22 21:14:55 +00001070 switch (mi_type) {
1071 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001072 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001073 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001074 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001075 break;
1076 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001077 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001078 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1079 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001080 break;
1081 case GSM_MI_TYPE_IMEI:
1082 case GSM_MI_TYPE_IMEISV:
1083 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001084 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001085 break;
1086 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001087 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001088 break;
1089 }
1090
Holger Freyther4a49e772009-04-12 05:37:29 +00001091 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001092 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001093 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001094 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001095
1096 subscr->equipment.classmark1 = idi->classmark1;
1097 db_sync_equipment(&subscr->equipment);
1098
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001099 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001100 } else
Harald Welte2a139372009-02-22 21:14:55 +00001101 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1102
Harald Welte31981a02009-12-20 09:58:40 +01001103 /* FIXME: iterate over all transactions and release them,
1104 * imagine an IMSI DETACH happening during an active call! */
1105
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001106 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001107 return 0;
1108}
1109
Harald Welted2a7f5a2009-06-05 20:08:20 +00001110static int gsm48_rx_mm_status(struct msgb *msg)
1111{
1112 struct gsm48_hdr *gh = msgb_l3(msg);
1113
1114 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1115
1116 return 0;
1117}
1118
Sylvain Munaut30a15382009-12-24 00:27:26 +01001119/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001120static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001121{
1122 struct gsm48_hdr *gh = msgb_l3(msg);
1123 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001124 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001125
Sylvain Munautc593cf12010-06-10 23:51:41 +02001126 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001127 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128
1129 /* Safety check */
1130 if (!conn->sec_operation) {
1131 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1132 return -EIO;
1133 }
1134
1135 /* Validate SRES */
Harald Welte121e9a42016-04-20 13:13:19 +02001136 if (memcmp(conn->sec_operation->atuple.vec.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001137 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001138 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001139
1140 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001141 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001142
Sylvain Munaut30a15382009-12-24 00:27:26 +01001143 if (cb)
1144 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001145 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001146
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001147 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001148 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001149 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001150 }
1151
Sylvain Munautc593cf12010-06-10 23:51:41 +02001152 DEBUGPC(DMM, "OK\n");
1153
Sylvain Munaut30a15382009-12-24 00:27:26 +01001154 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001155 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001156 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001157}
1158
Harald Weltebf5e8df2009-02-03 12:59:45 +00001159/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001160static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001161{
1162 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001163 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001164
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001165 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001166 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001167 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001168 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001169 break;
1170 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001171 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001172 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001173 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001174 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001175 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001176 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001177 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001178 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001179 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001180 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001181 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001182 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001183 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001184 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001185 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001186 break;
1187 case GSM48_MT_MM_CM_REEST_REQ:
1188 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1189 break;
1190 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001191 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001192 break;
1193 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001194 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001195 gh->msg_type);
1196 break;
1197 }
1198
1199 return rc;
1200}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001201
Harald Welte2d35ae62009-02-06 12:02:13 +00001202/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001203static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001204{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001205 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001206 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001207 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001208 uint8_t *classmark2_lv = gh->data + 1;
1209 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001210 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001211 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001212 int rc = 0;
1213
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001214 resp = (struct gsm48_pag_resp *) &gh->data[0];
1215 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1216 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001217 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1218 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001219
Harald Weltefe18d8f2009-02-22 21:14:24 +00001220 switch (mi_type) {
1221 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001222 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001223 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001224 break;
1225 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001226 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1227 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001228 break;
1229 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001230
1231 if (!subscr) {
1232 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001233 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001234 return -EINVAL;
1235 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001236 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001237 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001238 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001239
Harald Weltec2e302d2009-07-05 14:08:13 +02001240 subscr->equipment.classmark2_len = *classmark2_lv;
1241 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1242 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001243
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001244 /* We received a paging */
1245 conn->expire_timer_stopped = 1;
1246
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001247 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001248 return rc;
1249}
1250
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001251static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001252{
1253 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001254 uint8_t apdu_id_flags;
1255 uint8_t apdu_len;
1256 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001257
1258 apdu_id_flags = gh->data[0];
1259 apdu_len = gh->data[1];
1260 apdu_data = gh->data+2;
1261
Harald Welte70f92052012-07-16 13:22:19 +02001262 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001263 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001264
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001265 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266}
1267
Harald Weltebf5e8df2009-02-03 12:59:45 +00001268/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001269static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001270{
1271 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001272 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001273
1274 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001275 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001276 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001277 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001278 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001279 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001280 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001281 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001282 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001283 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001284 break;
1285 }
1286
Harald Welte2d35ae62009-02-06 12:02:13 +00001287 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001288}
1289
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001290int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1291 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001292{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001293 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001294 struct gsm48_hdr *gh;
1295
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001296 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001297
1298 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1299 apdu_id, apdu_len);
1300
1301 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1302 gh->proto_discr = GSM48_PDISC_RR;
1303 gh->msg_type = GSM48_MT_RR_APP_INFO;
1304 gh->data[0] = apdu_id;
1305 gh->data[1] = apdu_len;
1306 memcpy(gh->data+2, apdu, apdu_len);
1307
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001308 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001309}
1310
Harald Welte4bc90a12008-12-27 16:32:52 +00001311/* Call Control */
1312
Harald Welte7584aea2009-02-11 11:44:12 +00001313/* The entire call control code is written in accordance with Figure 7.10c
1314 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1315 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1316 * it for voice */
1317
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318static void new_cc_state(struct gsm_trans *trans, int state)
1319{
1320 if (state > 31 || state < 0)
1321 return;
1322
1323 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001324 gsm48_cc_state_name(trans->cc.state),
1325 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326
Harald Weltedcaf5652009-07-23 18:56:43 +02001327 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328}
1329
1330static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001331{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001332 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001333 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001334 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001335
Harald Welte4bc90a12008-12-27 16:32:52 +00001336 gh->msg_type = GSM48_MT_CC_STATUS;
1337
1338 cause = msgb_put(msg, 3);
1339 cause[0] = 2;
1340 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1341 cause[2] = 0x80 | 30; /* response to status inquiry */
1342
1343 call_state = msgb_put(msg, 1);
1344 call_state[0] = 0xc0 | 0x00;
1345
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001346 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001347}
1348
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001349static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001350 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001351{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001352 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001353 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1354
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001355 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001356
Harald Welte6f4b7532008-12-29 00:39:37 +00001357 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001358 gh->msg_type = msg_type;
1359
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001360 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001361}
1362
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1364{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001365 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001366 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001367 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001368 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 }
1370}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001371
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1373 int msg_type, struct gsm_mncc *mncc)
1374{
1375 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001376 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377
1378 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001379 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001380 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001382 trans->conn->lchan->ts->trx->bts->nr,
1383 trans->conn->lchan->ts->trx->nr,
1384 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 (trans->subscr)?(trans->subscr->extension):"-",
1386 get_mncc_name(msg_type));
1387 else
1388 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1389 "Sending '%s' to MNCC.\n",
1390 (trans->subscr)?(trans->subscr->extension):"-",
1391 get_mncc_name(msg_type));
1392 else
1393 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1394 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1395
1396 mncc->msg_type = msg_type;
1397
Harald Welte966636f2009-06-26 19:39:35 +02001398 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399 if (!msg)
1400 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001401
1402 data = msgb_put(msg, sizeof(struct gsm_mncc));
1403 memcpy(data, mncc, sizeof(struct gsm_mncc));
1404
Harald Welte54209c22010-12-22 23:43:29 +01001405 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406
1407 return 0;
1408}
1409
1410int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001411 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001412{
1413 struct gsm_mncc rel;
1414
Harald Welte92f70c52009-06-12 01:54:08 +08001415 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001417 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001418 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1419 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1421}
1422
Harald Weltedcaf5652009-07-23 18:56:43 +02001423/* Call Control Specific transaction release.
1424 * gets called by trans_free, DO NOT CALL YOURSELF! */
1425void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 gsm48_stop_cc_timer(trans);
1428
1429 /* send release to L4, if callref still exists */
1430 if (trans->callref) {
1431 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001432 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001433 GSM48_CAUSE_LOC_PRN_S_LU,
1434 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001436 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001437 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001438 if (trans->conn)
1439 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001440}
1441
1442static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001443
Harald Welte09e38af2009-02-16 22:52:23 +00001444/* call-back from paging the B-end of the connection */
1445static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001446 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001447{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001448 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001449 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001450
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001451 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001452
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001454 switch (event) {
1455 case GSM_PAGING_SUCCEEDED:
1456 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001457 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001458 /* Assign lchan */
1459 transt->conn = conn;
1460 /* send SETUP request to called party */
1461 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1462 break;
1463 case GSM_PAGING_EXPIRED:
1464 case GSM_PAGING_BUSY:
1465 DEBUGP(DCC, "Paging subscr %s expired!\n",
1466 transt->subscr->extension);
1467 /* Temporarily out of order */
1468 mncc_release_ind(transt->net, transt,
1469 transt->callref,
1470 GSM48_CAUSE_LOC_PRN_S_LU,
1471 GSM48_CC_CAUSE_DEST_OOO);
1472 transt->callref = 0;
1473 transt->paging_request = NULL;
1474 trans_free(transt);
1475 break;
1476 default:
1477 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1478 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001479 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001480
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001481 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001482 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001483}
Harald Welte7584aea2009-02-11 11:44:12 +00001484
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001485static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001486
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001487/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001488static int switch_for_handover(struct gsm_lchan *old_lchan,
1489 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001490{
1491 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001492
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001493 /* Ask the new socket to send to the already known port. */
1494 if (new_lchan->conn->mncc_rtp_bridge) {
1495 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1496 rsl_ipacc_mdcx(new_lchan,
1497 old_lchan->abis_ip.connect_ip,
1498 old_lchan->abis_ip.connect_port, 0);
1499 return 0;
1500 }
1501
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001502 if (ipacc_rtp_direct) {
1503 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1504 return 0;
1505 }
1506
1507 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001508 new_rs = new_lchan->abis_ip.rtp_socket;
1509 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001510
1511 if (!new_rs) {
1512 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1513 return -EIO;
1514 }
1515
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001516 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001517
1518 if (!old_rs) {
1519 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1520 return -EIO;
1521 }
1522
1523 /* copy rx_action and reference to other sock */
1524 new_rs->rx_action = old_rs->rx_action;
1525 new_rs->tx_action = old_rs->tx_action;
1526 new_rs->transmit = old_rs->transmit;
1527
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001528 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001529 case RTP_PROXY:
1530 other_rs = old_rs->proxy.other_sock;
1531 rtp_socket_proxy(new_rs, other_rs);
1532 /* delete reference to other end socket to prevent
1533 * rtp_socket_free() from removing the inverse reference */
1534 old_rs->proxy.other_sock = NULL;
1535 break;
1536 case RTP_RECV_UPSTREAM:
1537 new_rs->receive = old_rs->receive;
1538 break;
1539 case RTP_NONE:
1540 break;
1541 }
1542
1543 return 0;
1544}
1545
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001546static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1547{
1548 struct gsm_lchan *old_lchan;
1549 old_lchan = bsc_handover_pending(lchan);
1550 if (old_lchan)
1551 switch_for_handover(old_lchan, lchan);
1552}
1553
Harald Welte805f6442009-07-28 18:25:29 +02001554/* some other part of the code sends us a signal */
1555static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1556 void *handler_data, void *signal_data)
1557{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001558 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001559 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001560 struct gsm_network *net;
1561 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001562
1563 if (subsys != SS_ABISIP)
1564 return 0;
1565
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001566 /* RTP bridge handling */
1567 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1568 return tch_rtp_signal(lchan, signal);
1569
Harald Welte805f6442009-07-28 18:25:29 +02001570 /* in case we use direct BTS-to-BTS RTP */
1571 if (ipacc_rtp_direct)
1572 return 0;
1573
Harald Welte805f6442009-07-28 18:25:29 +02001574 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001575 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001576 /* in case we don't use direct BTS-to-BTS RTP */
1577 /* the BTS has successfully bound a TCH to a local ip/port,
1578 * which means we can connect our UDP socket to it */
1579 if (lchan->abis_ip.rtp_socket) {
1580 rtp_socket_free(lchan->abis_ip.rtp_socket);
1581 lchan->abis_ip.rtp_socket = NULL;
1582 }
1583
1584 lchan->abis_ip.rtp_socket = rtp_socket_create();
1585 if (!lchan->abis_ip.rtp_socket)
1586 return -EIO;
1587
1588 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1589 lchan->abis_ip.bound_ip,
1590 lchan->abis_ip.bound_port);
1591 if (rc < 0)
1592 return -EIO;
1593
Harald Welteda7ab742009-12-19 22:23:05 +01001594 /* check if any transactions on this lchan still have
1595 * a tch_recv_mncc request pending */
1596 net = lchan->ts->trx->bts->network;
1597 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001598 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001599 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1600 tch_recv_mncc(net, trans->callref, 1);
1601 }
1602 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001603
1604 /*
1605 * TODO: this appears to be too early? Why not until after
1606 * the handover detect or the handover complete?
1607 *
1608 * Do we have a handover pending for this new lchan? In that
1609 * case re-route the audio from the old channel to the new one.
1610 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001611 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001612 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001613 case S_ABISIP_DLCX_IND:
1614 /* the BTS tells us a RTP stream has been disconnected */
1615 if (lchan->abis_ip.rtp_socket) {
1616 rtp_socket_free(lchan->abis_ip.rtp_socket);
1617 lchan->abis_ip.rtp_socket = NULL;
1618 }
1619
1620 break;
Harald Welte805f6442009-07-28 18:25:29 +02001621 }
1622
1623 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001624}
1625
Harald Welte49f48b82009-02-17 15:29:33 +00001626/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001627static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001628{
Harald Welte11fa29c2009-02-19 17:24:39 +00001629 struct gsm_bts *bts = lchan->ts->trx->bts;
1630 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001631 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001632 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001633 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001634
Max8db12e42016-04-18 23:11:18 +02001635 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1636 "(bts=%u,trx=%u,ts=%u,%s)\n",
1637 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1638 get_value_string(gsm_chan_t_names, lt),
1639 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1640 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001641
1642 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001643 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001644 return -EINVAL;
1645 }
Harald Welteda7ab742009-12-19 22:23:05 +01001646
Max3ffce192016-04-25 15:22:00 +02001647 if (lt != rt) {
1648 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1649 " channel types: local = %s, remote = %s\n",
1650 get_value_string(gsm_chan_t_names, lt),
1651 get_value_string(gsm_chan_t_names, rt));
1652 return -EBADSLT;
1653 }
1654
1655 if (lm != rm) {
1656 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1657 " channel modes: local = %s, remote = %s\n",
1658 get_value_string(gsm48_chan_mode_names, lm),
1659 get_value_string(gsm48_chan_mode_names, rm));
1660 return -EMEDIUMTYPE;
1661 }
1662
Harald Welteda7ab742009-12-19 22:23:05 +01001663 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001664 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001665 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001666 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001667 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001668 if (!lchan->abis_ip.rtp_socket) {
1669 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1670 "lchan\n");
1671 return -EIO;
1672 }
1673 if (!remote_lchan->abis_ip.rtp_socket) {
1674 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1675 "remote_lchan\n");
1676 return -EIO;
1677 }
1678
Harald Welte805f6442009-07-28 18:25:29 +02001679 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001680 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001681 if (rc < 0)
1682 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001683 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001684 if (rc < 0)
1685 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001686 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301687 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1688 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001689 } else {
1690 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301691 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1692 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301693 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001694 if (rc < 0)
1695 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301696 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1697 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301698 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001699 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001700 break;
1701 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001702 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001703 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001704 trau_mux_map_lchan(lchan, remote_lchan);
1705 break;
1706 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001707 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001708 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001709 }
Harald Welte49f48b82009-02-17 15:29:33 +00001710
1711 return 0;
1712}
1713
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001715static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001716{
Harald Welte53d51f52015-12-03 14:59:04 +01001717 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1718 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001719
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001721 return -EIO;
1722
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001723 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 return -EIO;
1725
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001726 /* Which subscriber do we want to track trans1 or trans2? */
1727 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1728
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001730 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001731}
1732
Harald Welteda7ab742009-12-19 22:23:05 +01001733/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001734static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735{
1736 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001737 struct gsm_lchan *lchan;
1738 struct gsm_bts *bts;
1739 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001740
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001742 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 if (!trans)
1744 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001745 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001747
1748 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001749 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001750 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001752 /* store receive state */
1753 trans->tch_recv = enable;
1754
Harald Welteda7ab742009-12-19 22:23:05 +01001755 switch (bts->type) {
1756 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001757 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001758 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001759 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001760 return -EINVAL;
1761 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001762 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1763 * will not be connected to our RTP proxy and the socket will
1764 * not be assigned to the application interface. This method
1765 * will be called again, once the audio socket is created and
1766 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001767 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001768 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1769 return 0;
1770 }
1771 if (enable) {
1772 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001773 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001774 if (rc < 0)
1775 return rc;
1776 /* assign socket to application interface */
1777 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1778 net, callref);
1779 } else
1780 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1781 net, 0);
1782 break;
1783 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001784 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001785 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001786 /* In case we don't have a TCH with correct mode, the TRAU muxer
1787 * will not be asigned to the application interface. This is
1788 * performed by switch_trau_mux() after successful handover or
1789 * assignment. */
1790 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1791 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1792 return 0;
1793 }
Harald Welteda7ab742009-12-19 22:23:05 +01001794 if (enable)
1795 return trau_recv_lchan(lchan, callref);
1796 return trau_mux_unmap(NULL, callref);
1797 break;
1798 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001799 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001800 return -EINVAL;
1801 }
1802
1803 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804}
1805
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1807{
1808 DEBUGP(DCC, "-> STATUS ENQ\n");
1809 return gsm48_cc_tx_status(trans, msg);
1810}
1811
1812static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1813static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1814
1815static void gsm48_cc_timeout(void *arg)
1816{
1817 struct gsm_trans *trans = arg;
1818 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001819 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1820 int mo_location = GSM48_CAUSE_LOC_USER;
1821 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1822 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 struct gsm_mncc mo_rel, l4_rel;
1824
1825 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1826 mo_rel.callref = trans->callref;
1827 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1828 l4_rel.callref = trans->callref;
1829
Harald Weltedcaf5652009-07-23 18:56:43 +02001830 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 case 0x303:
1832 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001833 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 break;
1835 case 0x310:
1836 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001837 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 break;
1839 case 0x313:
1840 disconnect = 1;
1841 /* unknown, did not find it in the specs */
1842 break;
1843 case 0x301:
1844 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001845 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 break;
1847 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001848 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001850 gsm48_cc_tx_release(trans, &trans->cc.msg);
1851 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 break; /* stay in release state */
1853 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001854 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 return;
1856// release = 1;
1857// l4_cause = 14;
1858// break;
1859 case 0x306:
1860 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001861 mo_cause = trans->cc.msg.cause.value;
1862 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 break;
1864 case 0x323:
1865 disconnect = 1;
1866 break;
1867 default:
1868 release = 1;
1869 }
1870
1871 if (release && trans->callref) {
1872 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001873 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 l4_location, l4_cause);
1875 trans->callref = 0;
1876 }
1877
1878 if (disconnect && trans->callref) {
1879 /* process disconnect towards layer 4 */
1880 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001881 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 }
1883
1884 /* process disconnect towards mobile station */
1885 if (disconnect || release) {
1886 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001887 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1888 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1889 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 mo_rel.cause.diag_len = 3;
1891
1892 if (disconnect)
1893 gsm48_cc_tx_disconnect(trans, &mo_rel);
1894 if (release)
1895 gsm48_cc_tx_release(trans, &mo_rel);
1896 }
1897
1898}
1899
Max3ffce192016-04-25 15:22:00 +02001900/* disconnect both calls from the bridge */
1901static inline void disconnect_bridge(struct gsm_network *net,
1902 struct gsm_mncc_bridge *bridge, int err)
1903{
1904 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1905 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1906 struct gsm_mncc mx_rel;
1907 if (!trans0 || !trans1)
1908 return;
1909
1910 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1911 trans0->callref, trans1->callref, strerror(err));
1912
1913 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1914 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1915 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1916
1917 mx_rel.callref = trans0->callref;
1918 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1919
1920 mx_rel.callref = trans1->callref;
1921 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1922}
1923
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1925 int sec, int micro)
1926{
1927 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001928 trans->cc.timer.cb = gsm48_cc_timeout;
1929 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001930 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001931 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932}
1933
1934static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1935{
1936 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001937 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1939 struct tlv_parsed tp;
1940 struct gsm_mncc setup;
1941
1942 memset(&setup, 0, sizeof(struct gsm_mncc));
1943 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001944 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001945 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* emergency setup is identified by msg_type */
1947 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1948 setup.emergency = 1;
1949
1950 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001951 setup.fields |= MNCC_F_CALLING;
1952 strncpy(setup.calling.number, trans->subscr->extension,
1953 sizeof(setup.calling.number)-1);
1954 strncpy(setup.imsi, trans->subscr->imsi,
1955 sizeof(setup.imsi)-1);
1956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* bearer capability */
1958 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1959 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001962 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 }
1964 /* facility */
1965 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1966 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1969 }
1970 /* called party bcd number */
1971 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1972 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1975 }
1976 /* user-user */
1977 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1978 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1981 }
1982 /* ss-version */
1983 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1984 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1987 }
1988 /* CLIR suppression */
1989 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1990 setup.clir.sup = 1;
1991 /* CLIR invocation */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1993 setup.clir.inv = 1;
1994 /* cc cap */
1995 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1996 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001997 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1999 }
2000
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 new_cc_state(trans, GSM_CSTATE_INITIATED);
2002
Harald Welte (local)47df3992009-12-26 19:45:03 +01002003 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2004 subscr_name(trans->subscr), trans->subscr->extension,
2005 setup.called.number);
2006
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002007 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01002008
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002010 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011
Harald Welte13cac662009-07-29 12:10:35 +02002012 /* MNCC code will modify the channel asynchronously, we should
2013 * ipaccess-bind only after the modification has been made to the
2014 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 return 0;
2016}
2017
2018static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002019{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002020 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002021 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002023 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002024
Harald Welte7ccf7782009-02-17 01:43:01 +00002025 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002026
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* transaction id must not be assigned */
2028 if (trans->transaction_id != 0xff) { /* unasssigned */
2029 DEBUGP(DCC, "TX Setup with assigned transaction. "
2030 "This is not allowed!\n");
2031 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002032 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002033 GSM48_CAUSE_LOC_PRN_S_LU,
2034 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002036 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 return rc;
2038 }
2039
2040 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002041 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2042 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002043 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002045 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002046 GSM48_CAUSE_LOC_PRN_S_LU,
2047 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002049 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 return rc;
2051 }
Harald Welte78283ef2009-07-23 21:36:44 +02002052 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002053
Harald Welte65e74cc2008-12-29 01:55:35 +00002054 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002055
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002057
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 /* bearer capability */
2059 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002060 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 /* facility */
2062 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* progress */
2065 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002066 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 /* calling party BCD number */
2068 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002069 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 /* called party BCD number */
2071 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002072 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 /* user-user */
2074 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 /* redirecting party BCD number */
2077 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002078 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 /* signal */
2080 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002081 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082
2083 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002084
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002085 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01002086
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002087 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002088}
2089
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2091{
2092 struct gsm48_hdr *gh = msgb_l3(msg);
2093 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2094 struct tlv_parsed tp;
2095 struct gsm_mncc call_conf;
2096
2097 gsm48_stop_cc_timer(trans);
2098 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2099
2100 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2101 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002102 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002103 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104#if 0
2105 /* repeat */
2106 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2107 call_conf.repeat = 1;
2108 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2109 call_conf.repeat = 2;
2110#endif
2111 /* bearer capability */
2112 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2113 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002114 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002116 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 }
2118 /* cause */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2120 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002121 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2123 }
2124 /* cc cap */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2126 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2129 }
2130
Andreas Eversberg035b8742013-09-19 09:27:06 +02002131 /* IMSI of called subscriber */
2132 strncpy(call_conf.imsi, trans->subscr->imsi,
2133 sizeof(call_conf.imsi)-1);
2134
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2136
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002137 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002138 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139}
2140
2141static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2142{
2143 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002144 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2146
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2148
2149 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2150
2151 /* bearer capability */
2152 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002153 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154 /* facility */
2155 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002156 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 /* progress */
2158 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002159 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002161 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162}
2163
2164static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2165{
2166 struct gsm48_hdr *gh = msgb_l3(msg);
2167 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2168 struct tlv_parsed tp;
2169 struct gsm_mncc alerting;
2170
2171 gsm48_stop_cc_timer(trans);
2172 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2173
2174 memset(&alerting, 0, sizeof(struct gsm_mncc));
2175 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002176 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 /* facility */
2178 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2179 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2182 }
2183
2184 /* progress */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2186 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2189 }
2190 /* ss-version */
2191 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2192 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002193 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2195 }
2196
2197 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2198
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002199 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002200 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201}
2202
2203static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2204{
2205 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2208
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 gh->msg_type = GSM48_MT_CC_ALERTING;
2210
2211 /* facility */
2212 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002213 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* progress */
2215 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 /* user-user */
2218 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002219 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220
2221 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2222
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002223 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224}
2225
2226static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2227{
2228 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002229 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2231
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 gh->msg_type = GSM48_MT_CC_PROGRESS;
2233
2234 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002235 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 /* user-user */
2237 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002238 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002240 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241}
2242
2243static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2244{
2245 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002246 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2248
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249 gh->msg_type = GSM48_MT_CC_CONNECT;
2250
2251 gsm48_stop_cc_timer(trans);
2252 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2253
2254 /* facility */
2255 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 /* progress */
2258 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 /* connected number */
2261 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 /* user-user */
2264 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266
2267 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2268
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002269 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270}
2271
2272static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2273{
2274 struct gsm48_hdr *gh = msgb_l3(msg);
2275 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2276 struct tlv_parsed tp;
2277 struct gsm_mncc connect;
2278
2279 gsm48_stop_cc_timer(trans);
2280
2281 memset(&connect, 0, sizeof(struct gsm_mncc));
2282 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002283 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002285 connect.fields |= MNCC_F_CONNECTED;
2286 strncpy(connect.connected.number, trans->subscr->extension,
2287 sizeof(connect.connected.number)-1);
2288 strncpy(connect.imsi, trans->subscr->imsi,
2289 sizeof(connect.imsi)-1);
2290
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 /* facility */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2293 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2296 }
2297 /* user-user */
2298 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2299 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002300 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2302 }
2303 /* ss-version */
2304 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2305 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2308 }
2309
2310 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002311 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002313 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314}
2315
2316
2317static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2318{
2319 struct gsm_mncc connect_ack;
2320
2321 gsm48_stop_cc_timer(trans);
2322
2323 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002324 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325
2326 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2327 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002328
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002329 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 &connect_ack);
2331}
2332
2333static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2334{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002335 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2337
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2339
2340 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2341
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002342 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343}
2344
2345static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2346{
2347 struct gsm48_hdr *gh = msgb_l3(msg);
2348 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2349 struct tlv_parsed tp;
2350 struct gsm_mncc disc;
2351
2352 gsm48_stop_cc_timer(trans);
2353
2354 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2355
2356 memset(&disc, 0, sizeof(struct gsm_mncc));
2357 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002358 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 /* cause */
2360 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2361 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002362 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2364 }
2365 /* facility */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2367 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002368 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2370 }
2371 /* user-user */
2372 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2373 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002374 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2376 }
2377 /* ss-version */
2378 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2379 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002380 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2382 }
2383
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002384 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385
2386}
2387
Harald Weltec66b71c2009-06-11 14:23:20 +08002388static struct gsm_mncc_cause default_cause = {
2389 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2390 .coding = 0,
2391 .rec = 0,
2392 .rec_val = 0,
2393 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2394 .diag_len = 0,
2395 .diag = { 0 },
2396};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397
2398static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2399{
2400 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002401 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2403
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2405
2406 gsm48_stop_cc_timer(trans);
2407 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2408
2409 /* cause */
2410 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 else
Harald Welte55c8f352010-03-07 23:40:35 +01002413 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414
2415 /* facility */
2416 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002417 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 /* progress */
2419 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 /* user-user */
2422 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002423 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424
2425 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002426 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427
2428 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2429
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002430 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002431}
2432
2433static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2434{
2435 struct gsm48_hdr *gh = msgb_l3(msg);
2436 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2437 struct tlv_parsed tp;
2438 struct gsm_mncc rel;
2439 int rc;
2440
2441 gsm48_stop_cc_timer(trans);
2442
2443 memset(&rel, 0, sizeof(struct gsm_mncc));
2444 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002445 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 /* cause */
2447 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2448 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002449 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2451 }
2452 /* facility */
2453 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2454 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002455 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2457 }
2458 /* user-user */
2459 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2460 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002461 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2463 }
2464 /* ss-version */
2465 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2466 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002467 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2469 }
2470
Harald Weltedcaf5652009-07-23 18:56:43 +02002471 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002473 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002475 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002476 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002477 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002478 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 }
2480
2481 new_cc_state(trans, GSM_CSTATE_NULL);
2482
2483 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002484 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485
2486 return rc;
2487}
2488
2489static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2490{
2491 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002492 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2494
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 gh->msg_type = GSM48_MT_CC_RELEASE;
2496
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 gsm48_stop_cc_timer(trans);
2498 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2499
2500 /* cause */
2501 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002502 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 /* facility */
2504 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002505 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 /* user-user */
2507 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002508 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509
Harald Weltedcaf5652009-07-23 18:56:43 +02002510 trans->cc.T308_second = 0;
2511 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512
Harald Weltedcaf5652009-07-23 18:56:43 +02002513 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2515
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002516 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2520{
2521 struct gsm48_hdr *gh = msgb_l3(msg);
2522 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2523 struct tlv_parsed tp;
2524 struct gsm_mncc rel;
2525 int rc = 0;
2526
2527 gsm48_stop_cc_timer(trans);
2528
2529 memset(&rel, 0, sizeof(struct gsm_mncc));
2530 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002531 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532 /* cause */
2533 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2534 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002535 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2537 }
2538 /* facility */
2539 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2540 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002541 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2543 }
2544 /* user-user */
2545 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2546 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002547 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2549 }
2550 /* ss-version */
2551 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2552 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2555 }
2556
2557 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002558 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002560 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 MNCC_REJ_IND, &rel);
2562 break;
2563 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002564 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565 MNCC_REL_CNF, &rel);
2566 break;
2567 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002568 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 MNCC_REL_IND, &rel);
2570 }
2571 }
2572
2573 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002574 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575
2576 return rc;
2577}
2578
2579static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2580{
2581 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002584 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2587
2588 trans->callref = 0;
2589
2590 gsm48_stop_cc_timer(trans);
2591
2592 /* cause */
2593 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002594 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 /* facility */
2596 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002597 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598 /* user-user */
2599 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601
Harald Weltec7782de2010-12-21 19:31:41 +01002602 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2603
Harald Weltedcaf5652009-07-23 18:56:43 +02002604 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605
Harald Weltec7782de2010-12-21 19:31:41 +01002606 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2610{
2611 struct gsm48_hdr *gh = msgb_l3(msg);
2612 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2613 struct tlv_parsed tp;
2614 struct gsm_mncc fac;
2615
2616 memset(&fac, 0, sizeof(struct gsm_mncc));
2617 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002618 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 /* facility */
2620 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2621 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002622 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2624 }
2625 /* ss-version */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2627 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002628 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2630 }
2631
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002632 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002638 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_FACILITY;
2642
2643 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002644 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002646 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2650{
2651 struct gsm_mncc hold;
2652
2653 memset(&hold, 0, sizeof(struct gsm_mncc));
2654 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002655 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2659{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002660 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2662
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2664
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002665 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666}
2667
2668static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2669{
2670 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002671 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2673
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2675
2676 /* cause */
2677 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002678 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 else
Harald Welte55c8f352010-03-07 23:40:35 +01002680 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002682 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683}
2684
2685static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2686{
2687 struct gsm_mncc retrieve;
2688
2689 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2690 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002691 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002692 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693}
2694
2695static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2696{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002697 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2699
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2701
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002702 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703}
2704
2705static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2706{
2707 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002708 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2710
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2712
2713 /* cause */
2714 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002715 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716 else
Harald Welte55c8f352010-03-07 23:40:35 +01002717 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002719 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720}
2721
2722static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2723{
2724 struct gsm48_hdr *gh = msgb_l3(msg);
2725 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2726 struct tlv_parsed tp;
2727 struct gsm_mncc dtmf;
2728
2729 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2730 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002731 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 /* keypad facility */
2733 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2734 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002735 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2737 }
2738
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002739 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740}
2741
2742static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2743{
2744 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002745 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2747
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2749
2750 /* keypad */
2751 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002752 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002754 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755}
2756
2757static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2758{
2759 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002760 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2762
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2764
2765 /* cause */
2766 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002767 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768 else
Harald Welte55c8f352010-03-07 23:40:35 +01002769 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002771 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772}
2773
2774static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2775{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002776 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2778
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2780
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002781 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782}
2783
2784static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2785{
2786 struct gsm_mncc dtmf;
2787
2788 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2789 dtmf.callref = trans->callref;
2790
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002791 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792}
2793
2794static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2795{
2796 struct gsm48_hdr *gh = msgb_l3(msg);
2797 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2798 struct tlv_parsed tp;
2799 struct gsm_mncc modify;
2800
2801 memset(&modify, 0, sizeof(struct gsm_mncc));
2802 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002803 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 /* bearer capability */
2805 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2806 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002807 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002809 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 }
2811
2812 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2813
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002814 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815}
2816
2817static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2818{
2819 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002820 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2822
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 gh->msg_type = GSM48_MT_CC_MODIFY;
2824
2825 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2826
2827 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002828 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829
2830 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2831
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002832 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833}
2834
2835static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2836{
2837 struct gsm48_hdr *gh = msgb_l3(msg);
2838 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2839 struct tlv_parsed tp;
2840 struct gsm_mncc modify;
2841
2842 gsm48_stop_cc_timer(trans);
2843
2844 memset(&modify, 0, sizeof(struct gsm_mncc));
2845 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002846 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* bearer capability */
2848 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2849 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002850 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002852 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 }
2854
2855 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2856
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002857 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858}
2859
2860static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2861{
2862 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002863 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2865
Harald Welte4bfdfe72009-06-10 23:11:52 +08002866 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2867
2868 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002869 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870
2871 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2872
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002873 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874}
2875
2876static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2877{
2878 struct gsm48_hdr *gh = msgb_l3(msg);
2879 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2880 struct tlv_parsed tp;
2881 struct gsm_mncc modify;
2882
2883 gsm48_stop_cc_timer(trans);
2884
2885 memset(&modify, 0, sizeof(struct gsm_mncc));
2886 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002887 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888 /* bearer capability */
2889 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2890 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002891 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002893 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 }
2895 /* cause */
2896 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2897 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002898 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2900 }
2901
2902 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2903
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002904 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905}
2906
2907static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2908{
2909 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002910 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2912
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2914
2915 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002916 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002918 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919
2920 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2921
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002922 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923}
2924
2925static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2926{
2927 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002928 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2930
Harald Welte4bfdfe72009-06-10 23:11:52 +08002931 gh->msg_type = GSM48_MT_CC_NOTIFY;
2932
2933 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002934 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002936 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937}
2938
2939static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2940{
2941 struct gsm48_hdr *gh = msgb_l3(msg);
2942 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2943// struct tlv_parsed tp;
2944 struct gsm_mncc notify;
2945
2946 memset(&notify, 0, sizeof(struct gsm_mncc));
2947 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002948// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002950 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002952 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953}
2954
2955static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2956{
2957 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002958 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2960
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 gh->msg_type = GSM48_MT_CC_USER_INFO;
2962
2963 /* user-user */
2964 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002965 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966 /* more data */
2967 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002968 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002970 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971}
2972
2973static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2974{
2975 struct gsm48_hdr *gh = msgb_l3(msg);
2976 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2977 struct tlv_parsed tp;
2978 struct gsm_mncc user;
2979
2980 memset(&user, 0, sizeof(struct gsm_mncc));
2981 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002982 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983 /* user-user */
2984 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2985 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002986 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2988 }
2989 /* more data */
2990 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2991 user.more = 1;
2992
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002993 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994}
2995
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002996static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997{
2998 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002999 struct gsm_lchan *lchan = trans->conn->lchan;
3000
3001 /*
3002 * We were forced to make an assignment a lot earlier and
3003 * we should avoid sending another assignment that might
3004 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3005 * In case of rtp-bridge it is too late to change things
3006 * here.
3007 */
3008 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3009 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003011 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3012 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003013}
3014
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003015static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3016 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3017 uint32_t payload_msg_type)
3018{
3019 uint8_t data[sizeof(struct gsm_mncc)];
3020 struct gsm_mncc_rtp *rtp;
3021
3022 memset(&data, 0, sizeof(data));
3023 rtp = (struct gsm_mncc_rtp *) &data[0];
3024
3025 rtp->callref = callref;
3026 rtp->msg_type = cmd;
3027 rtp->ip = addr;
3028 rtp->port = port;
3029 rtp->payload_type = payload_type;
3030 rtp->payload_msg_type = payload_msg_type;
3031 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3032}
3033
3034static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3035{
3036 struct gsm_lchan *lchan;
3037 int msg_type;
3038
3039 lchan = trans->conn->lchan;
3040 switch (lchan->abis_ip.rtp_payload) {
3041 case RTP_PT_GSM_FULL:
3042 msg_type = GSM_TCHF_FRAME;
3043 break;
3044 case RTP_PT_GSM_EFR:
3045 msg_type = GSM_TCHF_FRAME_EFR;
3046 break;
3047 case RTP_PT_GSM_HALF:
3048 msg_type = GSM_TCHH_FRAME;
3049 break;
3050 case RTP_PT_AMR:
3051 msg_type = GSM_TCH_FRAME_AMR;
3052 break;
3053 default:
3054 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3055 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3056 msg_type = 0;
3057 break;
3058 }
3059
3060 return mncc_recv_rtp(net, trans->callref, cmd,
3061 lchan->abis_ip.bound_ip,
3062 lchan->abis_ip.bound_port,
3063 lchan->abis_ip.rtp_payload,
3064 msg_type);
3065}
3066
3067static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3068{
3069 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3070}
3071
3072static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3073{
3074 struct gsm_bts *bts;
3075 struct gsm_lchan *lchan;
3076 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003077 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003078
3079 /* Find callref */
3080 trans = trans_find_by_callref(net, callref);
3081 if (!trans) {
3082 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3083 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3084 return -EIO;
3085 }
3086 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3087 if (!trans->conn) {
3088 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3089 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3090 return 0;
3091 }
3092
3093 lchan = trans->conn->lchan;
3094 bts = lchan->ts->trx->bts;
3095 if (!is_ipaccess_bts(bts)) {
3096 /*
3097 * I want this to be straight forward and have no audio flow
3098 * through the nitb/osmo-mss system. This currently means that
3099 * this will not work with BS11/Nokia type BTS. We would need
3100 * to have a trau<->rtp bridge for these but still preferable
3101 * in another process.
3102 */
3103 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3104 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3105 return -EINVAL;
3106 }
3107
3108 trans->conn->mncc_rtp_bridge = 1;
3109 /*
3110 * *sigh* we need to pick a codec now. Pick the most generic one
3111 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003112 * similiar to the routine above.
3113 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003114 */
3115 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3116 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003117 m = mncc_codec_for_mode(lchan->type);
3118 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3119 get_value_string(gsm48_chan_mode_names, m),
3120 get_value_string(gsm_chan_t_names, lchan->type));
3121 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003122 lchan->type != GSM_LCHAN_TCH_H);
3123 }
3124
3125 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3126 return 0;
3127}
3128
3129static int tch_rtp_connect(struct gsm_network *net, void *arg)
3130{
3131 struct gsm_lchan *lchan;
3132 struct gsm_trans *trans;
3133 struct gsm_mncc_rtp *rtp = arg;
3134
3135 /* Find callref */
3136 trans = trans_find_by_callref(net, rtp->callref);
3137 if (!trans) {
3138 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3139 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3140 return -EIO;
3141 }
3142 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3143 if (!trans->conn) {
3144 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3145 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3146 return 0;
3147 }
3148
3149 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003150 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3151 get_value_string(gsm48_chan_mode_names,
3152 mncc_codec_for_mode(lchan->type)),
3153 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003154
3155 /* TODO: Check if payload_msg_type is compatible with what we have */
3156 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3157 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3158 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3159 }
3160
3161 /*
3162 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3163 * complains about both rtp and rtp payload2 being present in the
3164 * same package!
3165 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003166 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003167 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3168}
3169
3170static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3171{
3172 struct gsm_network *net;
3173 struct gsm_trans *tmp, *trans = NULL;
3174
3175 net = lchan->ts->trx->bts->network;
3176 llist_for_each_entry(tmp, &net->trans_list, entry) {
3177 if (!tmp->conn)
3178 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003179 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003180 continue;
3181 trans = tmp;
3182 break;
3183 }
3184
3185 if (!trans) {
3186 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3187 gsm_lchan_name(lchan));
3188 return 0;
3189 }
3190
3191 switch (signal) {
3192 case S_ABISIP_CRCX_ACK:
3193 if (lchan->conn->mncc_rtp_create_pending) {
3194 lchan->conn->mncc_rtp_create_pending = 0;
3195 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3196 gsm_lchan_name(lchan));
3197 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3198 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003199 /*
3200 * TODO: this appears to be too early? Why not until after
3201 * the handover detect or the handover complete?
3202 */
3203 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003204 break;
3205 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003206 if (lchan->conn->mncc_rtp_connect_pending) {
3207 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003208 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3209 gsm_lchan_name(lchan));
3210 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3211 }
3212 break;
3213 }
3214
3215 return 0;
3216}
3217
3218
Harald Welte4bfdfe72009-06-10 23:11:52 +08003219static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003220 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003221 int type;
3222 int (*rout) (struct gsm_trans *trans, void *arg);
3223} downstatelist[] = {
3224 /* mobile originating call establishment */
3225 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3226 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3227 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3228 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3229 {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 */
3230 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3231 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3232 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3233 /* mobile terminating call establishment */
3234 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3235 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3236 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3237 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3238 /* signalling during call */
3239 {SBIT(GSM_CSTATE_ACTIVE),
3240 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3241 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3242 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3243 {ALL_STATES,
3244 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3245 {ALL_STATES,
3246 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3247 {ALL_STATES,
3248 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3249 {SBIT(GSM_CSTATE_ACTIVE),
3250 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3251 {SBIT(GSM_CSTATE_ACTIVE),
3252 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3253 {SBIT(GSM_CSTATE_ACTIVE),
3254 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3255 {SBIT(GSM_CSTATE_ACTIVE),
3256 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3257 {SBIT(GSM_CSTATE_ACTIVE),
3258 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3259 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3260 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3261 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3262 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3263 {SBIT(GSM_CSTATE_ACTIVE),
3264 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3265 /* clearing */
3266 {SBIT(GSM_CSTATE_INITIATED),
3267 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3268 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3269 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3270 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3271 MNCC_REL_REQ, gsm48_cc_tx_release},
3272 /* special */
3273 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003274 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275};
3276
3277#define DOWNSLLEN \
3278 (sizeof(downstatelist) / sizeof(struct downstate))
3279
3280
Harald Welte76556372010-12-22 23:57:45 +01003281int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003282{
Harald Welte1a6f7982009-08-09 18:52:33 +02003283 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003284 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003285 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003286 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 struct gsm_mncc *data = arg, rel;
3288
Harald Welte04dc88f2010-12-22 21:45:05 +01003289 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3290
Harald Welte4bfdfe72009-06-10 23:11:52 +08003291 /* handle special messages */
3292 switch(msg_type) {
3293 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003294 rc = tch_bridge(net, arg);
3295 if (rc < 0)
3296 disconnect_bridge(net, arg, -rc);
3297 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003298 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003299 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003300 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003301 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003302 case MNCC_RTP_CREATE:
3303 return tch_rtp_create(net, data->callref);
3304 case MNCC_RTP_CONNECT:
3305 return tch_rtp_connect(net, arg);
3306 case MNCC_RTP_FREE:
3307 /* unused right now */
3308 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003309 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003310 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003311 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003312 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003313 /* Find callref */
3314 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003315 if (!trans) {
3316 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003317 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003318 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003319 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003320 if (!trans->conn) {
3321 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003322 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003323 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003324 if (!trans->conn->lchan) {
3325 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3326 return 0;
3327 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003328 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3329 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003330 /* This should be LOGL_ERROR or NOTICE, but
3331 * unfortuantely it happens for a couple of frames at
3332 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003333 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003334 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003335 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003336 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003337 switch (bts->type) {
3338 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003339 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003340 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003341 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003342 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003343 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003344 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003345 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003346 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003347 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003348 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003349 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003350 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003351 }
3352 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003353 }
3354
3355 memset(&rel, 0, sizeof(struct gsm_mncc));
3356 rel.callref = data->callref;
3357
3358 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003359 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003360
3361 /* Callref unknown */
3362 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003363 struct gsm_subscriber *subscr;
3364
Harald Welte4a3464c2009-07-04 10:11:24 +02003365 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003366 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3367 "Received '%s' from MNCC with "
3368 "unknown callref %d\n", data->called.number,
3369 get_mncc_name(msg_type), data->callref);
3370 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003371 return mncc_release_ind(net, NULL, data->callref,
3372 GSM48_CAUSE_LOC_PRN_S_LU,
3373 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003374 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003375 if (!data->called.number[0] && !data->imsi[0]) {
3376 DEBUGP(DCC, "(bts - trx - ts - ti) "
3377 "Received '%s' from MNCC with "
3378 "no number or IMSI\n", get_mncc_name(msg_type));
3379 /* Invalid number */
3380 return mncc_release_ind(net, NULL, data->callref,
3381 GSM48_CAUSE_LOC_PRN_S_LU,
3382 GSM48_CC_CAUSE_INV_NR_FORMAT);
3383 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003384 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003385 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003386 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003387 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003388 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003389 subscr = subscr_get_by_imsi(net->subscr_group,
3390 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003391
3392 /* update the subscriber we deal with */
3393 log_set_context(BSC_CTX_SUBSCR, subscr);
3394
Harald Welte4bfdfe72009-06-10 23:11:52 +08003395 /* If subscriber is not found */
3396 if (!subscr) {
3397 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3398 "Received '%s' from MNCC with "
3399 "unknown subscriber %s\n", data->called.number,
3400 get_mncc_name(msg_type), data->called.number);
3401 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003402 return mncc_release_ind(net, NULL, data->callref,
3403 GSM48_CAUSE_LOC_PRN_S_LU,
3404 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003405 }
3406 /* If subscriber is not "attached" */
3407 if (!subscr->lac) {
3408 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3409 "Received '%s' from MNCC with "
3410 "detached subscriber %s\n", data->called.number,
3411 get_mncc_name(msg_type), data->called.number);
3412 subscr_put(subscr);
3413 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003414 return mncc_release_ind(net, NULL, data->callref,
3415 GSM48_CAUSE_LOC_PRN_S_LU,
3416 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003417 }
3418 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003419 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003420 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003421 DEBUGP(DCC, "No memory for trans.\n");
3422 subscr_put(subscr);
3423 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003424 mncc_release_ind(net, NULL, data->callref,
3425 GSM48_CAUSE_LOC_PRN_S_LU,
3426 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003427 return -ENOMEM;
3428 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003429 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003430 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003431
Harald Welte4bfdfe72009-06-10 23:11:52 +08003432 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003433 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003434 /* find transaction with this subscriber already paging */
3435 llist_for_each_entry(transt, &net->trans_list, entry) {
3436 /* Transaction of our lchan? */
3437 if (transt == trans ||
3438 transt->subscr != subscr)
3439 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003440 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003441 "Received '%s' from MNCC with "
3442 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003443 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003444 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003445 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003446 subscr_put(subscr);
3447 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003448 return 0;
3449 }
3450 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003451 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003452
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003453 /* Request a channel */
3454 trans->paging_request = subscr_request_channel(subscr,
3455 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3456 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003457 if (!trans->paging_request) {
3458 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3459 subscr_put(subscr);
3460 trans_free(trans);
3461 return 0;
3462 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003463 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003464 return 0;
3465 }
3466 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003467 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003468 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003469 } else {
3470 /* update the subscriber we deal with */
3471 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003473
3474 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003475 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476
3477 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003478 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003479 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003480 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003481 (trans->subscr)?(trans->subscr->extension):"-",
3482 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003483 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3484 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003485 if (msg_type == MNCC_REL_REQ)
3486 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3487 else
3488 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3489 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003490 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003491 return rc;
3492 }
3493
3494 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3495 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003496 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003497 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003498 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003499 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003500 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003501
3502 /* Find function for current state and message */
3503 for (i = 0; i < DOWNSLLEN; i++)
3504 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003505 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 break;
3507 if (i == DOWNSLLEN) {
3508 DEBUGP(DCC, "Message unhandled at this state.\n");
3509 return 0;
3510 }
3511
3512 rc = downstatelist[i].rout(trans, arg);
3513
3514 return rc;
3515}
3516
3517
3518static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003519 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003520 int type;
3521 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3522} datastatelist[] = {
3523 /* mobile originating call establishment */
3524 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3525 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3526 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3527 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3528 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3529 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3530 /* mobile terminating call establishment */
3531 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3532 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3533 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3534 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003535 {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 +08003536 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3537 /* signalling during call */
3538 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3539 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3540 {SBIT(GSM_CSTATE_ACTIVE),
3541 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3542 {ALL_STATES,
3543 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3544 {ALL_STATES,
3545 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3546 {ALL_STATES,
3547 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3548 {SBIT(GSM_CSTATE_ACTIVE),
3549 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3550 {SBIT(GSM_CSTATE_ACTIVE),
3551 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3552 {SBIT(GSM_CSTATE_ACTIVE),
3553 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3554 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3555 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3556 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3557 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3558 {SBIT(GSM_CSTATE_ACTIVE),
3559 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3560 /* clearing */
3561 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3562 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3563 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3564 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3565 {ALL_STATES, /* 5.4.3.4 */
3566 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3567};
3568
3569#define DATASLLEN \
3570 (sizeof(datastatelist) / sizeof(struct datastate))
3571
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003572static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003573{
3574 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003575 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003576 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003577 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003578 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003579
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580 if (msg_type & 0x80) {
3581 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3582 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003583 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003584
Harald Welte4bfdfe72009-06-10 23:11:52 +08003585 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003586 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003587
Harald Welte6f5aee02009-07-23 21:21:14 +02003588 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003589 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003590 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003591 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003592 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3593 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003594
3595 /* Create transaction */
3596 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003597 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003598 "creating new trans.\n", transaction_id);
3599 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003600 trans = trans_alloc(conn->bts->network, conn->subscr,
3601 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003602 transaction_id, new_callref++);
3603 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003604 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003605 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003606 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003607 GSM48_MT_CC_RELEASE_COMPL);
3608 return -ENOMEM;
3609 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003610 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003611 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003612 }
3613
3614 /* find function for current state and message */
3615 for (i = 0; i < DATASLLEN; i++)
3616 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003617 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003618 break;
3619 if (i == DATASLLEN) {
3620 DEBUGP(DCC, "Message unhandled at this state.\n");
3621 return 0;
3622 }
3623
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003624 assert(trans->subscr);
3625
Harald Welte4bfdfe72009-06-10 23:11:52 +08003626 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003627
3628 return rc;
3629}
3630
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003631/* Create a dummy to wait five seconds */
3632static void release_anchor(struct gsm_subscriber_connection *conn)
3633{
3634 if (!conn->anch_operation)
3635 return;
3636
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003637 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003638 talloc_free(conn->anch_operation);
3639 conn->anch_operation = NULL;
3640}
3641
3642static void anchor_timeout(void *_data)
3643{
3644 struct gsm_subscriber_connection *con = _data;
3645
3646 release_anchor(con);
3647 msc_release_connection(con);
3648}
3649
3650int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3651{
3652 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3653 if (!conn->anch_operation)
3654 return -1;
3655
3656 conn->anch_operation->timeout.data = conn;
3657 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003658 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003659 return 0;
3660}
3661
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003662/* here we get data from the BSC level... */
3663int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003664{
3665 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003666 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003667 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003668
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003669 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003670 if (silent_call_reroute(conn, msg))
3671 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003672
3673 switch (pdisc) {
3674 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003675 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003676 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003677 break;
3678 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003679 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003680 break;
3681 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003682 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003683 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003684 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003685 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003686 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003687 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003688 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003689 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003690 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3691 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003692 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003693 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003694 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003695 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003696 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003697 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003698 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3699 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003700 break;
3701 }
3702
3703 return rc;
3704}
Harald Welte8470bf22008-12-25 23:28:35 +00003705
Harald Welte805f6442009-07-28 18:25:29 +02003706/*
3707 * This will be ran by the linker when loading the DSO. We use it to
3708 * do system initialization, e.g. registration of signal handlers.
3709 */
3710static __attribute__((constructor)) void on_dso_load_0408(void)
3711{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003712 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003713}